blob: 46b45abceb9ad0dae8f0ad3bc5eb8de5884d7c97 [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>
33#include <asm/atomic.h>
34#include <asm/types.h>
35#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/* Check to see if two rules are identical. It is called from
194 * audit_del_rule during AUDIT_DEL. */
195static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
196{
197 int i;
198
199 if (a->flags != b->flags)
200 return 1;
201
202 if (a->action != b->action)
203 return 1;
204
205 if (a->field_count != b->field_count)
206 return 1;
207
208 for (i = 0; i < a->field_count; i++) {
209 if (a->fields[i] != b->fields[i]
210 || a->values[i] != b->values[i])
211 return 1;
212 }
213
214 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
215 if (a->mask[i] != b->mask[i])
216 return 1;
217
218 return 0;
219}
220
221/* Note that audit_add_rule and audit_del_rule are called via
222 * audit_receive() in audit.c, and are protected by
223 * audit_netlink_sem. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100224static inline void audit_add_rule(struct audit_entry *entry,
225 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
David Woodhouse0f45aa12005-06-19 19:35:50 +0100227 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
228 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 list_add_rcu(&entry->list, list);
230 } else {
231 list_add_tail_rcu(&entry->list, list);
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static void audit_free_rule(struct rcu_head *head)
236{
237 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
238 kfree(e);
239}
240
241/* Note that audit_add_rule and audit_del_rule are called via
242 * audit_receive() in audit.c, and are protected by
243 * audit_netlink_sem. */
244static inline int audit_del_rule(struct audit_rule *rule,
245 struct list_head *list)
246{
247 struct audit_entry *e;
248
249 /* Do not use the _rcu iterator here, since this is the only
250 * deletion routine. */
251 list_for_each_entry(e, list, list) {
252 if (!audit_compare_rule(rule, &e->rule)) {
253 list_del_rcu(&e->list);
254 call_rcu(&e->rcu, audit_free_rule);
255 return 0;
256 }
257 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100258 return -ENOENT; /* No matching rule */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/* Copy rule from user-space to kernel-space. Called during
262 * AUDIT_ADD. */
263static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
264{
265 int i;
266
267 if (s->action != AUDIT_NEVER
268 && s->action != AUDIT_POSSIBLE
269 && s->action != AUDIT_ALWAYS)
270 return -1;
271 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
272 return -1;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100273 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
274 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 d->flags = s->flags;
277 d->action = s->action;
278 d->field_count = s->field_count;
279 for (i = 0; i < d->field_count; i++) {
280 d->fields[i] = s->fields[i];
281 d->values[i] = s->values[i];
282 }
283 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
284 return 0;
285}
286
David Woodhousef6a789d2005-06-21 16:22:01 +0100287static int audit_list_rules(void *_dest)
288{
289 int pid, seq;
290 int *dest = _dest;
291 struct audit_entry *entry;
292 int i;
293
294 pid = dest[0];
295 seq = dest[1];
296 kfree(dest);
297
298 down(&audit_netlink_sem);
299
300 /* The *_rcu iterators not needed here because we are
301 always called with audit_netlink_sem held. */
302 for (i=0; i<AUDIT_NR_FILTERS; i++) {
303 list_for_each_entry(entry, &audit_filter_list[i], list)
304 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
305 &entry->rule, sizeof(entry->rule));
306 }
307 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
308
309 up(&audit_netlink_sem);
310 return 0;
311}
312
Serge Hallync94c2572005-04-29 16:27:17 +0100313int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
314 uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct audit_entry *entry;
David Woodhousef6a789d2005-06-21 16:22:01 +0100317 struct task_struct *tsk;
318 int *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int err = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100320 unsigned listnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 switch (type) {
323 case AUDIT_LIST:
David Woodhousef6a789d2005-06-21 16:22:01 +0100324 /* We can't just spew out the rules here because we might fill
325 * the available socket buffer space and deadlock waiting for
326 * auditctl to read from it... which isn't ever going to
327 * happen if we're actually running in the context of auditctl
328 * trying to _send_ the stuff */
329
330 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
331 if (!dest)
332 return -ENOMEM;
333 dest[0] = pid;
334 dest[1] = seq;
335
336 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
337 if (IS_ERR(tsk)) {
338 kfree(dest);
339 err = PTR_ERR(tsk);
David Woodhouse0f45aa12005-06-19 19:35:50 +0100340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 break;
342 case AUDIT_ADD:
343 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
344 return -ENOMEM;
345 if (audit_copy_rule(&entry->rule, data)) {
346 kfree(entry);
347 return -EINVAL;
348 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100349 listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
350 audit_add_rule(entry, &audit_filter_list[listnr]);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100351 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100352 "auid=%u added an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 case AUDIT_DEL:
David Woodhouse0f45aa12005-06-19 19:35:50 +0100355 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
356 if (listnr >= AUDIT_NR_FILTERS)
357 return -EINVAL;
358
359 err = audit_del_rule(data, &audit_filter_list[listnr]);
360 if (!err)
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100361 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhouse0f45aa12005-06-19 19:35:50 +0100362 "auid=%u removed an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 default:
365 return -EINVAL;
366 }
367
368 return err;
369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371/* Compare a task_struct with an audit_rule. Return 1 on match, 0
372 * otherwise. */
373static int audit_filter_rules(struct task_struct *tsk,
374 struct audit_rule *rule,
375 struct audit_context *ctx,
376 enum audit_state *state)
377{
378 int i, j;
379
380 for (i = 0; i < rule->field_count; i++) {
381 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
382 u32 value = rule->values[i];
383 int result = 0;
384
385 switch (field) {
386 case AUDIT_PID:
387 result = (tsk->pid == value);
388 break;
389 case AUDIT_UID:
390 result = (tsk->uid == value);
391 break;
392 case AUDIT_EUID:
393 result = (tsk->euid == value);
394 break;
395 case AUDIT_SUID:
396 result = (tsk->suid == value);
397 break;
398 case AUDIT_FSUID:
399 result = (tsk->fsuid == value);
400 break;
401 case AUDIT_GID:
402 result = (tsk->gid == value);
403 break;
404 case AUDIT_EGID:
405 result = (tsk->egid == value);
406 break;
407 case AUDIT_SGID:
408 result = (tsk->sgid == value);
409 break;
410 case AUDIT_FSGID:
411 result = (tsk->fsgid == value);
412 break;
413 case AUDIT_PERS:
414 result = (tsk->personality == value);
415 break;
2fd6f582005-04-29 16:08:28 +0100416 case AUDIT_ARCH:
417 if (ctx)
418 result = (ctx->arch == value);
419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 case AUDIT_EXIT:
422 if (ctx && ctx->return_valid)
423 result = (ctx->return_code == value);
424 break;
425 case AUDIT_SUCCESS:
426 if (ctx && ctx->return_valid)
2fd6f582005-04-29 16:08:28 +0100427 result = (ctx->return_valid == AUDITSC_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 break;
429 case AUDIT_DEVMAJOR:
430 if (ctx) {
431 for (j = 0; j < ctx->name_count; j++) {
432 if (MAJOR(ctx->names[j].dev)==value) {
433 ++result;
434 break;
435 }
436 }
437 }
438 break;
439 case AUDIT_DEVMINOR:
440 if (ctx) {
441 for (j = 0; j < ctx->name_count; j++) {
442 if (MINOR(ctx->names[j].dev)==value) {
443 ++result;
444 break;
445 }
446 }
447 }
448 break;
449 case AUDIT_INODE:
450 if (ctx) {
451 for (j = 0; j < ctx->name_count; j++) {
452 if (ctx->names[j].ino == value) {
453 ++result;
454 break;
455 }
456 }
457 }
458 break;
459 case AUDIT_LOGINUID:
460 result = 0;
461 if (ctx)
462 result = (ctx->loginuid == value);
463 break;
464 case AUDIT_ARG0:
465 case AUDIT_ARG1:
466 case AUDIT_ARG2:
467 case AUDIT_ARG3:
468 if (ctx)
469 result = (ctx->argv[field-AUDIT_ARG0]==value);
470 break;
471 }
472
473 if (rule->fields[i] & AUDIT_NEGATE)
474 result = !result;
475 if (!result)
476 return 0;
477 }
478 switch (rule->action) {
479 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
480 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
481 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
482 }
483 return 1;
484}
485
486/* At process creation time, we can determine if system-call auditing is
487 * completely disabled for this task. Since we only have the task
488 * structure at this point, we can only check uid and gid.
489 */
490static enum audit_state audit_filter_task(struct task_struct *tsk)
491{
492 struct audit_entry *e;
493 enum audit_state state;
494
495 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100496 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
498 rcu_read_unlock();
499 return state;
500 }
501 }
502 rcu_read_unlock();
503 return AUDIT_BUILD_CONTEXT;
504}
505
506/* At syscall entry and exit time, this filter is called if the
507 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100508 * also not high enough that we already know we have to write an audit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
510 */
511static enum audit_state audit_filter_syscall(struct task_struct *tsk,
512 struct audit_context *ctx,
513 struct list_head *list)
514{
515 struct audit_entry *e;
516 enum audit_state state;
517 int word = AUDIT_WORD(ctx->major);
518 int bit = AUDIT_BIT(ctx->major);
519
David Woodhouse351bb722005-07-14 14:40:06 +0100520 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100521 return AUDIT_DISABLED;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 rcu_read_lock();
524 list_for_each_entry_rcu(e, list, list) {
525 if ((e->rule.mask[word] & bit) == bit
526 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
527 rcu_read_unlock();
528 return state;
529 }
530 }
531 rcu_read_unlock();
532 return AUDIT_BUILD_CONTEXT;
533}
534
David Woodhouse5bb289b2005-06-24 14:14:05 +0100535static int audit_filter_user_rules(struct netlink_skb_parms *cb,
536 struct audit_rule *rule,
537 enum audit_state *state)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100538{
David Woodhouse5bb289b2005-06-24 14:14:05 +0100539 int i;
540
541 for (i = 0; i < rule->field_count; i++) {
542 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
543 u32 value = rule->values[i];
544 int result = 0;
545
546 switch (field) {
547 case AUDIT_PID:
548 result = (cb->creds.pid == value);
549 break;
550 case AUDIT_UID:
551 result = (cb->creds.uid == value);
552 break;
553 case AUDIT_GID:
554 result = (cb->creds.gid == value);
555 break;
556 case AUDIT_LOGINUID:
557 result = (cb->loginuid == value);
558 break;
559 }
560
561 if (rule->fields[i] & AUDIT_NEGATE)
562 result = !result;
563 if (!result)
564 return 0;
565 }
566 switch (rule->action) {
567 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
568 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
569 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
570 }
571 return 1;
572}
573
574int audit_filter_user(struct netlink_skb_parms *cb, int type)
575{
David Woodhouse0f45aa12005-06-19 19:35:50 +0100576 struct audit_entry *e;
577 enum audit_state state;
David Woodhouse4a4cd632005-06-22 14:56:47 +0100578 int ret = 1;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100579
580 rcu_read_lock();
581 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
David Woodhouse5bb289b2005-06-24 14:14:05 +0100582 if (audit_filter_user_rules(cb, &e->rule, &state)) {
David Woodhouse4a4cd632005-06-22 14:56:47 +0100583 if (state == AUDIT_DISABLED)
584 ret = 0;
585 break;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100586 }
587 }
588 rcu_read_unlock();
David Woodhouse4a4cd632005-06-22 14:56:47 +0100589
David Woodhouse993e2d412005-06-24 08:21:49 +0100590 return ret; /* Audit by default */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/* This should be called with task_lock() held. */
594static inline struct audit_context *audit_get_context(struct task_struct *tsk,
595 int return_valid,
596 int return_code)
597{
598 struct audit_context *context = tsk->audit_context;
599
600 if (likely(!context))
601 return NULL;
602 context->return_valid = return_valid;
603 context->return_code = return_code;
604
David Woodhouse21af6c42005-07-02 14:10:46 +0100605 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100607 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (state == AUDIT_RECORD_CONTEXT)
609 context->auditable = 1;
610 }
611
612 context->pid = tsk->pid;
613 context->uid = tsk->uid;
614 context->gid = tsk->gid;
615 context->euid = tsk->euid;
616 context->suid = tsk->suid;
617 context->fsuid = tsk->fsuid;
618 context->egid = tsk->egid;
619 context->sgid = tsk->sgid;
620 context->fsgid = tsk->fsgid;
621 context->personality = tsk->personality;
622 tsk->audit_context = NULL;
623 return context;
624}
625
626static inline void audit_free_names(struct audit_context *context)
627{
628 int i;
629
630#if AUDIT_DEBUG == 2
631 if (context->auditable
632 ||context->put_count + context->ino_count != context->name_count) {
633 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
634 " name_count=%d put_count=%d"
635 " ino_count=%d [NOT freeing]\n",
636 __LINE__,
637 context->serial, context->major, context->in_syscall,
638 context->name_count, context->put_count,
639 context->ino_count);
640 for (i = 0; i < context->name_count; i++)
641 printk(KERN_ERR "names[%d] = %p = %s\n", i,
642 context->names[i].name,
643 context->names[i].name);
644 dump_stack();
645 return;
646 }
647#endif
648#if AUDIT_DEBUG
649 context->put_count = 0;
650 context->ino_count = 0;
651#endif
652
653 for (i = 0; i < context->name_count; i++)
654 if (context->names[i].name)
655 __putname(context->names[i].name);
656 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100657 if (context->pwd)
658 dput(context->pwd);
659 if (context->pwdmnt)
660 mntput(context->pwdmnt);
661 context->pwd = NULL;
662 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665static inline void audit_free_aux(struct audit_context *context)
666{
667 struct audit_aux_data *aux;
668
669 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100670 if (aux->type == AUDIT_AVC_PATH) {
671 struct audit_aux_data_path *axi = (void *)aux;
672 dput(axi->dentry);
673 mntput(axi->mnt);
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 context->aux = aux->next;
676 kfree(aux);
677 }
678}
679
680static inline void audit_zero_context(struct audit_context *context,
681 enum audit_state state)
682{
683 uid_t loginuid = context->loginuid;
684
685 memset(context, 0, sizeof(*context));
686 context->state = state;
687 context->loginuid = loginuid;
688}
689
690static inline struct audit_context *audit_alloc_context(enum audit_state state)
691{
692 struct audit_context *context;
693
694 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
695 return NULL;
696 audit_zero_context(context, state);
697 return context;
698}
699
700/* Filter on the task information and allocate a per-task audit context
701 * if necessary. Doing so turns on system call auditing for the
702 * specified task. This is called from copy_process, so no lock is
703 * needed. */
704int audit_alloc(struct task_struct *tsk)
705{
706 struct audit_context *context;
707 enum audit_state state;
708
709 if (likely(!audit_enabled))
710 return 0; /* Return if not auditing. */
711
712 state = audit_filter_task(tsk);
713 if (likely(state == AUDIT_DISABLED))
714 return 0;
715
716 if (!(context = audit_alloc_context(state))) {
717 audit_log_lost("out of memory in audit_alloc");
718 return -ENOMEM;
719 }
720
721 /* Preserve login uid */
722 context->loginuid = -1;
723 if (current->audit_context)
724 context->loginuid = current->audit_context->loginuid;
725
726 tsk->audit_context = context;
727 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
728 return 0;
729}
730
731static inline void audit_free_context(struct audit_context *context)
732{
733 struct audit_context *previous;
734 int count = 0;
735
736 do {
737 previous = context->previous;
738 if (previous || (count && count < 10)) {
739 ++count;
740 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
741 " freeing multiple contexts (%d)\n",
742 context->serial, context->major,
743 context->name_count, count);
744 }
745 audit_free_names(context);
746 audit_free_aux(context);
747 kfree(context);
748 context = previous;
749 } while (context);
750 if (count >= 10)
751 printk(KERN_ERR "audit: freed %d contexts\n", count);
752}
753
Stephen Smalley219f0812005-04-18 10:47:35 -0700754static void audit_log_task_info(struct audit_buffer *ab)
755{
756 char name[sizeof(current->comm)];
757 struct mm_struct *mm = current->mm;
758 struct vm_area_struct *vma;
759
760 get_task_comm(name, current);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100761 audit_log_format(ab, " comm=");
762 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700763
764 if (!mm)
765 return;
766
767 down_read(&mm->mmap_sem);
768 vma = mm->mmap;
769 while (vma) {
770 if ((vma->vm_flags & VM_EXECUTABLE) &&
771 vma->vm_file) {
772 audit_log_d_path(ab, "exe=",
773 vma->vm_file->f_dentry,
774 vma->vm_file->f_vfsmnt);
775 break;
776 }
777 vma = vma->vm_next;
778 }
779 up_read(&mm->mmap_sem);
780}
781
David Woodhousef5561962005-07-13 22:47:07 +0100782static void audit_log_exit(struct audit_context *context, unsigned int gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 int i;
785 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100786 struct audit_aux_data *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
David Woodhousef5561962005-07-13 22:47:07 +0100788 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!ab)
790 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100791 audit_log_format(ab, "arch=%x syscall=%d",
792 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (context->personality != PER_LINUX)
794 audit_log_format(ab, " per=%lx", context->personality);
795 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100796 audit_log_format(ab, " success=%s exit=%ld",
797 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
798 context->return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 audit_log_format(ab,
800 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100801 " pid=%d auid=%u uid=%u gid=%u"
802 " euid=%u suid=%u fsuid=%u"
803 " egid=%u sgid=%u fsgid=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 context->argv[0],
805 context->argv[1],
806 context->argv[2],
807 context->argv[3],
808 context->name_count,
809 context->pid,
810 context->loginuid,
811 context->uid,
812 context->gid,
813 context->euid, context->suid, context->fsuid,
814 context->egid, context->sgid, context->fsgid);
Stephen Smalley219f0812005-04-18 10:47:35 -0700815 audit_log_task_info(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
David Woodhouse7551ced2005-05-26 12:04:57 +0100818 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100819
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100820 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (!ab)
822 continue; /* audit_panic has been called */
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100825 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct audit_aux_data_ipcctl *axi = (void *)aux;
827 audit_log_format(ab,
Steve Grubb326e9c82005-05-21 00:22:31 +0100828 " qbytes=%lx iuid=%u igid=%u mode=%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 axi->qbytes, axi->uid, axi->gid, axi->mode);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100830 break; }
831
832 case AUDIT_SOCKETCALL: {
833 int i;
834 struct audit_aux_data_socketcall *axs = (void *)aux;
835 audit_log_format(ab, "nargs=%d", axs->nargs);
836 for (i=0; i<axs->nargs; i++)
837 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
838 break; }
839
840 case AUDIT_SOCKADDR: {
841 struct audit_aux_data_sockaddr *axs = (void *)aux;
842
843 audit_log_format(ab, "saddr=");
844 audit_log_hex(ab, axs->a, axs->len);
845 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100846
847 case AUDIT_AVC_PATH: {
848 struct audit_aux_data_path *axi = (void *)aux;
849 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100850 break; }
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
David Woodhouse8f37d472005-05-27 12:17:28 +0100856 if (context->pwd && context->pwdmnt) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100857 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100858 if (ab) {
859 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
860 audit_log_end(ab);
861 }
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 for (i = 0; i < context->name_count; i++) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100864 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (!ab)
866 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 audit_log_format(ab, "item=%d", i);
83c7d092005-04-29 15:54:44 +0100869 if (context->names[i].name) {
870 audit_log_format(ab, " name=");
871 audit_log_untrustedstring(ab, context->names[i].name);
872 }
David Woodhouseae7b9612005-06-20 16:11:05 +0100873 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (context->names[i].ino != (unsigned long)-1)
876 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
Steve Grubb326e9c82005-05-21 00:22:31 +0100877 " ouid=%u ogid=%u rdev=%02x:%02x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 context->names[i].ino,
879 MAJOR(context->names[i].dev),
880 MINOR(context->names[i].dev),
881 context->names[i].mode,
882 context->names[i].uid,
883 context->names[i].gid,
884 MAJOR(context->names[i].rdev),
885 MINOR(context->names[i].rdev));
886 audit_log_end(ab);
887 }
888}
889
890/* Free a per-task audit context. Called from copy_process and
891 * __put_task_struct. */
892void audit_free(struct task_struct *tsk)
893{
894 struct audit_context *context;
895
896 task_lock(tsk);
897 context = audit_get_context(tsk, 0, 0);
898 task_unlock(tsk);
899
900 if (likely(!context))
901 return;
902
903 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100904 * function (e.g., exit_group), then free context block.
905 * We use GFP_ATOMIC here because we might be doing this
906 * in the context of the idle thread */
David Woodhousef7056d62005-06-20 16:07:33 +0100907 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +0100908 audit_log_exit(context, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 audit_free_context(context);
911}
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913/* Fill in audit context at syscall entry. This only happens if the
914 * audit context was created when the task was created and the state or
915 * filters demand the audit context be built. If the state from the
916 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
917 * then the record will be written at syscall exit time (otherwise, it
918 * will only be written if another part of the kernel requests that it
919 * be written). */
2fd6f582005-04-29 16:08:28 +0100920void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 unsigned long a1, unsigned long a2,
922 unsigned long a3, unsigned long a4)
923{
924 struct audit_context *context = tsk->audit_context;
925 enum audit_state state;
926
927 BUG_ON(!context);
928
929 /* This happens only on certain architectures that make system
930 * calls in kernel_thread via the entry.S interface, instead of
931 * with direct calls. (If you are porting to a new
932 * architecture, hitting this condition can indicate that you
933 * got the _exit/_leave calls backward in entry.S.)
934 *
935 * i386 no
936 * x86_64 no
937 * ppc64 yes (see arch/ppc64/kernel/misc.S)
938 *
939 * This also happens with vm86 emulation in a non-nested manner
940 * (entries without exits), so this case must be caught.
941 */
942 if (context->in_syscall) {
943 struct audit_context *newctx;
944
945#if defined(__NR_vm86) && defined(__NR_vm86old)
946 /* vm86 mode should only be entered once */
947 if (major == __NR_vm86 || major == __NR_vm86old)
948 return;
949#endif
950#if AUDIT_DEBUG
951 printk(KERN_ERR
952 "audit(:%d) pid=%d in syscall=%d;"
953 " entering syscall=%d\n",
954 context->serial, tsk->pid, context->major, major);
955#endif
956 newctx = audit_alloc_context(context->state);
957 if (newctx) {
958 newctx->previous = context;
959 context = newctx;
960 tsk->audit_context = newctx;
961 } else {
962 /* If we can't alloc a new context, the best we
963 * can do is to leak memory (any pending putname
964 * will be lost). The only other alternative is
965 * to abandon auditing. */
966 audit_zero_context(context, context->state);
967 }
968 }
969 BUG_ON(context->in_syscall || context->name_count);
970
971 if (!audit_enabled)
972 return;
973
2fd6f582005-04-29 16:08:28 +0100974 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 context->major = major;
976 context->argv[0] = a1;
977 context->argv[1] = a2;
978 context->argv[2] = a3;
979 context->argv[3] = a4;
980
981 state = context->state;
982 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100983 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (likely(state == AUDIT_DISABLED))
985 return;
986
David Woodhousece625a82005-07-18 14:24:46 -0400987 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 context->ctime = CURRENT_TIME;
989 context->in_syscall = 1;
990 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
991}
992
993/* Tear down after system call. If the audit context has been marked as
994 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
995 * filtering, or because some other part of the kernel write an audit
996 * message), then write out the syscall information. In call cases,
997 * free the names stored from getname(). */
2fd6f582005-04-29 16:08:28 +0100998void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct audit_context *context;
1001
1002 get_task_struct(tsk);
1003 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +01001004 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 task_unlock(tsk);
1006
1007 /* Not having a context here is ok, since the parent may have
1008 * called __put_task_struct. */
1009 if (likely(!context))
1010 return;
1011
David Woodhousef7056d62005-06-20 16:07:33 +01001012 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +01001013 audit_log_exit(context, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 context->in_syscall = 0;
1016 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (context->previous) {
1019 struct audit_context *new_context = context->previous;
1020 context->previous = NULL;
1021 audit_free_context(context);
1022 tsk->audit_context = new_context;
1023 } else {
1024 audit_free_names(context);
1025 audit_free_aux(context);
1026 audit_zero_context(context, context->state);
1027 tsk->audit_context = context;
1028 }
1029 put_task_struct(tsk);
1030}
1031
1032/* Add a name to the list. Called from fs/namei.c:getname(). */
1033void audit_getname(const char *name)
1034{
1035 struct audit_context *context = current->audit_context;
1036
1037 if (!context || IS_ERR(name) || !name)
1038 return;
1039
1040 if (!context->in_syscall) {
1041#if AUDIT_DEBUG == 2
1042 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1043 __FILE__, __LINE__, context->serial, name);
1044 dump_stack();
1045#endif
1046 return;
1047 }
1048 BUG_ON(context->name_count >= AUDIT_NAMES);
1049 context->names[context->name_count].name = name;
1050 context->names[context->name_count].ino = (unsigned long)-1;
1051 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001052 if (!context->pwd) {
1053 read_lock(&current->fs->lock);
1054 context->pwd = dget(current->fs->pwd);
1055 context->pwdmnt = mntget(current->fs->pwdmnt);
1056 read_unlock(&current->fs->lock);
1057 }
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/* Intercept a putname request. Called from
1062 * include/linux/fs.h:putname(). If we have stored the name from
1063 * getname in the audit context, then we delay the putname until syscall
1064 * exit. */
1065void audit_putname(const char *name)
1066{
1067 struct audit_context *context = current->audit_context;
1068
1069 BUG_ON(!context);
1070 if (!context->in_syscall) {
1071#if AUDIT_DEBUG == 2
1072 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1073 __FILE__, __LINE__, context->serial, name);
1074 if (context->name_count) {
1075 int i;
1076 for (i = 0; i < context->name_count; i++)
1077 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1078 context->names[i].name,
1079 context->names[i].name);
1080 }
1081#endif
1082 __putname(name);
1083 }
1084#if AUDIT_DEBUG
1085 else {
1086 ++context->put_count;
1087 if (context->put_count > context->name_count) {
1088 printk(KERN_ERR "%s:%d(:%d): major=%d"
1089 " in_syscall=%d putname(%p) name_count=%d"
1090 " put_count=%d\n",
1091 __FILE__, __LINE__,
1092 context->serial, context->major,
1093 context->in_syscall, name, context->name_count,
1094 context->put_count);
1095 dump_stack();
1096 }
1097 }
1098#endif
1099}
1100
1101/* Store the inode and device from a lookup. Called from
1102 * fs/namei.c:path_lookup(). */
David Woodhouseae7b9612005-06-20 16:11:05 +01001103void audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 int idx;
1106 struct audit_context *context = current->audit_context;
1107
1108 if (!context->in_syscall)
1109 return;
1110 if (context->name_count
1111 && context->names[context->name_count-1].name
1112 && context->names[context->name_count-1].name == name)
1113 idx = context->name_count - 1;
1114 else if (context->name_count > 1
1115 && context->names[context->name_count-2].name
1116 && context->names[context->name_count-2].name == name)
1117 idx = context->name_count - 2;
1118 else {
1119 /* FIXME: how much do we care about inodes that have no
1120 * associated name? */
1121 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1122 return;
1123 idx = context->name_count++;
1124 context->names[idx].name = NULL;
1125#if AUDIT_DEBUG
1126 ++context->ino_count;
1127#endif
1128 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001129 context->names[idx].flags = flags;
1130 context->names[idx].ino = inode->i_ino;
1131 context->names[idx].dev = inode->i_sb->s_dev;
1132 context->names[idx].mode = inode->i_mode;
1133 context->names[idx].uid = inode->i_uid;
1134 context->names[idx].gid = inode->i_gid;
1135 context->names[idx].rdev = inode->i_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
David Woodhousebfb44962005-05-21 21:08:09 +01001138void auditsc_get_stamp(struct audit_context *ctx,
1139 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
David Woodhousece625a82005-07-18 14:24:46 -04001141 if (!ctx->serial)
1142 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001143 t->tv_sec = ctx->ctime.tv_sec;
1144 t->tv_nsec = ctx->ctime.tv_nsec;
1145 *serial = ctx->serial;
1146 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Steve Grubb456be6c2005-04-29 17:30:07 +01001149int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Steve Grubb456be6c2005-04-29 17:30:07 +01001151 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001152 struct audit_buffer *ab;
1153
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001154 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001155 if (ab) {
1156 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001157 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001158 task->pid, task->uid,
1159 task->audit_context->loginuid, loginuid);
1160 audit_log_end(ab);
1161 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001162 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164 return 0;
1165}
1166
1167uid_t audit_get_loginuid(struct audit_context *ctx)
1168{
1169 return ctx ? ctx->loginuid : -1;
1170}
1171
1172int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1173{
1174 struct audit_aux_data_ipcctl *ax;
1175 struct audit_context *context = current->audit_context;
1176
1177 if (likely(!context))
1178 return 0;
1179
1180 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1181 if (!ax)
1182 return -ENOMEM;
1183
1184 ax->qbytes = qbytes;
1185 ax->uid = uid;
1186 ax->gid = gid;
1187 ax->mode = mode;
1188
Steve Grubbc0404992005-05-13 18:17:42 +01001189 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 ax->d.next = context->aux;
1191 context->aux = (void *)ax;
1192 return 0;
1193}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001194
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001195int audit_socketcall(int nargs, unsigned long *args)
1196{
1197 struct audit_aux_data_socketcall *ax;
1198 struct audit_context *context = current->audit_context;
1199
1200 if (likely(!context))
1201 return 0;
1202
1203 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1204 if (!ax)
1205 return -ENOMEM;
1206
1207 ax->nargs = nargs;
1208 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1209
1210 ax->d.type = AUDIT_SOCKETCALL;
1211 ax->d.next = context->aux;
1212 context->aux = (void *)ax;
1213 return 0;
1214}
1215
1216int audit_sockaddr(int len, void *a)
1217{
1218 struct audit_aux_data_sockaddr *ax;
1219 struct audit_context *context = current->audit_context;
1220
1221 if (likely(!context))
1222 return 0;
1223
1224 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1225 if (!ax)
1226 return -ENOMEM;
1227
1228 ax->len = len;
1229 memcpy(ax->a, a, len);
1230
1231 ax->d.type = AUDIT_SOCKADDR;
1232 ax->d.next = context->aux;
1233 context->aux = (void *)ax;
1234 return 0;
1235}
1236
Stephen Smalley01116102005-05-21 00:15:52 +01001237int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1238{
1239 struct audit_aux_data_path *ax;
1240 struct audit_context *context = current->audit_context;
1241
1242 if (likely(!context))
1243 return 0;
1244
1245 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1246 if (!ax)
1247 return -ENOMEM;
1248
1249 ax->dentry = dget(dentry);
1250 ax->mnt = mntget(mnt);
1251
1252 ax->d.type = AUDIT_AVC_PATH;
1253 ax->d.next = context->aux;
1254 context->aux = (void *)ax;
1255 return 0;
1256}
1257
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001258void audit_signal_info(int sig, struct task_struct *t)
1259{
1260 extern pid_t audit_sig_pid;
1261 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001262
David Woodhouse582edda2005-07-13 22:39:34 +01001263 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001264 if (sig == SIGTERM || sig == SIGHUP) {
1265 struct audit_context *ctx = current->audit_context;
1266 audit_sig_pid = current->pid;
1267 if (ctx)
1268 audit_sig_uid = ctx->loginuid;
1269 else
1270 audit_sig_uid = current->uid;
1271 }
1272 }
1273}
1274