blob: 3229cd4206f5a5e940596d5d13cdc1b47a6b53f5 [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
Al Viro74c3cbe2007-07-22 08:04:18 -0400207struct audit_tree_refs {
208 struct audit_tree_refs *next;
209 struct audit_chunk *c[31];
210};
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/* The per-task audit context. */
213struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400214 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int in_syscall; /* 1 if task is in a syscall */
216 enum audit_state state;
217 unsigned int serial; /* serial number for record */
218 struct timespec ctime; /* time of syscall entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int major; /* syscall number */
220 unsigned long argv[4]; /* syscall arguments */
221 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100222 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int auditable; /* 1 if record should be written */
224 int name_count;
225 struct audit_names names[AUDIT_NAMES];
Amy Griffis5adc8a62006-06-14 18:45:21 -0400226 char * filterkey; /* key for rule that triggered record */
Jan Blunck44707fd2008-02-14 19:38:33 -0800227 struct path pwd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct audit_context *previous; /* For nested syscalls */
229 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400230 struct audit_aux_data *aux_pids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400233 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 uid_t uid, euid, suid, fsuid;
235 gid_t gid, egid, sgid, fsgid;
236 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100237 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Al Viroa5cb0132007-03-20 13:58:35 -0400239 pid_t target_pid;
Eric Parisc2a77802008-01-07 13:40:17 -0500240 uid_t target_auid;
241 uid_t target_uid;
Eric Paris4746ec52008-01-08 10:06:53 -0500242 unsigned int target_sessionid;
Al Viroa5cb0132007-03-20 13:58:35 -0400243 u32 target_sid;
Eric Parisc2a77802008-01-07 13:40:17 -0500244 char target_comm[TASK_COMM_LEN];
Al Viroa5cb0132007-03-20 13:58:35 -0400245
Al Viro74c3cbe2007-07-22 08:04:18 -0400246 struct audit_tree_refs *trees, *first_trees;
247 int tree_count;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#if AUDIT_DEBUG
250 int put_count;
251 int ino_count;
252#endif
253};
254
Al Viro55669bf2006-08-31 19:26:40 -0400255#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
256static inline int open_arg(int flags, int mask)
257{
258 int n = ACC_MODE(flags);
259 if (flags & (O_TRUNC | O_CREAT))
260 n |= AUDIT_PERM_WRITE;
261 return n & mask;
262}
263
264static int audit_match_perm(struct audit_context *ctx, int mask)
265{
Cordeliac4bacef2008-08-18 09:45:51 -0700266 unsigned n;
zhangxiliang1a61c882008-08-02 10:56:37 +0800267 if (unlikely(!ctx))
268 return 0;
Cordeliac4bacef2008-08-18 09:45:51 -0700269 n = ctx->major;
Alan Coxdbda4c02008-10-13 10:40:53 +0100270
Al Viro55669bf2006-08-31 19:26:40 -0400271 switch (audit_classify_syscall(ctx->arch, n)) {
272 case 0: /* native */
273 if ((mask & AUDIT_PERM_WRITE) &&
274 audit_match_class(AUDIT_CLASS_WRITE, n))
275 return 1;
276 if ((mask & AUDIT_PERM_READ) &&
277 audit_match_class(AUDIT_CLASS_READ, n))
278 return 1;
279 if ((mask & AUDIT_PERM_ATTR) &&
280 audit_match_class(AUDIT_CLASS_CHATTR, n))
281 return 1;
282 return 0;
283 case 1: /* 32bit on biarch */
284 if ((mask & AUDIT_PERM_WRITE) &&
285 audit_match_class(AUDIT_CLASS_WRITE_32, n))
286 return 1;
287 if ((mask & AUDIT_PERM_READ) &&
288 audit_match_class(AUDIT_CLASS_READ_32, n))
289 return 1;
290 if ((mask & AUDIT_PERM_ATTR) &&
291 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
292 return 1;
293 return 0;
294 case 2: /* open */
295 return mask & ACC_MODE(ctx->argv[1]);
296 case 3: /* openat */
297 return mask & ACC_MODE(ctx->argv[2]);
298 case 4: /* socketcall */
299 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
300 case 5: /* execve */
301 return mask & AUDIT_PERM_EXEC;
302 default:
303 return 0;
304 }
305}
306
Al Viro8b67dca2008-04-28 04:15:49 -0400307static int audit_match_filetype(struct audit_context *ctx, int which)
308{
309 unsigned index = which & ~S_IFMT;
310 mode_t mode = which & S_IFMT;
zhangxiliang1a61c882008-08-02 10:56:37 +0800311
312 if (unlikely(!ctx))
313 return 0;
314
Al Viro8b67dca2008-04-28 04:15:49 -0400315 if (index >= ctx->name_count)
316 return 0;
317 if (ctx->names[index].ino == -1)
318 return 0;
319 if ((ctx->names[index].mode ^ mode) & S_IFMT)
320 return 0;
321 return 1;
322}
323
Al Viro74c3cbe2007-07-22 08:04:18 -0400324/*
325 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
326 * ->first_trees points to its beginning, ->trees - to the current end of data.
327 * ->tree_count is the number of free entries in array pointed to by ->trees.
328 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
329 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
330 * it's going to remain 1-element for almost any setup) until we free context itself.
331 * References in it _are_ dropped - at the same time we free/drop aux stuff.
332 */
333
334#ifdef CONFIG_AUDIT_TREE
335static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
336{
337 struct audit_tree_refs *p = ctx->trees;
338 int left = ctx->tree_count;
339 if (likely(left)) {
340 p->c[--left] = chunk;
341 ctx->tree_count = left;
342 return 1;
343 }
344 if (!p)
345 return 0;
346 p = p->next;
347 if (p) {
348 p->c[30] = chunk;
349 ctx->trees = p;
350 ctx->tree_count = 30;
351 return 1;
352 }
353 return 0;
354}
355
356static int grow_tree_refs(struct audit_context *ctx)
357{
358 struct audit_tree_refs *p = ctx->trees;
359 ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
360 if (!ctx->trees) {
361 ctx->trees = p;
362 return 0;
363 }
364 if (p)
365 p->next = ctx->trees;
366 else
367 ctx->first_trees = ctx->trees;
368 ctx->tree_count = 31;
369 return 1;
370}
371#endif
372
373static void unroll_tree_refs(struct audit_context *ctx,
374 struct audit_tree_refs *p, int count)
375{
376#ifdef CONFIG_AUDIT_TREE
377 struct audit_tree_refs *q;
378 int n;
379 if (!p) {
380 /* we started with empty chain */
381 p = ctx->first_trees;
382 count = 31;
383 /* if the very first allocation has failed, nothing to do */
384 if (!p)
385 return;
386 }
387 n = count;
388 for (q = p; q != ctx->trees; q = q->next, n = 31) {
389 while (n--) {
390 audit_put_chunk(q->c[n]);
391 q->c[n] = NULL;
392 }
393 }
394 while (n-- > ctx->tree_count) {
395 audit_put_chunk(q->c[n]);
396 q->c[n] = NULL;
397 }
398 ctx->trees = p;
399 ctx->tree_count = count;
400#endif
401}
402
403static void free_tree_refs(struct audit_context *ctx)
404{
405 struct audit_tree_refs *p, *q;
406 for (p = ctx->first_trees; p; p = q) {
407 q = p->next;
408 kfree(p);
409 }
410}
411
412static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
413{
414#ifdef CONFIG_AUDIT_TREE
415 struct audit_tree_refs *p;
416 int n;
417 if (!tree)
418 return 0;
419 /* full ones */
420 for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
421 for (n = 0; n < 31; n++)
422 if (audit_tree_match(p->c[n], tree))
423 return 1;
424 }
425 /* partial */
426 if (p) {
427 for (n = ctx->tree_count; n < 31; n++)
428 if (audit_tree_match(p->c[n], tree))
429 return 1;
430 }
431#endif
432 return 0;
433}
434
Amy Griffisf368c07d2006-04-07 16:55:56 -0400435/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/* Compare a task_struct with an audit_rule. Return 1 on match, 0
437 * otherwise. */
438static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500439 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400441 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 enum audit_state *state)
443{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400444 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600445 u32 sid;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500448 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int result = 0;
450
Amy Griffis93315ed2006-02-07 12:05:27 -0500451 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500453 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
Al Viro3c662512006-05-06 08:26:27 -0400455 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400456 if (ctx) {
457 if (!ctx->ppid)
458 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400459 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400460 }
Al Viro3c662512006-05-06 08:26:27 -0400461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500463 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500466 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500469 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500472 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500475 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500478 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500481 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500484 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500487 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
2fd6f582005-04-29 16:08:28 +0100489 case AUDIT_ARCH:
Daniel Walker9f8dbe92007-10-18 03:06:09 -0700490 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500491 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 case AUDIT_EXIT:
495 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500496 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100499 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500500 if (f->val)
501 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100502 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500503 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
506 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400507 if (name)
508 result = audit_comparator(MAJOR(name->dev),
509 f->op, f->val);
510 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500512 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ++result;
514 break;
515 }
516 }
517 }
518 break;
519 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400520 if (name)
521 result = audit_comparator(MINOR(name->dev),
522 f->op, f->val);
523 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500525 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 ++result;
527 break;
528 }
529 }
530 }
531 break;
532 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400533 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400534 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400535 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400537 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ++result;
539 break;
540 }
541 }
542 }
543 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400544 case AUDIT_WATCH:
545 if (name && rule->watch->ino != (unsigned long)-1)
546 result = (name->dev == rule->watch->dev &&
Amy Griffis9c937dc2006-06-08 23:19:31 -0400547 name->ino == rule->watch->ino);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400548 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400549 case AUDIT_DIR:
550 if (ctx)
551 result = match_tree_refs(ctx, rule->tree);
552 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 case AUDIT_LOGINUID:
554 result = 0;
555 if (ctx)
Al Virobfef93a2008-01-10 04:53:18 -0500556 result = audit_comparator(tsk->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500558 case AUDIT_SUBJ_USER:
559 case AUDIT_SUBJ_ROLE:
560 case AUDIT_SUBJ_TYPE:
561 case AUDIT_SUBJ_SEN:
562 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600563 /* NOTE: this may return negative values indicating
564 a temporary error. We simply treat this as a
565 match for now to avoid losing information that
566 may be wanted. An error message will also be
567 logged upon error */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000568 if (f->lsm_rule) {
Steve Grubb2ad312d2006-04-11 08:50:56 -0400569 if (need_sid) {
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200570 security_task_getsecid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400571 need_sid = 0;
572 }
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200573 result = security_audit_rule_match(sid, f->type,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600574 f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000575 f->lsm_rule,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600576 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400577 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600578 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500579 case AUDIT_OBJ_USER:
580 case AUDIT_OBJ_ROLE:
581 case AUDIT_OBJ_TYPE:
582 case AUDIT_OBJ_LEV_LOW:
583 case AUDIT_OBJ_LEV_HIGH:
584 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
585 also applies here */
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000586 if (f->lsm_rule) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500587 /* Find files that match */
588 if (name) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200589 result = security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500590 name->osid, f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000591 f->lsm_rule, ctx);
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500592 } else if (ctx) {
593 for (j = 0; j < ctx->name_count; j++) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200594 if (security_audit_rule_match(
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500595 ctx->names[j].osid,
596 f->type, f->op,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000597 f->lsm_rule, ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500598 ++result;
599 break;
600 }
601 }
602 }
603 /* Find ipc objects that match */
604 if (ctx) {
605 struct audit_aux_data *aux;
606 for (aux = ctx->aux; aux;
607 aux = aux->next) {
608 if (aux->type == AUDIT_IPC) {
609 struct audit_aux_data_ipcctl *axi = (void *)aux;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000610 if (security_audit_rule_match(axi->osid, f->type, f->op, f->lsm_rule, ctx)) {
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500611 ++result;
612 break;
613 }
614 }
615 }
616 }
617 }
618 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 case AUDIT_ARG0:
620 case AUDIT_ARG1:
621 case AUDIT_ARG2:
622 case AUDIT_ARG3:
623 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500624 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400626 case AUDIT_FILTERKEY:
627 /* ignore this field for filtering */
628 result = 1;
629 break;
Al Viro55669bf2006-08-31 19:26:40 -0400630 case AUDIT_PERM:
631 result = audit_match_perm(ctx, f->val);
632 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400633 case AUDIT_FILETYPE:
634 result = audit_match_filetype(ctx, f->val);
635 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (!result)
639 return 0;
640 }
zhangxiliang980dfb02008-08-01 19:15:47 +0800641 if (rule->filterkey && ctx)
Amy Griffis5adc8a62006-06-14 18:45:21 -0400642 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 switch (rule->action) {
644 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
646 }
647 return 1;
648}
649
650/* At process creation time, we can determine if system-call auditing is
651 * completely disabled for this task. Since we only have the task
652 * structure at this point, we can only check uid and gid.
653 */
654static enum audit_state audit_filter_task(struct task_struct *tsk)
655{
656 struct audit_entry *e;
657 enum audit_state state;
658
659 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100660 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400661 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 rcu_read_unlock();
663 return state;
664 }
665 }
666 rcu_read_unlock();
667 return AUDIT_BUILD_CONTEXT;
668}
669
670/* At syscall entry and exit time, this filter is called if the
671 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100672 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700673 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
675static enum audit_state audit_filter_syscall(struct task_struct *tsk,
676 struct audit_context *ctx,
677 struct list_head *list)
678{
679 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100680 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
David Woodhouse351bb722005-07-14 14:40:06 +0100682 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100683 return AUDIT_DISABLED;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100686 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000687 int word = AUDIT_WORD(ctx->major);
688 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100689
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000690 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400691 if ((e->rule.mask[word] & bit) == bit &&
692 audit_filter_rules(tsk, &e->rule, ctx, NULL,
693 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000694 rcu_read_unlock();
695 return state;
696 }
697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 rcu_read_unlock();
700 return AUDIT_BUILD_CONTEXT;
701}
702
Amy Griffisf368c07d2006-04-07 16:55:56 -0400703/* At syscall exit time, this filter is called if any audit_names[] have been
704 * collected during syscall processing. We only check rules in sublists at hash
705 * buckets applicable to the inode numbers in audit_names[].
706 * Regarding audit_state, same rules apply as for audit_filter_syscall().
707 */
708enum audit_state audit_filter_inodes(struct task_struct *tsk,
709 struct audit_context *ctx)
710{
711 int i;
712 struct audit_entry *e;
713 enum audit_state state;
714
715 if (audit_pid && tsk->tgid == audit_pid)
716 return AUDIT_DISABLED;
717
718 rcu_read_lock();
719 for (i = 0; i < ctx->name_count; i++) {
720 int word = AUDIT_WORD(ctx->major);
721 int bit = AUDIT_BIT(ctx->major);
722 struct audit_names *n = &ctx->names[i];
723 int h = audit_hash_ino((u32)n->ino);
724 struct list_head *list = &audit_inode_hash[h];
725
726 if (list_empty(list))
727 continue;
728
729 list_for_each_entry_rcu(e, list, list) {
730 if ((e->rule.mask[word] & bit) == bit &&
731 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
732 rcu_read_unlock();
733 return state;
734 }
735 }
736 }
737 rcu_read_unlock();
738 return AUDIT_BUILD_CONTEXT;
739}
740
741void audit_set_auditable(struct audit_context *ctx)
742{
743 ctx->auditable = 1;
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static inline struct audit_context *audit_get_context(struct task_struct *tsk,
747 int return_valid,
748 int return_code)
749{
750 struct audit_context *context = tsk->audit_context;
751
752 if (likely(!context))
753 return NULL;
754 context->return_valid = return_valid;
Eric Parisf701b752008-01-07 13:34:51 -0500755
756 /*
757 * we need to fix up the return code in the audit logs if the actual
758 * return codes are later going to be fixed up by the arch specific
759 * signal handlers
760 *
761 * This is actually a test for:
762 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
763 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
764 *
765 * but is faster than a bunch of ||
766 */
767 if (unlikely(return_code <= -ERESTARTSYS) &&
768 (return_code >= -ERESTART_RESTARTBLOCK) &&
769 (return_code != -ENOIOCTLCMD))
770 context->return_code = -EINTR;
771 else
772 context->return_code = return_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Al Virod51374a2006-08-03 10:59:26 -0400774 if (context->in_syscall && !context->dummy && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400776
David Woodhouse0f45aa12005-06-19 19:35:50 +0100777 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400778 if (state == AUDIT_RECORD_CONTEXT) {
779 context->auditable = 1;
780 goto get_context;
781 }
782
783 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (state == AUDIT_RECORD_CONTEXT)
785 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
Amy Griffisf368c07d2006-04-07 16:55:56 -0400789get_context:
Al Viro3f2792f2006-07-16 06:43:48 -0400790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 tsk->audit_context = NULL;
792 return context;
793}
794
795static inline void audit_free_names(struct audit_context *context)
796{
797 int i;
798
799#if AUDIT_DEBUG == 2
800 if (context->auditable
801 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000802 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 " name_count=%d put_count=%d"
804 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000805 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 context->serial, context->major, context->in_syscall,
807 context->name_count, context->put_count,
808 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000809 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 printk(KERN_ERR "names[%d] = %p = %s\n", i,
811 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000812 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 dump_stack();
815 return;
816 }
817#endif
818#if AUDIT_DEBUG
819 context->put_count = 0;
820 context->ino_count = 0;
821#endif
822
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000823 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400824 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 context->name_count = 0;
Jan Blunck44707fd2008-02-14 19:38:33 -0800828 path_put(&context->pwd);
829 context->pwd.dentry = NULL;
830 context->pwd.mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833static inline void audit_free_aux(struct audit_context *context)
834{
835 struct audit_aux_data *aux;
836
837 while ((aux = context->aux)) {
838 context->aux = aux->next;
839 kfree(aux);
840 }
Amy Griffise54dc242007-03-29 18:01:04 -0400841 while ((aux = context->aux_pids)) {
842 context->aux_pids = aux->next;
843 kfree(aux);
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847static inline void audit_zero_context(struct audit_context *context,
848 enum audit_state state)
849{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 memset(context, 0, sizeof(*context));
851 context->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854static inline struct audit_context *audit_alloc_context(enum audit_state state)
855{
856 struct audit_context *context;
857
858 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
859 return NULL;
860 audit_zero_context(context, state);
861 return context;
862}
863
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700864/**
865 * audit_alloc - allocate an audit context block for a task
866 * @tsk: task
867 *
868 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * if necessary. Doing so turns on system call auditing for the
870 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700871 * needed.
872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873int audit_alloc(struct task_struct *tsk)
874{
875 struct audit_context *context;
876 enum audit_state state;
877
Eric Parisb593d382008-01-08 17:38:31 -0500878 if (likely(!audit_ever_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0; /* Return if not auditing. */
880
881 state = audit_filter_task(tsk);
882 if (likely(state == AUDIT_DISABLED))
883 return 0;
884
885 if (!(context = audit_alloc_context(state))) {
886 audit_log_lost("out of memory in audit_alloc");
887 return -ENOMEM;
888 }
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 tsk->audit_context = context;
891 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
892 return 0;
893}
894
895static inline void audit_free_context(struct audit_context *context)
896{
897 struct audit_context *previous;
898 int count = 0;
899
900 do {
901 previous = context->previous;
902 if (previous || (count && count < 10)) {
903 ++count;
904 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
905 " freeing multiple contexts (%d)\n",
906 context->serial, context->major,
907 context->name_count, count);
908 }
909 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -0400910 unroll_tree_refs(context, NULL, 0);
911 free_tree_refs(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400913 kfree(context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 kfree(context);
915 context = previous;
916 } while (context);
917 if (count >= 10)
918 printk(KERN_ERR "audit: freed %d contexts\n", count);
919}
920
Joy Latten161a09e2006-11-27 13:11:54 -0600921void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000922{
923 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000924 unsigned len;
925 int error;
926 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000927
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200928 security_task_getsecid(current, &sid);
Al Viroc4823bc2007-03-12 16:17:42 +0000929 if (!sid)
930 return;
931
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200932 error = security_secid_to_secctx(sid, &ctx, &len);
Al Viroc4823bc2007-03-12 16:17:42 +0000933 if (error) {
934 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000935 goto error_path;
936 return;
937 }
938
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000939 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200940 security_release_secctx(ctx, len);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000941 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000942
943error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000944 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000945 return;
946}
947
Joy Latten161a09e2006-11-27 13:11:54 -0600948EXPORT_SYMBOL(audit_log_task_context);
949
Al Viroe4951492006-03-29 20:17:10 -0500950static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700951{
Al Viro45d9bb02006-03-29 20:02:55 -0500952 char name[sizeof(tsk->comm)];
953 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700954 struct vm_area_struct *vma;
955
Al Viroe4951492006-03-29 20:17:10 -0500956 /* tsk == current */
957
Al Viro45d9bb02006-03-29 20:02:55 -0500958 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100959 audit_log_format(ab, " comm=");
960 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700961
Al Viroe4951492006-03-29 20:17:10 -0500962 if (mm) {
963 down_read(&mm->mmap_sem);
964 vma = mm->mmap;
965 while (vma) {
966 if ((vma->vm_flags & VM_EXECUTABLE) &&
967 vma->vm_file) {
968 audit_log_d_path(ab, "exe=",
Jan Blunck44707fd2008-02-14 19:38:33 -0800969 &vma->vm_file->f_path);
Al Viroe4951492006-03-29 20:17:10 -0500970 break;
971 }
972 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700973 }
Al Viroe4951492006-03-29 20:17:10 -0500974 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700975 }
Al Viroe4951492006-03-29 20:17:10 -0500976 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700977}
978
Amy Griffise54dc242007-03-29 18:01:04 -0400979static int audit_log_pid_context(struct audit_context *context, pid_t pid,
Eric Paris4746ec52008-01-08 10:06:53 -0500980 uid_t auid, uid_t uid, unsigned int sessionid,
981 u32 sid, char *comm)
Amy Griffise54dc242007-03-29 18:01:04 -0400982{
983 struct audit_buffer *ab;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200984 char *ctx = NULL;
Amy Griffise54dc242007-03-29 18:01:04 -0400985 u32 len;
986 int rc = 0;
987
988 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
989 if (!ab)
Eric Paris6246cca2008-01-07 14:01:18 -0500990 return rc;
Amy Griffise54dc242007-03-29 18:01:04 -0400991
Eric Paris4746ec52008-01-08 10:06:53 -0500992 audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
993 uid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200994 if (security_secid_to_secctx(sid, &ctx, &len)) {
Eric Parisc2a77802008-01-07 13:40:17 -0500995 audit_log_format(ab, " obj=(none)");
Amy Griffise54dc242007-03-29 18:01:04 -0400996 rc = 1;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +0200997 } else {
998 audit_log_format(ab, " obj=%s", ctx);
999 security_release_secctx(ctx, len);
1000 }
Eric Parisc2a77802008-01-07 13:40:17 -05001001 audit_log_format(ab, " ocomm=");
1002 audit_log_untrustedstring(ab, comm);
Amy Griffise54dc242007-03-29 18:01:04 -04001003 audit_log_end(ab);
Amy Griffise54dc242007-03-29 18:01:04 -04001004
1005 return rc;
1006}
1007
Eric Parisde6bbd12008-01-07 14:31:58 -05001008/*
1009 * to_send and len_sent accounting are very loose estimates. We aren't
1010 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1011 * within about 500 bytes (next page boundry)
1012 *
1013 * why snprintf? an int is up to 12 digits long. if we just assumed when
1014 * logging that a[%d]= was going to be 16 characters long we would be wasting
1015 * space in every audit message. In one 7500 byte message we can log up to
1016 * about 1000 min size arguments. That comes down to about 50% waste of space
1017 * if we didn't do the snprintf to find out how long arg_num_len was.
1018 */
1019static int audit_log_single_execve_arg(struct audit_context *context,
1020 struct audit_buffer **ab,
1021 int arg_num,
1022 size_t *len_sent,
1023 const char __user *p,
1024 char *buf)
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001025{
Eric Parisde6bbd12008-01-07 14:31:58 -05001026 char arg_num_len_buf[12];
1027 const char __user *tmp_p = p;
1028 /* how many digits are in arg_num? 3 is the length of a=\n */
1029 size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3;
1030 size_t len, len_left, to_send;
1031 size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
1032 unsigned int i, has_cntl = 0, too_long = 0;
1033 int ret;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001034
Eric Parisde6bbd12008-01-07 14:31:58 -05001035 /* strnlen_user includes the null we don't want to send */
1036 len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001037
Eric Parisde6bbd12008-01-07 14:31:58 -05001038 /*
1039 * We just created this mm, if we can't find the strings
1040 * we just copied into it something is _very_ wrong. Similar
1041 * for strings that are too long, we should not have created
1042 * any.
1043 */
Eric Parisb0abcfc2008-02-18 18:23:16 -05001044 if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
Eric Parisde6bbd12008-01-07 14:31:58 -05001045 WARN_ON(1);
1046 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001047 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001048 }
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001049
Eric Parisde6bbd12008-01-07 14:31:58 -05001050 /* walk the whole argument looking for non-ascii chars */
1051 do {
1052 if (len_left > MAX_EXECVE_AUDIT_LEN)
1053 to_send = MAX_EXECVE_AUDIT_LEN;
1054 else
1055 to_send = len_left;
1056 ret = copy_from_user(buf, tmp_p, to_send);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001057 /*
1058 * There is no reason for this copy to be short. We just
1059 * copied them here, and the mm hasn't been exposed to user-
1060 * space yet.
1061 */
Peter Zijlstra040b3a22007-07-28 00:55:18 +02001062 if (ret) {
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001063 WARN_ON(1);
1064 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001065 return -1;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001066 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001067 buf[to_send] = '\0';
1068 has_cntl = audit_string_contains_control(buf, to_send);
1069 if (has_cntl) {
1070 /*
1071 * hex messages get logged as 2 bytes, so we can only
1072 * send half as much in each message
1073 */
1074 max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
1075 break;
1076 }
1077 len_left -= to_send;
1078 tmp_p += to_send;
1079 } while (len_left > 0);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001080
Eric Parisde6bbd12008-01-07 14:31:58 -05001081 len_left = len;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001082
Eric Parisde6bbd12008-01-07 14:31:58 -05001083 if (len > max_execve_audit_len)
1084 too_long = 1;
1085
1086 /* rewalk the argument actually logging the message */
1087 for (i = 0; len_left > 0; i++) {
1088 int room_left;
1089
1090 if (len_left > max_execve_audit_len)
1091 to_send = max_execve_audit_len;
1092 else
1093 to_send = len_left;
1094
1095 /* do we have space left to send this argument in this ab? */
1096 room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
1097 if (has_cntl)
1098 room_left -= (to_send * 2);
1099 else
1100 room_left -= to_send;
1101 if (room_left < 0) {
1102 *len_sent = 0;
1103 audit_log_end(*ab);
1104 *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
1105 if (!*ab)
1106 return 0;
1107 }
1108
1109 /*
1110 * first record needs to say how long the original string was
1111 * so we can be sure nothing was lost.
1112 */
1113 if ((i == 0) && (too_long))
Paul Moore422b03c2008-02-27 10:39:22 -05001114 audit_log_format(*ab, "a%d_len=%zu ", arg_num,
Eric Parisde6bbd12008-01-07 14:31:58 -05001115 has_cntl ? 2*len : len);
1116
1117 /*
1118 * normally arguments are small enough to fit and we already
1119 * filled buf above when we checked for control characters
1120 * so don't bother with another copy_from_user
1121 */
1122 if (len >= max_execve_audit_len)
1123 ret = copy_from_user(buf, p, to_send);
1124 else
1125 ret = 0;
1126 if (ret) {
1127 WARN_ON(1);
1128 send_sig(SIGKILL, current, 0);
Eric Parisb0abcfc2008-02-18 18:23:16 -05001129 return -1;
Eric Parisde6bbd12008-01-07 14:31:58 -05001130 }
1131 buf[to_send] = '\0';
1132
1133 /* actually log it */
1134 audit_log_format(*ab, "a%d", arg_num);
1135 if (too_long)
1136 audit_log_format(*ab, "[%d]", i);
1137 audit_log_format(*ab, "=");
1138 if (has_cntl)
Eric Parisb556f8a2008-04-18 10:12:59 -04001139 audit_log_n_hex(*ab, buf, to_send);
Eric Parisde6bbd12008-01-07 14:31:58 -05001140 else
1141 audit_log_format(*ab, "\"%s\"", buf);
1142 audit_log_format(*ab, "\n");
1143
1144 p += to_send;
1145 len_left -= to_send;
1146 *len_sent += arg_num_len;
1147 if (has_cntl)
1148 *len_sent += to_send * 2;
1149 else
1150 *len_sent += to_send;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001151 }
Eric Parisde6bbd12008-01-07 14:31:58 -05001152 /* include the null we didn't log */
1153 return len + 1;
1154}
1155
1156static void audit_log_execve_info(struct audit_context *context,
1157 struct audit_buffer **ab,
1158 struct audit_aux_data_execve *axi)
1159{
1160 int i;
1161 size_t len, len_sent = 0;
1162 const char __user *p;
1163 char *buf;
1164
1165 if (axi->mm != current->mm)
1166 return; /* execve failed, no additional info */
1167
1168 p = (const char __user *)axi->mm->arg_start;
1169
1170 audit_log_format(*ab, "argc=%d ", axi->argc);
1171
1172 /*
1173 * we need some kernel buffer to hold the userspace args. Just
1174 * allocate one big one rather than allocating one of the right size
1175 * for every single argument inside audit_log_single_execve_arg()
1176 * should be <8k allocation so should be pretty safe.
1177 */
1178 buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
1179 if (!buf) {
1180 audit_panic("out of memory for argv string\n");
1181 return;
1182 }
1183
1184 for (i = 0; i < axi->argc; i++) {
1185 len = audit_log_single_execve_arg(context, ab, i,
1186 &len_sent, p, buf);
1187 if (len <= 0)
1188 break;
1189 p += len;
1190 }
1191 kfree(buf);
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07001192}
1193
Eric Paris851f7ff2008-11-11 21:48:14 +11001194static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
1195{
1196 int i;
1197
1198 audit_log_format(ab, " %s=", prefix);
1199 CAP_FOR_EACH_U32(i) {
1200 audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
1201 }
1202}
1203
1204static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
1205{
1206 kernel_cap_t *perm = &name->fcap.permitted;
1207 kernel_cap_t *inh = &name->fcap.inheritable;
1208 int log = 0;
1209
1210 if (!cap_isclear(*perm)) {
1211 audit_log_cap(ab, "cap_fp", perm);
1212 log = 1;
1213 }
1214 if (!cap_isclear(*inh)) {
1215 audit_log_cap(ab, "cap_fi", inh);
1216 log = 1;
1217 }
1218
1219 if (log)
1220 audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
1221}
1222
Al Viroe4951492006-03-29 20:17:10 -05001223static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001225 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +01001227 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -05001228 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Al Viroe4951492006-03-29 20:17:10 -05001230 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -04001231 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -04001232 if (!context->ppid)
1233 context->ppid = sys_getppid();
Al Viro3f2792f2006-07-16 06:43:48 -04001234 context->uid = tsk->uid;
1235 context->gid = tsk->gid;
1236 context->euid = tsk->euid;
1237 context->suid = tsk->suid;
1238 context->fsuid = tsk->fsuid;
1239 context->egid = tsk->egid;
1240 context->sgid = tsk->sgid;
1241 context->fsgid = tsk->fsgid;
1242 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -05001243
1244 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!ab)
1246 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +01001247 audit_log_format(ab, "arch=%x syscall=%d",
1248 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (context->personality != PER_LINUX)
1250 audit_log_format(ab, " per=%lx", context->personality);
1251 if (context->return_valid)
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001252 audit_log_format(ab, " success=%s exit=%ld",
2fd6f582005-04-29 16:08:28 +01001253 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
1254 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -07001255
Alan Coxdbda4c02008-10-13 10:40:53 +01001256 spin_lock_irq(&tsk->sighand->siglock);
Al Viro45d9bb02006-03-29 20:02:55 -05001257 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
1258 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -05001259 else
1260 tty = "(none)";
Alan Coxdbda4c02008-10-13 10:40:53 +01001261 spin_unlock_irq(&tsk->sighand->siglock);
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 audit_log_format(ab,
1264 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -04001265 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +01001266 " euid=%u suid=%u fsuid=%u"
Eric Paris4746ec52008-01-08 10:06:53 -05001267 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 context->argv[0],
1269 context->argv[1],
1270 context->argv[2],
1271 context->argv[3],
1272 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -04001273 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 context->pid,
Al Virobfef93a2008-01-10 04:53:18 -05001275 tsk->loginuid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 context->uid,
1277 context->gid,
1278 context->euid, context->suid, context->fsuid,
Eric Paris4746ec52008-01-08 10:06:53 -05001279 context->egid, context->sgid, context->fsgid, tty,
1280 tsk->sessionid);
Alan Coxeb84a202006-09-29 02:01:41 -07001281
Alan Coxeb84a202006-09-29 02:01:41 -07001282
Al Viroe4951492006-03-29 20:17:10 -05001283 audit_log_task_info(ab, tsk);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001284 if (context->filterkey) {
1285 audit_log_format(ab, " key=");
1286 audit_log_untrustedstring(ab, context->filterkey);
1287 } else
1288 audit_log_format(ab, " key=(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
David Woodhouse7551ced2005-05-26 12:04:57 +01001291 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +01001292
Al Viroe4951492006-03-29 20:17:10 -05001293 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (!ab)
1295 continue; /* audit_panic has been called */
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -05001298 case AUDIT_MQ_OPEN: {
1299 struct audit_aux_data_mq_open *axi = (void *)aux;
1300 audit_log_format(ab,
1301 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1302 "mq_msgsize=%ld mq_curmsgs=%ld",
1303 axi->oflag, axi->mode, axi->attr.mq_flags,
1304 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
1305 axi->attr.mq_curmsgs);
1306 break; }
1307
1308 case AUDIT_MQ_SENDRECV: {
1309 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
1310 audit_log_format(ab,
1311 "mqdes=%d msg_len=%zd msg_prio=%u "
1312 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1313 axi->mqdes, axi->msg_len, axi->msg_prio,
1314 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
1315 break; }
1316
1317 case AUDIT_MQ_NOTIFY: {
1318 struct audit_aux_data_mq_notify *axi = (void *)aux;
1319 audit_log_format(ab,
1320 "mqdes=%d sigev_signo=%d",
1321 axi->mqdes,
1322 axi->notification.sigev_signo);
1323 break; }
1324
1325 case AUDIT_MQ_GETSETATTR: {
1326 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
1327 audit_log_format(ab,
1328 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1329 "mq_curmsgs=%ld ",
1330 axi->mqdes,
1331 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
1332 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
1333 break; }
1334
Steve Grubbc0404992005-05-13 18:17:42 +01001335 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 struct audit_aux_data_ipcctl *axi = (void *)aux;
1337 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001338 "ouid=%u ogid=%u mode=%#o",
Linda Knippersac032212006-05-16 22:03:48 -04001339 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001340 if (axi->osid != 0) {
1341 char *ctx = NULL;
1342 u32 len;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001343 if (security_secid_to_secctx(
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001344 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -05001345 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001346 axi->osid);
1347 call_panic = 1;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001348 } else {
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001349 audit_log_format(ab, " obj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001350 security_release_secctx(ctx, len);
1351 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001352 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001353 break; }
1354
Steve Grubb073115d2006-04-02 17:07:33 -04001355 case AUDIT_IPC_SET_PERM: {
1356 struct audit_aux_data_ipcctl *axi = (void *)aux;
1357 audit_log_format(ab,
Steve Grubb5b9a4262007-05-29 10:38:18 -04001358 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
Steve Grubb073115d2006-04-02 17:07:33 -04001359 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -04001360 break; }
Linda Knippersac032212006-05-16 22:03:48 -04001361
Al Viro473ae302006-04-26 14:04:08 -04001362 case AUDIT_EXECVE: {
1363 struct audit_aux_data_execve *axi = (void *)aux;
Eric Parisde6bbd12008-01-07 14:31:58 -05001364 audit_log_execve_info(context, &ab, axi);
Al Viro473ae302006-04-26 14:04:08 -04001365 break; }
Steve Grubb073115d2006-04-02 17:07:33 -04001366
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001367 case AUDIT_SOCKETCALL: {
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001368 struct audit_aux_data_socketcall *axs = (void *)aux;
1369 audit_log_format(ab, "nargs=%d", axs->nargs);
1370 for (i=0; i<axs->nargs; i++)
1371 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
1372 break; }
1373
1374 case AUDIT_SOCKADDR: {
1375 struct audit_aux_data_sockaddr *axs = (void *)aux;
1376
1377 audit_log_format(ab, "saddr=");
Eric Parisb556f8a2008-04-18 10:12:59 -04001378 audit_log_n_hex(ab, axs->a, axs->len);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001379 break; }
Stephen Smalley01116102005-05-21 00:15:52 +01001380
Al Virodb349502007-02-07 01:48:00 -05001381 case AUDIT_FD_PAIR: {
1382 struct audit_aux_data_fd_pair *axs = (void *)aux;
1383 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1384 break; }
1385
Eric Paris3fc689e2008-11-11 21:48:18 +11001386 case AUDIT_BPRM_FCAPS: {
1387 struct audit_aux_data_bprm_fcaps *axs = (void *)aux;
1388 audit_log_format(ab, "fver=%x", axs->fcap_ver);
1389 audit_log_cap(ab, "fp", &axs->fcap.permitted);
1390 audit_log_cap(ab, "fi", &axs->fcap.inheritable);
1391 audit_log_format(ab, " fe=%d", axs->fcap.fE);
1392 audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted);
1393 audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable);
1394 audit_log_cap(ab, "old_pe", &axs->old_pcap.effective);
1395 audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted);
1396 audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable);
1397 audit_log_cap(ab, "new_pe", &axs->new_pcap.effective);
1398 break; }
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
Amy Griffise54dc242007-03-29 18:01:04 -04001404 for (aux = context->aux_pids; aux; aux = aux->next) {
1405 struct audit_aux_data_pids *axs = (void *)aux;
Amy Griffise54dc242007-03-29 18:01:04 -04001406
1407 for (i = 0; i < axs->pid_count; i++)
1408 if (audit_log_pid_context(context, axs->target_pid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001409 axs->target_auid[i],
1410 axs->target_uid[i],
Eric Paris4746ec52008-01-08 10:06:53 -05001411 axs->target_sessionid[i],
Eric Parisc2a77802008-01-07 13:40:17 -05001412 axs->target_sid[i],
1413 axs->target_comm[i]))
Amy Griffise54dc242007-03-29 18:01:04 -04001414 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001415 }
1416
Amy Griffise54dc242007-03-29 18:01:04 -04001417 if (context->target_pid &&
1418 audit_log_pid_context(context, context->target_pid,
Eric Parisc2a77802008-01-07 13:40:17 -05001419 context->target_auid, context->target_uid,
Eric Paris4746ec52008-01-08 10:06:53 -05001420 context->target_sessionid,
Eric Parisc2a77802008-01-07 13:40:17 -05001421 context->target_sid, context->target_comm))
Amy Griffise54dc242007-03-29 18:01:04 -04001422 call_panic = 1;
1423
Jan Blunck44707fd2008-02-14 19:38:33 -08001424 if (context->pwd.dentry && context->pwd.mnt) {
Al Viroe4951492006-03-29 20:17:10 -05001425 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001426 if (ab) {
Jan Blunck44707fd2008-02-14 19:38:33 -08001427 audit_log_d_path(ab, "cwd=", &context->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001428 audit_log_end(ab);
1429 }
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -04001432 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +00001433
Al Viroe4951492006-03-29 20:17:10 -05001434 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (!ab)
1436 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +01001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +00001439
Amy Griffis9c937dc2006-06-08 23:19:31 -04001440 if (n->name) {
1441 switch(n->name_len) {
1442 case AUDIT_NAME_FULL:
1443 /* log the full path */
1444 audit_log_format(ab, " name=");
1445 audit_log_untrustedstring(ab, n->name);
1446 break;
1447 case 0:
1448 /* name was specified as a relative path and the
1449 * directory component is the cwd */
Jan Blunck44707fd2008-02-14 19:38:33 -08001450 audit_log_d_path(ab, " name=", &context->pwd);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001451 break;
1452 default:
1453 /* log the name's directory component */
1454 audit_log_format(ab, " name=");
Eric Parisb556f8a2008-04-18 10:12:59 -04001455 audit_log_n_untrustedstring(ab, n->name,
1456 n->name_len);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001457 }
1458 } else
1459 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001460
Amy Griffis9c937dc2006-06-08 23:19:31 -04001461 if (n->ino != (unsigned long)-1) {
1462 audit_log_format(ab, " inode=%lu"
1463 " dev=%02x:%02x mode=%#o"
1464 " ouid=%u ogid=%u rdev=%02x:%02x",
1465 n->ino,
1466 MAJOR(n->dev),
1467 MINOR(n->dev),
1468 n->mode,
1469 n->uid,
1470 n->gid,
1471 MAJOR(n->rdev),
1472 MINOR(n->rdev));
1473 }
1474 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001475 char *ctx = NULL;
1476 u32 len;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001477 if (security_secid_to_secctx(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001478 n->osid, &ctx, &len)) {
1479 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001480 call_panic = 2;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001481 } else {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001482 audit_log_format(ab, " obj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001483 security_release_secctx(ctx, len);
1484 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001485 }
1486
Eric Paris851f7ff2008-11-11 21:48:14 +11001487 audit_log_fcaps(ab, n);
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 audit_log_end(ab);
1490 }
Eric Parisc0641f22008-01-07 13:49:15 -05001491
1492 /* Send end of event record to help user space know we are finished */
1493 ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
1494 if (ab)
1495 audit_log_end(ab);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001496 if (call_panic)
1497 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001500/**
1501 * audit_free - free a per-task audit context
1502 * @tsk: task whose audit context block to free
1503 *
Al Virofa84cb92006-03-29 20:30:19 -05001504 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506void audit_free(struct task_struct *tsk)
1507{
1508 struct audit_context *context;
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (likely(!context))
1512 return;
1513
1514 /* Check for system calls that do not go through the exit
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001515 * function (e.g., exit_group), then free context block.
1516 * We use GFP_ATOMIC here because we might be doing this
David Woodhousef5561962005-07-13 22:47:07 +01001517 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001518 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +01001519 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001520 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 audit_free_context(context);
1523}
1524
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001525/**
1526 * audit_syscall_entry - fill in an audit record at syscall entry
1527 * @tsk: task being audited
1528 * @arch: architecture type
1529 * @major: major syscall type (function)
1530 * @a1: additional syscall register 1
1531 * @a2: additional syscall register 2
1532 * @a3: additional syscall register 3
1533 * @a4: additional syscall register 4
1534 *
1535 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 * audit context was created when the task was created and the state or
1537 * filters demand the audit context be built. If the state from the
1538 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1539 * then the record will be written at syscall exit time (otherwise, it
1540 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001541 * be written).
1542 */
Al Viro5411be52006-03-29 20:23:36 -05001543void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 unsigned long a1, unsigned long a2,
1545 unsigned long a3, unsigned long a4)
1546{
Al Viro5411be52006-03-29 20:23:36 -05001547 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 struct audit_context *context = tsk->audit_context;
1549 enum audit_state state;
1550
Roland McGrath86a1c342008-06-23 15:37:04 -07001551 if (unlikely(!context))
1552 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001554 /*
1555 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 * calls in kernel_thread via the entry.S interface, instead of
1557 * with direct calls. (If you are porting to a new
1558 * architecture, hitting this condition can indicate that you
1559 * got the _exit/_leave calls backward in entry.S.)
1560 *
1561 * i386 no
1562 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001563 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 *
1565 * This also happens with vm86 emulation in a non-nested manner
1566 * (entries without exits), so this case must be caught.
1567 */
1568 if (context->in_syscall) {
1569 struct audit_context *newctx;
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571#if AUDIT_DEBUG
1572 printk(KERN_ERR
1573 "audit(:%d) pid=%d in syscall=%d;"
1574 " entering syscall=%d\n",
1575 context->serial, tsk->pid, context->major, major);
1576#endif
1577 newctx = audit_alloc_context(context->state);
1578 if (newctx) {
1579 newctx->previous = context;
1580 context = newctx;
1581 tsk->audit_context = newctx;
1582 } else {
1583 /* If we can't alloc a new context, the best we
1584 * can do is to leak memory (any pending putname
1585 * will be lost). The only other alternative is
1586 * to abandon auditing. */
1587 audit_zero_context(context, context->state);
1588 }
1589 }
1590 BUG_ON(context->in_syscall || context->name_count);
1591
1592 if (!audit_enabled)
1593 return;
1594
2fd6f582005-04-29 16:08:28 +01001595 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 context->major = major;
1597 context->argv[0] = a1;
1598 context->argv[1] = a2;
1599 context->argv[2] = a3;
1600 context->argv[3] = a4;
1601
1602 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001603 context->dummy = !audit_n_rules;
1604 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
David Woodhouse0f45aa12005-06-19 19:35:50 +01001605 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (likely(state == AUDIT_DISABLED))
1607 return;
1608
David Woodhousece625a82005-07-18 14:24:46 -04001609 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 context->ctime = CURRENT_TIME;
1611 context->in_syscall = 1;
1612 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
Alexander Viro419c58f2006-09-29 00:08:50 -04001613 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001616/**
1617 * audit_syscall_exit - deallocate audit context after a system call
1618 * @tsk: task being audited
1619 * @valid: success/failure flag
1620 * @return_code: syscall return value
1621 *
1622 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1624 * filtering, or because some other part of the kernel write an audit
1625 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001626 * free the names stored from getname().
1627 */
Al Viro5411be52006-03-29 20:23:36 -05001628void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Al Viro5411be52006-03-29 20:23:36 -05001630 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct audit_context *context;
1632
2fd6f582005-04-29 16:08:28 +01001633 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001636 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
David Woodhousef7056d62005-06-20 16:07:33 +01001638 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001639 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 context->in_syscall = 0;
1642 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (context->previous) {
1645 struct audit_context *new_context = context->previous;
1646 context->previous = NULL;
1647 audit_free_context(context);
1648 tsk->audit_context = new_context;
1649 } else {
1650 audit_free_names(context);
Al Viro74c3cbe2007-07-22 08:04:18 -04001651 unroll_tree_refs(context, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001653 context->aux = NULL;
1654 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001655 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001656 context->target_sid = 0;
Amy Griffis5adc8a62006-06-14 18:45:21 -04001657 kfree(context->filterkey);
1658 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 tsk->audit_context = context;
1660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Al Viro74c3cbe2007-07-22 08:04:18 -04001663static inline void handle_one(const struct inode *inode)
1664{
1665#ifdef CONFIG_AUDIT_TREE
1666 struct audit_context *context;
1667 struct audit_tree_refs *p;
1668 struct audit_chunk *chunk;
1669 int count;
1670 if (likely(list_empty(&inode->inotify_watches)))
1671 return;
1672 context = current->audit_context;
1673 p = context->trees;
1674 count = context->tree_count;
1675 rcu_read_lock();
1676 chunk = audit_tree_lookup(inode);
1677 rcu_read_unlock();
1678 if (!chunk)
1679 return;
1680 if (likely(put_tree_ref(context, chunk)))
1681 return;
1682 if (unlikely(!grow_tree_refs(context))) {
Eric Paris436c4052008-04-18 10:01:04 -04001683 printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001684 audit_set_auditable(context);
1685 audit_put_chunk(chunk);
1686 unroll_tree_refs(context, p, count);
1687 return;
1688 }
1689 put_tree_ref(context, chunk);
1690#endif
1691}
1692
1693static void handle_path(const struct dentry *dentry)
1694{
1695#ifdef CONFIG_AUDIT_TREE
1696 struct audit_context *context;
1697 struct audit_tree_refs *p;
1698 const struct dentry *d, *parent;
1699 struct audit_chunk *drop;
1700 unsigned long seq;
1701 int count;
1702
1703 context = current->audit_context;
1704 p = context->trees;
1705 count = context->tree_count;
1706retry:
1707 drop = NULL;
1708 d = dentry;
1709 rcu_read_lock();
1710 seq = read_seqbegin(&rename_lock);
1711 for(;;) {
1712 struct inode *inode = d->d_inode;
1713 if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
1714 struct audit_chunk *chunk;
1715 chunk = audit_tree_lookup(inode);
1716 if (chunk) {
1717 if (unlikely(!put_tree_ref(context, chunk))) {
1718 drop = chunk;
1719 break;
1720 }
1721 }
1722 }
1723 parent = d->d_parent;
1724 if (parent == d)
1725 break;
1726 d = parent;
1727 }
1728 if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
1729 rcu_read_unlock();
1730 if (!drop) {
1731 /* just a race with rename */
1732 unroll_tree_refs(context, p, count);
1733 goto retry;
1734 }
1735 audit_put_chunk(drop);
1736 if (grow_tree_refs(context)) {
1737 /* OK, got more space */
1738 unroll_tree_refs(context, p, count);
1739 goto retry;
1740 }
1741 /* too bad */
1742 printk(KERN_WARNING
Eric Paris436c4052008-04-18 10:01:04 -04001743 "out of memory, audit has lost a tree reference\n");
Al Viro74c3cbe2007-07-22 08:04:18 -04001744 unroll_tree_refs(context, p, count);
1745 audit_set_auditable(context);
1746 return;
1747 }
1748 rcu_read_unlock();
1749#endif
1750}
1751
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001752/**
1753 * audit_getname - add a name to the list
1754 * @name: name to add
1755 *
1756 * Add a name to the list of audit names for this context.
1757 * Called from fs/namei.c:getname().
1758 */
Al Virod8945bb52006-05-18 16:01:30 -04001759void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct audit_context *context = current->audit_context;
1762
Al Virod8945bb52006-05-18 16:01:30 -04001763 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return;
1765
1766 if (!context->in_syscall) {
1767#if AUDIT_DEBUG == 2
1768 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1769 __FILE__, __LINE__, context->serial, name);
1770 dump_stack();
1771#endif
1772 return;
1773 }
1774 BUG_ON(context->name_count >= AUDIT_NAMES);
1775 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001776 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1777 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 context->names[context->name_count].ino = (unsigned long)-1;
Amy Griffise41e8bd2007-02-13 14:14:09 -05001779 context->names[context->name_count].osid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 ++context->name_count;
Jan Blunck44707fd2008-02-14 19:38:33 -08001781 if (!context->pwd.dentry) {
David Woodhouse8f37d472005-05-27 12:17:28 +01001782 read_lock(&current->fs->lock);
Jan Blunck44707fd2008-02-14 19:38:33 -08001783 context->pwd = current->fs->pwd;
1784 path_get(&current->fs->pwd);
David Woodhouse8f37d472005-05-27 12:17:28 +01001785 read_unlock(&current->fs->lock);
1786 }
Daniel Walker9f8dbe92007-10-18 03:06:09 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001790/* audit_putname - intercept a putname request
1791 * @name: name to intercept and delay for putname
1792 *
1793 * If we have stored the name from getname in the audit context,
1794 * then we delay the putname until syscall exit.
1795 * Called from include/linux/fs.h:putname().
1796 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797void audit_putname(const char *name)
1798{
1799 struct audit_context *context = current->audit_context;
1800
1801 BUG_ON(!context);
1802 if (!context->in_syscall) {
1803#if AUDIT_DEBUG == 2
1804 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1805 __FILE__, __LINE__, context->serial, name);
1806 if (context->name_count) {
1807 int i;
1808 for (i = 0; i < context->name_count; i++)
1809 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1810 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001811 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813#endif
1814 __putname(name);
1815 }
1816#if AUDIT_DEBUG
1817 else {
1818 ++context->put_count;
1819 if (context->put_count > context->name_count) {
1820 printk(KERN_ERR "%s:%d(:%d): major=%d"
1821 " in_syscall=%d putname(%p) name_count=%d"
1822 " put_count=%d\n",
1823 __FILE__, __LINE__,
1824 context->serial, context->major,
1825 context->in_syscall, name, context->name_count,
1826 context->put_count);
1827 dump_stack();
1828 }
1829 }
1830#endif
1831}
1832
Amy Griffis5712e882007-02-13 14:15:22 -05001833static int audit_inc_name_count(struct audit_context *context,
1834 const struct inode *inode)
1835{
1836 if (context->name_count >= AUDIT_NAMES) {
1837 if (inode)
1838 printk(KERN_DEBUG "name_count maxed, losing inode data: "
Eric Paris436c4052008-04-18 10:01:04 -04001839 "dev=%02x:%02x, inode=%lu\n",
Amy Griffis5712e882007-02-13 14:15:22 -05001840 MAJOR(inode->i_sb->s_dev),
1841 MINOR(inode->i_sb->s_dev),
1842 inode->i_ino);
1843
1844 else
Eric Paris436c4052008-04-18 10:01:04 -04001845 printk(KERN_DEBUG "name_count maxed, losing inode data\n");
Amy Griffis5712e882007-02-13 14:15:22 -05001846 return 1;
1847 }
1848 context->name_count++;
1849#if AUDIT_DEBUG
1850 context->ino_count++;
1851#endif
1852 return 0;
1853}
1854
Eric Paris851f7ff2008-11-11 21:48:14 +11001855
1856static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
1857{
1858 struct cpu_vfs_cap_data caps;
1859 int rc;
1860
1861 memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
1862 memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
1863 name->fcap.fE = 0;
1864 name->fcap_ver = 0;
1865
1866 if (!dentry)
1867 return 0;
1868
1869 rc = get_vfs_caps_from_disk(dentry, &caps);
1870 if (rc)
1871 return rc;
1872
1873 name->fcap.permitted = caps.permitted;
1874 name->fcap.inheritable = caps.inheritable;
1875 name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
1876 name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
1877
1878 return 0;
1879}
1880
1881
Amy Griffis3e2efce2006-07-13 13:16:02 -04001882/* Copy inode data into an audit_names. */
Eric Paris851f7ff2008-11-11 21:48:14 +11001883static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
1884 const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001885{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001886 name->ino = inode->i_ino;
1887 name->dev = inode->i_sb->s_dev;
1888 name->mode = inode->i_mode;
1889 name->uid = inode->i_uid;
1890 name->gid = inode->i_gid;
1891 name->rdev = inode->i_rdev;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001892 security_inode_getsecid(inode, &name->osid);
Eric Paris851f7ff2008-11-11 21:48:14 +11001893 audit_copy_fcaps(name, dentry);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001894}
1895
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001896/**
1897 * audit_inode - store the inode and device from a lookup
1898 * @name: name being audited
Randy Dunlap481968f2007-10-21 20:59:53 -07001899 * @dentry: dentry being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001900 *
1901 * Called from fs/namei.c:path_lookup().
1902 */
Al Viro5a190ae2007-06-07 12:19:32 -04001903void __audit_inode(const char *name, const struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 int idx;
1906 struct audit_context *context = current->audit_context;
Al Viro74c3cbe2007-07-22 08:04:18 -04001907 const struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 if (!context->in_syscall)
1910 return;
1911 if (context->name_count
1912 && context->names[context->name_count-1].name
1913 && context->names[context->name_count-1].name == name)
1914 idx = context->name_count - 1;
1915 else if (context->name_count > 1
1916 && context->names[context->name_count-2].name
1917 && context->names[context->name_count-2].name == name)
1918 idx = context->name_count - 2;
1919 else {
1920 /* FIXME: how much do we care about inodes that have no
1921 * associated name? */
Amy Griffis5712e882007-02-13 14:15:22 -05001922 if (audit_inc_name_count(context, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001924 idx = context->name_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 context->names[idx].name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001927 handle_path(dentry);
Eric Paris851f7ff2008-11-11 21:48:14 +11001928 audit_copy_inode(&context->names[idx], dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Amy Griffis73241cc2005-11-03 16:00:25 +00001931/**
1932 * audit_inode_child - collect inode info for created/removed objects
1933 * @dname: inode's dentry name
Randy Dunlap481968f2007-10-21 20:59:53 -07001934 * @dentry: dentry being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04001935 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00001936 *
1937 * For syscalls that create or remove filesystem objects, audit_inode
1938 * can only collect information for the filesystem object's parent.
1939 * This call updates the audit context with the child's information.
1940 * Syscalls that create a new filesystem object must be hooked after
1941 * the object is created. Syscalls that remove a filesystem object
1942 * must be hooked prior, in order to capture the target inode during
1943 * unsuccessful attempts.
1944 */
Al Viro5a190ae2007-06-07 12:19:32 -04001945void __audit_inode_child(const char *dname, const struct dentry *dentry,
Amy Griffis73d3ec52006-07-13 13:16:39 -04001946 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00001947{
1948 int idx;
1949 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05001950 const char *found_parent = NULL, *found_child = NULL;
Al Viro5a190ae2007-06-07 12:19:32 -04001951 const struct inode *inode = dentry->d_inode;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001952 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00001953
1954 if (!context->in_syscall)
1955 return;
1956
Al Viro74c3cbe2007-07-22 08:04:18 -04001957 if (inode)
1958 handle_one(inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001959 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001960 if (!dname)
Amy Griffis5712e882007-02-13 14:15:22 -05001961 goto add_names;
Amy Griffis73241cc2005-11-03 16:00:25 +00001962
Amy Griffis5712e882007-02-13 14:15:22 -05001963 /* parent is more likely, look for it first */
1964 for (idx = 0; idx < context->name_count; idx++) {
1965 struct audit_names *n = &context->names[idx];
Amy Griffis73241cc2005-11-03 16:00:25 +00001966
Amy Griffis5712e882007-02-13 14:15:22 -05001967 if (!n->name)
1968 continue;
1969
1970 if (n->ino == parent->i_ino &&
1971 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1972 n->name_len = dirlen; /* update parent data in place */
1973 found_parent = n->name;
1974 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001975 }
Steve Grubbac9910c2006-09-28 14:31:32 -04001976 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001977
Amy Griffis5712e882007-02-13 14:15:22 -05001978 /* no matching parent, look for matching child */
1979 for (idx = 0; idx < context->name_count; idx++) {
1980 struct audit_names *n = &context->names[idx];
Amy Griffis73d3ec52006-07-13 13:16:39 -04001981
Amy Griffis5712e882007-02-13 14:15:22 -05001982 if (!n->name)
1983 continue;
1984
1985 /* strcmp() is the more likely scenario */
1986 if (!strcmp(dname, n->name) ||
1987 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1988 if (inode)
Eric Paris851f7ff2008-11-11 21:48:14 +11001989 audit_copy_inode(n, NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05001990 else
1991 n->ino = (unsigned long)-1;
1992 found_child = n->name;
1993 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04001994 }
Amy Griffis5712e882007-02-13 14:15:22 -05001995 }
1996
1997add_names:
1998 if (!found_parent) {
1999 if (audit_inc_name_count(context, parent))
2000 return;
2001 idx = context->name_count - 1;
2002 context->names[idx].name = NULL;
Eric Paris851f7ff2008-11-11 21:48:14 +11002003 audit_copy_inode(&context->names[idx], NULL, parent);
Amy Griffis73d3ec52006-07-13 13:16:39 -04002004 }
Amy Griffis5712e882007-02-13 14:15:22 -05002005
2006 if (!found_child) {
2007 if (audit_inc_name_count(context, inode))
2008 return;
2009 idx = context->name_count - 1;
2010
2011 /* Re-use the name belonging to the slot for a matching parent
2012 * directory. All names for this context are relinquished in
2013 * audit_free_names() */
2014 if (found_parent) {
2015 context->names[idx].name = found_parent;
2016 context->names[idx].name_len = AUDIT_NAME_FULL;
2017 /* don't call __putname() */
2018 context->names[idx].name_put = 0;
2019 } else {
2020 context->names[idx].name = NULL;
2021 }
2022
2023 if (inode)
Eric Paris851f7ff2008-11-11 21:48:14 +11002024 audit_copy_inode(&context->names[idx], NULL, inode);
Amy Griffis5712e882007-02-13 14:15:22 -05002025 else
2026 context->names[idx].ino = (unsigned long)-1;
2027 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04002028}
Trond Myklebust50e437d2007-06-07 22:44:34 -04002029EXPORT_SYMBOL_GPL(__audit_inode_child);
Amy Griffis3e2efce2006-07-13 13:16:02 -04002030
2031/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002032 * auditsc_get_stamp - get local copies of audit_context values
2033 * @ctx: audit_context for the task
2034 * @t: timespec to store time recorded in the audit_context
2035 * @serial: serial value that is recorded in the audit_context
2036 *
2037 * Also sets the context as auditable.
2038 */
David Woodhousebfb44962005-05-21 21:08:09 +01002039void auditsc_get_stamp(struct audit_context *ctx,
2040 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
David Woodhousece625a82005-07-18 14:24:46 -04002042 if (!ctx->serial)
2043 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01002044 t->tv_sec = ctx->ctime.tv_sec;
2045 t->tv_nsec = ctx->ctime.tv_nsec;
2046 *serial = ctx->serial;
2047 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Eric Paris4746ec52008-01-08 10:06:53 -05002050/* global counter which is incremented every time something logs in */
2051static atomic_t session_id = ATOMIC_INIT(0);
2052
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002053/**
2054 * audit_set_loginuid - set a task's audit_context loginuid
2055 * @task: task whose audit context is being modified
2056 * @loginuid: loginuid value
2057 *
2058 * Returns 0.
2059 *
2060 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2061 */
Steve Grubb456be6c2005-04-29 17:30:07 +01002062int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Eric Paris4746ec52008-01-08 10:06:53 -05002064 unsigned int sessionid = atomic_inc_return(&session_id);
Steve Grubb41757102006-06-12 07:48:28 -04002065 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01002066
Al Virobfef93a2008-01-10 04:53:18 -05002067 if (context && context->in_syscall) {
2068 struct audit_buffer *ab;
Steve Grubb41757102006-06-12 07:48:28 -04002069
Al Virobfef93a2008-01-10 04:53:18 -05002070 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
2071 if (ab) {
2072 audit_log_format(ab, "login pid=%d uid=%u "
Eric Paris4746ec52008-01-08 10:06:53 -05002073 "old auid=%u new auid=%u"
2074 " old ses=%u new ses=%u",
Al Virobfef93a2008-01-10 04:53:18 -05002075 task->pid, task->uid,
Eric Paris4746ec52008-01-08 10:06:53 -05002076 task->loginuid, loginuid,
2077 task->sessionid, sessionid);
Al Virobfef93a2008-01-10 04:53:18 -05002078 audit_log_end(ab);
Steve Grubbc0404992005-05-13 18:17:42 +01002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
Eric Paris4746ec52008-01-08 10:06:53 -05002081 task->sessionid = sessionid;
Al Virobfef93a2008-01-10 04:53:18 -05002082 task->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return 0;
2084}
2085
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002086/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05002087 * __audit_mq_open - record audit data for a POSIX MQ open
2088 * @oflag: open flag
2089 * @mode: mode bits
2090 * @u_attr: queue attributes
2091 *
2092 * Returns 0 for success or NULL context or < 0 on error.
2093 */
2094int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
2095{
2096 struct audit_aux_data_mq_open *ax;
2097 struct audit_context *context = current->audit_context;
2098
2099 if (!audit_enabled)
2100 return 0;
2101
2102 if (likely(!context))
2103 return 0;
2104
2105 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2106 if (!ax)
2107 return -ENOMEM;
2108
2109 if (u_attr != NULL) {
2110 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
2111 kfree(ax);
2112 return -EFAULT;
2113 }
2114 } else
2115 memset(&ax->attr, 0, sizeof(ax->attr));
2116
2117 ax->oflag = oflag;
2118 ax->mode = mode;
2119
2120 ax->d.type = AUDIT_MQ_OPEN;
2121 ax->d.next = context->aux;
2122 context->aux = (void *)ax;
2123 return 0;
2124}
2125
2126/**
2127 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
2128 * @mqdes: MQ descriptor
2129 * @msg_len: Message length
2130 * @msg_prio: Message priority
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07002131 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002132 *
2133 * Returns 0 for success or NULL context or < 0 on error.
2134 */
2135int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
2136 const struct timespec __user *u_abs_timeout)
2137{
2138 struct audit_aux_data_mq_sendrecv *ax;
2139 struct audit_context *context = current->audit_context;
2140
2141 if (!audit_enabled)
2142 return 0;
2143
2144 if (likely(!context))
2145 return 0;
2146
2147 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2148 if (!ax)
2149 return -ENOMEM;
2150
2151 if (u_abs_timeout != NULL) {
2152 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2153 kfree(ax);
2154 return -EFAULT;
2155 }
2156 } else
2157 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2158
2159 ax->mqdes = mqdes;
2160 ax->msg_len = msg_len;
2161 ax->msg_prio = msg_prio;
2162
2163 ax->d.type = AUDIT_MQ_SENDRECV;
2164 ax->d.next = context->aux;
2165 context->aux = (void *)ax;
2166 return 0;
2167}
2168
2169/**
2170 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
2171 * @mqdes: MQ descriptor
2172 * @msg_len: Message length
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07002173 * @u_msg_prio: Message priority
2174 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05002175 *
2176 * Returns 0 for success or NULL context or < 0 on error.
2177 */
2178int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
2179 unsigned int __user *u_msg_prio,
2180 const struct timespec __user *u_abs_timeout)
2181{
2182 struct audit_aux_data_mq_sendrecv *ax;
2183 struct audit_context *context = current->audit_context;
2184
2185 if (!audit_enabled)
2186 return 0;
2187
2188 if (likely(!context))
2189 return 0;
2190
2191 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2192 if (!ax)
2193 return -ENOMEM;
2194
2195 if (u_msg_prio != NULL) {
2196 if (get_user(ax->msg_prio, u_msg_prio)) {
2197 kfree(ax);
2198 return -EFAULT;
2199 }
2200 } else
2201 ax->msg_prio = 0;
2202
2203 if (u_abs_timeout != NULL) {
2204 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
2205 kfree(ax);
2206 return -EFAULT;
2207 }
2208 } else
2209 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
2210
2211 ax->mqdes = mqdes;
2212 ax->msg_len = msg_len;
2213
2214 ax->d.type = AUDIT_MQ_SENDRECV;
2215 ax->d.next = context->aux;
2216 context->aux = (void *)ax;
2217 return 0;
2218}
2219
2220/**
2221 * __audit_mq_notify - record audit data for a POSIX MQ notify
2222 * @mqdes: MQ descriptor
2223 * @u_notification: Notification event
2224 *
2225 * Returns 0 for success or NULL context or < 0 on error.
2226 */
2227
2228int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
2229{
2230 struct audit_aux_data_mq_notify *ax;
2231 struct audit_context *context = current->audit_context;
2232
2233 if (!audit_enabled)
2234 return 0;
2235
2236 if (likely(!context))
2237 return 0;
2238
2239 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2240 if (!ax)
2241 return -ENOMEM;
2242
2243 if (u_notification != NULL) {
2244 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
2245 kfree(ax);
2246 return -EFAULT;
2247 }
2248 } else
2249 memset(&ax->notification, 0, sizeof(ax->notification));
2250
2251 ax->mqdes = mqdes;
2252
2253 ax->d.type = AUDIT_MQ_NOTIFY;
2254 ax->d.next = context->aux;
2255 context->aux = (void *)ax;
2256 return 0;
2257}
2258
2259/**
2260 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2261 * @mqdes: MQ descriptor
2262 * @mqstat: MQ flags
2263 *
2264 * Returns 0 for success or NULL context or < 0 on error.
2265 */
2266int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
2267{
2268 struct audit_aux_data_mq_getsetattr *ax;
2269 struct audit_context *context = current->audit_context;
2270
2271 if (!audit_enabled)
2272 return 0;
2273
2274 if (likely(!context))
2275 return 0;
2276
2277 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2278 if (!ax)
2279 return -ENOMEM;
2280
2281 ax->mqdes = mqdes;
2282 ax->mqstat = *mqstat;
2283
2284 ax->d.type = AUDIT_MQ_GETSETATTR;
2285 ax->d.next = context->aux;
2286 context->aux = (void *)ax;
2287 return 0;
2288}
2289
2290/**
Steve Grubb073115d2006-04-02 17:07:33 -04002291 * audit_ipc_obj - record audit data for ipc object
2292 * @ipcp: ipc permissions
2293 *
2294 * Returns 0 for success or NULL context or < 0 on error.
2295 */
Al Virod8945bb52006-05-18 16:01:30 -04002296int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04002297{
2298 struct audit_aux_data_ipcctl *ax;
2299 struct audit_context *context = current->audit_context;
2300
Steve Grubb073115d2006-04-02 17:07:33 -04002301 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
2302 if (!ax)
2303 return -ENOMEM;
2304
2305 ax->uid = ipcp->uid;
2306 ax->gid = ipcp->gid;
2307 ax->mode = ipcp->mode;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002308 security_ipc_getsecid(ipcp, &ax->osid);
Steve Grubb073115d2006-04-02 17:07:33 -04002309 ax->d.type = AUDIT_IPC;
2310 ax->d.next = context->aux;
2311 context->aux = (void *)ax;
2312 return 0;
2313}
2314
2315/**
2316 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002317 * @qbytes: msgq bytes
2318 * @uid: msgq user id
2319 * @gid: msgq group id
2320 * @mode: msgq mode (permissions)
2321 *
2322 * Returns 0 for success or NULL context or < 0 on error.
2323 */
Al Virod8945bb52006-05-18 16:01:30 -04002324int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
2326 struct audit_aux_data_ipcctl *ax;
2327 struct audit_context *context = current->audit_context;
2328
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002329 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (!ax)
2331 return -ENOMEM;
2332
2333 ax->qbytes = qbytes;
2334 ax->uid = uid;
2335 ax->gid = gid;
2336 ax->mode = mode;
2337
Steve Grubb073115d2006-04-02 17:07:33 -04002338 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 ax->d.next = context->aux;
2340 context->aux = (void *)ax;
2341 return 0;
2342}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002343
Al Viro473ae302006-04-26 14:04:08 -04002344int audit_bprm(struct linux_binprm *bprm)
2345{
2346 struct audit_aux_data_execve *ax;
2347 struct audit_context *context = current->audit_context;
Al Viro473ae302006-04-26 14:04:08 -04002348
Al Viro5ac3a9c2006-07-16 06:38:45 -04002349 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04002350 return 0;
2351
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002352 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
Al Viro473ae302006-04-26 14:04:08 -04002353 if (!ax)
2354 return -ENOMEM;
2355
2356 ax->argc = bprm->argc;
2357 ax->envc = bprm->envc;
Peter Zijlstrabdf4c482007-07-19 01:48:15 -07002358 ax->mm = bprm->mm;
Al Viro473ae302006-04-26 14:04:08 -04002359 ax->d.type = AUDIT_EXECVE;
2360 ax->d.next = context->aux;
2361 context->aux = (void *)ax;
2362 return 0;
2363}
2364
2365
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002366/**
2367 * audit_socketcall - record audit data for sys_socketcall
2368 * @nargs: number of args
2369 * @args: args array
2370 *
2371 * Returns 0 for success or NULL context or < 0 on error.
2372 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002373int audit_socketcall(int nargs, unsigned long *args)
2374{
2375 struct audit_aux_data_socketcall *ax;
2376 struct audit_context *context = current->audit_context;
2377
Al Viro5ac3a9c2006-07-16 06:38:45 -04002378 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002379 return 0;
2380
2381 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
2382 if (!ax)
2383 return -ENOMEM;
2384
2385 ax->nargs = nargs;
2386 memcpy(ax->args, args, nargs * sizeof(unsigned long));
2387
2388 ax->d.type = AUDIT_SOCKETCALL;
2389 ax->d.next = context->aux;
2390 context->aux = (void *)ax;
2391 return 0;
2392}
2393
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002394/**
Al Virodb349502007-02-07 01:48:00 -05002395 * __audit_fd_pair - record audit data for pipe and socketpair
2396 * @fd1: the first file descriptor
2397 * @fd2: the second file descriptor
2398 *
2399 * Returns 0 for success or NULL context or < 0 on error.
2400 */
2401int __audit_fd_pair(int fd1, int fd2)
2402{
2403 struct audit_context *context = current->audit_context;
2404 struct audit_aux_data_fd_pair *ax;
2405
2406 if (likely(!context)) {
2407 return 0;
2408 }
2409
2410 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2411 if (!ax) {
2412 return -ENOMEM;
2413 }
2414
2415 ax->fd[0] = fd1;
2416 ax->fd[1] = fd2;
2417
2418 ax->d.type = AUDIT_FD_PAIR;
2419 ax->d.next = context->aux;
2420 context->aux = (void *)ax;
2421 return 0;
2422}
2423
2424/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002425 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2426 * @len: data length in user space
2427 * @a: data address in kernel space
2428 *
2429 * Returns 0 for success or NULL context or < 0 on error.
2430 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002431int audit_sockaddr(int len, void *a)
2432{
2433 struct audit_aux_data_sockaddr *ax;
2434 struct audit_context *context = current->audit_context;
2435
Al Viro5ac3a9c2006-07-16 06:38:45 -04002436 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01002437 return 0;
2438
2439 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
2440 if (!ax)
2441 return -ENOMEM;
2442
2443 ax->len = len;
2444 memcpy(ax->a, a, len);
2445
2446 ax->d.type = AUDIT_SOCKADDR;
2447 ax->d.next = context->aux;
2448 context->aux = (void *)ax;
2449 return 0;
2450}
2451
Al Viroa5cb0132007-03-20 13:58:35 -04002452void __audit_ptrace(struct task_struct *t)
2453{
2454 struct audit_context *context = current->audit_context;
2455
2456 context->target_pid = t->pid;
Eric Parisc2a77802008-01-07 13:40:17 -05002457 context->target_auid = audit_get_loginuid(t);
2458 context->target_uid = t->uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002459 context->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002460 security_task_getsecid(t, &context->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002461 memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
Al Viroa5cb0132007-03-20 13:58:35 -04002462}
2463
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002464/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07002465 * audit_signal_info - record signal info for shutting down audit subsystem
2466 * @sig: signal value
2467 * @t: task being signaled
2468 *
2469 * If the audit subsystem is being terminated, record the task (pid)
2470 * and uid that is doing that.
2471 */
Amy Griffise54dc242007-03-29 18:01:04 -04002472int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002473{
Amy Griffise54dc242007-03-29 18:01:04 -04002474 struct audit_aux_data_pids *axp;
2475 struct task_struct *tsk = current;
2476 struct audit_context *ctx = tsk->audit_context;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002477
Al Viro175fc482007-08-08 00:01:46 +01002478 if (audit_pid && t->tgid == audit_pid) {
Eric Parisee1d3152008-07-07 10:49:45 -04002479 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
Al Viro175fc482007-08-08 00:01:46 +01002480 audit_sig_pid = tsk->pid;
Al Virobfef93a2008-01-10 04:53:18 -05002481 if (tsk->loginuid != -1)
2482 audit_sig_uid = tsk->loginuid;
Al Viro175fc482007-08-08 00:01:46 +01002483 else
2484 audit_sig_uid = tsk->uid;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002485 security_task_getsecid(tsk, &audit_sig_sid);
Al Viro175fc482007-08-08 00:01:46 +01002486 }
2487 if (!audit_signals || audit_dummy_context())
2488 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002489 }
Amy Griffise54dc242007-03-29 18:01:04 -04002490
Amy Griffise54dc242007-03-29 18:01:04 -04002491 /* optimize the common case by putting first signal recipient directly
2492 * in audit_context */
2493 if (!ctx->target_pid) {
2494 ctx->target_pid = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002495 ctx->target_auid = audit_get_loginuid(t);
2496 ctx->target_uid = t->uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002497 ctx->target_sessionid = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002498 security_task_getsecid(t, &ctx->target_sid);
Eric Parisc2a77802008-01-07 13:40:17 -05002499 memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002500 return 0;
2501 }
2502
2503 axp = (void *)ctx->aux_pids;
2504 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2505 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2506 if (!axp)
2507 return -ENOMEM;
2508
2509 axp->d.type = AUDIT_OBJ_PID;
2510 axp->d.next = ctx->aux_pids;
2511 ctx->aux_pids = (void *)axp;
2512 }
Adrian Bunk88ae7042007-08-22 14:01:05 -07002513 BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
Amy Griffise54dc242007-03-29 18:01:04 -04002514
2515 axp->target_pid[axp->pid_count] = t->tgid;
Eric Parisc2a77802008-01-07 13:40:17 -05002516 axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
2517 axp->target_uid[axp->pid_count] = t->uid;
Eric Paris4746ec52008-01-08 10:06:53 -05002518 axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002519 security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
Eric Parisc2a77802008-01-07 13:40:17 -05002520 memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
Amy Griffise54dc242007-03-29 18:01:04 -04002521 axp->pid_count++;
2522
2523 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002524}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002525
2526/**
Eric Paris3fc689e2008-11-11 21:48:18 +11002527 * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps
2528 * @bprm pointer to the bprm being processed
2529 * @caps the caps read from the disk
2530 *
2531 * Simply check if the proc already has the caps given by the file and if not
2532 * store the priv escalation info for later auditing at the end of the syscall
2533 *
2534 * this can fail and we don't care. See the note in audit.h for
2535 * audit_log_bprm_fcaps() for my explaination....
2536 *
2537 * -Eric
2538 */
2539void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_cap_t *pE)
2540{
2541 struct audit_aux_data_bprm_fcaps *ax;
2542 struct audit_context *context = current->audit_context;
2543 struct cpu_vfs_cap_data vcaps;
2544 struct dentry *dentry;
2545
2546 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
2547 if (!ax)
2548 return;
2549
2550 ax->d.type = AUDIT_BPRM_FCAPS;
2551 ax->d.next = context->aux;
2552 context->aux = (void *)ax;
2553
2554 dentry = dget(bprm->file->f_dentry);
2555 get_vfs_caps_from_disk(dentry, &vcaps);
2556 dput(dentry);
2557
2558 ax->fcap.permitted = vcaps.permitted;
2559 ax->fcap.inheritable = vcaps.inheritable;
2560 ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
2561 ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
2562
2563 ax->old_pcap.permitted = *pP;
2564 ax->old_pcap.inheritable = current->cap_inheritable;
2565 ax->old_pcap.effective = *pE;
2566
2567 ax->new_pcap.permitted = current->cap_permitted;
2568 ax->new_pcap.inheritable = current->cap_inheritable;
2569 ax->new_pcap.effective = current->cap_effective;
2570}
2571
2572/**
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002573 * audit_core_dumps - record information about processes that end abnormally
Henrik Kretzschmar6d9525b2007-07-15 23:41:10 -07002574 * @signr: signal value
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002575 *
2576 * If a process ends with a core dump, something fishy is going on and we
2577 * should record the event for investigation.
2578 */
2579void audit_core_dumps(long signr)
2580{
2581 struct audit_buffer *ab;
2582 u32 sid;
Eric Paris4746ec52008-01-08 10:06:53 -05002583 uid_t auid = audit_get_loginuid(current);
2584 unsigned int sessionid = audit_get_sessionid(current);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002585
2586 if (!audit_enabled)
2587 return;
2588
2589 if (signr == SIGQUIT) /* don't care for those */
2590 return;
2591
2592 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
Eric Paris4746ec52008-01-08 10:06:53 -05002593 audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
2594 auid, current->uid, current->gid, sessionid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002595 security_task_getsecid(current, &sid);
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002596 if (sid) {
2597 char *ctx = NULL;
2598 u32 len;
2599
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002600 if (security_secid_to_secctx(sid, &ctx, &len))
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002601 audit_log_format(ab, " ssid=%u", sid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002602 else {
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002603 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02002604 security_release_secctx(ctx, len);
2605 }
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002606 }
2607 audit_log_format(ab, " pid=%d comm=", current->pid);
2608 audit_log_untrustedstring(ab, current->comm);
2609 audit_log_format(ab, " sig=%ld", signr);
2610 audit_log_end(ab);
2611}