blob: c95173a194bf39277ea8c7bac1c60c300fdfcd20 [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 Viroa1f8e7f2006-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];
179 u32 target_sid[AUDIT_AUX_PIDS];
180 int pid_count;
181};
182
Al Viro74c3cbe2007-07-22 08:04:18 -0400183struct audit_tree_refs {
184 struct audit_tree_refs *next;
185 struct audit_chunk *c[31];
186};
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* The per-task audit context. */
189struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400190 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int in_syscall; /* 1 if task is in a syscall */
192 enum audit_state state;
193 unsigned int serial; /* serial number for record */
194 struct timespec ctime; /* time of syscall entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int major; /* syscall number */
196 unsigned long argv[4]; /* syscall arguments */
197 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100198 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int auditable; /* 1 if record should be written */
200 int name_count;
201 struct audit_names names[AUDIT_NAMES];
Amy Griffis5adc8a62006-06-14 18:45:21 -0400202 char * filterkey; /* key for rule that triggered record */
David Woodhouse8f37d472005-05-27 12:17:28 +0100203 struct dentry * pwd;
204 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct audit_context *previous; /* For nested syscalls */
206 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400207 struct audit_aux_data *aux_pids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400210 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 uid_t uid, euid, suid, fsuid;
212 gid_t gid, egid, sgid, fsgid;
213 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100214 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Al Viroa5cb0132007-03-20 13:58:35 -0400216 pid_t target_pid;
217 u32 target_sid;
218
Al Viro74c3cbe2007-07-22 08:04:18 -0400219 struct audit_tree_refs *trees, *first_trees;
220 int tree_count;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#if AUDIT_DEBUG
223 int put_count;
224 int ino_count;
225#endif
226};
227
Al Viro55669bf2006-08-31 19:26:40 -0400228#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
229static inline int open_arg(int flags, int mask)
230{
231 int n = ACC_MODE(flags);
232 if (flags & (O_TRUNC | O_CREAT))
233 n |= AUDIT_PERM_WRITE;
234 return n & mask;
235}
236
237static int audit_match_perm(struct audit_context *ctx, int mask)
238{
239 unsigned n = ctx->major;
240 switch (audit_classify_syscall(ctx->arch, n)) {
241 case 0: /* native */
242 if ((mask & AUDIT_PERM_WRITE) &&
243 audit_match_class(AUDIT_CLASS_WRITE, n))
244 return 1;
245 if ((mask & AUDIT_PERM_READ) &&
246 audit_match_class(AUDIT_CLASS_READ, n))
247 return 1;
248 if ((mask & AUDIT_PERM_ATTR) &&
249 audit_match_class(AUDIT_CLASS_CHATTR, n))
250 return 1;
251 return 0;
252 case 1: /* 32bit on biarch */
253 if ((mask & AUDIT_PERM_WRITE) &&
254 audit_match_class(AUDIT_CLASS_WRITE_32, n))
255 return 1;
256 if ((mask & AUDIT_PERM_READ) &&
257 audit_match_class(AUDIT_CLASS_READ_32, n))
258 return 1;
259 if ((mask & AUDIT_PERM_ATTR) &&
260 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
261 return 1;
262 return 0;
263 case 2: /* open */
264 return mask & ACC_MODE(ctx->argv[1]);
265 case 3: /* openat */
266 return mask & ACC_MODE(ctx->argv[2]);
267 case 4: /* socketcall */
268 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
269 case 5: /* execve */
270 return mask & AUDIT_PERM_EXEC;
271 default:
272 return 0;
273 }
274}
275
Al Viro74c3cbe2007-07-22 08:04:18 -0400276/*
277 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
278 * ->first_trees points to its beginning, ->trees - to the current end of data.
279 * ->tree_count is the number of free entries in array pointed to by ->trees.
280 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
281 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
282 * it's going to remain 1-element for almost any setup) until we free context itself.
283 * References in it _are_ dropped - at the same time we free/drop aux stuff.
284 */
285
286#ifdef CONFIG_AUDIT_TREE
287static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
288{
289 struct audit_tree_refs *p = ctx->trees;
290 int left = ctx->tree_count;
291 if (likely(left)) {
292 p->c[--left] = chunk;
293 ctx->tree_count = left;
294 return 1;
295 }
296 if (!p)
297 return 0;
298 p = p->next;
299 if (p) {
300 p->c[30] = chunk;
301 ctx->trees = p;
302 ctx->tree_count = 30;
303 return 1;
304 }
305 return 0;
306}
307
308static int grow_tree_refs(struct audit_context *ctx)
309{
310 struct audit_tree_refs *p = ctx->trees;
311 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
312 if (!ctx->trees) {
313 ctx->trees = p;
314 return 0;
315 }
316 if (p)
317 p->next = ctx->trees;
318 else
319 ctx->first_trees = ctx->trees;
320 ctx->tree_count = 31;
321 return 1;
322}
323#endif
324
325static void unroll_tree_refs(struct audit_context *ctx,
326 struct audit_tree_refs *p, int count)
327{
328#ifdef CONFIG_AUDIT_TREE
329 struct audit_tree_refs *q;
330 int n;
331 if (!p) {
332 /* we started with empty chain */
333 p = ctx->first_trees;
334 count = 31;
335 /* if the very first allocation has failed, nothing to do */
336 if (!p)
337 return;
338 }
339 n = count;
340 for (q = p; q != ctx->trees; q = q->next, n = 31) {
341 while (n--) {
342 audit_put_chunk(q->c[n]);
343 q->c[n] = NULL;
344 }
345 }
346 while (n-- > ctx->tree_count) {
347 audit_put_chunk(q->c[n]);
348 q->c[n] = NULL;
349 }
350 ctx->trees = p;
351 ctx->tree_count = count;
352#endif
353}
354
355static void free_tree_refs(struct audit_context *ctx)
356{
357 struct audit_tree_refs *p, *q;
358 for (p = ctx->first_trees; p; p = q) {
359 q = p->next;
360 kfree(p);
361 }
362}
363
364static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
365{
366#ifdef CONFIG_AUDIT_TREE
367 struct audit_tree_refs *p;
368 int n;
369 if (!tree)
370 return 0;
371 /* full ones */
372 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
373 for (n = 0; n < 31; n++)
374 if (audit_tree_match(p->c[n], tree))
375 return 1;
376 }
377 /* partial */
378 if (p) {
379 for (n = ctx->tree_count; n < 31; n++)
380 if (audit_tree_match(p->c[n], tree))
381 return 1;
382 }
383#endif
384 return 0;
385}
386
Amy Griffisf368c07d2006-04-07 16:55:56 -0400387/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/* Compare a task_struct with an audit_rule. Return 1 on match, 0
389 * otherwise. */
390static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500391 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400393 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 enum audit_state *state)
395{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400396 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600397 u32 sid;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500400 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int result = 0;
402
Amy Griffis93315ed2006-02-07 12:05:27 -0500403 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500405 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 break;
Al Viro3c662512006-05-06 08:26:27 -0400407 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400408 if (ctx) {
409 if (!ctx->ppid)
410 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400411 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400412 }
Al Viro3c662512006-05-06 08:26:27 -0400413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500415 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 break;
417 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500418 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
420 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500421 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500424 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 break;
426 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500427 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 break;
429 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500430 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500433 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 break;
435 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500436 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 break;
438 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500439 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 break;
2fd6f582005-04-29 16:08:28 +0100441 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700442 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500443 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 case AUDIT_EXIT:
447 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500448 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 break;
450 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100451 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500452 if (f->val)
453 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100454 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500455 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400459 if (name)
460 result = audit_comparator(MAJOR(name->dev),
461 f->op, f->val);
462 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500464 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 ++result;
466 break;
467 }
468 }
469 }
470 break;
471 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400472 if (name)
473 result = audit_comparator(MINOR(name->dev),
474 f->op, f->val);
475 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500477 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ++result;
479 break;
480 }
481 }
482 }
483 break;
484 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400485 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400486 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400487 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400489 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ++result;
491 break;
492 }
493 }
494 }
495 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400496 case AUDIT_WATCH:
497 if (name && rule->watch->ino != (unsigned long)-1)
498 result = (name->dev == rule->watch->dev &&
Amy Griffis9c937dc2006-06-08 23:19:31 -0400499 name->ino == rule->watch->ino);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400500 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400501 case AUDIT_DIR:
502 if (ctx)
503 result = match_tree_refs(ctx, rule->tree);
504 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 case AUDIT_LOGINUID:
506 result = 0;
507 if (ctx)
Al Virobfef93a2008-01-10 04:53:18 -0500508 result = audit_comparator(tsk->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500510 case AUDIT_SUBJ_USER:
511 case AUDIT_SUBJ_ROLE:
512 case AUDIT_SUBJ_TYPE:
513 case AUDIT_SUBJ_SEN:
514 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600515 /* NOTE: this may return negative values indicating
516 a temporary error. We simply treat this as a
517 match for now to avoid losing information that
518 may be wanted. An error message will also be
519 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400520 if (f->se_rule) {
521 if (need_sid) {
Stephen Smalley62bac012006-09-25 23:31:56 -0700522 selinux_get_task_sid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400523 need_sid = 0;
524 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600525 result = selinux_audit_rule_match(sid, f->type,
526 f->op,
527 f->se_rule,
528 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400529 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600530 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500531 case AUDIT_OBJ_USER:
532 case AUDIT_OBJ_ROLE:
533 case AUDIT_OBJ_TYPE:
534 case AUDIT_OBJ_LEV_LOW:
535 case AUDIT_OBJ_LEV_HIGH:
536 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
537 also applies here */
538 if (f->se_rule) {
539 /* Find files that match */
540 if (name) {
541 result = selinux_audit_rule_match(
542 name->osid, f->type, f->op,
543 f->se_rule, ctx);
544 } else if (ctx) {
545 for (j = 0; j < ctx->name_count; j++) {
546 if (selinux_audit_rule_match(
547 ctx->names[j].osid,
548 f->type, f->op,
549 f->se_rule, ctx)) {
550 ++result;
551 break;
552 }
553 }
554 }
555 /* Find ipc objects that match */
556 if (ctx) {
557 struct audit_aux_data *aux;
558 for (aux = ctx->aux; aux;
559 aux = aux->next) {
560 if (aux->type == AUDIT_IPC) {
561 struct audit_aux_data_ipcctl *axi = (void *)aux;
562 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
563 ++result;
564 break;
565 }
566 }
567 }
568 }
569 }
570 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 case AUDIT_ARG0:
572 case AUDIT_ARG1:
573 case AUDIT_ARG2:
574 case AUDIT_ARG3:
575 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500576 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400578 case AUDIT_FILTERKEY:
579 /* ignore this field for filtering */
580 result = 1;
581 break;
Al Viro55669bf2006-08-31 19:26:40 -0400582 case AUDIT_PERM:
583 result = audit_match_perm(ctx, f->val);
584 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!result)
588 return 0;
589 }
Amy Griffis5adc8a62006-06-14 18:45:21 -0400590 if (rule->filterkey)
591 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 switch (rule->action) {
593 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
595 }
596 return 1;
597}
598
599/* At process creation time, we can determine if system-call auditing is
600 * completely disabled for this task. Since we only have the task
601 * structure at this point, we can only check uid and gid.
602 */
603static enum audit_state audit_filter_task(struct task_struct *tsk)
604{
605 struct audit_entry *e;
606 enum audit_state state;
607
608 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100609 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400610 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 rcu_read_unlock();
612 return state;
613 }
614 }
615 rcu_read_unlock();
616 return AUDIT_BUILD_CONTEXT;
617}
618
619/* At syscall entry and exit time, this filter is called if the
620 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100621 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700622 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
624static enum audit_state audit_filter_syscall(struct task_struct *tsk,
625 struct audit_context *ctx,
626 struct list_head *list)
627{
628 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100629 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
David Woodhouse351bb722005-07-14 14:40:06 +0100631 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100632 return AUDIT_DISABLED;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100635 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000636 int word = AUDIT_WORD(ctx->major);
637 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100638
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000639 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400640 if ((e->rule.mask[word] & bit) == bit &&
641 audit_filter_rules(tsk, &e->rule, ctx, NULL,
642 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000643 rcu_read_unlock();
644 return state;
645 }
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 rcu_read_unlock();
649 return AUDIT_BUILD_CONTEXT;
650}
651
Amy Griffisf368c07d2006-04-07 16:55:56 -0400652/* At syscall exit time, this filter is called if any audit_names[] have been
653 * collected during syscall processing. We only check rules in sublists at hash
654 * buckets applicable to the inode numbers in audit_names[].
655 * Regarding audit_state, same rules apply as for audit_filter_syscall().
656 */
657enum audit_state audit_filter_inodes(struct task_struct *tsk,
658 struct audit_context *ctx)
659{
660 int i;
661 struct audit_entry *e;
662 enum audit_state state;
663
664 if (audit_pid && tsk->tgid == audit_pid)
665 return AUDIT_DISABLED;
666
667 rcu_read_lock();
668 for (i = 0; i < ctx->name_count; i++) {
669 int word = AUDIT_WORD(ctx->major);
670 int bit = AUDIT_BIT(ctx->major);
671 struct audit_names *n = &ctx->names[i];
672 int h = audit_hash_ino((u32)n->ino);
673 struct list_head *list = &audit_inode_hash[h];
674
675 if (list_empty(list))
676 continue;
677
678 list_for_each_entry_rcu(e, list, list) {
679 if ((e->rule.mask[word] & bit) == bit &&
680 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
681 rcu_read_unlock();
682 return state;
683 }
684 }
685 }
686 rcu_read_unlock();
687 return AUDIT_BUILD_CONTEXT;
688}
689
690void audit_set_auditable(struct audit_context *ctx)
691{
692 ctx->auditable = 1;
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695static inline struct audit_context *audit_get_context(struct task_struct *tsk,
696 int return_valid,
697 int return_code)
698{
699 struct audit_context *context = tsk->audit_context;
700
701 if (likely(!context))
702 return NULL;
703 context->return_valid = return_valid;
704 context->return_code = return_code;
705
Al Virod51374a2006-08-03 10:59:26 -0400706 if (context->in_syscall && !context->dummy && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400708
David Woodhouse0f45aa12005-06-19 19:35:50 +0100709 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400710 if (state == AUDIT_RECORD_CONTEXT) {
711 context->auditable = 1;
712 goto get_context;
713 }
714
715 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (state == AUDIT_RECORD_CONTEXT)
717 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Amy Griffisf368c07d2006-04-07 16:55:56 -0400721get_context:
Al Viro3f2792f2006-07-16 06:43:48 -0400722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 tsk->audit_context = NULL;
724 return context;
725}
726
727static inline void audit_free_names(struct audit_context *context)
728{
729 int i;
730
731#if AUDIT_DEBUG == 2
732 if (context->auditable
733 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000734 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 " name_count=%d put_count=%d"
736 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000737 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 context->serial, context->major, context->in_syscall,
739 context->name_count, context->put_count,
740 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000741 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 printk(KERN_ERR "names[%d] = %p = %s\n", i,
743 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000744 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 dump_stack();
747 return;
748 }
749#endif
750#if AUDIT_DEBUG
751 context->put_count = 0;
752 context->ino_count = 0;
753#endif
754
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000755 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400756 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100760 if (context->pwd)
761 dput(context->pwd);
762 if (context->pwdmnt)
763 mntput(context->pwdmnt);
764 context->pwd = NULL;
765 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768static inline void audit_free_aux(struct audit_context *context)
769{
770 struct audit_aux_data *aux;
771
772 while ((aux = context->aux)) {
773 context->aux = aux->next;
774 kfree(aux);
775 }
Amy Griffise54dc242007-03-29 18:01:04 -0400776 while ((aux = context->aux_pids)) {
777 context->aux_pids = aux->next;
778 kfree(aux);
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782static inline void audit_zero_context(struct audit_context *context,
783 enum audit_state state)
784{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 memset(context, 0, sizeof(*context));
786 context->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789static inline struct audit_context *audit_alloc_context(enum audit_state state)
790{
791 struct audit_context *context;
792
793 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
794 return NULL;
795 audit_zero_context(context, state);
796 return context;
797}
798
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700799/**
800 * audit_alloc - allocate an audit context block for a task
801 * @tsk: task
802 *
803 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * if necessary. Doing so turns on system call auditing for the
805 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700806 * needed.
807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808int audit_alloc(struct task_struct *tsk)
809{
810 struct audit_context *context;
811 enum audit_state state;
812
813 if (likely(!audit_enabled))
814 return 0; /* Return if not auditing. */
815
816 state = audit_filter_task(tsk);
817 if (likely(state == AUDIT_DISABLED))
818 return 0;
819
820 if (!(context = audit_alloc_context(state))) {
821 audit_log_lost("out of memory in audit_alloc");
822 return -ENOMEM;
823 }
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 tsk->audit_context = context;
826 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
827 return 0;
828}
829
830static inline void audit_free_context(struct audit_context *context)
831{
832 struct audit_context *previous;
833 int count = 0;
834
835 do {
836 previous = context->previous;
837 if (previous || (count && count < 10)) {
838 ++count;
839 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
840 " freeing multiple contexts (%d)\n",
841 context->serial, context->major,
842 context->name_count, count);
843 }
844 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -0400845 unroll_tree_refs(context, NULL, 0);
846 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400848 kfree(context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 kfree(context);
850 context = previous;
851 } while (context);
852 if (count >= 10)
853 printk(KERN_ERR "audit: freed %d contexts\n", count);
854}
855
Joy Latten161a09e2006-11-27 13:11:54 -0600856void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000857{
858 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000859 unsigned len;
860 int error;
861 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000862
Al Viroc4823bc2007-03-12 16:17:42 +0000863 selinux_get_task_sid(current, &sid);
864 if (!sid)
865 return;
866
867 error = selinux_sid_to_string(sid, &ctx, &len);
868 if (error) {
869 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000870 goto error_path;
871 return;
872 }
873
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000874 audit_log_format(ab, " subj=%s", ctx);
Al Viroc4823bc2007-03-12 16:17:42 +0000875 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000876 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000877
878error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000879 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000880 return;
881}
882
Joy Latten161a09e2006-11-27 13:11:54 -0600883EXPORT_SYMBOL(audit_log_task_context);
884
Al Viroe4951492006-03-29 20:17:10 -0500885static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700886{
Al Viro45d9bb02006-03-29 20:02:55 -0500887 char name[sizeof(tsk->comm)];
888 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700889 struct vm_area_struct *vma;
890
Al Viroe4951492006-03-29 20:17:10 -0500891 /* tsk == current */
892
Al Viro45d9bb02006-03-29 20:02:55 -0500893 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100894 audit_log_format(ab, " comm=");
895 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700896
Al Viroe4951492006-03-29 20:17:10 -0500897 if (mm) {
898 down_read(&mm->mmap_sem);
899 vma = mm->mmap;
900 while (vma) {
901 if ((vma->vm_flags & VM_EXECUTABLE) &&
902 vma->vm_file) {
903 audit_log_d_path(ab, "exe=",
Josef Sipeka7a005f2006-12-08 02:37:17 -0800904 vma->vm_file->f_path.dentry,
905 vma->vm_file->f_path.mnt);
Al Viroe4951492006-03-29 20:17:10 -0500906 break;
907 }
908 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700909 }
Al Viroe4951492006-03-29 20:17:10 -0500910 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700911 }
Al Viroe4951492006-03-29 20:17:10 -0500912 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700913}
914
Amy Griffise54dc242007-03-29 18:01:04 -0400915static int audit_log_pid_context(struct audit_context *context, pid_t pid,
916 u32 sid)
917{
918 struct audit_buffer *ab;
919 char *s = NULL;
920 u32 len;
921 int rc = 0;
922
923 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
924 if (!ab)
925 return 1;
926
927 if (selinux_sid_to_string(sid, &s, &len)) {
928 audit_log_format(ab, "opid=%d obj=(none)", pid);
929 rc = 1;
930 } else
931 audit_log_format(ab, "opid=%d obj=%s", pid, s);
932 audit_log_end(ab);
933 kfree(s);
934
935 return rc;
936}
937
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700938static void audit_log_execve_info(struct audit_buffer *ab,
939 struct audit_aux_data_execve *axi)
940{
941 int i;
942 long len, ret;
Peter Zijlstra040b3a22007-07-28 00:55:18 +0200943 const char __user *p;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700944 char *buf;
945
946 if (axi->mm != current->mm)
947 return; /* execve failed, no additional info */
948
Peter Zijlstra040b3a22007-07-28 00:55:18 +0200949 p = (const char __user *)axi->mm->arg_start;
950
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700951 for (i = 0; i < axi->argc; i++, p += len) {
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700952 len = strnlen_user(p, MAX_ARG_STRLEN);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700953 /*
954 * We just created this mm, if we can't find the strings
955 * we just copied into it something is _very_ wrong. Similar
956 * for strings that are too long, we should not have created
957 * any.
958 */
959 if (!len || len > MAX_ARG_STRLEN) {
960 WARN_ON(1);
961 send_sig(SIGKILL, current, 0);
962 }
963
964 buf = kmalloc(len, GFP_KERNEL);
965 if (!buf) {
966 audit_panic("out of memory for argv string\n");
967 break;
968 }
969
970 ret = copy_from_user(buf, p, len);
971 /*
972 * There is no reason for this copy to be short. We just
973 * copied them here, and the mm hasn't been exposed to user-
974 * space yet.
975 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +0200976 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700977 WARN_ON(1);
978 send_sig(SIGKILL, current, 0);
979 }
980
981 audit_log_format(ab, "a%d=", i);
982 audit_log_untrustedstring(ab, buf);
983 audit_log_format(ab, "\n");
984
985 kfree(buf);
986 }
987}
988
Al Viroe4951492006-03-29 20:17:10 -0500989static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500991 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100993 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500994 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Al Viroe4951492006-03-29 20:17:10 -0500996 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -0400997 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -0400998 if (!context->ppid)
999 context->ppid = sys_getppid();
Al Viro3f2792f2006-07-16 06:43:48 -04001000 context->uid = tsk->uid;
1001 context->gid = tsk->gid;
1002 context->euid = tsk->euid;
1003 context->suid = tsk->suid;
1004 context->fsuid = tsk->fsuid;
1005 context->egid = tsk->egid;
1006 context->sgid = tsk->sgid;
1007 context->fsgid = tsk->fsgid;
1008 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001009
1010 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (!ab)
1012 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001013 audit_log_format(ab, "arch=%x syscall=%d",
1014 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (context->personality != PER_LINUX)
1016 audit_log_format(ab, " per=%lx", context->personality);
1017 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001018 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001019 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1020 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001021
1022 mutex_lock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001023 read_lock(&tasklist_lock);
Al Viro45d9bb02006-03-29 20:02:55 -05001024 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1025 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -05001026 else
1027 tty = "(none)";
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001028 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 audit_log_format(ab,
1030 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -04001031 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +01001032 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -05001033 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 context->argv[0],
1035 context->argv[1],
1036 context->argv[2],
1037 context->argv[3],
1038 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -04001039 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 context->pid,
Al Virobfef93a2008-01-10 04:53:18 -05001041 tsk->loginuid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 context->uid,
1043 context->gid,
1044 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -05001045 context->egid, context->sgid, context->fsgid, tty);
Alan Coxeb84a202006-09-29 02:01:41 -07001046
1047 mutex_unlock(&tty_mutex);
1048
Al Viroe4951492006-03-29 20:17:10 -05001049 audit_log_task_info(ab, tsk);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001050 if (context->filterkey) {
1051 audit_log_format(ab, " key=");
1052 audit_log_untrustedstring(ab, context->filterkey);
1053 } else
1054 audit_log_format(ab, " key=(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
David Woodhouse7551ced2005-05-26 12:04:57 +01001057 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001058
Al Viroe4951492006-03-29 20:17:10 -05001059 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (!ab)
1061 continue; /* audit_panic has been called */
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001064 case AUDIT_MQ_OPEN: {
1065 struct audit_aux_data_mq_open *axi = (void *)aux;
1066 audit_log_format(ab,
1067 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1068 "mq_msgsize=%ld mq_curmsgs=%ld",
1069 axi->oflag, axi->mode, axi->attr.mq_flags,
1070 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1071 axi->attr.mq_curmsgs);
1072 break; }
1073
1074 case AUDIT_MQ_SENDRECV: {
1075 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1076 audit_log_format(ab,
1077 "mqdes=%d msg_len=%zd msg_prio=%u "
1078 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1079 axi->mqdes, axi->msg_len, axi->msg_prio,
1080 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1081 break; }
1082
1083 case AUDIT_MQ_NOTIFY: {
1084 struct audit_aux_data_mq_notify *axi = (void *)aux;
1085 audit_log_format(ab,
1086 "mqdes=%d sigev_signo=%d",
1087 axi->mqdes,
1088 axi->notification.sigev_signo);
1089 break; }
1090
1091 case AUDIT_MQ_GETSETATTR: {
1092 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1093 audit_log_format(ab,
1094 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1095 "mq_curmsgs=%ld ",
1096 axi->mqdes,
1097 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1098 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1099 break; }
1100
Steve Grubbc0404992005-05-13 18:17:42 +01001101 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct audit_aux_data_ipcctl *axi = (void *)aux;
1103 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001104 "ouid=%u ogid=%u mode=%#o",
Linda Knippersac032212006-05-16 22:03:48 -04001105 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001106 if (axi->osid != 0) {
1107 char *ctx = NULL;
1108 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -07001109 if (selinux_sid_to_string(
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001110 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -05001111 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001112 axi->osid);
1113 call_panic = 1;
1114 } else
1115 audit_log_format(ab, " obj=%s", ctx);
1116 kfree(ctx);
1117 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001118 break; }
1119
Steve Grubb073115d2006-04-02 17:07:33 -04001120 case AUDIT_IPC_SET_PERM: {
1121 struct audit_aux_data_ipcctl *axi = (void *)aux;
1122 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001123 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
Steve Grubb073115d2006-04-02 17:07:33 -04001124 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -04001125 break; }
Linda Knippersac032212006-05-16 22:03:48 -04001126
Al Viro473ae302006-04-26 14:04:08 -04001127 case AUDIT_EXECVE: {
1128 struct audit_aux_data_execve *axi = (void *)aux;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001129 audit_log_execve_info(ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001130 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001131
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001132 case AUDIT_SOCKETCALL: {
1133 int i;
1134 struct audit_aux_data_socketcall *axs = (void *)aux;
1135 audit_log_format(ab, "nargs=%d", axs->nargs);
1136 for (i=0; i<axs->nargs; i++)
1137 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1138 break; }
1139
1140 case AUDIT_SOCKADDR: {
1141 struct audit_aux_data_sockaddr *axs = (void *)aux;
1142
1143 audit_log_format(ab, "saddr=");
1144 audit_log_hex(ab, axs->a, axs->len);
1145 break; }
Stephen Smalley01116102005-05-21 00:15:52 +01001146
Al Virodb349502007-02-07 01:48:00 -05001147 case AUDIT_FD_PAIR: {
1148 struct audit_aux_data_fd_pair *axs = (void *)aux;
1149 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1150 break; }
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155
Amy Griffise54dc242007-03-29 18:01:04 -04001156 for (aux = context->aux_pids; aux; aux = aux->next) {
1157 struct audit_aux_data_pids *axs = (void *)aux;
1158 int i;
1159
1160 for (i = 0; i < axs->pid_count; i++)
1161 if (audit_log_pid_context(context, axs->target_pid[i],
1162 axs->target_sid[i]))
1163 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001164 }
1165
Amy Griffise54dc242007-03-29 18:01:04 -04001166 if (context->target_pid &&
1167 audit_log_pid_context(context, context->target_pid,
1168 context->target_sid))
1169 call_panic = 1;
1170
David Woodhouse8f37d472005-05-27 12:17:28 +01001171 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -05001172 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001173 if (ab) {
1174 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1175 audit_log_end(ab);
1176 }
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -04001179 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +00001180
Al Viroe4951492006-03-29 20:17:10 -05001181 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (!ab)
1183 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +01001184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +00001186
Amy Griffis9c937dc2006-06-08 23:19:31 -04001187 if (n->name) {
1188 switch(n->name_len) {
1189 case AUDIT_NAME_FULL:
1190 /* log the full path */
1191 audit_log_format(ab, " name=");
1192 audit_log_untrustedstring(ab, n->name);
1193 break;
1194 case 0:
1195 /* name was specified as a relative path and the
1196 * directory component is the cwd */
1197 audit_log_d_path(ab, " name=", context->pwd,
1198 context->pwdmnt);
1199 break;
1200 default:
1201 /* log the name's directory component */
1202 audit_log_format(ab, " name=");
1203 audit_log_n_untrustedstring(ab, n->name_len,
1204 n->name);
1205 }
1206 } else
1207 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001208
Amy Griffis9c937dc2006-06-08 23:19:31 -04001209 if (n->ino != (unsigned long)-1) {
1210 audit_log_format(ab, " inode=%lu"
1211 " dev=%02x:%02x mode=%#o"
1212 " ouid=%u ogid=%u rdev=%02x:%02x",
1213 n->ino,
1214 MAJOR(n->dev),
1215 MINOR(n->dev),
1216 n->mode,
1217 n->uid,
1218 n->gid,
1219 MAJOR(n->rdev),
1220 MINOR(n->rdev));
1221 }
1222 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001223 char *ctx = NULL;
1224 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -07001225 if (selinux_sid_to_string(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001226 n->osid, &ctx, &len)) {
1227 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001228 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -04001229 } else
1230 audit_log_format(ab, " obj=%s", ctx);
1231 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001232 }
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 audit_log_end(ab);
1235 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001236 if (call_panic)
1237 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001240/**
1241 * audit_free - free a per-task audit context
1242 * @tsk: task whose audit context block to free
1243 *
Al Virofa84cb92006-03-29 20:30:19 -05001244 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246void audit_free(struct task_struct *tsk)
1247{
1248 struct audit_context *context;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (likely(!context))
1252 return;
1253
1254 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001255 * function (e.g., exit_group), then free context block.
1256 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001257 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001258 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +01001259 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001260 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 audit_free_context(context);
1263}
1264
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001265/**
1266 * audit_syscall_entry - fill in an audit record at syscall entry
1267 * @tsk: task being audited
1268 * @arch: architecture type
1269 * @major: major syscall type (function)
1270 * @a1: additional syscall register 1
1271 * @a2: additional syscall register 2
1272 * @a3: additional syscall register 3
1273 * @a4: additional syscall register 4
1274 *
1275 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * audit context was created when the task was created and the state or
1277 * filters demand the audit context be built. If the state from the
1278 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1279 * then the record will be written at syscall exit time (otherwise, it
1280 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001281 * be written).
1282 */
Al Viro5411be52006-03-29 20:23:36 -05001283void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 unsigned long a1, unsigned long a2,
1285 unsigned long a3, unsigned long a4)
1286{
Al Viro5411be52006-03-29 20:23:36 -05001287 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 struct audit_context *context = tsk->audit_context;
1289 enum audit_state state;
1290
1291 BUG_ON(!context);
1292
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001293 /*
1294 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 * calls in kernel_thread via the entry.S interface, instead of
1296 * with direct calls. (If you are porting to a new
1297 * architecture, hitting this condition can indicate that you
1298 * got the _exit/_leave calls backward in entry.S.)
1299 *
1300 * i386 no
1301 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001302 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 *
1304 * This also happens with vm86 emulation in a non-nested manner
1305 * (entries without exits), so this case must be caught.
1306 */
1307 if (context->in_syscall) {
1308 struct audit_context *newctx;
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310#if AUDIT_DEBUG
1311 printk(KERN_ERR
1312 "audit(:%d) pid=%d in syscall=%d;"
1313 " entering syscall=%d\n",
1314 context->serial, tsk->pid, context->major, major);
1315#endif
1316 newctx = audit_alloc_context(context->state);
1317 if (newctx) {
1318 newctx->previous = context;
1319 context = newctx;
1320 tsk->audit_context = newctx;
1321 } else {
1322 /* If we can't alloc a new context, the best we
1323 * can do is to leak memory (any pending putname
1324 * will be lost). The only other alternative is
1325 * to abandon auditing. */
1326 audit_zero_context(context, context->state);
1327 }
1328 }
1329 BUG_ON(context->in_syscall || context->name_count);
1330
1331 if (!audit_enabled)
1332 return;
1333
2fd6f582005-04-29 16:08:28 +01001334 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 context->major = major;
1336 context->argv[0] = a1;
1337 context->argv[1] = a2;
1338 context->argv[2] = a3;
1339 context->argv[3] = a4;
1340
1341 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001342 context->dummy = !audit_n_rules;
1343 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
David Woodhouse0f45aa12005-06-19 19:35:50 +01001344 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (likely(state == AUDIT_DISABLED))
1346 return;
1347
David Woodhousece625a82005-07-18 14:24:46 -04001348 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 context->ctime = CURRENT_TIME;
1350 context->in_syscall = 1;
1351 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
Alexander Viro419c58f2006-09-29 00:08:50 -04001352 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001355/**
1356 * audit_syscall_exit - deallocate audit context after a system call
1357 * @tsk: task being audited
1358 * @valid: success/failure flag
1359 * @return_code: syscall return value
1360 *
1361 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1363 * filtering, or because some other part of the kernel write an audit
1364 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001365 * free the names stored from getname().
1366 */
Al Viro5411be52006-03-29 20:23:36 -05001367void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Al Viro5411be52006-03-29 20:23:36 -05001369 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 struct audit_context *context;
1371
2fd6f582005-04-29 16:08:28 +01001372 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001375 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
David Woodhousef7056d62005-06-20 16:07:33 +01001377 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001378 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 context->in_syscall = 0;
1381 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (context->previous) {
1384 struct audit_context *new_context = context->previous;
1385 context->previous = NULL;
1386 audit_free_context(context);
1387 tsk->audit_context = new_context;
1388 } else {
1389 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001390 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001392 context->aux = NULL;
1393 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001394 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001395 context->target_sid = 0;
Amy Griffis5adc8a62006-06-14 18:45:21 -04001396 kfree(context->filterkey);
1397 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 tsk->audit_context = context;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Al Viro74c3cbe2007-07-22 08:04:18 -04001402static inline void handle_one(const struct inode *inode)
1403{
1404#ifdef CONFIG_AUDIT_TREE
1405 struct audit_context *context;
1406 struct audit_tree_refs *p;
1407 struct audit_chunk *chunk;
1408 int count;
1409 if (likely(list_empty(&inode->inotify_watches)))
1410 return;
1411 context = current->audit_context;
1412 p = context->trees;
1413 count = context->tree_count;
1414 rcu_read_lock();
1415 chunk = audit_tree_lookup(inode);
1416 rcu_read_unlock();
1417 if (!chunk)
1418 return;
1419 if (likely(put_tree_ref(context, chunk)))
1420 return;
1421 if (unlikely(!grow_tree_refs(context))) {
1422 printk(KERN_WARNING "out of memory, audit has lost a tree reference");
1423 audit_set_auditable(context);
1424 audit_put_chunk(chunk);
1425 unroll_tree_refs(context, p, count);
1426 return;
1427 }
1428 put_tree_ref(context, chunk);
1429#endif
1430}
1431
1432static void handle_path(const struct dentry *dentry)
1433{
1434#ifdef CONFIG_AUDIT_TREE
1435 struct audit_context *context;
1436 struct audit_tree_refs *p;
1437 const struct dentry *d, *parent;
1438 struct audit_chunk *drop;
1439 unsigned long seq;
1440 int count;
1441
1442 context = current->audit_context;
1443 p = context->trees;
1444 count = context->tree_count;
1445retry:
1446 drop = NULL;
1447 d = dentry;
1448 rcu_read_lock();
1449 seq = read_seqbegin(&rename_lock);
1450 for(;;) {
1451 struct inode *inode = d->d_inode;
1452 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1453 struct audit_chunk *chunk;
1454 chunk = audit_tree_lookup(inode);
1455 if (chunk) {
1456 if (unlikely(!put_tree_ref(context, chunk))) {
1457 drop = chunk;
1458 break;
1459 }
1460 }
1461 }
1462 parent = d->d_parent;
1463 if (parent == d)
1464 break;
1465 d = parent;
1466 }
1467 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1468 rcu_read_unlock();
1469 if (!drop) {
1470 /* just a race with rename */
1471 unroll_tree_refs(context, p, count);
1472 goto retry;
1473 }
1474 audit_put_chunk(drop);
1475 if (grow_tree_refs(context)) {
1476 /* OK, got more space */
1477 unroll_tree_refs(context, p, count);
1478 goto retry;
1479 }
1480 /* too bad */
1481 printk(KERN_WARNING
1482 "out of memory, audit has lost a tree reference");
1483 unroll_tree_refs(context, p, count);
1484 audit_set_auditable(context);
1485 return;
1486 }
1487 rcu_read_unlock();
1488#endif
1489}
1490
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001491/**
1492 * audit_getname - add a name to the list
1493 * @name: name to add
1494 *
1495 * Add a name to the list of audit names for this context.
1496 * Called from fs/namei.c:getname().
1497 */
Al Virod8945bb52006-05-18 16:01:30 -04001498void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 struct audit_context *context = current->audit_context;
1501
Al Virod8945bb52006-05-18 16:01:30 -04001502 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return;
1504
1505 if (!context->in_syscall) {
1506#if AUDIT_DEBUG == 2
1507 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1508 __FILE__, __LINE__, context->serial, name);
1509 dump_stack();
1510#endif
1511 return;
1512 }
1513 BUG_ON(context->name_count >= AUDIT_NAMES);
1514 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001515 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1516 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 context->names[context->name_count].ino = (unsigned long)-1;
Amy Griffise41e8bd2007-02-13 14:14:09 -05001518 context->names[context->name_count].osid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001520 if (!context->pwd) {
1521 read_lock(&current->fs->lock);
1522 context->pwd = dget(current->fs->pwd);
1523 context->pwdmnt = mntget(current->fs->pwdmnt);
1524 read_unlock(&current->fs->lock);
1525 }
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527}
1528
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001529/* audit_putname - intercept a putname request
1530 * @name: name to intercept and delay for putname
1531 *
1532 * If we have stored the name from getname in the audit context,
1533 * then we delay the putname until syscall exit.
1534 * Called from include/linux/fs.h:putname().
1535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536void audit_putname(const char *name)
1537{
1538 struct audit_context *context = current->audit_context;
1539
1540 BUG_ON(!context);
1541 if (!context->in_syscall) {
1542#if AUDIT_DEBUG == 2
1543 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1544 __FILE__, __LINE__, context->serial, name);
1545 if (context->name_count) {
1546 int i;
1547 for (i = 0; i < context->name_count; i++)
1548 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1549 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001550 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552#endif
1553 __putname(name);
1554 }
1555#if AUDIT_DEBUG
1556 else {
1557 ++context->put_count;
1558 if (context->put_count > context->name_count) {
1559 printk(KERN_ERR "%s:%d(:%d): major=%d"
1560 " in_syscall=%d putname(%p) name_count=%d"
1561 " put_count=%d\n",
1562 __FILE__, __LINE__,
1563 context->serial, context->major,
1564 context->in_syscall, name, context->name_count,
1565 context->put_count);
1566 dump_stack();
1567 }
1568 }
1569#endif
1570}
1571
Amy Griffis5712e882007-02-13 14:15:22 -05001572static int audit_inc_name_count(struct audit_context *context,
1573 const struct inode *inode)
1574{
1575 if (context->name_count >= AUDIT_NAMES) {
1576 if (inode)
1577 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1578 "dev=%02x:%02x, inode=%lu",
1579 MAJOR(inode->i_sb->s_dev),
1580 MINOR(inode->i_sb->s_dev),
1581 inode->i_ino);
1582
1583 else
1584 printk(KERN_DEBUG "name_count maxed, losing inode data");
1585 return 1;
1586 }
1587 context->name_count++;
1588#if AUDIT_DEBUG
1589 context->ino_count++;
1590#endif
1591 return 0;
1592}
1593
Amy Griffis3e2efce2006-07-13 13:16:02 -04001594/* Copy inode data into an audit_names. */
1595static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001596{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001597 name->ino = inode->i_ino;
1598 name->dev = inode->i_sb->s_dev;
1599 name->mode = inode->i_mode;
1600 name->uid = inode->i_uid;
1601 name->gid = inode->i_gid;
1602 name->rdev = inode->i_rdev;
1603 selinux_get_inode_sid(inode, &name->osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001604}
1605
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001606/**
1607 * audit_inode - store the inode and device from a lookup
1608 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001609 * @dentry: dentry being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001610 *
1611 * Called from fs/namei.c:path_lookup().
1612 */
Al Viro5a190ae2007-06-07 12:19:32 -04001613void __audit_inode(const char *name, const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 int idx;
1616 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04001617 const struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 if (!context->in_syscall)
1620 return;
1621 if (context->name_count
1622 && context->names[context->name_count-1].name
1623 && context->names[context->name_count-1].name == name)
1624 idx = context->name_count - 1;
1625 else if (context->name_count > 1
1626 && context->names[context->name_count-2].name
1627 && context->names[context->name_count-2].name == name)
1628 idx = context->name_count - 2;
1629 else {
1630 /* FIXME: how much do we care about inodes that have no
1631 * associated name? */
Amy Griffis5712e882007-02-13 14:15:22 -05001632 if (audit_inc_name_count(context, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001634 idx = context->name_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 context->names[idx].name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001637 handle_path(dentry);
Amy Griffis3e2efce2006-07-13 13:16:02 -04001638 audit_copy_inode(&context->names[idx], inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Amy Griffis73241cc2005-11-03 16:00:25 +00001641/**
1642 * audit_inode_child - collect inode info for created/removed objects
1643 * @dname: inode's dentry name
Randy Dunlap481968f2007-10-21 20:59:53 -07001644 * @dentry: dentry being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04001645 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00001646 *
1647 * For syscalls that create or remove filesystem objects, audit_inode
1648 * can only collect information for the filesystem object's parent.
1649 * This call updates the audit context with the child's information.
1650 * Syscalls that create a new filesystem object must be hooked after
1651 * the object is created. Syscalls that remove a filesystem object
1652 * must be hooked prior, in order to capture the target inode during
1653 * unsuccessful attempts.
1654 */
Al Viro5a190ae2007-06-07 12:19:32 -04001655void __audit_inode_child(const char *dname, const struct dentry *dentry,
Amy Griffis73d3ec52006-07-13 13:16:39 -04001656 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00001657{
1658 int idx;
1659 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05001660 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04001661 const struct inode *inode = dentry->d_inode;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001662 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00001663
1664 if (!context->in_syscall)
1665 return;
1666
Al Viro74c3cbe2007-07-22 08:04:18 -04001667 if (inode)
1668 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001669 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001670 if (!dname)
Amy Griffis5712e882007-02-13 14:15:22 -05001671 goto add_names;
Amy Griffis73241cc2005-11-03 16:00:25 +00001672
Amy Griffis5712e882007-02-13 14:15:22 -05001673 /* parent is more likely, look for it first */
1674 for (idx = 0; idx < context->name_count; idx++) {
1675 struct audit_names *n = &context->names[idx];
Amy Griffis73241cc2005-11-03 16:00:25 +00001676
Amy Griffis5712e882007-02-13 14:15:22 -05001677 if (!n->name)
1678 continue;
1679
1680 if (n->ino == parent->i_ino &&
1681 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1682 n->name_len = dirlen; /* update parent data in place */
1683 found_parent = n->name;
1684 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001685 }
Steve Grubbac9910c2006-09-28 14:31:32 -04001686 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001687
Amy Griffis5712e882007-02-13 14:15:22 -05001688 /* no matching parent, look for matching child */
1689 for (idx = 0; idx < context->name_count; idx++) {
1690 struct audit_names *n = &context->names[idx];
Amy Griffis73d3ec52006-07-13 13:16:39 -04001691
Amy Griffis5712e882007-02-13 14:15:22 -05001692 if (!n->name)
1693 continue;
1694
1695 /* strcmp() is the more likely scenario */
1696 if (!strcmp(dname, n->name) ||
1697 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1698 if (inode)
1699 audit_copy_inode(n, inode);
1700 else
1701 n->ino = (unsigned long)-1;
1702 found_child = n->name;
1703 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04001704 }
Amy Griffis5712e882007-02-13 14:15:22 -05001705 }
1706
1707add_names:
1708 if (!found_parent) {
1709 if (audit_inc_name_count(context, parent))
1710 return;
1711 idx = context->name_count - 1;
1712 context->names[idx].name = NULL;
Amy Griffis73d3ec52006-07-13 13:16:39 -04001713 audit_copy_inode(&context->names[idx], parent);
1714 }
Amy Griffis5712e882007-02-13 14:15:22 -05001715
1716 if (!found_child) {
1717 if (audit_inc_name_count(context, inode))
1718 return;
1719 idx = context->name_count - 1;
1720
1721 /* Re-use the name belonging to the slot for a matching parent
1722 * directory. All names for this context are relinquished in
1723 * audit_free_names() */
1724 if (found_parent) {
1725 context->names[idx].name = found_parent;
1726 context->names[idx].name_len = AUDIT_NAME_FULL;
1727 /* don't call __putname() */
1728 context->names[idx].name_put = 0;
1729 } else {
1730 context->names[idx].name = NULL;
1731 }
1732
1733 if (inode)
1734 audit_copy_inode(&context->names[idx], inode);
1735 else
1736 context->names[idx].ino = (unsigned long)-1;
1737 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001738}
Trond Myklebust50e437d2007-06-07 22:44:34 -04001739EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04001740
1741/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001742 * auditsc_get_stamp - get local copies of audit_context values
1743 * @ctx: audit_context for the task
1744 * @t: timespec to store time recorded in the audit_context
1745 * @serial: serial value that is recorded in the audit_context
1746 *
1747 * Also sets the context as auditable.
1748 */
David Woodhousebfb44962005-05-21 21:08:09 +01001749void auditsc_get_stamp(struct audit_context *ctx,
1750 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
David Woodhousece625a82005-07-18 14:24:46 -04001752 if (!ctx->serial)
1753 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001754 t->tv_sec = ctx->ctime.tv_sec;
1755 t->tv_nsec = ctx->ctime.tv_nsec;
1756 *serial = ctx->serial;
1757 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001760/**
1761 * audit_set_loginuid - set a task's audit_context loginuid
1762 * @task: task whose audit context is being modified
1763 * @loginuid: loginuid value
1764 *
1765 * Returns 0.
1766 *
1767 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1768 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001769int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Steve Grubb41757102006-06-12 07:48:28 -04001771 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01001772
Al Virobfef93a2008-01-10 04:53:18 -05001773 if (context && context->in_syscall) {
1774 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04001775
Al Virobfef93a2008-01-10 04:53:18 -05001776 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1777 if (ab) {
1778 audit_log_format(ab, "login pid=%d uid=%u "
1779 "old auid=%u new auid=%u",
1780 task->pid, task->uid,
1781 task->loginuid, loginuid);
1782 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01001783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
Al Virobfef93a2008-01-10 04:53:18 -05001785 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return 0;
1787}
1788
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001789/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05001790 * __audit_mq_open - record audit data for a POSIX MQ open
1791 * @oflag: open flag
1792 * @mode: mode bits
1793 * @u_attr: queue attributes
1794 *
1795 * Returns 0 for success or NULL context or < 0 on error.
1796 */
1797int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1798{
1799 struct audit_aux_data_mq_open *ax;
1800 struct audit_context *context = current->audit_context;
1801
1802 if (!audit_enabled)
1803 return 0;
1804
1805 if (likely(!context))
1806 return 0;
1807
1808 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1809 if (!ax)
1810 return -ENOMEM;
1811
1812 if (u_attr != NULL) {
1813 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1814 kfree(ax);
1815 return -EFAULT;
1816 }
1817 } else
1818 memset(&ax->attr, 0, sizeof(ax->attr));
1819
1820 ax->oflag = oflag;
1821 ax->mode = mode;
1822
1823 ax->d.type = AUDIT_MQ_OPEN;
1824 ax->d.next = context->aux;
1825 context->aux = (void *)ax;
1826 return 0;
1827}
1828
1829/**
1830 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1831 * @mqdes: MQ descriptor
1832 * @msg_len: Message length
1833 * @msg_prio: Message priority
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001834 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001835 *
1836 * Returns 0 for success or NULL context or < 0 on error.
1837 */
1838int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1839 const struct timespec __user *u_abs_timeout)
1840{
1841 struct audit_aux_data_mq_sendrecv *ax;
1842 struct audit_context *context = current->audit_context;
1843
1844 if (!audit_enabled)
1845 return 0;
1846
1847 if (likely(!context))
1848 return 0;
1849
1850 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1851 if (!ax)
1852 return -ENOMEM;
1853
1854 if (u_abs_timeout != NULL) {
1855 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1856 kfree(ax);
1857 return -EFAULT;
1858 }
1859 } else
1860 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1861
1862 ax->mqdes = mqdes;
1863 ax->msg_len = msg_len;
1864 ax->msg_prio = msg_prio;
1865
1866 ax->d.type = AUDIT_MQ_SENDRECV;
1867 ax->d.next = context->aux;
1868 context->aux = (void *)ax;
1869 return 0;
1870}
1871
1872/**
1873 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1874 * @mqdes: MQ descriptor
1875 * @msg_len: Message length
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001876 * @u_msg_prio: Message priority
1877 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001878 *
1879 * Returns 0 for success or NULL context or < 0 on error.
1880 */
1881int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1882 unsigned int __user *u_msg_prio,
1883 const struct timespec __user *u_abs_timeout)
1884{
1885 struct audit_aux_data_mq_sendrecv *ax;
1886 struct audit_context *context = current->audit_context;
1887
1888 if (!audit_enabled)
1889 return 0;
1890
1891 if (likely(!context))
1892 return 0;
1893
1894 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1895 if (!ax)
1896 return -ENOMEM;
1897
1898 if (u_msg_prio != NULL) {
1899 if (get_user(ax->msg_prio, u_msg_prio)) {
1900 kfree(ax);
1901 return -EFAULT;
1902 }
1903 } else
1904 ax->msg_prio = 0;
1905
1906 if (u_abs_timeout != NULL) {
1907 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1908 kfree(ax);
1909 return -EFAULT;
1910 }
1911 } else
1912 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1913
1914 ax->mqdes = mqdes;
1915 ax->msg_len = msg_len;
1916
1917 ax->d.type = AUDIT_MQ_SENDRECV;
1918 ax->d.next = context->aux;
1919 context->aux = (void *)ax;
1920 return 0;
1921}
1922
1923/**
1924 * __audit_mq_notify - record audit data for a POSIX MQ notify
1925 * @mqdes: MQ descriptor
1926 * @u_notification: Notification event
1927 *
1928 * Returns 0 for success or NULL context or < 0 on error.
1929 */
1930
1931int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1932{
1933 struct audit_aux_data_mq_notify *ax;
1934 struct audit_context *context = current->audit_context;
1935
1936 if (!audit_enabled)
1937 return 0;
1938
1939 if (likely(!context))
1940 return 0;
1941
1942 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1943 if (!ax)
1944 return -ENOMEM;
1945
1946 if (u_notification != NULL) {
1947 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1948 kfree(ax);
1949 return -EFAULT;
1950 }
1951 } else
1952 memset(&ax->notification, 0, sizeof(ax->notification));
1953
1954 ax->mqdes = mqdes;
1955
1956 ax->d.type = AUDIT_MQ_NOTIFY;
1957 ax->d.next = context->aux;
1958 context->aux = (void *)ax;
1959 return 0;
1960}
1961
1962/**
1963 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1964 * @mqdes: MQ descriptor
1965 * @mqstat: MQ flags
1966 *
1967 * Returns 0 for success or NULL context or < 0 on error.
1968 */
1969int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1970{
1971 struct audit_aux_data_mq_getsetattr *ax;
1972 struct audit_context *context = current->audit_context;
1973
1974 if (!audit_enabled)
1975 return 0;
1976
1977 if (likely(!context))
1978 return 0;
1979
1980 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1981 if (!ax)
1982 return -ENOMEM;
1983
1984 ax->mqdes = mqdes;
1985 ax->mqstat = *mqstat;
1986
1987 ax->d.type = AUDIT_MQ_GETSETATTR;
1988 ax->d.next = context->aux;
1989 context->aux = (void *)ax;
1990 return 0;
1991}
1992
1993/**
Steve Grubb073115d2006-04-02 17:07:33 -04001994 * audit_ipc_obj - record audit data for ipc object
1995 * @ipcp: ipc permissions
1996 *
1997 * Returns 0 for success or NULL context or < 0 on error.
1998 */
Al Virod8945bb52006-05-18 16:01:30 -04001999int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002000{
2001 struct audit_aux_data_ipcctl *ax;
2002 struct audit_context *context = current->audit_context;
2003
Steve Grubb073115d2006-04-02 17:07:33 -04002004 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2005 if (!ax)
2006 return -ENOMEM;
2007
2008 ax->uid = ipcp->uid;
2009 ax->gid = ipcp->gid;
2010 ax->mode = ipcp->mode;
2011 selinux_get_ipc_sid(ipcp, &ax->osid);
2012
2013 ax->d.type = AUDIT_IPC;
2014 ax->d.next = context->aux;
2015 context->aux = (void *)ax;
2016 return 0;
2017}
2018
2019/**
2020 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002021 * @qbytes: msgq bytes
2022 * @uid: msgq user id
2023 * @gid: msgq group id
2024 * @mode: msgq mode (permissions)
2025 *
2026 * Returns 0 for success or NULL context or < 0 on error.
2027 */
Al Virod8945bb52006-05-18 16:01:30 -04002028int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 struct audit_aux_data_ipcctl *ax;
2031 struct audit_context *context = current->audit_context;
2032
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002033 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (!ax)
2035 return -ENOMEM;
2036
2037 ax->qbytes = qbytes;
2038 ax->uid = uid;
2039 ax->gid = gid;
2040 ax->mode = mode;
2041
Steve Grubb073115d2006-04-02 17:07:33 -04002042 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 ax->d.next = context->aux;
2044 context->aux = (void *)ax;
2045 return 0;
2046}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002047
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002048int audit_argv_kb = 32;
2049
Al Viro473ae302006-04-26 14:04:08 -04002050int audit_bprm(struct linux_binprm *bprm)
2051{
2052 struct audit_aux_data_execve *ax;
2053 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002054
Al Viro5ac3a9c2006-07-16 06:38:45 -04002055 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04002056 return 0;
2057
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002058 /*
2059 * Even though the stack code doesn't limit the arg+env size any more,
2060 * the audit code requires that _all_ arguments be logged in a single
2061 * netlink skb. Hence cap it :-(
2062 */
2063 if (bprm->argv_len > (audit_argv_kb << 10))
2064 return -E2BIG;
2065
2066 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002067 if (!ax)
2068 return -ENOMEM;
2069
2070 ax->argc = bprm->argc;
2071 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002072 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002073 ax->d.type = AUDIT_EXECVE;
2074 ax->d.next = context->aux;
2075 context->aux = (void *)ax;
2076 return 0;
2077}
2078
2079
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002080/**
2081 * audit_socketcall - record audit data for sys_socketcall
2082 * @nargs: number of args
2083 * @args: args array
2084 *
2085 * Returns 0 for success or NULL context or < 0 on error.
2086 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002087int audit_socketcall(int nargs, unsigned long *args)
2088{
2089 struct audit_aux_data_socketcall *ax;
2090 struct audit_context *context = current->audit_context;
2091
Al Viro5ac3a9c2006-07-16 06:38:45 -04002092 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002093 return 0;
2094
2095 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2096 if (!ax)
2097 return -ENOMEM;
2098
2099 ax->nargs = nargs;
2100 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2101
2102 ax->d.type = AUDIT_SOCKETCALL;
2103 ax->d.next = context->aux;
2104 context->aux = (void *)ax;
2105 return 0;
2106}
2107
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002108/**
Al Virodb349502007-02-07 01:48:00 -05002109 * __audit_fd_pair - record audit data for pipe and socketpair
2110 * @fd1: the first file descriptor
2111 * @fd2: the second file descriptor
2112 *
2113 * Returns 0 for success or NULL context or < 0 on error.
2114 */
2115int __audit_fd_pair(int fd1, int fd2)
2116{
2117 struct audit_context *context = current->audit_context;
2118 struct audit_aux_data_fd_pair *ax;
2119
2120 if (likely(!context)) {
2121 return 0;
2122 }
2123
2124 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2125 if (!ax) {
2126 return -ENOMEM;
2127 }
2128
2129 ax->fd[0] = fd1;
2130 ax->fd[1] = fd2;
2131
2132 ax->d.type = AUDIT_FD_PAIR;
2133 ax->d.next = context->aux;
2134 context->aux = (void *)ax;
2135 return 0;
2136}
2137
2138/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002139 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2140 * @len: data length in user space
2141 * @a: data address in kernel space
2142 *
2143 * Returns 0 for success or NULL context or < 0 on error.
2144 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002145int audit_sockaddr(int len, void *a)
2146{
2147 struct audit_aux_data_sockaddr *ax;
2148 struct audit_context *context = current->audit_context;
2149
Al Viro5ac3a9c2006-07-16 06:38:45 -04002150 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002151 return 0;
2152
2153 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2154 if (!ax)
2155 return -ENOMEM;
2156
2157 ax->len = len;
2158 memcpy(ax->a, a, len);
2159
2160 ax->d.type = AUDIT_SOCKADDR;
2161 ax->d.next = context->aux;
2162 context->aux = (void *)ax;
2163 return 0;
2164}
2165
Al Viroa5cb0132007-03-20 13:58:35 -04002166void __audit_ptrace(struct task_struct *t)
2167{
2168 struct audit_context *context = current->audit_context;
2169
2170 context->target_pid = t->pid;
2171 selinux_get_task_sid(t, &context->target_sid);
2172}
2173
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002174/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002175 * audit_signal_info - record signal info for shutting down audit subsystem
2176 * @sig: signal value
2177 * @t: task being signaled
2178 *
2179 * If the audit subsystem is being terminated, record the task (pid)
2180 * and uid that is doing that.
2181 */
Amy Griffise54dc242007-03-29 18:01:04 -04002182int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002183{
Amy Griffise54dc242007-03-29 18:01:04 -04002184 struct audit_aux_data_pids *axp;
2185 struct task_struct *tsk = current;
2186 struct audit_context *ctx = tsk->audit_context;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002187 extern pid_t audit_sig_pid;
2188 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04002189 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002190
Al Viro175fc482007-08-08 00:01:46 +01002191 if (audit_pid && t->tgid == audit_pid) {
2192 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
2193 audit_sig_pid = tsk->pid;
Al Virobfef93a2008-01-10 04:53:18 -05002194 if (tsk->loginuid != -1)
2195 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002196 else
2197 audit_sig_uid = tsk->uid;
2198 selinux_get_task_sid(tsk, &audit_sig_sid);
2199 }
2200 if (!audit_signals || audit_dummy_context())
2201 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002202 }
Amy Griffise54dc242007-03-29 18:01:04 -04002203
Amy Griffise54dc242007-03-29 18:01:04 -04002204 /* optimize the common case by putting first signal recipient directly
2205 * in audit_context */
2206 if (!ctx->target_pid) {
2207 ctx->target_pid = t->tgid;
2208 selinux_get_task_sid(t, &ctx->target_sid);
2209 return 0;
2210 }
2211
2212 axp = (void *)ctx->aux_pids;
2213 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2214 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2215 if (!axp)
2216 return -ENOMEM;
2217
2218 axp->d.type = AUDIT_OBJ_PID;
2219 axp->d.next = ctx->aux_pids;
2220 ctx->aux_pids = (void *)axp;
2221 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002222 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002223
2224 axp->target_pid[axp->pid_count] = t->tgid;
2225 selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
2226 axp->pid_count++;
2227
2228 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002229}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002230
2231/**
2232 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002233 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002234 *
2235 * If a process ends with a core dump, something fishy is going on and we
2236 * should record the event for investigation.
2237 */
2238void audit_core_dumps(long signr)
2239{
2240 struct audit_buffer *ab;
2241 u32 sid;
2242
2243 if (!audit_enabled)
2244 return;
2245
2246 if (signr == SIGQUIT) /* don't care for those */
2247 return;
2248
2249 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2250 audit_log_format(ab, "auid=%u uid=%u gid=%u",
Al Viro0c11b942008-01-10 04:20:52 -05002251 audit_get_loginuid(current),
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002252 current->uid, current->gid);
2253 selinux_get_task_sid(current, &sid);
2254 if (sid) {
2255 char *ctx = NULL;
2256 u32 len;
2257
2258 if (selinux_sid_to_string(sid, &ctx, &len))
2259 audit_log_format(ab, " ssid=%u", sid);
2260 else
2261 audit_log_format(ab, " subj=%s", ctx);
2262 kfree(ctx);
2263 }
2264 audit_log_format(ab, " pid=%d comm=", current->pid);
2265 audit_log_untrustedstring(ab, current->comm);
2266 audit_log_format(ab, " sig=%ld", signr);
2267 audit_log_end(ab);
2268}