blob: 2cd5256dbff72dfbb4716563ff28d2f9e38a018e [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>
Paul Moore437614732016-07-19 17:42:57 -040075#include <linux/uaccess.h>
Paul Moorefcf22d82014-12-30 09:26:21 -050076#include <uapi/linux/limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
David Woodhousefe7752b2005-12-15 18:33:52 +000078#include "audit.h"
79
Eric Parisd7e75282012-01-03 14:23:06 -050080/* flags stating the success for a syscall */
81#define AUDITSC_INVALID 0
82#define AUDITSC_SUCCESS 1
83#define AUDITSC_FAILURE 2
84
Paul Moore437614732016-07-19 17:42:57 -040085/* no execve audit message should be longer than this (userspace limits),
86 * see the note near the top of audit_log_execve_info() about this value */
Eric Parisde6bbd12008-01-07 14:31:58 -050087#define MAX_EXECVE_AUDIT_LEN 7500
88
William Roberts3f1c8252014-02-11 10:12:01 -080089/* max length to print of cmdline/proctitle value during audit */
90#define MAX_PROCTITLE_AUDIT_LEN 128
91
Al Viro471a5c72006-07-10 08:29:24 -040092/* number of audit rules */
93int audit_n_rules;
94
Amy Griffise54dc242007-03-29 18:01:04 -040095/* determines whether we collect data for signals sent */
96int audit_signals;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct audit_aux_data {
99 struct audit_aux_data *next;
100 int type;
101};
102
103#define AUDIT_AUX_IPCPERM 0
104
Amy Griffise54dc242007-03-29 18:01:04 -0400105/* Number of target pids per aux struct. */
106#define AUDIT_AUX_PIDS 16
107
Amy Griffise54dc242007-03-29 18:01:04 -0400108struct audit_aux_data_pids {
109 struct audit_aux_data d;
110 pid_t target_pid[AUDIT_AUX_PIDS];
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700111 kuid_t target_auid[AUDIT_AUX_PIDS];
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800112 kuid_t target_uid[AUDIT_AUX_PIDS];
Eric Paris4746ec52008-01-08 10:06:53 -0500113 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400114 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500115 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400116 int pid_count;
117};
118
Eric Paris3fc689e2008-11-11 21:48:18 +1100119struct audit_aux_data_bprm_fcaps {
120 struct audit_aux_data d;
121 struct audit_cap_data fcap;
122 unsigned int fcap_ver;
123 struct audit_cap_data old_pcap;
124 struct audit_cap_data new_pcap;
125};
126
Al Viro74c3cbe2007-07-22 08:04:18 -0400127struct audit_tree_refs {
128 struct audit_tree_refs *next;
129 struct audit_chunk *c[31];
130};
131
Al Viro55669bf2006-08-31 19:26:40 -0400132static int audit_match_perm(struct audit_context *ctx, int mask)
133{
Cordeliac4bacef2008-08-18 09:45:51 -0700134 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800135 if (unlikely(!ctx))
136 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700137 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100138
Al Viro55669bf2006-08-31 19:26:40 -0400139 switch (audit_classify_syscall(ctx->arch, n)) {
140 case 0: /* native */
141 if ((mask & AUDIT_PERM_WRITE) &&
142 audit_match_class(AUDIT_CLASS_WRITE, n))
143 return 1;
144 if ((mask & AUDIT_PERM_READ) &&
145 audit_match_class(AUDIT_CLASS_READ, n))
146 return 1;
147 if ((mask & AUDIT_PERM_ATTR) &&
148 audit_match_class(AUDIT_CLASS_CHATTR, n))
149 return 1;
150 return 0;
151 case 1: /* 32bit on biarch */
152 if ((mask & AUDIT_PERM_WRITE) &&
153 audit_match_class(AUDIT_CLASS_WRITE_32, n))
154 return 1;
155 if ((mask & AUDIT_PERM_READ) &&
156 audit_match_class(AUDIT_CLASS_READ_32, n))
157 return 1;
158 if ((mask & AUDIT_PERM_ATTR) &&
159 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
160 return 1;
161 return 0;
162 case 2: /* open */
163 return mask & ACC_MODE(ctx->argv[1]);
164 case 3: /* openat */
165 return mask & ACC_MODE(ctx->argv[2]);
166 case 4: /* socketcall */
167 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
168 case 5: /* execve */
169 return mask & AUDIT_PERM_EXEC;
170 default:
171 return 0;
172 }
173}
174
Eric Paris5ef30ee2012-01-03 14:23:05 -0500175static int audit_match_filetype(struct audit_context *ctx, int val)
Al Viro8b67dca2008-04-28 04:15:49 -0400176{
Eric Paris5195d8e2012-01-03 14:23:05 -0500177 struct audit_names *n;
Eric Paris5ef30ee2012-01-03 14:23:05 -0500178 umode_t mode = (umode_t)val;
zhangxiliang1a61c882008-08-02 10:56:37 +0800179
180 if (unlikely(!ctx))
181 return 0;
182
Eric Paris5195d8e2012-01-03 14:23:05 -0500183 list_for_each_entry(n, &ctx->names_list, list) {
Richard Guy Briggs84cb7772015-08-05 23:48:20 -0400184 if ((n->ino != AUDIT_INO_UNSET) &&
Eric Paris5195d8e2012-01-03 14:23:05 -0500185 ((n->mode & S_IFMT) == mode))
Eric Paris5ef30ee2012-01-03 14:23:05 -0500186 return 1;
187 }
Eric Paris5195d8e2012-01-03 14:23:05 -0500188
Eric Paris5ef30ee2012-01-03 14:23:05 -0500189 return 0;
Al Viro8b67dca2008-04-28 04:15:49 -0400190}
191
Al Viro74c3cbe2007-07-22 08:04:18 -0400192/*
193 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
194 * ->first_trees points to its beginning, ->trees - to the current end of data.
195 * ->tree_count is the number of free entries in array pointed to by ->trees.
196 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
197 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
198 * it's going to remain 1-element for almost any setup) until we free context itself.
199 * References in it _are_ dropped - at the same time we free/drop aux stuff.
200 */
201
202#ifdef CONFIG_AUDIT_TREE
Eric Paris679173b2009-01-26 18:09:45 -0500203static void audit_set_auditable(struct audit_context *ctx)
204{
205 if (!ctx->prio) {
206 ctx->prio = 1;
207 ctx->current_state = AUDIT_RECORD_CONTEXT;
208 }
209}
210
Al Viro74c3cbe2007-07-22 08:04:18 -0400211static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
212{
213 struct audit_tree_refs *p = ctx->trees;
214 int left = ctx->tree_count;
215 if (likely(left)) {
216 p->c[--left] = chunk;
217 ctx->tree_count = left;
218 return 1;
219 }
220 if (!p)
221 return 0;
222 p = p->next;
223 if (p) {
224 p->c[30] = chunk;
225 ctx->trees = p;
226 ctx->tree_count = 30;
227 return 1;
228 }
229 return 0;
230}
231
232static int grow_tree_refs(struct audit_context *ctx)
233{
234 struct audit_tree_refs *p = ctx->trees;
235 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
236 if (!ctx->trees) {
237 ctx->trees = p;
238 return 0;
239 }
240 if (p)
241 p->next = ctx->trees;
242 else
243 ctx->first_trees = ctx->trees;
244 ctx->tree_count = 31;
245 return 1;
246}
247#endif
248
249static void unroll_tree_refs(struct audit_context *ctx,
250 struct audit_tree_refs *p, int count)
251{
252#ifdef CONFIG_AUDIT_TREE
253 struct audit_tree_refs *q;
254 int n;
255 if (!p) {
256 /* we started with empty chain */
257 p = ctx->first_trees;
258 count = 31;
259 /* if the very first allocation has failed, nothing to do */
260 if (!p)
261 return;
262 }
263 n = count;
264 for (q = p; q != ctx->trees; q = q->next, n = 31) {
265 while (n--) {
266 audit_put_chunk(q->c[n]);
267 q->c[n] = NULL;
268 }
269 }
270 while (n-- > ctx->tree_count) {
271 audit_put_chunk(q->c[n]);
272 q->c[n] = NULL;
273 }
274 ctx->trees = p;
275 ctx->tree_count = count;
276#endif
277}
278
279static void free_tree_refs(struct audit_context *ctx)
280{
281 struct audit_tree_refs *p, *q;
282 for (p = ctx->first_trees; p; p = q) {
283 q = p->next;
284 kfree(p);
285 }
286}
287
288static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
289{
290#ifdef CONFIG_AUDIT_TREE
291 struct audit_tree_refs *p;
292 int n;
293 if (!tree)
294 return 0;
295 /* full ones */
296 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
297 for (n = 0; n < 31; n++)
298 if (audit_tree_match(p->c[n], tree))
299 return 1;
300 }
301 /* partial */
302 if (p) {
303 for (n = ctx->tree_count; n < 31; n++)
304 if (audit_tree_match(p->c[n], tree))
305 return 1;
306 }
307#endif
308 return 0;
309}
310
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700311static int audit_compare_uid(kuid_t uid,
312 struct audit_names *name,
313 struct audit_field *f,
314 struct audit_context *ctx)
Eric Parisb34b0392012-01-03 14:23:08 -0500315{
316 struct audit_names *n;
Eric Parisb34b0392012-01-03 14:23:08 -0500317 int rc;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700318
Eric Parisb34b0392012-01-03 14:23:08 -0500319 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700320 rc = audit_uid_comparator(uid, f->op, name->uid);
Eric Parisb34b0392012-01-03 14:23:08 -0500321 if (rc)
322 return rc;
323 }
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700324
Eric Parisb34b0392012-01-03 14:23:08 -0500325 if (ctx) {
326 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700327 rc = audit_uid_comparator(uid, f->op, n->uid);
328 if (rc)
329 return rc;
330 }
331 }
332 return 0;
333}
Eric Parisb34b0392012-01-03 14:23:08 -0500334
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700335static int audit_compare_gid(kgid_t gid,
336 struct audit_names *name,
337 struct audit_field *f,
338 struct audit_context *ctx)
339{
340 struct audit_names *n;
341 int rc;
342
343 if (name) {
344 rc = audit_gid_comparator(gid, f->op, name->gid);
345 if (rc)
346 return rc;
347 }
348
349 if (ctx) {
350 list_for_each_entry(n, &ctx->names_list, list) {
351 rc = audit_gid_comparator(gid, f->op, n->gid);
Eric Parisb34b0392012-01-03 14:23:08 -0500352 if (rc)
353 return rc;
354 }
355 }
356 return 0;
357}
358
Eric Paris02d86a52012-01-03 14:23:08 -0500359static int audit_field_compare(struct task_struct *tsk,
360 const struct cred *cred,
361 struct audit_field *f,
362 struct audit_context *ctx,
363 struct audit_names *name)
364{
Eric Paris02d86a52012-01-03 14:23:08 -0500365 switch (f->val) {
Peter Moody4a6633e2011-12-13 16:17:51 -0800366 /* process to file object comparisons */
Eric Paris02d86a52012-01-03 14:23:08 -0500367 case AUDIT_COMPARE_UID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700368 return audit_compare_uid(cred->uid, name, f, ctx);
Eric Parisc9fe6852012-01-03 14:23:08 -0500369 case AUDIT_COMPARE_GID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700370 return audit_compare_gid(cred->gid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800371 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700372 return audit_compare_uid(cred->euid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800373 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700374 return audit_compare_gid(cred->egid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800375 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700376 return audit_compare_uid(tsk->loginuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800377 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700378 return audit_compare_uid(cred->suid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800379 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700380 return audit_compare_gid(cred->sgid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800381 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700382 return audit_compare_uid(cred->fsuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800383 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700384 return audit_compare_gid(cred->fsgid, name, f, ctx);
Peter Moody10d68362012-01-04 15:24:31 -0500385 /* uid comparisons */
386 case AUDIT_COMPARE_UID_TO_AUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700387 return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
Peter Moody10d68362012-01-04 15:24:31 -0500388 case AUDIT_COMPARE_UID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700389 return audit_uid_comparator(cred->uid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500390 case AUDIT_COMPARE_UID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700391 return audit_uid_comparator(cred->uid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500392 case AUDIT_COMPARE_UID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700393 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500394 /* auid comparisons */
395 case AUDIT_COMPARE_AUID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700396 return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500397 case AUDIT_COMPARE_AUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700398 return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500399 case AUDIT_COMPARE_AUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700400 return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500401 /* euid comparisons */
402 case AUDIT_COMPARE_EUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700403 return audit_uid_comparator(cred->euid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500404 case AUDIT_COMPARE_EUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700405 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500406 /* suid comparisons */
407 case AUDIT_COMPARE_SUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700408 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500409 /* gid comparisons */
410 case AUDIT_COMPARE_GID_TO_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700411 return audit_gid_comparator(cred->gid, f->op, cred->egid);
Peter Moody10d68362012-01-04 15:24:31 -0500412 case AUDIT_COMPARE_GID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700413 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500414 case AUDIT_COMPARE_GID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700415 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500416 /* egid comparisons */
417 case AUDIT_COMPARE_EGID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700418 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500419 case AUDIT_COMPARE_EGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700420 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500421 /* sgid comparison */
422 case AUDIT_COMPARE_SGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700423 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
Eric Paris02d86a52012-01-03 14:23:08 -0500424 default:
425 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
426 return 0;
427 }
428 return 0;
429}
430
Amy Griffisf368c07d2006-04-07 16:55:56 -0400431/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/* Compare a task_struct with an audit_rule. Return 1 on match, 0
Tony Jonesf5629882011-04-27 15:10:49 +0200433 * otherwise.
434 *
435 * If task_creation is true, this is an explicit indication that we are
436 * filtering a task rule at task creation time. This and tsk == current are
437 * the only situations where tsk->cred may be accessed without an rcu read lock.
438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500440 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400442 struct audit_names *name,
Tony Jonesf5629882011-04-27 15:10:49 +0200443 enum audit_state *state,
444 bool task_creation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Tony Jonesf5629882011-04-27 15:10:49 +0200446 const struct cred *cred;
Eric Paris5195d8e2012-01-03 14:23:05 -0500447 int i, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600448 u32 sid;
449
Tony Jonesf5629882011-04-27 15:10:49 +0200450 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500453 struct audit_field *f = &rule->fields[i];
Eric Paris5195d8e2012-01-03 14:23:05 -0500454 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int result = 0;
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500456 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Amy Griffis93315ed2006-02-07 12:05:27 -0500458 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case AUDIT_PID:
Paul Moorefa2bea22016-08-30 17:19:13 -0400460 pid = task_tgid_nr(tsk);
Richard Guy Briggsf1dc4862013-12-11 13:52:26 -0500461 result = audit_comparator(pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
Al Viro3c662512006-05-06 08:26:27 -0400463 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400464 if (ctx) {
465 if (!ctx->ppid)
Richard Guy Briggsc92cdeb2013-12-10 22:10:41 -0500466 ctx->ppid = task_ppid_nr(tsk);
Al Viro3c662512006-05-06 08:26:27 -0400467 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400468 }
Al Viro3c662512006-05-06 08:26:27 -0400469 break;
Richard Guy Briggs34d99af52015-08-05 16:29:37 -0400470 case AUDIT_EXE:
471 result = audit_exe_compare(tsk, rule->exe);
472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 case AUDIT_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700474 result = audit_uid_comparator(cred->uid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476 case AUDIT_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700477 result = audit_uid_comparator(cred->euid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 case AUDIT_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700480 result = audit_uid_comparator(cred->suid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 break;
482 case AUDIT_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700483 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 case AUDIT_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700486 result = audit_gid_comparator(cred->gid, f->op, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300487 if (f->op == Audit_equal) {
488 if (!result)
489 result = in_group_p(f->gid);
490 } else if (f->op == Audit_not_equal) {
491 if (result)
492 result = !in_group_p(f->gid);
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 break;
495 case AUDIT_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700496 result = audit_gid_comparator(cred->egid, f->op, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300497 if (f->op == Audit_equal) {
498 if (!result)
499 result = in_egroup_p(f->gid);
500 } else if (f->op == Audit_not_equal) {
501 if (result)
502 result = !in_egroup_p(f->gid);
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 case AUDIT_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700506 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508 case AUDIT_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700509 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 break;
511 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500512 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 break;
2fd6f582005-04-29 16:08:28 +0100514 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700515 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500516 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 case AUDIT_EXIT:
520 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500521 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100524 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500525 if (f->val)
526 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100527 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500528 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531 case AUDIT_DEVMAJOR:
Eric Paris16c174b2012-01-03 14:23:05 -0500532 if (name) {
533 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
534 audit_comparator(MAJOR(name->rdev), f->op, f->val))
535 ++result;
536 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500537 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174b2012-01-03 14:23:05 -0500538 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
539 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ++result;
541 break;
542 }
543 }
544 }
545 break;
546 case AUDIT_DEVMINOR:
Eric Paris16c174b2012-01-03 14:23:05 -0500547 if (name) {
548 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
549 audit_comparator(MINOR(name->rdev), f->op, f->val))
550 ++result;
551 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500552 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174b2012-01-03 14:23:05 -0500553 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
554 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ++result;
556 break;
557 }
558 }
559 }
560 break;
561 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400562 if (name)
Richard Guy Briggsdb510fc2013-07-04 12:56:11 -0400563 result = audit_comparator(name->ino, f->op, f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400564 else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500565 list_for_each_entry(n, &ctx->names_list, list) {
566 if (audit_comparator(n->ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 ++result;
568 break;
569 }
570 }
571 }
572 break;
Eric Parisefaffd62012-01-03 14:23:07 -0500573 case AUDIT_OBJ_UID:
574 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700575 result = audit_uid_comparator(name->uid, f->op, f->uid);
Eric Parisefaffd62012-01-03 14:23:07 -0500576 } else if (ctx) {
577 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700578 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
Eric Parisefaffd62012-01-03 14:23:07 -0500579 ++result;
580 break;
581 }
582 }
583 }
584 break;
Eric Paris54d32182012-01-03 14:23:07 -0500585 case AUDIT_OBJ_GID:
586 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700587 result = audit_gid_comparator(name->gid, f->op, f->gid);
Eric Paris54d32182012-01-03 14:23:07 -0500588 } else if (ctx) {
589 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700590 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
Eric Paris54d32182012-01-03 14:23:07 -0500591 ++result;
592 break;
593 }
594 }
595 }
596 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400597 case AUDIT_WATCH:
Eric Parisae7b8f42009-12-17 20:12:04 -0500598 if (name)
599 result = audit_watch_compare(rule->watch, name->ino, name->dev);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400600 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400601 case AUDIT_DIR:
602 if (ctx)
603 result = match_tree_refs(ctx, rule->tree);
604 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 case AUDIT_LOGINUID:
Mikhail Klementyev5c1390c2015-05-25 23:20:38 +0300606 result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 break;
Eric W. Biederman780a7652013-04-09 02:22:10 -0700608 case AUDIT_LOGINUID_SET:
609 result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
610 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500611 case AUDIT_SUBJ_USER:
612 case AUDIT_SUBJ_ROLE:
613 case AUDIT_SUBJ_TYPE:
614 case AUDIT_SUBJ_SEN:
615 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600616 /* NOTE: this may return negative values indicating
617 a temporary error. We simply treat this as a
618 match for now to avoid losing information that
619 may be wanted. An error message will also be
620 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000621 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400622 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200623 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400624 need_sid = 0;
625 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200626 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600627 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000628 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600629 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400630 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600631 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500632 case AUDIT_OBJ_USER:
633 case AUDIT_OBJ_ROLE:
634 case AUDIT_OBJ_TYPE:
635 case AUDIT_OBJ_LEV_LOW:
636 case AUDIT_OBJ_LEV_HIGH:
637 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
638 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000639 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500640 /* Find files that match */
641 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200642 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500643 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000644 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500645 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500646 list_for_each_entry(n, &ctx->names_list, list) {
647 if (security_audit_rule_match(n->osid, f->type,
648 f->op, f->lsm_rule,
649 ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500650 ++result;
651 break;
652 }
653 }
654 }
655 /* Find ipc objects that match */
Al Viroa33e6752008-12-10 03:40:06 -0500656 if (!ctx || ctx->type != AUDIT_IPC)
657 break;
658 if (security_audit_rule_match(ctx->ipc.osid,
659 f->type, f->op,
660 f->lsm_rule, ctx))
661 ++result;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500662 }
663 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 case AUDIT_ARG0:
665 case AUDIT_ARG1:
666 case AUDIT_ARG2:
667 case AUDIT_ARG3:
668 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500669 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400671 case AUDIT_FILTERKEY:
672 /* ignore this field for filtering */
673 result = 1;
674 break;
Al Viro55669bf2006-08-31 19:26:40 -0400675 case AUDIT_PERM:
676 result = audit_match_perm(ctx, f->val);
677 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400678 case AUDIT_FILETYPE:
679 result = audit_match_filetype(ctx, f->val);
680 break;
Eric Paris02d86a52012-01-03 14:23:08 -0500681 case AUDIT_FIELD_COMPARE:
682 result = audit_field_compare(tsk, cred, f, ctx, name);
683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Tony Jonesf5629882011-04-27 15:10:49 +0200685 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
687 }
Al Viro0590b932008-12-14 23:45:27 -0500688
689 if (ctx) {
690 if (rule->prio <= ctx->prio)
691 return 0;
692 if (rule->filterkey) {
693 kfree(ctx->filterkey);
694 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
695 }
696 ctx->prio = rule->prio;
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 switch (rule->action) {
Paul Moore66b12ab2016-06-16 17:08:19 -0400699 case AUDIT_NEVER:
700 *state = AUDIT_DISABLED;
701 break;
702 case AUDIT_ALWAYS:
703 *state = AUDIT_RECORD_CONTEXT;
704 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 return 1;
707}
708
709/* At process creation time, we can determine if system-call auditing is
710 * completely disabled for this task. Since we only have the task
711 * structure at this point, we can only check uid and gid.
712 */
Al Viroe048e022008-12-16 03:51:22 -0500713static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct audit_entry *e;
716 enum audit_state state;
717
718 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100719 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Tony Jonesf5629882011-04-27 15:10:49 +0200720 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
721 &state, true)) {
Al Viroe048e022008-12-16 03:51:22 -0500722 if (state == AUDIT_RECORD_CONTEXT)
723 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 rcu_read_unlock();
725 return state;
726 }
727 }
728 rcu_read_unlock();
729 return AUDIT_BUILD_CONTEXT;
730}
731
Andy Lutomirskia3c54932014-05-28 23:09:58 -0400732static int audit_in_mask(const struct audit_krule *rule, unsigned long val)
733{
734 int word, bit;
735
736 if (val > 0xffffffff)
737 return false;
738
739 word = AUDIT_WORD(val);
740 if (word >= AUDIT_BITMASK_SIZE)
741 return false;
742
743 bit = AUDIT_BIT(val);
744
745 return rule->mask[word] & bit;
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/* At syscall entry and exit time, this filter is called if the
749 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100750 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700751 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
753static enum audit_state audit_filter_syscall(struct task_struct *tsk,
754 struct audit_context *ctx,
755 struct list_head *list)
756{
757 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100758 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
David Woodhouse351bb722005-07-14 14:40:06 +0100760 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100761 return AUDIT_DISABLED;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100764 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000765 list_for_each_entry_rcu(e, list, list) {
Andy Lutomirskia3c54932014-05-28 23:09:58 -0400766 if (audit_in_mask(&e->rule, ctx->major) &&
Amy Griffisf368c07d2006-04-07 16:55:56 -0400767 audit_filter_rules(tsk, &e->rule, ctx, NULL,
Tony Jonesf5629882011-04-27 15:10:49 +0200768 &state, false)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000769 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500770 ctx->current_state = state;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000771 return state;
772 }
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 rcu_read_unlock();
776 return AUDIT_BUILD_CONTEXT;
777}
778
Eric Paris5195d8e2012-01-03 14:23:05 -0500779/*
780 * Given an audit_name check the inode hash table to see if they match.
781 * Called holding the rcu read lock to protect the use of audit_inode_hash
782 */
783static int audit_filter_inode_name(struct task_struct *tsk,
784 struct audit_names *n,
785 struct audit_context *ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500786 int h = audit_hash_ino((u32)n->ino);
787 struct list_head *list = &audit_inode_hash[h];
788 struct audit_entry *e;
789 enum audit_state state;
790
Eric Paris5195d8e2012-01-03 14:23:05 -0500791 if (list_empty(list))
792 return 0;
793
794 list_for_each_entry_rcu(e, list, list) {
Andy Lutomirskia3c54932014-05-28 23:09:58 -0400795 if (audit_in_mask(&e->rule, ctx->major) &&
Eric Paris5195d8e2012-01-03 14:23:05 -0500796 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
797 ctx->current_state = state;
798 return 1;
799 }
800 }
801
802 return 0;
803}
804
805/* At syscall exit time, this filter is called if any audit_names have been
Amy Griffisf368c07d2006-04-07 16:55:56 -0400806 * collected during syscall processing. We only check rules in sublists at hash
Eric Paris5195d8e2012-01-03 14:23:05 -0500807 * buckets applicable to the inode numbers in audit_names.
Amy Griffisf368c07d2006-04-07 16:55:56 -0400808 * Regarding audit_state, same rules apply as for audit_filter_syscall().
809 */
Al Viro0590b932008-12-14 23:45:27 -0500810void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400811{
Eric Paris5195d8e2012-01-03 14:23:05 -0500812 struct audit_names *n;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400813
814 if (audit_pid && tsk->tgid == audit_pid)
Al Viro0590b932008-12-14 23:45:27 -0500815 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400816
817 rcu_read_lock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400818
Eric Paris5195d8e2012-01-03 14:23:05 -0500819 list_for_each_entry(n, &ctx->names_list, list) {
820 if (audit_filter_inode_name(tsk, n, ctx))
821 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400822 }
823 rcu_read_unlock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400824}
825
Richard Guy Briggs4a3eb722014-02-18 15:29:43 -0500826/* Transfer the audit context pointer to the caller, clearing it in the tsk's struct */
827static inline struct audit_context *audit_take_context(struct task_struct *tsk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int return_valid,
Paul Moore6d208da2009-04-01 15:47:27 -0400829 long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 struct audit_context *context = tsk->audit_context;
832
Eric Paris56179a62012-01-03 14:23:06 -0500833 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return NULL;
835 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500836
837 /*
838 * we need to fix up the return code in the audit logs if the actual
839 * return codes are later going to be fixed up by the arch specific
840 * signal handlers
841 *
842 * This is actually a test for:
843 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
844 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
845 *
846 * but is faster than a bunch of ||
847 */
848 if (unlikely(return_code <= -ERESTARTSYS) &&
849 (return_code >= -ERESTART_RESTARTBLOCK) &&
850 (return_code != -ENOIOCTLCMD))
851 context->return_code = -EINTR;
852 else
853 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Al Viro0590b932008-12-14 23:45:27 -0500855 if (context->in_syscall && !context->dummy) {
856 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
857 audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 tsk->audit_context = NULL;
861 return context;
862}
863
William Roberts3f1c8252014-02-11 10:12:01 -0800864static inline void audit_proctitle_free(struct audit_context *context)
865{
866 kfree(context->proctitle.value);
867 context->proctitle.value = NULL;
868 context->proctitle.len = 0;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871static inline void audit_free_names(struct audit_context *context)
872{
Eric Paris5195d8e2012-01-03 14:23:05 -0500873 struct audit_names *n, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Eric Paris5195d8e2012-01-03 14:23:05 -0500875 list_for_each_entry_safe(n, next, &context->names_list, list) {
876 list_del(&n->list);
Paul Moore55422d02015-01-22 00:00:23 -0500877 if (n->name)
878 putname(n->name);
Eric Paris5195d8e2012-01-03 14:23:05 -0500879 if (n->should_free)
880 kfree(n);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -0800883 path_put(&context->pwd);
884 context->pwd.dentry = NULL;
885 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888static inline void audit_free_aux(struct audit_context *context)
889{
890 struct audit_aux_data *aux;
891
892 while ((aux = context->aux)) {
893 context->aux = aux->next;
894 kfree(aux);
895 }
Amy Griffise54dc242007-03-29 18:01:04 -0400896 while ((aux = context->aux_pids)) {
897 context->aux_pids = aux->next;
898 kfree(aux);
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902static inline struct audit_context *audit_alloc_context(enum audit_state state)
903{
904 struct audit_context *context;
905
Rakib Mullick17c6ee72013-04-07 16:14:18 +0600906 context = kzalloc(sizeof(*context), GFP_KERNEL);
907 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return NULL;
Andrew Mortone2c5adc2013-04-08 14:43:41 -0700909 context->state = state;
910 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Al Viro916d7572009-06-24 00:02:38 -0400911 INIT_LIST_HEAD(&context->killed_trees);
Eric Paris5195d8e2012-01-03 14:23:05 -0500912 INIT_LIST_HEAD(&context->names_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return context;
914}
915
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700916/**
917 * audit_alloc - allocate an audit context block for a task
918 * @tsk: task
919 *
920 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 * if necessary. Doing so turns on system call auditing for the
922 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700923 * needed.
924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925int audit_alloc(struct task_struct *tsk)
926{
927 struct audit_context *context;
928 enum audit_state state;
Al Viroe048e022008-12-16 03:51:22 -0500929 char *key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Eric Parisb593d382008-01-08 17:38:31 -0500931 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0; /* Return if not auditing. */
933
Al Viroe048e022008-12-16 03:51:22 -0500934 state = audit_filter_task(tsk, &key);
Oleg Nesterovd48d8052013-09-15 19:11:09 +0200935 if (state == AUDIT_DISABLED) {
936 clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
Oleg Nesterovd48d8052013-09-15 19:11:09 +0200938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (!(context = audit_alloc_context(state))) {
Al Viroe048e022008-12-16 03:51:22 -0500941 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 audit_log_lost("out of memory in audit_alloc");
943 return -ENOMEM;
944 }
Al Viroe048e022008-12-16 03:51:22 -0500945 context->filterkey = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 tsk->audit_context = context;
948 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
949 return 0;
950}
951
952static inline void audit_free_context(struct audit_context *context)
953{
Al Viroc62d7732012-10-20 15:07:18 -0400954 audit_free_names(context);
955 unroll_tree_refs(context, NULL, 0);
956 free_tree_refs(context);
957 audit_free_aux(context);
958 kfree(context->filterkey);
959 kfree(context->sockaddr);
William Roberts3f1c8252014-02-11 10:12:01 -0800960 audit_proctitle_free(context);
Al Viroc62d7732012-10-20 15:07:18 -0400961 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
Amy Griffise54dc242007-03-29 18:01:04 -0400964static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800965 kuid_t auid, kuid_t uid, unsigned int sessionid,
Eric Paris4746ec52008-01-08 10:06:53 -0500966 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400967{
968 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200969 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -0400970 u32 len;
971 int rc = 0;
972
973 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
974 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -0500975 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -0400976
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700977 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
978 from_kuid(&init_user_ns, auid),
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800979 from_kuid(&init_user_ns, uid), sessionid);
Eric Parisad395ab2012-10-23 08:58:35 -0400980 if (sid) {
981 if (security_secid_to_secctx(sid, &ctx, &len)) {
982 audit_log_format(ab, " obj=(none)");
983 rc = 1;
984 } else {
985 audit_log_format(ab, " obj=%s", ctx);
986 security_release_secctx(ctx, len);
987 }
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200988 }
Eric Parisc2a77802008-01-07 13:40:17 -0500989 audit_log_format(ab, " ocomm=");
990 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -0400991 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -0400992
993 return rc;
994}
995
Eric Parisde6bbd12008-01-07 14:31:58 -0500996static void audit_log_execve_info(struct audit_context *context,
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -0400997 struct audit_buffer **ab)
Eric Parisde6bbd12008-01-07 14:31:58 -0500998{
Paul Moore437614732016-07-19 17:42:57 -0400999 long len_max;
1000 long len_rem;
1001 long len_full;
1002 long len_buf;
1003 long len_abuf;
1004 long len_tmp;
1005 bool require_data;
1006 bool encode;
1007 unsigned int iter;
1008 unsigned int arg;
1009 char *buf_head;
Eric Parisde6bbd12008-01-07 14:31:58 -05001010 char *buf;
Paul Moore437614732016-07-19 17:42:57 -04001011 const char __user *p = (const char __user *)current->mm->arg_start;
Eric Parisde6bbd12008-01-07 14:31:58 -05001012
Paul Moore437614732016-07-19 17:42:57 -04001013 /* NOTE: this buffer needs to be large enough to hold all the non-arg
1014 * data we put in the audit record for this argument (see the
1015 * code below) ... at this point in time 96 is plenty */
1016 char abuf[96];
Eric Parisde6bbd12008-01-07 14:31:58 -05001017
Paul Moore437614732016-07-19 17:42:57 -04001018 /* NOTE: we set MAX_EXECVE_AUDIT_LEN to a rather arbitrary limit, the
1019 * current value of 7500 is not as important as the fact that it
1020 * is less than 8k, a setting of 7500 gives us plenty of wiggle
1021 * room if we go over a little bit in the logging below */
1022 WARN_ON_ONCE(MAX_EXECVE_AUDIT_LEN > 7500);
1023 len_max = MAX_EXECVE_AUDIT_LEN;
Eric Parisde6bbd12008-01-07 14:31:58 -05001024
Paul Moore437614732016-07-19 17:42:57 -04001025 /* scratch buffer to hold the userspace args */
1026 buf_head = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1027 if (!buf_head) {
Joe Perchesb7550782014-03-05 14:34:36 -08001028 audit_panic("out of memory for argv string");
Eric Parisde6bbd12008-01-07 14:31:58 -05001029 return;
1030 }
Paul Moore437614732016-07-19 17:42:57 -04001031 buf = buf_head;
Eric Parisde6bbd12008-01-07 14:31:58 -05001032
Paul Moore437614732016-07-19 17:42:57 -04001033 audit_log_format(*ab, "argc=%d", context->execve.argc);
1034
1035 len_rem = len_max;
1036 len_buf = 0;
1037 len_full = 0;
1038 require_data = true;
1039 encode = false;
1040 iter = 0;
1041 arg = 0;
1042 do {
1043 /* NOTE: we don't ever want to trust this value for anything
1044 * serious, but the audit record format insists we
1045 * provide an argument length for really long arguments,
1046 * e.g. > MAX_EXECVE_AUDIT_LEN, so we have no choice but
1047 * to use strncpy_from_user() to obtain this value for
1048 * recording in the log, although we don't use it
1049 * anywhere here to avoid a double-fetch problem */
1050 if (len_full == 0)
1051 len_full = strnlen_user(p, MAX_ARG_STRLEN) - 1;
1052
1053 /* read more data from userspace */
1054 if (require_data) {
1055 /* can we make more room in the buffer? */
1056 if (buf != buf_head) {
1057 memmove(buf_head, buf, len_buf);
1058 buf = buf_head;
1059 }
1060
1061 /* fetch as much as we can of the argument */
1062 len_tmp = strncpy_from_user(&buf_head[len_buf], p,
1063 len_max - len_buf);
1064 if (len_tmp == -EFAULT) {
1065 /* unable to copy from userspace */
1066 send_sig(SIGKILL, current, 0);
1067 goto out;
1068 } else if (len_tmp == (len_max - len_buf)) {
1069 /* buffer is not large enough */
1070 require_data = true;
1071 /* NOTE: if we are going to span multiple
1072 * buffers force the encoding so we stand
1073 * a chance at a sane len_full value and
1074 * consistent record encoding */
1075 encode = true;
1076 len_full = len_full * 2;
1077 p += len_tmp;
1078 } else {
1079 require_data = false;
1080 if (!encode)
1081 encode = audit_string_contains_control(
1082 buf, len_tmp);
1083 /* try to use a trusted value for len_full */
1084 if (len_full < len_max)
1085 len_full = (encode ?
1086 len_tmp * 2 : len_tmp);
1087 p += len_tmp + 1;
1088 }
1089 len_buf += len_tmp;
1090 buf_head[len_buf] = '\0';
1091
1092 /* length of the buffer in the audit record? */
1093 len_abuf = (encode ? len_buf * 2 : len_buf + 2);
1094 }
1095
1096 /* write as much as we can to the audit log */
1097 if (len_buf > 0) {
1098 /* NOTE: some magic numbers here - basically if we
1099 * can't fit a reasonable amount of data into the
1100 * existing audit buffer, flush it and start with
1101 * a new buffer */
1102 if ((sizeof(abuf) + 8) > len_rem) {
1103 len_rem = len_max;
1104 audit_log_end(*ab);
1105 *ab = audit_log_start(context,
1106 GFP_KERNEL, AUDIT_EXECVE);
1107 if (!*ab)
1108 goto out;
1109 }
1110
1111 /* create the non-arg portion of the arg record */
1112 len_tmp = 0;
1113 if (require_data || (iter > 0) ||
1114 ((len_abuf + sizeof(abuf)) > len_rem)) {
1115 if (iter == 0) {
1116 len_tmp += snprintf(&abuf[len_tmp],
1117 sizeof(abuf) - len_tmp,
1118 " a%d_len=%lu",
1119 arg, len_full);
1120 }
1121 len_tmp += snprintf(&abuf[len_tmp],
1122 sizeof(abuf) - len_tmp,
1123 " a%d[%d]=", arg, iter++);
1124 } else
1125 len_tmp += snprintf(&abuf[len_tmp],
1126 sizeof(abuf) - len_tmp,
1127 " a%d=", arg);
1128 WARN_ON(len_tmp >= sizeof(abuf));
1129 abuf[sizeof(abuf) - 1] = '\0';
1130
1131 /* log the arg in the audit record */
1132 audit_log_format(*ab, "%s", abuf);
1133 len_rem -= len_tmp;
1134 len_tmp = len_buf;
1135 if (encode) {
1136 if (len_abuf > len_rem)
1137 len_tmp = len_rem / 2; /* encoding */
1138 audit_log_n_hex(*ab, buf, len_tmp);
1139 len_rem -= len_tmp * 2;
1140 len_abuf -= len_tmp * 2;
1141 } else {
1142 if (len_abuf > len_rem)
1143 len_tmp = len_rem - 2; /* quotes */
1144 audit_log_n_string(*ab, buf, len_tmp);
1145 len_rem -= len_tmp + 2;
1146 /* don't subtract the "2" because we still need
1147 * to add quotes to the remaining string */
1148 len_abuf -= len_tmp;
1149 }
1150 len_buf -= len_tmp;
1151 buf += len_tmp;
1152 }
1153
1154 /* ready to move to the next argument? */
1155 if ((len_buf == 0) && !require_data) {
1156 arg++;
1157 iter = 0;
1158 len_full = 0;
1159 require_data = true;
1160 encode = false;
1161 }
1162 } while (arg < context->execve.argc);
1163
1164 /* NOTE: the caller handles the final audit_log_end() call */
1165
1166out:
1167 kfree(buf_head);
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) {
Steve Grubb0b7a0fd2016-07-14 10:59:19 -04001428 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 Briggs4a3eb722014-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 Briggs4a3eb722014-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 Briggs76a658c2016-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
Paul Moorefa2bea22016-08-30 17:19:13 -04001996 audit_log_format(ab, "pid=%d uid=%u", task_tgid_nr(current), uid);
Eric Parisddfad8a2011-01-19 19:22:35 -05001997 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
Paul Moorefa2bea22016-08-30 17:19:13 -04002223 context->target_pid = task_tgid_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) {
Paul Moorefa2bea22016-08-30 17:19:13 -04002248 audit_sig_pid = task_tgid_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;
Paul Moorefa2bea22016-08-30 17:19:13 -04002348 context->capset.pid = task_tgid_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 Paris85e7bac2012-01-03 14:23:05 -05002364{
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002365 kuid_t auid, uid;
2366 kgid_t gid;
Eric Paris85e7bac2012-01-03 14:23:05 -05002367 unsigned int sessionid;
Richard Guy Briggs9eab3392014-03-15 18:42:34 -04002368 char comm[sizeof(current->comm)];
Eric Paris85e7bac2012-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 Paris85e7bac2012-01-03 14:23:05 -05002379 audit_log_task_context(ab);
Paul Moorefa2bea22016-08-30 17:19:13 -04002380 audit_log_format(ab, " pid=%d comm=", task_tgid_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 Paris85e7bac2012-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 Paris85e7bac2012-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}