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