blob: f49a0318c2ed750437dd3ca810ff90e119994f68 [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>
Alan Cox715b49e2006-01-18 17:44:07 -080047#include <asm/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>
51#include <linux/module.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;
213 mode_t mode;
214 u32 osid;
Al Viroe816f372008-12-10 03:47:15 -0500215 int has_perm;
216 uid_t perm_uid;
217 gid_t perm_gid;
218 mode_t perm_mode;
219 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;
237 mode_t mode;
238 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
Al Viro8b67dca2008-04-28 04:15:49 -0400308static int audit_match_filetype(struct audit_context *ctx, int which)
309{
310 unsigned index = which & ~S_IFMT;
311 mode_t mode = which & S_IFMT;
zhangxiliang1a61c882008-08-02 10:56:37 +0800312
313 if (unlikely(!ctx))
314 return 0;
315
Al Viro8b67dca2008-04-28 04:15:49 -0400316 if (index >= ctx->name_count)
317 return 0;
318 if (ctx->names[index].ino == -1)
319 return 0;
320 if ((ctx->names[index].mode ^ mode) & S_IFMT)
321 return 0;
322 return 1;
323}
324
Al Viro74c3cbe2007-07-22 08:04:18 -0400325/*
326 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
327 * ->first_trees points to its beginning, ->trees - to the current end of data.
328 * ->tree_count is the number of free entries in array pointed to by ->trees.
329 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
330 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
331 * it's going to remain 1-element for almost any setup) until we free context itself.
332 * References in it _are_ dropped - at the same time we free/drop aux stuff.
333 */
334
335#ifdef CONFIG_AUDIT_TREE
Eric Paris679173b2009-01-26 18:09:45 -0500336static void audit_set_auditable(struct audit_context *ctx)
337{
338 if (!ctx->prio) {
339 ctx->prio = 1;
340 ctx->current_state = AUDIT_RECORD_CONTEXT;
341 }
342}
343
Al Viro74c3cbe2007-07-22 08:04:18 -0400344static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
345{
346 struct audit_tree_refs *p = ctx->trees;
347 int left = ctx->tree_count;
348 if (likely(left)) {
349 p->c[--left] = chunk;
350 ctx->tree_count = left;
351 return 1;
352 }
353 if (!p)
354 return 0;
355 p = p->next;
356 if (p) {
357 p->c[30] = chunk;
358 ctx->trees = p;
359 ctx->tree_count = 30;
360 return 1;
361 }
362 return 0;
363}
364
365static int grow_tree_refs(struct audit_context *ctx)
366{
367 struct audit_tree_refs *p = ctx->trees;
368 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
369 if (!ctx->trees) {
370 ctx->trees = p;
371 return 0;
372 }
373 if (p)
374 p->next = ctx->trees;
375 else
376 ctx->first_trees = ctx->trees;
377 ctx->tree_count = 31;
378 return 1;
379}
380#endif
381
382static void unroll_tree_refs(struct audit_context *ctx,
383 struct audit_tree_refs *p, int count)
384{
385#ifdef CONFIG_AUDIT_TREE
386 struct audit_tree_refs *q;
387 int n;
388 if (!p) {
389 /* we started with empty chain */
390 p = ctx->first_trees;
391 count = 31;
392 /* if the very first allocation has failed, nothing to do */
393 if (!p)
394 return;
395 }
396 n = count;
397 for (q = p; q != ctx->trees; q = q->next, n = 31) {
398 while (n--) {
399 audit_put_chunk(q->c[n]);
400 q->c[n] = NULL;
401 }
402 }
403 while (n-- > ctx->tree_count) {
404 audit_put_chunk(q->c[n]);
405 q->c[n] = NULL;
406 }
407 ctx->trees = p;
408 ctx->tree_count = count;
409#endif
410}
411
412static void free_tree_refs(struct audit_context *ctx)
413{
414 struct audit_tree_refs *p, *q;
415 for (p = ctx->first_trees; p; p = q) {
416 q = p->next;
417 kfree(p);
418 }
419}
420
421static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
422{
423#ifdef CONFIG_AUDIT_TREE
424 struct audit_tree_refs *p;
425 int n;
426 if (!tree)
427 return 0;
428 /* full ones */
429 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
430 for (n = 0; n < 31; n++)
431 if (audit_tree_match(p->c[n], tree))
432 return 1;
433 }
434 /* partial */
435 if (p) {
436 for (n = ctx->tree_count; n < 31; n++)
437 if (audit_tree_match(p->c[n], tree))
438 return 1;
439 }
440#endif
441 return 0;
442}
443
Amy Griffisf368c07d2006-04-07 16:55:56 -0400444/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/* Compare a task_struct with an audit_rule. Return 1 on match, 0
446 * otherwise. */
447static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500448 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400450 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 enum audit_state *state)
452{
David Howellsc69e8d92008-11-14 10:39:19 +1100453 const struct cred *cred = get_task_cred(tsk);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400454 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600455 u32 sid;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500458 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int result = 0;
460
Amy Griffis93315ed2006-02-07 12:05:27 -0500461 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500463 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
Al Viro3c662512006-05-06 08:26:27 -0400465 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400466 if (ctx) {
467 if (!ctx->ppid)
468 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400469 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400470 }
Al Viro3c662512006-05-06 08:26:27 -0400471 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 case AUDIT_UID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100473 result = audit_comparator(cred->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case AUDIT_EUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100476 result = audit_comparator(cred->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
478 case AUDIT_SUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100479 result = audit_comparator(cred->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 case AUDIT_FSUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100482 result = audit_comparator(cred->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 case AUDIT_GID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100485 result = audit_comparator(cred->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 case AUDIT_EGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100488 result = audit_comparator(cred->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 case AUDIT_SGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100491 result = audit_comparator(cred->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 case AUDIT_FSGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100494 result = audit_comparator(cred->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500497 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
2fd6f582005-04-29 16:08:28 +0100499 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700500 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500501 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100502 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 case AUDIT_EXIT:
505 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500506 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
508 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100509 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500510 if (f->val)
511 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100512 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500513 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400517 if (name)
518 result = audit_comparator(MAJOR(name->dev),
519 f->op, f->val);
520 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500522 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 ++result;
524 break;
525 }
526 }
527 }
528 break;
529 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400530 if (name)
531 result = audit_comparator(MINOR(name->dev),
532 f->op, f->val);
533 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500535 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ++result;
537 break;
538 }
539 }
540 }
541 break;
542 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400543 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400544 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400545 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400547 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ++result;
549 break;
550 }
551 }
552 }
553 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400554 case AUDIT_WATCH:
Eric Parisae7b8f42009-12-17 20:12:04 -0500555 if (name)
556 result = audit_watch_compare(rule->watch, name->ino, name->dev);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400557 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400558 case AUDIT_DIR:
559 if (ctx)
560 result = match_tree_refs(ctx, rule->tree);
561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 case AUDIT_LOGINUID:
563 result = 0;
564 if (ctx)
Al Virobfef93a2008-01-10 04:53:18 -0500565 result = audit_comparator(tsk->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500567 case AUDIT_SUBJ_USER:
568 case AUDIT_SUBJ_ROLE:
569 case AUDIT_SUBJ_TYPE:
570 case AUDIT_SUBJ_SEN:
571 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600572 /* NOTE: this may return negative values indicating
573 a temporary error. We simply treat this as a
574 match for now to avoid losing information that
575 may be wanted. An error message will also be
576 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000577 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400578 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200579 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400580 need_sid = 0;
581 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200582 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600583 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000584 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600585 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400586 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600587 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500588 case AUDIT_OBJ_USER:
589 case AUDIT_OBJ_ROLE:
590 case AUDIT_OBJ_TYPE:
591 case AUDIT_OBJ_LEV_LOW:
592 case AUDIT_OBJ_LEV_HIGH:
593 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
594 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000595 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500596 /* Find files that match */
597 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200598 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500599 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000600 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500601 } else if (ctx) {
602 for (j = 0; j < ctx->name_count; j++) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200603 if (security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500604 ctx->names[j].osid,
605 f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000606 f->lsm_rule, ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500607 ++result;
608 break;
609 }
610 }
611 }
612 /* Find ipc objects that match */
Al Viroa33e6752008-12-10 03:40:06 -0500613 if (!ctx || ctx->type != AUDIT_IPC)
614 break;
615 if (security_audit_rule_match(ctx->ipc.osid,
616 f->type, f->op,
617 f->lsm_rule, ctx))
618 ++result;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500619 }
620 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 case AUDIT_ARG0:
622 case AUDIT_ARG1:
623 case AUDIT_ARG2:
624 case AUDIT_ARG3:
625 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500626 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400628 case AUDIT_FILTERKEY:
629 /* ignore this field for filtering */
630 result = 1;
631 break;
Al Viro55669bf2006-08-31 19:26:40 -0400632 case AUDIT_PERM:
633 result = audit_match_perm(ctx, f->val);
634 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400635 case AUDIT_FILETYPE:
636 result = audit_match_filetype(ctx, f->val);
637 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
David Howellsc69e8d92008-11-14 10:39:19 +1100640 if (!result) {
641 put_cred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
David Howellsc69e8d92008-11-14 10:39:19 +1100643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Al Viro0590b932008-12-14 23:45:27 -0500645
646 if (ctx) {
647 if (rule->prio <= ctx->prio)
648 return 0;
649 if (rule->filterkey) {
650 kfree(ctx->filterkey);
651 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
652 }
653 ctx->prio = rule->prio;
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 switch (rule->action) {
656 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
658 }
David Howellsc69e8d92008-11-14 10:39:19 +1100659 put_cred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 1;
661}
662
663/* At process creation time, we can determine if system-call auditing is
664 * completely disabled for this task. Since we only have the task
665 * structure at this point, we can only check uid and gid.
666 */
Al Viroe048e022008-12-16 03:51:22 -0500667static enum audit_state audit_filter_task(struct task_struct *tsk, char **key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct audit_entry *e;
670 enum audit_state state;
671
672 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100673 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400674 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Al Viroe048e022008-12-16 03:51:22 -0500675 if (state == AUDIT_RECORD_CONTEXT)
676 *key = kstrdup(e->rule.filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 rcu_read_unlock();
678 return state;
679 }
680 }
681 rcu_read_unlock();
682 return AUDIT_BUILD_CONTEXT;
683}
684
685/* At syscall entry and exit time, this filter is called if the
686 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100687 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700688 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
690static enum audit_state audit_filter_syscall(struct task_struct *tsk,
691 struct audit_context *ctx,
692 struct list_head *list)
693{
694 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100695 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
David Woodhouse351bb722005-07-14 14:40:06 +0100697 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100698 return AUDIT_DISABLED;
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100701 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000702 int word = AUDIT_WORD(ctx->major);
703 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100704
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000705 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400706 if ((e->rule.mask[word] & bit) == bit &&
707 audit_filter_rules(tsk, &e->rule, ctx, NULL,
708 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000709 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500710 ctx->current_state = state;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000711 return state;
712 }
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 rcu_read_unlock();
716 return AUDIT_BUILD_CONTEXT;
717}
718
Amy Griffisf368c07d2006-04-07 16:55:56 -0400719/* At syscall exit time, this filter is called if any audit_names[] have been
720 * collected during syscall processing. We only check rules in sublists at hash
721 * buckets applicable to the inode numbers in audit_names[].
722 * Regarding audit_state, same rules apply as for audit_filter_syscall().
723 */
Al Viro0590b932008-12-14 23:45:27 -0500724void audit_filter_inodes(struct task_struct *tsk, struct audit_context *ctx)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400725{
726 int i;
727 struct audit_entry *e;
728 enum audit_state state;
729
730 if (audit_pid && tsk->tgid == audit_pid)
Al Viro0590b932008-12-14 23:45:27 -0500731 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400732
733 rcu_read_lock();
734 for (i = 0; i < ctx->name_count; i++) {
735 int word = AUDIT_WORD(ctx->major);
736 int bit = AUDIT_BIT(ctx->major);
737 struct audit_names *n = &ctx->names[i];
738 int h = audit_hash_ino((u32)n->ino);
739 struct list_head *list = &audit_inode_hash[h];
740
741 if (list_empty(list))
742 continue;
743
744 list_for_each_entry_rcu(e, list, list) {
745 if ((e->rule.mask[word] & bit) == bit &&
746 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
747 rcu_read_unlock();
Al Viro0590b932008-12-14 23:45:27 -0500748 ctx->current_state = state;
749 return;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400750 }
751 }
752 }
753 rcu_read_unlock();
Amy Griffisf368c07d2006-04-07 16:55:56 -0400754}
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756static inline struct audit_context *audit_get_context(struct task_struct *tsk,
757 int return_valid,
Paul Moore6d208da2009-04-01 15:47:27 -0400758 long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 struct audit_context *context = tsk->audit_context;
761
762 if (likely(!context))
763 return NULL;
764 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500765
766 /*
767 * we need to fix up the return code in the audit logs if the actual
768 * return codes are later going to be fixed up by the arch specific
769 * signal handlers
770 *
771 * This is actually a test for:
772 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
773 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
774 *
775 * but is faster than a bunch of ||
776 */
777 if (unlikely(return_code <= -ERESTARTSYS) &&
778 (return_code >= -ERESTART_RESTARTBLOCK) &&
779 (return_code != -ENOIOCTLCMD))
780 context->return_code = -EINTR;
781 else
782 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Al Viro0590b932008-12-14 23:45:27 -0500784 if (context->in_syscall && !context->dummy) {
785 audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
786 audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 tsk->audit_context = NULL;
790 return context;
791}
792
793static inline void audit_free_names(struct audit_context *context)
794{
795 int i;
796
797#if AUDIT_DEBUG == 2
Al Viro0590b932008-12-14 23:45:27 -0500798 if (context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000799 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 " name_count=%d put_count=%d"
801 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000802 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 context->serial, context->major, context->in_syscall,
804 context->name_count, context->put_count,
805 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000806 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 printk(KERN_ERR "names[%d] = %p = %s\n", i,
808 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000809 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 dump_stack();
812 return;
813 }
814#endif
815#if AUDIT_DEBUG
816 context->put_count = 0;
817 context->ino_count = 0;
818#endif
819
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000820 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400821 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -0800825 path_put(&context->pwd);
826 context->pwd.dentry = NULL;
827 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830static inline void audit_free_aux(struct audit_context *context)
831{
832 struct audit_aux_data *aux;
833
834 while ((aux = context->aux)) {
835 context->aux = aux->next;
836 kfree(aux);
837 }
Amy Griffise54dc242007-03-29 18:01:04 -0400838 while ((aux = context->aux_pids)) {
839 context->aux_pids = aux->next;
840 kfree(aux);
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844static inline void audit_zero_context(struct audit_context *context,
845 enum audit_state state)
846{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 memset(context, 0, sizeof(*context));
848 context->state = state;
Al Viro0590b932008-12-14 23:45:27 -0500849 context->prio = state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852static inline struct audit_context *audit_alloc_context(enum audit_state state)
853{
854 struct audit_context *context;
855
856 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
857 return NULL;
858 audit_zero_context(context, state);
Al Viro916d7572009-06-24 00:02:38 -0400859 INIT_LIST_HEAD(&context->killed_trees);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return context;
861}
862
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700863/**
864 * audit_alloc - allocate an audit context block for a task
865 * @tsk: task
866 *
867 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 * if necessary. Doing so turns on system call auditing for the
869 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700870 * needed.
871 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872int audit_alloc(struct task_struct *tsk)
873{
874 struct audit_context *context;
875 enum audit_state state;
Al Viroe048e022008-12-16 03:51:22 -0500876 char *key = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Eric Parisb593d382008-01-08 17:38:31 -0500878 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0; /* Return if not auditing. */
880
Al Viroe048e022008-12-16 03:51:22 -0500881 state = audit_filter_task(tsk, &key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (likely(state == AUDIT_DISABLED))
883 return 0;
884
885 if (!(context = audit_alloc_context(state))) {
Al Viroe048e022008-12-16 03:51:22 -0500886 kfree(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 audit_log_lost("out of memory in audit_alloc");
888 return -ENOMEM;
889 }
Al Viroe048e022008-12-16 03:51:22 -0500890 context->filterkey = key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 tsk->audit_context = context;
893 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
894 return 0;
895}
896
897static inline void audit_free_context(struct audit_context *context)
898{
899 struct audit_context *previous;
900 int count = 0;
901
902 do {
903 previous = context->previous;
904 if (previous || (count && count < 10)) {
905 ++count;
906 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
907 " freeing multiple contexts (%d)\n",
908 context->serial, context->major,
909 context->name_count, count);
910 }
911 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -0400912 unroll_tree_refs(context, NULL, 0);
913 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400915 kfree(context->filterkey);
Al Viro4f6b4342008-12-09 19:50:34 -0500916 kfree(context->sockaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 kfree(context);
918 context = previous;
919 } while (context);
920 if (count >= 10)
921 printk(KERN_ERR "audit: freed %d contexts\n", count);
922}
923
Joy Latten161a09e2006-11-27 13:11:54 -0600924void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000925{
926 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000927 unsigned len;
928 int error;
929 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000930
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200931 security_task_getsecid(current, &sid);
Al Viroc4823bc2007-03-12 16:17:42 +0000932 if (!sid)
933 return;
934
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200935 error = security_secid_to_secctx(sid, &ctx, &len);
Al Viroc4823bc2007-03-12 16:17:42 +0000936 if (error) {
937 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000938 goto error_path;
939 return;
940 }
941
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000942 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200943 security_release_secctx(ctx, len);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000944 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000945
946error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000947 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000948 return;
949}
950
Joy Latten161a09e2006-11-27 13:11:54 -0600951EXPORT_SYMBOL(audit_log_task_context);
952
Al Viroe4951492006-03-29 20:17:10 -0500953static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700954{
Al Viro45d9bb02006-03-29 20:02:55 -0500955 char name[sizeof(tsk->comm)];
956 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700957 struct vm_area_struct *vma;
958
Al Viroe4951492006-03-29 20:17:10 -0500959 /* tsk == current */
960
Al Viro45d9bb02006-03-29 20:02:55 -0500961 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100962 audit_log_format(ab, " comm=");
963 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700964
Al Viroe4951492006-03-29 20:17:10 -0500965 if (mm) {
966 down_read(&mm->mmap_sem);
967 vma = mm->mmap;
968 while (vma) {
969 if ((vma->vm_flags & VM_EXECUTABLE) &&
970 vma->vm_file) {
971 audit_log_d_path(ab, "exe=",
Jan Blunck44707fd2008-02-14 19:38:33 -0800972 &vma->vm_file->f_path);
Al Viroe4951492006-03-29 20:17:10 -0500973 break;
974 }
975 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700976 }
Al Viroe4951492006-03-29 20:17:10 -0500977 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700978 }
Al Viroe4951492006-03-29 20:17:10 -0500979 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700980}
981
Amy Griffise54dc242007-03-29 18:01:04 -0400982static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric Paris4746ec52008-01-08 10:06:53 -0500983 uid_t auid, uid_t uid, unsigned int sessionid,
984 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400985{
986 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200987 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -0400988 u32 len;
989 int rc = 0;
990
991 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
992 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -0500993 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -0400994
Eric Paris4746ec52008-01-08 10:06:53 -0500995 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
996 uid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200997 if (security_secid_to_secctx(sid, &ctx, &len)) {
Eric Parisc2a77802008-01-07 13:40:17 -0500998 audit_log_format(ab, " obj=(none)");
Amy Griffise54dc242007-03-29 18:01:04 -0400999 rc = 1;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001000 } else {
1001 audit_log_format(ab, " obj=%s", ctx);
1002 security_release_secctx(ctx, len);
1003 }
Eric Parisc2a77802008-01-07 13:40:17 -05001004 audit_log_format(ab, " ocomm=");
1005 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -04001006 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -04001007
1008 return rc;
1009}
1010
Eric Parisde6bbd12008-01-07 14:31:58 -05001011/*
1012 * to_send and len_sent accounting are very loose estimates. We aren't
1013 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1014 * within about 500 bytes (next page boundry)
1015 *
1016 * why snprintf? an int is up to 12 digits long. if we just assumed when
1017 * logging that a[%d]= was going to be 16 characters long we would be wasting
1018 * space in every audit message. In one 7500 byte message we can log up to
1019 * about 1000 min size arguments. That comes down to about 50% waste of space
1020 * if we didn't do the snprintf to find out how long arg_num_len was.
1021 */
1022static int audit_log_single_execve_arg(struct audit_context *context,
1023 struct audit_buffer **ab,
1024 int arg_num,
1025 size_t *len_sent,
1026 const char __user *p,
1027 char *buf)
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001028{
Eric Parisde6bbd12008-01-07 14:31:58 -05001029 char arg_num_len_buf[12];
1030 const char __user *tmp_p = p;
Eric Parisb87ce6e2009-06-11 14:31:34 -04001031 /* how many digits are in arg_num? 5 is the length of ' a=""' */
1032 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 5;
Eric Parisde6bbd12008-01-07 14:31:58 -05001033 size_t len, len_left, to_send;
1034 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1035 unsigned int i, has_cntl = 0, too_long = 0;
1036 int ret;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001037
Eric Parisde6bbd12008-01-07 14:31:58 -05001038 /* strnlen_user includes the null we don't want to send */
1039 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001040
Eric Parisde6bbd12008-01-07 14:31:58 -05001041 /*
1042 * We just created this mm, if we can't find the strings
1043 * we just copied into it something is _very_ wrong. Similar
1044 * for strings that are too long, we should not have created
1045 * any.
1046 */
Eric Parisb0abcfc2008-02-18 18:23:16 -05001047 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001048 WARN_ON(1);
1049 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001050 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001051 }
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001052
Eric Parisde6bbd12008-01-07 14:31:58 -05001053 /* walk the whole argument looking for non-ascii chars */
1054 do {
1055 if (len_left > MAX_EXECVE_AUDIT_LEN)
1056 to_send = MAX_EXECVE_AUDIT_LEN;
1057 else
1058 to_send = len_left;
1059 ret = copy_from_user(buf, tmp_p, to_send);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001060 /*
1061 * There is no reason for this copy to be short. We just
1062 * copied them here, and the mm hasn't been exposed to user-
1063 * space yet.
1064 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001065 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001066 WARN_ON(1);
1067 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001068 return -1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001069 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001070 buf[to_send] = '\0';
1071 has_cntl = audit_string_contains_control(buf, to_send);
1072 if (has_cntl) {
1073 /*
1074 * hex messages get logged as 2 bytes, so we can only
1075 * send half as much in each message
1076 */
1077 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1078 break;
1079 }
1080 len_left -= to_send;
1081 tmp_p += to_send;
1082 } while (len_left > 0);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001083
Eric Parisde6bbd12008-01-07 14:31:58 -05001084 len_left = len;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001085
Eric Parisde6bbd12008-01-07 14:31:58 -05001086 if (len > max_execve_audit_len)
1087 too_long = 1;
1088
1089 /* rewalk the argument actually logging the message */
1090 for (i = 0; len_left > 0; i++) {
1091 int room_left;
1092
1093 if (len_left > max_execve_audit_len)
1094 to_send = max_execve_audit_len;
1095 else
1096 to_send = len_left;
1097
1098 /* do we have space left to send this argument in this ab? */
1099 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1100 if (has_cntl)
1101 room_left -= (to_send * 2);
1102 else
1103 room_left -= to_send;
1104 if (room_left < 0) {
1105 *len_sent = 0;
1106 audit_log_end(*ab);
1107 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1108 if (!*ab)
1109 return 0;
1110 }
1111
1112 /*
1113 * first record needs to say how long the original string was
1114 * so we can be sure nothing was lost.
1115 */
1116 if ((i == 0) && (too_long))
Jiri Pirkoca96a892009-01-09 16:44:16 +01001117 audit_log_format(*ab, " a%d_len=%zu", arg_num,
Eric Parisde6bbd12008-01-07 14:31:58 -05001118 has_cntl ? 2*len : len);
1119
1120 /*
1121 * normally arguments are small enough to fit and we already
1122 * filled buf above when we checked for control characters
1123 * so don't bother with another copy_from_user
1124 */
1125 if (len >= max_execve_audit_len)
1126 ret = copy_from_user(buf, p, to_send);
1127 else
1128 ret = 0;
1129 if (ret) {
1130 WARN_ON(1);
1131 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001132 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001133 }
1134 buf[to_send] = '\0';
1135
1136 /* actually log it */
Jiri Pirkoca96a892009-01-09 16:44:16 +01001137 audit_log_format(*ab, " a%d", arg_num);
Eric Parisde6bbd12008-01-07 14:31:58 -05001138 if (too_long)
1139 audit_log_format(*ab, "[%d]", i);
1140 audit_log_format(*ab, "=");
1141 if (has_cntl)
Eric Parisb556f8a2008-04-18 10:12:59 -04001142 audit_log_n_hex(*ab, buf, to_send);
Eric Parisde6bbd12008-01-07 14:31:58 -05001143 else
Eric Paris9d960982009-06-11 14:31:37 -04001144 audit_log_string(*ab, buf);
Eric Parisde6bbd12008-01-07 14:31:58 -05001145
1146 p += to_send;
1147 len_left -= to_send;
1148 *len_sent += arg_num_len;
1149 if (has_cntl)
1150 *len_sent += to_send * 2;
1151 else
1152 *len_sent += to_send;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001153 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001154 /* include the null we didn't log */
1155 return len + 1;
1156}
1157
1158static void audit_log_execve_info(struct audit_context *context,
1159 struct audit_buffer **ab,
1160 struct audit_aux_data_execve *axi)
1161{
1162 int i;
1163 size_t len, len_sent = 0;
1164 const char __user *p;
1165 char *buf;
1166
1167 if (axi->mm != current->mm)
1168 return; /* execve failed, no additional info */
1169
1170 p = (const char __user *)axi->mm->arg_start;
1171
Jiri Pirkoca96a892009-01-09 16:44:16 +01001172 audit_log_format(*ab, "argc=%d", axi->argc);
Eric Parisde6bbd12008-01-07 14:31:58 -05001173
1174 /*
1175 * we need some kernel buffer to hold the userspace args. Just
1176 * allocate one big one rather than allocating one of the right size
1177 * for every single argument inside audit_log_single_execve_arg()
1178 * should be <8k allocation so should be pretty safe.
1179 */
1180 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1181 if (!buf) {
1182 audit_panic("out of memory for argv string\n");
1183 return;
1184 }
1185
1186 for (i = 0; i < axi->argc; i++) {
1187 len = audit_log_single_execve_arg(context, ab, i,
1188 &len_sent, p, buf);
1189 if (len <= 0)
1190 break;
1191 p += len;
1192 }
1193 kfree(buf);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001194}
1195
Eric Paris851f7ff2008-11-11 21:48:14 +11001196static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1197{
1198 int i;
1199
1200 audit_log_format(ab, " %s=", prefix);
1201 CAP_FOR_EACH_U32(i) {
1202 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1203 }
1204}
1205
1206static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1207{
1208 kernel_cap_t *perm = &name->fcap.permitted;
1209 kernel_cap_t *inh = &name->fcap.inheritable;
1210 int log = 0;
1211
1212 if (!cap_isclear(*perm)) {
1213 audit_log_cap(ab, "cap_fp", perm);
1214 log = 1;
1215 }
1216 if (!cap_isclear(*inh)) {
1217 audit_log_cap(ab, "cap_fi", inh);
1218 log = 1;
1219 }
1220
1221 if (log)
1222 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1223}
1224
Al Viroa33e6752008-12-10 03:40:06 -05001225static void show_special(struct audit_context *context, int *call_panic)
Al Virof3298dc2008-12-10 03:16:51 -05001226{
1227 struct audit_buffer *ab;
1228 int i;
1229
1230 ab = audit_log_start(context, GFP_KERNEL, context->type);
1231 if (!ab)
1232 return;
1233
1234 switch (context->type) {
1235 case AUDIT_SOCKETCALL: {
1236 int nargs = context->socketcall.nargs;
1237 audit_log_format(ab, "nargs=%d", nargs);
1238 for (i = 0; i < nargs; i++)
1239 audit_log_format(ab, " a%d=%lx", i,
1240 context->socketcall.args[i]);
1241 break; }
Al Viroa33e6752008-12-10 03:40:06 -05001242 case AUDIT_IPC: {
1243 u32 osid = context->ipc.osid;
1244
1245 audit_log_format(ab, "ouid=%u ogid=%u mode=%#o",
1246 context->ipc.uid, context->ipc.gid, context->ipc.mode);
1247 if (osid) {
1248 char *ctx = NULL;
1249 u32 len;
1250 if (security_secid_to_secctx(osid, &ctx, &len)) {
1251 audit_log_format(ab, " osid=%u", osid);
1252 *call_panic = 1;
1253 } else {
1254 audit_log_format(ab, " obj=%s", ctx);
1255 security_release_secctx(ctx, len);
1256 }
1257 }
Al Viroe816f372008-12-10 03:47:15 -05001258 if (context->ipc.has_perm) {
1259 audit_log_end(ab);
1260 ab = audit_log_start(context, GFP_KERNEL,
1261 AUDIT_IPC_SET_PERM);
1262 audit_log_format(ab,
1263 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1264 context->ipc.qbytes,
1265 context->ipc.perm_uid,
1266 context->ipc.perm_gid,
1267 context->ipc.perm_mode);
1268 if (!ab)
1269 return;
1270 }
Al Viroa33e6752008-12-10 03:40:06 -05001271 break; }
Al Viro564f6992008-12-14 04:02:26 -05001272 case AUDIT_MQ_OPEN: {
1273 audit_log_format(ab,
1274 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1275 "mq_msgsize=%ld mq_curmsgs=%ld",
1276 context->mq_open.oflag, context->mq_open.mode,
1277 context->mq_open.attr.mq_flags,
1278 context->mq_open.attr.mq_maxmsg,
1279 context->mq_open.attr.mq_msgsize,
1280 context->mq_open.attr.mq_curmsgs);
1281 break; }
Al Viroc32c8af2008-12-14 03:46:48 -05001282 case AUDIT_MQ_SENDRECV: {
1283 audit_log_format(ab,
1284 "mqdes=%d msg_len=%zd msg_prio=%u "
1285 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1286 context->mq_sendrecv.mqdes,
1287 context->mq_sendrecv.msg_len,
1288 context->mq_sendrecv.msg_prio,
1289 context->mq_sendrecv.abs_timeout.tv_sec,
1290 context->mq_sendrecv.abs_timeout.tv_nsec);
1291 break; }
Al Viro20114f72008-12-10 07:16:12 -05001292 case AUDIT_MQ_NOTIFY: {
1293 audit_log_format(ab, "mqdes=%d sigev_signo=%d",
1294 context->mq_notify.mqdes,
1295 context->mq_notify.sigev_signo);
1296 break; }
Al Viro73929062008-12-10 06:58:59 -05001297 case AUDIT_MQ_GETSETATTR: {
1298 struct mq_attr *attr = &context->mq_getsetattr.mqstat;
1299 audit_log_format(ab,
1300 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1301 "mq_curmsgs=%ld ",
1302 context->mq_getsetattr.mqdes,
1303 attr->mq_flags, attr->mq_maxmsg,
1304 attr->mq_msgsize, attr->mq_curmsgs);
1305 break; }
Al Viro57f71a02009-01-04 14:52:57 -05001306 case AUDIT_CAPSET: {
1307 audit_log_format(ab, "pid=%d", context->capset.pid);
1308 audit_log_cap(ab, "cap_pi", &context->capset.cap.inheritable);
1309 audit_log_cap(ab, "cap_pp", &context->capset.cap.permitted);
1310 audit_log_cap(ab, "cap_pe", &context->capset.cap.effective);
1311 break; }
Al Viro120a7952010-10-30 02:54:44 -04001312 case AUDIT_MMAP: {
1313 audit_log_format(ab, "fd=%d flags=0x%x", context->mmap.fd,
1314 context->mmap.flags);
1315 break; }
Al Virof3298dc2008-12-10 03:16:51 -05001316 }
1317 audit_log_end(ab);
1318}
1319
Al Viroe4951492006-03-29 20:17:10 -05001320static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
David Howellsc69e8d92008-11-14 10:39:19 +11001322 const struct cred *cred;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001323 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001325 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -05001326 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Al Viroe4951492006-03-29 20:17:10 -05001328 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001329 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -04001330 if (!context->ppid)
1331 context->ppid = sys_getppid();
David Howellsc69e8d92008-11-14 10:39:19 +11001332 cred = current_cred();
1333 context->uid = cred->uid;
1334 context->gid = cred->gid;
1335 context->euid = cred->euid;
1336 context->suid = cred->suid;
David Howellsb6dff3e2008-11-14 10:39:16 +11001337 context->fsuid = cred->fsuid;
David Howellsc69e8d92008-11-14 10:39:19 +11001338 context->egid = cred->egid;
1339 context->sgid = cred->sgid;
David Howellsb6dff3e2008-11-14 10:39:16 +11001340 context->fsgid = cred->fsgid;
Al Viro3f2792f2006-07-16 06:43:48 -04001341 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001342
1343 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!ab)
1345 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001346 audit_log_format(ab, "arch=%x syscall=%d",
1347 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (context->personality != PER_LINUX)
1349 audit_log_format(ab, " per=%lx", context->personality);
1350 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001351 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001352 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1353 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001354
Alan Coxdbda4c02008-10-13 10:40:53 +01001355 spin_lock_irq(&tsk->sighand->siglock);
Al Viro45d9bb02006-03-29 20:02:55 -05001356 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1357 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -05001358 else
1359 tty = "(none)";
Alan Coxdbda4c02008-10-13 10:40:53 +01001360 spin_unlock_irq(&tsk->sighand->siglock);
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 audit_log_format(ab,
1363 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -04001364 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +01001365 " euid=%u suid=%u fsuid=%u"
Eric Paris4746ec52008-01-08 10:06:53 -05001366 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 context->argv[0],
1368 context->argv[1],
1369 context->argv[2],
1370 context->argv[3],
1371 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -04001372 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 context->pid,
Al Virobfef93a2008-01-10 04:53:18 -05001374 tsk->loginuid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 context->uid,
1376 context->gid,
1377 context->euid, context->suid, context->fsuid,
Eric Paris4746ec52008-01-08 10:06:53 -05001378 context->egid, context->sgid, context->fsgid, tty,
1379 tsk->sessionid);
Alan Coxeb84a202006-09-29 02:01:41 -07001380
Alan Coxeb84a202006-09-29 02:01:41 -07001381
Al Viroe4951492006-03-29 20:17:10 -05001382 audit_log_task_info(ab, tsk);
Eric Paris9d960982009-06-11 14:31:37 -04001383 audit_log_key(ab, context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
David Woodhouse7551ced2005-05-26 12:04:57 +01001386 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001387
Al Viroe4951492006-03-29 20:17:10 -05001388 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (!ab)
1390 continue; /* audit_panic has been called */
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001393
Al Viro473ae302006-04-26 14:04:08 -04001394 case AUDIT_EXECVE: {
1395 struct audit_aux_data_execve *axi = (void *)aux;
Eric Parisde6bbd12008-01-07 14:31:58 -05001396 audit_log_execve_info(context, &ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001397 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001398
Eric Paris3fc689e2008-11-11 21:48:18 +11001399 case AUDIT_BPRM_FCAPS: {
1400 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1401 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1402 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1403 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1404 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1405 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1406 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1407 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1408 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1409 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1410 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1411 break; }
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Al Virof3298dc2008-12-10 03:16:51 -05001417 if (context->type)
Al Viroa33e6752008-12-10 03:40:06 -05001418 show_special(context, &call_panic);
Al Virof3298dc2008-12-10 03:16:51 -05001419
Al Viro157cf642008-12-14 04:57:47 -05001420 if (context->fds[0] >= 0) {
1421 ab = audit_log_start(context, GFP_KERNEL, AUDIT_FD_PAIR);
1422 if (ab) {
1423 audit_log_format(ab, "fd0=%d fd1=%d",
1424 context->fds[0], context->fds[1]);
1425 audit_log_end(ab);
1426 }
1427 }
1428
Al Viro4f6b4342008-12-09 19:50:34 -05001429 if (context->sockaddr_len) {
1430 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SOCKADDR);
1431 if (ab) {
1432 audit_log_format(ab, "saddr=");
1433 audit_log_n_hex(ab, (void *)context->sockaddr,
1434 context->sockaddr_len);
1435 audit_log_end(ab);
1436 }
1437 }
1438
Amy Griffise54dc242007-03-29 18:01:04 -04001439 for (aux = context->aux_pids; aux; aux = aux->next) {
1440 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001441
1442 for (i = 0; i < axs->pid_count; i++)
1443 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001444 axs->target_auid[i],
1445 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001446 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001447 axs->target_sid[i],
1448 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001449 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001450 }
1451
Amy Griffise54dc242007-03-29 18:01:04 -04001452 if (context->target_pid &&
1453 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001454 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001455 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001456 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001457 call_panic = 1;
1458
Jan Blunck44707fd2008-02-14 19:38:33 -08001459 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001460 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001461 if (ab) {
Jan Blunck44707fd2008-02-14 19:38:33 -08001462 audit_log_d_path(ab, "cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001463 audit_log_end(ab);
1464 }
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -04001467 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +00001468
Al Viroe4951492006-03-29 20:17:10 -05001469 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (!ab)
1471 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +01001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +00001474
Amy Griffis9c937dc2006-06-08 23:19:31 -04001475 if (n->name) {
1476 switch(n->name_len) {
1477 case AUDIT_NAME_FULL:
1478 /* log the full path */
1479 audit_log_format(ab, " name=");
1480 audit_log_untrustedstring(ab, n->name);
1481 break;
1482 case 0:
1483 /* name was specified as a relative path and the
1484 * directory component is the cwd */
Eric Parisdef57542009-03-10 18:00:14 -04001485 audit_log_d_path(ab, "name=", &context->pwd);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001486 break;
1487 default:
1488 /* log the name's directory component */
1489 audit_log_format(ab, " name=");
Eric Parisb556f8a2008-04-18 10:12:59 -04001490 audit_log_n_untrustedstring(ab, n->name,
1491 n->name_len);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001492 }
1493 } else
1494 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001495
Amy Griffis9c937dc2006-06-08 23:19:31 -04001496 if (n->ino != (unsigned long)-1) {
1497 audit_log_format(ab, " inode=%lu"
1498 " dev=%02x:%02x mode=%#o"
1499 " ouid=%u ogid=%u rdev=%02x:%02x",
1500 n->ino,
1501 MAJOR(n->dev),
1502 MINOR(n->dev),
1503 n->mode,
1504 n->uid,
1505 n->gid,
1506 MAJOR(n->rdev),
1507 MINOR(n->rdev));
1508 }
1509 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001510 char *ctx = NULL;
1511 u32 len;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001512 if (security_secid_to_secctx(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001513 n->osid, &ctx, &len)) {
1514 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001515 call_panic = 2;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001516 } else {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001517 audit_log_format(ab, " obj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001518 security_release_secctx(ctx, len);
1519 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001520 }
1521
Eric Paris851f7ff2008-11-11 21:48:14 +11001522 audit_log_fcaps(ab, n);
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 audit_log_end(ab);
1525 }
Eric Parisc0641f22008-01-07 13:49:15 -05001526
1527 /* Send end of event record to help user space know we are finished */
1528 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1529 if (ab)
1530 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001531 if (call_panic)
1532 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001535/**
1536 * audit_free - free a per-task audit context
1537 * @tsk: task whose audit context block to free
1538 *
Al Virofa84cb92006-03-29 20:30:19 -05001539 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541void audit_free(struct task_struct *tsk)
1542{
1543 struct audit_context *context;
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (likely(!context))
1547 return;
1548
1549 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001550 * function (e.g., exit_group), then free context block.
1551 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001552 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001553 /* that can happen only if we are called from do_exit() */
Al Viro0590b932008-12-14 23:45:27 -05001554 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001555 audit_log_exit(context, tsk);
Al Viro916d7572009-06-24 00:02:38 -04001556 if (!list_empty(&context->killed_trees))
1557 audit_kill_trees(&context->killed_trees);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 audit_free_context(context);
1560}
1561
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001562/**
1563 * audit_syscall_entry - fill in an audit record at syscall entry
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001564 * @arch: architecture type
1565 * @major: major syscall type (function)
1566 * @a1: additional syscall register 1
1567 * @a2: additional syscall register 2
1568 * @a3: additional syscall register 3
1569 * @a4: additional syscall register 4
1570 *
1571 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * audit context was created when the task was created and the state or
1573 * filters demand the audit context be built. If the state from the
1574 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1575 * then the record will be written at syscall exit time (otherwise, it
1576 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001577 * be written).
1578 */
Al Viro5411be52006-03-29 20:23:36 -05001579void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 unsigned long a1, unsigned long a2,
1581 unsigned long a3, unsigned long a4)
1582{
Al Viro5411be52006-03-29 20:23:36 -05001583 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 struct audit_context *context = tsk->audit_context;
1585 enum audit_state state;
1586
Roland McGrath86a1c342008-06-23 15:37:04 -07001587 if (unlikely(!context))
1588 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001590 /*
1591 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 * calls in kernel_thread via the entry.S interface, instead of
1593 * with direct calls. (If you are porting to a new
1594 * architecture, hitting this condition can indicate that you
1595 * got the _exit/_leave calls backward in entry.S.)
1596 *
1597 * i386 no
1598 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001599 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 *
1601 * This also happens with vm86 emulation in a non-nested manner
1602 * (entries without exits), so this case must be caught.
1603 */
1604 if (context->in_syscall) {
1605 struct audit_context *newctx;
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607#if AUDIT_DEBUG
1608 printk(KERN_ERR
1609 "audit(:%d) pid=%d in syscall=%d;"
1610 " entering syscall=%d\n",
1611 context->serial, tsk->pid, context->major, major);
1612#endif
1613 newctx = audit_alloc_context(context->state);
1614 if (newctx) {
1615 newctx->previous = context;
1616 context = newctx;
1617 tsk->audit_context = newctx;
1618 } else {
1619 /* If we can't alloc a new context, the best we
1620 * can do is to leak memory (any pending putname
1621 * will be lost). The only other alternative is
1622 * to abandon auditing. */
1623 audit_zero_context(context, context->state);
1624 }
1625 }
1626 BUG_ON(context->in_syscall || context->name_count);
1627
1628 if (!audit_enabled)
1629 return;
1630
2fd6f582005-04-29 16:08:28 +01001631 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 context->major = major;
1633 context->argv[0] = a1;
1634 context->argv[1] = a2;
1635 context->argv[2] = a3;
1636 context->argv[3] = a4;
1637
1638 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001639 context->dummy = !audit_n_rules;
Al Viro0590b932008-12-14 23:45:27 -05001640 if (!context->dummy && state == AUDIT_BUILD_CONTEXT) {
1641 context->prio = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +01001642 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Al Viro0590b932008-12-14 23:45:27 -05001643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (likely(state == AUDIT_DISABLED))
1645 return;
1646
David Woodhousece625a82005-07-18 14:24:46 -04001647 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 context->ctime = CURRENT_TIME;
1649 context->in_syscall = 1;
Al Viro0590b932008-12-14 23:45:27 -05001650 context->current_state = state;
Alexander Viro419c58f2006-09-29 00:08:50 -04001651 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Al Viroa64e6492008-11-12 18:37:41 -05001654void audit_finish_fork(struct task_struct *child)
1655{
1656 struct audit_context *ctx = current->audit_context;
1657 struct audit_context *p = child->audit_context;
Al Viro0590b932008-12-14 23:45:27 -05001658 if (!p || !ctx)
1659 return;
1660 if (!ctx->in_syscall || ctx->current_state != AUDIT_RECORD_CONTEXT)
Al Viroa64e6492008-11-12 18:37:41 -05001661 return;
1662 p->arch = ctx->arch;
1663 p->major = ctx->major;
1664 memcpy(p->argv, ctx->argv, sizeof(ctx->argv));
1665 p->ctime = ctx->ctime;
1666 p->dummy = ctx->dummy;
Al Viroa64e6492008-11-12 18:37:41 -05001667 p->in_syscall = ctx->in_syscall;
1668 p->filterkey = kstrdup(ctx->filterkey, GFP_KERNEL);
1669 p->ppid = current->pid;
Al Viro0590b932008-12-14 23:45:27 -05001670 p->prio = ctx->prio;
1671 p->current_state = ctx->current_state;
Al Viroa64e6492008-11-12 18:37:41 -05001672}
1673
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001674/**
1675 * audit_syscall_exit - deallocate audit context after a system call
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001676 * @valid: success/failure flag
1677 * @return_code: syscall return value
1678 *
1679 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1681 * filtering, or because some other part of the kernel write an audit
1682 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001683 * free the names stored from getname().
1684 */
Al Viro5411be52006-03-29 20:23:36 -05001685void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Al Viro5411be52006-03-29 20:23:36 -05001687 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 struct audit_context *context;
1689
2fd6f582005-04-29 16:08:28 +01001690 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001693 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Al Viro0590b932008-12-14 23:45:27 -05001695 if (context->in_syscall && context->current_state == AUDIT_RECORD_CONTEXT)
Al Viroe4951492006-03-29 20:17:10 -05001696 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 context->in_syscall = 0;
Al Viro0590b932008-12-14 23:45:27 -05001699 context->prio = context->state == AUDIT_RECORD_CONTEXT ? ~0ULL : 0;
2fd6f582005-04-29 16:08:28 +01001700
Al Viro916d7572009-06-24 00:02:38 -04001701 if (!list_empty(&context->killed_trees))
1702 audit_kill_trees(&context->killed_trees);
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (context->previous) {
1705 struct audit_context *new_context = context->previous;
1706 context->previous = NULL;
1707 audit_free_context(context);
1708 tsk->audit_context = new_context;
1709 } else {
1710 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001711 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001713 context->aux = NULL;
1714 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001715 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001716 context->target_sid = 0;
Al Viro4f6b4342008-12-09 19:50:34 -05001717 context->sockaddr_len = 0;
Al Virof3298dc2008-12-10 03:16:51 -05001718 context->type = 0;
Al Viro157cf642008-12-14 04:57:47 -05001719 context->fds[0] = -1;
Al Viroe048e022008-12-16 03:51:22 -05001720 if (context->state != AUDIT_RECORD_CONTEXT) {
1721 kfree(context->filterkey);
1722 context->filterkey = NULL;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 tsk->audit_context = context;
1725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Al Viro74c3cbe2007-07-22 08:04:18 -04001728static inline void handle_one(const struct inode *inode)
1729{
1730#ifdef CONFIG_AUDIT_TREE
1731 struct audit_context *context;
1732 struct audit_tree_refs *p;
1733 struct audit_chunk *chunk;
1734 int count;
Eric Parise61ce862009-12-17 21:24:24 -05001735 if (likely(hlist_empty(&inode->i_fsnotify_marks)))
Al Viro74c3cbe2007-07-22 08:04:18 -04001736 return;
1737 context = current->audit_context;
1738 p = context->trees;
1739 count = context->tree_count;
1740 rcu_read_lock();
1741 chunk = audit_tree_lookup(inode);
1742 rcu_read_unlock();
1743 if (!chunk)
1744 return;
1745 if (likely(put_tree_ref(context, chunk)))
1746 return;
1747 if (unlikely(!grow_tree_refs(context))) {
Eric Paris436c4052008-04-18 10:01:04 -04001748 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001749 audit_set_auditable(context);
1750 audit_put_chunk(chunk);
1751 unroll_tree_refs(context, p, count);
1752 return;
1753 }
1754 put_tree_ref(context, chunk);
1755#endif
1756}
1757
1758static void handle_path(const struct dentry *dentry)
1759{
1760#ifdef CONFIG_AUDIT_TREE
1761 struct audit_context *context;
1762 struct audit_tree_refs *p;
1763 const struct dentry *d, *parent;
1764 struct audit_chunk *drop;
1765 unsigned long seq;
1766 int count;
1767
1768 context = current->audit_context;
1769 p = context->trees;
1770 count = context->tree_count;
1771retry:
1772 drop = NULL;
1773 d = dentry;
1774 rcu_read_lock();
1775 seq = read_seqbegin(&rename_lock);
1776 for(;;) {
1777 struct inode *inode = d->d_inode;
Eric Parise61ce862009-12-17 21:24:24 -05001778 if (inode && unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
Al Viro74c3cbe2007-07-22 08:04:18 -04001779 struct audit_chunk *chunk;
1780 chunk = audit_tree_lookup(inode);
1781 if (chunk) {
1782 if (unlikely(!put_tree_ref(context, chunk))) {
1783 drop = chunk;
1784 break;
1785 }
1786 }
1787 }
1788 parent = d->d_parent;
1789 if (parent == d)
1790 break;
1791 d = parent;
1792 }
1793 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1794 rcu_read_unlock();
1795 if (!drop) {
1796 /* just a race with rename */
1797 unroll_tree_refs(context, p, count);
1798 goto retry;
1799 }
1800 audit_put_chunk(drop);
1801 if (grow_tree_refs(context)) {
1802 /* OK, got more space */
1803 unroll_tree_refs(context, p, count);
1804 goto retry;
1805 }
1806 /* too bad */
1807 printk(KERN_WARNING
Eric Paris436c4052008-04-18 10:01:04 -04001808 "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001809 unroll_tree_refs(context, p, count);
1810 audit_set_auditable(context);
1811 return;
1812 }
1813 rcu_read_unlock();
1814#endif
1815}
1816
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001817/**
1818 * audit_getname - add a name to the list
1819 * @name: name to add
1820 *
1821 * Add a name to the list of audit names for this context.
1822 * Called from fs/namei.c:getname().
1823 */
Al Virod8945bb52006-05-18 16:01:30 -04001824void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 struct audit_context *context = current->audit_context;
1827
Al Virod8945bb52006-05-18 16:01:30 -04001828 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return;
1830
1831 if (!context->in_syscall) {
1832#if AUDIT_DEBUG == 2
1833 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1834 __FILE__, __LINE__, context->serial, name);
1835 dump_stack();
1836#endif
1837 return;
1838 }
1839 BUG_ON(context->name_count >= AUDIT_NAMES);
1840 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001841 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1842 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 context->names[context->name_count].ino = (unsigned long)-1;
Amy Griffise41e8bd2007-02-13 14:14:09 -05001844 context->names[context->name_count].osid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 ++context->name_count;
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02001846 if (!context->pwd.dentry)
1847 get_fs_pwd(current->fs, &context->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
1849
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001850/* audit_putname - intercept a putname request
1851 * @name: name to intercept and delay for putname
1852 *
1853 * If we have stored the name from getname in the audit context,
1854 * then we delay the putname until syscall exit.
1855 * Called from include/linux/fs.h:putname().
1856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857void audit_putname(const char *name)
1858{
1859 struct audit_context *context = current->audit_context;
1860
1861 BUG_ON(!context);
1862 if (!context->in_syscall) {
1863#if AUDIT_DEBUG == 2
1864 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1865 __FILE__, __LINE__, context->serial, name);
1866 if (context->name_count) {
1867 int i;
1868 for (i = 0; i < context->name_count; i++)
1869 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1870 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001871 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873#endif
1874 __putname(name);
1875 }
1876#if AUDIT_DEBUG
1877 else {
1878 ++context->put_count;
1879 if (context->put_count > context->name_count) {
1880 printk(KERN_ERR "%s:%d(:%d): major=%d"
1881 " in_syscall=%d putname(%p) name_count=%d"
1882 " put_count=%d\n",
1883 __FILE__, __LINE__,
1884 context->serial, context->major,
1885 context->in_syscall, name, context->name_count,
1886 context->put_count);
1887 dump_stack();
1888 }
1889 }
1890#endif
1891}
1892
Amy Griffis5712e882007-02-13 14:15:22 -05001893static int audit_inc_name_count(struct audit_context *context,
1894 const struct inode *inode)
1895{
1896 if (context->name_count >= AUDIT_NAMES) {
1897 if (inode)
Eric Paris449cedf2010-04-05 16:16:26 -04001898 printk(KERN_DEBUG "audit: name_count maxed, losing inode data: "
Eric Paris436c4052008-04-18 10:01:04 -04001899 "dev=%02x:%02x, inode=%lu\n",
Amy Griffis5712e882007-02-13 14:15:22 -05001900 MAJOR(inode->i_sb->s_dev),
1901 MINOR(inode->i_sb->s_dev),
1902 inode->i_ino);
1903
1904 else
Eric Paris436c4052008-04-18 10:01:04 -04001905 printk(KERN_DEBUG "name_count maxed, losing inode data\n");
Amy Griffis5712e882007-02-13 14:15:22 -05001906 return 1;
1907 }
1908 context->name_count++;
1909#if AUDIT_DEBUG
1910 context->ino_count++;
1911#endif
1912 return 0;
1913}
1914
Eric Paris851f7ff2008-11-11 21:48:14 +11001915
1916static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1917{
1918 struct cpu_vfs_cap_data caps;
1919 int rc;
1920
1921 memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
1922 memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
1923 name->fcap.fE = 0;
1924 name->fcap_ver = 0;
1925
1926 if (!dentry)
1927 return 0;
1928
1929 rc = get_vfs_caps_from_disk(dentry, &caps);
1930 if (rc)
1931 return rc;
1932
1933 name->fcap.permitted = caps.permitted;
1934 name->fcap.inheritable = caps.inheritable;
1935 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1936 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1937
1938 return 0;
1939}
1940
1941
Amy Griffis3e2efce2006-07-13 13:16:02 -04001942/* Copy inode data into an audit_names. */
Eric Paris851f7ff2008-11-11 21:48:14 +11001943static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1944 const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001945{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001946 name->ino = inode->i_ino;
1947 name->dev = inode->i_sb->s_dev;
1948 name->mode = inode->i_mode;
1949 name->uid = inode->i_uid;
1950 name->gid = inode->i_gid;
1951 name->rdev = inode->i_rdev;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001952 security_inode_getsecid(inode, &name->osid);
Eric Paris851f7ff2008-11-11 21:48:14 +11001953 audit_copy_fcaps(name, dentry);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001954}
1955
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001956/**
1957 * audit_inode - store the inode and device from a lookup
1958 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001959 * @dentry: dentry being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001960 *
1961 * Called from fs/namei.c:path_lookup().
1962 */
Al Viro5a190ae2007-06-07 12:19:32 -04001963void __audit_inode(const char *name, const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 int idx;
1966 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04001967 const struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 if (!context->in_syscall)
1970 return;
1971 if (context->name_count
1972 && context->names[context->name_count-1].name
1973 && context->names[context->name_count-1].name == name)
1974 idx = context->name_count - 1;
1975 else if (context->name_count > 1
1976 && context->names[context->name_count-2].name
1977 && context->names[context->name_count-2].name == name)
1978 idx = context->name_count - 2;
1979 else {
1980 /* FIXME: how much do we care about inodes that have no
1981 * associated name? */
Amy Griffis5712e882007-02-13 14:15:22 -05001982 if (audit_inc_name_count(context, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001984 idx = context->name_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 context->names[idx].name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001987 handle_path(dentry);
Eric Paris851f7ff2008-11-11 21:48:14 +11001988 audit_copy_inode(&context->names[idx], dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Amy Griffis73241cc2005-11-03 16:00:25 +00001991/**
1992 * audit_inode_child - collect inode info for created/removed objects
Randy Dunlap481968f2007-10-21 20:59:53 -07001993 * @dentry: dentry being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04001994 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00001995 *
1996 * For syscalls that create or remove filesystem objects, audit_inode
1997 * can only collect information for the filesystem object's parent.
1998 * This call updates the audit context with the child's information.
1999 * Syscalls that create a new filesystem object must be hooked after
2000 * the object is created. Syscalls that remove a filesystem object
2001 * must be hooked prior, in order to capture the target inode during
2002 * unsuccessful attempts.
2003 */
Al Virocccc6bb2009-12-25 05:07:33 -05002004void __audit_inode_child(const struct dentry *dentry,
Amy Griffis73d3ec52006-07-13 13:16:39 -04002005 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00002006{
2007 int idx;
2008 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05002009 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04002010 const struct inode *inode = dentry->d_inode;
Al Virocccc6bb2009-12-25 05:07:33 -05002011 const char *dname = dentry->d_name.name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04002012 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00002013
2014 if (!context->in_syscall)
2015 return;
2016
Al Viro74c3cbe2007-07-22 08:04:18 -04002017 if (inode)
2018 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00002019
Amy Griffis5712e882007-02-13 14:15:22 -05002020 /* parent is more likely, look for it first */
2021 for (idx = 0; idx < context->name_count; idx++) {
2022 struct audit_names *n = &context->names[idx];
Amy Griffis73241cc2005-11-03 16:00:25 +00002023
Amy Griffis5712e882007-02-13 14:15:22 -05002024 if (!n->name)
2025 continue;
2026
2027 if (n->ino == parent->i_ino &&
2028 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2029 n->name_len = dirlen; /* update parent data in place */
2030 found_parent = n->name;
2031 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04002032 }
Steve Grubbac9910c2006-09-28 14:31:32 -04002033 }
Amy Griffis73241cc2005-11-03 16:00:25 +00002034
Amy Griffis5712e882007-02-13 14:15:22 -05002035 /* no matching parent, look for matching child */
2036 for (idx = 0; idx < context->name_count; idx++) {
2037 struct audit_names *n = &context->names[idx];
Amy Griffis73d3ec52006-07-13 13:16:39 -04002038
Amy Griffis5712e882007-02-13 14:15:22 -05002039 if (!n->name)
2040 continue;
2041
2042 /* strcmp() is the more likely scenario */
2043 if (!strcmp(dname, n->name) ||
2044 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2045 if (inode)
Eric Paris851f7ff2008-11-11 21:48:14 +11002046 audit_copy_inode(n, NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002047 else
2048 n->ino = (unsigned long)-1;
2049 found_child = n->name;
2050 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04002051 }
Amy Griffis5712e882007-02-13 14:15:22 -05002052 }
2053
2054add_names:
2055 if (!found_parent) {
2056 if (audit_inc_name_count(context, parent))
2057 return;
2058 idx = context->name_count - 1;
2059 context->names[idx].name = NULL;
Eric Paris851f7ff2008-11-11 21:48:14 +11002060 audit_copy_inode(&context->names[idx], NULL, parent);
Amy Griffis73d3ec52006-07-13 13:16:39 -04002061 }
Amy Griffis5712e882007-02-13 14:15:22 -05002062
2063 if (!found_child) {
2064 if (audit_inc_name_count(context, inode))
2065 return;
2066 idx = context->name_count - 1;
2067
2068 /* Re-use the name belonging to the slot for a matching parent
2069 * directory. All names for this context are relinquished in
2070 * audit_free_names() */
2071 if (found_parent) {
2072 context->names[idx].name = found_parent;
2073 context->names[idx].name_len = AUDIT_NAME_FULL;
2074 /* don't call __putname() */
2075 context->names[idx].name_put = 0;
2076 } else {
2077 context->names[idx].name = NULL;
2078 }
2079
2080 if (inode)
Eric Paris851f7ff2008-11-11 21:48:14 +11002081 audit_copy_inode(&context->names[idx], NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002082 else
2083 context->names[idx].ino = (unsigned long)-1;
2084 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04002085}
Trond Myklebust50e437d2007-06-07 22:44:34 -04002086EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04002087
2088/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002089 * auditsc_get_stamp - get local copies of audit_context values
2090 * @ctx: audit_context for the task
2091 * @t: timespec to store time recorded in the audit_context
2092 * @serial: serial value that is recorded in the audit_context
2093 *
2094 * Also sets the context as auditable.
2095 */
Al Viro48887e62008-12-06 01:05:50 -05002096int auditsc_get_stamp(struct audit_context *ctx,
David Woodhousebfb44962005-05-21 21:08:09 +01002097 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Al Viro48887e62008-12-06 01:05:50 -05002099 if (!ctx->in_syscall)
2100 return 0;
David Woodhousece625a82005-07-18 14:24:46 -04002101 if (!ctx->serial)
2102 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01002103 t->tv_sec = ctx->ctime.tv_sec;
2104 t->tv_nsec = ctx->ctime.tv_nsec;
2105 *serial = ctx->serial;
Al Viro0590b932008-12-14 23:45:27 -05002106 if (!ctx->prio) {
2107 ctx->prio = 1;
2108 ctx->current_state = AUDIT_RECORD_CONTEXT;
2109 }
Al Viro48887e62008-12-06 01:05:50 -05002110 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Eric Paris4746ec52008-01-08 10:06:53 -05002113/* global counter which is incremented every time something logs in */
2114static atomic_t session_id = ATOMIC_INIT(0);
2115
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002116/**
2117 * audit_set_loginuid - set a task's audit_context loginuid
2118 * @task: task whose audit context is being modified
2119 * @loginuid: loginuid value
2120 *
2121 * Returns 0.
2122 *
2123 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2124 */
Steve Grubb456be6c2005-04-29 17:30:07 +01002125int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Eric Paris4746ec52008-01-08 10:06:53 -05002127 unsigned int sessionid = atomic_inc_return(&session_id);
Steve Grubb41757102006-06-12 07:48:28 -04002128 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01002129
Al Virobfef93a2008-01-10 04:53:18 -05002130 if (context && context->in_syscall) {
2131 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04002132
Al Virobfef93a2008-01-10 04:53:18 -05002133 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2134 if (ab) {
2135 audit_log_format(ab, "login pid=%d uid=%u "
Eric Paris4746ec52008-01-08 10:06:53 -05002136 "old auid=%u new auid=%u"
2137 " old ses=%u new ses=%u",
David Howellsc69e8d92008-11-14 10:39:19 +11002138 task->pid, task_uid(task),
Eric Paris4746ec52008-01-08 10:06:53 -05002139 task->loginuid, loginuid,
2140 task->sessionid, sessionid);
Al Virobfef93a2008-01-10 04:53:18 -05002141 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01002142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
Eric Paris4746ec52008-01-08 10:06:53 -05002144 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05002145 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return 0;
2147}
2148
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002149/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002150 * __audit_mq_open - record audit data for a POSIX MQ open
2151 * @oflag: open flag
2152 * @mode: mode bits
Randy Dunlap6b962552009-01-05 13:41:13 -08002153 * @attr: queue attributes
George C. Wilson20ca73b2006-05-24 16:09:55 -05002154 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002155 */
Al Viro564f6992008-12-14 04:02:26 -05002156void __audit_mq_open(int oflag, mode_t mode, struct mq_attr *attr)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002157{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002158 struct audit_context *context = current->audit_context;
2159
Al Viro564f6992008-12-14 04:02:26 -05002160 if (attr)
2161 memcpy(&context->mq_open.attr, attr, sizeof(struct mq_attr));
2162 else
2163 memset(&context->mq_open.attr, 0, sizeof(struct mq_attr));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002164
Al Viro564f6992008-12-14 04:02:26 -05002165 context->mq_open.oflag = oflag;
2166 context->mq_open.mode = mode;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002167
Al Viro564f6992008-12-14 04:02:26 -05002168 context->type = AUDIT_MQ_OPEN;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002169}
2170
2171/**
Al Viroc32c8af2008-12-14 03:46:48 -05002172 * __audit_mq_sendrecv - record audit data for a POSIX MQ timed send/receive
George C. Wilson20ca73b2006-05-24 16:09:55 -05002173 * @mqdes: MQ descriptor
2174 * @msg_len: Message length
2175 * @msg_prio: Message priority
Al Viroc32c8af2008-12-14 03:46:48 -05002176 * @abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002177 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002178 */
Al Viroc32c8af2008-12-14 03:46:48 -05002179void __audit_mq_sendrecv(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2180 const struct timespec *abs_timeout)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002181{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002182 struct audit_context *context = current->audit_context;
Al Viroc32c8af2008-12-14 03:46:48 -05002183 struct timespec *p = &context->mq_sendrecv.abs_timeout;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002184
Al Viroc32c8af2008-12-14 03:46:48 -05002185 if (abs_timeout)
2186 memcpy(p, abs_timeout, sizeof(struct timespec));
2187 else
2188 memset(p, 0, sizeof(struct timespec));
George C. Wilson20ca73b2006-05-24 16:09:55 -05002189
Al Viroc32c8af2008-12-14 03:46:48 -05002190 context->mq_sendrecv.mqdes = mqdes;
2191 context->mq_sendrecv.msg_len = msg_len;
2192 context->mq_sendrecv.msg_prio = msg_prio;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002193
Al Viroc32c8af2008-12-14 03:46:48 -05002194 context->type = AUDIT_MQ_SENDRECV;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002195}
2196
2197/**
2198 * __audit_mq_notify - record audit data for a POSIX MQ notify
2199 * @mqdes: MQ descriptor
Randy Dunlap6b962552009-01-05 13:41:13 -08002200 * @notification: Notification event
George C. Wilson20ca73b2006-05-24 16:09:55 -05002201 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002202 */
2203
Al Viro20114f72008-12-10 07:16:12 -05002204void __audit_mq_notify(mqd_t mqdes, const struct sigevent *notification)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002205{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002206 struct audit_context *context = current->audit_context;
2207
Al Viro20114f72008-12-10 07:16:12 -05002208 if (notification)
2209 context->mq_notify.sigev_signo = notification->sigev_signo;
2210 else
2211 context->mq_notify.sigev_signo = 0;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002212
Al Viro20114f72008-12-10 07:16:12 -05002213 context->mq_notify.mqdes = mqdes;
2214 context->type = AUDIT_MQ_NOTIFY;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002215}
2216
2217/**
2218 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2219 * @mqdes: MQ descriptor
2220 * @mqstat: MQ flags
2221 *
George C. Wilson20ca73b2006-05-24 16:09:55 -05002222 */
Al Viro73929062008-12-10 06:58:59 -05002223void __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
George C. Wilson20ca73b2006-05-24 16:09:55 -05002224{
George C. Wilson20ca73b2006-05-24 16:09:55 -05002225 struct audit_context *context = current->audit_context;
Al Viro73929062008-12-10 06:58:59 -05002226 context->mq_getsetattr.mqdes = mqdes;
2227 context->mq_getsetattr.mqstat = *mqstat;
2228 context->type = AUDIT_MQ_GETSETATTR;
George C. Wilson20ca73b2006-05-24 16:09:55 -05002229}
2230
2231/**
Steve Grubb073115d2006-04-02 17:07:33 -04002232 * audit_ipc_obj - record audit data for ipc object
2233 * @ipcp: ipc permissions
2234 *
Steve Grubb073115d2006-04-02 17:07:33 -04002235 */
Al Viroa33e6752008-12-10 03:40:06 -05002236void __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002237{
Steve Grubb073115d2006-04-02 17:07:33 -04002238 struct audit_context *context = current->audit_context;
Al Viroa33e6752008-12-10 03:40:06 -05002239 context->ipc.uid = ipcp->uid;
2240 context->ipc.gid = ipcp->gid;
2241 context->ipc.mode = ipcp->mode;
Al Viroe816f372008-12-10 03:47:15 -05002242 context->ipc.has_perm = 0;
Al Viroa33e6752008-12-10 03:40:06 -05002243 security_ipc_getsecid(ipcp, &context->ipc.osid);
2244 context->type = AUDIT_IPC;
Steve Grubb073115d2006-04-02 17:07:33 -04002245}
2246
2247/**
2248 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002249 * @qbytes: msgq bytes
2250 * @uid: msgq user id
2251 * @gid: msgq group id
2252 * @mode: msgq mode (permissions)
2253 *
Al Viroe816f372008-12-10 03:47:15 -05002254 * Called only after audit_ipc_obj().
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002255 */
Al Viroe816f372008-12-10 03:47:15 -05002256void __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 struct audit_context *context = current->audit_context;
2259
Al Viroe816f372008-12-10 03:47:15 -05002260 context->ipc.qbytes = qbytes;
2261 context->ipc.perm_uid = uid;
2262 context->ipc.perm_gid = gid;
2263 context->ipc.perm_mode = mode;
2264 context->ipc.has_perm = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002266
Al Viro473ae302006-04-26 14:04:08 -04002267int audit_bprm(struct linux_binprm *bprm)
2268{
2269 struct audit_aux_data_execve *ax;
2270 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002271
Al Viro5ac3a9c2006-07-16 06:38:45 -04002272 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04002273 return 0;
2274
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002275 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002276 if (!ax)
2277 return -ENOMEM;
2278
2279 ax->argc = bprm->argc;
2280 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002281 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002282 ax->d.type = AUDIT_EXECVE;
2283 ax->d.next = context->aux;
2284 context->aux = (void *)ax;
2285 return 0;
2286}
2287
2288
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002289/**
2290 * audit_socketcall - record audit data for sys_socketcall
2291 * @nargs: number of args
2292 * @args: args array
2293 *
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002294 */
Al Virof3298dc2008-12-10 03:16:51 -05002295void audit_socketcall(int nargs, unsigned long *args)
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002296{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002297 struct audit_context *context = current->audit_context;
2298
Al Viro5ac3a9c2006-07-16 06:38:45 -04002299 if (likely(!context || context->dummy))
Al Virof3298dc2008-12-10 03:16:51 -05002300 return;
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002301
Al Virof3298dc2008-12-10 03:16:51 -05002302 context->type = AUDIT_SOCKETCALL;
2303 context->socketcall.nargs = nargs;
2304 memcpy(context->socketcall.args, args, nargs * sizeof(unsigned long));
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002305}
2306
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002307/**
Al Virodb349502007-02-07 01:48:00 -05002308 * __audit_fd_pair - record audit data for pipe and socketpair
2309 * @fd1: the first file descriptor
2310 * @fd2: the second file descriptor
2311 *
Al Virodb349502007-02-07 01:48:00 -05002312 */
Al Viro157cf642008-12-14 04:57:47 -05002313void __audit_fd_pair(int fd1, int fd2)
Al Virodb349502007-02-07 01:48:00 -05002314{
2315 struct audit_context *context = current->audit_context;
Al Viro157cf642008-12-14 04:57:47 -05002316 context->fds[0] = fd1;
2317 context->fds[1] = fd2;
Al Virodb349502007-02-07 01:48:00 -05002318}
2319
2320/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002321 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2322 * @len: data length in user space
2323 * @a: data address in kernel space
2324 *
2325 * Returns 0 for success or NULL context or < 0 on error.
2326 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002327int audit_sockaddr(int len, void *a)
2328{
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002329 struct audit_context *context = current->audit_context;
2330
Al Viro5ac3a9c2006-07-16 06:38:45 -04002331 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002332 return 0;
2333
Al Viro4f6b4342008-12-09 19:50:34 -05002334 if (!context->sockaddr) {
2335 void *p = kmalloc(sizeof(struct sockaddr_storage), GFP_KERNEL);
2336 if (!p)
2337 return -ENOMEM;
2338 context->sockaddr = p;
2339 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002340
Al Viro4f6b4342008-12-09 19:50:34 -05002341 context->sockaddr_len = len;
2342 memcpy(context->sockaddr, a, len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002343 return 0;
2344}
2345
Al Viroa5cb0132007-03-20 13:58:35 -04002346void __audit_ptrace(struct task_struct *t)
2347{
2348 struct audit_context *context = current->audit_context;
2349
2350 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002351 context->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002352 context->target_uid = task_uid(t);
Eric Paris4746ec52008-01-08 10:06:53 -05002353 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002354 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002355 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002356}
2357
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002358/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002359 * audit_signal_info - record signal info for shutting down audit subsystem
2360 * @sig: signal value
2361 * @t: task being signaled
2362 *
2363 * If the audit subsystem is being terminated, record the task (pid)
2364 * and uid that is doing that.
2365 */
Amy Griffise54dc242007-03-29 18:01:04 -04002366int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002367{
Amy Griffise54dc242007-03-29 18:01:04 -04002368 struct audit_aux_data_pids *axp;
2369 struct task_struct *tsk = current;
2370 struct audit_context *ctx = tsk->audit_context;
David Howellsc69e8d92008-11-14 10:39:19 +11002371 uid_t uid = current_uid(), t_uid = task_uid(t);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002372
Al Viro175fc482007-08-08 00:01:46 +01002373 if (audit_pid && t->tgid == audit_pid) {
Eric Parisee1d3152008-07-07 10:49:45 -04002374 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
Al Viro175fc482007-08-08 00:01:46 +01002375 audit_sig_pid = tsk->pid;
Al Virobfef93a2008-01-10 04:53:18 -05002376 if (tsk->loginuid != -1)
2377 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002378 else
David Howellsc69e8d92008-11-14 10:39:19 +11002379 audit_sig_uid = uid;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002380 security_task_getsecid(tsk, &audit_sig_sid);
Al Viro175fc482007-08-08 00:01:46 +01002381 }
2382 if (!audit_signals || audit_dummy_context())
2383 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002384 }
Amy Griffise54dc242007-03-29 18:01:04 -04002385
Amy Griffise54dc242007-03-29 18:01:04 -04002386 /* optimize the common case by putting first signal recipient directly
2387 * in audit_context */
2388 if (!ctx->target_pid) {
2389 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002390 ctx->target_auid = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002391 ctx->target_uid = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002392 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002393 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002394 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002395 return 0;
2396 }
2397
2398 axp = (void *)ctx->aux_pids;
2399 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2400 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2401 if (!axp)
2402 return -ENOMEM;
2403
2404 axp->d.type = AUDIT_OBJ_PID;
2405 axp->d.next = ctx->aux_pids;
2406 ctx->aux_pids = (void *)axp;
2407 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002408 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002409
2410 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002411 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
David Howellsc69e8d92008-11-14 10:39:19 +11002412 axp->target_uid[axp->pid_count] = t_uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002413 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002414 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002415 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002416 axp->pid_count++;
2417
2418 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002419}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002420
2421/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002422 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
David Howellsd84f4f92008-11-14 10:39:23 +11002423 * @bprm: pointer to the bprm being processed
2424 * @new: the proposed new credentials
2425 * @old: the old credentials
Eric Paris3fc689e2008-11-11 21:48:18 +11002426 *
2427 * Simply check if the proc already has the caps given by the file and if not
2428 * store the priv escalation info for later auditing at the end of the syscall
2429 *
Eric Paris3fc689e2008-11-11 21:48:18 +11002430 * -Eric
2431 */
David Howellsd84f4f92008-11-14 10:39:23 +11002432int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
2433 const struct cred *new, const struct cred *old)
Eric Paris3fc689e2008-11-11 21:48:18 +11002434{
2435 struct audit_aux_data_bprm_fcaps *ax;
2436 struct audit_context *context = current->audit_context;
2437 struct cpu_vfs_cap_data vcaps;
2438 struct dentry *dentry;
2439
2440 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2441 if (!ax)
David Howellsd84f4f92008-11-14 10:39:23 +11002442 return -ENOMEM;
Eric Paris3fc689e2008-11-11 21:48:18 +11002443
2444 ax->d.type = AUDIT_BPRM_FCAPS;
2445 ax->d.next = context->aux;
2446 context->aux = (void *)ax;
2447
2448 dentry = dget(bprm->file->f_dentry);
2449 get_vfs_caps_from_disk(dentry, &vcaps);
2450 dput(dentry);
2451
2452 ax->fcap.permitted = vcaps.permitted;
2453 ax->fcap.inheritable = vcaps.inheritable;
2454 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2455 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2456
David Howellsd84f4f92008-11-14 10:39:23 +11002457 ax->old_pcap.permitted = old->cap_permitted;
2458 ax->old_pcap.inheritable = old->cap_inheritable;
2459 ax->old_pcap.effective = old->cap_effective;
Eric Paris3fc689e2008-11-11 21:48:18 +11002460
David Howellsd84f4f92008-11-14 10:39:23 +11002461 ax->new_pcap.permitted = new->cap_permitted;
2462 ax->new_pcap.inheritable = new->cap_inheritable;
2463 ax->new_pcap.effective = new->cap_effective;
2464 return 0;
Eric Paris3fc689e2008-11-11 21:48:18 +11002465}
2466
2467/**
Eric Parise68b75a02008-11-11 21:48:22 +11002468 * __audit_log_capset - store information about the arguments to the capset syscall
David Howellsd84f4f92008-11-14 10:39:23 +11002469 * @pid: target pid of the capset call
2470 * @new: the new credentials
2471 * @old: the old (current) credentials
Eric Parise68b75a02008-11-11 21:48:22 +11002472 *
2473 * Record the aguments userspace sent to sys_capset for later printing by the
2474 * audit system if applicable
2475 */
Al Viro57f71a02009-01-04 14:52:57 -05002476void __audit_log_capset(pid_t pid,
David Howellsd84f4f92008-11-14 10:39:23 +11002477 const struct cred *new, const struct cred *old)
Eric Parise68b75a02008-11-11 21:48:22 +11002478{
Eric Parise68b75a02008-11-11 21:48:22 +11002479 struct audit_context *context = current->audit_context;
Al Viro57f71a02009-01-04 14:52:57 -05002480 context->capset.pid = pid;
2481 context->capset.cap.effective = new->cap_effective;
2482 context->capset.cap.inheritable = new->cap_effective;
2483 context->capset.cap.permitted = new->cap_permitted;
2484 context->type = AUDIT_CAPSET;
Eric Parise68b75a02008-11-11 21:48:22 +11002485}
2486
Al Viro120a7952010-10-30 02:54:44 -04002487void __audit_mmap_fd(int fd, int flags)
2488{
2489 struct audit_context *context = current->audit_context;
2490 context->mmap.fd = fd;
2491 context->mmap.flags = flags;
2492 context->type = AUDIT_MMAP;
2493}
2494
Eric Parise68b75a02008-11-11 21:48:22 +11002495/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002496 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002497 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002498 *
2499 * If a process ends with a core dump, something fishy is going on and we
2500 * should record the event for investigation.
2501 */
2502void audit_core_dumps(long signr)
2503{
2504 struct audit_buffer *ab;
2505 u32 sid;
David Howells76aac0e2008-11-14 10:39:12 +11002506 uid_t auid = audit_get_loginuid(current), uid;
2507 gid_t gid;
Eric Paris4746ec52008-01-08 10:06:53 -05002508 unsigned int sessionid = audit_get_sessionid(current);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002509
2510 if (!audit_enabled)
2511 return;
2512
2513 if (signr == SIGQUIT) /* don't care for those */
2514 return;
2515
2516 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
David Howells76aac0e2008-11-14 10:39:12 +11002517 current_uid_gid(&uid, &gid);
Eric Paris4746ec52008-01-08 10:06:53 -05002518 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
David Howells76aac0e2008-11-14 10:39:12 +11002519 auid, uid, gid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002520 security_task_getsecid(current, &sid);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002521 if (sid) {
2522 char *ctx = NULL;
2523 u32 len;
2524
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002525 if (security_secid_to_secctx(sid, &ctx, &len))
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002526 audit_log_format(ab, " ssid=%u", sid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002527 else {
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002528 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002529 security_release_secctx(ctx, len);
2530 }
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002531 }
2532 audit_log_format(ab, " pid=%d comm=", current->pid);
2533 audit_log_untrustedstring(ab, current->comm);
2534 audit_log_format(ab, " sig=%ld", signr);
2535 audit_log_end(ab);
2536}
Al Viro916d7572009-06-24 00:02:38 -04002537
2538struct list_head *audit_killed_trees(void)
2539{
2540 struct audit_context *ctx = current->audit_context;
2541 if (likely(!ctx || !ctx->in_syscall))
2542 return NULL;
2543 return &ctx->killed_trees;
2544}