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. |
20ca73bc | 6 | * Copyright (C) 2005, 2006 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 | * | |
20ca73bc GW |
32 | * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>, |
33 | * 2006. | |
34 | * | |
b63862f4 DK |
35 | * The support of additional filter rules compares (>, <, >=, <=) was |
36 | * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005. | |
37 | * | |
73241ccc AG |
38 | * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional |
39 | * filesystem information. | |
8c8570fb DK |
40 | * |
41 | * Subject and object context labeling support added by <danjones@us.ibm.com> | |
42 | * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance. | |
1da177e4 LT |
43 | */ |
44 | ||
45 | #include <linux/init.h> | |
1da177e4 | 46 | #include <asm/types.h> |
715b49ef | 47 | #include <asm/atomic.h> |
73241ccc AG |
48 | #include <linux/fs.h> |
49 | #include <linux/namei.h> | |
1da177e4 LT |
50 | #include <linux/mm.h> |
51 | #include <linux/module.h> | |
01116105 | 52 | #include <linux/mount.h> |
3ec3b2fb | 53 | #include <linux/socket.h> |
20ca73bc | 54 | #include <linux/mqueue.h> |
1da177e4 LT |
55 | #include <linux/audit.h> |
56 | #include <linux/personality.h> | |
57 | #include <linux/time.h> | |
5bb289b5 | 58 | #include <linux/netlink.h> |
f5561964 | 59 | #include <linux/compiler.h> |
1da177e4 | 60 | #include <asm/unistd.h> |
8c8570fb | 61 | #include <linux/security.h> |
fe7752ba | 62 | #include <linux/list.h> |
a6c043a8 | 63 | #include <linux/tty.h> |
473ae30b | 64 | #include <linux/binfmts.h> |
a1f8e7f7 | 65 | #include <linux/highmem.h> |
f46038ff | 66 | #include <linux/syscalls.h> |
74c3cbe3 | 67 | #include <linux/inotify.h> |
851f7ff5 | 68 | #include <linux/capability.h> |
1da177e4 | 69 | |
fe7752ba | 70 | #include "audit.h" |
1da177e4 | 71 | |
1da177e4 LT |
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 | ||
9c937dcc AG |
76 | /* Indicates that audit should log the full pathname. */ |
77 | #define AUDIT_NAME_FULL -1 | |
78 | ||
de6bbd1d EP |
79 | /* no execve audit message should be longer than this (userspace limits) */ |
80 | #define MAX_EXECVE_AUDIT_LEN 7500 | |
81 | ||
471a5c7c AV |
82 | /* number of audit rules */ |
83 | int audit_n_rules; | |
84 | ||
e54dc243 AG |
85 | /* determines whether we collect data for signals sent */ |
86 | int audit_signals; | |
87 | ||
851f7ff5 EP |
88 | struct audit_cap_data { |
89 | kernel_cap_t permitted; | |
90 | kernel_cap_t inheritable; | |
91 | union { | |
92 | unsigned int fE; /* effective bit of a file capability */ | |
93 | kernel_cap_t effective; /* effective set of a process */ | |
94 | }; | |
95 | }; | |
96 | ||
1da177e4 LT |
97 | /* When fs/namei.c:getname() is called, we store the pointer in name and |
98 | * we don't let putname() free it (instead we free all of the saved | |
99 | * pointers at syscall exit time). | |
100 | * | |
101 | * Further, in fs/namei.c:path_lookup() we store the inode and device. */ | |
102 | struct audit_names { | |
103 | const char *name; | |
9c937dcc AG |
104 | int name_len; /* number of name's characters to log */ |
105 | unsigned name_put; /* call __putname() for this name */ | |
1da177e4 LT |
106 | unsigned long ino; |
107 | dev_t dev; | |
108 | umode_t mode; | |
109 | uid_t uid; | |
110 | gid_t gid; | |
111 | dev_t rdev; | |
1b50eed9 | 112 | u32 osid; |
851f7ff5 EP |
113 | struct audit_cap_data fcap; |
114 | unsigned int fcap_ver; | |
1da177e4 LT |
115 | }; |
116 | ||
117 | struct audit_aux_data { | |
118 | struct audit_aux_data *next; | |
119 | int type; | |
120 | }; | |
121 | ||
122 | #define AUDIT_AUX_IPCPERM 0 | |
123 | ||
e54dc243 AG |
124 | /* Number of target pids per aux struct. */ |
125 | #define AUDIT_AUX_PIDS 16 | |
126 | ||
20ca73bc GW |
127 | struct audit_aux_data_mq_open { |
128 | struct audit_aux_data d; | |
129 | int oflag; | |
130 | mode_t mode; | |
131 | struct mq_attr attr; | |
132 | }; | |
133 | ||
134 | struct audit_aux_data_mq_sendrecv { | |
135 | struct audit_aux_data d; | |
136 | mqd_t mqdes; | |
137 | size_t msg_len; | |
138 | unsigned int msg_prio; | |
139 | struct timespec abs_timeout; | |
140 | }; | |
141 | ||
142 | struct audit_aux_data_mq_notify { | |
143 | struct audit_aux_data d; | |
144 | mqd_t mqdes; | |
145 | struct sigevent notification; | |
146 | }; | |
147 | ||
148 | struct audit_aux_data_mq_getsetattr { | |
149 | struct audit_aux_data d; | |
150 | mqd_t mqdes; | |
151 | struct mq_attr mqstat; | |
152 | }; | |
153 | ||
1da177e4 LT |
154 | struct audit_aux_data_ipcctl { |
155 | struct audit_aux_data d; | |
156 | struct ipc_perm p; | |
157 | unsigned long qbytes; | |
158 | uid_t uid; | |
159 | gid_t gid; | |
160 | mode_t mode; | |
9c7aa6aa | 161 | u32 osid; |
1da177e4 LT |
162 | }; |
163 | ||
473ae30b AV |
164 | struct audit_aux_data_execve { |
165 | struct audit_aux_data d; | |
166 | int argc; | |
167 | int envc; | |
bdf4c48a | 168 | struct mm_struct *mm; |
473ae30b AV |
169 | }; |
170 | ||
3ec3b2fb DW |
171 | struct audit_aux_data_socketcall { |
172 | struct audit_aux_data d; | |
173 | int nargs; | |
174 | unsigned long args[0]; | |
175 | }; | |
176 | ||
177 | struct audit_aux_data_sockaddr { | |
178 | struct audit_aux_data d; | |
179 | int len; | |
180 | char a[0]; | |
181 | }; | |
182 | ||
db349509 AV |
183 | struct audit_aux_data_fd_pair { |
184 | struct audit_aux_data d; | |
185 | int fd[2]; | |
186 | }; | |
187 | ||
e54dc243 AG |
188 | struct audit_aux_data_pids { |
189 | struct audit_aux_data d; | |
190 | pid_t target_pid[AUDIT_AUX_PIDS]; | |
c2a7780e EP |
191 | uid_t target_auid[AUDIT_AUX_PIDS]; |
192 | uid_t target_uid[AUDIT_AUX_PIDS]; | |
4746ec5b | 193 | unsigned int target_sessionid[AUDIT_AUX_PIDS]; |
e54dc243 | 194 | u32 target_sid[AUDIT_AUX_PIDS]; |
c2a7780e | 195 | char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN]; |
e54dc243 AG |
196 | int pid_count; |
197 | }; | |
198 | ||
74c3cbe3 AV |
199 | struct audit_tree_refs { |
200 | struct audit_tree_refs *next; | |
201 | struct audit_chunk *c[31]; | |
202 | }; | |
203 | ||
1da177e4 LT |
204 | /* The per-task audit context. */ |
205 | struct audit_context { | |
d51374ad | 206 | int dummy; /* must be the first element */ |
1da177e4 LT |
207 | int in_syscall; /* 1 if task is in a syscall */ |
208 | enum audit_state state; | |
209 | unsigned int serial; /* serial number for record */ | |
210 | struct timespec ctime; /* time of syscall entry */ | |
1da177e4 LT |
211 | int major; /* syscall number */ |
212 | unsigned long argv[4]; /* syscall arguments */ | |
213 | int return_valid; /* return code is valid */ | |
2fd6f58b | 214 | long return_code;/* syscall return code */ |
1da177e4 LT |
215 | int auditable; /* 1 if record should be written */ |
216 | int name_count; | |
217 | struct audit_names names[AUDIT_NAMES]; | |
5adc8a6a | 218 | char * filterkey; /* key for rule that triggered record */ |
44707fdf | 219 | struct path pwd; |
1da177e4 LT |
220 | struct audit_context *previous; /* For nested syscalls */ |
221 | struct audit_aux_data *aux; | |
e54dc243 | 222 | struct audit_aux_data *aux_pids; |
1da177e4 LT |
223 | |
224 | /* Save things to print about task_struct */ | |
f46038ff | 225 | pid_t pid, ppid; |
1da177e4 LT |
226 | uid_t uid, euid, suid, fsuid; |
227 | gid_t gid, egid, sgid, fsgid; | |
228 | unsigned long personality; | |
2fd6f58b | 229 | int arch; |
1da177e4 | 230 | |
a5cb013d | 231 | pid_t target_pid; |
c2a7780e EP |
232 | uid_t target_auid; |
233 | uid_t target_uid; | |
4746ec5b | 234 | unsigned int target_sessionid; |
a5cb013d | 235 | u32 target_sid; |
c2a7780e | 236 | char target_comm[TASK_COMM_LEN]; |
a5cb013d | 237 | |
74c3cbe3 AV |
238 | struct audit_tree_refs *trees, *first_trees; |
239 | int tree_count; | |
240 | ||
1da177e4 LT |
241 | #if AUDIT_DEBUG |
242 | int put_count; | |
243 | int ino_count; | |
244 | #endif | |
245 | }; | |
246 | ||
55669bfa AV |
247 | #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE]) |
248 | static inline int open_arg(int flags, int mask) | |
249 | { | |
250 | int n = ACC_MODE(flags); | |
251 | if (flags & (O_TRUNC | O_CREAT)) | |
252 | n |= AUDIT_PERM_WRITE; | |
253 | return n & mask; | |
254 | } | |
255 | ||
256 | static int audit_match_perm(struct audit_context *ctx, int mask) | |
257 | { | |
c4bacefb | 258 | unsigned n; |
1a61c88d | 259 | if (unlikely(!ctx)) |
260 | return 0; | |
c4bacefb | 261 | n = ctx->major; |
dbda4c0b | 262 | |
55669bfa AV |
263 | switch (audit_classify_syscall(ctx->arch, n)) { |
264 | case 0: /* native */ | |
265 | if ((mask & AUDIT_PERM_WRITE) && | |
266 | audit_match_class(AUDIT_CLASS_WRITE, n)) | |
267 | return 1; | |
268 | if ((mask & AUDIT_PERM_READ) && | |
269 | audit_match_class(AUDIT_CLASS_READ, n)) | |
270 | return 1; | |
271 | if ((mask & AUDIT_PERM_ATTR) && | |
272 | audit_match_class(AUDIT_CLASS_CHATTR, n)) | |
273 | return 1; | |
274 | return 0; | |
275 | case 1: /* 32bit on biarch */ | |
276 | if ((mask & AUDIT_PERM_WRITE) && | |
277 | audit_match_class(AUDIT_CLASS_WRITE_32, n)) | |
278 | return 1; | |
279 | if ((mask & AUDIT_PERM_READ) && | |
280 | audit_match_class(AUDIT_CLASS_READ_32, n)) | |
281 | return 1; | |
282 | if ((mask & AUDIT_PERM_ATTR) && | |
283 | audit_match_class(AUDIT_CLASS_CHATTR_32, n)) | |
284 | return 1; | |
285 | return 0; | |
286 | case 2: /* open */ | |
287 | return mask & ACC_MODE(ctx->argv[1]); | |
288 | case 3: /* openat */ | |
289 | return mask & ACC_MODE(ctx->argv[2]); | |
290 | case 4: /* socketcall */ | |
291 | return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND); | |
292 | case 5: /* execve */ | |
293 | return mask & AUDIT_PERM_EXEC; | |
294 | default: | |
295 | return 0; | |
296 | } | |
297 | } | |
298 | ||
8b67dca9 AV |
299 | static int audit_match_filetype(struct audit_context *ctx, int which) |
300 | { | |
301 | unsigned index = which & ~S_IFMT; | |
302 | mode_t mode = which & S_IFMT; | |
1a61c88d | 303 | |
304 | if (unlikely(!ctx)) | |
305 | return 0; | |
306 | ||
8b67dca9 AV |
307 | if (index >= ctx->name_count) |
308 | return 0; | |
309 | if (ctx->names[index].ino == -1) | |
310 | return 0; | |
311 | if ((ctx->names[index].mode ^ mode) & S_IFMT) | |
312 | return 0; | |
313 | return 1; | |
314 | } | |
315 | ||
74c3cbe3 AV |
316 | /* |
317 | * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *; | |
318 | * ->first_trees points to its beginning, ->trees - to the current end of data. | |
319 | * ->tree_count is the number of free entries in array pointed to by ->trees. | |
320 | * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL, | |
321 | * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously, | |
322 | * it's going to remain 1-element for almost any setup) until we free context itself. | |
323 | * References in it _are_ dropped - at the same time we free/drop aux stuff. | |
324 | */ | |
325 | ||
326 | #ifdef CONFIG_AUDIT_TREE | |
327 | static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk) | |
328 | { | |
329 | struct audit_tree_refs *p = ctx->trees; | |
330 | int left = ctx->tree_count; | |
331 | if (likely(left)) { | |
332 | p->c[--left] = chunk; | |
333 | ctx->tree_count = left; | |
334 | return 1; | |
335 | } | |
336 | if (!p) | |
337 | return 0; | |
338 | p = p->next; | |
339 | if (p) { | |
340 | p->c[30] = chunk; | |
341 | ctx->trees = p; | |
342 | ctx->tree_count = 30; | |
343 | return 1; | |
344 | } | |
345 | return 0; | |
346 | } | |
347 | ||
348 | static int grow_tree_refs(struct audit_context *ctx) | |
349 | { | |
350 | struct audit_tree_refs *p = ctx->trees; | |
351 | ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL); | |
352 | if (!ctx->trees) { | |
353 | ctx->trees = p; | |
354 | return 0; | |
355 | } | |
356 | if (p) | |
357 | p->next = ctx->trees; | |
358 | else | |
359 | ctx->first_trees = ctx->trees; | |
360 | ctx->tree_count = 31; | |
361 | return 1; | |
362 | } | |
363 | #endif | |
364 | ||
365 | static void unroll_tree_refs(struct audit_context *ctx, | |
366 | struct audit_tree_refs *p, int count) | |
367 | { | |
368 | #ifdef CONFIG_AUDIT_TREE | |
369 | struct audit_tree_refs *q; | |
370 | int n; | |
371 | if (!p) { | |
372 | /* we started with empty chain */ | |
373 | p = ctx->first_trees; | |
374 | count = 31; | |
375 | /* if the very first allocation has failed, nothing to do */ | |
376 | if (!p) | |
377 | return; | |
378 | } | |
379 | n = count; | |
380 | for (q = p; q != ctx->trees; q = q->next, n = 31) { | |
381 | while (n--) { | |
382 | audit_put_chunk(q->c[n]); | |
383 | q->c[n] = NULL; | |
384 | } | |
385 | } | |
386 | while (n-- > ctx->tree_count) { | |
387 | audit_put_chunk(q->c[n]); | |
388 | q->c[n] = NULL; | |
389 | } | |
390 | ctx->trees = p; | |
391 | ctx->tree_count = count; | |
392 | #endif | |
393 | } | |
394 | ||
395 | static void free_tree_refs(struct audit_context *ctx) | |
396 | { | |
397 | struct audit_tree_refs *p, *q; | |
398 | for (p = ctx->first_trees; p; p = q) { | |
399 | q = p->next; | |
400 | kfree(p); | |
401 | } | |
402 | } | |
403 | ||
404 | static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree) | |
405 | { | |
406 | #ifdef CONFIG_AUDIT_TREE | |
407 | struct audit_tree_refs *p; | |
408 | int n; | |
409 | if (!tree) | |
410 | return 0; | |
411 | /* full ones */ | |
412 | for (p = ctx->first_trees; p != ctx->trees; p = p->next) { | |
413 | for (n = 0; n < 31; n++) | |
414 | if (audit_tree_match(p->c[n], tree)) | |
415 | return 1; | |
416 | } | |
417 | /* partial */ | |
418 | if (p) { | |
419 | for (n = ctx->tree_count; n < 31; n++) | |
420 | if (audit_tree_match(p->c[n], tree)) | |
421 | return 1; | |
422 | } | |
423 | #endif | |
424 | return 0; | |
425 | } | |
426 | ||
f368c07d | 427 | /* Determine if any context name data matches a rule's watch data */ |
1da177e4 LT |
428 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 |
429 | * otherwise. */ | |
430 | static int audit_filter_rules(struct task_struct *tsk, | |
93315ed6 | 431 | struct audit_krule *rule, |
1da177e4 | 432 | struct audit_context *ctx, |
f368c07d | 433 | struct audit_names *name, |
1da177e4 LT |
434 | enum audit_state *state) |
435 | { | |
2ad312d2 | 436 | int i, j, need_sid = 1; |
3dc7e315 DG |
437 | u32 sid; |
438 | ||
1da177e4 | 439 | for (i = 0; i < rule->field_count; i++) { |
93315ed6 | 440 | struct audit_field *f = &rule->fields[i]; |
1da177e4 LT |
441 | int result = 0; |
442 | ||
93315ed6 | 443 | switch (f->type) { |
1da177e4 | 444 | case AUDIT_PID: |
93315ed6 | 445 | result = audit_comparator(tsk->pid, f->op, f->val); |
1da177e4 | 446 | break; |
3c66251e | 447 | case AUDIT_PPID: |
419c58f1 AV |
448 | if (ctx) { |
449 | if (!ctx->ppid) | |
450 | ctx->ppid = sys_getppid(); | |
3c66251e | 451 | result = audit_comparator(ctx->ppid, f->op, f->val); |
419c58f1 | 452 | } |
3c66251e | 453 | break; |
1da177e4 | 454 | case AUDIT_UID: |
93315ed6 | 455 | result = audit_comparator(tsk->uid, f->op, f->val); |
1da177e4 LT |
456 | break; |
457 | case AUDIT_EUID: | |
93315ed6 | 458 | result = audit_comparator(tsk->euid, f->op, f->val); |
1da177e4 LT |
459 | break; |
460 | case AUDIT_SUID: | |
93315ed6 | 461 | result = audit_comparator(tsk->suid, f->op, f->val); |
1da177e4 LT |
462 | break; |
463 | case AUDIT_FSUID: | |
93315ed6 | 464 | result = audit_comparator(tsk->fsuid, f->op, f->val); |
1da177e4 LT |
465 | break; |
466 | case AUDIT_GID: | |
93315ed6 | 467 | result = audit_comparator(tsk->gid, f->op, f->val); |
1da177e4 LT |
468 | break; |
469 | case AUDIT_EGID: | |
93315ed6 | 470 | result = audit_comparator(tsk->egid, f->op, f->val); |
1da177e4 LT |
471 | break; |
472 | case AUDIT_SGID: | |
93315ed6 | 473 | result = audit_comparator(tsk->sgid, f->op, f->val); |
1da177e4 LT |
474 | break; |
475 | case AUDIT_FSGID: | |
93315ed6 | 476 | result = audit_comparator(tsk->fsgid, f->op, f->val); |
1da177e4 LT |
477 | break; |
478 | case AUDIT_PERS: | |
93315ed6 | 479 | result = audit_comparator(tsk->personality, f->op, f->val); |
1da177e4 | 480 | break; |
2fd6f58b | 481 | case AUDIT_ARCH: |
9f8dbe9c | 482 | if (ctx) |
93315ed6 | 483 | result = audit_comparator(ctx->arch, f->op, f->val); |
2fd6f58b | 484 | break; |
1da177e4 LT |
485 | |
486 | case AUDIT_EXIT: | |
487 | if (ctx && ctx->return_valid) | |
93315ed6 | 488 | result = audit_comparator(ctx->return_code, f->op, f->val); |
1da177e4 LT |
489 | break; |
490 | case AUDIT_SUCCESS: | |
b01f2cc1 | 491 | if (ctx && ctx->return_valid) { |
93315ed6 AG |
492 | if (f->val) |
493 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS); | |
b01f2cc1 | 494 | else |
93315ed6 | 495 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE); |
b01f2cc1 | 496 | } |
1da177e4 LT |
497 | break; |
498 | case AUDIT_DEVMAJOR: | |
f368c07d AG |
499 | if (name) |
500 | result = audit_comparator(MAJOR(name->dev), | |
501 | f->op, f->val); | |
502 | else if (ctx) { | |
1da177e4 | 503 | for (j = 0; j < ctx->name_count; j++) { |
93315ed6 | 504 | if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) { |
1da177e4 LT |
505 | ++result; |
506 | break; | |
507 | } | |
508 | } | |
509 | } | |
510 | break; | |
511 | case AUDIT_DEVMINOR: | |
f368c07d AG |
512 | if (name) |
513 | result = audit_comparator(MINOR(name->dev), | |
514 | f->op, f->val); | |
515 | else if (ctx) { | |
1da177e4 | 516 | for (j = 0; j < ctx->name_count; j++) { |
93315ed6 | 517 | if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) { |
1da177e4 LT |
518 | ++result; |
519 | break; | |
520 | } | |
521 | } | |
522 | } | |
523 | break; | |
524 | case AUDIT_INODE: | |
f368c07d | 525 | if (name) |
9c937dcc | 526 | result = (name->ino == f->val); |
f368c07d | 527 | else if (ctx) { |
1da177e4 | 528 | for (j = 0; j < ctx->name_count; j++) { |
9c937dcc | 529 | if (audit_comparator(ctx->names[j].ino, f->op, f->val)) { |
1da177e4 LT |
530 | ++result; |
531 | break; | |
532 | } | |
533 | } | |
534 | } | |
535 | break; | |
f368c07d AG |
536 | case AUDIT_WATCH: |
537 | if (name && rule->watch->ino != (unsigned long)-1) | |
538 | result = (name->dev == rule->watch->dev && | |
9c937dcc | 539 | name->ino == rule->watch->ino); |
f368c07d | 540 | break; |
74c3cbe3 AV |
541 | case AUDIT_DIR: |
542 | if (ctx) | |
543 | result = match_tree_refs(ctx, rule->tree); | |
544 | break; | |
1da177e4 LT |
545 | case AUDIT_LOGINUID: |
546 | result = 0; | |
547 | if (ctx) | |
bfef93a5 | 548 | result = audit_comparator(tsk->loginuid, f->op, f->val); |
1da177e4 | 549 | break; |
3a6b9f85 DG |
550 | case AUDIT_SUBJ_USER: |
551 | case AUDIT_SUBJ_ROLE: | |
552 | case AUDIT_SUBJ_TYPE: | |
553 | case AUDIT_SUBJ_SEN: | |
554 | case AUDIT_SUBJ_CLR: | |
3dc7e315 DG |
555 | /* NOTE: this may return negative values indicating |
556 | a temporary error. We simply treat this as a | |
557 | match for now to avoid losing information that | |
558 | may be wanted. An error message will also be | |
559 | logged upon error */ | |
04305e4a | 560 | if (f->lsm_rule) { |
2ad312d2 | 561 | if (need_sid) { |
2a862b32 | 562 | security_task_getsecid(tsk, &sid); |
2ad312d2 SG |
563 | need_sid = 0; |
564 | } | |
d7a96f3a | 565 | result = security_audit_rule_match(sid, f->type, |
3dc7e315 | 566 | f->op, |
04305e4a | 567 | f->lsm_rule, |
3dc7e315 | 568 | ctx); |
2ad312d2 | 569 | } |
3dc7e315 | 570 | break; |
6e5a2d1d DG |
571 | case AUDIT_OBJ_USER: |
572 | case AUDIT_OBJ_ROLE: | |
573 | case AUDIT_OBJ_TYPE: | |
574 | case AUDIT_OBJ_LEV_LOW: | |
575 | case AUDIT_OBJ_LEV_HIGH: | |
576 | /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR | |
577 | also applies here */ | |
04305e4a | 578 | if (f->lsm_rule) { |
6e5a2d1d DG |
579 | /* Find files that match */ |
580 | if (name) { | |
d7a96f3a | 581 | result = security_audit_rule_match( |
6e5a2d1d | 582 | name->osid, f->type, f->op, |
04305e4a | 583 | f->lsm_rule, ctx); |
6e5a2d1d DG |
584 | } else if (ctx) { |
585 | for (j = 0; j < ctx->name_count; j++) { | |
d7a96f3a | 586 | if (security_audit_rule_match( |
6e5a2d1d DG |
587 | ctx->names[j].osid, |
588 | f->type, f->op, | |
04305e4a | 589 | f->lsm_rule, ctx)) { |
6e5a2d1d DG |
590 | ++result; |
591 | break; | |
592 | } | |
593 | } | |
594 | } | |
595 | /* Find ipc objects that match */ | |
596 | if (ctx) { | |
597 | struct audit_aux_data *aux; | |
598 | for (aux = ctx->aux; aux; | |
599 | aux = aux->next) { | |
600 | if (aux->type == AUDIT_IPC) { | |
601 | struct audit_aux_data_ipcctl *axi = (void *)aux; | |
04305e4a | 602 | if (security_audit_rule_match(axi->osid, f->type, f->op, f->lsm_rule, ctx)) { |
6e5a2d1d DG |
603 | ++result; |
604 | break; | |
605 | } | |
606 | } | |
607 | } | |
608 | } | |
609 | } | |
610 | break; | |
1da177e4 LT |
611 | case AUDIT_ARG0: |
612 | case AUDIT_ARG1: | |
613 | case AUDIT_ARG2: | |
614 | case AUDIT_ARG3: | |
615 | if (ctx) | |
93315ed6 | 616 | result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val); |
1da177e4 | 617 | break; |
5adc8a6a AG |
618 | case AUDIT_FILTERKEY: |
619 | /* ignore this field for filtering */ | |
620 | result = 1; | |
621 | break; | |
55669bfa AV |
622 | case AUDIT_PERM: |
623 | result = audit_match_perm(ctx, f->val); | |
624 | break; | |
8b67dca9 AV |
625 | case AUDIT_FILETYPE: |
626 | result = audit_match_filetype(ctx, f->val); | |
627 | break; | |
1da177e4 LT |
628 | } |
629 | ||
1da177e4 LT |
630 | if (!result) |
631 | return 0; | |
632 | } | |
980dfb0d | 633 | if (rule->filterkey && ctx) |
5adc8a6a | 634 | ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC); |
1da177e4 LT |
635 | switch (rule->action) { |
636 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; | |
1da177e4 LT |
637 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
638 | } | |
639 | return 1; | |
640 | } | |
641 | ||
642 | /* At process creation time, we can determine if system-call auditing is | |
643 | * completely disabled for this task. Since we only have the task | |
644 | * structure at this point, we can only check uid and gid. | |
645 | */ | |
646 | static enum audit_state audit_filter_task(struct task_struct *tsk) | |
647 | { | |
648 | struct audit_entry *e; | |
649 | enum audit_state state; | |
650 | ||
651 | rcu_read_lock(); | |
0f45aa18 | 652 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) { |
f368c07d | 653 | if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) { |
1da177e4 LT |
654 | rcu_read_unlock(); |
655 | return state; | |
656 | } | |
657 | } | |
658 | rcu_read_unlock(); | |
659 | return AUDIT_BUILD_CONTEXT; | |
660 | } | |
661 | ||
662 | /* At syscall entry and exit time, this filter is called if the | |
663 | * audit_state is not low enough that auditing cannot take place, but is | |
23f32d18 | 664 | * also not high enough that we already know we have to write an audit |
b0dd25a8 | 665 | * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT). |
1da177e4 LT |
666 | */ |
667 | static enum audit_state audit_filter_syscall(struct task_struct *tsk, | |
668 | struct audit_context *ctx, | |
669 | struct list_head *list) | |
670 | { | |
671 | struct audit_entry *e; | |
c3896495 | 672 | enum audit_state state; |
1da177e4 | 673 | |
351bb722 | 674 | if (audit_pid && tsk->tgid == audit_pid) |
f7056d64 DW |
675 | return AUDIT_DISABLED; |
676 | ||
1da177e4 | 677 | rcu_read_lock(); |
c3896495 | 678 | if (!list_empty(list)) { |
b63862f4 DK |
679 | int word = AUDIT_WORD(ctx->major); |
680 | int bit = AUDIT_BIT(ctx->major); | |
681 | ||
682 | list_for_each_entry_rcu(e, list, list) { | |
f368c07d AG |
683 | if ((e->rule.mask[word] & bit) == bit && |
684 | audit_filter_rules(tsk, &e->rule, ctx, NULL, | |
685 | &state)) { | |
686 | rcu_read_unlock(); | |
687 | return state; | |
688 | } | |
689 | } | |
690 | } | |
691 | rcu_read_unlock(); | |
692 | return AUDIT_BUILD_CONTEXT; | |
693 | } | |
694 | ||
695 | /* At syscall exit time, this filter is called if any audit_names[] have been | |
696 | * collected during syscall processing. We only check rules in sublists at hash | |
697 | * buckets applicable to the inode numbers in audit_names[]. | |
698 | * Regarding audit_state, same rules apply as for audit_filter_syscall(). | |
699 | */ | |
700 | enum audit_state audit_filter_inodes(struct task_struct *tsk, | |
701 | struct audit_context *ctx) | |
702 | { | |
703 | int i; | |
704 | struct audit_entry *e; | |
705 | enum audit_state state; | |
706 | ||
707 | if (audit_pid && tsk->tgid == audit_pid) | |
708 | return AUDIT_DISABLED; | |
709 | ||
710 | rcu_read_lock(); | |
711 | for (i = 0; i < ctx->name_count; i++) { | |
712 | int word = AUDIT_WORD(ctx->major); | |
713 | int bit = AUDIT_BIT(ctx->major); | |
714 | struct audit_names *n = &ctx->names[i]; | |
715 | int h = audit_hash_ino((u32)n->ino); | |
716 | struct list_head *list = &audit_inode_hash[h]; | |
717 | ||
718 | if (list_empty(list)) | |
719 | continue; | |
720 | ||
721 | list_for_each_entry_rcu(e, list, list) { | |
722 | if ((e->rule.mask[word] & bit) == bit && | |
723 | audit_filter_rules(tsk, &e->rule, ctx, n, &state)) { | |
b63862f4 DK |
724 | rcu_read_unlock(); |
725 | return state; | |
726 | } | |
0f45aa18 DW |
727 | } |
728 | } | |
729 | rcu_read_unlock(); | |
1da177e4 | 730 | return AUDIT_BUILD_CONTEXT; |
0f45aa18 DW |
731 | } |
732 | ||
f368c07d AG |
733 | void audit_set_auditable(struct audit_context *ctx) |
734 | { | |
735 | ctx->auditable = 1; | |
736 | } | |
737 | ||
1da177e4 LT |
738 | static inline struct audit_context *audit_get_context(struct task_struct *tsk, |
739 | int return_valid, | |
740 | int return_code) | |
741 | { | |
742 | struct audit_context *context = tsk->audit_context; | |
743 | ||
744 | if (likely(!context)) | |
745 | return NULL; | |
746 | context->return_valid = return_valid; | |
f701b75e EP |
747 | |
748 | /* | |
749 | * we need to fix up the return code in the audit logs if the actual | |
750 | * return codes are later going to be fixed up by the arch specific | |
751 | * signal handlers | |
752 | * | |
753 | * This is actually a test for: | |
754 | * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) || | |
755 | * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK) | |
756 | * | |
757 | * but is faster than a bunch of || | |
758 | */ | |
759 | if (unlikely(return_code <= -ERESTARTSYS) && | |
760 | (return_code >= -ERESTART_RESTARTBLOCK) && | |
761 | (return_code != -ENOIOCTLCMD)) | |
762 | context->return_code = -EINTR; | |
763 | else | |
764 | context->return_code = return_code; | |
1da177e4 | 765 | |
d51374ad | 766 | if (context->in_syscall && !context->dummy && !context->auditable) { |
1da177e4 | 767 | enum audit_state state; |
f368c07d | 768 | |
0f45aa18 | 769 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]); |
f368c07d AG |
770 | if (state == AUDIT_RECORD_CONTEXT) { |
771 | context->auditable = 1; | |
772 | goto get_context; | |
773 | } | |
774 | ||
775 | state = audit_filter_inodes(tsk, context); | |
1da177e4 LT |
776 | if (state == AUDIT_RECORD_CONTEXT) |
777 | context->auditable = 1; | |
f368c07d | 778 | |
1da177e4 LT |
779 | } |
780 | ||
f368c07d | 781 | get_context: |
3f2792ff | 782 | |
1da177e4 LT |
783 | tsk->audit_context = NULL; |
784 | return context; | |
785 | } | |
786 | ||
787 | static inline void audit_free_names(struct audit_context *context) | |
788 | { | |
789 | int i; | |
790 | ||
791 | #if AUDIT_DEBUG == 2 | |
792 | if (context->auditable | |
793 | ||context->put_count + context->ino_count != context->name_count) { | |
73241ccc | 794 | printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d" |
1da177e4 LT |
795 | " name_count=%d put_count=%d" |
796 | " ino_count=%d [NOT freeing]\n", | |
73241ccc | 797 | __FILE__, __LINE__, |
1da177e4 LT |
798 | context->serial, context->major, context->in_syscall, |
799 | context->name_count, context->put_count, | |
800 | context->ino_count); | |
8c8570fb | 801 | for (i = 0; i < context->name_count; i++) { |
1da177e4 LT |
802 | printk(KERN_ERR "names[%d] = %p = %s\n", i, |
803 | context->names[i].name, | |
73241ccc | 804 | context->names[i].name ?: "(null)"); |
8c8570fb | 805 | } |
1da177e4 LT |
806 | dump_stack(); |
807 | return; | |
808 | } | |
809 | #endif | |
810 | #if AUDIT_DEBUG | |
811 | context->put_count = 0; | |
812 | context->ino_count = 0; | |
813 | #endif | |
814 | ||
8c8570fb | 815 | for (i = 0; i < context->name_count; i++) { |
9c937dcc | 816 | if (context->names[i].name && context->names[i].name_put) |
1da177e4 | 817 | __putname(context->names[i].name); |
8c8570fb | 818 | } |
1da177e4 | 819 | context->name_count = 0; |
44707fdf JB |
820 | path_put(&context->pwd); |
821 | context->pwd.dentry = NULL; | |
822 | context->pwd.mnt = NULL; | |
1da177e4 LT |
823 | } |
824 | ||
825 | static inline void audit_free_aux(struct audit_context *context) | |
826 | { | |
827 | struct audit_aux_data *aux; | |
828 | ||
829 | while ((aux = context->aux)) { | |
830 | context->aux = aux->next; | |
831 | kfree(aux); | |
832 | } | |
e54dc243 AG |
833 | while ((aux = context->aux_pids)) { |
834 | context->aux_pids = aux->next; | |
835 | kfree(aux); | |
836 | } | |
1da177e4 LT |
837 | } |
838 | ||
839 | static inline void audit_zero_context(struct audit_context *context, | |
840 | enum audit_state state) | |
841 | { | |
1da177e4 LT |
842 | memset(context, 0, sizeof(*context)); |
843 | context->state = state; | |
1da177e4 LT |
844 | } |
845 | ||
846 | static inline struct audit_context *audit_alloc_context(enum audit_state state) | |
847 | { | |
848 | struct audit_context *context; | |
849 | ||
850 | if (!(context = kmalloc(sizeof(*context), GFP_KERNEL))) | |
851 | return NULL; | |
852 | audit_zero_context(context, state); | |
853 | return context; | |
854 | } | |
855 | ||
b0dd25a8 RD |
856 | /** |
857 | * audit_alloc - allocate an audit context block for a task | |
858 | * @tsk: task | |
859 | * | |
860 | * Filter on the task information and allocate a per-task audit context | |
1da177e4 LT |
861 | * if necessary. Doing so turns on system call auditing for the |
862 | * specified task. This is called from copy_process, so no lock is | |
b0dd25a8 RD |
863 | * needed. |
864 | */ | |
1da177e4 LT |
865 | int audit_alloc(struct task_struct *tsk) |
866 | { | |
867 | struct audit_context *context; | |
868 | enum audit_state state; | |
869 | ||
b593d384 | 870 | if (likely(!audit_ever_enabled)) |
1da177e4 LT |
871 | return 0; /* Return if not auditing. */ |
872 | ||
873 | state = audit_filter_task(tsk); | |
874 | if (likely(state == AUDIT_DISABLED)) | |
875 | return 0; | |
876 | ||
877 | if (!(context = audit_alloc_context(state))) { | |
878 | audit_log_lost("out of memory in audit_alloc"); | |
879 | return -ENOMEM; | |
880 | } | |
881 | ||
1da177e4 LT |
882 | tsk->audit_context = context; |
883 | set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); | |
884 | return 0; | |
885 | } | |
886 | ||
887 | static inline void audit_free_context(struct audit_context *context) | |
888 | { | |
889 | struct audit_context *previous; | |
890 | int count = 0; | |
891 | ||
892 | do { | |
893 | previous = context->previous; | |
894 | if (previous || (count && count < 10)) { | |
895 | ++count; | |
896 | printk(KERN_ERR "audit(:%d): major=%d name_count=%d:" | |
897 | " freeing multiple contexts (%d)\n", | |
898 | context->serial, context->major, | |
899 | context->name_count, count); | |
900 | } | |
901 | audit_free_names(context); | |
74c3cbe3 AV |
902 | unroll_tree_refs(context, NULL, 0); |
903 | free_tree_refs(context); | |
1da177e4 | 904 | audit_free_aux(context); |
5adc8a6a | 905 | kfree(context->filterkey); |
1da177e4 LT |
906 | kfree(context); |
907 | context = previous; | |
908 | } while (context); | |
909 | if (count >= 10) | |
910 | printk(KERN_ERR "audit: freed %d contexts\n", count); | |
911 | } | |
912 | ||
161a09e7 | 913 | void audit_log_task_context(struct audit_buffer *ab) |
8c8570fb DK |
914 | { |
915 | char *ctx = NULL; | |
c4823bce AV |
916 | unsigned len; |
917 | int error; | |
918 | u32 sid; | |
919 | ||
2a862b32 | 920 | security_task_getsecid(current, &sid); |
c4823bce AV |
921 | if (!sid) |
922 | return; | |
8c8570fb | 923 | |
2a862b32 | 924 | error = security_secid_to_secctx(sid, &ctx, &len); |
c4823bce AV |
925 | if (error) { |
926 | if (error != -EINVAL) | |
8c8570fb DK |
927 | goto error_path; |
928 | return; | |
929 | } | |
930 | ||
8c8570fb | 931 | audit_log_format(ab, " subj=%s", ctx); |
2a862b32 | 932 | security_release_secctx(ctx, len); |
7306a0b9 | 933 | return; |
8c8570fb DK |
934 | |
935 | error_path: | |
7306a0b9 | 936 | audit_panic("error in audit_log_task_context"); |
8c8570fb DK |
937 | return; |
938 | } | |
939 | ||
161a09e7 JL |
940 | EXPORT_SYMBOL(audit_log_task_context); |
941 | ||
e495149b | 942 | static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk) |
219f0817 | 943 | { |
45d9bb0e AV |
944 | char name[sizeof(tsk->comm)]; |
945 | struct mm_struct *mm = tsk->mm; | |
219f0817 SS |
946 | struct vm_area_struct *vma; |
947 | ||
e495149b AV |
948 | /* tsk == current */ |
949 | ||
45d9bb0e | 950 | get_task_comm(name, tsk); |
99e45eea DW |
951 | audit_log_format(ab, " comm="); |
952 | audit_log_untrustedstring(ab, name); | |
219f0817 | 953 | |
e495149b AV |
954 | if (mm) { |
955 | down_read(&mm->mmap_sem); | |
956 | vma = mm->mmap; | |
957 | while (vma) { | |
958 | if ((vma->vm_flags & VM_EXECUTABLE) && | |
959 | vma->vm_file) { | |
960 | audit_log_d_path(ab, "exe=", | |
44707fdf | 961 | &vma->vm_file->f_path); |
e495149b AV |
962 | break; |
963 | } | |
964 | vma = vma->vm_next; | |
219f0817 | 965 | } |
e495149b | 966 | up_read(&mm->mmap_sem); |
219f0817 | 967 | } |
e495149b | 968 | audit_log_task_context(ab); |
219f0817 SS |
969 | } |
970 | ||
e54dc243 | 971 | static int audit_log_pid_context(struct audit_context *context, pid_t pid, |
4746ec5b EP |
972 | uid_t auid, uid_t uid, unsigned int sessionid, |
973 | u32 sid, char *comm) | |
e54dc243 AG |
974 | { |
975 | struct audit_buffer *ab; | |
2a862b32 | 976 | char *ctx = NULL; |
e54dc243 AG |
977 | u32 len; |
978 | int rc = 0; | |
979 | ||
980 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID); | |
981 | if (!ab) | |
6246ccab | 982 | return rc; |
e54dc243 | 983 | |
4746ec5b EP |
984 | audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid, |
985 | uid, sessionid); | |
2a862b32 | 986 | if (security_secid_to_secctx(sid, &ctx, &len)) { |
c2a7780e | 987 | audit_log_format(ab, " obj=(none)"); |
e54dc243 | 988 | rc = 1; |
2a862b32 AD |
989 | } else { |
990 | audit_log_format(ab, " obj=%s", ctx); | |
991 | security_release_secctx(ctx, len); | |
992 | } | |
c2a7780e EP |
993 | audit_log_format(ab, " ocomm="); |
994 | audit_log_untrustedstring(ab, comm); | |
e54dc243 | 995 | audit_log_end(ab); |
e54dc243 AG |
996 | |
997 | return rc; | |
998 | } | |
999 | ||
de6bbd1d EP |
1000 | /* |
1001 | * to_send and len_sent accounting are very loose estimates. We aren't | |
1002 | * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being | |
1003 | * within about 500 bytes (next page boundry) | |
1004 | * | |
1005 | * why snprintf? an int is up to 12 digits long. if we just assumed when | |
1006 | * logging that a[%d]= was going to be 16 characters long we would be wasting | |
1007 | * space in every audit message. In one 7500 byte message we can log up to | |
1008 | * about 1000 min size arguments. That comes down to about 50% waste of space | |
1009 | * if we didn't do the snprintf to find out how long arg_num_len was. | |
1010 | */ | |
1011 | static int audit_log_single_execve_arg(struct audit_context *context, | |
1012 | struct audit_buffer **ab, | |
1013 | int arg_num, | |
1014 | size_t *len_sent, | |
1015 | const char __user *p, | |
1016 | char *buf) | |
bdf4c48a | 1017 | { |
de6bbd1d EP |
1018 | char arg_num_len_buf[12]; |
1019 | const char __user *tmp_p = p; | |
1020 | /* how many digits are in arg_num? 3 is the length of a=\n */ | |
1021 | size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3; | |
1022 | size_t len, len_left, to_send; | |
1023 | size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN; | |
1024 | unsigned int i, has_cntl = 0, too_long = 0; | |
1025 | int ret; | |
1026 | ||
1027 | /* strnlen_user includes the null we don't want to send */ | |
1028 | len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1; | |
bdf4c48a | 1029 | |
de6bbd1d EP |
1030 | /* |
1031 | * We just created this mm, if we can't find the strings | |
1032 | * we just copied into it something is _very_ wrong. Similar | |
1033 | * for strings that are too long, we should not have created | |
1034 | * any. | |
1035 | */ | |
b0abcfc1 | 1036 | if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) { |
de6bbd1d EP |
1037 | WARN_ON(1); |
1038 | send_sig(SIGKILL, current, 0); | |
b0abcfc1 | 1039 | return -1; |
de6bbd1d | 1040 | } |
040b3a2d | 1041 | |
de6bbd1d EP |
1042 | /* walk the whole argument looking for non-ascii chars */ |
1043 | do { | |
1044 | if (len_left > MAX_EXECVE_AUDIT_LEN) | |
1045 | to_send = MAX_EXECVE_AUDIT_LEN; | |
1046 | else | |
1047 | to_send = len_left; | |
1048 | ret = copy_from_user(buf, tmp_p, to_send); | |
bdf4c48a | 1049 | /* |
de6bbd1d EP |
1050 | * There is no reason for this copy to be short. We just |
1051 | * copied them here, and the mm hasn't been exposed to user- | |
1052 | * space yet. | |
bdf4c48a | 1053 | */ |
de6bbd1d | 1054 | if (ret) { |
bdf4c48a PZ |
1055 | WARN_ON(1); |
1056 | send_sig(SIGKILL, current, 0); | |
b0abcfc1 | 1057 | return -1; |
bdf4c48a | 1058 | } |
de6bbd1d EP |
1059 | buf[to_send] = '\0'; |
1060 | has_cntl = audit_string_contains_control(buf, to_send); | |
1061 | if (has_cntl) { | |
1062 | /* | |
1063 | * hex messages get logged as 2 bytes, so we can only | |
1064 | * send half as much in each message | |
1065 | */ | |
1066 | max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2; | |
bdf4c48a PZ |
1067 | break; |
1068 | } | |
de6bbd1d EP |
1069 | len_left -= to_send; |
1070 | tmp_p += to_send; | |
1071 | } while (len_left > 0); | |
1072 | ||
1073 | len_left = len; | |
1074 | ||
1075 | if (len > max_execve_audit_len) | |
1076 | too_long = 1; | |
1077 | ||
1078 | /* rewalk the argument actually logging the message */ | |
1079 | for (i = 0; len_left > 0; i++) { | |
1080 | int room_left; | |
1081 | ||
1082 | if (len_left > max_execve_audit_len) | |
1083 | to_send = max_execve_audit_len; | |
1084 | else | |
1085 | to_send = len_left; | |
1086 | ||
1087 | /* do we have space left to send this argument in this ab? */ | |
1088 | room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent; | |
1089 | if (has_cntl) | |
1090 | room_left -= (to_send * 2); | |
1091 | else | |
1092 | room_left -= to_send; | |
1093 | if (room_left < 0) { | |
1094 | *len_sent = 0; | |
1095 | audit_log_end(*ab); | |
1096 | *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE); | |
1097 | if (!*ab) | |
1098 | return 0; | |
1099 | } | |
bdf4c48a | 1100 | |
bdf4c48a | 1101 | /* |
de6bbd1d EP |
1102 | * first record needs to say how long the original string was |
1103 | * so we can be sure nothing was lost. | |
1104 | */ | |
1105 | if ((i == 0) && (too_long)) | |
422b03cf | 1106 | audit_log_format(*ab, "a%d_len=%zu ", arg_num, |
de6bbd1d EP |
1107 | has_cntl ? 2*len : len); |
1108 | ||
1109 | /* | |
1110 | * normally arguments are small enough to fit and we already | |
1111 | * filled buf above when we checked for control characters | |
1112 | * so don't bother with another copy_from_user | |
bdf4c48a | 1113 | */ |
de6bbd1d EP |
1114 | if (len >= max_execve_audit_len) |
1115 | ret = copy_from_user(buf, p, to_send); | |
1116 | else | |
1117 | ret = 0; | |
040b3a2d | 1118 | if (ret) { |
bdf4c48a PZ |
1119 | WARN_ON(1); |
1120 | send_sig(SIGKILL, current, 0); | |
b0abcfc1 | 1121 | return -1; |
bdf4c48a | 1122 | } |
de6bbd1d EP |
1123 | buf[to_send] = '\0'; |
1124 | ||
1125 | /* actually log it */ | |
1126 | audit_log_format(*ab, "a%d", arg_num); | |
1127 | if (too_long) | |
1128 | audit_log_format(*ab, "[%d]", i); | |
1129 | audit_log_format(*ab, "="); | |
1130 | if (has_cntl) | |
b556f8ad | 1131 | audit_log_n_hex(*ab, buf, to_send); |
de6bbd1d EP |
1132 | else |
1133 | audit_log_format(*ab, "\"%s\"", buf); | |
1134 | audit_log_format(*ab, "\n"); | |
1135 | ||
1136 | p += to_send; | |
1137 | len_left -= to_send; | |
1138 | *len_sent += arg_num_len; | |
1139 | if (has_cntl) | |
1140 | *len_sent += to_send * 2; | |
1141 | else | |
1142 | *len_sent += to_send; | |
1143 | } | |
1144 | /* include the null we didn't log */ | |
1145 | return len + 1; | |
1146 | } | |
1147 | ||
1148 | static void audit_log_execve_info(struct audit_context *context, | |
1149 | struct audit_buffer **ab, | |
1150 | struct audit_aux_data_execve *axi) | |
1151 | { | |
1152 | int i; | |
1153 | size_t len, len_sent = 0; | |
1154 | const char __user *p; | |
1155 | char *buf; | |
bdf4c48a | 1156 | |
de6bbd1d EP |
1157 | if (axi->mm != current->mm) |
1158 | return; /* execve failed, no additional info */ | |
1159 | ||
1160 | p = (const char __user *)axi->mm->arg_start; | |
bdf4c48a | 1161 | |
de6bbd1d EP |
1162 | audit_log_format(*ab, "argc=%d ", axi->argc); |
1163 | ||
1164 | /* | |
1165 | * we need some kernel buffer to hold the userspace args. Just | |
1166 | * allocate one big one rather than allocating one of the right size | |
1167 | * for every single argument inside audit_log_single_execve_arg() | |
1168 | * should be <8k allocation so should be pretty safe. | |
1169 | */ | |
1170 | buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL); | |
1171 | if (!buf) { | |
1172 | audit_panic("out of memory for argv string\n"); | |
1173 | return; | |
bdf4c48a | 1174 | } |
de6bbd1d EP |
1175 | |
1176 | for (i = 0; i < axi->argc; i++) { | |
1177 | len = audit_log_single_execve_arg(context, ab, i, | |
1178 | &len_sent, p, buf); | |
1179 | if (len <= 0) | |
1180 | break; | |
1181 | p += len; | |
1182 | } | |
1183 | kfree(buf); | |
bdf4c48a PZ |
1184 | } |
1185 | ||
851f7ff5 EP |
1186 | static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap) |
1187 | { | |
1188 | int i; | |
1189 | ||
1190 | audit_log_format(ab, " %s=", prefix); | |
1191 | CAP_FOR_EACH_U32(i) { | |
1192 | audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]); | |
1193 | } | |
1194 | } | |
1195 | ||
1196 | static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name) | |
1197 | { | |
1198 | kernel_cap_t *perm = &name->fcap.permitted; | |
1199 | kernel_cap_t *inh = &name->fcap.inheritable; | |
1200 | int log = 0; | |
1201 | ||
1202 | if (!cap_isclear(*perm)) { | |
1203 | audit_log_cap(ab, "cap_fp", perm); | |
1204 | log = 1; | |
1205 | } | |
1206 | if (!cap_isclear(*inh)) { | |
1207 | audit_log_cap(ab, "cap_fi", inh); | |
1208 | log = 1; | |
1209 | } | |
1210 | ||
1211 | if (log) | |
1212 | audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver); | |
1213 | } | |
1214 | ||
e495149b | 1215 | static void audit_log_exit(struct audit_context *context, struct task_struct *tsk) |
1da177e4 | 1216 | { |
9c7aa6aa | 1217 | int i, call_panic = 0; |
1da177e4 | 1218 | struct audit_buffer *ab; |
7551ced3 | 1219 | struct audit_aux_data *aux; |
a6c043a8 | 1220 | const char *tty; |
1da177e4 | 1221 | |
e495149b | 1222 | /* tsk == current */ |
3f2792ff | 1223 | context->pid = tsk->pid; |
419c58f1 AV |
1224 | if (!context->ppid) |
1225 | context->ppid = sys_getppid(); | |
3f2792ff AV |
1226 | context->uid = tsk->uid; |
1227 | context->gid = tsk->gid; | |
1228 | context->euid = tsk->euid; | |
1229 | context->suid = tsk->suid; | |
1230 | context->fsuid = tsk->fsuid; | |
1231 | context->egid = tsk->egid; | |
1232 | context->sgid = tsk->sgid; | |
1233 | context->fsgid = tsk->fsgid; | |
1234 | context->personality = tsk->personality; | |
e495149b AV |
1235 | |
1236 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL); | |
1da177e4 LT |
1237 | if (!ab) |
1238 | return; /* audit_panic has been called */ | |
bccf6ae0 DW |
1239 | audit_log_format(ab, "arch=%x syscall=%d", |
1240 | context->arch, context->major); | |
1da177e4 LT |
1241 | if (context->personality != PER_LINUX) |
1242 | audit_log_format(ab, " per=%lx", context->personality); | |
1243 | if (context->return_valid) | |
9f8dbe9c | 1244 | audit_log_format(ab, " success=%s exit=%ld", |
2fd6f58b | 1245 | (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", |
1246 | context->return_code); | |
eb84a20e | 1247 | |
dbda4c0b | 1248 | spin_lock_irq(&tsk->sighand->siglock); |
45d9bb0e AV |
1249 | if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name) |
1250 | tty = tsk->signal->tty->name; | |
a6c043a8 SG |
1251 | else |
1252 | tty = "(none)"; | |
dbda4c0b AC |
1253 | spin_unlock_irq(&tsk->sighand->siglock); |
1254 | ||
1da177e4 LT |
1255 | audit_log_format(ab, |
1256 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" | |
f46038ff | 1257 | " ppid=%d pid=%d auid=%u uid=%u gid=%u" |
326e9c8b | 1258 | " euid=%u suid=%u fsuid=%u" |
4746ec5b | 1259 | " egid=%u sgid=%u fsgid=%u tty=%s ses=%u", |
1da177e4 LT |
1260 | context->argv[0], |
1261 | context->argv[1], | |
1262 | context->argv[2], | |
1263 | context->argv[3], | |
1264 | context->name_count, | |
f46038ff | 1265 | context->ppid, |
1da177e4 | 1266 | context->pid, |
bfef93a5 | 1267 | tsk->loginuid, |
1da177e4 LT |
1268 | context->uid, |
1269 | context->gid, | |
1270 | context->euid, context->suid, context->fsuid, | |
4746ec5b EP |
1271 | context->egid, context->sgid, context->fsgid, tty, |
1272 | tsk->sessionid); | |
eb84a20e | 1273 | |
eb84a20e | 1274 | |
e495149b | 1275 | audit_log_task_info(ab, tsk); |
5adc8a6a AG |
1276 | if (context->filterkey) { |
1277 | audit_log_format(ab, " key="); | |
1278 | audit_log_untrustedstring(ab, context->filterkey); | |
1279 | } else | |
1280 | audit_log_format(ab, " key=(null)"); | |
1da177e4 | 1281 | audit_log_end(ab); |
1da177e4 | 1282 | |
7551ced3 | 1283 | for (aux = context->aux; aux; aux = aux->next) { |
c0404993 | 1284 | |
e495149b | 1285 | ab = audit_log_start(context, GFP_KERNEL, aux->type); |
1da177e4 LT |
1286 | if (!ab) |
1287 | continue; /* audit_panic has been called */ | |
1288 | ||
1da177e4 | 1289 | switch (aux->type) { |
20ca73bc GW |
1290 | case AUDIT_MQ_OPEN: { |
1291 | struct audit_aux_data_mq_open *axi = (void *)aux; | |
1292 | audit_log_format(ab, | |
1293 | "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld " | |
1294 | "mq_msgsize=%ld mq_curmsgs=%ld", | |
1295 | axi->oflag, axi->mode, axi->attr.mq_flags, | |
1296 | axi->attr.mq_maxmsg, axi->attr.mq_msgsize, | |
1297 | axi->attr.mq_curmsgs); | |
1298 | break; } | |
1299 | ||
1300 | case AUDIT_MQ_SENDRECV: { | |
1301 | struct audit_aux_data_mq_sendrecv *axi = (void *)aux; | |
1302 | audit_log_format(ab, | |
1303 | "mqdes=%d msg_len=%zd msg_prio=%u " | |
1304 | "abs_timeout_sec=%ld abs_timeout_nsec=%ld", | |
1305 | axi->mqdes, axi->msg_len, axi->msg_prio, | |
1306 | axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec); | |
1307 | break; } | |
1308 | ||
1309 | case AUDIT_MQ_NOTIFY: { | |
1310 | struct audit_aux_data_mq_notify *axi = (void *)aux; | |
1311 | audit_log_format(ab, | |
1312 | "mqdes=%d sigev_signo=%d", | |
1313 | axi->mqdes, | |
1314 | axi->notification.sigev_signo); | |
1315 | break; } | |
1316 | ||
1317 | case AUDIT_MQ_GETSETATTR: { | |
1318 | struct audit_aux_data_mq_getsetattr *axi = (void *)aux; | |
1319 | audit_log_format(ab, | |
1320 | "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld " | |
1321 | "mq_curmsgs=%ld ", | |
1322 | axi->mqdes, | |
1323 | axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg, | |
1324 | axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs); | |
1325 | break; } | |
1326 | ||
c0404993 | 1327 | case AUDIT_IPC: { |
1da177e4 LT |
1328 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
1329 | audit_log_format(ab, | |
5b9a4262 | 1330 | "ouid=%u ogid=%u mode=%#o", |
ac03221a | 1331 | axi->uid, axi->gid, axi->mode); |
9c7aa6aa SG |
1332 | if (axi->osid != 0) { |
1333 | char *ctx = NULL; | |
1334 | u32 len; | |
2a862b32 | 1335 | if (security_secid_to_secctx( |
9c7aa6aa | 1336 | axi->osid, &ctx, &len)) { |
ce29b682 | 1337 | audit_log_format(ab, " osid=%u", |
9c7aa6aa SG |
1338 | axi->osid); |
1339 | call_panic = 1; | |
2a862b32 | 1340 | } else { |
9c7aa6aa | 1341 | audit_log_format(ab, " obj=%s", ctx); |
2a862b32 AD |
1342 | security_release_secctx(ctx, len); |
1343 | } | |
9c7aa6aa | 1344 | } |
3ec3b2fb DW |
1345 | break; } |
1346 | ||
073115d6 SG |
1347 | case AUDIT_IPC_SET_PERM: { |
1348 | struct audit_aux_data_ipcctl *axi = (void *)aux; | |
1349 | audit_log_format(ab, | |
5b9a4262 | 1350 | "qbytes=%lx ouid=%u ogid=%u mode=%#o", |
073115d6 | 1351 | axi->qbytes, axi->uid, axi->gid, axi->mode); |
073115d6 | 1352 | break; } |
ac03221a | 1353 | |
473ae30b AV |
1354 | case AUDIT_EXECVE: { |
1355 | struct audit_aux_data_execve *axi = (void *)aux; | |
de6bbd1d | 1356 | audit_log_execve_info(context, &ab, axi); |
473ae30b | 1357 | break; } |
073115d6 | 1358 | |
3ec3b2fb | 1359 | case AUDIT_SOCKETCALL: { |
3ec3b2fb DW |
1360 | struct audit_aux_data_socketcall *axs = (void *)aux; |
1361 | audit_log_format(ab, "nargs=%d", axs->nargs); | |
1362 | for (i=0; i<axs->nargs; i++) | |
1363 | audit_log_format(ab, " a%d=%lx", i, axs->args[i]); | |
1364 | break; } | |
1365 | ||
1366 | case AUDIT_SOCKADDR: { | |
1367 | struct audit_aux_data_sockaddr *axs = (void *)aux; | |
1368 | ||
1369 | audit_log_format(ab, "saddr="); | |
b556f8ad | 1370 | audit_log_n_hex(ab, axs->a, axs->len); |
3ec3b2fb | 1371 | break; } |
01116105 | 1372 | |
db349509 AV |
1373 | case AUDIT_FD_PAIR: { |
1374 | struct audit_aux_data_fd_pair *axs = (void *)aux; | |
1375 | audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]); | |
1376 | break; } | |
1377 | ||
1da177e4 LT |
1378 | } |
1379 | audit_log_end(ab); | |
1da177e4 LT |
1380 | } |
1381 | ||
e54dc243 AG |
1382 | for (aux = context->aux_pids; aux; aux = aux->next) { |
1383 | struct audit_aux_data_pids *axs = (void *)aux; | |
e54dc243 AG |
1384 | |
1385 | for (i = 0; i < axs->pid_count; i++) | |
1386 | if (audit_log_pid_context(context, axs->target_pid[i], | |
c2a7780e EP |
1387 | axs->target_auid[i], |
1388 | axs->target_uid[i], | |
4746ec5b | 1389 | axs->target_sessionid[i], |
c2a7780e EP |
1390 | axs->target_sid[i], |
1391 | axs->target_comm[i])) | |
e54dc243 | 1392 | call_panic = 1; |
a5cb013d AV |
1393 | } |
1394 | ||
e54dc243 AG |
1395 | if (context->target_pid && |
1396 | audit_log_pid_context(context, context->target_pid, | |
c2a7780e | 1397 | context->target_auid, context->target_uid, |
4746ec5b | 1398 | context->target_sessionid, |
c2a7780e | 1399 | context->target_sid, context->target_comm)) |
e54dc243 AG |
1400 | call_panic = 1; |
1401 | ||
44707fdf | 1402 | if (context->pwd.dentry && context->pwd.mnt) { |
e495149b | 1403 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD); |
8f37d47c | 1404 | if (ab) { |
44707fdf | 1405 | audit_log_d_path(ab, "cwd=", &context->pwd); |
8f37d47c DW |
1406 | audit_log_end(ab); |
1407 | } | |
1408 | } | |
1da177e4 | 1409 | for (i = 0; i < context->name_count; i++) { |
9c937dcc | 1410 | struct audit_names *n = &context->names[i]; |
73241ccc | 1411 | |
e495149b | 1412 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH); |
1da177e4 LT |
1413 | if (!ab) |
1414 | continue; /* audit_panic has been called */ | |
8f37d47c | 1415 | |
1da177e4 | 1416 | audit_log_format(ab, "item=%d", i); |
73241ccc | 1417 | |
9c937dcc AG |
1418 | if (n->name) { |
1419 | switch(n->name_len) { | |
1420 | case AUDIT_NAME_FULL: | |
1421 | /* log the full path */ | |
1422 | audit_log_format(ab, " name="); | |
1423 | audit_log_untrustedstring(ab, n->name); | |
1424 | break; | |
1425 | case 0: | |
1426 | /* name was specified as a relative path and the | |
1427 | * directory component is the cwd */ | |
44707fdf | 1428 | audit_log_d_path(ab, " name=", &context->pwd); |
9c937dcc AG |
1429 | break; |
1430 | default: | |
1431 | /* log the name's directory component */ | |
1432 | audit_log_format(ab, " name="); | |
b556f8ad EP |
1433 | audit_log_n_untrustedstring(ab, n->name, |
1434 | n->name_len); | |
9c937dcc AG |
1435 | } |
1436 | } else | |
1437 | audit_log_format(ab, " name=(null)"); | |
1438 | ||
1439 | if (n->ino != (unsigned long)-1) { | |
1440 | audit_log_format(ab, " inode=%lu" | |
1441 | " dev=%02x:%02x mode=%#o" | |
1442 | " ouid=%u ogid=%u rdev=%02x:%02x", | |
1443 | n->ino, | |
1444 | MAJOR(n->dev), | |
1445 | MINOR(n->dev), | |
1446 | n->mode, | |
1447 | n->uid, | |
1448 | n->gid, | |
1449 | MAJOR(n->rdev), | |
1450 | MINOR(n->rdev)); | |
1451 | } | |
1452 | if (n->osid != 0) { | |
1b50eed9 SG |
1453 | char *ctx = NULL; |
1454 | u32 len; | |
2a862b32 | 1455 | if (security_secid_to_secctx( |
9c937dcc AG |
1456 | n->osid, &ctx, &len)) { |
1457 | audit_log_format(ab, " osid=%u", n->osid); | |
9c7aa6aa | 1458 | call_panic = 2; |
2a862b32 | 1459 | } else { |
1b50eed9 | 1460 | audit_log_format(ab, " obj=%s", ctx); |
2a862b32 AD |
1461 | security_release_secctx(ctx, len); |
1462 | } | |
8c8570fb DK |
1463 | } |
1464 | ||
851f7ff5 EP |
1465 | audit_log_fcaps(ab, n); |
1466 | ||
1da177e4 LT |
1467 | audit_log_end(ab); |
1468 | } | |
c0641f28 EP |
1469 | |
1470 | /* Send end of event record to help user space know we are finished */ | |
1471 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE); | |
1472 | if (ab) | |
1473 | audit_log_end(ab); | |
9c7aa6aa SG |
1474 | if (call_panic) |
1475 | audit_panic("error converting sid to string"); | |
1da177e4 LT |
1476 | } |
1477 | ||
b0dd25a8 RD |
1478 | /** |
1479 | * audit_free - free a per-task audit context | |
1480 | * @tsk: task whose audit context block to free | |
1481 | * | |
fa84cb93 | 1482 | * Called from copy_process and do_exit |
b0dd25a8 | 1483 | */ |
1da177e4 LT |
1484 | void audit_free(struct task_struct *tsk) |
1485 | { | |
1486 | struct audit_context *context; | |
1487 | ||
1da177e4 | 1488 | context = audit_get_context(tsk, 0, 0); |
1da177e4 LT |
1489 | if (likely(!context)) |
1490 | return; | |
1491 | ||
1492 | /* Check for system calls that do not go through the exit | |
9f8dbe9c DW |
1493 | * function (e.g., exit_group), then free context block. |
1494 | * We use GFP_ATOMIC here because we might be doing this | |
f5561964 | 1495 | * in the context of the idle thread */ |
e495149b | 1496 | /* that can happen only if we are called from do_exit() */ |
f7056d64 | 1497 | if (context->in_syscall && context->auditable) |
e495149b | 1498 | audit_log_exit(context, tsk); |
1da177e4 LT |
1499 | |
1500 | audit_free_context(context); | |
1501 | } | |
1502 | ||
b0dd25a8 RD |
1503 | /** |
1504 | * audit_syscall_entry - fill in an audit record at syscall entry | |
1505 | * @tsk: task being audited | |
1506 | * @arch: architecture type | |
1507 | * @major: major syscall type (function) | |
1508 | * @a1: additional syscall register 1 | |
1509 | * @a2: additional syscall register 2 | |
1510 | * @a3: additional syscall register 3 | |
1511 | * @a4: additional syscall register 4 | |
1512 | * | |
1513 | * Fill in audit context at syscall entry. This only happens if the | |
1da177e4 LT |
1514 | * audit context was created when the task was created and the state or |
1515 | * filters demand the audit context be built. If the state from the | |
1516 | * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT, | |
1517 | * then the record will be written at syscall exit time (otherwise, it | |
1518 | * will only be written if another part of the kernel requests that it | |
b0dd25a8 RD |
1519 | * be written). |
1520 | */ | |
5411be59 | 1521 | void audit_syscall_entry(int arch, int major, |
1da177e4 LT |
1522 | unsigned long a1, unsigned long a2, |
1523 | unsigned long a3, unsigned long a4) | |
1524 | { | |
5411be59 | 1525 | struct task_struct *tsk = current; |
1da177e4 LT |
1526 | struct audit_context *context = tsk->audit_context; |
1527 | enum audit_state state; | |
1528 | ||
86a1c34a RM |
1529 | if (unlikely(!context)) |
1530 | return; | |
1da177e4 | 1531 | |
b0dd25a8 RD |
1532 | /* |
1533 | * This happens only on certain architectures that make system | |
1da177e4 LT |
1534 | * calls in kernel_thread via the entry.S interface, instead of |
1535 | * with direct calls. (If you are porting to a new | |
1536 | * architecture, hitting this condition can indicate that you | |
1537 | * got the _exit/_leave calls backward in entry.S.) | |
1538 | * | |
1539 | * i386 no | |
1540 | * x86_64 no | |
2ef9481e | 1541 | * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S) |
1da177e4 LT |
1542 | * |
1543 | * This also happens with vm86 emulation in a non-nested manner | |
1544 | * (entries without exits), so this case must be caught. | |
1545 | */ | |
1546 | if (context->in_syscall) { | |
1547 | struct audit_context *newctx; | |
1548 | ||
1da177e4 LT |
1549 | #if AUDIT_DEBUG |
1550 | printk(KERN_ERR | |
1551 | "audit(:%d) pid=%d in syscall=%d;" | |
1552 | " entering syscall=%d\n", | |
1553 | context->serial, tsk->pid, context->major, major); | |
1554 | #endif | |
1555 | newctx = audit_alloc_context(context->state); | |
1556 | if (newctx) { | |
1557 | newctx->previous = context; | |
1558 | context = newctx; | |
1559 | tsk->audit_context = newctx; | |
1560 | } else { | |
1561 | /* If we can't alloc a new context, the best we | |
1562 | * can do is to leak memory (any pending putname | |
1563 | * will be lost). The only other alternative is | |
1564 | * to abandon auditing. */ | |
1565 | audit_zero_context(context, context->state); | |
1566 | } | |
1567 | } | |
1568 | BUG_ON(context->in_syscall || context->name_count); | |
1569 | ||
1570 | if (!audit_enabled) | |
1571 | return; | |
1572 | ||
2fd6f58b | 1573 | context->arch = arch; |
1da177e4 LT |
1574 | context->major = major; |
1575 | context->argv[0] = a1; | |
1576 | context->argv[1] = a2; | |
1577 | context->argv[2] = a3; | |
1578 | context->argv[3] = a4; | |
1579 | ||
1580 | state = context->state; | |
d51374ad AV |
1581 | context->dummy = !audit_n_rules; |
1582 | if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)) | |
0f45aa18 | 1583 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]); |
1da177e4 LT |
1584 | if (likely(state == AUDIT_DISABLED)) |
1585 | return; | |
1586 | ||
ce625a80 | 1587 | context->serial = 0; |
1da177e4 LT |
1588 | context->ctime = CURRENT_TIME; |
1589 | context->in_syscall = 1; | |
1590 | context->auditable = !!(state == AUDIT_RECORD_CONTEXT); | |
419c58f1 | 1591 | context->ppid = 0; |
1da177e4 LT |
1592 | } |
1593 | ||
b0dd25a8 RD |
1594 | /** |
1595 | * audit_syscall_exit - deallocate audit context after a system call | |
1596 | * @tsk: task being audited | |
1597 | * @valid: success/failure flag | |
1598 | * @return_code: syscall return value | |
1599 | * | |
1600 | * Tear down after system call. If the audit context has been marked as | |
1da177e4 LT |
1601 | * auditable (either because of the AUDIT_RECORD_CONTEXT state from |
1602 | * filtering, or because some other part of the kernel write an audit | |
1603 | * message), then write out the syscall information. In call cases, | |
b0dd25a8 RD |
1604 | * free the names stored from getname(). |
1605 | */ | |
5411be59 | 1606 | void audit_syscall_exit(int valid, long return_code) |
1da177e4 | 1607 | { |
5411be59 | 1608 | struct task_struct *tsk = current; |
1da177e4 LT |
1609 | struct audit_context *context; |
1610 | ||
2fd6f58b | 1611 | context = audit_get_context(tsk, valid, return_code); |
1da177e4 | 1612 | |
1da177e4 | 1613 | if (likely(!context)) |
97e94c45 | 1614 | return; |
1da177e4 | 1615 | |
f7056d64 | 1616 | if (context->in_syscall && context->auditable) |
e495149b | 1617 | audit_log_exit(context, tsk); |
1da177e4 LT |
1618 | |
1619 | context->in_syscall = 0; | |
1620 | context->auditable = 0; | |
2fd6f58b | 1621 | |
1da177e4 LT |
1622 | if (context->previous) { |
1623 | struct audit_context *new_context = context->previous; | |
1624 | context->previous = NULL; | |
1625 | audit_free_context(context); | |
1626 | tsk->audit_context = new_context; | |
1627 | } else { | |
1628 | audit_free_names(context); | |
74c3cbe3 | 1629 | unroll_tree_refs(context, NULL, 0); |
1da177e4 | 1630 | audit_free_aux(context); |
e54dc243 AG |
1631 | context->aux = NULL; |
1632 | context->aux_pids = NULL; | |
a5cb013d | 1633 | context->target_pid = 0; |
e54dc243 | 1634 | context->target_sid = 0; |
5adc8a6a AG |
1635 | kfree(context->filterkey); |
1636 | context->filterkey = NULL; | |
1da177e4 LT |
1637 | tsk->audit_context = context; |
1638 | } | |
1da177e4 LT |
1639 | } |
1640 | ||
74c3cbe3 AV |
1641 | static inline void handle_one(const struct inode *inode) |
1642 | { | |
1643 | #ifdef CONFIG_AUDIT_TREE | |
1644 | struct audit_context *context; | |
1645 | struct audit_tree_refs *p; | |
1646 | struct audit_chunk *chunk; | |
1647 | int count; | |
1648 | if (likely(list_empty(&inode->inotify_watches))) | |
1649 | return; | |
1650 | context = current->audit_context; | |
1651 | p = context->trees; | |
1652 | count = context->tree_count; | |
1653 | rcu_read_lock(); | |
1654 | chunk = audit_tree_lookup(inode); | |
1655 | rcu_read_unlock(); | |
1656 | if (!chunk) | |
1657 | return; | |
1658 | if (likely(put_tree_ref(context, chunk))) | |
1659 | return; | |
1660 | if (unlikely(!grow_tree_refs(context))) { | |
436c405c | 1661 | printk(KERN_WARNING "out of memory, audit has lost a tree reference\n"); |
74c3cbe3 AV |
1662 | audit_set_auditable(context); |
1663 | audit_put_chunk(chunk); | |
1664 | unroll_tree_refs(context, p, count); | |
1665 | return; | |
1666 | } | |
1667 | put_tree_ref(context, chunk); | |
1668 | #endif | |
1669 | } | |
1670 | ||
1671 | static void handle_path(const struct dentry *dentry) | |
1672 | { | |
1673 | #ifdef CONFIG_AUDIT_TREE | |
1674 | struct audit_context *context; | |
1675 | struct audit_tree_refs *p; | |
1676 | const struct dentry *d, *parent; | |
1677 | struct audit_chunk *drop; | |
1678 | unsigned long seq; | |
1679 | int count; | |
1680 | ||
1681 | context = current->audit_context; | |
1682 | p = context->trees; | |
1683 | count = context->tree_count; | |
1684 | retry: | |
1685 | drop = NULL; | |
1686 | d = dentry; | |
1687 | rcu_read_lock(); | |
1688 | seq = read_seqbegin(&rename_lock); | |
1689 | for(;;) { | |
1690 | struct inode *inode = d->d_inode; | |
1691 | if (inode && unlikely(!list_empty(&inode->inotify_watches))) { | |
1692 | struct audit_chunk *chunk; | |
1693 | chunk = audit_tree_lookup(inode); | |
1694 | if (chunk) { | |
1695 | if (unlikely(!put_tree_ref(context, chunk))) { | |
1696 | drop = chunk; | |
1697 | break; | |
1698 | } | |
1699 | } | |
1700 | } | |
1701 | parent = d->d_parent; | |
1702 | if (parent == d) | |
1703 | break; | |
1704 | d = parent; | |
1705 | } | |
1706 | if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */ | |
1707 | rcu_read_unlock(); | |
1708 | if (!drop) { | |
1709 | /* just a race with rename */ | |
1710 | unroll_tree_refs(context, p, count); | |
1711 | goto retry; | |
1712 | } | |
1713 | audit_put_chunk(drop); | |
1714 | if (grow_tree_refs(context)) { | |
1715 | /* OK, got more space */ | |
1716 | unroll_tree_refs(context, p, count); | |
1717 | goto retry; | |
1718 | } | |
1719 | /* too bad */ | |
1720 | printk(KERN_WARNING | |
436c405c | 1721 | "out of memory, audit has lost a tree reference\n"); |
74c3cbe3 AV |
1722 | unroll_tree_refs(context, p, count); |
1723 | audit_set_auditable(context); | |
1724 | return; | |
1725 | } | |
1726 | rcu_read_unlock(); | |
1727 | #endif | |
1728 | } | |
1729 | ||
b0dd25a8 RD |
1730 | /** |
1731 | * audit_getname - add a name to the list | |
1732 | * @name: name to add | |
1733 | * | |
1734 | * Add a name to the list of audit names for this context. | |
1735 | * Called from fs/namei.c:getname(). | |
1736 | */ | |
d8945bb5 | 1737 | void __audit_getname(const char *name) |
1da177e4 LT |
1738 | { |
1739 | struct audit_context *context = current->audit_context; | |
1740 | ||
d8945bb5 | 1741 | if (IS_ERR(name) || !name) |
1da177e4 LT |
1742 | return; |
1743 | ||
1744 | if (!context->in_syscall) { | |
1745 | #if AUDIT_DEBUG == 2 | |
1746 | printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n", | |
1747 | __FILE__, __LINE__, context->serial, name); | |
1748 | dump_stack(); | |
1749 | #endif | |
1750 | return; | |
1751 | } | |
1752 | BUG_ON(context->name_count >= AUDIT_NAMES); | |
1753 | context->names[context->name_count].name = name; | |
9c937dcc AG |
1754 | context->names[context->name_count].name_len = AUDIT_NAME_FULL; |
1755 | context->names[context->name_count].name_put = 1; | |
1da177e4 | 1756 | context->names[context->name_count].ino = (unsigned long)-1; |
e41e8bde | 1757 | context->names[context->name_count].osid = 0; |
1da177e4 | 1758 | ++context->name_count; |
44707fdf | 1759 | if (!context->pwd.dentry) { |
8f37d47c | 1760 | read_lock(¤t->fs->lock); |
44707fdf JB |
1761 | context->pwd = current->fs->pwd; |
1762 | path_get(¤t->fs->pwd); | |
8f37d47c DW |
1763 | read_unlock(¤t->fs->lock); |
1764 | } | |
9f8dbe9c | 1765 | |
1da177e4 LT |
1766 | } |
1767 | ||
b0dd25a8 RD |
1768 | /* audit_putname - intercept a putname request |
1769 | * @name: name to intercept and delay for putname | |
1770 | * | |
1771 | * If we have stored the name from getname in the audit context, | |
1772 | * then we delay the putname until syscall exit. | |
1773 | * Called from include/linux/fs.h:putname(). | |
1774 | */ | |
1da177e4 LT |
1775 | void audit_putname(const char *name) |
1776 | { | |
1777 | struct audit_context *context = current->audit_context; | |
1778 | ||
1779 | BUG_ON(!context); | |
1780 | if (!context->in_syscall) { | |
1781 | #if AUDIT_DEBUG == 2 | |
1782 | printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n", | |
1783 | __FILE__, __LINE__, context->serial, name); | |
1784 | if (context->name_count) { | |
1785 | int i; | |
1786 | for (i = 0; i < context->name_count; i++) | |
1787 | printk(KERN_ERR "name[%d] = %p = %s\n", i, | |
1788 | context->names[i].name, | |
73241ccc | 1789 | context->names[i].name ?: "(null)"); |
1da177e4 LT |
1790 | } |
1791 | #endif | |
1792 | __putname(name); | |
1793 | } | |
1794 | #if AUDIT_DEBUG | |
1795 | else { | |
1796 | ++context->put_count; | |
1797 | if (context->put_count > context->name_count) { | |
1798 | printk(KERN_ERR "%s:%d(:%d): major=%d" | |
1799 | " in_syscall=%d putname(%p) name_count=%d" | |
1800 | " put_count=%d\n", | |
1801 | __FILE__, __LINE__, | |
1802 | context->serial, context->major, | |
1803 | context->in_syscall, name, context->name_count, | |
1804 | context->put_count); | |
1805 | dump_stack(); | |
1806 | } | |
1807 | } | |
1808 | #endif | |
1809 | } | |
1810 | ||
5712e88f AG |
1811 | static int audit_inc_name_count(struct audit_context *context, |
1812 | const struct inode *inode) | |
1813 | { | |
1814 | if (context->name_count >= AUDIT_NAMES) { | |
1815 | if (inode) | |
1816 | printk(KERN_DEBUG "name_count maxed, losing inode data: " | |
436c405c | 1817 | "dev=%02x:%02x, inode=%lu\n", |
5712e88f AG |
1818 | MAJOR(inode->i_sb->s_dev), |
1819 | MINOR(inode->i_sb->s_dev), | |
1820 | inode->i_ino); | |
1821 | ||
1822 | else | |
436c405c | 1823 | printk(KERN_DEBUG "name_count maxed, losing inode data\n"); |
5712e88f AG |
1824 | return 1; |
1825 | } | |
1826 | context->name_count++; | |
1827 | #if AUDIT_DEBUG | |
1828 | context->ino_count++; | |
1829 | #endif | |
1830 | return 0; | |
1831 | } | |
1832 | ||
851f7ff5 EP |
1833 | |
1834 | static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry) | |
1835 | { | |
1836 | struct cpu_vfs_cap_data caps; | |
1837 | int rc; | |
1838 | ||
1839 | memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t)); | |
1840 | memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t)); | |
1841 | name->fcap.fE = 0; | |
1842 | name->fcap_ver = 0; | |
1843 | ||
1844 | if (!dentry) | |
1845 | return 0; | |
1846 | ||
1847 | rc = get_vfs_caps_from_disk(dentry, &caps); | |
1848 | if (rc) | |
1849 | return rc; | |
1850 | ||
1851 | name->fcap.permitted = caps.permitted; | |
1852 | name->fcap.inheritable = caps.inheritable; | |
1853 | name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE); | |
1854 | name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT; | |
1855 | ||
1856 | return 0; | |
1857 | } | |
1858 | ||
1859 | ||
3e2efce0 | 1860 | /* Copy inode data into an audit_names. */ |
851f7ff5 EP |
1861 | static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry, |
1862 | const struct inode *inode) | |
8c8570fb | 1863 | { |
3e2efce0 AG |
1864 | name->ino = inode->i_ino; |
1865 | name->dev = inode->i_sb->s_dev; | |
1866 | name->mode = inode->i_mode; | |
1867 | name->uid = inode->i_uid; | |
1868 | name->gid = inode->i_gid; | |
1869 | name->rdev = inode->i_rdev; | |
2a862b32 | 1870 | security_inode_getsecid(inode, &name->osid); |
851f7ff5 | 1871 | audit_copy_fcaps(name, dentry); |
8c8570fb DK |
1872 | } |
1873 | ||
b0dd25a8 RD |
1874 | /** |
1875 | * audit_inode - store the inode and device from a lookup | |
1876 | * @name: name being audited | |
481968f4 | 1877 | * @dentry: dentry being audited |
b0dd25a8 RD |
1878 | * |
1879 | * Called from fs/namei.c:path_lookup(). | |
1880 | */ | |
5a190ae6 | 1881 | void __audit_inode(const char *name, const struct dentry *dentry) |
1da177e4 LT |
1882 | { |
1883 | int idx; | |
1884 | struct audit_context *context = current->audit_context; | |
74c3cbe3 | 1885 | const struct inode *inode = dentry->d_inode; |
1da177e4 LT |
1886 | |
1887 | if (!context->in_syscall) | |
1888 | return; | |
1889 | if (context->name_count | |
1890 | && context->names[context->name_count-1].name | |
1891 | && context->names[context->name_count-1].name == name) | |
1892 | idx = context->name_count - 1; | |
1893 | else if (context->name_count > 1 | |
1894 | && context->names[context->name_count-2].name | |
1895 | && context->names[context->name_count-2].name == name) | |
1896 | idx = context->name_count - 2; | |
1897 | else { | |
1898 | /* FIXME: how much do we care about inodes that have no | |
1899 | * associated name? */ | |
5712e88f | 1900 | if (audit_inc_name_count(context, inode)) |
1da177e4 | 1901 | return; |
5712e88f | 1902 | idx = context->name_count - 1; |
1da177e4 | 1903 | context->names[idx].name = NULL; |
1da177e4 | 1904 | } |
74c3cbe3 | 1905 | handle_path(dentry); |
851f7ff5 | 1906 | audit_copy_inode(&context->names[idx], dentry, inode); |
73241ccc AG |
1907 | } |
1908 | ||
1909 | /** | |
1910 | * audit_inode_child - collect inode info for created/removed objects | |
1911 | * @dname: inode's dentry name | |
481968f4 | 1912 | * @dentry: dentry being audited |
73d3ec5a | 1913 | * @parent: inode of dentry parent |
73241ccc AG |
1914 | * |
1915 | * For syscalls that create or remove filesystem objects, audit_inode | |
1916 | * can only collect information for the filesystem object's parent. | |
1917 | * This call updates the audit context with the child's information. | |
1918 | * Syscalls that create a new filesystem object must be hooked after | |
1919 | * the object is created. Syscalls that remove a filesystem object | |
1920 | * must be hooked prior, in order to capture the target inode during | |
1921 | * unsuccessful attempts. | |
1922 | */ | |
5a190ae6 | 1923 | void __audit_inode_child(const char *dname, const struct dentry *dentry, |
73d3ec5a | 1924 | const struct inode *parent) |
73241ccc AG |
1925 | { |
1926 | int idx; | |
1927 | struct audit_context *context = current->audit_context; | |
5712e88f | 1928 | const char *found_parent = NULL, *found_child = NULL; |
5a190ae6 | 1929 | const struct inode *inode = dentry->d_inode; |
9c937dcc | 1930 | int dirlen = 0; |
73241ccc AG |
1931 | |
1932 | if (!context->in_syscall) | |
1933 | return; | |
1934 | ||
74c3cbe3 AV |
1935 | if (inode) |
1936 | handle_one(inode); | |
73241ccc | 1937 | /* determine matching parent */ |
f368c07d | 1938 | if (!dname) |
5712e88f | 1939 | goto add_names; |
73241ccc | 1940 | |
5712e88f AG |
1941 | /* parent is more likely, look for it first */ |
1942 | for (idx = 0; idx < context->name_count; idx++) { | |
1943 | struct audit_names *n = &context->names[idx]; | |
f368c07d | 1944 | |
5712e88f AG |
1945 | if (!n->name) |
1946 | continue; | |
1947 | ||
1948 | if (n->ino == parent->i_ino && | |
1949 | !audit_compare_dname_path(dname, n->name, &dirlen)) { | |
1950 | n->name_len = dirlen; /* update parent data in place */ | |
1951 | found_parent = n->name; | |
1952 | goto add_names; | |
f368c07d | 1953 | } |
5712e88f | 1954 | } |
73241ccc | 1955 | |
5712e88f AG |
1956 | /* no matching parent, look for matching child */ |
1957 | for (idx = 0; idx < context->name_count; idx++) { | |
1958 | struct audit_names *n = &context->names[idx]; | |
1959 | ||
1960 | if (!n->name) | |
1961 | continue; | |
1962 | ||
1963 | /* strcmp() is the more likely scenario */ | |
1964 | if (!strcmp(dname, n->name) || | |
1965 | !audit_compare_dname_path(dname, n->name, &dirlen)) { | |
1966 | if (inode) | |
851f7ff5 | 1967 | audit_copy_inode(n, NULL, inode); |
5712e88f AG |
1968 | else |
1969 | n->ino = (unsigned long)-1; | |
1970 | found_child = n->name; | |
1971 | goto add_names; | |
1972 | } | |
ac9910ce | 1973 | } |
5712e88f AG |
1974 | |
1975 | add_names: | |
1976 | if (!found_parent) { | |
1977 | if (audit_inc_name_count(context, parent)) | |
ac9910ce | 1978 | return; |
5712e88f AG |
1979 | idx = context->name_count - 1; |
1980 | context->names[idx].name = NULL; | |
851f7ff5 | 1981 | audit_copy_inode(&context->names[idx], NULL, parent); |
73d3ec5a | 1982 | } |
5712e88f AG |
1983 | |
1984 | if (!found_child) { | |
1985 | if (audit_inc_name_count(context, inode)) | |
1986 | return; | |
1987 | idx = context->name_count - 1; | |
1988 | ||
1989 | /* Re-use the name belonging to the slot for a matching parent | |
1990 | * directory. All names for this context are relinquished in | |
1991 | * audit_free_names() */ | |
1992 | if (found_parent) { | |
1993 | context->names[idx].name = found_parent; | |
1994 | context->names[idx].name_len = AUDIT_NAME_FULL; | |
1995 | /* don't call __putname() */ | |
1996 | context->names[idx].name_put = 0; | |
1997 | } else { | |
1998 | context->names[idx].name = NULL; | |
1999 | } | |
2000 | ||
2001 | if (inode) | |
851f7ff5 | 2002 | audit_copy_inode(&context->names[idx], NULL, inode); |
5712e88f AG |
2003 | else |
2004 | context->names[idx].ino = (unsigned long)-1; | |
2005 | } | |
3e2efce0 | 2006 | } |
50e437d5 | 2007 | EXPORT_SYMBOL_GPL(__audit_inode_child); |
3e2efce0 | 2008 | |
b0dd25a8 RD |
2009 | /** |
2010 | * auditsc_get_stamp - get local copies of audit_context values | |
2011 | * @ctx: audit_context for the task | |
2012 | * @t: timespec to store time recorded in the audit_context | |
2013 | * @serial: serial value that is recorded in the audit_context | |
2014 | * | |
2015 | * Also sets the context as auditable. | |
2016 | */ | |
bfb4496e DW |
2017 | void auditsc_get_stamp(struct audit_context *ctx, |
2018 | struct timespec *t, unsigned int *serial) | |
1da177e4 | 2019 | { |
ce625a80 DW |
2020 | if (!ctx->serial) |
2021 | ctx->serial = audit_serial(); | |
bfb4496e DW |
2022 | t->tv_sec = ctx->ctime.tv_sec; |
2023 | t->tv_nsec = ctx->ctime.tv_nsec; | |
2024 | *serial = ctx->serial; | |
2025 | ctx->auditable = 1; | |
1da177e4 LT |
2026 | } |
2027 | ||
4746ec5b EP |
2028 | /* global counter which is incremented every time something logs in */ |
2029 | static atomic_t session_id = ATOMIC_INIT(0); | |
2030 | ||
b0dd25a8 RD |
2031 | /** |
2032 | * audit_set_loginuid - set a task's audit_context loginuid | |
2033 | * @task: task whose audit context is being modified | |
2034 | * @loginuid: loginuid value | |
2035 | * | |
2036 | * Returns 0. | |
2037 | * | |
2038 | * Called (set) from fs/proc/base.c::proc_loginuid_write(). | |
2039 | */ | |
456be6cd | 2040 | int audit_set_loginuid(struct task_struct *task, uid_t loginuid) |
1da177e4 | 2041 | { |
4746ec5b | 2042 | unsigned int sessionid = atomic_inc_return(&session_id); |
41757106 SG |
2043 | struct audit_context *context = task->audit_context; |
2044 | ||
bfef93a5 AV |
2045 | if (context && context->in_syscall) { |
2046 | struct audit_buffer *ab; | |
2047 | ||
2048 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN); | |
2049 | if (ab) { | |
2050 | audit_log_format(ab, "login pid=%d uid=%u " | |
4746ec5b EP |
2051 | "old auid=%u new auid=%u" |
2052 | " old ses=%u new ses=%u", | |
bfef93a5 | 2053 | task->pid, task->uid, |
4746ec5b EP |
2054 | task->loginuid, loginuid, |
2055 | task->sessionid, sessionid); | |
bfef93a5 | 2056 | audit_log_end(ab); |
c0404993 | 2057 | } |
1da177e4 | 2058 | } |
4746ec5b | 2059 | task->sessionid = sessionid; |
bfef93a5 | 2060 | task->loginuid = loginuid; |
1da177e4 LT |
2061 | return 0; |
2062 | } | |
2063 | ||
20ca73bc GW |
2064 | /** |
2065 | * __audit_mq_open - record audit data for a POSIX MQ open | |
2066 | * @oflag: open flag | |
2067 | * @mode: mode bits | |
2068 | * @u_attr: queue attributes | |
2069 | * | |
2070 | * Returns 0 for success or NULL context or < 0 on error. | |
2071 | */ | |
2072 | int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr) | |
2073 | { | |
2074 | struct audit_aux_data_mq_open *ax; | |
2075 | struct audit_context *context = current->audit_context; | |
2076 | ||
2077 | if (!audit_enabled) | |
2078 | return 0; | |
2079 | ||
2080 | if (likely(!context)) | |
2081 | return 0; | |
2082 | ||
2083 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); | |
2084 | if (!ax) | |
2085 | return -ENOMEM; | |
2086 | ||
2087 | if (u_attr != NULL) { | |
2088 | if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) { | |
2089 | kfree(ax); | |
2090 | return -EFAULT; | |
2091 | } | |
2092 | } else | |
2093 | memset(&ax->attr, 0, sizeof(ax->attr)); | |
2094 | ||
2095 | ax->oflag = oflag; | |
2096 | ax->mode = mode; | |
2097 | ||
2098 | ax->d.type = AUDIT_MQ_OPEN; | |
2099 | ax->d.next = context->aux; | |
2100 | context->aux = (void *)ax; | |
2101 | return 0; | |
2102 | } | |
2103 | ||
2104 | /** | |
2105 | * __audit_mq_timedsend - record audit data for a POSIX MQ timed send | |
2106 | * @mqdes: MQ descriptor | |
2107 | * @msg_len: Message length | |
2108 | * @msg_prio: Message priority | |
1dbe83c3 | 2109 | * @u_abs_timeout: Message timeout in absolute time |
20ca73bc GW |
2110 | * |
2111 | * Returns 0 for success or NULL context or < 0 on error. | |
2112 | */ | |
2113 | int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, | |
2114 | const struct timespec __user *u_abs_timeout) | |
2115 | { | |
2116 | struct audit_aux_data_mq_sendrecv *ax; | |
2117 | struct audit_context *context = current->audit_context; | |
2118 | ||
2119 | if (!audit_enabled) | |
2120 | return 0; | |
2121 | ||
2122 | if (likely(!context)) | |
2123 | return 0; | |
2124 | ||
2125 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); | |
2126 | if (!ax) | |
2127 | return -ENOMEM; | |
2128 | ||
2129 | if (u_abs_timeout != NULL) { | |
2130 | if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) { | |
2131 | kfree(ax); | |
2132 | return -EFAULT; | |
2133 | } | |
2134 | } else | |
2135 | memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout)); | |
2136 | ||
2137 | ax->mqdes = mqdes; | |
2138 | ax->msg_len = msg_len; | |
2139 | ax->msg_prio = msg_prio; | |
2140 | ||
2141 | ax->d.type = AUDIT_MQ_SENDRECV; | |
2142 | ax->d.next = context->aux; | |
2143 | context->aux = (void *)ax; | |
2144 | return 0; | |
2145 | } | |
2146 | ||
2147 | /** | |
2148 | * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive | |
2149 | * @mqdes: MQ descriptor | |
2150 | * @msg_len: Message length | |
1dbe83c3 RD |
2151 | * @u_msg_prio: Message priority |
2152 | * @u_abs_timeout: Message timeout in absolute time | |
20ca73bc GW |
2153 | * |
2154 | * Returns 0 for success or NULL context or < 0 on error. | |
2155 | */ | |
2156 | int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len, | |
2157 | unsigned int __user *u_msg_prio, | |
2158 | const struct timespec __user *u_abs_timeout) | |
2159 | { | |
2160 | struct audit_aux_data_mq_sendrecv *ax; | |
2161 | struct audit_context *context = current->audit_context; | |
2162 | ||
2163 | if (!audit_enabled) | |
2164 | return 0; | |
2165 | ||
2166 | if (likely(!context)) | |
2167 | return 0; | |
2168 | ||
2169 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); | |
2170 | if (!ax) | |
2171 | return -ENOMEM; | |
2172 | ||
2173 | if (u_msg_prio != NULL) { | |
2174 | if (get_user(ax->msg_prio, u_msg_prio)) { | |
2175 | kfree(ax); | |
2176 | return -EFAULT; | |
2177 | } | |
2178 | } else | |
2179 | ax->msg_prio = 0; | |
2180 | ||
2181 | if (u_abs_timeout != NULL) { | |
2182 | if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) { | |
2183 | kfree(ax); | |
2184 | return -EFAULT; | |
2185 | } | |
2186 | } else | |
2187 | memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout)); | |
2188 | ||
2189 | ax->mqdes = mqdes; | |
2190 | ax->msg_len = msg_len; | |
2191 | ||
2192 | ax->d.type = AUDIT_MQ_SENDRECV; | |
2193 | ax->d.next = context->aux; | |
2194 | context->aux = (void *)ax; | |
2195 | return 0; | |
2196 | } | |
2197 | ||
2198 | /** | |
2199 | * __audit_mq_notify - record audit data for a POSIX MQ notify | |
2200 | * @mqdes: MQ descriptor | |
2201 | * @u_notification: Notification event | |
2202 | * | |
2203 | * Returns 0 for success or NULL context or < 0 on error. | |
2204 | */ | |
2205 | ||
2206 | int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification) | |
2207 | { | |
2208 | struct audit_aux_data_mq_notify *ax; | |
2209 | struct audit_context *context = current->audit_context; | |
2210 | ||
2211 | if (!audit_enabled) | |
2212 | return 0; | |
2213 | ||
2214 | if (likely(!context)) | |
2215 | return 0; | |
2216 | ||
2217 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); | |
2218 | if (!ax) | |
2219 | return -ENOMEM; | |
2220 | ||
2221 | if (u_notification != NULL) { | |
2222 | if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) { | |
2223 | kfree(ax); | |
2224 | return -EFAULT; | |
2225 | } | |
2226 | } else | |
2227 | memset(&ax->notification, 0, sizeof(ax->notification)); | |
2228 | ||
2229 | ax->mqdes = mqdes; | |
2230 | ||
2231 | ax->d.type = AUDIT_MQ_NOTIFY; | |
2232 | ax->d.next = context->aux; | |
2233 | context->aux = (void *)ax; | |
2234 | return 0; | |
2235 | } | |
2236 | ||
2237 | /** | |
2238 | * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute | |
2239 | * @mqdes: MQ descriptor | |
2240 | * @mqstat: MQ flags | |
2241 | * | |
2242 | * Returns 0 for success or NULL context or < 0 on error. | |
2243 | */ | |
2244 | int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat) | |
2245 | { | |
2246 | struct audit_aux_data_mq_getsetattr *ax; | |
2247 | struct audit_context *context = current->audit_context; | |
2248 | ||
2249 | if (!audit_enabled) | |
2250 | return 0; | |
2251 | ||
2252 | if (likely(!context)) | |
2253 | return 0; | |
2254 | ||
2255 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); | |
2256 | if (!ax) | |
2257 | return -ENOMEM; | |
2258 | ||
2259 | ax->mqdes = mqdes; | |
2260 | ax->mqstat = *mqstat; | |
2261 | ||
2262 | ax->d.type = AUDIT_MQ_GETSETATTR; | |
2263 | ax->d.next = context->aux; | |
2264 | context->aux = (void *)ax; | |
2265 | return 0; | |
2266 | } | |
2267 | ||
b0dd25a8 | 2268 | /** |
073115d6 SG |
2269 | * audit_ipc_obj - record audit data for ipc object |
2270 | * @ipcp: ipc permissions | |
2271 | * | |
2272 | * Returns 0 for success or NULL context or < 0 on error. | |
2273 | */ | |
d8945bb5 | 2274 | int __audit_ipc_obj(struct kern_ipc_perm *ipcp) |
073115d6 SG |
2275 | { |
2276 | struct audit_aux_data_ipcctl *ax; | |
2277 | struct audit_context *context = current->audit_context; | |
2278 | ||
073115d6 SG |
2279 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
2280 | if (!ax) | |
2281 | return -ENOMEM; | |
2282 | ||
2283 | ax->uid = ipcp->uid; | |
2284 | ax->gid = ipcp->gid; | |
2285 | ax->mode = ipcp->mode; | |
2a862b32 | 2286 | security_ipc_getsecid(ipcp, &ax->osid); |
073115d6 SG |
2287 | ax->d.type = AUDIT_IPC; |
2288 | ax->d.next = context->aux; | |
2289 | context->aux = (void *)ax; | |
2290 | return 0; | |
2291 | } | |
2292 | ||
2293 | /** | |
2294 | * audit_ipc_set_perm - record audit data for new ipc permissions | |
b0dd25a8 RD |
2295 | * @qbytes: msgq bytes |
2296 | * @uid: msgq user id | |
2297 | * @gid: msgq group id | |
2298 | * @mode: msgq mode (permissions) | |
2299 | * | |
2300 | * Returns 0 for success or NULL context or < 0 on error. | |
2301 | */ | |
d8945bb5 | 2302 | int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode) |
1da177e4 LT |
2303 | { |
2304 | struct audit_aux_data_ipcctl *ax; | |
2305 | struct audit_context *context = current->audit_context; | |
2306 | ||
8c8570fb | 2307 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
1da177e4 LT |
2308 | if (!ax) |
2309 | return -ENOMEM; | |
2310 | ||
2311 | ax->qbytes = qbytes; | |
2312 | ax->uid = uid; | |
2313 | ax->gid = gid; | |
2314 | ax->mode = mode; | |
2315 | ||
073115d6 | 2316 | ax->d.type = AUDIT_IPC_SET_PERM; |
1da177e4 LT |
2317 | ax->d.next = context->aux; |
2318 | context->aux = (void *)ax; | |
2319 | return 0; | |
2320 | } | |
c2f0c7c3 | 2321 | |
473ae30b AV |
2322 | int audit_bprm(struct linux_binprm *bprm) |
2323 | { | |
2324 | struct audit_aux_data_execve *ax; | |
2325 | struct audit_context *context = current->audit_context; | |
473ae30b | 2326 | |
5ac3a9c2 | 2327 | if (likely(!audit_enabled || !context || context->dummy)) |
473ae30b AV |
2328 | return 0; |
2329 | ||
bdf4c48a | 2330 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); |
473ae30b AV |
2331 | if (!ax) |
2332 | return -ENOMEM; | |
2333 | ||
2334 | ax->argc = bprm->argc; | |
2335 | ax->envc = bprm->envc; | |
bdf4c48a | 2336 | ax->mm = bprm->mm; |
473ae30b AV |
2337 | ax->d.type = AUDIT_EXECVE; |
2338 | ax->d.next = context->aux; | |
2339 | context->aux = (void *)ax; | |
2340 | return 0; | |
2341 | } | |
2342 | ||
2343 | ||
b0dd25a8 RD |
2344 | /** |
2345 | * audit_socketcall - record audit data for sys_socketcall | |
2346 | * @nargs: number of args | |
2347 | * @args: args array | |
2348 | * | |
2349 | * Returns 0 for success or NULL context or < 0 on error. | |
2350 | */ | |
3ec3b2fb DW |
2351 | int audit_socketcall(int nargs, unsigned long *args) |
2352 | { | |
2353 | struct audit_aux_data_socketcall *ax; | |
2354 | struct audit_context *context = current->audit_context; | |
2355 | ||
5ac3a9c2 | 2356 | if (likely(!context || context->dummy)) |
3ec3b2fb DW |
2357 | return 0; |
2358 | ||
2359 | ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL); | |
2360 | if (!ax) | |
2361 | return -ENOMEM; | |
2362 | ||
2363 | ax->nargs = nargs; | |
2364 | memcpy(ax->args, args, nargs * sizeof(unsigned long)); | |
2365 | ||
2366 | ax->d.type = AUDIT_SOCKETCALL; | |
2367 | ax->d.next = context->aux; | |
2368 | context->aux = (void *)ax; | |
2369 | return 0; | |
2370 | } | |
2371 | ||
db349509 AV |
2372 | /** |
2373 | * __audit_fd_pair - record audit data for pipe and socketpair | |
2374 | * @fd1: the first file descriptor | |
2375 | * @fd2: the second file descriptor | |
2376 | * | |
2377 | * Returns 0 for success or NULL context or < 0 on error. | |
2378 | */ | |
2379 | int __audit_fd_pair(int fd1, int fd2) | |
2380 | { | |
2381 | struct audit_context *context = current->audit_context; | |
2382 | struct audit_aux_data_fd_pair *ax; | |
2383 | ||
2384 | if (likely(!context)) { | |
2385 | return 0; | |
2386 | } | |
2387 | ||
2388 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); | |
2389 | if (!ax) { | |
2390 | return -ENOMEM; | |
2391 | } | |
2392 | ||
2393 | ax->fd[0] = fd1; | |
2394 | ax->fd[1] = fd2; | |
2395 | ||
2396 | ax->d.type = AUDIT_FD_PAIR; | |
2397 | ax->d.next = context->aux; | |
2398 | context->aux = (void *)ax; | |
2399 | return 0; | |
2400 | } | |
2401 | ||
b0dd25a8 RD |
2402 | /** |
2403 | * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto | |
2404 | * @len: data length in user space | |
2405 | * @a: data address in kernel space | |
2406 | * | |
2407 | * Returns 0 for success or NULL context or < 0 on error. | |
2408 | */ | |
3ec3b2fb DW |
2409 | int audit_sockaddr(int len, void *a) |
2410 | { | |
2411 | struct audit_aux_data_sockaddr *ax; | |
2412 | struct audit_context *context = current->audit_context; | |
2413 | ||
5ac3a9c2 | 2414 | if (likely(!context || context->dummy)) |
3ec3b2fb DW |
2415 | return 0; |
2416 | ||
2417 | ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL); | |
2418 | if (!ax) | |
2419 | return -ENOMEM; | |
2420 | ||
2421 | ax->len = len; | |
2422 | memcpy(ax->a, a, len); | |
2423 | ||
2424 | ax->d.type = AUDIT_SOCKADDR; | |
2425 | ax->d.next = context->aux; | |
2426 | context->aux = (void *)ax; | |
2427 | return 0; | |
2428 | } | |
2429 | ||
a5cb013d AV |
2430 | void __audit_ptrace(struct task_struct *t) |
2431 | { | |
2432 | struct audit_context *context = current->audit_context; | |
2433 | ||
2434 | context->target_pid = t->pid; | |
c2a7780e EP |
2435 | context->target_auid = audit_get_loginuid(t); |
2436 | context->target_uid = t->uid; | |
4746ec5b | 2437 | context->target_sessionid = audit_get_sessionid(t); |
2a862b32 | 2438 | security_task_getsecid(t, &context->target_sid); |
c2a7780e | 2439 | memcpy(context->target_comm, t->comm, TASK_COMM_LEN); |
a5cb013d AV |
2440 | } |
2441 | ||
b0dd25a8 RD |
2442 | /** |
2443 | * audit_signal_info - record signal info for shutting down audit subsystem | |
2444 | * @sig: signal value | |
2445 | * @t: task being signaled | |
2446 | * | |
2447 | * If the audit subsystem is being terminated, record the task (pid) | |
2448 | * and uid that is doing that. | |
2449 | */ | |
e54dc243 | 2450 | int __audit_signal_info(int sig, struct task_struct *t) |
c2f0c7c3 | 2451 | { |
e54dc243 AG |
2452 | struct audit_aux_data_pids *axp; |
2453 | struct task_struct *tsk = current; | |
2454 | struct audit_context *ctx = tsk->audit_context; | |
e1396065 | 2455 | |
175fc484 | 2456 | if (audit_pid && t->tgid == audit_pid) { |
ee1d3156 | 2457 | if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) { |
175fc484 | 2458 | audit_sig_pid = tsk->pid; |
bfef93a5 AV |
2459 | if (tsk->loginuid != -1) |
2460 | audit_sig_uid = tsk->loginuid; | |
175fc484 AV |
2461 | else |
2462 | audit_sig_uid = tsk->uid; | |
2a862b32 | 2463 | security_task_getsecid(tsk, &audit_sig_sid); |
175fc484 AV |
2464 | } |
2465 | if (!audit_signals || audit_dummy_context()) | |
2466 | return 0; | |
c2f0c7c3 | 2467 | } |
e54dc243 | 2468 | |
e54dc243 AG |
2469 | /* optimize the common case by putting first signal recipient directly |
2470 | * in audit_context */ | |
2471 | if (!ctx->target_pid) { | |
2472 | ctx->target_pid = t->tgid; | |
c2a7780e EP |
2473 | ctx->target_auid = audit_get_loginuid(t); |
2474 | ctx->target_uid = t->uid; | |
4746ec5b | 2475 | ctx->target_sessionid = audit_get_sessionid(t); |
2a862b32 | 2476 | security_task_getsecid(t, &ctx->target_sid); |
c2a7780e | 2477 | memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN); |
e54dc243 AG |
2478 | return 0; |
2479 | } | |
2480 | ||
2481 | axp = (void *)ctx->aux_pids; | |
2482 | if (!axp || axp->pid_count == AUDIT_AUX_PIDS) { | |
2483 | axp = kzalloc(sizeof(*axp), GFP_ATOMIC); | |
2484 | if (!axp) | |
2485 | return -ENOMEM; | |
2486 | ||
2487 | axp->d.type = AUDIT_OBJ_PID; | |
2488 | axp->d.next = ctx->aux_pids; | |
2489 | ctx->aux_pids = (void *)axp; | |
2490 | } | |
88ae704c | 2491 | BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS); |
e54dc243 AG |
2492 | |
2493 | axp->target_pid[axp->pid_count] = t->tgid; | |
c2a7780e EP |
2494 | axp->target_auid[axp->pid_count] = audit_get_loginuid(t); |
2495 | axp->target_uid[axp->pid_count] = t->uid; | |
4746ec5b | 2496 | axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t); |
2a862b32 | 2497 | security_task_getsecid(t, &axp->target_sid[axp->pid_count]); |
c2a7780e | 2498 | memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); |
e54dc243 AG |
2499 | axp->pid_count++; |
2500 | ||
2501 | return 0; | |
c2f0c7c3 | 2502 | } |
0a4ff8c2 SG |
2503 | |
2504 | /** | |
2505 | * audit_core_dumps - record information about processes that end abnormally | |
6d9525b5 | 2506 | * @signr: signal value |
0a4ff8c2 SG |
2507 | * |
2508 | * If a process ends with a core dump, something fishy is going on and we | |
2509 | * should record the event for investigation. | |
2510 | */ | |
2511 | void audit_core_dumps(long signr) | |
2512 | { | |
2513 | struct audit_buffer *ab; | |
2514 | u32 sid; | |
4746ec5b EP |
2515 | uid_t auid = audit_get_loginuid(current); |
2516 | unsigned int sessionid = audit_get_sessionid(current); | |
0a4ff8c2 SG |
2517 | |
2518 | if (!audit_enabled) | |
2519 | return; | |
2520 | ||
2521 | if (signr == SIGQUIT) /* don't care for those */ | |
2522 | return; | |
2523 | ||
2524 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND); | |
4746ec5b EP |
2525 | audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u", |
2526 | auid, current->uid, current->gid, sessionid); | |
2a862b32 | 2527 | security_task_getsecid(current, &sid); |
0a4ff8c2 SG |
2528 | if (sid) { |
2529 | char *ctx = NULL; | |
2530 | u32 len; | |
2531 | ||
2a862b32 | 2532 | if (security_secid_to_secctx(sid, &ctx, &len)) |
0a4ff8c2 | 2533 | audit_log_format(ab, " ssid=%u", sid); |
2a862b32 | 2534 | else { |
0a4ff8c2 | 2535 | audit_log_format(ab, " subj=%s", ctx); |
2a862b32 AD |
2536 | security_release_secctx(ctx, len); |
2537 | } | |
0a4ff8c2 SG |
2538 | } |
2539 | audit_log_format(ab, " pid=%d comm=", current->pid); | |
2540 | audit_log_untrustedstring(ab, current->comm); | |
2541 | audit_log_format(ab, " sig=%ld", signr); | |
2542 | audit_log_end(ab); | |
2543 | } |