blob: 0160a68b4d7f0678de1b31b9180238fc93fdb334 [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
Eric Parisde6bbd12008-01-07 14:31:58 -050084/* no execve audit message should be longer than this (userspace limits) */
85#define MAX_EXECVE_AUDIT_LEN 7500
86
Al Viro471a5c72006-07-10 08:29:24 -040087/* number of audit rules */
88int audit_n_rules;
89
Amy Griffise54dc242007-03-29 18:01:04 -040090/* determines whether we collect data for signals sent */
91int audit_signals;
92
Eric Paris851f7ff2008-11-11 21:48:14 +110093struct audit_cap_data {
94 kernel_cap_t permitted;
95 kernel_cap_t inheritable;
96 union {
97 unsigned int fE; /* effective bit of a file capability */
98 kernel_cap_t effective; /* effective set of a process */
99 };
100};
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* When fs/namei.c:getname() is called, we store the pointer in name and
103 * we don't let putname() free it (instead we free all of the saved
104 * pointers at syscall exit time).
105 *
106 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
107struct audit_names {
Eric Paris5195d8e2012-01-03 14:23:05 -0500108 struct list_head list; /* audit_context->names_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 const char *name;
110 unsigned long ino;
111 dev_t dev;
112 umode_t mode;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700113 kuid_t uid;
114 kgid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400116 u32 osid;
Eric Paris851f7ff2008-11-11 21:48:14 +1100117 struct audit_cap_data fcap;
118 unsigned int fcap_ver;
Eric Paris5195d8e2012-01-03 14:23:05 -0500119 int name_len; /* number of name's characters to log */
Jeff Layton78e2e802012-10-10 15:25:22 -0400120 unsigned char type; /* record type */
Eric Paris5195d8e2012-01-03 14:23:05 -0500121 bool name_put; /* call __putname() for this name */
122 /*
123 * This was an allocated audit_names and not from the array of
124 * names allocated in the task audit context. Thus this name
125 * should be freed on syscall exit
126 */
127 bool should_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130struct audit_aux_data {
131 struct audit_aux_data *next;
132 int type;
133};
134
135#define AUDIT_AUX_IPCPERM 0
136
Amy Griffise54dc242007-03-29 18:01:04 -0400137/* Number of target pids per aux struct. */
138#define AUDIT_AUX_PIDS 16
139
Al Viro473ae302006-04-26 14:04:08 -0400140struct audit_aux_data_execve {
141 struct audit_aux_data d;
142 int argc;
143 int envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700144 struct mm_struct *mm;
Al Viro473ae302006-04-26 14:04:08 -0400145};
146
Amy Griffise54dc242007-03-29 18:01:04 -0400147struct audit_aux_data_pids {
148 struct audit_aux_data d;
149 pid_t target_pid[AUDIT_AUX_PIDS];
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700150 kuid_t target_auid[AUDIT_AUX_PIDS];
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800151 kuid_t target_uid[AUDIT_AUX_PIDS];
Eric Paris4746ec52008-01-08 10:06:53 -0500152 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400153 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500154 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400155 int pid_count;
156};
157
Eric Paris3fc689e2008-11-11 21:48:18 +1100158struct audit_aux_data_bprm_fcaps {
159 struct audit_aux_data d;
160 struct audit_cap_data fcap;
161 unsigned int fcap_ver;
162 struct audit_cap_data old_pcap;
163 struct audit_cap_data new_pcap;
164};
165
Eric Parise68b75a02008-11-11 21:48:22 +1100166struct audit_aux_data_capset {
167 struct audit_aux_data d;
168 pid_t pid;
169 struct audit_cap_data cap;
170};
171
Al Viro74c3cbe2007-07-22 08:04:18 -0400172struct audit_tree_refs {
173 struct audit_tree_refs *next;
174 struct audit_chunk *c[31];
175};
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/* The per-task audit context. */
178struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400179 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int in_syscall; /* 1 if task is in a syscall */
Al Viro0590b932008-12-14 23:45:27 -0500181 enum audit_state state, current_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned int serial; /* serial number for record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int major; /* syscall number */
Eric Paris44e51a12009-08-07 16:54:29 -0400184 struct timespec ctime; /* time of syscall entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 unsigned long argv[4]; /* syscall arguments */
2fd6f582005-04-29 16:08:28 +0100186 long return_code;/* syscall return code */
Al Viro0590b932008-12-14 23:45:27 -0500187 u64 prio;
Eric Paris44e51a12009-08-07 16:54:29 -0400188 int return_valid; /* return code is valid */
Eric Paris5195d8e2012-01-03 14:23:05 -0500189 /*
190 * The names_list is the list of all audit_names collected during this
191 * syscall. The first AUDIT_NAMES entries in the names_list will
192 * actually be from the preallocated_names array for performance
193 * reasons. Except during allocation they should never be referenced
194 * through the preallocated_names array and should only be found/used
195 * by running the names_list.
196 */
197 struct audit_names preallocated_names[AUDIT_NAMES];
198 int name_count; /* total records in names_list */
199 struct list_head names_list; /* anchor for struct audit_names->list */
Amy Griffis5adc8a62006-06-14 18:45:21 -0400200 char * filterkey; /* key for rule that triggered record */
Jan Blunck44707fd2008-02-14 19:38:33 -0800201 struct path pwd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct audit_context *previous; /* For nested syscalls */
203 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400204 struct audit_aux_data *aux_pids;
Al Viro4f6b4342008-12-09 19:50:34 -0500205 struct sockaddr_storage *sockaddr;
206 size_t sockaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400208 pid_t pid, ppid;
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800209 kuid_t uid, euid, suid, fsuid;
210 kgid_t gid, egid, sgid, fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100212 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Al Viroa5cb0132007-03-20 13:58:35 -0400214 pid_t target_pid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -0700215 kuid_t target_auid;
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800216 kuid_t target_uid;
Eric Paris4746ec52008-01-08 10:06:53 -0500217 unsigned int target_sessionid;
Al Viroa5cb0132007-03-20 13:58:35 -0400218 u32 target_sid;
Eric Parisc2a77802008-01-07 13:40:17 -0500219 char target_comm[TASK_COMM_LEN];
Al Viroa5cb0132007-03-20 13:58:35 -0400220
Al Viro74c3cbe2007-07-22 08:04:18 -0400221 struct audit_tree_refs *trees, *first_trees;
Al Viro916d7572009-06-24 00:02:38 -0400222 struct list_head killed_trees;
Eric Paris44e51a12009-08-07 16:54:29 -0400223 int tree_count;
Al Viro74c3cbe2007-07-22 08:04:18 -0400224
Al Virof3298dc2008-12-10 03:16:51 -0500225 int type;
226 union {
227 struct {
228 int nargs;
229 long args[6];
230 } socketcall;
Al Viroa33e6752008-12-10 03:40:06 -0500231 struct {
Eric W. Biedermancca080d2012-02-07 16:53:48 -0800232 kuid_t uid;
233 kgid_t gid;
Al Viro2570ebb2011-07-27 14:03:22 -0400234 umode_t mode;
Al Viroa33e6752008-12-10 03:40:06 -0500235 u32 osid;
Al Viroe816f372008-12-10 03:47:15 -0500236 int has_perm;
237 uid_t perm_uid;
238 gid_t perm_gid;
Al Viro2570ebb2011-07-27 14:03:22 -0400239 umode_t perm_mode;
Al Viroe816f372008-12-10 03:47:15 -0500240 unsigned long qbytes;
Al Viroa33e6752008-12-10 03:40:06 -0500241 } ipc;
Al Viro73929062008-12-10 06:58:59 -0500242 struct {
243 mqd_t mqdes;
244 struct mq_attr mqstat;
245 } mq_getsetattr;
Al Viro20114f72008-12-10 07:16:12 -0500246 struct {
247 mqd_t mqdes;
248 int sigev_signo;
249 } mq_notify;
Al Viroc32c8af2008-12-14 03:46:48 -0500250 struct {
251 mqd_t mqdes;
252 size_t msg_len;
253 unsigned int msg_prio;
254 struct timespec abs_timeout;
255 } mq_sendrecv;
Al Viro564f6992008-12-14 04:02:26 -0500256 struct {
257 int oflag;
Al Virodf0a4282011-07-26 05:26:10 -0400258 umode_t mode;
Al Viro564f6992008-12-14 04:02:26 -0500259 struct mq_attr attr;
260 } mq_open;
Al Viro57f71a02009-01-04 14:52:57 -0500261 struct {
262 pid_t pid;
263 struct audit_cap_data cap;
264 } capset;
Al Viro120a7952010-10-30 02:54:44 -0400265 struct {
266 int fd;
267 int flags;
268 } mmap;
Al Virof3298dc2008-12-10 03:16:51 -0500269 };
Al Viro157cf642008-12-14 04:57:47 -0500270 int fds[2];
Al Virof3298dc2008-12-10 03:16:51 -0500271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272#if AUDIT_DEBUG
273 int put_count;
274 int ino_count;
275#endif
276};
277
Al Viro55669bf2006-08-31 19:26:40 -0400278static inline int open_arg(int flags, int mask)
279{
280 int n = ACC_MODE(flags);
281 if (flags & (O_TRUNC | O_CREAT))
282 n |= AUDIT_PERM_WRITE;
283 return n & mask;
284}
285
286static int audit_match_perm(struct audit_context *ctx, int mask)
287{
Cordeliac4bacef2008-08-18 09:45:51 -0700288 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800289 if (unlikely(!ctx))
290 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700291 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100292
Al Viro55669bf2006-08-31 19:26:40 -0400293 switch (audit_classify_syscall(ctx->arch, n)) {
294 case 0: /* native */
295 if ((mask & AUDIT_PERM_WRITE) &&
296 audit_match_class(AUDIT_CLASS_WRITE, n))
297 return 1;
298 if ((mask & AUDIT_PERM_READ) &&
299 audit_match_class(AUDIT_CLASS_READ, n))
300 return 1;
301 if ((mask & AUDIT_PERM_ATTR) &&
302 audit_match_class(AUDIT_CLASS_CHATTR, n))
303 return 1;
304 return 0;
305 case 1: /* 32bit on biarch */
306 if ((mask & AUDIT_PERM_WRITE) &&
307 audit_match_class(AUDIT_CLASS_WRITE_32, n))
308 return 1;
309 if ((mask & AUDIT_PERM_READ) &&
310 audit_match_class(AUDIT_CLASS_READ_32, n))
311 return 1;
312 if ((mask & AUDIT_PERM_ATTR) &&
313 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
314 return 1;
315 return 0;
316 case 2: /* open */
317 return mask & ACC_MODE(ctx->argv[1]);
318 case 3: /* openat */
319 return mask & ACC_MODE(ctx->argv[2]);
320 case 4: /* socketcall */
321 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
322 case 5: /* execve */
323 return mask & AUDIT_PERM_EXEC;
324 default:
325 return 0;
326 }
327}
328
Eric Paris5ef30ee2012-01-03 14:23:05 -0500329static int audit_match_filetype(struct audit_context *ctx, int val)
Al Viro8b67dca2008-04-28 04:15:49 -0400330{
Eric Paris5195d8e2012-01-03 14:23:05 -0500331 struct audit_names *n;
Eric Paris5ef30ee2012-01-03 14:23:05 -0500332 umode_t mode = (umode_t)val;
zhangxiliang1a61c882008-08-02 10:56:37 +0800333
334 if (unlikely(!ctx))
335 return 0;
336
Eric Paris5195d8e2012-01-03 14:23:05 -0500337 list_for_each_entry(n, &ctx->names_list, list) {
338 if ((n->ino != -1) &&
339 ((n->mode & S_IFMT) == mode))
Eric Paris5ef30ee2012-01-03 14:23:05 -0500340 return 1;
341 }
Eric Paris5195d8e2012-01-03 14:23:05 -0500342
Eric Paris5ef30ee2012-01-03 14:23:05 -0500343 return 0;
Al Viro8b67dca2008-04-28 04:15:49 -0400344}
345
Al Viro74c3cbe2007-07-22 08:04:18 -0400346/*
347 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
348 * ->first_trees points to its beginning, ->trees - to the current end of data.
349 * ->tree_count is the number of free entries in array pointed to by ->trees.
350 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
351 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
352 * it's going to remain 1-element for almost any setup) until we free context itself.
353 * References in it _are_ dropped - at the same time we free/drop aux stuff.
354 */
355
356#ifdef CONFIG_AUDIT_TREE
Eric Paris679173b2009-01-26 18:09:45 -0500357static void audit_set_auditable(struct audit_context *ctx)
358{
359 if (!ctx->prio) {
360 ctx->prio = 1;
361 ctx->current_state = AUDIT_RECORD_CONTEXT;
362 }
363}
364
Al Viro74c3cbe2007-07-22 08:04:18 -0400365static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
366{
367 struct audit_tree_refs *p = ctx->trees;
368 int left = ctx->tree_count;
369 if (likely(left)) {
370 p->c[--left] = chunk;
371 ctx->tree_count = left;
372 return 1;
373 }
374 if (!p)
375 return 0;
376 p = p->next;
377 if (p) {
378 p->c[30] = chunk;
379 ctx->trees = p;
380 ctx->tree_count = 30;
381 return 1;
382 }
383 return 0;
384}
385
386static int grow_tree_refs(struct audit_context *ctx)
387{
388 struct audit_tree_refs *p = ctx->trees;
389 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
390 if (!ctx->trees) {
391 ctx->trees = p;
392 return 0;
393 }
394 if (p)
395 p->next = ctx->trees;
396 else
397 ctx->first_trees = ctx->trees;
398 ctx->tree_count = 31;
399 return 1;
400}
401#endif
402
403static void unroll_tree_refs(struct audit_context *ctx,
404 struct audit_tree_refs *p, int count)
405{
406#ifdef CONFIG_AUDIT_TREE
407 struct audit_tree_refs *q;
408 int n;
409 if (!p) {
410 /* we started with empty chain */
411 p = ctx->first_trees;
412 count = 31;
413 /* if the very first allocation has failed, nothing to do */
414 if (!p)
415 return;
416 }
417 n = count;
418 for (q = p; q != ctx->trees; q = q->next, n = 31) {
419 while (n--) {
420 audit_put_chunk(q->c[n]);
421 q->c[n] = NULL;
422 }
423 }
424 while (n-- > ctx->tree_count) {
425 audit_put_chunk(q->c[n]);
426 q->c[n] = NULL;
427 }
428 ctx->trees = p;
429 ctx->tree_count = count;
430#endif
431}
432
433static void free_tree_refs(struct audit_context *ctx)
434{
435 struct audit_tree_refs *p, *q;
436 for (p = ctx->first_trees; p; p = q) {
437 q = p->next;
438 kfree(p);
439 }
440}
441
442static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
443{
444#ifdef CONFIG_AUDIT_TREE
445 struct audit_tree_refs *p;
446 int n;
447 if (!tree)
448 return 0;
449 /* full ones */
450 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
451 for (n = 0; n < 31; n++)
452 if (audit_tree_match(p->c[n], tree))
453 return 1;
454 }
455 /* partial */
456 if (p) {
457 for (n = ctx->tree_count; n < 31; n++)
458 if (audit_tree_match(p->c[n], tree))
459 return 1;
460 }
461#endif
462 return 0;
463}
464
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700465static int audit_compare_uid(kuid_t uid,
466 struct audit_names *name,
467 struct audit_field *f,
468 struct audit_context *ctx)
Eric Parisb34b0392012-01-03 14:23:08 -0500469{
470 struct audit_names *n;
Eric Parisb34b0392012-01-03 14:23:08 -0500471 int rc;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700472
Eric Parisb34b0392012-01-03 14:23:08 -0500473 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700474 rc = audit_uid_comparator(uid, f->op, name->uid);
Eric Parisb34b0392012-01-03 14:23:08 -0500475 if (rc)
476 return rc;
477 }
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700478
Eric Parisb34b0392012-01-03 14:23:08 -0500479 if (ctx) {
480 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700481 rc = audit_uid_comparator(uid, f->op, n->uid);
482 if (rc)
483 return rc;
484 }
485 }
486 return 0;
487}
Eric Parisb34b0392012-01-03 14:23:08 -0500488
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700489static int audit_compare_gid(kgid_t gid,
490 struct audit_names *name,
491 struct audit_field *f,
492 struct audit_context *ctx)
493{
494 struct audit_names *n;
495 int rc;
496
497 if (name) {
498 rc = audit_gid_comparator(gid, f->op, name->gid);
499 if (rc)
500 return rc;
501 }
502
503 if (ctx) {
504 list_for_each_entry(n, &ctx->names_list, list) {
505 rc = audit_gid_comparator(gid, f->op, n->gid);
Eric Parisb34b0392012-01-03 14:23:08 -0500506 if (rc)
507 return rc;
508 }
509 }
510 return 0;
511}
512
Eric Paris02d86a52012-01-03 14:23:08 -0500513static int audit_field_compare(struct task_struct *tsk,
514 const struct cred *cred,
515 struct audit_field *f,
516 struct audit_context *ctx,
517 struct audit_names *name)
518{
Eric Paris02d86a52012-01-03 14:23:08 -0500519 switch (f->val) {
Peter Moody4a6633e2011-12-13 16:17:51 -0800520 /* process to file object comparisons */
Eric Paris02d86a52012-01-03 14:23:08 -0500521 case AUDIT_COMPARE_UID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700522 return audit_compare_uid(cred->uid, name, f, ctx);
Eric Parisc9fe6852012-01-03 14:23:08 -0500523 case AUDIT_COMPARE_GID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700524 return audit_compare_gid(cred->gid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800525 case AUDIT_COMPARE_EUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700526 return audit_compare_uid(cred->euid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800527 case AUDIT_COMPARE_EGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700528 return audit_compare_gid(cred->egid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800529 case AUDIT_COMPARE_AUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700530 return audit_compare_uid(tsk->loginuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800531 case AUDIT_COMPARE_SUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700532 return audit_compare_uid(cred->suid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800533 case AUDIT_COMPARE_SGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700534 return audit_compare_gid(cred->sgid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800535 case AUDIT_COMPARE_FSUID_TO_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700536 return audit_compare_uid(cred->fsuid, name, f, ctx);
Peter Moody4a6633e2011-12-13 16:17:51 -0800537 case AUDIT_COMPARE_FSGID_TO_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700538 return audit_compare_gid(cred->fsgid, name, f, ctx);
Peter Moody10d68362012-01-04 15:24:31 -0500539 /* uid comparisons */
540 case AUDIT_COMPARE_UID_TO_AUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700541 return audit_uid_comparator(cred->uid, f->op, tsk->loginuid);
Peter Moody10d68362012-01-04 15:24:31 -0500542 case AUDIT_COMPARE_UID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700543 return audit_uid_comparator(cred->uid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500544 case AUDIT_COMPARE_UID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700545 return audit_uid_comparator(cred->uid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500546 case AUDIT_COMPARE_UID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700547 return audit_uid_comparator(cred->uid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500548 /* auid comparisons */
549 case AUDIT_COMPARE_AUID_TO_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700550 return audit_uid_comparator(tsk->loginuid, f->op, cred->euid);
Peter Moody10d68362012-01-04 15:24:31 -0500551 case AUDIT_COMPARE_AUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700552 return audit_uid_comparator(tsk->loginuid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500553 case AUDIT_COMPARE_AUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700554 return audit_uid_comparator(tsk->loginuid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500555 /* euid comparisons */
556 case AUDIT_COMPARE_EUID_TO_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700557 return audit_uid_comparator(cred->euid, f->op, cred->suid);
Peter Moody10d68362012-01-04 15:24:31 -0500558 case AUDIT_COMPARE_EUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700559 return audit_uid_comparator(cred->euid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500560 /* suid comparisons */
561 case AUDIT_COMPARE_SUID_TO_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700562 return audit_uid_comparator(cred->suid, f->op, cred->fsuid);
Peter Moody10d68362012-01-04 15:24:31 -0500563 /* gid comparisons */
564 case AUDIT_COMPARE_GID_TO_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700565 return audit_gid_comparator(cred->gid, f->op, cred->egid);
Peter Moody10d68362012-01-04 15:24:31 -0500566 case AUDIT_COMPARE_GID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700567 return audit_gid_comparator(cred->gid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500568 case AUDIT_COMPARE_GID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700569 return audit_gid_comparator(cred->gid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500570 /* egid comparisons */
571 case AUDIT_COMPARE_EGID_TO_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700572 return audit_gid_comparator(cred->egid, f->op, cred->sgid);
Peter Moody10d68362012-01-04 15:24:31 -0500573 case AUDIT_COMPARE_EGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700574 return audit_gid_comparator(cred->egid, f->op, cred->fsgid);
Peter Moody10d68362012-01-04 15:24:31 -0500575 /* sgid comparison */
576 case AUDIT_COMPARE_SGID_TO_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700577 return audit_gid_comparator(cred->sgid, f->op, cred->fsgid);
Eric Paris02d86a52012-01-03 14:23:08 -0500578 default:
579 WARN(1, "Missing AUDIT_COMPARE define. Report as a bug\n");
580 return 0;
581 }
582 return 0;
583}
584
Amy Griffisf368c07d2006-04-07 16:55:56 -0400585/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/* Compare a task_struct with an audit_rule. Return 1 on match, 0
Tony Jonesf5629882011-04-27 15:10:49 +0200587 * otherwise.
588 *
589 * If task_creation is true, this is an explicit indication that we are
590 * filtering a task rule at task creation time. This and tsk == current are
591 * the only situations where tsk->cred may be accessed without an rcu read lock.
592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500594 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400596 struct audit_names *name,
Tony Jonesf5629882011-04-27 15:10:49 +0200597 enum audit_state *state,
598 bool task_creation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Tony Jonesf5629882011-04-27 15:10:49 +0200600 const struct cred *cred;
Eric Paris5195d8e2012-01-03 14:23:05 -0500601 int i, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600602 u32 sid;
603
Tony Jonesf5629882011-04-27 15:10:49 +0200604 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500607 struct audit_field *f = &rule->fields[i];
Eric Paris5195d8e2012-01-03 14:23:05 -0500608 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int result = 0;
610
Amy Griffis93315ed2006-02-07 12:05:27 -0500611 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500613 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 break;
Al Viro3c662512006-05-06 08:26:27 -0400615 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400616 if (ctx) {
617 if (!ctx->ppid)
618 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400619 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400620 }
Al Viro3c662512006-05-06 08:26:27 -0400621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 case AUDIT_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700623 result = audit_uid_comparator(cred->uid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
625 case AUDIT_EUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700626 result = audit_uid_comparator(cred->euid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
628 case AUDIT_SUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700629 result = audit_uid_comparator(cred->suid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 break;
631 case AUDIT_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700632 result = audit_uid_comparator(cred->fsuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634 case AUDIT_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700635 result = audit_gid_comparator(cred->gid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
637 case AUDIT_EGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700638 result = audit_gid_comparator(cred->egid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640 case AUDIT_SGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700641 result = audit_gid_comparator(cred->sgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 case AUDIT_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700644 result = audit_gid_comparator(cred->fsgid, f->op, f->gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500647 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 break;
2fd6f582005-04-29 16:08:28 +0100649 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700650 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500651 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 case AUDIT_EXIT:
655 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500656 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100659 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500660 if (f->val)
661 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100662 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500663 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 break;
666 case AUDIT_DEVMAJOR:
Eric Paris16c174bd2012-01-03 14:23:05 -0500667 if (name) {
668 if (audit_comparator(MAJOR(name->dev), f->op, f->val) ||
669 audit_comparator(MAJOR(name->rdev), f->op, f->val))
670 ++result;
671 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500672 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174bd2012-01-03 14:23:05 -0500673 if (audit_comparator(MAJOR(n->dev), f->op, f->val) ||
674 audit_comparator(MAJOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 ++result;
676 break;
677 }
678 }
679 }
680 break;
681 case AUDIT_DEVMINOR:
Eric Paris16c174bd2012-01-03 14:23:05 -0500682 if (name) {
683 if (audit_comparator(MINOR(name->dev), f->op, f->val) ||
684 audit_comparator(MINOR(name->rdev), f->op, f->val))
685 ++result;
686 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500687 list_for_each_entry(n, &ctx->names_list, list) {
Eric Paris16c174bd2012-01-03 14:23:05 -0500688 if (audit_comparator(MINOR(n->dev), f->op, f->val) ||
689 audit_comparator(MINOR(n->rdev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 ++result;
691 break;
692 }
693 }
694 }
695 break;
696 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400697 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400698 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400699 else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500700 list_for_each_entry(n, &ctx->names_list, list) {
701 if (audit_comparator(n->ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ++result;
703 break;
704 }
705 }
706 }
707 break;
Eric Parisefaffd62012-01-03 14:23:07 -0500708 case AUDIT_OBJ_UID:
709 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700710 result = audit_uid_comparator(name->uid, f->op, f->uid);
Eric Parisefaffd62012-01-03 14:23:07 -0500711 } else if (ctx) {
712 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700713 if (audit_uid_comparator(n->uid, f->op, f->uid)) {
Eric Parisefaffd62012-01-03 14:23:07 -0500714 ++result;
715 break;
716 }
717 }
718 }
719 break;
Eric Paris54d32182012-01-03 14:23:07 -0500720 case AUDIT_OBJ_GID:
721 if (name) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700722 result = audit_gid_comparator(name->gid, f->op, f->gid);
Eric Paris54d32182012-01-03 14:23:07 -0500723 } else if (ctx) {
724 list_for_each_entry(n, &ctx->names_list, list) {
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700725 if (audit_gid_comparator(n->gid, f->op, f->gid)) {
Eric Paris54d32182012-01-03 14:23:07 -0500726 ++result;
727 break;
728 }
729 }
730 }
731 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400732 case AUDIT_WATCH:
Eric Parisae7b8f42009-12-17 20:12:04 -0500733 if (name)
734 result = audit_watch_compare(rule->watch, name->ino, name->dev);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400735 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400736 case AUDIT_DIR:
737 if (ctx)
738 result = match_tree_refs(ctx, rule->tree);
739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 case AUDIT_LOGINUID:
741 result = 0;
742 if (ctx)
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700743 result = audit_uid_comparator(tsk->loginuid, f->op, f->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500745 case AUDIT_SUBJ_USER:
746 case AUDIT_SUBJ_ROLE:
747 case AUDIT_SUBJ_TYPE:
748 case AUDIT_SUBJ_SEN:
749 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600750 /* NOTE: this may return negative values indicating
751 a temporary error. We simply treat this as a
752 match for now to avoid losing information that
753 may be wanted. An error message will also be
754 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000755 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400756 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200757 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400758 need_sid = 0;
759 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200760 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600761 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000762 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600763 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400764 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600765 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500766 case AUDIT_OBJ_USER:
767 case AUDIT_OBJ_ROLE:
768 case AUDIT_OBJ_TYPE:
769 case AUDIT_OBJ_LEV_LOW:
770 case AUDIT_OBJ_LEV_HIGH:
771 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
772 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000773 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500774 /* Find files that match */
775 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200776 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500777 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000778 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500779 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500780 list_for_each_entry(n, &ctx->names_list, list) {
781 if (security_audit_rule_match(n->osid, f->type,
782 f->op, f->lsm_rule,
783 ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500784 ++result;
785 break;
786 }
787 }
788 }
789 /* Find ipc objects that match */
Al Viroa33e6752008-12-10 03:40:06 -0500790 if (!ctx || ctx->type != AUDIT_IPC)
791 break;
792 if (security_audit_rule_match(ctx->ipc.osid,
793 f->type, f->op,
794 f->lsm_rule, ctx))
795 ++result;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500796 }
797 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 case AUDIT_ARG0:
799 case AUDIT_ARG1:
800 case AUDIT_ARG2:
801 case AUDIT_ARG3:
802 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500803 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400805 case AUDIT_FILTERKEY:
806 /* ignore this field for filtering */
807 result = 1;
808 break;
Al Viro55669bf2006-08-31 19:26:40 -0400809 case AUDIT_PERM:
810 result = audit_match_perm(ctx, f->val);
811 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400812 case AUDIT_FILETYPE:
813 result = audit_match_filetype(ctx, f->val);
814 break;
Eric Paris02d86a52012-01-03 14:23:08 -0500815 case AUDIT_FIELD_COMPARE:
816 result = audit_field_compare(tsk, cred, f, ctx, name);
817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
Tony Jonesf5629882011-04-27 15:10:49 +0200819 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return 0;
821 }
Al Viro0590b932008-12-14 23:45:27 -0500822
823 if (ctx) {
824 if (rule->prio <= ctx->prio)
825 return 0;
826 if (rule->filterkey) {
827 kfree(ctx->filterkey);
828 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
829 }
830 ctx->prio = rule->prio;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 switch (rule->action) {
833 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
835 }
836 return 1;
837}
838
839/* At process creation time, we can determine if system-call auditing is
840 * completely disabled for this task. Since we only have the task
841 * structure at this point, we can only check uid and gid.
842 */
Al Viroe048e022008-12-16 03:51:22 -0500843static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 struct audit_entry *e;
846 enum audit_state state;
847
848 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100849 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Tony Jonesf5629882011-04-27 15:10:49 +0200850 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
851 &state, true)) {
Al Viroe048e022008-12-16 03:51:22 -0500852 if (state == AUDIT_RECORD_CONTEXT)
853 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 rcu_read_unlock();
855 return state;
856 }
857 }
858 rcu_read_unlock();
859 return AUDIT_BUILD_CONTEXT;
860}
861
862/* At syscall entry and exit time, this filter is called if the
863 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100864 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700865 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
867static enum audit_state audit_filter_syscall(struct task_struct *tsk,
868 struct audit_context *ctx,
869 struct list_head *list)
870{
871 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100872 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
David Woodhouse351bb722005-07-14 14:40:06 +0100874 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100875 return AUDIT_DISABLED;
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100878 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000879 int word = AUDIT_WORD(ctx->major);
880 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100881
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000882 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400883 if ((e->rule.mask[word] & bit) == bit &&
884 audit_filter_rules(tsk, &e->rule, ctx, NULL,
Tony Jonesf5629882011-04-27 15:10:49 +0200885 &state, false)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000886 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500887 ctx->current_state = state;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000888 return state;
889 }
890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892 rcu_read_unlock();
893 return AUDIT_BUILD_CONTEXT;
894}
895
Eric Paris5195d8e2012-01-03 14:23:05 -0500896/*
897 * Given an audit_name check the inode hash table to see if they match.
898 * Called holding the rcu read lock to protect the use of audit_inode_hash
899 */
900static int audit_filter_inode_name(struct task_struct *tsk,
901 struct audit_names *n,
902 struct audit_context *ctx) {
903 int word, bit;
904 int h = audit_hash_ino((u32)n->ino);
905 struct list_head *list = &audit_inode_hash[h];
906 struct audit_entry *e;
907 enum audit_state state;
908
909 word = AUDIT_WORD(ctx->major);
910 bit = AUDIT_BIT(ctx->major);
911
912 if (list_empty(list))
913 return 0;
914
915 list_for_each_entry_rcu(e, list, list) {
916 if ((e->rule.mask[word] & bit) == bit &&
917 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
918 ctx->current_state = state;
919 return 1;
920 }
921 }
922
923 return 0;
924}
925
926/* At syscall exit time, this filter is called if any audit_names have been
Amy Griffisf368c07d2006-04-07 16:55:56 -0400927 * collected during syscall processing. We only check rules in sublists at hash
Eric Paris5195d8e2012-01-03 14:23:05 -0500928 * buckets applicable to the inode numbers in audit_names.
Amy Griffisf368c07d2006-04-07 16:55:56 -0400929 * Regarding audit_state, same rules apply as for audit_filter_syscall().
930 */
Al Viro0590b932008-12-14 23:45:27 -0500931void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400932{
Eric Paris5195d8e2012-01-03 14:23:05 -0500933 struct audit_names *n;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400934
935 if (audit_pid && tsk->tgid == audit_pid)
Al Viro0590b932008-12-14 23:45:27 -0500936 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400937
938 rcu_read_lock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400939
Eric Paris5195d8e2012-01-03 14:23:05 -0500940 list_for_each_entry(n, &ctx->names_list, list) {
941 if (audit_filter_inode_name(tsk, n, ctx))
942 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400943 }
944 rcu_read_unlock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400945}
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947static inline struct audit_context *audit_get_context(struct task_struct *tsk,
948 int return_valid,
Paul Moore6d208da2009-04-01 15:47:27 -0400949 long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 struct audit_context *context = tsk->audit_context;
952
Eric Paris56179a62012-01-03 14:23:06 -0500953 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return NULL;
955 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500956
957 /*
958 * we need to fix up the return code in the audit logs if the actual
959 * return codes are later going to be fixed up by the arch specific
960 * signal handlers
961 *
962 * This is actually a test for:
963 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
964 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
965 *
966 * but is faster than a bunch of ||
967 */
968 if (unlikely(return_code <= -ERESTARTSYS) &&
969 (return_code >= -ERESTART_RESTARTBLOCK) &&
970 (return_code != -ENOIOCTLCMD))
971 context->return_code = -EINTR;
972 else
973 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Al Viro0590b932008-12-14 23:45:27 -0500975 if (context->in_syscall && !context->dummy) {
976 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
977 audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 tsk->audit_context = NULL;
981 return context;
982}
983
984static inline void audit_free_names(struct audit_context *context)
985{
Eric Paris5195d8e2012-01-03 14:23:05 -0500986 struct audit_names *n, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988#if AUDIT_DEBUG == 2
Al Viro0590b932008-12-14 23:45:27 -0500989 if (context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000990 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 " name_count=%d put_count=%d"
992 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000993 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 context->serial, context->major, context->in_syscall,
995 context->name_count, context->put_count,
996 context->ino_count);
Eric Paris5195d8e2012-01-03 14:23:05 -0500997 list_for_each_entry(n, &context->names_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 printk(KERN_ERR "names[%d] = %p = %s\n", i,
Eric Paris5195d8e2012-01-03 14:23:05 -0500999 n->name, n->name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 dump_stack();
1002 return;
1003 }
1004#endif
1005#if AUDIT_DEBUG
1006 context->put_count = 0;
1007 context->ino_count = 0;
1008#endif
1009
Eric Paris5195d8e2012-01-03 14:23:05 -05001010 list_for_each_entry_safe(n, next, &context->names_list, list) {
1011 list_del(&n->list);
1012 if (n->name && n->name_put)
1013 __putname(n->name);
1014 if (n->should_free)
1015 kfree(n);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -08001018 path_put(&context->pwd);
1019 context->pwd.dentry = NULL;
1020 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023static inline void audit_free_aux(struct audit_context *context)
1024{
1025 struct audit_aux_data *aux;
1026
1027 while ((aux = context->aux)) {
1028 context->aux = aux->next;
1029 kfree(aux);
1030 }
Amy Griffise54dc242007-03-29 18:01:04 -04001031 while ((aux = context->aux_pids)) {
1032 context->aux_pids = aux->next;
1033 kfree(aux);
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037static inline void audit_zero_context(struct audit_context *context,
1038 enum audit_state state)
1039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 memset(context, 0, sizeof(*context));
1041 context->state = state;
Al Viro0590b932008-12-14 23:45:27 -05001042 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045static inline struct audit_context *audit_alloc_context(enum audit_state state)
1046{
1047 struct audit_context *context;
1048
1049 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
1050 return NULL;
1051 audit_zero_context(context, state);
Al Viro916d7572009-06-24 00:02:38 -04001052 INIT_LIST_HEAD(&context->killed_trees);
Eric Paris5195d8e2012-01-03 14:23:05 -05001053 INIT_LIST_HEAD(&context->names_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return context;
1055}
1056
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001057/**
1058 * audit_alloc - allocate an audit context block for a task
1059 * @tsk: task
1060 *
1061 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * if necessary. Doing so turns on system call auditing for the
1063 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001064 * needed.
1065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066int audit_alloc(struct task_struct *tsk)
1067{
1068 struct audit_context *context;
1069 enum audit_state state;
Al Viroe048e022008-12-16 03:51:22 -05001070 char *key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Eric Parisb593d382008-01-08 17:38:31 -05001072 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return 0; /* Return if not auditing. */
1074
Al Viroe048e022008-12-16 03:51:22 -05001075 state = audit_filter_task(tsk, &key);
Eric Paris56179a62012-01-03 14:23:06 -05001076 if (state == AUDIT_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 0;
1078
1079 if (!(context = audit_alloc_context(state))) {
Al Viroe048e022008-12-16 03:51:22 -05001080 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 audit_log_lost("out of memory in audit_alloc");
1082 return -ENOMEM;
1083 }
Al Viroe048e022008-12-16 03:51:22 -05001084 context->filterkey = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 tsk->audit_context = context;
1087 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
1088 return 0;
1089}
1090
1091static inline void audit_free_context(struct audit_context *context)
1092{
1093 struct audit_context *previous;
1094 int count = 0;
1095
1096 do {
1097 previous = context->previous;
1098 if (previous || (count && count < 10)) {
1099 ++count;
1100 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
1101 " freeing multiple contexts (%d)\n",
1102 context->serial, context->major,
1103 context->name_count, count);
1104 }
1105 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001106 unroll_tree_refs(context, NULL, 0);
1107 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001109 kfree(context->filterkey);
Al Viro4f6b4342008-12-09 19:50:34 -05001110 kfree(context->sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 kfree(context);
1112 context = previous;
1113 } while (context);
1114 if (count >= 10)
1115 printk(KERN_ERR "audit: freed %d contexts\n", count);
1116}
1117
Joy Latten161a09e2006-11-27 13:11:54 -06001118void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001119{
1120 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +00001121 unsigned len;
1122 int error;
1123 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001124
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001125 security_task_getsecid(current, &sid);
Al Viroc4823bc2007-03-12 16:17:42 +00001126 if (!sid)
1127 return;
1128
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001129 error = security_secid_to_secctx(sid, &ctx, &len);
Al Viroc4823bc2007-03-12 16:17:42 +00001130 if (error) {
1131 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001132 goto error_path;
1133 return;
1134 }
1135
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001136 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001137 security_release_secctx(ctx, len);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +00001138 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001139
1140error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +00001141 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001142 return;
1143}
1144
Joy Latten161a09e2006-11-27 13:11:54 -06001145EXPORT_SYMBOL(audit_log_task_context);
1146
Peter Moodye23eb922012-06-14 10:04:35 -07001147void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -07001148{
Peter Moodye23eb922012-06-14 10:04:35 -07001149 const struct cred *cred;
Al Viro45d9bb02006-03-29 20:02:55 -05001150 char name[sizeof(tsk->comm)];
1151 struct mm_struct *mm = tsk->mm;
Peter Moodye23eb922012-06-14 10:04:35 -07001152 char *tty;
1153
1154 if (!ab)
1155 return;
Stephen Smalley219f0812005-04-18 10:47:35 -07001156
Al Viroe4951492006-03-29 20:17:10 -05001157 /* tsk == current */
Peter Moodye23eb922012-06-14 10:04:35 -07001158 cred = current_cred();
1159
1160 spin_lock_irq(&tsk->sighand->siglock);
1161 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1162 tty = tsk->signal->tty->name;
1163 else
1164 tty = "(none)";
1165 spin_unlock_irq(&tsk->sighand->siglock);
1166
1167
1168 audit_log_format(ab,
1169 " ppid=%ld pid=%d auid=%u uid=%u gid=%u"
1170 " euid=%u suid=%u fsuid=%u"
1171 " egid=%u sgid=%u fsgid=%u ses=%u tty=%s",
1172 sys_getppid(),
1173 tsk->pid,
Linus Torvalds88265322012-10-02 21:38:48 -07001174 from_kuid(&init_user_ns, tsk->loginuid),
1175 from_kuid(&init_user_ns, cred->uid),
1176 from_kgid(&init_user_ns, cred->gid),
1177 from_kuid(&init_user_ns, cred->euid),
1178 from_kuid(&init_user_ns, cred->suid),
1179 from_kuid(&init_user_ns, cred->fsuid),
1180 from_kgid(&init_user_ns, cred->egid),
1181 from_kgid(&init_user_ns, cred->sgid),
1182 from_kgid(&init_user_ns, cred->fsgid),
Peter Moodye23eb922012-06-14 10:04:35 -07001183 tsk->sessionid, tty);
Al Viroe4951492006-03-29 20:17:10 -05001184
Al Viro45d9bb02006-03-29 20:02:55 -05001185 get_task_comm(name, tsk);
David Woodhouse99e45eea2005-05-23 21:57:41 +01001186 audit_log_format(ab, " comm=");
1187 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -07001188
Al Viroe4951492006-03-29 20:17:10 -05001189 if (mm) {
1190 down_read(&mm->mmap_sem);
Konstantin Khlebnikov2dd8ad82012-10-08 16:28:51 -07001191 if (mm->exe_file)
1192 audit_log_d_path(ab, " exe=", &mm->exe_file->f_path);
Al Viroe4951492006-03-29 20:17:10 -05001193 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -07001194 }
Al Viroe4951492006-03-29 20:17:10 -05001195 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -07001196}
1197
Peter Moodye23eb922012-06-14 10:04:35 -07001198EXPORT_SYMBOL(audit_log_task_info);
1199
Amy Griffise54dc242007-03-29 18:01:04 -04001200static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001201 kuid_t auid, kuid_t uid, unsigned int sessionid,
Eric Paris4746ec52008-01-08 10:06:53 -05001202 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -04001203{
1204 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001205 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -04001206 u32 len;
1207 int rc = 0;
1208
1209 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
1210 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -05001211 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -04001212
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001213 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid,
1214 from_kuid(&init_user_ns, auid),
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001215 from_kuid(&init_user_ns, uid), sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001216 if (security_secid_to_secctx(sid, &ctx, &len)) {
Eric Parisc2a77802008-01-07 13:40:17 -05001217 audit_log_format(ab, " obj=(none)");
Amy Griffise54dc242007-03-29 18:01:04 -04001218 rc = 1;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001219 } else {
1220 audit_log_format(ab, " obj=%s", ctx);
1221 security_release_secctx(ctx, len);
1222 }
Eric Parisc2a77802008-01-07 13:40:17 -05001223 audit_log_format(ab, " ocomm=");
1224 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -04001225 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -04001226
1227 return rc;
1228}
1229
Eric Parisde6bbd12008-01-07 14:31:58 -05001230/*
1231 * to_send and len_sent accounting are very loose estimates. We aren't
1232 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001233 * within about 500 bytes (next page boundary)
Eric Parisde6bbd12008-01-07 14:31:58 -05001234 *
1235 * why snprintf? an int is up to 12 digits long. if we just assumed when
1236 * logging that a[%d]= was going to be 16 characters long we would be wasting
1237 * space in every audit message. In one 7500 byte message we can log up to
1238 * about 1000 min size arguments. That comes down to about 50% waste of space
1239 * if we didn't do the snprintf to find out how long arg_num_len was.
1240 */
1241static int audit_log_single_execve_arg(struct audit_context *context,
1242 struct audit_buffer **ab,
1243 int arg_num,
1244 size_t *len_sent,
1245 const char __user *p,
1246 char *buf)
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001247{
Eric Parisde6bbd12008-01-07 14:31:58 -05001248 char arg_num_len_buf[12];
1249 const char __user *tmp_p = p;
Eric Parisb87ce6e2009-06-11 14:31:34 -04001250 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1251 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
Eric Parisde6bbd12008-01-07 14:31:58 -05001252 size_t len, len_left, to_send;
1253 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1254 unsigned int i, has_cntl = 0, too_long = 0;
1255 int ret;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001256
Eric Parisde6bbd12008-01-07 14:31:58 -05001257 /* strnlen_user includes the null we don't want to send */
1258 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001259
Eric Parisde6bbd12008-01-07 14:31:58 -05001260 /*
1261 * We just created this mm, if we can't find the strings
1262 * we just copied into it something is _very_ wrong. Similar
1263 * for strings that are too long, we should not have created
1264 * any.
1265 */
Eric Parisb0abcfc2008-02-18 18:23:16 -05001266 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001267 WARN_ON(1);
1268 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001269 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001270 }
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001271
Eric Parisde6bbd12008-01-07 14:31:58 -05001272 /* walk the whole argument looking for non-ascii chars */
1273 do {
1274 if (len_left > MAX_EXECVE_AUDIT_LEN)
1275 to_send = MAX_EXECVE_AUDIT_LEN;
1276 else
1277 to_send = len_left;
1278 ret = copy_from_user(buf, tmp_p, to_send);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001279 /*
1280 * There is no reason for this copy to be short. We just
1281 * copied them here, and the mm hasn't been exposed to user-
1282 * space yet.
1283 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001284 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001285 WARN_ON(1);
1286 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001287 return -1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001288 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001289 buf[to_send] = '\0';
1290 has_cntl = audit_string_contains_control(buf, to_send);
1291 if (has_cntl) {
1292 /*
1293 * hex messages get logged as 2 bytes, so we can only
1294 * send half as much in each message
1295 */
1296 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1297 break;
1298 }
1299 len_left -= to_send;
1300 tmp_p += to_send;
1301 } while (len_left > 0);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001302
Eric Parisde6bbd12008-01-07 14:31:58 -05001303 len_left = len;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001304
Eric Parisde6bbd12008-01-07 14:31:58 -05001305 if (len > max_execve_audit_len)
1306 too_long = 1;
1307
1308 /* rewalk the argument actually logging the message */
1309 for (i = 0; len_left > 0; i++) {
1310 int room_left;
1311
1312 if (len_left > max_execve_audit_len)
1313 to_send = max_execve_audit_len;
1314 else
1315 to_send = len_left;
1316
1317 /* do we have space left to send this argument in this ab? */
1318 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1319 if (has_cntl)
1320 room_left -= (to_send * 2);
1321 else
1322 room_left -= to_send;
1323 if (room_left < 0) {
1324 *len_sent = 0;
1325 audit_log_end(*ab);
1326 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1327 if (!*ab)
1328 return 0;
1329 }
1330
1331 /*
1332 * first record needs to say how long the original string was
1333 * so we can be sure nothing was lost.
1334 */
1335 if ((i == 0) && (too_long))
Jiri Pirkoca96a892009-01-09 16:44:16 +01001336 audit_log_format(*ab, " a%d_len=%zu", arg_num,
Eric Parisde6bbd12008-01-07 14:31:58 -05001337 has_cntl ? 2*len : len);
1338
1339 /*
1340 * normally arguments are small enough to fit and we already
1341 * filled buf above when we checked for control characters
1342 * so don't bother with another copy_from_user
1343 */
1344 if (len >= max_execve_audit_len)
1345 ret = copy_from_user(buf, p, to_send);
1346 else
1347 ret = 0;
1348 if (ret) {
1349 WARN_ON(1);
1350 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001351 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001352 }
1353 buf[to_send] = '\0';
1354
1355 /* actually log it */
Jiri Pirkoca96a892009-01-09 16:44:16 +01001356 audit_log_format(*ab, " a%d", arg_num);
Eric Parisde6bbd12008-01-07 14:31:58 -05001357 if (too_long)
1358 audit_log_format(*ab, "[%d]", i);
1359 audit_log_format(*ab, "=");
1360 if (has_cntl)
Eric Parisb556f8a2008-04-18 10:12:59 -04001361 audit_log_n_hex(*ab, buf, to_send);
Eric Parisde6bbd12008-01-07 14:31:58 -05001362 else
Eric Paris9d960982009-06-11 14:31:37 -04001363 audit_log_string(*ab, buf);
Eric Parisde6bbd12008-01-07 14:31:58 -05001364
1365 p += to_send;
1366 len_left -= to_send;
1367 *len_sent += arg_num_len;
1368 if (has_cntl)
1369 *len_sent += to_send * 2;
1370 else
1371 *len_sent += to_send;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001372 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001373 /* include the null we didn't log */
1374 return len + 1;
1375}
1376
1377static void audit_log_execve_info(struct audit_context *context,
1378 struct audit_buffer **ab,
1379 struct audit_aux_data_execve *axi)
1380{
Xi Wang5afb8a32011-12-20 18:39:41 -05001381 int i, len;
1382 size_t len_sent = 0;
Eric Parisde6bbd12008-01-07 14:31:58 -05001383 const char __user *p;
1384 char *buf;
1385
1386 if (axi->mm != current->mm)
1387 return; /* execve failed, no additional info */
1388
1389 p = (const char __user *)axi->mm->arg_start;
1390
Jiri Pirkoca96a892009-01-09 16:44:16 +01001391 audit_log_format(*ab, "argc=%d", axi->argc);
Eric Parisde6bbd12008-01-07 14:31:58 -05001392
1393 /*
1394 * we need some kernel buffer to hold the userspace args. Just
1395 * allocate one big one rather than allocating one of the right size
1396 * for every single argument inside audit_log_single_execve_arg()
1397 * should be <8k allocation so should be pretty safe.
1398 */
1399 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1400 if (!buf) {
1401 audit_panic("out of memory for argv string\n");
1402 return;
1403 }
1404
1405 for (i = 0; i < axi->argc; i++) {
1406 len = audit_log_single_execve_arg(context, ab, i,
1407 &len_sent, p, buf);
1408 if (len <= 0)
1409 break;
1410 p += len;
1411 }
1412 kfree(buf);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001413}
1414
Eric Paris851f7ff2008-11-11 21:48:14 +11001415static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1416{
1417 int i;
1418
1419 audit_log_format(ab, " %s=", prefix);
1420 CAP_FOR_EACH_U32(i) {
1421 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1422 }
1423}
1424
1425static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1426{
1427 kernel_cap_t *perm = &name->fcap.permitted;
1428 kernel_cap_t *inh = &name->fcap.inheritable;
1429 int log = 0;
1430
1431 if (!cap_isclear(*perm)) {
1432 audit_log_cap(ab, "cap_fp", perm);
1433 log = 1;
1434 }
1435 if (!cap_isclear(*inh)) {
1436 audit_log_cap(ab, "cap_fi", inh);
1437 log = 1;
1438 }
1439
1440 if (log)
1441 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1442}
1443
Al Viroa33e6752008-12-10 03:40:06 -05001444static void show_special(struct audit_context *context, int *call_panic)
Al Virof3298dc2008-12-10 03:16:51 -05001445{
1446 struct audit_buffer *ab;
1447 int i;
1448
1449 ab = audit_log_start(context, GFP_KERNEL, context->type);
1450 if (!ab)
1451 return;
1452
1453 switch (context->type) {
1454 case AUDIT_SOCKETCALL: {
1455 int nargs = context->socketcall.nargs;
1456 audit_log_format(ab, "nargs=%d", nargs);
1457 for (i = 0; i < nargs; i++)
1458 audit_log_format(ab, " a%d=%lx", i,
1459 context->socketcall.args[i]);
1460 break; }
Al Viroa33e6752008-12-10 03:40:06 -05001461 case AUDIT_IPC: {
1462 u32 osid = context->ipc.osid;
1463
Al Viro2570ebb2011-07-27 14:03:22 -04001464 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001465 from_kuid(&init_user_ns, context->ipc.uid),
1466 from_kgid(&init_user_ns, context->ipc.gid),
1467 context->ipc.mode);
Al Viroa33e6752008-12-10 03:40:06 -05001468 if (osid) {
1469 char *ctx = NULL;
1470 u32 len;
1471 if (security_secid_to_secctx(osid, &ctx, &len)) {
1472 audit_log_format(ab, " osid=%u", osid);
1473 *call_panic = 1;
1474 } else {
1475 audit_log_format(ab, " obj=%s", ctx);
1476 security_release_secctx(ctx, len);
1477 }
1478 }
Al Viroe816f372008-12-10 03:47:15 -05001479 if (context->ipc.has_perm) {
1480 audit_log_end(ab);
1481 ab = audit_log_start(context, GFP_KERNEL,
1482 AUDIT_IPC_SET_PERM);
1483 audit_log_format(ab,
Al Viro2570ebb2011-07-27 14:03:22 -04001484 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
Al Viroe816f372008-12-10 03:47:15 -05001485 context->ipc.qbytes,
1486 context->ipc.perm_uid,
1487 context->ipc.perm_gid,
1488 context->ipc.perm_mode);
1489 if (!ab)
1490 return;
1491 }
Al Viroa33e6752008-12-10 03:40:06 -05001492 break; }
Al Viro564f6992008-12-14 04:02:26 -05001493 case AUDIT_MQ_OPEN: {
1494 audit_log_format(ab,
Al Virodf0a4282011-07-26 05:26:10 -04001495 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
Al Viro564f6992008-12-14 04:02:26 -05001496 "mq_msgsize=%ld mq_curmsgs=%ld",
1497 context->mq_open.oflag, context->mq_open.mode,
1498 context->mq_open.attr.mq_flags,
1499 context->mq_open.attr.mq_maxmsg,
1500 context->mq_open.attr.mq_msgsize,
1501 context->mq_open.attr.mq_curmsgs);
1502 break; }
Al Viroc32c8af2008-12-14 03:46:48 -05001503 case AUDIT_MQ_SENDRECV: {
1504 audit_log_format(ab,
1505 "mqdes=%d msg_len=%zd msg_prio=%u "
1506 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1507 context->mq_sendrecv.mqdes,
1508 context->mq_sendrecv.msg_len,
1509 context->mq_sendrecv.msg_prio,
1510 context->mq_sendrecv.abs_timeout.tv_sec,
1511 context->mq_sendrecv.abs_timeout.tv_nsec);
1512 break; }
Al Viro20114f72008-12-10 07:16:12 -05001513 case AUDIT_MQ_NOTIFY: {
1514 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1515 context->mq_notify.mqdes,
1516 context->mq_notify.sigev_signo);
1517 break; }
Al Viro73929062008-12-10 06:58:59 -05001518 case AUDIT_MQ_GETSETATTR: {
1519 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1520 audit_log_format(ab,
1521 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1522 "mq_curmsgs=%ld ",
1523 context->mq_getsetattr.mqdes,
1524 attr->mq_flags, attr->mq_maxmsg,
1525 attr->mq_msgsize, attr->mq_curmsgs);
1526 break; }
Al Viro57f71a02009-01-04 14:52:57 -05001527 case AUDIT_CAPSET: {
1528 audit_log_format(ab, "pid=%d", context->capset.pid);
1529 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1530 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1531 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1532 break; }
Al Viro120a7952010-10-30 02:54:44 -04001533 case AUDIT_MMAP: {
1534 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1535 context->mmap.flags);
1536 break; }
Al Virof3298dc2008-12-10 03:16:51 -05001537 }
1538 audit_log_end(ab);
1539}
1540
Eric Paris5195d8e2012-01-03 14:23:05 -05001541static void audit_log_name(struct audit_context *context, struct audit_names *n,
1542 int record_num, int *call_panic)
1543{
1544 struct audit_buffer *ab;
1545 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1546 if (!ab)
1547 return; /* audit_panic has been called */
1548
1549 audit_log_format(ab, "item=%d", record_num);
1550
1551 if (n->name) {
1552 switch (n->name_len) {
1553 case AUDIT_NAME_FULL:
1554 /* log the full path */
1555 audit_log_format(ab, " name=");
1556 audit_log_untrustedstring(ab, n->name);
1557 break;
1558 case 0:
1559 /* name was specified as a relative path and the
1560 * directory component is the cwd */
Kees Cookc158a352012-01-06 14:07:10 -08001561 audit_log_d_path(ab, " name=", &context->pwd);
Eric Paris5195d8e2012-01-03 14:23:05 -05001562 break;
1563 default:
1564 /* log the name's directory component */
1565 audit_log_format(ab, " name=");
1566 audit_log_n_untrustedstring(ab, n->name,
1567 n->name_len);
1568 }
1569 } else
1570 audit_log_format(ab, " name=(null)");
1571
1572 if (n->ino != (unsigned long)-1) {
1573 audit_log_format(ab, " inode=%lu"
1574 " dev=%02x:%02x mode=%#ho"
1575 " ouid=%u ogid=%u rdev=%02x:%02x",
1576 n->ino,
1577 MAJOR(n->dev),
1578 MINOR(n->dev),
1579 n->mode,
Eric W. Biedermancca080d2012-02-07 16:53:48 -08001580 from_kuid(&init_user_ns, n->uid),
1581 from_kgid(&init_user_ns, n->gid),
Eric Paris5195d8e2012-01-03 14:23:05 -05001582 MAJOR(n->rdev),
1583 MINOR(n->rdev));
1584 }
1585 if (n->osid != 0) {
1586 char *ctx = NULL;
1587 u32 len;
1588 if (security_secid_to_secctx(
1589 n->osid, &ctx, &len)) {
1590 audit_log_format(ab, " osid=%u", n->osid);
1591 *call_panic = 2;
1592 } else {
1593 audit_log_format(ab, " obj=%s", ctx);
1594 security_release_secctx(ctx, len);
1595 }
1596 }
1597
1598 audit_log_fcaps(ab, n);
1599
1600 audit_log_end(ab);
1601}
1602
Al Viroe4951492006-03-29 20:17:10 -05001603static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001605 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001607 struct audit_aux_data *aux;
Eric Paris5195d8e2012-01-03 14:23:05 -05001608 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Al Viroe4951492006-03-29 20:17:10 -05001610 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001611 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001612
1613 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (!ab)
1615 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001616 audit_log_format(ab, "arch=%x syscall=%d",
1617 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (context->personality != PER_LINUX)
1619 audit_log_format(ab, " per=%lx", context->personality);
1620 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001621 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001622 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1623 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 audit_log_format(ab,
Peter Moodye23eb922012-06-14 10:04:35 -07001626 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1627 context->argv[0],
1628 context->argv[1],
1629 context->argv[2],
1630 context->argv[3],
1631 context->name_count);
Alan Coxeb84a202006-09-29 02:01:41 -07001632
Al Viroe4951492006-03-29 20:17:10 -05001633 audit_log_task_info(ab, tsk);
Eric Paris9d960982009-06-11 14:31:37 -04001634 audit_log_key(ab, context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
David Woodhouse7551ced2005-05-26 12:04:57 +01001637 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001638
Al Viroe4951492006-03-29 20:17:10 -05001639 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (!ab)
1641 continue; /* audit_panic has been called */
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001644
Al Viro473ae302006-04-26 14:04:08 -04001645 case AUDIT_EXECVE: {
1646 struct audit_aux_data_execve *axi = (void *)aux;
Eric Parisde6bbd12008-01-07 14:31:58 -05001647 audit_log_execve_info(context, &ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001648 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001649
Eric Paris3fc689e2008-11-11 21:48:18 +11001650 case AUDIT_BPRM_FCAPS: {
1651 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1652 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1653 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1654 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1655 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1656 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1657 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1658 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1659 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1660 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1661 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1662 break; }
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
Al Virof3298dc2008-12-10 03:16:51 -05001668 if (context->type)
Al Viroa33e6752008-12-10 03:40:06 -05001669 show_special(context, &call_panic);
Al Virof3298dc2008-12-10 03:16:51 -05001670
Al Viro157cf642008-12-14 04:57:47 -05001671 if (context->fds[0] >= 0) {
1672 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1673 if (ab) {
1674 audit_log_format(ab, "fd0=%d fd1=%d",
1675 context->fds[0], context->fds[1]);
1676 audit_log_end(ab);
1677 }
1678 }
1679
Al Viro4f6b4342008-12-09 19:50:34 -05001680 if (context->sockaddr_len) {
1681 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1682 if (ab) {
1683 audit_log_format(ab, "saddr=");
1684 audit_log_n_hex(ab, (void *)context->sockaddr,
1685 context->sockaddr_len);
1686 audit_log_end(ab);
1687 }
1688 }
1689
Amy Griffise54dc242007-03-29 18:01:04 -04001690 for (aux = context->aux_pids; aux; aux = aux->next) {
1691 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001692
1693 for (i = 0; i < axs->pid_count; i++)
1694 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001695 axs->target_auid[i],
1696 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001697 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001698 axs->target_sid[i],
1699 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001700 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001701 }
1702
Amy Griffise54dc242007-03-29 18:01:04 -04001703 if (context->target_pid &&
1704 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001705 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001706 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001707 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001708 call_panic = 1;
1709
Jan Blunck44707fd2008-02-14 19:38:33 -08001710 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001711 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001712 if (ab) {
Kees Cookc158a352012-01-06 14:07:10 -08001713 audit_log_d_path(ab, " cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001714 audit_log_end(ab);
1715 }
1716 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001717
Eric Paris5195d8e2012-01-03 14:23:05 -05001718 i = 0;
1719 list_for_each_entry(n, &context->names_list, list)
1720 audit_log_name(context, n, i++, &call_panic);
Eric Parisc0641f22008-01-07 13:49:15 -05001721
1722 /* Send end of event record to help user space know we are finished */
1723 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1724 if (ab)
1725 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001726 if (call_panic)
1727 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001730/**
1731 * audit_free - free a per-task audit context
1732 * @tsk: task whose audit context block to free
1733 *
Al Virofa84cb92006-03-29 20:30:19 -05001734 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001735 */
Eric Parisa4ff8db2012-01-03 14:23:07 -05001736void __audit_free(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct audit_context *context;
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 context = audit_get_context(tsk, 0, 0);
Eric Paris56179a62012-01-03 14:23:06 -05001741 if (!context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return;
1743
1744 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001745 * function (e.g., exit_group), then free context block.
1746 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001747 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001748 /* that can happen only if we are called from do_exit() */
Al Viro0590b932008-12-14 23:45:27 -05001749 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001750 audit_log_exit(context, tsk);
Al Viro916d7572009-06-24 00:02:38 -04001751 if (!list_empty(&context->killed_trees))
1752 audit_kill_trees(&context->killed_trees);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 audit_free_context(context);
1755}
1756
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001757/**
1758 * audit_syscall_entry - fill in an audit record at syscall entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001759 * @arch: architecture type
1760 * @major: major syscall type (function)
1761 * @a1: additional syscall register 1
1762 * @a2: additional syscall register 2
1763 * @a3: additional syscall register 3
1764 * @a4: additional syscall register 4
1765 *
1766 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 * audit context was created when the task was created and the state or
1768 * filters demand the audit context be built. If the state from the
1769 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1770 * then the record will be written at syscall exit time (otherwise, it
1771 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001772 * be written).
1773 */
Eric Parisb05d8442012-01-03 14:23:06 -05001774void __audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 unsigned long a1, unsigned long a2,
1776 unsigned long a3, unsigned long a4)
1777{
Al Viro5411be52006-03-29 20:23:36 -05001778 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct audit_context *context = tsk->audit_context;
1780 enum audit_state state;
1781
Eric Paris56179a62012-01-03 14:23:06 -05001782 if (!context)
Roland McGrath86a1c342008-06-23 15:37:04 -07001783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001785 /*
1786 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 * calls in kernel_thread via the entry.S interface, instead of
1788 * with direct calls. (If you are porting to a new
1789 * architecture, hitting this condition can indicate that you
1790 * got the _exit/_leave calls backward in entry.S.)
1791 *
1792 * i386 no
1793 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001794 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 *
1796 * This also happens with vm86 emulation in a non-nested manner
1797 * (entries without exits), so this case must be caught.
1798 */
1799 if (context->in_syscall) {
1800 struct audit_context *newctx;
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802#if AUDIT_DEBUG
1803 printk(KERN_ERR
1804 "audit(:%d) pid=%d in syscall=%d;"
1805 " entering syscall=%d\n",
1806 context->serial, tsk->pid, context->major, major);
1807#endif
1808 newctx = audit_alloc_context(context->state);
1809 if (newctx) {
1810 newctx->previous = context;
1811 context = newctx;
1812 tsk->audit_context = newctx;
1813 } else {
1814 /* If we can't alloc a new context, the best we
1815 * can do is to leak memory (any pending putname
1816 * will be lost). The only other alternative is
1817 * to abandon auditing. */
1818 audit_zero_context(context, context->state);
1819 }
1820 }
1821 BUG_ON(context->in_syscall || context->name_count);
1822
1823 if (!audit_enabled)
1824 return;
1825
2fd6f582005-04-29 16:08:28 +01001826 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 context->major = major;
1828 context->argv[0] = a1;
1829 context->argv[1] = a2;
1830 context->argv[2] = a3;
1831 context->argv[3] = a4;
1832
1833 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001834 context->dummy = !audit_n_rules;
Al Viro0590b932008-12-14 23:45:27 -05001835 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1836 context->prio = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001837 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Al Viro0590b932008-12-14 23:45:27 -05001838 }
Eric Paris56179a62012-01-03 14:23:06 -05001839 if (state == AUDIT_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return;
1841
David Woodhousece625a82005-07-18 14:24:46 -04001842 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 context->ctime = CURRENT_TIME;
1844 context->in_syscall = 1;
Al Viro0590b932008-12-14 23:45:27 -05001845 context->current_state = state;
Alexander Viro419c58f2006-09-29 00:08:50 -04001846 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001849/**
1850 * audit_syscall_exit - deallocate audit context after a system call
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001851 * @success: success value of the syscall
1852 * @return_code: return value of the syscall
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001853 *
1854 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
Randy Dunlap42ae610c2012-01-21 11:02:24 -08001856 * filtering, or because some other part of the kernel wrote an audit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001858 * free the names stored from getname().
1859 */
Eric Parisd7e75282012-01-03 14:23:06 -05001860void __audit_syscall_exit(int success, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Al Viro5411be52006-03-29 20:23:36 -05001862 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct audit_context *context;
1864
Eric Parisd7e75282012-01-03 14:23:06 -05001865 if (success)
1866 success = AUDITSC_SUCCESS;
1867 else
1868 success = AUDITSC_FAILURE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Eric Parisd7e75282012-01-03 14:23:06 -05001870 context = audit_get_context(tsk, success, return_code);
Eric Paris56179a62012-01-03 14:23:06 -05001871 if (!context)
Al Viro97e94c42006-03-29 20:26:24 -05001872 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Al Viro0590b932008-12-14 23:45:27 -05001874 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001875 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 context->in_syscall = 0;
Al Viro0590b932008-12-14 23:45:27 -05001878 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
2fd6f582005-04-29 16:08:28 +01001879
Al Viro916d7572009-06-24 00:02:38 -04001880 if (!list_empty(&context->killed_trees))
1881 audit_kill_trees(&context->killed_trees);
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (context->previous) {
1884 struct audit_context *new_context = context->previous;
1885 context->previous = NULL;
1886 audit_free_context(context);
1887 tsk->audit_context = new_context;
1888 } else {
1889 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001890 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001892 context->aux = NULL;
1893 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001894 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001895 context->target_sid = 0;
Al Viro4f6b4342008-12-09 19:50:34 -05001896 context->sockaddr_len = 0;
Al Virof3298dc2008-12-10 03:16:51 -05001897 context->type = 0;
Al Viro157cf642008-12-14 04:57:47 -05001898 context->fds[0] = -1;
Al Viroe048e022008-12-16 03:51:22 -05001899 if (context->state != AUDIT_RECORD_CONTEXT) {
1900 kfree(context->filterkey);
1901 context->filterkey = NULL;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 tsk->audit_context = context;
1904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Al Viro74c3cbe2007-07-22 08:04:18 -04001907static inline void handle_one(const struct inode *inode)
1908{
1909#ifdef CONFIG_AUDIT_TREE
1910 struct audit_context *context;
1911 struct audit_tree_refs *p;
1912 struct audit_chunk *chunk;
1913 int count;
Eric Parise61ce862009-12-17 21:24:24 -05001914 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
Al Viro74c3cbe2007-07-22 08:04:18 -04001915 return;
1916 context = current->audit_context;
1917 p = context->trees;
1918 count = context->tree_count;
1919 rcu_read_lock();
1920 chunk = audit_tree_lookup(inode);
1921 rcu_read_unlock();
1922 if (!chunk)
1923 return;
1924 if (likely(put_tree_ref(context, chunk)))
1925 return;
1926 if (unlikely(!grow_tree_refs(context))) {
Eric Paris436c4052008-04-18 10:01:04 -04001927 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001928 audit_set_auditable(context);
1929 audit_put_chunk(chunk);
1930 unroll_tree_refs(context, p, count);
1931 return;
1932 }
1933 put_tree_ref(context, chunk);
1934#endif
1935}
1936
1937static void handle_path(const struct dentry *dentry)
1938{
1939#ifdef CONFIG_AUDIT_TREE
1940 struct audit_context *context;
1941 struct audit_tree_refs *p;
1942 const struct dentry *d, *parent;
1943 struct audit_chunk *drop;
1944 unsigned long seq;
1945 int count;
1946
1947 context = current->audit_context;
1948 p = context->trees;
1949 count = context->tree_count;
1950retry:
1951 drop = NULL;
1952 d = dentry;
1953 rcu_read_lock();
1954 seq = read_seqbegin(&rename_lock);
1955 for(;;) {
1956 struct inode *inode = d->d_inode;
Eric Parise61ce862009-12-17 21:24:24 -05001957 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
Al Viro74c3cbe2007-07-22 08:04:18 -04001958 struct audit_chunk *chunk;
1959 chunk = audit_tree_lookup(inode);
1960 if (chunk) {
1961 if (unlikely(!put_tree_ref(context, chunk))) {
1962 drop = chunk;
1963 break;
1964 }
1965 }
1966 }
1967 parent = d->d_parent;
1968 if (parent == d)
1969 break;
1970 d = parent;
1971 }
1972 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1973 rcu_read_unlock();
1974 if (!drop) {
1975 /* just a race with rename */
1976 unroll_tree_refs(context, p, count);
1977 goto retry;
1978 }
1979 audit_put_chunk(drop);
1980 if (grow_tree_refs(context)) {
1981 /* OK, got more space */
1982 unroll_tree_refs(context, p, count);
1983 goto retry;
1984 }
1985 /* too bad */
1986 printk(KERN_WARNING
Eric Paris436c4052008-04-18 10:01:04 -04001987 "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001988 unroll_tree_refs(context, p, count);
1989 audit_set_auditable(context);
1990 return;
1991 }
1992 rcu_read_unlock();
1993#endif
1994}
1995
Jeff Layton78e2e802012-10-10 15:25:22 -04001996static struct audit_names *audit_alloc_name(struct audit_context *context,
1997 unsigned char type)
Eric Paris5195d8e2012-01-03 14:23:05 -05001998{
1999 struct audit_names *aname;
2000
2001 if (context->name_count < AUDIT_NAMES) {
2002 aname = &context->preallocated_names[context->name_count];
2003 memset(aname, 0, sizeof(*aname));
2004 } else {
2005 aname = kzalloc(sizeof(*aname), GFP_NOFS);
2006 if (!aname)
2007 return NULL;
2008 aname->should_free = true;
2009 }
2010
2011 aname->ino = (unsigned long)-1;
Jeff Layton78e2e802012-10-10 15:25:22 -04002012 aname->type = type;
Eric Paris5195d8e2012-01-03 14:23:05 -05002013 list_add_tail(&aname->list, &context->names_list);
2014
2015 context->name_count++;
2016#if AUDIT_DEBUG
2017 context->ino_count++;
2018#endif
2019 return aname;
2020}
2021
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002022/**
2023 * audit_getname - add a name to the list
2024 * @name: name to add
2025 *
2026 * Add a name to the list of audit names for this context.
2027 * Called from fs/namei.c:getname().
2028 */
Al Virod8945bb52006-05-18 16:01:30 -04002029void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 struct audit_context *context = current->audit_context;
Eric Paris5195d8e2012-01-03 14:23:05 -05002032 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (!context->in_syscall) {
2035#if AUDIT_DEBUG == 2
2036 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
2037 __FILE__, __LINE__, context->serial, name);
2038 dump_stack();
2039#endif
2040 return;
2041 }
Eric Paris5195d8e2012-01-03 14:23:05 -05002042
Jeff Layton78e2e802012-10-10 15:25:22 -04002043 n = audit_alloc_name(context, AUDIT_TYPE_UNKNOWN);
Eric Paris5195d8e2012-01-03 14:23:05 -05002044 if (!n)
2045 return;
2046
2047 n->name = name;
2048 n->name_len = AUDIT_NAME_FULL;
2049 n->name_put = true;
2050
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002051 if (!context->pwd.dentry)
2052 get_fs_pwd(current->fs, &context->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002055/* audit_putname - intercept a putname request
2056 * @name: name to intercept and delay for putname
2057 *
2058 * If we have stored the name from getname in the audit context,
2059 * then we delay the putname until syscall exit.
2060 * Called from include/linux/fs.h:putname().
2061 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062void audit_putname(const char *name)
2063{
2064 struct audit_context *context = current->audit_context;
2065
2066 BUG_ON(!context);
2067 if (!context->in_syscall) {
2068#if AUDIT_DEBUG == 2
2069 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
2070 __FILE__, __LINE__, context->serial, name);
2071 if (context->name_count) {
Eric Paris5195d8e2012-01-03 14:23:05 -05002072 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 int i;
Eric Paris5195d8e2012-01-03 14:23:05 -05002074
2075 list_for_each_entry(n, &context->names_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 printk(KERN_ERR "name[%d] = %p = %s\n", i,
Eric Paris5195d8e2012-01-03 14:23:05 -05002077 n->name, n->name ?: "(null)");
2078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079#endif
2080 __putname(name);
2081 }
2082#if AUDIT_DEBUG
2083 else {
2084 ++context->put_count;
2085 if (context->put_count > context->name_count) {
2086 printk(KERN_ERR "%s:%d(:%d): major=%d"
2087 " in_syscall=%d putname(%p) name_count=%d"
2088 " put_count=%d\n",
2089 __FILE__, __LINE__,
2090 context->serial, context->major,
2091 context->in_syscall, name, context->name_count,
2092 context->put_count);
2093 dump_stack();
2094 }
2095 }
2096#endif
2097}
2098
Eric Paris851f7ff2008-11-11 21:48:14 +11002099static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
2100{
2101 struct cpu_vfs_cap_data caps;
2102 int rc;
2103
Eric Paris851f7ff2008-11-11 21:48:14 +11002104 if (!dentry)
2105 return 0;
2106
2107 rc = get_vfs_caps_from_disk(dentry, &caps);
2108 if (rc)
2109 return rc;
2110
2111 name->fcap.permitted = caps.permitted;
2112 name->fcap.inheritable = caps.inheritable;
2113 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2114 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2115
2116 return 0;
2117}
2118
2119
Amy Griffis3e2efce2006-07-13 13:16:02 -04002120/* Copy inode data into an audit_names. */
Eric Paris851f7ff2008-11-11 21:48:14 +11002121static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
2122 const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002123{
Amy Griffis3e2efce2006-07-13 13:16:02 -04002124 name->ino = inode->i_ino;
2125 name->dev = inode->i_sb->s_dev;
2126 name->mode = inode->i_mode;
2127 name->uid = inode->i_uid;
2128 name->gid = inode->i_gid;
2129 name->rdev = inode->i_rdev;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002130 security_inode_getsecid(inode, &name->osid);
Eric Paris851f7ff2008-11-11 21:48:14 +11002131 audit_copy_fcaps(name, dentry);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002132}
2133
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002134/**
Jeff Laytonbfcec702012-10-10 15:25:23 -04002135 * __audit_inode - store the inode and device from a lookup
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002136 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07002137 * @dentry: dentry being audited
Jeff Laytonbfcec702012-10-10 15:25:23 -04002138 * @parent: does this dentry represent the parent?
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002139 */
Jeff Laytonbfcec702012-10-10 15:25:23 -04002140void __audit_inode(const char *name, const struct dentry *dentry,
2141 unsigned int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04002144 const struct inode *inode = dentry->d_inode;
Eric Paris5195d8e2012-01-03 14:23:05 -05002145 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 if (!context->in_syscall)
2148 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05002149
Jeff Layton9cec9d62012-10-10 15:25:21 -04002150 if (!name)
2151 goto out_alloc;
2152
Eric Paris5195d8e2012-01-03 14:23:05 -05002153 list_for_each_entry_reverse(n, &context->names_list, list) {
Jeff Laytonbfcec702012-10-10 15:25:23 -04002154 /* does the name pointer match? */
2155 if (n->name != name)
2156 continue;
2157
2158 /* match the correct record type */
2159 if (parent) {
2160 if (n->type == AUDIT_TYPE_PARENT ||
2161 n->type == AUDIT_TYPE_UNKNOWN)
2162 goto out;
2163 } else {
2164 if (n->type != AUDIT_TYPE_PARENT)
2165 goto out;
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
Eric Paris5195d8e2012-01-03 14:23:05 -05002168
Jeff Layton9cec9d62012-10-10 15:25:21 -04002169out_alloc:
Jeff Laytonbfcec702012-10-10 15:25:23 -04002170 /* unable to find the name from a previous getname(). Allocate a new
2171 * anonymous entry.
2172 */
Jeff Layton78e2e802012-10-10 15:25:22 -04002173 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
Eric Paris5195d8e2012-01-03 14:23:05 -05002174 if (!n)
2175 return;
2176out:
Jeff Laytonbfcec702012-10-10 15:25:23 -04002177 if (parent) {
2178 n->name_len = n->name ? parent_len(n->name) : AUDIT_NAME_FULL;
2179 n->type = AUDIT_TYPE_PARENT;
2180 } else {
2181 n->name_len = AUDIT_NAME_FULL;
2182 n->type = AUDIT_TYPE_NORMAL;
2183 }
Al Viro74c3cbe2007-07-22 08:04:18 -04002184 handle_path(dentry);
Eric Paris5195d8e2012-01-03 14:23:05 -05002185 audit_copy_inode(n, dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
Amy Griffis73241cc2005-11-03 16:00:25 +00002188/**
Jeff Laytonc43a25a2012-10-10 15:25:21 -04002189 * __audit_inode_child - collect inode info for created/removed objects
Amy Griffis73d3ec52006-07-13 13:16:39 -04002190 * @parent: inode of dentry parent
Jeff Laytonc43a25a2012-10-10 15:25:21 -04002191 * @dentry: dentry being audited
Amy Griffis73241cc2005-11-03 16:00:25 +00002192 *
2193 * For syscalls that create or remove filesystem objects, audit_inode
2194 * can only collect information for the filesystem object's parent.
2195 * This call updates the audit context with the child's information.
2196 * Syscalls that create a new filesystem object must be hooked after
2197 * the object is created. Syscalls that remove a filesystem object
2198 * must be hooked prior, in order to capture the target inode during
2199 * unsuccessful attempts.
2200 */
Jeff Laytonc43a25a2012-10-10 15:25:21 -04002201void __audit_inode_child(const struct inode *parent,
2202 const struct dentry *dentry)
Amy Griffis73241cc2005-11-03 16:00:25 +00002203{
Amy Griffis73241cc2005-11-03 16:00:25 +00002204 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05002205 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04002206 const struct inode *inode = dentry->d_inode;
Al Virocccc6bb2009-12-25 05:07:33 -05002207 const char *dname = dentry->d_name.name;
Eric Paris5195d8e2012-01-03 14:23:05 -05002208 struct audit_names *n;
Amy Griffis73241cc2005-11-03 16:00:25 +00002209
2210 if (!context->in_syscall)
2211 return;
2212
Al Viro74c3cbe2007-07-22 08:04:18 -04002213 if (inode)
2214 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00002215
Amy Griffis5712e882007-02-13 14:15:22 -05002216 /* parent is more likely, look for it first */
Eric Paris5195d8e2012-01-03 14:23:05 -05002217 list_for_each_entry(n, &context->names_list, list) {
Amy Griffis5712e882007-02-13 14:15:22 -05002218 if (!n->name)
2219 continue;
2220
2221 if (n->ino == parent->i_ino &&
Jeff Laytone3d6b072012-10-10 15:25:25 -04002222 !audit_compare_dname_path(dname, n->name, n->name_len)) {
Amy Griffis5712e882007-02-13 14:15:22 -05002223 found_parent = n->name;
2224 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04002225 }
Steve Grubbac9910c2006-09-28 14:31:32 -04002226 }
Amy Griffis73241cc2005-11-03 16:00:25 +00002227
Amy Griffis5712e882007-02-13 14:15:22 -05002228 /* no matching parent, look for matching child */
Eric Paris5195d8e2012-01-03 14:23:05 -05002229 list_for_each_entry(n, &context->names_list, list) {
Amy Griffis5712e882007-02-13 14:15:22 -05002230 if (!n->name)
2231 continue;
2232
2233 /* strcmp() is the more likely scenario */
2234 if (!strcmp(dname, n->name) ||
Jeff Laytone3d6b072012-10-10 15:25:25 -04002235 !audit_compare_dname_path(dname, n->name,
2236 AUDIT_NAME_FULL)) {
Amy Griffis5712e882007-02-13 14:15:22 -05002237 if (inode)
Jeff Layton1c2e51e2012-10-10 15:25:20 -04002238 audit_copy_inode(n, dentry, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002239 else
2240 n->ino = (unsigned long)-1;
Jeff Layton78e2e802012-10-10 15:25:22 -04002241 n->type = AUDIT_TYPE_NORMAL;
Amy Griffis5712e882007-02-13 14:15:22 -05002242 found_child = n->name;
2243 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04002244 }
Amy Griffis5712e882007-02-13 14:15:22 -05002245 }
2246
2247add_names:
2248 if (!found_parent) {
Jeff Layton78e2e802012-10-10 15:25:22 -04002249 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
Eric Paris5195d8e2012-01-03 14:23:05 -05002250 if (!n)
Amy Griffis5712e882007-02-13 14:15:22 -05002251 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05002252 audit_copy_inode(n, NULL, parent);
Amy Griffis73d3ec52006-07-13 13:16:39 -04002253 }
Amy Griffis5712e882007-02-13 14:15:22 -05002254
2255 if (!found_child) {
Jeff Layton78e2e802012-10-10 15:25:22 -04002256 n = audit_alloc_name(context, AUDIT_TYPE_NORMAL);
Eric Paris5195d8e2012-01-03 14:23:05 -05002257 if (!n)
Amy Griffis5712e882007-02-13 14:15:22 -05002258 return;
Amy Griffis5712e882007-02-13 14:15:22 -05002259
2260 /* Re-use the name belonging to the slot for a matching parent
2261 * directory. All names for this context are relinquished in
2262 * audit_free_names() */
2263 if (found_parent) {
Eric Paris5195d8e2012-01-03 14:23:05 -05002264 n->name = found_parent;
2265 n->name_len = AUDIT_NAME_FULL;
Amy Griffis5712e882007-02-13 14:15:22 -05002266 /* don't call __putname() */
Eric Paris5195d8e2012-01-03 14:23:05 -05002267 n->name_put = false;
Amy Griffis5712e882007-02-13 14:15:22 -05002268 }
2269
2270 if (inode)
Jeff Layton1c2e51e2012-10-10 15:25:20 -04002271 audit_copy_inode(n, dentry, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002272 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04002273}
Trond Myklebust50e437d2007-06-07 22:44:34 -04002274EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04002275
2276/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002277 * auditsc_get_stamp - get local copies of audit_context values
2278 * @ctx: audit_context for the task
2279 * @t: timespec to store time recorded in the audit_context
2280 * @serial: serial value that is recorded in the audit_context
2281 *
2282 * Also sets the context as auditable.
2283 */
Al Viro48887e62008-12-06 01:05:50 -05002284int auditsc_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01002285 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Al Viro48887e62008-12-06 01:05:50 -05002287 if (!ctx->in_syscall)
2288 return 0;
David Woodhousece625a82005-07-18 14:24:46 -04002289 if (!ctx->serial)
2290 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01002291 t->tv_sec = ctx->ctime.tv_sec;
2292 t->tv_nsec = ctx->ctime.tv_nsec;
2293 *serial = ctx->serial;
Al Viro0590b932008-12-14 23:45:27 -05002294 if (!ctx->prio) {
2295 ctx->prio = 1;
2296 ctx->current_state = AUDIT_RECORD_CONTEXT;
2297 }
Al Viro48887e62008-12-06 01:05:50 -05002298 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
Eric Paris4746ec52008-01-08 10:06:53 -05002301/* global counter which is incremented every time something logs in */
2302static atomic_t session_id = ATOMIC_INIT(0);
2303
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002304/**
Eric Paris0a300be2012-01-03 14:23:08 -05002305 * audit_set_loginuid - set current task's audit_context loginuid
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002306 * @loginuid: loginuid value
2307 *
2308 * Returns 0.
2309 *
2310 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2311 */
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002312int audit_set_loginuid(kuid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Eric Paris0a300be2012-01-03 14:23:08 -05002314 struct task_struct *task = current;
Steve Grubb41757102006-06-12 07:48:28 -04002315 struct audit_context *context = task->audit_context;
Eric Paris633b4542012-01-03 14:23:08 -05002316 unsigned int sessionid;
Steve Grubbc0404992005-05-13 18:17:42 +01002317
Eric Paris633b4542012-01-03 14:23:08 -05002318#ifdef CONFIG_AUDIT_LOGINUID_IMMUTABLE
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002319 if (uid_valid(task->loginuid))
Eric Paris633b4542012-01-03 14:23:08 -05002320 return -EPERM;
2321#else /* CONFIG_AUDIT_LOGINUID_IMMUTABLE */
2322 if (!capable(CAP_AUDIT_CONTROL))
2323 return -EPERM;
2324#endif /* CONFIG_AUDIT_LOGINUID_IMMUTABLE */
2325
2326 sessionid = atomic_inc_return(&session_id);
Al Virobfef93a2008-01-10 04:53:18 -05002327 if (context && context->in_syscall) {
2328 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04002329
Al Virobfef93a2008-01-10 04:53:18 -05002330 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2331 if (ab) {
2332 audit_log_format(ab, "login pid=%d uid=%u "
Eric Paris4746ec52008-01-08 10:06:53 -05002333 "old auid=%u new auid=%u"
2334 " old ses=%u new ses=%u",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002335 task->pid,
2336 from_kuid(&init_user_ns, task_uid(task)),
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002337 from_kuid(&init_user_ns, task->loginuid),
2338 from_kuid(&init_user_ns, loginuid),
Eric Paris4746ec52008-01-08 10:06:53 -05002339 task->sessionid, sessionid);
Al Virobfef93a2008-01-10 04:53:18 -05002340 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01002341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
Eric Paris4746ec52008-01-08 10:06:53 -05002343 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05002344 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return 0;
2346}
2347
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002348/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002349 * __audit_mq_open - record audit data for a POSIX MQ open
2350 * @oflag: open flag
2351 * @mode: mode bits
Randy Dunlap6b962552009-01-05 13:41:13 -08002352 * @attr: queue attributes
George C. Wilson20ca73b2006-05-24 16:09:55 -05002353 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002354 */
Al Virodf0a4282011-07-26 05:26:10 -04002355void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002356{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002357 struct audit_context *context = current->audit_context;
2358
Al Viro564f6992008-12-14 04:02:26 -05002359 if (attr)
2360 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2361 else
2362 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002363
Al Viro564f6992008-12-14 04:02:26 -05002364 context->mq_open.oflag = oflag;
2365 context->mq_open.mode = mode;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002366
Al Viro564f6992008-12-14 04:02:26 -05002367 context->type = AUDIT_MQ_OPEN;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002368}
2369
2370/**
Al Viroc32c8af2008-12-14 03:46:48 -05002371 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
George C. Wilson20ca73b2006-05-24 16:09:55 -05002372 * @mqdes: MQ descriptor
2373 * @msg_len: Message length
2374 * @msg_prio: Message priority
Al Viroc32c8af2008-12-14 03:46:48 -05002375 * @abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002376 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002377 */
Al Viroc32c8af2008-12-14 03:46:48 -05002378void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2379 const struct timespec *abs_timeout)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002380{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002381 struct audit_context *context = current->audit_context;
Al Viroc32c8af2008-12-14 03:46:48 -05002382 struct timespec *p = &context->mq_sendrecv.abs_timeout;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002383
Al Viroc32c8af2008-12-14 03:46:48 -05002384 if (abs_timeout)
2385 memcpy(p, abs_timeout, sizeof(struct timespec));
2386 else
2387 memset(p, 0, sizeof(struct timespec));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002388
Al Viroc32c8af2008-12-14 03:46:48 -05002389 context->mq_sendrecv.mqdes = mqdes;
2390 context->mq_sendrecv.msg_len = msg_len;
2391 context->mq_sendrecv.msg_prio = msg_prio;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002392
Al Viroc32c8af2008-12-14 03:46:48 -05002393 context->type = AUDIT_MQ_SENDRECV;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002394}
2395
2396/**
2397 * __audit_mq_notify - record audit data for a POSIX MQ notify
2398 * @mqdes: MQ descriptor
Randy Dunlap6b962552009-01-05 13:41:13 -08002399 * @notification: Notification event
George C. Wilson20ca73b2006-05-24 16:09:55 -05002400 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002401 */
2402
Al Viro20114f72008-12-10 07:16:12 -05002403void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002404{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002405 struct audit_context *context = current->audit_context;
2406
Al Viro20114f72008-12-10 07:16:12 -05002407 if (notification)
2408 context->mq_notify.sigev_signo = notification->sigev_signo;
2409 else
2410 context->mq_notify.sigev_signo = 0;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002411
Al Viro20114f72008-12-10 07:16:12 -05002412 context->mq_notify.mqdes = mqdes;
2413 context->type = AUDIT_MQ_NOTIFY;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002414}
2415
2416/**
2417 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2418 * @mqdes: MQ descriptor
2419 * @mqstat: MQ flags
2420 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002421 */
Al Viro73929062008-12-10 06:58:59 -05002422void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002423{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002424 struct audit_context *context = current->audit_context;
Al Viro73929062008-12-10 06:58:59 -05002425 context->mq_getsetattr.mqdes = mqdes;
2426 context->mq_getsetattr.mqstat = *mqstat;
2427 context->type = AUDIT_MQ_GETSETATTR;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002428}
2429
2430/**
Steve Grubb073115d2006-04-02 17:07:33 -04002431 * audit_ipc_obj - record audit data for ipc object
2432 * @ipcp: ipc permissions
2433 *
Steve Grubb073115d2006-04-02 17:07:33 -04002434 */
Al Viroa33e6752008-12-10 03:40:06 -05002435void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002436{
Steve Grubb073115d2006-04-02 17:07:33 -04002437 struct audit_context *context = current->audit_context;
Al Viroa33e6752008-12-10 03:40:06 -05002438 context->ipc.uid = ipcp->uid;
2439 context->ipc.gid = ipcp->gid;
2440 context->ipc.mode = ipcp->mode;
Al Viroe816f372008-12-10 03:47:15 -05002441 context->ipc.has_perm = 0;
Al Viroa33e6752008-12-10 03:40:06 -05002442 security_ipc_getsecid(ipcp, &context->ipc.osid);
2443 context->type = AUDIT_IPC;
Steve Grubb073115d2006-04-02 17:07:33 -04002444}
2445
2446/**
2447 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002448 * @qbytes: msgq bytes
2449 * @uid: msgq user id
2450 * @gid: msgq group id
2451 * @mode: msgq mode (permissions)
2452 *
Al Viroe816f372008-12-10 03:47:15 -05002453 * Called only after audit_ipc_obj().
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002454 */
Al Viro2570ebb2011-07-27 14:03:22 -04002455void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct audit_context *context = current->audit_context;
2458
Al Viroe816f372008-12-10 03:47:15 -05002459 context->ipc.qbytes = qbytes;
2460 context->ipc.perm_uid = uid;
2461 context->ipc.perm_gid = gid;
2462 context->ipc.perm_mode = mode;
2463 context->ipc.has_perm = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002465
Eric Paris07c49412012-01-03 14:23:07 -05002466int __audit_bprm(struct linux_binprm *bprm)
Al Viro473ae302006-04-26 14:04:08 -04002467{
2468 struct audit_aux_data_execve *ax;
2469 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002470
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002471 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002472 if (!ax)
2473 return -ENOMEM;
2474
2475 ax->argc = bprm->argc;
2476 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002477 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002478 ax->d.type = AUDIT_EXECVE;
2479 ax->d.next = context->aux;
2480 context->aux = (void *)ax;
2481 return 0;
2482}
2483
2484
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002485/**
2486 * audit_socketcall - record audit data for sys_socketcall
2487 * @nargs: number of args
2488 * @args: args array
2489 *
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002490 */
Eric Paris07c49412012-01-03 14:23:07 -05002491void __audit_socketcall(int nargs, unsigned long *args)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002492{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002493 struct audit_context *context = current->audit_context;
2494
Al Virof3298dc2008-12-10 03:16:51 -05002495 context->type = AUDIT_SOCKETCALL;
2496 context->socketcall.nargs = nargs;
2497 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002498}
2499
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002500/**
Al Virodb349502007-02-07 01:48:00 -05002501 * __audit_fd_pair - record audit data for pipe and socketpair
2502 * @fd1: the first file descriptor
2503 * @fd2: the second file descriptor
2504 *
Al Virodb349502007-02-07 01:48:00 -05002505 */
Al Viro157cf642008-12-14 04:57:47 -05002506void __audit_fd_pair(int fd1, int fd2)
Al Virodb349502007-02-07 01:48:00 -05002507{
2508 struct audit_context *context = current->audit_context;
Al Viro157cf642008-12-14 04:57:47 -05002509 context->fds[0] = fd1;
2510 context->fds[1] = fd2;
Al Virodb349502007-02-07 01:48:00 -05002511}
2512
2513/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002514 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2515 * @len: data length in user space
2516 * @a: data address in kernel space
2517 *
2518 * Returns 0 for success or NULL context or < 0 on error.
2519 */
Eric Paris07c49412012-01-03 14:23:07 -05002520int __audit_sockaddr(int len, void *a)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002521{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002522 struct audit_context *context = current->audit_context;
2523
Al Viro4f6b4342008-12-09 19:50:34 -05002524 if (!context->sockaddr) {
2525 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2526 if (!p)
2527 return -ENOMEM;
2528 context->sockaddr = p;
2529 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002530
Al Viro4f6b4342008-12-09 19:50:34 -05002531 context->sockaddr_len = len;
2532 memcpy(context->sockaddr, a, len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002533 return 0;
2534}
2535
Al Viroa5cb0132007-03-20 13:58:35 -04002536void __audit_ptrace(struct task_struct *t)
2537{
2538 struct audit_context *context = current->audit_context;
2539
2540 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002541 context->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002542 context->target_uid = task_uid(t);
Eric Paris4746ec52008-01-08 10:06:53 -05002543 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002544 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002545 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002546}
2547
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002548/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002549 * audit_signal_info - record signal info for shutting down audit subsystem
2550 * @sig: signal value
2551 * @t: task being signaled
2552 *
2553 * If the audit subsystem is being terminated, record the task (pid)
2554 * and uid that is doing that.
2555 */
Amy Griffise54dc242007-03-29 18:01:04 -04002556int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002557{
Amy Griffise54dc242007-03-29 18:01:04 -04002558 struct audit_aux_data_pids *axp;
2559 struct task_struct *tsk = current;
2560 struct audit_context *ctx = tsk->audit_context;
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002561 kuid_t uid = current_uid(), t_uid = task_uid(t);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002562
Al Viro175fc482007-08-08 00:01:46 +01002563 if (audit_pid && t->tgid == audit_pid) {
Eric Parisee1d3152008-07-07 10:49:45 -04002564 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
Al Viro175fc482007-08-08 00:01:46 +01002565 audit_sig_pid = tsk->pid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07002566 if (uid_valid(tsk->loginuid))
Al Virobfef93a2008-01-10 04:53:18 -05002567 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002568 else
David Howellsc69e8d92008-11-14 10:39:19 +11002569 audit_sig_uid = uid;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002570 security_task_getsecid(tsk, &audit_sig_sid);
Al Viro175fc482007-08-08 00:01:46 +01002571 }
2572 if (!audit_signals || audit_dummy_context())
2573 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002574 }
Amy Griffise54dc242007-03-29 18:01:04 -04002575
Amy Griffise54dc242007-03-29 18:01:04 -04002576 /* optimize the common case by putting first signal recipient directly
2577 * in audit_context */
2578 if (!ctx->target_pid) {
2579 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002580 ctx->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002581 ctx->target_uid = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002582 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002583 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002584 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002585 return 0;
2586 }
2587
2588 axp = (void *)ctx->aux_pids;
2589 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2590 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2591 if (!axp)
2592 return -ENOMEM;
2593
2594 axp->d.type = AUDIT_OBJ_PID;
2595 axp->d.next = ctx->aux_pids;
2596 ctx->aux_pids = (void *)axp;
2597 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002598 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002599
2600 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002601 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002602 axp->target_uid[axp->pid_count] = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002603 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002604 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002605 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002606 axp->pid_count++;
2607
2608 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002609}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002610
2611/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002612 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
David Howellsd84f4f92008-11-14 10:39:23 +11002613 * @bprm: pointer to the bprm being processed
2614 * @new: the proposed new credentials
2615 * @old: the old credentials
Eric Paris3fc689e2008-11-11 21:48:18 +11002616 *
2617 * Simply check if the proc already has the caps given by the file and if not
2618 * store the priv escalation info for later auditing at the end of the syscall
2619 *
Eric Paris3fc689e2008-11-11 21:48:18 +11002620 * -Eric
2621 */
David Howellsd84f4f92008-11-14 10:39:23 +11002622int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2623 const struct cred *new, const struct cred *old)
Eric Paris3fc689e2008-11-11 21:48:18 +11002624{
2625 struct audit_aux_data_bprm_fcaps *ax;
2626 struct audit_context *context = current->audit_context;
2627 struct cpu_vfs_cap_data vcaps;
2628 struct dentry *dentry;
2629
2630 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2631 if (!ax)
David Howellsd84f4f92008-11-14 10:39:23 +11002632 return -ENOMEM;
Eric Paris3fc689e2008-11-11 21:48:18 +11002633
2634 ax->d.type = AUDIT_BPRM_FCAPS;
2635 ax->d.next = context->aux;
2636 context->aux = (void *)ax;
2637
2638 dentry = dget(bprm->file->f_dentry);
2639 get_vfs_caps_from_disk(dentry, &vcaps);
2640 dput(dentry);
2641
2642 ax->fcap.permitted = vcaps.permitted;
2643 ax->fcap.inheritable = vcaps.inheritable;
2644 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2645 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2646
David Howellsd84f4f92008-11-14 10:39:23 +11002647 ax->old_pcap.permitted = old->cap_permitted;
2648 ax->old_pcap.inheritable = old->cap_inheritable;
2649 ax->old_pcap.effective = old->cap_effective;
Eric Paris3fc689e2008-11-11 21:48:18 +11002650
David Howellsd84f4f92008-11-14 10:39:23 +11002651 ax->new_pcap.permitted = new->cap_permitted;
2652 ax->new_pcap.inheritable = new->cap_inheritable;
2653 ax->new_pcap.effective = new->cap_effective;
2654 return 0;
Eric Paris3fc689e2008-11-11 21:48:18 +11002655}
2656
2657/**
Eric Parise68b75a02008-11-11 21:48:22 +11002658 * __audit_log_capset - store information about the arguments to the capset syscall
David Howellsd84f4f92008-11-14 10:39:23 +11002659 * @pid: target pid of the capset call
2660 * @new: the new credentials
2661 * @old: the old (current) credentials
Eric Parise68b75a02008-11-11 21:48:22 +11002662 *
2663 * Record the aguments userspace sent to sys_capset for later printing by the
2664 * audit system if applicable
2665 */
Al Viro57f71a02009-01-04 14:52:57 -05002666void __audit_log_capset(pid_t pid,
David Howellsd84f4f92008-11-14 10:39:23 +11002667 const struct cred *new, const struct cred *old)
Eric Parise68b75a02008-11-11 21:48:22 +11002668{
Eric Parise68b75a02008-11-11 21:48:22 +11002669 struct audit_context *context = current->audit_context;
Al Viro57f71a02009-01-04 14:52:57 -05002670 context->capset.pid = pid;
2671 context->capset.cap.effective = new->cap_effective;
2672 context->capset.cap.inheritable = new->cap_effective;
2673 context->capset.cap.permitted = new->cap_permitted;
2674 context->type = AUDIT_CAPSET;
Eric Parise68b75a02008-11-11 21:48:22 +11002675}
2676
Al Viro120a7952010-10-30 02:54:44 -04002677void __audit_mmap_fd(int fd, int flags)
2678{
2679 struct audit_context *context = current->audit_context;
2680 context->mmap.fd = fd;
2681 context->mmap.flags = flags;
2682 context->type = AUDIT_MMAP;
2683}
2684
Eric Paris85e7bac2012-01-03 14:23:05 -05002685static void audit_log_abend(struct audit_buffer *ab, char *reason, long signr)
2686{
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002687 kuid_t auid, uid;
2688 kgid_t gid;
Eric Paris85e7bac2012-01-03 14:23:05 -05002689 unsigned int sessionid;
2690
2691 auid = audit_get_loginuid(current);
2692 sessionid = audit_get_sessionid(current);
2693 current_uid_gid(&uid, &gid);
2694
2695 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
Eric W. Biedermancca080d2012-02-07 16:53:48 -08002696 from_kuid(&init_user_ns, auid),
2697 from_kuid(&init_user_ns, uid),
2698 from_kgid(&init_user_ns, gid),
2699 sessionid);
Eric Paris85e7bac2012-01-03 14:23:05 -05002700 audit_log_task_context(ab);
2701 audit_log_format(ab, " pid=%d comm=", current->pid);
2702 audit_log_untrustedstring(ab, current->comm);
2703 audit_log_format(ab, " reason=");
2704 audit_log_string(ab, reason);
2705 audit_log_format(ab, " sig=%ld", signr);
2706}
Eric Parise68b75a02008-11-11 21:48:22 +11002707/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002708 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002709 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002710 *
2711 * If a process ends with a core dump, something fishy is going on and we
2712 * should record the event for investigation.
2713 */
2714void audit_core_dumps(long signr)
2715{
2716 struct audit_buffer *ab;
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002717
2718 if (!audit_enabled)
2719 return;
2720
2721 if (signr == SIGQUIT) /* don't care for those */
2722 return;
2723
2724 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
Eric Paris85e7bac2012-01-03 14:23:05 -05002725 audit_log_abend(ab, "memory violation", signr);
2726 audit_log_end(ab);
2727}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002728
Kees Cook3dc1c1b2012-04-12 16:47:58 -05002729void __audit_seccomp(unsigned long syscall, long signr, int code)
Eric Paris85e7bac2012-01-03 14:23:05 -05002730{
2731 struct audit_buffer *ab;
2732
2733 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
Kees Cook3dc1c1b2012-04-12 16:47:58 -05002734 audit_log_abend(ab, "seccomp", signr);
Eric Paris85e7bac2012-01-03 14:23:05 -05002735 audit_log_format(ab, " syscall=%ld", syscall);
Kees Cook3dc1c1b2012-04-12 16:47:58 -05002736 audit_log_format(ab, " compat=%d", is_compat_task());
2737 audit_log_format(ab, " ip=0x%lx", KSTK_EIP(current));
2738 audit_log_format(ab, " code=0x%x", code);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002739 audit_log_end(ab);
2740}
Al Viro916d7572009-06-24 00:02:38 -04002741
2742struct list_head *audit_killed_trees(void)
2743{
2744 struct audit_context *ctx = current->audit_context;
2745 if (likely(!ctx || !ctx->in_syscall))
2746 return NULL;
2747 return &ctx->killed_trees;
2748}