blob: 95076fa12202d3b68fd134f9025ab2886b7e5517 [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* auditsc.c -- System-call auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handles all system-call specific auditing features.
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
Dustin Kirklandb63862f2005-11-03 15:41:46 +00005 * Copyright (C) 2005 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * All Rights Reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
23 *
24 * Many of the ideas implemented here are from Stephen C. Tweedie,
25 * especially the idea of avoiding a copy by using getname.
26 *
27 * The method for actual interception of syscall entry and exit (not in
28 * this file -- see entry.S) is based on a GPL'd patch written by
29 * okir@suse.de and Copyright 2003 SuSE Linux AG.
30 *
Dustin Kirklandb63862f2005-11-03 15:41:46 +000031 * The support of additional filter rules compares (>, <, >=, <=) was
32 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
33 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
36#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/types.h>
Alan Cox715b49e2006-01-18 17:44:07 -080038#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/mm.h>
40#include <linux/module.h>
Stephen Smalley01116102005-05-21 00:15:52 +010041#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010042#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/audit.h>
44#include <linux/personality.h>
45#include <linux/time.h>
David Woodhousef6a789d2005-06-21 16:22:01 +010046#include <linux/kthread.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010047#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010048#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/unistd.h>
50
51/* 0 = no checking
52 1 = put_count checking
53 2 = verbose put_count checking
54*/
55#define AUDIT_DEBUG 0
56
57/* No syscall auditing will take place unless audit_enabled != 0. */
58extern int audit_enabled;
59
60/* AUDIT_NAMES is the number of slots we reserve in the audit_context
61 * for saving names from getname(). */
62#define AUDIT_NAMES 20
63
64/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
65 * audit_context from being used for nameless inodes from
66 * path_lookup. */
67#define AUDIT_NAMES_RESERVED 7
68
69/* At task start time, the audit_state is set in the audit_context using
70 a per-task filter. At syscall entry, the audit_state is augmented by
71 the syscall filter. */
72enum audit_state {
73 AUDIT_DISABLED, /* Do not create per-task audit_context.
74 * No syscall-specific audit records can
75 * be generated. */
76 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
77 * but don't necessarily fill it in at
78 * syscall entry time (i.e., filter
79 * instead). */
80 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
81 * and always fill it in at syscall
82 * entry time. This makes a full
83 * syscall record available if some
84 * other part of the kernel decides it
85 * should be recorded. */
86 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
87 * always fill it in at syscall entry
88 * time, and always write out the audit
89 * record at syscall exit time. */
90};
91
92/* When fs/namei.c:getname() is called, we store the pointer in name and
93 * we don't let putname() free it (instead we free all of the saved
94 * pointers at syscall exit time).
95 *
96 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
97struct audit_names {
98 const char *name;
99 unsigned long ino;
100 dev_t dev;
101 umode_t mode;
102 uid_t uid;
103 gid_t gid;
104 dev_t rdev;
David Woodhouseae7b9612005-06-20 16:11:05 +0100105 unsigned flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108struct audit_aux_data {
109 struct audit_aux_data *next;
110 int type;
111};
112
113#define AUDIT_AUX_IPCPERM 0
114
115struct audit_aux_data_ipcctl {
116 struct audit_aux_data d;
117 struct ipc_perm p;
118 unsigned long qbytes;
119 uid_t uid;
120 gid_t gid;
121 mode_t mode;
122};
123
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100124struct audit_aux_data_socketcall {
125 struct audit_aux_data d;
126 int nargs;
127 unsigned long args[0];
128};
129
130struct audit_aux_data_sockaddr {
131 struct audit_aux_data d;
132 int len;
133 char a[0];
134};
135
Stephen Smalley01116102005-05-21 00:15:52 +0100136struct audit_aux_data_path {
137 struct audit_aux_data d;
138 struct dentry *dentry;
139 struct vfsmount *mnt;
140};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142/* The per-task audit context. */
143struct audit_context {
144 int in_syscall; /* 1 if task is in a syscall */
145 enum audit_state state;
146 unsigned int serial; /* serial number for record */
147 struct timespec ctime; /* time of syscall entry */
148 uid_t loginuid; /* login uid (identity) */
149 int major; /* syscall number */
150 unsigned long argv[4]; /* syscall arguments */
151 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100152 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int auditable; /* 1 if record should be written */
154 int name_count;
155 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100156 struct dentry * pwd;
157 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct audit_context *previous; /* For nested syscalls */
159 struct audit_aux_data *aux;
160
161 /* Save things to print about task_struct */
162 pid_t pid;
163 uid_t uid, euid, suid, fsuid;
164 gid_t gid, egid, sgid, fsgid;
165 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100166 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168#if AUDIT_DEBUG
169 int put_count;
170 int ino_count;
171#endif
172};
173
174 /* Public API */
175/* There are three lists of rules -- one to search at task creation
176 * time, one to search at syscall entry time, and another to search at
177 * syscall exit time. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100178static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
179 LIST_HEAD_INIT(audit_filter_list[0]),
180 LIST_HEAD_INIT(audit_filter_list[1]),
181 LIST_HEAD_INIT(audit_filter_list[2]),
182 LIST_HEAD_INIT(audit_filter_list[3]),
183 LIST_HEAD_INIT(audit_filter_list[4]),
184#if AUDIT_NR_FILTERS != 5
185#error Fix audit_filter_list initialiser
186#endif
187};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189struct audit_entry {
190 struct list_head list;
191 struct rcu_head rcu;
192 struct audit_rule rule;
193};
194
David Woodhouse7ca00262005-05-19 11:23:13 +0100195extern int audit_pid;
196
Amy Griffis3c789a12005-08-17 16:05:35 +0100197/* Copy rule from user-space to kernel-space. Called from
198 * audit_add_rule during AUDIT_ADD. */
199static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
200{
201 int i;
202
203 if (s->action != AUDIT_NEVER
204 && s->action != AUDIT_POSSIBLE
205 && s->action != AUDIT_ALWAYS)
206 return -1;
207 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
208 return -1;
209 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
210 return -1;
211
212 d->flags = s->flags;
213 d->action = s->action;
214 d->field_count = s->field_count;
215 for (i = 0; i < d->field_count; i++) {
216 d->fields[i] = s->fields[i];
217 d->values[i] = s->values[i];
218 }
219 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
220 return 0;
221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/* Check to see if two rules are identical. It is called from
Amy Griffis3c789a12005-08-17 16:05:35 +0100224 * audit_add_rule during AUDIT_ADD and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * audit_del_rule during AUDIT_DEL. */
Amy Griffis3c789a12005-08-17 16:05:35 +0100226static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 int i;
229
230 if (a->flags != b->flags)
231 return 1;
232
233 if (a->action != b->action)
234 return 1;
235
236 if (a->field_count != b->field_count)
237 return 1;
238
239 for (i = 0; i < a->field_count; i++) {
240 if (a->fields[i] != b->fields[i]
241 || a->values[i] != b->values[i])
242 return 1;
243 }
244
245 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
246 if (a->mask[i] != b->mask[i])
247 return 1;
248
249 return 0;
250}
251
252/* Note that audit_add_rule and audit_del_rule are called via
253 * audit_receive() in audit.c, and are protected by
254 * audit_netlink_sem. */
Amy Griffis3c789a12005-08-17 16:05:35 +0100255static inline int audit_add_rule(struct audit_rule *rule,
David Woodhouse0f45aa12005-06-19 19:35:50 +0100256 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Amy Griffis3c789a12005-08-17 16:05:35 +0100258 struct audit_entry *entry;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000259 int i;
Amy Griffis3c789a12005-08-17 16:05:35 +0100260
261 /* Do not use the _rcu iterator here, since this is the only
262 * addition routine. */
263 list_for_each_entry(entry, list, list) {
264 if (!audit_compare_rule(rule, &entry->rule)) {
265 return -EEXIST;
266 }
267 }
268
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000269 for (i = 0; i < rule->field_count; i++) {
270 if (rule->fields[i] & AUDIT_UNUSED_BITS)
271 return -EINVAL;
272 if ( rule->fields[i] & AUDIT_NEGATE )
273 rule->fields[i] |= AUDIT_NOT_EQUAL;
274 else if ( (rule->fields[i] & AUDIT_OPERATORS) == 0 )
275 rule->fields[i] |= AUDIT_EQUAL;
276 rule->fields[i] &= (~AUDIT_NEGATE);
277 }
278
Amy Griffis3c789a12005-08-17 16:05:35 +0100279 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
280 return -ENOMEM;
281 if (audit_copy_rule(&entry->rule, rule)) {
282 kfree(entry);
283 return -EINVAL;
284 }
285
David Woodhouse0f45aa12005-06-19 19:35:50 +0100286 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
287 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 list_add_rcu(&entry->list, list);
289 } else {
290 list_add_tail_rcu(&entry->list, list);
291 }
Amy Griffis3c789a12005-08-17 16:05:35 +0100292
293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Amy Griffis3c789a12005-08-17 16:05:35 +0100296static inline void audit_free_rule(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
299 kfree(e);
300}
301
302/* Note that audit_add_rule and audit_del_rule are called via
303 * audit_receive() in audit.c, and are protected by
304 * audit_netlink_sem. */
305static inline int audit_del_rule(struct audit_rule *rule,
306 struct list_head *list)
307{
308 struct audit_entry *e;
309
310 /* Do not use the _rcu iterator here, since this is the only
311 * deletion routine. */
312 list_for_each_entry(e, list, list) {
313 if (!audit_compare_rule(rule, &e->rule)) {
314 list_del_rcu(&e->list);
315 call_rcu(&e->rcu, audit_free_rule);
316 return 0;
317 }
318 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100319 return -ENOENT; /* No matching rule */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
David Woodhousef6a789d2005-06-21 16:22:01 +0100322static int audit_list_rules(void *_dest)
323{
324 int pid, seq;
325 int *dest = _dest;
326 struct audit_entry *entry;
327 int i;
328
329 pid = dest[0];
330 seq = dest[1];
331 kfree(dest);
332
333 down(&audit_netlink_sem);
334
335 /* The *_rcu iterators not needed here because we are
336 always called with audit_netlink_sem held. */
337 for (i=0; i<AUDIT_NR_FILTERS; i++) {
338 list_for_each_entry(entry, &audit_filter_list[i], list)
339 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
340 &entry->rule, sizeof(entry->rule));
341 }
342 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
343
344 up(&audit_netlink_sem);
345 return 0;
346}
347
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700348/**
349 * audit_receive_filter - apply all rules to the specified message type
350 * @type: audit message type
351 * @pid: target pid for netlink audit messages
352 * @uid: target uid for netlink audit messages
353 * @seq: netlink audit message sequence (serial) number
354 * @data: payload data
355 * @loginuid: loginuid of sender
356 */
Serge Hallync94c2572005-04-29 16:27:17 +0100357int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
358 uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
David Woodhousef6a789d2005-06-21 16:22:01 +0100360 struct task_struct *tsk;
361 int *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int err = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100363 unsigned listnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 switch (type) {
366 case AUDIT_LIST:
David Woodhousef6a789d2005-06-21 16:22:01 +0100367 /* We can't just spew out the rules here because we might fill
368 * the available socket buffer space and deadlock waiting for
369 * auditctl to read from it... which isn't ever going to
370 * happen if we're actually running in the context of auditctl
371 * trying to _send_ the stuff */
372
373 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
374 if (!dest)
375 return -ENOMEM;
376 dest[0] = pid;
377 dest[1] = seq;
378
379 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
380 if (IS_ERR(tsk)) {
381 kfree(dest);
382 err = PTR_ERR(tsk);
David Woodhouse0f45aa12005-06-19 19:35:50 +0100383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
385 case AUDIT_ADD:
Amy Griffis3c789a12005-08-17 16:05:35 +0100386 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
387 if (listnr >= AUDIT_NR_FILTERS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return -EINVAL;
Amy Griffis3c789a12005-08-17 16:05:35 +0100389
390 err = audit_add_rule(data, &audit_filter_list[listnr]);
391 if (!err)
392 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
393 "auid=%u added an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 break;
395 case AUDIT_DEL:
David Woodhouse0f45aa12005-06-19 19:35:50 +0100396 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
397 if (listnr >= AUDIT_NR_FILTERS)
398 return -EINVAL;
399
400 err = audit_del_rule(data, &audit_filter_list[listnr]);
401 if (!err)
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100402 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhouse0f45aa12005-06-19 19:35:50 +0100403 "auid=%u removed an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
405 default:
406 return -EINVAL;
407 }
408
409 return err;
410}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000412static int audit_comparator(const u32 left, const u32 op, const u32 right)
413{
414 switch (op) {
415 case AUDIT_EQUAL:
416 return (left == right);
417 case AUDIT_NOT_EQUAL:
418 return (left != right);
419 case AUDIT_LESS_THAN:
420 return (left < right);
421 case AUDIT_LESS_THAN_OR_EQUAL:
422 return (left <= right);
423 case AUDIT_GREATER_THAN:
424 return (left > right);
425 case AUDIT_GREATER_THAN_OR_EQUAL:
426 return (left >= right);
427 default:
428 return -EINVAL;
429 }
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/* Compare a task_struct with an audit_rule. Return 1 on match, 0
433 * otherwise. */
434static int audit_filter_rules(struct task_struct *tsk,
435 struct audit_rule *rule,
436 struct audit_context *ctx,
437 enum audit_state *state)
438{
439 int i, j;
440
441 for (i = 0; i < rule->field_count; i++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000442 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
443 u32 op = rule->fields[i] & AUDIT_OPERATORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 u32 value = rule->values[i];
445 int result = 0;
446
447 switch (field) {
448 case AUDIT_PID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000449 result = audit_comparator(tsk->pid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 case AUDIT_UID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000452 result = audit_comparator(tsk->uid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454 case AUDIT_EUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000455 result = audit_comparator(tsk->euid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
457 case AUDIT_SUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000458 result = audit_comparator(tsk->suid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460 case AUDIT_FSUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000461 result = audit_comparator(tsk->fsuid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
463 case AUDIT_GID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000464 result = audit_comparator(tsk->gid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 case AUDIT_EGID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000467 result = audit_comparator(tsk->egid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469 case AUDIT_SGID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000470 result = audit_comparator(tsk->sgid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 case AUDIT_FSGID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000473 result = audit_comparator(tsk->fsgid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case AUDIT_PERS:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000476 result = audit_comparator(tsk->personality, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
2fd6f582005-04-29 16:08:28 +0100478 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000479 if (ctx)
480 result = audit_comparator(ctx->arch, op, value);
2fd6f582005-04-29 16:08:28 +0100481 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 case AUDIT_EXIT:
484 if (ctx && ctx->return_valid)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000485 result = audit_comparator(ctx->return_code, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100488 if (ctx && ctx->return_valid) {
489 if (value)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000490 result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100491 else
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000492 result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 break;
495 case AUDIT_DEVMAJOR:
496 if (ctx) {
497 for (j = 0; j < ctx->name_count; j++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000498 if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 ++result;
500 break;
501 }
502 }
503 }
504 break;
505 case AUDIT_DEVMINOR:
506 if (ctx) {
507 for (j = 0; j < ctx->name_count; j++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000508 if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 ++result;
510 break;
511 }
512 }
513 }
514 break;
515 case AUDIT_INODE:
516 if (ctx) {
517 for (j = 0; j < ctx->name_count; j++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000518 if (audit_comparator(ctx->names[j].ino, op, value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ++result;
520 break;
521 }
522 }
523 }
524 break;
525 case AUDIT_LOGINUID:
526 result = 0;
527 if (ctx)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000528 result = audit_comparator(ctx->loginuid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
530 case AUDIT_ARG0:
531 case AUDIT_ARG1:
532 case AUDIT_ARG2:
533 case AUDIT_ARG3:
534 if (ctx)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000535 result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 break;
537 }
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!result)
540 return 0;
541 }
542 switch (rule->action) {
543 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
544 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
545 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
546 }
547 return 1;
548}
549
550/* At process creation time, we can determine if system-call auditing is
551 * completely disabled for this task. Since we only have the task
552 * structure at this point, we can only check uid and gid.
553 */
554static enum audit_state audit_filter_task(struct task_struct *tsk)
555{
556 struct audit_entry *e;
557 enum audit_state state;
558
559 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100560 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
562 rcu_read_unlock();
563 return state;
564 }
565 }
566 rcu_read_unlock();
567 return AUDIT_BUILD_CONTEXT;
568}
569
570/* At syscall entry and exit time, this filter is called if the
571 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100572 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700573 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
575static enum audit_state audit_filter_syscall(struct task_struct *tsk,
576 struct audit_context *ctx,
577 struct list_head *list)
578{
579 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100580 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
David Woodhouse351bb722005-07-14 14:40:06 +0100582 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100583 return AUDIT_DISABLED;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100586 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000587 int word = AUDIT_WORD(ctx->major);
588 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100589
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000590 list_for_each_entry_rcu(e, list, list) {
591 if ((e->rule.mask[word] & bit) == bit
592 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
593 rcu_read_unlock();
594 return state;
595 }
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 rcu_read_unlock();
599 return AUDIT_BUILD_CONTEXT;
600}
601
David Woodhouse5bb289b2005-06-24 14:14:05 +0100602static int audit_filter_user_rules(struct netlink_skb_parms *cb,
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000603 struct audit_rule *rule,
604 enum audit_state *state)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100605{
David Woodhouse5bb289b2005-06-24 14:14:05 +0100606 int i;
607
608 for (i = 0; i < rule->field_count; i++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000609 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
610 u32 op = rule->fields[i] & AUDIT_OPERATORS;
David Woodhouse5bb289b2005-06-24 14:14:05 +0100611 u32 value = rule->values[i];
612 int result = 0;
613
614 switch (field) {
615 case AUDIT_PID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000616 result = audit_comparator(cb->creds.pid, op, value);
David Woodhouse5bb289b2005-06-24 14:14:05 +0100617 break;
618 case AUDIT_UID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000619 result = audit_comparator(cb->creds.uid, op, value);
David Woodhouse5bb289b2005-06-24 14:14:05 +0100620 break;
621 case AUDIT_GID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000622 result = audit_comparator(cb->creds.gid, op, value);
David Woodhouse5bb289b2005-06-24 14:14:05 +0100623 break;
624 case AUDIT_LOGINUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000625 result = audit_comparator(cb->loginuid, op, value);
David Woodhouse5bb289b2005-06-24 14:14:05 +0100626 break;
627 }
628
David Woodhouse5bb289b2005-06-24 14:14:05 +0100629 if (!result)
630 return 0;
631 }
632 switch (rule->action) {
633 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
634 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
635 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
636 }
637 return 1;
638}
639
640int audit_filter_user(struct netlink_skb_parms *cb, int type)
641{
David Woodhouse0f45aa12005-06-19 19:35:50 +0100642 struct audit_entry *e;
643 enum audit_state state;
David Woodhouse4a4cd632005-06-22 14:56:47 +0100644 int ret = 1;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100645
646 rcu_read_lock();
647 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
David Woodhouse5bb289b2005-06-24 14:14:05 +0100648 if (audit_filter_user_rules(cb, &e->rule, &state)) {
David Woodhouse4a4cd632005-06-22 14:56:47 +0100649 if (state == AUDIT_DISABLED)
650 ret = 0;
651 break;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100652 }
653 }
654 rcu_read_unlock();
David Woodhouse4a4cd632005-06-22 14:56:47 +0100655
David Woodhouse993e2d42005-06-24 08:21:49 +0100656 return ret; /* Audit by default */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/* This should be called with task_lock() held. */
660static inline struct audit_context *audit_get_context(struct task_struct *tsk,
661 int return_valid,
662 int return_code)
663{
664 struct audit_context *context = tsk->audit_context;
665
666 if (likely(!context))
667 return NULL;
668 context->return_valid = return_valid;
669 context->return_code = return_code;
670
David Woodhouse21af6c42005-07-02 14:10:46 +0100671 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100673 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (state == AUDIT_RECORD_CONTEXT)
675 context->auditable = 1;
676 }
677
678 context->pid = tsk->pid;
679 context->uid = tsk->uid;
680 context->gid = tsk->gid;
681 context->euid = tsk->euid;
682 context->suid = tsk->suid;
683 context->fsuid = tsk->fsuid;
684 context->egid = tsk->egid;
685 context->sgid = tsk->sgid;
686 context->fsgid = tsk->fsgid;
687 context->personality = tsk->personality;
688 tsk->audit_context = NULL;
689 return context;
690}
691
692static inline void audit_free_names(struct audit_context *context)
693{
694 int i;
695
696#if AUDIT_DEBUG == 2
697 if (context->auditable
698 ||context->put_count + context->ino_count != context->name_count) {
699 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
700 " name_count=%d put_count=%d"
701 " ino_count=%d [NOT freeing]\n",
702 __LINE__,
703 context->serial, context->major, context->in_syscall,
704 context->name_count, context->put_count,
705 context->ino_count);
706 for (i = 0; i < context->name_count; i++)
707 printk(KERN_ERR "names[%d] = %p = %s\n", i,
708 context->names[i].name,
709 context->names[i].name);
710 dump_stack();
711 return;
712 }
713#endif
714#if AUDIT_DEBUG
715 context->put_count = 0;
716 context->ino_count = 0;
717#endif
718
719 for (i = 0; i < context->name_count; i++)
720 if (context->names[i].name)
721 __putname(context->names[i].name);
722 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100723 if (context->pwd)
724 dput(context->pwd);
725 if (context->pwdmnt)
726 mntput(context->pwdmnt);
727 context->pwd = NULL;
728 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731static inline void audit_free_aux(struct audit_context *context)
732{
733 struct audit_aux_data *aux;
734
735 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100736 if (aux->type == AUDIT_AVC_PATH) {
737 struct audit_aux_data_path *axi = (void *)aux;
738 dput(axi->dentry);
739 mntput(axi->mnt);
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 context->aux = aux->next;
742 kfree(aux);
743 }
744}
745
746static inline void audit_zero_context(struct audit_context *context,
747 enum audit_state state)
748{
749 uid_t loginuid = context->loginuid;
750
751 memset(context, 0, sizeof(*context));
752 context->state = state;
753 context->loginuid = loginuid;
754}
755
756static inline struct audit_context *audit_alloc_context(enum audit_state state)
757{
758 struct audit_context *context;
759
760 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
761 return NULL;
762 audit_zero_context(context, state);
763 return context;
764}
765
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700766/**
767 * audit_alloc - allocate an audit context block for a task
768 * @tsk: task
769 *
770 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * if necessary. Doing so turns on system call auditing for the
772 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700773 * needed.
774 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775int audit_alloc(struct task_struct *tsk)
776{
777 struct audit_context *context;
778 enum audit_state state;
779
780 if (likely(!audit_enabled))
781 return 0; /* Return if not auditing. */
782
783 state = audit_filter_task(tsk);
784 if (likely(state == AUDIT_DISABLED))
785 return 0;
786
787 if (!(context = audit_alloc_context(state))) {
788 audit_log_lost("out of memory in audit_alloc");
789 return -ENOMEM;
790 }
791
792 /* Preserve login uid */
793 context->loginuid = -1;
794 if (current->audit_context)
795 context->loginuid = current->audit_context->loginuid;
796
797 tsk->audit_context = context;
798 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
799 return 0;
800}
801
802static inline void audit_free_context(struct audit_context *context)
803{
804 struct audit_context *previous;
805 int count = 0;
806
807 do {
808 previous = context->previous;
809 if (previous || (count && count < 10)) {
810 ++count;
811 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
812 " freeing multiple contexts (%d)\n",
813 context->serial, context->major,
814 context->name_count, count);
815 }
816 audit_free_names(context);
817 audit_free_aux(context);
818 kfree(context);
819 context = previous;
820 } while (context);
821 if (count >= 10)
822 printk(KERN_ERR "audit: freed %d contexts\n", count);
823}
824
Stephen Smalley219f0812005-04-18 10:47:35 -0700825static void audit_log_task_info(struct audit_buffer *ab)
826{
827 char name[sizeof(current->comm)];
828 struct mm_struct *mm = current->mm;
829 struct vm_area_struct *vma;
830
831 get_task_comm(name, current);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100832 audit_log_format(ab, " comm=");
833 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700834
835 if (!mm)
836 return;
837
838 down_read(&mm->mmap_sem);
839 vma = mm->mmap;
840 while (vma) {
841 if ((vma->vm_flags & VM_EXECUTABLE) &&
842 vma->vm_file) {
843 audit_log_d_path(ab, "exe=",
844 vma->vm_file->f_dentry,
845 vma->vm_file->f_vfsmnt);
846 break;
847 }
848 vma = vma->vm_next;
849 }
850 up_read(&mm->mmap_sem);
851}
852
Al Viro9796fdd2005-10-21 03:22:03 -0400853static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 int i;
856 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100857 struct audit_aux_data *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
David Woodhousef5561962005-07-13 22:47:07 +0100859 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (!ab)
861 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100862 audit_log_format(ab, "arch=%x syscall=%d",
863 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (context->personality != PER_LINUX)
865 audit_log_format(ab, " per=%lx", context->personality);
866 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100867 audit_log_format(ab, " success=%s exit=%ld",
868 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
869 context->return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 audit_log_format(ab,
871 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100872 " pid=%d auid=%u uid=%u gid=%u"
873 " euid=%u suid=%u fsuid=%u"
874 " egid=%u sgid=%u fsgid=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 context->argv[0],
876 context->argv[1],
877 context->argv[2],
878 context->argv[3],
879 context->name_count,
880 context->pid,
881 context->loginuid,
882 context->uid,
883 context->gid,
884 context->euid, context->suid, context->fsuid,
885 context->egid, context->sgid, context->fsgid);
Stephen Smalley219f0812005-04-18 10:47:35 -0700886 audit_log_task_info(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
David Woodhouse7551ced2005-05-26 12:04:57 +0100889 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100890
Al Viroef20c8c2006-02-18 15:41:50 -0500891 ab = audit_log_start(context, gfp_mask, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (!ab)
893 continue; /* audit_panic has been called */
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100896 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct audit_aux_data_ipcctl *axi = (void *)aux;
898 audit_log_format(ab,
Steve Grubb326e9c82005-05-21 00:22:31 +0100899 " qbytes=%lx iuid=%u igid=%u mode=%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 axi->qbytes, axi->uid, axi->gid, axi->mode);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100901 break; }
902
903 case AUDIT_SOCKETCALL: {
904 int i;
905 struct audit_aux_data_socketcall *axs = (void *)aux;
906 audit_log_format(ab, "nargs=%d", axs->nargs);
907 for (i=0; i<axs->nargs; i++)
908 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
909 break; }
910
911 case AUDIT_SOCKADDR: {
912 struct audit_aux_data_sockaddr *axs = (void *)aux;
913
914 audit_log_format(ab, "saddr=");
915 audit_log_hex(ab, axs->a, axs->len);
916 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100917
918 case AUDIT_AVC_PATH: {
919 struct audit_aux_data_path *axi = (void *)aux;
920 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100921 break; }
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
David Woodhouse8f37d472005-05-27 12:17:28 +0100927 if (context->pwd && context->pwdmnt) {
Al Viroef20c8c2006-02-18 15:41:50 -0500928 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100929 if (ab) {
930 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
931 audit_log_end(ab);
932 }
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 for (i = 0; i < context->name_count; i++) {
Al Viroef20c8c2006-02-18 15:41:50 -0500935 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (!ab)
937 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 audit_log_format(ab, "item=%d", i);
83c7d092005-04-29 15:54:44 +0100940 if (context->names[i].name) {
941 audit_log_format(ab, " name=");
942 audit_log_untrustedstring(ab, context->names[i].name);
943 }
David Woodhouseae7b9612005-06-20 16:11:05 +0100944 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (context->names[i].ino != (unsigned long)-1)
947 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
Steve Grubb326e9c82005-05-21 00:22:31 +0100948 " ouid=%u ogid=%u rdev=%02x:%02x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 context->names[i].ino,
950 MAJOR(context->names[i].dev),
951 MINOR(context->names[i].dev),
952 context->names[i].mode,
953 context->names[i].uid,
954 context->names[i].gid,
955 MAJOR(context->names[i].rdev),
956 MINOR(context->names[i].rdev));
957 audit_log_end(ab);
958 }
959}
960
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700961/**
962 * audit_free - free a per-task audit context
963 * @tsk: task whose audit context block to free
964 *
965 * Called from copy_process and __put_task_struct.
966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967void audit_free(struct task_struct *tsk)
968{
969 struct audit_context *context;
970
971 task_lock(tsk);
972 context = audit_get_context(tsk, 0, 0);
973 task_unlock(tsk);
974
975 if (likely(!context))
976 return;
977
978 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100979 * function (e.g., exit_group), then free context block.
980 * We use GFP_ATOMIC here because we might be doing this
981 * in the context of the idle thread */
David Woodhousef7056d62005-06-20 16:07:33 +0100982 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +0100983 audit_log_exit(context, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 audit_free_context(context);
986}
987
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700988/**
989 * audit_syscall_entry - fill in an audit record at syscall entry
990 * @tsk: task being audited
991 * @arch: architecture type
992 * @major: major syscall type (function)
993 * @a1: additional syscall register 1
994 * @a2: additional syscall register 2
995 * @a3: additional syscall register 3
996 * @a4: additional syscall register 4
997 *
998 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 * audit context was created when the task was created and the state or
1000 * filters demand the audit context be built. If the state from the
1001 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1002 * then the record will be written at syscall exit time (otherwise, it
1003 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001004 * be written).
1005 */
2fd6f582005-04-29 16:08:28 +01001006void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 unsigned long a1, unsigned long a2,
1008 unsigned long a3, unsigned long a4)
1009{
1010 struct audit_context *context = tsk->audit_context;
1011 enum audit_state state;
1012
1013 BUG_ON(!context);
1014
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001015 /*
1016 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 * calls in kernel_thread via the entry.S interface, instead of
1018 * with direct calls. (If you are porting to a new
1019 * architecture, hitting this condition can indicate that you
1020 * got the _exit/_leave calls backward in entry.S.)
1021 *
1022 * i386 no
1023 * x86_64 no
1024 * ppc64 yes (see arch/ppc64/kernel/misc.S)
1025 *
1026 * This also happens with vm86 emulation in a non-nested manner
1027 * (entries without exits), so this case must be caught.
1028 */
1029 if (context->in_syscall) {
1030 struct audit_context *newctx;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032#if AUDIT_DEBUG
1033 printk(KERN_ERR
1034 "audit(:%d) pid=%d in syscall=%d;"
1035 " entering syscall=%d\n",
1036 context->serial, tsk->pid, context->major, major);
1037#endif
1038 newctx = audit_alloc_context(context->state);
1039 if (newctx) {
1040 newctx->previous = context;
1041 context = newctx;
1042 tsk->audit_context = newctx;
1043 } else {
1044 /* If we can't alloc a new context, the best we
1045 * can do is to leak memory (any pending putname
1046 * will be lost). The only other alternative is
1047 * to abandon auditing. */
1048 audit_zero_context(context, context->state);
1049 }
1050 }
1051 BUG_ON(context->in_syscall || context->name_count);
1052
1053 if (!audit_enabled)
1054 return;
1055
2fd6f582005-04-29 16:08:28 +01001056 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 context->major = major;
1058 context->argv[0] = a1;
1059 context->argv[1] = a2;
1060 context->argv[2] = a3;
1061 context->argv[3] = a4;
1062
1063 state = context->state;
1064 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +01001065 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (likely(state == AUDIT_DISABLED))
1067 return;
1068
David Woodhousece625a82005-07-18 14:24:46 -04001069 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 context->ctime = CURRENT_TIME;
1071 context->in_syscall = 1;
1072 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1073}
1074
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001075/**
1076 * audit_syscall_exit - deallocate audit context after a system call
1077 * @tsk: task being audited
1078 * @valid: success/failure flag
1079 * @return_code: syscall return value
1080 *
1081 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1083 * filtering, or because some other part of the kernel write an audit
1084 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001085 * free the names stored from getname().
1086 */
2fd6f582005-04-29 16:08:28 +01001087void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct audit_context *context;
1090
1091 get_task_struct(tsk);
1092 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +01001093 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 task_unlock(tsk);
1095
1096 /* Not having a context here is ok, since the parent may have
1097 * called __put_task_struct. */
1098 if (likely(!context))
David Woodhouse413a1c72005-08-17 14:45:55 +01001099 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
David Woodhousef7056d62005-06-20 16:07:33 +01001101 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +01001102 audit_log_exit(context, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 context->in_syscall = 0;
1105 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (context->previous) {
1108 struct audit_context *new_context = context->previous;
1109 context->previous = NULL;
1110 audit_free_context(context);
1111 tsk->audit_context = new_context;
1112 } else {
1113 audit_free_names(context);
1114 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 tsk->audit_context = context;
1116 }
David Woodhouse413a1c72005-08-17 14:45:55 +01001117 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 put_task_struct(tsk);
1119}
1120
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001121/**
1122 * audit_getname - add a name to the list
1123 * @name: name to add
1124 *
1125 * Add a name to the list of audit names for this context.
1126 * Called from fs/namei.c:getname().
1127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128void audit_getname(const char *name)
1129{
1130 struct audit_context *context = current->audit_context;
1131
1132 if (!context || IS_ERR(name) || !name)
1133 return;
1134
1135 if (!context->in_syscall) {
1136#if AUDIT_DEBUG == 2
1137 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1138 __FILE__, __LINE__, context->serial, name);
1139 dump_stack();
1140#endif
1141 return;
1142 }
1143 BUG_ON(context->name_count >= AUDIT_NAMES);
1144 context->names[context->name_count].name = name;
1145 context->names[context->name_count].ino = (unsigned long)-1;
1146 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001147 if (!context->pwd) {
1148 read_lock(&current->fs->lock);
1149 context->pwd = dget(current->fs->pwd);
1150 context->pwdmnt = mntget(current->fs->pwdmnt);
1151 read_unlock(&current->fs->lock);
1152 }
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001156/* audit_putname - intercept a putname request
1157 * @name: name to intercept and delay for putname
1158 *
1159 * If we have stored the name from getname in the audit context,
1160 * then we delay the putname until syscall exit.
1161 * Called from include/linux/fs.h:putname().
1162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163void audit_putname(const char *name)
1164{
1165 struct audit_context *context = current->audit_context;
1166
1167 BUG_ON(!context);
1168 if (!context->in_syscall) {
1169#if AUDIT_DEBUG == 2
1170 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1171 __FILE__, __LINE__, context->serial, name);
1172 if (context->name_count) {
1173 int i;
1174 for (i = 0; i < context->name_count; i++)
1175 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1176 context->names[i].name,
1177 context->names[i].name);
1178 }
1179#endif
1180 __putname(name);
1181 }
1182#if AUDIT_DEBUG
1183 else {
1184 ++context->put_count;
1185 if (context->put_count > context->name_count) {
1186 printk(KERN_ERR "%s:%d(:%d): major=%d"
1187 " in_syscall=%d putname(%p) name_count=%d"
1188 " put_count=%d\n",
1189 __FILE__, __LINE__,
1190 context->serial, context->major,
1191 context->in_syscall, name, context->name_count,
1192 context->put_count);
1193 dump_stack();
1194 }
1195 }
1196#endif
1197}
1198
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001199/**
1200 * audit_inode - store the inode and device from a lookup
1201 * @name: name being audited
1202 * @inode: inode being audited
1203 * @flags: lookup flags (as used in path_lookup())
1204 *
1205 * Called from fs/namei.c:path_lookup().
1206 */
David Woodhouseae7b9612005-06-20 16:11:05 +01001207void audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 int idx;
1210 struct audit_context *context = current->audit_context;
1211
1212 if (!context->in_syscall)
1213 return;
1214 if (context->name_count
1215 && context->names[context->name_count-1].name
1216 && context->names[context->name_count-1].name == name)
1217 idx = context->name_count - 1;
1218 else if (context->name_count > 1
1219 && context->names[context->name_count-2].name
1220 && context->names[context->name_count-2].name == name)
1221 idx = context->name_count - 2;
1222 else {
1223 /* FIXME: how much do we care about inodes that have no
1224 * associated name? */
1225 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1226 return;
1227 idx = context->name_count++;
1228 context->names[idx].name = NULL;
1229#if AUDIT_DEBUG
1230 ++context->ino_count;
1231#endif
1232 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001233 context->names[idx].flags = flags;
1234 context->names[idx].ino = inode->i_ino;
1235 context->names[idx].dev = inode->i_sb->s_dev;
1236 context->names[idx].mode = inode->i_mode;
1237 context->names[idx].uid = inode->i_uid;
1238 context->names[idx].gid = inode->i_gid;
1239 context->names[idx].rdev = inode->i_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001242/**
1243 * auditsc_get_stamp - get local copies of audit_context values
1244 * @ctx: audit_context for the task
1245 * @t: timespec to store time recorded in the audit_context
1246 * @serial: serial value that is recorded in the audit_context
1247 *
1248 * Also sets the context as auditable.
1249 */
David Woodhousebfb44962005-05-21 21:08:09 +01001250void auditsc_get_stamp(struct audit_context *ctx,
1251 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
David Woodhousece625a82005-07-18 14:24:46 -04001253 if (!ctx->serial)
1254 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001255 t->tv_sec = ctx->ctime.tv_sec;
1256 t->tv_nsec = ctx->ctime.tv_nsec;
1257 *serial = ctx->serial;
1258 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001261/**
1262 * audit_set_loginuid - set a task's audit_context loginuid
1263 * @task: task whose audit context is being modified
1264 * @loginuid: loginuid value
1265 *
1266 * Returns 0.
1267 *
1268 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1269 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001270int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Steve Grubb456be6c2005-04-29 17:30:07 +01001272 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001273 struct audit_buffer *ab;
1274
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001275 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001276 if (ab) {
1277 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001278 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001279 task->pid, task->uid,
1280 task->audit_context->loginuid, loginuid);
1281 audit_log_end(ab);
1282 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001283 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 return 0;
1286}
1287
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001288/**
1289 * audit_get_loginuid - get the loginuid for an audit_context
1290 * @ctx: the audit_context
1291 *
1292 * Returns the context's loginuid or -1 if @ctx is NULL.
1293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294uid_t audit_get_loginuid(struct audit_context *ctx)
1295{
1296 return ctx ? ctx->loginuid : -1;
1297}
1298
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001299/**
1300 * audit_ipc_perms - record audit data for ipc
1301 * @qbytes: msgq bytes
1302 * @uid: msgq user id
1303 * @gid: msgq group id
1304 * @mode: msgq mode (permissions)
1305 *
1306 * Returns 0 for success or NULL context or < 0 on error.
1307 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1309{
1310 struct audit_aux_data_ipcctl *ax;
1311 struct audit_context *context = current->audit_context;
1312
1313 if (likely(!context))
1314 return 0;
1315
1316 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1317 if (!ax)
1318 return -ENOMEM;
1319
1320 ax->qbytes = qbytes;
1321 ax->uid = uid;
1322 ax->gid = gid;
1323 ax->mode = mode;
1324
Steve Grubbc0404992005-05-13 18:17:42 +01001325 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 ax->d.next = context->aux;
1327 context->aux = (void *)ax;
1328 return 0;
1329}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001330
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001331/**
1332 * audit_socketcall - record audit data for sys_socketcall
1333 * @nargs: number of args
1334 * @args: args array
1335 *
1336 * Returns 0 for success or NULL context or < 0 on error.
1337 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001338int audit_socketcall(int nargs, unsigned long *args)
1339{
1340 struct audit_aux_data_socketcall *ax;
1341 struct audit_context *context = current->audit_context;
1342
1343 if (likely(!context))
1344 return 0;
1345
1346 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1347 if (!ax)
1348 return -ENOMEM;
1349
1350 ax->nargs = nargs;
1351 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1352
1353 ax->d.type = AUDIT_SOCKETCALL;
1354 ax->d.next = context->aux;
1355 context->aux = (void *)ax;
1356 return 0;
1357}
1358
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001359/**
1360 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1361 * @len: data length in user space
1362 * @a: data address in kernel space
1363 *
1364 * Returns 0 for success or NULL context or < 0 on error.
1365 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001366int audit_sockaddr(int len, void *a)
1367{
1368 struct audit_aux_data_sockaddr *ax;
1369 struct audit_context *context = current->audit_context;
1370
1371 if (likely(!context))
1372 return 0;
1373
1374 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1375 if (!ax)
1376 return -ENOMEM;
1377
1378 ax->len = len;
1379 memcpy(ax->a, a, len);
1380
1381 ax->d.type = AUDIT_SOCKADDR;
1382 ax->d.next = context->aux;
1383 context->aux = (void *)ax;
1384 return 0;
1385}
1386
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001387/**
1388 * audit_avc_path - record the granting or denial of permissions
1389 * @dentry: dentry to record
1390 * @mnt: mnt to record
1391 *
1392 * Returns 0 for success or NULL context or < 0 on error.
1393 *
1394 * Called from security/selinux/avc.c::avc_audit()
1395 */
Stephen Smalley01116102005-05-21 00:15:52 +01001396int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1397{
1398 struct audit_aux_data_path *ax;
1399 struct audit_context *context = current->audit_context;
1400
1401 if (likely(!context))
1402 return 0;
1403
1404 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1405 if (!ax)
1406 return -ENOMEM;
1407
1408 ax->dentry = dget(dentry);
1409 ax->mnt = mntget(mnt);
1410
1411 ax->d.type = AUDIT_AVC_PATH;
1412 ax->d.next = context->aux;
1413 context->aux = (void *)ax;
1414 return 0;
1415}
1416
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001417/**
1418 * audit_signal_info - record signal info for shutting down audit subsystem
1419 * @sig: signal value
1420 * @t: task being signaled
1421 *
1422 * If the audit subsystem is being terminated, record the task (pid)
1423 * and uid that is doing that.
1424 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001425void audit_signal_info(int sig, struct task_struct *t)
1426{
1427 extern pid_t audit_sig_pid;
1428 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001429
David Woodhouse582edda2005-07-13 22:39:34 +01001430 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001431 if (sig == SIGTERM || sig == SIGHUP) {
1432 struct audit_context *ctx = current->audit_context;
1433 audit_sig_pid = current->pid;
1434 if (ctx)
1435 audit_sig_uid = ctx->loginuid;
1436 else
1437 audit_sig_uid = current->uid;
1438 }
1439 }
1440}