blob: 51a4f58a4d81774709cfc6034ce66b8bf89bd8aa [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* auditsc.c -- System-call auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handles all system-call specific auditing features.
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
5 * All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
22 *
23 * Many of the ideas implemented here are from Stephen C. Tweedie,
24 * especially the idea of avoiding a copy by using getname.
25 *
26 * The method for actual interception of syscall entry and exit (not in
27 * this file -- see entry.S) is based on a GPL'd patch written by
28 * okir@suse.de and Copyright 2003 SuSE Linux AG.
29 *
30 */
31
32#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/types.h>
Alan Cox715b49e2006-01-18 17:44:07 -080034#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/mm.h>
36#include <linux/module.h>
Stephen Smalley01116102005-05-21 00:15:52 +010037#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010038#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/audit.h>
40#include <linux/personality.h>
41#include <linux/time.h>
David Woodhousef6a789d2005-06-21 16:22:01 +010042#include <linux/kthread.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010043#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010044#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/unistd.h>
46
47/* 0 = no checking
48 1 = put_count checking
49 2 = verbose put_count checking
50*/
51#define AUDIT_DEBUG 0
52
53/* No syscall auditing will take place unless audit_enabled != 0. */
54extern int audit_enabled;
55
56/* AUDIT_NAMES is the number of slots we reserve in the audit_context
57 * for saving names from getname(). */
58#define AUDIT_NAMES 20
59
60/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
61 * audit_context from being used for nameless inodes from
62 * path_lookup. */
63#define AUDIT_NAMES_RESERVED 7
64
65/* At task start time, the audit_state is set in the audit_context using
66 a per-task filter. At syscall entry, the audit_state is augmented by
67 the syscall filter. */
68enum audit_state {
69 AUDIT_DISABLED, /* Do not create per-task audit_context.
70 * No syscall-specific audit records can
71 * be generated. */
72 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
73 * but don't necessarily fill it in at
74 * syscall entry time (i.e., filter
75 * instead). */
76 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
77 * and always fill it in at syscall
78 * entry time. This makes a full
79 * syscall record available if some
80 * other part of the kernel decides it
81 * should be recorded. */
82 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
83 * always fill it in at syscall entry
84 * time, and always write out the audit
85 * record at syscall exit time. */
86};
87
88/* When fs/namei.c:getname() is called, we store the pointer in name and
89 * we don't let putname() free it (instead we free all of the saved
90 * pointers at syscall exit time).
91 *
92 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
93struct audit_names {
94 const char *name;
95 unsigned long ino;
96 dev_t dev;
97 umode_t mode;
98 uid_t uid;
99 gid_t gid;
100 dev_t rdev;
David Woodhouseae7b9612005-06-20 16:11:05 +0100101 unsigned flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104struct audit_aux_data {
105 struct audit_aux_data *next;
106 int type;
107};
108
109#define AUDIT_AUX_IPCPERM 0
110
111struct audit_aux_data_ipcctl {
112 struct audit_aux_data d;
113 struct ipc_perm p;
114 unsigned long qbytes;
115 uid_t uid;
116 gid_t gid;
117 mode_t mode;
118};
119
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100120struct audit_aux_data_socketcall {
121 struct audit_aux_data d;
122 int nargs;
123 unsigned long args[0];
124};
125
126struct audit_aux_data_sockaddr {
127 struct audit_aux_data d;
128 int len;
129 char a[0];
130};
131
Stephen Smalley01116102005-05-21 00:15:52 +0100132struct audit_aux_data_path {
133 struct audit_aux_data d;
134 struct dentry *dentry;
135 struct vfsmount *mnt;
136};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/* The per-task audit context. */
139struct audit_context {
140 int in_syscall; /* 1 if task is in a syscall */
141 enum audit_state state;
142 unsigned int serial; /* serial number for record */
143 struct timespec ctime; /* time of syscall entry */
144 uid_t loginuid; /* login uid (identity) */
145 int major; /* syscall number */
146 unsigned long argv[4]; /* syscall arguments */
147 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100148 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int auditable; /* 1 if record should be written */
150 int name_count;
151 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100152 struct dentry * pwd;
153 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct audit_context *previous; /* For nested syscalls */
155 struct audit_aux_data *aux;
156
157 /* Save things to print about task_struct */
158 pid_t pid;
159 uid_t uid, euid, suid, fsuid;
160 gid_t gid, egid, sgid, fsgid;
161 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100162 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164#if AUDIT_DEBUG
165 int put_count;
166 int ino_count;
167#endif
168};
169
170 /* Public API */
171/* There are three lists of rules -- one to search at task creation
172 * time, one to search at syscall entry time, and another to search at
173 * syscall exit time. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100174static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
175 LIST_HEAD_INIT(audit_filter_list[0]),
176 LIST_HEAD_INIT(audit_filter_list[1]),
177 LIST_HEAD_INIT(audit_filter_list[2]),
178 LIST_HEAD_INIT(audit_filter_list[3]),
179 LIST_HEAD_INIT(audit_filter_list[4]),
180#if AUDIT_NR_FILTERS != 5
181#error Fix audit_filter_list initialiser
182#endif
183};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185struct audit_entry {
186 struct list_head list;
187 struct rcu_head rcu;
188 struct audit_rule rule;
189};
190
David Woodhouse7ca00262005-05-19 11:23:13 +0100191extern int audit_pid;
192
Amy Griffis3c789a12005-08-17 16:05:35 +0100193/* Copy rule from user-space to kernel-space. Called from
194 * audit_add_rule during AUDIT_ADD. */
195static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
196{
197 int i;
198
199 if (s->action != AUDIT_NEVER
200 && s->action != AUDIT_POSSIBLE
201 && s->action != AUDIT_ALWAYS)
202 return -1;
203 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
204 return -1;
205 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
206 return -1;
207
208 d->flags = s->flags;
209 d->action = s->action;
210 d->field_count = s->field_count;
211 for (i = 0; i < d->field_count; i++) {
212 d->fields[i] = s->fields[i];
213 d->values[i] = s->values[i];
214 }
215 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
216 return 0;
217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/* Check to see if two rules are identical. It is called from
Amy Griffis3c789a12005-08-17 16:05:35 +0100220 * audit_add_rule during AUDIT_ADD and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * audit_del_rule during AUDIT_DEL. */
Amy Griffis3c789a12005-08-17 16:05:35 +0100222static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 int i;
225
226 if (a->flags != b->flags)
227 return 1;
228
229 if (a->action != b->action)
230 return 1;
231
232 if (a->field_count != b->field_count)
233 return 1;
234
235 for (i = 0; i < a->field_count; i++) {
236 if (a->fields[i] != b->fields[i]
237 || a->values[i] != b->values[i])
238 return 1;
239 }
240
241 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
242 if (a->mask[i] != b->mask[i])
243 return 1;
244
245 return 0;
246}
247
248/* Note that audit_add_rule and audit_del_rule are called via
249 * audit_receive() in audit.c, and are protected by
250 * audit_netlink_sem. */
Amy Griffis3c789a12005-08-17 16:05:35 +0100251static inline int audit_add_rule(struct audit_rule *rule,
David Woodhouse0f45aa12005-06-19 19:35:50 +0100252 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Amy Griffis3c789a12005-08-17 16:05:35 +0100254 struct audit_entry *entry;
255
256 /* Do not use the _rcu iterator here, since this is the only
257 * addition routine. */
258 list_for_each_entry(entry, list, list) {
259 if (!audit_compare_rule(rule, &entry->rule)) {
260 return -EEXIST;
261 }
262 }
263
264 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
265 return -ENOMEM;
266 if (audit_copy_rule(&entry->rule, rule)) {
267 kfree(entry);
268 return -EINVAL;
269 }
270
David Woodhouse0f45aa12005-06-19 19:35:50 +0100271 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
272 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 list_add_rcu(&entry->list, list);
274 } else {
275 list_add_tail_rcu(&entry->list, list);
276 }
Amy Griffis3c789a12005-08-17 16:05:35 +0100277
278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Amy Griffis3c789a12005-08-17 16:05:35 +0100281static inline void audit_free_rule(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
284 kfree(e);
285}
286
287/* Note that audit_add_rule and audit_del_rule are called via
288 * audit_receive() in audit.c, and are protected by
289 * audit_netlink_sem. */
290static inline int audit_del_rule(struct audit_rule *rule,
291 struct list_head *list)
292{
293 struct audit_entry *e;
294
295 /* Do not use the _rcu iterator here, since this is the only
296 * deletion routine. */
297 list_for_each_entry(e, list, list) {
298 if (!audit_compare_rule(rule, &e->rule)) {
299 list_del_rcu(&e->list);
300 call_rcu(&e->rcu, audit_free_rule);
301 return 0;
302 }
303 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100304 return -ENOENT; /* No matching rule */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
David Woodhousef6a789d2005-06-21 16:22:01 +0100307static int audit_list_rules(void *_dest)
308{
309 int pid, seq;
310 int *dest = _dest;
311 struct audit_entry *entry;
312 int i;
313
314 pid = dest[0];
315 seq = dest[1];
316 kfree(dest);
317
318 down(&audit_netlink_sem);
319
320 /* The *_rcu iterators not needed here because we are
321 always called with audit_netlink_sem held. */
322 for (i=0; i<AUDIT_NR_FILTERS; i++) {
323 list_for_each_entry(entry, &audit_filter_list[i], list)
324 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
325 &entry->rule, sizeof(entry->rule));
326 }
327 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
328
329 up(&audit_netlink_sem);
330 return 0;
331}
332
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700333/**
334 * audit_receive_filter - apply all rules to the specified message type
335 * @type: audit message type
336 * @pid: target pid for netlink audit messages
337 * @uid: target uid for netlink audit messages
338 * @seq: netlink audit message sequence (serial) number
339 * @data: payload data
340 * @loginuid: loginuid of sender
341 */
Serge Hallync94c2572005-04-29 16:27:17 +0100342int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
343 uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
David Woodhousef6a789d2005-06-21 16:22:01 +0100345 struct task_struct *tsk;
346 int *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int err = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100348 unsigned listnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 switch (type) {
351 case AUDIT_LIST:
David Woodhousef6a789d2005-06-21 16:22:01 +0100352 /* We can't just spew out the rules here because we might fill
353 * the available socket buffer space and deadlock waiting for
354 * auditctl to read from it... which isn't ever going to
355 * happen if we're actually running in the context of auditctl
356 * trying to _send_ the stuff */
357
358 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
359 if (!dest)
360 return -ENOMEM;
361 dest[0] = pid;
362 dest[1] = seq;
363
364 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
365 if (IS_ERR(tsk)) {
366 kfree(dest);
367 err = PTR_ERR(tsk);
David Woodhouse0f45aa12005-06-19 19:35:50 +0100368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 break;
370 case AUDIT_ADD:
Amy Griffis3c789a12005-08-17 16:05:35 +0100371 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
372 if (listnr >= AUDIT_NR_FILTERS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -EINVAL;
Amy Griffis3c789a12005-08-17 16:05:35 +0100374
375 err = audit_add_rule(data, &audit_filter_list[listnr]);
376 if (!err)
377 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
378 "auid=%u added an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380 case AUDIT_DEL:
David Woodhouse0f45aa12005-06-19 19:35:50 +0100381 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
382 if (listnr >= AUDIT_NR_FILTERS)
383 return -EINVAL;
384
385 err = audit_del_rule(data, &audit_filter_list[listnr]);
386 if (!err)
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100387 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhouse0f45aa12005-06-19 19:35:50 +0100388 "auid=%u removed an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 break;
390 default:
391 return -EINVAL;
392 }
393
394 return err;
395}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/* Compare a task_struct with an audit_rule. Return 1 on match, 0
398 * otherwise. */
399static int audit_filter_rules(struct task_struct *tsk,
400 struct audit_rule *rule,
401 struct audit_context *ctx,
402 enum audit_state *state)
403{
404 int i, j;
405
406 for (i = 0; i < rule->field_count; i++) {
407 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
408 u32 value = rule->values[i];
409 int result = 0;
410
411 switch (field) {
412 case AUDIT_PID:
413 result = (tsk->pid == value);
414 break;
415 case AUDIT_UID:
416 result = (tsk->uid == value);
417 break;
418 case AUDIT_EUID:
419 result = (tsk->euid == value);
420 break;
421 case AUDIT_SUID:
422 result = (tsk->suid == value);
423 break;
424 case AUDIT_FSUID:
425 result = (tsk->fsuid == value);
426 break;
427 case AUDIT_GID:
428 result = (tsk->gid == value);
429 break;
430 case AUDIT_EGID:
431 result = (tsk->egid == value);
432 break;
433 case AUDIT_SGID:
434 result = (tsk->sgid == value);
435 break;
436 case AUDIT_FSGID:
437 result = (tsk->fsgid == value);
438 break;
439 case AUDIT_PERS:
440 result = (tsk->personality == value);
441 break;
2fd6f582005-04-29 16:08:28 +0100442 case AUDIT_ARCH:
443 if (ctx)
444 result = (ctx->arch == value);
445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 case AUDIT_EXIT:
448 if (ctx && ctx->return_valid)
449 result = (ctx->return_code == value);
450 break;
451 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100452 if (ctx && ctx->return_valid) {
453 if (value)
454 result = (ctx->return_valid == AUDITSC_SUCCESS);
455 else
456 result = (ctx->return_valid == AUDITSC_FAILURE);
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
459 case AUDIT_DEVMAJOR:
460 if (ctx) {
461 for (j = 0; j < ctx->name_count; j++) {
462 if (MAJOR(ctx->names[j].dev)==value) {
463 ++result;
464 break;
465 }
466 }
467 }
468 break;
469 case AUDIT_DEVMINOR:
470 if (ctx) {
471 for (j = 0; j < ctx->name_count; j++) {
472 if (MINOR(ctx->names[j].dev)==value) {
473 ++result;
474 break;
475 }
476 }
477 }
478 break;
479 case AUDIT_INODE:
480 if (ctx) {
481 for (j = 0; j < ctx->name_count; j++) {
482 if (ctx->names[j].ino == value) {
483 ++result;
484 break;
485 }
486 }
487 }
488 break;
489 case AUDIT_LOGINUID:
490 result = 0;
491 if (ctx)
492 result = (ctx->loginuid == value);
493 break;
494 case AUDIT_ARG0:
495 case AUDIT_ARG1:
496 case AUDIT_ARG2:
497 case AUDIT_ARG3:
498 if (ctx)
499 result = (ctx->argv[field-AUDIT_ARG0]==value);
500 break;
501 }
502
503 if (rule->fields[i] & AUDIT_NEGATE)
504 result = !result;
505 if (!result)
506 return 0;
507 }
508 switch (rule->action) {
509 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
510 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
511 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
512 }
513 return 1;
514}
515
516/* At process creation time, we can determine if system-call auditing is
517 * completely disabled for this task. Since we only have the task
518 * structure at this point, we can only check uid and gid.
519 */
520static enum audit_state audit_filter_task(struct task_struct *tsk)
521{
522 struct audit_entry *e;
523 enum audit_state state;
524
525 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100526 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
528 rcu_read_unlock();
529 return state;
530 }
531 }
532 rcu_read_unlock();
533 return AUDIT_BUILD_CONTEXT;
534}
535
536/* At syscall entry and exit time, this filter is called if the
537 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100538 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700539 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
541static enum audit_state audit_filter_syscall(struct task_struct *tsk,
542 struct audit_context *ctx,
543 struct list_head *list)
544{
545 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100546 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
David Woodhouse351bb722005-07-14 14:40:06 +0100548 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100549 return AUDIT_DISABLED;
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100552 if (!list_empty(list)) {
553 int word = AUDIT_WORD(ctx->major);
554 int bit = AUDIT_BIT(ctx->major);
555
556 list_for_each_entry_rcu(e, list, list) {
557 if ((e->rule.mask[word] & bit) == bit
558 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
559 rcu_read_unlock();
560 return state;
561 }
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 rcu_read_unlock();
565 return AUDIT_BUILD_CONTEXT;
566}
567
David Woodhouse5bb289b2005-06-24 14:14:05 +0100568static int audit_filter_user_rules(struct netlink_skb_parms *cb,
569 struct audit_rule *rule,
570 enum audit_state *state)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100571{
David Woodhouse5bb289b2005-06-24 14:14:05 +0100572 int i;
573
574 for (i = 0; i < rule->field_count; i++) {
575 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
576 u32 value = rule->values[i];
577 int result = 0;
578
579 switch (field) {
580 case AUDIT_PID:
581 result = (cb->creds.pid == value);
582 break;
583 case AUDIT_UID:
584 result = (cb->creds.uid == value);
585 break;
586 case AUDIT_GID:
587 result = (cb->creds.gid == value);
588 break;
589 case AUDIT_LOGINUID:
590 result = (cb->loginuid == value);
591 break;
592 }
593
594 if (rule->fields[i] & AUDIT_NEGATE)
595 result = !result;
596 if (!result)
597 return 0;
598 }
599 switch (rule->action) {
600 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
601 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
602 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
603 }
604 return 1;
605}
606
607int audit_filter_user(struct netlink_skb_parms *cb, int type)
608{
David Woodhouse0f45aa12005-06-19 19:35:50 +0100609 struct audit_entry *e;
610 enum audit_state state;
David Woodhouse4a4cd632005-06-22 14:56:47 +0100611 int ret = 1;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100612
613 rcu_read_lock();
614 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
David Woodhouse5bb289b2005-06-24 14:14:05 +0100615 if (audit_filter_user_rules(cb, &e->rule, &state)) {
David Woodhouse4a4cd632005-06-22 14:56:47 +0100616 if (state == AUDIT_DISABLED)
617 ret = 0;
618 break;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100619 }
620 }
621 rcu_read_unlock();
David Woodhouse4a4cd632005-06-22 14:56:47 +0100622
David Woodhouse993e2d412005-06-24 08:21:49 +0100623 return ret; /* Audit by default */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/* This should be called with task_lock() held. */
627static inline struct audit_context *audit_get_context(struct task_struct *tsk,
628 int return_valid,
629 int return_code)
630{
631 struct audit_context *context = tsk->audit_context;
632
633 if (likely(!context))
634 return NULL;
635 context->return_valid = return_valid;
636 context->return_code = return_code;
637
David Woodhouse21af6c42005-07-02 14:10:46 +0100638 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100640 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (state == AUDIT_RECORD_CONTEXT)
642 context->auditable = 1;
643 }
644
645 context->pid = tsk->pid;
646 context->uid = tsk->uid;
647 context->gid = tsk->gid;
648 context->euid = tsk->euid;
649 context->suid = tsk->suid;
650 context->fsuid = tsk->fsuid;
651 context->egid = tsk->egid;
652 context->sgid = tsk->sgid;
653 context->fsgid = tsk->fsgid;
654 context->personality = tsk->personality;
655 tsk->audit_context = NULL;
656 return context;
657}
658
659static inline void audit_free_names(struct audit_context *context)
660{
661 int i;
662
663#if AUDIT_DEBUG == 2
664 if (context->auditable
665 ||context->put_count + context->ino_count != context->name_count) {
666 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
667 " name_count=%d put_count=%d"
668 " ino_count=%d [NOT freeing]\n",
669 __LINE__,
670 context->serial, context->major, context->in_syscall,
671 context->name_count, context->put_count,
672 context->ino_count);
673 for (i = 0; i < context->name_count; i++)
674 printk(KERN_ERR "names[%d] = %p = %s\n", i,
675 context->names[i].name,
676 context->names[i].name);
677 dump_stack();
678 return;
679 }
680#endif
681#if AUDIT_DEBUG
682 context->put_count = 0;
683 context->ino_count = 0;
684#endif
685
686 for (i = 0; i < context->name_count; i++)
687 if (context->names[i].name)
688 __putname(context->names[i].name);
689 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100690 if (context->pwd)
691 dput(context->pwd);
692 if (context->pwdmnt)
693 mntput(context->pwdmnt);
694 context->pwd = NULL;
695 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
698static inline void audit_free_aux(struct audit_context *context)
699{
700 struct audit_aux_data *aux;
701
702 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100703 if (aux->type == AUDIT_AVC_PATH) {
704 struct audit_aux_data_path *axi = (void *)aux;
705 dput(axi->dentry);
706 mntput(axi->mnt);
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 context->aux = aux->next;
709 kfree(aux);
710 }
711}
712
713static inline void audit_zero_context(struct audit_context *context,
714 enum audit_state state)
715{
716 uid_t loginuid = context->loginuid;
717
718 memset(context, 0, sizeof(*context));
719 context->state = state;
720 context->loginuid = loginuid;
721}
722
723static inline struct audit_context *audit_alloc_context(enum audit_state state)
724{
725 struct audit_context *context;
726
727 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
728 return NULL;
729 audit_zero_context(context, state);
730 return context;
731}
732
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700733/**
734 * audit_alloc - allocate an audit context block for a task
735 * @tsk: task
736 *
737 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * if necessary. Doing so turns on system call auditing for the
739 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700740 * needed.
741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742int audit_alloc(struct task_struct *tsk)
743{
744 struct audit_context *context;
745 enum audit_state state;
746
747 if (likely(!audit_enabled))
748 return 0; /* Return if not auditing. */
749
750 state = audit_filter_task(tsk);
751 if (likely(state == AUDIT_DISABLED))
752 return 0;
753
754 if (!(context = audit_alloc_context(state))) {
755 audit_log_lost("out of memory in audit_alloc");
756 return -ENOMEM;
757 }
758
759 /* Preserve login uid */
760 context->loginuid = -1;
761 if (current->audit_context)
762 context->loginuid = current->audit_context->loginuid;
763
764 tsk->audit_context = context;
765 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
766 return 0;
767}
768
769static inline void audit_free_context(struct audit_context *context)
770{
771 struct audit_context *previous;
772 int count = 0;
773
774 do {
775 previous = context->previous;
776 if (previous || (count && count < 10)) {
777 ++count;
778 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
779 " freeing multiple contexts (%d)\n",
780 context->serial, context->major,
781 context->name_count, count);
782 }
783 audit_free_names(context);
784 audit_free_aux(context);
785 kfree(context);
786 context = previous;
787 } while (context);
788 if (count >= 10)
789 printk(KERN_ERR "audit: freed %d contexts\n", count);
790}
791
Stephen Smalley219f0812005-04-18 10:47:35 -0700792static void audit_log_task_info(struct audit_buffer *ab)
793{
794 char name[sizeof(current->comm)];
795 struct mm_struct *mm = current->mm;
796 struct vm_area_struct *vma;
797
798 get_task_comm(name, current);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100799 audit_log_format(ab, " comm=");
800 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700801
802 if (!mm)
803 return;
804
805 down_read(&mm->mmap_sem);
806 vma = mm->mmap;
807 while (vma) {
808 if ((vma->vm_flags & VM_EXECUTABLE) &&
809 vma->vm_file) {
810 audit_log_d_path(ab, "exe=",
811 vma->vm_file->f_dentry,
812 vma->vm_file->f_vfsmnt);
813 break;
814 }
815 vma = vma->vm_next;
816 }
817 up_read(&mm->mmap_sem);
818}
819
Al Viro9796fdd2005-10-21 03:22:03 -0400820static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 int i;
823 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100824 struct audit_aux_data *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
David Woodhousef5561962005-07-13 22:47:07 +0100826 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (!ab)
828 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100829 audit_log_format(ab, "arch=%x syscall=%d",
830 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (context->personality != PER_LINUX)
832 audit_log_format(ab, " per=%lx", context->personality);
833 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100834 audit_log_format(ab, " success=%s exit=%ld",
835 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
836 context->return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 audit_log_format(ab,
838 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100839 " pid=%d auid=%u uid=%u gid=%u"
840 " euid=%u suid=%u fsuid=%u"
841 " egid=%u sgid=%u fsgid=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 context->argv[0],
843 context->argv[1],
844 context->argv[2],
845 context->argv[3],
846 context->name_count,
847 context->pid,
848 context->loginuid,
849 context->uid,
850 context->gid,
851 context->euid, context->suid, context->fsuid,
852 context->egid, context->sgid, context->fsgid);
Stephen Smalley219f0812005-04-18 10:47:35 -0700853 audit_log_task_info(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
David Woodhouse7551ced2005-05-26 12:04:57 +0100856 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100857
Al Viroef20c8c2006-02-18 15:41:50 -0500858 ab = audit_log_start(context, gfp_mask, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!ab)
860 continue; /* audit_panic has been called */
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100863 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct audit_aux_data_ipcctl *axi = (void *)aux;
865 audit_log_format(ab,
Steve Grubb326e9c82005-05-21 00:22:31 +0100866 " qbytes=%lx iuid=%u igid=%u mode=%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 axi->qbytes, axi->uid, axi->gid, axi->mode);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100868 break; }
869
870 case AUDIT_SOCKETCALL: {
871 int i;
872 struct audit_aux_data_socketcall *axs = (void *)aux;
873 audit_log_format(ab, "nargs=%d", axs->nargs);
874 for (i=0; i<axs->nargs; i++)
875 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
876 break; }
877
878 case AUDIT_SOCKADDR: {
879 struct audit_aux_data_sockaddr *axs = (void *)aux;
880
881 audit_log_format(ab, "saddr=");
882 audit_log_hex(ab, axs->a, axs->len);
883 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100884
885 case AUDIT_AVC_PATH: {
886 struct audit_aux_data_path *axi = (void *)aux;
887 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100888 break; }
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
David Woodhouse8f37d472005-05-27 12:17:28 +0100894 if (context->pwd && context->pwdmnt) {
Al Viroef20c8c2006-02-18 15:41:50 -0500895 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100896 if (ab) {
897 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
898 audit_log_end(ab);
899 }
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 for (i = 0; i < context->name_count; i++) {
Al Viroef20c8c2006-02-18 15:41:50 -0500902 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (!ab)
904 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 audit_log_format(ab, "item=%d", i);
83c7d092005-04-29 15:54:44 +0100907 if (context->names[i].name) {
908 audit_log_format(ab, " name=");
909 audit_log_untrustedstring(ab, context->names[i].name);
910 }
David Woodhouseae7b9612005-06-20 16:11:05 +0100911 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (context->names[i].ino != (unsigned long)-1)
914 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
Steve Grubb326e9c82005-05-21 00:22:31 +0100915 " ouid=%u ogid=%u rdev=%02x:%02x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 context->names[i].ino,
917 MAJOR(context->names[i].dev),
918 MINOR(context->names[i].dev),
919 context->names[i].mode,
920 context->names[i].uid,
921 context->names[i].gid,
922 MAJOR(context->names[i].rdev),
923 MINOR(context->names[i].rdev));
924 audit_log_end(ab);
925 }
926}
927
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700928/**
929 * audit_free - free a per-task audit context
930 * @tsk: task whose audit context block to free
931 *
932 * Called from copy_process and __put_task_struct.
933 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934void audit_free(struct task_struct *tsk)
935{
936 struct audit_context *context;
937
938 task_lock(tsk);
939 context = audit_get_context(tsk, 0, 0);
940 task_unlock(tsk);
941
942 if (likely(!context))
943 return;
944
945 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100946 * function (e.g., exit_group), then free context block.
947 * We use GFP_ATOMIC here because we might be doing this
948 * in the context of the idle thread */
David Woodhousef7056d62005-06-20 16:07:33 +0100949 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +0100950 audit_log_exit(context, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 audit_free_context(context);
953}
954
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700955/**
956 * audit_syscall_entry - fill in an audit record at syscall entry
957 * @tsk: task being audited
958 * @arch: architecture type
959 * @major: major syscall type (function)
960 * @a1: additional syscall register 1
961 * @a2: additional syscall register 2
962 * @a3: additional syscall register 3
963 * @a4: additional syscall register 4
964 *
965 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * audit context was created when the task was created and the state or
967 * filters demand the audit context be built. If the state from the
968 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
969 * then the record will be written at syscall exit time (otherwise, it
970 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700971 * be written).
972 */
2fd6f582005-04-29 16:08:28 +0100973void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned long a1, unsigned long a2,
975 unsigned long a3, unsigned long a4)
976{
977 struct audit_context *context = tsk->audit_context;
978 enum audit_state state;
979
980 BUG_ON(!context);
981
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700982 /*
983 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * calls in kernel_thread via the entry.S interface, instead of
985 * with direct calls. (If you are porting to a new
986 * architecture, hitting this condition can indicate that you
987 * got the _exit/_leave calls backward in entry.S.)
988 *
989 * i386 no
990 * x86_64 no
991 * ppc64 yes (see arch/ppc64/kernel/misc.S)
992 *
993 * This also happens with vm86 emulation in a non-nested manner
994 * (entries without exits), so this case must be caught.
995 */
996 if (context->in_syscall) {
997 struct audit_context *newctx;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999#if AUDIT_DEBUG
1000 printk(KERN_ERR
1001 "audit(:%d) pid=%d in syscall=%d;"
1002 " entering syscall=%d\n",
1003 context->serial, tsk->pid, context->major, major);
1004#endif
1005 newctx = audit_alloc_context(context->state);
1006 if (newctx) {
1007 newctx->previous = context;
1008 context = newctx;
1009 tsk->audit_context = newctx;
1010 } else {
1011 /* If we can't alloc a new context, the best we
1012 * can do is to leak memory (any pending putname
1013 * will be lost). The only other alternative is
1014 * to abandon auditing. */
1015 audit_zero_context(context, context->state);
1016 }
1017 }
1018 BUG_ON(context->in_syscall || context->name_count);
1019
1020 if (!audit_enabled)
1021 return;
1022
2fd6f582005-04-29 16:08:28 +01001023 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 context->major = major;
1025 context->argv[0] = a1;
1026 context->argv[1] = a2;
1027 context->argv[2] = a3;
1028 context->argv[3] = a4;
1029
1030 state = context->state;
1031 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +01001032 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (likely(state == AUDIT_DISABLED))
1034 return;
1035
David Woodhousece625a82005-07-18 14:24:46 -04001036 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 context->ctime = CURRENT_TIME;
1038 context->in_syscall = 1;
1039 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1040}
1041
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001042/**
1043 * audit_syscall_exit - deallocate audit context after a system call
1044 * @tsk: task being audited
1045 * @valid: success/failure flag
1046 * @return_code: syscall return value
1047 *
1048 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1050 * filtering, or because some other part of the kernel write an audit
1051 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001052 * free the names stored from getname().
1053 */
2fd6f582005-04-29 16:08:28 +01001054void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct audit_context *context;
1057
1058 get_task_struct(tsk);
1059 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +01001060 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 task_unlock(tsk);
1062
1063 /* Not having a context here is ok, since the parent may have
1064 * called __put_task_struct. */
1065 if (likely(!context))
David Woodhouse413a1c72005-08-17 14:45:55 +01001066 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
David Woodhousef7056d62005-06-20 16:07:33 +01001068 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +01001069 audit_log_exit(context, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 context->in_syscall = 0;
1072 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (context->previous) {
1075 struct audit_context *new_context = context->previous;
1076 context->previous = NULL;
1077 audit_free_context(context);
1078 tsk->audit_context = new_context;
1079 } else {
1080 audit_free_names(context);
1081 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 tsk->audit_context = context;
1083 }
David Woodhouse413a1c72005-08-17 14:45:55 +01001084 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 put_task_struct(tsk);
1086}
1087
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001088/**
1089 * audit_getname - add a name to the list
1090 * @name: name to add
1091 *
1092 * Add a name to the list of audit names for this context.
1093 * Called from fs/namei.c:getname().
1094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095void audit_getname(const char *name)
1096{
1097 struct audit_context *context = current->audit_context;
1098
1099 if (!context || IS_ERR(name) || !name)
1100 return;
1101
1102 if (!context->in_syscall) {
1103#if AUDIT_DEBUG == 2
1104 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1105 __FILE__, __LINE__, context->serial, name);
1106 dump_stack();
1107#endif
1108 return;
1109 }
1110 BUG_ON(context->name_count >= AUDIT_NAMES);
1111 context->names[context->name_count].name = name;
1112 context->names[context->name_count].ino = (unsigned long)-1;
1113 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001114 if (!context->pwd) {
1115 read_lock(&current->fs->lock);
1116 context->pwd = dget(current->fs->pwd);
1117 context->pwdmnt = mntget(current->fs->pwdmnt);
1118 read_unlock(&current->fs->lock);
1119 }
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001123/* audit_putname - intercept a putname request
1124 * @name: name to intercept and delay for putname
1125 *
1126 * If we have stored the name from getname in the audit context,
1127 * then we delay the putname until syscall exit.
1128 * Called from include/linux/fs.h:putname().
1129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130void audit_putname(const char *name)
1131{
1132 struct audit_context *context = current->audit_context;
1133
1134 BUG_ON(!context);
1135 if (!context->in_syscall) {
1136#if AUDIT_DEBUG == 2
1137 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1138 __FILE__, __LINE__, context->serial, name);
1139 if (context->name_count) {
1140 int i;
1141 for (i = 0; i < context->name_count; i++)
1142 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1143 context->names[i].name,
1144 context->names[i].name);
1145 }
1146#endif
1147 __putname(name);
1148 }
1149#if AUDIT_DEBUG
1150 else {
1151 ++context->put_count;
1152 if (context->put_count > context->name_count) {
1153 printk(KERN_ERR "%s:%d(:%d): major=%d"
1154 " in_syscall=%d putname(%p) name_count=%d"
1155 " put_count=%d\n",
1156 __FILE__, __LINE__,
1157 context->serial, context->major,
1158 context->in_syscall, name, context->name_count,
1159 context->put_count);
1160 dump_stack();
1161 }
1162 }
1163#endif
1164}
1165
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001166/**
1167 * audit_inode - store the inode and device from a lookup
1168 * @name: name being audited
1169 * @inode: inode being audited
1170 * @flags: lookup flags (as used in path_lookup())
1171 *
1172 * Called from fs/namei.c:path_lookup().
1173 */
David Woodhouseae7b9612005-06-20 16:11:05 +01001174void audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 int idx;
1177 struct audit_context *context = current->audit_context;
1178
1179 if (!context->in_syscall)
1180 return;
1181 if (context->name_count
1182 && context->names[context->name_count-1].name
1183 && context->names[context->name_count-1].name == name)
1184 idx = context->name_count - 1;
1185 else if (context->name_count > 1
1186 && context->names[context->name_count-2].name
1187 && context->names[context->name_count-2].name == name)
1188 idx = context->name_count - 2;
1189 else {
1190 /* FIXME: how much do we care about inodes that have no
1191 * associated name? */
1192 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1193 return;
1194 idx = context->name_count++;
1195 context->names[idx].name = NULL;
1196#if AUDIT_DEBUG
1197 ++context->ino_count;
1198#endif
1199 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001200 context->names[idx].flags = flags;
1201 context->names[idx].ino = inode->i_ino;
1202 context->names[idx].dev = inode->i_sb->s_dev;
1203 context->names[idx].mode = inode->i_mode;
1204 context->names[idx].uid = inode->i_uid;
1205 context->names[idx].gid = inode->i_gid;
1206 context->names[idx].rdev = inode->i_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001209/**
1210 * auditsc_get_stamp - get local copies of audit_context values
1211 * @ctx: audit_context for the task
1212 * @t: timespec to store time recorded in the audit_context
1213 * @serial: serial value that is recorded in the audit_context
1214 *
1215 * Also sets the context as auditable.
1216 */
David Woodhousebfb44962005-05-21 21:08:09 +01001217void auditsc_get_stamp(struct audit_context *ctx,
1218 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
David Woodhousece625a82005-07-18 14:24:46 -04001220 if (!ctx->serial)
1221 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001222 t->tv_sec = ctx->ctime.tv_sec;
1223 t->tv_nsec = ctx->ctime.tv_nsec;
1224 *serial = ctx->serial;
1225 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001228/**
1229 * audit_set_loginuid - set a task's audit_context loginuid
1230 * @task: task whose audit context is being modified
1231 * @loginuid: loginuid value
1232 *
1233 * Returns 0.
1234 *
1235 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1236 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001237int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Steve Grubb456be6c2005-04-29 17:30:07 +01001239 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001240 struct audit_buffer *ab;
1241
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001242 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001243 if (ab) {
1244 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001245 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001246 task->pid, task->uid,
1247 task->audit_context->loginuid, loginuid);
1248 audit_log_end(ab);
1249 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001250 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 return 0;
1253}
1254
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001255/**
1256 * audit_get_loginuid - get the loginuid for an audit_context
1257 * @ctx: the audit_context
1258 *
1259 * Returns the context's loginuid or -1 if @ctx is NULL.
1260 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261uid_t audit_get_loginuid(struct audit_context *ctx)
1262{
1263 return ctx ? ctx->loginuid : -1;
1264}
1265
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001266/**
1267 * audit_ipc_perms - record audit data for ipc
1268 * @qbytes: msgq bytes
1269 * @uid: msgq user id
1270 * @gid: msgq group id
1271 * @mode: msgq mode (permissions)
1272 *
1273 * Returns 0 for success or NULL context or < 0 on error.
1274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1276{
1277 struct audit_aux_data_ipcctl *ax;
1278 struct audit_context *context = current->audit_context;
1279
1280 if (likely(!context))
1281 return 0;
1282
1283 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1284 if (!ax)
1285 return -ENOMEM;
1286
1287 ax->qbytes = qbytes;
1288 ax->uid = uid;
1289 ax->gid = gid;
1290 ax->mode = mode;
1291
Steve Grubbc0404992005-05-13 18:17:42 +01001292 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ax->d.next = context->aux;
1294 context->aux = (void *)ax;
1295 return 0;
1296}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001297
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001298/**
1299 * audit_socketcall - record audit data for sys_socketcall
1300 * @nargs: number of args
1301 * @args: args array
1302 *
1303 * Returns 0 for success or NULL context or < 0 on error.
1304 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001305int audit_socketcall(int nargs, unsigned long *args)
1306{
1307 struct audit_aux_data_socketcall *ax;
1308 struct audit_context *context = current->audit_context;
1309
1310 if (likely(!context))
1311 return 0;
1312
1313 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1314 if (!ax)
1315 return -ENOMEM;
1316
1317 ax->nargs = nargs;
1318 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1319
1320 ax->d.type = AUDIT_SOCKETCALL;
1321 ax->d.next = context->aux;
1322 context->aux = (void *)ax;
1323 return 0;
1324}
1325
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001326/**
1327 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1328 * @len: data length in user space
1329 * @a: data address in kernel space
1330 *
1331 * Returns 0 for success or NULL context or < 0 on error.
1332 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001333int audit_sockaddr(int len, void *a)
1334{
1335 struct audit_aux_data_sockaddr *ax;
1336 struct audit_context *context = current->audit_context;
1337
1338 if (likely(!context))
1339 return 0;
1340
1341 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1342 if (!ax)
1343 return -ENOMEM;
1344
1345 ax->len = len;
1346 memcpy(ax->a, a, len);
1347
1348 ax->d.type = AUDIT_SOCKADDR;
1349 ax->d.next = context->aux;
1350 context->aux = (void *)ax;
1351 return 0;
1352}
1353
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001354/**
1355 * audit_avc_path - record the granting or denial of permissions
1356 * @dentry: dentry to record
1357 * @mnt: mnt to record
1358 *
1359 * Returns 0 for success or NULL context or < 0 on error.
1360 *
1361 * Called from security/selinux/avc.c::avc_audit()
1362 */
Stephen Smalley01116102005-05-21 00:15:52 +01001363int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1364{
1365 struct audit_aux_data_path *ax;
1366 struct audit_context *context = current->audit_context;
1367
1368 if (likely(!context))
1369 return 0;
1370
1371 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1372 if (!ax)
1373 return -ENOMEM;
1374
1375 ax->dentry = dget(dentry);
1376 ax->mnt = mntget(mnt);
1377
1378 ax->d.type = AUDIT_AVC_PATH;
1379 ax->d.next = context->aux;
1380 context->aux = (void *)ax;
1381 return 0;
1382}
1383
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001384/**
1385 * audit_signal_info - record signal info for shutting down audit subsystem
1386 * @sig: signal value
1387 * @t: task being signaled
1388 *
1389 * If the audit subsystem is being terminated, record the task (pid)
1390 * and uid that is doing that.
1391 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001392void audit_signal_info(int sig, struct task_struct *t)
1393{
1394 extern pid_t audit_sig_pid;
1395 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001396
David Woodhouse582edda2005-07-13 22:39:34 +01001397 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001398 if (sig == SIGTERM || sig == SIGHUP) {
1399 struct audit_context *ctx = current->audit_context;
1400 audit_sig_pid = current->pid;
1401 if (ctx)
1402 audit_sig_uid = ctx->loginuid;
1403 else
1404 audit_sig_uid = current->uid;
1405 }
1406 }
1407}