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