blob: 2672d105cffcdaf7703d9f655e7338dd0b102cc0 [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* auditsc.c -- System-call auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handles all system-call specific auditing features.
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
Amy Griffis73241cc2005-11-03 16:00:25 +00005 * Copyright 2005 Hewlett-Packard Development Company, L.P.
George C. Wilson20ca73b2006-05-24 16:09:55 -05006 * Copyright (C) 2005, 2006 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 *
George C. Wilson20ca73b2006-05-24 16:09:55 -050032 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
33 * 2006.
34 *
Dustin Kirklandb63862f2005-11-03 15:41:46 +000035 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
37 *
Amy Griffis73241cc2005-11-03 16:00:25 +000038 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000040 *
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
Richard Guy Briggsf952d102014-01-27 17:38:42 -050045#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070049#include <linux/atomic.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000050#include <linux/fs.h>
51#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/mm.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040053#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Stephen Smalley01116102005-05-21 00:15:52 +010055#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010056#include <linux/socket.h>
George C. Wilson20ca73b2006-05-24 16:09:55 -050057#include <linux/mqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/audit.h>
59#include <linux/personality.h>
60#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010061#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010062#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000064#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000065#include <linux/list.h>
Al Viro473ae302006-04-26 14:04:08 -040066#include <linux/binfmts.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040067#include <linux/highmem.h>
Al Virof46038f2006-05-06 08:22:52 -040068#include <linux/syscalls.h>
Richard Guy Briggs84db5642014-01-29 16:17:58 -050069#include <asm/syscall.h>
Eric Paris851f7ff2008-11-11 21:48:14 +110070#include <linux/capability.h>
Al Viro5ad4e532009-03-29 19:50:06 -040071#include <linux/fs_struct.h>
Kees Cook3dc1c1b2012-04-12 16:47:58 -050072#include <linux/compat.h>
William Roberts3f1c8252014-02-11 10:12:01 -080073#include <linux/ctype.h>
Paul Moorefcf22d82014-12-30 09:26:21 -050074#include <linux/string.h>
75#include <uapi/linux/limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
David Woodhousefe7752b2005-12-15 18:33:52 +000077#include "audit.h"
78
Eric Parisd7e75282012-01-03 14:23:06 -050079/* flags stating the success for a syscall */
80#define AUDITSC_INVALID 0
81#define AUDITSC_SUCCESS 1
82#define AUDITSC_FAILURE 2
83
Eric Parisde6bbd12008-01-07 14:31:58 -050084/* no execve audit message should be longer than this (userspace limits) */
85#define MAX_EXECVE_AUDIT_LEN 7500
86
William Roberts3f1c8252014-02-11 10:12:01 -080087/* max length to print of cmdline/proctitle value during audit */
88#define MAX_PROCTITLE_AUDIT_LEN 128
89
Al Viro471a5c72006-07-10 08:29:24 -040090/* number of audit rules */
91int audit_n_rules;
92
Amy Griffise54dc242007-03-29 18:01:04 -040093/* determines whether we collect data for signals sent */
94int audit_signals;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096struct audit_aux_data {
97 struct audit_aux_data *next;
98 int type;
99};
100
101#define AUDIT_AUX_IPCPERM 0
102
Amy Griffise54dc242007-03-29 18:01:04 -0400103/* Number of target pids per aux struct. */
104#define AUDIT_AUX_PIDS 16
105
Amy Griffise54dc242007-03-29 18:01:04 -0400106struct audit_aux_data_pids {
107 struct audit_aux_data d;
108 pid_t target_pid[AUDIT_AUX_PIDS];
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700109 kuid_t target_auid[AUDIT_AUX_PIDS];
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800110 kuid_t target_uid[AUDIT_AUX_PIDS];
Eric Paris4746ec52008-01-08 10:06:53 -0500111 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400112 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500113 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400114 int pid_count;
115};
116
Eric Paris3fc689e2008-11-11 21:48:18 +1100117struct audit_aux_data_bprm_fcaps {
118 struct audit_aux_data d;
119 struct audit_cap_data fcap;
120 unsigned int fcap_ver;
121 struct audit_cap_data old_pcap;
122 struct audit_cap_data new_pcap;
123};
124
Al Viro74c3cbe2007-07-22 08:04:18 -0400125struct audit_tree_refs {
126 struct audit_tree_refs *next;
127 struct audit_chunk *c[31];
128};
129
Al Viro55669bf2006-08-31 19:26:40 -0400130static int audit_match_perm(struct audit_context *ctx, int mask)
131{
Cordeliac4bacef2008-08-18 09:45:51 -0700132 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800133 if (unlikely(!ctx))
134 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700135 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100136
Al Viro55669bf2006-08-31 19:26:40 -0400137 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
Eric Paris5ef30ee2012-01-03 14:23:05 -0500173static int audit_match_filetype(struct audit_context *ctx, int val)
Al Viro8b67dca2008-04-28 04:15:49 -0400174{
Eric Paris5195d8e2012-01-03 14:23:05 -0500175 struct audit_names *n;
Eric Paris5ef30ee2012-01-03 14:23:05 -0500176 umode_t mode = (umode_t)val;
zhangxiliang1a61c882008-08-02 10:56:37 +0800177
178 if (unlikely(!ctx))
179 return 0;
180
Eric Paris5195d8e2012-01-03 14:23:05 -0500181 list_for_each_entry(n, &ctx->names_list, list) {
Richard Guy Briggs84cb7772015-08-05 23:48:20 -0400182 if ((n->ino != AUDIT_INO_UNSET) &&
Eric Paris5195d8e2012-01-03 14:23:05 -0500183 ((n->mode & S_IFMT) == mode))
Eric Paris5ef30ee2012-01-03 14:23:05 -0500184 return 1;
185 }
Eric Paris5195d8e2012-01-03 14:23:05 -0500186
Eric Paris5ef30ee2012-01-03 14:23:05 -0500187 return 0;
Al Viro8b67dca2008-04-28 04:15:49 -0400188}
189
Al Viro74c3cbe2007-07-22 08:04:18 -0400190/*
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
Eric Paris679173b2009-01-26 18:09:45 -0500201static 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
Al Viro74c3cbe2007-07-22 08:04:18 -0400209static 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
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700309static int audit_compare_uid(kuid_t uid,
310 struct audit_names *name,
311 struct audit_field *f,
312 struct audit_context *ctx)
Eric Parisb34b0392012-01-03 14:23:08 -0500313{
314 struct audit_names *n;
Eric Parisb34b0392012-01-03 14:23:08 -0500315 int rc;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700316
Eric Parisb34b0392012-01-03 14:23:08 -0500317 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700318 rc = audit_uid_comparator(uid, f->op, name->uid);
Eric Parisb34b0392012-01-03 14:23:08 -0500319 if (rc)
320 return rc;
321 }
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700322
Eric Parisb34b0392012-01-03 14:23:08 -0500323 if (ctx) {
324 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700325 rc = audit_uid_comparator(uid, f->op, n->uid);
326 if (rc)
327 return rc;
328 }
329 }
330 return 0;
331}
Eric Parisb34b0392012-01-03 14:23:08 -0500332
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700333static 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);
Eric Parisb34b0392012-01-03 14:23:08 -0500350 if (rc)
351 return rc;
352 }
353 }
354 return 0;
355}
356
Eric Paris02d86a52012-01-03 14:23:08 -0500357static 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{
Eric Paris02d86a52012-01-03 14:23:08 -0500363 switch (f->val) {
Peter Moody4a6633e2011-12-13 16:17:51 -0800364 /* process to file object comparisons */
Eric Paris02d86a52012-01-03 14:23:08 -0500365 case AUDIT_COMPARE_UID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700366 return audit_compare_uid(cred->uid, name, f, ctx);
Eric Parisc9fe6852012-01-03 14:23:08 -0500367 case AUDIT_COMPARE_GID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700368 return audit_compare_gid(cred->gid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800369 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700370 return audit_compare_uid(cred->euid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800371 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700372 return audit_compare_gid(cred->egid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800373 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700374 return audit_compare_uid(tsk->loginuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800375 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700376 return audit_compare_uid(cred->suid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800377 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700378 return audit_compare_gid(cred->sgid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800379 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700380 return audit_compare_uid(cred->fsuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800381 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700382 return audit_compare_gid(cred->fsgid, name, f, ctx);
Peter Moody10d68362012-01-04 15:24:31 -0500383 /* uid comparisons */
384 case AUDIT_COMPARE_UID_TO_AUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700385 return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
Peter Moody10d68362012-01-04 15:24:31 -0500386 case AUDIT_COMPARE_UID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700387 return audit_uid_comparator(cred->uid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500388 case AUDIT_COMPARE_UID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700389 return audit_uid_comparator(cred->uid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500390 case AUDIT_COMPARE_UID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700391 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500392 /* auid comparisons */
393 case AUDIT_COMPARE_AUID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700394 return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500395 case AUDIT_COMPARE_AUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700396 return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500397 case AUDIT_COMPARE_AUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700398 return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500399 /* euid comparisons */
400 case AUDIT_COMPARE_EUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700401 return audit_uid_comparator(cred->euid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500402 case AUDIT_COMPARE_EUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700403 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500404 /* suid comparisons */
405 case AUDIT_COMPARE_SUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700406 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500407 /* gid comparisons */
408 case AUDIT_COMPARE_GID_TO_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700409 return audit_gid_comparator(cred->gid, f->op, cred->egid);
Peter Moody10d68362012-01-04 15:24:31 -0500410 case AUDIT_COMPARE_GID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700411 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500412 case AUDIT_COMPARE_GID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700413 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500414 /* egid comparisons */
415 case AUDIT_COMPARE_EGID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700416 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500417 case AUDIT_COMPARE_EGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700418 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500419 /* sgid comparison */
420 case AUDIT_COMPARE_SGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700421 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
Eric Paris02d86a52012-01-03 14:23:08 -0500422 default:
423 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
424 return 0;
425 }
426 return 0;
427}
428
Amy Griffisf368c07d2006-04-07 16:55:56 -0400429/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/* Compare a task_struct with an audit_rule. Return 1 on match, 0
Tony Jonesf5629882011-04-27 15:10:49 +0200431 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500438 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400440 struct audit_names *name,
Tony Jonesf5629882011-04-27 15:10:49 +0200441 enum audit_state *state,
442 bool task_creation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Tony Jonesf5629882011-04-27 15:10:49 +0200444 const struct cred *cred;
Eric Paris5195d8e2012-01-03 14:23:05 -0500445 int i, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600446 u32 sid;
447
Tony Jonesf5629882011-04-27 15:10:49 +0200448 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500451 struct audit_field *f = &rule->fields[i];
Eric Paris5195d8e2012-01-03 14:23:05 -0500452 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int result = 0;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500454 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Amy Griffis93315ed2006-02-07 12:05:27 -0500456 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 case AUDIT_PID:
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500458 pid = task_pid_nr(tsk);
459 result = audit_comparator(pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
Al Viro3c662512006-05-06 08:26:27 -0400461 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400462 if (ctx) {
463 if (!ctx->ppid)
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -0500464 ctx->ppid = task_ppid_nr(tsk);
Al Viro3c662512006-05-06 08:26:27 -0400465 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400466 }
Al Viro3c662512006-05-06 08:26:27 -0400467 break;
Richard Guy Briggs34d99af52015-08-05 16:29:37 -0400468 case AUDIT_EXE:
469 result = audit_exe_compare(tsk, rule->exe);
470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 case AUDIT_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700472 result = audit_uid_comparator(cred->uid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474 case AUDIT_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700475 result = audit_uid_comparator(cred->euid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 case AUDIT_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700478 result = audit_uid_comparator(cred->suid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case AUDIT_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700481 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483 case AUDIT_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700484 result = audit_gid_comparator(cred->gid, f->op, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300485 if (f->op == Audit_equal) {
486 if (!result)
487 result = in_group_p(f->gid);
488 } else if (f->op == Audit_not_equal) {
489 if (result)
490 result = !in_group_p(f->gid);
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 case AUDIT_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700494 result = audit_gid_comparator(cred->egid, f->op, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300495 if (f->op == Audit_equal) {
496 if (!result)
497 result = in_egroup_p(f->gid);
498 } else if (f->op == Audit_not_equal) {
499 if (result)
500 result = !in_egroup_p(f->gid);
501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503 case AUDIT_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700504 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
506 case AUDIT_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700507 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
509 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500510 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 break;
2fd6f582005-04-29 16:08:28 +0100512 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700513 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500514 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 case AUDIT_EXIT:
518 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500519 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100522 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500523 if (f->val)
524 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100525 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500526 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529 case AUDIT_DEVMAJOR:
Eric Paris16c174b2012-01-03 14:23:05 -0500530 if (name) {
531 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
532 audit_comparator(MAJOR(name->rdev), f->op, f->val))
533 ++result;
534 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500535 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174b2012-01-03 14:23:05 -0500536 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
537 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ++result;
539 break;
540 }
541 }
542 }
543 break;
544 case AUDIT_DEVMINOR:
Eric Paris16c174b2012-01-03 14:23:05 -0500545 if (name) {
546 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
547 audit_comparator(MINOR(name->rdev), f->op, f->val))
548 ++result;
549 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500550 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174b2012-01-03 14:23:05 -0500551 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
552 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ++result;
554 break;
555 }
556 }
557 }
558 break;
559 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400560 if (name)
Richard Guy Briggsdb510fc2013-07-04 12:56:11 -0400561 result = audit_comparator(name->ino, f->op, f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400562 else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500563 list_for_each_entry(n, &ctx->names_list, list) {
564 if (audit_comparator(n->ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 ++result;
566 break;
567 }
568 }
569 }
570 break;
Eric Parisefaffd62012-01-03 14:23:07 -0500571 case AUDIT_OBJ_UID:
572 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700573 result = audit_uid_comparator(name->uid, f->op, f->uid);
Eric Parisefaffd62012-01-03 14:23:07 -0500574 } else if (ctx) {
575 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700576 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
Eric Parisefaffd62012-01-03 14:23:07 -0500577 ++result;
578 break;
579 }
580 }
581 }
582 break;
Eric Paris54d32182012-01-03 14:23:07 -0500583 case AUDIT_OBJ_GID:
584 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700585 result = audit_gid_comparator(name->gid, f->op, f->gid);
Eric Paris54d32182012-01-03 14:23:07 -0500586 } else if (ctx) {
587 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700588 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
Eric Paris54d32182012-01-03 14:23:07 -0500589 ++result;
590 break;
591 }
592 }
593 }
594 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400595 case AUDIT_WATCH:
Eric Parisae7b8f42009-12-17 20:12:04 -0500596 if (name)
597 result = audit_watch_compare(rule->watch, name->ino, name->dev);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400598 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400599 case AUDIT_DIR:
600 if (ctx)
601 result = match_tree_refs(ctx, rule->tree);
602 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 case AUDIT_LOGINUID:
Mikhail Klementyev5c1390c2015-05-25 23:20:38 +0300604 result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
Eric W. Biederman780a7652013-04-09 02:22:10 -0700606 case AUDIT_LOGINUID_SET:
607 result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
608 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500609 case AUDIT_SUBJ_USER:
610 case AUDIT_SUBJ_ROLE:
611 case AUDIT_SUBJ_TYPE:
612 case AUDIT_SUBJ_SEN:
613 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600614 /* NOTE: this may return negative values indicating
615 a temporary error. We simply treat this as a
616 match for now to avoid losing information that
617 may be wanted. An error message will also be
618 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000619 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400620 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200621 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400622 need_sid = 0;
623 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200624 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600625 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000626 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600627 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400628 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600629 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500630 case AUDIT_OBJ_USER:
631 case AUDIT_OBJ_ROLE:
632 case AUDIT_OBJ_TYPE:
633 case AUDIT_OBJ_LEV_LOW:
634 case AUDIT_OBJ_LEV_HIGH:
635 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
636 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000637 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500638 /* Find files that match */
639 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200640 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500641 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000642 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500643 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500644 list_for_each_entry(n, &ctx->names_list, list) {
645 if (security_audit_rule_match(n->osid, f->type,
646 f->op, f->lsm_rule,
647 ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500648 ++result;
649 break;
650 }
651 }
652 }
653 /* Find ipc objects that match */
Al Viroa33e6752008-12-10 03:40:06 -0500654 if (!ctx || ctx->type != AUDIT_IPC)
655 break;
656 if (security_audit_rule_match(ctx->ipc.osid,
657 f->type, f->op,
658 f->lsm_rule, ctx))
659 ++result;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500660 }
661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 case AUDIT_ARG0:
663 case AUDIT_ARG1:
664 case AUDIT_ARG2:
665 case AUDIT_ARG3:
666 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500667 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400669 case AUDIT_FILTERKEY:
670 /* ignore this field for filtering */
671 result = 1;
672 break;
Al Viro55669bf2006-08-31 19:26:40 -0400673 case AUDIT_PERM:
674 result = audit_match_perm(ctx, f->val);
675 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400676 case AUDIT_FILETYPE:
677 result = audit_match_filetype(ctx, f->val);
678 break;
Eric Paris02d86a52012-01-03 14:23:08 -0500679 case AUDIT_FIELD_COMPARE:
680 result = audit_field_compare(tsk, cred, f, ctx, name);
681 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Tony Jonesf5629882011-04-27 15:10:49 +0200683 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 0;
685 }
Al Viro0590b932008-12-14 23:45:27 -0500686
687 if (ctx) {
688 if (rule->prio <= ctx->prio)
689 return 0;
690 if (rule->filterkey) {
691 kfree(ctx->filterkey);
692 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
693 }
694 ctx->prio = rule->prio;
695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 switch (rule->action) {
697 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
699 }
700 return 1;
701}
702
703/* At process creation time, we can determine if system-call auditing is
704 * completely disabled for this task. Since we only have the task
705 * structure at this point, we can only check uid and gid.
706 */
Al Viroe048e022008-12-16 03:51:22 -0500707static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 struct audit_entry *e;
710 enum audit_state state;
711
712 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100713 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Tony Jonesf5629882011-04-27 15:10:49 +0200714 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
715 &state, true)) {
Al Viroe048e022008-12-16 03:51:22 -0500716 if (state == AUDIT_RECORD_CONTEXT)
717 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 rcu_read_unlock();
719 return state;
720 }
721 }
722 rcu_read_unlock();
723 return AUDIT_BUILD_CONTEXT;
724}
725
Andy Lutomirskia3c54932014-05-28 23:09:58 -0400726static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
727{
728 int word, bit;
729
730 if (val > 0xffffffff)
731 return false;
732
733 word = AUDIT_WORD(val);
734 if (word >= AUDIT_BITMASK_SIZE)
735 return false;
736
737 bit = AUDIT_BIT(val);
738
739 return rule->mask[word] & bit;
740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/* At syscall entry and exit time, this filter is called if the
743 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100744 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700745 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
747static enum audit_state audit_filter_syscall(struct task_struct *tsk,
748 struct audit_context *ctx,
749 struct list_head *list)
750{
751 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100752 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
David Woodhouse351bb722005-07-14 14:40:06 +0100754 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100755 return AUDIT_DISABLED;
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100758 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000759 list_for_each_entry_rcu(e, list, list) {
Andy Lutomirskia3c54932014-05-28 23:09:58 -0400760 if (audit_in_mask(&e->rule, ctx->major) &&
Amy Griffisf368c07d2006-04-07 16:55:56 -0400761 audit_filter_rules(tsk, &e->rule, ctx, NULL,
Tony Jonesf5629882011-04-27 15:10:49 +0200762 &state, false)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000763 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500764 ctx->current_state = state;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000765 return state;
766 }
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 rcu_read_unlock();
770 return AUDIT_BUILD_CONTEXT;
771}
772
Eric Paris5195d8e2012-01-03 14:23:05 -0500773/*
774 * Given an audit_name check the inode hash table to see if they match.
775 * Called holding the rcu read lock to protect the use of audit_inode_hash
776 */
777static int audit_filter_inode_name(struct task_struct *tsk,
778 struct audit_names *n,
779 struct audit_context *ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500780 int h = audit_hash_ino((u32)n->ino);
781 struct list_head *list = &audit_inode_hash[h];
782 struct audit_entry *e;
783 enum audit_state state;
784
Eric Paris5195d8e2012-01-03 14:23:05 -0500785 if (list_empty(list))
786 return 0;
787
788 list_for_each_entry_rcu(e, list, list) {
Andy Lutomirskia3c54932014-05-28 23:09:58 -0400789 if (audit_in_mask(&e->rule, ctx->major) &&
Eric Paris5195d8e2012-01-03 14:23:05 -0500790 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
791 ctx->current_state = state;
792 return 1;
793 }
794 }
795
796 return 0;
797}
798
799/* At syscall exit time, this filter is called if any audit_names have been
Amy Griffisf368c07d2006-04-07 16:55:56 -0400800 * collected during syscall processing. We only check rules in sublists at hash
Eric Paris5195d8e2012-01-03 14:23:05 -0500801 * buckets applicable to the inode numbers in audit_names.
Amy Griffisf368c07d2006-04-07 16:55:56 -0400802 * Regarding audit_state, same rules apply as for audit_filter_syscall().
803 */
Al Viro0590b932008-12-14 23:45:27 -0500804void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400805{
Eric Paris5195d8e2012-01-03 14:23:05 -0500806 struct audit_names *n;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400807
808 if (audit_pid && tsk->tgid == audit_pid)
Al Viro0590b932008-12-14 23:45:27 -0500809 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400810
811 rcu_read_lock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400812
Eric Paris5195d8e2012-01-03 14:23:05 -0500813 list_for_each_entry(n, &ctx->names_list, list) {
814 if (audit_filter_inode_name(tsk, n, ctx))
815 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400816 }
817 rcu_read_unlock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400818}
819
Richard Guy Briggs4a3eb7262014-02-18 15:29:43 -0500820/* Transfer the audit context pointer to the caller, clearing it in the tsk's struct */
821static inline struct audit_context *audit_take_context(struct task_struct *tsk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int return_valid,
Paul Moore6d208da2009-04-01 15:47:27 -0400823 long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct audit_context *context = tsk->audit_context;
826
Eric Paris56179a62012-01-03 14:23:06 -0500827 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return NULL;
829 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500830
831 /*
832 * we need to fix up the return code in the audit logs if the actual
833 * return codes are later going to be fixed up by the arch specific
834 * signal handlers
835 *
836 * This is actually a test for:
837 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
838 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
839 *
840 * but is faster than a bunch of ||
841 */
842 if (unlikely(return_code <= -ERESTARTSYS) &&
843 (return_code >= -ERESTART_RESTARTBLOCK) &&
844 (return_code != -ENOIOCTLCMD))
845 context->return_code = -EINTR;
846 else
847 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Al Viro0590b932008-12-14 23:45:27 -0500849 if (context->in_syscall && !context->dummy) {
850 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
851 audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 tsk->audit_context = NULL;
855 return context;
856}
857
William Roberts3f1c8252014-02-11 10:12:01 -0800858static inline void audit_proctitle_free(struct audit_context *context)
859{
860 kfree(context->proctitle.value);
861 context->proctitle.value = NULL;
862 context->proctitle.len = 0;
863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865static inline void audit_free_names(struct audit_context *context)
866{
Eric Paris5195d8e2012-01-03 14:23:05 -0500867 struct audit_names *n, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Eric Paris5195d8e2012-01-03 14:23:05 -0500869 list_for_each_entry_safe(n, next, &context->names_list, list) {
870 list_del(&n->list);
Paul Moore55422d02015-01-22 00:00:23 -0500871 if (n->name)
872 putname(n->name);
Eric Paris5195d8e2012-01-03 14:23:05 -0500873 if (n->should_free)
874 kfree(n);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -0800877 path_put(&context->pwd);
878 context->pwd.dentry = NULL;
879 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882static inline void audit_free_aux(struct audit_context *context)
883{
884 struct audit_aux_data *aux;
885
886 while ((aux = context->aux)) {
887 context->aux = aux->next;
888 kfree(aux);
889 }
Amy Griffise54dc242007-03-29 18:01:04 -0400890 while ((aux = context->aux_pids)) {
891 context->aux_pids = aux->next;
892 kfree(aux);
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896static inline struct audit_context *audit_alloc_context(enum audit_state state)
897{
898 struct audit_context *context;
899
Rakib Mullick17c6ee72013-04-07 16:14:18 +0600900 context = kzalloc(sizeof(*context), GFP_KERNEL);
901 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return NULL;
Andrew Mortone2c5adc2013-04-08 14:43:41 -0700903 context->state = state;
904 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Al Viro916d7572009-06-24 00:02:38 -0400905 INIT_LIST_HEAD(&context->killed_trees);
Eric Paris5195d8e2012-01-03 14:23:05 -0500906 INIT_LIST_HEAD(&context->names_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return context;
908}
909
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700910/**
911 * audit_alloc - allocate an audit context block for a task
912 * @tsk: task
913 *
914 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * if necessary. Doing so turns on system call auditing for the
916 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700917 * needed.
918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919int audit_alloc(struct task_struct *tsk)
920{
921 struct audit_context *context;
922 enum audit_state state;
Al Viroe048e022008-12-16 03:51:22 -0500923 char *key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Eric Parisb593d382008-01-08 17:38:31 -0500925 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 0; /* Return if not auditing. */
927
Al Viroe048e022008-12-16 03:51:22 -0500928 state = audit_filter_task(tsk, &key);
Oleg Nesterovd48d8052013-09-15 19:11:09 +0200929 if (state == AUDIT_DISABLED) {
930 clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
Oleg Nesterovd48d8052013-09-15 19:11:09 +0200932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 if (!(context = audit_alloc_context(state))) {
Al Viroe048e022008-12-16 03:51:22 -0500935 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 audit_log_lost("out of memory in audit_alloc");
937 return -ENOMEM;
938 }
Al Viroe048e022008-12-16 03:51:22 -0500939 context->filterkey = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 tsk->audit_context = context;
942 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
943 return 0;
944}
945
946static inline void audit_free_context(struct audit_context *context)
947{
Al Viroc62d7732012-10-20 15:07:18 -0400948 audit_free_names(context);
949 unroll_tree_refs(context, NULL, 0);
950 free_tree_refs(context);
951 audit_free_aux(context);
952 kfree(context->filterkey);
953 kfree(context->sockaddr);
William Roberts3f1c8252014-02-11 10:12:01 -0800954 audit_proctitle_free(context);
Al Viroc62d7732012-10-20 15:07:18 -0400955 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Amy Griffise54dc242007-03-29 18:01:04 -0400958static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800959 kuid_t auid, kuid_t uid, unsigned int sessionid,
Eric Paris4746ec52008-01-08 10:06:53 -0500960 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400961{
962 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200963 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -0400964 u32 len;
965 int rc = 0;
966
967 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
968 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -0500969 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -0400970
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700971 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
972 from_kuid(&init_user_ns, auid),
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800973 from_kuid(&init_user_ns, uid), sessionid);
Eric Parisad395ab2012-10-23 08:58:35 -0400974 if (sid) {
975 if (security_secid_to_secctx(sid, &ctx, &len)) {
976 audit_log_format(ab, " obj=(none)");
977 rc = 1;
978 } else {
979 audit_log_format(ab, " obj=%s", ctx);
980 security_release_secctx(ctx, len);
981 }
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200982 }
Eric Parisc2a77802008-01-07 13:40:17 -0500983 audit_log_format(ab, " ocomm=");
984 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -0400985 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -0400986
987 return rc;
988}
989
Eric Parisde6bbd12008-01-07 14:31:58 -0500990/*
991 * to_send and len_sent accounting are very loose estimates. We aren't
992 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300993 * within about 500 bytes (next page boundary)
Eric Parisde6bbd12008-01-07 14:31:58 -0500994 *
995 * why snprintf? an int is up to 12 digits long. if we just assumed when
996 * logging that a[%d]= was going to be 16 characters long we would be wasting
997 * space in every audit message. In one 7500 byte message we can log up to
998 * about 1000 min size arguments. That comes down to about 50% waste of space
999 * if we didn't do the snprintf to find out how long arg_num_len was.
1000 */
1001static int audit_log_single_execve_arg(struct audit_context *context,
1002 struct audit_buffer **ab,
1003 int arg_num,
1004 size_t *len_sent,
1005 const char __user *p,
1006 char *buf)
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001007{
Eric Parisde6bbd12008-01-07 14:31:58 -05001008 char arg_num_len_buf[12];
1009 const char __user *tmp_p = p;
Eric Parisb87ce6e2009-06-11 14:31:34 -04001010 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1011 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
Eric Parisde6bbd12008-01-07 14:31:58 -05001012 size_t len, len_left, to_send;
1013 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1014 unsigned int i, has_cntl = 0, too_long = 0;
1015 int ret;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001016
Eric Parisde6bbd12008-01-07 14:31:58 -05001017 /* strnlen_user includes the null we don't want to send */
1018 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001019
Eric Parisde6bbd12008-01-07 14:31:58 -05001020 /*
1021 * We just created this mm, if we can't find the strings
1022 * we just copied into it something is _very_ wrong. Similar
1023 * for strings that are too long, we should not have created
1024 * any.
1025 */
Linus Torvalds45820c22015-07-08 09:33:38 -07001026 if (WARN_ON_ONCE(len < 0 || len > MAX_ARG_STRLEN - 1)) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001027 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001028 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001029 }
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001030
Eric Parisde6bbd12008-01-07 14:31:58 -05001031 /* walk the whole argument looking for non-ascii chars */
1032 do {
1033 if (len_left > MAX_EXECVE_AUDIT_LEN)
1034 to_send = MAX_EXECVE_AUDIT_LEN;
1035 else
1036 to_send = len_left;
1037 ret = copy_from_user(buf, tmp_p, to_send);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001038 /*
1039 * There is no reason for this copy to be short. We just
1040 * copied them here, and the mm hasn't been exposed to user-
1041 * space yet.
1042 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001043 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001044 WARN_ON(1);
1045 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001046 return -1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001047 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001048 buf[to_send] = '\0';
1049 has_cntl = audit_string_contains_control(buf, to_send);
1050 if (has_cntl) {
1051 /*
1052 * hex messages get logged as 2 bytes, so we can only
1053 * send half as much in each message
1054 */
1055 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1056 break;
1057 }
1058 len_left -= to_send;
1059 tmp_p += to_send;
1060 } while (len_left > 0);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001061
Eric Parisde6bbd12008-01-07 14:31:58 -05001062 len_left = len;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001063
Eric Parisde6bbd12008-01-07 14:31:58 -05001064 if (len > max_execve_audit_len)
1065 too_long = 1;
1066
1067 /* rewalk the argument actually logging the message */
1068 for (i = 0; len_left > 0; i++) {
1069 int room_left;
1070
1071 if (len_left > max_execve_audit_len)
1072 to_send = max_execve_audit_len;
1073 else
1074 to_send = len_left;
1075
1076 /* do we have space left to send this argument in this ab? */
1077 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1078 if (has_cntl)
1079 room_left -= (to_send * 2);
1080 else
1081 room_left -= to_send;
1082 if (room_left < 0) {
1083 *len_sent = 0;
1084 audit_log_end(*ab);
1085 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1086 if (!*ab)
1087 return 0;
1088 }
1089
1090 /*
1091 * first record needs to say how long the original string was
1092 * so we can be sure nothing was lost.
1093 */
1094 if ((i == 0) && (too_long))
Jiri Pirkoca96a892009-01-09 16:44:16 +01001095 audit_log_format(*ab, " a%d_len=%zu", arg_num,
Eric Parisde6bbd12008-01-07 14:31:58 -05001096 has_cntl ? 2*len : len);
1097
1098 /*
1099 * normally arguments are small enough to fit and we already
1100 * filled buf above when we checked for control characters
1101 * so don't bother with another copy_from_user
1102 */
1103 if (len >= max_execve_audit_len)
1104 ret = copy_from_user(buf, p, to_send);
1105 else
1106 ret = 0;
1107 if (ret) {
1108 WARN_ON(1);
1109 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001110 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001111 }
1112 buf[to_send] = '\0';
1113
1114 /* actually log it */
Jiri Pirkoca96a892009-01-09 16:44:16 +01001115 audit_log_format(*ab, " a%d", arg_num);
Eric Parisde6bbd12008-01-07 14:31:58 -05001116 if (too_long)
1117 audit_log_format(*ab, "[%d]", i);
1118 audit_log_format(*ab, "=");
1119 if (has_cntl)
Eric Parisb556f8a2008-04-18 10:12:59 -04001120 audit_log_n_hex(*ab, buf, to_send);
Eric Parisde6bbd12008-01-07 14:31:58 -05001121 else
Eric Paris9d960982009-06-11 14:31:37 -04001122 audit_log_string(*ab, buf);
Eric Parisde6bbd12008-01-07 14:31:58 -05001123
1124 p += to_send;
1125 len_left -= to_send;
1126 *len_sent += arg_num_len;
1127 if (has_cntl)
1128 *len_sent += to_send * 2;
1129 else
1130 *len_sent += to_send;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001131 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001132 /* include the null we didn't log */
1133 return len + 1;
1134}
1135
1136static void audit_log_execve_info(struct audit_context *context,
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001137 struct audit_buffer **ab)
Eric Parisde6bbd12008-01-07 14:31:58 -05001138{
Xi Wang5afb8a32011-12-20 18:39:41 -05001139 int i, len;
1140 size_t len_sent = 0;
Eric Parisde6bbd12008-01-07 14:31:58 -05001141 const char __user *p;
1142 char *buf;
1143
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001144 p = (const char __user *)current->mm->arg_start;
Eric Parisde6bbd12008-01-07 14:31:58 -05001145
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001146 audit_log_format(*ab, "argc=%d", context->execve.argc);
Eric Parisde6bbd12008-01-07 14:31:58 -05001147
1148 /*
1149 * we need some kernel buffer to hold the userspace args. Just
1150 * allocate one big one rather than allocating one of the right size
1151 * for every single argument inside audit_log_single_execve_arg()
1152 * should be <8k allocation so should be pretty safe.
1153 */
1154 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1155 if (!buf) {
Joe Perchesb7550782014-03-05 14:34:36 -08001156 audit_panic("out of memory for argv string");
Eric Parisde6bbd12008-01-07 14:31:58 -05001157 return;
1158 }
1159
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001160 for (i = 0; i < context->execve.argc; i++) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001161 len = audit_log_single_execve_arg(context, ab, i,
1162 &len_sent, p, buf);
1163 if (len <= 0)
1164 break;
1165 p += len;
1166 }
1167 kfree(buf);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001168}
1169
Al Viroa33e6752008-12-10 03:40:06 -05001170static void show_special(struct audit_context *context, int *call_panic)
Al Virof3298dc2008-12-10 03:16:51 -05001171{
1172 struct audit_buffer *ab;
1173 int i;
1174
1175 ab = audit_log_start(context, GFP_KERNEL, context->type);
1176 if (!ab)
1177 return;
1178
1179 switch (context->type) {
1180 case AUDIT_SOCKETCALL: {
1181 int nargs = context->socketcall.nargs;
1182 audit_log_format(ab, "nargs=%d", nargs);
1183 for (i = 0; i < nargs; i++)
1184 audit_log_format(ab, " a%d=%lx", i,
1185 context->socketcall.args[i]);
1186 break; }
Al Viroa33e6752008-12-10 03:40:06 -05001187 case AUDIT_IPC: {
1188 u32 osid = context->ipc.osid;
1189
Al Viro2570ebb2011-07-27 14:03:22 -04001190 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001191 from_kuid(&init_user_ns, context->ipc.uid),
1192 from_kgid(&init_user_ns, context->ipc.gid),
1193 context->ipc.mode);
Al Viroa33e6752008-12-10 03:40:06 -05001194 if (osid) {
1195 char *ctx = NULL;
1196 u32 len;
1197 if (security_secid_to_secctx(osid, &ctx, &len)) {
1198 audit_log_format(ab, " osid=%u", osid);
1199 *call_panic = 1;
1200 } else {
1201 audit_log_format(ab, " obj=%s", ctx);
1202 security_release_secctx(ctx, len);
1203 }
1204 }
Al Viroe816f372008-12-10 03:47:15 -05001205 if (context->ipc.has_perm) {
1206 audit_log_end(ab);
1207 ab = audit_log_start(context, GFP_KERNEL,
1208 AUDIT_IPC_SET_PERM);
Kees Cook0644ec02013-01-11 14:32:07 -08001209 if (unlikely(!ab))
1210 return;
Al Viroe816f372008-12-10 03:47:15 -05001211 audit_log_format(ab,
Al Viro2570ebb2011-07-27 14:03:22 -04001212 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
Al Viroe816f372008-12-10 03:47:15 -05001213 context->ipc.qbytes,
1214 context->ipc.perm_uid,
1215 context->ipc.perm_gid,
1216 context->ipc.perm_mode);
Al Viroe816f372008-12-10 03:47:15 -05001217 }
Al Viroa33e6752008-12-10 03:40:06 -05001218 break; }
Al Viro564f6992008-12-14 04:02:26 -05001219 case AUDIT_MQ_OPEN: {
1220 audit_log_format(ab,
Al Virodf0a4282011-07-26 05:26:10 -04001221 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
Al Viro564f6992008-12-14 04:02:26 -05001222 "mq_msgsize=%ld mq_curmsgs=%ld",
1223 context->mq_open.oflag, context->mq_open.mode,
1224 context->mq_open.attr.mq_flags,
1225 context->mq_open.attr.mq_maxmsg,
1226 context->mq_open.attr.mq_msgsize,
1227 context->mq_open.attr.mq_curmsgs);
1228 break; }
Al Viroc32c8af2008-12-14 03:46:48 -05001229 case AUDIT_MQ_SENDRECV: {
1230 audit_log_format(ab,
1231 "mqdes=%d msg_len=%zd msg_prio=%u "
1232 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1233 context->mq_sendrecv.mqdes,
1234 context->mq_sendrecv.msg_len,
1235 context->mq_sendrecv.msg_prio,
1236 context->mq_sendrecv.abs_timeout.tv_sec,
1237 context->mq_sendrecv.abs_timeout.tv_nsec);
1238 break; }
Al Viro20114f72008-12-10 07:16:12 -05001239 case AUDIT_MQ_NOTIFY: {
1240 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1241 context->mq_notify.mqdes,
1242 context->mq_notify.sigev_signo);
1243 break; }
Al Viro73929062008-12-10 06:58:59 -05001244 case AUDIT_MQ_GETSETATTR: {
1245 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1246 audit_log_format(ab,
1247 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1248 "mq_curmsgs=%ld ",
1249 context->mq_getsetattr.mqdes,
1250 attr->mq_flags, attr->mq_maxmsg,
1251 attr->mq_msgsize, attr->mq_curmsgs);
1252 break; }
Al Viro57f71a02009-01-04 14:52:57 -05001253 case AUDIT_CAPSET: {
1254 audit_log_format(ab, "pid=%d", context->capset.pid);
1255 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1256 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1257 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1258 break; }
Al Viro120a7952010-10-30 02:54:44 -04001259 case AUDIT_MMAP: {
1260 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1261 context->mmap.flags);
1262 break; }
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001263 case AUDIT_EXECVE: {
1264 audit_log_execve_info(context, &ab);
1265 break; }
Al Virof3298dc2008-12-10 03:16:51 -05001266 }
1267 audit_log_end(ab);
1268}
1269
William Roberts3f1c8252014-02-11 10:12:01 -08001270static inline int audit_proctitle_rtrim(char *proctitle, int len)
1271{
1272 char *end = proctitle + len - 1;
1273 while (end > proctitle && !isprint(*end))
1274 end--;
1275
1276 /* catch the case where proctitle is only 1 non-print character */
1277 len = end - proctitle + 1;
1278 len -= isprint(proctitle[len-1]) == 0;
1279 return len;
1280}
1281
1282static void audit_log_proctitle(struct task_struct *tsk,
1283 struct audit_context *context)
1284{
1285 int res;
1286 char *buf;
1287 char *msg = "(null)";
1288 int len = strlen(msg);
1289 struct audit_buffer *ab;
1290
1291 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PROCTITLE);
1292 if (!ab)
1293 return; /* audit_panic or being filtered */
1294
1295 audit_log_format(ab, "proctitle=");
1296
1297 /* Not cached */
1298 if (!context->proctitle.value) {
1299 buf = kmalloc(MAX_PROCTITLE_AUDIT_LEN, GFP_KERNEL);
1300 if (!buf)
1301 goto out;
1302 /* Historically called this from procfs naming */
1303 res = get_cmdline(tsk, buf, MAX_PROCTITLE_AUDIT_LEN);
1304 if (res == 0) {
1305 kfree(buf);
1306 goto out;
1307 }
1308 res = audit_proctitle_rtrim(buf, res);
1309 if (res == 0) {
1310 kfree(buf);
1311 goto out;
1312 }
1313 context->proctitle.value = buf;
1314 context->proctitle.len = res;
1315 }
1316 msg = context->proctitle.value;
1317 len = context->proctitle.len;
1318out:
1319 audit_log_n_untrustedstring(ab, msg, len);
1320 audit_log_end(ab);
1321}
1322
Al Viroe4951492006-03-29 20:17:10 -05001323static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001325 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001327 struct audit_aux_data *aux;
Eric Paris5195d8e2012-01-03 14:23:05 -05001328 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Al Viroe4951492006-03-29 20:17:10 -05001330 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001331 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001332
1333 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!ab)
1335 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001336 audit_log_format(ab, "arch=%x syscall=%d",
1337 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (context->personality != PER_LINUX)
1339 audit_log_format(ab, " per=%lx", context->personality);
1340 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001341 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001342 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1343 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 audit_log_format(ab,
Peter Moodye23eb922012-06-14 10:04:35 -07001346 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1347 context->argv[0],
1348 context->argv[1],
1349 context->argv[2],
1350 context->argv[3],
1351 context->name_count);
Alan Coxeb84a202006-09-29 02:01:41 -07001352
Al Viroe4951492006-03-29 20:17:10 -05001353 audit_log_task_info(ab, tsk);
Eric Paris9d960982009-06-11 14:31:37 -04001354 audit_log_key(ab, context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
David Woodhouse7551ced2005-05-26 12:04:57 +01001357 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001358
Al Viroe4951492006-03-29 20:17:10 -05001359 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (!ab)
1361 continue; /* audit_panic has been called */
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001364
Eric Paris3fc689e2008-11-11 21:48:18 +11001365 case AUDIT_BPRM_FCAPS: {
1366 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1367 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1368 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1369 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1370 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1371 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1372 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1373 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1374 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1375 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1376 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1377 break; }
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Al Virof3298dc2008-12-10 03:16:51 -05001383 if (context->type)
Al Viroa33e6752008-12-10 03:40:06 -05001384 show_special(context, &call_panic);
Al Virof3298dc2008-12-10 03:16:51 -05001385
Al Viro157cf642008-12-14 04:57:47 -05001386 if (context->fds[0] >= 0) {
1387 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1388 if (ab) {
1389 audit_log_format(ab, "fd0=%d fd1=%d",
1390 context->fds[0], context->fds[1]);
1391 audit_log_end(ab);
1392 }
1393 }
1394
Al Viro4f6b4342008-12-09 19:50:34 -05001395 if (context->sockaddr_len) {
1396 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1397 if (ab) {
1398 audit_log_format(ab, "saddr=");
1399 audit_log_n_hex(ab, (void *)context->sockaddr,
1400 context->sockaddr_len);
1401 audit_log_end(ab);
1402 }
1403 }
1404
Amy Griffise54dc242007-03-29 18:01:04 -04001405 for (aux = context->aux_pids; aux; aux = aux->next) {
1406 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001407
1408 for (i = 0; i < axs->pid_count; i++)
1409 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001410 axs->target_auid[i],
1411 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001412 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001413 axs->target_sid[i],
1414 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001415 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001416 }
1417
Amy Griffise54dc242007-03-29 18:01:04 -04001418 if (context->target_pid &&
1419 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001420 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001421 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001422 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001423 call_panic = 1;
1424
Jan Blunck44707fd2008-02-14 19:38:33 -08001425 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001426 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001427 if (ab) {
Kees Cookc158a352012-01-06 14:07:10 -08001428 audit_log_d_path(ab, " cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001429 audit_log_end(ab);
1430 }
1431 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001432
Eric Paris5195d8e2012-01-03 14:23:05 -05001433 i = 0;
Jeff Layton79f65302013-07-08 15:59:36 -07001434 list_for_each_entry(n, &context->names_list, list) {
1435 if (n->hidden)
1436 continue;
Eric Parisb24a30a2013-04-30 15:30:32 -04001437 audit_log_name(context, n, NULL, i++, &call_panic);
Jeff Layton79f65302013-07-08 15:59:36 -07001438 }
Eric Parisc0641f22008-01-07 13:49:15 -05001439
William Roberts3f1c8252014-02-11 10:12:01 -08001440 audit_log_proctitle(tsk, context);
1441
Eric Parisc0641f22008-01-07 13:49:15 -05001442 /* Send end of event record to help user space know we are finished */
1443 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1444 if (ab)
1445 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001446 if (call_panic)
1447 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001450/**
1451 * audit_free - free a per-task audit context
1452 * @tsk: task whose audit context block to free
1453 *
Al Virofa84cb92006-03-29 20:30:19 -05001454 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001455 */
Eric Parisa4ff8db2012-01-03 14:23:07 -05001456void __audit_free(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct audit_context *context;
1459
Richard Guy Briggs4a3eb7262014-02-18 15:29:43 -05001460 context = audit_take_context(tsk, 0, 0);
Eric Paris56179a62012-01-03 14:23:06 -05001461 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return;
1463
1464 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001465 * function (e.g., exit_group), then free context block.
1466 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001467 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001468 /* that can happen only if we are called from do_exit() */
Al Viro0590b932008-12-14 23:45:27 -05001469 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001470 audit_log_exit(context, tsk);
Al Viro916d7572009-06-24 00:02:38 -04001471 if (!list_empty(&context->killed_trees))
1472 audit_kill_trees(&context->killed_trees);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 audit_free_context(context);
1475}
1476
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001477/**
1478 * audit_syscall_entry - fill in an audit record at syscall entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001479 * @major: major syscall type (function)
1480 * @a1: additional syscall register 1
1481 * @a2: additional syscall register 2
1482 * @a3: additional syscall register 3
1483 * @a4: additional syscall register 4
1484 *
1485 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 * audit context was created when the task was created and the state or
1487 * filters demand the audit context be built. If the state from the
1488 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1489 * then the record will be written at syscall exit time (otherwise, it
1490 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001491 * be written).
1492 */
Richard Guy Briggsb4f0d372014-03-04 10:38:06 -05001493void __audit_syscall_entry(int major, unsigned long a1, unsigned long a2,
1494 unsigned long a3, unsigned long a4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Al Viro5411be52006-03-29 20:23:36 -05001496 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 struct audit_context *context = tsk->audit_context;
1498 enum audit_state state;
1499
Eric Paris56179a62012-01-03 14:23:06 -05001500 if (!context)
Roland McGrath86a1c342008-06-23 15:37:04 -07001501 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 BUG_ON(context->in_syscall || context->name_count);
1504
1505 if (!audit_enabled)
1506 return;
1507
Richard Guy Briggs4a998542014-02-28 14:30:45 -05001508 context->arch = syscall_get_arch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 context->major = major;
1510 context->argv[0] = a1;
1511 context->argv[1] = a2;
1512 context->argv[2] = a3;
1513 context->argv[3] = a4;
1514
1515 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001516 context->dummy = !audit_n_rules;
Al Viro0590b932008-12-14 23:45:27 -05001517 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1518 context->prio = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001519 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Al Viro0590b932008-12-14 23:45:27 -05001520 }
Eric Paris56179a62012-01-03 14:23:06 -05001521 if (state == AUDIT_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return;
1523
David Woodhousece625a82005-07-18 14:24:46 -04001524 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 context->ctime = CURRENT_TIME;
1526 context->in_syscall = 1;
Al Viro0590b932008-12-14 23:45:27 -05001527 context->current_state = state;
Alexander Viro419c58f2006-09-29 00:08:50 -04001528 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001531/**
1532 * audit_syscall_exit - deallocate audit context after a system call
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001533 * @success: success value of the syscall
1534 * @return_code: return value of the syscall
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001535 *
1536 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001538 * filtering, or because some other part of the kernel wrote an audit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001540 * free the names stored from getname().
1541 */
Eric Parisd7e75282012-01-03 14:23:06 -05001542void __audit_syscall_exit(int success, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Al Viro5411be52006-03-29 20:23:36 -05001544 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct audit_context *context;
1546
Eric Parisd7e75282012-01-03 14:23:06 -05001547 if (success)
1548 success = AUDITSC_SUCCESS;
1549 else
1550 success = AUDITSC_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Richard Guy Briggs4a3eb7262014-02-18 15:29:43 -05001552 context = audit_take_context(tsk, success, return_code);
Eric Paris56179a62012-01-03 14:23:06 -05001553 if (!context)
Al Viro97e94c42006-03-29 20:26:24 -05001554 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Al Viro0590b932008-12-14 23:45:27 -05001556 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001557 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 context->in_syscall = 0;
Al Viro0590b932008-12-14 23:45:27 -05001560 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
2fd6f582005-04-29 16:08:28 +01001561
Al Viro916d7572009-06-24 00:02:38 -04001562 if (!list_empty(&context->killed_trees))
1563 audit_kill_trees(&context->killed_trees);
1564
Al Viroc62d7732012-10-20 15:07:18 -04001565 audit_free_names(context);
1566 unroll_tree_refs(context, NULL, 0);
1567 audit_free_aux(context);
1568 context->aux = NULL;
1569 context->aux_pids = NULL;
1570 context->target_pid = 0;
1571 context->target_sid = 0;
1572 context->sockaddr_len = 0;
1573 context->type = 0;
1574 context->fds[0] = -1;
1575 if (context->state != AUDIT_RECORD_CONTEXT) {
1576 kfree(context->filterkey);
1577 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
Al Viroc62d7732012-10-20 15:07:18 -04001579 tsk->audit_context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Al Viro74c3cbe2007-07-22 08:04:18 -04001582static inline void handle_one(const struct inode *inode)
1583{
1584#ifdef CONFIG_AUDIT_TREE
1585 struct audit_context *context;
1586 struct audit_tree_refs *p;
1587 struct audit_chunk *chunk;
1588 int count;
Eric Parise61ce862009-12-17 21:24:24 -05001589 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
Al Viro74c3cbe2007-07-22 08:04:18 -04001590 return;
1591 context = current->audit_context;
1592 p = context->trees;
1593 count = context->tree_count;
1594 rcu_read_lock();
1595 chunk = audit_tree_lookup(inode);
1596 rcu_read_unlock();
1597 if (!chunk)
1598 return;
1599 if (likely(put_tree_ref(context, chunk)))
1600 return;
1601 if (unlikely(!grow_tree_refs(context))) {
Richard Guy Briggsf952d102014-01-27 17:38:42 -05001602 pr_warn("out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001603 audit_set_auditable(context);
1604 audit_put_chunk(chunk);
1605 unroll_tree_refs(context, p, count);
1606 return;
1607 }
1608 put_tree_ref(context, chunk);
1609#endif
1610}
1611
1612static void handle_path(const struct dentry *dentry)
1613{
1614#ifdef CONFIG_AUDIT_TREE
1615 struct audit_context *context;
1616 struct audit_tree_refs *p;
1617 const struct dentry *d, *parent;
1618 struct audit_chunk *drop;
1619 unsigned long seq;
1620 int count;
1621
1622 context = current->audit_context;
1623 p = context->trees;
1624 count = context->tree_count;
1625retry:
1626 drop = NULL;
1627 d = dentry;
1628 rcu_read_lock();
1629 seq = read_seqbegin(&rename_lock);
1630 for(;;) {
David Howells3b362152015-03-17 22:26:21 +00001631 struct inode *inode = d_backing_inode(d);
Eric Parise61ce862009-12-17 21:24:24 -05001632 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
Al Viro74c3cbe2007-07-22 08:04:18 -04001633 struct audit_chunk *chunk;
1634 chunk = audit_tree_lookup(inode);
1635 if (chunk) {
1636 if (unlikely(!put_tree_ref(context, chunk))) {
1637 drop = chunk;
1638 break;
1639 }
1640 }
1641 }
1642 parent = d->d_parent;
1643 if (parent == d)
1644 break;
1645 d = parent;
1646 }
1647 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1648 rcu_read_unlock();
1649 if (!drop) {
1650 /* just a race with rename */
1651 unroll_tree_refs(context, p, count);
1652 goto retry;
1653 }
1654 audit_put_chunk(drop);
1655 if (grow_tree_refs(context)) {
1656 /* OK, got more space */
1657 unroll_tree_refs(context, p, count);
1658 goto retry;
1659 }
1660 /* too bad */
Richard Guy Briggsf952d102014-01-27 17:38:42 -05001661 pr_warn("out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001662 unroll_tree_refs(context, p, count);
1663 audit_set_auditable(context);
1664 return;
1665 }
1666 rcu_read_unlock();
1667#endif
1668}
1669
Jeff Layton78e2e802012-10-10 15:25:22 -04001670static struct audit_names *audit_alloc_name(struct audit_context *context,
1671 unsigned char type)
Eric Paris5195d8e2012-01-03 14:23:05 -05001672{
1673 struct audit_names *aname;
1674
1675 if (context->name_count < AUDIT_NAMES) {
1676 aname = &context->preallocated_names[context->name_count];
1677 memset(aname, 0, sizeof(*aname));
1678 } else {
1679 aname = kzalloc(sizeof(*aname), GFP_NOFS);
1680 if (!aname)
1681 return NULL;
1682 aname->should_free = true;
1683 }
1684
Richard Guy Briggs84cb7772015-08-05 23:48:20 -04001685 aname->ino = AUDIT_INO_UNSET;
Jeff Layton78e2e802012-10-10 15:25:22 -04001686 aname->type = type;
Eric Paris5195d8e2012-01-03 14:23:05 -05001687 list_add_tail(&aname->list, &context->names_list);
1688
1689 context->name_count++;
Eric Paris5195d8e2012-01-03 14:23:05 -05001690 return aname;
1691}
1692
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001693/**
Jeff Layton7ac86262012-10-10 15:25:28 -04001694 * audit_reusename - fill out filename with info from existing entry
1695 * @uptr: userland ptr to pathname
1696 *
1697 * Search the audit_names list for the current audit context. If there is an
1698 * existing entry with a matching "uptr" then return the filename
1699 * associated with that audit_name. If not, return NULL.
1700 */
1701struct filename *
1702__audit_reusename(const __user char *uptr)
1703{
1704 struct audit_context *context = current->audit_context;
1705 struct audit_names *n;
1706
1707 list_for_each_entry(n, &context->names_list, list) {
1708 if (!n->name)
1709 continue;
Paul Moore55422d02015-01-22 00:00:23 -05001710 if (n->name->uptr == uptr) {
1711 n->name->refcnt++;
Jeff Layton7ac86262012-10-10 15:25:28 -04001712 return n->name;
Paul Moore55422d02015-01-22 00:00:23 -05001713 }
Jeff Layton7ac86262012-10-10 15:25:28 -04001714 }
1715 return NULL;
1716}
1717
1718/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001719 * audit_getname - add a name to the list
1720 * @name: name to add
1721 *
1722 * Add a name to the list of audit names for this context.
1723 * Called from fs/namei.c:getname().
1724 */
Jeff Layton91a27b22012-10-10 15:25:28 -04001725void __audit_getname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct audit_context *context = current->audit_context;
Eric Paris5195d8e2012-01-03 14:23:05 -05001728 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Paul Moore55422d02015-01-22 00:00:23 -05001730 if (!context->in_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return;
Jeff Layton91a27b22012-10-10 15:25:28 -04001732
Jeff Layton78e2e802012-10-10 15:25:22 -04001733 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
Eric Paris5195d8e2012-01-03 14:23:05 -05001734 if (!n)
1735 return;
1736
1737 n->name = name;
1738 n->name_len = AUDIT_NAME_FULL;
Jeff Laytonadb5c242012-10-10 16:43:13 -04001739 name->aname = n;
Paul Moore55422d02015-01-22 00:00:23 -05001740 name->refcnt++;
Eric Paris5195d8e2012-01-03 14:23:05 -05001741
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02001742 if (!context->pwd.dentry)
1743 get_fs_pwd(current->fs, &context->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001746/**
Jeff Laytonbfcec702012-10-10 15:25:23 -04001747 * __audit_inode - store the inode and device from a lookup
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001748 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001749 * @dentry: dentry being audited
Jeff Layton79f65302013-07-08 15:59:36 -07001750 * @flags: attributes for this particular entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001751 */
Jeff Laytonadb5c242012-10-10 16:43:13 -04001752void __audit_inode(struct filename *name, const struct dentry *dentry,
Jeff Layton79f65302013-07-08 15:59:36 -07001753 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 struct audit_context *context = current->audit_context;
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001756 struct inode *inode = d_backing_inode(dentry);
Eric Paris5195d8e2012-01-03 14:23:05 -05001757 struct audit_names *n;
Jeff Layton79f65302013-07-08 15:59:36 -07001758 bool parent = flags & AUDIT_INODE_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 if (!context->in_syscall)
1761 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05001762
Jeff Layton9cec9d62012-10-10 15:25:21 -04001763 if (!name)
1764 goto out_alloc;
1765
Jeff Laytonadb5c242012-10-10 16:43:13 -04001766 /*
1767 * If we have a pointer to an audit_names entry already, then we can
1768 * just use it directly if the type is correct.
1769 */
1770 n = name->aname;
1771 if (n) {
1772 if (parent) {
1773 if (n->type == AUDIT_TYPE_PARENT ||
1774 n->type == AUDIT_TYPE_UNKNOWN)
1775 goto out;
1776 } else {
1777 if (n->type != AUDIT_TYPE_PARENT)
1778 goto out;
1779 }
1780 }
1781
Eric Paris5195d8e2012-01-03 14:23:05 -05001782 list_for_each_entry_reverse(n, &context->names_list, list) {
Paul Moore57c59f52015-01-22 00:00:16 -05001783 if (n->ino) {
1784 /* valid inode number, use that for the comparison */
1785 if (n->ino != inode->i_ino ||
1786 n->dev != inode->i_sb->s_dev)
1787 continue;
1788 } else if (n->name) {
1789 /* inode number has not been set, check the name */
1790 if (strcmp(n->name->name, name->name))
1791 continue;
1792 } else
1793 /* no inode and no name (?!) ... this is odd ... */
Jeff Laytonbfcec702012-10-10 15:25:23 -04001794 continue;
1795
1796 /* match the correct record type */
1797 if (parent) {
1798 if (n->type == AUDIT_TYPE_PARENT ||
1799 n->type == AUDIT_TYPE_UNKNOWN)
1800 goto out;
1801 } else {
1802 if (n->type != AUDIT_TYPE_PARENT)
1803 goto out;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Eric Paris5195d8e2012-01-03 14:23:05 -05001806
Jeff Layton9cec9d62012-10-10 15:25:21 -04001807out_alloc:
Paul Moore4a928432014-12-22 12:27:39 -05001808 /* unable to find an entry with both a matching name and type */
1809 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
Eric Paris5195d8e2012-01-03 14:23:05 -05001810 if (!n)
1811 return;
Paul Moorefcf22d82014-12-30 09:26:21 -05001812 if (name) {
Paul Moorefd3522f2015-01-22 00:00:10 -05001813 n->name = name;
Paul Moore55422d02015-01-22 00:00:23 -05001814 name->refcnt++;
Paul Moorefcf22d82014-12-30 09:26:21 -05001815 }
Jeff Laytonbfcec702012-10-10 15:25:23 -04001816
Eric Paris5195d8e2012-01-03 14:23:05 -05001817out:
Jeff Laytonbfcec702012-10-10 15:25:23 -04001818 if (parent) {
Jeff Layton91a27b22012-10-10 15:25:28 -04001819 n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
Jeff Laytonbfcec702012-10-10 15:25:23 -04001820 n->type = AUDIT_TYPE_PARENT;
Jeff Layton79f65302013-07-08 15:59:36 -07001821 if (flags & AUDIT_INODE_HIDDEN)
1822 n->hidden = true;
Jeff Laytonbfcec702012-10-10 15:25:23 -04001823 } else {
1824 n->name_len = AUDIT_NAME_FULL;
1825 n->type = AUDIT_TYPE_NORMAL;
1826 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001827 handle_path(dentry);
Eric Paris5195d8e2012-01-03 14:23:05 -05001828 audit_copy_inode(n, dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
Al Viro9f45f5b2014-10-31 17:44:57 -04001831void __audit_file(const struct file *file)
1832{
1833 __audit_inode(NULL, file->f_path.dentry, 0);
1834}
1835
Amy Griffis73241cc2005-11-03 16:00:25 +00001836/**
Jeff Laytonc43a25a2012-10-10 15:25:21 -04001837 * __audit_inode_child - collect inode info for created/removed objects
Amy Griffis73d3ec52006-07-13 13:16:39 -04001838 * @parent: inode of dentry parent
Jeff Laytonc43a25a2012-10-10 15:25:21 -04001839 * @dentry: dentry being audited
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001840 * @type: AUDIT_TYPE_* value that we're looking for
Amy Griffis73241cc2005-11-03 16:00:25 +00001841 *
1842 * For syscalls that create or remove filesystem objects, audit_inode
1843 * can only collect information for the filesystem object's parent.
1844 * This call updates the audit context with the child's information.
1845 * Syscalls that create a new filesystem object must be hooked after
1846 * the object is created. Syscalls that remove a filesystem object
1847 * must be hooked prior, in order to capture the target inode during
1848 * unsuccessful attempts.
1849 */
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001850void __audit_inode_child(struct inode *parent,
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001851 const struct dentry *dentry,
1852 const unsigned char type)
Amy Griffis73241cc2005-11-03 16:00:25 +00001853{
Amy Griffis73241cc2005-11-03 16:00:25 +00001854 struct audit_context *context = current->audit_context;
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05001855 struct inode *inode = d_backing_inode(dentry);
Al Virocccc6bb2009-12-25 05:07:33 -05001856 const char *dname = dentry->d_name.name;
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001857 struct audit_names *n, *found_parent = NULL, *found_child = NULL;
Amy Griffis73241cc2005-11-03 16:00:25 +00001858
1859 if (!context->in_syscall)
1860 return;
1861
Al Viro74c3cbe2007-07-22 08:04:18 -04001862 if (inode)
1863 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001864
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001865 /* look for a parent entry first */
Eric Paris5195d8e2012-01-03 14:23:05 -05001866 list_for_each_entry(n, &context->names_list, list) {
Paul Moore57c59f52015-01-22 00:00:16 -05001867 if (!n->name ||
1868 (n->type != AUDIT_TYPE_PARENT &&
1869 n->type != AUDIT_TYPE_UNKNOWN))
Amy Griffis5712e882007-02-13 14:15:22 -05001870 continue;
1871
Paul Moore57c59f52015-01-22 00:00:16 -05001872 if (n->ino == parent->i_ino && n->dev == parent->i_sb->s_dev &&
1873 !audit_compare_dname_path(dname,
1874 n->name->name, n->name_len)) {
1875 if (n->type == AUDIT_TYPE_UNKNOWN)
1876 n->type = AUDIT_TYPE_PARENT;
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001877 found_parent = n;
1878 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001879 }
Steve Grubbac9910c2006-09-28 14:31:32 -04001880 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001881
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001882 /* is there a matching child entry? */
Eric Paris5195d8e2012-01-03 14:23:05 -05001883 list_for_each_entry(n, &context->names_list, list) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001884 /* can only match entries that have a name */
Paul Moore57c59f52015-01-22 00:00:16 -05001885 if (!n->name ||
1886 (n->type != type && n->type != AUDIT_TYPE_UNKNOWN))
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001887 continue;
1888
Jeff Layton91a27b22012-10-10 15:25:28 -04001889 if (!strcmp(dname, n->name->name) ||
1890 !audit_compare_dname_path(dname, n->name->name,
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001891 found_parent ?
1892 found_parent->name_len :
Jeff Laytone3d6b072012-10-10 15:25:25 -04001893 AUDIT_NAME_FULL)) {
Paul Moore57c59f52015-01-22 00:00:16 -05001894 if (n->type == AUDIT_TYPE_UNKNOWN)
1895 n->type = type;
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001896 found_child = n;
1897 break;
Steve Grubbac9910c2006-09-28 14:31:32 -04001898 }
Amy Griffis5712e882007-02-13 14:15:22 -05001899 }
1900
Amy Griffis5712e882007-02-13 14:15:22 -05001901 if (!found_parent) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001902 /* create a new, "anonymous" parent record */
1903 n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
Eric Paris5195d8e2012-01-03 14:23:05 -05001904 if (!n)
Amy Griffis5712e882007-02-13 14:15:22 -05001905 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05001906 audit_copy_inode(n, NULL, parent);
Amy Griffis73d3ec52006-07-13 13:16:39 -04001907 }
Amy Griffis5712e882007-02-13 14:15:22 -05001908
1909 if (!found_child) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001910 found_child = audit_alloc_name(context, type);
1911 if (!found_child)
Amy Griffis5712e882007-02-13 14:15:22 -05001912 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001913
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) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001918 found_child->name = found_parent->name;
1919 found_child->name_len = AUDIT_NAME_FULL;
Paul Moore55422d02015-01-22 00:00:23 -05001920 found_child->name->refcnt++;
Amy Griffis5712e882007-02-13 14:15:22 -05001921 }
Amy Griffis5712e882007-02-13 14:15:22 -05001922 }
Paul Moore57c59f52015-01-22 00:00:16 -05001923
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001924 if (inode)
1925 audit_copy_inode(found_child, dentry, inode);
1926 else
Richard Guy Briggs84cb7772015-08-05 23:48:20 -04001927 found_child->ino = AUDIT_INO_UNSET;
Amy Griffis3e2efce2006-07-13 13:16:02 -04001928}
Trond Myklebust50e437d2007-06-07 22:44:34 -04001929EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04001930
1931/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001932 * 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 */
Al Viro48887e62008-12-06 01:05:50 -05001939int auditsc_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01001940 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Al Viro48887e62008-12-06 01:05:50 -05001942 if (!ctx->in_syscall)
1943 return 0;
David Woodhousece625a82005-07-18 14:24:46 -04001944 if (!ctx->serial)
1945 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001946 t->tv_sec = ctx->ctime.tv_sec;
1947 t->tv_nsec = ctx->ctime.tv_nsec;
1948 *serial = ctx->serial;
Al Viro0590b932008-12-14 23:45:27 -05001949 if (!ctx->prio) {
1950 ctx->prio = 1;
1951 ctx->current_state = AUDIT_RECORD_CONTEXT;
1952 }
Al Viro48887e62008-12-06 01:05:50 -05001953 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Eric Paris4746ec52008-01-08 10:06:53 -05001956/* global counter which is incremented every time something logs in */
1957static atomic_t session_id = ATOMIC_INIT(0);
1958
Eric Parisda0a6102013-05-24 08:58:31 -04001959static int audit_set_loginuid_perm(kuid_t loginuid)
1960{
Eric Parisda0a6102013-05-24 08:58:31 -04001961 /* if we are unset, we don't need privs */
1962 if (!audit_loginuid_set(current))
1963 return 0;
Eric Paris21b85c32013-05-23 14:26:00 -04001964 /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/
1965 if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE))
1966 return -EPERM;
Eric Paris83fa6bb2013-05-24 09:39:29 -04001967 /* it is set, you need permission */
1968 if (!capable(CAP_AUDIT_CONTROL))
1969 return -EPERM;
Eric Parisd040e5a2013-05-24 09:18:04 -04001970 /* 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;
Eric Paris83fa6bb2013-05-24 09:39:29 -04001973 return 0;
Eric Parisda0a6102013-05-24 08:58:31 -04001974}
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;
Richard Guy Briggs5ee9a752013-12-11 15:28:09 -05001981 uid_t uid, oldloginuid, loginuid;
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04001982 struct tty_struct *tty;
Eric Parisda0a6102013-05-24 08:58:31 -04001983
Gao fengc2412d92013-11-01 19:34:45 +08001984 if (!audit_enabled)
1985 return;
1986
Richard Guy Briggs76a658c22016-06-28 12:06:58 -04001987 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1988 if (!ab)
1989 return;
1990
Eric Parisda0a6102013-05-24 08:58:31 -04001991 uid = from_kuid(&init_user_ns, task_uid(current));
Richard Guy Briggs5ee9a752013-12-11 15:28:09 -05001992 oldloginuid = from_kuid(&init_user_ns, koldloginuid);
1993 loginuid = from_kuid(&init_user_ns, kloginuid),
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04001994 tty = audit_get_tty(current);
Eric Parisda0a6102013-05-24 08:58:31 -04001995
Eric Parisddfad8a2011-01-19 19:22:35 -05001996 audit_log_format(ab, "pid=%d uid=%u", task_pid_nr(current), uid);
1997 audit_log_task_context(ab);
Richard Guy Briggsdb0a6fb2016-04-21 14:14:01 -04001998 audit_log_format(ab, " old-auid=%u auid=%u tty=%s old-ses=%u ses=%u res=%d",
1999 oldloginuid, loginuid, tty ? tty_name(tty) : "(none)",
2000 oldsessionid, sessionid, !rc);
2001 audit_put_tty(tty);
Eric Parisda0a6102013-05-24 08:58:31 -04002002 audit_log_end(ab);
2003}
2004
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002005/**
Eric Paris0a300be2012-01-03 14:23:08 -05002006 * audit_set_loginuid - set current task's audit_context loginuid
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002007 * @loginuid: loginuid value
2008 *
2009 * Returns 0.
2010 *
2011 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2012 */
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002013int audit_set_loginuid(kuid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Eric Paris0a300be2012-01-03 14:23:08 -05002015 struct task_struct *task = current;
Eric Paris9175c9d2013-11-06 10:47:17 -05002016 unsigned int oldsessionid, sessionid = (unsigned int)-1;
2017 kuid_t oldloginuid;
Eric Parisda0a6102013-05-24 08:58:31 -04002018 int rc;
Steve Grubbc0404992005-05-13 18:17:42 +01002019
Eric Parisda0a6102013-05-24 08:58:31 -04002020 oldloginuid = audit_get_loginuid(current);
2021 oldsessionid = audit_get_sessionid(current);
2022
2023 rc = audit_set_loginuid_perm(loginuid);
2024 if (rc)
2025 goto out;
Eric Paris633b4542012-01-03 14:23:08 -05002026
Eric Paris81407c82013-05-24 09:49:14 -04002027 /* are we setting or clearing? */
2028 if (uid_valid(loginuid))
Eric Paris4440e852013-11-27 17:35:17 -05002029 sessionid = (unsigned int)atomic_inc_return(&session_id);
Steve Grubb41757102006-06-12 07:48:28 -04002030
Eric Paris4746ec52008-01-08 10:06:53 -05002031 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05002032 task->loginuid = loginuid;
Eric Parisda0a6102013-05-24 08:58:31 -04002033out:
2034 audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
2035 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002038/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002039 * __audit_mq_open - record audit data for a POSIX MQ open
2040 * @oflag: open flag
2041 * @mode: mode bits
Randy Dunlap6b962552009-01-05 13:41:13 -08002042 * @attr: queue attributes
George C. Wilson20ca73b2006-05-24 16:09:55 -05002043 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002044 */
Al Virodf0a4282011-07-26 05:26:10 -04002045void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002046{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002047 struct audit_context *context = current->audit_context;
2048
Al Viro564f6992008-12-14 04:02:26 -05002049 if (attr)
2050 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2051 else
2052 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002053
Al Viro564f6992008-12-14 04:02:26 -05002054 context->mq_open.oflag = oflag;
2055 context->mq_open.mode = mode;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002056
Al Viro564f6992008-12-14 04:02:26 -05002057 context->type = AUDIT_MQ_OPEN;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002058}
2059
2060/**
Al Viroc32c8af2008-12-14 03:46:48 -05002061 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
George C. Wilson20ca73b2006-05-24 16:09:55 -05002062 * @mqdes: MQ descriptor
2063 * @msg_len: Message length
2064 * @msg_prio: Message priority
Al Viroc32c8af2008-12-14 03:46:48 -05002065 * @abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002066 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002067 */
Al Viroc32c8af2008-12-14 03:46:48 -05002068void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2069 const struct timespec *abs_timeout)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002070{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002071 struct audit_context *context = current->audit_context;
Al Viroc32c8af2008-12-14 03:46:48 -05002072 struct timespec *p = &context->mq_sendrecv.abs_timeout;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002073
Al Viroc32c8af2008-12-14 03:46:48 -05002074 if (abs_timeout)
2075 memcpy(p, abs_timeout, sizeof(struct timespec));
2076 else
2077 memset(p, 0, sizeof(struct timespec));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002078
Al Viroc32c8af2008-12-14 03:46:48 -05002079 context->mq_sendrecv.mqdes = mqdes;
2080 context->mq_sendrecv.msg_len = msg_len;
2081 context->mq_sendrecv.msg_prio = msg_prio;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002082
Al Viroc32c8af2008-12-14 03:46:48 -05002083 context->type = AUDIT_MQ_SENDRECV;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002084}
2085
2086/**
2087 * __audit_mq_notify - record audit data for a POSIX MQ notify
2088 * @mqdes: MQ descriptor
Randy Dunlap6b962552009-01-05 13:41:13 -08002089 * @notification: Notification event
George C. Wilson20ca73b2006-05-24 16:09:55 -05002090 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002091 */
2092
Al Viro20114f72008-12-10 07:16:12 -05002093void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002094{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002095 struct audit_context *context = current->audit_context;
2096
Al Viro20114f72008-12-10 07:16:12 -05002097 if (notification)
2098 context->mq_notify.sigev_signo = notification->sigev_signo;
2099 else
2100 context->mq_notify.sigev_signo = 0;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002101
Al Viro20114f72008-12-10 07:16:12 -05002102 context->mq_notify.mqdes = mqdes;
2103 context->type = AUDIT_MQ_NOTIFY;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002104}
2105
2106/**
2107 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2108 * @mqdes: MQ descriptor
2109 * @mqstat: MQ flags
2110 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002111 */
Al Viro73929062008-12-10 06:58:59 -05002112void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002113{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002114 struct audit_context *context = current->audit_context;
Al Viro73929062008-12-10 06:58:59 -05002115 context->mq_getsetattr.mqdes = mqdes;
2116 context->mq_getsetattr.mqstat = *mqstat;
2117 context->type = AUDIT_MQ_GETSETATTR;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002118}
2119
2120/**
Steve Grubb073115d2006-04-02 17:07:33 -04002121 * audit_ipc_obj - record audit data for ipc object
2122 * @ipcp: ipc permissions
2123 *
Steve Grubb073115d2006-04-02 17:07:33 -04002124 */
Al Viroa33e6752008-12-10 03:40:06 -05002125void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002126{
Steve Grubb073115d2006-04-02 17:07:33 -04002127 struct audit_context *context = current->audit_context;
Al Viroa33e6752008-12-10 03:40:06 -05002128 context->ipc.uid = ipcp->uid;
2129 context->ipc.gid = ipcp->gid;
2130 context->ipc.mode = ipcp->mode;
Al Viroe816f372008-12-10 03:47:15 -05002131 context->ipc.has_perm = 0;
Al Viroa33e6752008-12-10 03:40:06 -05002132 security_ipc_getsecid(ipcp, &context->ipc.osid);
2133 context->type = AUDIT_IPC;
Steve Grubb073115d2006-04-02 17:07:33 -04002134}
2135
2136/**
2137 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002138 * @qbytes: msgq bytes
2139 * @uid: msgq user id
2140 * @gid: msgq group id
2141 * @mode: msgq mode (permissions)
2142 *
Al Viroe816f372008-12-10 03:47:15 -05002143 * Called only after audit_ipc_obj().
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002144 */
Al Viro2570ebb2011-07-27 14:03:22 -04002145void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 struct audit_context *context = current->audit_context;
2148
Al Viroe816f372008-12-10 03:47:15 -05002149 context->ipc.qbytes = qbytes;
2150 context->ipc.perm_uid = uid;
2151 context->ipc.perm_gid = gid;
2152 context->ipc.perm_mode = mode;
2153 context->ipc.has_perm = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002155
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04002156void __audit_bprm(struct linux_binprm *bprm)
Al Viro473ae302006-04-26 14:04:08 -04002157{
Al Viro473ae302006-04-26 14:04:08 -04002158 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002159
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04002160 context->type = AUDIT_EXECVE;
2161 context->execve.argc = bprm->argc;
Al Viro473ae302006-04-26 14:04:08 -04002162}
2163
2164
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002165/**
2166 * audit_socketcall - record audit data for sys_socketcall
Chen Gang2950fa92013-04-07 16:55:23 +08002167 * @nargs: number of args, which should not be more than AUDITSC_ARGS.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002168 * @args: args array
2169 *
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002170 */
Chen Gang2950fa92013-04-07 16:55:23 +08002171int __audit_socketcall(int nargs, unsigned long *args)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002172{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002173 struct audit_context *context = current->audit_context;
2174
Chen Gang2950fa92013-04-07 16:55:23 +08002175 if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
2176 return -EINVAL;
Al Virof3298dc2008-12-10 03:16:51 -05002177 context->type = AUDIT_SOCKETCALL;
2178 context->socketcall.nargs = nargs;
2179 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
Chen Gang2950fa92013-04-07 16:55:23 +08002180 return 0;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002181}
2182
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002183/**
Al Virodb349502007-02-07 01:48:00 -05002184 * __audit_fd_pair - record audit data for pipe and socketpair
2185 * @fd1: the first file descriptor
2186 * @fd2: the second file descriptor
2187 *
Al Virodb349502007-02-07 01:48:00 -05002188 */
Al Viro157cf642008-12-14 04:57:47 -05002189void __audit_fd_pair(int fd1, int fd2)
Al Virodb349502007-02-07 01:48:00 -05002190{
2191 struct audit_context *context = current->audit_context;
Al Viro157cf642008-12-14 04:57:47 -05002192 context->fds[0] = fd1;
2193 context->fds[1] = fd2;
Al Virodb349502007-02-07 01:48:00 -05002194}
2195
2196/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002197 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2198 * @len: data length in user space
2199 * @a: data address in kernel space
2200 *
2201 * Returns 0 for success or NULL context or < 0 on error.
2202 */
Eric Paris07c49412012-01-03 14:23:07 -05002203int __audit_sockaddr(int len, void *a)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002204{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002205 struct audit_context *context = current->audit_context;
2206
Al Viro4f6b4342008-12-09 19:50:34 -05002207 if (!context->sockaddr) {
2208 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2209 if (!p)
2210 return -ENOMEM;
2211 context->sockaddr = p;
2212 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002213
Al Viro4f6b4342008-12-09 19:50:34 -05002214 context->sockaddr_len = len;
2215 memcpy(context->sockaddr, a, len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002216 return 0;
2217}
2218
Al Viroa5cb0132007-03-20 13:58:35 -04002219void __audit_ptrace(struct task_struct *t)
2220{
2221 struct audit_context *context = current->audit_context;
2222
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002223 context->target_pid = task_pid_nr(t);
Eric Parisc2a77802008-01-07 13:40:17 -05002224 context->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002225 context->target_uid = task_uid(t);
Eric Paris4746ec52008-01-08 10:06:53 -05002226 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002227 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002228 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002229}
2230
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002231/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002232 * audit_signal_info - record signal info for shutting down audit subsystem
2233 * @sig: signal value
2234 * @t: task being signaled
2235 *
2236 * If the audit subsystem is being terminated, record the task (pid)
2237 * and uid that is doing that.
2238 */
Amy Griffise54dc242007-03-29 18:01:04 -04002239int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002240{
Amy Griffise54dc242007-03-29 18:01:04 -04002241 struct audit_aux_data_pids *axp;
2242 struct task_struct *tsk = current;
2243 struct audit_context *ctx = tsk->audit_context;
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002244 kuid_t uid = current_uid(), t_uid = task_uid(t);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002245
Al Viro175fc482007-08-08 00:01:46 +01002246 if (audit_pid && t->tgid == audit_pid) {
Eric Parisee1d3152008-07-07 10:49:45 -04002247 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002248 audit_sig_pid = task_pid_nr(tsk);
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002249 if (uid_valid(tsk->loginuid))
Al Virobfef93a2008-01-10 04:53:18 -05002250 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002251 else
David Howellsc69e8d92008-11-14 10:39:19 +11002252 audit_sig_uid = uid;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002253 security_task_getsecid(tsk, &audit_sig_sid);
Al Viro175fc482007-08-08 00:01:46 +01002254 }
2255 if (!audit_signals || audit_dummy_context())
2256 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002257 }
Amy Griffise54dc242007-03-29 18:01:04 -04002258
Amy Griffise54dc242007-03-29 18:01:04 -04002259 /* optimize the common case by putting first signal recipient directly
2260 * in audit_context */
2261 if (!ctx->target_pid) {
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002262 ctx->target_pid = task_tgid_nr(t);
Eric Parisc2a77802008-01-07 13:40:17 -05002263 ctx->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002264 ctx->target_uid = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002265 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002266 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002267 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002268 return 0;
2269 }
2270
2271 axp = (void *)ctx->aux_pids;
2272 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2273 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2274 if (!axp)
2275 return -ENOMEM;
2276
2277 axp->d.type = AUDIT_OBJ_PID;
2278 axp->d.next = ctx->aux_pids;
2279 ctx->aux_pids = (void *)axp;
2280 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002281 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002282
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002283 axp->target_pid[axp->pid_count] = task_tgid_nr(t);
Eric Parisc2a77802008-01-07 13:40:17 -05002284 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002285 axp->target_uid[axp->pid_count] = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002286 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002287 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002288 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002289 axp->pid_count++;
2290
2291 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002292}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002293
2294/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002295 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
David Howellsd84f4f92008-11-14 10:39:23 +11002296 * @bprm: pointer to the bprm being processed
2297 * @new: the proposed new credentials
2298 * @old: the old credentials
Eric Paris3fc689e2008-11-11 21:48:18 +11002299 *
2300 * Simply check if the proc already has the caps given by the file and if not
2301 * store the priv escalation info for later auditing at the end of the syscall
2302 *
Eric Paris3fc689e2008-11-11 21:48:18 +11002303 * -Eric
2304 */
David Howellsd84f4f92008-11-14 10:39:23 +11002305int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2306 const struct cred *new, const struct cred *old)
Eric Paris3fc689e2008-11-11 21:48:18 +11002307{
2308 struct audit_aux_data_bprm_fcaps *ax;
2309 struct audit_context *context = current->audit_context;
2310 struct cpu_vfs_cap_data vcaps;
Eric Paris3fc689e2008-11-11 21:48:18 +11002311
2312 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2313 if (!ax)
David Howellsd84f4f92008-11-14 10:39:23 +11002314 return -ENOMEM;
Eric Paris3fc689e2008-11-11 21:48:18 +11002315
2316 ax->d.type = AUDIT_BPRM_FCAPS;
2317 ax->d.next = context->aux;
2318 context->aux = (void *)ax;
2319
Al Virof4a4a8b2014-12-28 09:27:07 -05002320 get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
Eric Paris3fc689e2008-11-11 21:48:18 +11002321
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
David Howellsd84f4f92008-11-14 10:39:23 +11002327 ax->old_pcap.permitted = old->cap_permitted;
2328 ax->old_pcap.inheritable = old->cap_inheritable;
2329 ax->old_pcap.effective = old->cap_effective;
Eric Paris3fc689e2008-11-11 21:48:18 +11002330
David Howellsd84f4f92008-11-14 10:39:23 +11002331 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;
Eric Paris3fc689e2008-11-11 21:48:18 +11002335}
2336
2337/**
Eric Parise68b75a02008-11-11 21:48:22 +11002338 * __audit_log_capset - store information about the arguments to the capset syscall
David Howellsd84f4f92008-11-14 10:39:23 +11002339 * @new: the new credentials
2340 * @old: the old (current) credentials
Eric Parise68b75a02008-11-11 21:48:22 +11002341 *
Masanari Iidada3dae52014-09-09 01:27:23 +09002342 * Record the arguments userspace sent to sys_capset for later printing by the
Eric Parise68b75a02008-11-11 21:48:22 +11002343 * audit system if applicable
2344 */
Eric W. Biedermanca24a232013-03-19 00:02:25 -07002345void __audit_log_capset(const struct cred *new, const struct cred *old)
Eric Parise68b75a02008-11-11 21:48:22 +11002346{
Eric Parise68b75a02008-11-11 21:48:22 +11002347 struct audit_context *context = current->audit_context;
Eric W. Biedermanca24a232013-03-19 00:02:25 -07002348 context->capset.pid = task_pid_nr(current);
Al Viro57f71a02009-01-04 14:52:57 -05002349 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;
Eric Parise68b75a02008-11-11 21:48:22 +11002353}
2354
Al Viro120a7952010-10-30 02:54:44 -04002355void __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
Kees Cook7b9205b2013-01-11 14:32:05 -08002363static void audit_log_task(struct audit_buffer *ab)
Eric Paris85e7bac32012-01-03 14:23:05 -05002364{
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002365 kuid_t auid, uid;
2366 kgid_t gid;
Eric Paris85e7bac32012-01-03 14:23:05 -05002367 unsigned int sessionid;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002368 char comm[sizeof(current->comm)];
Eric Paris85e7bac32012-01-03 14:23:05 -05002369
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",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002375 from_kuid(&init_user_ns, auid),
2376 from_kuid(&init_user_ns, uid),
2377 from_kgid(&init_user_ns, gid),
2378 sessionid);
Eric Paris85e7bac32012-01-03 14:23:05 -05002379 audit_log_task_context(ab);
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -05002380 audit_log_format(ab, " pid=%d comm=", task_pid_nr(current));
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002381 audit_log_untrustedstring(ab, get_task_comm(comm, current));
Davidlohr Bueso4766b192015-02-22 18:20:00 -08002382 audit_log_d_path_exe(ab, current->mm);
Kees Cook7b9205b2013-01-11 14:32:05 -08002383}
2384
Eric Parise68b75a02008-11-11 21:48:22 +11002385/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002386 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002387 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002388 *
2389 * If a process ends with a core dump, something fishy is going on and we
2390 * should record the event for investigation.
2391 */
2392void audit_core_dumps(long signr)
2393{
2394 struct audit_buffer *ab;
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002395
2396 if (!audit_enabled)
2397 return;
2398
2399 if (signr == SIGQUIT) /* don't care for those */
2400 return;
2401
2402 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
Kees Cook0644ec02013-01-11 14:32:07 -08002403 if (unlikely(!ab))
2404 return;
Paul Davies C61c0ee82013-11-08 09:57:39 +05302405 audit_log_task(ab);
2406 audit_log_format(ab, " sig=%ld", signr);
Eric Paris85e7bac32012-01-03 14:23:05 -05002407 audit_log_end(ab);
2408}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002409
Kees Cook3dc1c1b2012-04-12 16:47:58 -05002410void __audit_seccomp(unsigned long syscall, long signr, int code)
Eric Paris85e7bac32012-01-03 14:23:05 -05002411{
2412 struct audit_buffer *ab;
2413
Kees Cook7b9205b2013-01-11 14:32:05 -08002414 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP);
2415 if (unlikely(!ab))
2416 return;
2417 audit_log_task(ab);
Richard Guy Briggs84db5642014-01-29 16:17:58 -05002418 audit_log_format(ab, " sig=%ld arch=%x syscall=%ld compat=%d ip=0x%lx code=0x%x",
Andy Lutomirskiefbc0fb2016-03-22 14:24:58 -07002419 signr, syscall_get_arch(), syscall,
2420 in_compat_syscall(), KSTK_EIP(current), code);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002421 audit_log_end(ab);
2422}
Al Viro916d7572009-06-24 00:02:38 -04002423
2424struct list_head *audit_killed_trees(void)
2425{
2426 struct audit_context *ctx = current->audit_context;
2427 if (likely(!ctx || !ctx->in_syscall))
2428 return NULL;
2429 return &ctx->killed_trees;
2430}