blob: 685c25175d96374a601d33460dae6e7a35ff0f07 [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
Serge Hallync94c2572005-04-29 16:27:17 +0100333int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
334 uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
David Woodhousef6a789d2005-06-21 16:22:01 +0100336 struct task_struct *tsk;
337 int *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int err = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100339 unsigned listnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 switch (type) {
342 case AUDIT_LIST:
David Woodhousef6a789d2005-06-21 16:22:01 +0100343 /* We can't just spew out the rules here because we might fill
344 * the available socket buffer space and deadlock waiting for
345 * auditctl to read from it... which isn't ever going to
346 * happen if we're actually running in the context of auditctl
347 * trying to _send_ the stuff */
348
349 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
350 if (!dest)
351 return -ENOMEM;
352 dest[0] = pid;
353 dest[1] = seq;
354
355 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
356 if (IS_ERR(tsk)) {
357 kfree(dest);
358 err = PTR_ERR(tsk);
David Woodhouse0f45aa12005-06-19 19:35:50 +0100359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 case AUDIT_ADD:
Amy Griffis3c789a12005-08-17 16:05:35 +0100362 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
363 if (listnr >= AUDIT_NR_FILTERS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -EINVAL;
Amy Griffis3c789a12005-08-17 16:05:35 +0100365
366 err = audit_add_rule(data, &audit_filter_list[listnr]);
367 if (!err)
368 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
369 "auid=%u added an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 break;
371 case AUDIT_DEL:
David Woodhouse0f45aa12005-06-19 19:35:50 +0100372 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
373 if (listnr >= AUDIT_NR_FILTERS)
374 return -EINVAL;
375
376 err = audit_del_rule(data, &audit_filter_list[listnr]);
377 if (!err)
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100378 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhouse0f45aa12005-06-19 19:35:50 +0100379 "auid=%u removed an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 break;
381 default:
382 return -EINVAL;
383 }
384
385 return err;
386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388/* Compare a task_struct with an audit_rule. Return 1 on match, 0
389 * otherwise. */
390static int audit_filter_rules(struct task_struct *tsk,
391 struct audit_rule *rule,
392 struct audit_context *ctx,
393 enum audit_state *state)
394{
395 int i, j;
396
397 for (i = 0; i < rule->field_count; i++) {
398 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
399 u32 value = rule->values[i];
400 int result = 0;
401
402 switch (field) {
403 case AUDIT_PID:
404 result = (tsk->pid == value);
405 break;
406 case AUDIT_UID:
407 result = (tsk->uid == value);
408 break;
409 case AUDIT_EUID:
410 result = (tsk->euid == value);
411 break;
412 case AUDIT_SUID:
413 result = (tsk->suid == value);
414 break;
415 case AUDIT_FSUID:
416 result = (tsk->fsuid == value);
417 break;
418 case AUDIT_GID:
419 result = (tsk->gid == value);
420 break;
421 case AUDIT_EGID:
422 result = (tsk->egid == value);
423 break;
424 case AUDIT_SGID:
425 result = (tsk->sgid == value);
426 break;
427 case AUDIT_FSGID:
428 result = (tsk->fsgid == value);
429 break;
430 case AUDIT_PERS:
431 result = (tsk->personality == value);
432 break;
2fd6f582005-04-29 16:08:28 +0100433 case AUDIT_ARCH:
434 if (ctx)
435 result = (ctx->arch == value);
436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 case AUDIT_EXIT:
439 if (ctx && ctx->return_valid)
440 result = (ctx->return_code == value);
441 break;
442 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100443 if (ctx && ctx->return_valid) {
444 if (value)
445 result = (ctx->return_valid == AUDITSC_SUCCESS);
446 else
447 result = (ctx->return_valid == AUDITSC_FAILURE);
448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
450 case AUDIT_DEVMAJOR:
451 if (ctx) {
452 for (j = 0; j < ctx->name_count; j++) {
453 if (MAJOR(ctx->names[j].dev)==value) {
454 ++result;
455 break;
456 }
457 }
458 }
459 break;
460 case AUDIT_DEVMINOR:
461 if (ctx) {
462 for (j = 0; j < ctx->name_count; j++) {
463 if (MINOR(ctx->names[j].dev)==value) {
464 ++result;
465 break;
466 }
467 }
468 }
469 break;
470 case AUDIT_INODE:
471 if (ctx) {
472 for (j = 0; j < ctx->name_count; j++) {
473 if (ctx->names[j].ino == value) {
474 ++result;
475 break;
476 }
477 }
478 }
479 break;
480 case AUDIT_LOGINUID:
481 result = 0;
482 if (ctx)
483 result = (ctx->loginuid == value);
484 break;
485 case AUDIT_ARG0:
486 case AUDIT_ARG1:
487 case AUDIT_ARG2:
488 case AUDIT_ARG3:
489 if (ctx)
490 result = (ctx->argv[field-AUDIT_ARG0]==value);
491 break;
492 }
493
494 if (rule->fields[i] & AUDIT_NEGATE)
495 result = !result;
496 if (!result)
497 return 0;
498 }
499 switch (rule->action) {
500 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
501 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
502 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
503 }
504 return 1;
505}
506
507/* At process creation time, we can determine if system-call auditing is
508 * completely disabled for this task. Since we only have the task
509 * structure at this point, we can only check uid and gid.
510 */
511static enum audit_state audit_filter_task(struct task_struct *tsk)
512{
513 struct audit_entry *e;
514 enum audit_state state;
515
516 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100517 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
519 rcu_read_unlock();
520 return state;
521 }
522 }
523 rcu_read_unlock();
524 return AUDIT_BUILD_CONTEXT;
525}
526
527/* At syscall entry and exit time, this filter is called if the
528 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100529 * also not high enough that we already know we have to write an audit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
531 */
532static enum audit_state audit_filter_syscall(struct task_struct *tsk,
533 struct audit_context *ctx,
534 struct list_head *list)
535{
536 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100537 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
David Woodhouse351bb722005-07-14 14:40:06 +0100539 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100540 return AUDIT_DISABLED;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100543 if (!list_empty(list)) {
544 int word = AUDIT_WORD(ctx->major);
545 int bit = AUDIT_BIT(ctx->major);
546
547 list_for_each_entry_rcu(e, list, list) {
548 if ((e->rule.mask[word] & bit) == bit
549 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
550 rcu_read_unlock();
551 return state;
552 }
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 rcu_read_unlock();
556 return AUDIT_BUILD_CONTEXT;
557}
558
David Woodhouse5bb289b2005-06-24 14:14:05 +0100559static int audit_filter_user_rules(struct netlink_skb_parms *cb,
560 struct audit_rule *rule,
561 enum audit_state *state)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100562{
David Woodhouse5bb289b2005-06-24 14:14:05 +0100563 int i;
564
565 for (i = 0; i < rule->field_count; i++) {
566 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
567 u32 value = rule->values[i];
568 int result = 0;
569
570 switch (field) {
571 case AUDIT_PID:
572 result = (cb->creds.pid == value);
573 break;
574 case AUDIT_UID:
575 result = (cb->creds.uid == value);
576 break;
577 case AUDIT_GID:
578 result = (cb->creds.gid == value);
579 break;
580 case AUDIT_LOGINUID:
581 result = (cb->loginuid == value);
582 break;
583 }
584
585 if (rule->fields[i] & AUDIT_NEGATE)
586 result = !result;
587 if (!result)
588 return 0;
589 }
590 switch (rule->action) {
591 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
592 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
593 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
594 }
595 return 1;
596}
597
598int audit_filter_user(struct netlink_skb_parms *cb, int type)
599{
David Woodhouse0f45aa12005-06-19 19:35:50 +0100600 struct audit_entry *e;
601 enum audit_state state;
David Woodhouse4a4cd632005-06-22 14:56:47 +0100602 int ret = 1;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100603
604 rcu_read_lock();
605 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
David Woodhouse5bb289b2005-06-24 14:14:05 +0100606 if (audit_filter_user_rules(cb, &e->rule, &state)) {
David Woodhouse4a4cd632005-06-22 14:56:47 +0100607 if (state == AUDIT_DISABLED)
608 ret = 0;
609 break;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100610 }
611 }
612 rcu_read_unlock();
David Woodhouse4a4cd632005-06-22 14:56:47 +0100613
David Woodhouse993e2d42005-06-24 08:21:49 +0100614 return ret; /* Audit by default */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/* This should be called with task_lock() held. */
618static inline struct audit_context *audit_get_context(struct task_struct *tsk,
619 int return_valid,
620 int return_code)
621{
622 struct audit_context *context = tsk->audit_context;
623
624 if (likely(!context))
625 return NULL;
626 context->return_valid = return_valid;
627 context->return_code = return_code;
628
David Woodhouse21af6c42005-07-02 14:10:46 +0100629 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100631 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (state == AUDIT_RECORD_CONTEXT)
633 context->auditable = 1;
634 }
635
636 context->pid = tsk->pid;
637 context->uid = tsk->uid;
638 context->gid = tsk->gid;
639 context->euid = tsk->euid;
640 context->suid = tsk->suid;
641 context->fsuid = tsk->fsuid;
642 context->egid = tsk->egid;
643 context->sgid = tsk->sgid;
644 context->fsgid = tsk->fsgid;
645 context->personality = tsk->personality;
646 tsk->audit_context = NULL;
647 return context;
648}
649
650static inline void audit_free_names(struct audit_context *context)
651{
652 int i;
653
654#if AUDIT_DEBUG == 2
655 if (context->auditable
656 ||context->put_count + context->ino_count != context->name_count) {
657 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
658 " name_count=%d put_count=%d"
659 " ino_count=%d [NOT freeing]\n",
660 __LINE__,
661 context->serial, context->major, context->in_syscall,
662 context->name_count, context->put_count,
663 context->ino_count);
664 for (i = 0; i < context->name_count; i++)
665 printk(KERN_ERR "names[%d] = %p = %s\n", i,
666 context->names[i].name,
667 context->names[i].name);
668 dump_stack();
669 return;
670 }
671#endif
672#if AUDIT_DEBUG
673 context->put_count = 0;
674 context->ino_count = 0;
675#endif
676
677 for (i = 0; i < context->name_count; i++)
678 if (context->names[i].name)
679 __putname(context->names[i].name);
680 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100681 if (context->pwd)
682 dput(context->pwd);
683 if (context->pwdmnt)
684 mntput(context->pwdmnt);
685 context->pwd = NULL;
686 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689static inline void audit_free_aux(struct audit_context *context)
690{
691 struct audit_aux_data *aux;
692
693 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100694 if (aux->type == AUDIT_AVC_PATH) {
695 struct audit_aux_data_path *axi = (void *)aux;
696 dput(axi->dentry);
697 mntput(axi->mnt);
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 context->aux = aux->next;
700 kfree(aux);
701 }
702}
703
704static inline void audit_zero_context(struct audit_context *context,
705 enum audit_state state)
706{
707 uid_t loginuid = context->loginuid;
708
709 memset(context, 0, sizeof(*context));
710 context->state = state;
711 context->loginuid = loginuid;
712}
713
714static inline struct audit_context *audit_alloc_context(enum audit_state state)
715{
716 struct audit_context *context;
717
718 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
719 return NULL;
720 audit_zero_context(context, state);
721 return context;
722}
723
724/* Filter on the task information and allocate a per-task audit context
725 * if necessary. Doing so turns on system call auditing for the
726 * specified task. This is called from copy_process, so no lock is
727 * needed. */
728int audit_alloc(struct task_struct *tsk)
729{
730 struct audit_context *context;
731 enum audit_state state;
732
733 if (likely(!audit_enabled))
734 return 0; /* Return if not auditing. */
735
736 state = audit_filter_task(tsk);
737 if (likely(state == AUDIT_DISABLED))
738 return 0;
739
740 if (!(context = audit_alloc_context(state))) {
741 audit_log_lost("out of memory in audit_alloc");
742 return -ENOMEM;
743 }
744
745 /* Preserve login uid */
746 context->loginuid = -1;
747 if (current->audit_context)
748 context->loginuid = current->audit_context->loginuid;
749
750 tsk->audit_context = context;
751 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
752 return 0;
753}
754
755static inline void audit_free_context(struct audit_context *context)
756{
757 struct audit_context *previous;
758 int count = 0;
759
760 do {
761 previous = context->previous;
762 if (previous || (count && count < 10)) {
763 ++count;
764 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
765 " freeing multiple contexts (%d)\n",
766 context->serial, context->major,
767 context->name_count, count);
768 }
769 audit_free_names(context);
770 audit_free_aux(context);
771 kfree(context);
772 context = previous;
773 } while (context);
774 if (count >= 10)
775 printk(KERN_ERR "audit: freed %d contexts\n", count);
776}
777
Stephen Smalley219f0812005-04-18 10:47:35 -0700778static void audit_log_task_info(struct audit_buffer *ab)
779{
780 char name[sizeof(current->comm)];
781 struct mm_struct *mm = current->mm;
782 struct vm_area_struct *vma;
783
784 get_task_comm(name, current);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100785 audit_log_format(ab, " comm=");
786 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700787
788 if (!mm)
789 return;
790
791 down_read(&mm->mmap_sem);
792 vma = mm->mmap;
793 while (vma) {
794 if ((vma->vm_flags & VM_EXECUTABLE) &&
795 vma->vm_file) {
796 audit_log_d_path(ab, "exe=",
797 vma->vm_file->f_dentry,
798 vma->vm_file->f_vfsmnt);
799 break;
800 }
801 vma = vma->vm_next;
802 }
803 up_read(&mm->mmap_sem);
804}
805
Al Viro9796fdd2005-10-21 03:22:03 -0400806static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 int i;
809 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100810 struct audit_aux_data *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
David Woodhousef5561962005-07-13 22:47:07 +0100812 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (!ab)
814 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100815 audit_log_format(ab, "arch=%x syscall=%d",
816 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (context->personality != PER_LINUX)
818 audit_log_format(ab, " per=%lx", context->personality);
819 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100820 audit_log_format(ab, " success=%s exit=%ld",
821 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
822 context->return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 audit_log_format(ab,
824 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100825 " pid=%d auid=%u uid=%u gid=%u"
826 " euid=%u suid=%u fsuid=%u"
827 " egid=%u sgid=%u fsgid=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 context->argv[0],
829 context->argv[1],
830 context->argv[2],
831 context->argv[3],
832 context->name_count,
833 context->pid,
834 context->loginuid,
835 context->uid,
836 context->gid,
837 context->euid, context->suid, context->fsuid,
838 context->egid, context->sgid, context->fsgid);
Stephen Smalley219f0812005-04-18 10:47:35 -0700839 audit_log_task_info(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
David Woodhouse7551ced2005-05-26 12:04:57 +0100842 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100843
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100844 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (!ab)
846 continue; /* audit_panic has been called */
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100849 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct audit_aux_data_ipcctl *axi = (void *)aux;
851 audit_log_format(ab,
Steve Grubb326e9c82005-05-21 00:22:31 +0100852 " qbytes=%lx iuid=%u igid=%u mode=%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 axi->qbytes, axi->uid, axi->gid, axi->mode);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100854 break; }
855
856 case AUDIT_SOCKETCALL: {
857 int i;
858 struct audit_aux_data_socketcall *axs = (void *)aux;
859 audit_log_format(ab, "nargs=%d", axs->nargs);
860 for (i=0; i<axs->nargs; i++)
861 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
862 break; }
863
864 case AUDIT_SOCKADDR: {
865 struct audit_aux_data_sockaddr *axs = (void *)aux;
866
867 audit_log_format(ab, "saddr=");
868 audit_log_hex(ab, axs->a, axs->len);
869 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100870
871 case AUDIT_AVC_PATH: {
872 struct audit_aux_data_path *axi = (void *)aux;
873 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100874 break; }
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
David Woodhouse8f37d472005-05-27 12:17:28 +0100880 if (context->pwd && context->pwdmnt) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100881 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100882 if (ab) {
883 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
884 audit_log_end(ab);
885 }
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 for (i = 0; i < context->name_count; i++) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100888 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!ab)
890 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 audit_log_format(ab, "item=%d", i);
83c7d092005-04-29 15:54:44 +0100893 if (context->names[i].name) {
894 audit_log_format(ab, " name=");
895 audit_log_untrustedstring(ab, context->names[i].name);
896 }
David Woodhouseae7b9612005-06-20 16:11:05 +0100897 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (context->names[i].ino != (unsigned long)-1)
900 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
Steve Grubb326e9c82005-05-21 00:22:31 +0100901 " ouid=%u ogid=%u rdev=%02x:%02x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 context->names[i].ino,
903 MAJOR(context->names[i].dev),
904 MINOR(context->names[i].dev),
905 context->names[i].mode,
906 context->names[i].uid,
907 context->names[i].gid,
908 MAJOR(context->names[i].rdev),
909 MINOR(context->names[i].rdev));
910 audit_log_end(ab);
911 }
912}
913
914/* Free a per-task audit context. Called from copy_process and
915 * __put_task_struct. */
916void audit_free(struct task_struct *tsk)
917{
918 struct audit_context *context;
919
920 task_lock(tsk);
921 context = audit_get_context(tsk, 0, 0);
922 task_unlock(tsk);
923
924 if (likely(!context))
925 return;
926
927 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100928 * function (e.g., exit_group), then free context block.
929 * We use GFP_ATOMIC here because we might be doing this
930 * in the context of the idle thread */
David Woodhousef7056d62005-06-20 16:07:33 +0100931 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +0100932 audit_log_exit(context, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 audit_free_context(context);
935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/* Fill in audit context at syscall entry. This only happens if the
938 * audit context was created when the task was created and the state or
939 * filters demand the audit context be built. If the state from the
940 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
941 * then the record will be written at syscall exit time (otherwise, it
942 * will only be written if another part of the kernel requests that it
943 * be written). */
2fd6f582005-04-29 16:08:28 +0100944void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 unsigned long a1, unsigned long a2,
946 unsigned long a3, unsigned long a4)
947{
948 struct audit_context *context = tsk->audit_context;
949 enum audit_state state;
950
951 BUG_ON(!context);
952
953 /* This happens only on certain architectures that make system
954 * calls in kernel_thread via the entry.S interface, instead of
955 * with direct calls. (If you are porting to a new
956 * architecture, hitting this condition can indicate that you
957 * got the _exit/_leave calls backward in entry.S.)
958 *
959 * i386 no
960 * x86_64 no
961 * ppc64 yes (see arch/ppc64/kernel/misc.S)
962 *
963 * This also happens with vm86 emulation in a non-nested manner
964 * (entries without exits), so this case must be caught.
965 */
966 if (context->in_syscall) {
967 struct audit_context *newctx;
968
969#if defined(__NR_vm86) && defined(__NR_vm86old)
970 /* vm86 mode should only be entered once */
971 if (major == __NR_vm86 || major == __NR_vm86old)
972 return;
973#endif
974#if AUDIT_DEBUG
975 printk(KERN_ERR
976 "audit(:%d) pid=%d in syscall=%d;"
977 " entering syscall=%d\n",
978 context->serial, tsk->pid, context->major, major);
979#endif
980 newctx = audit_alloc_context(context->state);
981 if (newctx) {
982 newctx->previous = context;
983 context = newctx;
984 tsk->audit_context = newctx;
985 } else {
986 /* If we can't alloc a new context, the best we
987 * can do is to leak memory (any pending putname
988 * will be lost). The only other alternative is
989 * to abandon auditing. */
990 audit_zero_context(context, context->state);
991 }
992 }
993 BUG_ON(context->in_syscall || context->name_count);
994
995 if (!audit_enabled)
996 return;
997
2fd6f582005-04-29 16:08:28 +0100998 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 context->major = major;
1000 context->argv[0] = a1;
1001 context->argv[1] = a2;
1002 context->argv[2] = a3;
1003 context->argv[3] = a4;
1004
1005 state = context->state;
1006 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +01001007 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (likely(state == AUDIT_DISABLED))
1009 return;
1010
David Woodhousece625a82005-07-18 14:24:46 -04001011 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 context->ctime = CURRENT_TIME;
1013 context->in_syscall = 1;
1014 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1015}
1016
1017/* Tear down after system call. If the audit context has been marked as
1018 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1019 * filtering, or because some other part of the kernel write an audit
1020 * message), then write out the syscall information. In call cases,
1021 * free the names stored from getname(). */
2fd6f582005-04-29 16:08:28 +01001022void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 struct audit_context *context;
1025
1026 get_task_struct(tsk);
1027 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +01001028 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 task_unlock(tsk);
1030
1031 /* Not having a context here is ok, since the parent may have
1032 * called __put_task_struct. */
1033 if (likely(!context))
David Woodhouse413a1c72005-08-17 14:45:55 +01001034 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
David Woodhousef7056d62005-06-20 16:07:33 +01001036 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +01001037 audit_log_exit(context, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 context->in_syscall = 0;
1040 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (context->previous) {
1043 struct audit_context *new_context = context->previous;
1044 context->previous = NULL;
1045 audit_free_context(context);
1046 tsk->audit_context = new_context;
1047 } else {
1048 audit_free_names(context);
1049 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 tsk->audit_context = context;
1051 }
David Woodhouse413a1c72005-08-17 14:45:55 +01001052 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 put_task_struct(tsk);
1054}
1055
1056/* Add a name to the list. Called from fs/namei.c:getname(). */
1057void audit_getname(const char *name)
1058{
1059 struct audit_context *context = current->audit_context;
1060
1061 if (!context || IS_ERR(name) || !name)
1062 return;
1063
1064 if (!context->in_syscall) {
1065#if AUDIT_DEBUG == 2
1066 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1067 __FILE__, __LINE__, context->serial, name);
1068 dump_stack();
1069#endif
1070 return;
1071 }
1072 BUG_ON(context->name_count >= AUDIT_NAMES);
1073 context->names[context->name_count].name = name;
1074 context->names[context->name_count].ino = (unsigned long)-1;
1075 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001076 if (!context->pwd) {
1077 read_lock(&current->fs->lock);
1078 context->pwd = dget(current->fs->pwd);
1079 context->pwdmnt = mntget(current->fs->pwdmnt);
1080 read_unlock(&current->fs->lock);
1081 }
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
1085/* Intercept a putname request. Called from
1086 * include/linux/fs.h:putname(). If we have stored the name from
1087 * getname in the audit context, then we delay the putname until syscall
1088 * exit. */
1089void audit_putname(const char *name)
1090{
1091 struct audit_context *context = current->audit_context;
1092
1093 BUG_ON(!context);
1094 if (!context->in_syscall) {
1095#if AUDIT_DEBUG == 2
1096 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1097 __FILE__, __LINE__, context->serial, name);
1098 if (context->name_count) {
1099 int i;
1100 for (i = 0; i < context->name_count; i++)
1101 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1102 context->names[i].name,
1103 context->names[i].name);
1104 }
1105#endif
1106 __putname(name);
1107 }
1108#if AUDIT_DEBUG
1109 else {
1110 ++context->put_count;
1111 if (context->put_count > context->name_count) {
1112 printk(KERN_ERR "%s:%d(:%d): major=%d"
1113 " in_syscall=%d putname(%p) name_count=%d"
1114 " put_count=%d\n",
1115 __FILE__, __LINE__,
1116 context->serial, context->major,
1117 context->in_syscall, name, context->name_count,
1118 context->put_count);
1119 dump_stack();
1120 }
1121 }
1122#endif
1123}
1124
1125/* Store the inode and device from a lookup. Called from
1126 * fs/namei.c:path_lookup(). */
David Woodhouseae7b9612005-06-20 16:11:05 +01001127void audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
1129 int idx;
1130 struct audit_context *context = current->audit_context;
1131
1132 if (!context->in_syscall)
1133 return;
1134 if (context->name_count
1135 && context->names[context->name_count-1].name
1136 && context->names[context->name_count-1].name == name)
1137 idx = context->name_count - 1;
1138 else if (context->name_count > 1
1139 && context->names[context->name_count-2].name
1140 && context->names[context->name_count-2].name == name)
1141 idx = context->name_count - 2;
1142 else {
1143 /* FIXME: how much do we care about inodes that have no
1144 * associated name? */
1145 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1146 return;
1147 idx = context->name_count++;
1148 context->names[idx].name = NULL;
1149#if AUDIT_DEBUG
1150 ++context->ino_count;
1151#endif
1152 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001153 context->names[idx].flags = flags;
1154 context->names[idx].ino = inode->i_ino;
1155 context->names[idx].dev = inode->i_sb->s_dev;
1156 context->names[idx].mode = inode->i_mode;
1157 context->names[idx].uid = inode->i_uid;
1158 context->names[idx].gid = inode->i_gid;
1159 context->names[idx].rdev = inode->i_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
David Woodhousebfb44962005-05-21 21:08:09 +01001162void auditsc_get_stamp(struct audit_context *ctx,
1163 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
David Woodhousece625a82005-07-18 14:24:46 -04001165 if (!ctx->serial)
1166 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001167 t->tv_sec = ctx->ctime.tv_sec;
1168 t->tv_nsec = ctx->ctime.tv_nsec;
1169 *serial = ctx->serial;
1170 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Steve Grubb456be6c2005-04-29 17:30:07 +01001173int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Steve Grubb456be6c2005-04-29 17:30:07 +01001175 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001176 struct audit_buffer *ab;
1177
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001178 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001179 if (ab) {
1180 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001181 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001182 task->pid, task->uid,
1183 task->audit_context->loginuid, loginuid);
1184 audit_log_end(ab);
1185 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001186 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 return 0;
1189}
1190
1191uid_t audit_get_loginuid(struct audit_context *ctx)
1192{
1193 return ctx ? ctx->loginuid : -1;
1194}
1195
1196int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1197{
1198 struct audit_aux_data_ipcctl *ax;
1199 struct audit_context *context = current->audit_context;
1200
1201 if (likely(!context))
1202 return 0;
1203
1204 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1205 if (!ax)
1206 return -ENOMEM;
1207
1208 ax->qbytes = qbytes;
1209 ax->uid = uid;
1210 ax->gid = gid;
1211 ax->mode = mode;
1212
Steve Grubbc0404992005-05-13 18:17:42 +01001213 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 ax->d.next = context->aux;
1215 context->aux = (void *)ax;
1216 return 0;
1217}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001218
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001219int audit_socketcall(int nargs, unsigned long *args)
1220{
1221 struct audit_aux_data_socketcall *ax;
1222 struct audit_context *context = current->audit_context;
1223
1224 if (likely(!context))
1225 return 0;
1226
1227 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1228 if (!ax)
1229 return -ENOMEM;
1230
1231 ax->nargs = nargs;
1232 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1233
1234 ax->d.type = AUDIT_SOCKETCALL;
1235 ax->d.next = context->aux;
1236 context->aux = (void *)ax;
1237 return 0;
1238}
1239
1240int audit_sockaddr(int len, void *a)
1241{
1242 struct audit_aux_data_sockaddr *ax;
1243 struct audit_context *context = current->audit_context;
1244
1245 if (likely(!context))
1246 return 0;
1247
1248 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1249 if (!ax)
1250 return -ENOMEM;
1251
1252 ax->len = len;
1253 memcpy(ax->a, a, len);
1254
1255 ax->d.type = AUDIT_SOCKADDR;
1256 ax->d.next = context->aux;
1257 context->aux = (void *)ax;
1258 return 0;
1259}
1260
Stephen Smalley01116102005-05-21 00:15:52 +01001261int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1262{
1263 struct audit_aux_data_path *ax;
1264 struct audit_context *context = current->audit_context;
1265
1266 if (likely(!context))
1267 return 0;
1268
1269 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1270 if (!ax)
1271 return -ENOMEM;
1272
1273 ax->dentry = dget(dentry);
1274 ax->mnt = mntget(mnt);
1275
1276 ax->d.type = AUDIT_AVC_PATH;
1277 ax->d.next = context->aux;
1278 context->aux = (void *)ax;
1279 return 0;
1280}
1281
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001282void audit_signal_info(int sig, struct task_struct *t)
1283{
1284 extern pid_t audit_sig_pid;
1285 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001286
David Woodhouse582edda2005-07-13 22:39:34 +01001287 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001288 if (sig == SIGTERM || sig == SIGHUP) {
1289 struct audit_context *ctx = current->audit_context;
1290 audit_sig_pid = current->pid;
1291 if (ctx)
1292 audit_sig_uid = ctx->loginuid;
1293 else
1294 audit_sig_uid = current->uid;
1295 }
1296 }
1297}
1298