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