blob: aaaca8a13bbe57cd17f084af9bc29a497fac1e62 [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];
Eric Paris4746ec52008-01-08 10:06:53 -0500181 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400182 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500183 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400184 int pid_count;
185};
186
Al Viro74c3cbe2007-07-22 08:04:18 -0400187struct audit_tree_refs {
188 struct audit_tree_refs *next;
189 struct audit_chunk *c[31];
190};
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* The per-task audit context. */
193struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400194 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int in_syscall; /* 1 if task is in a syscall */
196 enum audit_state state;
197 unsigned int serial; /* serial number for record */
198 struct timespec ctime; /* time of syscall entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int major; /* syscall number */
200 unsigned long argv[4]; /* syscall arguments */
201 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100202 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int auditable; /* 1 if record should be written */
204 int name_count;
205 struct audit_names names[AUDIT_NAMES];
Amy Griffis5adc8a62006-06-14 18:45:21 -0400206 char * filterkey; /* key for rule that triggered record */
David Woodhouse8f37d472005-05-27 12:17:28 +0100207 struct dentry * pwd;
208 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct audit_context *previous; /* For nested syscalls */
210 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400211 struct audit_aux_data *aux_pids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400214 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 uid_t uid, euid, suid, fsuid;
216 gid_t gid, egid, sgid, fsgid;
217 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100218 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Al Viroa5cb0132007-03-20 13:58:35 -0400220 pid_t target_pid;
Eric Parisc2a77802008-01-07 13:40:17 -0500221 uid_t target_auid;
222 uid_t target_uid;
Eric Paris4746ec52008-01-08 10:06:53 -0500223 unsigned int target_sessionid;
Al Viroa5cb0132007-03-20 13:58:35 -0400224 u32 target_sid;
Eric Parisc2a77802008-01-07 13:40:17 -0500225 char target_comm[TASK_COMM_LEN];
Al Viroa5cb0132007-03-20 13:58:35 -0400226
Al Viro74c3cbe2007-07-22 08:04:18 -0400227 struct audit_tree_refs *trees, *first_trees;
228 int tree_count;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#if AUDIT_DEBUG
231 int put_count;
232 int ino_count;
233#endif
234};
235
Al Viro55669bf2006-08-31 19:26:40 -0400236#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
237static inline int open_arg(int flags, int mask)
238{
239 int n = ACC_MODE(flags);
240 if (flags & (O_TRUNC | O_CREAT))
241 n |= AUDIT_PERM_WRITE;
242 return n & mask;
243}
244
245static int audit_match_perm(struct audit_context *ctx, int mask)
246{
247 unsigned n = ctx->major;
248 switch (audit_classify_syscall(ctx->arch, n)) {
249 case 0: /* native */
250 if ((mask & AUDIT_PERM_WRITE) &&
251 audit_match_class(AUDIT_CLASS_WRITE, n))
252 return 1;
253 if ((mask & AUDIT_PERM_READ) &&
254 audit_match_class(AUDIT_CLASS_READ, n))
255 return 1;
256 if ((mask & AUDIT_PERM_ATTR) &&
257 audit_match_class(AUDIT_CLASS_CHATTR, n))
258 return 1;
259 return 0;
260 case 1: /* 32bit on biarch */
261 if ((mask & AUDIT_PERM_WRITE) &&
262 audit_match_class(AUDIT_CLASS_WRITE_32, n))
263 return 1;
264 if ((mask & AUDIT_PERM_READ) &&
265 audit_match_class(AUDIT_CLASS_READ_32, n))
266 return 1;
267 if ((mask & AUDIT_PERM_ATTR) &&
268 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
269 return 1;
270 return 0;
271 case 2: /* open */
272 return mask & ACC_MODE(ctx->argv[1]);
273 case 3: /* openat */
274 return mask & ACC_MODE(ctx->argv[2]);
275 case 4: /* socketcall */
276 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
277 case 5: /* execve */
278 return mask & AUDIT_PERM_EXEC;
279 default:
280 return 0;
281 }
282}
283
Al Viro74c3cbe2007-07-22 08:04:18 -0400284/*
285 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
286 * ->first_trees points to its beginning, ->trees - to the current end of data.
287 * ->tree_count is the number of free entries in array pointed to by ->trees.
288 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
289 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
290 * it's going to remain 1-element for almost any setup) until we free context itself.
291 * References in it _are_ dropped - at the same time we free/drop aux stuff.
292 */
293
294#ifdef CONFIG_AUDIT_TREE
295static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
296{
297 struct audit_tree_refs *p = ctx->trees;
298 int left = ctx->tree_count;
299 if (likely(left)) {
300 p->c[--left] = chunk;
301 ctx->tree_count = left;
302 return 1;
303 }
304 if (!p)
305 return 0;
306 p = p->next;
307 if (p) {
308 p->c[30] = chunk;
309 ctx->trees = p;
310 ctx->tree_count = 30;
311 return 1;
312 }
313 return 0;
314}
315
316static int grow_tree_refs(struct audit_context *ctx)
317{
318 struct audit_tree_refs *p = ctx->trees;
319 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
320 if (!ctx->trees) {
321 ctx->trees = p;
322 return 0;
323 }
324 if (p)
325 p->next = ctx->trees;
326 else
327 ctx->first_trees = ctx->trees;
328 ctx->tree_count = 31;
329 return 1;
330}
331#endif
332
333static void unroll_tree_refs(struct audit_context *ctx,
334 struct audit_tree_refs *p, int count)
335{
336#ifdef CONFIG_AUDIT_TREE
337 struct audit_tree_refs *q;
338 int n;
339 if (!p) {
340 /* we started with empty chain */
341 p = ctx->first_trees;
342 count = 31;
343 /* if the very first allocation has failed, nothing to do */
344 if (!p)
345 return;
346 }
347 n = count;
348 for (q = p; q != ctx->trees; q = q->next, n = 31) {
349 while (n--) {
350 audit_put_chunk(q->c[n]);
351 q->c[n] = NULL;
352 }
353 }
354 while (n-- > ctx->tree_count) {
355 audit_put_chunk(q->c[n]);
356 q->c[n] = NULL;
357 }
358 ctx->trees = p;
359 ctx->tree_count = count;
360#endif
361}
362
363static void free_tree_refs(struct audit_context *ctx)
364{
365 struct audit_tree_refs *p, *q;
366 for (p = ctx->first_trees; p; p = q) {
367 q = p->next;
368 kfree(p);
369 }
370}
371
372static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
373{
374#ifdef CONFIG_AUDIT_TREE
375 struct audit_tree_refs *p;
376 int n;
377 if (!tree)
378 return 0;
379 /* full ones */
380 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
381 for (n = 0; n < 31; n++)
382 if (audit_tree_match(p->c[n], tree))
383 return 1;
384 }
385 /* partial */
386 if (p) {
387 for (n = ctx->tree_count; n < 31; n++)
388 if (audit_tree_match(p->c[n], tree))
389 return 1;
390 }
391#endif
392 return 0;
393}
394
Amy Griffisf368c07d2006-04-07 16:55:56 -0400395/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/* Compare a task_struct with an audit_rule. Return 1 on match, 0
397 * otherwise. */
398static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500399 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400401 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 enum audit_state *state)
403{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400404 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600405 u32 sid;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500408 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int result = 0;
410
Amy Griffis93315ed2006-02-07 12:05:27 -0500411 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500413 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
Al Viro3c662512006-05-06 08:26:27 -0400415 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400416 if (ctx) {
417 if (!ctx->ppid)
418 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400419 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400420 }
Al Viro3c662512006-05-06 08:26:27 -0400421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500423 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break;
425 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500426 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
428 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500429 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
431 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500432 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
434 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500435 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 break;
437 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500438 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
440 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500441 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
443 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500444 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500447 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 break;
2fd6f582005-04-29 16:08:28 +0100449 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700450 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500451 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 case AUDIT_EXIT:
455 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500456 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100459 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500460 if (f->val)
461 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100462 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500463 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400467 if (name)
468 result = audit_comparator(MAJOR(name->dev),
469 f->op, f->val);
470 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500472 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 ++result;
474 break;
475 }
476 }
477 }
478 break;
479 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400480 if (name)
481 result = audit_comparator(MINOR(name->dev),
482 f->op, f->val);
483 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500485 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 ++result;
487 break;
488 }
489 }
490 }
491 break;
492 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400493 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400494 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400495 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400497 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 ++result;
499 break;
500 }
501 }
502 }
503 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400504 case AUDIT_WATCH:
505 if (name && rule->watch->ino != (unsigned long)-1)
506 result = (name->dev == rule->watch->dev &&
Amy Griffis9c937dc2006-06-08 23:19:31 -0400507 name->ino == rule->watch->ino);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400508 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400509 case AUDIT_DIR:
510 if (ctx)
511 result = match_tree_refs(ctx, rule->tree);
512 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 case AUDIT_LOGINUID:
514 result = 0;
515 if (ctx)
Al Virobfef93a2008-01-10 04:53:18 -0500516 result = audit_comparator(tsk->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500518 case AUDIT_SUBJ_USER:
519 case AUDIT_SUBJ_ROLE:
520 case AUDIT_SUBJ_TYPE:
521 case AUDIT_SUBJ_SEN:
522 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600523 /* NOTE: this may return negative values indicating
524 a temporary error. We simply treat this as a
525 match for now to avoid losing information that
526 may be wanted. An error message will also be
527 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400528 if (f->se_rule) {
529 if (need_sid) {
Stephen Smalley62bac012006-09-25 23:31:56 -0700530 selinux_get_task_sid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400531 need_sid = 0;
532 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600533 result = selinux_audit_rule_match(sid, f->type,
534 f->op,
535 f->se_rule,
536 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400537 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600538 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500539 case AUDIT_OBJ_USER:
540 case AUDIT_OBJ_ROLE:
541 case AUDIT_OBJ_TYPE:
542 case AUDIT_OBJ_LEV_LOW:
543 case AUDIT_OBJ_LEV_HIGH:
544 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
545 also applies here */
546 if (f->se_rule) {
547 /* Find files that match */
548 if (name) {
549 result = selinux_audit_rule_match(
550 name->osid, f->type, f->op,
551 f->se_rule, ctx);
552 } else if (ctx) {
553 for (j = 0; j < ctx->name_count; j++) {
554 if (selinux_audit_rule_match(
555 ctx->names[j].osid,
556 f->type, f->op,
557 f->se_rule, ctx)) {
558 ++result;
559 break;
560 }
561 }
562 }
563 /* Find ipc objects that match */
564 if (ctx) {
565 struct audit_aux_data *aux;
566 for (aux = ctx->aux; aux;
567 aux = aux->next) {
568 if (aux->type == AUDIT_IPC) {
569 struct audit_aux_data_ipcctl *axi = (void *)aux;
570 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
571 ++result;
572 break;
573 }
574 }
575 }
576 }
577 }
578 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 case AUDIT_ARG0:
580 case AUDIT_ARG1:
581 case AUDIT_ARG2:
582 case AUDIT_ARG3:
583 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500584 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400586 case AUDIT_FILTERKEY:
587 /* ignore this field for filtering */
588 result = 1;
589 break;
Al Viro55669bf2006-08-31 19:26:40 -0400590 case AUDIT_PERM:
591 result = audit_match_perm(ctx, f->val);
592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!result)
596 return 0;
597 }
Amy Griffis5adc8a62006-06-14 18:45:21 -0400598 if (rule->filterkey)
599 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 switch (rule->action) {
601 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
603 }
604 return 1;
605}
606
607/* At process creation time, we can determine if system-call auditing is
608 * completely disabled for this task. Since we only have the task
609 * structure at this point, we can only check uid and gid.
610 */
611static enum audit_state audit_filter_task(struct task_struct *tsk)
612{
613 struct audit_entry *e;
614 enum audit_state state;
615
616 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100617 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400618 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 rcu_read_unlock();
620 return state;
621 }
622 }
623 rcu_read_unlock();
624 return AUDIT_BUILD_CONTEXT;
625}
626
627/* At syscall entry and exit time, this filter is called if the
628 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100629 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700630 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
632static enum audit_state audit_filter_syscall(struct task_struct *tsk,
633 struct audit_context *ctx,
634 struct list_head *list)
635{
636 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100637 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
David Woodhouse351bb722005-07-14 14:40:06 +0100639 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100640 return AUDIT_DISABLED;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100643 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000644 int word = AUDIT_WORD(ctx->major);
645 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100646
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000647 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400648 if ((e->rule.mask[word] & bit) == bit &&
649 audit_filter_rules(tsk, &e->rule, ctx, NULL,
650 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000651 rcu_read_unlock();
652 return state;
653 }
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 rcu_read_unlock();
657 return AUDIT_BUILD_CONTEXT;
658}
659
Amy Griffisf368c07d2006-04-07 16:55:56 -0400660/* At syscall exit time, this filter is called if any audit_names[] have been
661 * collected during syscall processing. We only check rules in sublists at hash
662 * buckets applicable to the inode numbers in audit_names[].
663 * Regarding audit_state, same rules apply as for audit_filter_syscall().
664 */
665enum audit_state audit_filter_inodes(struct task_struct *tsk,
666 struct audit_context *ctx)
667{
668 int i;
669 struct audit_entry *e;
670 enum audit_state state;
671
672 if (audit_pid && tsk->tgid == audit_pid)
673 return AUDIT_DISABLED;
674
675 rcu_read_lock();
676 for (i = 0; i < ctx->name_count; i++) {
677 int word = AUDIT_WORD(ctx->major);
678 int bit = AUDIT_BIT(ctx->major);
679 struct audit_names *n = &ctx->names[i];
680 int h = audit_hash_ino((u32)n->ino);
681 struct list_head *list = &audit_inode_hash[h];
682
683 if (list_empty(list))
684 continue;
685
686 list_for_each_entry_rcu(e, list, list) {
687 if ((e->rule.mask[word] & bit) == bit &&
688 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
689 rcu_read_unlock();
690 return state;
691 }
692 }
693 }
694 rcu_read_unlock();
695 return AUDIT_BUILD_CONTEXT;
696}
697
698void audit_set_auditable(struct audit_context *ctx)
699{
700 ctx->auditable = 1;
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static inline struct audit_context *audit_get_context(struct task_struct *tsk,
704 int return_valid,
705 int return_code)
706{
707 struct audit_context *context = tsk->audit_context;
708
709 if (likely(!context))
710 return NULL;
711 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500712
713 /*
714 * we need to fix up the return code in the audit logs if the actual
715 * return codes are later going to be fixed up by the arch specific
716 * signal handlers
717 *
718 * This is actually a test for:
719 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
720 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
721 *
722 * but is faster than a bunch of ||
723 */
724 if (unlikely(return_code <= -ERESTARTSYS) &&
725 (return_code >= -ERESTART_RESTARTBLOCK) &&
726 (return_code != -ENOIOCTLCMD))
727 context->return_code = -EINTR;
728 else
729 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Al Virod51374a2006-08-03 10:59:26 -0400731 if (context->in_syscall && !context->dummy && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400733
David Woodhouse0f45aa12005-06-19 19:35:50 +0100734 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400735 if (state == AUDIT_RECORD_CONTEXT) {
736 context->auditable = 1;
737 goto get_context;
738 }
739
740 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (state == AUDIT_RECORD_CONTEXT)
742 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Amy Griffisf368c07d2006-04-07 16:55:56 -0400746get_context:
Al Viro3f2792f2006-07-16 06:43:48 -0400747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 tsk->audit_context = NULL;
749 return context;
750}
751
752static inline void audit_free_names(struct audit_context *context)
753{
754 int i;
755
756#if AUDIT_DEBUG == 2
757 if (context->auditable
758 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000759 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 " name_count=%d put_count=%d"
761 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000762 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 context->serial, context->major, context->in_syscall,
764 context->name_count, context->put_count,
765 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000766 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 printk(KERN_ERR "names[%d] = %p = %s\n", i,
768 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000769 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 dump_stack();
772 return;
773 }
774#endif
775#if AUDIT_DEBUG
776 context->put_count = 0;
777 context->ino_count = 0;
778#endif
779
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000780 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400781 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100785 if (context->pwd)
786 dput(context->pwd);
787 if (context->pwdmnt)
788 mntput(context->pwdmnt);
789 context->pwd = NULL;
790 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793static inline void audit_free_aux(struct audit_context *context)
794{
795 struct audit_aux_data *aux;
796
797 while ((aux = context->aux)) {
798 context->aux = aux->next;
799 kfree(aux);
800 }
Amy Griffise54dc242007-03-29 18:01:04 -0400801 while ((aux = context->aux_pids)) {
802 context->aux_pids = aux->next;
803 kfree(aux);
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807static inline void audit_zero_context(struct audit_context *context,
808 enum audit_state state)
809{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 memset(context, 0, sizeof(*context));
811 context->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814static inline struct audit_context *audit_alloc_context(enum audit_state state)
815{
816 struct audit_context *context;
817
818 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
819 return NULL;
820 audit_zero_context(context, state);
821 return context;
822}
823
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700824/**
825 * audit_alloc - allocate an audit context block for a task
826 * @tsk: task
827 *
828 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * if necessary. Doing so turns on system call auditing for the
830 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700831 * needed.
832 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833int audit_alloc(struct task_struct *tsk)
834{
835 struct audit_context *context;
836 enum audit_state state;
837
838 if (likely(!audit_enabled))
839 return 0; /* Return if not auditing. */
840
841 state = audit_filter_task(tsk);
842 if (likely(state == AUDIT_DISABLED))
843 return 0;
844
845 if (!(context = audit_alloc_context(state))) {
846 audit_log_lost("out of memory in audit_alloc");
847 return -ENOMEM;
848 }
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 tsk->audit_context = context;
851 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
852 return 0;
853}
854
855static inline void audit_free_context(struct audit_context *context)
856{
857 struct audit_context *previous;
858 int count = 0;
859
860 do {
861 previous = context->previous;
862 if (previous || (count && count < 10)) {
863 ++count;
864 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
865 " freeing multiple contexts (%d)\n",
866 context->serial, context->major,
867 context->name_count, count);
868 }
869 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -0400870 unroll_tree_refs(context, NULL, 0);
871 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400873 kfree(context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 kfree(context);
875 context = previous;
876 } while (context);
877 if (count >= 10)
878 printk(KERN_ERR "audit: freed %d contexts\n", count);
879}
880
Joy Latten161a09e2006-11-27 13:11:54 -0600881void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000882{
883 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000884 unsigned len;
885 int error;
886 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000887
Al Viroc4823bc2007-03-12 16:17:42 +0000888 selinux_get_task_sid(current, &sid);
889 if (!sid)
890 return;
891
892 error = selinux_sid_to_string(sid, &ctx, &len);
893 if (error) {
894 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000895 goto error_path;
896 return;
897 }
898
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000899 audit_log_format(ab, " subj=%s", ctx);
Al Viroc4823bc2007-03-12 16:17:42 +0000900 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000901 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000902
903error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000904 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000905 return;
906}
907
Joy Latten161a09e2006-11-27 13:11:54 -0600908EXPORT_SYMBOL(audit_log_task_context);
909
Al Viroe4951492006-03-29 20:17:10 -0500910static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700911{
Al Viro45d9bb02006-03-29 20:02:55 -0500912 char name[sizeof(tsk->comm)];
913 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700914 struct vm_area_struct *vma;
915
Al Viroe4951492006-03-29 20:17:10 -0500916 /* tsk == current */
917
Al Viro45d9bb02006-03-29 20:02:55 -0500918 get_task_comm(name, tsk);
David Woodhouse99e45eea2005-05-23 21:57:41 +0100919 audit_log_format(ab, " comm=");
920 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700921
Al Viroe4951492006-03-29 20:17:10 -0500922 if (mm) {
923 down_read(&mm->mmap_sem);
924 vma = mm->mmap;
925 while (vma) {
926 if ((vma->vm_flags & VM_EXECUTABLE) &&
927 vma->vm_file) {
928 audit_log_d_path(ab, "exe=",
Josef Sipeka7a005f2006-12-08 02:37:17 -0800929 vma->vm_file->f_path.dentry,
930 vma->vm_file->f_path.mnt);
Al Viroe4951492006-03-29 20:17:10 -0500931 break;
932 }
933 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700934 }
Al Viroe4951492006-03-29 20:17:10 -0500935 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700936 }
Al Viroe4951492006-03-29 20:17:10 -0500937 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700938}
939
Amy Griffise54dc242007-03-29 18:01:04 -0400940static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric Paris4746ec52008-01-08 10:06:53 -0500941 uid_t auid, uid_t uid, unsigned int sessionid,
942 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400943{
944 struct audit_buffer *ab;
945 char *s = NULL;
946 u32 len;
947 int rc = 0;
948
949 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
950 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -0500951 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -0400952
Eric Paris4746ec52008-01-08 10:06:53 -0500953 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
954 uid, sessionid);
Amy Griffise54dc242007-03-29 18:01:04 -0400955 if (selinux_sid_to_string(sid, &s, &len)) {
Eric Parisc2a77802008-01-07 13:40:17 -0500956 audit_log_format(ab, " obj=(none)");
Amy Griffise54dc242007-03-29 18:01:04 -0400957 rc = 1;
958 } else
Eric Parisc2a77802008-01-07 13:40:17 -0500959 audit_log_format(ab, " obj=%s", s);
960 audit_log_format(ab, " ocomm=");
961 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -0400962 audit_log_end(ab);
963 kfree(s);
964
965 return rc;
966}
967
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700968static void audit_log_execve_info(struct audit_buffer *ab,
969 struct audit_aux_data_execve *axi)
970{
971 int i;
972 long len, ret;
Peter Zijlstra040b3a22007-07-28 00:55:18 +0200973 const char __user *p;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700974 char *buf;
975
976 if (axi->mm != current->mm)
977 return; /* execve failed, no additional info */
978
Peter Zijlstra040b3a22007-07-28 00:55:18 +0200979 p = (const char __user *)axi->mm->arg_start;
980
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700981 for (i = 0; i < axi->argc; i++, p += len) {
Ollie Wildb6a2fea2007-07-19 01:48:16 -0700982 len = strnlen_user(p, MAX_ARG_STRLEN);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700983 /*
984 * We just created this mm, if we can't find the strings
985 * we just copied into it something is _very_ wrong. Similar
986 * for strings that are too long, we should not have created
987 * any.
988 */
989 if (!len || len > MAX_ARG_STRLEN) {
990 WARN_ON(1);
991 send_sig(SIGKILL, current, 0);
992 }
993
994 buf = kmalloc(len, GFP_KERNEL);
995 if (!buf) {
996 audit_panic("out of memory for argv string\n");
997 break;
998 }
999
1000 ret = copy_from_user(buf, p, len);
1001 /*
1002 * There is no reason for this copy to be short. We just
1003 * copied them here, and the mm hasn't been exposed to user-
1004 * space yet.
1005 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001006 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001007 WARN_ON(1);
1008 send_sig(SIGKILL, current, 0);
1009 }
1010
1011 audit_log_format(ab, "a%d=", i);
1012 audit_log_untrustedstring(ab, buf);
1013 audit_log_format(ab, "\n");
1014
1015 kfree(buf);
1016 }
1017}
1018
Al Viroe4951492006-03-29 20:17:10 -05001019static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001021 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001023 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -05001024 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Al Viroe4951492006-03-29 20:17:10 -05001026 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001027 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -04001028 if (!context->ppid)
1029 context->ppid = sys_getppid();
Al Viro3f2792f2006-07-16 06:43:48 -04001030 context->uid = tsk->uid;
1031 context->gid = tsk->gid;
1032 context->euid = tsk->euid;
1033 context->suid = tsk->suid;
1034 context->fsuid = tsk->fsuid;
1035 context->egid = tsk->egid;
1036 context->sgid = tsk->sgid;
1037 context->fsgid = tsk->fsgid;
1038 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001039
1040 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!ab)
1042 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001043 audit_log_format(ab, "arch=%x syscall=%d",
1044 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (context->personality != PER_LINUX)
1046 audit_log_format(ab, " per=%lx", context->personality);
1047 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001048 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001049 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1050 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001051
1052 mutex_lock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001053 read_lock(&tasklist_lock);
Al Viro45d9bb02006-03-29 20:02:55 -05001054 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1055 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -05001056 else
1057 tty = "(none)";
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001058 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 audit_log_format(ab,
1060 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -04001061 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +01001062 " euid=%u suid=%u fsuid=%u"
Eric Paris4746ec52008-01-08 10:06:53 -05001063 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 context->argv[0],
1065 context->argv[1],
1066 context->argv[2],
1067 context->argv[3],
1068 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -04001069 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 context->pid,
Al Virobfef93a2008-01-10 04:53:18 -05001071 tsk->loginuid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 context->uid,
1073 context->gid,
1074 context->euid, context->suid, context->fsuid,
Eric Paris4746ec52008-01-08 10:06:53 -05001075 context->egid, context->sgid, context->fsgid, tty,
1076 tsk->sessionid);
Alan Coxeb84a202006-09-29 02:01:41 -07001077
1078 mutex_unlock(&tty_mutex);
1079
Al Viroe4951492006-03-29 20:17:10 -05001080 audit_log_task_info(ab, tsk);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001081 if (context->filterkey) {
1082 audit_log_format(ab, " key=");
1083 audit_log_untrustedstring(ab, context->filterkey);
1084 } else
1085 audit_log_format(ab, " key=(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
David Woodhouse7551ced2005-05-26 12:04:57 +01001088 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001089
Al Viroe4951492006-03-29 20:17:10 -05001090 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!ab)
1092 continue; /* audit_panic has been called */
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001095 case AUDIT_MQ_OPEN: {
1096 struct audit_aux_data_mq_open *axi = (void *)aux;
1097 audit_log_format(ab,
1098 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1099 "mq_msgsize=%ld mq_curmsgs=%ld",
1100 axi->oflag, axi->mode, axi->attr.mq_flags,
1101 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1102 axi->attr.mq_curmsgs);
1103 break; }
1104
1105 case AUDIT_MQ_SENDRECV: {
1106 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1107 audit_log_format(ab,
1108 "mqdes=%d msg_len=%zd msg_prio=%u "
1109 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1110 axi->mqdes, axi->msg_len, axi->msg_prio,
1111 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1112 break; }
1113
1114 case AUDIT_MQ_NOTIFY: {
1115 struct audit_aux_data_mq_notify *axi = (void *)aux;
1116 audit_log_format(ab,
1117 "mqdes=%d sigev_signo=%d",
1118 axi->mqdes,
1119 axi->notification.sigev_signo);
1120 break; }
1121
1122 case AUDIT_MQ_GETSETATTR: {
1123 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1124 audit_log_format(ab,
1125 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1126 "mq_curmsgs=%ld ",
1127 axi->mqdes,
1128 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1129 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1130 break; }
1131
Steve Grubbc0404992005-05-13 18:17:42 +01001132 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 struct audit_aux_data_ipcctl *axi = (void *)aux;
1134 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001135 "ouid=%u ogid=%u mode=%#o",
Linda Knippersac032212006-05-16 22:03:48 -04001136 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001137 if (axi->osid != 0) {
1138 char *ctx = NULL;
1139 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -07001140 if (selinux_sid_to_string(
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001141 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -05001142 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001143 axi->osid);
1144 call_panic = 1;
1145 } else
1146 audit_log_format(ab, " obj=%s", ctx);
1147 kfree(ctx);
1148 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001149 break; }
1150
Steve Grubb073115d2006-04-02 17:07:33 -04001151 case AUDIT_IPC_SET_PERM: {
1152 struct audit_aux_data_ipcctl *axi = (void *)aux;
1153 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001154 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
Steve Grubb073115d2006-04-02 17:07:33 -04001155 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -04001156 break; }
Linda Knippersac032212006-05-16 22:03:48 -04001157
Al Viro473ae302006-04-26 14:04:08 -04001158 case AUDIT_EXECVE: {
1159 struct audit_aux_data_execve *axi = (void *)aux;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001160 audit_log_execve_info(ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001161 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001162
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001163 case AUDIT_SOCKETCALL: {
1164 int i;
1165 struct audit_aux_data_socketcall *axs = (void *)aux;
1166 audit_log_format(ab, "nargs=%d", axs->nargs);
1167 for (i=0; i<axs->nargs; i++)
1168 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1169 break; }
1170
1171 case AUDIT_SOCKADDR: {
1172 struct audit_aux_data_sockaddr *axs = (void *)aux;
1173
1174 audit_log_format(ab, "saddr=");
1175 audit_log_hex(ab, axs->a, axs->len);
1176 break; }
Stephen Smalley01116102005-05-21 00:15:52 +01001177
Al Virodb349502007-02-07 01:48:00 -05001178 case AUDIT_FD_PAIR: {
1179 struct audit_aux_data_fd_pair *axs = (void *)aux;
1180 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1181 break; }
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
Amy Griffise54dc242007-03-29 18:01:04 -04001187 for (aux = context->aux_pids; aux; aux = aux->next) {
1188 struct audit_aux_data_pids *axs = (void *)aux;
1189 int i;
1190
1191 for (i = 0; i < axs->pid_count; i++)
1192 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001193 axs->target_auid[i],
1194 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001195 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001196 axs->target_sid[i],
1197 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001198 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001199 }
1200
Amy Griffise54dc242007-03-29 18:01:04 -04001201 if (context->target_pid &&
1202 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001203 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001204 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001205 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001206 call_panic = 1;
1207
David Woodhouse8f37d472005-05-27 12:17:28 +01001208 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -05001209 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001210 if (ab) {
1211 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1212 audit_log_end(ab);
1213 }
1214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -04001216 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +00001217
Al Viroe4951492006-03-29 20:17:10 -05001218 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (!ab)
1220 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +01001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +00001223
Amy Griffis9c937dc2006-06-08 23:19:31 -04001224 if (n->name) {
1225 switch(n->name_len) {
1226 case AUDIT_NAME_FULL:
1227 /* log the full path */
1228 audit_log_format(ab, " name=");
1229 audit_log_untrustedstring(ab, n->name);
1230 break;
1231 case 0:
1232 /* name was specified as a relative path and the
1233 * directory component is the cwd */
1234 audit_log_d_path(ab, " name=", context->pwd,
1235 context->pwdmnt);
1236 break;
1237 default:
1238 /* log the name's directory component */
1239 audit_log_format(ab, " name=");
1240 audit_log_n_untrustedstring(ab, n->name_len,
1241 n->name);
1242 }
1243 } else
1244 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001245
Amy Griffis9c937dc2006-06-08 23:19:31 -04001246 if (n->ino != (unsigned long)-1) {
1247 audit_log_format(ab, " inode=%lu"
1248 " dev=%02x:%02x mode=%#o"
1249 " ouid=%u ogid=%u rdev=%02x:%02x",
1250 n->ino,
1251 MAJOR(n->dev),
1252 MINOR(n->dev),
1253 n->mode,
1254 n->uid,
1255 n->gid,
1256 MAJOR(n->rdev),
1257 MINOR(n->rdev));
1258 }
1259 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001260 char *ctx = NULL;
1261 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -07001262 if (selinux_sid_to_string(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001263 n->osid, &ctx, &len)) {
1264 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001265 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -04001266 } else
1267 audit_log_format(ab, " obj=%s", ctx);
1268 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001269 }
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 audit_log_end(ab);
1272 }
Eric Parisc0641f22008-01-07 13:49:15 -05001273
1274 /* Send end of event record to help user space know we are finished */
1275 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1276 if (ab)
1277 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001278 if (call_panic)
1279 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001282/**
1283 * audit_free - free a per-task audit context
1284 * @tsk: task whose audit context block to free
1285 *
Al Virofa84cb92006-03-29 20:30:19 -05001286 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288void audit_free(struct task_struct *tsk)
1289{
1290 struct audit_context *context;
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (likely(!context))
1294 return;
1295
1296 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001297 * function (e.g., exit_group), then free context block.
1298 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001299 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001300 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +01001301 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001302 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 audit_free_context(context);
1305}
1306
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001307/**
1308 * audit_syscall_entry - fill in an audit record at syscall entry
1309 * @tsk: task being audited
1310 * @arch: architecture type
1311 * @major: major syscall type (function)
1312 * @a1: additional syscall register 1
1313 * @a2: additional syscall register 2
1314 * @a3: additional syscall register 3
1315 * @a4: additional syscall register 4
1316 *
1317 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * audit context was created when the task was created and the state or
1319 * filters demand the audit context be built. If the state from the
1320 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1321 * then the record will be written at syscall exit time (otherwise, it
1322 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001323 * be written).
1324 */
Al Viro5411be52006-03-29 20:23:36 -05001325void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 unsigned long a1, unsigned long a2,
1327 unsigned long a3, unsigned long a4)
1328{
Al Viro5411be52006-03-29 20:23:36 -05001329 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 struct audit_context *context = tsk->audit_context;
1331 enum audit_state state;
1332
1333 BUG_ON(!context);
1334
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001335 /*
1336 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * calls in kernel_thread via the entry.S interface, instead of
1338 * with direct calls. (If you are porting to a new
1339 * architecture, hitting this condition can indicate that you
1340 * got the _exit/_leave calls backward in entry.S.)
1341 *
1342 * i386 no
1343 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001344 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 *
1346 * This also happens with vm86 emulation in a non-nested manner
1347 * (entries without exits), so this case must be caught.
1348 */
1349 if (context->in_syscall) {
1350 struct audit_context *newctx;
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352#if AUDIT_DEBUG
1353 printk(KERN_ERR
1354 "audit(:%d) pid=%d in syscall=%d;"
1355 " entering syscall=%d\n",
1356 context->serial, tsk->pid, context->major, major);
1357#endif
1358 newctx = audit_alloc_context(context->state);
1359 if (newctx) {
1360 newctx->previous = context;
1361 context = newctx;
1362 tsk->audit_context = newctx;
1363 } else {
1364 /* If we can't alloc a new context, the best we
1365 * can do is to leak memory (any pending putname
1366 * will be lost). The only other alternative is
1367 * to abandon auditing. */
1368 audit_zero_context(context, context->state);
1369 }
1370 }
1371 BUG_ON(context->in_syscall || context->name_count);
1372
1373 if (!audit_enabled)
1374 return;
1375
2fd6f582005-04-29 16:08:28 +01001376 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 context->major = major;
1378 context->argv[0] = a1;
1379 context->argv[1] = a2;
1380 context->argv[2] = a3;
1381 context->argv[3] = a4;
1382
1383 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001384 context->dummy = !audit_n_rules;
1385 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
David Woodhouse0f45aa12005-06-19 19:35:50 +01001386 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (likely(state == AUDIT_DISABLED))
1388 return;
1389
David Woodhousece625a82005-07-18 14:24:46 -04001390 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 context->ctime = CURRENT_TIME;
1392 context->in_syscall = 1;
1393 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
Alexander Viro419c58f2006-09-29 00:08:50 -04001394 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001397/**
1398 * audit_syscall_exit - deallocate audit context after a system call
1399 * @tsk: task being audited
1400 * @valid: success/failure flag
1401 * @return_code: syscall return value
1402 *
1403 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1405 * filtering, or because some other part of the kernel write an audit
1406 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001407 * free the names stored from getname().
1408 */
Al Viro5411be52006-03-29 20:23:36 -05001409void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Al Viro5411be52006-03-29 20:23:36 -05001411 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct audit_context *context;
1413
2fd6f582005-04-29 16:08:28 +01001414 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001417 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
David Woodhousef7056d62005-06-20 16:07:33 +01001419 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001420 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 context->in_syscall = 0;
1423 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (context->previous) {
1426 struct audit_context *new_context = context->previous;
1427 context->previous = NULL;
1428 audit_free_context(context);
1429 tsk->audit_context = new_context;
1430 } else {
1431 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001432 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001434 context->aux = NULL;
1435 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001436 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001437 context->target_sid = 0;
Amy Griffis5adc8a62006-06-14 18:45:21 -04001438 kfree(context->filterkey);
1439 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 tsk->audit_context = context;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Al Viro74c3cbe2007-07-22 08:04:18 -04001444static inline void handle_one(const struct inode *inode)
1445{
1446#ifdef CONFIG_AUDIT_TREE
1447 struct audit_context *context;
1448 struct audit_tree_refs *p;
1449 struct audit_chunk *chunk;
1450 int count;
1451 if (likely(list_empty(&inode->inotify_watches)))
1452 return;
1453 context = current->audit_context;
1454 p = context->trees;
1455 count = context->tree_count;
1456 rcu_read_lock();
1457 chunk = audit_tree_lookup(inode);
1458 rcu_read_unlock();
1459 if (!chunk)
1460 return;
1461 if (likely(put_tree_ref(context, chunk)))
1462 return;
1463 if (unlikely(!grow_tree_refs(context))) {
1464 printk(KERN_WARNING "out of memory, audit has lost a tree reference");
1465 audit_set_auditable(context);
1466 audit_put_chunk(chunk);
1467 unroll_tree_refs(context, p, count);
1468 return;
1469 }
1470 put_tree_ref(context, chunk);
1471#endif
1472}
1473
1474static void handle_path(const struct dentry *dentry)
1475{
1476#ifdef CONFIG_AUDIT_TREE
1477 struct audit_context *context;
1478 struct audit_tree_refs *p;
1479 const struct dentry *d, *parent;
1480 struct audit_chunk *drop;
1481 unsigned long seq;
1482 int count;
1483
1484 context = current->audit_context;
1485 p = context->trees;
1486 count = context->tree_count;
1487retry:
1488 drop = NULL;
1489 d = dentry;
1490 rcu_read_lock();
1491 seq = read_seqbegin(&rename_lock);
1492 for(;;) {
1493 struct inode *inode = d->d_inode;
1494 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1495 struct audit_chunk *chunk;
1496 chunk = audit_tree_lookup(inode);
1497 if (chunk) {
1498 if (unlikely(!put_tree_ref(context, chunk))) {
1499 drop = chunk;
1500 break;
1501 }
1502 }
1503 }
1504 parent = d->d_parent;
1505 if (parent == d)
1506 break;
1507 d = parent;
1508 }
1509 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1510 rcu_read_unlock();
1511 if (!drop) {
1512 /* just a race with rename */
1513 unroll_tree_refs(context, p, count);
1514 goto retry;
1515 }
1516 audit_put_chunk(drop);
1517 if (grow_tree_refs(context)) {
1518 /* OK, got more space */
1519 unroll_tree_refs(context, p, count);
1520 goto retry;
1521 }
1522 /* too bad */
1523 printk(KERN_WARNING
1524 "out of memory, audit has lost a tree reference");
1525 unroll_tree_refs(context, p, count);
1526 audit_set_auditable(context);
1527 return;
1528 }
1529 rcu_read_unlock();
1530#endif
1531}
1532
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001533/**
1534 * audit_getname - add a name to the list
1535 * @name: name to add
1536 *
1537 * Add a name to the list of audit names for this context.
1538 * Called from fs/namei.c:getname().
1539 */
Al Virod8945bb52006-05-18 16:01:30 -04001540void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct audit_context *context = current->audit_context;
1543
Al Virod8945bb52006-05-18 16:01:30 -04001544 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return;
1546
1547 if (!context->in_syscall) {
1548#if AUDIT_DEBUG == 2
1549 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1550 __FILE__, __LINE__, context->serial, name);
1551 dump_stack();
1552#endif
1553 return;
1554 }
1555 BUG_ON(context->name_count >= AUDIT_NAMES);
1556 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001557 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1558 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 context->names[context->name_count].ino = (unsigned long)-1;
Amy Griffise41e8bd2007-02-13 14:14:09 -05001560 context->names[context->name_count].osid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001562 if (!context->pwd) {
1563 read_lock(&current->fs->lock);
1564 context->pwd = dget(current->fs->pwd);
1565 context->pwdmnt = mntget(current->fs->pwdmnt);
1566 read_unlock(&current->fs->lock);
1567 }
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001571/* audit_putname - intercept a putname request
1572 * @name: name to intercept and delay for putname
1573 *
1574 * If we have stored the name from getname in the audit context,
1575 * then we delay the putname until syscall exit.
1576 * Called from include/linux/fs.h:putname().
1577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578void audit_putname(const char *name)
1579{
1580 struct audit_context *context = current->audit_context;
1581
1582 BUG_ON(!context);
1583 if (!context->in_syscall) {
1584#if AUDIT_DEBUG == 2
1585 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1586 __FILE__, __LINE__, context->serial, name);
1587 if (context->name_count) {
1588 int i;
1589 for (i = 0; i < context->name_count; i++)
1590 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1591 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001592 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594#endif
1595 __putname(name);
1596 }
1597#if AUDIT_DEBUG
1598 else {
1599 ++context->put_count;
1600 if (context->put_count > context->name_count) {
1601 printk(KERN_ERR "%s:%d(:%d): major=%d"
1602 " in_syscall=%d putname(%p) name_count=%d"
1603 " put_count=%d\n",
1604 __FILE__, __LINE__,
1605 context->serial, context->major,
1606 context->in_syscall, name, context->name_count,
1607 context->put_count);
1608 dump_stack();
1609 }
1610 }
1611#endif
1612}
1613
Amy Griffis5712e882007-02-13 14:15:22 -05001614static int audit_inc_name_count(struct audit_context *context,
1615 const struct inode *inode)
1616{
1617 if (context->name_count >= AUDIT_NAMES) {
1618 if (inode)
1619 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1620 "dev=%02x:%02x, inode=%lu",
1621 MAJOR(inode->i_sb->s_dev),
1622 MINOR(inode->i_sb->s_dev),
1623 inode->i_ino);
1624
1625 else
1626 printk(KERN_DEBUG "name_count maxed, losing inode data");
1627 return 1;
1628 }
1629 context->name_count++;
1630#if AUDIT_DEBUG
1631 context->ino_count++;
1632#endif
1633 return 0;
1634}
1635
Amy Griffis3e2efce2006-07-13 13:16:02 -04001636/* Copy inode data into an audit_names. */
1637static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001638{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001639 name->ino = inode->i_ino;
1640 name->dev = inode->i_sb->s_dev;
1641 name->mode = inode->i_mode;
1642 name->uid = inode->i_uid;
1643 name->gid = inode->i_gid;
1644 name->rdev = inode->i_rdev;
1645 selinux_get_inode_sid(inode, &name->osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001646}
1647
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001648/**
1649 * audit_inode - store the inode and device from a lookup
1650 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001651 * @dentry: dentry being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001652 *
1653 * Called from fs/namei.c:path_lookup().
1654 */
Al Viro5a190ae2007-06-07 12:19:32 -04001655void __audit_inode(const char *name, const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 int idx;
1658 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04001659 const struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 if (!context->in_syscall)
1662 return;
1663 if (context->name_count
1664 && context->names[context->name_count-1].name
1665 && context->names[context->name_count-1].name == name)
1666 idx = context->name_count - 1;
1667 else if (context->name_count > 1
1668 && context->names[context->name_count-2].name
1669 && context->names[context->name_count-2].name == name)
1670 idx = context->name_count - 2;
1671 else {
1672 /* FIXME: how much do we care about inodes that have no
1673 * associated name? */
Amy Griffis5712e882007-02-13 14:15:22 -05001674 if (audit_inc_name_count(context, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001676 idx = context->name_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 context->names[idx].name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001679 handle_path(dentry);
Amy Griffis3e2efce2006-07-13 13:16:02 -04001680 audit_copy_inode(&context->names[idx], inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Amy Griffis73241cc2005-11-03 16:00:25 +00001683/**
1684 * audit_inode_child - collect inode info for created/removed objects
1685 * @dname: inode's dentry name
Randy Dunlap481968f2007-10-21 20:59:53 -07001686 * @dentry: dentry being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04001687 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00001688 *
1689 * For syscalls that create or remove filesystem objects, audit_inode
1690 * can only collect information for the filesystem object's parent.
1691 * This call updates the audit context with the child's information.
1692 * Syscalls that create a new filesystem object must be hooked after
1693 * the object is created. Syscalls that remove a filesystem object
1694 * must be hooked prior, in order to capture the target inode during
1695 * unsuccessful attempts.
1696 */
Al Viro5a190ae2007-06-07 12:19:32 -04001697void __audit_inode_child(const char *dname, const struct dentry *dentry,
Amy Griffis73d3ec52006-07-13 13:16:39 -04001698 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00001699{
1700 int idx;
1701 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05001702 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04001703 const struct inode *inode = dentry->d_inode;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001704 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00001705
1706 if (!context->in_syscall)
1707 return;
1708
Al Viro74c3cbe2007-07-22 08:04:18 -04001709 if (inode)
1710 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001711 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001712 if (!dname)
Amy Griffis5712e882007-02-13 14:15:22 -05001713 goto add_names;
Amy Griffis73241cc2005-11-03 16:00:25 +00001714
Amy Griffis5712e882007-02-13 14:15:22 -05001715 /* parent is more likely, look for it first */
1716 for (idx = 0; idx < context->name_count; idx++) {
1717 struct audit_names *n = &context->names[idx];
Amy Griffis73241cc2005-11-03 16:00:25 +00001718
Amy Griffis5712e882007-02-13 14:15:22 -05001719 if (!n->name)
1720 continue;
1721
1722 if (n->ino == parent->i_ino &&
1723 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1724 n->name_len = dirlen; /* update parent data in place */
1725 found_parent = n->name;
1726 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001727 }
Steve Grubbac9910c2006-09-28 14:31:32 -04001728 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001729
Amy Griffis5712e882007-02-13 14:15:22 -05001730 /* no matching parent, look for matching child */
1731 for (idx = 0; idx < context->name_count; idx++) {
1732 struct audit_names *n = &context->names[idx];
Amy Griffis73d3ec52006-07-13 13:16:39 -04001733
Amy Griffis5712e882007-02-13 14:15:22 -05001734 if (!n->name)
1735 continue;
1736
1737 /* strcmp() is the more likely scenario */
1738 if (!strcmp(dname, n->name) ||
1739 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1740 if (inode)
1741 audit_copy_inode(n, inode);
1742 else
1743 n->ino = (unsigned long)-1;
1744 found_child = n->name;
1745 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04001746 }
Amy Griffis5712e882007-02-13 14:15:22 -05001747 }
1748
1749add_names:
1750 if (!found_parent) {
1751 if (audit_inc_name_count(context, parent))
1752 return;
1753 idx = context->name_count - 1;
1754 context->names[idx].name = NULL;
Amy Griffis73d3ec52006-07-13 13:16:39 -04001755 audit_copy_inode(&context->names[idx], parent);
1756 }
Amy Griffis5712e882007-02-13 14:15:22 -05001757
1758 if (!found_child) {
1759 if (audit_inc_name_count(context, inode))
1760 return;
1761 idx = context->name_count - 1;
1762
1763 /* Re-use the name belonging to the slot for a matching parent
1764 * directory. All names for this context are relinquished in
1765 * audit_free_names() */
1766 if (found_parent) {
1767 context->names[idx].name = found_parent;
1768 context->names[idx].name_len = AUDIT_NAME_FULL;
1769 /* don't call __putname() */
1770 context->names[idx].name_put = 0;
1771 } else {
1772 context->names[idx].name = NULL;
1773 }
1774
1775 if (inode)
1776 audit_copy_inode(&context->names[idx], inode);
1777 else
1778 context->names[idx].ino = (unsigned long)-1;
1779 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001780}
Trond Myklebust50e437d2007-06-07 22:44:34 -04001781EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04001782
1783/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001784 * auditsc_get_stamp - get local copies of audit_context values
1785 * @ctx: audit_context for the task
1786 * @t: timespec to store time recorded in the audit_context
1787 * @serial: serial value that is recorded in the audit_context
1788 *
1789 * Also sets the context as auditable.
1790 */
David Woodhousebfb44962005-05-21 21:08:09 +01001791void auditsc_get_stamp(struct audit_context *ctx,
1792 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
David Woodhousece625a82005-07-18 14:24:46 -04001794 if (!ctx->serial)
1795 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001796 t->tv_sec = ctx->ctime.tv_sec;
1797 t->tv_nsec = ctx->ctime.tv_nsec;
1798 *serial = ctx->serial;
1799 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
Eric Paris4746ec52008-01-08 10:06:53 -05001802/* global counter which is incremented every time something logs in */
1803static atomic_t session_id = ATOMIC_INIT(0);
1804
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001805/**
1806 * audit_set_loginuid - set a task's audit_context loginuid
1807 * @task: task whose audit context is being modified
1808 * @loginuid: loginuid value
1809 *
1810 * Returns 0.
1811 *
1812 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1813 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001814int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Eric Paris4746ec52008-01-08 10:06:53 -05001816 unsigned int sessionid = atomic_inc_return(&session_id);
Steve Grubb41757102006-06-12 07:48:28 -04001817 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01001818
Al Virobfef93a2008-01-10 04:53:18 -05001819 if (context && context->in_syscall) {
1820 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04001821
Al Virobfef93a2008-01-10 04:53:18 -05001822 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1823 if (ab) {
1824 audit_log_format(ab, "login pid=%d uid=%u "
Eric Paris4746ec52008-01-08 10:06:53 -05001825 "old auid=%u new auid=%u"
1826 " old ses=%u new ses=%u",
Al Virobfef93a2008-01-10 04:53:18 -05001827 task->pid, task->uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001828 task->loginuid, loginuid,
1829 task->sessionid, sessionid);
Al Virobfef93a2008-01-10 04:53:18 -05001830 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01001831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
Eric Paris4746ec52008-01-08 10:06:53 -05001833 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05001834 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return 0;
1836}
1837
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001838/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05001839 * __audit_mq_open - record audit data for a POSIX MQ open
1840 * @oflag: open flag
1841 * @mode: mode bits
1842 * @u_attr: queue attributes
1843 *
1844 * Returns 0 for success or NULL context or < 0 on error.
1845 */
1846int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1847{
1848 struct audit_aux_data_mq_open *ax;
1849 struct audit_context *context = current->audit_context;
1850
1851 if (!audit_enabled)
1852 return 0;
1853
1854 if (likely(!context))
1855 return 0;
1856
1857 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1858 if (!ax)
1859 return -ENOMEM;
1860
1861 if (u_attr != NULL) {
1862 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1863 kfree(ax);
1864 return -EFAULT;
1865 }
1866 } else
1867 memset(&ax->attr, 0, sizeof(ax->attr));
1868
1869 ax->oflag = oflag;
1870 ax->mode = mode;
1871
1872 ax->d.type = AUDIT_MQ_OPEN;
1873 ax->d.next = context->aux;
1874 context->aux = (void *)ax;
1875 return 0;
1876}
1877
1878/**
1879 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1880 * @mqdes: MQ descriptor
1881 * @msg_len: Message length
1882 * @msg_prio: Message priority
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001883 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001884 *
1885 * Returns 0 for success or NULL context or < 0 on error.
1886 */
1887int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1888 const struct timespec __user *u_abs_timeout)
1889{
1890 struct audit_aux_data_mq_sendrecv *ax;
1891 struct audit_context *context = current->audit_context;
1892
1893 if (!audit_enabled)
1894 return 0;
1895
1896 if (likely(!context))
1897 return 0;
1898
1899 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1900 if (!ax)
1901 return -ENOMEM;
1902
1903 if (u_abs_timeout != NULL) {
1904 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1905 kfree(ax);
1906 return -EFAULT;
1907 }
1908 } else
1909 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1910
1911 ax->mqdes = mqdes;
1912 ax->msg_len = msg_len;
1913 ax->msg_prio = msg_prio;
1914
1915 ax->d.type = AUDIT_MQ_SENDRECV;
1916 ax->d.next = context->aux;
1917 context->aux = (void *)ax;
1918 return 0;
1919}
1920
1921/**
1922 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1923 * @mqdes: MQ descriptor
1924 * @msg_len: Message length
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001925 * @u_msg_prio: Message priority
1926 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001927 *
1928 * Returns 0 for success or NULL context or < 0 on error.
1929 */
1930int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1931 unsigned int __user *u_msg_prio,
1932 const struct timespec __user *u_abs_timeout)
1933{
1934 struct audit_aux_data_mq_sendrecv *ax;
1935 struct audit_context *context = current->audit_context;
1936
1937 if (!audit_enabled)
1938 return 0;
1939
1940 if (likely(!context))
1941 return 0;
1942
1943 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1944 if (!ax)
1945 return -ENOMEM;
1946
1947 if (u_msg_prio != NULL) {
1948 if (get_user(ax->msg_prio, u_msg_prio)) {
1949 kfree(ax);
1950 return -EFAULT;
1951 }
1952 } else
1953 ax->msg_prio = 0;
1954
1955 if (u_abs_timeout != NULL) {
1956 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1957 kfree(ax);
1958 return -EFAULT;
1959 }
1960 } else
1961 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1962
1963 ax->mqdes = mqdes;
1964 ax->msg_len = msg_len;
1965
1966 ax->d.type = AUDIT_MQ_SENDRECV;
1967 ax->d.next = context->aux;
1968 context->aux = (void *)ax;
1969 return 0;
1970}
1971
1972/**
1973 * __audit_mq_notify - record audit data for a POSIX MQ notify
1974 * @mqdes: MQ descriptor
1975 * @u_notification: Notification event
1976 *
1977 * Returns 0 for success or NULL context or < 0 on error.
1978 */
1979
1980int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1981{
1982 struct audit_aux_data_mq_notify *ax;
1983 struct audit_context *context = current->audit_context;
1984
1985 if (!audit_enabled)
1986 return 0;
1987
1988 if (likely(!context))
1989 return 0;
1990
1991 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1992 if (!ax)
1993 return -ENOMEM;
1994
1995 if (u_notification != NULL) {
1996 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1997 kfree(ax);
1998 return -EFAULT;
1999 }
2000 } else
2001 memset(&ax->notification, 0, sizeof(ax->notification));
2002
2003 ax->mqdes = mqdes;
2004
2005 ax->d.type = AUDIT_MQ_NOTIFY;
2006 ax->d.next = context->aux;
2007 context->aux = (void *)ax;
2008 return 0;
2009}
2010
2011/**
2012 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2013 * @mqdes: MQ descriptor
2014 * @mqstat: MQ flags
2015 *
2016 * Returns 0 for success or NULL context or < 0 on error.
2017 */
2018int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2019{
2020 struct audit_aux_data_mq_getsetattr *ax;
2021 struct audit_context *context = current->audit_context;
2022
2023 if (!audit_enabled)
2024 return 0;
2025
2026 if (likely(!context))
2027 return 0;
2028
2029 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2030 if (!ax)
2031 return -ENOMEM;
2032
2033 ax->mqdes = mqdes;
2034 ax->mqstat = *mqstat;
2035
2036 ax->d.type = AUDIT_MQ_GETSETATTR;
2037 ax->d.next = context->aux;
2038 context->aux = (void *)ax;
2039 return 0;
2040}
2041
2042/**
Steve Grubb073115d2006-04-02 17:07:33 -04002043 * audit_ipc_obj - record audit data for ipc object
2044 * @ipcp: ipc permissions
2045 *
2046 * Returns 0 for success or NULL context or < 0 on error.
2047 */
Al Virod8945bb52006-05-18 16:01:30 -04002048int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002049{
2050 struct audit_aux_data_ipcctl *ax;
2051 struct audit_context *context = current->audit_context;
2052
Steve Grubb073115d2006-04-02 17:07:33 -04002053 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2054 if (!ax)
2055 return -ENOMEM;
2056
2057 ax->uid = ipcp->uid;
2058 ax->gid = ipcp->gid;
2059 ax->mode = ipcp->mode;
2060 selinux_get_ipc_sid(ipcp, &ax->osid);
2061
2062 ax->d.type = AUDIT_IPC;
2063 ax->d.next = context->aux;
2064 context->aux = (void *)ax;
2065 return 0;
2066}
2067
2068/**
2069 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002070 * @qbytes: msgq bytes
2071 * @uid: msgq user id
2072 * @gid: msgq group id
2073 * @mode: msgq mode (permissions)
2074 *
2075 * Returns 0 for success or NULL context or < 0 on error.
2076 */
Al Virod8945bb52006-05-18 16:01:30 -04002077int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 struct audit_aux_data_ipcctl *ax;
2080 struct audit_context *context = current->audit_context;
2081
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002082 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (!ax)
2084 return -ENOMEM;
2085
2086 ax->qbytes = qbytes;
2087 ax->uid = uid;
2088 ax->gid = gid;
2089 ax->mode = mode;
2090
Steve Grubb073115d2006-04-02 17:07:33 -04002091 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 ax->d.next = context->aux;
2093 context->aux = (void *)ax;
2094 return 0;
2095}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002096
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002097int audit_argv_kb = 32;
2098
Al Viro473ae302006-04-26 14:04:08 -04002099int audit_bprm(struct linux_binprm *bprm)
2100{
2101 struct audit_aux_data_execve *ax;
2102 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002103
Al Viro5ac3a9c2006-07-16 06:38:45 -04002104 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04002105 return 0;
2106
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002107 /*
2108 * Even though the stack code doesn't limit the arg+env size any more,
2109 * the audit code requires that _all_ arguments be logged in a single
2110 * netlink skb. Hence cap it :-(
2111 */
2112 if (bprm->argv_len > (audit_argv_kb << 10))
2113 return -E2BIG;
2114
2115 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002116 if (!ax)
2117 return -ENOMEM;
2118
2119 ax->argc = bprm->argc;
2120 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002121 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002122 ax->d.type = AUDIT_EXECVE;
2123 ax->d.next = context->aux;
2124 context->aux = (void *)ax;
2125 return 0;
2126}
2127
2128
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002129/**
2130 * audit_socketcall - record audit data for sys_socketcall
2131 * @nargs: number of args
2132 * @args: args array
2133 *
2134 * Returns 0 for success or NULL context or < 0 on error.
2135 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002136int audit_socketcall(int nargs, unsigned long *args)
2137{
2138 struct audit_aux_data_socketcall *ax;
2139 struct audit_context *context = current->audit_context;
2140
Al Viro5ac3a9c2006-07-16 06:38:45 -04002141 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002142 return 0;
2143
2144 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2145 if (!ax)
2146 return -ENOMEM;
2147
2148 ax->nargs = nargs;
2149 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2150
2151 ax->d.type = AUDIT_SOCKETCALL;
2152 ax->d.next = context->aux;
2153 context->aux = (void *)ax;
2154 return 0;
2155}
2156
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002157/**
Al Virodb349502007-02-07 01:48:00 -05002158 * __audit_fd_pair - record audit data for pipe and socketpair
2159 * @fd1: the first file descriptor
2160 * @fd2: the second file descriptor
2161 *
2162 * Returns 0 for success or NULL context or < 0 on error.
2163 */
2164int __audit_fd_pair(int fd1, int fd2)
2165{
2166 struct audit_context *context = current->audit_context;
2167 struct audit_aux_data_fd_pair *ax;
2168
2169 if (likely(!context)) {
2170 return 0;
2171 }
2172
2173 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2174 if (!ax) {
2175 return -ENOMEM;
2176 }
2177
2178 ax->fd[0] = fd1;
2179 ax->fd[1] = fd2;
2180
2181 ax->d.type = AUDIT_FD_PAIR;
2182 ax->d.next = context->aux;
2183 context->aux = (void *)ax;
2184 return 0;
2185}
2186
2187/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002188 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2189 * @len: data length in user space
2190 * @a: data address in kernel space
2191 *
2192 * Returns 0 for success or NULL context or < 0 on error.
2193 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002194int audit_sockaddr(int len, void *a)
2195{
2196 struct audit_aux_data_sockaddr *ax;
2197 struct audit_context *context = current->audit_context;
2198
Al Viro5ac3a9c2006-07-16 06:38:45 -04002199 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002200 return 0;
2201
2202 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2203 if (!ax)
2204 return -ENOMEM;
2205
2206 ax->len = len;
2207 memcpy(ax->a, a, len);
2208
2209 ax->d.type = AUDIT_SOCKADDR;
2210 ax->d.next = context->aux;
2211 context->aux = (void *)ax;
2212 return 0;
2213}
2214
Al Viroa5cb0132007-03-20 13:58:35 -04002215void __audit_ptrace(struct task_struct *t)
2216{
2217 struct audit_context *context = current->audit_context;
2218
2219 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002220 context->target_auid = audit_get_loginuid(t);
2221 context->target_uid = t->uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002222 context->target_sessionid = audit_get_sessionid(t);
Al Viroa5cb0132007-03-20 13:58:35 -04002223 selinux_get_task_sid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002224 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002225}
2226
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002227/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002228 * audit_signal_info - record signal info for shutting down audit subsystem
2229 * @sig: signal value
2230 * @t: task being signaled
2231 *
2232 * If the audit subsystem is being terminated, record the task (pid)
2233 * and uid that is doing that.
2234 */
Amy Griffise54dc242007-03-29 18:01:04 -04002235int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002236{
Amy Griffise54dc242007-03-29 18:01:04 -04002237 struct audit_aux_data_pids *axp;
2238 struct task_struct *tsk = current;
2239 struct audit_context *ctx = tsk->audit_context;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002240 extern pid_t audit_sig_pid;
2241 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04002242 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002243
Al Viro175fc482007-08-08 00:01:46 +01002244 if (audit_pid && t->tgid == audit_pid) {
2245 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
2246 audit_sig_pid = tsk->pid;
Al Virobfef93a2008-01-10 04:53:18 -05002247 if (tsk->loginuid != -1)
2248 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002249 else
2250 audit_sig_uid = tsk->uid;
2251 selinux_get_task_sid(tsk, &audit_sig_sid);
2252 }
2253 if (!audit_signals || audit_dummy_context())
2254 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002255 }
Amy Griffise54dc242007-03-29 18:01:04 -04002256
Amy Griffise54dc242007-03-29 18:01:04 -04002257 /* optimize the common case by putting first signal recipient directly
2258 * in audit_context */
2259 if (!ctx->target_pid) {
2260 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002261 ctx->target_auid = audit_get_loginuid(t);
2262 ctx->target_uid = t->uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002263 ctx->target_sessionid = audit_get_sessionid(t);
Amy Griffise54dc242007-03-29 18:01:04 -04002264 selinux_get_task_sid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002265 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002266 return 0;
2267 }
2268
2269 axp = (void *)ctx->aux_pids;
2270 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2271 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2272 if (!axp)
2273 return -ENOMEM;
2274
2275 axp->d.type = AUDIT_OBJ_PID;
2276 axp->d.next = ctx->aux_pids;
2277 ctx->aux_pids = (void *)axp;
2278 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002279 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002280
2281 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002282 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2283 axp->target_uid[axp->pid_count] = t->uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002284 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Amy Griffise54dc242007-03-29 18:01:04 -04002285 selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002286 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002287 axp->pid_count++;
2288
2289 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002290}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002291
2292/**
2293 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002294 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002295 *
2296 * If a process ends with a core dump, something fishy is going on and we
2297 * should record the event for investigation.
2298 */
2299void audit_core_dumps(long signr)
2300{
2301 struct audit_buffer *ab;
2302 u32 sid;
Eric Paris4746ec52008-01-08 10:06:53 -05002303 uid_t auid = audit_get_loginuid(current);
2304 unsigned int sessionid = audit_get_sessionid(current);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002305
2306 if (!audit_enabled)
2307 return;
2308
2309 if (signr == SIGQUIT) /* don't care for those */
2310 return;
2311
2312 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
Eric Paris4746ec52008-01-08 10:06:53 -05002313 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2314 auid, current->uid, current->gid, sessionid);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002315 selinux_get_task_sid(current, &sid);
2316 if (sid) {
2317 char *ctx = NULL;
2318 u32 len;
2319
2320 if (selinux_sid_to_string(sid, &ctx, &len))
2321 audit_log_format(ab, " ssid=%u", sid);
2322 else
2323 audit_log_format(ab, " subj=%s", ctx);
2324 kfree(ctx);
2325 }
2326 audit_log_format(ab, " pid=%d comm=", current->pid);
2327 audit_log_untrustedstring(ab, current->comm);
2328 audit_log_format(ab, " sig=%ld", signr);
2329 audit_log_end(ab);
2330}