blob: 2febf5165fade49ef54981042c9bffd73961b096 [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>
Al Viro473ae302006-04-26 14:04:08 -040064#include <linux/binfmts.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040065#include <linux/highmem.h>
Al Virof46038f2006-05-06 08:22:52 -040066#include <linux/syscalls.h>
Al Viro74c3cbe2007-07-22 08:04:18 -040067#include <linux/inotify.h>
Eric Paris851f7ff2008-11-11 21:48:14 +110068#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
David Woodhousefe7752b2005-12-15 18:33:52 +000070#include "audit.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* AUDIT_NAMES is the number of slots we reserve in the audit_context
73 * for saving names from getname(). */
74#define AUDIT_NAMES 20
75
Amy Griffis9c937dc2006-06-08 23:19:31 -040076/* Indicates that audit should log the full pathname. */
77#define AUDIT_NAME_FULL -1
78
Eric Parisde6bbd12008-01-07 14:31:58 -050079/* no execve audit message should be longer than this (userspace limits) */
80#define MAX_EXECVE_AUDIT_LEN 7500
81
Al Viro471a5c72006-07-10 08:29:24 -040082/* number of audit rules */
83int audit_n_rules;
84
Amy Griffise54dc242007-03-29 18:01:04 -040085/* determines whether we collect data for signals sent */
86int audit_signals;
87
Eric Paris851f7ff2008-11-11 21:48:14 +110088struct audit_cap_data {
89 kernel_cap_t permitted;
90 kernel_cap_t inheritable;
91 union {
92 unsigned int fE; /* effective bit of a file capability */
93 kernel_cap_t effective; /* effective set of a process */
94 };
95};
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* When fs/namei.c:getname() is called, we store the pointer in name and
98 * we don't let putname() free it (instead we free all of the saved
99 * pointers at syscall exit time).
100 *
101 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
102struct audit_names {
103 const char *name;
Amy Griffis9c937dc2006-06-08 23:19:31 -0400104 int name_len; /* number of name's characters to log */
105 unsigned name_put; /* call __putname() for this name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 unsigned long ino;
107 dev_t dev;
108 umode_t mode;
109 uid_t uid;
110 gid_t gid;
111 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400112 u32 osid;
Eric Paris851f7ff2008-11-11 21:48:14 +1100113 struct audit_cap_data fcap;
114 unsigned int fcap_ver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117struct audit_aux_data {
118 struct audit_aux_data *next;
119 int type;
120};
121
122#define AUDIT_AUX_IPCPERM 0
123
Amy Griffise54dc242007-03-29 18:01:04 -0400124/* Number of target pids per aux struct. */
125#define AUDIT_AUX_PIDS 16
126
George C. Wilson20ca73b2006-05-24 16:09:55 -0500127struct audit_aux_data_mq_open {
128 struct audit_aux_data d;
129 int oflag;
130 mode_t mode;
131 struct mq_attr attr;
132};
133
134struct audit_aux_data_mq_sendrecv {
135 struct audit_aux_data d;
136 mqd_t mqdes;
137 size_t msg_len;
138 unsigned int msg_prio;
139 struct timespec abs_timeout;
140};
141
142struct audit_aux_data_mq_notify {
143 struct audit_aux_data d;
144 mqd_t mqdes;
145 struct sigevent notification;
146};
147
148struct audit_aux_data_mq_getsetattr {
149 struct audit_aux_data d;
150 mqd_t mqdes;
151 struct mq_attr mqstat;
152};
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154struct audit_aux_data_ipcctl {
155 struct audit_aux_data d;
156 struct ipc_perm p;
157 unsigned long qbytes;
158 uid_t uid;
159 gid_t gid;
160 mode_t mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500161 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
163
Al Viro473ae302006-04-26 14:04:08 -0400164struct audit_aux_data_execve {
165 struct audit_aux_data d;
166 int argc;
167 int envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -0700168 struct mm_struct *mm;
Al Viro473ae302006-04-26 14:04:08 -0400169};
170
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100171struct audit_aux_data_socketcall {
172 struct audit_aux_data d;
173 int nargs;
174 unsigned long args[0];
175};
176
177struct audit_aux_data_sockaddr {
178 struct audit_aux_data d;
179 int len;
180 char a[0];
181};
182
Al Virodb349502007-02-07 01:48:00 -0500183struct audit_aux_data_fd_pair {
184 struct audit_aux_data d;
185 int fd[2];
186};
187
Amy Griffise54dc242007-03-29 18:01:04 -0400188struct audit_aux_data_pids {
189 struct audit_aux_data d;
190 pid_t target_pid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500191 uid_t target_auid[AUDIT_AUX_PIDS];
192 uid_t target_uid[AUDIT_AUX_PIDS];
Eric Paris4746ec52008-01-08 10:06:53 -0500193 unsigned int target_sessionid[AUDIT_AUX_PIDS];
Amy Griffise54dc242007-03-29 18:01:04 -0400194 u32 target_sid[AUDIT_AUX_PIDS];
Eric Parisc2a77802008-01-07 13:40:17 -0500195 char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
Amy Griffise54dc242007-03-29 18:01:04 -0400196 int pid_count;
197};
198
Eric Paris3fc689e2008-11-11 21:48:18 +1100199struct audit_aux_data_bprm_fcaps {
200 struct audit_aux_data d;
201 struct audit_cap_data fcap;
202 unsigned int fcap_ver;
203 struct audit_cap_data old_pcap;
204 struct audit_cap_data new_pcap;
205};
206
Eric Parise68b75a02008-11-11 21:48:22 +1100207struct audit_aux_data_capset {
208 struct audit_aux_data d;
209 pid_t pid;
210 struct audit_cap_data cap;
211};
212
Al Viro74c3cbe2007-07-22 08:04:18 -0400213struct audit_tree_refs {
214 struct audit_tree_refs *next;
215 struct audit_chunk *c[31];
216};
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/* The per-task audit context. */
219struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400220 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int in_syscall; /* 1 if task is in a syscall */
222 enum audit_state state;
223 unsigned int serial; /* serial number for record */
224 struct timespec ctime; /* time of syscall entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int major; /* syscall number */
226 unsigned long argv[4]; /* syscall arguments */
227 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100228 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int auditable; /* 1 if record should be written */
230 int name_count;
231 struct audit_names names[AUDIT_NAMES];
Amy Griffis5adc8a62006-06-14 18:45:21 -0400232 char * filterkey; /* key for rule that triggered record */
Jan Blunck44707fd2008-02-14 19:38:33 -0800233 struct path pwd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 struct audit_context *previous; /* For nested syscalls */
235 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400236 struct audit_aux_data *aux_pids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400239 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 uid_t uid, euid, suid, fsuid;
241 gid_t gid, egid, sgid, fsgid;
242 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100243 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Al Viroa5cb0132007-03-20 13:58:35 -0400245 pid_t target_pid;
Eric Parisc2a77802008-01-07 13:40:17 -0500246 uid_t target_auid;
247 uid_t target_uid;
Eric Paris4746ec52008-01-08 10:06:53 -0500248 unsigned int target_sessionid;
Al Viroa5cb0132007-03-20 13:58:35 -0400249 u32 target_sid;
Eric Parisc2a77802008-01-07 13:40:17 -0500250 char target_comm[TASK_COMM_LEN];
Al Viroa5cb0132007-03-20 13:58:35 -0400251
Al Viro74c3cbe2007-07-22 08:04:18 -0400252 struct audit_tree_refs *trees, *first_trees;
253 int tree_count;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#if AUDIT_DEBUG
256 int put_count;
257 int ino_count;
258#endif
259};
260
Al Viro55669bf2006-08-31 19:26:40 -0400261#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
262static inline int open_arg(int flags, int mask)
263{
264 int n = ACC_MODE(flags);
265 if (flags & (O_TRUNC | O_CREAT))
266 n |= AUDIT_PERM_WRITE;
267 return n & mask;
268}
269
270static int audit_match_perm(struct audit_context *ctx, int mask)
271{
Cordeliac4bacef2008-08-18 09:45:51 -0700272 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800273 if (unlikely(!ctx))
274 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700275 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100276
Al Viro55669bf2006-08-31 19:26:40 -0400277 switch (audit_classify_syscall(ctx->arch, n)) {
278 case 0: /* native */
279 if ((mask & AUDIT_PERM_WRITE) &&
280 audit_match_class(AUDIT_CLASS_WRITE, n))
281 return 1;
282 if ((mask & AUDIT_PERM_READ) &&
283 audit_match_class(AUDIT_CLASS_READ, n))
284 return 1;
285 if ((mask & AUDIT_PERM_ATTR) &&
286 audit_match_class(AUDIT_CLASS_CHATTR, n))
287 return 1;
288 return 0;
289 case 1: /* 32bit on biarch */
290 if ((mask & AUDIT_PERM_WRITE) &&
291 audit_match_class(AUDIT_CLASS_WRITE_32, n))
292 return 1;
293 if ((mask & AUDIT_PERM_READ) &&
294 audit_match_class(AUDIT_CLASS_READ_32, n))
295 return 1;
296 if ((mask & AUDIT_PERM_ATTR) &&
297 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
298 return 1;
299 return 0;
300 case 2: /* open */
301 return mask & ACC_MODE(ctx->argv[1]);
302 case 3: /* openat */
303 return mask & ACC_MODE(ctx->argv[2]);
304 case 4: /* socketcall */
305 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
306 case 5: /* execve */
307 return mask & AUDIT_PERM_EXEC;
308 default:
309 return 0;
310 }
311}
312
Al Viro8b67dca2008-04-28 04:15:49 -0400313static int audit_match_filetype(struct audit_context *ctx, int which)
314{
315 unsigned index = which & ~S_IFMT;
316 mode_t mode = which & S_IFMT;
zhangxiliang1a61c882008-08-02 10:56:37 +0800317
318 if (unlikely(!ctx))
319 return 0;
320
Al Viro8b67dca2008-04-28 04:15:49 -0400321 if (index >= ctx->name_count)
322 return 0;
323 if (ctx->names[index].ino == -1)
324 return 0;
325 if ((ctx->names[index].mode ^ mode) & S_IFMT)
326 return 0;
327 return 1;
328}
329
Al Viro74c3cbe2007-07-22 08:04:18 -0400330/*
331 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
332 * ->first_trees points to its beginning, ->trees - to the current end of data.
333 * ->tree_count is the number of free entries in array pointed to by ->trees.
334 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
335 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
336 * it's going to remain 1-element for almost any setup) until we free context itself.
337 * References in it _are_ dropped - at the same time we free/drop aux stuff.
338 */
339
340#ifdef CONFIG_AUDIT_TREE
341static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
342{
343 struct audit_tree_refs *p = ctx->trees;
344 int left = ctx->tree_count;
345 if (likely(left)) {
346 p->c[--left] = chunk;
347 ctx->tree_count = left;
348 return 1;
349 }
350 if (!p)
351 return 0;
352 p = p->next;
353 if (p) {
354 p->c[30] = chunk;
355 ctx->trees = p;
356 ctx->tree_count = 30;
357 return 1;
358 }
359 return 0;
360}
361
362static int grow_tree_refs(struct audit_context *ctx)
363{
364 struct audit_tree_refs *p = ctx->trees;
365 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
366 if (!ctx->trees) {
367 ctx->trees = p;
368 return 0;
369 }
370 if (p)
371 p->next = ctx->trees;
372 else
373 ctx->first_trees = ctx->trees;
374 ctx->tree_count = 31;
375 return 1;
376}
377#endif
378
379static void unroll_tree_refs(struct audit_context *ctx,
380 struct audit_tree_refs *p, int count)
381{
382#ifdef CONFIG_AUDIT_TREE
383 struct audit_tree_refs *q;
384 int n;
385 if (!p) {
386 /* we started with empty chain */
387 p = ctx->first_trees;
388 count = 31;
389 /* if the very first allocation has failed, nothing to do */
390 if (!p)
391 return;
392 }
393 n = count;
394 for (q = p; q != ctx->trees; q = q->next, n = 31) {
395 while (n--) {
396 audit_put_chunk(q->c[n]);
397 q->c[n] = NULL;
398 }
399 }
400 while (n-- > ctx->tree_count) {
401 audit_put_chunk(q->c[n]);
402 q->c[n] = NULL;
403 }
404 ctx->trees = p;
405 ctx->tree_count = count;
406#endif
407}
408
409static void free_tree_refs(struct audit_context *ctx)
410{
411 struct audit_tree_refs *p, *q;
412 for (p = ctx->first_trees; p; p = q) {
413 q = p->next;
414 kfree(p);
415 }
416}
417
418static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
419{
420#ifdef CONFIG_AUDIT_TREE
421 struct audit_tree_refs *p;
422 int n;
423 if (!tree)
424 return 0;
425 /* full ones */
426 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
427 for (n = 0; n < 31; n++)
428 if (audit_tree_match(p->c[n], tree))
429 return 1;
430 }
431 /* partial */
432 if (p) {
433 for (n = ctx->tree_count; n < 31; n++)
434 if (audit_tree_match(p->c[n], tree))
435 return 1;
436 }
437#endif
438 return 0;
439}
440
Amy Griffisf368c07d2006-04-07 16:55:56 -0400441/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/* Compare a task_struct with an audit_rule. Return 1 on match, 0
443 * otherwise. */
444static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500445 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400447 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 enum audit_state *state)
449{
David Howellsb6dff3e2008-11-14 10:39:16 +1100450 struct cred *cred = tsk->cred;
Steve Grubb2ad312d2006-04-11 08:50:56 -0400451 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600452 u32 sid;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500455 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int result = 0;
457
Amy Griffis93315ed2006-02-07 12:05:27 -0500458 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500460 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
Al Viro3c662512006-05-06 08:26:27 -0400462 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400463 if (ctx) {
464 if (!ctx->ppid)
465 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400466 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400467 }
Al Viro3c662512006-05-06 08:26:27 -0400468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case AUDIT_UID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100470 result = audit_comparator(cred->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 case AUDIT_EUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100473 result = audit_comparator(cred->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case AUDIT_SUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100476 result = audit_comparator(cred->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
478 case AUDIT_FSUID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100479 result = audit_comparator(cred->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 case AUDIT_GID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100482 result = audit_comparator(cred->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 case AUDIT_EGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100485 result = audit_comparator(cred->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 case AUDIT_SGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100488 result = audit_comparator(cred->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 case AUDIT_FSGID:
David Howellsb6dff3e2008-11-14 10:39:16 +1100491 result = audit_comparator(cred->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500494 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
2fd6f582005-04-29 16:08:28 +0100496 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700497 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500498 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100499 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 case AUDIT_EXIT:
502 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500503 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
505 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100506 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500507 if (f->val)
508 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100509 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500510 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 break;
513 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400514 if (name)
515 result = audit_comparator(MAJOR(name->dev),
516 f->op, f->val);
517 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500519 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ++result;
521 break;
522 }
523 }
524 }
525 break;
526 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400527 if (name)
528 result = audit_comparator(MINOR(name->dev),
529 f->op, f->val);
530 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500532 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 ++result;
534 break;
535 }
536 }
537 }
538 break;
539 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400540 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400541 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400542 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400544 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 ++result;
546 break;
547 }
548 }
549 }
550 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400551 case AUDIT_WATCH:
552 if (name && rule->watch->ino != (unsigned long)-1)
553 result = (name->dev == rule->watch->dev &&
Amy Griffis9c937dc2006-06-08 23:19:31 -0400554 name->ino == rule->watch->ino);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400555 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400556 case AUDIT_DIR:
557 if (ctx)
558 result = match_tree_refs(ctx, rule->tree);
559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 case AUDIT_LOGINUID:
561 result = 0;
562 if (ctx)
Al Virobfef93a2008-01-10 04:53:18 -0500563 result = audit_comparator(tsk->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500565 case AUDIT_SUBJ_USER:
566 case AUDIT_SUBJ_ROLE:
567 case AUDIT_SUBJ_TYPE:
568 case AUDIT_SUBJ_SEN:
569 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600570 /* NOTE: this may return negative values indicating
571 a temporary error. We simply treat this as a
572 match for now to avoid losing information that
573 may be wanted. An error message will also be
574 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000575 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400576 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200577 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400578 need_sid = 0;
579 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200580 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600581 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000582 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600583 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400584 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600585 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500586 case AUDIT_OBJ_USER:
587 case AUDIT_OBJ_ROLE:
588 case AUDIT_OBJ_TYPE:
589 case AUDIT_OBJ_LEV_LOW:
590 case AUDIT_OBJ_LEV_HIGH:
591 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
592 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000593 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500594 /* Find files that match */
595 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200596 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500597 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000598 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500599 } else if (ctx) {
600 for (j = 0; j < ctx->name_count; j++) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200601 if (security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500602 ctx->names[j].osid,
603 f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000604 f->lsm_rule, ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500605 ++result;
606 break;
607 }
608 }
609 }
610 /* Find ipc objects that match */
611 if (ctx) {
612 struct audit_aux_data *aux;
613 for (aux = ctx->aux; aux;
614 aux = aux->next) {
615 if (aux->type == AUDIT_IPC) {
616 struct audit_aux_data_ipcctl *axi = (void *)aux;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000617 if (security_audit_rule_match(axi->osid, f->type, f->op, f->lsm_rule, ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500618 ++result;
619 break;
620 }
621 }
622 }
623 }
624 }
625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 case AUDIT_ARG0:
627 case AUDIT_ARG1:
628 case AUDIT_ARG2:
629 case AUDIT_ARG3:
630 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500631 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400633 case AUDIT_FILTERKEY:
634 /* ignore this field for filtering */
635 result = 1;
636 break;
Al Viro55669bf2006-08-31 19:26:40 -0400637 case AUDIT_PERM:
638 result = audit_match_perm(ctx, f->val);
639 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400640 case AUDIT_FILETYPE:
641 result = audit_match_filetype(ctx, f->val);
642 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!result)
646 return 0;
647 }
zhangxiliang980dfb02008-08-01 19:15:47 +0800648 if (rule->filterkey && ctx)
Amy Griffis5adc8a62006-06-14 18:45:21 -0400649 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 switch (rule->action) {
651 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
653 }
654 return 1;
655}
656
657/* At process creation time, we can determine if system-call auditing is
658 * completely disabled for this task. Since we only have the task
659 * structure at this point, we can only check uid and gid.
660 */
661static enum audit_state audit_filter_task(struct task_struct *tsk)
662{
663 struct audit_entry *e;
664 enum audit_state state;
665
666 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100667 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400668 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 rcu_read_unlock();
670 return state;
671 }
672 }
673 rcu_read_unlock();
674 return AUDIT_BUILD_CONTEXT;
675}
676
677/* At syscall entry and exit time, this filter is called if the
678 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100679 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700680 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
682static enum audit_state audit_filter_syscall(struct task_struct *tsk,
683 struct audit_context *ctx,
684 struct list_head *list)
685{
686 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100687 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
David Woodhouse351bb722005-07-14 14:40:06 +0100689 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100690 return AUDIT_DISABLED;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100693 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000694 int word = AUDIT_WORD(ctx->major);
695 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100696
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000697 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400698 if ((e->rule.mask[word] & bit) == bit &&
699 audit_filter_rules(tsk, &e->rule, ctx, NULL,
700 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000701 rcu_read_unlock();
702 return state;
703 }
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 rcu_read_unlock();
707 return AUDIT_BUILD_CONTEXT;
708}
709
Amy Griffisf368c07d2006-04-07 16:55:56 -0400710/* At syscall exit time, this filter is called if any audit_names[] have been
711 * collected during syscall processing. We only check rules in sublists at hash
712 * buckets applicable to the inode numbers in audit_names[].
713 * Regarding audit_state, same rules apply as for audit_filter_syscall().
714 */
715enum audit_state audit_filter_inodes(struct task_struct *tsk,
716 struct audit_context *ctx)
717{
718 int i;
719 struct audit_entry *e;
720 enum audit_state state;
721
722 if (audit_pid && tsk->tgid == audit_pid)
723 return AUDIT_DISABLED;
724
725 rcu_read_lock();
726 for (i = 0; i < ctx->name_count; i++) {
727 int word = AUDIT_WORD(ctx->major);
728 int bit = AUDIT_BIT(ctx->major);
729 struct audit_names *n = &ctx->names[i];
730 int h = audit_hash_ino((u32)n->ino);
731 struct list_head *list = &audit_inode_hash[h];
732
733 if (list_empty(list))
734 continue;
735
736 list_for_each_entry_rcu(e, list, list) {
737 if ((e->rule.mask[word] & bit) == bit &&
738 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
739 rcu_read_unlock();
740 return state;
741 }
742 }
743 }
744 rcu_read_unlock();
745 return AUDIT_BUILD_CONTEXT;
746}
747
748void audit_set_auditable(struct audit_context *ctx)
749{
750 ctx->auditable = 1;
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753static inline struct audit_context *audit_get_context(struct task_struct *tsk,
754 int return_valid,
755 int return_code)
756{
757 struct audit_context *context = tsk->audit_context;
758
759 if (likely(!context))
760 return NULL;
761 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500762
763 /*
764 * we need to fix up the return code in the audit logs if the actual
765 * return codes are later going to be fixed up by the arch specific
766 * signal handlers
767 *
768 * This is actually a test for:
769 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
770 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
771 *
772 * but is faster than a bunch of ||
773 */
774 if (unlikely(return_code <= -ERESTARTSYS) &&
775 (return_code >= -ERESTART_RESTARTBLOCK) &&
776 (return_code != -ENOIOCTLCMD))
777 context->return_code = -EINTR;
778 else
779 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Al Virod51374a2006-08-03 10:59:26 -0400781 if (context->in_syscall && !context->dummy && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400783
David Woodhouse0f45aa12005-06-19 19:35:50 +0100784 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400785 if (state == AUDIT_RECORD_CONTEXT) {
786 context->auditable = 1;
787 goto get_context;
788 }
789
790 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (state == AUDIT_RECORD_CONTEXT)
792 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Amy Griffisf368c07d2006-04-07 16:55:56 -0400796get_context:
Al Viro3f2792f2006-07-16 06:43:48 -0400797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 tsk->audit_context = NULL;
799 return context;
800}
801
802static inline void audit_free_names(struct audit_context *context)
803{
804 int i;
805
806#if AUDIT_DEBUG == 2
807 if (context->auditable
808 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000809 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 " name_count=%d put_count=%d"
811 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000812 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 context->serial, context->major, context->in_syscall,
814 context->name_count, context->put_count,
815 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000816 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 printk(KERN_ERR "names[%d] = %p = %s\n", i,
818 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000819 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 dump_stack();
822 return;
823 }
824#endif
825#if AUDIT_DEBUG
826 context->put_count = 0;
827 context->ino_count = 0;
828#endif
829
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000830 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400831 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -0800835 path_put(&context->pwd);
836 context->pwd.dentry = NULL;
837 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840static inline void audit_free_aux(struct audit_context *context)
841{
842 struct audit_aux_data *aux;
843
844 while ((aux = context->aux)) {
845 context->aux = aux->next;
846 kfree(aux);
847 }
Amy Griffise54dc242007-03-29 18:01:04 -0400848 while ((aux = context->aux_pids)) {
849 context->aux_pids = aux->next;
850 kfree(aux);
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854static inline void audit_zero_context(struct audit_context *context,
855 enum audit_state state)
856{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 memset(context, 0, sizeof(*context));
858 context->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861static inline struct audit_context *audit_alloc_context(enum audit_state state)
862{
863 struct audit_context *context;
864
865 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
866 return NULL;
867 audit_zero_context(context, state);
868 return context;
869}
870
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700871/**
872 * audit_alloc - allocate an audit context block for a task
873 * @tsk: task
874 *
875 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * if necessary. Doing so turns on system call auditing for the
877 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700878 * needed.
879 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880int audit_alloc(struct task_struct *tsk)
881{
882 struct audit_context *context;
883 enum audit_state state;
884
Eric Parisb593d382008-01-08 17:38:31 -0500885 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return 0; /* Return if not auditing. */
887
888 state = audit_filter_task(tsk);
889 if (likely(state == AUDIT_DISABLED))
890 return 0;
891
892 if (!(context = audit_alloc_context(state))) {
893 audit_log_lost("out of memory in audit_alloc");
894 return -ENOMEM;
895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 tsk->audit_context = context;
898 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
899 return 0;
900}
901
902static inline void audit_free_context(struct audit_context *context)
903{
904 struct audit_context *previous;
905 int count = 0;
906
907 do {
908 previous = context->previous;
909 if (previous || (count && count < 10)) {
910 ++count;
911 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
912 " freeing multiple contexts (%d)\n",
913 context->serial, context->major,
914 context->name_count, count);
915 }
916 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -0400917 unroll_tree_refs(context, NULL, 0);
918 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400920 kfree(context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 kfree(context);
922 context = previous;
923 } while (context);
924 if (count >= 10)
925 printk(KERN_ERR "audit: freed %d contexts\n", count);
926}
927
Joy Latten161a09e2006-11-27 13:11:54 -0600928void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000929{
930 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000931 unsigned len;
932 int error;
933 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000934
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200935 security_task_getsecid(current, &sid);
Al Viroc4823bc2007-03-12 16:17:42 +0000936 if (!sid)
937 return;
938
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200939 error = security_secid_to_secctx(sid, &ctx, &len);
Al Viroc4823bc2007-03-12 16:17:42 +0000940 if (error) {
941 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000942 goto error_path;
943 return;
944 }
945
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000946 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200947 security_release_secctx(ctx, len);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000948 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000949
950error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000951 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000952 return;
953}
954
Joy Latten161a09e2006-11-27 13:11:54 -0600955EXPORT_SYMBOL(audit_log_task_context);
956
Al Viroe4951492006-03-29 20:17:10 -0500957static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700958{
Al Viro45d9bb02006-03-29 20:02:55 -0500959 char name[sizeof(tsk->comm)];
960 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700961 struct vm_area_struct *vma;
962
Al Viroe4951492006-03-29 20:17:10 -0500963 /* tsk == current */
964
Al Viro45d9bb02006-03-29 20:02:55 -0500965 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100966 audit_log_format(ab, " comm=");
967 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700968
Al Viroe4951492006-03-29 20:17:10 -0500969 if (mm) {
970 down_read(&mm->mmap_sem);
971 vma = mm->mmap;
972 while (vma) {
973 if ((vma->vm_flags & VM_EXECUTABLE) &&
974 vma->vm_file) {
975 audit_log_d_path(ab, "exe=",
Jan Blunck44707fd2008-02-14 19:38:33 -0800976 &vma->vm_file->f_path);
Al Viroe4951492006-03-29 20:17:10 -0500977 break;
978 }
979 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700980 }
Al Viroe4951492006-03-29 20:17:10 -0500981 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700982 }
Al Viroe4951492006-03-29 20:17:10 -0500983 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700984}
985
Amy Griffise54dc242007-03-29 18:01:04 -0400986static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric Paris4746ec52008-01-08 10:06:53 -0500987 uid_t auid, uid_t uid, unsigned int sessionid,
988 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400989{
990 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200991 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -0400992 u32 len;
993 int rc = 0;
994
995 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
996 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -0500997 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -0400998
Eric Paris4746ec52008-01-08 10:06:53 -0500999 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
1000 uid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001001 if (security_secid_to_secctx(sid, &ctx, &len)) {
Eric Parisc2a77802008-01-07 13:40:17 -05001002 audit_log_format(ab, " obj=(none)");
Amy Griffise54dc242007-03-29 18:01:04 -04001003 rc = 1;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001004 } else {
1005 audit_log_format(ab, " obj=%s", ctx);
1006 security_release_secctx(ctx, len);
1007 }
Eric Parisc2a77802008-01-07 13:40:17 -05001008 audit_log_format(ab, " ocomm=");
1009 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -04001010 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -04001011
1012 return rc;
1013}
1014
Eric Parisde6bbd12008-01-07 14:31:58 -05001015/*
1016 * to_send and len_sent accounting are very loose estimates. We aren't
1017 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1018 * within about 500 bytes (next page boundry)
1019 *
1020 * why snprintf? an int is up to 12 digits long. if we just assumed when
1021 * logging that a[%d]= was going to be 16 characters long we would be wasting
1022 * space in every audit message. In one 7500 byte message we can log up to
1023 * about 1000 min size arguments. That comes down to about 50% waste of space
1024 * if we didn't do the snprintf to find out how long arg_num_len was.
1025 */
1026static int audit_log_single_execve_arg(struct audit_context *context,
1027 struct audit_buffer **ab,
1028 int arg_num,
1029 size_t *len_sent,
1030 const char __user *p,
1031 char *buf)
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001032{
Eric Parisde6bbd12008-01-07 14:31:58 -05001033 char arg_num_len_buf[12];
1034 const char __user *tmp_p = p;
1035 /* how many digits are in arg_num? 3 is the length of a=\n */
1036 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3;
1037 size_t len, len_left, to_send;
1038 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1039 unsigned int i, has_cntl = 0, too_long = 0;
1040 int ret;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001041
Eric Parisde6bbd12008-01-07 14:31:58 -05001042 /* strnlen_user includes the null we don't want to send */
1043 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001044
Eric Parisde6bbd12008-01-07 14:31:58 -05001045 /*
1046 * We just created this mm, if we can't find the strings
1047 * we just copied into it something is _very_ wrong. Similar
1048 * for strings that are too long, we should not have created
1049 * any.
1050 */
Eric Parisb0abcfc2008-02-18 18:23:16 -05001051 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001052 WARN_ON(1);
1053 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001054 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001055 }
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001056
Eric Parisde6bbd12008-01-07 14:31:58 -05001057 /* walk the whole argument looking for non-ascii chars */
1058 do {
1059 if (len_left > MAX_EXECVE_AUDIT_LEN)
1060 to_send = MAX_EXECVE_AUDIT_LEN;
1061 else
1062 to_send = len_left;
1063 ret = copy_from_user(buf, tmp_p, to_send);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001064 /*
1065 * There is no reason for this copy to be short. We just
1066 * copied them here, and the mm hasn't been exposed to user-
1067 * space yet.
1068 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001069 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001070 WARN_ON(1);
1071 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001072 return -1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001073 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001074 buf[to_send] = '\0';
1075 has_cntl = audit_string_contains_control(buf, to_send);
1076 if (has_cntl) {
1077 /*
1078 * hex messages get logged as 2 bytes, so we can only
1079 * send half as much in each message
1080 */
1081 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1082 break;
1083 }
1084 len_left -= to_send;
1085 tmp_p += to_send;
1086 } while (len_left > 0);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001087
Eric Parisde6bbd12008-01-07 14:31:58 -05001088 len_left = len;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001089
Eric Parisde6bbd12008-01-07 14:31:58 -05001090 if (len > max_execve_audit_len)
1091 too_long = 1;
1092
1093 /* rewalk the argument actually logging the message */
1094 for (i = 0; len_left > 0; i++) {
1095 int room_left;
1096
1097 if (len_left > max_execve_audit_len)
1098 to_send = max_execve_audit_len;
1099 else
1100 to_send = len_left;
1101
1102 /* do we have space left to send this argument in this ab? */
1103 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1104 if (has_cntl)
1105 room_left -= (to_send * 2);
1106 else
1107 room_left -= to_send;
1108 if (room_left < 0) {
1109 *len_sent = 0;
1110 audit_log_end(*ab);
1111 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1112 if (!*ab)
1113 return 0;
1114 }
1115
1116 /*
1117 * first record needs to say how long the original string was
1118 * so we can be sure nothing was lost.
1119 */
1120 if ((i == 0) && (too_long))
Paul Moore422b03c2008-02-27 10:39:22 -05001121 audit_log_format(*ab, "a%d_len=%zu ", arg_num,
Eric Parisde6bbd12008-01-07 14:31:58 -05001122 has_cntl ? 2*len : len);
1123
1124 /*
1125 * normally arguments are small enough to fit and we already
1126 * filled buf above when we checked for control characters
1127 * so don't bother with another copy_from_user
1128 */
1129 if (len >= max_execve_audit_len)
1130 ret = copy_from_user(buf, p, to_send);
1131 else
1132 ret = 0;
1133 if (ret) {
1134 WARN_ON(1);
1135 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001136 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001137 }
1138 buf[to_send] = '\0';
1139
1140 /* actually log it */
1141 audit_log_format(*ab, "a%d", arg_num);
1142 if (too_long)
1143 audit_log_format(*ab, "[%d]", i);
1144 audit_log_format(*ab, "=");
1145 if (has_cntl)
Eric Parisb556f8a2008-04-18 10:12:59 -04001146 audit_log_n_hex(*ab, buf, to_send);
Eric Parisde6bbd12008-01-07 14:31:58 -05001147 else
1148 audit_log_format(*ab, "\"%s\"", buf);
1149 audit_log_format(*ab, "\n");
1150
1151 p += to_send;
1152 len_left -= to_send;
1153 *len_sent += arg_num_len;
1154 if (has_cntl)
1155 *len_sent += to_send * 2;
1156 else
1157 *len_sent += to_send;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001158 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001159 /* include the null we didn't log */
1160 return len + 1;
1161}
1162
1163static void audit_log_execve_info(struct audit_context *context,
1164 struct audit_buffer **ab,
1165 struct audit_aux_data_execve *axi)
1166{
1167 int i;
1168 size_t len, len_sent = 0;
1169 const char __user *p;
1170 char *buf;
1171
1172 if (axi->mm != current->mm)
1173 return; /* execve failed, no additional info */
1174
1175 p = (const char __user *)axi->mm->arg_start;
1176
1177 audit_log_format(*ab, "argc=%d ", axi->argc);
1178
1179 /*
1180 * we need some kernel buffer to hold the userspace args. Just
1181 * allocate one big one rather than allocating one of the right size
1182 * for every single argument inside audit_log_single_execve_arg()
1183 * should be <8k allocation so should be pretty safe.
1184 */
1185 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1186 if (!buf) {
1187 audit_panic("out of memory for argv string\n");
1188 return;
1189 }
1190
1191 for (i = 0; i < axi->argc; i++) {
1192 len = audit_log_single_execve_arg(context, ab, i,
1193 &len_sent, p, buf);
1194 if (len <= 0)
1195 break;
1196 p += len;
1197 }
1198 kfree(buf);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001199}
1200
Eric Paris851f7ff2008-11-11 21:48:14 +11001201static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1202{
1203 int i;
1204
1205 audit_log_format(ab, " %s=", prefix);
1206 CAP_FOR_EACH_U32(i) {
1207 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1208 }
1209}
1210
1211static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1212{
1213 kernel_cap_t *perm = &name->fcap.permitted;
1214 kernel_cap_t *inh = &name->fcap.inheritable;
1215 int log = 0;
1216
1217 if (!cap_isclear(*perm)) {
1218 audit_log_cap(ab, "cap_fp", perm);
1219 log = 1;
1220 }
1221 if (!cap_isclear(*inh)) {
1222 audit_log_cap(ab, "cap_fi", inh);
1223 log = 1;
1224 }
1225
1226 if (log)
1227 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1228}
1229
Al Viroe4951492006-03-29 20:17:10 -05001230static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
David Howellsb6dff3e2008-11-14 10:39:16 +11001232 struct cred *cred = tsk->cred;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001233 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001235 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -05001236 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Al Viroe4951492006-03-29 20:17:10 -05001238 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001239 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -04001240 if (!context->ppid)
1241 context->ppid = sys_getppid();
David Howellsb6dff3e2008-11-14 10:39:16 +11001242 context->uid = cred->uid;
1243 context->gid = cred->gid;
1244 context->euid = cred->euid;
1245 context->suid = cred->suid;
1246 context->fsuid = cred->fsuid;
1247 context->egid = cred->egid;
1248 context->sgid = cred->sgid;
1249 context->fsgid = cred->fsgid;
Al Viro3f2792f2006-07-16 06:43:48 -04001250 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001251
1252 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (!ab)
1254 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001255 audit_log_format(ab, "arch=%x syscall=%d",
1256 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (context->personality != PER_LINUX)
1258 audit_log_format(ab, " per=%lx", context->personality);
1259 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001260 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001261 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1262 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001263
Alan Coxdbda4c02008-10-13 10:40:53 +01001264 spin_lock_irq(&tsk->sighand->siglock);
Al Viro45d9bb02006-03-29 20:02:55 -05001265 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1266 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -05001267 else
1268 tty = "(none)";
Alan Coxdbda4c02008-10-13 10:40:53 +01001269 spin_unlock_irq(&tsk->sighand->siglock);
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 audit_log_format(ab,
1272 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -04001273 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +01001274 " euid=%u suid=%u fsuid=%u"
Eric Paris4746ec52008-01-08 10:06:53 -05001275 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 context->argv[0],
1277 context->argv[1],
1278 context->argv[2],
1279 context->argv[3],
1280 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -04001281 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 context->pid,
Al Virobfef93a2008-01-10 04:53:18 -05001283 tsk->loginuid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 context->uid,
1285 context->gid,
1286 context->euid, context->suid, context->fsuid,
Eric Paris4746ec52008-01-08 10:06:53 -05001287 context->egid, context->sgid, context->fsgid, tty,
1288 tsk->sessionid);
Alan Coxeb84a202006-09-29 02:01:41 -07001289
Alan Coxeb84a202006-09-29 02:01:41 -07001290
Al Viroe4951492006-03-29 20:17:10 -05001291 audit_log_task_info(ab, tsk);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001292 if (context->filterkey) {
1293 audit_log_format(ab, " key=");
1294 audit_log_untrustedstring(ab, context->filterkey);
1295 } else
1296 audit_log_format(ab, " key=(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
David Woodhouse7551ced2005-05-26 12:04:57 +01001299 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001300
Al Viroe4951492006-03-29 20:17:10 -05001301 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (!ab)
1303 continue; /* audit_panic has been called */
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001306 case AUDIT_MQ_OPEN: {
1307 struct audit_aux_data_mq_open *axi = (void *)aux;
1308 audit_log_format(ab,
1309 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1310 "mq_msgsize=%ld mq_curmsgs=%ld",
1311 axi->oflag, axi->mode, axi->attr.mq_flags,
1312 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1313 axi->attr.mq_curmsgs);
1314 break; }
1315
1316 case AUDIT_MQ_SENDRECV: {
1317 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1318 audit_log_format(ab,
1319 "mqdes=%d msg_len=%zd msg_prio=%u "
1320 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1321 axi->mqdes, axi->msg_len, axi->msg_prio,
1322 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1323 break; }
1324
1325 case AUDIT_MQ_NOTIFY: {
1326 struct audit_aux_data_mq_notify *axi = (void *)aux;
1327 audit_log_format(ab,
1328 "mqdes=%d sigev_signo=%d",
1329 axi->mqdes,
1330 axi->notification.sigev_signo);
1331 break; }
1332
1333 case AUDIT_MQ_GETSETATTR: {
1334 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1335 audit_log_format(ab,
1336 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1337 "mq_curmsgs=%ld ",
1338 axi->mqdes,
1339 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1340 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1341 break; }
1342
Steve Grubbc0404992005-05-13 18:17:42 +01001343 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct audit_aux_data_ipcctl *axi = (void *)aux;
1345 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001346 "ouid=%u ogid=%u mode=%#o",
Linda Knippersac032212006-05-16 22:03:48 -04001347 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001348 if (axi->osid != 0) {
1349 char *ctx = NULL;
1350 u32 len;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001351 if (security_secid_to_secctx(
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001352 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -05001353 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001354 axi->osid);
1355 call_panic = 1;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001356 } else {
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001357 audit_log_format(ab, " obj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001358 security_release_secctx(ctx, len);
1359 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001360 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001361 break; }
1362
Steve Grubb073115d2006-04-02 17:07:33 -04001363 case AUDIT_IPC_SET_PERM: {
1364 struct audit_aux_data_ipcctl *axi = (void *)aux;
1365 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001366 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
Steve Grubb073115d2006-04-02 17:07:33 -04001367 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -04001368 break; }
Linda Knippersac032212006-05-16 22:03:48 -04001369
Al Viro473ae302006-04-26 14:04:08 -04001370 case AUDIT_EXECVE: {
1371 struct audit_aux_data_execve *axi = (void *)aux;
Eric Parisde6bbd12008-01-07 14:31:58 -05001372 audit_log_execve_info(context, &ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001373 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001374
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001375 case AUDIT_SOCKETCALL: {
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001376 struct audit_aux_data_socketcall *axs = (void *)aux;
1377 audit_log_format(ab, "nargs=%d", axs->nargs);
1378 for (i=0; i<axs->nargs; i++)
1379 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1380 break; }
1381
1382 case AUDIT_SOCKADDR: {
1383 struct audit_aux_data_sockaddr *axs = (void *)aux;
1384
1385 audit_log_format(ab, "saddr=");
Eric Parisb556f8a2008-04-18 10:12:59 -04001386 audit_log_n_hex(ab, axs->a, axs->len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001387 break; }
Stephen Smalley01116102005-05-21 00:15:52 +01001388
Al Virodb349502007-02-07 01:48:00 -05001389 case AUDIT_FD_PAIR: {
1390 struct audit_aux_data_fd_pair *axs = (void *)aux;
1391 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1392 break; }
1393
Eric Paris3fc689e2008-11-11 21:48:18 +11001394 case AUDIT_BPRM_FCAPS: {
1395 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1396 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1397 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1398 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1399 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1400 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1401 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1402 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1403 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1404 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1405 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1406 break; }
1407
Eric Parise68b75a02008-11-11 21:48:22 +11001408 case AUDIT_CAPSET: {
1409 struct audit_aux_data_capset *axs = (void *)aux;
1410 audit_log_format(ab, "pid=%d", axs->pid);
1411 audit_log_cap(ab, "cap_pi", &axs->cap.inheritable);
1412 audit_log_cap(ab, "cap_pp", &axs->cap.permitted);
1413 audit_log_cap(ab, "cap_pe", &axs->cap.effective);
1414 break; }
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
Amy Griffise54dc242007-03-29 18:01:04 -04001420 for (aux = context->aux_pids; aux; aux = aux->next) {
1421 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001422
1423 for (i = 0; i < axs->pid_count; i++)
1424 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001425 axs->target_auid[i],
1426 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001427 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001428 axs->target_sid[i],
1429 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001430 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001431 }
1432
Amy Griffise54dc242007-03-29 18:01:04 -04001433 if (context->target_pid &&
1434 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001435 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001436 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001437 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001438 call_panic = 1;
1439
Jan Blunck44707fd2008-02-14 19:38:33 -08001440 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001441 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001442 if (ab) {
Jan Blunck44707fd2008-02-14 19:38:33 -08001443 audit_log_d_path(ab, "cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001444 audit_log_end(ab);
1445 }
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -04001448 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +00001449
Al Viroe4951492006-03-29 20:17:10 -05001450 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (!ab)
1452 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +01001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +00001455
Amy Griffis9c937dc2006-06-08 23:19:31 -04001456 if (n->name) {
1457 switch(n->name_len) {
1458 case AUDIT_NAME_FULL:
1459 /* log the full path */
1460 audit_log_format(ab, " name=");
1461 audit_log_untrustedstring(ab, n->name);
1462 break;
1463 case 0:
1464 /* name was specified as a relative path and the
1465 * directory component is the cwd */
Jan Blunck44707fd2008-02-14 19:38:33 -08001466 audit_log_d_path(ab, " name=", &context->pwd);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001467 break;
1468 default:
1469 /* log the name's directory component */
1470 audit_log_format(ab, " name=");
Eric Parisb556f8a2008-04-18 10:12:59 -04001471 audit_log_n_untrustedstring(ab, n->name,
1472 n->name_len);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001473 }
1474 } else
1475 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001476
Amy Griffis9c937dc2006-06-08 23:19:31 -04001477 if (n->ino != (unsigned long)-1) {
1478 audit_log_format(ab, " inode=%lu"
1479 " dev=%02x:%02x mode=%#o"
1480 " ouid=%u ogid=%u rdev=%02x:%02x",
1481 n->ino,
1482 MAJOR(n->dev),
1483 MINOR(n->dev),
1484 n->mode,
1485 n->uid,
1486 n->gid,
1487 MAJOR(n->rdev),
1488 MINOR(n->rdev));
1489 }
1490 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001491 char *ctx = NULL;
1492 u32 len;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001493 if (security_secid_to_secctx(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001494 n->osid, &ctx, &len)) {
1495 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001496 call_panic = 2;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001497 } else {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001498 audit_log_format(ab, " obj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001499 security_release_secctx(ctx, len);
1500 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001501 }
1502
Eric Paris851f7ff2008-11-11 21:48:14 +11001503 audit_log_fcaps(ab, n);
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 audit_log_end(ab);
1506 }
Eric Parisc0641f22008-01-07 13:49:15 -05001507
1508 /* Send end of event record to help user space know we are finished */
1509 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1510 if (ab)
1511 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001512 if (call_panic)
1513 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001516/**
1517 * audit_free - free a per-task audit context
1518 * @tsk: task whose audit context block to free
1519 *
Al Virofa84cb92006-03-29 20:30:19 -05001520 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522void audit_free(struct task_struct *tsk)
1523{
1524 struct audit_context *context;
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (likely(!context))
1528 return;
1529
1530 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001531 * function (e.g., exit_group), then free context block.
1532 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001533 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001534 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +01001535 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001536 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 audit_free_context(context);
1539}
1540
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001541/**
1542 * audit_syscall_entry - fill in an audit record at syscall entry
1543 * @tsk: task being audited
1544 * @arch: architecture type
1545 * @major: major syscall type (function)
1546 * @a1: additional syscall register 1
1547 * @a2: additional syscall register 2
1548 * @a3: additional syscall register 3
1549 * @a4: additional syscall register 4
1550 *
1551 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 * audit context was created when the task was created and the state or
1553 * filters demand the audit context be built. If the state from the
1554 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1555 * then the record will be written at syscall exit time (otherwise, it
1556 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001557 * be written).
1558 */
Al Viro5411be52006-03-29 20:23:36 -05001559void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 unsigned long a1, unsigned long a2,
1561 unsigned long a3, unsigned long a4)
1562{
Al Viro5411be52006-03-29 20:23:36 -05001563 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct audit_context *context = tsk->audit_context;
1565 enum audit_state state;
1566
Roland McGrath86a1c342008-06-23 15:37:04 -07001567 if (unlikely(!context))
1568 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001570 /*
1571 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * calls in kernel_thread via the entry.S interface, instead of
1573 * with direct calls. (If you are porting to a new
1574 * architecture, hitting this condition can indicate that you
1575 * got the _exit/_leave calls backward in entry.S.)
1576 *
1577 * i386 no
1578 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001579 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 *
1581 * This also happens with vm86 emulation in a non-nested manner
1582 * (entries without exits), so this case must be caught.
1583 */
1584 if (context->in_syscall) {
1585 struct audit_context *newctx;
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587#if AUDIT_DEBUG
1588 printk(KERN_ERR
1589 "audit(:%d) pid=%d in syscall=%d;"
1590 " entering syscall=%d\n",
1591 context->serial, tsk->pid, context->major, major);
1592#endif
1593 newctx = audit_alloc_context(context->state);
1594 if (newctx) {
1595 newctx->previous = context;
1596 context = newctx;
1597 tsk->audit_context = newctx;
1598 } else {
1599 /* If we can't alloc a new context, the best we
1600 * can do is to leak memory (any pending putname
1601 * will be lost). The only other alternative is
1602 * to abandon auditing. */
1603 audit_zero_context(context, context->state);
1604 }
1605 }
1606 BUG_ON(context->in_syscall || context->name_count);
1607
1608 if (!audit_enabled)
1609 return;
1610
2fd6f582005-04-29 16:08:28 +01001611 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 context->major = major;
1613 context->argv[0] = a1;
1614 context->argv[1] = a2;
1615 context->argv[2] = a3;
1616 context->argv[3] = a4;
1617
1618 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001619 context->dummy = !audit_n_rules;
1620 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
David Woodhouse0f45aa12005-06-19 19:35:50 +01001621 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (likely(state == AUDIT_DISABLED))
1623 return;
1624
David Woodhousece625a82005-07-18 14:24:46 -04001625 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 context->ctime = CURRENT_TIME;
1627 context->in_syscall = 1;
1628 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
Alexander Viro419c58f2006-09-29 00:08:50 -04001629 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001632/**
1633 * audit_syscall_exit - deallocate audit context after a system call
1634 * @tsk: task being audited
1635 * @valid: success/failure flag
1636 * @return_code: syscall return value
1637 *
1638 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1640 * filtering, or because some other part of the kernel write an audit
1641 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001642 * free the names stored from getname().
1643 */
Al Viro5411be52006-03-29 20:23:36 -05001644void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Al Viro5411be52006-03-29 20:23:36 -05001646 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 struct audit_context *context;
1648
2fd6f582005-04-29 16:08:28 +01001649 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
David Woodhousef7056d62005-06-20 16:07:33 +01001654 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001655 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 context->in_syscall = 0;
1658 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (context->previous) {
1661 struct audit_context *new_context = context->previous;
1662 context->previous = NULL;
1663 audit_free_context(context);
1664 tsk->audit_context = new_context;
1665 } else {
1666 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001667 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001669 context->aux = NULL;
1670 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001671 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001672 context->target_sid = 0;
Amy Griffis5adc8a62006-06-14 18:45:21 -04001673 kfree(context->filterkey);
1674 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 tsk->audit_context = context;
1676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
Al Viro74c3cbe2007-07-22 08:04:18 -04001679static inline void handle_one(const struct inode *inode)
1680{
1681#ifdef CONFIG_AUDIT_TREE
1682 struct audit_context *context;
1683 struct audit_tree_refs *p;
1684 struct audit_chunk *chunk;
1685 int count;
1686 if (likely(list_empty(&inode->inotify_watches)))
1687 return;
1688 context = current->audit_context;
1689 p = context->trees;
1690 count = context->tree_count;
1691 rcu_read_lock();
1692 chunk = audit_tree_lookup(inode);
1693 rcu_read_unlock();
1694 if (!chunk)
1695 return;
1696 if (likely(put_tree_ref(context, chunk)))
1697 return;
1698 if (unlikely(!grow_tree_refs(context))) {
Eric Paris436c4052008-04-18 10:01:04 -04001699 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001700 audit_set_auditable(context);
1701 audit_put_chunk(chunk);
1702 unroll_tree_refs(context, p, count);
1703 return;
1704 }
1705 put_tree_ref(context, chunk);
1706#endif
1707}
1708
1709static void handle_path(const struct dentry *dentry)
1710{
1711#ifdef CONFIG_AUDIT_TREE
1712 struct audit_context *context;
1713 struct audit_tree_refs *p;
1714 const struct dentry *d, *parent;
1715 struct audit_chunk *drop;
1716 unsigned long seq;
1717 int count;
1718
1719 context = current->audit_context;
1720 p = context->trees;
1721 count = context->tree_count;
1722retry:
1723 drop = NULL;
1724 d = dentry;
1725 rcu_read_lock();
1726 seq = read_seqbegin(&rename_lock);
1727 for(;;) {
1728 struct inode *inode = d->d_inode;
1729 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1730 struct audit_chunk *chunk;
1731 chunk = audit_tree_lookup(inode);
1732 if (chunk) {
1733 if (unlikely(!put_tree_ref(context, chunk))) {
1734 drop = chunk;
1735 break;
1736 }
1737 }
1738 }
1739 parent = d->d_parent;
1740 if (parent == d)
1741 break;
1742 d = parent;
1743 }
1744 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1745 rcu_read_unlock();
1746 if (!drop) {
1747 /* just a race with rename */
1748 unroll_tree_refs(context, p, count);
1749 goto retry;
1750 }
1751 audit_put_chunk(drop);
1752 if (grow_tree_refs(context)) {
1753 /* OK, got more space */
1754 unroll_tree_refs(context, p, count);
1755 goto retry;
1756 }
1757 /* too bad */
1758 printk(KERN_WARNING
Eric Paris436c4052008-04-18 10:01:04 -04001759 "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001760 unroll_tree_refs(context, p, count);
1761 audit_set_auditable(context);
1762 return;
1763 }
1764 rcu_read_unlock();
1765#endif
1766}
1767
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001768/**
1769 * audit_getname - add a name to the list
1770 * @name: name to add
1771 *
1772 * Add a name to the list of audit names for this context.
1773 * Called from fs/namei.c:getname().
1774 */
Al Virod8945bb52006-05-18 16:01:30 -04001775void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct audit_context *context = current->audit_context;
1778
Al Virod8945bb52006-05-18 16:01:30 -04001779 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return;
1781
1782 if (!context->in_syscall) {
1783#if AUDIT_DEBUG == 2
1784 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1785 __FILE__, __LINE__, context->serial, name);
1786 dump_stack();
1787#endif
1788 return;
1789 }
1790 BUG_ON(context->name_count >= AUDIT_NAMES);
1791 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001792 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1793 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 context->names[context->name_count].ino = (unsigned long)-1;
Amy Griffise41e8bd2007-02-13 14:14:09 -05001795 context->names[context->name_count].osid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 ++context->name_count;
Jan Blunck44707fd2008-02-14 19:38:33 -08001797 if (!context->pwd.dentry) {
David Woodhouse8f37d472005-05-27 12:17:28 +01001798 read_lock(&current->fs->lock);
Jan Blunck44707fd2008-02-14 19:38:33 -08001799 context->pwd = current->fs->pwd;
1800 path_get(&current->fs->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001801 read_unlock(&current->fs->lock);
1802 }
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001806/* audit_putname - intercept a putname request
1807 * @name: name to intercept and delay for putname
1808 *
1809 * If we have stored the name from getname in the audit context,
1810 * then we delay the putname until syscall exit.
1811 * Called from include/linux/fs.h:putname().
1812 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813void audit_putname(const char *name)
1814{
1815 struct audit_context *context = current->audit_context;
1816
1817 BUG_ON(!context);
1818 if (!context->in_syscall) {
1819#if AUDIT_DEBUG == 2
1820 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1821 __FILE__, __LINE__, context->serial, name);
1822 if (context->name_count) {
1823 int i;
1824 for (i = 0; i < context->name_count; i++)
1825 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1826 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001827 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829#endif
1830 __putname(name);
1831 }
1832#if AUDIT_DEBUG
1833 else {
1834 ++context->put_count;
1835 if (context->put_count > context->name_count) {
1836 printk(KERN_ERR "%s:%d(:%d): major=%d"
1837 " in_syscall=%d putname(%p) name_count=%d"
1838 " put_count=%d\n",
1839 __FILE__, __LINE__,
1840 context->serial, context->major,
1841 context->in_syscall, name, context->name_count,
1842 context->put_count);
1843 dump_stack();
1844 }
1845 }
1846#endif
1847}
1848
Amy Griffis5712e882007-02-13 14:15:22 -05001849static int audit_inc_name_count(struct audit_context *context,
1850 const struct inode *inode)
1851{
1852 if (context->name_count >= AUDIT_NAMES) {
1853 if (inode)
1854 printk(KERN_DEBUG "name_count maxed, losing inode data: "
Eric Paris436c4052008-04-18 10:01:04 -04001855 "dev=%02x:%02x, inode=%lu\n",
Amy Griffis5712e882007-02-13 14:15:22 -05001856 MAJOR(inode->i_sb->s_dev),
1857 MINOR(inode->i_sb->s_dev),
1858 inode->i_ino);
1859
1860 else
Eric Paris436c4052008-04-18 10:01:04 -04001861 printk(KERN_DEBUG "name_count maxed, losing inode data\n");
Amy Griffis5712e882007-02-13 14:15:22 -05001862 return 1;
1863 }
1864 context->name_count++;
1865#if AUDIT_DEBUG
1866 context->ino_count++;
1867#endif
1868 return 0;
1869}
1870
Eric Paris851f7ff2008-11-11 21:48:14 +11001871
1872static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1873{
1874 struct cpu_vfs_cap_data caps;
1875 int rc;
1876
1877 memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
1878 memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
1879 name->fcap.fE = 0;
1880 name->fcap_ver = 0;
1881
1882 if (!dentry)
1883 return 0;
1884
1885 rc = get_vfs_caps_from_disk(dentry, &caps);
1886 if (rc)
1887 return rc;
1888
1889 name->fcap.permitted = caps.permitted;
1890 name->fcap.inheritable = caps.inheritable;
1891 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1892 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1893
1894 return 0;
1895}
1896
1897
Amy Griffis3e2efce2006-07-13 13:16:02 -04001898/* Copy inode data into an audit_names. */
Eric Paris851f7ff2008-11-11 21:48:14 +11001899static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1900 const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001901{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001902 name->ino = inode->i_ino;
1903 name->dev = inode->i_sb->s_dev;
1904 name->mode = inode->i_mode;
1905 name->uid = inode->i_uid;
1906 name->gid = inode->i_gid;
1907 name->rdev = inode->i_rdev;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001908 security_inode_getsecid(inode, &name->osid);
Eric Paris851f7ff2008-11-11 21:48:14 +11001909 audit_copy_fcaps(name, dentry);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001910}
1911
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001912/**
1913 * audit_inode - store the inode and device from a lookup
1914 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001915 * @dentry: dentry being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001916 *
1917 * Called from fs/namei.c:path_lookup().
1918 */
Al Viro5a190ae2007-06-07 12:19:32 -04001919void __audit_inode(const char *name, const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 int idx;
1922 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04001923 const struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 if (!context->in_syscall)
1926 return;
1927 if (context->name_count
1928 && context->names[context->name_count-1].name
1929 && context->names[context->name_count-1].name == name)
1930 idx = context->name_count - 1;
1931 else if (context->name_count > 1
1932 && context->names[context->name_count-2].name
1933 && context->names[context->name_count-2].name == name)
1934 idx = context->name_count - 2;
1935 else {
1936 /* FIXME: how much do we care about inodes that have no
1937 * associated name? */
Amy Griffis5712e882007-02-13 14:15:22 -05001938 if (audit_inc_name_count(context, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001940 idx = context->name_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 context->names[idx].name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001943 handle_path(dentry);
Eric Paris851f7ff2008-11-11 21:48:14 +11001944 audit_copy_inode(&context->names[idx], dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Amy Griffis73241cc2005-11-03 16:00:25 +00001947/**
1948 * audit_inode_child - collect inode info for created/removed objects
1949 * @dname: inode's dentry name
Randy Dunlap481968f2007-10-21 20:59:53 -07001950 * @dentry: dentry being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04001951 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00001952 *
1953 * For syscalls that create or remove filesystem objects, audit_inode
1954 * can only collect information for the filesystem object's parent.
1955 * This call updates the audit context with the child's information.
1956 * Syscalls that create a new filesystem object must be hooked after
1957 * the object is created. Syscalls that remove a filesystem object
1958 * must be hooked prior, in order to capture the target inode during
1959 * unsuccessful attempts.
1960 */
Al Viro5a190ae2007-06-07 12:19:32 -04001961void __audit_inode_child(const char *dname, const struct dentry *dentry,
Amy Griffis73d3ec52006-07-13 13:16:39 -04001962 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00001963{
1964 int idx;
1965 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05001966 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04001967 const struct inode *inode = dentry->d_inode;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001968 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00001969
1970 if (!context->in_syscall)
1971 return;
1972
Al Viro74c3cbe2007-07-22 08:04:18 -04001973 if (inode)
1974 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001975 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001976 if (!dname)
Amy Griffis5712e882007-02-13 14:15:22 -05001977 goto add_names;
Amy Griffis73241cc2005-11-03 16:00:25 +00001978
Amy Griffis5712e882007-02-13 14:15:22 -05001979 /* parent is more likely, look for it first */
1980 for (idx = 0; idx < context->name_count; idx++) {
1981 struct audit_names *n = &context->names[idx];
Amy Griffis73241cc2005-11-03 16:00:25 +00001982
Amy Griffis5712e882007-02-13 14:15:22 -05001983 if (!n->name)
1984 continue;
1985
1986 if (n->ino == parent->i_ino &&
1987 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1988 n->name_len = dirlen; /* update parent data in place */
1989 found_parent = n->name;
1990 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001991 }
Steve Grubbac9910c2006-09-28 14:31:32 -04001992 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001993
Amy Griffis5712e882007-02-13 14:15:22 -05001994 /* no matching parent, look for matching child */
1995 for (idx = 0; idx < context->name_count; idx++) {
1996 struct audit_names *n = &context->names[idx];
Amy Griffis73d3ec52006-07-13 13:16:39 -04001997
Amy Griffis5712e882007-02-13 14:15:22 -05001998 if (!n->name)
1999 continue;
2000
2001 /* strcmp() is the more likely scenario */
2002 if (!strcmp(dname, n->name) ||
2003 !audit_compare_dname_path(dname, n->name, &dirlen)) {
2004 if (inode)
Eric Paris851f7ff2008-11-11 21:48:14 +11002005 audit_copy_inode(n, NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002006 else
2007 n->ino = (unsigned long)-1;
2008 found_child = n->name;
2009 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04002010 }
Amy Griffis5712e882007-02-13 14:15:22 -05002011 }
2012
2013add_names:
2014 if (!found_parent) {
2015 if (audit_inc_name_count(context, parent))
2016 return;
2017 idx = context->name_count - 1;
2018 context->names[idx].name = NULL;
Eric Paris851f7ff2008-11-11 21:48:14 +11002019 audit_copy_inode(&context->names[idx], NULL, parent);
Amy Griffis73d3ec52006-07-13 13:16:39 -04002020 }
Amy Griffis5712e882007-02-13 14:15:22 -05002021
2022 if (!found_child) {
2023 if (audit_inc_name_count(context, inode))
2024 return;
2025 idx = context->name_count - 1;
2026
2027 /* Re-use the name belonging to the slot for a matching parent
2028 * directory. All names for this context are relinquished in
2029 * audit_free_names() */
2030 if (found_parent) {
2031 context->names[idx].name = found_parent;
2032 context->names[idx].name_len = AUDIT_NAME_FULL;
2033 /* don't call __putname() */
2034 context->names[idx].name_put = 0;
2035 } else {
2036 context->names[idx].name = NULL;
2037 }
2038
2039 if (inode)
Eric Paris851f7ff2008-11-11 21:48:14 +11002040 audit_copy_inode(&context->names[idx], NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002041 else
2042 context->names[idx].ino = (unsigned long)-1;
2043 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04002044}
Trond Myklebust50e437d2007-06-07 22:44:34 -04002045EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04002046
2047/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002048 * auditsc_get_stamp - get local copies of audit_context values
2049 * @ctx: audit_context for the task
2050 * @t: timespec to store time recorded in the audit_context
2051 * @serial: serial value that is recorded in the audit_context
2052 *
2053 * Also sets the context as auditable.
2054 */
David Woodhousebfb44962005-05-21 21:08:09 +01002055void auditsc_get_stamp(struct audit_context *ctx,
2056 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
David Woodhousece625a82005-07-18 14:24:46 -04002058 if (!ctx->serial)
2059 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01002060 t->tv_sec = ctx->ctime.tv_sec;
2061 t->tv_nsec = ctx->ctime.tv_nsec;
2062 *serial = ctx->serial;
2063 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Eric Paris4746ec52008-01-08 10:06:53 -05002066/* global counter which is incremented every time something logs in */
2067static atomic_t session_id = ATOMIC_INIT(0);
2068
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002069/**
2070 * audit_set_loginuid - set a task's audit_context loginuid
2071 * @task: task whose audit context is being modified
2072 * @loginuid: loginuid value
2073 *
2074 * Returns 0.
2075 *
2076 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2077 */
Steve Grubb456be6c2005-04-29 17:30:07 +01002078int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Eric Paris4746ec52008-01-08 10:06:53 -05002080 unsigned int sessionid = atomic_inc_return(&session_id);
Steve Grubb41757102006-06-12 07:48:28 -04002081 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01002082
Al Virobfef93a2008-01-10 04:53:18 -05002083 if (context && context->in_syscall) {
2084 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04002085
Al Virobfef93a2008-01-10 04:53:18 -05002086 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2087 if (ab) {
2088 audit_log_format(ab, "login pid=%d uid=%u "
Eric Paris4746ec52008-01-08 10:06:53 -05002089 "old auid=%u new auid=%u"
2090 " old ses=%u new ses=%u",
David Howellsb6dff3e2008-11-14 10:39:16 +11002091 task->pid, task->cred->uid,
Eric Paris4746ec52008-01-08 10:06:53 -05002092 task->loginuid, loginuid,
2093 task->sessionid, sessionid);
Al Virobfef93a2008-01-10 04:53:18 -05002094 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01002095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Eric Paris4746ec52008-01-08 10:06:53 -05002097 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05002098 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 return 0;
2100}
2101
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002102/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002103 * __audit_mq_open - record audit data for a POSIX MQ open
2104 * @oflag: open flag
2105 * @mode: mode bits
2106 * @u_attr: queue attributes
2107 *
2108 * Returns 0 for success or NULL context or < 0 on error.
2109 */
2110int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
2111{
2112 struct audit_aux_data_mq_open *ax;
2113 struct audit_context *context = current->audit_context;
2114
2115 if (!audit_enabled)
2116 return 0;
2117
2118 if (likely(!context))
2119 return 0;
2120
2121 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2122 if (!ax)
2123 return -ENOMEM;
2124
2125 if (u_attr != NULL) {
2126 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
2127 kfree(ax);
2128 return -EFAULT;
2129 }
2130 } else
2131 memset(&ax->attr, 0, sizeof(ax->attr));
2132
2133 ax->oflag = oflag;
2134 ax->mode = mode;
2135
2136 ax->d.type = AUDIT_MQ_OPEN;
2137 ax->d.next = context->aux;
2138 context->aux = (void *)ax;
2139 return 0;
2140}
2141
2142/**
2143 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
2144 * @mqdes: MQ descriptor
2145 * @msg_len: Message length
2146 * @msg_prio: Message priority
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07002147 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002148 *
2149 * Returns 0 for success or NULL context or < 0 on error.
2150 */
2151int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2152 const struct timespec __user *u_abs_timeout)
2153{
2154 struct audit_aux_data_mq_sendrecv *ax;
2155 struct audit_context *context = current->audit_context;
2156
2157 if (!audit_enabled)
2158 return 0;
2159
2160 if (likely(!context))
2161 return 0;
2162
2163 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2164 if (!ax)
2165 return -ENOMEM;
2166
2167 if (u_abs_timeout != NULL) {
2168 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2169 kfree(ax);
2170 return -EFAULT;
2171 }
2172 } else
2173 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2174
2175 ax->mqdes = mqdes;
2176 ax->msg_len = msg_len;
2177 ax->msg_prio = msg_prio;
2178
2179 ax->d.type = AUDIT_MQ_SENDRECV;
2180 ax->d.next = context->aux;
2181 context->aux = (void *)ax;
2182 return 0;
2183}
2184
2185/**
2186 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
2187 * @mqdes: MQ descriptor
2188 * @msg_len: Message length
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07002189 * @u_msg_prio: Message priority
2190 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002191 *
2192 * Returns 0 for success or NULL context or < 0 on error.
2193 */
2194int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
2195 unsigned int __user *u_msg_prio,
2196 const struct timespec __user *u_abs_timeout)
2197{
2198 struct audit_aux_data_mq_sendrecv *ax;
2199 struct audit_context *context = current->audit_context;
2200
2201 if (!audit_enabled)
2202 return 0;
2203
2204 if (likely(!context))
2205 return 0;
2206
2207 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2208 if (!ax)
2209 return -ENOMEM;
2210
2211 if (u_msg_prio != NULL) {
2212 if (get_user(ax->msg_prio, u_msg_prio)) {
2213 kfree(ax);
2214 return -EFAULT;
2215 }
2216 } else
2217 ax->msg_prio = 0;
2218
2219 if (u_abs_timeout != NULL) {
2220 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2221 kfree(ax);
2222 return -EFAULT;
2223 }
2224 } else
2225 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2226
2227 ax->mqdes = mqdes;
2228 ax->msg_len = msg_len;
2229
2230 ax->d.type = AUDIT_MQ_SENDRECV;
2231 ax->d.next = context->aux;
2232 context->aux = (void *)ax;
2233 return 0;
2234}
2235
2236/**
2237 * __audit_mq_notify - record audit data for a POSIX MQ notify
2238 * @mqdes: MQ descriptor
2239 * @u_notification: Notification event
2240 *
2241 * Returns 0 for success or NULL context or < 0 on error.
2242 */
2243
2244int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
2245{
2246 struct audit_aux_data_mq_notify *ax;
2247 struct audit_context *context = current->audit_context;
2248
2249 if (!audit_enabled)
2250 return 0;
2251
2252 if (likely(!context))
2253 return 0;
2254
2255 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2256 if (!ax)
2257 return -ENOMEM;
2258
2259 if (u_notification != NULL) {
2260 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
2261 kfree(ax);
2262 return -EFAULT;
2263 }
2264 } else
2265 memset(&ax->notification, 0, sizeof(ax->notification));
2266
2267 ax->mqdes = mqdes;
2268
2269 ax->d.type = AUDIT_MQ_NOTIFY;
2270 ax->d.next = context->aux;
2271 context->aux = (void *)ax;
2272 return 0;
2273}
2274
2275/**
2276 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2277 * @mqdes: MQ descriptor
2278 * @mqstat: MQ flags
2279 *
2280 * Returns 0 for success or NULL context or < 0 on error.
2281 */
2282int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2283{
2284 struct audit_aux_data_mq_getsetattr *ax;
2285 struct audit_context *context = current->audit_context;
2286
2287 if (!audit_enabled)
2288 return 0;
2289
2290 if (likely(!context))
2291 return 0;
2292
2293 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2294 if (!ax)
2295 return -ENOMEM;
2296
2297 ax->mqdes = mqdes;
2298 ax->mqstat = *mqstat;
2299
2300 ax->d.type = AUDIT_MQ_GETSETATTR;
2301 ax->d.next = context->aux;
2302 context->aux = (void *)ax;
2303 return 0;
2304}
2305
2306/**
Steve Grubb073115d2006-04-02 17:07:33 -04002307 * audit_ipc_obj - record audit data for ipc object
2308 * @ipcp: ipc permissions
2309 *
2310 * Returns 0 for success or NULL context or < 0 on error.
2311 */
Al Virod8945bb52006-05-18 16:01:30 -04002312int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002313{
2314 struct audit_aux_data_ipcctl *ax;
2315 struct audit_context *context = current->audit_context;
2316
Steve Grubb073115d2006-04-02 17:07:33 -04002317 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2318 if (!ax)
2319 return -ENOMEM;
2320
2321 ax->uid = ipcp->uid;
2322 ax->gid = ipcp->gid;
2323 ax->mode = ipcp->mode;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002324 security_ipc_getsecid(ipcp, &ax->osid);
Steve Grubb073115d2006-04-02 17:07:33 -04002325 ax->d.type = AUDIT_IPC;
2326 ax->d.next = context->aux;
2327 context->aux = (void *)ax;
2328 return 0;
2329}
2330
2331/**
2332 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002333 * @qbytes: msgq bytes
2334 * @uid: msgq user id
2335 * @gid: msgq group id
2336 * @mode: msgq mode (permissions)
2337 *
2338 * Returns 0 for success or NULL context or < 0 on error.
2339 */
Al Virod8945bb52006-05-18 16:01:30 -04002340int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 struct audit_aux_data_ipcctl *ax;
2343 struct audit_context *context = current->audit_context;
2344
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002345 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (!ax)
2347 return -ENOMEM;
2348
2349 ax->qbytes = qbytes;
2350 ax->uid = uid;
2351 ax->gid = gid;
2352 ax->mode = mode;
2353
Steve Grubb073115d2006-04-02 17:07:33 -04002354 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 ax->d.next = context->aux;
2356 context->aux = (void *)ax;
2357 return 0;
2358}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002359
Al Viro473ae302006-04-26 14:04:08 -04002360int audit_bprm(struct linux_binprm *bprm)
2361{
2362 struct audit_aux_data_execve *ax;
2363 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002364
Al Viro5ac3a9c2006-07-16 06:38:45 -04002365 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04002366 return 0;
2367
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002368 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002369 if (!ax)
2370 return -ENOMEM;
2371
2372 ax->argc = bprm->argc;
2373 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002374 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002375 ax->d.type = AUDIT_EXECVE;
2376 ax->d.next = context->aux;
2377 context->aux = (void *)ax;
2378 return 0;
2379}
2380
2381
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002382/**
2383 * audit_socketcall - record audit data for sys_socketcall
2384 * @nargs: number of args
2385 * @args: args array
2386 *
2387 * Returns 0 for success or NULL context or < 0 on error.
2388 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002389int audit_socketcall(int nargs, unsigned long *args)
2390{
2391 struct audit_aux_data_socketcall *ax;
2392 struct audit_context *context = current->audit_context;
2393
Al Viro5ac3a9c2006-07-16 06:38:45 -04002394 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002395 return 0;
2396
2397 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2398 if (!ax)
2399 return -ENOMEM;
2400
2401 ax->nargs = nargs;
2402 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2403
2404 ax->d.type = AUDIT_SOCKETCALL;
2405 ax->d.next = context->aux;
2406 context->aux = (void *)ax;
2407 return 0;
2408}
2409
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002410/**
Al Virodb349502007-02-07 01:48:00 -05002411 * __audit_fd_pair - record audit data for pipe and socketpair
2412 * @fd1: the first file descriptor
2413 * @fd2: the second file descriptor
2414 *
2415 * Returns 0 for success or NULL context or < 0 on error.
2416 */
2417int __audit_fd_pair(int fd1, int fd2)
2418{
2419 struct audit_context *context = current->audit_context;
2420 struct audit_aux_data_fd_pair *ax;
2421
2422 if (likely(!context)) {
2423 return 0;
2424 }
2425
2426 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2427 if (!ax) {
2428 return -ENOMEM;
2429 }
2430
2431 ax->fd[0] = fd1;
2432 ax->fd[1] = fd2;
2433
2434 ax->d.type = AUDIT_FD_PAIR;
2435 ax->d.next = context->aux;
2436 context->aux = (void *)ax;
2437 return 0;
2438}
2439
2440/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002441 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2442 * @len: data length in user space
2443 * @a: data address in kernel space
2444 *
2445 * Returns 0 for success or NULL context or < 0 on error.
2446 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002447int audit_sockaddr(int len, void *a)
2448{
2449 struct audit_aux_data_sockaddr *ax;
2450 struct audit_context *context = current->audit_context;
2451
Al Viro5ac3a9c2006-07-16 06:38:45 -04002452 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002453 return 0;
2454
2455 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2456 if (!ax)
2457 return -ENOMEM;
2458
2459 ax->len = len;
2460 memcpy(ax->a, a, len);
2461
2462 ax->d.type = AUDIT_SOCKADDR;
2463 ax->d.next = context->aux;
2464 context->aux = (void *)ax;
2465 return 0;
2466}
2467
Al Viroa5cb0132007-03-20 13:58:35 -04002468void __audit_ptrace(struct task_struct *t)
2469{
2470 struct audit_context *context = current->audit_context;
2471
2472 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002473 context->target_auid = audit_get_loginuid(t);
David Howellsb6dff3e2008-11-14 10:39:16 +11002474 context->target_uid = t->cred->uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002475 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002476 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002477 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002478}
2479
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002480/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002481 * audit_signal_info - record signal info for shutting down audit subsystem
2482 * @sig: signal value
2483 * @t: task being signaled
2484 *
2485 * If the audit subsystem is being terminated, record the task (pid)
2486 * and uid that is doing that.
2487 */
Amy Griffise54dc242007-03-29 18:01:04 -04002488int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002489{
Amy Griffise54dc242007-03-29 18:01:04 -04002490 struct audit_aux_data_pids *axp;
2491 struct task_struct *tsk = current;
2492 struct audit_context *ctx = tsk->audit_context;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002493
Al Viro175fc482007-08-08 00:01:46 +01002494 if (audit_pid && t->tgid == audit_pid) {
Eric Parisee1d3152008-07-07 10:49:45 -04002495 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
Al Viro175fc482007-08-08 00:01:46 +01002496 audit_sig_pid = tsk->pid;
Al Virobfef93a2008-01-10 04:53:18 -05002497 if (tsk->loginuid != -1)
2498 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002499 else
David Howellsb6dff3e2008-11-14 10:39:16 +11002500 audit_sig_uid = tsk->cred->uid;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002501 security_task_getsecid(tsk, &audit_sig_sid);
Al Viro175fc482007-08-08 00:01:46 +01002502 }
2503 if (!audit_signals || audit_dummy_context())
2504 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002505 }
Amy Griffise54dc242007-03-29 18:01:04 -04002506
Amy Griffise54dc242007-03-29 18:01:04 -04002507 /* optimize the common case by putting first signal recipient directly
2508 * in audit_context */
2509 if (!ctx->target_pid) {
2510 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002511 ctx->target_auid = audit_get_loginuid(t);
David Howellsb6dff3e2008-11-14 10:39:16 +11002512 ctx->target_uid = t->cred->uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002513 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002514 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002515 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002516 return 0;
2517 }
2518
2519 axp = (void *)ctx->aux_pids;
2520 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2521 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2522 if (!axp)
2523 return -ENOMEM;
2524
2525 axp->d.type = AUDIT_OBJ_PID;
2526 axp->d.next = ctx->aux_pids;
2527 ctx->aux_pids = (void *)axp;
2528 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002529 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002530
2531 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002532 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
David Howellsb6dff3e2008-11-14 10:39:16 +11002533 axp->target_uid[axp->pid_count] = t->cred->uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002534 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002535 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002536 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002537 axp->pid_count++;
2538
2539 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002540}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002541
2542/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002543 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2544 * @bprm pointer to the bprm being processed
2545 * @caps the caps read from the disk
2546 *
2547 * Simply check if the proc already has the caps given by the file and if not
2548 * store the priv escalation info for later auditing at the end of the syscall
2549 *
2550 * this can fail and we don't care. See the note in audit.h for
2551 * audit_log_bprm_fcaps() for my explaination....
2552 *
2553 * -Eric
2554 */
2555void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_cap_t *pE)
2556{
2557 struct audit_aux_data_bprm_fcaps *ax;
2558 struct audit_context *context = current->audit_context;
2559 struct cpu_vfs_cap_data vcaps;
2560 struct dentry *dentry;
2561
2562 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2563 if (!ax)
2564 return;
2565
2566 ax->d.type = AUDIT_BPRM_FCAPS;
2567 ax->d.next = context->aux;
2568 context->aux = (void *)ax;
2569
2570 dentry = dget(bprm->file->f_dentry);
2571 get_vfs_caps_from_disk(dentry, &vcaps);
2572 dput(dentry);
2573
2574 ax->fcap.permitted = vcaps.permitted;
2575 ax->fcap.inheritable = vcaps.inheritable;
2576 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2577 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2578
2579 ax->old_pcap.permitted = *pP;
David Howellsb6dff3e2008-11-14 10:39:16 +11002580 ax->old_pcap.inheritable = current->cred->cap_inheritable;
Eric Paris3fc689e2008-11-11 21:48:18 +11002581 ax->old_pcap.effective = *pE;
2582
David Howellsb6dff3e2008-11-14 10:39:16 +11002583 ax->new_pcap.permitted = current->cred->cap_permitted;
2584 ax->new_pcap.inheritable = current->cred->cap_inheritable;
2585 ax->new_pcap.effective = current->cred->cap_effective;
Eric Paris3fc689e2008-11-11 21:48:18 +11002586}
2587
2588/**
Eric Parise68b75a02008-11-11 21:48:22 +11002589 * __audit_log_capset - store information about the arguments to the capset syscall
2590 * @pid target pid of the capset call
2591 * @eff effective cap set
2592 * @inh inheritible cap set
2593 * @perm permited cap set
2594 *
2595 * Record the aguments userspace sent to sys_capset for later printing by the
2596 * audit system if applicable
2597 */
2598int __audit_log_capset(pid_t pid, kernel_cap_t *eff, kernel_cap_t *inh, kernel_cap_t *perm)
2599{
2600 struct audit_aux_data_capset *ax;
2601 struct audit_context *context = current->audit_context;
2602
2603 if (likely(!audit_enabled || !context || context->dummy))
2604 return 0;
2605
2606 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2607 if (!ax)
2608 return -ENOMEM;
2609
2610 ax->d.type = AUDIT_CAPSET;
2611 ax->d.next = context->aux;
2612 context->aux = (void *)ax;
2613
2614 ax->pid = pid;
2615 ax->cap.effective = *eff;
2616 ax->cap.inheritable = *eff;
2617 ax->cap.permitted = *perm;
2618
2619 return 0;
2620}
2621
2622/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002623 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002624 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002625 *
2626 * If a process ends with a core dump, something fishy is going on and we
2627 * should record the event for investigation.
2628 */
2629void audit_core_dumps(long signr)
2630{
2631 struct audit_buffer *ab;
2632 u32 sid;
David Howells76aac0e2008-11-14 10:39:12 +11002633 uid_t auid = audit_get_loginuid(current), uid;
2634 gid_t gid;
Eric Paris4746ec52008-01-08 10:06:53 -05002635 unsigned int sessionid = audit_get_sessionid(current);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002636
2637 if (!audit_enabled)
2638 return;
2639
2640 if (signr == SIGQUIT) /* don't care for those */
2641 return;
2642
2643 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
David Howells76aac0e2008-11-14 10:39:12 +11002644 current_uid_gid(&uid, &gid);
Eric Paris4746ec52008-01-08 10:06:53 -05002645 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
David Howells76aac0e2008-11-14 10:39:12 +11002646 auid, uid, gid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002647 security_task_getsecid(current, &sid);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002648 if (sid) {
2649 char *ctx = NULL;
2650 u32 len;
2651
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002652 if (security_secid_to_secctx(sid, &ctx, &len))
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002653 audit_log_format(ab, " ssid=%u", sid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002654 else {
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002655 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002656 security_release_secctx(ctx, len);
2657 }
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002658 }
2659 audit_log_format(ab, " pid=%d comm=", current->pid);
2660 audit_log_untrustedstring(ab, current->comm);
2661 audit_log_format(ab, " sig=%ld", signr);
2662 audit_log_end(ab);
2663}