blob: 19b232f86d705109c0bfacdfe5082655e02643c9 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* AUDIT_NAMES is the number of slots we reserve in the audit_context
Eric Paris5195d8e2012-01-03 14:23:05 -050080 * for saving names from getname(). If we get more names we will allocate
81 * a name dynamically and also add those to the list anchored by names_list. */
82#define AUDIT_NAMES 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Amy Griffis9c937dc2006-06-08 23:19:31 -040084/* Indicates that audit should log the full pathname. */
85#define AUDIT_NAME_FULL -1
86
Eric Parisde6bbd12008-01-07 14:31:58 -050087/* no execve audit message should be longer than this (userspace limits) */
88#define MAX_EXECVE_AUDIT_LEN 7500
89
Al Viro471a5c72006-07-10 08:29:24 -040090/* number of audit rules */
91int audit_n_rules;
92
Amy Griffise54dc242007-03-29 18:01:04 -040093/* determines whether we collect data for signals sent */
94int audit_signals;
95
Eric Paris851f7ff2008-11-11 21:48:14 +110096struct audit_cap_data {
97 kernel_cap_t permitted;
98 kernel_cap_t inheritable;
99 union {
100 unsigned int fE; /* effective bit of a file capability */
101 kernel_cap_t effective; /* effective set of a process */
102 };
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/* When fs/namei.c:getname() is called, we store the pointer in name and
106 * we don't let putname() free it (instead we free all of the saved
107 * pointers at syscall exit time).
108 *
109 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
110struct audit_names {
Eric Paris5195d8e2012-01-03 14:23:05 -0500111 struct list_head list; /* audit_context->names_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 const char *name;
113 unsigned long ino;
114 dev_t dev;
115 umode_t mode;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700116 kuid_t uid;
117 kgid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400119 u32 osid;
Eric Paris851f7ff2008-11-11 21:48:14 +1100120 struct audit_cap_data fcap;
121 unsigned int fcap_ver;
Eric Paris5195d8e2012-01-03 14:23:05 -0500122 int name_len; /* number of name's characters to log */
Jeff Layton78e2e802012-10-10 15:25:22 -0400123 unsigned char type; /* record type */
Eric Paris5195d8e2012-01-03 14:23:05 -0500124 bool name_put; /* call __putname() for this name */
125 /*
126 * This was an allocated audit_names and not from the array of
127 * names allocated in the task audit context. Thus this name
128 * should be freed on syscall exit
129 */
130 bool should_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133struct audit_aux_data {
134 struct audit_aux_data *next;
135 int type;
136};
137
138#define AUDIT_AUX_IPCPERM 0
139
Amy Griffise54dc242007-03-29 18:01:04 -0400140/* Number of target pids per aux struct. */
141#define AUDIT_AUX_PIDS 16
142
Al Viro473ae302006-04-26 14:04:08 -0400143struct audit_aux_data_execve {
144 struct audit_aux_data d;
145 int argc;
146 int envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700147 struct mm_struct *mm;
Al Viro473ae302006-04-26 14:04:08 -0400148};
149
Amy Griffise54dc242007-03-29 18:01:04 -0400150struct audit_aux_data_pids {
151 struct audit_aux_data d;
152 pid_t target_pid[AUDIT_AUX_PIDS];
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700153 kuid_t target_auid[AUDIT_AUX_PIDS];
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800154 kuid_t target_uid[AUDIT_AUX_PIDS];
Eric Paris4746ec52008-01-08 10:06:53 -0500155 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400156 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500157 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400158 int pid_count;
159};
160
Eric Paris3fc689e2008-11-11 21:48:18 +1100161struct audit_aux_data_bprm_fcaps {
162 struct audit_aux_data d;
163 struct audit_cap_data fcap;
164 unsigned int fcap_ver;
165 struct audit_cap_data old_pcap;
166 struct audit_cap_data new_pcap;
167};
168
Eric Parise68b75a02008-11-11 21:48:22 +1100169struct audit_aux_data_capset {
170 struct audit_aux_data d;
171 pid_t pid;
172 struct audit_cap_data cap;
173};
174
Al Viro74c3cbe2007-07-22 08:04:18 -0400175struct audit_tree_refs {
176 struct audit_tree_refs *next;
177 struct audit_chunk *c[31];
178};
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/* The per-task audit context. */
181struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400182 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int in_syscall; /* 1 if task is in a syscall */
Al Viro0590b932008-12-14 23:45:27 -0500184 enum audit_state state, current_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 unsigned int serial; /* serial number for record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int major; /* syscall number */
Eric Paris44e51a12009-08-07 16:54:29 -0400187 struct timespec ctime; /* time of syscall entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned long argv[4]; /* syscall arguments */
2fd6f582005-04-29 16:08:28 +0100189 long return_code;/* syscall return code */
Al Viro0590b932008-12-14 23:45:27 -0500190 u64 prio;
Eric Paris44e51a12009-08-07 16:54:29 -0400191 int return_valid; /* return code is valid */
Eric Paris5195d8e2012-01-03 14:23:05 -0500192 /*
193 * The names_list is the list of all audit_names collected during this
194 * syscall. The first AUDIT_NAMES entries in the names_list will
195 * actually be from the preallocated_names array for performance
196 * reasons. Except during allocation they should never be referenced
197 * through the preallocated_names array and should only be found/used
198 * by running the names_list.
199 */
200 struct audit_names preallocated_names[AUDIT_NAMES];
201 int name_count; /* total records in names_list */
202 struct list_head names_list; /* anchor for struct audit_names->list */
Amy Griffis5adc8a62006-06-14 18:45:21 -0400203 char * filterkey; /* key for rule that triggered record */
Jan Blunck44707fd2008-02-14 19:38:33 -0800204 struct path pwd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct audit_context *previous; /* For nested syscalls */
206 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400207 struct audit_aux_data *aux_pids;
Al Viro4f6b4342008-12-09 19:50:34 -0500208 struct sockaddr_storage *sockaddr;
209 size_t sockaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400211 pid_t pid, ppid;
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800212 kuid_t uid, euid, suid, fsuid;
213 kgid_t gid, egid, sgid, fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100215 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Al Viroa5cb0132007-03-20 13:58:35 -0400217 pid_t target_pid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700218 kuid_t target_auid;
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800219 kuid_t target_uid;
Eric Paris4746ec52008-01-08 10:06:53 -0500220 unsigned int target_sessionid;
Al Viroa5cb0132007-03-20 13:58:35 -0400221 u32 target_sid;
Eric Parisc2a77802008-01-07 13:40:17 -0500222 char target_comm[TASK_COMM_LEN];
Al Viroa5cb0132007-03-20 13:58:35 -0400223
Al Viro74c3cbe2007-07-22 08:04:18 -0400224 struct audit_tree_refs *trees, *first_trees;
Al Viro916d7572009-06-24 00:02:38 -0400225 struct list_head killed_trees;
Eric Paris44e51a12009-08-07 16:54:29 -0400226 int tree_count;
Al Viro74c3cbe2007-07-22 08:04:18 -0400227
Al Virof3298dc2008-12-10 03:16:51 -0500228 int type;
229 union {
230 struct {
231 int nargs;
232 long args[6];
233 } socketcall;
Al Viroa33e6752008-12-10 03:40:06 -0500234 struct {
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800235 kuid_t uid;
236 kgid_t gid;
Al Viro2570ebb2011-07-27 14:03:22 -0400237 umode_t mode;
Al Viroa33e6752008-12-10 03:40:06 -0500238 u32 osid;
Al Viroe816f372008-12-10 03:47:15 -0500239 int has_perm;
240 uid_t perm_uid;
241 gid_t perm_gid;
Al Viro2570ebb2011-07-27 14:03:22 -0400242 umode_t perm_mode;
Al Viroe816f372008-12-10 03:47:15 -0500243 unsigned long qbytes;
Al Viroa33e6752008-12-10 03:40:06 -0500244 } ipc;
Al Viro73929062008-12-10 06:58:59 -0500245 struct {
246 mqd_t mqdes;
247 struct mq_attr mqstat;
248 } mq_getsetattr;
Al Viro20114f72008-12-10 07:16:12 -0500249 struct {
250 mqd_t mqdes;
251 int sigev_signo;
252 } mq_notify;
Al Viroc32c8af2008-12-14 03:46:48 -0500253 struct {
254 mqd_t mqdes;
255 size_t msg_len;
256 unsigned int msg_prio;
257 struct timespec abs_timeout;
258 } mq_sendrecv;
Al Viro564f6992008-12-14 04:02:26 -0500259 struct {
260 int oflag;
Al Virodf0a4282011-07-26 05:26:10 -0400261 umode_t mode;
Al Viro564f6992008-12-14 04:02:26 -0500262 struct mq_attr attr;
263 } mq_open;
Al Viro57f71a02009-01-04 14:52:57 -0500264 struct {
265 pid_t pid;
266 struct audit_cap_data cap;
267 } capset;
Al Viro120a7952010-10-30 02:54:44 -0400268 struct {
269 int fd;
270 int flags;
271 } mmap;
Al Virof3298dc2008-12-10 03:16:51 -0500272 };
Al Viro157cf642008-12-14 04:57:47 -0500273 int fds[2];
Al Virof3298dc2008-12-10 03:16:51 -0500274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#if AUDIT_DEBUG
276 int put_count;
277 int ino_count;
278#endif
279};
280
Al Viro55669bf2006-08-31 19:26:40 -0400281static inline int open_arg(int flags, int mask)
282{
283 int n = ACC_MODE(flags);
284 if (flags & (O_TRUNC | O_CREAT))
285 n |= AUDIT_PERM_WRITE;
286 return n & mask;
287}
288
289static int audit_match_perm(struct audit_context *ctx, int mask)
290{
Cordeliac4bacef2008-08-18 09:45:51 -0700291 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800292 if (unlikely(!ctx))
293 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700294 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100295
Al Viro55669bf2006-08-31 19:26:40 -0400296 switch (audit_classify_syscall(ctx->arch, n)) {
297 case 0: /* native */
298 if ((mask & AUDIT_PERM_WRITE) &&
299 audit_match_class(AUDIT_CLASS_WRITE, n))
300 return 1;
301 if ((mask & AUDIT_PERM_READ) &&
302 audit_match_class(AUDIT_CLASS_READ, n))
303 return 1;
304 if ((mask & AUDIT_PERM_ATTR) &&
305 audit_match_class(AUDIT_CLASS_CHATTR, n))
306 return 1;
307 return 0;
308 case 1: /* 32bit on biarch */
309 if ((mask & AUDIT_PERM_WRITE) &&
310 audit_match_class(AUDIT_CLASS_WRITE_32, n))
311 return 1;
312 if ((mask & AUDIT_PERM_READ) &&
313 audit_match_class(AUDIT_CLASS_READ_32, n))
314 return 1;
315 if ((mask & AUDIT_PERM_ATTR) &&
316 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
317 return 1;
318 return 0;
319 case 2: /* open */
320 return mask & ACC_MODE(ctx->argv[1]);
321 case 3: /* openat */
322 return mask & ACC_MODE(ctx->argv[2]);
323 case 4: /* socketcall */
324 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
325 case 5: /* execve */
326 return mask & AUDIT_PERM_EXEC;
327 default:
328 return 0;
329 }
330}
331
Eric Paris5ef30ee2012-01-03 14:23:05 -0500332static int audit_match_filetype(struct audit_context *ctx, int val)
Al Viro8b67dca2008-04-28 04:15:49 -0400333{
Eric Paris5195d8e2012-01-03 14:23:05 -0500334 struct audit_names *n;
Eric Paris5ef30ee2012-01-03 14:23:05 -0500335 umode_t mode = (umode_t)val;
zhangxiliang1a61c882008-08-02 10:56:37 +0800336
337 if (unlikely(!ctx))
338 return 0;
339
Eric Paris5195d8e2012-01-03 14:23:05 -0500340 list_for_each_entry(n, &ctx->names_list, list) {
341 if ((n->ino != -1) &&
342 ((n->mode & S_IFMT) == mode))
Eric Paris5ef30ee2012-01-03 14:23:05 -0500343 return 1;
344 }
Eric Paris5195d8e2012-01-03 14:23:05 -0500345
Eric Paris5ef30ee2012-01-03 14:23:05 -0500346 return 0;
Al Viro8b67dca2008-04-28 04:15:49 -0400347}
348
Al Viro74c3cbe2007-07-22 08:04:18 -0400349/*
350 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
351 * ->first_trees points to its beginning, ->trees - to the current end of data.
352 * ->tree_count is the number of free entries in array pointed to by ->trees.
353 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
354 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
355 * it's going to remain 1-element for almost any setup) until we free context itself.
356 * References in it _are_ dropped - at the same time we free/drop aux stuff.
357 */
358
359#ifdef CONFIG_AUDIT_TREE
Eric Paris679173b2009-01-26 18:09:45 -0500360static void audit_set_auditable(struct audit_context *ctx)
361{
362 if (!ctx->prio) {
363 ctx->prio = 1;
364 ctx->current_state = AUDIT_RECORD_CONTEXT;
365 }
366}
367
Al Viro74c3cbe2007-07-22 08:04:18 -0400368static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
369{
370 struct audit_tree_refs *p = ctx->trees;
371 int left = ctx->tree_count;
372 if (likely(left)) {
373 p->c[--left] = chunk;
374 ctx->tree_count = left;
375 return 1;
376 }
377 if (!p)
378 return 0;
379 p = p->next;
380 if (p) {
381 p->c[30] = chunk;
382 ctx->trees = p;
383 ctx->tree_count = 30;
384 return 1;
385 }
386 return 0;
387}
388
389static int grow_tree_refs(struct audit_context *ctx)
390{
391 struct audit_tree_refs *p = ctx->trees;
392 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
393 if (!ctx->trees) {
394 ctx->trees = p;
395 return 0;
396 }
397 if (p)
398 p->next = ctx->trees;
399 else
400 ctx->first_trees = ctx->trees;
401 ctx->tree_count = 31;
402 return 1;
403}
404#endif
405
406static void unroll_tree_refs(struct audit_context *ctx,
407 struct audit_tree_refs *p, int count)
408{
409#ifdef CONFIG_AUDIT_TREE
410 struct audit_tree_refs *q;
411 int n;
412 if (!p) {
413 /* we started with empty chain */
414 p = ctx->first_trees;
415 count = 31;
416 /* if the very first allocation has failed, nothing to do */
417 if (!p)
418 return;
419 }
420 n = count;
421 for (q = p; q != ctx->trees; q = q->next, n = 31) {
422 while (n--) {
423 audit_put_chunk(q->c[n]);
424 q->c[n] = NULL;
425 }
426 }
427 while (n-- > ctx->tree_count) {
428 audit_put_chunk(q->c[n]);
429 q->c[n] = NULL;
430 }
431 ctx->trees = p;
432 ctx->tree_count = count;
433#endif
434}
435
436static void free_tree_refs(struct audit_context *ctx)
437{
438 struct audit_tree_refs *p, *q;
439 for (p = ctx->first_trees; p; p = q) {
440 q = p->next;
441 kfree(p);
442 }
443}
444
445static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
446{
447#ifdef CONFIG_AUDIT_TREE
448 struct audit_tree_refs *p;
449 int n;
450 if (!tree)
451 return 0;
452 /* full ones */
453 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
454 for (n = 0; n < 31; n++)
455 if (audit_tree_match(p->c[n], tree))
456 return 1;
457 }
458 /* partial */
459 if (p) {
460 for (n = ctx->tree_count; n < 31; n++)
461 if (audit_tree_match(p->c[n], tree))
462 return 1;
463 }
464#endif
465 return 0;
466}
467
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700468static int audit_compare_uid(kuid_t uid,
469 struct audit_names *name,
470 struct audit_field *f,
471 struct audit_context *ctx)
Eric Parisb34b0392012-01-03 14:23:08 -0500472{
473 struct audit_names *n;
Eric Parisb34b0392012-01-03 14:23:08 -0500474 int rc;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700475
Eric Parisb34b0392012-01-03 14:23:08 -0500476 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700477 rc = audit_uid_comparator(uid, f->op, name->uid);
Eric Parisb34b0392012-01-03 14:23:08 -0500478 if (rc)
479 return rc;
480 }
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700481
Eric Parisb34b0392012-01-03 14:23:08 -0500482 if (ctx) {
483 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700484 rc = audit_uid_comparator(uid, f->op, n->uid);
485 if (rc)
486 return rc;
487 }
488 }
489 return 0;
490}
Eric Parisb34b0392012-01-03 14:23:08 -0500491
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700492static int audit_compare_gid(kgid_t gid,
493 struct audit_names *name,
494 struct audit_field *f,
495 struct audit_context *ctx)
496{
497 struct audit_names *n;
498 int rc;
499
500 if (name) {
501 rc = audit_gid_comparator(gid, f->op, name->gid);
502 if (rc)
503 return rc;
504 }
505
506 if (ctx) {
507 list_for_each_entry(n, &ctx->names_list, list) {
508 rc = audit_gid_comparator(gid, f->op, n->gid);
Eric Parisb34b0392012-01-03 14:23:08 -0500509 if (rc)
510 return rc;
511 }
512 }
513 return 0;
514}
515
Eric Paris02d86a52012-01-03 14:23:08 -0500516static int audit_field_compare(struct task_struct *tsk,
517 const struct cred *cred,
518 struct audit_field *f,
519 struct audit_context *ctx,
520 struct audit_names *name)
521{
Eric Paris02d86a52012-01-03 14:23:08 -0500522 switch (f->val) {
Peter Moody4a6633e2011-12-13 16:17:51 -0800523 /* process to file object comparisons */
Eric Paris02d86a52012-01-03 14:23:08 -0500524 case AUDIT_COMPARE_UID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700525 return audit_compare_uid(cred->uid, name, f, ctx);
Eric Parisc9fe6852012-01-03 14:23:08 -0500526 case AUDIT_COMPARE_GID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700527 return audit_compare_gid(cred->gid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800528 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700529 return audit_compare_uid(cred->euid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800530 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700531 return audit_compare_gid(cred->egid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800532 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700533 return audit_compare_uid(tsk->loginuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800534 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700535 return audit_compare_uid(cred->suid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800536 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700537 return audit_compare_gid(cred->sgid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800538 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700539 return audit_compare_uid(cred->fsuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800540 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700541 return audit_compare_gid(cred->fsgid, name, f, ctx);
Peter Moody10d68362012-01-04 15:24:31 -0500542 /* uid comparisons */
543 case AUDIT_COMPARE_UID_TO_AUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700544 return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
Peter Moody10d68362012-01-04 15:24:31 -0500545 case AUDIT_COMPARE_UID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700546 return audit_uid_comparator(cred->uid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500547 case AUDIT_COMPARE_UID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700548 return audit_uid_comparator(cred->uid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500549 case AUDIT_COMPARE_UID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700550 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500551 /* auid comparisons */
552 case AUDIT_COMPARE_AUID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700553 return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500554 case AUDIT_COMPARE_AUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700555 return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500556 case AUDIT_COMPARE_AUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700557 return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500558 /* euid comparisons */
559 case AUDIT_COMPARE_EUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700560 return audit_uid_comparator(cred->euid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500561 case AUDIT_COMPARE_EUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700562 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500563 /* suid comparisons */
564 case AUDIT_COMPARE_SUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700565 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500566 /* gid comparisons */
567 case AUDIT_COMPARE_GID_TO_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700568 return audit_gid_comparator(cred->gid, f->op, cred->egid);
Peter Moody10d68362012-01-04 15:24:31 -0500569 case AUDIT_COMPARE_GID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700570 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500571 case AUDIT_COMPARE_GID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700572 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500573 /* egid comparisons */
574 case AUDIT_COMPARE_EGID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700575 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500576 case AUDIT_COMPARE_EGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700577 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500578 /* sgid comparison */
579 case AUDIT_COMPARE_SGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700580 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
Eric Paris02d86a52012-01-03 14:23:08 -0500581 default:
582 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
583 return 0;
584 }
585 return 0;
586}
587
Amy Griffisf368c07d2006-04-07 16:55:56 -0400588/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589/* Compare a task_struct with an audit_rule. Return 1 on match, 0
Tony Jonesf5629882011-04-27 15:10:49 +0200590 * otherwise.
591 *
592 * If task_creation is true, this is an explicit indication that we are
593 * filtering a task rule at task creation time. This and tsk == current are
594 * the only situations where tsk->cred may be accessed without an rcu read lock.
595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500597 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400599 struct audit_names *name,
Tony Jonesf5629882011-04-27 15:10:49 +0200600 enum audit_state *state,
601 bool task_creation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Tony Jonesf5629882011-04-27 15:10:49 +0200603 const struct cred *cred;
Eric Paris5195d8e2012-01-03 14:23:05 -0500604 int i, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600605 u32 sid;
606
Tony Jonesf5629882011-04-27 15:10:49 +0200607 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500610 struct audit_field *f = &rule->fields[i];
Eric Paris5195d8e2012-01-03 14:23:05 -0500611 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int result = 0;
613
Amy Griffis93315ed2006-02-07 12:05:27 -0500614 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500616 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
Al Viro3c662512006-05-06 08:26:27 -0400618 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400619 if (ctx) {
620 if (!ctx->ppid)
621 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400622 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400623 }
Al Viro3c662512006-05-06 08:26:27 -0400624 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 case AUDIT_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700626 result = audit_uid_comparator(cred->uid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
628 case AUDIT_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700629 result = audit_uid_comparator(cred->euid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 break;
631 case AUDIT_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700632 result = audit_uid_comparator(cred->suid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634 case AUDIT_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700635 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
637 case AUDIT_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700638 result = audit_gid_comparator(cred->gid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640 case AUDIT_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700641 result = audit_gid_comparator(cred->egid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 case AUDIT_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700644 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 case AUDIT_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700647 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 break;
649 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500650 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
2fd6f582005-04-29 16:08:28 +0100652 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700653 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500654 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 case AUDIT_EXIT:
658 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500659 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
661 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100662 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500663 if (f->val)
664 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100665 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500666 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
669 case AUDIT_DEVMAJOR:
Eric Paris16c174bd2012-01-03 14:23:05 -0500670 if (name) {
671 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
672 audit_comparator(MAJOR(name->rdev), f->op, f->val))
673 ++result;
674 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500675 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174bd2012-01-03 14:23:05 -0500676 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
677 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 ++result;
679 break;
680 }
681 }
682 }
683 break;
684 case AUDIT_DEVMINOR:
Eric Paris16c174bd2012-01-03 14:23:05 -0500685 if (name) {
686 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
687 audit_comparator(MINOR(name->rdev), f->op, f->val))
688 ++result;
689 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500690 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174bd2012-01-03 14:23:05 -0500691 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
692 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ++result;
694 break;
695 }
696 }
697 }
698 break;
699 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400700 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400701 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400702 else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500703 list_for_each_entry(n, &ctx->names_list, list) {
704 if (audit_comparator(n->ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 ++result;
706 break;
707 }
708 }
709 }
710 break;
Eric Parisefaffd62012-01-03 14:23:07 -0500711 case AUDIT_OBJ_UID:
712 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700713 result = audit_uid_comparator(name->uid, f->op, f->uid);
Eric Parisefaffd62012-01-03 14:23:07 -0500714 } else if (ctx) {
715 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700716 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
Eric Parisefaffd62012-01-03 14:23:07 -0500717 ++result;
718 break;
719 }
720 }
721 }
722 break;
Eric Paris54d32182012-01-03 14:23:07 -0500723 case AUDIT_OBJ_GID:
724 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700725 result = audit_gid_comparator(name->gid, f->op, f->gid);
Eric Paris54d32182012-01-03 14:23:07 -0500726 } else if (ctx) {
727 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700728 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
Eric Paris54d32182012-01-03 14:23:07 -0500729 ++result;
730 break;
731 }
732 }
733 }
734 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400735 case AUDIT_WATCH:
Eric Parisae7b8f42009-12-17 20:12:04 -0500736 if (name)
737 result = audit_watch_compare(rule->watch, name->ino, name->dev);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400738 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400739 case AUDIT_DIR:
740 if (ctx)
741 result = match_tree_refs(ctx, rule->tree);
742 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 case AUDIT_LOGINUID:
744 result = 0;
745 if (ctx)
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700746 result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500748 case AUDIT_SUBJ_USER:
749 case AUDIT_SUBJ_ROLE:
750 case AUDIT_SUBJ_TYPE:
751 case AUDIT_SUBJ_SEN:
752 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600753 /* NOTE: this may return negative values indicating
754 a temporary error. We simply treat this as a
755 match for now to avoid losing information that
756 may be wanted. An error message will also be
757 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000758 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400759 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200760 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400761 need_sid = 0;
762 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200763 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600764 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000765 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600766 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400767 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600768 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500769 case AUDIT_OBJ_USER:
770 case AUDIT_OBJ_ROLE:
771 case AUDIT_OBJ_TYPE:
772 case AUDIT_OBJ_LEV_LOW:
773 case AUDIT_OBJ_LEV_HIGH:
774 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
775 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000776 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500777 /* Find files that match */
778 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200779 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500780 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000781 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500782 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500783 list_for_each_entry(n, &ctx->names_list, list) {
784 if (security_audit_rule_match(n->osid, f->type,
785 f->op, f->lsm_rule,
786 ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500787 ++result;
788 break;
789 }
790 }
791 }
792 /* Find ipc objects that match */
Al Viroa33e6752008-12-10 03:40:06 -0500793 if (!ctx || ctx->type != AUDIT_IPC)
794 break;
795 if (security_audit_rule_match(ctx->ipc.osid,
796 f->type, f->op,
797 f->lsm_rule, ctx))
798 ++result;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500799 }
800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 case AUDIT_ARG0:
802 case AUDIT_ARG1:
803 case AUDIT_ARG2:
804 case AUDIT_ARG3:
805 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500806 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400808 case AUDIT_FILTERKEY:
809 /* ignore this field for filtering */
810 result = 1;
811 break;
Al Viro55669bf2006-08-31 19:26:40 -0400812 case AUDIT_PERM:
813 result = audit_match_perm(ctx, f->val);
814 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400815 case AUDIT_FILETYPE:
816 result = audit_match_filetype(ctx, f->val);
817 break;
Eric Paris02d86a52012-01-03 14:23:08 -0500818 case AUDIT_FIELD_COMPARE:
819 result = audit_field_compare(tsk, cred, f, ctx, name);
820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Tony Jonesf5629882011-04-27 15:10:49 +0200822 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824 }
Al Viro0590b932008-12-14 23:45:27 -0500825
826 if (ctx) {
827 if (rule->prio <= ctx->prio)
828 return 0;
829 if (rule->filterkey) {
830 kfree(ctx->filterkey);
831 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
832 }
833 ctx->prio = rule->prio;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 switch (rule->action) {
836 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
838 }
839 return 1;
840}
841
842/* At process creation time, we can determine if system-call auditing is
843 * completely disabled for this task. Since we only have the task
844 * structure at this point, we can only check uid and gid.
845 */
Al Viroe048e022008-12-16 03:51:22 -0500846static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct audit_entry *e;
849 enum audit_state state;
850
851 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100852 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Tony Jonesf5629882011-04-27 15:10:49 +0200853 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
854 &state, true)) {
Al Viroe048e022008-12-16 03:51:22 -0500855 if (state == AUDIT_RECORD_CONTEXT)
856 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 rcu_read_unlock();
858 return state;
859 }
860 }
861 rcu_read_unlock();
862 return AUDIT_BUILD_CONTEXT;
863}
864
865/* At syscall entry and exit time, this filter is called if the
866 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100867 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700868 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
870static enum audit_state audit_filter_syscall(struct task_struct *tsk,
871 struct audit_context *ctx,
872 struct list_head *list)
873{
874 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100875 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
David Woodhouse351bb722005-07-14 14:40:06 +0100877 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100878 return AUDIT_DISABLED;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100881 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000882 int word = AUDIT_WORD(ctx->major);
883 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100884
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000885 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400886 if ((e->rule.mask[word] & bit) == bit &&
887 audit_filter_rules(tsk, &e->rule, ctx, NULL,
Tony Jonesf5629882011-04-27 15:10:49 +0200888 &state, false)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000889 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500890 ctx->current_state = state;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000891 return state;
892 }
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 rcu_read_unlock();
896 return AUDIT_BUILD_CONTEXT;
897}
898
Eric Paris5195d8e2012-01-03 14:23:05 -0500899/*
900 * Given an audit_name check the inode hash table to see if they match.
901 * Called holding the rcu read lock to protect the use of audit_inode_hash
902 */
903static int audit_filter_inode_name(struct task_struct *tsk,
904 struct audit_names *n,
905 struct audit_context *ctx) {
906 int word, bit;
907 int h = audit_hash_ino((u32)n->ino);
908 struct list_head *list = &audit_inode_hash[h];
909 struct audit_entry *e;
910 enum audit_state state;
911
912 word = AUDIT_WORD(ctx->major);
913 bit = AUDIT_BIT(ctx->major);
914
915 if (list_empty(list))
916 return 0;
917
918 list_for_each_entry_rcu(e, list, list) {
919 if ((e->rule.mask[word] & bit) == bit &&
920 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
921 ctx->current_state = state;
922 return 1;
923 }
924 }
925
926 return 0;
927}
928
929/* At syscall exit time, this filter is called if any audit_names have been
Amy Griffisf368c07d2006-04-07 16:55:56 -0400930 * collected during syscall processing. We only check rules in sublists at hash
Eric Paris5195d8e2012-01-03 14:23:05 -0500931 * buckets applicable to the inode numbers in audit_names.
Amy Griffisf368c07d2006-04-07 16:55:56 -0400932 * Regarding audit_state, same rules apply as for audit_filter_syscall().
933 */
Al Viro0590b932008-12-14 23:45:27 -0500934void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400935{
Eric Paris5195d8e2012-01-03 14:23:05 -0500936 struct audit_names *n;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400937
938 if (audit_pid && tsk->tgid == audit_pid)
Al Viro0590b932008-12-14 23:45:27 -0500939 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400940
941 rcu_read_lock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400942
Eric Paris5195d8e2012-01-03 14:23:05 -0500943 list_for_each_entry(n, &ctx->names_list, list) {
944 if (audit_filter_inode_name(tsk, n, ctx))
945 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400946 }
947 rcu_read_unlock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950static inline struct audit_context *audit_get_context(struct task_struct *tsk,
951 int return_valid,
Paul Moore6d208da2009-04-01 15:47:27 -0400952 long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct audit_context *context = tsk->audit_context;
955
Eric Paris56179a62012-01-03 14:23:06 -0500956 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return NULL;
958 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500959
960 /*
961 * we need to fix up the return code in the audit logs if the actual
962 * return codes are later going to be fixed up by the arch specific
963 * signal handlers
964 *
965 * This is actually a test for:
966 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
967 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
968 *
969 * but is faster than a bunch of ||
970 */
971 if (unlikely(return_code <= -ERESTARTSYS) &&
972 (return_code >= -ERESTART_RESTARTBLOCK) &&
973 (return_code != -ENOIOCTLCMD))
974 context->return_code = -EINTR;
975 else
976 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Al Viro0590b932008-12-14 23:45:27 -0500978 if (context->in_syscall && !context->dummy) {
979 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
980 audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 tsk->audit_context = NULL;
984 return context;
985}
986
987static inline void audit_free_names(struct audit_context *context)
988{
Eric Paris5195d8e2012-01-03 14:23:05 -0500989 struct audit_names *n, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991#if AUDIT_DEBUG == 2
Al Viro0590b932008-12-14 23:45:27 -0500992 if (context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000993 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 " name_count=%d put_count=%d"
995 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000996 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 context->serial, context->major, context->in_syscall,
998 context->name_count, context->put_count,
999 context->ino_count);
Eric Paris5195d8e2012-01-03 14:23:05 -05001000 list_for_each_entry(n, &context->names_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 printk(KERN_ERR "names[%d] = %p = %s\n", i,
Eric Paris5195d8e2012-01-03 14:23:05 -05001002 n->name, n->name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 dump_stack();
1005 return;
1006 }
1007#endif
1008#if AUDIT_DEBUG
1009 context->put_count = 0;
1010 context->ino_count = 0;
1011#endif
1012
Eric Paris5195d8e2012-01-03 14:23:05 -05001013 list_for_each_entry_safe(n, next, &context->names_list, list) {
1014 list_del(&n->list);
1015 if (n->name && n->name_put)
1016 __putname(n->name);
1017 if (n->should_free)
1018 kfree(n);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -08001021 path_put(&context->pwd);
1022 context->pwd.dentry = NULL;
1023 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026static inline void audit_free_aux(struct audit_context *context)
1027{
1028 struct audit_aux_data *aux;
1029
1030 while ((aux = context->aux)) {
1031 context->aux = aux->next;
1032 kfree(aux);
1033 }
Amy Griffise54dc242007-03-29 18:01:04 -04001034 while ((aux = context->aux_pids)) {
1035 context->aux_pids = aux->next;
1036 kfree(aux);
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040static inline void audit_zero_context(struct audit_context *context,
1041 enum audit_state state)
1042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 memset(context, 0, sizeof(*context));
1044 context->state = state;
Al Viro0590b932008-12-14 23:45:27 -05001045 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048static inline struct audit_context *audit_alloc_context(enum audit_state state)
1049{
1050 struct audit_context *context;
1051
1052 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
1053 return NULL;
1054 audit_zero_context(context, state);
Al Viro916d7572009-06-24 00:02:38 -04001055 INIT_LIST_HEAD(&context->killed_trees);
Eric Paris5195d8e2012-01-03 14:23:05 -05001056 INIT_LIST_HEAD(&context->names_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return context;
1058}
1059
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001060/**
1061 * audit_alloc - allocate an audit context block for a task
1062 * @tsk: task
1063 *
1064 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * if necessary. Doing so turns on system call auditing for the
1066 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001067 * needed.
1068 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069int audit_alloc(struct task_struct *tsk)
1070{
1071 struct audit_context *context;
1072 enum audit_state state;
Al Viroe048e022008-12-16 03:51:22 -05001073 char *key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Eric Parisb593d382008-01-08 17:38:31 -05001075 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 0; /* Return if not auditing. */
1077
Al Viroe048e022008-12-16 03:51:22 -05001078 state = audit_filter_task(tsk, &key);
Eric Paris56179a62012-01-03 14:23:06 -05001079 if (state == AUDIT_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return 0;
1081
1082 if (!(context = audit_alloc_context(state))) {
Al Viroe048e022008-12-16 03:51:22 -05001083 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 audit_log_lost("out of memory in audit_alloc");
1085 return -ENOMEM;
1086 }
Al Viroe048e022008-12-16 03:51:22 -05001087 context->filterkey = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 tsk->audit_context = context;
1090 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
1091 return 0;
1092}
1093
1094static inline void audit_free_context(struct audit_context *context)
1095{
1096 struct audit_context *previous;
1097 int count = 0;
1098
1099 do {
1100 previous = context->previous;
1101 if (previous || (count && count < 10)) {
1102 ++count;
1103 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
1104 " freeing multiple contexts (%d)\n",
1105 context->serial, context->major,
1106 context->name_count, count);
1107 }
1108 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001109 unroll_tree_refs(context, NULL, 0);
1110 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001112 kfree(context->filterkey);
Al Viro4f6b4342008-12-09 19:50:34 -05001113 kfree(context->sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 kfree(context);
1115 context = previous;
1116 } while (context);
1117 if (count >= 10)
1118 printk(KERN_ERR "audit: freed %d contexts\n", count);
1119}
1120
Joy Latten161a09e2006-11-27 13:11:54 -06001121void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001122{
1123 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +00001124 unsigned len;
1125 int error;
1126 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001127
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001128 security_task_getsecid(current, &sid);
Al Viroc4823bc2007-03-12 16:17:42 +00001129 if (!sid)
1130 return;
1131
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001132 error = security_secid_to_secctx(sid, &ctx, &len);
Al Viroc4823bc2007-03-12 16:17:42 +00001133 if (error) {
1134 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001135 goto error_path;
1136 return;
1137 }
1138
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001139 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001140 security_release_secctx(ctx, len);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +00001141 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001142
1143error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +00001144 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001145 return;
1146}
1147
Joy Latten161a09e2006-11-27 13:11:54 -06001148EXPORT_SYMBOL(audit_log_task_context);
1149
Peter Moodye23eb922012-06-14 10:04:35 -07001150void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -07001151{
Peter Moodye23eb922012-06-14 10:04:35 -07001152 const struct cred *cred;
Al Viro45d9bb02006-03-29 20:02:55 -05001153 char name[sizeof(tsk->comm)];
1154 struct mm_struct *mm = tsk->mm;
Peter Moodye23eb922012-06-14 10:04:35 -07001155 char *tty;
1156
1157 if (!ab)
1158 return;
Stephen Smalley219f0812005-04-18 10:47:35 -07001159
Al Viroe4951492006-03-29 20:17:10 -05001160 /* tsk == current */
Peter Moodye23eb922012-06-14 10:04:35 -07001161 cred = current_cred();
1162
1163 spin_lock_irq(&tsk->sighand->siglock);
1164 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1165 tty = tsk->signal->tty->name;
1166 else
1167 tty = "(none)";
1168 spin_unlock_irq(&tsk->sighand->siglock);
1169
1170
1171 audit_log_format(ab,
1172 " ppid=%ld pid=%d auid=%u uid=%u gid=%u"
1173 " euid=%u suid=%u fsuid=%u"
1174 " egid=%u sgid=%u fsgid=%u ses=%u tty=%s",
1175 sys_getppid(),
1176 tsk->pid,
Linus Torvalds88265322012-10-02 21:38:48 -07001177 from_kuid(&init_user_ns, tsk->loginuid),
1178 from_kuid(&init_user_ns, cred->uid),
1179 from_kgid(&init_user_ns, cred->gid),
1180 from_kuid(&init_user_ns, cred->euid),
1181 from_kuid(&init_user_ns, cred->suid),
1182 from_kuid(&init_user_ns, cred->fsuid),
1183 from_kgid(&init_user_ns, cred->egid),
1184 from_kgid(&init_user_ns, cred->sgid),
1185 from_kgid(&init_user_ns, cred->fsgid),
Peter Moodye23eb922012-06-14 10:04:35 -07001186 tsk->sessionid, tty);
Al Viroe4951492006-03-29 20:17:10 -05001187
Al Viro45d9bb02006-03-29 20:02:55 -05001188 get_task_comm(name, tsk);
David Woodhouse99e45eea2005-05-23 21:57:41 +01001189 audit_log_format(ab, " comm=");
1190 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -07001191
Al Viroe4951492006-03-29 20:17:10 -05001192 if (mm) {
1193 down_read(&mm->mmap_sem);
Konstantin Khlebnikov2dd8ad82012-10-08 16:28:51 -07001194 if (mm->exe_file)
1195 audit_log_d_path(ab, " exe=", &mm->exe_file->f_path);
Al Viroe4951492006-03-29 20:17:10 -05001196 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -07001197 }
Al Viroe4951492006-03-29 20:17:10 -05001198 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -07001199}
1200
Peter Moodye23eb922012-06-14 10:04:35 -07001201EXPORT_SYMBOL(audit_log_task_info);
1202
Amy Griffise54dc242007-03-29 18:01:04 -04001203static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001204 kuid_t auid, kuid_t uid, unsigned int sessionid,
Eric Paris4746ec52008-01-08 10:06:53 -05001205 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -04001206{
1207 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001208 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -04001209 u32 len;
1210 int rc = 0;
1211
1212 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
1213 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -05001214 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -04001215
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001216 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
1217 from_kuid(&init_user_ns, auid),
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001218 from_kuid(&init_user_ns, uid), sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001219 if (security_secid_to_secctx(sid, &ctx, &len)) {
Eric Parisc2a77802008-01-07 13:40:17 -05001220 audit_log_format(ab, " obj=(none)");
Amy Griffise54dc242007-03-29 18:01:04 -04001221 rc = 1;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001222 } else {
1223 audit_log_format(ab, " obj=%s", ctx);
1224 security_release_secctx(ctx, len);
1225 }
Eric Parisc2a77802008-01-07 13:40:17 -05001226 audit_log_format(ab, " ocomm=");
1227 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -04001228 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -04001229
1230 return rc;
1231}
1232
Eric Parisde6bbd12008-01-07 14:31:58 -05001233/*
1234 * to_send and len_sent accounting are very loose estimates. We aren't
1235 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001236 * within about 500 bytes (next page boundary)
Eric Parisde6bbd12008-01-07 14:31:58 -05001237 *
1238 * why snprintf? an int is up to 12 digits long. if we just assumed when
1239 * logging that a[%d]= was going to be 16 characters long we would be wasting
1240 * space in every audit message. In one 7500 byte message we can log up to
1241 * about 1000 min size arguments. That comes down to about 50% waste of space
1242 * if we didn't do the snprintf to find out how long arg_num_len was.
1243 */
1244static int audit_log_single_execve_arg(struct audit_context *context,
1245 struct audit_buffer **ab,
1246 int arg_num,
1247 size_t *len_sent,
1248 const char __user *p,
1249 char *buf)
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001250{
Eric Parisde6bbd12008-01-07 14:31:58 -05001251 char arg_num_len_buf[12];
1252 const char __user *tmp_p = p;
Eric Parisb87ce6e2009-06-11 14:31:34 -04001253 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1254 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
Eric Parisde6bbd12008-01-07 14:31:58 -05001255 size_t len, len_left, to_send;
1256 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1257 unsigned int i, has_cntl = 0, too_long = 0;
1258 int ret;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001259
Eric Parisde6bbd12008-01-07 14:31:58 -05001260 /* strnlen_user includes the null we don't want to send */
1261 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001262
Eric Parisde6bbd12008-01-07 14:31:58 -05001263 /*
1264 * We just created this mm, if we can't find the strings
1265 * we just copied into it something is _very_ wrong. Similar
1266 * for strings that are too long, we should not have created
1267 * any.
1268 */
Eric Parisb0abcfc2008-02-18 18:23:16 -05001269 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001270 WARN_ON(1);
1271 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001272 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001273 }
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001274
Eric Parisde6bbd12008-01-07 14:31:58 -05001275 /* walk the whole argument looking for non-ascii chars */
1276 do {
1277 if (len_left > MAX_EXECVE_AUDIT_LEN)
1278 to_send = MAX_EXECVE_AUDIT_LEN;
1279 else
1280 to_send = len_left;
1281 ret = copy_from_user(buf, tmp_p, to_send);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001282 /*
1283 * There is no reason for this copy to be short. We just
1284 * copied them here, and the mm hasn't been exposed to user-
1285 * space yet.
1286 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001287 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001288 WARN_ON(1);
1289 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001290 return -1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001291 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001292 buf[to_send] = '\0';
1293 has_cntl = audit_string_contains_control(buf, to_send);
1294 if (has_cntl) {
1295 /*
1296 * hex messages get logged as 2 bytes, so we can only
1297 * send half as much in each message
1298 */
1299 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1300 break;
1301 }
1302 len_left -= to_send;
1303 tmp_p += to_send;
1304 } while (len_left > 0);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001305
Eric Parisde6bbd12008-01-07 14:31:58 -05001306 len_left = len;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001307
Eric Parisde6bbd12008-01-07 14:31:58 -05001308 if (len > max_execve_audit_len)
1309 too_long = 1;
1310
1311 /* rewalk the argument actually logging the message */
1312 for (i = 0; len_left > 0; i++) {
1313 int room_left;
1314
1315 if (len_left > max_execve_audit_len)
1316 to_send = max_execve_audit_len;
1317 else
1318 to_send = len_left;
1319
1320 /* do we have space left to send this argument in this ab? */
1321 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1322 if (has_cntl)
1323 room_left -= (to_send * 2);
1324 else
1325 room_left -= to_send;
1326 if (room_left < 0) {
1327 *len_sent = 0;
1328 audit_log_end(*ab);
1329 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1330 if (!*ab)
1331 return 0;
1332 }
1333
1334 /*
1335 * first record needs to say how long the original string was
1336 * so we can be sure nothing was lost.
1337 */
1338 if ((i == 0) && (too_long))
Jiri Pirkoca96a892009-01-09 16:44:16 +01001339 audit_log_format(*ab, " a%d_len=%zu", arg_num,
Eric Parisde6bbd12008-01-07 14:31:58 -05001340 has_cntl ? 2*len : len);
1341
1342 /*
1343 * normally arguments are small enough to fit and we already
1344 * filled buf above when we checked for control characters
1345 * so don't bother with another copy_from_user
1346 */
1347 if (len >= max_execve_audit_len)
1348 ret = copy_from_user(buf, p, to_send);
1349 else
1350 ret = 0;
1351 if (ret) {
1352 WARN_ON(1);
1353 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001354 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001355 }
1356 buf[to_send] = '\0';
1357
1358 /* actually log it */
Jiri Pirkoca96a892009-01-09 16:44:16 +01001359 audit_log_format(*ab, " a%d", arg_num);
Eric Parisde6bbd12008-01-07 14:31:58 -05001360 if (too_long)
1361 audit_log_format(*ab, "[%d]", i);
1362 audit_log_format(*ab, "=");
1363 if (has_cntl)
Eric Parisb556f8a2008-04-18 10:12:59 -04001364 audit_log_n_hex(*ab, buf, to_send);
Eric Parisde6bbd12008-01-07 14:31:58 -05001365 else
Eric Paris9d960982009-06-11 14:31:37 -04001366 audit_log_string(*ab, buf);
Eric Parisde6bbd12008-01-07 14:31:58 -05001367
1368 p += to_send;
1369 len_left -= to_send;
1370 *len_sent += arg_num_len;
1371 if (has_cntl)
1372 *len_sent += to_send * 2;
1373 else
1374 *len_sent += to_send;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001375 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001376 /* include the null we didn't log */
1377 return len + 1;
1378}
1379
1380static void audit_log_execve_info(struct audit_context *context,
1381 struct audit_buffer **ab,
1382 struct audit_aux_data_execve *axi)
1383{
Xi Wang5afb8a32011-12-20 18:39:41 -05001384 int i, len;
1385 size_t len_sent = 0;
Eric Parisde6bbd12008-01-07 14:31:58 -05001386 const char __user *p;
1387 char *buf;
1388
1389 if (axi->mm != current->mm)
1390 return; /* execve failed, no additional info */
1391
1392 p = (const char __user *)axi->mm->arg_start;
1393
Jiri Pirkoca96a892009-01-09 16:44:16 +01001394 audit_log_format(*ab, "argc=%d", axi->argc);
Eric Parisde6bbd12008-01-07 14:31:58 -05001395
1396 /*
1397 * we need some kernel buffer to hold the userspace args. Just
1398 * allocate one big one rather than allocating one of the right size
1399 * for every single argument inside audit_log_single_execve_arg()
1400 * should be <8k allocation so should be pretty safe.
1401 */
1402 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1403 if (!buf) {
1404 audit_panic("out of memory for argv string\n");
1405 return;
1406 }
1407
1408 for (i = 0; i < axi->argc; i++) {
1409 len = audit_log_single_execve_arg(context, ab, i,
1410 &len_sent, p, buf);
1411 if (len <= 0)
1412 break;
1413 p += len;
1414 }
1415 kfree(buf);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001416}
1417
Eric Paris851f7ff2008-11-11 21:48:14 +11001418static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1419{
1420 int i;
1421
1422 audit_log_format(ab, " %s=", prefix);
1423 CAP_FOR_EACH_U32(i) {
1424 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1425 }
1426}
1427
1428static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1429{
1430 kernel_cap_t *perm = &name->fcap.permitted;
1431 kernel_cap_t *inh = &name->fcap.inheritable;
1432 int log = 0;
1433
1434 if (!cap_isclear(*perm)) {
1435 audit_log_cap(ab, "cap_fp", perm);
1436 log = 1;
1437 }
1438 if (!cap_isclear(*inh)) {
1439 audit_log_cap(ab, "cap_fi", inh);
1440 log = 1;
1441 }
1442
1443 if (log)
1444 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1445}
1446
Al Viroa33e6752008-12-10 03:40:06 -05001447static void show_special(struct audit_context *context, int *call_panic)
Al Virof3298dc2008-12-10 03:16:51 -05001448{
1449 struct audit_buffer *ab;
1450 int i;
1451
1452 ab = audit_log_start(context, GFP_KERNEL, context->type);
1453 if (!ab)
1454 return;
1455
1456 switch (context->type) {
1457 case AUDIT_SOCKETCALL: {
1458 int nargs = context->socketcall.nargs;
1459 audit_log_format(ab, "nargs=%d", nargs);
1460 for (i = 0; i < nargs; i++)
1461 audit_log_format(ab, " a%d=%lx", i,
1462 context->socketcall.args[i]);
1463 break; }
Al Viroa33e6752008-12-10 03:40:06 -05001464 case AUDIT_IPC: {
1465 u32 osid = context->ipc.osid;
1466
Al Viro2570ebb2011-07-27 14:03:22 -04001467 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001468 from_kuid(&init_user_ns, context->ipc.uid),
1469 from_kgid(&init_user_ns, context->ipc.gid),
1470 context->ipc.mode);
Al Viroa33e6752008-12-10 03:40:06 -05001471 if (osid) {
1472 char *ctx = NULL;
1473 u32 len;
1474 if (security_secid_to_secctx(osid, &ctx, &len)) {
1475 audit_log_format(ab, " osid=%u", osid);
1476 *call_panic = 1;
1477 } else {
1478 audit_log_format(ab, " obj=%s", ctx);
1479 security_release_secctx(ctx, len);
1480 }
1481 }
Al Viroe816f372008-12-10 03:47:15 -05001482 if (context->ipc.has_perm) {
1483 audit_log_end(ab);
1484 ab = audit_log_start(context, GFP_KERNEL,
1485 AUDIT_IPC_SET_PERM);
1486 audit_log_format(ab,
Al Viro2570ebb2011-07-27 14:03:22 -04001487 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
Al Viroe816f372008-12-10 03:47:15 -05001488 context->ipc.qbytes,
1489 context->ipc.perm_uid,
1490 context->ipc.perm_gid,
1491 context->ipc.perm_mode);
1492 if (!ab)
1493 return;
1494 }
Al Viroa33e6752008-12-10 03:40:06 -05001495 break; }
Al Viro564f6992008-12-14 04:02:26 -05001496 case AUDIT_MQ_OPEN: {
1497 audit_log_format(ab,
Al Virodf0a4282011-07-26 05:26:10 -04001498 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
Al Viro564f6992008-12-14 04:02:26 -05001499 "mq_msgsize=%ld mq_curmsgs=%ld",
1500 context->mq_open.oflag, context->mq_open.mode,
1501 context->mq_open.attr.mq_flags,
1502 context->mq_open.attr.mq_maxmsg,
1503 context->mq_open.attr.mq_msgsize,
1504 context->mq_open.attr.mq_curmsgs);
1505 break; }
Al Viroc32c8af2008-12-14 03:46:48 -05001506 case AUDIT_MQ_SENDRECV: {
1507 audit_log_format(ab,
1508 "mqdes=%d msg_len=%zd msg_prio=%u "
1509 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1510 context->mq_sendrecv.mqdes,
1511 context->mq_sendrecv.msg_len,
1512 context->mq_sendrecv.msg_prio,
1513 context->mq_sendrecv.abs_timeout.tv_sec,
1514 context->mq_sendrecv.abs_timeout.tv_nsec);
1515 break; }
Al Viro20114f72008-12-10 07:16:12 -05001516 case AUDIT_MQ_NOTIFY: {
1517 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1518 context->mq_notify.mqdes,
1519 context->mq_notify.sigev_signo);
1520 break; }
Al Viro73929062008-12-10 06:58:59 -05001521 case AUDIT_MQ_GETSETATTR: {
1522 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1523 audit_log_format(ab,
1524 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1525 "mq_curmsgs=%ld ",
1526 context->mq_getsetattr.mqdes,
1527 attr->mq_flags, attr->mq_maxmsg,
1528 attr->mq_msgsize, attr->mq_curmsgs);
1529 break; }
Al Viro57f71a02009-01-04 14:52:57 -05001530 case AUDIT_CAPSET: {
1531 audit_log_format(ab, "pid=%d", context->capset.pid);
1532 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1533 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1534 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1535 break; }
Al Viro120a7952010-10-30 02:54:44 -04001536 case AUDIT_MMAP: {
1537 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1538 context->mmap.flags);
1539 break; }
Al Virof3298dc2008-12-10 03:16:51 -05001540 }
1541 audit_log_end(ab);
1542}
1543
Eric Paris5195d8e2012-01-03 14:23:05 -05001544static void audit_log_name(struct audit_context *context, struct audit_names *n,
1545 int record_num, int *call_panic)
1546{
1547 struct audit_buffer *ab;
1548 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1549 if (!ab)
1550 return; /* audit_panic has been called */
1551
1552 audit_log_format(ab, "item=%d", record_num);
1553
1554 if (n->name) {
1555 switch (n->name_len) {
1556 case AUDIT_NAME_FULL:
1557 /* log the full path */
1558 audit_log_format(ab, " name=");
1559 audit_log_untrustedstring(ab, n->name);
1560 break;
1561 case 0:
1562 /* name was specified as a relative path and the
1563 * directory component is the cwd */
Kees Cookc158a352012-01-06 14:07:10 -08001564 audit_log_d_path(ab, " name=", &context->pwd);
Eric Paris5195d8e2012-01-03 14:23:05 -05001565 break;
1566 default:
1567 /* log the name's directory component */
1568 audit_log_format(ab, " name=");
1569 audit_log_n_untrustedstring(ab, n->name,
1570 n->name_len);
1571 }
1572 } else
1573 audit_log_format(ab, " name=(null)");
1574
1575 if (n->ino != (unsigned long)-1) {
1576 audit_log_format(ab, " inode=%lu"
1577 " dev=%02x:%02x mode=%#ho"
1578 " ouid=%u ogid=%u rdev=%02x:%02x",
1579 n->ino,
1580 MAJOR(n->dev),
1581 MINOR(n->dev),
1582 n->mode,
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001583 from_kuid(&init_user_ns, n->uid),
1584 from_kgid(&init_user_ns, n->gid),
Eric Paris5195d8e2012-01-03 14:23:05 -05001585 MAJOR(n->rdev),
1586 MINOR(n->rdev));
1587 }
1588 if (n->osid != 0) {
1589 char *ctx = NULL;
1590 u32 len;
1591 if (security_secid_to_secctx(
1592 n->osid, &ctx, &len)) {
1593 audit_log_format(ab, " osid=%u", n->osid);
1594 *call_panic = 2;
1595 } else {
1596 audit_log_format(ab, " obj=%s", ctx);
1597 security_release_secctx(ctx, len);
1598 }
1599 }
1600
1601 audit_log_fcaps(ab, n);
1602
1603 audit_log_end(ab);
1604}
1605
Al Viroe4951492006-03-29 20:17:10 -05001606static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001608 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001610 struct audit_aux_data *aux;
Eric Paris5195d8e2012-01-03 14:23:05 -05001611 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Al Viroe4951492006-03-29 20:17:10 -05001613 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001614 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001615
1616 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (!ab)
1618 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001619 audit_log_format(ab, "arch=%x syscall=%d",
1620 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (context->personality != PER_LINUX)
1622 audit_log_format(ab, " per=%lx", context->personality);
1623 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001624 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001625 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1626 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 audit_log_format(ab,
Peter Moodye23eb922012-06-14 10:04:35 -07001629 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1630 context->argv[0],
1631 context->argv[1],
1632 context->argv[2],
1633 context->argv[3],
1634 context->name_count);
Alan Coxeb84a202006-09-29 02:01:41 -07001635
Al Viroe4951492006-03-29 20:17:10 -05001636 audit_log_task_info(ab, tsk);
Eric Paris9d960982009-06-11 14:31:37 -04001637 audit_log_key(ab, context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
David Woodhouse7551ced2005-05-26 12:04:57 +01001640 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001641
Al Viroe4951492006-03-29 20:17:10 -05001642 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (!ab)
1644 continue; /* audit_panic has been called */
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001647
Al Viro473ae302006-04-26 14:04:08 -04001648 case AUDIT_EXECVE: {
1649 struct audit_aux_data_execve *axi = (void *)aux;
Eric Parisde6bbd12008-01-07 14:31:58 -05001650 audit_log_execve_info(context, &ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001651 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001652
Eric Paris3fc689e2008-11-11 21:48:18 +11001653 case AUDIT_BPRM_FCAPS: {
1654 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1655 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1656 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1657 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1658 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1659 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1660 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1661 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1662 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1663 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1664 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1665 break; }
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670
Al Virof3298dc2008-12-10 03:16:51 -05001671 if (context->type)
Al Viroa33e6752008-12-10 03:40:06 -05001672 show_special(context, &call_panic);
Al Virof3298dc2008-12-10 03:16:51 -05001673
Al Viro157cf642008-12-14 04:57:47 -05001674 if (context->fds[0] >= 0) {
1675 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1676 if (ab) {
1677 audit_log_format(ab, "fd0=%d fd1=%d",
1678 context->fds[0], context->fds[1]);
1679 audit_log_end(ab);
1680 }
1681 }
1682
Al Viro4f6b4342008-12-09 19:50:34 -05001683 if (context->sockaddr_len) {
1684 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1685 if (ab) {
1686 audit_log_format(ab, "saddr=");
1687 audit_log_n_hex(ab, (void *)context->sockaddr,
1688 context->sockaddr_len);
1689 audit_log_end(ab);
1690 }
1691 }
1692
Amy Griffise54dc242007-03-29 18:01:04 -04001693 for (aux = context->aux_pids; aux; aux = aux->next) {
1694 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001695
1696 for (i = 0; i < axs->pid_count; i++)
1697 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001698 axs->target_auid[i],
1699 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001700 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001701 axs->target_sid[i],
1702 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001703 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001704 }
1705
Amy Griffise54dc242007-03-29 18:01:04 -04001706 if (context->target_pid &&
1707 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001708 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001709 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001710 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001711 call_panic = 1;
1712
Jan Blunck44707fd2008-02-14 19:38:33 -08001713 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001714 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001715 if (ab) {
Kees Cookc158a352012-01-06 14:07:10 -08001716 audit_log_d_path(ab, " cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001717 audit_log_end(ab);
1718 }
1719 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001720
Eric Paris5195d8e2012-01-03 14:23:05 -05001721 i = 0;
1722 list_for_each_entry(n, &context->names_list, list)
1723 audit_log_name(context, n, i++, &call_panic);
Eric Parisc0641f22008-01-07 13:49:15 -05001724
1725 /* Send end of event record to help user space know we are finished */
1726 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1727 if (ab)
1728 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001729 if (call_panic)
1730 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001733/**
1734 * audit_free - free a per-task audit context
1735 * @tsk: task whose audit context block to free
1736 *
Al Virofa84cb92006-03-29 20:30:19 -05001737 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001738 */
Eric Parisa4ff8db2012-01-03 14:23:07 -05001739void __audit_free(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 struct audit_context *context;
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 context = audit_get_context(tsk, 0, 0);
Eric Paris56179a62012-01-03 14:23:06 -05001744 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return;
1746
1747 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001748 * function (e.g., exit_group), then free context block.
1749 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001750 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001751 /* that can happen only if we are called from do_exit() */
Al Viro0590b932008-12-14 23:45:27 -05001752 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001753 audit_log_exit(context, tsk);
Al Viro916d7572009-06-24 00:02:38 -04001754 if (!list_empty(&context->killed_trees))
1755 audit_kill_trees(&context->killed_trees);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 audit_free_context(context);
1758}
1759
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001760/**
1761 * audit_syscall_entry - fill in an audit record at syscall entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001762 * @arch: architecture type
1763 * @major: major syscall type (function)
1764 * @a1: additional syscall register 1
1765 * @a2: additional syscall register 2
1766 * @a3: additional syscall register 3
1767 * @a4: additional syscall register 4
1768 *
1769 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * audit context was created when the task was created and the state or
1771 * filters demand the audit context be built. If the state from the
1772 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1773 * then the record will be written at syscall exit time (otherwise, it
1774 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001775 * be written).
1776 */
Eric Parisb05d8442012-01-03 14:23:06 -05001777void __audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 unsigned long a1, unsigned long a2,
1779 unsigned long a3, unsigned long a4)
1780{
Al Viro5411be52006-03-29 20:23:36 -05001781 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct audit_context *context = tsk->audit_context;
1783 enum audit_state state;
1784
Eric Paris56179a62012-01-03 14:23:06 -05001785 if (!context)
Roland McGrath86a1c342008-06-23 15:37:04 -07001786 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001788 /*
1789 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 * calls in kernel_thread via the entry.S interface, instead of
1791 * with direct calls. (If you are porting to a new
1792 * architecture, hitting this condition can indicate that you
1793 * got the _exit/_leave calls backward in entry.S.)
1794 *
1795 * i386 no
1796 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001797 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 *
1799 * This also happens with vm86 emulation in a non-nested manner
1800 * (entries without exits), so this case must be caught.
1801 */
1802 if (context->in_syscall) {
1803 struct audit_context *newctx;
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805#if AUDIT_DEBUG
1806 printk(KERN_ERR
1807 "audit(:%d) pid=%d in syscall=%d;"
1808 " entering syscall=%d\n",
1809 context->serial, tsk->pid, context->major, major);
1810#endif
1811 newctx = audit_alloc_context(context->state);
1812 if (newctx) {
1813 newctx->previous = context;
1814 context = newctx;
1815 tsk->audit_context = newctx;
1816 } else {
1817 /* If we can't alloc a new context, the best we
1818 * can do is to leak memory (any pending putname
1819 * will be lost). The only other alternative is
1820 * to abandon auditing. */
1821 audit_zero_context(context, context->state);
1822 }
1823 }
1824 BUG_ON(context->in_syscall || context->name_count);
1825
1826 if (!audit_enabled)
1827 return;
1828
2fd6f582005-04-29 16:08:28 +01001829 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 context->major = major;
1831 context->argv[0] = a1;
1832 context->argv[1] = a2;
1833 context->argv[2] = a3;
1834 context->argv[3] = a4;
1835
1836 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001837 context->dummy = !audit_n_rules;
Al Viro0590b932008-12-14 23:45:27 -05001838 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1839 context->prio = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001840 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Al Viro0590b932008-12-14 23:45:27 -05001841 }
Eric Paris56179a62012-01-03 14:23:06 -05001842 if (state == AUDIT_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return;
1844
David Woodhousece625a82005-07-18 14:24:46 -04001845 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 context->ctime = CURRENT_TIME;
1847 context->in_syscall = 1;
Al Viro0590b932008-12-14 23:45:27 -05001848 context->current_state = state;
Alexander Viro419c58f2006-09-29 00:08:50 -04001849 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001852/**
1853 * audit_syscall_exit - deallocate audit context after a system call
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001854 * @success: success value of the syscall
1855 * @return_code: return value of the syscall
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001856 *
1857 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001859 * filtering, or because some other part of the kernel wrote an audit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001861 * free the names stored from getname().
1862 */
Eric Parisd7e75282012-01-03 14:23:06 -05001863void __audit_syscall_exit(int success, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Al Viro5411be52006-03-29 20:23:36 -05001865 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 struct audit_context *context;
1867
Eric Parisd7e75282012-01-03 14:23:06 -05001868 if (success)
1869 success = AUDITSC_SUCCESS;
1870 else
1871 success = AUDITSC_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Eric Parisd7e75282012-01-03 14:23:06 -05001873 context = audit_get_context(tsk, success, return_code);
Eric Paris56179a62012-01-03 14:23:06 -05001874 if (!context)
Al Viro97e94c42006-03-29 20:26:24 -05001875 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Al Viro0590b932008-12-14 23:45:27 -05001877 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001878 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 context->in_syscall = 0;
Al Viro0590b932008-12-14 23:45:27 -05001881 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
2fd6f582005-04-29 16:08:28 +01001882
Al Viro916d7572009-06-24 00:02:38 -04001883 if (!list_empty(&context->killed_trees))
1884 audit_kill_trees(&context->killed_trees);
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (context->previous) {
1887 struct audit_context *new_context = context->previous;
1888 context->previous = NULL;
1889 audit_free_context(context);
1890 tsk->audit_context = new_context;
1891 } else {
1892 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001893 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001895 context->aux = NULL;
1896 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001897 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001898 context->target_sid = 0;
Al Viro4f6b4342008-12-09 19:50:34 -05001899 context->sockaddr_len = 0;
Al Virof3298dc2008-12-10 03:16:51 -05001900 context->type = 0;
Al Viro157cf642008-12-14 04:57:47 -05001901 context->fds[0] = -1;
Al Viroe048e022008-12-16 03:51:22 -05001902 if (context->state != AUDIT_RECORD_CONTEXT) {
1903 kfree(context->filterkey);
1904 context->filterkey = NULL;
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 tsk->audit_context = context;
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Al Viro74c3cbe2007-07-22 08:04:18 -04001910static inline void handle_one(const struct inode *inode)
1911{
1912#ifdef CONFIG_AUDIT_TREE
1913 struct audit_context *context;
1914 struct audit_tree_refs *p;
1915 struct audit_chunk *chunk;
1916 int count;
Eric Parise61ce862009-12-17 21:24:24 -05001917 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
Al Viro74c3cbe2007-07-22 08:04:18 -04001918 return;
1919 context = current->audit_context;
1920 p = context->trees;
1921 count = context->tree_count;
1922 rcu_read_lock();
1923 chunk = audit_tree_lookup(inode);
1924 rcu_read_unlock();
1925 if (!chunk)
1926 return;
1927 if (likely(put_tree_ref(context, chunk)))
1928 return;
1929 if (unlikely(!grow_tree_refs(context))) {
Eric Paris436c4052008-04-18 10:01:04 -04001930 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001931 audit_set_auditable(context);
1932 audit_put_chunk(chunk);
1933 unroll_tree_refs(context, p, count);
1934 return;
1935 }
1936 put_tree_ref(context, chunk);
1937#endif
1938}
1939
1940static void handle_path(const struct dentry *dentry)
1941{
1942#ifdef CONFIG_AUDIT_TREE
1943 struct audit_context *context;
1944 struct audit_tree_refs *p;
1945 const struct dentry *d, *parent;
1946 struct audit_chunk *drop;
1947 unsigned long seq;
1948 int count;
1949
1950 context = current->audit_context;
1951 p = context->trees;
1952 count = context->tree_count;
1953retry:
1954 drop = NULL;
1955 d = dentry;
1956 rcu_read_lock();
1957 seq = read_seqbegin(&rename_lock);
1958 for(;;) {
1959 struct inode *inode = d->d_inode;
Eric Parise61ce862009-12-17 21:24:24 -05001960 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
Al Viro74c3cbe2007-07-22 08:04:18 -04001961 struct audit_chunk *chunk;
1962 chunk = audit_tree_lookup(inode);
1963 if (chunk) {
1964 if (unlikely(!put_tree_ref(context, chunk))) {
1965 drop = chunk;
1966 break;
1967 }
1968 }
1969 }
1970 parent = d->d_parent;
1971 if (parent == d)
1972 break;
1973 d = parent;
1974 }
1975 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1976 rcu_read_unlock();
1977 if (!drop) {
1978 /* just a race with rename */
1979 unroll_tree_refs(context, p, count);
1980 goto retry;
1981 }
1982 audit_put_chunk(drop);
1983 if (grow_tree_refs(context)) {
1984 /* OK, got more space */
1985 unroll_tree_refs(context, p, count);
1986 goto retry;
1987 }
1988 /* too bad */
1989 printk(KERN_WARNING
Eric Paris436c4052008-04-18 10:01:04 -04001990 "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001991 unroll_tree_refs(context, p, count);
1992 audit_set_auditable(context);
1993 return;
1994 }
1995 rcu_read_unlock();
1996#endif
1997}
1998
Jeff Layton78e2e802012-10-10 15:25:22 -04001999static struct audit_names *audit_alloc_name(struct audit_context *context,
2000 unsigned char type)
Eric Paris5195d8e2012-01-03 14:23:05 -05002001{
2002 struct audit_names *aname;
2003
2004 if (context->name_count < AUDIT_NAMES) {
2005 aname = &context->preallocated_names[context->name_count];
2006 memset(aname, 0, sizeof(*aname));
2007 } else {
2008 aname = kzalloc(sizeof(*aname), GFP_NOFS);
2009 if (!aname)
2010 return NULL;
2011 aname->should_free = true;
2012 }
2013
2014 aname->ino = (unsigned long)-1;
Jeff Layton78e2e802012-10-10 15:25:22 -04002015 aname->type = type;
Eric Paris5195d8e2012-01-03 14:23:05 -05002016 list_add_tail(&aname->list, &context->names_list);
2017
2018 context->name_count++;
2019#if AUDIT_DEBUG
2020 context->ino_count++;
2021#endif
2022 return aname;
2023}
2024
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002025/**
2026 * audit_getname - add a name to the list
2027 * @name: name to add
2028 *
2029 * Add a name to the list of audit names for this context.
2030 * Called from fs/namei.c:getname().
2031 */
Al Virod8945bb52006-05-18 16:01:30 -04002032void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
2034 struct audit_context *context = current->audit_context;
Eric Paris5195d8e2012-01-03 14:23:05 -05002035 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (!context->in_syscall) {
2038#if AUDIT_DEBUG == 2
2039 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
2040 __FILE__, __LINE__, context->serial, name);
2041 dump_stack();
2042#endif
2043 return;
2044 }
Eric Paris5195d8e2012-01-03 14:23:05 -05002045
Jeff Layton78e2e802012-10-10 15:25:22 -04002046 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
Eric Paris5195d8e2012-01-03 14:23:05 -05002047 if (!n)
2048 return;
2049
2050 n->name = name;
2051 n->name_len = AUDIT_NAME_FULL;
2052 n->name_put = true;
2053
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002054 if (!context->pwd.dentry)
2055 get_fs_pwd(current->fs, &context->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
2057
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002058/* audit_putname - intercept a putname request
2059 * @name: name to intercept and delay for putname
2060 *
2061 * If we have stored the name from getname in the audit context,
2062 * then we delay the putname until syscall exit.
2063 * Called from include/linux/fs.h:putname().
2064 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065void audit_putname(const char *name)
2066{
2067 struct audit_context *context = current->audit_context;
2068
2069 BUG_ON(!context);
2070 if (!context->in_syscall) {
2071#if AUDIT_DEBUG == 2
2072 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
2073 __FILE__, __LINE__, context->serial, name);
2074 if (context->name_count) {
Eric Paris5195d8e2012-01-03 14:23:05 -05002075 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 int i;
Eric Paris5195d8e2012-01-03 14:23:05 -05002077
2078 list_for_each_entry(n, &context->names_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 printk(KERN_ERR "name[%d] = %p = %s\n", i,
Eric Paris5195d8e2012-01-03 14:23:05 -05002080 n->name, n->name ?: "(null)");
2081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#endif
2083 __putname(name);
2084 }
2085#if AUDIT_DEBUG
2086 else {
2087 ++context->put_count;
2088 if (context->put_count > context->name_count) {
2089 printk(KERN_ERR "%s:%d(:%d): major=%d"
2090 " in_syscall=%d putname(%p) name_count=%d"
2091 " put_count=%d\n",
2092 __FILE__, __LINE__,
2093 context->serial, context->major,
2094 context->in_syscall, name, context->name_count,
2095 context->put_count);
2096 dump_stack();
2097 }
2098 }
2099#endif
2100}
2101
Eric Paris851f7ff2008-11-11 21:48:14 +11002102static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
2103{
2104 struct cpu_vfs_cap_data caps;
2105 int rc;
2106
Eric Paris851f7ff2008-11-11 21:48:14 +11002107 if (!dentry)
2108 return 0;
2109
2110 rc = get_vfs_caps_from_disk(dentry, &caps);
2111 if (rc)
2112 return rc;
2113
2114 name->fcap.permitted = caps.permitted;
2115 name->fcap.inheritable = caps.inheritable;
2116 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2117 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2118
2119 return 0;
2120}
2121
2122
Amy Griffis3e2efce2006-07-13 13:16:02 -04002123/* Copy inode data into an audit_names. */
Eric Paris851f7ff2008-11-11 21:48:14 +11002124static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
2125 const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002126{
Amy Griffis3e2efce2006-07-13 13:16:02 -04002127 name->ino = inode->i_ino;
2128 name->dev = inode->i_sb->s_dev;
2129 name->mode = inode->i_mode;
2130 name->uid = inode->i_uid;
2131 name->gid = inode->i_gid;
2132 name->rdev = inode->i_rdev;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002133 security_inode_getsecid(inode, &name->osid);
Eric Paris851f7ff2008-11-11 21:48:14 +11002134 audit_copy_fcaps(name, dentry);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002135}
2136
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002137/**
2138 * audit_inode - store the inode and device from a lookup
2139 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07002140 * @dentry: dentry being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002141 *
2142 * Called from fs/namei.c:path_lookup().
2143 */
Al Viro5a190ae2007-06-07 12:19:32 -04002144void __audit_inode(const char *name, const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04002147 const struct inode *inode = dentry->d_inode;
Eric Paris5195d8e2012-01-03 14:23:05 -05002148 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 if (!context->in_syscall)
2151 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05002152
Jeff Layton9cec9d62012-10-10 15:25:21 -04002153 if (!name)
2154 goto out_alloc;
2155
Eric Paris5195d8e2012-01-03 14:23:05 -05002156 list_for_each_entry_reverse(n, &context->names_list, list) {
Jeff Layton9cec9d62012-10-10 15:25:21 -04002157 if (n->name == name)
Eric Paris5195d8e2012-01-03 14:23:05 -05002158 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
Eric Paris5195d8e2012-01-03 14:23:05 -05002160
Jeff Layton9cec9d62012-10-10 15:25:21 -04002161out_alloc:
Eric Paris5195d8e2012-01-03 14:23:05 -05002162 /* unable to find the name from a previous getname() */
Jeff Layton78e2e802012-10-10 15:25:22 -04002163 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
Eric Paris5195d8e2012-01-03 14:23:05 -05002164 if (!n)
2165 return;
2166out:
Al Viro74c3cbe2007-07-22 08:04:18 -04002167 handle_path(dentry);
Eric Paris5195d8e2012-01-03 14:23:05 -05002168 audit_copy_inode(n, dentry, inode);
Jeff Layton78e2e802012-10-10 15:25:22 -04002169 n->type = AUDIT_TYPE_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Amy Griffis73241cc2005-11-03 16:00:25 +00002172/**
Jeff Laytonc43a25a2012-10-10 15:25:21 -04002173 * __audit_inode_child - collect inode info for created/removed objects
Amy Griffis73d3ec52006-07-13 13:16:39 -04002174 * @parent: inode of dentry parent
Jeff Laytonc43a25a2012-10-10 15:25:21 -04002175 * @dentry: dentry being audited
Amy Griffis73241cc2005-11-03 16:00:25 +00002176 *
2177 * For syscalls that create or remove filesystem objects, audit_inode
2178 * can only collect information for the filesystem object's parent.
2179 * This call updates the audit context with the child's information.
2180 * Syscalls that create a new filesystem object must be hooked after
2181 * the object is created. Syscalls that remove a filesystem object
2182 * must be hooked prior, in order to capture the target inode during
2183 * unsuccessful attempts.
2184 */
Jeff Laytonc43a25a2012-10-10 15:25:21 -04002185void __audit_inode_child(const struct inode *parent,
2186 const struct dentry *dentry)
Amy Griffis73241cc2005-11-03 16:00:25 +00002187{
Amy Griffis73241cc2005-11-03 16:00:25 +00002188 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05002189 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04002190 const struct inode *inode = dentry->d_inode;
Al Virocccc6bb2009-12-25 05:07:33 -05002191 const char *dname = dentry->d_name.name;
Eric Paris5195d8e2012-01-03 14:23:05 -05002192 struct audit_names *n;
Amy Griffis9c937dc2006-06-08 23:19:31 -04002193 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00002194
2195 if (!context->in_syscall)
2196 return;
2197
Al Viro74c3cbe2007-07-22 08:04:18 -04002198 if (inode)
2199 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00002200
Amy Griffis5712e882007-02-13 14:15:22 -05002201 /* parent is more likely, look for it first */
Eric Paris5195d8e2012-01-03 14:23:05 -05002202 list_for_each_entry(n, &context->names_list, list) {
Amy Griffis5712e882007-02-13 14:15:22 -05002203 if (!n->name)
2204 continue;
2205
2206 if (n->ino == parent->i_ino &&
2207 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2208 n->name_len = dirlen; /* update parent data in place */
2209 found_parent = n->name;
2210 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04002211 }
Steve Grubbac9910c2006-09-28 14:31:32 -04002212 }
Amy Griffis73241cc2005-11-03 16:00:25 +00002213
Amy Griffis5712e882007-02-13 14:15:22 -05002214 /* no matching parent, look for matching child */
Eric Paris5195d8e2012-01-03 14:23:05 -05002215 list_for_each_entry(n, &context->names_list, list) {
Amy Griffis5712e882007-02-13 14:15:22 -05002216 if (!n->name)
2217 continue;
2218
2219 /* strcmp() is the more likely scenario */
2220 if (!strcmp(dname, n->name) ||
2221 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2222 if (inode)
Jeff Layton1c2e51e2012-10-10 15:25:20 -04002223 audit_copy_inode(n, dentry, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002224 else
2225 n->ino = (unsigned long)-1;
Jeff Layton78e2e802012-10-10 15:25:22 -04002226 n->type = AUDIT_TYPE_NORMAL;
Amy Griffis5712e882007-02-13 14:15:22 -05002227 found_child = n->name;
2228 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04002229 }
Amy Griffis5712e882007-02-13 14:15:22 -05002230 }
2231
2232add_names:
2233 if (!found_parent) {
Jeff Layton78e2e802012-10-10 15:25:22 -04002234 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
Eric Paris5195d8e2012-01-03 14:23:05 -05002235 if (!n)
Amy Griffis5712e882007-02-13 14:15:22 -05002236 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05002237 audit_copy_inode(n, NULL, parent);
Amy Griffis73d3ec52006-07-13 13:16:39 -04002238 }
Amy Griffis5712e882007-02-13 14:15:22 -05002239
2240 if (!found_child) {
Jeff Layton78e2e802012-10-10 15:25:22 -04002241 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
Eric Paris5195d8e2012-01-03 14:23:05 -05002242 if (!n)
Amy Griffis5712e882007-02-13 14:15:22 -05002243 return;
Amy Griffis5712e882007-02-13 14:15:22 -05002244
2245 /* Re-use the name belonging to the slot for a matching parent
2246 * directory. All names for this context are relinquished in
2247 * audit_free_names() */
2248 if (found_parent) {
Eric Paris5195d8e2012-01-03 14:23:05 -05002249 n->name = found_parent;
2250 n->name_len = AUDIT_NAME_FULL;
Amy Griffis5712e882007-02-13 14:15:22 -05002251 /* don't call __putname() */
Eric Paris5195d8e2012-01-03 14:23:05 -05002252 n->name_put = false;
Amy Griffis5712e882007-02-13 14:15:22 -05002253 }
2254
2255 if (inode)
Jeff Layton1c2e51e2012-10-10 15:25:20 -04002256 audit_copy_inode(n, dentry, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002257 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04002258}
Trond Myklebust50e437d2007-06-07 22:44:34 -04002259EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04002260
2261/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002262 * auditsc_get_stamp - get local copies of audit_context values
2263 * @ctx: audit_context for the task
2264 * @t: timespec to store time recorded in the audit_context
2265 * @serial: serial value that is recorded in the audit_context
2266 *
2267 * Also sets the context as auditable.
2268 */
Al Viro48887e62008-12-06 01:05:50 -05002269int auditsc_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01002270 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Al Viro48887e62008-12-06 01:05:50 -05002272 if (!ctx->in_syscall)
2273 return 0;
David Woodhousece625a82005-07-18 14:24:46 -04002274 if (!ctx->serial)
2275 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01002276 t->tv_sec = ctx->ctime.tv_sec;
2277 t->tv_nsec = ctx->ctime.tv_nsec;
2278 *serial = ctx->serial;
Al Viro0590b932008-12-14 23:45:27 -05002279 if (!ctx->prio) {
2280 ctx->prio = 1;
2281 ctx->current_state = AUDIT_RECORD_CONTEXT;
2282 }
Al Viro48887e62008-12-06 01:05:50 -05002283 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Eric Paris4746ec52008-01-08 10:06:53 -05002286/* global counter which is incremented every time something logs in */
2287static atomic_t session_id = ATOMIC_INIT(0);
2288
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002289/**
Eric Paris0a300be2012-01-03 14:23:08 -05002290 * audit_set_loginuid - set current task's audit_context loginuid
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002291 * @loginuid: loginuid value
2292 *
2293 * Returns 0.
2294 *
2295 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2296 */
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002297int audit_set_loginuid(kuid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Eric Paris0a300be2012-01-03 14:23:08 -05002299 struct task_struct *task = current;
Steve Grubb41757102006-06-12 07:48:28 -04002300 struct audit_context *context = task->audit_context;
Eric Paris633b4542012-01-03 14:23:08 -05002301 unsigned int sessionid;
Steve Grubbc0404992005-05-13 18:17:42 +01002302
Eric Paris633b4542012-01-03 14:23:08 -05002303#ifdef CONFIG_AUDIT_LOGINUID_IMMUTABLE
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002304 if (uid_valid(task->loginuid))
Eric Paris633b4542012-01-03 14:23:08 -05002305 return -EPERM;
2306#else /* CONFIG_AUDIT_LOGINUID_IMMUTABLE */
2307 if (!capable(CAP_AUDIT_CONTROL))
2308 return -EPERM;
2309#endif /* CONFIG_AUDIT_LOGINUID_IMMUTABLE */
2310
2311 sessionid = atomic_inc_return(&session_id);
Al Virobfef93a2008-01-10 04:53:18 -05002312 if (context && context->in_syscall) {
2313 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04002314
Al Virobfef93a2008-01-10 04:53:18 -05002315 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2316 if (ab) {
2317 audit_log_format(ab, "login pid=%d uid=%u "
Eric Paris4746ec52008-01-08 10:06:53 -05002318 "old auid=%u new auid=%u"
2319 " old ses=%u new ses=%u",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002320 task->pid,
2321 from_kuid(&init_user_ns, task_uid(task)),
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002322 from_kuid(&init_user_ns, task->loginuid),
2323 from_kuid(&init_user_ns, loginuid),
Eric Paris4746ec52008-01-08 10:06:53 -05002324 task->sessionid, sessionid);
Al Virobfef93a2008-01-10 04:53:18 -05002325 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01002326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
Eric Paris4746ec52008-01-08 10:06:53 -05002328 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05002329 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return 0;
2331}
2332
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002333/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002334 * __audit_mq_open - record audit data for a POSIX MQ open
2335 * @oflag: open flag
2336 * @mode: mode bits
Randy Dunlap6b962552009-01-05 13:41:13 -08002337 * @attr: queue attributes
George C. Wilson20ca73b2006-05-24 16:09:55 -05002338 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002339 */
Al Virodf0a4282011-07-26 05:26:10 -04002340void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002341{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002342 struct audit_context *context = current->audit_context;
2343
Al Viro564f6992008-12-14 04:02:26 -05002344 if (attr)
2345 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2346 else
2347 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002348
Al Viro564f6992008-12-14 04:02:26 -05002349 context->mq_open.oflag = oflag;
2350 context->mq_open.mode = mode;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002351
Al Viro564f6992008-12-14 04:02:26 -05002352 context->type = AUDIT_MQ_OPEN;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002353}
2354
2355/**
Al Viroc32c8af2008-12-14 03:46:48 -05002356 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
George C. Wilson20ca73b2006-05-24 16:09:55 -05002357 * @mqdes: MQ descriptor
2358 * @msg_len: Message length
2359 * @msg_prio: Message priority
Al Viroc32c8af2008-12-14 03:46:48 -05002360 * @abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002361 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002362 */
Al Viroc32c8af2008-12-14 03:46:48 -05002363void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2364 const struct timespec *abs_timeout)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002365{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002366 struct audit_context *context = current->audit_context;
Al Viroc32c8af2008-12-14 03:46:48 -05002367 struct timespec *p = &context->mq_sendrecv.abs_timeout;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002368
Al Viroc32c8af2008-12-14 03:46:48 -05002369 if (abs_timeout)
2370 memcpy(p, abs_timeout, sizeof(struct timespec));
2371 else
2372 memset(p, 0, sizeof(struct timespec));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002373
Al Viroc32c8af2008-12-14 03:46:48 -05002374 context->mq_sendrecv.mqdes = mqdes;
2375 context->mq_sendrecv.msg_len = msg_len;
2376 context->mq_sendrecv.msg_prio = msg_prio;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002377
Al Viroc32c8af2008-12-14 03:46:48 -05002378 context->type = AUDIT_MQ_SENDRECV;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002379}
2380
2381/**
2382 * __audit_mq_notify - record audit data for a POSIX MQ notify
2383 * @mqdes: MQ descriptor
Randy Dunlap6b962552009-01-05 13:41:13 -08002384 * @notification: Notification event
George C. Wilson20ca73b2006-05-24 16:09:55 -05002385 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002386 */
2387
Al Viro20114f72008-12-10 07:16:12 -05002388void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002389{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002390 struct audit_context *context = current->audit_context;
2391
Al Viro20114f72008-12-10 07:16:12 -05002392 if (notification)
2393 context->mq_notify.sigev_signo = notification->sigev_signo;
2394 else
2395 context->mq_notify.sigev_signo = 0;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002396
Al Viro20114f72008-12-10 07:16:12 -05002397 context->mq_notify.mqdes = mqdes;
2398 context->type = AUDIT_MQ_NOTIFY;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002399}
2400
2401/**
2402 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2403 * @mqdes: MQ descriptor
2404 * @mqstat: MQ flags
2405 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002406 */
Al Viro73929062008-12-10 06:58:59 -05002407void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002408{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002409 struct audit_context *context = current->audit_context;
Al Viro73929062008-12-10 06:58:59 -05002410 context->mq_getsetattr.mqdes = mqdes;
2411 context->mq_getsetattr.mqstat = *mqstat;
2412 context->type = AUDIT_MQ_GETSETATTR;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002413}
2414
2415/**
Steve Grubb073115d2006-04-02 17:07:33 -04002416 * audit_ipc_obj - record audit data for ipc object
2417 * @ipcp: ipc permissions
2418 *
Steve Grubb073115d2006-04-02 17:07:33 -04002419 */
Al Viroa33e6752008-12-10 03:40:06 -05002420void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002421{
Steve Grubb073115d2006-04-02 17:07:33 -04002422 struct audit_context *context = current->audit_context;
Al Viroa33e6752008-12-10 03:40:06 -05002423 context->ipc.uid = ipcp->uid;
2424 context->ipc.gid = ipcp->gid;
2425 context->ipc.mode = ipcp->mode;
Al Viroe816f372008-12-10 03:47:15 -05002426 context->ipc.has_perm = 0;
Al Viroa33e6752008-12-10 03:40:06 -05002427 security_ipc_getsecid(ipcp, &context->ipc.osid);
2428 context->type = AUDIT_IPC;
Steve Grubb073115d2006-04-02 17:07:33 -04002429}
2430
2431/**
2432 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002433 * @qbytes: msgq bytes
2434 * @uid: msgq user id
2435 * @gid: msgq group id
2436 * @mode: msgq mode (permissions)
2437 *
Al Viroe816f372008-12-10 03:47:15 -05002438 * Called only after audit_ipc_obj().
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002439 */
Al Viro2570ebb2011-07-27 14:03:22 -04002440void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 struct audit_context *context = current->audit_context;
2443
Al Viroe816f372008-12-10 03:47:15 -05002444 context->ipc.qbytes = qbytes;
2445 context->ipc.perm_uid = uid;
2446 context->ipc.perm_gid = gid;
2447 context->ipc.perm_mode = mode;
2448 context->ipc.has_perm = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002450
Eric Paris07c49412012-01-03 14:23:07 -05002451int __audit_bprm(struct linux_binprm *bprm)
Al Viro473ae302006-04-26 14:04:08 -04002452{
2453 struct audit_aux_data_execve *ax;
2454 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002455
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002456 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002457 if (!ax)
2458 return -ENOMEM;
2459
2460 ax->argc = bprm->argc;
2461 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002462 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002463 ax->d.type = AUDIT_EXECVE;
2464 ax->d.next = context->aux;
2465 context->aux = (void *)ax;
2466 return 0;
2467}
2468
2469
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002470/**
2471 * audit_socketcall - record audit data for sys_socketcall
2472 * @nargs: number of args
2473 * @args: args array
2474 *
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002475 */
Eric Paris07c49412012-01-03 14:23:07 -05002476void __audit_socketcall(int nargs, unsigned long *args)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002477{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002478 struct audit_context *context = current->audit_context;
2479
Al Virof3298dc2008-12-10 03:16:51 -05002480 context->type = AUDIT_SOCKETCALL;
2481 context->socketcall.nargs = nargs;
2482 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002483}
2484
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002485/**
Al Virodb349502007-02-07 01:48:00 -05002486 * __audit_fd_pair - record audit data for pipe and socketpair
2487 * @fd1: the first file descriptor
2488 * @fd2: the second file descriptor
2489 *
Al Virodb349502007-02-07 01:48:00 -05002490 */
Al Viro157cf642008-12-14 04:57:47 -05002491void __audit_fd_pair(int fd1, int fd2)
Al Virodb349502007-02-07 01:48:00 -05002492{
2493 struct audit_context *context = current->audit_context;
Al Viro157cf642008-12-14 04:57:47 -05002494 context->fds[0] = fd1;
2495 context->fds[1] = fd2;
Al Virodb349502007-02-07 01:48:00 -05002496}
2497
2498/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002499 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2500 * @len: data length in user space
2501 * @a: data address in kernel space
2502 *
2503 * Returns 0 for success or NULL context or < 0 on error.
2504 */
Eric Paris07c49412012-01-03 14:23:07 -05002505int __audit_sockaddr(int len, void *a)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002506{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002507 struct audit_context *context = current->audit_context;
2508
Al Viro4f6b4342008-12-09 19:50:34 -05002509 if (!context->sockaddr) {
2510 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2511 if (!p)
2512 return -ENOMEM;
2513 context->sockaddr = p;
2514 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002515
Al Viro4f6b4342008-12-09 19:50:34 -05002516 context->sockaddr_len = len;
2517 memcpy(context->sockaddr, a, len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002518 return 0;
2519}
2520
Al Viroa5cb0132007-03-20 13:58:35 -04002521void __audit_ptrace(struct task_struct *t)
2522{
2523 struct audit_context *context = current->audit_context;
2524
2525 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002526 context->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002527 context->target_uid = task_uid(t);
Eric Paris4746ec52008-01-08 10:06:53 -05002528 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002529 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002530 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002531}
2532
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002533/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002534 * audit_signal_info - record signal info for shutting down audit subsystem
2535 * @sig: signal value
2536 * @t: task being signaled
2537 *
2538 * If the audit subsystem is being terminated, record the task (pid)
2539 * and uid that is doing that.
2540 */
Amy Griffise54dc242007-03-29 18:01:04 -04002541int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002542{
Amy Griffise54dc242007-03-29 18:01:04 -04002543 struct audit_aux_data_pids *axp;
2544 struct task_struct *tsk = current;
2545 struct audit_context *ctx = tsk->audit_context;
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002546 kuid_t uid = current_uid(), t_uid = task_uid(t);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002547
Al Viro175fc482007-08-08 00:01:46 +01002548 if (audit_pid && t->tgid == audit_pid) {
Eric Parisee1d3152008-07-07 10:49:45 -04002549 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
Al Viro175fc482007-08-08 00:01:46 +01002550 audit_sig_pid = tsk->pid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002551 if (uid_valid(tsk->loginuid))
Al Virobfef93a2008-01-10 04:53:18 -05002552 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002553 else
David Howellsc69e8d92008-11-14 10:39:19 +11002554 audit_sig_uid = uid;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002555 security_task_getsecid(tsk, &audit_sig_sid);
Al Viro175fc482007-08-08 00:01:46 +01002556 }
2557 if (!audit_signals || audit_dummy_context())
2558 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002559 }
Amy Griffise54dc242007-03-29 18:01:04 -04002560
Amy Griffise54dc242007-03-29 18:01:04 -04002561 /* optimize the common case by putting first signal recipient directly
2562 * in audit_context */
2563 if (!ctx->target_pid) {
2564 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002565 ctx->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002566 ctx->target_uid = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002567 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002568 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002569 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002570 return 0;
2571 }
2572
2573 axp = (void *)ctx->aux_pids;
2574 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2575 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2576 if (!axp)
2577 return -ENOMEM;
2578
2579 axp->d.type = AUDIT_OBJ_PID;
2580 axp->d.next = ctx->aux_pids;
2581 ctx->aux_pids = (void *)axp;
2582 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002583 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002584
2585 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002586 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002587 axp->target_uid[axp->pid_count] = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002588 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002589 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002590 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002591 axp->pid_count++;
2592
2593 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002594}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002595
2596/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002597 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
David Howellsd84f4f92008-11-14 10:39:23 +11002598 * @bprm: pointer to the bprm being processed
2599 * @new: the proposed new credentials
2600 * @old: the old credentials
Eric Paris3fc689e2008-11-11 21:48:18 +11002601 *
2602 * Simply check if the proc already has the caps given by the file and if not
2603 * store the priv escalation info for later auditing at the end of the syscall
2604 *
Eric Paris3fc689e2008-11-11 21:48:18 +11002605 * -Eric
2606 */
David Howellsd84f4f92008-11-14 10:39:23 +11002607int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2608 const struct cred *new, const struct cred *old)
Eric Paris3fc689e2008-11-11 21:48:18 +11002609{
2610 struct audit_aux_data_bprm_fcaps *ax;
2611 struct audit_context *context = current->audit_context;
2612 struct cpu_vfs_cap_data vcaps;
2613 struct dentry *dentry;
2614
2615 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2616 if (!ax)
David Howellsd84f4f92008-11-14 10:39:23 +11002617 return -ENOMEM;
Eric Paris3fc689e2008-11-11 21:48:18 +11002618
2619 ax->d.type = AUDIT_BPRM_FCAPS;
2620 ax->d.next = context->aux;
2621 context->aux = (void *)ax;
2622
2623 dentry = dget(bprm->file->f_dentry);
2624 get_vfs_caps_from_disk(dentry, &vcaps);
2625 dput(dentry);
2626
2627 ax->fcap.permitted = vcaps.permitted;
2628 ax->fcap.inheritable = vcaps.inheritable;
2629 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2630 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2631
David Howellsd84f4f92008-11-14 10:39:23 +11002632 ax->old_pcap.permitted = old->cap_permitted;
2633 ax->old_pcap.inheritable = old->cap_inheritable;
2634 ax->old_pcap.effective = old->cap_effective;
Eric Paris3fc689e2008-11-11 21:48:18 +11002635
David Howellsd84f4f92008-11-14 10:39:23 +11002636 ax->new_pcap.permitted = new->cap_permitted;
2637 ax->new_pcap.inheritable = new->cap_inheritable;
2638 ax->new_pcap.effective = new->cap_effective;
2639 return 0;
Eric Paris3fc689e2008-11-11 21:48:18 +11002640}
2641
2642/**
Eric Parise68b75a02008-11-11 21:48:22 +11002643 * __audit_log_capset - store information about the arguments to the capset syscall
David Howellsd84f4f92008-11-14 10:39:23 +11002644 * @pid: target pid of the capset call
2645 * @new: the new credentials
2646 * @old: the old (current) credentials
Eric Parise68b75a02008-11-11 21:48:22 +11002647 *
2648 * Record the aguments userspace sent to sys_capset for later printing by the
2649 * audit system if applicable
2650 */
Al Viro57f71a02009-01-04 14:52:57 -05002651void __audit_log_capset(pid_t pid,
David Howellsd84f4f92008-11-14 10:39:23 +11002652 const struct cred *new, const struct cred *old)
Eric Parise68b75a02008-11-11 21:48:22 +11002653{
Eric Parise68b75a02008-11-11 21:48:22 +11002654 struct audit_context *context = current->audit_context;
Al Viro57f71a02009-01-04 14:52:57 -05002655 context->capset.pid = pid;
2656 context->capset.cap.effective = new->cap_effective;
2657 context->capset.cap.inheritable = new->cap_effective;
2658 context->capset.cap.permitted = new->cap_permitted;
2659 context->type = AUDIT_CAPSET;
Eric Parise68b75a02008-11-11 21:48:22 +11002660}
2661
Al Viro120a7952010-10-30 02:54:44 -04002662void __audit_mmap_fd(int fd, int flags)
2663{
2664 struct audit_context *context = current->audit_context;
2665 context->mmap.fd = fd;
2666 context->mmap.flags = flags;
2667 context->type = AUDIT_MMAP;
2668}
2669
Eric Paris85e7bac2012-01-03 14:23:05 -05002670static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr)
2671{
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002672 kuid_t auid, uid;
2673 kgid_t gid;
Eric Paris85e7bac2012-01-03 14:23:05 -05002674 unsigned int sessionid;
2675
2676 auid = audit_get_loginuid(current);
2677 sessionid = audit_get_sessionid(current);
2678 current_uid_gid(&uid, &gid);
2679
2680 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002681 from_kuid(&init_user_ns, auid),
2682 from_kuid(&init_user_ns, uid),
2683 from_kgid(&init_user_ns, gid),
2684 sessionid);
Eric Paris85e7bac2012-01-03 14:23:05 -05002685 audit_log_task_context(ab);
2686 audit_log_format(ab, " pid=%d comm=", current->pid);
2687 audit_log_untrustedstring(ab, current->comm);
2688 audit_log_format(ab, " reason=");
2689 audit_log_string(ab, reason);
2690 audit_log_format(ab, " sig=%ld", signr);
2691}
Eric Parise68b75a02008-11-11 21:48:22 +11002692/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002693 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002694 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002695 *
2696 * If a process ends with a core dump, something fishy is going on and we
2697 * should record the event for investigation.
2698 */
2699void audit_core_dumps(long signr)
2700{
2701 struct audit_buffer *ab;
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002702
2703 if (!audit_enabled)
2704 return;
2705
2706 if (signr == SIGQUIT) /* don't care for those */
2707 return;
2708
2709 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
Eric Paris85e7bac2012-01-03 14:23:05 -05002710 audit_log_abend(ab, "memory violation", signr);
2711 audit_log_end(ab);
2712}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002713
Kees Cook3dc1c1b2012-04-12 16:47:58 -05002714void __audit_seccomp(unsigned long syscall, long signr, int code)
Eric Paris85e7bac2012-01-03 14:23:05 -05002715{
2716 struct audit_buffer *ab;
2717
2718 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
Kees Cook3dc1c1b2012-04-12 16:47:58 -05002719 audit_log_abend(ab, "seccomp", signr);
Eric Paris85e7bac2012-01-03 14:23:05 -05002720 audit_log_format(ab, " syscall=%ld", syscall);
Kees Cook3dc1c1b2012-04-12 16:47:58 -05002721 audit_log_format(ab, " compat=%d", is_compat_task());
2722 audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
2723 audit_log_format(ab, " code=0x%x", code);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002724 audit_log_end(ab);
2725}
Al Viro916d7572009-06-24 00:02:38 -04002726
2727struct list_head *audit_killed_trees(void)
2728{
2729 struct audit_context *ctx = current->audit_context;
2730 if (likely(!ctx || !ctx->in_syscall))
2731 return NULL;
2732 return &ctx->killed_trees;
2733}