blob: a222e73fec74d7364cda498ab4489c2aca551601 [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>
Stephen Smalley01116102005-05-21 00:15:52 +010052#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010053#include <linux/socket.h>
George C. Wilson20ca73b2006-05-24 16:09:55 -050054#include <linux/mqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/audit.h>
56#include <linux/personality.h>
57#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010058#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010059#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000061#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000062#include <linux/list.h>
Steve Grubba6c043a2006-01-01 14:07:00 -050063#include <linux/tty.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060064#include <linux/selinux.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>
Al Viro74c3cbe2007-07-22 08:04:18 -040068#include <linux/inotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
David Woodhousefe7752b2005-12-15 18:33:52 +000070#include "audit.h"
71
72extern struct list_head audit_filter_list[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* AUDIT_NAMES is the number of slots we reserve in the audit_context
75 * for saving names from getname(). */
76#define AUDIT_NAMES 20
77
Amy Griffis9c937dc2006-06-08 23:19:31 -040078/* Indicates that audit should log the full pathname. */
79#define AUDIT_NAME_FULL -1
80
Al Viro471a5c72006-07-10 08:29:24 -040081/* number of audit rules */
82int audit_n_rules;
83
Amy Griffise54dc242007-03-29 18:01:04 -040084/* determines whether we collect data for signals sent */
85int audit_signals;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* When fs/namei.c:getname() is called, we store the pointer in name and
88 * we don't let putname() free it (instead we free all of the saved
89 * pointers at syscall exit time).
90 *
91 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
92struct audit_names {
93 const char *name;
Amy Griffis9c937dc2006-06-08 23:19:31 -040094 int name_len; /* number of name's characters to log */
95 unsigned name_put; /* call __putname() for this name */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned long ino;
97 dev_t dev;
98 umode_t mode;
99 uid_t uid;
100 gid_t gid;
101 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400102 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105struct audit_aux_data {
106 struct audit_aux_data *next;
107 int type;
108};
109
110#define AUDIT_AUX_IPCPERM 0
111
Amy Griffise54dc242007-03-29 18:01:04 -0400112/* Number of target pids per aux struct. */
113#define AUDIT_AUX_PIDS 16
114
George C. Wilson20ca73b2006-05-24 16:09:55 -0500115struct audit_aux_data_mq_open {
116 struct audit_aux_data d;
117 int oflag;
118 mode_t mode;
119 struct mq_attr attr;
120};
121
122struct audit_aux_data_mq_sendrecv {
123 struct audit_aux_data d;
124 mqd_t mqdes;
125 size_t msg_len;
126 unsigned int msg_prio;
127 struct timespec abs_timeout;
128};
129
130struct audit_aux_data_mq_notify {
131 struct audit_aux_data d;
132 mqd_t mqdes;
133 struct sigevent notification;
134};
135
136struct audit_aux_data_mq_getsetattr {
137 struct audit_aux_data d;
138 mqd_t mqdes;
139 struct mq_attr mqstat;
140};
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142struct audit_aux_data_ipcctl {
143 struct audit_aux_data d;
144 struct ipc_perm p;
145 unsigned long qbytes;
146 uid_t uid;
147 gid_t gid;
148 mode_t mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500149 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Al Viro473ae302006-04-26 14:04:08 -0400152struct audit_aux_data_execve {
153 struct audit_aux_data d;
154 int argc;
155 int envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700156 struct mm_struct *mm;
Al Viro473ae302006-04-26 14:04:08 -0400157};
158
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100159struct audit_aux_data_socketcall {
160 struct audit_aux_data d;
161 int nargs;
162 unsigned long args[0];
163};
164
165struct audit_aux_data_sockaddr {
166 struct audit_aux_data d;
167 int len;
168 char a[0];
169};
170
Al Virodb349502007-02-07 01:48:00 -0500171struct audit_aux_data_fd_pair {
172 struct audit_aux_data d;
173 int fd[2];
174};
175
Amy Griffise54dc242007-03-29 18:01:04 -0400176struct audit_aux_data_pids {
177 struct audit_aux_data d;
178 pid_t target_pid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500179 uid_t target_auid[AUDIT_AUX_PIDS];
180 uid_t target_uid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400181 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500182 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400183 int pid_count;
184};
185
Al Viro74c3cbe2007-07-22 08:04:18 -0400186struct audit_tree_refs {
187 struct audit_tree_refs *next;
188 struct audit_chunk *c[31];
189};
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* The per-task audit context. */
192struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400193 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int in_syscall; /* 1 if task is in a syscall */
195 enum audit_state state;
196 unsigned int serial; /* serial number for record */
197 struct timespec ctime; /* time of syscall entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int major; /* syscall number */
199 unsigned long argv[4]; /* syscall arguments */
200 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100201 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int auditable; /* 1 if record should be written */
203 int name_count;
204 struct audit_names names[AUDIT_NAMES];
Amy Griffis5adc8a62006-06-14 18:45:21 -0400205 char * filterkey; /* key for rule that triggered record */
David Woodhouse8f37d472005-05-27 12:17:28 +0100206 struct dentry * pwd;
207 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct audit_context *previous; /* For nested syscalls */
209 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400210 struct audit_aux_data *aux_pids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400213 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 uid_t uid, euid, suid, fsuid;
215 gid_t gid, egid, sgid, fsgid;
216 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100217 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Al Viroa5cb0132007-03-20 13:58:35 -0400219 pid_t target_pid;
Eric Parisc2a77802008-01-07 13:40:17 -0500220 uid_t target_auid;
221 uid_t target_uid;
Al Viroa5cb0132007-03-20 13:58:35 -0400222 u32 target_sid;
Eric Parisc2a77802008-01-07 13:40:17 -0500223 char target_comm[TASK_COMM_LEN];
Al Viroa5cb0132007-03-20 13:58:35 -0400224
Al Viro74c3cbe2007-07-22 08:04:18 -0400225 struct audit_tree_refs *trees, *first_trees;
226 int tree_count;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#if AUDIT_DEBUG
229 int put_count;
230 int ino_count;
231#endif
232};
233
Al Viro55669bf2006-08-31 19:26:40 -0400234#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
235static inline int open_arg(int flags, int mask)
236{
237 int n = ACC_MODE(flags);
238 if (flags & (O_TRUNC | O_CREAT))
239 n |= AUDIT_PERM_WRITE;
240 return n & mask;
241}
242
243static int audit_match_perm(struct audit_context *ctx, int mask)
244{
245 unsigned n = ctx->major;
246 switch (audit_classify_syscall(ctx->arch, n)) {
247 case 0: /* native */
248 if ((mask & AUDIT_PERM_WRITE) &&
249 audit_match_class(AUDIT_CLASS_WRITE, n))
250 return 1;
251 if ((mask & AUDIT_PERM_READ) &&
252 audit_match_class(AUDIT_CLASS_READ, n))
253 return 1;
254 if ((mask & AUDIT_PERM_ATTR) &&
255 audit_match_class(AUDIT_CLASS_CHATTR, n))
256 return 1;
257 return 0;
258 case 1: /* 32bit on biarch */
259 if ((mask & AUDIT_PERM_WRITE) &&
260 audit_match_class(AUDIT_CLASS_WRITE_32, n))
261 return 1;
262 if ((mask & AUDIT_PERM_READ) &&
263 audit_match_class(AUDIT_CLASS_READ_32, n))
264 return 1;
265 if ((mask & AUDIT_PERM_ATTR) &&
266 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
267 return 1;
268 return 0;
269 case 2: /* open */
270 return mask & ACC_MODE(ctx->argv[1]);
271 case 3: /* openat */
272 return mask & ACC_MODE(ctx->argv[2]);
273 case 4: /* socketcall */
274 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
275 case 5: /* execve */
276 return mask & AUDIT_PERM_EXEC;
277 default:
278 return 0;
279 }
280}
281
Al Viro74c3cbe2007-07-22 08:04:18 -0400282/*
283 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
284 * ->first_trees points to its beginning, ->trees - to the current end of data.
285 * ->tree_count is the number of free entries in array pointed to by ->trees.
286 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
287 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
288 * it's going to remain 1-element for almost any setup) until we free context itself.
289 * References in it _are_ dropped - at the same time we free/drop aux stuff.
290 */
291
292#ifdef CONFIG_AUDIT_TREE
293static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
294{
295 struct audit_tree_refs *p = ctx->trees;
296 int left = ctx->tree_count;
297 if (likely(left)) {
298 p->c[--left] = chunk;
299 ctx->tree_count = left;
300 return 1;
301 }
302 if (!p)
303 return 0;
304 p = p->next;
305 if (p) {
306 p->c[30] = chunk;
307 ctx->trees = p;
308 ctx->tree_count = 30;
309 return 1;
310 }
311 return 0;
312}
313
314static int grow_tree_refs(struct audit_context *ctx)
315{
316 struct audit_tree_refs *p = ctx->trees;
317 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
318 if (!ctx->trees) {
319 ctx->trees = p;
320 return 0;
321 }
322 if (p)
323 p->next = ctx->trees;
324 else
325 ctx->first_trees = ctx->trees;
326 ctx->tree_count = 31;
327 return 1;
328}
329#endif
330
331static void unroll_tree_refs(struct audit_context *ctx,
332 struct audit_tree_refs *p, int count)
333{
334#ifdef CONFIG_AUDIT_TREE
335 struct audit_tree_refs *q;
336 int n;
337 if (!p) {
338 /* we started with empty chain */
339 p = ctx->first_trees;
340 count = 31;
341 /* if the very first allocation has failed, nothing to do */
342 if (!p)
343 return;
344 }
345 n = count;
346 for (q = p; q != ctx->trees; q = q->next, n = 31) {
347 while (n--) {
348 audit_put_chunk(q->c[n]);
349 q->c[n] = NULL;
350 }
351 }
352 while (n-- > ctx->tree_count) {
353 audit_put_chunk(q->c[n]);
354 q->c[n] = NULL;
355 }
356 ctx->trees = p;
357 ctx->tree_count = count;
358#endif
359}
360
361static void free_tree_refs(struct audit_context *ctx)
362{
363 struct audit_tree_refs *p, *q;
364 for (p = ctx->first_trees; p; p = q) {
365 q = p->next;
366 kfree(p);
367 }
368}
369
370static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
371{
372#ifdef CONFIG_AUDIT_TREE
373 struct audit_tree_refs *p;
374 int n;
375 if (!tree)
376 return 0;
377 /* full ones */
378 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
379 for (n = 0; n < 31; n++)
380 if (audit_tree_match(p->c[n], tree))
381 return 1;
382 }
383 /* partial */
384 if (p) {
385 for (n = ctx->tree_count; n < 31; n++)
386 if (audit_tree_match(p->c[n], tree))
387 return 1;
388 }
389#endif
390 return 0;
391}
392
Amy Griffisf368c07d2006-04-07 16:55:56 -0400393/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/* Compare a task_struct with an audit_rule. Return 1 on match, 0
395 * otherwise. */
396static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500397 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400399 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 enum audit_state *state)
401{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400402 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600403 u32 sid;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500406 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int result = 0;
408
Amy Griffis93315ed2006-02-07 12:05:27 -0500409 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500411 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 break;
Al Viro3c662512006-05-06 08:26:27 -0400413 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400414 if (ctx) {
415 if (!ctx->ppid)
416 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400417 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400418 }
Al Viro3c662512006-05-06 08:26:27 -0400419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500421 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500424 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
426 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500427 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 break;
429 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500430 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500433 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 break;
435 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500436 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500439 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
441 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500442 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500445 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
2fd6f582005-04-29 16:08:28 +0100447 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700448 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500449 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100450 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 case AUDIT_EXIT:
453 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500454 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 break;
456 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100457 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500458 if (f->val)
459 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100460 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500461 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400465 if (name)
466 result = audit_comparator(MAJOR(name->dev),
467 f->op, f->val);
468 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500470 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 ++result;
472 break;
473 }
474 }
475 }
476 break;
477 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400478 if (name)
479 result = audit_comparator(MINOR(name->dev),
480 f->op, f->val);
481 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500483 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 ++result;
485 break;
486 }
487 }
488 }
489 break;
490 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400491 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400492 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400493 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400495 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 ++result;
497 break;
498 }
499 }
500 }
501 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400502 case AUDIT_WATCH:
503 if (name && rule->watch->ino != (unsigned long)-1)
504 result = (name->dev == rule->watch->dev &&
Amy Griffis9c937dc2006-06-08 23:19:31 -0400505 name->ino == rule->watch->ino);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400506 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400507 case AUDIT_DIR:
508 if (ctx)
509 result = match_tree_refs(ctx, rule->tree);
510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 case AUDIT_LOGINUID:
512 result = 0;
513 if (ctx)
Al Virobfef93a2008-01-10 04:53:18 -0500514 result = audit_comparator(tsk->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500516 case AUDIT_SUBJ_USER:
517 case AUDIT_SUBJ_ROLE:
518 case AUDIT_SUBJ_TYPE:
519 case AUDIT_SUBJ_SEN:
520 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600521 /* NOTE: this may return negative values indicating
522 a temporary error. We simply treat this as a
523 match for now to avoid losing information that
524 may be wanted. An error message will also be
525 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400526 if (f->se_rule) {
527 if (need_sid) {
Stephen Smalley62bac012006-09-25 23:31:56 -0700528 selinux_get_task_sid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400529 need_sid = 0;
530 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600531 result = selinux_audit_rule_match(sid, f->type,
532 f->op,
533 f->se_rule,
534 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400535 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600536 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500537 case AUDIT_OBJ_USER:
538 case AUDIT_OBJ_ROLE:
539 case AUDIT_OBJ_TYPE:
540 case AUDIT_OBJ_LEV_LOW:
541 case AUDIT_OBJ_LEV_HIGH:
542 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
543 also applies here */
544 if (f->se_rule) {
545 /* Find files that match */
546 if (name) {
547 result = selinux_audit_rule_match(
548 name->osid, f->type, f->op,
549 f->se_rule, ctx);
550 } else if (ctx) {
551 for (j = 0; j < ctx->name_count; j++) {
552 if (selinux_audit_rule_match(
553 ctx->names[j].osid,
554 f->type, f->op,
555 f->se_rule, ctx)) {
556 ++result;
557 break;
558 }
559 }
560 }
561 /* Find ipc objects that match */
562 if (ctx) {
563 struct audit_aux_data *aux;
564 for (aux = ctx->aux; aux;
565 aux = aux->next) {
566 if (aux->type == AUDIT_IPC) {
567 struct audit_aux_data_ipcctl *axi = (void *)aux;
568 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
569 ++result;
570 break;
571 }
572 }
573 }
574 }
575 }
576 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 case AUDIT_ARG0:
578 case AUDIT_ARG1:
579 case AUDIT_ARG2:
580 case AUDIT_ARG3:
581 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500582 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400584 case AUDIT_FILTERKEY:
585 /* ignore this field for filtering */
586 result = 1;
587 break;
Al Viro55669bf2006-08-31 19:26:40 -0400588 case AUDIT_PERM:
589 result = audit_match_perm(ctx, f->val);
590 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!result)
594 return 0;
595 }
Amy Griffis5adc8a62006-06-14 18:45:21 -0400596 if (rule->filterkey)
597 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 switch (rule->action) {
599 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
601 }
602 return 1;
603}
604
605/* At process creation time, we can determine if system-call auditing is
606 * completely disabled for this task. Since we only have the task
607 * structure at this point, we can only check uid and gid.
608 */
609static enum audit_state audit_filter_task(struct task_struct *tsk)
610{
611 struct audit_entry *e;
612 enum audit_state state;
613
614 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100615 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400616 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 rcu_read_unlock();
618 return state;
619 }
620 }
621 rcu_read_unlock();
622 return AUDIT_BUILD_CONTEXT;
623}
624
625/* At syscall entry and exit time, this filter is called if the
626 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100627 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700628 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
630static enum audit_state audit_filter_syscall(struct task_struct *tsk,
631 struct audit_context *ctx,
632 struct list_head *list)
633{
634 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100635 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
David Woodhouse351bb722005-07-14 14:40:06 +0100637 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100638 return AUDIT_DISABLED;
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100641 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000642 int word = AUDIT_WORD(ctx->major);
643 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100644
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000645 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400646 if ((e->rule.mask[word] & bit) == bit &&
647 audit_filter_rules(tsk, &e->rule, ctx, NULL,
648 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000649 rcu_read_unlock();
650 return state;
651 }
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 rcu_read_unlock();
655 return AUDIT_BUILD_CONTEXT;
656}
657
Amy Griffisf368c07d2006-04-07 16:55:56 -0400658/* At syscall exit time, this filter is called if any audit_names[] have been
659 * collected during syscall processing. We only check rules in sublists at hash
660 * buckets applicable to the inode numbers in audit_names[].
661 * Regarding audit_state, same rules apply as for audit_filter_syscall().
662 */
663enum audit_state audit_filter_inodes(struct task_struct *tsk,
664 struct audit_context *ctx)
665{
666 int i;
667 struct audit_entry *e;
668 enum audit_state state;
669
670 if (audit_pid && tsk->tgid == audit_pid)
671 return AUDIT_DISABLED;
672
673 rcu_read_lock();
674 for (i = 0; i < ctx->name_count; i++) {
675 int word = AUDIT_WORD(ctx->major);
676 int bit = AUDIT_BIT(ctx->major);
677 struct audit_names *n = &ctx->names[i];
678 int h = audit_hash_ino((u32)n->ino);
679 struct list_head *list = &audit_inode_hash[h];
680
681 if (list_empty(list))
682 continue;
683
684 list_for_each_entry_rcu(e, list, list) {
685 if ((e->rule.mask[word] & bit) == bit &&
686 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
687 rcu_read_unlock();
688 return state;
689 }
690 }
691 }
692 rcu_read_unlock();
693 return AUDIT_BUILD_CONTEXT;
694}
695
696void audit_set_auditable(struct audit_context *ctx)
697{
698 ctx->auditable = 1;
699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static inline struct audit_context *audit_get_context(struct task_struct *tsk,
702 int return_valid,
703 int return_code)
704{
705 struct audit_context *context = tsk->audit_context;
706
707 if (likely(!context))
708 return NULL;
709 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500710
711 /*
712 * we need to fix up the return code in the audit logs if the actual
713 * return codes are later going to be fixed up by the arch specific
714 * signal handlers
715 *
716 * This is actually a test for:
717 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
718 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
719 *
720 * but is faster than a bunch of ||
721 */
722 if (unlikely(return_code <= -ERESTARTSYS) &&
723 (return_code >= -ERESTART_RESTARTBLOCK) &&
724 (return_code != -ENOIOCTLCMD))
725 context->return_code = -EINTR;
726 else
727 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Al Virod51374a2006-08-03 10:59:26 -0400729 if (context->in_syscall && !context->dummy && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400731
David Woodhouse0f45aa12005-06-19 19:35:50 +0100732 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400733 if (state == AUDIT_RECORD_CONTEXT) {
734 context->auditable = 1;
735 goto get_context;
736 }
737
738 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (state == AUDIT_RECORD_CONTEXT)
740 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
Amy Griffisf368c07d2006-04-07 16:55:56 -0400744get_context:
Al Viro3f2792f2006-07-16 06:43:48 -0400745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 tsk->audit_context = NULL;
747 return context;
748}
749
750static inline void audit_free_names(struct audit_context *context)
751{
752 int i;
753
754#if AUDIT_DEBUG == 2
755 if (context->auditable
756 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000757 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 " name_count=%d put_count=%d"
759 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000760 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 context->serial, context->major, context->in_syscall,
762 context->name_count, context->put_count,
763 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000764 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 printk(KERN_ERR "names[%d] = %p = %s\n", i,
766 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000767 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 dump_stack();
770 return;
771 }
772#endif
773#if AUDIT_DEBUG
774 context->put_count = 0;
775 context->ino_count = 0;
776#endif
777
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000778 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400779 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100783 if (context->pwd)
784 dput(context->pwd);
785 if (context->pwdmnt)
786 mntput(context->pwdmnt);
787 context->pwd = NULL;
788 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791static inline void audit_free_aux(struct audit_context *context)
792{
793 struct audit_aux_data *aux;
794
795 while ((aux = context->aux)) {
796 context->aux = aux->next;
797 kfree(aux);
798 }
Amy Griffise54dc242007-03-29 18:01:04 -0400799 while ((aux = context->aux_pids)) {
800 context->aux_pids = aux->next;
801 kfree(aux);
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805static inline void audit_zero_context(struct audit_context *context,
806 enum audit_state state)
807{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 memset(context, 0, sizeof(*context));
809 context->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812static inline struct audit_context *audit_alloc_context(enum audit_state state)
813{
814 struct audit_context *context;
815
816 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
817 return NULL;
818 audit_zero_context(context, state);
819 return context;
820}
821
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700822/**
823 * audit_alloc - allocate an audit context block for a task
824 * @tsk: task
825 *
826 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * if necessary. Doing so turns on system call auditing for the
828 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700829 * needed.
830 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831int audit_alloc(struct task_struct *tsk)
832{
833 struct audit_context *context;
834 enum audit_state state;
835
836 if (likely(!audit_enabled))
837 return 0; /* Return if not auditing. */
838
839 state = audit_filter_task(tsk);
840 if (likely(state == AUDIT_DISABLED))
841 return 0;
842
843 if (!(context = audit_alloc_context(state))) {
844 audit_log_lost("out of memory in audit_alloc");
845 return -ENOMEM;
846 }
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 tsk->audit_context = context;
849 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
850 return 0;
851}
852
853static inline void audit_free_context(struct audit_context *context)
854{
855 struct audit_context *previous;
856 int count = 0;
857
858 do {
859 previous = context->previous;
860 if (previous || (count && count < 10)) {
861 ++count;
862 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
863 " freeing multiple contexts (%d)\n",
864 context->serial, context->major,
865 context->name_count, count);
866 }
867 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -0400868 unroll_tree_refs(context, NULL, 0);
869 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400871 kfree(context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 kfree(context);
873 context = previous;
874 } while (context);
875 if (count >= 10)
876 printk(KERN_ERR "audit: freed %d contexts\n", count);
877}
878
Joy Latten161a09e2006-11-27 13:11:54 -0600879void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000880{
881 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000882 unsigned len;
883 int error;
884 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000885
Al Viroc4823bc2007-03-12 16:17:42 +0000886 selinux_get_task_sid(current, &sid);
887 if (!sid)
888 return;
889
890 error = selinux_sid_to_string(sid, &ctx, &len);
891 if (error) {
892 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000893 goto error_path;
894 return;
895 }
896
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000897 audit_log_format(ab, " subj=%s", ctx);
Al Viroc4823bc2007-03-12 16:17:42 +0000898 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000899 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000900
901error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000902 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000903 return;
904}
905
Joy Latten161a09e2006-11-27 13:11:54 -0600906EXPORT_SYMBOL(audit_log_task_context);
907
Al Viroe4951492006-03-29 20:17:10 -0500908static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700909{
Al Viro45d9bb02006-03-29 20:02:55 -0500910 char name[sizeof(tsk->comm)];
911 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700912 struct vm_area_struct *vma;
913
Al Viroe4951492006-03-29 20:17:10 -0500914 /* tsk == current */
915
Al Viro45d9bb02006-03-29 20:02:55 -0500916 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100917 audit_log_format(ab, " comm=");
918 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700919
Al Viroe4951492006-03-29 20:17:10 -0500920 if (mm) {
921 down_read(&mm->mmap_sem);
922 vma = mm->mmap;
923 while (vma) {
924 if ((vma->vm_flags & VM_EXECUTABLE) &&
925 vma->vm_file) {
926 audit_log_d_path(ab, "exe=",
Josef Sipeka7a005f2006-12-08 02:37:17 -0800927 vma->vm_file->f_path.dentry,
928 vma->vm_file->f_path.mnt);
Al Viroe4951492006-03-29 20:17:10 -0500929 break;
930 }
931 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700932 }
Al Viroe4951492006-03-29 20:17:10 -0500933 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700934 }
Al Viroe4951492006-03-29 20:17:10 -0500935 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700936}
937
Amy Griffise54dc242007-03-29 18:01:04 -0400938static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric Parisc2a77802008-01-07 13:40:17 -0500939 uid_t auid, uid_t uid, u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400940{
941 struct audit_buffer *ab;
942 char *s = NULL;
943 u32 len;
944 int rc = 0;
945
946 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
947 if (!ab)
948 return 1;
949
Eric Parisc2a77802008-01-07 13:40:17 -0500950 audit_log_format(ab, "opid=%d oauid=%d ouid=%d", pid, auid, uid);
Amy Griffise54dc242007-03-29 18:01:04 -0400951 if (selinux_sid_to_string(sid, &s, &len)) {
Eric Parisc2a77802008-01-07 13:40:17 -0500952 audit_log_format(ab, " obj=(none)");
Amy Griffise54dc242007-03-29 18:01:04 -0400953 rc = 1;
954 } else
Eric Parisc2a77802008-01-07 13:40:17 -0500955 audit_log_format(ab, " obj=%s", s);
956 audit_log_format(ab, " ocomm=");
957 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -0400958 audit_log_end(ab);
959 kfree(s);
960
961 return rc;
962}
963
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700964static void audit_log_execve_info(struct audit_buffer *ab,
965 struct audit_aux_data_execve *axi)
966{
967 int i;
968 long len, ret;
Peter Zijlstra040b3a22007-07-28 00:55:18 +0200969 const char __user *p;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700970 char *buf;
971
972 if (axi->mm != current->mm)
973 return; /* execve failed, no additional info */
974
Peter Zijlstra040b3a22007-07-28 00:55:18 +0200975 p = (const char __user *)axi->mm->arg_start;
976
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700977 for (i = 0; i < axi->argc; i++, p += len) {
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700978 len = strnlen_user(p, MAX_ARG_STRLEN);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700979 /*
980 * We just created this mm, if we can't find the strings
981 * we just copied into it something is _very_ wrong. Similar
982 * for strings that are too long, we should not have created
983 * any.
984 */
985 if (!len || len > MAX_ARG_STRLEN) {
986 WARN_ON(1);
987 send_sig(SIGKILL, current, 0);
988 }
989
990 buf = kmalloc(len, GFP_KERNEL);
991 if (!buf) {
992 audit_panic("out of memory for argv string\n");
993 break;
994 }
995
996 ret = copy_from_user(buf, p, len);
997 /*
998 * There is no reason for this copy to be short. We just
999 * copied them here, and the mm hasn't been exposed to user-
1000 * space yet.
1001 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001002 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001003 WARN_ON(1);
1004 send_sig(SIGKILL, current, 0);
1005 }
1006
1007 audit_log_format(ab, "a%d=", i);
1008 audit_log_untrustedstring(ab, buf);
1009 audit_log_format(ab, "\n");
1010
1011 kfree(buf);
1012 }
1013}
1014
Al Viroe4951492006-03-29 20:17:10 -05001015static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001017 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001019 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -05001020 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Al Viroe4951492006-03-29 20:17:10 -05001022 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001023 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -04001024 if (!context->ppid)
1025 context->ppid = sys_getppid();
Al Viro3f2792f2006-07-16 06:43:48 -04001026 context->uid = tsk->uid;
1027 context->gid = tsk->gid;
1028 context->euid = tsk->euid;
1029 context->suid = tsk->suid;
1030 context->fsuid = tsk->fsuid;
1031 context->egid = tsk->egid;
1032 context->sgid = tsk->sgid;
1033 context->fsgid = tsk->fsgid;
1034 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001035
1036 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (!ab)
1038 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001039 audit_log_format(ab, "arch=%x syscall=%d",
1040 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (context->personality != PER_LINUX)
1042 audit_log_format(ab, " per=%lx", context->personality);
1043 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001044 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001045 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1046 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001047
1048 mutex_lock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001049 read_lock(&tasklist_lock);
Al Viro45d9bb02006-03-29 20:02:55 -05001050 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1051 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -05001052 else
1053 tty = "(none)";
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001054 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 audit_log_format(ab,
1056 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -04001057 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +01001058 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -05001059 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 context->argv[0],
1061 context->argv[1],
1062 context->argv[2],
1063 context->argv[3],
1064 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -04001065 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 context->pid,
Al Virobfef93a2008-01-10 04:53:18 -05001067 tsk->loginuid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 context->uid,
1069 context->gid,
1070 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -05001071 context->egid, context->sgid, context->fsgid, tty);
Alan Coxeb84a202006-09-29 02:01:41 -07001072
1073 mutex_unlock(&tty_mutex);
1074
Al Viroe4951492006-03-29 20:17:10 -05001075 audit_log_task_info(ab, tsk);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001076 if (context->filterkey) {
1077 audit_log_format(ab, " key=");
1078 audit_log_untrustedstring(ab, context->filterkey);
1079 } else
1080 audit_log_format(ab, " key=(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
David Woodhouse7551ced2005-05-26 12:04:57 +01001083 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001084
Al Viroe4951492006-03-29 20:17:10 -05001085 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (!ab)
1087 continue; /* audit_panic has been called */
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001090 case AUDIT_MQ_OPEN: {
1091 struct audit_aux_data_mq_open *axi = (void *)aux;
1092 audit_log_format(ab,
1093 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1094 "mq_msgsize=%ld mq_curmsgs=%ld",
1095 axi->oflag, axi->mode, axi->attr.mq_flags,
1096 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1097 axi->attr.mq_curmsgs);
1098 break; }
1099
1100 case AUDIT_MQ_SENDRECV: {
1101 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1102 audit_log_format(ab,
1103 "mqdes=%d msg_len=%zd msg_prio=%u "
1104 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1105 axi->mqdes, axi->msg_len, axi->msg_prio,
1106 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1107 break; }
1108
1109 case AUDIT_MQ_NOTIFY: {
1110 struct audit_aux_data_mq_notify *axi = (void *)aux;
1111 audit_log_format(ab,
1112 "mqdes=%d sigev_signo=%d",
1113 axi->mqdes,
1114 axi->notification.sigev_signo);
1115 break; }
1116
1117 case AUDIT_MQ_GETSETATTR: {
1118 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1119 audit_log_format(ab,
1120 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1121 "mq_curmsgs=%ld ",
1122 axi->mqdes,
1123 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1124 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1125 break; }
1126
Steve Grubbc0404992005-05-13 18:17:42 +01001127 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct audit_aux_data_ipcctl *axi = (void *)aux;
1129 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001130 "ouid=%u ogid=%u mode=%#o",
Linda Knippersac032212006-05-16 22:03:48 -04001131 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001132 if (axi->osid != 0) {
1133 char *ctx = NULL;
1134 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -07001135 if (selinux_sid_to_string(
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001136 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -05001137 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001138 axi->osid);
1139 call_panic = 1;
1140 } else
1141 audit_log_format(ab, " obj=%s", ctx);
1142 kfree(ctx);
1143 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001144 break; }
1145
Steve Grubb073115d2006-04-02 17:07:33 -04001146 case AUDIT_IPC_SET_PERM: {
1147 struct audit_aux_data_ipcctl *axi = (void *)aux;
1148 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001149 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
Steve Grubb073115d2006-04-02 17:07:33 -04001150 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -04001151 break; }
Linda Knippersac032212006-05-16 22:03:48 -04001152
Al Viro473ae302006-04-26 14:04:08 -04001153 case AUDIT_EXECVE: {
1154 struct audit_aux_data_execve *axi = (void *)aux;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001155 audit_log_execve_info(ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001156 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001157
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001158 case AUDIT_SOCKETCALL: {
1159 int i;
1160 struct audit_aux_data_socketcall *axs = (void *)aux;
1161 audit_log_format(ab, "nargs=%d", axs->nargs);
1162 for (i=0; i<axs->nargs; i++)
1163 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1164 break; }
1165
1166 case AUDIT_SOCKADDR: {
1167 struct audit_aux_data_sockaddr *axs = (void *)aux;
1168
1169 audit_log_format(ab, "saddr=");
1170 audit_log_hex(ab, axs->a, axs->len);
1171 break; }
Stephen Smalley01116102005-05-21 00:15:52 +01001172
Al Virodb349502007-02-07 01:48:00 -05001173 case AUDIT_FD_PAIR: {
1174 struct audit_aux_data_fd_pair *axs = (void *)aux;
1175 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1176 break; }
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
Amy Griffise54dc242007-03-29 18:01:04 -04001182 for (aux = context->aux_pids; aux; aux = aux->next) {
1183 struct audit_aux_data_pids *axs = (void *)aux;
1184 int i;
1185
1186 for (i = 0; i < axs->pid_count; i++)
1187 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001188 axs->target_auid[i],
1189 axs->target_uid[i],
1190 axs->target_sid[i],
1191 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001192 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001193 }
1194
Amy Griffise54dc242007-03-29 18:01:04 -04001195 if (context->target_pid &&
1196 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001197 context->target_auid, context->target_uid,
1198 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001199 call_panic = 1;
1200
David Woodhouse8f37d472005-05-27 12:17:28 +01001201 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -05001202 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001203 if (ab) {
1204 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1205 audit_log_end(ab);
1206 }
1207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -04001209 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +00001210
Al Viroe4951492006-03-29 20:17:10 -05001211 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!ab)
1213 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +01001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +00001216
Amy Griffis9c937dc2006-06-08 23:19:31 -04001217 if (n->name) {
1218 switch(n->name_len) {
1219 case AUDIT_NAME_FULL:
1220 /* log the full path */
1221 audit_log_format(ab, " name=");
1222 audit_log_untrustedstring(ab, n->name);
1223 break;
1224 case 0:
1225 /* name was specified as a relative path and the
1226 * directory component is the cwd */
1227 audit_log_d_path(ab, " name=", context->pwd,
1228 context->pwdmnt);
1229 break;
1230 default:
1231 /* log the name's directory component */
1232 audit_log_format(ab, " name=");
1233 audit_log_n_untrustedstring(ab, n->name_len,
1234 n->name);
1235 }
1236 } else
1237 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001238
Amy Griffis9c937dc2006-06-08 23:19:31 -04001239 if (n->ino != (unsigned long)-1) {
1240 audit_log_format(ab, " inode=%lu"
1241 " dev=%02x:%02x mode=%#o"
1242 " ouid=%u ogid=%u rdev=%02x:%02x",
1243 n->ino,
1244 MAJOR(n->dev),
1245 MINOR(n->dev),
1246 n->mode,
1247 n->uid,
1248 n->gid,
1249 MAJOR(n->rdev),
1250 MINOR(n->rdev));
1251 }
1252 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001253 char *ctx = NULL;
1254 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -07001255 if (selinux_sid_to_string(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001256 n->osid, &ctx, &len)) {
1257 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001258 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -04001259 } else
1260 audit_log_format(ab, " obj=%s", ctx);
1261 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001262 }
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 audit_log_end(ab);
1265 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001266 if (call_panic)
1267 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001270/**
1271 * audit_free - free a per-task audit context
1272 * @tsk: task whose audit context block to free
1273 *
Al Virofa84cb92006-03-29 20:30:19 -05001274 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276void audit_free(struct task_struct *tsk)
1277{
1278 struct audit_context *context;
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (likely(!context))
1282 return;
1283
1284 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001285 * function (e.g., exit_group), then free context block.
1286 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001287 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001288 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +01001289 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001290 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 audit_free_context(context);
1293}
1294
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001295/**
1296 * audit_syscall_entry - fill in an audit record at syscall entry
1297 * @tsk: task being audited
1298 * @arch: architecture type
1299 * @major: major syscall type (function)
1300 * @a1: additional syscall register 1
1301 * @a2: additional syscall register 2
1302 * @a3: additional syscall register 3
1303 * @a4: additional syscall register 4
1304 *
1305 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 * audit context was created when the task was created and the state or
1307 * filters demand the audit context be built. If the state from the
1308 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1309 * then the record will be written at syscall exit time (otherwise, it
1310 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001311 * be written).
1312 */
Al Viro5411be52006-03-29 20:23:36 -05001313void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 unsigned long a1, unsigned long a2,
1315 unsigned long a3, unsigned long a4)
1316{
Al Viro5411be52006-03-29 20:23:36 -05001317 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 struct audit_context *context = tsk->audit_context;
1319 enum audit_state state;
1320
1321 BUG_ON(!context);
1322
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001323 /*
1324 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 * calls in kernel_thread via the entry.S interface, instead of
1326 * with direct calls. (If you are porting to a new
1327 * architecture, hitting this condition can indicate that you
1328 * got the _exit/_leave calls backward in entry.S.)
1329 *
1330 * i386 no
1331 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001332 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
1334 * This also happens with vm86 emulation in a non-nested manner
1335 * (entries without exits), so this case must be caught.
1336 */
1337 if (context->in_syscall) {
1338 struct audit_context *newctx;
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340#if AUDIT_DEBUG
1341 printk(KERN_ERR
1342 "audit(:%d) pid=%d in syscall=%d;"
1343 " entering syscall=%d\n",
1344 context->serial, tsk->pid, context->major, major);
1345#endif
1346 newctx = audit_alloc_context(context->state);
1347 if (newctx) {
1348 newctx->previous = context;
1349 context = newctx;
1350 tsk->audit_context = newctx;
1351 } else {
1352 /* If we can't alloc a new context, the best we
1353 * can do is to leak memory (any pending putname
1354 * will be lost). The only other alternative is
1355 * to abandon auditing. */
1356 audit_zero_context(context, context->state);
1357 }
1358 }
1359 BUG_ON(context->in_syscall || context->name_count);
1360
1361 if (!audit_enabled)
1362 return;
1363
2fd6f582005-04-29 16:08:28 +01001364 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 context->major = major;
1366 context->argv[0] = a1;
1367 context->argv[1] = a2;
1368 context->argv[2] = a3;
1369 context->argv[3] = a4;
1370
1371 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001372 context->dummy = !audit_n_rules;
1373 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
David Woodhouse0f45aa12005-06-19 19:35:50 +01001374 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (likely(state == AUDIT_DISABLED))
1376 return;
1377
David Woodhousece625a82005-07-18 14:24:46 -04001378 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 context->ctime = CURRENT_TIME;
1380 context->in_syscall = 1;
1381 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
Alexander Viro419c58f2006-09-29 00:08:50 -04001382 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001385/**
1386 * audit_syscall_exit - deallocate audit context after a system call
1387 * @tsk: task being audited
1388 * @valid: success/failure flag
1389 * @return_code: syscall return value
1390 *
1391 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1393 * filtering, or because some other part of the kernel write an audit
1394 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001395 * free the names stored from getname().
1396 */
Al Viro5411be52006-03-29 20:23:36 -05001397void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Al Viro5411be52006-03-29 20:23:36 -05001399 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct audit_context *context;
1401
2fd6f582005-04-29 16:08:28 +01001402 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001405 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
David Woodhousef7056d62005-06-20 16:07:33 +01001407 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001408 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 context->in_syscall = 0;
1411 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (context->previous) {
1414 struct audit_context *new_context = context->previous;
1415 context->previous = NULL;
1416 audit_free_context(context);
1417 tsk->audit_context = new_context;
1418 } else {
1419 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001420 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001422 context->aux = NULL;
1423 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001424 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001425 context->target_sid = 0;
Amy Griffis5adc8a62006-06-14 18:45:21 -04001426 kfree(context->filterkey);
1427 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 tsk->audit_context = context;
1429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Al Viro74c3cbe2007-07-22 08:04:18 -04001432static inline void handle_one(const struct inode *inode)
1433{
1434#ifdef CONFIG_AUDIT_TREE
1435 struct audit_context *context;
1436 struct audit_tree_refs *p;
1437 struct audit_chunk *chunk;
1438 int count;
1439 if (likely(list_empty(&inode->inotify_watches)))
1440 return;
1441 context = current->audit_context;
1442 p = context->trees;
1443 count = context->tree_count;
1444 rcu_read_lock();
1445 chunk = audit_tree_lookup(inode);
1446 rcu_read_unlock();
1447 if (!chunk)
1448 return;
1449 if (likely(put_tree_ref(context, chunk)))
1450 return;
1451 if (unlikely(!grow_tree_refs(context))) {
1452 printk(KERN_WARNING "out of memory, audit has lost a tree reference");
1453 audit_set_auditable(context);
1454 audit_put_chunk(chunk);
1455 unroll_tree_refs(context, p, count);
1456 return;
1457 }
1458 put_tree_ref(context, chunk);
1459#endif
1460}
1461
1462static void handle_path(const struct dentry *dentry)
1463{
1464#ifdef CONFIG_AUDIT_TREE
1465 struct audit_context *context;
1466 struct audit_tree_refs *p;
1467 const struct dentry *d, *parent;
1468 struct audit_chunk *drop;
1469 unsigned long seq;
1470 int count;
1471
1472 context = current->audit_context;
1473 p = context->trees;
1474 count = context->tree_count;
1475retry:
1476 drop = NULL;
1477 d = dentry;
1478 rcu_read_lock();
1479 seq = read_seqbegin(&rename_lock);
1480 for(;;) {
1481 struct inode *inode = d->d_inode;
1482 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1483 struct audit_chunk *chunk;
1484 chunk = audit_tree_lookup(inode);
1485 if (chunk) {
1486 if (unlikely(!put_tree_ref(context, chunk))) {
1487 drop = chunk;
1488 break;
1489 }
1490 }
1491 }
1492 parent = d->d_parent;
1493 if (parent == d)
1494 break;
1495 d = parent;
1496 }
1497 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1498 rcu_read_unlock();
1499 if (!drop) {
1500 /* just a race with rename */
1501 unroll_tree_refs(context, p, count);
1502 goto retry;
1503 }
1504 audit_put_chunk(drop);
1505 if (grow_tree_refs(context)) {
1506 /* OK, got more space */
1507 unroll_tree_refs(context, p, count);
1508 goto retry;
1509 }
1510 /* too bad */
1511 printk(KERN_WARNING
1512 "out of memory, audit has lost a tree reference");
1513 unroll_tree_refs(context, p, count);
1514 audit_set_auditable(context);
1515 return;
1516 }
1517 rcu_read_unlock();
1518#endif
1519}
1520
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001521/**
1522 * audit_getname - add a name to the list
1523 * @name: name to add
1524 *
1525 * Add a name to the list of audit names for this context.
1526 * Called from fs/namei.c:getname().
1527 */
Al Virod8945bb52006-05-18 16:01:30 -04001528void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 struct audit_context *context = current->audit_context;
1531
Al Virod8945bb52006-05-18 16:01:30 -04001532 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return;
1534
1535 if (!context->in_syscall) {
1536#if AUDIT_DEBUG == 2
1537 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1538 __FILE__, __LINE__, context->serial, name);
1539 dump_stack();
1540#endif
1541 return;
1542 }
1543 BUG_ON(context->name_count >= AUDIT_NAMES);
1544 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001545 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1546 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 context->names[context->name_count].ino = (unsigned long)-1;
Amy Griffise41e8bd2007-02-13 14:14:09 -05001548 context->names[context->name_count].osid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001550 if (!context->pwd) {
1551 read_lock(&current->fs->lock);
1552 context->pwd = dget(current->fs->pwd);
1553 context->pwdmnt = mntget(current->fs->pwdmnt);
1554 read_unlock(&current->fs->lock);
1555 }
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001559/* audit_putname - intercept a putname request
1560 * @name: name to intercept and delay for putname
1561 *
1562 * If we have stored the name from getname in the audit context,
1563 * then we delay the putname until syscall exit.
1564 * Called from include/linux/fs.h:putname().
1565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566void audit_putname(const char *name)
1567{
1568 struct audit_context *context = current->audit_context;
1569
1570 BUG_ON(!context);
1571 if (!context->in_syscall) {
1572#if AUDIT_DEBUG == 2
1573 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1574 __FILE__, __LINE__, context->serial, name);
1575 if (context->name_count) {
1576 int i;
1577 for (i = 0; i < context->name_count; i++)
1578 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1579 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001580 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582#endif
1583 __putname(name);
1584 }
1585#if AUDIT_DEBUG
1586 else {
1587 ++context->put_count;
1588 if (context->put_count > context->name_count) {
1589 printk(KERN_ERR "%s:%d(:%d): major=%d"
1590 " in_syscall=%d putname(%p) name_count=%d"
1591 " put_count=%d\n",
1592 __FILE__, __LINE__,
1593 context->serial, context->major,
1594 context->in_syscall, name, context->name_count,
1595 context->put_count);
1596 dump_stack();
1597 }
1598 }
1599#endif
1600}
1601
Amy Griffis5712e882007-02-13 14:15:22 -05001602static int audit_inc_name_count(struct audit_context *context,
1603 const struct inode *inode)
1604{
1605 if (context->name_count >= AUDIT_NAMES) {
1606 if (inode)
1607 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1608 "dev=%02x:%02x, inode=%lu",
1609 MAJOR(inode->i_sb->s_dev),
1610 MINOR(inode->i_sb->s_dev),
1611 inode->i_ino);
1612
1613 else
1614 printk(KERN_DEBUG "name_count maxed, losing inode data");
1615 return 1;
1616 }
1617 context->name_count++;
1618#if AUDIT_DEBUG
1619 context->ino_count++;
1620#endif
1621 return 0;
1622}
1623
Amy Griffis3e2efce2006-07-13 13:16:02 -04001624/* Copy inode data into an audit_names. */
1625static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001626{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001627 name->ino = inode->i_ino;
1628 name->dev = inode->i_sb->s_dev;
1629 name->mode = inode->i_mode;
1630 name->uid = inode->i_uid;
1631 name->gid = inode->i_gid;
1632 name->rdev = inode->i_rdev;
1633 selinux_get_inode_sid(inode, &name->osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001634}
1635
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001636/**
1637 * audit_inode - store the inode and device from a lookup
1638 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001639 * @dentry: dentry being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001640 *
1641 * Called from fs/namei.c:path_lookup().
1642 */
Al Viro5a190ae2007-06-07 12:19:32 -04001643void __audit_inode(const char *name, const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 int idx;
1646 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04001647 const struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 if (!context->in_syscall)
1650 return;
1651 if (context->name_count
1652 && context->names[context->name_count-1].name
1653 && context->names[context->name_count-1].name == name)
1654 idx = context->name_count - 1;
1655 else if (context->name_count > 1
1656 && context->names[context->name_count-2].name
1657 && context->names[context->name_count-2].name == name)
1658 idx = context->name_count - 2;
1659 else {
1660 /* FIXME: how much do we care about inodes that have no
1661 * associated name? */
Amy Griffis5712e882007-02-13 14:15:22 -05001662 if (audit_inc_name_count(context, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001664 idx = context->name_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 context->names[idx].name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001667 handle_path(dentry);
Amy Griffis3e2efce2006-07-13 13:16:02 -04001668 audit_copy_inode(&context->names[idx], inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Amy Griffis73241cc2005-11-03 16:00:25 +00001671/**
1672 * audit_inode_child - collect inode info for created/removed objects
1673 * @dname: inode's dentry name
Randy Dunlap481968f2007-10-21 20:59:53 -07001674 * @dentry: dentry being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04001675 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00001676 *
1677 * For syscalls that create or remove filesystem objects, audit_inode
1678 * can only collect information for the filesystem object's parent.
1679 * This call updates the audit context with the child's information.
1680 * Syscalls that create a new filesystem object must be hooked after
1681 * the object is created. Syscalls that remove a filesystem object
1682 * must be hooked prior, in order to capture the target inode during
1683 * unsuccessful attempts.
1684 */
Al Viro5a190ae2007-06-07 12:19:32 -04001685void __audit_inode_child(const char *dname, const struct dentry *dentry,
Amy Griffis73d3ec52006-07-13 13:16:39 -04001686 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00001687{
1688 int idx;
1689 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05001690 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04001691 const struct inode *inode = dentry->d_inode;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001692 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00001693
1694 if (!context->in_syscall)
1695 return;
1696
Al Viro74c3cbe2007-07-22 08:04:18 -04001697 if (inode)
1698 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001699 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001700 if (!dname)
Amy Griffis5712e882007-02-13 14:15:22 -05001701 goto add_names;
Amy Griffis73241cc2005-11-03 16:00:25 +00001702
Amy Griffis5712e882007-02-13 14:15:22 -05001703 /* parent is more likely, look for it first */
1704 for (idx = 0; idx < context->name_count; idx++) {
1705 struct audit_names *n = &context->names[idx];
Amy Griffis73241cc2005-11-03 16:00:25 +00001706
Amy Griffis5712e882007-02-13 14:15:22 -05001707 if (!n->name)
1708 continue;
1709
1710 if (n->ino == parent->i_ino &&
1711 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1712 n->name_len = dirlen; /* update parent data in place */
1713 found_parent = n->name;
1714 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001715 }
Steve Grubbac9910c2006-09-28 14:31:32 -04001716 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001717
Amy Griffis5712e882007-02-13 14:15:22 -05001718 /* no matching parent, look for matching child */
1719 for (idx = 0; idx < context->name_count; idx++) {
1720 struct audit_names *n = &context->names[idx];
Amy Griffis73d3ec52006-07-13 13:16:39 -04001721
Amy Griffis5712e882007-02-13 14:15:22 -05001722 if (!n->name)
1723 continue;
1724
1725 /* strcmp() is the more likely scenario */
1726 if (!strcmp(dname, n->name) ||
1727 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1728 if (inode)
1729 audit_copy_inode(n, inode);
1730 else
1731 n->ino = (unsigned long)-1;
1732 found_child = n->name;
1733 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04001734 }
Amy Griffis5712e882007-02-13 14:15:22 -05001735 }
1736
1737add_names:
1738 if (!found_parent) {
1739 if (audit_inc_name_count(context, parent))
1740 return;
1741 idx = context->name_count - 1;
1742 context->names[idx].name = NULL;
Amy Griffis73d3ec52006-07-13 13:16:39 -04001743 audit_copy_inode(&context->names[idx], parent);
1744 }
Amy Griffis5712e882007-02-13 14:15:22 -05001745
1746 if (!found_child) {
1747 if (audit_inc_name_count(context, inode))
1748 return;
1749 idx = context->name_count - 1;
1750
1751 /* Re-use the name belonging to the slot for a matching parent
1752 * directory. All names for this context are relinquished in
1753 * audit_free_names() */
1754 if (found_parent) {
1755 context->names[idx].name = found_parent;
1756 context->names[idx].name_len = AUDIT_NAME_FULL;
1757 /* don't call __putname() */
1758 context->names[idx].name_put = 0;
1759 } else {
1760 context->names[idx].name = NULL;
1761 }
1762
1763 if (inode)
1764 audit_copy_inode(&context->names[idx], inode);
1765 else
1766 context->names[idx].ino = (unsigned long)-1;
1767 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001768}
Trond Myklebust50e437d2007-06-07 22:44:34 -04001769EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04001770
1771/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001772 * auditsc_get_stamp - get local copies of audit_context values
1773 * @ctx: audit_context for the task
1774 * @t: timespec to store time recorded in the audit_context
1775 * @serial: serial value that is recorded in the audit_context
1776 *
1777 * Also sets the context as auditable.
1778 */
David Woodhousebfb44962005-05-21 21:08:09 +01001779void auditsc_get_stamp(struct audit_context *ctx,
1780 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
David Woodhousece625a82005-07-18 14:24:46 -04001782 if (!ctx->serial)
1783 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001784 t->tv_sec = ctx->ctime.tv_sec;
1785 t->tv_nsec = ctx->ctime.tv_nsec;
1786 *serial = ctx->serial;
1787 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001790/**
1791 * audit_set_loginuid - set a task's audit_context loginuid
1792 * @task: task whose audit context is being modified
1793 * @loginuid: loginuid value
1794 *
1795 * Returns 0.
1796 *
1797 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1798 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001799int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Steve Grubb41757102006-06-12 07:48:28 -04001801 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01001802
Al Virobfef93a2008-01-10 04:53:18 -05001803 if (context && context->in_syscall) {
1804 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04001805
Al Virobfef93a2008-01-10 04:53:18 -05001806 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1807 if (ab) {
1808 audit_log_format(ab, "login pid=%d uid=%u "
1809 "old auid=%u new auid=%u",
1810 task->pid, task->uid,
1811 task->loginuid, loginuid);
1812 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01001813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
Al Virobfef93a2008-01-10 04:53:18 -05001815 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 0;
1817}
1818
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001819/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05001820 * __audit_mq_open - record audit data for a POSIX MQ open
1821 * @oflag: open flag
1822 * @mode: mode bits
1823 * @u_attr: queue attributes
1824 *
1825 * Returns 0 for success or NULL context or < 0 on error.
1826 */
1827int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1828{
1829 struct audit_aux_data_mq_open *ax;
1830 struct audit_context *context = current->audit_context;
1831
1832 if (!audit_enabled)
1833 return 0;
1834
1835 if (likely(!context))
1836 return 0;
1837
1838 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1839 if (!ax)
1840 return -ENOMEM;
1841
1842 if (u_attr != NULL) {
1843 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1844 kfree(ax);
1845 return -EFAULT;
1846 }
1847 } else
1848 memset(&ax->attr, 0, sizeof(ax->attr));
1849
1850 ax->oflag = oflag;
1851 ax->mode = mode;
1852
1853 ax->d.type = AUDIT_MQ_OPEN;
1854 ax->d.next = context->aux;
1855 context->aux = (void *)ax;
1856 return 0;
1857}
1858
1859/**
1860 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1861 * @mqdes: MQ descriptor
1862 * @msg_len: Message length
1863 * @msg_prio: Message priority
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001864 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001865 *
1866 * Returns 0 for success or NULL context or < 0 on error.
1867 */
1868int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1869 const struct timespec __user *u_abs_timeout)
1870{
1871 struct audit_aux_data_mq_sendrecv *ax;
1872 struct audit_context *context = current->audit_context;
1873
1874 if (!audit_enabled)
1875 return 0;
1876
1877 if (likely(!context))
1878 return 0;
1879
1880 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1881 if (!ax)
1882 return -ENOMEM;
1883
1884 if (u_abs_timeout != NULL) {
1885 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1886 kfree(ax);
1887 return -EFAULT;
1888 }
1889 } else
1890 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1891
1892 ax->mqdes = mqdes;
1893 ax->msg_len = msg_len;
1894 ax->msg_prio = msg_prio;
1895
1896 ax->d.type = AUDIT_MQ_SENDRECV;
1897 ax->d.next = context->aux;
1898 context->aux = (void *)ax;
1899 return 0;
1900}
1901
1902/**
1903 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1904 * @mqdes: MQ descriptor
1905 * @msg_len: Message length
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001906 * @u_msg_prio: Message priority
1907 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001908 *
1909 * Returns 0 for success or NULL context or < 0 on error.
1910 */
1911int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1912 unsigned int __user *u_msg_prio,
1913 const struct timespec __user *u_abs_timeout)
1914{
1915 struct audit_aux_data_mq_sendrecv *ax;
1916 struct audit_context *context = current->audit_context;
1917
1918 if (!audit_enabled)
1919 return 0;
1920
1921 if (likely(!context))
1922 return 0;
1923
1924 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1925 if (!ax)
1926 return -ENOMEM;
1927
1928 if (u_msg_prio != NULL) {
1929 if (get_user(ax->msg_prio, u_msg_prio)) {
1930 kfree(ax);
1931 return -EFAULT;
1932 }
1933 } else
1934 ax->msg_prio = 0;
1935
1936 if (u_abs_timeout != NULL) {
1937 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1938 kfree(ax);
1939 return -EFAULT;
1940 }
1941 } else
1942 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1943
1944 ax->mqdes = mqdes;
1945 ax->msg_len = msg_len;
1946
1947 ax->d.type = AUDIT_MQ_SENDRECV;
1948 ax->d.next = context->aux;
1949 context->aux = (void *)ax;
1950 return 0;
1951}
1952
1953/**
1954 * __audit_mq_notify - record audit data for a POSIX MQ notify
1955 * @mqdes: MQ descriptor
1956 * @u_notification: Notification event
1957 *
1958 * Returns 0 for success or NULL context or < 0 on error.
1959 */
1960
1961int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1962{
1963 struct audit_aux_data_mq_notify *ax;
1964 struct audit_context *context = current->audit_context;
1965
1966 if (!audit_enabled)
1967 return 0;
1968
1969 if (likely(!context))
1970 return 0;
1971
1972 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1973 if (!ax)
1974 return -ENOMEM;
1975
1976 if (u_notification != NULL) {
1977 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1978 kfree(ax);
1979 return -EFAULT;
1980 }
1981 } else
1982 memset(&ax->notification, 0, sizeof(ax->notification));
1983
1984 ax->mqdes = mqdes;
1985
1986 ax->d.type = AUDIT_MQ_NOTIFY;
1987 ax->d.next = context->aux;
1988 context->aux = (void *)ax;
1989 return 0;
1990}
1991
1992/**
1993 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1994 * @mqdes: MQ descriptor
1995 * @mqstat: MQ flags
1996 *
1997 * Returns 0 for success or NULL context or < 0 on error.
1998 */
1999int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2000{
2001 struct audit_aux_data_mq_getsetattr *ax;
2002 struct audit_context *context = current->audit_context;
2003
2004 if (!audit_enabled)
2005 return 0;
2006
2007 if (likely(!context))
2008 return 0;
2009
2010 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2011 if (!ax)
2012 return -ENOMEM;
2013
2014 ax->mqdes = mqdes;
2015 ax->mqstat = *mqstat;
2016
2017 ax->d.type = AUDIT_MQ_GETSETATTR;
2018 ax->d.next = context->aux;
2019 context->aux = (void *)ax;
2020 return 0;
2021}
2022
2023/**
Steve Grubb073115d2006-04-02 17:07:33 -04002024 * audit_ipc_obj - record audit data for ipc object
2025 * @ipcp: ipc permissions
2026 *
2027 * Returns 0 for success or NULL context or < 0 on error.
2028 */
Al Virod8945bb52006-05-18 16:01:30 -04002029int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002030{
2031 struct audit_aux_data_ipcctl *ax;
2032 struct audit_context *context = current->audit_context;
2033
Steve Grubb073115d2006-04-02 17:07:33 -04002034 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2035 if (!ax)
2036 return -ENOMEM;
2037
2038 ax->uid = ipcp->uid;
2039 ax->gid = ipcp->gid;
2040 ax->mode = ipcp->mode;
2041 selinux_get_ipc_sid(ipcp, &ax->osid);
2042
2043 ax->d.type = AUDIT_IPC;
2044 ax->d.next = context->aux;
2045 context->aux = (void *)ax;
2046 return 0;
2047}
2048
2049/**
2050 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002051 * @qbytes: msgq bytes
2052 * @uid: msgq user id
2053 * @gid: msgq group id
2054 * @mode: msgq mode (permissions)
2055 *
2056 * Returns 0 for success or NULL context or < 0 on error.
2057 */
Al Virod8945bb52006-05-18 16:01:30 -04002058int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060 struct audit_aux_data_ipcctl *ax;
2061 struct audit_context *context = current->audit_context;
2062
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002063 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (!ax)
2065 return -ENOMEM;
2066
2067 ax->qbytes = qbytes;
2068 ax->uid = uid;
2069 ax->gid = gid;
2070 ax->mode = mode;
2071
Steve Grubb073115d2006-04-02 17:07:33 -04002072 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 ax->d.next = context->aux;
2074 context->aux = (void *)ax;
2075 return 0;
2076}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002077
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002078int audit_argv_kb = 32;
2079
Al Viro473ae302006-04-26 14:04:08 -04002080int audit_bprm(struct linux_binprm *bprm)
2081{
2082 struct audit_aux_data_execve *ax;
2083 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002084
Al Viro5ac3a9c2006-07-16 06:38:45 -04002085 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04002086 return 0;
2087
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002088 /*
2089 * Even though the stack code doesn't limit the arg+env size any more,
2090 * the audit code requires that _all_ arguments be logged in a single
2091 * netlink skb. Hence cap it :-(
2092 */
2093 if (bprm->argv_len > (audit_argv_kb << 10))
2094 return -E2BIG;
2095
2096 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002097 if (!ax)
2098 return -ENOMEM;
2099
2100 ax->argc = bprm->argc;
2101 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002102 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002103 ax->d.type = AUDIT_EXECVE;
2104 ax->d.next = context->aux;
2105 context->aux = (void *)ax;
2106 return 0;
2107}
2108
2109
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002110/**
2111 * audit_socketcall - record audit data for sys_socketcall
2112 * @nargs: number of args
2113 * @args: args array
2114 *
2115 * Returns 0 for success or NULL context or < 0 on error.
2116 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002117int audit_socketcall(int nargs, unsigned long *args)
2118{
2119 struct audit_aux_data_socketcall *ax;
2120 struct audit_context *context = current->audit_context;
2121
Al Viro5ac3a9c2006-07-16 06:38:45 -04002122 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002123 return 0;
2124
2125 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2126 if (!ax)
2127 return -ENOMEM;
2128
2129 ax->nargs = nargs;
2130 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2131
2132 ax->d.type = AUDIT_SOCKETCALL;
2133 ax->d.next = context->aux;
2134 context->aux = (void *)ax;
2135 return 0;
2136}
2137
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002138/**
Al Virodb349502007-02-07 01:48:00 -05002139 * __audit_fd_pair - record audit data for pipe and socketpair
2140 * @fd1: the first file descriptor
2141 * @fd2: the second file descriptor
2142 *
2143 * Returns 0 for success or NULL context or < 0 on error.
2144 */
2145int __audit_fd_pair(int fd1, int fd2)
2146{
2147 struct audit_context *context = current->audit_context;
2148 struct audit_aux_data_fd_pair *ax;
2149
2150 if (likely(!context)) {
2151 return 0;
2152 }
2153
2154 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2155 if (!ax) {
2156 return -ENOMEM;
2157 }
2158
2159 ax->fd[0] = fd1;
2160 ax->fd[1] = fd2;
2161
2162 ax->d.type = AUDIT_FD_PAIR;
2163 ax->d.next = context->aux;
2164 context->aux = (void *)ax;
2165 return 0;
2166}
2167
2168/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002169 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2170 * @len: data length in user space
2171 * @a: data address in kernel space
2172 *
2173 * Returns 0 for success or NULL context or < 0 on error.
2174 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002175int audit_sockaddr(int len, void *a)
2176{
2177 struct audit_aux_data_sockaddr *ax;
2178 struct audit_context *context = current->audit_context;
2179
Al Viro5ac3a9c2006-07-16 06:38:45 -04002180 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002181 return 0;
2182
2183 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2184 if (!ax)
2185 return -ENOMEM;
2186
2187 ax->len = len;
2188 memcpy(ax->a, a, len);
2189
2190 ax->d.type = AUDIT_SOCKADDR;
2191 ax->d.next = context->aux;
2192 context->aux = (void *)ax;
2193 return 0;
2194}
2195
Al Viroa5cb0132007-03-20 13:58:35 -04002196void __audit_ptrace(struct task_struct *t)
2197{
2198 struct audit_context *context = current->audit_context;
2199
2200 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002201 context->target_auid = audit_get_loginuid(t);
2202 context->target_uid = t->uid;
Al Viroa5cb0132007-03-20 13:58:35 -04002203 selinux_get_task_sid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002204 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002205}
2206
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002207/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002208 * audit_signal_info - record signal info for shutting down audit subsystem
2209 * @sig: signal value
2210 * @t: task being signaled
2211 *
2212 * If the audit subsystem is being terminated, record the task (pid)
2213 * and uid that is doing that.
2214 */
Amy Griffise54dc242007-03-29 18:01:04 -04002215int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002216{
Amy Griffise54dc242007-03-29 18:01:04 -04002217 struct audit_aux_data_pids *axp;
2218 struct task_struct *tsk = current;
2219 struct audit_context *ctx = tsk->audit_context;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002220 extern pid_t audit_sig_pid;
2221 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04002222 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002223
Al Viro175fc482007-08-08 00:01:46 +01002224 if (audit_pid && t->tgid == audit_pid) {
2225 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
2226 audit_sig_pid = tsk->pid;
Al Virobfef93a2008-01-10 04:53:18 -05002227 if (tsk->loginuid != -1)
2228 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002229 else
2230 audit_sig_uid = tsk->uid;
2231 selinux_get_task_sid(tsk, &audit_sig_sid);
2232 }
2233 if (!audit_signals || audit_dummy_context())
2234 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002235 }
Amy Griffise54dc242007-03-29 18:01:04 -04002236
Amy Griffise54dc242007-03-29 18:01:04 -04002237 /* optimize the common case by putting first signal recipient directly
2238 * in audit_context */
2239 if (!ctx->target_pid) {
2240 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002241 ctx->target_auid = audit_get_loginuid(t);
2242 ctx->target_uid = t->uid;
Amy Griffise54dc242007-03-29 18:01:04 -04002243 selinux_get_task_sid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002244 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002245 return 0;
2246 }
2247
2248 axp = (void *)ctx->aux_pids;
2249 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2250 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2251 if (!axp)
2252 return -ENOMEM;
2253
2254 axp->d.type = AUDIT_OBJ_PID;
2255 axp->d.next = ctx->aux_pids;
2256 ctx->aux_pids = (void *)axp;
2257 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002258 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002259
2260 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002261 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2262 axp->target_uid[axp->pid_count] = t->uid;
Amy Griffise54dc242007-03-29 18:01:04 -04002263 selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002264 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002265 axp->pid_count++;
2266
2267 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002268}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002269
2270/**
2271 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002272 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002273 *
2274 * If a process ends with a core dump, something fishy is going on and we
2275 * should record the event for investigation.
2276 */
2277void audit_core_dumps(long signr)
2278{
2279 struct audit_buffer *ab;
2280 u32 sid;
2281
2282 if (!audit_enabled)
2283 return;
2284
2285 if (signr == SIGQUIT) /* don't care for those */
2286 return;
2287
2288 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2289 audit_log_format(ab, "auid=%u uid=%u gid=%u",
Al Viro0c11b942008-01-10 04:20:52 -05002290 audit_get_loginuid(current),
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002291 current->uid, current->gid);
2292 selinux_get_task_sid(current, &sid);
2293 if (sid) {
2294 char *ctx = NULL;
2295 u32 len;
2296
2297 if (selinux_sid_to_string(sid, &ctx, &len))
2298 audit_log_format(ab, " ssid=%u", sid);
2299 else
2300 audit_log_format(ab, " subj=%s", ctx);
2301 kfree(ctx);
2302 }
2303 audit_log_format(ab, " pid=%d comm=", current->pid);
2304 audit_log_untrustedstring(ab, current->comm);
2305 audit_log_format(ab, " sig=%ld", signr);
2306 audit_log_end(ab);
2307}