[PATCH] Collect more inode information during syscall processing.
[linux-block.git] / kernel / auditsc.c
CommitLineData
85c8721f 1/* auditsc.c -- System-call auditing support
1da177e4
LT
2 * Handles all system-call specific auditing features.
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
73241ccc 5 * Copyright 2005 Hewlett-Packard Development Company, L.P.
b63862f4 6 * Copyright (C) 2005 IBM Corporation
1da177e4
LT
7 * All Rights Reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
24 *
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
27 *
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
31 *
b63862f4
DK
32 * The support of additional filter rules compares (>, <, >=, <=) was
33 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
34 *
73241ccc
AG
35 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36 * filesystem information.
1da177e4
LT
37 */
38
39#include <linux/init.h>
1da177e4 40#include <asm/types.h>
715b49ef 41#include <asm/atomic.h>
73241ccc
AG
42#include <asm/types.h>
43#include <linux/fs.h>
44#include <linux/namei.h>
1da177e4
LT
45#include <linux/mm.h>
46#include <linux/module.h>
01116105 47#include <linux/mount.h>
3ec3b2fb 48#include <linux/socket.h>
1da177e4
LT
49#include <linux/audit.h>
50#include <linux/personality.h>
51#include <linux/time.h>
f6a789d1 52#include <linux/kthread.h>
5bb289b5 53#include <linux/netlink.h>
f5561964 54#include <linux/compiler.h>
1da177e4
LT
55#include <asm/unistd.h>
56
57/* 0 = no checking
58 1 = put_count checking
59 2 = verbose put_count checking
60*/
61#define AUDIT_DEBUG 0
62
63/* No syscall auditing will take place unless audit_enabled != 0. */
64extern int audit_enabled;
65
66/* AUDIT_NAMES is the number of slots we reserve in the audit_context
67 * for saving names from getname(). */
68#define AUDIT_NAMES 20
69
70/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
71 * audit_context from being used for nameless inodes from
72 * path_lookup. */
73#define AUDIT_NAMES_RESERVED 7
74
75/* At task start time, the audit_state is set in the audit_context using
76 a per-task filter. At syscall entry, the audit_state is augmented by
77 the syscall filter. */
78enum audit_state {
79 AUDIT_DISABLED, /* Do not create per-task audit_context.
80 * No syscall-specific audit records can
81 * be generated. */
82 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
83 * but don't necessarily fill it in at
84 * syscall entry time (i.e., filter
85 * instead). */
86 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
87 * and always fill it in at syscall
88 * entry time. This makes a full
89 * syscall record available if some
90 * other part of the kernel decides it
91 * should be recorded. */
92 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
93 * always fill it in at syscall entry
94 * time, and always write out the audit
95 * record at syscall exit time. */
96};
97
98/* When fs/namei.c:getname() is called, we store the pointer in name and
99 * we don't let putname() free it (instead we free all of the saved
100 * pointers at syscall exit time).
101 *
102 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
103struct audit_names {
104 const char *name;
105 unsigned long ino;
73241ccc 106 unsigned long pino;
1da177e4
LT
107 dev_t dev;
108 umode_t mode;
109 uid_t uid;
110 gid_t gid;
111 dev_t rdev;
112};
113
114struct audit_aux_data {
115 struct audit_aux_data *next;
116 int type;
117};
118
119#define AUDIT_AUX_IPCPERM 0
120
121struct audit_aux_data_ipcctl {
122 struct audit_aux_data d;
123 struct ipc_perm p;
124 unsigned long qbytes;
125 uid_t uid;
126 gid_t gid;
127 mode_t mode;
128};
129
3ec3b2fb
DW
130struct audit_aux_data_socketcall {
131 struct audit_aux_data d;
132 int nargs;
133 unsigned long args[0];
134};
135
136struct audit_aux_data_sockaddr {
137 struct audit_aux_data d;
138 int len;
139 char a[0];
140};
141
01116105
SS
142struct audit_aux_data_path {
143 struct audit_aux_data d;
144 struct dentry *dentry;
145 struct vfsmount *mnt;
146};
1da177e4
LT
147
148/* The per-task audit context. */
149struct audit_context {
150 int in_syscall; /* 1 if task is in a syscall */
151 enum audit_state state;
152 unsigned int serial; /* serial number for record */
153 struct timespec ctime; /* time of syscall entry */
154 uid_t loginuid; /* login uid (identity) */
155 int major; /* syscall number */
156 unsigned long argv[4]; /* syscall arguments */
157 int return_valid; /* return code is valid */
2fd6f58b 158 long return_code;/* syscall return code */
1da177e4
LT
159 int auditable; /* 1 if record should be written */
160 int name_count;
161 struct audit_names names[AUDIT_NAMES];
8f37d47c
DW
162 struct dentry * pwd;
163 struct vfsmount * pwdmnt;
1da177e4
LT
164 struct audit_context *previous; /* For nested syscalls */
165 struct audit_aux_data *aux;
166
167 /* Save things to print about task_struct */
168 pid_t pid;
169 uid_t uid, euid, suid, fsuid;
170 gid_t gid, egid, sgid, fsgid;
171 unsigned long personality;
2fd6f58b 172 int arch;
1da177e4
LT
173
174#if AUDIT_DEBUG
175 int put_count;
176 int ino_count;
177#endif
178};
179
180 /* Public API */
181/* There are three lists of rules -- one to search at task creation
182 * time, one to search at syscall entry time, and another to search at
183 * syscall exit time. */
0f45aa18
DW
184static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
185 LIST_HEAD_INIT(audit_filter_list[0]),
186 LIST_HEAD_INIT(audit_filter_list[1]),
187 LIST_HEAD_INIT(audit_filter_list[2]),
188 LIST_HEAD_INIT(audit_filter_list[3]),
189 LIST_HEAD_INIT(audit_filter_list[4]),
190#if AUDIT_NR_FILTERS != 5
191#error Fix audit_filter_list initialiser
192#endif
193};
1da177e4
LT
194
195struct audit_entry {
196 struct list_head list;
197 struct rcu_head rcu;
198 struct audit_rule rule;
199};
200
7ca00264
DW
201extern int audit_pid;
202
3c789a19
AG
203/* Copy rule from user-space to kernel-space. Called from
204 * audit_add_rule during AUDIT_ADD. */
205static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
206{
207 int i;
208
209 if (s->action != AUDIT_NEVER
210 && s->action != AUDIT_POSSIBLE
211 && s->action != AUDIT_ALWAYS)
212 return -1;
213 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
214 return -1;
215 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
216 return -1;
217
218 d->flags = s->flags;
219 d->action = s->action;
220 d->field_count = s->field_count;
221 for (i = 0; i < d->field_count; i++) {
222 d->fields[i] = s->fields[i];
223 d->values[i] = s->values[i];
224 }
225 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
226 return 0;
227}
228
1da177e4 229/* Check to see if two rules are identical. It is called from
3c789a19 230 * audit_add_rule during AUDIT_ADD and
1da177e4 231 * audit_del_rule during AUDIT_DEL. */
3c789a19 232static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
1da177e4
LT
233{
234 int i;
235
236 if (a->flags != b->flags)
237 return 1;
238
239 if (a->action != b->action)
240 return 1;
241
242 if (a->field_count != b->field_count)
243 return 1;
244
245 for (i = 0; i < a->field_count; i++) {
246 if (a->fields[i] != b->fields[i]
247 || a->values[i] != b->values[i])
248 return 1;
249 }
250
251 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
252 if (a->mask[i] != b->mask[i])
253 return 1;
254
255 return 0;
256}
257
258/* Note that audit_add_rule and audit_del_rule are called via
259 * audit_receive() in audit.c, and are protected by
260 * audit_netlink_sem. */
3c789a19 261static inline int audit_add_rule(struct audit_rule *rule,
0f45aa18 262 struct list_head *list)
1da177e4 263{
3c789a19 264 struct audit_entry *entry;
b63862f4 265 int i;
3c789a19
AG
266
267 /* Do not use the _rcu iterator here, since this is the only
268 * addition routine. */
269 list_for_each_entry(entry, list, list) {
270 if (!audit_compare_rule(rule, &entry->rule)) {
271 return -EEXIST;
272 }
273 }
274
b63862f4
DK
275 for (i = 0; i < rule->field_count; i++) {
276 if (rule->fields[i] & AUDIT_UNUSED_BITS)
277 return -EINVAL;
278 if ( rule->fields[i] & AUDIT_NEGATE )
279 rule->fields[i] |= AUDIT_NOT_EQUAL;
280 else if ( (rule->fields[i] & AUDIT_OPERATORS) == 0 )
281 rule->fields[i] |= AUDIT_EQUAL;
282 rule->fields[i] &= (~AUDIT_NEGATE);
283 }
284
3c789a19
AG
285 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
286 return -ENOMEM;
287 if (audit_copy_rule(&entry->rule, rule)) {
288 kfree(entry);
289 return -EINVAL;
290 }
291
0f45aa18
DW
292 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
293 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
1da177e4
LT
294 list_add_rcu(&entry->list, list);
295 } else {
296 list_add_tail_rcu(&entry->list, list);
297 }
3c789a19
AG
298
299 return 0;
1da177e4
LT
300}
301
3c789a19 302static inline void audit_free_rule(struct rcu_head *head)
1da177e4
LT
303{
304 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
305 kfree(e);
306}
307
308/* Note that audit_add_rule and audit_del_rule are called via
309 * audit_receive() in audit.c, and are protected by
310 * audit_netlink_sem. */
311static inline int audit_del_rule(struct audit_rule *rule,
312 struct list_head *list)
313{
314 struct audit_entry *e;
315
316 /* Do not use the _rcu iterator here, since this is the only
317 * deletion routine. */
318 list_for_each_entry(e, list, list) {
319 if (!audit_compare_rule(rule, &e->rule)) {
320 list_del_rcu(&e->list);
321 call_rcu(&e->rcu, audit_free_rule);
322 return 0;
323 }
324 }
0f45aa18 325 return -ENOENT; /* No matching rule */
1da177e4
LT
326}
327
f6a789d1
DW
328static int audit_list_rules(void *_dest)
329{
330 int pid, seq;
331 int *dest = _dest;
332 struct audit_entry *entry;
333 int i;
334
335 pid = dest[0];
336 seq = dest[1];
337 kfree(dest);
338
339 down(&audit_netlink_sem);
340
341 /* The *_rcu iterators not needed here because we are
342 always called with audit_netlink_sem held. */
343 for (i=0; i<AUDIT_NR_FILTERS; i++) {
344 list_for_each_entry(entry, &audit_filter_list[i], list)
345 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
346 &entry->rule, sizeof(entry->rule));
347 }
348 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
349
350 up(&audit_netlink_sem);
351 return 0;
352}
353
b0dd25a8
RD
354/**
355 * audit_receive_filter - apply all rules to the specified message type
356 * @type: audit message type
357 * @pid: target pid for netlink audit messages
358 * @uid: target uid for netlink audit messages
359 * @seq: netlink audit message sequence (serial) number
360 * @data: payload data
361 * @loginuid: loginuid of sender
362 */
c94c257c
SH
363int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
364 uid_t loginuid)
1da177e4 365{
f6a789d1
DW
366 struct task_struct *tsk;
367 int *dest;
1da177e4 368 int err = 0;
0f45aa18 369 unsigned listnr;
1da177e4
LT
370
371 switch (type) {
372 case AUDIT_LIST:
f6a789d1
DW
373 /* We can't just spew out the rules here because we might fill
374 * the available socket buffer space and deadlock waiting for
375 * auditctl to read from it... which isn't ever going to
376 * happen if we're actually running in the context of auditctl
377 * trying to _send_ the stuff */
378
379 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
380 if (!dest)
381 return -ENOMEM;
382 dest[0] = pid;
383 dest[1] = seq;
384
385 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
386 if (IS_ERR(tsk)) {
387 kfree(dest);
388 err = PTR_ERR(tsk);
0f45aa18 389 }
1da177e4
LT
390 break;
391 case AUDIT_ADD:
3c789a19
AG
392 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
393 if (listnr >= AUDIT_NR_FILTERS)
1da177e4 394 return -EINVAL;
3c789a19
AG
395
396 err = audit_add_rule(data, &audit_filter_list[listnr]);
397 if (!err)
398 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
399 "auid=%u added an audit rule\n", loginuid);
1da177e4
LT
400 break;
401 case AUDIT_DEL:
0f45aa18
DW
402 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
403 if (listnr >= AUDIT_NR_FILTERS)
404 return -EINVAL;
405
406 err = audit_del_rule(data, &audit_filter_list[listnr]);
407 if (!err)
9ad9ad38 408 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
0f45aa18 409 "auid=%u removed an audit rule\n", loginuid);
1da177e4
LT
410 break;
411 default:
412 return -EINVAL;
413 }
414
415 return err;
416}
1da177e4 417
b63862f4
DK
418static int audit_comparator(const u32 left, const u32 op, const u32 right)
419{
420 switch (op) {
421 case AUDIT_EQUAL:
422 return (left == right);
423 case AUDIT_NOT_EQUAL:
424 return (left != right);
425 case AUDIT_LESS_THAN:
426 return (left < right);
427 case AUDIT_LESS_THAN_OR_EQUAL:
428 return (left <= right);
429 case AUDIT_GREATER_THAN:
430 return (left > right);
431 case AUDIT_GREATER_THAN_OR_EQUAL:
432 return (left >= right);
433 default:
434 return -EINVAL;
435 }
436}
437
1da177e4
LT
438/* Compare a task_struct with an audit_rule. Return 1 on match, 0
439 * otherwise. */
440static int audit_filter_rules(struct task_struct *tsk,
441 struct audit_rule *rule,
442 struct audit_context *ctx,
443 enum audit_state *state)
444{
445 int i, j;
446
447 for (i = 0; i < rule->field_count; i++) {
b63862f4
DK
448 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
449 u32 op = rule->fields[i] & AUDIT_OPERATORS;
1da177e4
LT
450 u32 value = rule->values[i];
451 int result = 0;
452
453 switch (field) {
454 case AUDIT_PID:
b63862f4 455 result = audit_comparator(tsk->pid, op, value);
1da177e4
LT
456 break;
457 case AUDIT_UID:
b63862f4 458 result = audit_comparator(tsk->uid, op, value);
1da177e4
LT
459 break;
460 case AUDIT_EUID:
b63862f4 461 result = audit_comparator(tsk->euid, op, value);
1da177e4
LT
462 break;
463 case AUDIT_SUID:
b63862f4 464 result = audit_comparator(tsk->suid, op, value);
1da177e4
LT
465 break;
466 case AUDIT_FSUID:
b63862f4 467 result = audit_comparator(tsk->fsuid, op, value);
1da177e4
LT
468 break;
469 case AUDIT_GID:
b63862f4 470 result = audit_comparator(tsk->gid, op, value);
1da177e4
LT
471 break;
472 case AUDIT_EGID:
b63862f4 473 result = audit_comparator(tsk->egid, op, value);
1da177e4
LT
474 break;
475 case AUDIT_SGID:
b63862f4 476 result = audit_comparator(tsk->sgid, op, value);
1da177e4
LT
477 break;
478 case AUDIT_FSGID:
b63862f4 479 result = audit_comparator(tsk->fsgid, op, value);
1da177e4
LT
480 break;
481 case AUDIT_PERS:
b63862f4 482 result = audit_comparator(tsk->personality, op, value);
1da177e4 483 break;
2fd6f58b 484 case AUDIT_ARCH:
b63862f4
DK
485 if (ctx)
486 result = audit_comparator(ctx->arch, op, value);
2fd6f58b 487 break;
1da177e4
LT
488
489 case AUDIT_EXIT:
490 if (ctx && ctx->return_valid)
b63862f4 491 result = audit_comparator(ctx->return_code, op, value);
1da177e4
LT
492 break;
493 case AUDIT_SUCCESS:
b01f2cc1
DW
494 if (ctx && ctx->return_valid) {
495 if (value)
b63862f4 496 result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS);
b01f2cc1 497 else
b63862f4 498 result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE);
b01f2cc1 499 }
1da177e4
LT
500 break;
501 case AUDIT_DEVMAJOR:
502 if (ctx) {
503 for (j = 0; j < ctx->name_count; j++) {
b63862f4 504 if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) {
1da177e4
LT
505 ++result;
506 break;
507 }
508 }
509 }
510 break;
511 case AUDIT_DEVMINOR:
512 if (ctx) {
513 for (j = 0; j < ctx->name_count; j++) {
b63862f4 514 if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) {
1da177e4
LT
515 ++result;
516 break;
517 }
518 }
519 }
520 break;
521 case AUDIT_INODE:
522 if (ctx) {
523 for (j = 0; j < ctx->name_count; j++) {
73241ccc
AG
524 if (audit_comparator(ctx->names[j].ino, op, value) ||
525 audit_comparator(ctx->names[j].pino, op, value)) {
1da177e4
LT
526 ++result;
527 break;
528 }
529 }
530 }
531 break;
532 case AUDIT_LOGINUID:
533 result = 0;
534 if (ctx)
b63862f4 535 result = audit_comparator(ctx->loginuid, op, value);
1da177e4
LT
536 break;
537 case AUDIT_ARG0:
538 case AUDIT_ARG1:
539 case AUDIT_ARG2:
540 case AUDIT_ARG3:
541 if (ctx)
b63862f4 542 result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value);
1da177e4
LT
543 break;
544 }
545
1da177e4
LT
546 if (!result)
547 return 0;
548 }
549 switch (rule->action) {
550 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
551 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
552 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
553 }
554 return 1;
555}
556
557/* At process creation time, we can determine if system-call auditing is
558 * completely disabled for this task. Since we only have the task
559 * structure at this point, we can only check uid and gid.
560 */
561static enum audit_state audit_filter_task(struct task_struct *tsk)
562{
563 struct audit_entry *e;
564 enum audit_state state;
565
566 rcu_read_lock();
0f45aa18 567 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
1da177e4
LT
568 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
569 rcu_read_unlock();
570 return state;
571 }
572 }
573 rcu_read_unlock();
574 return AUDIT_BUILD_CONTEXT;
575}
576
577/* At syscall entry and exit time, this filter is called if the
578 * audit_state is not low enough that auditing cannot take place, but is
23f32d18 579 * also not high enough that we already know we have to write an audit
b0dd25a8 580 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
1da177e4
LT
581 */
582static enum audit_state audit_filter_syscall(struct task_struct *tsk,
583 struct audit_context *ctx,
584 struct list_head *list)
585{
586 struct audit_entry *e;
c3896495 587 enum audit_state state;
1da177e4 588
351bb722 589 if (audit_pid && tsk->tgid == audit_pid)
f7056d64
DW
590 return AUDIT_DISABLED;
591
1da177e4 592 rcu_read_lock();
c3896495 593 if (!list_empty(list)) {
b63862f4
DK
594 int word = AUDIT_WORD(ctx->major);
595 int bit = AUDIT_BIT(ctx->major);
596
597 list_for_each_entry_rcu(e, list, list) {
598 if ((e->rule.mask[word] & bit) == bit
599 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
600 rcu_read_unlock();
601 return state;
602 }
603 }
1da177e4
LT
604 }
605 rcu_read_unlock();
606 return AUDIT_BUILD_CONTEXT;
607}
608
5bb289b5 609static int audit_filter_user_rules(struct netlink_skb_parms *cb,
b63862f4
DK
610 struct audit_rule *rule,
611 enum audit_state *state)
5bb289b5
DW
612{
613 int i;
614
615 for (i = 0; i < rule->field_count; i++) {
b63862f4
DK
616 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
617 u32 op = rule->fields[i] & AUDIT_OPERATORS;
5bb289b5
DW
618 u32 value = rule->values[i];
619 int result = 0;
620
621 switch (field) {
622 case AUDIT_PID:
b63862f4 623 result = audit_comparator(cb->creds.pid, op, value);
5bb289b5
DW
624 break;
625 case AUDIT_UID:
b63862f4 626 result = audit_comparator(cb->creds.uid, op, value);
5bb289b5
DW
627 break;
628 case AUDIT_GID:
b63862f4 629 result = audit_comparator(cb->creds.gid, op, value);
5bb289b5
DW
630 break;
631 case AUDIT_LOGINUID:
b63862f4 632 result = audit_comparator(cb->loginuid, op, value);
5bb289b5
DW
633 break;
634 }
635
5bb289b5
DW
636 if (!result)
637 return 0;
638 }
639 switch (rule->action) {
640 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
641 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
642 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
643 }
644 return 1;
645}
646
647int audit_filter_user(struct netlink_skb_parms *cb, int type)
0f45aa18
DW
648{
649 struct audit_entry *e;
650 enum audit_state state;
4a4cd633 651 int ret = 1;
0f45aa18
DW
652
653 rcu_read_lock();
654 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
5bb289b5 655 if (audit_filter_user_rules(cb, &e->rule, &state)) {
4a4cd633
DW
656 if (state == AUDIT_DISABLED)
657 ret = 0;
658 break;
0f45aa18
DW
659 }
660 }
661 rcu_read_unlock();
4a4cd633 662
993e2d41 663 return ret; /* Audit by default */
0f45aa18
DW
664}
665
1da177e4
LT
666/* This should be called with task_lock() held. */
667static inline struct audit_context *audit_get_context(struct task_struct *tsk,
668 int return_valid,
669 int return_code)
670{
671 struct audit_context *context = tsk->audit_context;
672
673 if (likely(!context))
674 return NULL;
675 context->return_valid = return_valid;
676 context->return_code = return_code;
677
21af6c4f 678 if (context->in_syscall && !context->auditable) {
1da177e4 679 enum audit_state state;
0f45aa18 680 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
1da177e4
LT
681 if (state == AUDIT_RECORD_CONTEXT)
682 context->auditable = 1;
683 }
684
685 context->pid = tsk->pid;
686 context->uid = tsk->uid;
687 context->gid = tsk->gid;
688 context->euid = tsk->euid;
689 context->suid = tsk->suid;
690 context->fsuid = tsk->fsuid;
691 context->egid = tsk->egid;
692 context->sgid = tsk->sgid;
693 context->fsgid = tsk->fsgid;
694 context->personality = tsk->personality;
695 tsk->audit_context = NULL;
696 return context;
697}
698
699static inline void audit_free_names(struct audit_context *context)
700{
701 int i;
702
703#if AUDIT_DEBUG == 2
704 if (context->auditable
705 ||context->put_count + context->ino_count != context->name_count) {
73241ccc 706 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
1da177e4
LT
707 " name_count=%d put_count=%d"
708 " ino_count=%d [NOT freeing]\n",
73241ccc 709 __FILE__, __LINE__,
1da177e4
LT
710 context->serial, context->major, context->in_syscall,
711 context->name_count, context->put_count,
712 context->ino_count);
713 for (i = 0; i < context->name_count; i++)
714 printk(KERN_ERR "names[%d] = %p = %s\n", i,
715 context->names[i].name,
73241ccc 716 context->names[i].name ?: "(null)");
1da177e4
LT
717 dump_stack();
718 return;
719 }
720#endif
721#if AUDIT_DEBUG
722 context->put_count = 0;
723 context->ino_count = 0;
724#endif
725
726 for (i = 0; i < context->name_count; i++)
727 if (context->names[i].name)
728 __putname(context->names[i].name);
729 context->name_count = 0;
8f37d47c
DW
730 if (context->pwd)
731 dput(context->pwd);
732 if (context->pwdmnt)
733 mntput(context->pwdmnt);
734 context->pwd = NULL;
735 context->pwdmnt = NULL;
1da177e4
LT
736}
737
738static inline void audit_free_aux(struct audit_context *context)
739{
740 struct audit_aux_data *aux;
741
742 while ((aux = context->aux)) {
01116105
SS
743 if (aux->type == AUDIT_AVC_PATH) {
744 struct audit_aux_data_path *axi = (void *)aux;
745 dput(axi->dentry);
746 mntput(axi->mnt);
747 }
1da177e4
LT
748 context->aux = aux->next;
749 kfree(aux);
750 }
751}
752
753static inline void audit_zero_context(struct audit_context *context,
754 enum audit_state state)
755{
756 uid_t loginuid = context->loginuid;
757
758 memset(context, 0, sizeof(*context));
759 context->state = state;
760 context->loginuid = loginuid;
761}
762
763static inline struct audit_context *audit_alloc_context(enum audit_state state)
764{
765 struct audit_context *context;
766
767 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
768 return NULL;
769 audit_zero_context(context, state);
770 return context;
771}
772
b0dd25a8
RD
773/**
774 * audit_alloc - allocate an audit context block for a task
775 * @tsk: task
776 *
777 * Filter on the task information and allocate a per-task audit context
1da177e4
LT
778 * if necessary. Doing so turns on system call auditing for the
779 * specified task. This is called from copy_process, so no lock is
b0dd25a8
RD
780 * needed.
781 */
1da177e4
LT
782int audit_alloc(struct task_struct *tsk)
783{
784 struct audit_context *context;
785 enum audit_state state;
786
787 if (likely(!audit_enabled))
788 return 0; /* Return if not auditing. */
789
790 state = audit_filter_task(tsk);
791 if (likely(state == AUDIT_DISABLED))
792 return 0;
793
794 if (!(context = audit_alloc_context(state))) {
795 audit_log_lost("out of memory in audit_alloc");
796 return -ENOMEM;
797 }
798
799 /* Preserve login uid */
800 context->loginuid = -1;
801 if (current->audit_context)
802 context->loginuid = current->audit_context->loginuid;
803
804 tsk->audit_context = context;
805 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
806 return 0;
807}
808
809static inline void audit_free_context(struct audit_context *context)
810{
811 struct audit_context *previous;
812 int count = 0;
813
814 do {
815 previous = context->previous;
816 if (previous || (count && count < 10)) {
817 ++count;
818 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
819 " freeing multiple contexts (%d)\n",
820 context->serial, context->major,
821 context->name_count, count);
822 }
823 audit_free_names(context);
824 audit_free_aux(context);
825 kfree(context);
826 context = previous;
827 } while (context);
828 if (count >= 10)
829 printk(KERN_ERR "audit: freed %d contexts\n", count);
830}
831
219f0817
SS
832static void audit_log_task_info(struct audit_buffer *ab)
833{
834 char name[sizeof(current->comm)];
835 struct mm_struct *mm = current->mm;
836 struct vm_area_struct *vma;
837
838 get_task_comm(name, current);
99e45eea
DW
839 audit_log_format(ab, " comm=");
840 audit_log_untrustedstring(ab, name);
219f0817
SS
841
842 if (!mm)
843 return;
844
845 down_read(&mm->mmap_sem);
846 vma = mm->mmap;
847 while (vma) {
848 if ((vma->vm_flags & VM_EXECUTABLE) &&
849 vma->vm_file) {
850 audit_log_d_path(ab, "exe=",
851 vma->vm_file->f_dentry,
852 vma->vm_file->f_vfsmnt);
853 break;
854 }
855 vma = vma->vm_next;
856 }
857 up_read(&mm->mmap_sem);
858}
859
9796fdd8 860static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
1da177e4
LT
861{
862 int i;
863 struct audit_buffer *ab;
7551ced3 864 struct audit_aux_data *aux;
1da177e4 865
f5561964 866 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
1da177e4
LT
867 if (!ab)
868 return; /* audit_panic has been called */
bccf6ae0
DW
869 audit_log_format(ab, "arch=%x syscall=%d",
870 context->arch, context->major);
1da177e4
LT
871 if (context->personality != PER_LINUX)
872 audit_log_format(ab, " per=%lx", context->personality);
873 if (context->return_valid)
2fd6f58b 874 audit_log_format(ab, " success=%s exit=%ld",
875 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
876 context->return_code);
1da177e4
LT
877 audit_log_format(ab,
878 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
326e9c8b
SG
879 " pid=%d auid=%u uid=%u gid=%u"
880 " euid=%u suid=%u fsuid=%u"
881 " egid=%u sgid=%u fsgid=%u",
1da177e4
LT
882 context->argv[0],
883 context->argv[1],
884 context->argv[2],
885 context->argv[3],
886 context->name_count,
887 context->pid,
888 context->loginuid,
889 context->uid,
890 context->gid,
891 context->euid, context->suid, context->fsuid,
892 context->egid, context->sgid, context->fsgid);
219f0817 893 audit_log_task_info(ab);
1da177e4 894 audit_log_end(ab);
1da177e4 895
7551ced3 896 for (aux = context->aux; aux; aux = aux->next) {
c0404993 897
ef20c8c1 898 ab = audit_log_start(context, gfp_mask, aux->type);
1da177e4
LT
899 if (!ab)
900 continue; /* audit_panic has been called */
901
1da177e4 902 switch (aux->type) {
c0404993 903 case AUDIT_IPC: {
1da177e4
LT
904 struct audit_aux_data_ipcctl *axi = (void *)aux;
905 audit_log_format(ab,
326e9c8b 906 " qbytes=%lx iuid=%u igid=%u mode=%x",
1da177e4 907 axi->qbytes, axi->uid, axi->gid, axi->mode);
3ec3b2fb
DW
908 break; }
909
910 case AUDIT_SOCKETCALL: {
911 int i;
912 struct audit_aux_data_socketcall *axs = (void *)aux;
913 audit_log_format(ab, "nargs=%d", axs->nargs);
914 for (i=0; i<axs->nargs; i++)
915 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
916 break; }
917
918 case AUDIT_SOCKADDR: {
919 struct audit_aux_data_sockaddr *axs = (void *)aux;
920
921 audit_log_format(ab, "saddr=");
922 audit_log_hex(ab, axs->a, axs->len);
923 break; }
01116105
SS
924
925 case AUDIT_AVC_PATH: {
926 struct audit_aux_data_path *axi = (void *)aux;
927 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
01116105
SS
928 break; }
929
1da177e4
LT
930 }
931 audit_log_end(ab);
1da177e4
LT
932 }
933
8f37d47c 934 if (context->pwd && context->pwdmnt) {
ef20c8c1 935 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
8f37d47c
DW
936 if (ab) {
937 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
938 audit_log_end(ab);
939 }
940 }
1da177e4 941 for (i = 0; i < context->name_count; i++) {
73241ccc
AG
942 unsigned long ino = context->names[i].ino;
943 unsigned long pino = context->names[i].pino;
944
ef20c8c1 945 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
1da177e4
LT
946 if (!ab)
947 continue; /* audit_panic has been called */
8f37d47c 948
1da177e4 949 audit_log_format(ab, "item=%d", i);
73241ccc
AG
950
951 audit_log_format(ab, " name=");
952 if (context->names[i].name)
83c7d091 953 audit_log_untrustedstring(ab, context->names[i].name);
73241ccc
AG
954 else
955 audit_log_format(ab, "(null)");
956
957 if (pino != (unsigned long)-1)
958 audit_log_format(ab, " parent=%lu", pino);
959 if (ino != (unsigned long)-1)
960 audit_log_format(ab, " inode=%lu", ino);
961 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
962 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
963 " ouid=%u ogid=%u rdev=%02x:%02x",
964 MAJOR(context->names[i].dev),
965 MINOR(context->names[i].dev),
966 context->names[i].mode,
967 context->names[i].uid,
968 context->names[i].gid,
969 MAJOR(context->names[i].rdev),
1da177e4
LT
970 MINOR(context->names[i].rdev));
971 audit_log_end(ab);
972 }
973}
974
b0dd25a8
RD
975/**
976 * audit_free - free a per-task audit context
977 * @tsk: task whose audit context block to free
978 *
979 * Called from copy_process and __put_task_struct.
980 */
1da177e4
LT
981void audit_free(struct task_struct *tsk)
982{
983 struct audit_context *context;
984
985 task_lock(tsk);
986 context = audit_get_context(tsk, 0, 0);
987 task_unlock(tsk);
988
989 if (likely(!context))
990 return;
991
992 /* Check for system calls that do not go through the exit
f5561964
DW
993 * function (e.g., exit_group), then free context block.
994 * We use GFP_ATOMIC here because we might be doing this
995 * in the context of the idle thread */
f7056d64 996 if (context->in_syscall && context->auditable)
f5561964 997 audit_log_exit(context, GFP_ATOMIC);
1da177e4
LT
998
999 audit_free_context(context);
1000}
1001
b0dd25a8
RD
1002/**
1003 * audit_syscall_entry - fill in an audit record at syscall entry
1004 * @tsk: task being audited
1005 * @arch: architecture type
1006 * @major: major syscall type (function)
1007 * @a1: additional syscall register 1
1008 * @a2: additional syscall register 2
1009 * @a3: additional syscall register 3
1010 * @a4: additional syscall register 4
1011 *
1012 * Fill in audit context at syscall entry. This only happens if the
1da177e4
LT
1013 * audit context was created when the task was created and the state or
1014 * filters demand the audit context be built. If the state from the
1015 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1016 * then the record will be written at syscall exit time (otherwise, it
1017 * will only be written if another part of the kernel requests that it
b0dd25a8
RD
1018 * be written).
1019 */
2fd6f58b 1020void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
1da177e4
LT
1021 unsigned long a1, unsigned long a2,
1022 unsigned long a3, unsigned long a4)
1023{
1024 struct audit_context *context = tsk->audit_context;
1025 enum audit_state state;
1026
1027 BUG_ON(!context);
1028
b0dd25a8
RD
1029 /*
1030 * This happens only on certain architectures that make system
1da177e4
LT
1031 * calls in kernel_thread via the entry.S interface, instead of
1032 * with direct calls. (If you are porting to a new
1033 * architecture, hitting this condition can indicate that you
1034 * got the _exit/_leave calls backward in entry.S.)
1035 *
1036 * i386 no
1037 * x86_64 no
1038 * ppc64 yes (see arch/ppc64/kernel/misc.S)
1039 *
1040 * This also happens with vm86 emulation in a non-nested manner
1041 * (entries without exits), so this case must be caught.
1042 */
1043 if (context->in_syscall) {
1044 struct audit_context *newctx;
1045
1da177e4
LT
1046#if AUDIT_DEBUG
1047 printk(KERN_ERR
1048 "audit(:%d) pid=%d in syscall=%d;"
1049 " entering syscall=%d\n",
1050 context->serial, tsk->pid, context->major, major);
1051#endif
1052 newctx = audit_alloc_context(context->state);
1053 if (newctx) {
1054 newctx->previous = context;
1055 context = newctx;
1056 tsk->audit_context = newctx;
1057 } else {
1058 /* If we can't alloc a new context, the best we
1059 * can do is to leak memory (any pending putname
1060 * will be lost). The only other alternative is
1061 * to abandon auditing. */
1062 audit_zero_context(context, context->state);
1063 }
1064 }
1065 BUG_ON(context->in_syscall || context->name_count);
1066
1067 if (!audit_enabled)
1068 return;
1069
2fd6f58b 1070 context->arch = arch;
1da177e4
LT
1071 context->major = major;
1072 context->argv[0] = a1;
1073 context->argv[1] = a2;
1074 context->argv[2] = a3;
1075 context->argv[3] = a4;
1076
1077 state = context->state;
1078 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
0f45aa18 1079 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1da177e4
LT
1080 if (likely(state == AUDIT_DISABLED))
1081 return;
1082
ce625a80 1083 context->serial = 0;
1da177e4
LT
1084 context->ctime = CURRENT_TIME;
1085 context->in_syscall = 1;
1086 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1087}
1088
b0dd25a8
RD
1089/**
1090 * audit_syscall_exit - deallocate audit context after a system call
1091 * @tsk: task being audited
1092 * @valid: success/failure flag
1093 * @return_code: syscall return value
1094 *
1095 * Tear down after system call. If the audit context has been marked as
1da177e4
LT
1096 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1097 * filtering, or because some other part of the kernel write an audit
1098 * message), then write out the syscall information. In call cases,
b0dd25a8
RD
1099 * free the names stored from getname().
1100 */
2fd6f58b 1101void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
1da177e4
LT
1102{
1103 struct audit_context *context;
1104
1105 get_task_struct(tsk);
1106 task_lock(tsk);
2fd6f58b 1107 context = audit_get_context(tsk, valid, return_code);
1da177e4
LT
1108 task_unlock(tsk);
1109
1110 /* Not having a context here is ok, since the parent may have
1111 * called __put_task_struct. */
1112 if (likely(!context))
413a1c75 1113 goto out;
1da177e4 1114
f7056d64 1115 if (context->in_syscall && context->auditable)
f5561964 1116 audit_log_exit(context, GFP_KERNEL);
1da177e4
LT
1117
1118 context->in_syscall = 0;
1119 context->auditable = 0;
2fd6f58b 1120
1da177e4
LT
1121 if (context->previous) {
1122 struct audit_context *new_context = context->previous;
1123 context->previous = NULL;
1124 audit_free_context(context);
1125 tsk->audit_context = new_context;
1126 } else {
1127 audit_free_names(context);
1128 audit_free_aux(context);
1da177e4
LT
1129 tsk->audit_context = context;
1130 }
413a1c75 1131 out:
1da177e4
LT
1132 put_task_struct(tsk);
1133}
1134
b0dd25a8
RD
1135/**
1136 * audit_getname - add a name to the list
1137 * @name: name to add
1138 *
1139 * Add a name to the list of audit names for this context.
1140 * Called from fs/namei.c:getname().
1141 */
1da177e4
LT
1142void audit_getname(const char *name)
1143{
1144 struct audit_context *context = current->audit_context;
1145
1146 if (!context || IS_ERR(name) || !name)
1147 return;
1148
1149 if (!context->in_syscall) {
1150#if AUDIT_DEBUG == 2
1151 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1152 __FILE__, __LINE__, context->serial, name);
1153 dump_stack();
1154#endif
1155 return;
1156 }
1157 BUG_ON(context->name_count >= AUDIT_NAMES);
1158 context->names[context->name_count].name = name;
1159 context->names[context->name_count].ino = (unsigned long)-1;
1160 ++context->name_count;
8f37d47c
DW
1161 if (!context->pwd) {
1162 read_lock(&current->fs->lock);
1163 context->pwd = dget(current->fs->pwd);
1164 context->pwdmnt = mntget(current->fs->pwdmnt);
1165 read_unlock(&current->fs->lock);
1166 }
1167
1da177e4
LT
1168}
1169
b0dd25a8
RD
1170/* audit_putname - intercept a putname request
1171 * @name: name to intercept and delay for putname
1172 *
1173 * If we have stored the name from getname in the audit context,
1174 * then we delay the putname until syscall exit.
1175 * Called from include/linux/fs.h:putname().
1176 */
1da177e4
LT
1177void audit_putname(const char *name)
1178{
1179 struct audit_context *context = current->audit_context;
1180
1181 BUG_ON(!context);
1182 if (!context->in_syscall) {
1183#if AUDIT_DEBUG == 2
1184 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1185 __FILE__, __LINE__, context->serial, name);
1186 if (context->name_count) {
1187 int i;
1188 for (i = 0; i < context->name_count; i++)
1189 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1190 context->names[i].name,
73241ccc 1191 context->names[i].name ?: "(null)");
1da177e4
LT
1192 }
1193#endif
1194 __putname(name);
1195 }
1196#if AUDIT_DEBUG
1197 else {
1198 ++context->put_count;
1199 if (context->put_count > context->name_count) {
1200 printk(KERN_ERR "%s:%d(:%d): major=%d"
1201 " in_syscall=%d putname(%p) name_count=%d"
1202 " put_count=%d\n",
1203 __FILE__, __LINE__,
1204 context->serial, context->major,
1205 context->in_syscall, name, context->name_count,
1206 context->put_count);
1207 dump_stack();
1208 }
1209 }
1210#endif
1211}
1212
b0dd25a8
RD
1213/**
1214 * audit_inode - store the inode and device from a lookup
1215 * @name: name being audited
1216 * @inode: inode being audited
1217 * @flags: lookup flags (as used in path_lookup())
1218 *
1219 * Called from fs/namei.c:path_lookup().
1220 */
73241ccc 1221void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
1da177e4
LT
1222{
1223 int idx;
1224 struct audit_context *context = current->audit_context;
1225
1226 if (!context->in_syscall)
1227 return;
1228 if (context->name_count
1229 && context->names[context->name_count-1].name
1230 && context->names[context->name_count-1].name == name)
1231 idx = context->name_count - 1;
1232 else if (context->name_count > 1
1233 && context->names[context->name_count-2].name
1234 && context->names[context->name_count-2].name == name)
1235 idx = context->name_count - 2;
1236 else {
1237 /* FIXME: how much do we care about inodes that have no
1238 * associated name? */
1239 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1240 return;
1241 idx = context->name_count++;
1242 context->names[idx].name = NULL;
1243#if AUDIT_DEBUG
1244 ++context->ino_count;
1245#endif
1246 }
ae7b961b
DW
1247 context->names[idx].dev = inode->i_sb->s_dev;
1248 context->names[idx].mode = inode->i_mode;
1249 context->names[idx].uid = inode->i_uid;
1250 context->names[idx].gid = inode->i_gid;
1251 context->names[idx].rdev = inode->i_rdev;
73241ccc
AG
1252 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1253 (strcmp(name, ".") != 0)) {
1254 context->names[idx].ino = (unsigned long)-1;
1255 context->names[idx].pino = inode->i_ino;
1256 } else {
1257 context->names[idx].ino = inode->i_ino;
1258 context->names[idx].pino = (unsigned long)-1;
1259 }
1260}
1261
1262/**
1263 * audit_inode_child - collect inode info for created/removed objects
1264 * @dname: inode's dentry name
1265 * @inode: inode being audited
1266 * @pino: inode number of dentry parent
1267 *
1268 * For syscalls that create or remove filesystem objects, audit_inode
1269 * can only collect information for the filesystem object's parent.
1270 * This call updates the audit context with the child's information.
1271 * Syscalls that create a new filesystem object must be hooked after
1272 * the object is created. Syscalls that remove a filesystem object
1273 * must be hooked prior, in order to capture the target inode during
1274 * unsuccessful attempts.
1275 */
1276void __audit_inode_child(const char *dname, const struct inode *inode,
1277 unsigned long pino)
1278{
1279 int idx;
1280 struct audit_context *context = current->audit_context;
1281
1282 if (!context->in_syscall)
1283 return;
1284
1285 /* determine matching parent */
1286 if (dname)
1287 for (idx = 0; idx < context->name_count; idx++)
1288 if (context->names[idx].pino == pino) {
1289 const char *n;
1290 const char *name = context->names[idx].name;
1291 int dlen = strlen(dname);
1292 int nlen = name ? strlen(name) : 0;
1293
1294 if (nlen < dlen)
1295 continue;
1296
1297 /* disregard trailing slashes */
1298 n = name + nlen - 1;
1299 while ((*n == '/') && (n > name))
1300 n--;
1301
1302 /* find last path component */
1303 n = n - dlen + 1;
1304 if (n < name)
1305 continue;
1306 else if (n > name) {
1307 if (*--n != '/')
1308 continue;
1309 else
1310 n++;
1311 }
1312
1313 if (strncmp(n, dname, dlen) == 0)
1314 goto update_context;
1315 }
1316
1317 /* catch-all in case match not found */
1318 idx = context->name_count++;
1319 context->names[idx].name = NULL;
1320 context->names[idx].pino = pino;
1321#if AUDIT_DEBUG
1322 context->ino_count++;
1323#endif
1324
1325update_context:
1326 if (inode) {
1327 context->names[idx].ino = inode->i_ino;
1328 context->names[idx].dev = inode->i_sb->s_dev;
1329 context->names[idx].mode = inode->i_mode;
1330 context->names[idx].uid = inode->i_uid;
1331 context->names[idx].gid = inode->i_gid;
1332 context->names[idx].rdev = inode->i_rdev;
1333 }
1da177e4
LT
1334}
1335
b0dd25a8
RD
1336/**
1337 * auditsc_get_stamp - get local copies of audit_context values
1338 * @ctx: audit_context for the task
1339 * @t: timespec to store time recorded in the audit_context
1340 * @serial: serial value that is recorded in the audit_context
1341 *
1342 * Also sets the context as auditable.
1343 */
bfb4496e
DW
1344void auditsc_get_stamp(struct audit_context *ctx,
1345 struct timespec *t, unsigned int *serial)
1da177e4 1346{
ce625a80
DW
1347 if (!ctx->serial)
1348 ctx->serial = audit_serial();
bfb4496e
DW
1349 t->tv_sec = ctx->ctime.tv_sec;
1350 t->tv_nsec = ctx->ctime.tv_nsec;
1351 *serial = ctx->serial;
1352 ctx->auditable = 1;
1da177e4
LT
1353}
1354
b0dd25a8
RD
1355/**
1356 * audit_set_loginuid - set a task's audit_context loginuid
1357 * @task: task whose audit context is being modified
1358 * @loginuid: loginuid value
1359 *
1360 * Returns 0.
1361 *
1362 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1363 */
456be6cd 1364int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1da177e4 1365{
456be6cd 1366 if (task->audit_context) {
c0404993
SG
1367 struct audit_buffer *ab;
1368
9ad9ad38 1369 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
c0404993
SG
1370 if (ab) {
1371 audit_log_format(ab, "login pid=%d uid=%u "
326e9c8b 1372 "old auid=%u new auid=%u",
c0404993
SG
1373 task->pid, task->uid,
1374 task->audit_context->loginuid, loginuid);
1375 audit_log_end(ab);
1376 }
456be6cd 1377 task->audit_context->loginuid = loginuid;
1da177e4
LT
1378 }
1379 return 0;
1380}
1381
b0dd25a8
RD
1382/**
1383 * audit_get_loginuid - get the loginuid for an audit_context
1384 * @ctx: the audit_context
1385 *
1386 * Returns the context's loginuid or -1 if @ctx is NULL.
1387 */
1da177e4
LT
1388uid_t audit_get_loginuid(struct audit_context *ctx)
1389{
1390 return ctx ? ctx->loginuid : -1;
1391}
1392
b0dd25a8
RD
1393/**
1394 * audit_ipc_perms - record audit data for ipc
1395 * @qbytes: msgq bytes
1396 * @uid: msgq user id
1397 * @gid: msgq group id
1398 * @mode: msgq mode (permissions)
1399 *
1400 * Returns 0 for success or NULL context or < 0 on error.
1401 */
1da177e4
LT
1402int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1403{
1404 struct audit_aux_data_ipcctl *ax;
1405 struct audit_context *context = current->audit_context;
1406
1407 if (likely(!context))
1408 return 0;
1409
1410 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1411 if (!ax)
1412 return -ENOMEM;
1413
1414 ax->qbytes = qbytes;
1415 ax->uid = uid;
1416 ax->gid = gid;
1417 ax->mode = mode;
1418
c0404993 1419 ax->d.type = AUDIT_IPC;
1da177e4
LT
1420 ax->d.next = context->aux;
1421 context->aux = (void *)ax;
1422 return 0;
1423}
c2f0c7c3 1424
b0dd25a8
RD
1425/**
1426 * audit_socketcall - record audit data for sys_socketcall
1427 * @nargs: number of args
1428 * @args: args array
1429 *
1430 * Returns 0 for success or NULL context or < 0 on error.
1431 */
3ec3b2fb
DW
1432int audit_socketcall(int nargs, unsigned long *args)
1433{
1434 struct audit_aux_data_socketcall *ax;
1435 struct audit_context *context = current->audit_context;
1436
1437 if (likely(!context))
1438 return 0;
1439
1440 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1441 if (!ax)
1442 return -ENOMEM;
1443
1444 ax->nargs = nargs;
1445 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1446
1447 ax->d.type = AUDIT_SOCKETCALL;
1448 ax->d.next = context->aux;
1449 context->aux = (void *)ax;
1450 return 0;
1451}
1452
b0dd25a8
RD
1453/**
1454 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1455 * @len: data length in user space
1456 * @a: data address in kernel space
1457 *
1458 * Returns 0 for success or NULL context or < 0 on error.
1459 */
3ec3b2fb
DW
1460int audit_sockaddr(int len, void *a)
1461{
1462 struct audit_aux_data_sockaddr *ax;
1463 struct audit_context *context = current->audit_context;
1464
1465 if (likely(!context))
1466 return 0;
1467
1468 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1469 if (!ax)
1470 return -ENOMEM;
1471
1472 ax->len = len;
1473 memcpy(ax->a, a, len);
1474
1475 ax->d.type = AUDIT_SOCKADDR;
1476 ax->d.next = context->aux;
1477 context->aux = (void *)ax;
1478 return 0;
1479}
1480
b0dd25a8
RD
1481/**
1482 * audit_avc_path - record the granting or denial of permissions
1483 * @dentry: dentry to record
1484 * @mnt: mnt to record
1485 *
1486 * Returns 0 for success or NULL context or < 0 on error.
1487 *
1488 * Called from security/selinux/avc.c::avc_audit()
1489 */
01116105
SS
1490int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1491{
1492 struct audit_aux_data_path *ax;
1493 struct audit_context *context = current->audit_context;
1494
1495 if (likely(!context))
1496 return 0;
1497
1498 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1499 if (!ax)
1500 return -ENOMEM;
1501
1502 ax->dentry = dget(dentry);
1503 ax->mnt = mntget(mnt);
1504
1505 ax->d.type = AUDIT_AVC_PATH;
1506 ax->d.next = context->aux;
1507 context->aux = (void *)ax;
1508 return 0;
1509}
1510
b0dd25a8
RD
1511/**
1512 * audit_signal_info - record signal info for shutting down audit subsystem
1513 * @sig: signal value
1514 * @t: task being signaled
1515 *
1516 * If the audit subsystem is being terminated, record the task (pid)
1517 * and uid that is doing that.
1518 */
c2f0c7c3
SG
1519void audit_signal_info(int sig, struct task_struct *t)
1520{
1521 extern pid_t audit_sig_pid;
1522 extern uid_t audit_sig_uid;
c2f0c7c3 1523
582edda5 1524 if (unlikely(audit_pid && t->tgid == audit_pid)) {
c2f0c7c3
SG
1525 if (sig == SIGTERM || sig == SIGHUP) {
1526 struct audit_context *ctx = current->audit_context;
1527 audit_sig_pid = current->pid;
1528 if (ctx)
1529 audit_sig_uid = ctx->loginuid;
1530 else
1531 audit_sig_uid = current->uid;
1532 }
1533 }
1534}