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