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