blob: a09c503170597e9624a0491e85abb8b15e6798f8 [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* auditsc.c -- System-call auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handles all system-call specific auditing features.
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
Amy Griffis73241cc2005-11-03 16:00:25 +00005 * Copyright 2005 Hewlett-Packard Development Company, L.P.
George C. Wilson20ca73b2006-05-24 16:09:55 -05006 * Copyright (C) 2005, 2006 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * All Rights Reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
24 *
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
27 *
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
31 *
George C. Wilson20ca73b2006-05-24 16:09:55 -050032 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
33 * 2006.
34 *
Dustin Kirklandb63862f2005-11-03 15:41:46 +000035 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
37 *
Amy Griffis73241cc2005-11-03 16:00:25 +000038 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000040 *
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
45#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/types.h>
Arun Sharma600634972011-07-26 16:09:06 -070047#include <linux/atomic.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000048#include <linux/fs.h>
49#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/mm.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040051#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
Stephen Smalley01116102005-05-21 00:15:52 +010053#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010054#include <linux/socket.h>
George C. Wilson20ca73b2006-05-24 16:09:55 -050055#include <linux/mqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/audit.h>
57#include <linux/personality.h>
58#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010059#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010060#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000062#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000063#include <linux/list.h>
Steve Grubba6c043a2006-01-01 14:07:00 -050064#include <linux/tty.h>
Al Viro473ae302006-04-26 14:04:08 -040065#include <linux/binfmts.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040066#include <linux/highmem.h>
Al Virof46038f2006-05-06 08:22:52 -040067#include <linux/syscalls.h>
Eric Paris851f7ff2008-11-11 21:48:14 +110068#include <linux/capability.h>
Al Viro5ad4e532009-03-29 19:50:06 -040069#include <linux/fs_struct.h>
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
74 * for saving names from getname(). */
75#define AUDIT_NAMES 20
76
Amy Griffis9c937dc2006-06-08 23:19:31 -040077/* Indicates that audit should log the full pathname. */
78#define AUDIT_NAME_FULL -1
79
Eric Parisde6bbd12008-01-07 14:31:58 -050080/* no execve audit message should be longer than this (userspace limits) */
81#define MAX_EXECVE_AUDIT_LEN 7500
82
Al Viro471a5c72006-07-10 08:29:24 -040083/* number of audit rules */
84int audit_n_rules;
85
Amy Griffise54dc242007-03-29 18:01:04 -040086/* determines whether we collect data for signals sent */
87int audit_signals;
88
Eric Paris851f7ff2008-11-11 21:48:14 +110089struct audit_cap_data {
90 kernel_cap_t permitted;
91 kernel_cap_t inheritable;
92 union {
93 unsigned int fE; /* effective bit of a file capability */
94 kernel_cap_t effective; /* effective set of a process */
95 };
96};
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* When fs/namei.c:getname() is called, we store the pointer in name and
99 * we don't let putname() free it (instead we free all of the saved
100 * pointers at syscall exit time).
101 *
102 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
103struct audit_names {
104 const char *name;
Amy Griffis9c937dc2006-06-08 23:19:31 -0400105 int name_len; /* number of name's characters to log */
106 unsigned name_put; /* call __putname() for this name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116};
117
118struct audit_aux_data {
119 struct audit_aux_data *next;
120 int type;
121};
122
123#define AUDIT_AUX_IPCPERM 0
124
Amy Griffise54dc242007-03-29 18:01:04 -0400125/* Number of target pids per aux struct. */
126#define AUDIT_AUX_PIDS 16
127
Al Viro473ae302006-04-26 14:04:08 -0400128struct audit_aux_data_execve {
129 struct audit_aux_data d;
130 int argc;
131 int envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700132 struct mm_struct *mm;
Al Viro473ae302006-04-26 14:04:08 -0400133};
134
Amy Griffise54dc242007-03-29 18:01:04 -0400135struct audit_aux_data_pids {
136 struct audit_aux_data d;
137 pid_t target_pid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500138 uid_t target_auid[AUDIT_AUX_PIDS];
139 uid_t target_uid[AUDIT_AUX_PIDS];
Eric Paris4746ec52008-01-08 10:06:53 -0500140 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400141 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500142 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400143 int pid_count;
144};
145
Eric Paris3fc689e2008-11-11 21:48:18 +1100146struct audit_aux_data_bprm_fcaps {
147 struct audit_aux_data d;
148 struct audit_cap_data fcap;
149 unsigned int fcap_ver;
150 struct audit_cap_data old_pcap;
151 struct audit_cap_data new_pcap;
152};
153
Eric Parise68b75a02008-11-11 21:48:22 +1100154struct audit_aux_data_capset {
155 struct audit_aux_data d;
156 pid_t pid;
157 struct audit_cap_data cap;
158};
159
Al Viro74c3cbe2007-07-22 08:04:18 -0400160struct audit_tree_refs {
161 struct audit_tree_refs *next;
162 struct audit_chunk *c[31];
163};
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* The per-task audit context. */
166struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400167 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int in_syscall; /* 1 if task is in a syscall */
Al Viro0590b932008-12-14 23:45:27 -0500169 enum audit_state state, current_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int serial; /* serial number for record */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 int major; /* syscall number */
Eric Paris44e51a12009-08-07 16:54:29 -0400172 struct timespec ctime; /* time of syscall entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned long argv[4]; /* syscall arguments */
2fd6f582005-04-29 16:08:28 +0100174 long return_code;/* syscall return code */
Al Viro0590b932008-12-14 23:45:27 -0500175 u64 prio;
Eric Paris44e51a12009-08-07 16:54:29 -0400176 int return_valid; /* return code is valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int name_count;
178 struct audit_names names[AUDIT_NAMES];
Amy Griffis5adc8a62006-06-14 18:45:21 -0400179 char * filterkey; /* key for rule that triggered record */
Jan Blunck44707fd2008-02-14 19:38:33 -0800180 struct path pwd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct audit_context *previous; /* For nested syscalls */
182 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400183 struct audit_aux_data *aux_pids;
Al Viro4f6b4342008-12-09 19:50:34 -0500184 struct sockaddr_storage *sockaddr;
185 size_t sockaddr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400187 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 uid_t uid, euid, suid, fsuid;
189 gid_t gid, egid, sgid, fsgid;
190 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100191 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Al Viroa5cb0132007-03-20 13:58:35 -0400193 pid_t target_pid;
Eric Parisc2a77802008-01-07 13:40:17 -0500194 uid_t target_auid;
195 uid_t target_uid;
Eric Paris4746ec52008-01-08 10:06:53 -0500196 unsigned int target_sessionid;
Al Viroa5cb0132007-03-20 13:58:35 -0400197 u32 target_sid;
Eric Parisc2a77802008-01-07 13:40:17 -0500198 char target_comm[TASK_COMM_LEN];
Al Viroa5cb0132007-03-20 13:58:35 -0400199
Al Viro74c3cbe2007-07-22 08:04:18 -0400200 struct audit_tree_refs *trees, *first_trees;
Al Viro916d7572009-06-24 00:02:38 -0400201 struct list_head killed_trees;
Eric Paris44e51a12009-08-07 16:54:29 -0400202 int tree_count;
Al Viro74c3cbe2007-07-22 08:04:18 -0400203
Al Virof3298dc2008-12-10 03:16:51 -0500204 int type;
205 union {
206 struct {
207 int nargs;
208 long args[6];
209 } socketcall;
Al Viroa33e6752008-12-10 03:40:06 -0500210 struct {
211 uid_t uid;
212 gid_t gid;
Al Viro2570ebb2011-07-27 14:03:22 -0400213 umode_t mode;
Al Viroa33e6752008-12-10 03:40:06 -0500214 u32 osid;
Al Viroe816f372008-12-10 03:47:15 -0500215 int has_perm;
216 uid_t perm_uid;
217 gid_t perm_gid;
Al Viro2570ebb2011-07-27 14:03:22 -0400218 umode_t perm_mode;
Al Viroe816f372008-12-10 03:47:15 -0500219 unsigned long qbytes;
Al Viroa33e6752008-12-10 03:40:06 -0500220 } ipc;
Al Viro73929062008-12-10 06:58:59 -0500221 struct {
222 mqd_t mqdes;
223 struct mq_attr mqstat;
224 } mq_getsetattr;
Al Viro20114f72008-12-10 07:16:12 -0500225 struct {
226 mqd_t mqdes;
227 int sigev_signo;
228 } mq_notify;
Al Viroc32c8af2008-12-14 03:46:48 -0500229 struct {
230 mqd_t mqdes;
231 size_t msg_len;
232 unsigned int msg_prio;
233 struct timespec abs_timeout;
234 } mq_sendrecv;
Al Viro564f6992008-12-14 04:02:26 -0500235 struct {
236 int oflag;
Al Virodf0a4282011-07-26 05:26:10 -0400237 umode_t mode;
Al Viro564f6992008-12-14 04:02:26 -0500238 struct mq_attr attr;
239 } mq_open;
Al Viro57f71a02009-01-04 14:52:57 -0500240 struct {
241 pid_t pid;
242 struct audit_cap_data cap;
243 } capset;
Al Viro120a7952010-10-30 02:54:44 -0400244 struct {
245 int fd;
246 int flags;
247 } mmap;
Al Virof3298dc2008-12-10 03:16:51 -0500248 };
Al Viro157cf642008-12-14 04:57:47 -0500249 int fds[2];
Al Virof3298dc2008-12-10 03:16:51 -0500250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251#if AUDIT_DEBUG
252 int put_count;
253 int ino_count;
254#endif
255};
256
Al Viro55669bf2006-08-31 19:26:40 -0400257static inline int open_arg(int flags, int mask)
258{
259 int n = ACC_MODE(flags);
260 if (flags & (O_TRUNC | O_CREAT))
261 n |= AUDIT_PERM_WRITE;
262 return n & mask;
263}
264
265static int audit_match_perm(struct audit_context *ctx, int mask)
266{
Cordeliac4bacef2008-08-18 09:45:51 -0700267 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800268 if (unlikely(!ctx))
269 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700270 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100271
Al Viro55669bf2006-08-31 19:26:40 -0400272 switch (audit_classify_syscall(ctx->arch, n)) {
273 case 0: /* native */
274 if ((mask & AUDIT_PERM_WRITE) &&
275 audit_match_class(AUDIT_CLASS_WRITE, n))
276 return 1;
277 if ((mask & AUDIT_PERM_READ) &&
278 audit_match_class(AUDIT_CLASS_READ, n))
279 return 1;
280 if ((mask & AUDIT_PERM_ATTR) &&
281 audit_match_class(AUDIT_CLASS_CHATTR, n))
282 return 1;
283 return 0;
284 case 1: /* 32bit on biarch */
285 if ((mask & AUDIT_PERM_WRITE) &&
286 audit_match_class(AUDIT_CLASS_WRITE_32, n))
287 return 1;
288 if ((mask & AUDIT_PERM_READ) &&
289 audit_match_class(AUDIT_CLASS_READ_32, n))
290 return 1;
291 if ((mask & AUDIT_PERM_ATTR) &&
292 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
293 return 1;
294 return 0;
295 case 2: /* open */
296 return mask & ACC_MODE(ctx->argv[1]);
297 case 3: /* openat */
298 return mask & ACC_MODE(ctx->argv[2]);
299 case 4: /* socketcall */
300 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
301 case 5: /* execve */
302 return mask & AUDIT_PERM_EXEC;
303 default:
304 return 0;
305 }
306}
307
Eric Paris5ef30ee2012-01-03 14:23:05 -0500308static int audit_match_filetype(struct audit_context *ctx, int val)
Al Viro8b67dca2008-04-28 04:15:49 -0400309{
Eric Paris5ef30ee2012-01-03 14:23:05 -0500310 int index;
311 umode_t mode = (umode_t)val;
zhangxiliang1a61c882008-08-02 10:56:37 +0800312
313 if (unlikely(!ctx))
314 return 0;
315
Eric Paris5ef30ee2012-01-03 14:23:05 -0500316 for (index = 0; index < ctx->name_count; index++) {
317 if ((ctx->names[index].ino != -1) &&
318 ((ctx->names[index].mode & S_IFMT) == mode))
319 return 1;
320 }
321 return 0;
Al Viro8b67dca2008-04-28 04:15:49 -0400322}
323
Al Viro74c3cbe2007-07-22 08:04:18 -0400324/*
325 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
326 * ->first_trees points to its beginning, ->trees - to the current end of data.
327 * ->tree_count is the number of free entries in array pointed to by ->trees.
328 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
329 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
330 * it's going to remain 1-element for almost any setup) until we free context itself.
331 * References in it _are_ dropped - at the same time we free/drop aux stuff.
332 */
333
334#ifdef CONFIG_AUDIT_TREE
Eric Paris679173b2009-01-26 18:09:45 -0500335static void audit_set_auditable(struct audit_context *ctx)
336{
337 if (!ctx->prio) {
338 ctx->prio = 1;
339 ctx->current_state = AUDIT_RECORD_CONTEXT;
340 }
341}
342
Al Viro74c3cbe2007-07-22 08:04:18 -0400343static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
344{
345 struct audit_tree_refs *p = ctx->trees;
346 int left = ctx->tree_count;
347 if (likely(left)) {
348 p->c[--left] = chunk;
349 ctx->tree_count = left;
350 return 1;
351 }
352 if (!p)
353 return 0;
354 p = p->next;
355 if (p) {
356 p->c[30] = chunk;
357 ctx->trees = p;
358 ctx->tree_count = 30;
359 return 1;
360 }
361 return 0;
362}
363
364static int grow_tree_refs(struct audit_context *ctx)
365{
366 struct audit_tree_refs *p = ctx->trees;
367 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
368 if (!ctx->trees) {
369 ctx->trees = p;
370 return 0;
371 }
372 if (p)
373 p->next = ctx->trees;
374 else
375 ctx->first_trees = ctx->trees;
376 ctx->tree_count = 31;
377 return 1;
378}
379#endif
380
381static void unroll_tree_refs(struct audit_context *ctx,
382 struct audit_tree_refs *p, int count)
383{
384#ifdef CONFIG_AUDIT_TREE
385 struct audit_tree_refs *q;
386 int n;
387 if (!p) {
388 /* we started with empty chain */
389 p = ctx->first_trees;
390 count = 31;
391 /* if the very first allocation has failed, nothing to do */
392 if (!p)
393 return;
394 }
395 n = count;
396 for (q = p; q != ctx->trees; q = q->next, n = 31) {
397 while (n--) {
398 audit_put_chunk(q->c[n]);
399 q->c[n] = NULL;
400 }
401 }
402 while (n-- > ctx->tree_count) {
403 audit_put_chunk(q->c[n]);
404 q->c[n] = NULL;
405 }
406 ctx->trees = p;
407 ctx->tree_count = count;
408#endif
409}
410
411static void free_tree_refs(struct audit_context *ctx)
412{
413 struct audit_tree_refs *p, *q;
414 for (p = ctx->first_trees; p; p = q) {
415 q = p->next;
416 kfree(p);
417 }
418}
419
420static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
421{
422#ifdef CONFIG_AUDIT_TREE
423 struct audit_tree_refs *p;
424 int n;
425 if (!tree)
426 return 0;
427 /* full ones */
428 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
429 for (n = 0; n < 31; n++)
430 if (audit_tree_match(p->c[n], tree))
431 return 1;
432 }
433 /* partial */
434 if (p) {
435 for (n = ctx->tree_count; n < 31; n++)
436 if (audit_tree_match(p->c[n], tree))
437 return 1;
438 }
439#endif
440 return 0;
441}
442
Amy Griffisf368c07d2006-04-07 16:55:56 -0400443/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/* Compare a task_struct with an audit_rule. Return 1 on match, 0
Tony Jonesf5629882011-04-27 15:10:49 +0200445 * otherwise.
446 *
447 * If task_creation is true, this is an explicit indication that we are
448 * filtering a task rule at task creation time. This and tsk == current are
449 * the only situations where tsk->cred may be accessed without an rcu read lock.
450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500452 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400454 struct audit_names *name,
Tony Jonesf5629882011-04-27 15:10:49 +0200455 enum audit_state *state,
456 bool task_creation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Tony Jonesf5629882011-04-27 15:10:49 +0200458 const struct cred *cred;
Steve Grubb2ad312d2006-04-11 08:50:56 -0400459 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600460 u32 sid;
461
Tony Jonesf5629882011-04-27 15:10:49 +0200462 cred = rcu_dereference_check(tsk->cred, tsk == current || task_creation);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500465 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int result = 0;
467
Amy Griffis93315ed2006-02-07 12:05:27 -0500468 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500470 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
Al Viro3c662512006-05-06 08:26:27 -0400472 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400473 if (ctx) {
474 if (!ctx->ppid)
475 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400476 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400477 }
Al Viro3c662512006-05-06 08:26:27 -0400478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 case AUDIT_UID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100480 result = audit_comparator(cred->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 break;
482 case AUDIT_EUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100483 result = audit_comparator(cred->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 case AUDIT_SUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100486 result = audit_comparator(cred->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 break;
488 case AUDIT_FSUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100489 result = audit_comparator(cred->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
491 case AUDIT_GID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100492 result = audit_comparator(cred->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494 case AUDIT_EGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100495 result = audit_comparator(cred->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 break;
497 case AUDIT_SGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100498 result = audit_comparator(cred->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 break;
500 case AUDIT_FSGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100501 result = audit_comparator(cred->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500504 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
2fd6f582005-04-29 16:08:28 +0100506 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700507 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500508 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 case AUDIT_EXIT:
512 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500513 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100516 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500517 if (f->val)
518 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100519 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500520 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400524 if (name)
525 result = audit_comparator(MAJOR(name->dev),
526 f->op, f->val);
527 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500529 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ++result;
531 break;
532 }
533 }
534 }
535 break;
536 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400537 if (name)
538 result = audit_comparator(MINOR(name->dev),
539 f->op, f->val);
540 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500542 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ++result;
544 break;
545 }
546 }
547 }
548 break;
549 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400550 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400551 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400552 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400554 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ++result;
556 break;
557 }
558 }
559 }
560 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400561 case AUDIT_WATCH:
Eric Parisae7b8f42009-12-17 20:12:04 -0500562 if (name)
563 result = audit_watch_compare(rule->watch, name->ino, name->dev);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400564 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400565 case AUDIT_DIR:
566 if (ctx)
567 result = match_tree_refs(ctx, rule->tree);
568 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 case AUDIT_LOGINUID:
570 result = 0;
571 if (ctx)
Al Virobfef93a2008-01-10 04:53:18 -0500572 result = audit_comparator(tsk->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500574 case AUDIT_SUBJ_USER:
575 case AUDIT_SUBJ_ROLE:
576 case AUDIT_SUBJ_TYPE:
577 case AUDIT_SUBJ_SEN:
578 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600579 /* NOTE: this may return negative values indicating
580 a temporary error. We simply treat this as a
581 match for now to avoid losing information that
582 may be wanted. An error message will also be
583 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000584 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400585 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200586 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400587 need_sid = 0;
588 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200589 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600590 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000591 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600592 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400593 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600594 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500595 case AUDIT_OBJ_USER:
596 case AUDIT_OBJ_ROLE:
597 case AUDIT_OBJ_TYPE:
598 case AUDIT_OBJ_LEV_LOW:
599 case AUDIT_OBJ_LEV_HIGH:
600 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
601 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000602 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500603 /* Find files that match */
604 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200605 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500606 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000607 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500608 } else if (ctx) {
609 for (j = 0; j < ctx->name_count; j++) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200610 if (security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500611 ctx->names[j].osid,
612 f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000613 f->lsm_rule, ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500614 ++result;
615 break;
616 }
617 }
618 }
619 /* Find ipc objects that match */
Al Viroa33e6752008-12-10 03:40:06 -0500620 if (!ctx || ctx->type != AUDIT_IPC)
621 break;
622 if (security_audit_rule_match(ctx->ipc.osid,
623 f->type, f->op,
624 f->lsm_rule, ctx))
625 ++result;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500626 }
627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 case AUDIT_ARG0:
629 case AUDIT_ARG1:
630 case AUDIT_ARG2:
631 case AUDIT_ARG3:
632 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500633 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400635 case AUDIT_FILTERKEY:
636 /* ignore this field for filtering */
637 result = 1;
638 break;
Al Viro55669bf2006-08-31 19:26:40 -0400639 case AUDIT_PERM:
640 result = audit_match_perm(ctx, f->val);
641 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400642 case AUDIT_FILETYPE:
643 result = audit_match_filetype(ctx, f->val);
644 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
Tony Jonesf5629882011-04-27 15:10:49 +0200647 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return 0;
649 }
Al Viro0590b932008-12-14 23:45:27 -0500650
651 if (ctx) {
652 if (rule->prio <= ctx->prio)
653 return 0;
654 if (rule->filterkey) {
655 kfree(ctx->filterkey);
656 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
657 }
658 ctx->prio = rule->prio;
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 switch (rule->action) {
661 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
663 }
664 return 1;
665}
666
667/* At process creation time, we can determine if system-call auditing is
668 * completely disabled for this task. Since we only have the task
669 * structure at this point, we can only check uid and gid.
670 */
Al Viroe048e022008-12-16 03:51:22 -0500671static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct audit_entry *e;
674 enum audit_state state;
675
676 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100677 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Tony Jonesf5629882011-04-27 15:10:49 +0200678 if (audit_filter_rules(tsk, &e->rule, NULL, NULL,
679 &state, true)) {
Al Viroe048e022008-12-16 03:51:22 -0500680 if (state == AUDIT_RECORD_CONTEXT)
681 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 rcu_read_unlock();
683 return state;
684 }
685 }
686 rcu_read_unlock();
687 return AUDIT_BUILD_CONTEXT;
688}
689
690/* At syscall entry and exit time, this filter is called if the
691 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100692 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700693 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
695static enum audit_state audit_filter_syscall(struct task_struct *tsk,
696 struct audit_context *ctx,
697 struct list_head *list)
698{
699 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100700 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
David Woodhouse351bb722005-07-14 14:40:06 +0100702 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100703 return AUDIT_DISABLED;
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100706 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000707 int word = AUDIT_WORD(ctx->major);
708 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100709
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000710 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400711 if ((e->rule.mask[word] & bit) == bit &&
712 audit_filter_rules(tsk, &e->rule, ctx, NULL,
Tony Jonesf5629882011-04-27 15:10:49 +0200713 &state, false)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000714 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500715 ctx->current_state = state;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000716 return state;
717 }
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 rcu_read_unlock();
721 return AUDIT_BUILD_CONTEXT;
722}
723
Amy Griffisf368c07d2006-04-07 16:55:56 -0400724/* At syscall exit time, this filter is called if any audit_names[] have been
725 * collected during syscall processing. We only check rules in sublists at hash
726 * buckets applicable to the inode numbers in audit_names[].
727 * Regarding audit_state, same rules apply as for audit_filter_syscall().
728 */
Al Viro0590b932008-12-14 23:45:27 -0500729void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400730{
731 int i;
732 struct audit_entry *e;
733 enum audit_state state;
734
735 if (audit_pid && tsk->tgid == audit_pid)
Al Viro0590b932008-12-14 23:45:27 -0500736 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400737
738 rcu_read_lock();
739 for (i = 0; i < ctx->name_count; i++) {
740 int word = AUDIT_WORD(ctx->major);
741 int bit = AUDIT_BIT(ctx->major);
742 struct audit_names *n = &ctx->names[i];
743 int h = audit_hash_ino((u32)n->ino);
744 struct list_head *list = &audit_inode_hash[h];
745
746 if (list_empty(list))
747 continue;
748
749 list_for_each_entry_rcu(e, list, list) {
750 if ((e->rule.mask[word] & bit) == bit &&
Tony Jonesf5629882011-04-27 15:10:49 +0200751 audit_filter_rules(tsk, &e->rule, ctx, n,
752 &state, false)) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400753 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500754 ctx->current_state = state;
755 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400756 }
757 }
758 }
759 rcu_read_unlock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762static inline struct audit_context *audit_get_context(struct task_struct *tsk,
763 int return_valid,
Paul Moore6d208da2009-04-01 15:47:27 -0400764 long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct audit_context *context = tsk->audit_context;
767
768 if (likely(!context))
769 return NULL;
770 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500771
772 /*
773 * we need to fix up the return code in the audit logs if the actual
774 * return codes are later going to be fixed up by the arch specific
775 * signal handlers
776 *
777 * This is actually a test for:
778 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
779 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
780 *
781 * but is faster than a bunch of ||
782 */
783 if (unlikely(return_code <= -ERESTARTSYS) &&
784 (return_code >= -ERESTART_RESTARTBLOCK) &&
785 (return_code != -ENOIOCTLCMD))
786 context->return_code = -EINTR;
787 else
788 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Al Viro0590b932008-12-14 23:45:27 -0500790 if (context->in_syscall && !context->dummy) {
791 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
792 audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 tsk->audit_context = NULL;
796 return context;
797}
798
799static inline void audit_free_names(struct audit_context *context)
800{
801 int i;
802
803#if AUDIT_DEBUG == 2
Al Viro0590b932008-12-14 23:45:27 -0500804 if (context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000805 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 " name_count=%d put_count=%d"
807 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000808 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 context->serial, context->major, context->in_syscall,
810 context->name_count, context->put_count,
811 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000812 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 printk(KERN_ERR "names[%d] = %p = %s\n", i,
814 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000815 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 dump_stack();
818 return;
819 }
820#endif
821#if AUDIT_DEBUG
822 context->put_count = 0;
823 context->ino_count = 0;
824#endif
825
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000826 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400827 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -0800831 path_put(&context->pwd);
832 context->pwd.dentry = NULL;
833 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836static inline void audit_free_aux(struct audit_context *context)
837{
838 struct audit_aux_data *aux;
839
840 while ((aux = context->aux)) {
841 context->aux = aux->next;
842 kfree(aux);
843 }
Amy Griffise54dc242007-03-29 18:01:04 -0400844 while ((aux = context->aux_pids)) {
845 context->aux_pids = aux->next;
846 kfree(aux);
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850static inline void audit_zero_context(struct audit_context *context,
851 enum audit_state state)
852{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 memset(context, 0, sizeof(*context));
854 context->state = state;
Al Viro0590b932008-12-14 23:45:27 -0500855 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858static inline struct audit_context *audit_alloc_context(enum audit_state state)
859{
860 struct audit_context *context;
861
862 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
863 return NULL;
864 audit_zero_context(context, state);
Al Viro916d7572009-06-24 00:02:38 -0400865 INIT_LIST_HEAD(&context->killed_trees);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return context;
867}
868
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700869/**
870 * audit_alloc - allocate an audit context block for a task
871 * @tsk: task
872 *
873 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * if necessary. Doing so turns on system call auditing for the
875 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700876 * needed.
877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878int audit_alloc(struct task_struct *tsk)
879{
880 struct audit_context *context;
881 enum audit_state state;
Al Viroe048e022008-12-16 03:51:22 -0500882 char *key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Eric Parisb593d382008-01-08 17:38:31 -0500884 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0; /* Return if not auditing. */
886
Al Viroe048e022008-12-16 03:51:22 -0500887 state = audit_filter_task(tsk, &key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (likely(state == AUDIT_DISABLED))
889 return 0;
890
891 if (!(context = audit_alloc_context(state))) {
Al Viroe048e022008-12-16 03:51:22 -0500892 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 audit_log_lost("out of memory in audit_alloc");
894 return -ENOMEM;
895 }
Al Viroe048e022008-12-16 03:51:22 -0500896 context->filterkey = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 tsk->audit_context = context;
899 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
900 return 0;
901}
902
903static inline void audit_free_context(struct audit_context *context)
904{
905 struct audit_context *previous;
906 int count = 0;
907
908 do {
909 previous = context->previous;
910 if (previous || (count && count < 10)) {
911 ++count;
912 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
913 " freeing multiple contexts (%d)\n",
914 context->serial, context->major,
915 context->name_count, count);
916 }
917 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -0400918 unroll_tree_refs(context, NULL, 0);
919 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400921 kfree(context->filterkey);
Al Viro4f6b4342008-12-09 19:50:34 -0500922 kfree(context->sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 kfree(context);
924 context = previous;
925 } while (context);
926 if (count >= 10)
927 printk(KERN_ERR "audit: freed %d contexts\n", count);
928}
929
Joy Latten161a09e2006-11-27 13:11:54 -0600930void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000931{
932 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000933 unsigned len;
934 int error;
935 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000936
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200937 security_task_getsecid(current, &sid);
Al Viroc4823bc2007-03-12 16:17:42 +0000938 if (!sid)
939 return;
940
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200941 error = security_secid_to_secctx(sid, &ctx, &len);
Al Viroc4823bc2007-03-12 16:17:42 +0000942 if (error) {
943 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000944 goto error_path;
945 return;
946 }
947
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000948 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200949 security_release_secctx(ctx, len);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000950 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000951
952error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000953 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000954 return;
955}
956
Joy Latten161a09e2006-11-27 13:11:54 -0600957EXPORT_SYMBOL(audit_log_task_context);
958
Al Viroe4951492006-03-29 20:17:10 -0500959static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700960{
Al Viro45d9bb02006-03-29 20:02:55 -0500961 char name[sizeof(tsk->comm)];
962 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700963 struct vm_area_struct *vma;
964
Al Viroe4951492006-03-29 20:17:10 -0500965 /* tsk == current */
966
Al Viro45d9bb02006-03-29 20:02:55 -0500967 get_task_comm(name, tsk);
David Woodhouse99e45eea2005-05-23 21:57:41 +0100968 audit_log_format(ab, " comm=");
969 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700970
Al Viroe4951492006-03-29 20:17:10 -0500971 if (mm) {
972 down_read(&mm->mmap_sem);
973 vma = mm->mmap;
974 while (vma) {
975 if ((vma->vm_flags & VM_EXECUTABLE) &&
976 vma->vm_file) {
977 audit_log_d_path(ab, "exe=",
Jan Blunck44707fd2008-02-14 19:38:33 -0800978 &vma->vm_file->f_path);
Al Viroe4951492006-03-29 20:17:10 -0500979 break;
980 }
981 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700982 }
Al Viroe4951492006-03-29 20:17:10 -0500983 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700984 }
Al Viroe4951492006-03-29 20:17:10 -0500985 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700986}
987
Amy Griffise54dc242007-03-29 18:01:04 -0400988static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric Paris4746ec52008-01-08 10:06:53 -0500989 uid_t auid, uid_t uid, unsigned int sessionid,
990 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400991{
992 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200993 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -0400994 u32 len;
995 int rc = 0;
996
997 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
998 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -0500999 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -04001000
Eric Paris4746ec52008-01-08 10:06:53 -05001001 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
1002 uid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001003 if (security_secid_to_secctx(sid, &ctx, &len)) {
Eric Parisc2a77802008-01-07 13:40:17 -05001004 audit_log_format(ab, " obj=(none)");
Amy Griffise54dc242007-03-29 18:01:04 -04001005 rc = 1;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001006 } else {
1007 audit_log_format(ab, " obj=%s", ctx);
1008 security_release_secctx(ctx, len);
1009 }
Eric Parisc2a77802008-01-07 13:40:17 -05001010 audit_log_format(ab, " ocomm=");
1011 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -04001012 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -04001013
1014 return rc;
1015}
1016
Eric Parisde6bbd12008-01-07 14:31:58 -05001017/*
1018 * to_send and len_sent accounting are very loose estimates. We aren't
1019 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001020 * within about 500 bytes (next page boundary)
Eric Parisde6bbd12008-01-07 14:31:58 -05001021 *
1022 * why snprintf? an int is up to 12 digits long. if we just assumed when
1023 * logging that a[%d]= was going to be 16 characters long we would be wasting
1024 * space in every audit message. In one 7500 byte message we can log up to
1025 * about 1000 min size arguments. That comes down to about 50% waste of space
1026 * if we didn't do the snprintf to find out how long arg_num_len was.
1027 */
1028static int audit_log_single_execve_arg(struct audit_context *context,
1029 struct audit_buffer **ab,
1030 int arg_num,
1031 size_t *len_sent,
1032 const char __user *p,
1033 char *buf)
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001034{
Eric Parisde6bbd12008-01-07 14:31:58 -05001035 char arg_num_len_buf[12];
1036 const char __user *tmp_p = p;
Eric Parisb87ce6e2009-06-11 14:31:34 -04001037 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1038 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
Eric Parisde6bbd12008-01-07 14:31:58 -05001039 size_t len, len_left, to_send;
1040 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1041 unsigned int i, has_cntl = 0, too_long = 0;
1042 int ret;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001043
Eric Parisde6bbd12008-01-07 14:31:58 -05001044 /* strnlen_user includes the null we don't want to send */
1045 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001046
Eric Parisde6bbd12008-01-07 14:31:58 -05001047 /*
1048 * We just created this mm, if we can't find the strings
1049 * we just copied into it something is _very_ wrong. Similar
1050 * for strings that are too long, we should not have created
1051 * any.
1052 */
Eric Parisb0abcfc2008-02-18 18:23:16 -05001053 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001054 WARN_ON(1);
1055 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001056 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001057 }
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001058
Eric Parisde6bbd12008-01-07 14:31:58 -05001059 /* walk the whole argument looking for non-ascii chars */
1060 do {
1061 if (len_left > MAX_EXECVE_AUDIT_LEN)
1062 to_send = MAX_EXECVE_AUDIT_LEN;
1063 else
1064 to_send = len_left;
1065 ret = copy_from_user(buf, tmp_p, to_send);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001066 /*
1067 * There is no reason for this copy to be short. We just
1068 * copied them here, and the mm hasn't been exposed to user-
1069 * space yet.
1070 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001071 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001072 WARN_ON(1);
1073 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001074 return -1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001075 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001076 buf[to_send] = '\0';
1077 has_cntl = audit_string_contains_control(buf, to_send);
1078 if (has_cntl) {
1079 /*
1080 * hex messages get logged as 2 bytes, so we can only
1081 * send half as much in each message
1082 */
1083 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1084 break;
1085 }
1086 len_left -= to_send;
1087 tmp_p += to_send;
1088 } while (len_left > 0);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001089
Eric Parisde6bbd12008-01-07 14:31:58 -05001090 len_left = len;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001091
Eric Parisde6bbd12008-01-07 14:31:58 -05001092 if (len > max_execve_audit_len)
1093 too_long = 1;
1094
1095 /* rewalk the argument actually logging the message */
1096 for (i = 0; len_left > 0; i++) {
1097 int room_left;
1098
1099 if (len_left > max_execve_audit_len)
1100 to_send = max_execve_audit_len;
1101 else
1102 to_send = len_left;
1103
1104 /* do we have space left to send this argument in this ab? */
1105 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1106 if (has_cntl)
1107 room_left -= (to_send * 2);
1108 else
1109 room_left -= to_send;
1110 if (room_left < 0) {
1111 *len_sent = 0;
1112 audit_log_end(*ab);
1113 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1114 if (!*ab)
1115 return 0;
1116 }
1117
1118 /*
1119 * first record needs to say how long the original string was
1120 * so we can be sure nothing was lost.
1121 */
1122 if ((i == 0) && (too_long))
Jiri Pirkoca96a892009-01-09 16:44:16 +01001123 audit_log_format(*ab, " a%d_len=%zu", arg_num,
Eric Parisde6bbd12008-01-07 14:31:58 -05001124 has_cntl ? 2*len : len);
1125
1126 /*
1127 * normally arguments are small enough to fit and we already
1128 * filled buf above when we checked for control characters
1129 * so don't bother with another copy_from_user
1130 */
1131 if (len >= max_execve_audit_len)
1132 ret = copy_from_user(buf, p, to_send);
1133 else
1134 ret = 0;
1135 if (ret) {
1136 WARN_ON(1);
1137 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001138 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001139 }
1140 buf[to_send] = '\0';
1141
1142 /* actually log it */
Jiri Pirkoca96a892009-01-09 16:44:16 +01001143 audit_log_format(*ab, " a%d", arg_num);
Eric Parisde6bbd12008-01-07 14:31:58 -05001144 if (too_long)
1145 audit_log_format(*ab, "[%d]", i);
1146 audit_log_format(*ab, "=");
1147 if (has_cntl)
Eric Parisb556f8a2008-04-18 10:12:59 -04001148 audit_log_n_hex(*ab, buf, to_send);
Eric Parisde6bbd12008-01-07 14:31:58 -05001149 else
Eric Paris9d960982009-06-11 14:31:37 -04001150 audit_log_string(*ab, buf);
Eric Parisde6bbd12008-01-07 14:31:58 -05001151
1152 p += to_send;
1153 len_left -= to_send;
1154 *len_sent += arg_num_len;
1155 if (has_cntl)
1156 *len_sent += to_send * 2;
1157 else
1158 *len_sent += to_send;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001159 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001160 /* include the null we didn't log */
1161 return len + 1;
1162}
1163
1164static void audit_log_execve_info(struct audit_context *context,
1165 struct audit_buffer **ab,
1166 struct audit_aux_data_execve *axi)
1167{
1168 int i;
1169 size_t len, len_sent = 0;
1170 const char __user *p;
1171 char *buf;
1172
1173 if (axi->mm != current->mm)
1174 return; /* execve failed, no additional info */
1175
1176 p = (const char __user *)axi->mm->arg_start;
1177
Jiri Pirkoca96a892009-01-09 16:44:16 +01001178 audit_log_format(*ab, "argc=%d", axi->argc);
Eric Parisde6bbd12008-01-07 14:31:58 -05001179
1180 /*
1181 * we need some kernel buffer to hold the userspace args. Just
1182 * allocate one big one rather than allocating one of the right size
1183 * for every single argument inside audit_log_single_execve_arg()
1184 * should be <8k allocation so should be pretty safe.
1185 */
1186 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1187 if (!buf) {
1188 audit_panic("out of memory for argv string\n");
1189 return;
1190 }
1191
1192 for (i = 0; i < axi->argc; i++) {
1193 len = audit_log_single_execve_arg(context, ab, i,
1194 &len_sent, p, buf);
1195 if (len <= 0)
1196 break;
1197 p += len;
1198 }
1199 kfree(buf);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001200}
1201
Eric Paris851f7ff2008-11-11 21:48:14 +11001202static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1203{
1204 int i;
1205
1206 audit_log_format(ab, " %s=", prefix);
1207 CAP_FOR_EACH_U32(i) {
1208 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1209 }
1210}
1211
1212static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1213{
1214 kernel_cap_t *perm = &name->fcap.permitted;
1215 kernel_cap_t *inh = &name->fcap.inheritable;
1216 int log = 0;
1217
1218 if (!cap_isclear(*perm)) {
1219 audit_log_cap(ab, "cap_fp", perm);
1220 log = 1;
1221 }
1222 if (!cap_isclear(*inh)) {
1223 audit_log_cap(ab, "cap_fi", inh);
1224 log = 1;
1225 }
1226
1227 if (log)
1228 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1229}
1230
Al Viroa33e6752008-12-10 03:40:06 -05001231static void show_special(struct audit_context *context, int *call_panic)
Al Virof3298dc2008-12-10 03:16:51 -05001232{
1233 struct audit_buffer *ab;
1234 int i;
1235
1236 ab = audit_log_start(context, GFP_KERNEL, context->type);
1237 if (!ab)
1238 return;
1239
1240 switch (context->type) {
1241 case AUDIT_SOCKETCALL: {
1242 int nargs = context->socketcall.nargs;
1243 audit_log_format(ab, "nargs=%d", nargs);
1244 for (i = 0; i < nargs; i++)
1245 audit_log_format(ab, " a%d=%lx", i,
1246 context->socketcall.args[i]);
1247 break; }
Al Viroa33e6752008-12-10 03:40:06 -05001248 case AUDIT_IPC: {
1249 u32 osid = context->ipc.osid;
1250
Al Viro2570ebb2011-07-27 14:03:22 -04001251 audit_log_format(ab, "ouid=%u ogid=%u mode=%#ho",
Al Viroa33e6752008-12-10 03:40:06 -05001252 context->ipc.uid, context->ipc.gid, context->ipc.mode);
1253 if (osid) {
1254 char *ctx = NULL;
1255 u32 len;
1256 if (security_secid_to_secctx(osid, &ctx, &len)) {
1257 audit_log_format(ab, " osid=%u", osid);
1258 *call_panic = 1;
1259 } else {
1260 audit_log_format(ab, " obj=%s", ctx);
1261 security_release_secctx(ctx, len);
1262 }
1263 }
Al Viroe816f372008-12-10 03:47:15 -05001264 if (context->ipc.has_perm) {
1265 audit_log_end(ab);
1266 ab = audit_log_start(context, GFP_KERNEL,
1267 AUDIT_IPC_SET_PERM);
1268 audit_log_format(ab,
Al Viro2570ebb2011-07-27 14:03:22 -04001269 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
Al Viroe816f372008-12-10 03:47:15 -05001270 context->ipc.qbytes,
1271 context->ipc.perm_uid,
1272 context->ipc.perm_gid,
1273 context->ipc.perm_mode);
1274 if (!ab)
1275 return;
1276 }
Al Viroa33e6752008-12-10 03:40:06 -05001277 break; }
Al Viro564f6992008-12-14 04:02:26 -05001278 case AUDIT_MQ_OPEN: {
1279 audit_log_format(ab,
Al Virodf0a4282011-07-26 05:26:10 -04001280 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
Al Viro564f6992008-12-14 04:02:26 -05001281 "mq_msgsize=%ld mq_curmsgs=%ld",
1282 context->mq_open.oflag, context->mq_open.mode,
1283 context->mq_open.attr.mq_flags,
1284 context->mq_open.attr.mq_maxmsg,
1285 context->mq_open.attr.mq_msgsize,
1286 context->mq_open.attr.mq_curmsgs);
1287 break; }
Al Viroc32c8af2008-12-14 03:46:48 -05001288 case AUDIT_MQ_SENDRECV: {
1289 audit_log_format(ab,
1290 "mqdes=%d msg_len=%zd msg_prio=%u "
1291 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1292 context->mq_sendrecv.mqdes,
1293 context->mq_sendrecv.msg_len,
1294 context->mq_sendrecv.msg_prio,
1295 context->mq_sendrecv.abs_timeout.tv_sec,
1296 context->mq_sendrecv.abs_timeout.tv_nsec);
1297 break; }
Al Viro20114f72008-12-10 07:16:12 -05001298 case AUDIT_MQ_NOTIFY: {
1299 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1300 context->mq_notify.mqdes,
1301 context->mq_notify.sigev_signo);
1302 break; }
Al Viro73929062008-12-10 06:58:59 -05001303 case AUDIT_MQ_GETSETATTR: {
1304 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1305 audit_log_format(ab,
1306 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1307 "mq_curmsgs=%ld ",
1308 context->mq_getsetattr.mqdes,
1309 attr->mq_flags, attr->mq_maxmsg,
1310 attr->mq_msgsize, attr->mq_curmsgs);
1311 break; }
Al Viro57f71a02009-01-04 14:52:57 -05001312 case AUDIT_CAPSET: {
1313 audit_log_format(ab, "pid=%d", context->capset.pid);
1314 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1315 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1316 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1317 break; }
Al Viro120a7952010-10-30 02:54:44 -04001318 case AUDIT_MMAP: {
1319 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1320 context->mmap.flags);
1321 break; }
Al Virof3298dc2008-12-10 03:16:51 -05001322 }
1323 audit_log_end(ab);
1324}
1325
Al Viroe4951492006-03-29 20:17:10 -05001326static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
David Howellsc69e8d92008-11-14 10:39:19 +11001328 const struct cred *cred;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001329 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001331 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -05001332 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Al Viroe4951492006-03-29 20:17:10 -05001334 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001335 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -04001336 if (!context->ppid)
1337 context->ppid = sys_getppid();
David Howellsc69e8d92008-11-14 10:39:19 +11001338 cred = current_cred();
1339 context->uid = cred->uid;
1340 context->gid = cred->gid;
1341 context->euid = cred->euid;
1342 context->suid = cred->suid;
David Howellsb6dff3e2008-11-14 10:39:16 +11001343 context->fsuid = cred->fsuid;
David Howellsc69e8d92008-11-14 10:39:19 +11001344 context->egid = cred->egid;
1345 context->sgid = cred->sgid;
David Howellsb6dff3e2008-11-14 10:39:16 +11001346 context->fsgid = cred->fsgid;
Al Viro3f2792f2006-07-16 06:43:48 -04001347 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001348
1349 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!ab)
1351 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001352 audit_log_format(ab, "arch=%x syscall=%d",
1353 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (context->personality != PER_LINUX)
1355 audit_log_format(ab, " per=%lx", context->personality);
1356 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001357 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001358 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1359 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001360
Alan Coxdbda4c02008-10-13 10:40:53 +01001361 spin_lock_irq(&tsk->sighand->siglock);
Al Viro45d9bb02006-03-29 20:02:55 -05001362 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1363 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -05001364 else
1365 tty = "(none)";
Alan Coxdbda4c02008-10-13 10:40:53 +01001366 spin_unlock_irq(&tsk->sighand->siglock);
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 audit_log_format(ab,
1369 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -04001370 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +01001371 " euid=%u suid=%u fsuid=%u"
Eric Paris4746ec52008-01-08 10:06:53 -05001372 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 context->argv[0],
1374 context->argv[1],
1375 context->argv[2],
1376 context->argv[3],
1377 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -04001378 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 context->pid,
Al Virobfef93a2008-01-10 04:53:18 -05001380 tsk->loginuid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 context->uid,
1382 context->gid,
1383 context->euid, context->suid, context->fsuid,
Eric Paris4746ec52008-01-08 10:06:53 -05001384 context->egid, context->sgid, context->fsgid, tty,
1385 tsk->sessionid);
Alan Coxeb84a202006-09-29 02:01:41 -07001386
Alan Coxeb84a202006-09-29 02:01:41 -07001387
Al Viroe4951492006-03-29 20:17:10 -05001388 audit_log_task_info(ab, tsk);
Eric Paris9d960982009-06-11 14:31:37 -04001389 audit_log_key(ab, context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
David Woodhouse7551ced2005-05-26 12:04:57 +01001392 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001393
Al Viroe4951492006-03-29 20:17:10 -05001394 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (!ab)
1396 continue; /* audit_panic has been called */
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001399
Al Viro473ae302006-04-26 14:04:08 -04001400 case AUDIT_EXECVE: {
1401 struct audit_aux_data_execve *axi = (void *)aux;
Eric Parisde6bbd12008-01-07 14:31:58 -05001402 audit_log_execve_info(context, &ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001403 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001404
Eric Paris3fc689e2008-11-11 21:48:18 +11001405 case AUDIT_BPRM_FCAPS: {
1406 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1407 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1408 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1409 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1410 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1411 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1412 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1413 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1414 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1415 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1416 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1417 break; }
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422
Al Virof3298dc2008-12-10 03:16:51 -05001423 if (context->type)
Al Viroa33e6752008-12-10 03:40:06 -05001424 show_special(context, &call_panic);
Al Virof3298dc2008-12-10 03:16:51 -05001425
Al Viro157cf642008-12-14 04:57:47 -05001426 if (context->fds[0] >= 0) {
1427 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1428 if (ab) {
1429 audit_log_format(ab, "fd0=%d fd1=%d",
1430 context->fds[0], context->fds[1]);
1431 audit_log_end(ab);
1432 }
1433 }
1434
Al Viro4f6b4342008-12-09 19:50:34 -05001435 if (context->sockaddr_len) {
1436 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1437 if (ab) {
1438 audit_log_format(ab, "saddr=");
1439 audit_log_n_hex(ab, (void *)context->sockaddr,
1440 context->sockaddr_len);
1441 audit_log_end(ab);
1442 }
1443 }
1444
Amy Griffise54dc242007-03-29 18:01:04 -04001445 for (aux = context->aux_pids; aux; aux = aux->next) {
1446 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001447
1448 for (i = 0; i < axs->pid_count; i++)
1449 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001450 axs->target_auid[i],
1451 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001452 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001453 axs->target_sid[i],
1454 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001455 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001456 }
1457
Amy Griffise54dc242007-03-29 18:01:04 -04001458 if (context->target_pid &&
1459 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001460 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001461 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001462 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001463 call_panic = 1;
1464
Jan Blunck44707fd2008-02-14 19:38:33 -08001465 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001466 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001467 if (ab) {
Jan Blunck44707fd2008-02-14 19:38:33 -08001468 audit_log_d_path(ab, "cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001469 audit_log_end(ab);
1470 }
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -04001473 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +00001474
Al Viroe4951492006-03-29 20:17:10 -05001475 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (!ab)
1477 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +01001478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +00001480
Amy Griffis9c937dc2006-06-08 23:19:31 -04001481 if (n->name) {
1482 switch(n->name_len) {
1483 case AUDIT_NAME_FULL:
1484 /* log the full path */
1485 audit_log_format(ab, " name=");
1486 audit_log_untrustedstring(ab, n->name);
1487 break;
1488 case 0:
1489 /* name was specified as a relative path and the
1490 * directory component is the cwd */
Eric Parisdef57542009-03-10 18:00:14 -04001491 audit_log_d_path(ab, "name=", &context->pwd);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001492 break;
1493 default:
1494 /* log the name's directory component */
1495 audit_log_format(ab, " name=");
Eric Parisb556f8a2008-04-18 10:12:59 -04001496 audit_log_n_untrustedstring(ab, n->name,
1497 n->name_len);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001498 }
1499 } else
1500 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001501
Amy Griffis9c937dc2006-06-08 23:19:31 -04001502 if (n->ino != (unsigned long)-1) {
1503 audit_log_format(ab, " inode=%lu"
Al Viro93d3a102011-07-27 14:04:25 -04001504 " dev=%02x:%02x mode=%#ho"
Amy Griffis9c937dc2006-06-08 23:19:31 -04001505 " ouid=%u ogid=%u rdev=%02x:%02x",
1506 n->ino,
1507 MAJOR(n->dev),
1508 MINOR(n->dev),
1509 n->mode,
1510 n->uid,
1511 n->gid,
1512 MAJOR(n->rdev),
1513 MINOR(n->rdev));
1514 }
1515 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001516 char *ctx = NULL;
1517 u32 len;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001518 if (security_secid_to_secctx(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001519 n->osid, &ctx, &len)) {
1520 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001521 call_panic = 2;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001522 } else {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001523 audit_log_format(ab, " obj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001524 security_release_secctx(ctx, len);
1525 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001526 }
1527
Eric Paris851f7ff2008-11-11 21:48:14 +11001528 audit_log_fcaps(ab, n);
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 audit_log_end(ab);
1531 }
Eric Parisc0641f22008-01-07 13:49:15 -05001532
1533 /* Send end of event record to help user space know we are finished */
1534 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1535 if (ab)
1536 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001537 if (call_panic)
1538 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001541/**
1542 * audit_free - free a per-task audit context
1543 * @tsk: task whose audit context block to free
1544 *
Al Virofa84cb92006-03-29 20:30:19 -05001545 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001546 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547void audit_free(struct task_struct *tsk)
1548{
1549 struct audit_context *context;
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (likely(!context))
1553 return;
1554
1555 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001556 * function (e.g., exit_group), then free context block.
1557 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001558 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001559 /* that can happen only if we are called from do_exit() */
Al Viro0590b932008-12-14 23:45:27 -05001560 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001561 audit_log_exit(context, tsk);
Al Viro916d7572009-06-24 00:02:38 -04001562 if (!list_empty(&context->killed_trees))
1563 audit_kill_trees(&context->killed_trees);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 audit_free_context(context);
1566}
1567
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001568/**
1569 * audit_syscall_entry - fill in an audit record at syscall entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001570 * @arch: architecture type
1571 * @major: major syscall type (function)
1572 * @a1: additional syscall register 1
1573 * @a2: additional syscall register 2
1574 * @a3: additional syscall register 3
1575 * @a4: additional syscall register 4
1576 *
1577 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * audit context was created when the task was created and the state or
1579 * filters demand the audit context be built. If the state from the
1580 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1581 * then the record will be written at syscall exit time (otherwise, it
1582 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001583 * be written).
1584 */
Al Viro5411be52006-03-29 20:23:36 -05001585void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 unsigned long a1, unsigned long a2,
1587 unsigned long a3, unsigned long a4)
1588{
Al Viro5411be52006-03-29 20:23:36 -05001589 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 struct audit_context *context = tsk->audit_context;
1591 enum audit_state state;
1592
Roland McGrath86a1c342008-06-23 15:37:04 -07001593 if (unlikely(!context))
1594 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001596 /*
1597 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 * calls in kernel_thread via the entry.S interface, instead of
1599 * with direct calls. (If you are porting to a new
1600 * architecture, hitting this condition can indicate that you
1601 * got the _exit/_leave calls backward in entry.S.)
1602 *
1603 * i386 no
1604 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001605 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 *
1607 * This also happens with vm86 emulation in a non-nested manner
1608 * (entries without exits), so this case must be caught.
1609 */
1610 if (context->in_syscall) {
1611 struct audit_context *newctx;
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613#if AUDIT_DEBUG
1614 printk(KERN_ERR
1615 "audit(:%d) pid=%d in syscall=%d;"
1616 " entering syscall=%d\n",
1617 context->serial, tsk->pid, context->major, major);
1618#endif
1619 newctx = audit_alloc_context(context->state);
1620 if (newctx) {
1621 newctx->previous = context;
1622 context = newctx;
1623 tsk->audit_context = newctx;
1624 } else {
1625 /* If we can't alloc a new context, the best we
1626 * can do is to leak memory (any pending putname
1627 * will be lost). The only other alternative is
1628 * to abandon auditing. */
1629 audit_zero_context(context, context->state);
1630 }
1631 }
1632 BUG_ON(context->in_syscall || context->name_count);
1633
1634 if (!audit_enabled)
1635 return;
1636
2fd6f582005-04-29 16:08:28 +01001637 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 context->major = major;
1639 context->argv[0] = a1;
1640 context->argv[1] = a2;
1641 context->argv[2] = a3;
1642 context->argv[3] = a4;
1643
1644 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001645 context->dummy = !audit_n_rules;
Al Viro0590b932008-12-14 23:45:27 -05001646 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1647 context->prio = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001648 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Al Viro0590b932008-12-14 23:45:27 -05001649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (likely(state == AUDIT_DISABLED))
1651 return;
1652
David Woodhousece625a82005-07-18 14:24:46 -04001653 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 context->ctime = CURRENT_TIME;
1655 context->in_syscall = 1;
Al Viro0590b932008-12-14 23:45:27 -05001656 context->current_state = state;
Alexander Viro419c58f2006-09-29 00:08:50 -04001657 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Al Viroa64e6492008-11-12 18:37:41 -05001660void audit_finish_fork(struct task_struct *child)
1661{
1662 struct audit_context *ctx = current->audit_context;
1663 struct audit_context *p = child->audit_context;
Al Viro0590b932008-12-14 23:45:27 -05001664 if (!p || !ctx)
1665 return;
1666 if (!ctx->in_syscall || ctx->current_state != AUDIT_RECORD_CONTEXT)
Al Viroa64e6492008-11-12 18:37:41 -05001667 return;
1668 p->arch = ctx->arch;
1669 p->major = ctx->major;
1670 memcpy(p->argv, ctx->argv, sizeof(ctx->argv));
1671 p->ctime = ctx->ctime;
1672 p->dummy = ctx->dummy;
Al Viroa64e6492008-11-12 18:37:41 -05001673 p->in_syscall = ctx->in_syscall;
1674 p->filterkey = kstrdup(ctx->filterkey, GFP_KERNEL);
1675 p->ppid = current->pid;
Al Viro0590b932008-12-14 23:45:27 -05001676 p->prio = ctx->prio;
1677 p->current_state = ctx->current_state;
Al Viroa64e6492008-11-12 18:37:41 -05001678}
1679
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001680/**
1681 * audit_syscall_exit - deallocate audit context after a system call
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001682 * @valid: success/failure flag
1683 * @return_code: syscall return value
1684 *
1685 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1687 * filtering, or because some other part of the kernel write an audit
1688 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001689 * free the names stored from getname().
1690 */
Al Viro5411be52006-03-29 20:23:36 -05001691void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Al Viro5411be52006-03-29 20:23:36 -05001693 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct audit_context *context;
1695
2fd6f582005-04-29 16:08:28 +01001696 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001699 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Al Viro0590b932008-12-14 23:45:27 -05001701 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001702 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 context->in_syscall = 0;
Al Viro0590b932008-12-14 23:45:27 -05001705 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
2fd6f582005-04-29 16:08:28 +01001706
Al Viro916d7572009-06-24 00:02:38 -04001707 if (!list_empty(&context->killed_trees))
1708 audit_kill_trees(&context->killed_trees);
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (context->previous) {
1711 struct audit_context *new_context = context->previous;
1712 context->previous = NULL;
1713 audit_free_context(context);
1714 tsk->audit_context = new_context;
1715 } else {
1716 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001717 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001719 context->aux = NULL;
1720 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001721 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001722 context->target_sid = 0;
Al Viro4f6b4342008-12-09 19:50:34 -05001723 context->sockaddr_len = 0;
Al Virof3298dc2008-12-10 03:16:51 -05001724 context->type = 0;
Al Viro157cf642008-12-14 04:57:47 -05001725 context->fds[0] = -1;
Al Viroe048e022008-12-16 03:51:22 -05001726 if (context->state != AUDIT_RECORD_CONTEXT) {
1727 kfree(context->filterkey);
1728 context->filterkey = NULL;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 tsk->audit_context = context;
1731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
Al Viro74c3cbe2007-07-22 08:04:18 -04001734static inline void handle_one(const struct inode *inode)
1735{
1736#ifdef CONFIG_AUDIT_TREE
1737 struct audit_context *context;
1738 struct audit_tree_refs *p;
1739 struct audit_chunk *chunk;
1740 int count;
Eric Parise61ce862009-12-17 21:24:24 -05001741 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
Al Viro74c3cbe2007-07-22 08:04:18 -04001742 return;
1743 context = current->audit_context;
1744 p = context->trees;
1745 count = context->tree_count;
1746 rcu_read_lock();
1747 chunk = audit_tree_lookup(inode);
1748 rcu_read_unlock();
1749 if (!chunk)
1750 return;
1751 if (likely(put_tree_ref(context, chunk)))
1752 return;
1753 if (unlikely(!grow_tree_refs(context))) {
Eric Paris436c4052008-04-18 10:01:04 -04001754 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001755 audit_set_auditable(context);
1756 audit_put_chunk(chunk);
1757 unroll_tree_refs(context, p, count);
1758 return;
1759 }
1760 put_tree_ref(context, chunk);
1761#endif
1762}
1763
1764static void handle_path(const struct dentry *dentry)
1765{
1766#ifdef CONFIG_AUDIT_TREE
1767 struct audit_context *context;
1768 struct audit_tree_refs *p;
1769 const struct dentry *d, *parent;
1770 struct audit_chunk *drop;
1771 unsigned long seq;
1772 int count;
1773
1774 context = current->audit_context;
1775 p = context->trees;
1776 count = context->tree_count;
1777retry:
1778 drop = NULL;
1779 d = dentry;
1780 rcu_read_lock();
1781 seq = read_seqbegin(&rename_lock);
1782 for(;;) {
1783 struct inode *inode = d->d_inode;
Eric Parise61ce862009-12-17 21:24:24 -05001784 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
Al Viro74c3cbe2007-07-22 08:04:18 -04001785 struct audit_chunk *chunk;
1786 chunk = audit_tree_lookup(inode);
1787 if (chunk) {
1788 if (unlikely(!put_tree_ref(context, chunk))) {
1789 drop = chunk;
1790 break;
1791 }
1792 }
1793 }
1794 parent = d->d_parent;
1795 if (parent == d)
1796 break;
1797 d = parent;
1798 }
1799 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1800 rcu_read_unlock();
1801 if (!drop) {
1802 /* just a race with rename */
1803 unroll_tree_refs(context, p, count);
1804 goto retry;
1805 }
1806 audit_put_chunk(drop);
1807 if (grow_tree_refs(context)) {
1808 /* OK, got more space */
1809 unroll_tree_refs(context, p, count);
1810 goto retry;
1811 }
1812 /* too bad */
1813 printk(KERN_WARNING
Eric Paris436c4052008-04-18 10:01:04 -04001814 "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001815 unroll_tree_refs(context, p, count);
1816 audit_set_auditable(context);
1817 return;
1818 }
1819 rcu_read_unlock();
1820#endif
1821}
1822
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001823/**
1824 * audit_getname - add a name to the list
1825 * @name: name to add
1826 *
1827 * Add a name to the list of audit names for this context.
1828 * Called from fs/namei.c:getname().
1829 */
Al Virod8945bb52006-05-18 16:01:30 -04001830void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 struct audit_context *context = current->audit_context;
1833
Al Virod8945bb52006-05-18 16:01:30 -04001834 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return;
1836
1837 if (!context->in_syscall) {
1838#if AUDIT_DEBUG == 2
1839 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1840 __FILE__, __LINE__, context->serial, name);
1841 dump_stack();
1842#endif
1843 return;
1844 }
1845 BUG_ON(context->name_count >= AUDIT_NAMES);
1846 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001847 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1848 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 context->names[context->name_count].ino = (unsigned long)-1;
Amy Griffise41e8bd2007-02-13 14:14:09 -05001850 context->names[context->name_count].osid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 ++context->name_count;
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02001852 if (!context->pwd.dentry)
1853 get_fs_pwd(current->fs, &context->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001856/* audit_putname - intercept a putname request
1857 * @name: name to intercept and delay for putname
1858 *
1859 * If we have stored the name from getname in the audit context,
1860 * then we delay the putname until syscall exit.
1861 * Called from include/linux/fs.h:putname().
1862 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863void audit_putname(const char *name)
1864{
1865 struct audit_context *context = current->audit_context;
1866
1867 BUG_ON(!context);
1868 if (!context->in_syscall) {
1869#if AUDIT_DEBUG == 2
1870 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1871 __FILE__, __LINE__, context->serial, name);
1872 if (context->name_count) {
1873 int i;
1874 for (i = 0; i < context->name_count; i++)
1875 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1876 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001877 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879#endif
1880 __putname(name);
1881 }
1882#if AUDIT_DEBUG
1883 else {
1884 ++context->put_count;
1885 if (context->put_count > context->name_count) {
1886 printk(KERN_ERR "%s:%d(:%d): major=%d"
1887 " in_syscall=%d putname(%p) name_count=%d"
1888 " put_count=%d\n",
1889 __FILE__, __LINE__,
1890 context->serial, context->major,
1891 context->in_syscall, name, context->name_count,
1892 context->put_count);
1893 dump_stack();
1894 }
1895 }
1896#endif
1897}
1898
Amy Griffis5712e882007-02-13 14:15:22 -05001899static int audit_inc_name_count(struct audit_context *context,
1900 const struct inode *inode)
1901{
1902 if (context->name_count >= AUDIT_NAMES) {
1903 if (inode)
Eric Paris449cedf2010-04-05 16:16:26 -04001904 printk(KERN_DEBUG "audit: name_count maxed, losing inode data: "
Eric Paris436c4052008-04-18 10:01:04 -04001905 "dev=%02x:%02x, inode=%lu\n",
Amy Griffis5712e882007-02-13 14:15:22 -05001906 MAJOR(inode->i_sb->s_dev),
1907 MINOR(inode->i_sb->s_dev),
1908 inode->i_ino);
1909
1910 else
Eric Paris436c4052008-04-18 10:01:04 -04001911 printk(KERN_DEBUG "name_count maxed, losing inode data\n");
Amy Griffis5712e882007-02-13 14:15:22 -05001912 return 1;
1913 }
1914 context->name_count++;
1915#if AUDIT_DEBUG
1916 context->ino_count++;
1917#endif
1918 return 0;
1919}
1920
Eric Paris851f7ff2008-11-11 21:48:14 +11001921
1922static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1923{
1924 struct cpu_vfs_cap_data caps;
1925 int rc;
1926
1927 memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
1928 memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
1929 name->fcap.fE = 0;
1930 name->fcap_ver = 0;
1931
1932 if (!dentry)
1933 return 0;
1934
1935 rc = get_vfs_caps_from_disk(dentry, &caps);
1936 if (rc)
1937 return rc;
1938
1939 name->fcap.permitted = caps.permitted;
1940 name->fcap.inheritable = caps.inheritable;
1941 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1942 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1943
1944 return 0;
1945}
1946
1947
Amy Griffis3e2efce2006-07-13 13:16:02 -04001948/* Copy inode data into an audit_names. */
Eric Paris851f7ff2008-11-11 21:48:14 +11001949static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1950 const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001951{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001952 name->ino = inode->i_ino;
1953 name->dev = inode->i_sb->s_dev;
1954 name->mode = inode->i_mode;
1955 name->uid = inode->i_uid;
1956 name->gid = inode->i_gid;
1957 name->rdev = inode->i_rdev;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001958 security_inode_getsecid(inode, &name->osid);
Eric Paris851f7ff2008-11-11 21:48:14 +11001959 audit_copy_fcaps(name, dentry);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001960}
1961
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001962/**
1963 * audit_inode - store the inode and device from a lookup
1964 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001965 * @dentry: dentry being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001966 *
1967 * Called from fs/namei.c:path_lookup().
1968 */
Al Viro5a190ae2007-06-07 12:19:32 -04001969void __audit_inode(const char *name, const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 int idx;
1972 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04001973 const struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 if (!context->in_syscall)
1976 return;
1977 if (context->name_count
1978 && context->names[context->name_count-1].name
1979 && context->names[context->name_count-1].name == name)
1980 idx = context->name_count - 1;
1981 else if (context->name_count > 1
1982 && context->names[context->name_count-2].name
1983 && context->names[context->name_count-2].name == name)
1984 idx = context->name_count - 2;
1985 else {
1986 /* FIXME: how much do we care about inodes that have no
1987 * associated name? */
Amy Griffis5712e882007-02-13 14:15:22 -05001988 if (audit_inc_name_count(context, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001990 idx = context->name_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 context->names[idx].name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001993 handle_path(dentry);
Eric Paris851f7ff2008-11-11 21:48:14 +11001994 audit_copy_inode(&context->names[idx], dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
Amy Griffis73241cc2005-11-03 16:00:25 +00001997/**
1998 * audit_inode_child - collect inode info for created/removed objects
Randy Dunlap481968f2007-10-21 20:59:53 -07001999 * @dentry: dentry being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04002000 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00002001 *
2002 * For syscalls that create or remove filesystem objects, audit_inode
2003 * can only collect information for the filesystem object's parent.
2004 * This call updates the audit context with the child's information.
2005 * Syscalls that create a new filesystem object must be hooked after
2006 * the object is created. Syscalls that remove a filesystem object
2007 * must be hooked prior, in order to capture the target inode during
2008 * unsuccessful attempts.
2009 */
Al Virocccc6bb2009-12-25 05:07:33 -05002010void __audit_inode_child(const struct dentry *dentry,
Amy Griffis73d3ec52006-07-13 13:16:39 -04002011 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00002012{
2013 int idx;
2014 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05002015 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04002016 const struct inode *inode = dentry->d_inode;
Al Virocccc6bb2009-12-25 05:07:33 -05002017 const char *dname = dentry->d_name.name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04002018 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00002019
2020 if (!context->in_syscall)
2021 return;
2022
Al Viro74c3cbe2007-07-22 08:04:18 -04002023 if (inode)
2024 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00002025
Amy Griffis5712e882007-02-13 14:15:22 -05002026 /* parent is more likely, look for it first */
2027 for (idx = 0; idx < context->name_count; idx++) {
2028 struct audit_names *n = &context->names[idx];
Amy Griffis73241cc2005-11-03 16:00:25 +00002029
Amy Griffis5712e882007-02-13 14:15:22 -05002030 if (!n->name)
2031 continue;
2032
2033 if (n->ino == parent->i_ino &&
2034 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2035 n->name_len = dirlen; /* update parent data in place */
2036 found_parent = n->name;
2037 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04002038 }
Steve Grubbac9910c2006-09-28 14:31:32 -04002039 }
Amy Griffis73241cc2005-11-03 16:00:25 +00002040
Amy Griffis5712e882007-02-13 14:15:22 -05002041 /* no matching parent, look for matching child */
2042 for (idx = 0; idx < context->name_count; idx++) {
2043 struct audit_names *n = &context->names[idx];
Amy Griffis73d3ec52006-07-13 13:16:39 -04002044
Amy Griffis5712e882007-02-13 14:15:22 -05002045 if (!n->name)
2046 continue;
2047
2048 /* strcmp() is the more likely scenario */
2049 if (!strcmp(dname, n->name) ||
2050 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2051 if (inode)
Eric Paris851f7ff2008-11-11 21:48:14 +11002052 audit_copy_inode(n, NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002053 else
2054 n->ino = (unsigned long)-1;
2055 found_child = n->name;
2056 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04002057 }
Amy Griffis5712e882007-02-13 14:15:22 -05002058 }
2059
2060add_names:
2061 if (!found_parent) {
2062 if (audit_inc_name_count(context, parent))
2063 return;
2064 idx = context->name_count - 1;
2065 context->names[idx].name = NULL;
Eric Paris851f7ff2008-11-11 21:48:14 +11002066 audit_copy_inode(&context->names[idx], NULL, parent);
Amy Griffis73d3ec52006-07-13 13:16:39 -04002067 }
Amy Griffis5712e882007-02-13 14:15:22 -05002068
2069 if (!found_child) {
2070 if (audit_inc_name_count(context, inode))
2071 return;
2072 idx = context->name_count - 1;
2073
2074 /* Re-use the name belonging to the slot for a matching parent
2075 * directory. All names for this context are relinquished in
2076 * audit_free_names() */
2077 if (found_parent) {
2078 context->names[idx].name = found_parent;
2079 context->names[idx].name_len = AUDIT_NAME_FULL;
2080 /* don't call __putname() */
2081 context->names[idx].name_put = 0;
2082 } else {
2083 context->names[idx].name = NULL;
2084 }
2085
2086 if (inode)
Eric Paris851f7ff2008-11-11 21:48:14 +11002087 audit_copy_inode(&context->names[idx], NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002088 else
2089 context->names[idx].ino = (unsigned long)-1;
2090 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04002091}
Trond Myklebust50e437d2007-06-07 22:44:34 -04002092EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04002093
2094/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002095 * auditsc_get_stamp - get local copies of audit_context values
2096 * @ctx: audit_context for the task
2097 * @t: timespec to store time recorded in the audit_context
2098 * @serial: serial value that is recorded in the audit_context
2099 *
2100 * Also sets the context as auditable.
2101 */
Al Viro48887e62008-12-06 01:05:50 -05002102int auditsc_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01002103 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Al Viro48887e62008-12-06 01:05:50 -05002105 if (!ctx->in_syscall)
2106 return 0;
David Woodhousece625a82005-07-18 14:24:46 -04002107 if (!ctx->serial)
2108 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01002109 t->tv_sec = ctx->ctime.tv_sec;
2110 t->tv_nsec = ctx->ctime.tv_nsec;
2111 *serial = ctx->serial;
Al Viro0590b932008-12-14 23:45:27 -05002112 if (!ctx->prio) {
2113 ctx->prio = 1;
2114 ctx->current_state = AUDIT_RECORD_CONTEXT;
2115 }
Al Viro48887e62008-12-06 01:05:50 -05002116 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Eric Paris4746ec52008-01-08 10:06:53 -05002119/* global counter which is incremented every time something logs in */
2120static atomic_t session_id = ATOMIC_INIT(0);
2121
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002122/**
2123 * audit_set_loginuid - set a task's audit_context loginuid
2124 * @task: task whose audit context is being modified
2125 * @loginuid: loginuid value
2126 *
2127 * Returns 0.
2128 *
2129 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2130 */
Steve Grubb456be6c2005-04-29 17:30:07 +01002131int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Eric Paris4746ec52008-01-08 10:06:53 -05002133 unsigned int sessionid = atomic_inc_return(&session_id);
Steve Grubb41757102006-06-12 07:48:28 -04002134 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01002135
Al Virobfef93a2008-01-10 04:53:18 -05002136 if (context && context->in_syscall) {
2137 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04002138
Al Virobfef93a2008-01-10 04:53:18 -05002139 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2140 if (ab) {
2141 audit_log_format(ab, "login pid=%d uid=%u "
Eric Paris4746ec52008-01-08 10:06:53 -05002142 "old auid=%u new auid=%u"
2143 " old ses=%u new ses=%u",
David Howellsc69e8d92008-11-14 10:39:19 +11002144 task->pid, task_uid(task),
Eric Paris4746ec52008-01-08 10:06:53 -05002145 task->loginuid, loginuid,
2146 task->sessionid, sessionid);
Al Virobfef93a2008-01-10 04:53:18 -05002147 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01002148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Eric Paris4746ec52008-01-08 10:06:53 -05002150 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05002151 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return 0;
2153}
2154
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002155/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002156 * __audit_mq_open - record audit data for a POSIX MQ open
2157 * @oflag: open flag
2158 * @mode: mode bits
Randy Dunlap6b962552009-01-05 13:41:13 -08002159 * @attr: queue attributes
George C. Wilson20ca73b2006-05-24 16:09:55 -05002160 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002161 */
Al Virodf0a4282011-07-26 05:26:10 -04002162void __audit_mq_open(int oflag, umode_t mode, struct mq_attr *attr)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002163{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002164 struct audit_context *context = current->audit_context;
2165
Al Viro564f6992008-12-14 04:02:26 -05002166 if (attr)
2167 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2168 else
2169 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002170
Al Viro564f6992008-12-14 04:02:26 -05002171 context->mq_open.oflag = oflag;
2172 context->mq_open.mode = mode;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002173
Al Viro564f6992008-12-14 04:02:26 -05002174 context->type = AUDIT_MQ_OPEN;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002175}
2176
2177/**
Al Viroc32c8af2008-12-14 03:46:48 -05002178 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
George C. Wilson20ca73b2006-05-24 16:09:55 -05002179 * @mqdes: MQ descriptor
2180 * @msg_len: Message length
2181 * @msg_prio: Message priority
Al Viroc32c8af2008-12-14 03:46:48 -05002182 * @abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002183 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002184 */
Al Viroc32c8af2008-12-14 03:46:48 -05002185void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2186 const struct timespec *abs_timeout)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002187{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002188 struct audit_context *context = current->audit_context;
Al Viroc32c8af2008-12-14 03:46:48 -05002189 struct timespec *p = &context->mq_sendrecv.abs_timeout;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002190
Al Viroc32c8af2008-12-14 03:46:48 -05002191 if (abs_timeout)
2192 memcpy(p, abs_timeout, sizeof(struct timespec));
2193 else
2194 memset(p, 0, sizeof(struct timespec));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002195
Al Viroc32c8af2008-12-14 03:46:48 -05002196 context->mq_sendrecv.mqdes = mqdes;
2197 context->mq_sendrecv.msg_len = msg_len;
2198 context->mq_sendrecv.msg_prio = msg_prio;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002199
Al Viroc32c8af2008-12-14 03:46:48 -05002200 context->type = AUDIT_MQ_SENDRECV;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002201}
2202
2203/**
2204 * __audit_mq_notify - record audit data for a POSIX MQ notify
2205 * @mqdes: MQ descriptor
Randy Dunlap6b962552009-01-05 13:41:13 -08002206 * @notification: Notification event
George C. Wilson20ca73b2006-05-24 16:09:55 -05002207 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002208 */
2209
Al Viro20114f72008-12-10 07:16:12 -05002210void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002211{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002212 struct audit_context *context = current->audit_context;
2213
Al Viro20114f72008-12-10 07:16:12 -05002214 if (notification)
2215 context->mq_notify.sigev_signo = notification->sigev_signo;
2216 else
2217 context->mq_notify.sigev_signo = 0;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002218
Al Viro20114f72008-12-10 07:16:12 -05002219 context->mq_notify.mqdes = mqdes;
2220 context->type = AUDIT_MQ_NOTIFY;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002221}
2222
2223/**
2224 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2225 * @mqdes: MQ descriptor
2226 * @mqstat: MQ flags
2227 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002228 */
Al Viro73929062008-12-10 06:58:59 -05002229void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002230{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002231 struct audit_context *context = current->audit_context;
Al Viro73929062008-12-10 06:58:59 -05002232 context->mq_getsetattr.mqdes = mqdes;
2233 context->mq_getsetattr.mqstat = *mqstat;
2234 context->type = AUDIT_MQ_GETSETATTR;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002235}
2236
2237/**
Steve Grubb073115d2006-04-02 17:07:33 -04002238 * audit_ipc_obj - record audit data for ipc object
2239 * @ipcp: ipc permissions
2240 *
Steve Grubb073115d2006-04-02 17:07:33 -04002241 */
Al Viroa33e6752008-12-10 03:40:06 -05002242void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002243{
Steve Grubb073115d2006-04-02 17:07:33 -04002244 struct audit_context *context = current->audit_context;
Al Viroa33e6752008-12-10 03:40:06 -05002245 context->ipc.uid = ipcp->uid;
2246 context->ipc.gid = ipcp->gid;
2247 context->ipc.mode = ipcp->mode;
Al Viroe816f372008-12-10 03:47:15 -05002248 context->ipc.has_perm = 0;
Al Viroa33e6752008-12-10 03:40:06 -05002249 security_ipc_getsecid(ipcp, &context->ipc.osid);
2250 context->type = AUDIT_IPC;
Steve Grubb073115d2006-04-02 17:07:33 -04002251}
2252
2253/**
2254 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002255 * @qbytes: msgq bytes
2256 * @uid: msgq user id
2257 * @gid: msgq group id
2258 * @mode: msgq mode (permissions)
2259 *
Al Viroe816f372008-12-10 03:47:15 -05002260 * Called only after audit_ipc_obj().
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002261 */
Al Viro2570ebb2011-07-27 14:03:22 -04002262void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 struct audit_context *context = current->audit_context;
2265
Al Viroe816f372008-12-10 03:47:15 -05002266 context->ipc.qbytes = qbytes;
2267 context->ipc.perm_uid = uid;
2268 context->ipc.perm_gid = gid;
2269 context->ipc.perm_mode = mode;
2270 context->ipc.has_perm = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002272
Al Viro473ae302006-04-26 14:04:08 -04002273int audit_bprm(struct linux_binprm *bprm)
2274{
2275 struct audit_aux_data_execve *ax;
2276 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002277
Al Viro5ac3a9c2006-07-16 06:38:45 -04002278 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04002279 return 0;
2280
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002281 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002282 if (!ax)
2283 return -ENOMEM;
2284
2285 ax->argc = bprm->argc;
2286 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002287 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002288 ax->d.type = AUDIT_EXECVE;
2289 ax->d.next = context->aux;
2290 context->aux = (void *)ax;
2291 return 0;
2292}
2293
2294
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002295/**
2296 * audit_socketcall - record audit data for sys_socketcall
2297 * @nargs: number of args
2298 * @args: args array
2299 *
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002300 */
Al Virof3298dc2008-12-10 03:16:51 -05002301void audit_socketcall(int nargs, unsigned long *args)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002302{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002303 struct audit_context *context = current->audit_context;
2304
Al Viro5ac3a9c2006-07-16 06:38:45 -04002305 if (likely(!context || context->dummy))
Al Virof3298dc2008-12-10 03:16:51 -05002306 return;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002307
Al Virof3298dc2008-12-10 03:16:51 -05002308 context->type = AUDIT_SOCKETCALL;
2309 context->socketcall.nargs = nargs;
2310 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002311}
2312
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002313/**
Al Virodb349502007-02-07 01:48:00 -05002314 * __audit_fd_pair - record audit data for pipe and socketpair
2315 * @fd1: the first file descriptor
2316 * @fd2: the second file descriptor
2317 *
Al Virodb349502007-02-07 01:48:00 -05002318 */
Al Viro157cf642008-12-14 04:57:47 -05002319void __audit_fd_pair(int fd1, int fd2)
Al Virodb349502007-02-07 01:48:00 -05002320{
2321 struct audit_context *context = current->audit_context;
Al Viro157cf642008-12-14 04:57:47 -05002322 context->fds[0] = fd1;
2323 context->fds[1] = fd2;
Al Virodb349502007-02-07 01:48:00 -05002324}
2325
2326/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002327 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2328 * @len: data length in user space
2329 * @a: data address in kernel space
2330 *
2331 * Returns 0 for success or NULL context or < 0 on error.
2332 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002333int audit_sockaddr(int len, void *a)
2334{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002335 struct audit_context *context = current->audit_context;
2336
Al Viro5ac3a9c2006-07-16 06:38:45 -04002337 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002338 return 0;
2339
Al Viro4f6b4342008-12-09 19:50:34 -05002340 if (!context->sockaddr) {
2341 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2342 if (!p)
2343 return -ENOMEM;
2344 context->sockaddr = p;
2345 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002346
Al Viro4f6b4342008-12-09 19:50:34 -05002347 context->sockaddr_len = len;
2348 memcpy(context->sockaddr, a, len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002349 return 0;
2350}
2351
Al Viroa5cb0132007-03-20 13:58:35 -04002352void __audit_ptrace(struct task_struct *t)
2353{
2354 struct audit_context *context = current->audit_context;
2355
2356 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002357 context->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002358 context->target_uid = task_uid(t);
Eric Paris4746ec52008-01-08 10:06:53 -05002359 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002360 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002361 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002362}
2363
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002364/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002365 * audit_signal_info - record signal info for shutting down audit subsystem
2366 * @sig: signal value
2367 * @t: task being signaled
2368 *
2369 * If the audit subsystem is being terminated, record the task (pid)
2370 * and uid that is doing that.
2371 */
Amy Griffise54dc242007-03-29 18:01:04 -04002372int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002373{
Amy Griffise54dc242007-03-29 18:01:04 -04002374 struct audit_aux_data_pids *axp;
2375 struct task_struct *tsk = current;
2376 struct audit_context *ctx = tsk->audit_context;
David Howellsc69e8d92008-11-14 10:39:19 +11002377 uid_t uid = current_uid(), t_uid = task_uid(t);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002378
Al Viro175fc482007-08-08 00:01:46 +01002379 if (audit_pid && t->tgid == audit_pid) {
Eric Parisee1d3152008-07-07 10:49:45 -04002380 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
Al Viro175fc482007-08-08 00:01:46 +01002381 audit_sig_pid = tsk->pid;
Al Virobfef93a2008-01-10 04:53:18 -05002382 if (tsk->loginuid != -1)
2383 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002384 else
David Howellsc69e8d92008-11-14 10:39:19 +11002385 audit_sig_uid = uid;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002386 security_task_getsecid(tsk, &audit_sig_sid);
Al Viro175fc482007-08-08 00:01:46 +01002387 }
2388 if (!audit_signals || audit_dummy_context())
2389 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002390 }
Amy Griffise54dc242007-03-29 18:01:04 -04002391
Amy Griffise54dc242007-03-29 18:01:04 -04002392 /* optimize the common case by putting first signal recipient directly
2393 * in audit_context */
2394 if (!ctx->target_pid) {
2395 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002396 ctx->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002397 ctx->target_uid = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002398 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002399 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002400 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002401 return 0;
2402 }
2403
2404 axp = (void *)ctx->aux_pids;
2405 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2406 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2407 if (!axp)
2408 return -ENOMEM;
2409
2410 axp->d.type = AUDIT_OBJ_PID;
2411 axp->d.next = ctx->aux_pids;
2412 ctx->aux_pids = (void *)axp;
2413 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002414 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002415
2416 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002417 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002418 axp->target_uid[axp->pid_count] = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002419 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002420 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002421 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002422 axp->pid_count++;
2423
2424 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002425}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002426
2427/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002428 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
David Howellsd84f4f92008-11-14 10:39:23 +11002429 * @bprm: pointer to the bprm being processed
2430 * @new: the proposed new credentials
2431 * @old: the old credentials
Eric Paris3fc689e2008-11-11 21:48:18 +11002432 *
2433 * Simply check if the proc already has the caps given by the file and if not
2434 * store the priv escalation info for later auditing at the end of the syscall
2435 *
Eric Paris3fc689e2008-11-11 21:48:18 +11002436 * -Eric
2437 */
David Howellsd84f4f92008-11-14 10:39:23 +11002438int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2439 const struct cred *new, const struct cred *old)
Eric Paris3fc689e2008-11-11 21:48:18 +11002440{
2441 struct audit_aux_data_bprm_fcaps *ax;
2442 struct audit_context *context = current->audit_context;
2443 struct cpu_vfs_cap_data vcaps;
2444 struct dentry *dentry;
2445
2446 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2447 if (!ax)
David Howellsd84f4f92008-11-14 10:39:23 +11002448 return -ENOMEM;
Eric Paris3fc689e2008-11-11 21:48:18 +11002449
2450 ax->d.type = AUDIT_BPRM_FCAPS;
2451 ax->d.next = context->aux;
2452 context->aux = (void *)ax;
2453
2454 dentry = dget(bprm->file->f_dentry);
2455 get_vfs_caps_from_disk(dentry, &vcaps);
2456 dput(dentry);
2457
2458 ax->fcap.permitted = vcaps.permitted;
2459 ax->fcap.inheritable = vcaps.inheritable;
2460 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2461 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2462
David Howellsd84f4f92008-11-14 10:39:23 +11002463 ax->old_pcap.permitted = old->cap_permitted;
2464 ax->old_pcap.inheritable = old->cap_inheritable;
2465 ax->old_pcap.effective = old->cap_effective;
Eric Paris3fc689e2008-11-11 21:48:18 +11002466
David Howellsd84f4f92008-11-14 10:39:23 +11002467 ax->new_pcap.permitted = new->cap_permitted;
2468 ax->new_pcap.inheritable = new->cap_inheritable;
2469 ax->new_pcap.effective = new->cap_effective;
2470 return 0;
Eric Paris3fc689e2008-11-11 21:48:18 +11002471}
2472
2473/**
Eric Parise68b75a02008-11-11 21:48:22 +11002474 * __audit_log_capset - store information about the arguments to the capset syscall
David Howellsd84f4f92008-11-14 10:39:23 +11002475 * @pid: target pid of the capset call
2476 * @new: the new credentials
2477 * @old: the old (current) credentials
Eric Parise68b75a02008-11-11 21:48:22 +11002478 *
2479 * Record the aguments userspace sent to sys_capset for later printing by the
2480 * audit system if applicable
2481 */
Al Viro57f71a02009-01-04 14:52:57 -05002482void __audit_log_capset(pid_t pid,
David Howellsd84f4f92008-11-14 10:39:23 +11002483 const struct cred *new, const struct cred *old)
Eric Parise68b75a02008-11-11 21:48:22 +11002484{
Eric Parise68b75a02008-11-11 21:48:22 +11002485 struct audit_context *context = current->audit_context;
Al Viro57f71a02009-01-04 14:52:57 -05002486 context->capset.pid = pid;
2487 context->capset.cap.effective = new->cap_effective;
2488 context->capset.cap.inheritable = new->cap_effective;
2489 context->capset.cap.permitted = new->cap_permitted;
2490 context->type = AUDIT_CAPSET;
Eric Parise68b75a02008-11-11 21:48:22 +11002491}
2492
Al Viro120a7952010-10-30 02:54:44 -04002493void __audit_mmap_fd(int fd, int flags)
2494{
2495 struct audit_context *context = current->audit_context;
2496 context->mmap.fd = fd;
2497 context->mmap.flags = flags;
2498 context->type = AUDIT_MMAP;
2499}
2500
Eric Parise68b75a02008-11-11 21:48:22 +11002501/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002502 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002503 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002504 *
2505 * If a process ends with a core dump, something fishy is going on and we
2506 * should record the event for investigation.
2507 */
2508void audit_core_dumps(long signr)
2509{
2510 struct audit_buffer *ab;
2511 u32 sid;
David Howells76aac0e2008-11-14 10:39:12 +11002512 uid_t auid = audit_get_loginuid(current), uid;
2513 gid_t gid;
Eric Paris4746ec52008-01-08 10:06:53 -05002514 unsigned int sessionid = audit_get_sessionid(current);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002515
2516 if (!audit_enabled)
2517 return;
2518
2519 if (signr == SIGQUIT) /* don't care for those */
2520 return;
2521
2522 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
David Howells76aac0e2008-11-14 10:39:12 +11002523 current_uid_gid(&uid, &gid);
Eric Paris4746ec52008-01-08 10:06:53 -05002524 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
David Howells76aac0e2008-11-14 10:39:12 +11002525 auid, uid, gid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002526 security_task_getsecid(current, &sid);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002527 if (sid) {
2528 char *ctx = NULL;
2529 u32 len;
2530
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002531 if (security_secid_to_secctx(sid, &ctx, &len))
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002532 audit_log_format(ab, " ssid=%u", sid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002533 else {
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002534 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002535 security_release_secctx(ctx, len);
2536 }
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002537 }
2538 audit_log_format(ab, " pid=%d comm=", current->pid);
2539 audit_log_untrustedstring(ab, current->comm);
2540 audit_log_format(ab, " sig=%ld", signr);
2541 audit_log_end(ab);
2542}
Al Viro916d7572009-06-24 00:02:38 -04002543
2544struct list_head *audit_killed_trees(void)
2545{
2546 struct audit_context *ctx = current->audit_context;
2547 if (likely(!ctx || !ctx->in_syscall))
2548 return NULL;
2549 return &ctx->killed_trees;
2550}