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