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