]>
Commit | Line | Data |
---|---|---|
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. | |
b63862f4 | 5 | * Copyright (C) 2005 IBM Corporation |
1da177e4 LT |
6 | * 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 | * | |
b63862f4 DK |
31 | * The support of additional filter rules compares (>, <, >=, <=) was |
32 | * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005. | |
33 | * | |
1da177e4 LT |
34 | */ |
35 | ||
36 | #include <linux/init.h> | |
1da177e4 | 37 | #include <asm/types.h> |
715b49ef | 38 | #include <asm/atomic.h> |
1da177e4 LT |
39 | #include <linux/mm.h> |
40 | #include <linux/module.h> | |
01116105 | 41 | #include <linux/mount.h> |
3ec3b2fb | 42 | #include <linux/socket.h> |
1da177e4 LT |
43 | #include <linux/audit.h> |
44 | #include <linux/personality.h> | |
45 | #include <linux/time.h> | |
f6a789d1 | 46 | #include <linux/kthread.h> |
5bb289b5 | 47 | #include <linux/netlink.h> |
f5561964 | 48 | #include <linux/compiler.h> |
1da177e4 LT |
49 | #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. */ | |
58 | extern 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. */ | |
72 | enum 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. */ | |
97 | struct 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; | |
ae7b961b | 105 | unsigned flags; |
1da177e4 LT |
106 | }; |
107 | ||
108 | struct audit_aux_data { | |
109 | struct audit_aux_data *next; | |
110 | int type; | |
111 | }; | |
112 | ||
113 | #define AUDIT_AUX_IPCPERM 0 | |
114 | ||
115 | struct 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 | ||
3ec3b2fb DW |
124 | struct audit_aux_data_socketcall { |
125 | struct audit_aux_data d; | |
126 | int nargs; | |
127 | unsigned long args[0]; | |
128 | }; | |
129 | ||
130 | struct audit_aux_data_sockaddr { | |
131 | struct audit_aux_data d; | |
132 | int len; | |
133 | char a[0]; | |
134 | }; | |
135 | ||
01116105 SS |
136 | struct audit_aux_data_path { |
137 | struct audit_aux_data d; | |
138 | struct dentry *dentry; | |
139 | struct vfsmount *mnt; | |
140 | }; | |
1da177e4 LT |
141 | |
142 | /* The per-task audit context. */ | |
143 | struct 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 */ | |
2fd6f58b | 152 | long return_code;/* syscall return code */ |
1da177e4 LT |
153 | int auditable; /* 1 if record should be written */ |
154 | int name_count; | |
155 | struct audit_names names[AUDIT_NAMES]; | |
8f37d47c DW |
156 | struct dentry * pwd; |
157 | struct vfsmount * pwdmnt; | |
1da177e4 LT |
158 | 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; | |
2fd6f58b | 166 | int arch; |
1da177e4 LT |
167 | |
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. */ | |
0f45aa18 DW |
178 | static 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 | }; | |
1da177e4 LT |
188 | |
189 | struct audit_entry { | |
190 | struct list_head list; | |
191 | struct rcu_head rcu; | |
192 | struct audit_rule rule; | |
193 | }; | |
194 | ||
7ca00264 DW |
195 | extern int audit_pid; |
196 | ||
3c789a19 AG |
197 | /* Copy rule from user-space to kernel-space. Called from |
198 | * audit_add_rule during AUDIT_ADD. */ | |
199 | static 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 | ||
1da177e4 | 223 | /* Check to see if two rules are identical. It is called from |
3c789a19 | 224 | * audit_add_rule during AUDIT_ADD and |
1da177e4 | 225 | * audit_del_rule during AUDIT_DEL. */ |
3c789a19 | 226 | static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b) |
1da177e4 LT |
227 | { |
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. */ | |
3c789a19 | 255 | static inline int audit_add_rule(struct audit_rule *rule, |
0f45aa18 | 256 | struct list_head *list) |
1da177e4 | 257 | { |
3c789a19 | 258 | struct audit_entry *entry; |
b63862f4 | 259 | int i; |
3c789a19 AG |
260 | |
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 | ||
b63862f4 DK |
269 | 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 | ||
3c789a19 AG |
279 | 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 | ||
0f45aa18 DW |
286 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) { |
287 | entry->rule.flags &= ~AUDIT_FILTER_PREPEND; | |
1da177e4 LT |
288 | list_add_rcu(&entry->list, list); |
289 | } else { | |
290 | list_add_tail_rcu(&entry->list, list); | |
291 | } | |
3c789a19 AG |
292 | |
293 | return 0; | |
1da177e4 LT |
294 | } |
295 | ||
3c789a19 | 296 | static inline void audit_free_rule(struct rcu_head *head) |
1da177e4 LT |
297 | { |
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. */ | |
305 | static 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 | } | |
0f45aa18 | 319 | return -ENOENT; /* No matching rule */ |
1da177e4 LT |
320 | } |
321 | ||
f6a789d1 DW |
322 | static 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 | ||
b0dd25a8 RD |
348 | /** |
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 | */ | |
c94c257c SH |
357 | int audit_receive_filter(int type, int pid, int uid, int seq, void *data, |
358 | uid_t loginuid) | |
1da177e4 | 359 | { |
f6a789d1 DW |
360 | struct task_struct *tsk; |
361 | int *dest; | |
1da177e4 | 362 | int err = 0; |
0f45aa18 | 363 | unsigned listnr; |
1da177e4 LT |
364 | |
365 | switch (type) { | |
366 | case AUDIT_LIST: | |
f6a789d1 DW |
367 | /* 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); | |
0f45aa18 | 383 | } |
1da177e4 LT |
384 | break; |
385 | case AUDIT_ADD: | |
3c789a19 AG |
386 | listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND; |
387 | if (listnr >= AUDIT_NR_FILTERS) | |
1da177e4 | 388 | return -EINVAL; |
3c789a19 AG |
389 | |
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); | |
1da177e4 LT |
394 | break; |
395 | case AUDIT_DEL: | |
0f45aa18 DW |
396 | 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) | |
9ad9ad38 | 402 | audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE, |
0f45aa18 | 403 | "auid=%u removed an audit rule\n", loginuid); |
1da177e4 LT |
404 | break; |
405 | default: | |
406 | return -EINVAL; | |
407 | } | |
408 | ||
409 | return err; | |
410 | } | |
1da177e4 | 411 | |
b63862f4 DK |
412 | static 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 | ||
1da177e4 LT |
432 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 |
433 | * otherwise. */ | |
434 | static 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++) { | |
b63862f4 DK |
442 | u32 field = rule->fields[i] & ~AUDIT_OPERATORS; |
443 | u32 op = rule->fields[i] & AUDIT_OPERATORS; | |
1da177e4 LT |
444 | u32 value = rule->values[i]; |
445 | int result = 0; | |
446 | ||
447 | switch (field) { | |
448 | case AUDIT_PID: | |
b63862f4 | 449 | result = audit_comparator(tsk->pid, op, value); |
1da177e4 LT |
450 | break; |
451 | case AUDIT_UID: | |
b63862f4 | 452 | result = audit_comparator(tsk->uid, op, value); |
1da177e4 LT |
453 | break; |
454 | case AUDIT_EUID: | |
b63862f4 | 455 | result = audit_comparator(tsk->euid, op, value); |
1da177e4 LT |
456 | break; |
457 | case AUDIT_SUID: | |
b63862f4 | 458 | result = audit_comparator(tsk->suid, op, value); |
1da177e4 LT |
459 | break; |
460 | case AUDIT_FSUID: | |
b63862f4 | 461 | result = audit_comparator(tsk->fsuid, op, value); |
1da177e4 LT |
462 | break; |
463 | case AUDIT_GID: | |
b63862f4 | 464 | result = audit_comparator(tsk->gid, op, value); |
1da177e4 LT |
465 | break; |
466 | case AUDIT_EGID: | |
b63862f4 | 467 | result = audit_comparator(tsk->egid, op, value); |
1da177e4 LT |
468 | break; |
469 | case AUDIT_SGID: | |
b63862f4 | 470 | result = audit_comparator(tsk->sgid, op, value); |
1da177e4 LT |
471 | break; |
472 | case AUDIT_FSGID: | |
b63862f4 | 473 | result = audit_comparator(tsk->fsgid, op, value); |
1da177e4 LT |
474 | break; |
475 | case AUDIT_PERS: | |
b63862f4 | 476 | result = audit_comparator(tsk->personality, op, value); |
1da177e4 | 477 | break; |
2fd6f58b | 478 | case AUDIT_ARCH: |
b63862f4 DK |
479 | if (ctx) |
480 | result = audit_comparator(ctx->arch, op, value); | |
2fd6f58b | 481 | break; |
1da177e4 LT |
482 | |
483 | case AUDIT_EXIT: | |
484 | if (ctx && ctx->return_valid) | |
b63862f4 | 485 | result = audit_comparator(ctx->return_code, op, value); |
1da177e4 LT |
486 | break; |
487 | case AUDIT_SUCCESS: | |
b01f2cc1 DW |
488 | if (ctx && ctx->return_valid) { |
489 | if (value) | |
b63862f4 | 490 | result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS); |
b01f2cc1 | 491 | else |
b63862f4 | 492 | result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE); |
b01f2cc1 | 493 | } |
1da177e4 LT |
494 | break; |
495 | case AUDIT_DEVMAJOR: | |
496 | if (ctx) { | |
497 | for (j = 0; j < ctx->name_count; j++) { | |
b63862f4 | 498 | if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) { |
1da177e4 LT |
499 | ++result; |
500 | break; | |
501 | } | |
502 | } | |
503 | } | |
504 | break; | |
505 | case AUDIT_DEVMINOR: | |
506 | if (ctx) { | |
507 | for (j = 0; j < ctx->name_count; j++) { | |
b63862f4 | 508 | if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) { |
1da177e4 LT |
509 | ++result; |
510 | break; | |
511 | } | |
512 | } | |
513 | } | |
514 | break; | |
515 | case AUDIT_INODE: | |
516 | if (ctx) { | |
517 | for (j = 0; j < ctx->name_count; j++) { | |
f38aa942 | 518 | if ( audit_comparator(ctx->names[j].ino, op, value)) { |
1da177e4 LT |
519 | ++result; |
520 | break; | |
521 | } | |
522 | } | |
523 | } | |
524 | break; | |
525 | case AUDIT_LOGINUID: | |
526 | result = 0; | |
527 | if (ctx) | |
b63862f4 | 528 | result = audit_comparator(ctx->loginuid, op, value); |
1da177e4 LT |
529 | break; |
530 | case AUDIT_ARG0: | |
531 | case AUDIT_ARG1: | |
532 | case AUDIT_ARG2: | |
533 | case AUDIT_ARG3: | |
534 | if (ctx) | |
b63862f4 | 535 | result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value); |
1da177e4 LT |
536 | break; |
537 | } | |
538 | ||
1da177e4 LT |
539 | 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 | */ | |
554 | static 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(); | |
0f45aa18 | 560 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) { |
1da177e4 LT |
561 | 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 | |
23f32d18 | 572 | * also not high enough that we already know we have to write an audit |
b0dd25a8 | 573 | * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT). |
1da177e4 LT |
574 | */ |
575 | static 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; | |
c3896495 | 580 | enum audit_state state; |
1da177e4 | 581 | |
351bb722 | 582 | if (audit_pid && tsk->tgid == audit_pid) |
f7056d64 DW |
583 | return AUDIT_DISABLED; |
584 | ||
1da177e4 | 585 | rcu_read_lock(); |
c3896495 | 586 | if (!list_empty(list)) { |
b63862f4 DK |
587 | int word = AUDIT_WORD(ctx->major); |
588 | int bit = AUDIT_BIT(ctx->major); | |
589 | ||
590 | 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 | } | |
1da177e4 LT |
597 | } |
598 | rcu_read_unlock(); | |
599 | return AUDIT_BUILD_CONTEXT; | |
600 | } | |
601 | ||
5bb289b5 | 602 | static int audit_filter_user_rules(struct netlink_skb_parms *cb, |
b63862f4 DK |
603 | struct audit_rule *rule, |
604 | enum audit_state *state) | |
5bb289b5 DW |
605 | { |
606 | int i; | |
607 | ||
608 | for (i = 0; i < rule->field_count; i++) { | |
b63862f4 DK |
609 | u32 field = rule->fields[i] & ~AUDIT_OPERATORS; |
610 | u32 op = rule->fields[i] & AUDIT_OPERATORS; | |
5bb289b5 DW |
611 | u32 value = rule->values[i]; |
612 | int result = 0; | |
613 | ||
614 | switch (field) { | |
615 | case AUDIT_PID: | |
b63862f4 | 616 | result = audit_comparator(cb->creds.pid, op, value); |
5bb289b5 DW |
617 | break; |
618 | case AUDIT_UID: | |
b63862f4 | 619 | result = audit_comparator(cb->creds.uid, op, value); |
5bb289b5 DW |
620 | break; |
621 | case AUDIT_GID: | |
b63862f4 | 622 | result = audit_comparator(cb->creds.gid, op, value); |
5bb289b5 DW |
623 | break; |
624 | case AUDIT_LOGINUID: | |
b63862f4 | 625 | result = audit_comparator(cb->loginuid, op, value); |
5bb289b5 DW |
626 | break; |
627 | } | |
628 | ||
5bb289b5 DW |
629 | 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 | ||
640 | int audit_filter_user(struct netlink_skb_parms *cb, int type) | |
0f45aa18 DW |
641 | { |
642 | struct audit_entry *e; | |
643 | enum audit_state state; | |
4a4cd633 | 644 | int ret = 1; |
0f45aa18 DW |
645 | |
646 | rcu_read_lock(); | |
647 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) { | |
5bb289b5 | 648 | if (audit_filter_user_rules(cb, &e->rule, &state)) { |
4a4cd633 DW |
649 | if (state == AUDIT_DISABLED) |
650 | ret = 0; | |
651 | break; | |
0f45aa18 DW |
652 | } |
653 | } | |
654 | rcu_read_unlock(); | |
4a4cd633 | 655 | |
993e2d41 | 656 | return ret; /* Audit by default */ |
0f45aa18 DW |
657 | } |
658 | ||
1da177e4 LT |
659 | /* This should be called with task_lock() held. */ |
660 | static 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 | ||
21af6c4f | 671 | if (context->in_syscall && !context->auditable) { |
1da177e4 | 672 | enum audit_state state; |
0f45aa18 | 673 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]); |
1da177e4 LT |
674 | 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 | ||
692 | static 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; | |
8f37d47c DW |
723 | if (context->pwd) |
724 | dput(context->pwd); | |
725 | if (context->pwdmnt) | |
726 | mntput(context->pwdmnt); | |
727 | context->pwd = NULL; | |
728 | context->pwdmnt = NULL; | |
1da177e4 LT |
729 | } |
730 | ||
731 | static inline void audit_free_aux(struct audit_context *context) | |
732 | { | |
733 | struct audit_aux_data *aux; | |
734 | ||
735 | while ((aux = context->aux)) { | |
01116105 SS |
736 | if (aux->type == AUDIT_AVC_PATH) { |
737 | struct audit_aux_data_path *axi = (void *)aux; | |
738 | dput(axi->dentry); | |
739 | mntput(axi->mnt); | |
740 | } | |
1da177e4 LT |
741 | context->aux = aux->next; |
742 | kfree(aux); | |
743 | } | |
744 | } | |
745 | ||
746 | static 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 | ||
756 | static 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 | ||
b0dd25a8 RD |
766 | /** |
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 | |
1da177e4 LT |
771 | * if necessary. Doing so turns on system call auditing for the |
772 | * specified task. This is called from copy_process, so no lock is | |
b0dd25a8 RD |
773 | * needed. |
774 | */ | |
1da177e4 LT |
775 | int 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 | ||
802 | static 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 | ||
219f0817 SS |
825 | static 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); | |
99e45eea DW |
832 | audit_log_format(ab, " comm="); |
833 | audit_log_untrustedstring(ab, name); | |
219f0817 SS |
834 | |
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 | ||
9796fdd8 | 853 | static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask) |
1da177e4 LT |
854 | { |
855 | int i; | |
856 | struct audit_buffer *ab; | |
7551ced3 | 857 | struct audit_aux_data *aux; |
1da177e4 | 858 | |
f5561964 | 859 | ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL); |
1da177e4 LT |
860 | if (!ab) |
861 | return; /* audit_panic has been called */ | |
bccf6ae0 DW |
862 | audit_log_format(ab, "arch=%x syscall=%d", |
863 | context->arch, context->major); | |
1da177e4 LT |
864 | if (context->personality != PER_LINUX) |
865 | audit_log_format(ab, " per=%lx", context->personality); | |
866 | if (context->return_valid) | |
2fd6f58b DW |
867 | audit_log_format(ab, " success=%s exit=%ld", |
868 | (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", | |
869 | context->return_code); | |
1da177e4 LT |
870 | audit_log_format(ab, |
871 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" | |
326e9c8b SG |
872 | " pid=%d auid=%u uid=%u gid=%u" |
873 | " euid=%u suid=%u fsuid=%u" | |
874 | " egid=%u sgid=%u fsgid=%u", | |
1da177e4 LT |
875 | 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); | |
219f0817 | 886 | audit_log_task_info(ab); |
1da177e4 | 887 | audit_log_end(ab); |
1da177e4 | 888 | |
7551ced3 | 889 | for (aux = context->aux; aux; aux = aux->next) { |
c0404993 | 890 | |
ef20c8c1 | 891 | ab = audit_log_start(context, gfp_mask, aux->type); |
1da177e4 LT |
892 | if (!ab) |
893 | continue; /* audit_panic has been called */ | |
894 | ||
1da177e4 | 895 | switch (aux->type) { |
c0404993 | 896 | case AUDIT_IPC: { |
1da177e4 LT |
897 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
898 | audit_log_format(ab, | |
326e9c8b | 899 | " qbytes=%lx iuid=%u igid=%u mode=%x", |
1da177e4 | 900 | axi->qbytes, axi->uid, axi->gid, axi->mode); |
3ec3b2fb DW |
901 | 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; } | |
01116105 SS |
917 | |
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); | |
01116105 SS |
921 | break; } |
922 | ||
1da177e4 LT |
923 | } |
924 | audit_log_end(ab); | |
1da177e4 LT |
925 | } |
926 | ||
8f37d47c | 927 | if (context->pwd && context->pwdmnt) { |
ef20c8c1 | 928 | ab = audit_log_start(context, gfp_mask, AUDIT_CWD); |
8f37d47c DW |
929 | if (ab) { |
930 | audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt); | |
931 | audit_log_end(ab); | |
932 | } | |
933 | } | |
1da177e4 | 934 | for (i = 0; i < context->name_count; i++) { |
ef20c8c1 | 935 | ab = audit_log_start(context, gfp_mask, AUDIT_PATH); |
1da177e4 LT |
936 | if (!ab) |
937 | continue; /* audit_panic has been called */ | |
8f37d47c | 938 | |
1da177e4 | 939 | audit_log_format(ab, "item=%d", i); |
83c7d091 DW |
940 | if (context->names[i].name) { |
941 | audit_log_format(ab, " name="); | |
942 | audit_log_untrustedstring(ab, context->names[i].name); | |
943 | } | |
ae7b961b DW |
944 | audit_log_format(ab, " flags=%x\n", context->names[i].flags); |
945 | ||
1da177e4 LT |
946 | if (context->names[i].ino != (unsigned long)-1) |
947 | audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o" | |
326e9c8b | 948 | " ouid=%u ogid=%u rdev=%02x:%02x", |
1da177e4 LT |
949 | 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 | ||
b0dd25a8 RD |
961 | /** |
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 | */ | |
1da177e4 LT |
967 | void 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 | |
f5561964 DW |
979 | * 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 */ | |
f7056d64 | 982 | if (context->in_syscall && context->auditable) |
f5561964 | 983 | audit_log_exit(context, GFP_ATOMIC); |
1da177e4 LT |
984 | |
985 | audit_free_context(context); | |
986 | } | |
987 | ||
b0dd25a8 RD |
988 | /** |
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 | |
1da177e4 LT |
999 | * 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 | |
b0dd25a8 RD |
1004 | * be written). |
1005 | */ | |
2fd6f58b | 1006 | void audit_syscall_entry(struct task_struct *tsk, int arch, int major, |
1da177e4 LT |
1007 | 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 | ||
b0dd25a8 RD |
1015 | /* |
1016 | * This happens only on certain architectures that make system | |
1da177e4 LT |
1017 | * 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 | ||
1da177e4 LT |
1032 | #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 | ||
2fd6f58b | 1056 | context->arch = arch; |
1da177e4 LT |
1057 | 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) | |
0f45aa18 | 1065 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]); |
1da177e4 LT |
1066 | if (likely(state == AUDIT_DISABLED)) |
1067 | return; | |
1068 | ||
ce625a80 | 1069 | context->serial = 0; |
1da177e4 LT |
1070 | context->ctime = CURRENT_TIME; |
1071 | context->in_syscall = 1; | |
1072 | context->auditable = !!(state == AUDIT_RECORD_CONTEXT); | |
1073 | } | |
1074 | ||
b0dd25a8 RD |
1075 | /** |
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 | |
1da177e4 LT |
1082 | * 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, | |
b0dd25a8 RD |
1085 | * free the names stored from getname(). |
1086 | */ | |
2fd6f58b | 1087 | void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code) |
1da177e4 LT |
1088 | { |
1089 | struct audit_context *context; | |
1090 | ||
1091 | get_task_struct(tsk); | |
1092 | task_lock(tsk); | |
2fd6f58b | 1093 | context = audit_get_context(tsk, valid, return_code); |
1da177e4 LT |
1094 | 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)) | |
413a1c75 | 1099 | goto out; |
1da177e4 | 1100 | |
f7056d64 | 1101 | if (context->in_syscall && context->auditable) |
f5561964 | 1102 | audit_log_exit(context, GFP_KERNEL); |
1da177e4 LT |
1103 | |
1104 | context->in_syscall = 0; | |
1105 | context->auditable = 0; | |
2fd6f58b | 1106 | |
1da177e4 LT |
1107 | 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); | |
1da177e4 LT |
1115 | tsk->audit_context = context; |
1116 | } | |
413a1c75 | 1117 | out: |
1da177e4 LT |
1118 | put_task_struct(tsk); |
1119 | } | |
1120 | ||
b0dd25a8 RD |
1121 | /** |
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 | */ | |
1da177e4 LT |
1128 | void 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; | |
8f37d47c DW |
1147 | if (!context->pwd) { |
1148 | read_lock(¤t->fs->lock); | |
1149 | context->pwd = dget(current->fs->pwd); | |
1150 | context->pwdmnt = mntget(current->fs->pwdmnt); | |
1151 | read_unlock(¤t->fs->lock); | |
1152 | } | |
1153 | ||
1da177e4 LT |
1154 | } |
1155 | ||
b0dd25a8 RD |
1156 | /* 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 | */ | |
1da177e4 LT |
1163 | void 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 | ||
b0dd25a8 RD |
1199 | /** |
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 | */ | |
ae7b961b | 1207 | void audit_inode(const char *name, const struct inode *inode, unsigned flags) |
1da177e4 LT |
1208 | { |
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 | } | |
ae7b961b DW |
1233 | 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; | |
1da177e4 LT |
1240 | } |
1241 | ||
b0dd25a8 RD |
1242 | /** |
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 | */ | |
bfb4496e DW |
1250 | void auditsc_get_stamp(struct audit_context *ctx, |
1251 | struct timespec *t, unsigned int *serial) | |
1da177e4 | 1252 | { |
ce625a80 DW |
1253 | if (!ctx->serial) |
1254 | ctx->serial = audit_serial(); | |
bfb4496e DW |
1255 | t->tv_sec = ctx->ctime.tv_sec; |
1256 | t->tv_nsec = ctx->ctime.tv_nsec; | |
1257 | *serial = ctx->serial; | |
1258 | ctx->auditable = 1; | |
1da177e4 LT |
1259 | } |
1260 | ||
b0dd25a8 RD |
1261 | /** |
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 | */ | |
456be6cd | 1270 | int audit_set_loginuid(struct task_struct *task, uid_t loginuid) |
1da177e4 | 1271 | { |
456be6cd | 1272 | if (task->audit_context) { |
c0404993 SG |
1273 | struct audit_buffer *ab; |
1274 | ||
9ad9ad38 | 1275 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN); |
c0404993 SG |
1276 | if (ab) { |
1277 | audit_log_format(ab, "login pid=%d uid=%u " | |
326e9c8b | 1278 | "old auid=%u new auid=%u", |
c0404993 SG |
1279 | task->pid, task->uid, |
1280 | task->audit_context->loginuid, loginuid); | |
1281 | audit_log_end(ab); | |
1282 | } | |
456be6cd | 1283 | task->audit_context->loginuid = loginuid; |
1da177e4 LT |
1284 | } |
1285 | return 0; | |
1286 | } | |
1287 | ||
b0dd25a8 RD |
1288 | /** |
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 | */ | |
1da177e4 LT |
1294 | uid_t audit_get_loginuid(struct audit_context *ctx) |
1295 | { | |
1296 | return ctx ? ctx->loginuid : -1; | |
1297 | } | |
1298 | ||
b0dd25a8 RD |
1299 | /** |
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 | */ | |
1da177e4 LT |
1308 | int 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 | ||
c0404993 | 1325 | ax->d.type = AUDIT_IPC; |
1da177e4 LT |
1326 | ax->d.next = context->aux; |
1327 | context->aux = (void *)ax; | |
1328 | return 0; | |
1329 | } | |
c2f0c7c3 | 1330 | |
b0dd25a8 RD |
1331 | /** |
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 | */ | |
3ec3b2fb DW |
1338 | int 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 | ||
b0dd25a8 RD |
1359 | /** |
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 | */ | |
3ec3b2fb DW |
1366 | int 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 | ||
b0dd25a8 RD |
1387 | /** |
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 | */ | |
01116105 SS |
1396 | int 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 | ||
b0dd25a8 RD |
1417 | /** |
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 | */ | |
c2f0c7c3 SG |
1425 | void audit_signal_info(int sig, struct task_struct *t) |
1426 | { | |
1427 | extern pid_t audit_sig_pid; | |
1428 | extern uid_t audit_sig_uid; | |
c2f0c7c3 | 1429 | |
582edda5 | 1430 | if (unlikely(audit_pid && t->tgid == audit_pid)) { |
c2f0c7c3 SG |
1431 | 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 | } |