blob: 425a8939be1a7e42e7e7bd5d9616e6d1db9c8748 [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
45#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070047#include <linux/atomic.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000048#include <linux/fs.h>
49#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040051#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Stephen Smalley01116102005-05-21 00:15:52 +010053#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010054#include <linux/socket.h>
George C. Wilson20ca73b2006-05-24 16:09:55 -050055#include <linux/mqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/audit.h>
57#include <linux/personality.h>
58#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010059#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010060#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000062#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000063#include <linux/list.h>
Steve Grubba6c043a2006-01-01 14:07:00 -050064#include <linux/tty.h>
Al Viro473ae302006-04-26 14:04:08 -040065#include <linux/binfmts.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040066#include <linux/highmem.h>
Al Virof46038f2006-05-06 08:22:52 -040067#include <linux/syscalls.h>
Eric Paris851f7ff2008-11-11 21:48:14 +110068#include <linux/capability.h>
Al Viro5ad4e532009-03-29 19:50:06 -040069#include <linux/fs_struct.h>
Kees Cook3dc1c1b2012-04-12 16:47:58 -050070#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
David Woodhousefe7752b2005-12-15 18:33:52 +000072#include "audit.h"
73
Eric Parisd7e75282012-01-03 14:23:06 -050074/* flags stating the success for a syscall */
75#define AUDITSC_INVALID 0
76#define AUDITSC_SUCCESS 1
77#define AUDITSC_FAILURE 2
78
Eric Parisde6bbd12008-01-07 14:31:58 -050079/* no execve audit message should be longer than this (userspace limits) */
80#define MAX_EXECVE_AUDIT_LEN 7500
81
Al Viro471a5c72006-07-10 08:29:24 -040082/* number of audit rules */
83int audit_n_rules;
84
Amy Griffise54dc242007-03-29 18:01:04 -040085/* determines whether we collect data for signals sent */
86int audit_signals;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088struct audit_aux_data {
89 struct audit_aux_data *next;
90 int type;
91};
92
93#define AUDIT_AUX_IPCPERM 0
94
Amy Griffise54dc242007-03-29 18:01:04 -040095/* Number of target pids per aux struct. */
96#define AUDIT_AUX_PIDS 16
97
Amy Griffise54dc242007-03-29 18:01:04 -040098struct audit_aux_data_pids {
99 struct audit_aux_data d;
100 pid_t target_pid[AUDIT_AUX_PIDS];
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700101 kuid_t target_auid[AUDIT_AUX_PIDS];
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800102 kuid_t target_uid[AUDIT_AUX_PIDS];
Eric Paris4746ec52008-01-08 10:06:53 -0500103 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400104 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500105 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400106 int pid_count;
107};
108
Eric Paris3fc689e2008-11-11 21:48:18 +1100109struct audit_aux_data_bprm_fcaps {
110 struct audit_aux_data d;
111 struct audit_cap_data fcap;
112 unsigned int fcap_ver;
113 struct audit_cap_data old_pcap;
114 struct audit_cap_data new_pcap;
115};
116
Al Viro74c3cbe2007-07-22 08:04:18 -0400117struct audit_tree_refs {
118 struct audit_tree_refs *next;
119 struct audit_chunk *c[31];
120};
121
Al Viro55669bf2006-08-31 19:26:40 -0400122static inline int open_arg(int flags, int mask)
123{
124 int n = ACC_MODE(flags);
125 if (flags & (O_TRUNC | O_CREAT))
126 n |= AUDIT_PERM_WRITE;
127 return n & mask;
128}
129
130static int audit_match_perm(struct audit_context *ctx, int mask)
131{
Cordeliac4bacef2008-08-18 09:45:51 -0700132 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800133 if (unlikely(!ctx))
134 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700135 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100136
Al Viro55669bf2006-08-31 19:26:40 -0400137 switch (audit_classify_syscall(ctx->arch, n)) {
138 case 0: /* native */
139 if ((mask & AUDIT_PERM_WRITE) &&
140 audit_match_class(AUDIT_CLASS_WRITE, n))
141 return 1;
142 if ((mask & AUDIT_PERM_READ) &&
143 audit_match_class(AUDIT_CLASS_READ, n))
144 return 1;
145 if ((mask & AUDIT_PERM_ATTR) &&
146 audit_match_class(AUDIT_CLASS_CHATTR, n))
147 return 1;
148 return 0;
149 case 1: /* 32bit on biarch */
150 if ((mask & AUDIT_PERM_WRITE) &&
151 audit_match_class(AUDIT_CLASS_WRITE_32, n))
152 return 1;
153 if ((mask & AUDIT_PERM_READ) &&
154 audit_match_class(AUDIT_CLASS_READ_32, n))
155 return 1;
156 if ((mask & AUDIT_PERM_ATTR) &&
157 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
158 return 1;
159 return 0;
160 case 2: /* open */
161 return mask & ACC_MODE(ctx->argv[1]);
162 case 3: /* openat */
163 return mask & ACC_MODE(ctx->argv[2]);
164 case 4: /* socketcall */
165 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
166 case 5: /* execve */
167 return mask & AUDIT_PERM_EXEC;
168 default:
169 return 0;
170 }
171}
172
Eric Paris5ef30ee2012-01-03 14:23:05 -0500173static int audit_match_filetype(struct audit_context *ctx, int val)
Al Viro8b67dca2008-04-28 04:15:49 -0400174{
Eric Paris5195d8e2012-01-03 14:23:05 -0500175 struct audit_names *n;
Eric Paris5ef30ee2012-01-03 14:23:05 -0500176 umode_t mode = (umode_t)val;
zhangxiliang1a61c882008-08-02 10:56:37 +0800177
178 if (unlikely(!ctx))
179 return 0;
180
Eric Paris5195d8e2012-01-03 14:23:05 -0500181 list_for_each_entry(n, &ctx->names_list, list) {
182 if ((n->ino != -1) &&
183 ((n->mode & S_IFMT) == mode))
Eric Paris5ef30ee2012-01-03 14:23:05 -0500184 return 1;
185 }
Eric Paris5195d8e2012-01-03 14:23:05 -0500186
Eric Paris5ef30ee2012-01-03 14:23:05 -0500187 return 0;
Al Viro8b67dca2008-04-28 04:15:49 -0400188}
189
Al Viro74c3cbe2007-07-22 08:04:18 -0400190/*
191 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
192 * ->first_trees points to its beginning, ->trees - to the current end of data.
193 * ->tree_count is the number of free entries in array pointed to by ->trees.
194 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
195 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
196 * it's going to remain 1-element for almost any setup) until we free context itself.
197 * References in it _are_ dropped - at the same time we free/drop aux stuff.
198 */
199
200#ifdef CONFIG_AUDIT_TREE
Eric Paris679173b2009-01-26 18:09:45 -0500201static void audit_set_auditable(struct audit_context *ctx)
202{
203 if (!ctx->prio) {
204 ctx->prio = 1;
205 ctx->current_state = AUDIT_RECORD_CONTEXT;
206 }
207}
208
Al Viro74c3cbe2007-07-22 08:04:18 -0400209static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
210{
211 struct audit_tree_refs *p = ctx->trees;
212 int left = ctx->tree_count;
213 if (likely(left)) {
214 p->c[--left] = chunk;
215 ctx->tree_count = left;
216 return 1;
217 }
218 if (!p)
219 return 0;
220 p = p->next;
221 if (p) {
222 p->c[30] = chunk;
223 ctx->trees = p;
224 ctx->tree_count = 30;
225 return 1;
226 }
227 return 0;
228}
229
230static int grow_tree_refs(struct audit_context *ctx)
231{
232 struct audit_tree_refs *p = ctx->trees;
233 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
234 if (!ctx->trees) {
235 ctx->trees = p;
236 return 0;
237 }
238 if (p)
239 p->next = ctx->trees;
240 else
241 ctx->first_trees = ctx->trees;
242 ctx->tree_count = 31;
243 return 1;
244}
245#endif
246
247static void unroll_tree_refs(struct audit_context *ctx,
248 struct audit_tree_refs *p, int count)
249{
250#ifdef CONFIG_AUDIT_TREE
251 struct audit_tree_refs *q;
252 int n;
253 if (!p) {
254 /* we started with empty chain */
255 p = ctx->first_trees;
256 count = 31;
257 /* if the very first allocation has failed, nothing to do */
258 if (!p)
259 return;
260 }
261 n = count;
262 for (q = p; q != ctx->trees; q = q->next, n = 31) {
263 while (n--) {
264 audit_put_chunk(q->c[n]);
265 q->c[n] = NULL;
266 }
267 }
268 while (n-- > ctx->tree_count) {
269 audit_put_chunk(q->c[n]);
270 q->c[n] = NULL;
271 }
272 ctx->trees = p;
273 ctx->tree_count = count;
274#endif
275}
276
277static void free_tree_refs(struct audit_context *ctx)
278{
279 struct audit_tree_refs *p, *q;
280 for (p = ctx->first_trees; p; p = q) {
281 q = p->next;
282 kfree(p);
283 }
284}
285
286static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
287{
288#ifdef CONFIG_AUDIT_TREE
289 struct audit_tree_refs *p;
290 int n;
291 if (!tree)
292 return 0;
293 /* full ones */
294 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
295 for (n = 0; n < 31; n++)
296 if (audit_tree_match(p->c[n], tree))
297 return 1;
298 }
299 /* partial */
300 if (p) {
301 for (n = ctx->tree_count; n < 31; n++)
302 if (audit_tree_match(p->c[n], tree))
303 return 1;
304 }
305#endif
306 return 0;
307}
308
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700309static int audit_compare_uid(kuid_t uid,
310 struct audit_names *name,
311 struct audit_field *f,
312 struct audit_context *ctx)
Eric Parisb34b0392012-01-03 14:23:08 -0500313{
314 struct audit_names *n;
Eric Parisb34b0392012-01-03 14:23:08 -0500315 int rc;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700316
Eric Parisb34b0392012-01-03 14:23:08 -0500317 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700318 rc = audit_uid_comparator(uid, f->op, name->uid);
Eric Parisb34b0392012-01-03 14:23:08 -0500319 if (rc)
320 return rc;
321 }
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700322
Eric Parisb34b0392012-01-03 14:23:08 -0500323 if (ctx) {
324 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700325 rc = audit_uid_comparator(uid, f->op, n->uid);
326 if (rc)
327 return rc;
328 }
329 }
330 return 0;
331}
Eric Parisb34b0392012-01-03 14:23:08 -0500332
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700333static int audit_compare_gid(kgid_t gid,
334 struct audit_names *name,
335 struct audit_field *f,
336 struct audit_context *ctx)
337{
338 struct audit_names *n;
339 int rc;
340
341 if (name) {
342 rc = audit_gid_comparator(gid, f->op, name->gid);
343 if (rc)
344 return rc;
345 }
346
347 if (ctx) {
348 list_for_each_entry(n, &ctx->names_list, list) {
349 rc = audit_gid_comparator(gid, f->op, n->gid);
Eric Parisb34b0392012-01-03 14:23:08 -0500350 if (rc)
351 return rc;
352 }
353 }
354 return 0;
355}
356
Eric Paris02d86a52012-01-03 14:23:08 -0500357static int audit_field_compare(struct task_struct *tsk,
358 const struct cred *cred,
359 struct audit_field *f,
360 struct audit_context *ctx,
361 struct audit_names *name)
362{
Eric Paris02d86a52012-01-03 14:23:08 -0500363 switch (f->val) {
Peter Moody4a6633e2011-12-13 16:17:51 -0800364 /* process to file object comparisons */
Eric Paris02d86a52012-01-03 14:23:08 -0500365 case AUDIT_COMPARE_UID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700366 return audit_compare_uid(cred->uid, name, f, ctx);
Eric Parisc9fe6852012-01-03 14:23:08 -0500367 case AUDIT_COMPARE_GID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700368 return audit_compare_gid(cred->gid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800369 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700370 return audit_compare_uid(cred->euid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800371 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700372 return audit_compare_gid(cred->egid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800373 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700374 return audit_compare_uid(tsk->loginuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800375 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700376 return audit_compare_uid(cred->suid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800377 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700378 return audit_compare_gid(cred->sgid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800379 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700380 return audit_compare_uid(cred->fsuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800381 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700382 return audit_compare_gid(cred->fsgid, name, f, ctx);
Peter Moody10d68362012-01-04 15:24:31 -0500383 /* uid comparisons */
384 case AUDIT_COMPARE_UID_TO_AUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700385 return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
Peter Moody10d68362012-01-04 15:24:31 -0500386 case AUDIT_COMPARE_UID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700387 return audit_uid_comparator(cred->uid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500388 case AUDIT_COMPARE_UID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700389 return audit_uid_comparator(cred->uid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500390 case AUDIT_COMPARE_UID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700391 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500392 /* auid comparisons */
393 case AUDIT_COMPARE_AUID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700394 return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500395 case AUDIT_COMPARE_AUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700396 return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500397 case AUDIT_COMPARE_AUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700398 return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500399 /* euid comparisons */
400 case AUDIT_COMPARE_EUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700401 return audit_uid_comparator(cred->euid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500402 case AUDIT_COMPARE_EUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700403 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500404 /* suid comparisons */
405 case AUDIT_COMPARE_SUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700406 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500407 /* gid comparisons */
408 case AUDIT_COMPARE_GID_TO_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700409 return audit_gid_comparator(cred->gid, f->op, cred->egid);
Peter Moody10d68362012-01-04 15:24:31 -0500410 case AUDIT_COMPARE_GID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700411 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500412 case AUDIT_COMPARE_GID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700413 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500414 /* egid comparisons */
415 case AUDIT_COMPARE_EGID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700416 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500417 case AUDIT_COMPARE_EGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700418 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500419 /* sgid comparison */
420 case AUDIT_COMPARE_SGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700421 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
Eric Paris02d86a52012-01-03 14:23:08 -0500422 default:
423 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
424 return 0;
425 }
426 return 0;
427}
428
Amy Griffisf368c07d2006-04-07 16:55:56 -0400429/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/* Compare a task_struct with an audit_rule. Return 1 on match, 0
Tony Jonesf5629882011-04-27 15:10:49 +0200431 * otherwise.
432 *
433 * If task_creation is true, this is an explicit indication that we are
434 * filtering a task rule at task creation time. This and tsk == current are
435 * the only situations where tsk->cred may be accessed without an rcu read lock.
436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500438 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400440 struct audit_names *name,
Tony Jonesf5629882011-04-27 15:10:49 +0200441 enum audit_state *state,
442 bool task_creation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Tony Jonesf5629882011-04-27 15:10:49 +0200444 const struct cred *cred;
Eric Paris5195d8e2012-01-03 14:23:05 -0500445 int i, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600446 u32 sid;
447
Tony Jonesf5629882011-04-27 15:10:49 +0200448 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500451 struct audit_field *f = &rule->fields[i];
Eric Paris5195d8e2012-01-03 14:23:05 -0500452 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int result = 0;
454
Amy Griffis93315ed2006-02-07 12:05:27 -0500455 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500457 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
Al Viro3c662512006-05-06 08:26:27 -0400459 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400460 if (ctx) {
461 if (!ctx->ppid)
462 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400463 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400464 }
Al Viro3c662512006-05-06 08:26:27 -0400465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 case AUDIT_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700467 result = audit_uid_comparator(cred->uid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469 case AUDIT_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700470 result = audit_uid_comparator(cred->euid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 case AUDIT_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700473 result = audit_uid_comparator(cred->suid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case AUDIT_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700476 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
478 case AUDIT_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700479 result = audit_gid_comparator(cred->gid, f->op, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300480 if (f->op == Audit_equal) {
481 if (!result)
482 result = in_group_p(f->gid);
483 } else if (f->op == Audit_not_equal) {
484 if (result)
485 result = !in_group_p(f->gid);
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 break;
488 case AUDIT_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700489 result = audit_gid_comparator(cred->egid, f->op, f->gid);
Matvejchikov Ilya37eebe32011-12-13 23:09:08 +0300490 if (f->op == Audit_equal) {
491 if (!result)
492 result = in_egroup_p(f->gid);
493 } else if (f->op == Audit_not_equal) {
494 if (result)
495 result = !in_egroup_p(f->gid);
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498 case AUDIT_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700499 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 break;
501 case AUDIT_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700502 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500505 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
2fd6f582005-04-29 16:08:28 +0100507 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700508 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500509 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 case AUDIT_EXIT:
513 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500514 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100517 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500518 if (f->val)
519 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100520 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500521 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
524 case AUDIT_DEVMAJOR:
Eric Paris16c174b2012-01-03 14:23:05 -0500525 if (name) {
526 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
527 audit_comparator(MAJOR(name->rdev), f->op, f->val))
528 ++result;
529 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500530 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174b2012-01-03 14:23:05 -0500531 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
532 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ++result;
534 break;
535 }
536 }
537 }
538 break;
539 case AUDIT_DEVMINOR:
Eric Paris16c174b2012-01-03 14:23:05 -0500540 if (name) {
541 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
542 audit_comparator(MINOR(name->rdev), f->op, f->val))
543 ++result;
544 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500545 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174b2012-01-03 14:23:05 -0500546 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
547 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ++result;
549 break;
550 }
551 }
552 }
553 break;
554 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400555 if (name)
Richard Guy Briggsdb510fc2013-07-04 12:56:11 -0400556 result = audit_comparator(name->ino, f->op, f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400557 else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500558 list_for_each_entry(n, &ctx->names_list, list) {
559 if (audit_comparator(n->ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ++result;
561 break;
562 }
563 }
564 }
565 break;
Eric Parisefaffd62012-01-03 14:23:07 -0500566 case AUDIT_OBJ_UID:
567 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700568 result = audit_uid_comparator(name->uid, f->op, f->uid);
Eric Parisefaffd62012-01-03 14:23:07 -0500569 } else if (ctx) {
570 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700571 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
Eric Parisefaffd62012-01-03 14:23:07 -0500572 ++result;
573 break;
574 }
575 }
576 }
577 break;
Eric Paris54d32182012-01-03 14:23:07 -0500578 case AUDIT_OBJ_GID:
579 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700580 result = audit_gid_comparator(name->gid, f->op, f->gid);
Eric Paris54d32182012-01-03 14:23:07 -0500581 } else if (ctx) {
582 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700583 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
Eric Paris54d32182012-01-03 14:23:07 -0500584 ++result;
585 break;
586 }
587 }
588 }
589 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400590 case AUDIT_WATCH:
Eric Parisae7b8f42009-12-17 20:12:04 -0500591 if (name)
592 result = audit_watch_compare(rule->watch, name->ino, name->dev);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400593 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400594 case AUDIT_DIR:
595 if (ctx)
596 result = match_tree_refs(ctx, rule->tree);
597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 case AUDIT_LOGINUID:
599 result = 0;
600 if (ctx)
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700601 result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
Eric W. Biederman780a7652013-04-09 02:22:10 -0700603 case AUDIT_LOGINUID_SET:
604 result = audit_comparator(audit_loginuid_set(tsk), f->op, f->val);
605 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500606 case AUDIT_SUBJ_USER:
607 case AUDIT_SUBJ_ROLE:
608 case AUDIT_SUBJ_TYPE:
609 case AUDIT_SUBJ_SEN:
610 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600611 /* NOTE: this may return negative values indicating
612 a temporary error. We simply treat this as a
613 match for now to avoid losing information that
614 may be wanted. An error message will also be
615 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000616 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400617 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200618 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400619 need_sid = 0;
620 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200621 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600622 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000623 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600624 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400625 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600626 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500627 case AUDIT_OBJ_USER:
628 case AUDIT_OBJ_ROLE:
629 case AUDIT_OBJ_TYPE:
630 case AUDIT_OBJ_LEV_LOW:
631 case AUDIT_OBJ_LEV_HIGH:
632 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
633 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000634 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500635 /* Find files that match */
636 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200637 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500638 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000639 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500640 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500641 list_for_each_entry(n, &ctx->names_list, list) {
642 if (security_audit_rule_match(n->osid, f->type,
643 f->op, f->lsm_rule,
644 ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500645 ++result;
646 break;
647 }
648 }
649 }
650 /* Find ipc objects that match */
Al Viroa33e6752008-12-10 03:40:06 -0500651 if (!ctx || ctx->type != AUDIT_IPC)
652 break;
653 if (security_audit_rule_match(ctx->ipc.osid,
654 f->type, f->op,
655 f->lsm_rule, ctx))
656 ++result;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500657 }
658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 case AUDIT_ARG0:
660 case AUDIT_ARG1:
661 case AUDIT_ARG2:
662 case AUDIT_ARG3:
663 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500664 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400666 case AUDIT_FILTERKEY:
667 /* ignore this field for filtering */
668 result = 1;
669 break;
Al Viro55669bf2006-08-31 19:26:40 -0400670 case AUDIT_PERM:
671 result = audit_match_perm(ctx, f->val);
672 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400673 case AUDIT_FILETYPE:
674 result = audit_match_filetype(ctx, f->val);
675 break;
Eric Paris02d86a52012-01-03 14:23:08 -0500676 case AUDIT_FIELD_COMPARE:
677 result = audit_field_compare(tsk, cred, f, ctx, name);
678 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Tony Jonesf5629882011-04-27 15:10:49 +0200680 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
682 }
Al Viro0590b932008-12-14 23:45:27 -0500683
684 if (ctx) {
685 if (rule->prio <= ctx->prio)
686 return 0;
687 if (rule->filterkey) {
688 kfree(ctx->filterkey);
689 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
690 }
691 ctx->prio = rule->prio;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 switch (rule->action) {
694 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
696 }
697 return 1;
698}
699
700/* At process creation time, we can determine if system-call auditing is
701 * completely disabled for this task. Since we only have the task
702 * structure at this point, we can only check uid and gid.
703 */
Al Viroe048e022008-12-16 03:51:22 -0500704static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct audit_entry *e;
707 enum audit_state state;
708
709 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100710 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Tony Jonesf5629882011-04-27 15:10:49 +0200711 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
712 &state, true)) {
Al Viroe048e022008-12-16 03:51:22 -0500713 if (state == AUDIT_RECORD_CONTEXT)
714 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 rcu_read_unlock();
716 return state;
717 }
718 }
719 rcu_read_unlock();
720 return AUDIT_BUILD_CONTEXT;
721}
722
723/* At syscall entry and exit time, this filter is called if the
724 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100725 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700726 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
728static enum audit_state audit_filter_syscall(struct task_struct *tsk,
729 struct audit_context *ctx,
730 struct list_head *list)
731{
732 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100733 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David Woodhouse351bb722005-07-14 14:40:06 +0100735 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100736 return AUDIT_DISABLED;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100739 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000740 int word = AUDIT_WORD(ctx->major);
741 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100742
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000743 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400744 if ((e->rule.mask[word] & bit) == bit &&
745 audit_filter_rules(tsk, &e->rule, ctx, NULL,
Tony Jonesf5629882011-04-27 15:10:49 +0200746 &state, false)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000747 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500748 ctx->current_state = state;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000749 return state;
750 }
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 rcu_read_unlock();
754 return AUDIT_BUILD_CONTEXT;
755}
756
Eric Paris5195d8e2012-01-03 14:23:05 -0500757/*
758 * Given an audit_name check the inode hash table to see if they match.
759 * Called holding the rcu read lock to protect the use of audit_inode_hash
760 */
761static int audit_filter_inode_name(struct task_struct *tsk,
762 struct audit_names *n,
763 struct audit_context *ctx) {
764 int word, bit;
765 int h = audit_hash_ino((u32)n->ino);
766 struct list_head *list = &audit_inode_hash[h];
767 struct audit_entry *e;
768 enum audit_state state;
769
770 word = AUDIT_WORD(ctx->major);
771 bit = AUDIT_BIT(ctx->major);
772
773 if (list_empty(list))
774 return 0;
775
776 list_for_each_entry_rcu(e, list, list) {
777 if ((e->rule.mask[word] & bit) == bit &&
778 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
779 ctx->current_state = state;
780 return 1;
781 }
782 }
783
784 return 0;
785}
786
787/* At syscall exit time, this filter is called if any audit_names have been
Amy Griffisf368c07d2006-04-07 16:55:56 -0400788 * collected during syscall processing. We only check rules in sublists at hash
Eric Paris5195d8e2012-01-03 14:23:05 -0500789 * buckets applicable to the inode numbers in audit_names.
Amy Griffisf368c07d2006-04-07 16:55:56 -0400790 * Regarding audit_state, same rules apply as for audit_filter_syscall().
791 */
Al Viro0590b932008-12-14 23:45:27 -0500792void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400793{
Eric Paris5195d8e2012-01-03 14:23:05 -0500794 struct audit_names *n;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400795
796 if (audit_pid && tsk->tgid == audit_pid)
Al Viro0590b932008-12-14 23:45:27 -0500797 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400798
799 rcu_read_lock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400800
Eric Paris5195d8e2012-01-03 14:23:05 -0500801 list_for_each_entry(n, &ctx->names_list, list) {
802 if (audit_filter_inode_name(tsk, n, ctx))
803 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400804 }
805 rcu_read_unlock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808static inline struct audit_context *audit_get_context(struct task_struct *tsk,
809 int return_valid,
Paul Moore6d208da2009-04-01 15:47:27 -0400810 long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct audit_context *context = tsk->audit_context;
813
Eric Paris56179a62012-01-03 14:23:06 -0500814 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return NULL;
816 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500817
818 /*
819 * we need to fix up the return code in the audit logs if the actual
820 * return codes are later going to be fixed up by the arch specific
821 * signal handlers
822 *
823 * This is actually a test for:
824 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
825 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
826 *
827 * but is faster than a bunch of ||
828 */
829 if (unlikely(return_code <= -ERESTARTSYS) &&
830 (return_code >= -ERESTART_RESTARTBLOCK) &&
831 (return_code != -ENOIOCTLCMD))
832 context->return_code = -EINTR;
833 else
834 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Al Viro0590b932008-12-14 23:45:27 -0500836 if (context->in_syscall && !context->dummy) {
837 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
838 audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 tsk->audit_context = NULL;
842 return context;
843}
844
845static inline void audit_free_names(struct audit_context *context)
846{
Eric Paris5195d8e2012-01-03 14:23:05 -0500847 struct audit_names *n, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849#if AUDIT_DEBUG == 2
Al Viro0590b932008-12-14 23:45:27 -0500850 if (context->put_count + context->ino_count != context->name_count) {
Eric Paris34c474d2013-04-16 10:17:02 -0400851 int i = 0;
852
Amy Griffis73241cc2005-11-03 16:00:25 +0000853 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 " name_count=%d put_count=%d"
855 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000856 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 context->serial, context->major, context->in_syscall,
858 context->name_count, context->put_count,
859 context->ino_count);
Eric Paris5195d8e2012-01-03 14:23:05 -0500860 list_for_each_entry(n, &context->names_list, list) {
Eric Paris34c474d2013-04-16 10:17:02 -0400861 printk(KERN_ERR "names[%d] = %p = %s\n", i++,
Jeff Layton91a27b22012-10-10 15:25:28 -0400862 n->name, n->name->name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 dump_stack();
865 return;
866 }
867#endif
868#if AUDIT_DEBUG
869 context->put_count = 0;
870 context->ino_count = 0;
871#endif
872
Eric Paris5195d8e2012-01-03 14:23:05 -0500873 list_for_each_entry_safe(n, next, &context->names_list, list) {
874 list_del(&n->list);
875 if (n->name && n->name_put)
Dmitry Monakhov65ada7b2013-04-01 11:00:00 +0400876 final_putname(n->name);
Eric Paris5195d8e2012-01-03 14:23:05 -0500877 if (n->should_free)
878 kfree(n);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -0800881 path_put(&context->pwd);
882 context->pwd.dentry = NULL;
883 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static inline void audit_free_aux(struct audit_context *context)
887{
888 struct audit_aux_data *aux;
889
890 while ((aux = context->aux)) {
891 context->aux = aux->next;
892 kfree(aux);
893 }
Amy Griffise54dc242007-03-29 18:01:04 -0400894 while ((aux = context->aux_pids)) {
895 context->aux_pids = aux->next;
896 kfree(aux);
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static inline struct audit_context *audit_alloc_context(enum audit_state state)
901{
902 struct audit_context *context;
903
Rakib Mullick17c6ee72013-04-07 16:14:18 +0600904 context = kzalloc(sizeof(*context), GFP_KERNEL);
905 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return NULL;
Andrew Mortone2c5adc2013-04-08 14:43:41 -0700907 context->state = state;
908 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Al Viro916d7572009-06-24 00:02:38 -0400909 INIT_LIST_HEAD(&context->killed_trees);
Eric Paris5195d8e2012-01-03 14:23:05 -0500910 INIT_LIST_HEAD(&context->names_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return context;
912}
913
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700914/**
915 * audit_alloc - allocate an audit context block for a task
916 * @tsk: task
917 *
918 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * if necessary. Doing so turns on system call auditing for the
920 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700921 * needed.
922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923int audit_alloc(struct task_struct *tsk)
924{
925 struct audit_context *context;
926 enum audit_state state;
Al Viroe048e022008-12-16 03:51:22 -0500927 char *key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Eric Parisb593d382008-01-08 17:38:31 -0500929 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0; /* Return if not auditing. */
931
Al Viroe048e022008-12-16 03:51:22 -0500932 state = audit_filter_task(tsk, &key);
Oleg Nesterovd48d8052013-09-15 19:11:09 +0200933 if (state == AUDIT_DISABLED) {
934 clear_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
Oleg Nesterovd48d8052013-09-15 19:11:09 +0200936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (!(context = audit_alloc_context(state))) {
Al Viroe048e022008-12-16 03:51:22 -0500939 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 audit_log_lost("out of memory in audit_alloc");
941 return -ENOMEM;
942 }
Al Viroe048e022008-12-16 03:51:22 -0500943 context->filterkey = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 tsk->audit_context = context;
946 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
947 return 0;
948}
949
950static inline void audit_free_context(struct audit_context *context)
951{
Al Viroc62d7732012-10-20 15:07:18 -0400952 audit_free_names(context);
953 unroll_tree_refs(context, NULL, 0);
954 free_tree_refs(context);
955 audit_free_aux(context);
956 kfree(context->filterkey);
957 kfree(context->sockaddr);
958 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Amy Griffise54dc242007-03-29 18:01:04 -0400961static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800962 kuid_t auid, kuid_t uid, unsigned int sessionid,
Eric Paris4746ec52008-01-08 10:06:53 -0500963 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400964{
965 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200966 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -0400967 u32 len;
968 int rc = 0;
969
970 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
971 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -0500972 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -0400973
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700974 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
975 from_kuid(&init_user_ns, auid),
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800976 from_kuid(&init_user_ns, uid), sessionid);
Eric Parisad395ab2012-10-23 08:58:35 -0400977 if (sid) {
978 if (security_secid_to_secctx(sid, &ctx, &len)) {
979 audit_log_format(ab, " obj=(none)");
980 rc = 1;
981 } else {
982 audit_log_format(ab, " obj=%s", ctx);
983 security_release_secctx(ctx, len);
984 }
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200985 }
Eric Parisc2a77802008-01-07 13:40:17 -0500986 audit_log_format(ab, " ocomm=");
987 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -0400988 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -0400989
990 return rc;
991}
992
Eric Parisde6bbd12008-01-07 14:31:58 -0500993/*
994 * to_send and len_sent accounting are very loose estimates. We aren't
995 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300996 * within about 500 bytes (next page boundary)
Eric Parisde6bbd12008-01-07 14:31:58 -0500997 *
998 * why snprintf? an int is up to 12 digits long. if we just assumed when
999 * logging that a[%d]= was going to be 16 characters long we would be wasting
1000 * space in every audit message. In one 7500 byte message we can log up to
1001 * about 1000 min size arguments. That comes down to about 50% waste of space
1002 * if we didn't do the snprintf to find out how long arg_num_len was.
1003 */
1004static int audit_log_single_execve_arg(struct audit_context *context,
1005 struct audit_buffer **ab,
1006 int arg_num,
1007 size_t *len_sent,
1008 const char __user *p,
1009 char *buf)
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001010{
Eric Parisde6bbd12008-01-07 14:31:58 -05001011 char arg_num_len_buf[12];
1012 const char __user *tmp_p = p;
Eric Parisb87ce6e2009-06-11 14:31:34 -04001013 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1014 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
Eric Parisde6bbd12008-01-07 14:31:58 -05001015 size_t len, len_left, to_send;
1016 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1017 unsigned int i, has_cntl = 0, too_long = 0;
1018 int ret;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001019
Eric Parisde6bbd12008-01-07 14:31:58 -05001020 /* strnlen_user includes the null we don't want to send */
1021 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001022
Eric Parisde6bbd12008-01-07 14:31:58 -05001023 /*
1024 * We just created this mm, if we can't find the strings
1025 * we just copied into it something is _very_ wrong. Similar
1026 * for strings that are too long, we should not have created
1027 * any.
1028 */
Eric Parisb0abcfc2008-02-18 18:23:16 -05001029 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001030 WARN_ON(1);
1031 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001032 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001033 }
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001034
Eric Parisde6bbd12008-01-07 14:31:58 -05001035 /* walk the whole argument looking for non-ascii chars */
1036 do {
1037 if (len_left > MAX_EXECVE_AUDIT_LEN)
1038 to_send = MAX_EXECVE_AUDIT_LEN;
1039 else
1040 to_send = len_left;
1041 ret = copy_from_user(buf, tmp_p, to_send);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001042 /*
1043 * There is no reason for this copy to be short. We just
1044 * copied them here, and the mm hasn't been exposed to user-
1045 * space yet.
1046 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001047 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001048 WARN_ON(1);
1049 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001050 return -1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001051 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001052 buf[to_send] = '\0';
1053 has_cntl = audit_string_contains_control(buf, to_send);
1054 if (has_cntl) {
1055 /*
1056 * hex messages get logged as 2 bytes, so we can only
1057 * send half as much in each message
1058 */
1059 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1060 break;
1061 }
1062 len_left -= to_send;
1063 tmp_p += to_send;
1064 } while (len_left > 0);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001065
Eric Parisde6bbd12008-01-07 14:31:58 -05001066 len_left = len;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001067
Eric Parisde6bbd12008-01-07 14:31:58 -05001068 if (len > max_execve_audit_len)
1069 too_long = 1;
1070
1071 /* rewalk the argument actually logging the message */
1072 for (i = 0; len_left > 0; i++) {
1073 int room_left;
1074
1075 if (len_left > max_execve_audit_len)
1076 to_send = max_execve_audit_len;
1077 else
1078 to_send = len_left;
1079
1080 /* do we have space left to send this argument in this ab? */
1081 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1082 if (has_cntl)
1083 room_left -= (to_send * 2);
1084 else
1085 room_left -= to_send;
1086 if (room_left < 0) {
1087 *len_sent = 0;
1088 audit_log_end(*ab);
1089 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1090 if (!*ab)
1091 return 0;
1092 }
1093
1094 /*
1095 * first record needs to say how long the original string was
1096 * so we can be sure nothing was lost.
1097 */
1098 if ((i == 0) && (too_long))
Jiri Pirkoca96a892009-01-09 16:44:16 +01001099 audit_log_format(*ab, " a%d_len=%zu", arg_num,
Eric Parisde6bbd12008-01-07 14:31:58 -05001100 has_cntl ? 2*len : len);
1101
1102 /*
1103 * normally arguments are small enough to fit and we already
1104 * filled buf above when we checked for control characters
1105 * so don't bother with another copy_from_user
1106 */
1107 if (len >= max_execve_audit_len)
1108 ret = copy_from_user(buf, p, to_send);
1109 else
1110 ret = 0;
1111 if (ret) {
1112 WARN_ON(1);
1113 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001114 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001115 }
1116 buf[to_send] = '\0';
1117
1118 /* actually log it */
Jiri Pirkoca96a892009-01-09 16:44:16 +01001119 audit_log_format(*ab, " a%d", arg_num);
Eric Parisde6bbd12008-01-07 14:31:58 -05001120 if (too_long)
1121 audit_log_format(*ab, "[%d]", i);
1122 audit_log_format(*ab, "=");
1123 if (has_cntl)
Eric Parisb556f8a2008-04-18 10:12:59 -04001124 audit_log_n_hex(*ab, buf, to_send);
Eric Parisde6bbd12008-01-07 14:31:58 -05001125 else
Eric Paris9d960982009-06-11 14:31:37 -04001126 audit_log_string(*ab, buf);
Eric Parisde6bbd12008-01-07 14:31:58 -05001127
1128 p += to_send;
1129 len_left -= to_send;
1130 *len_sent += arg_num_len;
1131 if (has_cntl)
1132 *len_sent += to_send * 2;
1133 else
1134 *len_sent += to_send;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001135 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001136 /* include the null we didn't log */
1137 return len + 1;
1138}
1139
1140static void audit_log_execve_info(struct audit_context *context,
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001141 struct audit_buffer **ab)
Eric Parisde6bbd12008-01-07 14:31:58 -05001142{
Xi Wang5afb8a32011-12-20 18:39:41 -05001143 int i, len;
1144 size_t len_sent = 0;
Eric Parisde6bbd12008-01-07 14:31:58 -05001145 const char __user *p;
1146 char *buf;
1147
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001148 if (context->execve.mm != current->mm)
Eric Parisde6bbd12008-01-07 14:31:58 -05001149 return; /* execve failed, no additional info */
1150
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001151 p = (const char __user *)current->mm->arg_start;
Eric Parisde6bbd12008-01-07 14:31:58 -05001152
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001153 audit_log_format(*ab, "argc=%d", context->execve.argc);
Eric Parisde6bbd12008-01-07 14:31:58 -05001154
1155 /*
1156 * we need some kernel buffer to hold the userspace args. Just
1157 * allocate one big one rather than allocating one of the right size
1158 * for every single argument inside audit_log_single_execve_arg()
1159 * should be <8k allocation so should be pretty safe.
1160 */
1161 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1162 if (!buf) {
1163 audit_panic("out of memory for argv string\n");
1164 return;
1165 }
1166
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001167 for (i = 0; i < context->execve.argc; i++) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001168 len = audit_log_single_execve_arg(context, ab, i,
1169 &len_sent, p, buf);
1170 if (len <= 0)
1171 break;
1172 p += len;
1173 }
1174 kfree(buf);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001175}
1176
Al Viroa33e6752008-12-10 03:40:06 -05001177static void show_special(struct audit_context *context, int *call_panic)
Al Virof3298dc2008-12-10 03:16:51 -05001178{
1179 struct audit_buffer *ab;
1180 int i;
1181
1182 ab = audit_log_start(context, GFP_KERNEL, context->type);
1183 if (!ab)
1184 return;
1185
1186 switch (context->type) {
1187 case AUDIT_SOCKETCALL: {
1188 int nargs = context->socketcall.nargs;
1189 audit_log_format(ab, "nargs=%d", nargs);
1190 for (i = 0; i < nargs; i++)
1191 audit_log_format(ab, " a%d=%lx", i,
1192 context->socketcall.args[i]);
1193 break; }
Al Viroa33e6752008-12-10 03:40:06 -05001194 case AUDIT_IPC: {
1195 u32 osid = context->ipc.osid;
1196
Al Viro2570ebb2011-07-27 14:03:22 -04001197 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001198 from_kuid(&init_user_ns, context->ipc.uid),
1199 from_kgid(&init_user_ns, context->ipc.gid),
1200 context->ipc.mode);
Al Viroa33e6752008-12-10 03:40:06 -05001201 if (osid) {
1202 char *ctx = NULL;
1203 u32 len;
1204 if (security_secid_to_secctx(osid, &ctx, &len)) {
1205 audit_log_format(ab, " osid=%u", osid);
1206 *call_panic = 1;
1207 } else {
1208 audit_log_format(ab, " obj=%s", ctx);
1209 security_release_secctx(ctx, len);
1210 }
1211 }
Al Viroe816f372008-12-10 03:47:15 -05001212 if (context->ipc.has_perm) {
1213 audit_log_end(ab);
1214 ab = audit_log_start(context, GFP_KERNEL,
1215 AUDIT_IPC_SET_PERM);
Kees Cook0644ec02013-01-11 14:32:07 -08001216 if (unlikely(!ab))
1217 return;
Al Viroe816f372008-12-10 03:47:15 -05001218 audit_log_format(ab,
Al Viro2570ebb2011-07-27 14:03:22 -04001219 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
Al Viroe816f372008-12-10 03:47:15 -05001220 context->ipc.qbytes,
1221 context->ipc.perm_uid,
1222 context->ipc.perm_gid,
1223 context->ipc.perm_mode);
Al Viroe816f372008-12-10 03:47:15 -05001224 }
Al Viroa33e6752008-12-10 03:40:06 -05001225 break; }
Al Viro564f6992008-12-14 04:02:26 -05001226 case AUDIT_MQ_OPEN: {
1227 audit_log_format(ab,
Al Virodf0a4282011-07-26 05:26:10 -04001228 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
Al Viro564f6992008-12-14 04:02:26 -05001229 "mq_msgsize=%ld mq_curmsgs=%ld",
1230 context->mq_open.oflag, context->mq_open.mode,
1231 context->mq_open.attr.mq_flags,
1232 context->mq_open.attr.mq_maxmsg,
1233 context->mq_open.attr.mq_msgsize,
1234 context->mq_open.attr.mq_curmsgs);
1235 break; }
Al Viroc32c8af2008-12-14 03:46:48 -05001236 case AUDIT_MQ_SENDRECV: {
1237 audit_log_format(ab,
1238 "mqdes=%d msg_len=%zd msg_prio=%u "
1239 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1240 context->mq_sendrecv.mqdes,
1241 context->mq_sendrecv.msg_len,
1242 context->mq_sendrecv.msg_prio,
1243 context->mq_sendrecv.abs_timeout.tv_sec,
1244 context->mq_sendrecv.abs_timeout.tv_nsec);
1245 break; }
Al Viro20114f72008-12-10 07:16:12 -05001246 case AUDIT_MQ_NOTIFY: {
1247 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1248 context->mq_notify.mqdes,
1249 context->mq_notify.sigev_signo);
1250 break; }
Al Viro73929062008-12-10 06:58:59 -05001251 case AUDIT_MQ_GETSETATTR: {
1252 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1253 audit_log_format(ab,
1254 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1255 "mq_curmsgs=%ld ",
1256 context->mq_getsetattr.mqdes,
1257 attr->mq_flags, attr->mq_maxmsg,
1258 attr->mq_msgsize, attr->mq_curmsgs);
1259 break; }
Al Viro57f71a02009-01-04 14:52:57 -05001260 case AUDIT_CAPSET: {
1261 audit_log_format(ab, "pid=%d", context->capset.pid);
1262 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1263 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1264 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1265 break; }
Al Viro120a7952010-10-30 02:54:44 -04001266 case AUDIT_MMAP: {
1267 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1268 context->mmap.flags);
1269 break; }
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04001270 case AUDIT_EXECVE: {
1271 audit_log_execve_info(context, &ab);
1272 break; }
Al Virof3298dc2008-12-10 03:16:51 -05001273 }
1274 audit_log_end(ab);
1275}
1276
Al Viroe4951492006-03-29 20:17:10 -05001277static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001279 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001281 struct audit_aux_data *aux;
Eric Paris5195d8e2012-01-03 14:23:05 -05001282 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Al Viroe4951492006-03-29 20:17:10 -05001284 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001285 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001286
1287 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (!ab)
1289 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001290 audit_log_format(ab, "arch=%x syscall=%d",
1291 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (context->personality != PER_LINUX)
1293 audit_log_format(ab, " per=%lx", context->personality);
1294 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001295 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001296 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1297 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 audit_log_format(ab,
Peter Moodye23eb922012-06-14 10:04:35 -07001300 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1301 context->argv[0],
1302 context->argv[1],
1303 context->argv[2],
1304 context->argv[3],
1305 context->name_count);
Alan Coxeb84a202006-09-29 02:01:41 -07001306
Al Viroe4951492006-03-29 20:17:10 -05001307 audit_log_task_info(ab, tsk);
Eric Paris9d960982009-06-11 14:31:37 -04001308 audit_log_key(ab, context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
David Woodhouse7551ced2005-05-26 12:04:57 +01001311 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001312
Al Viroe4951492006-03-29 20:17:10 -05001313 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (!ab)
1315 continue; /* audit_panic has been called */
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001318
Eric Paris3fc689e2008-11-11 21:48:18 +11001319 case AUDIT_BPRM_FCAPS: {
1320 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1321 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1322 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1323 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1324 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1325 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1326 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1327 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1328 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1329 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1330 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1331 break; }
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336
Al Virof3298dc2008-12-10 03:16:51 -05001337 if (context->type)
Al Viroa33e6752008-12-10 03:40:06 -05001338 show_special(context, &call_panic);
Al Virof3298dc2008-12-10 03:16:51 -05001339
Al Viro157cf642008-12-14 04:57:47 -05001340 if (context->fds[0] >= 0) {
1341 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1342 if (ab) {
1343 audit_log_format(ab, "fd0=%d fd1=%d",
1344 context->fds[0], context->fds[1]);
1345 audit_log_end(ab);
1346 }
1347 }
1348
Al Viro4f6b4342008-12-09 19:50:34 -05001349 if (context->sockaddr_len) {
1350 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1351 if (ab) {
1352 audit_log_format(ab, "saddr=");
1353 audit_log_n_hex(ab, (void *)context->sockaddr,
1354 context->sockaddr_len);
1355 audit_log_end(ab);
1356 }
1357 }
1358
Amy Griffise54dc242007-03-29 18:01:04 -04001359 for (aux = context->aux_pids; aux; aux = aux->next) {
1360 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001361
1362 for (i = 0; i < axs->pid_count; i++)
1363 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001364 axs->target_auid[i],
1365 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001366 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001367 axs->target_sid[i],
1368 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001369 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001370 }
1371
Amy Griffise54dc242007-03-29 18:01:04 -04001372 if (context->target_pid &&
1373 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001374 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001375 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001376 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001377 call_panic = 1;
1378
Jan Blunck44707fd2008-02-14 19:38:33 -08001379 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001380 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001381 if (ab) {
Kees Cookc158a352012-01-06 14:07:10 -08001382 audit_log_d_path(ab, " cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001383 audit_log_end(ab);
1384 }
1385 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001386
Eric Paris5195d8e2012-01-03 14:23:05 -05001387 i = 0;
Jeff Layton79f65302013-07-08 15:59:36 -07001388 list_for_each_entry(n, &context->names_list, list) {
1389 if (n->hidden)
1390 continue;
Eric Parisb24a30a2013-04-30 15:30:32 -04001391 audit_log_name(context, n, NULL, i++, &call_panic);
Jeff Layton79f65302013-07-08 15:59:36 -07001392 }
Eric Parisc0641f22008-01-07 13:49:15 -05001393
1394 /* Send end of event record to help user space know we are finished */
1395 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1396 if (ab)
1397 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001398 if (call_panic)
1399 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001402/**
1403 * audit_free - free a per-task audit context
1404 * @tsk: task whose audit context block to free
1405 *
Al Virofa84cb92006-03-29 20:30:19 -05001406 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001407 */
Eric Parisa4ff8db2012-01-03 14:23:07 -05001408void __audit_free(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 struct audit_context *context;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 context = audit_get_context(tsk, 0, 0);
Eric Paris56179a62012-01-03 14:23:06 -05001413 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return;
1415
1416 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001417 * function (e.g., exit_group), then free context block.
1418 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001419 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001420 /* that can happen only if we are called from do_exit() */
Al Viro0590b932008-12-14 23:45:27 -05001421 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001422 audit_log_exit(context, tsk);
Al Viro916d7572009-06-24 00:02:38 -04001423 if (!list_empty(&context->killed_trees))
1424 audit_kill_trees(&context->killed_trees);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 audit_free_context(context);
1427}
1428
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001429/**
1430 * audit_syscall_entry - fill in an audit record at syscall entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001431 * @arch: architecture type
1432 * @major: major syscall type (function)
1433 * @a1: additional syscall register 1
1434 * @a2: additional syscall register 2
1435 * @a3: additional syscall register 3
1436 * @a4: additional syscall register 4
1437 *
1438 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 * audit context was created when the task was created and the state or
1440 * filters demand the audit context be built. If the state from the
1441 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1442 * then the record will be written at syscall exit time (otherwise, it
1443 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001444 * be written).
1445 */
Eric Parisb05d8442012-01-03 14:23:06 -05001446void __audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 unsigned long a1, unsigned long a2,
1448 unsigned long a3, unsigned long a4)
1449{
Al Viro5411be52006-03-29 20:23:36 -05001450 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 struct audit_context *context = tsk->audit_context;
1452 enum audit_state state;
1453
Eric Paris56179a62012-01-03 14:23:06 -05001454 if (!context)
Roland McGrath86a1c342008-06-23 15:37:04 -07001455 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 BUG_ON(context->in_syscall || context->name_count);
1458
1459 if (!audit_enabled)
1460 return;
1461
2fd6f582005-04-29 16:08:28 +01001462 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 context->major = major;
1464 context->argv[0] = a1;
1465 context->argv[1] = a2;
1466 context->argv[2] = a3;
1467 context->argv[3] = a4;
1468
1469 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001470 context->dummy = !audit_n_rules;
Al Viro0590b932008-12-14 23:45:27 -05001471 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1472 context->prio = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001473 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Al Viro0590b932008-12-14 23:45:27 -05001474 }
Eric Paris56179a62012-01-03 14:23:06 -05001475 if (state == AUDIT_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return;
1477
David Woodhousece625a82005-07-18 14:24:46 -04001478 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 context->ctime = CURRENT_TIME;
1480 context->in_syscall = 1;
Al Viro0590b932008-12-14 23:45:27 -05001481 context->current_state = state;
Alexander Viro419c58f2006-09-29 00:08:50 -04001482 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001485/**
1486 * audit_syscall_exit - deallocate audit context after a system call
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001487 * @success: success value of the syscall
1488 * @return_code: return value of the syscall
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001489 *
1490 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001492 * filtering, or because some other part of the kernel wrote an audit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001494 * free the names stored from getname().
1495 */
Eric Parisd7e75282012-01-03 14:23:06 -05001496void __audit_syscall_exit(int success, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Al Viro5411be52006-03-29 20:23:36 -05001498 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct audit_context *context;
1500
Eric Parisd7e75282012-01-03 14:23:06 -05001501 if (success)
1502 success = AUDITSC_SUCCESS;
1503 else
1504 success = AUDITSC_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Eric Parisd7e75282012-01-03 14:23:06 -05001506 context = audit_get_context(tsk, success, return_code);
Eric Paris56179a62012-01-03 14:23:06 -05001507 if (!context)
Al Viro97e94c42006-03-29 20:26:24 -05001508 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Al Viro0590b932008-12-14 23:45:27 -05001510 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001511 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 context->in_syscall = 0;
Al Viro0590b932008-12-14 23:45:27 -05001514 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
2fd6f582005-04-29 16:08:28 +01001515
Al Viro916d7572009-06-24 00:02:38 -04001516 if (!list_empty(&context->killed_trees))
1517 audit_kill_trees(&context->killed_trees);
1518
Al Viroc62d7732012-10-20 15:07:18 -04001519 audit_free_names(context);
1520 unroll_tree_refs(context, NULL, 0);
1521 audit_free_aux(context);
1522 context->aux = NULL;
1523 context->aux_pids = NULL;
1524 context->target_pid = 0;
1525 context->target_sid = 0;
1526 context->sockaddr_len = 0;
1527 context->type = 0;
1528 context->fds[0] = -1;
1529 if (context->state != AUDIT_RECORD_CONTEXT) {
1530 kfree(context->filterkey);
1531 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
Al Viroc62d7732012-10-20 15:07:18 -04001533 tsk->audit_context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Al Viro74c3cbe2007-07-22 08:04:18 -04001536static inline void handle_one(const struct inode *inode)
1537{
1538#ifdef CONFIG_AUDIT_TREE
1539 struct audit_context *context;
1540 struct audit_tree_refs *p;
1541 struct audit_chunk *chunk;
1542 int count;
Eric Parise61ce862009-12-17 21:24:24 -05001543 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
Al Viro74c3cbe2007-07-22 08:04:18 -04001544 return;
1545 context = current->audit_context;
1546 p = context->trees;
1547 count = context->tree_count;
1548 rcu_read_lock();
1549 chunk = audit_tree_lookup(inode);
1550 rcu_read_unlock();
1551 if (!chunk)
1552 return;
1553 if (likely(put_tree_ref(context, chunk)))
1554 return;
1555 if (unlikely(!grow_tree_refs(context))) {
Eric Paris436c4052008-04-18 10:01:04 -04001556 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001557 audit_set_auditable(context);
1558 audit_put_chunk(chunk);
1559 unroll_tree_refs(context, p, count);
1560 return;
1561 }
1562 put_tree_ref(context, chunk);
1563#endif
1564}
1565
1566static void handle_path(const struct dentry *dentry)
1567{
1568#ifdef CONFIG_AUDIT_TREE
1569 struct audit_context *context;
1570 struct audit_tree_refs *p;
1571 const struct dentry *d, *parent;
1572 struct audit_chunk *drop;
1573 unsigned long seq;
1574 int count;
1575
1576 context = current->audit_context;
1577 p = context->trees;
1578 count = context->tree_count;
1579retry:
1580 drop = NULL;
1581 d = dentry;
1582 rcu_read_lock();
1583 seq = read_seqbegin(&rename_lock);
1584 for(;;) {
1585 struct inode *inode = d->d_inode;
Eric Parise61ce862009-12-17 21:24:24 -05001586 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
Al Viro74c3cbe2007-07-22 08:04:18 -04001587 struct audit_chunk *chunk;
1588 chunk = audit_tree_lookup(inode);
1589 if (chunk) {
1590 if (unlikely(!put_tree_ref(context, chunk))) {
1591 drop = chunk;
1592 break;
1593 }
1594 }
1595 }
1596 parent = d->d_parent;
1597 if (parent == d)
1598 break;
1599 d = parent;
1600 }
1601 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1602 rcu_read_unlock();
1603 if (!drop) {
1604 /* just a race with rename */
1605 unroll_tree_refs(context, p, count);
1606 goto retry;
1607 }
1608 audit_put_chunk(drop);
1609 if (grow_tree_refs(context)) {
1610 /* OK, got more space */
1611 unroll_tree_refs(context, p, count);
1612 goto retry;
1613 }
1614 /* too bad */
1615 printk(KERN_WARNING
Eric Paris436c4052008-04-18 10:01:04 -04001616 "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001617 unroll_tree_refs(context, p, count);
1618 audit_set_auditable(context);
1619 return;
1620 }
1621 rcu_read_unlock();
1622#endif
1623}
1624
Jeff Layton78e2e802012-10-10 15:25:22 -04001625static struct audit_names *audit_alloc_name(struct audit_context *context,
1626 unsigned char type)
Eric Paris5195d8e2012-01-03 14:23:05 -05001627{
1628 struct audit_names *aname;
1629
1630 if (context->name_count < AUDIT_NAMES) {
1631 aname = &context->preallocated_names[context->name_count];
1632 memset(aname, 0, sizeof(*aname));
1633 } else {
1634 aname = kzalloc(sizeof(*aname), GFP_NOFS);
1635 if (!aname)
1636 return NULL;
1637 aname->should_free = true;
1638 }
1639
1640 aname->ino = (unsigned long)-1;
Jeff Layton78e2e802012-10-10 15:25:22 -04001641 aname->type = type;
Eric Paris5195d8e2012-01-03 14:23:05 -05001642 list_add_tail(&aname->list, &context->names_list);
1643
1644 context->name_count++;
1645#if AUDIT_DEBUG
1646 context->ino_count++;
1647#endif
1648 return aname;
1649}
1650
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001651/**
Jeff Layton7ac86262012-10-10 15:25:28 -04001652 * audit_reusename - fill out filename with info from existing entry
1653 * @uptr: userland ptr to pathname
1654 *
1655 * Search the audit_names list for the current audit context. If there is an
1656 * existing entry with a matching "uptr" then return the filename
1657 * associated with that audit_name. If not, return NULL.
1658 */
1659struct filename *
1660__audit_reusename(const __user char *uptr)
1661{
1662 struct audit_context *context = current->audit_context;
1663 struct audit_names *n;
1664
1665 list_for_each_entry(n, &context->names_list, list) {
1666 if (!n->name)
1667 continue;
1668 if (n->name->uptr == uptr)
1669 return n->name;
1670 }
1671 return NULL;
1672}
1673
1674/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001675 * audit_getname - add a name to the list
1676 * @name: name to add
1677 *
1678 * Add a name to the list of audit names for this context.
1679 * Called from fs/namei.c:getname().
1680 */
Jeff Layton91a27b22012-10-10 15:25:28 -04001681void __audit_getname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 struct audit_context *context = current->audit_context;
Eric Paris5195d8e2012-01-03 14:23:05 -05001684 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (!context->in_syscall) {
1687#if AUDIT_DEBUG == 2
1688 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1689 __FILE__, __LINE__, context->serial, name);
1690 dump_stack();
1691#endif
1692 return;
1693 }
Eric Paris5195d8e2012-01-03 14:23:05 -05001694
Jeff Layton91a27b22012-10-10 15:25:28 -04001695#if AUDIT_DEBUG
1696 /* The filename _must_ have a populated ->name */
1697 BUG_ON(!name->name);
1698#endif
1699
Jeff Layton78e2e802012-10-10 15:25:22 -04001700 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
Eric Paris5195d8e2012-01-03 14:23:05 -05001701 if (!n)
1702 return;
1703
1704 n->name = name;
1705 n->name_len = AUDIT_NAME_FULL;
1706 n->name_put = true;
Jeff Laytonadb5c242012-10-10 16:43:13 -04001707 name->aname = n;
Eric Paris5195d8e2012-01-03 14:23:05 -05001708
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02001709 if (!context->pwd.dentry)
1710 get_fs_pwd(current->fs, &context->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001713/* audit_putname - intercept a putname request
1714 * @name: name to intercept and delay for putname
1715 *
1716 * If we have stored the name from getname in the audit context,
1717 * then we delay the putname until syscall exit.
1718 * Called from include/linux/fs.h:putname().
1719 */
Jeff Layton91a27b22012-10-10 15:25:28 -04001720void audit_putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 struct audit_context *context = current->audit_context;
1723
1724 BUG_ON(!context);
1725 if (!context->in_syscall) {
1726#if AUDIT_DEBUG == 2
Dmitry Monakhov65ada7b2013-04-01 11:00:00 +04001727 printk(KERN_ERR "%s:%d(:%d): final_putname(%p)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 __FILE__, __LINE__, context->serial, name);
1729 if (context->name_count) {
Eric Paris5195d8e2012-01-03 14:23:05 -05001730 struct audit_names *n;
Eric Paris34c474d2013-04-16 10:17:02 -04001731 int i = 0;
Eric Paris5195d8e2012-01-03 14:23:05 -05001732
1733 list_for_each_entry(n, &context->names_list, list)
Eric Paris34c474d2013-04-16 10:17:02 -04001734 printk(KERN_ERR "name[%d] = %p = %s\n", i++,
Jeff Layton91a27b22012-10-10 15:25:28 -04001735 n->name, n->name->name ?: "(null)");
Eric Paris5195d8e2012-01-03 14:23:05 -05001736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737#endif
Dmitry Monakhov65ada7b2013-04-01 11:00:00 +04001738 final_putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740#if AUDIT_DEBUG
1741 else {
1742 ++context->put_count;
1743 if (context->put_count > context->name_count) {
1744 printk(KERN_ERR "%s:%d(:%d): major=%d"
1745 " in_syscall=%d putname(%p) name_count=%d"
1746 " put_count=%d\n",
1747 __FILE__, __LINE__,
1748 context->serial, context->major,
Jeff Layton91a27b22012-10-10 15:25:28 -04001749 context->in_syscall, name->name,
1750 context->name_count, context->put_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 dump_stack();
1752 }
1753 }
1754#endif
1755}
1756
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001757/**
Jeff Laytonbfcec702012-10-10 15:25:23 -04001758 * __audit_inode - store the inode and device from a lookup
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001759 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001760 * @dentry: dentry being audited
Jeff Layton79f65302013-07-08 15:59:36 -07001761 * @flags: attributes for this particular entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001762 */
Jeff Laytonadb5c242012-10-10 16:43:13 -04001763void __audit_inode(struct filename *name, const struct dentry *dentry,
Jeff Layton79f65302013-07-08 15:59:36 -07001764 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04001767 const struct inode *inode = dentry->d_inode;
Eric Paris5195d8e2012-01-03 14:23:05 -05001768 struct audit_names *n;
Jeff Layton79f65302013-07-08 15:59:36 -07001769 bool parent = flags & AUDIT_INODE_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 if (!context->in_syscall)
1772 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05001773
Jeff Layton9cec9d62012-10-10 15:25:21 -04001774 if (!name)
1775 goto out_alloc;
1776
Jeff Laytonadb5c242012-10-10 16:43:13 -04001777#if AUDIT_DEBUG
1778 /* The struct filename _must_ have a populated ->name */
1779 BUG_ON(!name->name);
1780#endif
1781 /*
1782 * If we have a pointer to an audit_names entry already, then we can
1783 * just use it directly if the type is correct.
1784 */
1785 n = name->aname;
1786 if (n) {
1787 if (parent) {
1788 if (n->type == AUDIT_TYPE_PARENT ||
1789 n->type == AUDIT_TYPE_UNKNOWN)
1790 goto out;
1791 } else {
1792 if (n->type != AUDIT_TYPE_PARENT)
1793 goto out;
1794 }
1795 }
1796
Eric Paris5195d8e2012-01-03 14:23:05 -05001797 list_for_each_entry_reverse(n, &context->names_list, list) {
Jeff Laytonbfcec702012-10-10 15:25:23 -04001798 /* does the name pointer match? */
Jeff Laytonadb5c242012-10-10 16:43:13 -04001799 if (!n->name || n->name->name != name->name)
Jeff Laytonbfcec702012-10-10 15:25:23 -04001800 continue;
1801
1802 /* match the correct record type */
1803 if (parent) {
1804 if (n->type == AUDIT_TYPE_PARENT ||
1805 n->type == AUDIT_TYPE_UNKNOWN)
1806 goto out;
1807 } else {
1808 if (n->type != AUDIT_TYPE_PARENT)
1809 goto out;
1810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
Eric Paris5195d8e2012-01-03 14:23:05 -05001812
Jeff Layton9cec9d62012-10-10 15:25:21 -04001813out_alloc:
Jeff Laytonbfcec702012-10-10 15:25:23 -04001814 /* unable to find the name from a previous getname(). Allocate a new
1815 * anonymous entry.
1816 */
Jeff Layton78e2e802012-10-10 15:25:22 -04001817 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
Eric Paris5195d8e2012-01-03 14:23:05 -05001818 if (!n)
1819 return;
1820out:
Jeff Laytonbfcec702012-10-10 15:25:23 -04001821 if (parent) {
Jeff Layton91a27b22012-10-10 15:25:28 -04001822 n->name_len = n->name ? parent_len(n->name->name) : AUDIT_NAME_FULL;
Jeff Laytonbfcec702012-10-10 15:25:23 -04001823 n->type = AUDIT_TYPE_PARENT;
Jeff Layton79f65302013-07-08 15:59:36 -07001824 if (flags & AUDIT_INODE_HIDDEN)
1825 n->hidden = true;
Jeff Laytonbfcec702012-10-10 15:25:23 -04001826 } else {
1827 n->name_len = AUDIT_NAME_FULL;
1828 n->type = AUDIT_TYPE_NORMAL;
1829 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001830 handle_path(dentry);
Eric Paris5195d8e2012-01-03 14:23:05 -05001831 audit_copy_inode(n, dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
Amy Griffis73241cc2005-11-03 16:00:25 +00001834/**
Jeff Laytonc43a25a2012-10-10 15:25:21 -04001835 * __audit_inode_child - collect inode info for created/removed objects
Amy Griffis73d3ec52006-07-13 13:16:39 -04001836 * @parent: inode of dentry parent
Jeff Laytonc43a25a2012-10-10 15:25:21 -04001837 * @dentry: dentry being audited
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001838 * @type: AUDIT_TYPE_* value that we're looking for
Amy Griffis73241cc2005-11-03 16:00:25 +00001839 *
1840 * For syscalls that create or remove filesystem objects, audit_inode
1841 * can only collect information for the filesystem object's parent.
1842 * This call updates the audit context with the child's information.
1843 * Syscalls that create a new filesystem object must be hooked after
1844 * the object is created. Syscalls that remove a filesystem object
1845 * must be hooked prior, in order to capture the target inode during
1846 * unsuccessful attempts.
1847 */
Jeff Laytonc43a25a2012-10-10 15:25:21 -04001848void __audit_inode_child(const struct inode *parent,
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001849 const struct dentry *dentry,
1850 const unsigned char type)
Amy Griffis73241cc2005-11-03 16:00:25 +00001851{
Amy Griffis73241cc2005-11-03 16:00:25 +00001852 struct audit_context *context = current->audit_context;
Al Viro5a190ae2007-06-07 12:19:32 -04001853 const struct inode *inode = dentry->d_inode;
Al Virocccc6bb2009-12-25 05:07:33 -05001854 const char *dname = dentry->d_name.name;
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001855 struct audit_names *n, *found_parent = NULL, *found_child = NULL;
Amy Griffis73241cc2005-11-03 16:00:25 +00001856
1857 if (!context->in_syscall)
1858 return;
1859
Al Viro74c3cbe2007-07-22 08:04:18 -04001860 if (inode)
1861 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001862
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001863 /* look for a parent entry first */
Eric Paris5195d8e2012-01-03 14:23:05 -05001864 list_for_each_entry(n, &context->names_list, list) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001865 if (!n->name || n->type != AUDIT_TYPE_PARENT)
Amy Griffis5712e882007-02-13 14:15:22 -05001866 continue;
1867
1868 if (n->ino == parent->i_ino &&
Jeff Layton91a27b22012-10-10 15:25:28 -04001869 !audit_compare_dname_path(dname, n->name->name, n->name_len)) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001870 found_parent = n;
1871 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001872 }
Steve Grubbac9910c2006-09-28 14:31:32 -04001873 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001874
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001875 /* is there a matching child entry? */
Eric Paris5195d8e2012-01-03 14:23:05 -05001876 list_for_each_entry(n, &context->names_list, list) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001877 /* can only match entries that have a name */
1878 if (!n->name || n->type != type)
Amy Griffis5712e882007-02-13 14:15:22 -05001879 continue;
1880
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001881 /* if we found a parent, make sure this one is a child of it */
1882 if (found_parent && (n->name != found_parent->name))
1883 continue;
1884
Jeff Layton91a27b22012-10-10 15:25:28 -04001885 if (!strcmp(dname, n->name->name) ||
1886 !audit_compare_dname_path(dname, n->name->name,
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001887 found_parent ?
1888 found_parent->name_len :
Jeff Laytone3d6b072012-10-10 15:25:25 -04001889 AUDIT_NAME_FULL)) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001890 found_child = n;
1891 break;
Steve Grubbac9910c2006-09-28 14:31:32 -04001892 }
Amy Griffis5712e882007-02-13 14:15:22 -05001893 }
1894
Amy Griffis5712e882007-02-13 14:15:22 -05001895 if (!found_parent) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001896 /* create a new, "anonymous" parent record */
1897 n = audit_alloc_name(context, AUDIT_TYPE_PARENT);
Eric Paris5195d8e2012-01-03 14:23:05 -05001898 if (!n)
Amy Griffis5712e882007-02-13 14:15:22 -05001899 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05001900 audit_copy_inode(n, NULL, parent);
Amy Griffis73d3ec52006-07-13 13:16:39 -04001901 }
Amy Griffis5712e882007-02-13 14:15:22 -05001902
1903 if (!found_child) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001904 found_child = audit_alloc_name(context, type);
1905 if (!found_child)
Amy Griffis5712e882007-02-13 14:15:22 -05001906 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001907
1908 /* Re-use the name belonging to the slot for a matching parent
1909 * directory. All names for this context are relinquished in
1910 * audit_free_names() */
1911 if (found_parent) {
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001912 found_child->name = found_parent->name;
1913 found_child->name_len = AUDIT_NAME_FULL;
Amy Griffis5712e882007-02-13 14:15:22 -05001914 /* don't call __putname() */
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001915 found_child->name_put = false;
Amy Griffis5712e882007-02-13 14:15:22 -05001916 }
Amy Griffis5712e882007-02-13 14:15:22 -05001917 }
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04001918 if (inode)
1919 audit_copy_inode(found_child, dentry, inode);
1920 else
1921 found_child->ino = (unsigned long)-1;
Amy Griffis3e2efce2006-07-13 13:16:02 -04001922}
Trond Myklebust50e437d2007-06-07 22:44:34 -04001923EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04001924
1925/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001926 * auditsc_get_stamp - get local copies of audit_context values
1927 * @ctx: audit_context for the task
1928 * @t: timespec to store time recorded in the audit_context
1929 * @serial: serial value that is recorded in the audit_context
1930 *
1931 * Also sets the context as auditable.
1932 */
Al Viro48887e62008-12-06 01:05:50 -05001933int auditsc_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01001934 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Al Viro48887e62008-12-06 01:05:50 -05001936 if (!ctx->in_syscall)
1937 return 0;
David Woodhousece625a82005-07-18 14:24:46 -04001938 if (!ctx->serial)
1939 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001940 t->tv_sec = ctx->ctime.tv_sec;
1941 t->tv_nsec = ctx->ctime.tv_nsec;
1942 *serial = ctx->serial;
Al Viro0590b932008-12-14 23:45:27 -05001943 if (!ctx->prio) {
1944 ctx->prio = 1;
1945 ctx->current_state = AUDIT_RECORD_CONTEXT;
1946 }
Al Viro48887e62008-12-06 01:05:50 -05001947 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Eric Paris4746ec52008-01-08 10:06:53 -05001950/* global counter which is incremented every time something logs in */
1951static atomic_t session_id = ATOMIC_INIT(0);
1952
Eric Parisda0a6102013-05-24 08:58:31 -04001953static int audit_set_loginuid_perm(kuid_t loginuid)
1954{
Eric Parisda0a6102013-05-24 08:58:31 -04001955 /* if we are unset, we don't need privs */
1956 if (!audit_loginuid_set(current))
1957 return 0;
Eric Paris21b85c32013-05-23 14:26:00 -04001958 /* if AUDIT_FEATURE_LOGINUID_IMMUTABLE means never ever allow a change*/
1959 if (is_audit_feature_set(AUDIT_FEATURE_LOGINUID_IMMUTABLE))
1960 return -EPERM;
Eric Paris83fa6bb2013-05-24 09:39:29 -04001961 /* it is set, you need permission */
1962 if (!capable(CAP_AUDIT_CONTROL))
1963 return -EPERM;
Eric Parisd040e5a2013-05-24 09:18:04 -04001964 /* reject if this is not an unset and we don't allow that */
1965 if (is_audit_feature_set(AUDIT_FEATURE_ONLY_UNSET_LOGINUID) && uid_valid(loginuid))
1966 return -EPERM;
Eric Paris83fa6bb2013-05-24 09:39:29 -04001967 return 0;
Eric Parisda0a6102013-05-24 08:58:31 -04001968}
1969
1970static void audit_log_set_loginuid(kuid_t koldloginuid, kuid_t kloginuid,
1971 unsigned int oldsessionid, unsigned int sessionid,
1972 int rc)
1973{
1974 struct audit_buffer *ab;
1975 uid_t uid, ologinuid, nloginuid;
1976
1977 uid = from_kuid(&init_user_ns, task_uid(current));
1978 ologinuid = from_kuid(&init_user_ns, koldloginuid);
1979 nloginuid = from_kuid(&init_user_ns, kloginuid),
1980
1981 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1982 if (!ab)
1983 return;
1984 audit_log_format(ab, "pid=%d uid=%u old auid=%u new auid=%u old "
1985 "ses=%u new ses=%u res=%d", current->pid, uid, ologinuid,
1986 nloginuid, oldsessionid, sessionid, !rc);
1987 audit_log_end(ab);
1988}
1989
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001990/**
Eric Paris0a300be2012-01-03 14:23:08 -05001991 * audit_set_loginuid - set current task's audit_context loginuid
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001992 * @loginuid: loginuid value
1993 *
1994 * Returns 0.
1995 *
1996 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1997 */
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001998int audit_set_loginuid(kuid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Eric Paris0a300be2012-01-03 14:23:08 -05002000 struct task_struct *task = current;
Eric Parisda0a6102013-05-24 08:58:31 -04002001 unsigned int sessionid = -1;
2002 kuid_t oldloginuid, oldsessionid;
2003 int rc;
Steve Grubbc0404992005-05-13 18:17:42 +01002004
Eric Parisda0a6102013-05-24 08:58:31 -04002005 oldloginuid = audit_get_loginuid(current);
2006 oldsessionid = audit_get_sessionid(current);
2007
2008 rc = audit_set_loginuid_perm(loginuid);
2009 if (rc)
2010 goto out;
Eric Paris633b4542012-01-03 14:23:08 -05002011
Eric Paris81407c82013-05-24 09:49:14 -04002012 /* are we setting or clearing? */
2013 if (uid_valid(loginuid))
2014 sessionid = atomic_inc_return(&session_id);
Steve Grubb41757102006-06-12 07:48:28 -04002015
Eric Paris4746ec52008-01-08 10:06:53 -05002016 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05002017 task->loginuid = loginuid;
Eric Parisda0a6102013-05-24 08:58:31 -04002018out:
2019 audit_log_set_loginuid(oldloginuid, loginuid, oldsessionid, sessionid, rc);
2020 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002023/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002024 * __audit_mq_open - record audit data for a POSIX MQ open
2025 * @oflag: open flag
2026 * @mode: mode bits
Randy Dunlap6b962552009-01-05 13:41:13 -08002027 * @attr: queue attributes
George C. Wilson20ca73b2006-05-24 16:09:55 -05002028 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002029 */
Al Virodf0a4282011-07-26 05:26:10 -04002030void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002031{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002032 struct audit_context *context = current->audit_context;
2033
Al Viro564f6992008-12-14 04:02:26 -05002034 if (attr)
2035 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2036 else
2037 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002038
Al Viro564f6992008-12-14 04:02:26 -05002039 context->mq_open.oflag = oflag;
2040 context->mq_open.mode = mode;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002041
Al Viro564f6992008-12-14 04:02:26 -05002042 context->type = AUDIT_MQ_OPEN;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002043}
2044
2045/**
Al Viroc32c8af2008-12-14 03:46:48 -05002046 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
George C. Wilson20ca73b2006-05-24 16:09:55 -05002047 * @mqdes: MQ descriptor
2048 * @msg_len: Message length
2049 * @msg_prio: Message priority
Al Viroc32c8af2008-12-14 03:46:48 -05002050 * @abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002051 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002052 */
Al Viroc32c8af2008-12-14 03:46:48 -05002053void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2054 const struct timespec *abs_timeout)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002055{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002056 struct audit_context *context = current->audit_context;
Al Viroc32c8af2008-12-14 03:46:48 -05002057 struct timespec *p = &context->mq_sendrecv.abs_timeout;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002058
Al Viroc32c8af2008-12-14 03:46:48 -05002059 if (abs_timeout)
2060 memcpy(p, abs_timeout, sizeof(struct timespec));
2061 else
2062 memset(p, 0, sizeof(struct timespec));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002063
Al Viroc32c8af2008-12-14 03:46:48 -05002064 context->mq_sendrecv.mqdes = mqdes;
2065 context->mq_sendrecv.msg_len = msg_len;
2066 context->mq_sendrecv.msg_prio = msg_prio;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002067
Al Viroc32c8af2008-12-14 03:46:48 -05002068 context->type = AUDIT_MQ_SENDRECV;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002069}
2070
2071/**
2072 * __audit_mq_notify - record audit data for a POSIX MQ notify
2073 * @mqdes: MQ descriptor
Randy Dunlap6b962552009-01-05 13:41:13 -08002074 * @notification: Notification event
George C. Wilson20ca73b2006-05-24 16:09:55 -05002075 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002076 */
2077
Al Viro20114f72008-12-10 07:16:12 -05002078void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002079{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002080 struct audit_context *context = current->audit_context;
2081
Al Viro20114f72008-12-10 07:16:12 -05002082 if (notification)
2083 context->mq_notify.sigev_signo = notification->sigev_signo;
2084 else
2085 context->mq_notify.sigev_signo = 0;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002086
Al Viro20114f72008-12-10 07:16:12 -05002087 context->mq_notify.mqdes = mqdes;
2088 context->type = AUDIT_MQ_NOTIFY;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002089}
2090
2091/**
2092 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2093 * @mqdes: MQ descriptor
2094 * @mqstat: MQ flags
2095 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002096 */
Al Viro73929062008-12-10 06:58:59 -05002097void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002098{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002099 struct audit_context *context = current->audit_context;
Al Viro73929062008-12-10 06:58:59 -05002100 context->mq_getsetattr.mqdes = mqdes;
2101 context->mq_getsetattr.mqstat = *mqstat;
2102 context->type = AUDIT_MQ_GETSETATTR;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002103}
2104
2105/**
Steve Grubb073115d2006-04-02 17:07:33 -04002106 * audit_ipc_obj - record audit data for ipc object
2107 * @ipcp: ipc permissions
2108 *
Steve Grubb073115d2006-04-02 17:07:33 -04002109 */
Al Viroa33e6752008-12-10 03:40:06 -05002110void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002111{
Steve Grubb073115d2006-04-02 17:07:33 -04002112 struct audit_context *context = current->audit_context;
Al Viroa33e6752008-12-10 03:40:06 -05002113 context->ipc.uid = ipcp->uid;
2114 context->ipc.gid = ipcp->gid;
2115 context->ipc.mode = ipcp->mode;
Al Viroe816f372008-12-10 03:47:15 -05002116 context->ipc.has_perm = 0;
Al Viroa33e6752008-12-10 03:40:06 -05002117 security_ipc_getsecid(ipcp, &context->ipc.osid);
2118 context->type = AUDIT_IPC;
Steve Grubb073115d2006-04-02 17:07:33 -04002119}
2120
2121/**
2122 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002123 * @qbytes: msgq bytes
2124 * @uid: msgq user id
2125 * @gid: msgq group id
2126 * @mode: msgq mode (permissions)
2127 *
Al Viroe816f372008-12-10 03:47:15 -05002128 * Called only after audit_ipc_obj().
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002129 */
Al Viro2570ebb2011-07-27 14:03:22 -04002130void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 struct audit_context *context = current->audit_context;
2133
Al Viroe816f372008-12-10 03:47:15 -05002134 context->ipc.qbytes = qbytes;
2135 context->ipc.perm_uid = uid;
2136 context->ipc.perm_gid = gid;
2137 context->ipc.perm_mode = mode;
2138 context->ipc.has_perm = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002140
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04002141void __audit_bprm(struct linux_binprm *bprm)
Al Viro473ae302006-04-26 14:04:08 -04002142{
Al Viro473ae302006-04-26 14:04:08 -04002143 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002144
Richard Guy Briggsd9cfea92013-10-30 17:56:13 -04002145 context->type = AUDIT_EXECVE;
2146 context->execve.argc = bprm->argc;
2147 context->execve.mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002148}
2149
2150
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002151/**
2152 * audit_socketcall - record audit data for sys_socketcall
Chen Gang2950fa92013-04-07 16:55:23 +08002153 * @nargs: number of args, which should not be more than AUDITSC_ARGS.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002154 * @args: args array
2155 *
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002156 */
Chen Gang2950fa92013-04-07 16:55:23 +08002157int __audit_socketcall(int nargs, unsigned long *args)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002158{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002159 struct audit_context *context = current->audit_context;
2160
Chen Gang2950fa92013-04-07 16:55:23 +08002161 if (nargs <= 0 || nargs > AUDITSC_ARGS || !args)
2162 return -EINVAL;
Al Virof3298dc2008-12-10 03:16:51 -05002163 context->type = AUDIT_SOCKETCALL;
2164 context->socketcall.nargs = nargs;
2165 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
Chen Gang2950fa92013-04-07 16:55:23 +08002166 return 0;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002167}
2168
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002169/**
Al Virodb349502007-02-07 01:48:00 -05002170 * __audit_fd_pair - record audit data for pipe and socketpair
2171 * @fd1: the first file descriptor
2172 * @fd2: the second file descriptor
2173 *
Al Virodb349502007-02-07 01:48:00 -05002174 */
Al Viro157cf642008-12-14 04:57:47 -05002175void __audit_fd_pair(int fd1, int fd2)
Al Virodb349502007-02-07 01:48:00 -05002176{
2177 struct audit_context *context = current->audit_context;
Al Viro157cf642008-12-14 04:57:47 -05002178 context->fds[0] = fd1;
2179 context->fds[1] = fd2;
Al Virodb349502007-02-07 01:48:00 -05002180}
2181
2182/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002183 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2184 * @len: data length in user space
2185 * @a: data address in kernel space
2186 *
2187 * Returns 0 for success or NULL context or < 0 on error.
2188 */
Eric Paris07c49412012-01-03 14:23:07 -05002189int __audit_sockaddr(int len, void *a)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002190{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002191 struct audit_context *context = current->audit_context;
2192
Al Viro4f6b4342008-12-09 19:50:34 -05002193 if (!context->sockaddr) {
2194 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2195 if (!p)
2196 return -ENOMEM;
2197 context->sockaddr = p;
2198 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002199
Al Viro4f6b4342008-12-09 19:50:34 -05002200 context->sockaddr_len = len;
2201 memcpy(context->sockaddr, a, len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002202 return 0;
2203}
2204
Al Viroa5cb0132007-03-20 13:58:35 -04002205void __audit_ptrace(struct task_struct *t)
2206{
2207 struct audit_context *context = current->audit_context;
2208
2209 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002210 context->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002211 context->target_uid = task_uid(t);
Eric Paris4746ec52008-01-08 10:06:53 -05002212 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002213 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002214 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002215}
2216
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002217/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002218 * audit_signal_info - record signal info for shutting down audit subsystem
2219 * @sig: signal value
2220 * @t: task being signaled
2221 *
2222 * If the audit subsystem is being terminated, record the task (pid)
2223 * and uid that is doing that.
2224 */
Amy Griffise54dc242007-03-29 18:01:04 -04002225int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002226{
Amy Griffise54dc242007-03-29 18:01:04 -04002227 struct audit_aux_data_pids *axp;
2228 struct task_struct *tsk = current;
2229 struct audit_context *ctx = tsk->audit_context;
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002230 kuid_t uid = current_uid(), t_uid = task_uid(t);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002231
Al Viro175fc482007-08-08 00:01:46 +01002232 if (audit_pid && t->tgid == audit_pid) {
Eric Parisee1d3152008-07-07 10:49:45 -04002233 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
Al Viro175fc482007-08-08 00:01:46 +01002234 audit_sig_pid = tsk->pid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002235 if (uid_valid(tsk->loginuid))
Al Virobfef93a2008-01-10 04:53:18 -05002236 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002237 else
David Howellsc69e8d92008-11-14 10:39:19 +11002238 audit_sig_uid = uid;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002239 security_task_getsecid(tsk, &audit_sig_sid);
Al Viro175fc482007-08-08 00:01:46 +01002240 }
2241 if (!audit_signals || audit_dummy_context())
2242 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002243 }
Amy Griffise54dc242007-03-29 18:01:04 -04002244
Amy Griffise54dc242007-03-29 18:01:04 -04002245 /* optimize the common case by putting first signal recipient directly
2246 * in audit_context */
2247 if (!ctx->target_pid) {
2248 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002249 ctx->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002250 ctx->target_uid = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002251 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002252 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002253 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002254 return 0;
2255 }
2256
2257 axp = (void *)ctx->aux_pids;
2258 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2259 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2260 if (!axp)
2261 return -ENOMEM;
2262
2263 axp->d.type = AUDIT_OBJ_PID;
2264 axp->d.next = ctx->aux_pids;
2265 ctx->aux_pids = (void *)axp;
2266 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002267 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002268
2269 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002270 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002271 axp->target_uid[axp->pid_count] = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002272 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002273 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002274 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002275 axp->pid_count++;
2276
2277 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002278}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002279
2280/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002281 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
David Howellsd84f4f92008-11-14 10:39:23 +11002282 * @bprm: pointer to the bprm being processed
2283 * @new: the proposed new credentials
2284 * @old: the old credentials
Eric Paris3fc689e2008-11-11 21:48:18 +11002285 *
2286 * Simply check if the proc already has the caps given by the file and if not
2287 * store the priv escalation info for later auditing at the end of the syscall
2288 *
Eric Paris3fc689e2008-11-11 21:48:18 +11002289 * -Eric
2290 */
David Howellsd84f4f92008-11-14 10:39:23 +11002291int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2292 const struct cred *new, const struct cred *old)
Eric Paris3fc689e2008-11-11 21:48:18 +11002293{
2294 struct audit_aux_data_bprm_fcaps *ax;
2295 struct audit_context *context = current->audit_context;
2296 struct cpu_vfs_cap_data vcaps;
2297 struct dentry *dentry;
2298
2299 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2300 if (!ax)
David Howellsd84f4f92008-11-14 10:39:23 +11002301 return -ENOMEM;
Eric Paris3fc689e2008-11-11 21:48:18 +11002302
2303 ax->d.type = AUDIT_BPRM_FCAPS;
2304 ax->d.next = context->aux;
2305 context->aux = (void *)ax;
2306
2307 dentry = dget(bprm->file->f_dentry);
2308 get_vfs_caps_from_disk(dentry, &vcaps);
2309 dput(dentry);
2310
2311 ax->fcap.permitted = vcaps.permitted;
2312 ax->fcap.inheritable = vcaps.inheritable;
2313 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2314 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2315
David Howellsd84f4f92008-11-14 10:39:23 +11002316 ax->old_pcap.permitted = old->cap_permitted;
2317 ax->old_pcap.inheritable = old->cap_inheritable;
2318 ax->old_pcap.effective = old->cap_effective;
Eric Paris3fc689e2008-11-11 21:48:18 +11002319
David Howellsd84f4f92008-11-14 10:39:23 +11002320 ax->new_pcap.permitted = new->cap_permitted;
2321 ax->new_pcap.inheritable = new->cap_inheritable;
2322 ax->new_pcap.effective = new->cap_effective;
2323 return 0;
Eric Paris3fc689e2008-11-11 21:48:18 +11002324}
2325
2326/**
Eric Parise68b75a02008-11-11 21:48:22 +11002327 * __audit_log_capset - store information about the arguments to the capset syscall
David Howellsd84f4f92008-11-14 10:39:23 +11002328 * @pid: target pid of the capset call
2329 * @new: the new credentials
2330 * @old: the old (current) credentials
Eric Parise68b75a02008-11-11 21:48:22 +11002331 *
2332 * Record the aguments userspace sent to sys_capset for later printing by the
2333 * audit system if applicable
2334 */
Al Viro57f71a02009-01-04 14:52:57 -05002335void __audit_log_capset(pid_t pid,
David Howellsd84f4f92008-11-14 10:39:23 +11002336 const struct cred *new, const struct cred *old)
Eric Parise68b75a02008-11-11 21:48:22 +11002337{
Eric Parise68b75a02008-11-11 21:48:22 +11002338 struct audit_context *context = current->audit_context;
Al Viro57f71a02009-01-04 14:52:57 -05002339 context->capset.pid = pid;
2340 context->capset.cap.effective = new->cap_effective;
2341 context->capset.cap.inheritable = new->cap_effective;
2342 context->capset.cap.permitted = new->cap_permitted;
2343 context->type = AUDIT_CAPSET;
Eric Parise68b75a02008-11-11 21:48:22 +11002344}
2345
Al Viro120a7952010-10-30 02:54:44 -04002346void __audit_mmap_fd(int fd, int flags)
2347{
2348 struct audit_context *context = current->audit_context;
2349 context->mmap.fd = fd;
2350 context->mmap.flags = flags;
2351 context->type = AUDIT_MMAP;
2352}
2353
Kees Cook7b9205b2013-01-11 14:32:05 -08002354static void audit_log_task(struct audit_buffer *ab)
Eric Paris85e7bac2012-01-03 14:23:05 -05002355{
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002356 kuid_t auid, uid;
2357 kgid_t gid;
Eric Paris85e7bac2012-01-03 14:23:05 -05002358 unsigned int sessionid;
2359
2360 auid = audit_get_loginuid(current);
2361 sessionid = audit_get_sessionid(current);
2362 current_uid_gid(&uid, &gid);
2363
2364 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002365 from_kuid(&init_user_ns, auid),
2366 from_kuid(&init_user_ns, uid),
2367 from_kgid(&init_user_ns, gid),
2368 sessionid);
Eric Paris85e7bac2012-01-03 14:23:05 -05002369 audit_log_task_context(ab);
2370 audit_log_format(ab, " pid=%d comm=", current->pid);
2371 audit_log_untrustedstring(ab, current->comm);
Kees Cook7b9205b2013-01-11 14:32:05 -08002372}
2373
2374static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr)
2375{
2376 audit_log_task(ab);
Eric Paris85e7bac2012-01-03 14:23:05 -05002377 audit_log_format(ab, " reason=");
2378 audit_log_string(ab, reason);
2379 audit_log_format(ab, " sig=%ld", signr);
2380}
Eric Parise68b75a02008-11-11 21:48:22 +11002381/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002382 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002383 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002384 *
2385 * If a process ends with a core dump, something fishy is going on and we
2386 * should record the event for investigation.
2387 */
2388void audit_core_dumps(long signr)
2389{
2390 struct audit_buffer *ab;
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002391
2392 if (!audit_enabled)
2393 return;
2394
2395 if (signr == SIGQUIT) /* don't care for those */
2396 return;
2397
2398 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
Kees Cook0644ec02013-01-11 14:32:07 -08002399 if (unlikely(!ab))
2400 return;
Eric Paris85e7bac2012-01-03 14:23:05 -05002401 audit_log_abend(ab, "memory violation", signr);
2402 audit_log_end(ab);
2403}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002404
Kees Cook3dc1c1b2012-04-12 16:47:58 -05002405void __audit_seccomp(unsigned long syscall, long signr, int code)
Eric Paris85e7bac2012-01-03 14:23:05 -05002406{
2407 struct audit_buffer *ab;
2408
Kees Cook7b9205b2013-01-11 14:32:05 -08002409 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_SECCOMP);
2410 if (unlikely(!ab))
2411 return;
2412 audit_log_task(ab);
2413 audit_log_format(ab, " sig=%ld", signr);
Eric Paris85e7bac2012-01-03 14:23:05 -05002414 audit_log_format(ab, " syscall=%ld", syscall);
Kees Cook3dc1c1b2012-04-12 16:47:58 -05002415 audit_log_format(ab, " compat=%d", is_compat_task());
2416 audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
2417 audit_log_format(ab, " code=0x%x", code);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002418 audit_log_end(ab);
2419}
Al Viro916d7572009-06-24 00:02:38 -04002420
2421struct list_head *audit_killed_trees(void)
2422{
2423 struct audit_context *ctx = current->audit_context;
2424 if (likely(!ctx || !ctx->in_syscall))
2425 return NULL;
2426 return &ctx->killed_trees;
2427}