blob: 1a92d61ddd276587b63262a780a16872ade664cd [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 Sharma60063492011-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 Viroa1f8e7f2006-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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
David Woodhousefe7752b2005-12-15 18:33:52 +000071#include "audit.h"
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* AUDIT_NAMES is the number of slots we reserve in the audit_context
Eric Paris5195d8e2012-01-03 14:23:05 -050074 * for saving names from getname(). If we get more names we will allocate
75 * a name dynamically and also add those to the list anchored by names_list. */
76#define AUDIT_NAMES 5
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Amy Griffis9c937dc2006-06-08 23:19:31 -040078/* Indicates that audit should log the full pathname. */
79#define AUDIT_NAME_FULL -1
80
Eric Parisde6bbd12008-01-07 14:31:58 -050081/* no execve audit message should be longer than this (userspace limits) */
82#define MAX_EXECVE_AUDIT_LEN 7500
83
Al Viro471a5c72006-07-10 08:29:24 -040084/* number of audit rules */
85int audit_n_rules;
86
Amy Griffise54dc242007-03-29 18:01:04 -040087/* determines whether we collect data for signals sent */
88int audit_signals;
89
Eric Paris851f7ff2008-11-11 21:48:14 +110090struct audit_cap_data {
91 kernel_cap_t permitted;
92 kernel_cap_t inheritable;
93 union {
94 unsigned int fE; /* effective bit of a file capability */
95 kernel_cap_t effective; /* effective set of a process */
96 };
97};
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* When fs/namei.c:getname() is called, we store the pointer in name and
100 * we don't let putname() free it (instead we free all of the saved
101 * pointers at syscall exit time).
102 *
103 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
104struct audit_names {
Eric Paris5195d8e2012-01-03 14:23:05 -0500105 struct list_head list; /* audit_context->names_list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 const char *name;
107 unsigned long ino;
108 dev_t dev;
109 umode_t mode;
110 uid_t uid;
111 gid_t gid;
112 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400113 u32 osid;
Eric Paris851f7ff2008-11-11 21:48:14 +1100114 struct audit_cap_data fcap;
115 unsigned int fcap_ver;
Eric Paris5195d8e2012-01-03 14:23:05 -0500116 int name_len; /* number of name's characters to log */
117 bool name_put; /* call __putname() for this name */
118 /*
119 * This was an allocated audit_names and not from the array of
120 * names allocated in the task audit context. Thus this name
121 * should be freed on syscall exit
122 */
123 bool should_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126struct audit_aux_data {
127 struct audit_aux_data *next;
128 int type;
129};
130
131#define AUDIT_AUX_IPCPERM 0
132
Amy Griffise54dc242007-03-29 18:01:04 -0400133/* Number of target pids per aux struct. */
134#define AUDIT_AUX_PIDS 16
135
Al Viro473ae302006-04-26 14:04:08 -0400136struct audit_aux_data_execve {
137 struct audit_aux_data d;
138 int argc;
139 int envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700140 struct mm_struct *mm;
Al Viro473ae302006-04-26 14:04:08 -0400141};
142
Amy Griffise54dc242007-03-29 18:01:04 -0400143struct audit_aux_data_pids {
144 struct audit_aux_data d;
145 pid_t target_pid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500146 uid_t target_auid[AUDIT_AUX_PIDS];
147 uid_t target_uid[AUDIT_AUX_PIDS];
Eric Paris4746ec52008-01-08 10:06:53 -0500148 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400149 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500150 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400151 int pid_count;
152};
153
Eric Paris3fc689e2008-11-11 21:48:18 +1100154struct audit_aux_data_bprm_fcaps {
155 struct audit_aux_data d;
156 struct audit_cap_data fcap;
157 unsigned int fcap_ver;
158 struct audit_cap_data old_pcap;
159 struct audit_cap_data new_pcap;
160};
161
Eric Parise68b75a02008-11-11 21:48:22 +1100162struct audit_aux_data_capset {
163 struct audit_aux_data d;
164 pid_t pid;
165 struct audit_cap_data cap;
166};
167
Al Viro74c3cbe2007-07-22 08:04:18 -0400168struct audit_tree_refs {
169 struct audit_tree_refs *next;
170 struct audit_chunk *c[31];
171};
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* The per-task audit context. */
174struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400175 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int in_syscall; /* 1 if task is in a syscall */
Al Viro0590b932008-12-14 23:45:27 -0500177 enum audit_state state, current_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned int serial; /* serial number for record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int major; /* syscall number */
Eric Paris44e51a12009-08-07 16:54:29 -0400180 struct timespec ctime; /* time of syscall entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned long argv[4]; /* syscall arguments */
2fd6f582005-04-29 16:08:28 +0100182 long return_code;/* syscall return code */
Al Viro0590b932008-12-14 23:45:27 -0500183 u64 prio;
Eric Paris44e51a12009-08-07 16:54:29 -0400184 int return_valid; /* return code is valid */
Eric Paris5195d8e2012-01-03 14:23:05 -0500185 /*
186 * The names_list is the list of all audit_names collected during this
187 * syscall. The first AUDIT_NAMES entries in the names_list will
188 * actually be from the preallocated_names array for performance
189 * reasons. Except during allocation they should never be referenced
190 * through the preallocated_names array and should only be found/used
191 * by running the names_list.
192 */
193 struct audit_names preallocated_names[AUDIT_NAMES];
194 int name_count; /* total records in names_list */
195 struct list_head names_list; /* anchor for struct audit_names->list */
Amy Griffis5adc8a62006-06-14 18:45:21 -0400196 char * filterkey; /* key for rule that triggered record */
Jan Blunck44707fd2008-02-14 19:38:33 -0800197 struct path pwd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct audit_context *previous; /* For nested syscalls */
199 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400200 struct audit_aux_data *aux_pids;
Al Viro4f6b4342008-12-09 19:50:34 -0500201 struct sockaddr_storage *sockaddr;
202 size_t sockaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400204 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 uid_t uid, euid, suid, fsuid;
206 gid_t gid, egid, sgid, fsgid;
207 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100208 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Al Viroa5cb0132007-03-20 13:58:35 -0400210 pid_t target_pid;
Eric Parisc2a77802008-01-07 13:40:17 -0500211 uid_t target_auid;
212 uid_t target_uid;
Eric Paris4746ec52008-01-08 10:06:53 -0500213 unsigned int target_sessionid;
Al Viroa5cb0132007-03-20 13:58:35 -0400214 u32 target_sid;
Eric Parisc2a77802008-01-07 13:40:17 -0500215 char target_comm[TASK_COMM_LEN];
Al Viroa5cb0132007-03-20 13:58:35 -0400216
Al Viro74c3cbe2007-07-22 08:04:18 -0400217 struct audit_tree_refs *trees, *first_trees;
Al Viro916d7572009-06-24 00:02:38 -0400218 struct list_head killed_trees;
Eric Paris44e51a12009-08-07 16:54:29 -0400219 int tree_count;
Al Viro74c3cbe2007-07-22 08:04:18 -0400220
Al Virof3298dc2008-12-10 03:16:51 -0500221 int type;
222 union {
223 struct {
224 int nargs;
225 long args[6];
226 } socketcall;
Al Viroa33e6752008-12-10 03:40:06 -0500227 struct {
228 uid_t uid;
229 gid_t gid;
Al Viro2570ebb2011-07-27 14:03:22 -0400230 umode_t mode;
Al Viroa33e6752008-12-10 03:40:06 -0500231 u32 osid;
Al Viroe816f372008-12-10 03:47:15 -0500232 int has_perm;
233 uid_t perm_uid;
234 gid_t perm_gid;
Al Viro2570ebb2011-07-27 14:03:22 -0400235 umode_t perm_mode;
Al Viroe816f372008-12-10 03:47:15 -0500236 unsigned long qbytes;
Al Viroa33e6752008-12-10 03:40:06 -0500237 } ipc;
Al Viro73929062008-12-10 06:58:59 -0500238 struct {
239 mqd_t mqdes;
240 struct mq_attr mqstat;
241 } mq_getsetattr;
Al Viro20114f72008-12-10 07:16:12 -0500242 struct {
243 mqd_t mqdes;
244 int sigev_signo;
245 } mq_notify;
Al Viroc32c8af2008-12-14 03:46:48 -0500246 struct {
247 mqd_t mqdes;
248 size_t msg_len;
249 unsigned int msg_prio;
250 struct timespec abs_timeout;
251 } mq_sendrecv;
Al Viro564f6992008-12-14 04:02:26 -0500252 struct {
253 int oflag;
Al Virodf0a4282011-07-26 05:26:10 -0400254 umode_t mode;
Al Viro564f6992008-12-14 04:02:26 -0500255 struct mq_attr attr;
256 } mq_open;
Al Viro57f71a02009-01-04 14:52:57 -0500257 struct {
258 pid_t pid;
259 struct audit_cap_data cap;
260 } capset;
Al Viro120a7952010-10-30 02:54:44 -0400261 struct {
262 int fd;
263 int flags;
264 } mmap;
Al Virof3298dc2008-12-10 03:16:51 -0500265 };
Al Viro157cf642008-12-14 04:57:47 -0500266 int fds[2];
Al Virof3298dc2008-12-10 03:16:51 -0500267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268#if AUDIT_DEBUG
269 int put_count;
270 int ino_count;
271#endif
272};
273
Al Viro55669bf2006-08-31 19:26:40 -0400274static inline int open_arg(int flags, int mask)
275{
276 int n = ACC_MODE(flags);
277 if (flags & (O_TRUNC | O_CREAT))
278 n |= AUDIT_PERM_WRITE;
279 return n & mask;
280}
281
282static int audit_match_perm(struct audit_context *ctx, int mask)
283{
Cordeliac4bacef2008-08-18 09:45:51 -0700284 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800285 if (unlikely(!ctx))
286 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700287 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100288
Al Viro55669bf2006-08-31 19:26:40 -0400289 switch (audit_classify_syscall(ctx->arch, n)) {
290 case 0: /* native */
291 if ((mask & AUDIT_PERM_WRITE) &&
292 audit_match_class(AUDIT_CLASS_WRITE, n))
293 return 1;
294 if ((mask & AUDIT_PERM_READ) &&
295 audit_match_class(AUDIT_CLASS_READ, n))
296 return 1;
297 if ((mask & AUDIT_PERM_ATTR) &&
298 audit_match_class(AUDIT_CLASS_CHATTR, n))
299 return 1;
300 return 0;
301 case 1: /* 32bit on biarch */
302 if ((mask & AUDIT_PERM_WRITE) &&
303 audit_match_class(AUDIT_CLASS_WRITE_32, n))
304 return 1;
305 if ((mask & AUDIT_PERM_READ) &&
306 audit_match_class(AUDIT_CLASS_READ_32, n))
307 return 1;
308 if ((mask & AUDIT_PERM_ATTR) &&
309 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
310 return 1;
311 return 0;
312 case 2: /* open */
313 return mask & ACC_MODE(ctx->argv[1]);
314 case 3: /* openat */
315 return mask & ACC_MODE(ctx->argv[2]);
316 case 4: /* socketcall */
317 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
318 case 5: /* execve */
319 return mask & AUDIT_PERM_EXEC;
320 default:
321 return 0;
322 }
323}
324
Eric Paris5ef30ee2012-01-03 14:23:05 -0500325static int audit_match_filetype(struct audit_context *ctx, int val)
Al Viro8b67dca2008-04-28 04:15:49 -0400326{
Eric Paris5195d8e2012-01-03 14:23:05 -0500327 struct audit_names *n;
Eric Paris5ef30ee2012-01-03 14:23:05 -0500328 umode_t mode = (umode_t)val;
zhangxiliang1a61c882008-08-02 10:56:37 +0800329
330 if (unlikely(!ctx))
331 return 0;
332
Eric Paris5195d8e2012-01-03 14:23:05 -0500333 list_for_each_entry(n, &ctx->names_list, list) {
334 if ((n->ino != -1) &&
335 ((n->mode & S_IFMT) == mode))
Eric Paris5ef30ee2012-01-03 14:23:05 -0500336 return 1;
337 }
Eric Paris5195d8e2012-01-03 14:23:05 -0500338
Eric Paris5ef30ee2012-01-03 14:23:05 -0500339 return 0;
Al Viro8b67dca2008-04-28 04:15:49 -0400340}
341
Al Viro74c3cbe2007-07-22 08:04:18 -0400342/*
343 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
344 * ->first_trees points to its beginning, ->trees - to the current end of data.
345 * ->tree_count is the number of free entries in array pointed to by ->trees.
346 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
347 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
348 * it's going to remain 1-element for almost any setup) until we free context itself.
349 * References in it _are_ dropped - at the same time we free/drop aux stuff.
350 */
351
352#ifdef CONFIG_AUDIT_TREE
Eric Paris679173b2009-01-26 18:09:45 -0500353static void audit_set_auditable(struct audit_context *ctx)
354{
355 if (!ctx->prio) {
356 ctx->prio = 1;
357 ctx->current_state = AUDIT_RECORD_CONTEXT;
358 }
359}
360
Al Viro74c3cbe2007-07-22 08:04:18 -0400361static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
362{
363 struct audit_tree_refs *p = ctx->trees;
364 int left = ctx->tree_count;
365 if (likely(left)) {
366 p->c[--left] = chunk;
367 ctx->tree_count = left;
368 return 1;
369 }
370 if (!p)
371 return 0;
372 p = p->next;
373 if (p) {
374 p->c[30] = chunk;
375 ctx->trees = p;
376 ctx->tree_count = 30;
377 return 1;
378 }
379 return 0;
380}
381
382static int grow_tree_refs(struct audit_context *ctx)
383{
384 struct audit_tree_refs *p = ctx->trees;
385 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
386 if (!ctx->trees) {
387 ctx->trees = p;
388 return 0;
389 }
390 if (p)
391 p->next = ctx->trees;
392 else
393 ctx->first_trees = ctx->trees;
394 ctx->tree_count = 31;
395 return 1;
396}
397#endif
398
399static void unroll_tree_refs(struct audit_context *ctx,
400 struct audit_tree_refs *p, int count)
401{
402#ifdef CONFIG_AUDIT_TREE
403 struct audit_tree_refs *q;
404 int n;
405 if (!p) {
406 /* we started with empty chain */
407 p = ctx->first_trees;
408 count = 31;
409 /* if the very first allocation has failed, nothing to do */
410 if (!p)
411 return;
412 }
413 n = count;
414 for (q = p; q != ctx->trees; q = q->next, n = 31) {
415 while (n--) {
416 audit_put_chunk(q->c[n]);
417 q->c[n] = NULL;
418 }
419 }
420 while (n-- > ctx->tree_count) {
421 audit_put_chunk(q->c[n]);
422 q->c[n] = NULL;
423 }
424 ctx->trees = p;
425 ctx->tree_count = count;
426#endif
427}
428
429static void free_tree_refs(struct audit_context *ctx)
430{
431 struct audit_tree_refs *p, *q;
432 for (p = ctx->first_trees; p; p = q) {
433 q = p->next;
434 kfree(p);
435 }
436}
437
438static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
439{
440#ifdef CONFIG_AUDIT_TREE
441 struct audit_tree_refs *p;
442 int n;
443 if (!tree)
444 return 0;
445 /* full ones */
446 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
447 for (n = 0; n < 31; n++)
448 if (audit_tree_match(p->c[n], tree))
449 return 1;
450 }
451 /* partial */
452 if (p) {
453 for (n = ctx->tree_count; n < 31; n++)
454 if (audit_tree_match(p->c[n], tree))
455 return 1;
456 }
457#endif
458 return 0;
459}
460
Amy Griffisf368c07d2006-04-07 16:55:56 -0400461/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/* Compare a task_struct with an audit_rule. Return 1 on match, 0
Tony Jonesf5629882011-04-27 15:10:49 +0200463 * otherwise.
464 *
465 * If task_creation is true, this is an explicit indication that we are
466 * filtering a task rule at task creation time. This and tsk == current are
467 * the only situations where tsk->cred may be accessed without an rcu read lock.
468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500470 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400472 struct audit_names *name,
Tony Jonesf5629882011-04-27 15:10:49 +0200473 enum audit_state *state,
474 bool task_creation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Tony Jonesf5629882011-04-27 15:10:49 +0200476 const struct cred *cred;
Eric Paris5195d8e2012-01-03 14:23:05 -0500477 int i, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600478 u32 sid;
479
Tony Jonesf5629882011-04-27 15:10:49 +0200480 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500483 struct audit_field *f = &rule->fields[i];
Eric Paris5195d8e2012-01-03 14:23:05 -0500484 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int result = 0;
486
Amy Griffis93315ed2006-02-07 12:05:27 -0500487 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500489 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
Al Viro3c662512006-05-06 08:26:27 -0400491 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400492 if (ctx) {
493 if (!ctx->ppid)
494 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400495 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400496 }
Al Viro3c662512006-05-06 08:26:27 -0400497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 case AUDIT_UID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100499 result = audit_comparator(cred->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 break;
501 case AUDIT_EUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100502 result = audit_comparator(cred->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 case AUDIT_SUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100505 result = audit_comparator(cred->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507 case AUDIT_FSUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100508 result = audit_comparator(cred->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
510 case AUDIT_GID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100511 result = audit_comparator(cred->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
513 case AUDIT_EGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100514 result = audit_comparator(cred->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516 case AUDIT_SGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100517 result = audit_comparator(cred->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
519 case AUDIT_FSGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100520 result = audit_comparator(cred->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
522 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500523 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
2fd6f582005-04-29 16:08:28 +0100525 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700526 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500527 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 case AUDIT_EXIT:
531 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500532 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100535 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500536 if (f->val)
537 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100538 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500539 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400543 if (name)
544 result = audit_comparator(MAJOR(name->dev),
545 f->op, f->val);
546 else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500547 list_for_each_entry(n, &ctx->names_list, list) {
548 if (audit_comparator(MAJOR(n->dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 ++result;
550 break;
551 }
552 }
553 }
554 break;
555 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400556 if (name)
557 result = audit_comparator(MINOR(name->dev),
558 f->op, f->val);
559 else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500560 list_for_each_entry(n, &ctx->names_list, list) {
561 if (audit_comparator(MINOR(n->dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 ++result;
563 break;
564 }
565 }
566 }
567 break;
568 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400569 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400570 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400571 else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500572 list_for_each_entry(n, &ctx->names_list, list) {
573 if (audit_comparator(n->ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ++result;
575 break;
576 }
577 }
578 }
579 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400580 case AUDIT_WATCH:
Eric Parisae7b8f42009-12-17 20:12:04 -0500581 if (name)
582 result = audit_watch_compare(rule->watch, name->ino, name->dev);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400583 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400584 case AUDIT_DIR:
585 if (ctx)
586 result = match_tree_refs(ctx, rule->tree);
587 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 case AUDIT_LOGINUID:
589 result = 0;
590 if (ctx)
Al Virobfef93a2008-01-10 04:53:18 -0500591 result = audit_comparator(tsk->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500593 case AUDIT_SUBJ_USER:
594 case AUDIT_SUBJ_ROLE:
595 case AUDIT_SUBJ_TYPE:
596 case AUDIT_SUBJ_SEN:
597 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600598 /* NOTE: this may return negative values indicating
599 a temporary error. We simply treat this as a
600 match for now to avoid losing information that
601 may be wanted. An error message will also be
602 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000603 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400604 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200605 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400606 need_sid = 0;
607 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200608 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600609 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000610 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600611 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400612 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600613 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500614 case AUDIT_OBJ_USER:
615 case AUDIT_OBJ_ROLE:
616 case AUDIT_OBJ_TYPE:
617 case AUDIT_OBJ_LEV_LOW:
618 case AUDIT_OBJ_LEV_HIGH:
619 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
620 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000621 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500622 /* Find files that match */
623 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200624 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500625 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000626 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500627 } else if (ctx) {
Eric Paris5195d8e2012-01-03 14:23:05 -0500628 list_for_each_entry(n, &ctx->names_list, list) {
629 if (security_audit_rule_match(n->osid, f->type,
630 f->op, f->lsm_rule,
631 ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500632 ++result;
633 break;
634 }
635 }
636 }
637 /* Find ipc objects that match */
Al Viroa33e6752008-12-10 03:40:06 -0500638 if (!ctx || ctx->type != AUDIT_IPC)
639 break;
640 if (security_audit_rule_match(ctx->ipc.osid,
641 f->type, f->op,
642 f->lsm_rule, ctx))
643 ++result;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500644 }
645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 case AUDIT_ARG0:
647 case AUDIT_ARG1:
648 case AUDIT_ARG2:
649 case AUDIT_ARG3:
650 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500651 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400653 case AUDIT_FILTERKEY:
654 /* ignore this field for filtering */
655 result = 1;
656 break;
Al Viro55669bf2006-08-31 19:26:40 -0400657 case AUDIT_PERM:
658 result = audit_match_perm(ctx, f->val);
659 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400660 case AUDIT_FILETYPE:
661 result = audit_match_filetype(ctx, f->val);
662 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Tony Jonesf5629882011-04-27 15:10:49 +0200665 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667 }
Al Viro0590b932008-12-14 23:45:27 -0500668
669 if (ctx) {
670 if (rule->prio <= ctx->prio)
671 return 0;
672 if (rule->filterkey) {
673 kfree(ctx->filterkey);
674 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
675 }
676 ctx->prio = rule->prio;
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 switch (rule->action) {
679 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
681 }
682 return 1;
683}
684
685/* At process creation time, we can determine if system-call auditing is
686 * completely disabled for this task. Since we only have the task
687 * structure at this point, we can only check uid and gid.
688 */
Al Viroe048e022008-12-16 03:51:22 -0500689static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 struct audit_entry *e;
692 enum audit_state state;
693
694 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100695 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Tony Jonesf5629882011-04-27 15:10:49 +0200696 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
697 &state, true)) {
Al Viroe048e022008-12-16 03:51:22 -0500698 if (state == AUDIT_RECORD_CONTEXT)
699 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 rcu_read_unlock();
701 return state;
702 }
703 }
704 rcu_read_unlock();
705 return AUDIT_BUILD_CONTEXT;
706}
707
708/* At syscall entry and exit time, this filter is called if the
709 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100710 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700711 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
713static enum audit_state audit_filter_syscall(struct task_struct *tsk,
714 struct audit_context *ctx,
715 struct list_head *list)
716{
717 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100718 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
David Woodhouse351bb722005-07-14 14:40:06 +0100720 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100721 return AUDIT_DISABLED;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100724 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000725 int word = AUDIT_WORD(ctx->major);
726 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100727
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000728 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400729 if ((e->rule.mask[word] & bit) == bit &&
730 audit_filter_rules(tsk, &e->rule, ctx, NULL,
Tony Jonesf5629882011-04-27 15:10:49 +0200731 &state, false)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000732 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500733 ctx->current_state = state;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000734 return state;
735 }
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 rcu_read_unlock();
739 return AUDIT_BUILD_CONTEXT;
740}
741
Eric Paris5195d8e2012-01-03 14:23:05 -0500742/*
743 * Given an audit_name check the inode hash table to see if they match.
744 * Called holding the rcu read lock to protect the use of audit_inode_hash
745 */
746static int audit_filter_inode_name(struct task_struct *tsk,
747 struct audit_names *n,
748 struct audit_context *ctx) {
749 int word, bit;
750 int h = audit_hash_ino((u32)n->ino);
751 struct list_head *list = &audit_inode_hash[h];
752 struct audit_entry *e;
753 enum audit_state state;
754
755 word = AUDIT_WORD(ctx->major);
756 bit = AUDIT_BIT(ctx->major);
757
758 if (list_empty(list))
759 return 0;
760
761 list_for_each_entry_rcu(e, list, list) {
762 if ((e->rule.mask[word] & bit) == bit &&
763 audit_filter_rules(tsk, &e->rule, ctx, n, &state, false)) {
764 ctx->current_state = state;
765 return 1;
766 }
767 }
768
769 return 0;
770}
771
772/* At syscall exit time, this filter is called if any audit_names have been
Amy Griffisf368c07d2006-04-07 16:55:56 -0400773 * collected during syscall processing. We only check rules in sublists at hash
Eric Paris5195d8e2012-01-03 14:23:05 -0500774 * buckets applicable to the inode numbers in audit_names.
Amy Griffisf368c07d2006-04-07 16:55:56 -0400775 * Regarding audit_state, same rules apply as for audit_filter_syscall().
776 */
Al Viro0590b932008-12-14 23:45:27 -0500777void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400778{
Eric Paris5195d8e2012-01-03 14:23:05 -0500779 struct audit_names *n;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400780
781 if (audit_pid && tsk->tgid == audit_pid)
Al Viro0590b932008-12-14 23:45:27 -0500782 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400783
784 rcu_read_lock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400785
Eric Paris5195d8e2012-01-03 14:23:05 -0500786 list_for_each_entry(n, &ctx->names_list, list) {
787 if (audit_filter_inode_name(tsk, n, ctx))
788 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400789 }
790 rcu_read_unlock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400791}
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793static inline struct audit_context *audit_get_context(struct task_struct *tsk,
794 int return_valid,
Paul Moore6d208da2009-04-01 15:47:27 -0400795 long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct audit_context *context = tsk->audit_context;
798
799 if (likely(!context))
800 return NULL;
801 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500802
803 /*
804 * we need to fix up the return code in the audit logs if the actual
805 * return codes are later going to be fixed up by the arch specific
806 * signal handlers
807 *
808 * This is actually a test for:
809 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
810 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
811 *
812 * but is faster than a bunch of ||
813 */
814 if (unlikely(return_code <= -ERESTARTSYS) &&
815 (return_code >= -ERESTART_RESTARTBLOCK) &&
816 (return_code != -ENOIOCTLCMD))
817 context->return_code = -EINTR;
818 else
819 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Al Viro0590b932008-12-14 23:45:27 -0500821 if (context->in_syscall && !context->dummy) {
822 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
823 audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 tsk->audit_context = NULL;
827 return context;
828}
829
830static inline void audit_free_names(struct audit_context *context)
831{
Eric Paris5195d8e2012-01-03 14:23:05 -0500832 struct audit_names *n, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834#if AUDIT_DEBUG == 2
Al Viro0590b932008-12-14 23:45:27 -0500835 if (context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000836 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 " name_count=%d put_count=%d"
838 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000839 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 context->serial, context->major, context->in_syscall,
841 context->name_count, context->put_count,
842 context->ino_count);
Eric Paris5195d8e2012-01-03 14:23:05 -0500843 list_for_each_entry(n, &context->names_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 printk(KERN_ERR "names[%d] = %p = %s\n", i,
Eric Paris5195d8e2012-01-03 14:23:05 -0500845 n->name, n->name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 dump_stack();
848 return;
849 }
850#endif
851#if AUDIT_DEBUG
852 context->put_count = 0;
853 context->ino_count = 0;
854#endif
855
Eric Paris5195d8e2012-01-03 14:23:05 -0500856 list_for_each_entry_safe(n, next, &context->names_list, list) {
857 list_del(&n->list);
858 if (n->name && n->name_put)
859 __putname(n->name);
860 if (n->should_free)
861 kfree(n);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -0800864 path_put(&context->pwd);
865 context->pwd.dentry = NULL;
866 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869static inline void audit_free_aux(struct audit_context *context)
870{
871 struct audit_aux_data *aux;
872
873 while ((aux = context->aux)) {
874 context->aux = aux->next;
875 kfree(aux);
876 }
Amy Griffise54dc242007-03-29 18:01:04 -0400877 while ((aux = context->aux_pids)) {
878 context->aux_pids = aux->next;
879 kfree(aux);
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883static inline void audit_zero_context(struct audit_context *context,
884 enum audit_state state)
885{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 memset(context, 0, sizeof(*context));
887 context->state = state;
Al Viro0590b932008-12-14 23:45:27 -0500888 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891static inline struct audit_context *audit_alloc_context(enum audit_state state)
892{
893 struct audit_context *context;
894
895 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
896 return NULL;
897 audit_zero_context(context, state);
Al Viro916d7572009-06-24 00:02:38 -0400898 INIT_LIST_HEAD(&context->killed_trees);
Eric Paris5195d8e2012-01-03 14:23:05 -0500899 INIT_LIST_HEAD(&context->names_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return context;
901}
902
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700903/**
904 * audit_alloc - allocate an audit context block for a task
905 * @tsk: task
906 *
907 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * if necessary. Doing so turns on system call auditing for the
909 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700910 * needed.
911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912int audit_alloc(struct task_struct *tsk)
913{
914 struct audit_context *context;
915 enum audit_state state;
Al Viroe048e022008-12-16 03:51:22 -0500916 char *key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Eric Parisb593d382008-01-08 17:38:31 -0500918 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 0; /* Return if not auditing. */
920
Al Viroe048e022008-12-16 03:51:22 -0500921 state = audit_filter_task(tsk, &key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (likely(state == AUDIT_DISABLED))
923 return 0;
924
925 if (!(context = audit_alloc_context(state))) {
Al Viroe048e022008-12-16 03:51:22 -0500926 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 audit_log_lost("out of memory in audit_alloc");
928 return -ENOMEM;
929 }
Al Viroe048e022008-12-16 03:51:22 -0500930 context->filterkey = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 tsk->audit_context = context;
933 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
934 return 0;
935}
936
937static inline void audit_free_context(struct audit_context *context)
938{
939 struct audit_context *previous;
940 int count = 0;
941
942 do {
943 previous = context->previous;
944 if (previous || (count && count < 10)) {
945 ++count;
946 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
947 " freeing multiple contexts (%d)\n",
948 context->serial, context->major,
949 context->name_count, count);
950 }
951 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -0400952 unroll_tree_refs(context, NULL, 0);
953 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400955 kfree(context->filterkey);
Al Viro4f6b4342008-12-09 19:50:34 -0500956 kfree(context->sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 kfree(context);
958 context = previous;
959 } while (context);
960 if (count >= 10)
961 printk(KERN_ERR "audit: freed %d contexts\n", count);
962}
963
Joy Latten161a09e2006-11-27 13:11:54 -0600964void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000965{
966 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000967 unsigned len;
968 int error;
969 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000970
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200971 security_task_getsecid(current, &sid);
Al Viroc4823bc2007-03-12 16:17:42 +0000972 if (!sid)
973 return;
974
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200975 error = security_secid_to_secctx(sid, &ctx, &len);
Al Viroc4823bc2007-03-12 16:17:42 +0000976 if (error) {
977 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000978 goto error_path;
979 return;
980 }
981
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000982 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200983 security_release_secctx(ctx, len);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000984 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000985
986error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000987 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000988 return;
989}
990
Joy Latten161a09e2006-11-27 13:11:54 -0600991EXPORT_SYMBOL(audit_log_task_context);
992
Al Viroe4951492006-03-29 20:17:10 -0500993static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700994{
Al Viro45d9bb02006-03-29 20:02:55 -0500995 char name[sizeof(tsk->comm)];
996 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700997 struct vm_area_struct *vma;
998
Al Viroe4951492006-03-29 20:17:10 -0500999 /* tsk == current */
1000
Al Viro45d9bb02006-03-29 20:02:55 -05001001 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +01001002 audit_log_format(ab, " comm=");
1003 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -07001004
Al Viroe4951492006-03-29 20:17:10 -05001005 if (mm) {
1006 down_read(&mm->mmap_sem);
1007 vma = mm->mmap;
1008 while (vma) {
1009 if ((vma->vm_flags & VM_EXECUTABLE) &&
1010 vma->vm_file) {
1011 audit_log_d_path(ab, "exe=",
Jan Blunck44707fd2008-02-14 19:38:33 -08001012 &vma->vm_file->f_path);
Al Viroe4951492006-03-29 20:17:10 -05001013 break;
1014 }
1015 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -07001016 }
Al Viroe4951492006-03-29 20:17:10 -05001017 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -07001018 }
Al Viroe4951492006-03-29 20:17:10 -05001019 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -07001020}
1021
Amy Griffise54dc242007-03-29 18:01:04 -04001022static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric Paris4746ec52008-01-08 10:06:53 -05001023 uid_t auid, uid_t uid, unsigned int sessionid,
1024 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -04001025{
1026 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001027 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -04001028 u32 len;
1029 int rc = 0;
1030
1031 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
1032 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -05001033 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -04001034
Eric Paris4746ec52008-01-08 10:06:53 -05001035 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
1036 uid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001037 if (security_secid_to_secctx(sid, &ctx, &len)) {
Eric Parisc2a77802008-01-07 13:40:17 -05001038 audit_log_format(ab, " obj=(none)");
Amy Griffise54dc242007-03-29 18:01:04 -04001039 rc = 1;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001040 } else {
1041 audit_log_format(ab, " obj=%s", ctx);
1042 security_release_secctx(ctx, len);
1043 }
Eric Parisc2a77802008-01-07 13:40:17 -05001044 audit_log_format(ab, " ocomm=");
1045 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -04001046 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -04001047
1048 return rc;
1049}
1050
Eric Parisde6bbd12008-01-07 14:31:58 -05001051/*
1052 * to_send and len_sent accounting are very loose estimates. We aren't
1053 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001054 * within about 500 bytes (next page boundary)
Eric Parisde6bbd12008-01-07 14:31:58 -05001055 *
1056 * why snprintf? an int is up to 12 digits long. if we just assumed when
1057 * logging that a[%d]= was going to be 16 characters long we would be wasting
1058 * space in every audit message. In one 7500 byte message we can log up to
1059 * about 1000 min size arguments. That comes down to about 50% waste of space
1060 * if we didn't do the snprintf to find out how long arg_num_len was.
1061 */
1062static int audit_log_single_execve_arg(struct audit_context *context,
1063 struct audit_buffer **ab,
1064 int arg_num,
1065 size_t *len_sent,
1066 const char __user *p,
1067 char *buf)
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001068{
Eric Parisde6bbd12008-01-07 14:31:58 -05001069 char arg_num_len_buf[12];
1070 const char __user *tmp_p = p;
Eric Parisb87ce6e2009-06-11 14:31:34 -04001071 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1072 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
Eric Parisde6bbd12008-01-07 14:31:58 -05001073 size_t len, len_left, to_send;
1074 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1075 unsigned int i, has_cntl = 0, too_long = 0;
1076 int ret;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001077
Eric Parisde6bbd12008-01-07 14:31:58 -05001078 /* strnlen_user includes the null we don't want to send */
1079 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001080
Eric Parisde6bbd12008-01-07 14:31:58 -05001081 /*
1082 * We just created this mm, if we can't find the strings
1083 * we just copied into it something is _very_ wrong. Similar
1084 * for strings that are too long, we should not have created
1085 * any.
1086 */
Eric Parisb0abcfc2008-02-18 18:23:16 -05001087 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001088 WARN_ON(1);
1089 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001090 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001091 }
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001092
Eric Parisde6bbd12008-01-07 14:31:58 -05001093 /* walk the whole argument looking for non-ascii chars */
1094 do {
1095 if (len_left > MAX_EXECVE_AUDIT_LEN)
1096 to_send = MAX_EXECVE_AUDIT_LEN;
1097 else
1098 to_send = len_left;
1099 ret = copy_from_user(buf, tmp_p, to_send);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001100 /*
1101 * There is no reason for this copy to be short. We just
1102 * copied them here, and the mm hasn't been exposed to user-
1103 * space yet.
1104 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001105 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001106 WARN_ON(1);
1107 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001108 return -1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001109 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001110 buf[to_send] = '\0';
1111 has_cntl = audit_string_contains_control(buf, to_send);
1112 if (has_cntl) {
1113 /*
1114 * hex messages get logged as 2 bytes, so we can only
1115 * send half as much in each message
1116 */
1117 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1118 break;
1119 }
1120 len_left -= to_send;
1121 tmp_p += to_send;
1122 } while (len_left > 0);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001123
Eric Parisde6bbd12008-01-07 14:31:58 -05001124 len_left = len;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001125
Eric Parisde6bbd12008-01-07 14:31:58 -05001126 if (len > max_execve_audit_len)
1127 too_long = 1;
1128
1129 /* rewalk the argument actually logging the message */
1130 for (i = 0; len_left > 0; i++) {
1131 int room_left;
1132
1133 if (len_left > max_execve_audit_len)
1134 to_send = max_execve_audit_len;
1135 else
1136 to_send = len_left;
1137
1138 /* do we have space left to send this argument in this ab? */
1139 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1140 if (has_cntl)
1141 room_left -= (to_send * 2);
1142 else
1143 room_left -= to_send;
1144 if (room_left < 0) {
1145 *len_sent = 0;
1146 audit_log_end(*ab);
1147 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1148 if (!*ab)
1149 return 0;
1150 }
1151
1152 /*
1153 * first record needs to say how long the original string was
1154 * so we can be sure nothing was lost.
1155 */
1156 if ((i == 0) && (too_long))
Jiri Pirkoca96a892009-01-09 16:44:16 +01001157 audit_log_format(*ab, " a%d_len=%zu", arg_num,
Eric Parisde6bbd12008-01-07 14:31:58 -05001158 has_cntl ? 2*len : len);
1159
1160 /*
1161 * normally arguments are small enough to fit and we already
1162 * filled buf above when we checked for control characters
1163 * so don't bother with another copy_from_user
1164 */
1165 if (len >= max_execve_audit_len)
1166 ret = copy_from_user(buf, p, to_send);
1167 else
1168 ret = 0;
1169 if (ret) {
1170 WARN_ON(1);
1171 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001172 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001173 }
1174 buf[to_send] = '\0';
1175
1176 /* actually log it */
Jiri Pirkoca96a892009-01-09 16:44:16 +01001177 audit_log_format(*ab, " a%d", arg_num);
Eric Parisde6bbd12008-01-07 14:31:58 -05001178 if (too_long)
1179 audit_log_format(*ab, "[%d]", i);
1180 audit_log_format(*ab, "=");
1181 if (has_cntl)
Eric Parisb556f8a2008-04-18 10:12:59 -04001182 audit_log_n_hex(*ab, buf, to_send);
Eric Parisde6bbd12008-01-07 14:31:58 -05001183 else
Eric Paris9d960982009-06-11 14:31:37 -04001184 audit_log_string(*ab, buf);
Eric Parisde6bbd12008-01-07 14:31:58 -05001185
1186 p += to_send;
1187 len_left -= to_send;
1188 *len_sent += arg_num_len;
1189 if (has_cntl)
1190 *len_sent += to_send * 2;
1191 else
1192 *len_sent += to_send;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001193 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001194 /* include the null we didn't log */
1195 return len + 1;
1196}
1197
1198static void audit_log_execve_info(struct audit_context *context,
1199 struct audit_buffer **ab,
1200 struct audit_aux_data_execve *axi)
1201{
1202 int i;
1203 size_t len, len_sent = 0;
1204 const char __user *p;
1205 char *buf;
1206
1207 if (axi->mm != current->mm)
1208 return; /* execve failed, no additional info */
1209
1210 p = (const char __user *)axi->mm->arg_start;
1211
Jiri Pirkoca96a892009-01-09 16:44:16 +01001212 audit_log_format(*ab, "argc=%d", axi->argc);
Eric Parisde6bbd12008-01-07 14:31:58 -05001213
1214 /*
1215 * we need some kernel buffer to hold the userspace args. Just
1216 * allocate one big one rather than allocating one of the right size
1217 * for every single argument inside audit_log_single_execve_arg()
1218 * should be <8k allocation so should be pretty safe.
1219 */
1220 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1221 if (!buf) {
1222 audit_panic("out of memory for argv string\n");
1223 return;
1224 }
1225
1226 for (i = 0; i < axi->argc; i++) {
1227 len = audit_log_single_execve_arg(context, ab, i,
1228 &len_sent, p, buf);
1229 if (len <= 0)
1230 break;
1231 p += len;
1232 }
1233 kfree(buf);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001234}
1235
Eric Paris851f7ff2008-11-11 21:48:14 +11001236static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1237{
1238 int i;
1239
1240 audit_log_format(ab, " %s=", prefix);
1241 CAP_FOR_EACH_U32(i) {
1242 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1243 }
1244}
1245
1246static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1247{
1248 kernel_cap_t *perm = &name->fcap.permitted;
1249 kernel_cap_t *inh = &name->fcap.inheritable;
1250 int log = 0;
1251
1252 if (!cap_isclear(*perm)) {
1253 audit_log_cap(ab, "cap_fp", perm);
1254 log = 1;
1255 }
1256 if (!cap_isclear(*inh)) {
1257 audit_log_cap(ab, "cap_fi", inh);
1258 log = 1;
1259 }
1260
1261 if (log)
1262 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1263}
1264
Al Viroa33e6752008-12-10 03:40:06 -05001265static void show_special(struct audit_context *context, int *call_panic)
Al Virof3298dc2008-12-10 03:16:51 -05001266{
1267 struct audit_buffer *ab;
1268 int i;
1269
1270 ab = audit_log_start(context, GFP_KERNEL, context->type);
1271 if (!ab)
1272 return;
1273
1274 switch (context->type) {
1275 case AUDIT_SOCKETCALL: {
1276 int nargs = context->socketcall.nargs;
1277 audit_log_format(ab, "nargs=%d", nargs);
1278 for (i = 0; i < nargs; i++)
1279 audit_log_format(ab, " a%d=%lx", i,
1280 context->socketcall.args[i]);
1281 break; }
Al Viroa33e6752008-12-10 03:40:06 -05001282 case AUDIT_IPC: {
1283 u32 osid = context->ipc.osid;
1284
Al Viro2570ebb2011-07-27 14:03:22 -04001285 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
Al Viroa33e6752008-12-10 03:40:06 -05001286 context->ipc.uid, context->ipc.gid, context->ipc.mode);
1287 if (osid) {
1288 char *ctx = NULL;
1289 u32 len;
1290 if (security_secid_to_secctx(osid, &ctx, &len)) {
1291 audit_log_format(ab, " osid=%u", osid);
1292 *call_panic = 1;
1293 } else {
1294 audit_log_format(ab, " obj=%s", ctx);
1295 security_release_secctx(ctx, len);
1296 }
1297 }
Al Viroe816f372008-12-10 03:47:15 -05001298 if (context->ipc.has_perm) {
1299 audit_log_end(ab);
1300 ab = audit_log_start(context, GFP_KERNEL,
1301 AUDIT_IPC_SET_PERM);
1302 audit_log_format(ab,
Al Viro2570ebb2011-07-27 14:03:22 -04001303 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
Al Viroe816f372008-12-10 03:47:15 -05001304 context->ipc.qbytes,
1305 context->ipc.perm_uid,
1306 context->ipc.perm_gid,
1307 context->ipc.perm_mode);
1308 if (!ab)
1309 return;
1310 }
Al Viroa33e6752008-12-10 03:40:06 -05001311 break; }
Al Viro564f6992008-12-14 04:02:26 -05001312 case AUDIT_MQ_OPEN: {
1313 audit_log_format(ab,
Al Virodf0a4282011-07-26 05:26:10 -04001314 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
Al Viro564f6992008-12-14 04:02:26 -05001315 "mq_msgsize=%ld mq_curmsgs=%ld",
1316 context->mq_open.oflag, context->mq_open.mode,
1317 context->mq_open.attr.mq_flags,
1318 context->mq_open.attr.mq_maxmsg,
1319 context->mq_open.attr.mq_msgsize,
1320 context->mq_open.attr.mq_curmsgs);
1321 break; }
Al Viroc32c8af2008-12-14 03:46:48 -05001322 case AUDIT_MQ_SENDRECV: {
1323 audit_log_format(ab,
1324 "mqdes=%d msg_len=%zd msg_prio=%u "
1325 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1326 context->mq_sendrecv.mqdes,
1327 context->mq_sendrecv.msg_len,
1328 context->mq_sendrecv.msg_prio,
1329 context->mq_sendrecv.abs_timeout.tv_sec,
1330 context->mq_sendrecv.abs_timeout.tv_nsec);
1331 break; }
Al Viro20114f72008-12-10 07:16:12 -05001332 case AUDIT_MQ_NOTIFY: {
1333 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1334 context->mq_notify.mqdes,
1335 context->mq_notify.sigev_signo);
1336 break; }
Al Viro73929062008-12-10 06:58:59 -05001337 case AUDIT_MQ_GETSETATTR: {
1338 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1339 audit_log_format(ab,
1340 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1341 "mq_curmsgs=%ld ",
1342 context->mq_getsetattr.mqdes,
1343 attr->mq_flags, attr->mq_maxmsg,
1344 attr->mq_msgsize, attr->mq_curmsgs);
1345 break; }
Al Viro57f71a02009-01-04 14:52:57 -05001346 case AUDIT_CAPSET: {
1347 audit_log_format(ab, "pid=%d", context->capset.pid);
1348 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1349 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1350 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1351 break; }
Al Viro120a7952010-10-30 02:54:44 -04001352 case AUDIT_MMAP: {
1353 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1354 context->mmap.flags);
1355 break; }
Al Virof3298dc2008-12-10 03:16:51 -05001356 }
1357 audit_log_end(ab);
1358}
1359
Eric Paris5195d8e2012-01-03 14:23:05 -05001360static void audit_log_name(struct audit_context *context, struct audit_names *n,
1361 int record_num, int *call_panic)
1362{
1363 struct audit_buffer *ab;
1364 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1365 if (!ab)
1366 return; /* audit_panic has been called */
1367
1368 audit_log_format(ab, "item=%d", record_num);
1369
1370 if (n->name) {
1371 switch (n->name_len) {
1372 case AUDIT_NAME_FULL:
1373 /* log the full path */
1374 audit_log_format(ab, " name=");
1375 audit_log_untrustedstring(ab, n->name);
1376 break;
1377 case 0:
1378 /* name was specified as a relative path and the
1379 * directory component is the cwd */
1380 audit_log_d_path(ab, "name=", &context->pwd);
1381 break;
1382 default:
1383 /* log the name's directory component */
1384 audit_log_format(ab, " name=");
1385 audit_log_n_untrustedstring(ab, n->name,
1386 n->name_len);
1387 }
1388 } else
1389 audit_log_format(ab, " name=(null)");
1390
1391 if (n->ino != (unsigned long)-1) {
1392 audit_log_format(ab, " inode=%lu"
1393 " dev=%02x:%02x mode=%#ho"
1394 " ouid=%u ogid=%u rdev=%02x:%02x",
1395 n->ino,
1396 MAJOR(n->dev),
1397 MINOR(n->dev),
1398 n->mode,
1399 n->uid,
1400 n->gid,
1401 MAJOR(n->rdev),
1402 MINOR(n->rdev));
1403 }
1404 if (n->osid != 0) {
1405 char *ctx = NULL;
1406 u32 len;
1407 if (security_secid_to_secctx(
1408 n->osid, &ctx, &len)) {
1409 audit_log_format(ab, " osid=%u", n->osid);
1410 *call_panic = 2;
1411 } else {
1412 audit_log_format(ab, " obj=%s", ctx);
1413 security_release_secctx(ctx, len);
1414 }
1415 }
1416
1417 audit_log_fcaps(ab, n);
1418
1419 audit_log_end(ab);
1420}
1421
Al Viroe4951492006-03-29 20:17:10 -05001422static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
David Howellsc69e8d92008-11-14 10:39:19 +11001424 const struct cred *cred;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001425 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001427 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -05001428 const char *tty;
Eric Paris5195d8e2012-01-03 14:23:05 -05001429 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Al Viroe4951492006-03-29 20:17:10 -05001431 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001432 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -04001433 if (!context->ppid)
1434 context->ppid = sys_getppid();
David Howellsc69e8d92008-11-14 10:39:19 +11001435 cred = current_cred();
1436 context->uid = cred->uid;
1437 context->gid = cred->gid;
1438 context->euid = cred->euid;
1439 context->suid = cred->suid;
David Howellsb6dff3e2008-11-14 10:39:16 +11001440 context->fsuid = cred->fsuid;
David Howellsc69e8d92008-11-14 10:39:19 +11001441 context->egid = cred->egid;
1442 context->sgid = cred->sgid;
David Howellsb6dff3e2008-11-14 10:39:16 +11001443 context->fsgid = cred->fsgid;
Al Viro3f2792f2006-07-16 06:43:48 -04001444 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001445
1446 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (!ab)
1448 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001449 audit_log_format(ab, "arch=%x syscall=%d",
1450 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (context->personality != PER_LINUX)
1452 audit_log_format(ab, " per=%lx", context->personality);
1453 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001454 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001455 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1456 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001457
Alan Coxdbda4c02008-10-13 10:40:53 +01001458 spin_lock_irq(&tsk->sighand->siglock);
Al Viro45d9bb02006-03-29 20:02:55 -05001459 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1460 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -05001461 else
1462 tty = "(none)";
Alan Coxdbda4c02008-10-13 10:40:53 +01001463 spin_unlock_irq(&tsk->sighand->siglock);
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 audit_log_format(ab,
1466 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -04001467 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +01001468 " euid=%u suid=%u fsuid=%u"
Eric Paris4746ec52008-01-08 10:06:53 -05001469 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 context->argv[0],
1471 context->argv[1],
1472 context->argv[2],
1473 context->argv[3],
1474 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -04001475 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 context->pid,
Al Virobfef93a2008-01-10 04:53:18 -05001477 tsk->loginuid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 context->uid,
1479 context->gid,
1480 context->euid, context->suid, context->fsuid,
Eric Paris4746ec52008-01-08 10:06:53 -05001481 context->egid, context->sgid, context->fsgid, tty,
1482 tsk->sessionid);
Alan Coxeb84a202006-09-29 02:01:41 -07001483
Alan Coxeb84a202006-09-29 02:01:41 -07001484
Al Viroe4951492006-03-29 20:17:10 -05001485 audit_log_task_info(ab, tsk);
Eric Paris9d960982009-06-11 14:31:37 -04001486 audit_log_key(ab, context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
David Woodhouse7551ced2005-05-26 12:04:57 +01001489 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001490
Al Viroe4951492006-03-29 20:17:10 -05001491 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (!ab)
1493 continue; /* audit_panic has been called */
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001496
Al Viro473ae302006-04-26 14:04:08 -04001497 case AUDIT_EXECVE: {
1498 struct audit_aux_data_execve *axi = (void *)aux;
Eric Parisde6bbd12008-01-07 14:31:58 -05001499 audit_log_execve_info(context, &ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001500 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001501
Eric Paris3fc689e2008-11-11 21:48:18 +11001502 case AUDIT_BPRM_FCAPS: {
1503 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1504 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1505 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1506 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1507 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1508 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1509 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1510 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1511 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1512 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1513 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1514 break; }
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519
Al Virof3298dc2008-12-10 03:16:51 -05001520 if (context->type)
Al Viroa33e6752008-12-10 03:40:06 -05001521 show_special(context, &call_panic);
Al Virof3298dc2008-12-10 03:16:51 -05001522
Al Viro157cf642008-12-14 04:57:47 -05001523 if (context->fds[0] >= 0) {
1524 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1525 if (ab) {
1526 audit_log_format(ab, "fd0=%d fd1=%d",
1527 context->fds[0], context->fds[1]);
1528 audit_log_end(ab);
1529 }
1530 }
1531
Al Viro4f6b4342008-12-09 19:50:34 -05001532 if (context->sockaddr_len) {
1533 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1534 if (ab) {
1535 audit_log_format(ab, "saddr=");
1536 audit_log_n_hex(ab, (void *)context->sockaddr,
1537 context->sockaddr_len);
1538 audit_log_end(ab);
1539 }
1540 }
1541
Amy Griffise54dc242007-03-29 18:01:04 -04001542 for (aux = context->aux_pids; aux; aux = aux->next) {
1543 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001544
1545 for (i = 0; i < axs->pid_count; i++)
1546 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001547 axs->target_auid[i],
1548 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001549 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001550 axs->target_sid[i],
1551 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001552 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001553 }
1554
Amy Griffise54dc242007-03-29 18:01:04 -04001555 if (context->target_pid &&
1556 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001557 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001558 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001559 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001560 call_panic = 1;
1561
Jan Blunck44707fd2008-02-14 19:38:33 -08001562 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001563 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001564 if (ab) {
Jan Blunck44707fd2008-02-14 19:38:33 -08001565 audit_log_d_path(ab, "cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001566 audit_log_end(ab);
1567 }
1568 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001569
Eric Paris5195d8e2012-01-03 14:23:05 -05001570 i = 0;
1571 list_for_each_entry(n, &context->names_list, list)
1572 audit_log_name(context, n, i++, &call_panic);
Eric Parisc0641f22008-01-07 13:49:15 -05001573
1574 /* Send end of event record to help user space know we are finished */
1575 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1576 if (ab)
1577 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001578 if (call_panic)
1579 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001582/**
1583 * audit_free - free a per-task audit context
1584 * @tsk: task whose audit context block to free
1585 *
Al Virofa84cb92006-03-29 20:30:19 -05001586 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588void audit_free(struct task_struct *tsk)
1589{
1590 struct audit_context *context;
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (likely(!context))
1594 return;
1595
1596 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001597 * function (e.g., exit_group), then free context block.
1598 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001599 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001600 /* that can happen only if we are called from do_exit() */
Al Viro0590b932008-12-14 23:45:27 -05001601 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001602 audit_log_exit(context, tsk);
Al Viro916d7572009-06-24 00:02:38 -04001603 if (!list_empty(&context->killed_trees))
1604 audit_kill_trees(&context->killed_trees);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 audit_free_context(context);
1607}
1608
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001609/**
1610 * audit_syscall_entry - fill in an audit record at syscall entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001611 * @arch: architecture type
1612 * @major: major syscall type (function)
1613 * @a1: additional syscall register 1
1614 * @a2: additional syscall register 2
1615 * @a3: additional syscall register 3
1616 * @a4: additional syscall register 4
1617 *
1618 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 * audit context was created when the task was created and the state or
1620 * filters demand the audit context be built. If the state from the
1621 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1622 * then the record will be written at syscall exit time (otherwise, it
1623 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001624 * be written).
1625 */
Al Viro5411be52006-03-29 20:23:36 -05001626void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 unsigned long a1, unsigned long a2,
1628 unsigned long a3, unsigned long a4)
1629{
Al Viro5411be52006-03-29 20:23:36 -05001630 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct audit_context *context = tsk->audit_context;
1632 enum audit_state state;
1633
Roland McGrath86a1c342008-06-23 15:37:04 -07001634 if (unlikely(!context))
1635 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001637 /*
1638 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * calls in kernel_thread via the entry.S interface, instead of
1640 * with direct calls. (If you are porting to a new
1641 * architecture, hitting this condition can indicate that you
1642 * got the _exit/_leave calls backward in entry.S.)
1643 *
1644 * i386 no
1645 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001646 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 *
1648 * This also happens with vm86 emulation in a non-nested manner
1649 * (entries without exits), so this case must be caught.
1650 */
1651 if (context->in_syscall) {
1652 struct audit_context *newctx;
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654#if AUDIT_DEBUG
1655 printk(KERN_ERR
1656 "audit(:%d) pid=%d in syscall=%d;"
1657 " entering syscall=%d\n",
1658 context->serial, tsk->pid, context->major, major);
1659#endif
1660 newctx = audit_alloc_context(context->state);
1661 if (newctx) {
1662 newctx->previous = context;
1663 context = newctx;
1664 tsk->audit_context = newctx;
1665 } else {
1666 /* If we can't alloc a new context, the best we
1667 * can do is to leak memory (any pending putname
1668 * will be lost). The only other alternative is
1669 * to abandon auditing. */
1670 audit_zero_context(context, context->state);
1671 }
1672 }
1673 BUG_ON(context->in_syscall || context->name_count);
1674
1675 if (!audit_enabled)
1676 return;
1677
2fd6f582005-04-29 16:08:28 +01001678 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 context->major = major;
1680 context->argv[0] = a1;
1681 context->argv[1] = a2;
1682 context->argv[2] = a3;
1683 context->argv[3] = a4;
1684
1685 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001686 context->dummy = !audit_n_rules;
Al Viro0590b932008-12-14 23:45:27 -05001687 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1688 context->prio = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001689 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Al Viro0590b932008-12-14 23:45:27 -05001690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (likely(state == AUDIT_DISABLED))
1692 return;
1693
David Woodhousece625a82005-07-18 14:24:46 -04001694 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 context->ctime = CURRENT_TIME;
1696 context->in_syscall = 1;
Al Viro0590b932008-12-14 23:45:27 -05001697 context->current_state = state;
Alexander Viro419c58f2006-09-29 00:08:50 -04001698 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Al Viroa64e6492008-11-12 18:37:41 -05001701void audit_finish_fork(struct task_struct *child)
1702{
1703 struct audit_context *ctx = current->audit_context;
1704 struct audit_context *p = child->audit_context;
Al Viro0590b932008-12-14 23:45:27 -05001705 if (!p || !ctx)
1706 return;
1707 if (!ctx->in_syscall || ctx->current_state != AUDIT_RECORD_CONTEXT)
Al Viroa64e6492008-11-12 18:37:41 -05001708 return;
1709 p->arch = ctx->arch;
1710 p->major = ctx->major;
1711 memcpy(p->argv, ctx->argv, sizeof(ctx->argv));
1712 p->ctime = ctx->ctime;
1713 p->dummy = ctx->dummy;
Al Viroa64e6492008-11-12 18:37:41 -05001714 p->in_syscall = ctx->in_syscall;
1715 p->filterkey = kstrdup(ctx->filterkey, GFP_KERNEL);
1716 p->ppid = current->pid;
Al Viro0590b932008-12-14 23:45:27 -05001717 p->prio = ctx->prio;
1718 p->current_state = ctx->current_state;
Al Viroa64e6492008-11-12 18:37:41 -05001719}
1720
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001721/**
1722 * audit_syscall_exit - deallocate audit context after a system call
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001723 * @valid: success/failure flag
1724 * @return_code: syscall return value
1725 *
1726 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1728 * filtering, or because some other part of the kernel write an audit
1729 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001730 * free the names stored from getname().
1731 */
Al Viro5411be52006-03-29 20:23:36 -05001732void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Al Viro5411be52006-03-29 20:23:36 -05001734 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 struct audit_context *context;
1736
2fd6f582005-04-29 16:08:28 +01001737 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001740 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Al Viro0590b932008-12-14 23:45:27 -05001742 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001743 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 context->in_syscall = 0;
Al Viro0590b932008-12-14 23:45:27 -05001746 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
2fd6f582005-04-29 16:08:28 +01001747
Al Viro916d7572009-06-24 00:02:38 -04001748 if (!list_empty(&context->killed_trees))
1749 audit_kill_trees(&context->killed_trees);
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (context->previous) {
1752 struct audit_context *new_context = context->previous;
1753 context->previous = NULL;
1754 audit_free_context(context);
1755 tsk->audit_context = new_context;
1756 } else {
1757 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001758 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001760 context->aux = NULL;
1761 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001762 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001763 context->target_sid = 0;
Al Viro4f6b4342008-12-09 19:50:34 -05001764 context->sockaddr_len = 0;
Al Virof3298dc2008-12-10 03:16:51 -05001765 context->type = 0;
Al Viro157cf642008-12-14 04:57:47 -05001766 context->fds[0] = -1;
Al Viroe048e022008-12-16 03:51:22 -05001767 if (context->state != AUDIT_RECORD_CONTEXT) {
1768 kfree(context->filterkey);
1769 context->filterkey = NULL;
1770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 tsk->audit_context = context;
1772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
Al Viro74c3cbe2007-07-22 08:04:18 -04001775static inline void handle_one(const struct inode *inode)
1776{
1777#ifdef CONFIG_AUDIT_TREE
1778 struct audit_context *context;
1779 struct audit_tree_refs *p;
1780 struct audit_chunk *chunk;
1781 int count;
Eric Parise61ce862009-12-17 21:24:24 -05001782 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
Al Viro74c3cbe2007-07-22 08:04:18 -04001783 return;
1784 context = current->audit_context;
1785 p = context->trees;
1786 count = context->tree_count;
1787 rcu_read_lock();
1788 chunk = audit_tree_lookup(inode);
1789 rcu_read_unlock();
1790 if (!chunk)
1791 return;
1792 if (likely(put_tree_ref(context, chunk)))
1793 return;
1794 if (unlikely(!grow_tree_refs(context))) {
Eric Paris436c4052008-04-18 10:01:04 -04001795 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001796 audit_set_auditable(context);
1797 audit_put_chunk(chunk);
1798 unroll_tree_refs(context, p, count);
1799 return;
1800 }
1801 put_tree_ref(context, chunk);
1802#endif
1803}
1804
1805static void handle_path(const struct dentry *dentry)
1806{
1807#ifdef CONFIG_AUDIT_TREE
1808 struct audit_context *context;
1809 struct audit_tree_refs *p;
1810 const struct dentry *d, *parent;
1811 struct audit_chunk *drop;
1812 unsigned long seq;
1813 int count;
1814
1815 context = current->audit_context;
1816 p = context->trees;
1817 count = context->tree_count;
1818retry:
1819 drop = NULL;
1820 d = dentry;
1821 rcu_read_lock();
1822 seq = read_seqbegin(&rename_lock);
1823 for(;;) {
1824 struct inode *inode = d->d_inode;
Eric Parise61ce862009-12-17 21:24:24 -05001825 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
Al Viro74c3cbe2007-07-22 08:04:18 -04001826 struct audit_chunk *chunk;
1827 chunk = audit_tree_lookup(inode);
1828 if (chunk) {
1829 if (unlikely(!put_tree_ref(context, chunk))) {
1830 drop = chunk;
1831 break;
1832 }
1833 }
1834 }
1835 parent = d->d_parent;
1836 if (parent == d)
1837 break;
1838 d = parent;
1839 }
1840 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1841 rcu_read_unlock();
1842 if (!drop) {
1843 /* just a race with rename */
1844 unroll_tree_refs(context, p, count);
1845 goto retry;
1846 }
1847 audit_put_chunk(drop);
1848 if (grow_tree_refs(context)) {
1849 /* OK, got more space */
1850 unroll_tree_refs(context, p, count);
1851 goto retry;
1852 }
1853 /* too bad */
1854 printk(KERN_WARNING
Eric Paris436c4052008-04-18 10:01:04 -04001855 "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001856 unroll_tree_refs(context, p, count);
1857 audit_set_auditable(context);
1858 return;
1859 }
1860 rcu_read_unlock();
1861#endif
1862}
1863
Eric Paris5195d8e2012-01-03 14:23:05 -05001864static struct audit_names *audit_alloc_name(struct audit_context *context)
1865{
1866 struct audit_names *aname;
1867
1868 if (context->name_count < AUDIT_NAMES) {
1869 aname = &context->preallocated_names[context->name_count];
1870 memset(aname, 0, sizeof(*aname));
1871 } else {
1872 aname = kzalloc(sizeof(*aname), GFP_NOFS);
1873 if (!aname)
1874 return NULL;
1875 aname->should_free = true;
1876 }
1877
1878 aname->ino = (unsigned long)-1;
1879 list_add_tail(&aname->list, &context->names_list);
1880
1881 context->name_count++;
1882#if AUDIT_DEBUG
1883 context->ino_count++;
1884#endif
1885 return aname;
1886}
1887
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001888/**
1889 * audit_getname - add a name to the list
1890 * @name: name to add
1891 *
1892 * Add a name to the list of audit names for this context.
1893 * Called from fs/namei.c:getname().
1894 */
Al Virod8945bb52006-05-18 16:01:30 -04001895void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 struct audit_context *context = current->audit_context;
Eric Paris5195d8e2012-01-03 14:23:05 -05001898 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Al Virod8945bb52006-05-18 16:01:30 -04001900 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return;
1902
1903 if (!context->in_syscall) {
1904#if AUDIT_DEBUG == 2
1905 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1906 __FILE__, __LINE__, context->serial, name);
1907 dump_stack();
1908#endif
1909 return;
1910 }
Eric Paris5195d8e2012-01-03 14:23:05 -05001911
1912 n = audit_alloc_name(context);
1913 if (!n)
1914 return;
1915
1916 n->name = name;
1917 n->name_len = AUDIT_NAME_FULL;
1918 n->name_put = true;
1919
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02001920 if (!context->pwd.dentry)
1921 get_fs_pwd(current->fs, &context->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001924/* audit_putname - intercept a putname request
1925 * @name: name to intercept and delay for putname
1926 *
1927 * If we have stored the name from getname in the audit context,
1928 * then we delay the putname until syscall exit.
1929 * Called from include/linux/fs.h:putname().
1930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931void audit_putname(const char *name)
1932{
1933 struct audit_context *context = current->audit_context;
1934
1935 BUG_ON(!context);
1936 if (!context->in_syscall) {
1937#if AUDIT_DEBUG == 2
1938 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1939 __FILE__, __LINE__, context->serial, name);
1940 if (context->name_count) {
Eric Paris5195d8e2012-01-03 14:23:05 -05001941 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 int i;
Eric Paris5195d8e2012-01-03 14:23:05 -05001943
1944 list_for_each_entry(n, &context->names_list, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 printk(KERN_ERR "name[%d] = %p = %s\n", i,
Eric Paris5195d8e2012-01-03 14:23:05 -05001946 n->name, n->name ?: "(null)");
1947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948#endif
1949 __putname(name);
1950 }
1951#if AUDIT_DEBUG
1952 else {
1953 ++context->put_count;
1954 if (context->put_count > context->name_count) {
1955 printk(KERN_ERR "%s:%d(:%d): major=%d"
1956 " in_syscall=%d putname(%p) name_count=%d"
1957 " put_count=%d\n",
1958 __FILE__, __LINE__,
1959 context->serial, context->major,
1960 context->in_syscall, name, context->name_count,
1961 context->put_count);
1962 dump_stack();
1963 }
1964 }
1965#endif
1966}
1967
Eric Paris851f7ff2008-11-11 21:48:14 +11001968static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1969{
1970 struct cpu_vfs_cap_data caps;
1971 int rc;
1972
Eric Paris851f7ff2008-11-11 21:48:14 +11001973 if (!dentry)
1974 return 0;
1975
1976 rc = get_vfs_caps_from_disk(dentry, &caps);
1977 if (rc)
1978 return rc;
1979
1980 name->fcap.permitted = caps.permitted;
1981 name->fcap.inheritable = caps.inheritable;
1982 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1983 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1984
1985 return 0;
1986}
1987
1988
Amy Griffis3e2efce2006-07-13 13:16:02 -04001989/* Copy inode data into an audit_names. */
Eric Paris851f7ff2008-11-11 21:48:14 +11001990static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1991 const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001992{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001993 name->ino = inode->i_ino;
1994 name->dev = inode->i_sb->s_dev;
1995 name->mode = inode->i_mode;
1996 name->uid = inode->i_uid;
1997 name->gid = inode->i_gid;
1998 name->rdev = inode->i_rdev;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001999 security_inode_getsecid(inode, &name->osid);
Eric Paris851f7ff2008-11-11 21:48:14 +11002000 audit_copy_fcaps(name, dentry);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002001}
2002
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002003/**
2004 * audit_inode - store the inode and device from a lookup
2005 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07002006 * @dentry: dentry being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002007 *
2008 * Called from fs/namei.c:path_lookup().
2009 */
Al Viro5a190ae2007-06-07 12:19:32 -04002010void __audit_inode(const char *name, const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04002013 const struct inode *inode = dentry->d_inode;
Eric Paris5195d8e2012-01-03 14:23:05 -05002014 struct audit_names *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 if (!context->in_syscall)
2017 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05002018
2019 list_for_each_entry_reverse(n, &context->names_list, list) {
2020 if (n->name && (n->name == name))
2021 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Eric Paris5195d8e2012-01-03 14:23:05 -05002023
2024 /* unable to find the name from a previous getname() */
2025 n = audit_alloc_name(context);
2026 if (!n)
2027 return;
2028out:
Al Viro74c3cbe2007-07-22 08:04:18 -04002029 handle_path(dentry);
Eric Paris5195d8e2012-01-03 14:23:05 -05002030 audit_copy_inode(n, dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Amy Griffis73241cc2005-11-03 16:00:25 +00002033/**
2034 * audit_inode_child - collect inode info for created/removed objects
Randy Dunlap481968f2007-10-21 20:59:53 -07002035 * @dentry: dentry being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04002036 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00002037 *
2038 * For syscalls that create or remove filesystem objects, audit_inode
2039 * can only collect information for the filesystem object's parent.
2040 * This call updates the audit context with the child's information.
2041 * Syscalls that create a new filesystem object must be hooked after
2042 * the object is created. Syscalls that remove a filesystem object
2043 * must be hooked prior, in order to capture the target inode during
2044 * unsuccessful attempts.
2045 */
Al Virocccc6bb2009-12-25 05:07:33 -05002046void __audit_inode_child(const struct dentry *dentry,
Amy Griffis73d3ec52006-07-13 13:16:39 -04002047 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00002048{
Amy Griffis73241cc2005-11-03 16:00:25 +00002049 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05002050 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04002051 const struct inode *inode = dentry->d_inode;
Al Virocccc6bb2009-12-25 05:07:33 -05002052 const char *dname = dentry->d_name.name;
Eric Paris5195d8e2012-01-03 14:23:05 -05002053 struct audit_names *n;
Amy Griffis9c937dc2006-06-08 23:19:31 -04002054 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00002055
2056 if (!context->in_syscall)
2057 return;
2058
Al Viro74c3cbe2007-07-22 08:04:18 -04002059 if (inode)
2060 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00002061
Amy Griffis5712e882007-02-13 14:15:22 -05002062 /* parent is more likely, look for it first */
Eric Paris5195d8e2012-01-03 14:23:05 -05002063 list_for_each_entry(n, &context->names_list, list) {
Amy Griffis5712e882007-02-13 14:15:22 -05002064 if (!n->name)
2065 continue;
2066
2067 if (n->ino == parent->i_ino &&
2068 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2069 n->name_len = dirlen; /* update parent data in place */
2070 found_parent = n->name;
2071 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04002072 }
Steve Grubbac9910c2006-09-28 14:31:32 -04002073 }
Amy Griffis73241cc2005-11-03 16:00:25 +00002074
Amy Griffis5712e882007-02-13 14:15:22 -05002075 /* no matching parent, look for matching child */
Eric Paris5195d8e2012-01-03 14:23:05 -05002076 list_for_each_entry(n, &context->names_list, list) {
Amy Griffis5712e882007-02-13 14:15:22 -05002077 if (!n->name)
2078 continue;
2079
2080 /* strcmp() is the more likely scenario */
2081 if (!strcmp(dname, n->name) ||
2082 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2083 if (inode)
Eric Paris851f7ff2008-11-11 21:48:14 +11002084 audit_copy_inode(n, NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002085 else
2086 n->ino = (unsigned long)-1;
2087 found_child = n->name;
2088 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04002089 }
Amy Griffis5712e882007-02-13 14:15:22 -05002090 }
2091
2092add_names:
2093 if (!found_parent) {
Eric Paris5195d8e2012-01-03 14:23:05 -05002094 n = audit_alloc_name(context);
2095 if (!n)
Amy Griffis5712e882007-02-13 14:15:22 -05002096 return;
Eric Paris5195d8e2012-01-03 14:23:05 -05002097 audit_copy_inode(n, NULL, parent);
Amy Griffis73d3ec52006-07-13 13:16:39 -04002098 }
Amy Griffis5712e882007-02-13 14:15:22 -05002099
2100 if (!found_child) {
Eric Paris5195d8e2012-01-03 14:23:05 -05002101 n = audit_alloc_name(context);
2102 if (!n)
Amy Griffis5712e882007-02-13 14:15:22 -05002103 return;
Amy Griffis5712e882007-02-13 14:15:22 -05002104
2105 /* Re-use the name belonging to the slot for a matching parent
2106 * directory. All names for this context are relinquished in
2107 * audit_free_names() */
2108 if (found_parent) {
Eric Paris5195d8e2012-01-03 14:23:05 -05002109 n->name = found_parent;
2110 n->name_len = AUDIT_NAME_FULL;
Amy Griffis5712e882007-02-13 14:15:22 -05002111 /* don't call __putname() */
Eric Paris5195d8e2012-01-03 14:23:05 -05002112 n->name_put = false;
Amy Griffis5712e882007-02-13 14:15:22 -05002113 }
2114
2115 if (inode)
Eric Paris5195d8e2012-01-03 14:23:05 -05002116 audit_copy_inode(n, NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002117 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04002118}
Trond Myklebust50e437d2007-06-07 22:44:34 -04002119EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04002120
2121/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002122 * auditsc_get_stamp - get local copies of audit_context values
2123 * @ctx: audit_context for the task
2124 * @t: timespec to store time recorded in the audit_context
2125 * @serial: serial value that is recorded in the audit_context
2126 *
2127 * Also sets the context as auditable.
2128 */
Al Viro48887e62008-12-06 01:05:50 -05002129int auditsc_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01002130 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Al Viro48887e62008-12-06 01:05:50 -05002132 if (!ctx->in_syscall)
2133 return 0;
David Woodhousece625a82005-07-18 14:24:46 -04002134 if (!ctx->serial)
2135 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01002136 t->tv_sec = ctx->ctime.tv_sec;
2137 t->tv_nsec = ctx->ctime.tv_nsec;
2138 *serial = ctx->serial;
Al Viro0590b932008-12-14 23:45:27 -05002139 if (!ctx->prio) {
2140 ctx->prio = 1;
2141 ctx->current_state = AUDIT_RECORD_CONTEXT;
2142 }
Al Viro48887e62008-12-06 01:05:50 -05002143 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Eric Paris4746ec52008-01-08 10:06:53 -05002146/* global counter which is incremented every time something logs in */
2147static atomic_t session_id = ATOMIC_INIT(0);
2148
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002149/**
2150 * audit_set_loginuid - set a task's audit_context loginuid
2151 * @task: task whose audit context is being modified
2152 * @loginuid: loginuid value
2153 *
2154 * Returns 0.
2155 *
2156 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2157 */
Steve Grubb456be6c2005-04-29 17:30:07 +01002158int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Eric Paris4746ec52008-01-08 10:06:53 -05002160 unsigned int sessionid = atomic_inc_return(&session_id);
Steve Grubb41757102006-06-12 07:48:28 -04002161 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01002162
Al Virobfef93a2008-01-10 04:53:18 -05002163 if (context && context->in_syscall) {
2164 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04002165
Al Virobfef93a2008-01-10 04:53:18 -05002166 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2167 if (ab) {
2168 audit_log_format(ab, "login pid=%d uid=%u "
Eric Paris4746ec52008-01-08 10:06:53 -05002169 "old auid=%u new auid=%u"
2170 " old ses=%u new ses=%u",
David Howellsc69e8d92008-11-14 10:39:19 +11002171 task->pid, task_uid(task),
Eric Paris4746ec52008-01-08 10:06:53 -05002172 task->loginuid, loginuid,
2173 task->sessionid, sessionid);
Al Virobfef93a2008-01-10 04:53:18 -05002174 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01002175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Eric Paris4746ec52008-01-08 10:06:53 -05002177 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05002178 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return 0;
2180}
2181
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002182/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002183 * __audit_mq_open - record audit data for a POSIX MQ open
2184 * @oflag: open flag
2185 * @mode: mode bits
Randy Dunlap6b962552009-01-05 13:41:13 -08002186 * @attr: queue attributes
George C. Wilson20ca73b2006-05-24 16:09:55 -05002187 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002188 */
Al Virodf0a4282011-07-26 05:26:10 -04002189void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002190{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002191 struct audit_context *context = current->audit_context;
2192
Al Viro564f6992008-12-14 04:02:26 -05002193 if (attr)
2194 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2195 else
2196 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002197
Al Viro564f6992008-12-14 04:02:26 -05002198 context->mq_open.oflag = oflag;
2199 context->mq_open.mode = mode;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002200
Al Viro564f6992008-12-14 04:02:26 -05002201 context->type = AUDIT_MQ_OPEN;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002202}
2203
2204/**
Al Viroc32c8af2008-12-14 03:46:48 -05002205 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
George C. Wilson20ca73b2006-05-24 16:09:55 -05002206 * @mqdes: MQ descriptor
2207 * @msg_len: Message length
2208 * @msg_prio: Message priority
Al Viroc32c8af2008-12-14 03:46:48 -05002209 * @abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002210 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002211 */
Al Viroc32c8af2008-12-14 03:46:48 -05002212void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2213 const struct timespec *abs_timeout)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002214{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002215 struct audit_context *context = current->audit_context;
Al Viroc32c8af2008-12-14 03:46:48 -05002216 struct timespec *p = &context->mq_sendrecv.abs_timeout;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002217
Al Viroc32c8af2008-12-14 03:46:48 -05002218 if (abs_timeout)
2219 memcpy(p, abs_timeout, sizeof(struct timespec));
2220 else
2221 memset(p, 0, sizeof(struct timespec));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002222
Al Viroc32c8af2008-12-14 03:46:48 -05002223 context->mq_sendrecv.mqdes = mqdes;
2224 context->mq_sendrecv.msg_len = msg_len;
2225 context->mq_sendrecv.msg_prio = msg_prio;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002226
Al Viroc32c8af2008-12-14 03:46:48 -05002227 context->type = AUDIT_MQ_SENDRECV;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002228}
2229
2230/**
2231 * __audit_mq_notify - record audit data for a POSIX MQ notify
2232 * @mqdes: MQ descriptor
Randy Dunlap6b962552009-01-05 13:41:13 -08002233 * @notification: Notification event
George C. Wilson20ca73b2006-05-24 16:09:55 -05002234 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002235 */
2236
Al Viro20114f72008-12-10 07:16:12 -05002237void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002238{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002239 struct audit_context *context = current->audit_context;
2240
Al Viro20114f72008-12-10 07:16:12 -05002241 if (notification)
2242 context->mq_notify.sigev_signo = notification->sigev_signo;
2243 else
2244 context->mq_notify.sigev_signo = 0;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002245
Al Viro20114f72008-12-10 07:16:12 -05002246 context->mq_notify.mqdes = mqdes;
2247 context->type = AUDIT_MQ_NOTIFY;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002248}
2249
2250/**
2251 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2252 * @mqdes: MQ descriptor
2253 * @mqstat: MQ flags
2254 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002255 */
Al Viro73929062008-12-10 06:58:59 -05002256void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002257{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002258 struct audit_context *context = current->audit_context;
Al Viro73929062008-12-10 06:58:59 -05002259 context->mq_getsetattr.mqdes = mqdes;
2260 context->mq_getsetattr.mqstat = *mqstat;
2261 context->type = AUDIT_MQ_GETSETATTR;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002262}
2263
2264/**
Steve Grubb073115d2006-04-02 17:07:33 -04002265 * audit_ipc_obj - record audit data for ipc object
2266 * @ipcp: ipc permissions
2267 *
Steve Grubb073115d2006-04-02 17:07:33 -04002268 */
Al Viroa33e6752008-12-10 03:40:06 -05002269void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002270{
Steve Grubb073115d2006-04-02 17:07:33 -04002271 struct audit_context *context = current->audit_context;
Al Viroa33e6752008-12-10 03:40:06 -05002272 context->ipc.uid = ipcp->uid;
2273 context->ipc.gid = ipcp->gid;
2274 context->ipc.mode = ipcp->mode;
Al Viroe816f372008-12-10 03:47:15 -05002275 context->ipc.has_perm = 0;
Al Viroa33e6752008-12-10 03:40:06 -05002276 security_ipc_getsecid(ipcp, &context->ipc.osid);
2277 context->type = AUDIT_IPC;
Steve Grubb073115d2006-04-02 17:07:33 -04002278}
2279
2280/**
2281 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002282 * @qbytes: msgq bytes
2283 * @uid: msgq user id
2284 * @gid: msgq group id
2285 * @mode: msgq mode (permissions)
2286 *
Al Viroe816f372008-12-10 03:47:15 -05002287 * Called only after audit_ipc_obj().
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002288 */
Al Viro2570ebb2011-07-27 14:03:22 -04002289void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 struct audit_context *context = current->audit_context;
2292
Al Viroe816f372008-12-10 03:47:15 -05002293 context->ipc.qbytes = qbytes;
2294 context->ipc.perm_uid = uid;
2295 context->ipc.perm_gid = gid;
2296 context->ipc.perm_mode = mode;
2297 context->ipc.has_perm = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002299
Al Viro473ae302006-04-26 14:04:08 -04002300int audit_bprm(struct linux_binprm *bprm)
2301{
2302 struct audit_aux_data_execve *ax;
2303 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002304
Al Viro5ac3a9c2006-07-16 06:38:45 -04002305 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04002306 return 0;
2307
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002308 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002309 if (!ax)
2310 return -ENOMEM;
2311
2312 ax->argc = bprm->argc;
2313 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002314 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002315 ax->d.type = AUDIT_EXECVE;
2316 ax->d.next = context->aux;
2317 context->aux = (void *)ax;
2318 return 0;
2319}
2320
2321
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002322/**
2323 * audit_socketcall - record audit data for sys_socketcall
2324 * @nargs: number of args
2325 * @args: args array
2326 *
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002327 */
Al Virof3298dc2008-12-10 03:16:51 -05002328void audit_socketcall(int nargs, unsigned long *args)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002329{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002330 struct audit_context *context = current->audit_context;
2331
Al Viro5ac3a9c2006-07-16 06:38:45 -04002332 if (likely(!context || context->dummy))
Al Virof3298dc2008-12-10 03:16:51 -05002333 return;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002334
Al Virof3298dc2008-12-10 03:16:51 -05002335 context->type = AUDIT_SOCKETCALL;
2336 context->socketcall.nargs = nargs;
2337 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002338}
2339
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002340/**
Al Virodb349502007-02-07 01:48:00 -05002341 * __audit_fd_pair - record audit data for pipe and socketpair
2342 * @fd1: the first file descriptor
2343 * @fd2: the second file descriptor
2344 *
Al Virodb349502007-02-07 01:48:00 -05002345 */
Al Viro157cf642008-12-14 04:57:47 -05002346void __audit_fd_pair(int fd1, int fd2)
Al Virodb349502007-02-07 01:48:00 -05002347{
2348 struct audit_context *context = current->audit_context;
Al Viro157cf642008-12-14 04:57:47 -05002349 context->fds[0] = fd1;
2350 context->fds[1] = fd2;
Al Virodb349502007-02-07 01:48:00 -05002351}
2352
2353/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002354 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2355 * @len: data length in user space
2356 * @a: data address in kernel space
2357 *
2358 * Returns 0 for success or NULL context or < 0 on error.
2359 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002360int audit_sockaddr(int len, void *a)
2361{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002362 struct audit_context *context = current->audit_context;
2363
Al Viro5ac3a9c2006-07-16 06:38:45 -04002364 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002365 return 0;
2366
Al Viro4f6b4342008-12-09 19:50:34 -05002367 if (!context->sockaddr) {
2368 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2369 if (!p)
2370 return -ENOMEM;
2371 context->sockaddr = p;
2372 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002373
Al Viro4f6b4342008-12-09 19:50:34 -05002374 context->sockaddr_len = len;
2375 memcpy(context->sockaddr, a, len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002376 return 0;
2377}
2378
Al Viroa5cb0132007-03-20 13:58:35 -04002379void __audit_ptrace(struct task_struct *t)
2380{
2381 struct audit_context *context = current->audit_context;
2382
2383 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002384 context->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002385 context->target_uid = task_uid(t);
Eric Paris4746ec52008-01-08 10:06:53 -05002386 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002387 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002388 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002389}
2390
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002391/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002392 * audit_signal_info - record signal info for shutting down audit subsystem
2393 * @sig: signal value
2394 * @t: task being signaled
2395 *
2396 * If the audit subsystem is being terminated, record the task (pid)
2397 * and uid that is doing that.
2398 */
Amy Griffise54dc242007-03-29 18:01:04 -04002399int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002400{
Amy Griffise54dc242007-03-29 18:01:04 -04002401 struct audit_aux_data_pids *axp;
2402 struct task_struct *tsk = current;
2403 struct audit_context *ctx = tsk->audit_context;
David Howellsc69e8d92008-11-14 10:39:19 +11002404 uid_t uid = current_uid(), t_uid = task_uid(t);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002405
Al Viro175fc482007-08-08 00:01:46 +01002406 if (audit_pid && t->tgid == audit_pid) {
Eric Parisee1d3152008-07-07 10:49:45 -04002407 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
Al Viro175fc482007-08-08 00:01:46 +01002408 audit_sig_pid = tsk->pid;
Al Virobfef93a2008-01-10 04:53:18 -05002409 if (tsk->loginuid != -1)
2410 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002411 else
David Howellsc69e8d92008-11-14 10:39:19 +11002412 audit_sig_uid = uid;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002413 security_task_getsecid(tsk, &audit_sig_sid);
Al Viro175fc482007-08-08 00:01:46 +01002414 }
2415 if (!audit_signals || audit_dummy_context())
2416 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002417 }
Amy Griffise54dc242007-03-29 18:01:04 -04002418
Amy Griffise54dc242007-03-29 18:01:04 -04002419 /* optimize the common case by putting first signal recipient directly
2420 * in audit_context */
2421 if (!ctx->target_pid) {
2422 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002423 ctx->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002424 ctx->target_uid = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002425 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002426 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002427 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002428 return 0;
2429 }
2430
2431 axp = (void *)ctx->aux_pids;
2432 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2433 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2434 if (!axp)
2435 return -ENOMEM;
2436
2437 axp->d.type = AUDIT_OBJ_PID;
2438 axp->d.next = ctx->aux_pids;
2439 ctx->aux_pids = (void *)axp;
2440 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002441 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002442
2443 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002444 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002445 axp->target_uid[axp->pid_count] = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002446 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002447 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002448 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002449 axp->pid_count++;
2450
2451 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002452}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002453
2454/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002455 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
David Howellsd84f4f92008-11-14 10:39:23 +11002456 * @bprm: pointer to the bprm being processed
2457 * @new: the proposed new credentials
2458 * @old: the old credentials
Eric Paris3fc689e2008-11-11 21:48:18 +11002459 *
2460 * Simply check if the proc already has the caps given by the file and if not
2461 * store the priv escalation info for later auditing at the end of the syscall
2462 *
Eric Paris3fc689e2008-11-11 21:48:18 +11002463 * -Eric
2464 */
David Howellsd84f4f92008-11-14 10:39:23 +11002465int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2466 const struct cred *new, const struct cred *old)
Eric Paris3fc689e2008-11-11 21:48:18 +11002467{
2468 struct audit_aux_data_bprm_fcaps *ax;
2469 struct audit_context *context = current->audit_context;
2470 struct cpu_vfs_cap_data vcaps;
2471 struct dentry *dentry;
2472
2473 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2474 if (!ax)
David Howellsd84f4f92008-11-14 10:39:23 +11002475 return -ENOMEM;
Eric Paris3fc689e2008-11-11 21:48:18 +11002476
2477 ax->d.type = AUDIT_BPRM_FCAPS;
2478 ax->d.next = context->aux;
2479 context->aux = (void *)ax;
2480
2481 dentry = dget(bprm->file->f_dentry);
2482 get_vfs_caps_from_disk(dentry, &vcaps);
2483 dput(dentry);
2484
2485 ax->fcap.permitted = vcaps.permitted;
2486 ax->fcap.inheritable = vcaps.inheritable;
2487 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2488 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2489
David Howellsd84f4f92008-11-14 10:39:23 +11002490 ax->old_pcap.permitted = old->cap_permitted;
2491 ax->old_pcap.inheritable = old->cap_inheritable;
2492 ax->old_pcap.effective = old->cap_effective;
Eric Paris3fc689e2008-11-11 21:48:18 +11002493
David Howellsd84f4f92008-11-14 10:39:23 +11002494 ax->new_pcap.permitted = new->cap_permitted;
2495 ax->new_pcap.inheritable = new->cap_inheritable;
2496 ax->new_pcap.effective = new->cap_effective;
2497 return 0;
Eric Paris3fc689e2008-11-11 21:48:18 +11002498}
2499
2500/**
Eric Parise68b75a02008-11-11 21:48:22 +11002501 * __audit_log_capset - store information about the arguments to the capset syscall
David Howellsd84f4f92008-11-14 10:39:23 +11002502 * @pid: target pid of the capset call
2503 * @new: the new credentials
2504 * @old: the old (current) credentials
Eric Parise68b75a02008-11-11 21:48:22 +11002505 *
2506 * Record the aguments userspace sent to sys_capset for later printing by the
2507 * audit system if applicable
2508 */
Al Viro57f71a02009-01-04 14:52:57 -05002509void __audit_log_capset(pid_t pid,
David Howellsd84f4f92008-11-14 10:39:23 +11002510 const struct cred *new, const struct cred *old)
Eric Parise68b75a02008-11-11 21:48:22 +11002511{
Eric Parise68b75a02008-11-11 21:48:22 +11002512 struct audit_context *context = current->audit_context;
Al Viro57f71a02009-01-04 14:52:57 -05002513 context->capset.pid = pid;
2514 context->capset.cap.effective = new->cap_effective;
2515 context->capset.cap.inheritable = new->cap_effective;
2516 context->capset.cap.permitted = new->cap_permitted;
2517 context->type = AUDIT_CAPSET;
Eric Parise68b75a02008-11-11 21:48:22 +11002518}
2519
Al Viro120a7952010-10-30 02:54:44 -04002520void __audit_mmap_fd(int fd, int flags)
2521{
2522 struct audit_context *context = current->audit_context;
2523 context->mmap.fd = fd;
2524 context->mmap.flags = flags;
2525 context->type = AUDIT_MMAP;
2526}
2527
Eric Parise68b75a02008-11-11 21:48:22 +11002528/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002529 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002530 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002531 *
2532 * If a process ends with a core dump, something fishy is going on and we
2533 * should record the event for investigation.
2534 */
2535void audit_core_dumps(long signr)
2536{
2537 struct audit_buffer *ab;
2538 u32 sid;
David Howells76aac0e2008-11-14 10:39:12 +11002539 uid_t auid = audit_get_loginuid(current), uid;
2540 gid_t gid;
Eric Paris4746ec52008-01-08 10:06:53 -05002541 unsigned int sessionid = audit_get_sessionid(current);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002542
2543 if (!audit_enabled)
2544 return;
2545
2546 if (signr == SIGQUIT) /* don't care for those */
2547 return;
2548
2549 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
David Howells76aac0e2008-11-14 10:39:12 +11002550 current_uid_gid(&uid, &gid);
Eric Paris4746ec52008-01-08 10:06:53 -05002551 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
David Howells76aac0e2008-11-14 10:39:12 +11002552 auid, uid, gid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002553 security_task_getsecid(current, &sid);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002554 if (sid) {
2555 char *ctx = NULL;
2556 u32 len;
2557
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002558 if (security_secid_to_secctx(sid, &ctx, &len))
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002559 audit_log_format(ab, " ssid=%u", sid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002560 else {
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002561 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002562 security_release_secctx(ctx, len);
2563 }
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002564 }
2565 audit_log_format(ab, " pid=%d comm=", current->pid);
2566 audit_log_untrustedstring(ab, current->comm);
2567 audit_log_format(ab, " sig=%ld", signr);
2568 audit_log_end(ab);
2569}
Al Viro916d7572009-06-24 00:02:38 -04002570
2571struct list_head *audit_killed_trees(void)
2572{
2573 struct audit_context *ctx = current->audit_context;
2574 if (likely(!ctx || !ctx->in_syscall))
2575 return NULL;
2576 return &ctx->killed_trees;
2577}