blob: 174a3f6248928e5dc14462b1d37b6aeaee252176 [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 <asm/types.h>
49#include <linux/fs.h>
50#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/mm.h>
52#include <linux/module.h>
Stephen Smalley01116102005-05-21 00:15:52 +010053#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010054#include <linux/socket.h>
George C. Wilson20ca73b2006-05-24 16:09:55 -050055#include <linux/mqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/audit.h>
57#include <linux/personality.h>
58#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010059#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010060#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000062#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000063#include <linux/list.h>
Steve Grubba6c043a2006-01-01 14:07:00 -050064#include <linux/tty.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060065#include <linux/selinux.h>
Al Viro473ae302006-04-26 14:04:08 -040066#include <linux/binfmts.h>
Al Virof46038f2006-05-06 08:22:52 -040067#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
David Woodhousefe7752b2005-12-15 18:33:52 +000069#include "audit.h"
70
71extern struct list_head audit_filter_list[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/* No syscall auditing will take place unless audit_enabled != 0. */
74extern int audit_enabled;
75
76/* AUDIT_NAMES is the number of slots we reserve in the audit_context
77 * for saving names from getname(). */
78#define AUDIT_NAMES 20
79
80/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
81 * audit_context from being used for nameless inodes from
82 * path_lookup. */
83#define AUDIT_NAMES_RESERVED 7
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* When fs/namei.c:getname() is called, we store the pointer in name and
86 * we don't let putname() free it (instead we free all of the saved
87 * pointers at syscall exit time).
88 *
89 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
90struct audit_names {
91 const char *name;
92 unsigned long ino;
Amy Griffis73241cc2005-11-03 16:00:25 +000093 unsigned long pino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 dev_t dev;
95 umode_t mode;
96 uid_t uid;
97 gid_t gid;
98 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -040099 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102struct audit_aux_data {
103 struct audit_aux_data *next;
104 int type;
105};
106
107#define AUDIT_AUX_IPCPERM 0
108
George C. Wilson20ca73b2006-05-24 16:09:55 -0500109struct audit_aux_data_mq_open {
110 struct audit_aux_data d;
111 int oflag;
112 mode_t mode;
113 struct mq_attr attr;
114};
115
116struct audit_aux_data_mq_sendrecv {
117 struct audit_aux_data d;
118 mqd_t mqdes;
119 size_t msg_len;
120 unsigned int msg_prio;
121 struct timespec abs_timeout;
122};
123
124struct audit_aux_data_mq_notify {
125 struct audit_aux_data d;
126 mqd_t mqdes;
127 struct sigevent notification;
128};
129
130struct audit_aux_data_mq_getsetattr {
131 struct audit_aux_data d;
132 mqd_t mqdes;
133 struct mq_attr mqstat;
134};
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136struct audit_aux_data_ipcctl {
137 struct audit_aux_data d;
138 struct ipc_perm p;
139 unsigned long qbytes;
140 uid_t uid;
141 gid_t gid;
142 mode_t mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500143 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Al Viro473ae302006-04-26 14:04:08 -0400146struct audit_aux_data_execve {
147 struct audit_aux_data d;
148 int argc;
149 int envc;
150 char mem[0];
151};
152
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100153struct audit_aux_data_socketcall {
154 struct audit_aux_data d;
155 int nargs;
156 unsigned long args[0];
157};
158
159struct audit_aux_data_sockaddr {
160 struct audit_aux_data d;
161 int len;
162 char a[0];
163};
164
Stephen Smalley01116102005-05-21 00:15:52 +0100165struct audit_aux_data_path {
166 struct audit_aux_data d;
167 struct dentry *dentry;
168 struct vfsmount *mnt;
169};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/* The per-task audit context. */
172struct audit_context {
173 int in_syscall; /* 1 if task is in a syscall */
174 enum audit_state state;
175 unsigned int serial; /* serial number for record */
176 struct timespec ctime; /* time of syscall entry */
177 uid_t loginuid; /* login uid (identity) */
178 int major; /* syscall number */
179 unsigned long argv[4]; /* syscall arguments */
180 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100181 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int auditable; /* 1 if record should be written */
183 int name_count;
184 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100185 struct dentry * pwd;
186 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 struct audit_context *previous; /* For nested syscalls */
188 struct audit_aux_data *aux;
189
190 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400191 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 uid_t uid, euid, suid, fsuid;
193 gid_t gid, egid, sgid, fsgid;
194 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100195 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197#if AUDIT_DEBUG
198 int put_count;
199 int ino_count;
200#endif
201};
202
Amy Griffisf368c07d2006-04-07 16:55:56 -0400203/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/* Compare a task_struct with an audit_rule. Return 1 on match, 0
205 * otherwise. */
206static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500207 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400209 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 enum audit_state *state)
211{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400212 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600213 u32 sid;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500216 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int result = 0;
218
Amy Griffis93315ed2006-02-07 12:05:27 -0500219 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500221 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
Al Viro3c662512006-05-06 08:26:27 -0400223 case AUDIT_PPID:
224 if (ctx)
225 result = audit_comparator(ctx->ppid, f->op, f->val);
226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500228 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
230 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500231 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 break;
233 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500234 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
236 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500237 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
239 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500240 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 break;
242 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500243 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 break;
245 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500246 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 break;
248 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500249 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500252 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 break;
2fd6f582005-04-29 16:08:28 +0100254 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000255 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500256 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100257 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 case AUDIT_EXIT:
260 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500261 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 break;
263 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100264 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500265 if (f->val)
266 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100267 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500268 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
271 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400272 if (name)
273 result = audit_comparator(MAJOR(name->dev),
274 f->op, f->val);
275 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500277 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 ++result;
279 break;
280 }
281 }
282 }
283 break;
284 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400285 if (name)
286 result = audit_comparator(MINOR(name->dev),
287 f->op, f->val);
288 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500290 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 ++result;
292 break;
293 }
294 }
295 }
296 break;
297 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400298 if (name)
299 result = (name->ino == f->val ||
300 name->pino == f->val);
301 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500303 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
304 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 ++result;
306 break;
307 }
308 }
309 }
310 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400311 case AUDIT_WATCH:
312 if (name && rule->watch->ino != (unsigned long)-1)
313 result = (name->dev == rule->watch->dev &&
314 (name->ino == rule->watch->ino ||
315 name->pino == rule->watch->ino));
316 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 case AUDIT_LOGINUID:
318 result = 0;
319 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500320 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 break;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600322 case AUDIT_SE_USER:
323 case AUDIT_SE_ROLE:
324 case AUDIT_SE_TYPE:
325 case AUDIT_SE_SEN:
326 case AUDIT_SE_CLR:
327 /* NOTE: this may return negative values indicating
328 a temporary error. We simply treat this as a
329 match for now to avoid losing information that
330 may be wanted. An error message will also be
331 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400332 if (f->se_rule) {
333 if (need_sid) {
334 selinux_task_ctxid(tsk, &sid);
335 need_sid = 0;
336 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600337 result = selinux_audit_rule_match(sid, f->type,
338 f->op,
339 f->se_rule,
340 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400341 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 case AUDIT_ARG0:
344 case AUDIT_ARG1:
345 case AUDIT_ARG2:
346 case AUDIT_ARG3:
347 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500348 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350 }
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!result)
353 return 0;
354 }
355 switch (rule->action) {
356 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
358 }
359 return 1;
360}
361
362/* At process creation time, we can determine if system-call auditing is
363 * completely disabled for this task. Since we only have the task
364 * structure at this point, we can only check uid and gid.
365 */
366static enum audit_state audit_filter_task(struct task_struct *tsk)
367{
368 struct audit_entry *e;
369 enum audit_state state;
370
371 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100372 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400373 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 rcu_read_unlock();
375 return state;
376 }
377 }
378 rcu_read_unlock();
379 return AUDIT_BUILD_CONTEXT;
380}
381
382/* At syscall entry and exit time, this filter is called if the
383 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100384 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700385 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
387static enum audit_state audit_filter_syscall(struct task_struct *tsk,
388 struct audit_context *ctx,
389 struct list_head *list)
390{
391 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100392 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David Woodhouse351bb722005-07-14 14:40:06 +0100394 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100395 return AUDIT_DISABLED;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100398 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000399 int word = AUDIT_WORD(ctx->major);
400 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100401
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000402 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400403 if ((e->rule.mask[word] & bit) == bit &&
404 audit_filter_rules(tsk, &e->rule, ctx, NULL,
405 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000406 rcu_read_unlock();
407 return state;
408 }
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 rcu_read_unlock();
412 return AUDIT_BUILD_CONTEXT;
413}
414
Amy Griffisf368c07d2006-04-07 16:55:56 -0400415/* At syscall exit time, this filter is called if any audit_names[] have been
416 * collected during syscall processing. We only check rules in sublists at hash
417 * buckets applicable to the inode numbers in audit_names[].
418 * Regarding audit_state, same rules apply as for audit_filter_syscall().
419 */
420enum audit_state audit_filter_inodes(struct task_struct *tsk,
421 struct audit_context *ctx)
422{
423 int i;
424 struct audit_entry *e;
425 enum audit_state state;
426
427 if (audit_pid && tsk->tgid == audit_pid)
428 return AUDIT_DISABLED;
429
430 rcu_read_lock();
431 for (i = 0; i < ctx->name_count; i++) {
432 int word = AUDIT_WORD(ctx->major);
433 int bit = AUDIT_BIT(ctx->major);
434 struct audit_names *n = &ctx->names[i];
435 int h = audit_hash_ino((u32)n->ino);
436 struct list_head *list = &audit_inode_hash[h];
437
438 if (list_empty(list))
439 continue;
440
441 list_for_each_entry_rcu(e, list, list) {
442 if ((e->rule.mask[word] & bit) == bit &&
443 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
444 rcu_read_unlock();
445 return state;
446 }
447 }
448 }
449 rcu_read_unlock();
450 return AUDIT_BUILD_CONTEXT;
451}
452
453void audit_set_auditable(struct audit_context *ctx)
454{
455 ctx->auditable = 1;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static inline struct audit_context *audit_get_context(struct task_struct *tsk,
459 int return_valid,
460 int return_code)
461{
462 struct audit_context *context = tsk->audit_context;
463
464 if (likely(!context))
465 return NULL;
466 context->return_valid = return_valid;
467 context->return_code = return_code;
468
David Woodhouse21af6c42005-07-02 14:10:46 +0100469 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400471
David Woodhouse0f45aa12005-06-19 19:35:50 +0100472 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400473 if (state == AUDIT_RECORD_CONTEXT) {
474 context->auditable = 1;
475 goto get_context;
476 }
477
478 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (state == AUDIT_RECORD_CONTEXT)
480 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
Amy Griffisf368c07d2006-04-07 16:55:56 -0400484get_context:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 context->pid = tsk->pid;
Al Virof46038f2006-05-06 08:22:52 -0400486 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 context->uid = tsk->uid;
488 context->gid = tsk->gid;
489 context->euid = tsk->euid;
490 context->suid = tsk->suid;
491 context->fsuid = tsk->fsuid;
492 context->egid = tsk->egid;
493 context->sgid = tsk->sgid;
494 context->fsgid = tsk->fsgid;
495 context->personality = tsk->personality;
496 tsk->audit_context = NULL;
497 return context;
498}
499
500static inline void audit_free_names(struct audit_context *context)
501{
502 int i;
503
504#if AUDIT_DEBUG == 2
505 if (context->auditable
506 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000507 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 " name_count=%d put_count=%d"
509 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000510 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 context->serial, context->major, context->in_syscall,
512 context->name_count, context->put_count,
513 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000514 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 printk(KERN_ERR "names[%d] = %p = %s\n", i,
516 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000517 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 dump_stack();
520 return;
521 }
522#endif
523#if AUDIT_DEBUG
524 context->put_count = 0;
525 context->ino_count = 0;
526#endif
527
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000528 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (context->names[i].name)
530 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100533 if (context->pwd)
534 dput(context->pwd);
535 if (context->pwdmnt)
536 mntput(context->pwdmnt);
537 context->pwd = NULL;
538 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541static inline void audit_free_aux(struct audit_context *context)
542{
543 struct audit_aux_data *aux;
544
545 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100546 if (aux->type == AUDIT_AVC_PATH) {
547 struct audit_aux_data_path *axi = (void *)aux;
548 dput(axi->dentry);
549 mntput(axi->mnt);
550 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 context->aux = aux->next;
553 kfree(aux);
554 }
555}
556
557static inline void audit_zero_context(struct audit_context *context,
558 enum audit_state state)
559{
560 uid_t loginuid = context->loginuid;
561
562 memset(context, 0, sizeof(*context));
563 context->state = state;
564 context->loginuid = loginuid;
565}
566
567static inline struct audit_context *audit_alloc_context(enum audit_state state)
568{
569 struct audit_context *context;
570
571 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
572 return NULL;
573 audit_zero_context(context, state);
574 return context;
575}
576
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700577/**
578 * audit_alloc - allocate an audit context block for a task
579 * @tsk: task
580 *
581 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * if necessary. Doing so turns on system call auditing for the
583 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700584 * needed.
585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586int audit_alloc(struct task_struct *tsk)
587{
588 struct audit_context *context;
589 enum audit_state state;
590
591 if (likely(!audit_enabled))
592 return 0; /* Return if not auditing. */
593
594 state = audit_filter_task(tsk);
595 if (likely(state == AUDIT_DISABLED))
596 return 0;
597
598 if (!(context = audit_alloc_context(state))) {
599 audit_log_lost("out of memory in audit_alloc");
600 return -ENOMEM;
601 }
602
603 /* Preserve login uid */
604 context->loginuid = -1;
605 if (current->audit_context)
606 context->loginuid = current->audit_context->loginuid;
607
608 tsk->audit_context = context;
609 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
610 return 0;
611}
612
613static inline void audit_free_context(struct audit_context *context)
614{
615 struct audit_context *previous;
616 int count = 0;
617
618 do {
619 previous = context->previous;
620 if (previous || (count && count < 10)) {
621 ++count;
622 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
623 " freeing multiple contexts (%d)\n",
624 context->serial, context->major,
625 context->name_count, count);
626 }
627 audit_free_names(context);
628 audit_free_aux(context);
629 kfree(context);
630 context = previous;
631 } while (context);
632 if (count >= 10)
633 printk(KERN_ERR "audit: freed %d contexts\n", count);
634}
635
Al Viroe4951492006-03-29 20:17:10 -0500636static void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000637{
638 char *ctx = NULL;
639 ssize_t len = 0;
640
641 len = security_getprocattr(current, "current", NULL, 0);
642 if (len < 0) {
643 if (len != -EINVAL)
644 goto error_path;
645 return;
646 }
647
Al Viroe4951492006-03-29 20:17:10 -0500648 ctx = kmalloc(len, GFP_KERNEL);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000649 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000650 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000651
652 len = security_getprocattr(current, "current", ctx, len);
653 if (len < 0 )
654 goto error_path;
655
656 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000657 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000658
659error_path:
660 if (ctx)
661 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000662 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000663 return;
664}
665
Al Viroe4951492006-03-29 20:17:10 -0500666static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700667{
Al Viro45d9bb02006-03-29 20:02:55 -0500668 char name[sizeof(tsk->comm)];
669 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700670 struct vm_area_struct *vma;
671
Al Viroe4951492006-03-29 20:17:10 -0500672 /* tsk == current */
673
Al Viro45d9bb02006-03-29 20:02:55 -0500674 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100675 audit_log_format(ab, " comm=");
676 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700677
Al Viroe4951492006-03-29 20:17:10 -0500678 if (mm) {
679 down_read(&mm->mmap_sem);
680 vma = mm->mmap;
681 while (vma) {
682 if ((vma->vm_flags & VM_EXECUTABLE) &&
683 vma->vm_file) {
684 audit_log_d_path(ab, "exe=",
685 vma->vm_file->f_dentry,
686 vma->vm_file->f_vfsmnt);
687 break;
688 }
689 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700690 }
Al Viroe4951492006-03-29 20:17:10 -0500691 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700692 }
Al Viroe4951492006-03-29 20:17:10 -0500693 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700694}
695
Al Viroe4951492006-03-29 20:17:10 -0500696static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500698 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100700 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500701 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Al Viroe4951492006-03-29 20:17:10 -0500703 /* tsk == current */
704
705 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!ab)
707 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100708 audit_log_format(ab, "arch=%x syscall=%d",
709 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (context->personality != PER_LINUX)
711 audit_log_format(ab, " per=%lx", context->personality);
712 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100713 audit_log_format(ab, " success=%s exit=%ld",
714 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
715 context->return_code);
Al Viro45d9bb02006-03-29 20:02:55 -0500716 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
717 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500718 else
719 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 audit_log_format(ab,
721 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -0400722 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +0100723 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500724 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 context->argv[0],
726 context->argv[1],
727 context->argv[2],
728 context->argv[3],
729 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -0400730 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 context->pid,
732 context->loginuid,
733 context->uid,
734 context->gid,
735 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500736 context->egid, context->sgid, context->fsgid, tty);
Al Viroe4951492006-03-29 20:17:10 -0500737 audit_log_task_info(ab, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
David Woodhouse7551ced2005-05-26 12:04:57 +0100740 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100741
Al Viroe4951492006-03-29 20:17:10 -0500742 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (!ab)
744 continue; /* audit_panic has been called */
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -0500747 case AUDIT_MQ_OPEN: {
748 struct audit_aux_data_mq_open *axi = (void *)aux;
749 audit_log_format(ab,
750 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
751 "mq_msgsize=%ld mq_curmsgs=%ld",
752 axi->oflag, axi->mode, axi->attr.mq_flags,
753 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
754 axi->attr.mq_curmsgs);
755 break; }
756
757 case AUDIT_MQ_SENDRECV: {
758 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
759 audit_log_format(ab,
760 "mqdes=%d msg_len=%zd msg_prio=%u "
761 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
762 axi->mqdes, axi->msg_len, axi->msg_prio,
763 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
764 break; }
765
766 case AUDIT_MQ_NOTIFY: {
767 struct audit_aux_data_mq_notify *axi = (void *)aux;
768 audit_log_format(ab,
769 "mqdes=%d sigev_signo=%d",
770 axi->mqdes,
771 axi->notification.sigev_signo);
772 break; }
773
774 case AUDIT_MQ_GETSETATTR: {
775 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
776 audit_log_format(ab,
777 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
778 "mq_curmsgs=%ld ",
779 axi->mqdes,
780 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
781 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
782 break; }
783
Steve Grubbc0404992005-05-13 18:17:42 +0100784 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct audit_aux_data_ipcctl *axi = (void *)aux;
786 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400787 "ouid=%u ogid=%u mode=%x",
788 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500789 if (axi->osid != 0) {
790 char *ctx = NULL;
791 u32 len;
792 if (selinux_ctxid_to_string(
793 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500794 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500795 axi->osid);
796 call_panic = 1;
797 } else
798 audit_log_format(ab, " obj=%s", ctx);
799 kfree(ctx);
800 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100801 break; }
802
Steve Grubb073115d2006-04-02 17:07:33 -0400803 case AUDIT_IPC_SET_PERM: {
804 struct audit_aux_data_ipcctl *axi = (void *)aux;
805 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400806 "qbytes=%lx ouid=%u ogid=%u mode=%x",
Steve Grubb073115d2006-04-02 17:07:33 -0400807 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400808 break; }
Linda Knippersac032212006-05-16 22:03:48 -0400809
Al Viro473ae302006-04-26 14:04:08 -0400810 case AUDIT_EXECVE: {
811 struct audit_aux_data_execve *axi = (void *)aux;
812 int i;
813 const char *p;
814 for (i = 0, p = axi->mem; i < axi->argc; i++) {
815 audit_log_format(ab, "a%d=", i);
816 p = audit_log_untrustedstring(ab, p);
817 audit_log_format(ab, "\n");
818 }
819 break; }
Steve Grubb073115d2006-04-02 17:07:33 -0400820
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100821 case AUDIT_SOCKETCALL: {
822 int i;
823 struct audit_aux_data_socketcall *axs = (void *)aux;
824 audit_log_format(ab, "nargs=%d", axs->nargs);
825 for (i=0; i<axs->nargs; i++)
826 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
827 break; }
828
829 case AUDIT_SOCKADDR: {
830 struct audit_aux_data_sockaddr *axs = (void *)aux;
831
832 audit_log_format(ab, "saddr=");
833 audit_log_hex(ab, axs->a, axs->len);
834 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100835
836 case AUDIT_AVC_PATH: {
837 struct audit_aux_data_path *axi = (void *)aux;
838 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100839 break; }
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
David Woodhouse8f37d472005-05-27 12:17:28 +0100845 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -0500846 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100847 if (ab) {
848 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
849 audit_log_end(ab);
850 }
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000853 unsigned long ino = context->names[i].ino;
854 unsigned long pino = context->names[i].pino;
855
Al Viroe4951492006-03-29 20:17:10 -0500856 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!ab)
858 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000861
862 audit_log_format(ab, " name=");
863 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100864 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000865 else
866 audit_log_format(ab, "(null)");
867
868 if (pino != (unsigned long)-1)
869 audit_log_format(ab, " parent=%lu", pino);
870 if (ino != (unsigned long)-1)
871 audit_log_format(ab, " inode=%lu", ino);
872 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
873 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
874 " ouid=%u ogid=%u rdev=%02x:%02x",
875 MAJOR(context->names[i].dev),
876 MINOR(context->names[i].dev),
877 context->names[i].mode,
878 context->names[i].uid,
879 context->names[i].gid,
880 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 MINOR(context->names[i].rdev));
Steve Grubb1b50eed2006-04-03 14:06:13 -0400882 if (context->names[i].osid != 0) {
883 char *ctx = NULL;
884 u32 len;
885 if (selinux_ctxid_to_string(
886 context->names[i].osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500887 audit_log_format(ab, " osid=%u",
Steve Grubb1b50eed2006-04-03 14:06:13 -0400888 context->names[i].osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500889 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400890 } else
891 audit_log_format(ab, " obj=%s", ctx);
892 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000893 }
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 audit_log_end(ab);
896 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500897 if (call_panic)
898 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700901/**
902 * audit_free - free a per-task audit context
903 * @tsk: task whose audit context block to free
904 *
Al Virofa84cb92006-03-29 20:30:19 -0500905 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700906 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907void audit_free(struct task_struct *tsk)
908{
909 struct audit_context *context;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (likely(!context))
913 return;
914
915 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100916 * function (e.g., exit_group), then free context block.
917 * We use GFP_ATOMIC here because we might be doing this
918 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -0500919 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +0100920 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500921 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 audit_free_context(context);
924}
925
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700926/**
927 * audit_syscall_entry - fill in an audit record at syscall entry
928 * @tsk: task being audited
929 * @arch: architecture type
930 * @major: major syscall type (function)
931 * @a1: additional syscall register 1
932 * @a2: additional syscall register 2
933 * @a3: additional syscall register 3
934 * @a4: additional syscall register 4
935 *
936 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * audit context was created when the task was created and the state or
938 * filters demand the audit context be built. If the state from the
939 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
940 * then the record will be written at syscall exit time (otherwise, it
941 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700942 * be written).
943 */
Al Viro5411be52006-03-29 20:23:36 -0500944void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 unsigned long a1, unsigned long a2,
946 unsigned long a3, unsigned long a4)
947{
Al Viro5411be52006-03-29 20:23:36 -0500948 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 struct audit_context *context = tsk->audit_context;
950 enum audit_state state;
951
952 BUG_ON(!context);
953
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700954 /*
955 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * calls in kernel_thread via the entry.S interface, instead of
957 * with direct calls. (If you are porting to a new
958 * architecture, hitting this condition can indicate that you
959 * got the _exit/_leave calls backward in entry.S.)
960 *
961 * i386 no
962 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -0600963 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 *
965 * This also happens with vm86 emulation in a non-nested manner
966 * (entries without exits), so this case must be caught.
967 */
968 if (context->in_syscall) {
969 struct audit_context *newctx;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971#if AUDIT_DEBUG
972 printk(KERN_ERR
973 "audit(:%d) pid=%d in syscall=%d;"
974 " entering syscall=%d\n",
975 context->serial, tsk->pid, context->major, major);
976#endif
977 newctx = audit_alloc_context(context->state);
978 if (newctx) {
979 newctx->previous = context;
980 context = newctx;
981 tsk->audit_context = newctx;
982 } else {
983 /* If we can't alloc a new context, the best we
984 * can do is to leak memory (any pending putname
985 * will be lost). The only other alternative is
986 * to abandon auditing. */
987 audit_zero_context(context, context->state);
988 }
989 }
990 BUG_ON(context->in_syscall || context->name_count);
991
992 if (!audit_enabled)
993 return;
994
2fd6f582005-04-29 16:08:28 +0100995 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 context->major = major;
997 context->argv[0] = a1;
998 context->argv[1] = a2;
999 context->argv[2] = a3;
1000 context->argv[3] = a4;
1001
1002 state = context->state;
1003 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +01001004 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (likely(state == AUDIT_DISABLED))
1006 return;
1007
David Woodhousece625a82005-07-18 14:24:46 -04001008 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 context->ctime = CURRENT_TIME;
1010 context->in_syscall = 1;
1011 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1012}
1013
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001014/**
1015 * audit_syscall_exit - deallocate audit context after a system call
1016 * @tsk: task being audited
1017 * @valid: success/failure flag
1018 * @return_code: syscall return value
1019 *
1020 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1022 * filtering, or because some other part of the kernel write an audit
1023 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001024 * free the names stored from getname().
1025 */
Al Viro5411be52006-03-29 20:23:36 -05001026void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Al Viro5411be52006-03-29 20:23:36 -05001028 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct audit_context *context;
1030
2fd6f582005-04-29 16:08:28 +01001031 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001034 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
David Woodhousef7056d62005-06-20 16:07:33 +01001036 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001037 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 context->in_syscall = 0;
1040 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (context->previous) {
1043 struct audit_context *new_context = context->previous;
1044 context->previous = NULL;
1045 audit_free_context(context);
1046 tsk->audit_context = new_context;
1047 } else {
1048 audit_free_names(context);
1049 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 tsk->audit_context = context;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001054/**
1055 * audit_getname - add a name to the list
1056 * @name: name to add
1057 *
1058 * Add a name to the list of audit names for this context.
1059 * Called from fs/namei.c:getname().
1060 */
Al Virod8945bb52006-05-18 16:01:30 -04001061void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 struct audit_context *context = current->audit_context;
1064
Al Virod8945bb52006-05-18 16:01:30 -04001065 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return;
1067
1068 if (!context->in_syscall) {
1069#if AUDIT_DEBUG == 2
1070 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1071 __FILE__, __LINE__, context->serial, name);
1072 dump_stack();
1073#endif
1074 return;
1075 }
1076 BUG_ON(context->name_count >= AUDIT_NAMES);
1077 context->names[context->name_count].name = name;
1078 context->names[context->name_count].ino = (unsigned long)-1;
1079 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001080 if (!context->pwd) {
1081 read_lock(&current->fs->lock);
1082 context->pwd = dget(current->fs->pwd);
1083 context->pwdmnt = mntget(current->fs->pwdmnt);
1084 read_unlock(&current->fs->lock);
1085 }
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001089/* audit_putname - intercept a putname request
1090 * @name: name to intercept and delay for putname
1091 *
1092 * If we have stored the name from getname in the audit context,
1093 * then we delay the putname until syscall exit.
1094 * Called from include/linux/fs.h:putname().
1095 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096void audit_putname(const char *name)
1097{
1098 struct audit_context *context = current->audit_context;
1099
1100 BUG_ON(!context);
1101 if (!context->in_syscall) {
1102#if AUDIT_DEBUG == 2
1103 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1104 __FILE__, __LINE__, context->serial, name);
1105 if (context->name_count) {
1106 int i;
1107 for (i = 0; i < context->name_count; i++)
1108 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1109 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001110 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112#endif
1113 __putname(name);
1114 }
1115#if AUDIT_DEBUG
1116 else {
1117 ++context->put_count;
1118 if (context->put_count > context->name_count) {
1119 printk(KERN_ERR "%s:%d(:%d): major=%d"
1120 " in_syscall=%d putname(%p) name_count=%d"
1121 " put_count=%d\n",
1122 __FILE__, __LINE__,
1123 context->serial, context->major,
1124 context->in_syscall, name, context->name_count,
1125 context->put_count);
1126 dump_stack();
1127 }
1128 }
1129#endif
1130}
1131
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001132static void audit_inode_context(int idx, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001133{
1134 struct audit_context *context = current->audit_context;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001135
Steve Grubb1b50eed2006-04-03 14:06:13 -04001136 selinux_get_inode_sid(inode, &context->names[idx].osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001137}
1138
1139
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001140/**
1141 * audit_inode - store the inode and device from a lookup
1142 * @name: name being audited
1143 * @inode: inode being audited
1144 * @flags: lookup flags (as used in path_lookup())
1145 *
1146 * Called from fs/namei.c:path_lookup().
1147 */
Amy Griffis73241cc2005-11-03 16:00:25 +00001148void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 int idx;
1151 struct audit_context *context = current->audit_context;
1152
1153 if (!context->in_syscall)
1154 return;
1155 if (context->name_count
1156 && context->names[context->name_count-1].name
1157 && context->names[context->name_count-1].name == name)
1158 idx = context->name_count - 1;
1159 else if (context->name_count > 1
1160 && context->names[context->name_count-2].name
1161 && context->names[context->name_count-2].name == name)
1162 idx = context->name_count - 2;
1163 else {
1164 /* FIXME: how much do we care about inodes that have no
1165 * associated name? */
1166 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1167 return;
1168 idx = context->name_count++;
1169 context->names[idx].name = NULL;
1170#if AUDIT_DEBUG
1171 ++context->ino_count;
1172#endif
1173 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001174 context->names[idx].dev = inode->i_sb->s_dev;
1175 context->names[idx].mode = inode->i_mode;
1176 context->names[idx].uid = inode->i_uid;
1177 context->names[idx].gid = inode->i_gid;
1178 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001179 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001180 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1181 (strcmp(name, ".") != 0)) {
1182 context->names[idx].ino = (unsigned long)-1;
1183 context->names[idx].pino = inode->i_ino;
1184 } else {
1185 context->names[idx].ino = inode->i_ino;
1186 context->names[idx].pino = (unsigned long)-1;
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Amy Griffis73241cc2005-11-03 16:00:25 +00001190/**
1191 * audit_inode_child - collect inode info for created/removed objects
1192 * @dname: inode's dentry name
1193 * @inode: inode being audited
1194 * @pino: inode number of dentry parent
1195 *
1196 * For syscalls that create or remove filesystem objects, audit_inode
1197 * can only collect information for the filesystem object's parent.
1198 * This call updates the audit context with the child's information.
1199 * Syscalls that create a new filesystem object must be hooked after
1200 * the object is created. Syscalls that remove a filesystem object
1201 * must be hooked prior, in order to capture the target inode during
1202 * unsuccessful attempts.
1203 */
1204void __audit_inode_child(const char *dname, const struct inode *inode,
1205 unsigned long pino)
1206{
1207 int idx;
1208 struct audit_context *context = current->audit_context;
1209
1210 if (!context->in_syscall)
1211 return;
1212
1213 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001214 if (!dname)
1215 goto no_match;
1216 for (idx = 0; idx < context->name_count; idx++)
1217 if (context->names[idx].pino == pino) {
1218 const char *name = context->names[idx].name;
Amy Griffis73241cc2005-11-03 16:00:25 +00001219
Amy Griffisf368c07d2006-04-07 16:55:56 -04001220 if (!name)
1221 continue;
Amy Griffis73241cc2005-11-03 16:00:25 +00001222
Amy Griffisf368c07d2006-04-07 16:55:56 -04001223 if (audit_compare_dname_path(dname, name) == 0)
1224 goto update_context;
1225 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001226
Amy Griffisf368c07d2006-04-07 16:55:56 -04001227no_match:
Amy Griffis73241cc2005-11-03 16:00:25 +00001228 /* catch-all in case match not found */
1229 idx = context->name_count++;
1230 context->names[idx].name = NULL;
1231 context->names[idx].pino = pino;
1232#if AUDIT_DEBUG
1233 context->ino_count++;
1234#endif
1235
1236update_context:
1237 if (inode) {
1238 context->names[idx].ino = inode->i_ino;
1239 context->names[idx].dev = inode->i_sb->s_dev;
1240 context->names[idx].mode = inode->i_mode;
1241 context->names[idx].uid = inode->i_uid;
1242 context->names[idx].gid = inode->i_gid;
1243 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001244 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001248/**
1249 * auditsc_get_stamp - get local copies of audit_context values
1250 * @ctx: audit_context for the task
1251 * @t: timespec to store time recorded in the audit_context
1252 * @serial: serial value that is recorded in the audit_context
1253 *
1254 * Also sets the context as auditable.
1255 */
David Woodhousebfb44962005-05-21 21:08:09 +01001256void auditsc_get_stamp(struct audit_context *ctx,
1257 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
David Woodhousece625a82005-07-18 14:24:46 -04001259 if (!ctx->serial)
1260 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001261 t->tv_sec = ctx->ctime.tv_sec;
1262 t->tv_nsec = ctx->ctime.tv_nsec;
1263 *serial = ctx->serial;
1264 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001267/**
1268 * audit_set_loginuid - set a task's audit_context loginuid
1269 * @task: task whose audit context is being modified
1270 * @loginuid: loginuid value
1271 *
1272 * Returns 0.
1273 *
1274 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1275 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001276int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Steve Grubb456be6c2005-04-29 17:30:07 +01001278 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001279 struct audit_buffer *ab;
1280
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001281 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001282 if (ab) {
1283 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001284 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001285 task->pid, task->uid,
1286 task->audit_context->loginuid, loginuid);
1287 audit_log_end(ab);
1288 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001289 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291 return 0;
1292}
1293
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001294/**
1295 * audit_get_loginuid - get the loginuid for an audit_context
1296 * @ctx: the audit_context
1297 *
1298 * Returns the context's loginuid or -1 if @ctx is NULL.
1299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300uid_t audit_get_loginuid(struct audit_context *ctx)
1301{
1302 return ctx ? ctx->loginuid : -1;
1303}
1304
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001305/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05001306 * __audit_mq_open - record audit data for a POSIX MQ open
1307 * @oflag: open flag
1308 * @mode: mode bits
1309 * @u_attr: queue attributes
1310 *
1311 * Returns 0 for success or NULL context or < 0 on error.
1312 */
1313int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1314{
1315 struct audit_aux_data_mq_open *ax;
1316 struct audit_context *context = current->audit_context;
1317
1318 if (!audit_enabled)
1319 return 0;
1320
1321 if (likely(!context))
1322 return 0;
1323
1324 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1325 if (!ax)
1326 return -ENOMEM;
1327
1328 if (u_attr != NULL) {
1329 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1330 kfree(ax);
1331 return -EFAULT;
1332 }
1333 } else
1334 memset(&ax->attr, 0, sizeof(ax->attr));
1335
1336 ax->oflag = oflag;
1337 ax->mode = mode;
1338
1339 ax->d.type = AUDIT_MQ_OPEN;
1340 ax->d.next = context->aux;
1341 context->aux = (void *)ax;
1342 return 0;
1343}
1344
1345/**
1346 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1347 * @mqdes: MQ descriptor
1348 * @msg_len: Message length
1349 * @msg_prio: Message priority
1350 * @abs_timeout: Message timeout in absolute time
1351 *
1352 * Returns 0 for success or NULL context or < 0 on error.
1353 */
1354int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1355 const struct timespec __user *u_abs_timeout)
1356{
1357 struct audit_aux_data_mq_sendrecv *ax;
1358 struct audit_context *context = current->audit_context;
1359
1360 if (!audit_enabled)
1361 return 0;
1362
1363 if (likely(!context))
1364 return 0;
1365
1366 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1367 if (!ax)
1368 return -ENOMEM;
1369
1370 if (u_abs_timeout != NULL) {
1371 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1372 kfree(ax);
1373 return -EFAULT;
1374 }
1375 } else
1376 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1377
1378 ax->mqdes = mqdes;
1379 ax->msg_len = msg_len;
1380 ax->msg_prio = msg_prio;
1381
1382 ax->d.type = AUDIT_MQ_SENDRECV;
1383 ax->d.next = context->aux;
1384 context->aux = (void *)ax;
1385 return 0;
1386}
1387
1388/**
1389 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1390 * @mqdes: MQ descriptor
1391 * @msg_len: Message length
1392 * @msg_prio: Message priority
1393 * @abs_timeout: Message timeout in absolute time
1394 *
1395 * Returns 0 for success or NULL context or < 0 on error.
1396 */
1397int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1398 unsigned int __user *u_msg_prio,
1399 const struct timespec __user *u_abs_timeout)
1400{
1401 struct audit_aux_data_mq_sendrecv *ax;
1402 struct audit_context *context = current->audit_context;
1403
1404 if (!audit_enabled)
1405 return 0;
1406
1407 if (likely(!context))
1408 return 0;
1409
1410 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1411 if (!ax)
1412 return -ENOMEM;
1413
1414 if (u_msg_prio != NULL) {
1415 if (get_user(ax->msg_prio, u_msg_prio)) {
1416 kfree(ax);
1417 return -EFAULT;
1418 }
1419 } else
1420 ax->msg_prio = 0;
1421
1422 if (u_abs_timeout != NULL) {
1423 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1424 kfree(ax);
1425 return -EFAULT;
1426 }
1427 } else
1428 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1429
1430 ax->mqdes = mqdes;
1431 ax->msg_len = msg_len;
1432
1433 ax->d.type = AUDIT_MQ_SENDRECV;
1434 ax->d.next = context->aux;
1435 context->aux = (void *)ax;
1436 return 0;
1437}
1438
1439/**
1440 * __audit_mq_notify - record audit data for a POSIX MQ notify
1441 * @mqdes: MQ descriptor
1442 * @u_notification: Notification event
1443 *
1444 * Returns 0 for success or NULL context or < 0 on error.
1445 */
1446
1447int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1448{
1449 struct audit_aux_data_mq_notify *ax;
1450 struct audit_context *context = current->audit_context;
1451
1452 if (!audit_enabled)
1453 return 0;
1454
1455 if (likely(!context))
1456 return 0;
1457
1458 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1459 if (!ax)
1460 return -ENOMEM;
1461
1462 if (u_notification != NULL) {
1463 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1464 kfree(ax);
1465 return -EFAULT;
1466 }
1467 } else
1468 memset(&ax->notification, 0, sizeof(ax->notification));
1469
1470 ax->mqdes = mqdes;
1471
1472 ax->d.type = AUDIT_MQ_NOTIFY;
1473 ax->d.next = context->aux;
1474 context->aux = (void *)ax;
1475 return 0;
1476}
1477
1478/**
1479 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1480 * @mqdes: MQ descriptor
1481 * @mqstat: MQ flags
1482 *
1483 * Returns 0 for success or NULL context or < 0 on error.
1484 */
1485int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1486{
1487 struct audit_aux_data_mq_getsetattr *ax;
1488 struct audit_context *context = current->audit_context;
1489
1490 if (!audit_enabled)
1491 return 0;
1492
1493 if (likely(!context))
1494 return 0;
1495
1496 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1497 if (!ax)
1498 return -ENOMEM;
1499
1500 ax->mqdes = mqdes;
1501 ax->mqstat = *mqstat;
1502
1503 ax->d.type = AUDIT_MQ_GETSETATTR;
1504 ax->d.next = context->aux;
1505 context->aux = (void *)ax;
1506 return 0;
1507}
1508
1509/**
Steve Grubb073115d2006-04-02 17:07:33 -04001510 * audit_ipc_obj - record audit data for ipc object
1511 * @ipcp: ipc permissions
1512 *
1513 * Returns 0 for success or NULL context or < 0 on error.
1514 */
Al Virod8945bb52006-05-18 16:01:30 -04001515int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04001516{
1517 struct audit_aux_data_ipcctl *ax;
1518 struct audit_context *context = current->audit_context;
1519
Steve Grubb073115d2006-04-02 17:07:33 -04001520 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1521 if (!ax)
1522 return -ENOMEM;
1523
1524 ax->uid = ipcp->uid;
1525 ax->gid = ipcp->gid;
1526 ax->mode = ipcp->mode;
1527 selinux_get_ipc_sid(ipcp, &ax->osid);
1528
1529 ax->d.type = AUDIT_IPC;
1530 ax->d.next = context->aux;
1531 context->aux = (void *)ax;
1532 return 0;
1533}
1534
1535/**
1536 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001537 * @qbytes: msgq bytes
1538 * @uid: msgq user id
1539 * @gid: msgq group id
1540 * @mode: msgq mode (permissions)
1541 *
1542 * Returns 0 for success or NULL context or < 0 on error.
1543 */
Al Virod8945bb52006-05-18 16:01:30 -04001544int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
1546 struct audit_aux_data_ipcctl *ax;
1547 struct audit_context *context = current->audit_context;
1548
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001549 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (!ax)
1551 return -ENOMEM;
1552
1553 ax->qbytes = qbytes;
1554 ax->uid = uid;
1555 ax->gid = gid;
1556 ax->mode = mode;
1557
Steve Grubb073115d2006-04-02 17:07:33 -04001558 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 ax->d.next = context->aux;
1560 context->aux = (void *)ax;
1561 return 0;
1562}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001563
Al Viro473ae302006-04-26 14:04:08 -04001564int audit_bprm(struct linux_binprm *bprm)
1565{
1566 struct audit_aux_data_execve *ax;
1567 struct audit_context *context = current->audit_context;
1568 unsigned long p, next;
1569 void *to;
1570
1571 if (likely(!audit_enabled || !context))
1572 return 0;
1573
1574 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1575 GFP_KERNEL);
1576 if (!ax)
1577 return -ENOMEM;
1578
1579 ax->argc = bprm->argc;
1580 ax->envc = bprm->envc;
1581 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1582 struct page *page = bprm->page[p / PAGE_SIZE];
1583 void *kaddr = kmap(page);
1584 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1585 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1586 to += next - p;
1587 kunmap(page);
1588 }
1589
1590 ax->d.type = AUDIT_EXECVE;
1591 ax->d.next = context->aux;
1592 context->aux = (void *)ax;
1593 return 0;
1594}
1595
1596
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001597/**
1598 * audit_socketcall - record audit data for sys_socketcall
1599 * @nargs: number of args
1600 * @args: args array
1601 *
1602 * Returns 0 for success or NULL context or < 0 on error.
1603 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001604int audit_socketcall(int nargs, unsigned long *args)
1605{
1606 struct audit_aux_data_socketcall *ax;
1607 struct audit_context *context = current->audit_context;
1608
1609 if (likely(!context))
1610 return 0;
1611
1612 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1613 if (!ax)
1614 return -ENOMEM;
1615
1616 ax->nargs = nargs;
1617 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1618
1619 ax->d.type = AUDIT_SOCKETCALL;
1620 ax->d.next = context->aux;
1621 context->aux = (void *)ax;
1622 return 0;
1623}
1624
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001625/**
1626 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1627 * @len: data length in user space
1628 * @a: data address in kernel space
1629 *
1630 * Returns 0 for success or NULL context or < 0 on error.
1631 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001632int audit_sockaddr(int len, void *a)
1633{
1634 struct audit_aux_data_sockaddr *ax;
1635 struct audit_context *context = current->audit_context;
1636
1637 if (likely(!context))
1638 return 0;
1639
1640 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1641 if (!ax)
1642 return -ENOMEM;
1643
1644 ax->len = len;
1645 memcpy(ax->a, a, len);
1646
1647 ax->d.type = AUDIT_SOCKADDR;
1648 ax->d.next = context->aux;
1649 context->aux = (void *)ax;
1650 return 0;
1651}
1652
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001653/**
1654 * audit_avc_path - record the granting or denial of permissions
1655 * @dentry: dentry to record
1656 * @mnt: mnt to record
1657 *
1658 * Returns 0 for success or NULL context or < 0 on error.
1659 *
1660 * Called from security/selinux/avc.c::avc_audit()
1661 */
Stephen Smalley01116102005-05-21 00:15:52 +01001662int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1663{
1664 struct audit_aux_data_path *ax;
1665 struct audit_context *context = current->audit_context;
1666
1667 if (likely(!context))
1668 return 0;
1669
1670 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1671 if (!ax)
1672 return -ENOMEM;
1673
1674 ax->dentry = dget(dentry);
1675 ax->mnt = mntget(mnt);
1676
1677 ax->d.type = AUDIT_AVC_PATH;
1678 ax->d.next = context->aux;
1679 context->aux = (void *)ax;
1680 return 0;
1681}
1682
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001683/**
1684 * audit_signal_info - record signal info for shutting down audit subsystem
1685 * @sig: signal value
1686 * @t: task being signaled
1687 *
1688 * If the audit subsystem is being terminated, record the task (pid)
1689 * and uid that is doing that.
1690 */
Al Viroe1396062006-05-25 10:19:47 -04001691void __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001692{
1693 extern pid_t audit_sig_pid;
1694 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04001695 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001696
Al Viroe1396062006-05-25 10:19:47 -04001697 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1698 struct task_struct *tsk = current;
1699 struct audit_context *ctx = tsk->audit_context;
1700 audit_sig_pid = tsk->pid;
1701 if (ctx)
1702 audit_sig_uid = ctx->loginuid;
1703 else
1704 audit_sig_uid = tsk->uid;
1705 selinux_get_task_sid(tsk, &audit_sig_sid);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001706 }
1707}