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