blob: 14e295a4121b06d2d198d5052a900ea4fe9db046 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204/* 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,
209 enum audit_state *state)
210{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400211 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600212 u32 sid;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500215 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int result = 0;
217
Amy Griffis93315ed2006-02-07 12:05:27 -0500218 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500220 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
Al Viro3c662512006-05-06 08:26:27 -0400222 case AUDIT_PPID:
223 if (ctx)
224 result = audit_comparator(ctx->ppid, f->op, f->val);
225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500227 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 break;
229 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500230 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 break;
232 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500233 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 break;
235 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500236 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 break;
238 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500239 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 break;
241 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500242 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 break;
244 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500245 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
247 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500248 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500251 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 break;
2fd6f582005-04-29 16:08:28 +0100253 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000254 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500255 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 case AUDIT_EXIT:
259 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500260 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 break;
262 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100263 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500264 if (f->val)
265 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100266 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500267 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 break;
270 case AUDIT_DEVMAJOR:
271 if (ctx) {
272 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500273 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 ++result;
275 break;
276 }
277 }
278 }
279 break;
280 case AUDIT_DEVMINOR:
281 if (ctx) {
282 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500283 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 ++result;
285 break;
286 }
287 }
288 }
289 break;
290 case AUDIT_INODE:
291 if (ctx) {
292 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500293 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
294 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ++result;
296 break;
297 }
298 }
299 }
300 break;
301 case AUDIT_LOGINUID:
302 result = 0;
303 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500304 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600306 case AUDIT_SE_USER:
307 case AUDIT_SE_ROLE:
308 case AUDIT_SE_TYPE:
309 case AUDIT_SE_SEN:
310 case AUDIT_SE_CLR:
311 /* NOTE: this may return negative values indicating
312 a temporary error. We simply treat this as a
313 match for now to avoid losing information that
314 may be wanted. An error message will also be
315 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400316 if (f->se_rule) {
317 if (need_sid) {
318 selinux_task_ctxid(tsk, &sid);
319 need_sid = 0;
320 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600321 result = selinux_audit_rule_match(sid, f->type,
322 f->op,
323 f->se_rule,
324 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400325 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 case AUDIT_ARG0:
328 case AUDIT_ARG1:
329 case AUDIT_ARG2:
330 case AUDIT_ARG3:
331 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500332 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 break;
334 }
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (!result)
337 return 0;
338 }
339 switch (rule->action) {
340 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
342 }
343 return 1;
344}
345
346/* At process creation time, we can determine if system-call auditing is
347 * completely disabled for this task. Since we only have the task
348 * structure at this point, we can only check uid and gid.
349 */
350static enum audit_state audit_filter_task(struct task_struct *tsk)
351{
352 struct audit_entry *e;
353 enum audit_state state;
354
355 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100356 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
358 rcu_read_unlock();
359 return state;
360 }
361 }
362 rcu_read_unlock();
363 return AUDIT_BUILD_CONTEXT;
364}
365
366/* At syscall entry and exit time, this filter is called if the
367 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100368 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700369 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
371static enum audit_state audit_filter_syscall(struct task_struct *tsk,
372 struct audit_context *ctx,
373 struct list_head *list)
374{
375 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100376 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
David Woodhouse351bb722005-07-14 14:40:06 +0100378 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100379 return AUDIT_DISABLED;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100382 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000383 int word = AUDIT_WORD(ctx->major);
384 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100385
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000386 list_for_each_entry_rcu(e, list, list) {
387 if ((e->rule.mask[word] & bit) == bit
388 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
389 rcu_read_unlock();
390 return state;
391 }
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 rcu_read_unlock();
395 return AUDIT_BUILD_CONTEXT;
396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static inline struct audit_context *audit_get_context(struct task_struct *tsk,
399 int return_valid,
400 int return_code)
401{
402 struct audit_context *context = tsk->audit_context;
403
404 if (likely(!context))
405 return NULL;
406 context->return_valid = return_valid;
407 context->return_code = return_code;
408
David Woodhouse21af6c42005-07-02 14:10:46 +0100409 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100411 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (state == AUDIT_RECORD_CONTEXT)
413 context->auditable = 1;
414 }
415
416 context->pid = tsk->pid;
Al Virof46038f2006-05-06 08:22:52 -0400417 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 context->uid = tsk->uid;
419 context->gid = tsk->gid;
420 context->euid = tsk->euid;
421 context->suid = tsk->suid;
422 context->fsuid = tsk->fsuid;
423 context->egid = tsk->egid;
424 context->sgid = tsk->sgid;
425 context->fsgid = tsk->fsgid;
426 context->personality = tsk->personality;
427 tsk->audit_context = NULL;
428 return context;
429}
430
431static inline void audit_free_names(struct audit_context *context)
432{
433 int i;
434
435#if AUDIT_DEBUG == 2
436 if (context->auditable
437 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000438 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 " name_count=%d put_count=%d"
440 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000441 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 context->serial, context->major, context->in_syscall,
443 context->name_count, context->put_count,
444 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000445 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 printk(KERN_ERR "names[%d] = %p = %s\n", i,
447 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000448 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 dump_stack();
451 return;
452 }
453#endif
454#if AUDIT_DEBUG
455 context->put_count = 0;
456 context->ino_count = 0;
457#endif
458
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000459 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (context->names[i].name)
461 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100464 if (context->pwd)
465 dput(context->pwd);
466 if (context->pwdmnt)
467 mntput(context->pwdmnt);
468 context->pwd = NULL;
469 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472static inline void audit_free_aux(struct audit_context *context)
473{
474 struct audit_aux_data *aux;
475
476 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100477 if (aux->type == AUDIT_AVC_PATH) {
478 struct audit_aux_data_path *axi = (void *)aux;
479 dput(axi->dentry);
480 mntput(axi->mnt);
481 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 context->aux = aux->next;
484 kfree(aux);
485 }
486}
487
488static inline void audit_zero_context(struct audit_context *context,
489 enum audit_state state)
490{
491 uid_t loginuid = context->loginuid;
492
493 memset(context, 0, sizeof(*context));
494 context->state = state;
495 context->loginuid = loginuid;
496}
497
498static inline struct audit_context *audit_alloc_context(enum audit_state state)
499{
500 struct audit_context *context;
501
502 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
503 return NULL;
504 audit_zero_context(context, state);
505 return context;
506}
507
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700508/**
509 * audit_alloc - allocate an audit context block for a task
510 * @tsk: task
511 *
512 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 * if necessary. Doing so turns on system call auditing for the
514 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700515 * needed.
516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517int audit_alloc(struct task_struct *tsk)
518{
519 struct audit_context *context;
520 enum audit_state state;
521
522 if (likely(!audit_enabled))
523 return 0; /* Return if not auditing. */
524
525 state = audit_filter_task(tsk);
526 if (likely(state == AUDIT_DISABLED))
527 return 0;
528
529 if (!(context = audit_alloc_context(state))) {
530 audit_log_lost("out of memory in audit_alloc");
531 return -ENOMEM;
532 }
533
534 /* Preserve login uid */
535 context->loginuid = -1;
536 if (current->audit_context)
537 context->loginuid = current->audit_context->loginuid;
538
539 tsk->audit_context = context;
540 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
541 return 0;
542}
543
544static inline void audit_free_context(struct audit_context *context)
545{
546 struct audit_context *previous;
547 int count = 0;
548
549 do {
550 previous = context->previous;
551 if (previous || (count && count < 10)) {
552 ++count;
553 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
554 " freeing multiple contexts (%d)\n",
555 context->serial, context->major,
556 context->name_count, count);
557 }
558 audit_free_names(context);
559 audit_free_aux(context);
560 kfree(context);
561 context = previous;
562 } while (context);
563 if (count >= 10)
564 printk(KERN_ERR "audit: freed %d contexts\n", count);
565}
566
Al Viroe4951492006-03-29 20:17:10 -0500567static void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000568{
569 char *ctx = NULL;
570 ssize_t len = 0;
571
572 len = security_getprocattr(current, "current", NULL, 0);
573 if (len < 0) {
574 if (len != -EINVAL)
575 goto error_path;
576 return;
577 }
578
Al Viroe4951492006-03-29 20:17:10 -0500579 ctx = kmalloc(len, GFP_KERNEL);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000580 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000581 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000582
583 len = security_getprocattr(current, "current", ctx, len);
584 if (len < 0 )
585 goto error_path;
586
587 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000588 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000589
590error_path:
591 if (ctx)
592 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000593 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000594 return;
595}
596
Al Viroe4951492006-03-29 20:17:10 -0500597static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700598{
Al Viro45d9bb02006-03-29 20:02:55 -0500599 char name[sizeof(tsk->comm)];
600 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700601 struct vm_area_struct *vma;
602
Al Viroe4951492006-03-29 20:17:10 -0500603 /* tsk == current */
604
Al Viro45d9bb02006-03-29 20:02:55 -0500605 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100606 audit_log_format(ab, " comm=");
607 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700608
Al Viroe4951492006-03-29 20:17:10 -0500609 if (mm) {
610 down_read(&mm->mmap_sem);
611 vma = mm->mmap;
612 while (vma) {
613 if ((vma->vm_flags & VM_EXECUTABLE) &&
614 vma->vm_file) {
615 audit_log_d_path(ab, "exe=",
616 vma->vm_file->f_dentry,
617 vma->vm_file->f_vfsmnt);
618 break;
619 }
620 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700621 }
Al Viroe4951492006-03-29 20:17:10 -0500622 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700623 }
Al Viroe4951492006-03-29 20:17:10 -0500624 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700625}
626
Al Viroe4951492006-03-29 20:17:10 -0500627static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500629 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100631 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500632 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Al Viroe4951492006-03-29 20:17:10 -0500634 /* tsk == current */
635
636 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!ab)
638 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100639 audit_log_format(ab, "arch=%x syscall=%d",
640 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (context->personality != PER_LINUX)
642 audit_log_format(ab, " per=%lx", context->personality);
643 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100644 audit_log_format(ab, " success=%s exit=%ld",
645 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
646 context->return_code);
Al Viro45d9bb02006-03-29 20:02:55 -0500647 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
648 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500649 else
650 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 audit_log_format(ab,
652 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -0400653 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +0100654 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500655 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 context->argv[0],
657 context->argv[1],
658 context->argv[2],
659 context->argv[3],
660 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -0400661 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 context->pid,
663 context->loginuid,
664 context->uid,
665 context->gid,
666 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500667 context->egid, context->sgid, context->fsgid, tty);
Al Viroe4951492006-03-29 20:17:10 -0500668 audit_log_task_info(ab, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
David Woodhouse7551ced2005-05-26 12:04:57 +0100671 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100672
Al Viroe4951492006-03-29 20:17:10 -0500673 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (!ab)
675 continue; /* audit_panic has been called */
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -0500678 case AUDIT_MQ_OPEN: {
679 struct audit_aux_data_mq_open *axi = (void *)aux;
680 audit_log_format(ab,
681 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
682 "mq_msgsize=%ld mq_curmsgs=%ld",
683 axi->oflag, axi->mode, axi->attr.mq_flags,
684 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
685 axi->attr.mq_curmsgs);
686 break; }
687
688 case AUDIT_MQ_SENDRECV: {
689 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
690 audit_log_format(ab,
691 "mqdes=%d msg_len=%zd msg_prio=%u "
692 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
693 axi->mqdes, axi->msg_len, axi->msg_prio,
694 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
695 break; }
696
697 case AUDIT_MQ_NOTIFY: {
698 struct audit_aux_data_mq_notify *axi = (void *)aux;
699 audit_log_format(ab,
700 "mqdes=%d sigev_signo=%d",
701 axi->mqdes,
702 axi->notification.sigev_signo);
703 break; }
704
705 case AUDIT_MQ_GETSETATTR: {
706 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
707 audit_log_format(ab,
708 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
709 "mq_curmsgs=%ld ",
710 axi->mqdes,
711 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
712 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
713 break; }
714
Steve Grubbc0404992005-05-13 18:17:42 +0100715 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct audit_aux_data_ipcctl *axi = (void *)aux;
717 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400718 "ouid=%u ogid=%u mode=%x",
719 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500720 if (axi->osid != 0) {
721 char *ctx = NULL;
722 u32 len;
723 if (selinux_ctxid_to_string(
724 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500725 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500726 axi->osid);
727 call_panic = 1;
728 } else
729 audit_log_format(ab, " obj=%s", ctx);
730 kfree(ctx);
731 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100732 break; }
733
Steve Grubb073115d2006-04-02 17:07:33 -0400734 case AUDIT_IPC_SET_PERM: {
735 struct audit_aux_data_ipcctl *axi = (void *)aux;
736 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400737 "qbytes=%lx ouid=%u ogid=%u mode=%x",
Steve Grubb073115d2006-04-02 17:07:33 -0400738 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400739 break; }
Linda Knippersac032212006-05-16 22:03:48 -0400740
Al Viro473ae302006-04-26 14:04:08 -0400741 case AUDIT_EXECVE: {
742 struct audit_aux_data_execve *axi = (void *)aux;
743 int i;
744 const char *p;
745 for (i = 0, p = axi->mem; i < axi->argc; i++) {
746 audit_log_format(ab, "a%d=", i);
747 p = audit_log_untrustedstring(ab, p);
748 audit_log_format(ab, "\n");
749 }
750 break; }
Steve Grubb073115d2006-04-02 17:07:33 -0400751
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100752 case AUDIT_SOCKETCALL: {
753 int i;
754 struct audit_aux_data_socketcall *axs = (void *)aux;
755 audit_log_format(ab, "nargs=%d", axs->nargs);
756 for (i=0; i<axs->nargs; i++)
757 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
758 break; }
759
760 case AUDIT_SOCKADDR: {
761 struct audit_aux_data_sockaddr *axs = (void *)aux;
762
763 audit_log_format(ab, "saddr=");
764 audit_log_hex(ab, axs->a, axs->len);
765 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100766
767 case AUDIT_AVC_PATH: {
768 struct audit_aux_data_path *axi = (void *)aux;
769 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100770 break; }
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775
David Woodhouse8f37d472005-05-27 12:17:28 +0100776 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -0500777 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100778 if (ab) {
779 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
780 audit_log_end(ab);
781 }
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000784 unsigned long ino = context->names[i].ino;
785 unsigned long pino = context->names[i].pino;
786
Al Viroe4951492006-03-29 20:17:10 -0500787 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (!ab)
789 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000792
793 audit_log_format(ab, " name=");
794 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100795 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000796 else
797 audit_log_format(ab, "(null)");
798
799 if (pino != (unsigned long)-1)
800 audit_log_format(ab, " parent=%lu", pino);
801 if (ino != (unsigned long)-1)
802 audit_log_format(ab, " inode=%lu", ino);
803 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
804 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
805 " ouid=%u ogid=%u rdev=%02x:%02x",
806 MAJOR(context->names[i].dev),
807 MINOR(context->names[i].dev),
808 context->names[i].mode,
809 context->names[i].uid,
810 context->names[i].gid,
811 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 MINOR(context->names[i].rdev));
Steve Grubb1b50eed2006-04-03 14:06:13 -0400813 if (context->names[i].osid != 0) {
814 char *ctx = NULL;
815 u32 len;
816 if (selinux_ctxid_to_string(
817 context->names[i].osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500818 audit_log_format(ab, " osid=%u",
Steve Grubb1b50eed2006-04-03 14:06:13 -0400819 context->names[i].osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500820 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400821 } else
822 audit_log_format(ab, " obj=%s", ctx);
823 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000824 }
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 audit_log_end(ab);
827 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500828 if (call_panic)
829 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700832/**
833 * audit_free - free a per-task audit context
834 * @tsk: task whose audit context block to free
835 *
Al Virofa84cb92006-03-29 20:30:19 -0500836 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700837 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838void audit_free(struct task_struct *tsk)
839{
840 struct audit_context *context;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (likely(!context))
844 return;
845
846 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100847 * function (e.g., exit_group), then free context block.
848 * We use GFP_ATOMIC here because we might be doing this
849 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -0500850 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +0100851 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500852 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 audit_free_context(context);
855}
856
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700857/**
858 * audit_syscall_entry - fill in an audit record at syscall entry
859 * @tsk: task being audited
860 * @arch: architecture type
861 * @major: major syscall type (function)
862 * @a1: additional syscall register 1
863 * @a2: additional syscall register 2
864 * @a3: additional syscall register 3
865 * @a4: additional syscall register 4
866 *
867 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 * audit context was created when the task was created and the state or
869 * filters demand the audit context be built. If the state from the
870 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
871 * then the record will be written at syscall exit time (otherwise, it
872 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700873 * be written).
874 */
Al Viro5411be52006-03-29 20:23:36 -0500875void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned long a1, unsigned long a2,
877 unsigned long a3, unsigned long a4)
878{
Al Viro5411be52006-03-29 20:23:36 -0500879 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct audit_context *context = tsk->audit_context;
881 enum audit_state state;
882
883 BUG_ON(!context);
884
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700885 /*
886 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 * calls in kernel_thread via the entry.S interface, instead of
888 * with direct calls. (If you are porting to a new
889 * architecture, hitting this condition can indicate that you
890 * got the _exit/_leave calls backward in entry.S.)
891 *
892 * i386 no
893 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -0600894 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *
896 * This also happens with vm86 emulation in a non-nested manner
897 * (entries without exits), so this case must be caught.
898 */
899 if (context->in_syscall) {
900 struct audit_context *newctx;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902#if AUDIT_DEBUG
903 printk(KERN_ERR
904 "audit(:%d) pid=%d in syscall=%d;"
905 " entering syscall=%d\n",
906 context->serial, tsk->pid, context->major, major);
907#endif
908 newctx = audit_alloc_context(context->state);
909 if (newctx) {
910 newctx->previous = context;
911 context = newctx;
912 tsk->audit_context = newctx;
913 } else {
914 /* If we can't alloc a new context, the best we
915 * can do is to leak memory (any pending putname
916 * will be lost). The only other alternative is
917 * to abandon auditing. */
918 audit_zero_context(context, context->state);
919 }
920 }
921 BUG_ON(context->in_syscall || context->name_count);
922
923 if (!audit_enabled)
924 return;
925
2fd6f582005-04-29 16:08:28 +0100926 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 context->major = major;
928 context->argv[0] = a1;
929 context->argv[1] = a2;
930 context->argv[2] = a3;
931 context->argv[3] = a4;
932
933 state = context->state;
934 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100935 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (likely(state == AUDIT_DISABLED))
937 return;
938
David Woodhousece625a82005-07-18 14:24:46 -0400939 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 context->ctime = CURRENT_TIME;
941 context->in_syscall = 1;
942 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
943}
944
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700945/**
946 * audit_syscall_exit - deallocate audit context after a system call
947 * @tsk: task being audited
948 * @valid: success/failure flag
949 * @return_code: syscall return value
950 *
951 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
953 * filtering, or because some other part of the kernel write an audit
954 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700955 * free the names stored from getname().
956 */
Al Viro5411be52006-03-29 20:23:36 -0500957void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Al Viro5411be52006-03-29 20:23:36 -0500959 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 struct audit_context *context;
961
2fd6f582005-04-29 16:08:28 +0100962 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -0500965 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
David Woodhousef7056d62005-06-20 16:07:33 +0100967 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500968 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 context->in_syscall = 0;
971 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (context->previous) {
974 struct audit_context *new_context = context->previous;
975 context->previous = NULL;
976 audit_free_context(context);
977 tsk->audit_context = new_context;
978 } else {
979 audit_free_names(context);
980 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 tsk->audit_context = context;
982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700985/**
986 * audit_getname - add a name to the list
987 * @name: name to add
988 *
989 * Add a name to the list of audit names for this context.
990 * Called from fs/namei.c:getname().
991 */
Al Virod8945bb52006-05-18 16:01:30 -0400992void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 struct audit_context *context = current->audit_context;
995
Al Virod8945bb52006-05-18 16:01:30 -0400996 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return;
998
999 if (!context->in_syscall) {
1000#if AUDIT_DEBUG == 2
1001 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1002 __FILE__, __LINE__, context->serial, name);
1003 dump_stack();
1004#endif
1005 return;
1006 }
1007 BUG_ON(context->name_count >= AUDIT_NAMES);
1008 context->names[context->name_count].name = name;
1009 context->names[context->name_count].ino = (unsigned long)-1;
1010 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001011 if (!context->pwd) {
1012 read_lock(&current->fs->lock);
1013 context->pwd = dget(current->fs->pwd);
1014 context->pwdmnt = mntget(current->fs->pwdmnt);
1015 read_unlock(&current->fs->lock);
1016 }
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001020/* audit_putname - intercept a putname request
1021 * @name: name to intercept and delay for putname
1022 *
1023 * If we have stored the name from getname in the audit context,
1024 * then we delay the putname until syscall exit.
1025 * Called from include/linux/fs.h:putname().
1026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027void audit_putname(const char *name)
1028{
1029 struct audit_context *context = current->audit_context;
1030
1031 BUG_ON(!context);
1032 if (!context->in_syscall) {
1033#if AUDIT_DEBUG == 2
1034 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1035 __FILE__, __LINE__, context->serial, name);
1036 if (context->name_count) {
1037 int i;
1038 for (i = 0; i < context->name_count; i++)
1039 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1040 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001041 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043#endif
1044 __putname(name);
1045 }
1046#if AUDIT_DEBUG
1047 else {
1048 ++context->put_count;
1049 if (context->put_count > context->name_count) {
1050 printk(KERN_ERR "%s:%d(:%d): major=%d"
1051 " in_syscall=%d putname(%p) name_count=%d"
1052 " put_count=%d\n",
1053 __FILE__, __LINE__,
1054 context->serial, context->major,
1055 context->in_syscall, name, context->name_count,
1056 context->put_count);
1057 dump_stack();
1058 }
1059 }
1060#endif
1061}
1062
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001063static void audit_inode_context(int idx, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001064{
1065 struct audit_context *context = current->audit_context;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001066
Steve Grubb1b50eed2006-04-03 14:06:13 -04001067 selinux_get_inode_sid(inode, &context->names[idx].osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001068}
1069
1070
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001071/**
1072 * audit_inode - store the inode and device from a lookup
1073 * @name: name being audited
1074 * @inode: inode being audited
1075 * @flags: lookup flags (as used in path_lookup())
1076 *
1077 * Called from fs/namei.c:path_lookup().
1078 */
Amy Griffis73241cc2005-11-03 16:00:25 +00001079void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 int idx;
1082 struct audit_context *context = current->audit_context;
1083
1084 if (!context->in_syscall)
1085 return;
1086 if (context->name_count
1087 && context->names[context->name_count-1].name
1088 && context->names[context->name_count-1].name == name)
1089 idx = context->name_count - 1;
1090 else if (context->name_count > 1
1091 && context->names[context->name_count-2].name
1092 && context->names[context->name_count-2].name == name)
1093 idx = context->name_count - 2;
1094 else {
1095 /* FIXME: how much do we care about inodes that have no
1096 * associated name? */
1097 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1098 return;
1099 idx = context->name_count++;
1100 context->names[idx].name = NULL;
1101#if AUDIT_DEBUG
1102 ++context->ino_count;
1103#endif
1104 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001105 context->names[idx].dev = inode->i_sb->s_dev;
1106 context->names[idx].mode = inode->i_mode;
1107 context->names[idx].uid = inode->i_uid;
1108 context->names[idx].gid = inode->i_gid;
1109 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001110 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001111 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1112 (strcmp(name, ".") != 0)) {
1113 context->names[idx].ino = (unsigned long)-1;
1114 context->names[idx].pino = inode->i_ino;
1115 } else {
1116 context->names[idx].ino = inode->i_ino;
1117 context->names[idx].pino = (unsigned long)-1;
1118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Amy Griffis73241cc2005-11-03 16:00:25 +00001121/**
1122 * audit_inode_child - collect inode info for created/removed objects
1123 * @dname: inode's dentry name
1124 * @inode: inode being audited
1125 * @pino: inode number of dentry parent
1126 *
1127 * For syscalls that create or remove filesystem objects, audit_inode
1128 * can only collect information for the filesystem object's parent.
1129 * This call updates the audit context with the child's information.
1130 * Syscalls that create a new filesystem object must be hooked after
1131 * the object is created. Syscalls that remove a filesystem object
1132 * must be hooked prior, in order to capture the target inode during
1133 * unsuccessful attempts.
1134 */
1135void __audit_inode_child(const char *dname, const struct inode *inode,
1136 unsigned long pino)
1137{
1138 int idx;
1139 struct audit_context *context = current->audit_context;
1140
1141 if (!context->in_syscall)
1142 return;
1143
1144 /* determine matching parent */
1145 if (dname)
1146 for (idx = 0; idx < context->name_count; idx++)
1147 if (context->names[idx].pino == pino) {
1148 const char *n;
1149 const char *name = context->names[idx].name;
1150 int dlen = strlen(dname);
1151 int nlen = name ? strlen(name) : 0;
1152
1153 if (nlen < dlen)
1154 continue;
1155
1156 /* disregard trailing slashes */
1157 n = name + nlen - 1;
1158 while ((*n == '/') && (n > name))
1159 n--;
1160
1161 /* find last path component */
1162 n = n - dlen + 1;
1163 if (n < name)
1164 continue;
1165 else if (n > name) {
1166 if (*--n != '/')
1167 continue;
1168 else
1169 n++;
1170 }
1171
1172 if (strncmp(n, dname, dlen) == 0)
1173 goto update_context;
1174 }
1175
1176 /* catch-all in case match not found */
1177 idx = context->name_count++;
1178 context->names[idx].name = NULL;
1179 context->names[idx].pino = pino;
1180#if AUDIT_DEBUG
1181 context->ino_count++;
1182#endif
1183
1184update_context:
1185 if (inode) {
1186 context->names[idx].ino = inode->i_ino;
1187 context->names[idx].dev = inode->i_sb->s_dev;
1188 context->names[idx].mode = inode->i_mode;
1189 context->names[idx].uid = inode->i_uid;
1190 context->names[idx].gid = inode->i_gid;
1191 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001192 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001196/**
1197 * auditsc_get_stamp - get local copies of audit_context values
1198 * @ctx: audit_context for the task
1199 * @t: timespec to store time recorded in the audit_context
1200 * @serial: serial value that is recorded in the audit_context
1201 *
1202 * Also sets the context as auditable.
1203 */
David Woodhousebfb44962005-05-21 21:08:09 +01001204void auditsc_get_stamp(struct audit_context *ctx,
1205 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
David Woodhousece625a82005-07-18 14:24:46 -04001207 if (!ctx->serial)
1208 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001209 t->tv_sec = ctx->ctime.tv_sec;
1210 t->tv_nsec = ctx->ctime.tv_nsec;
1211 *serial = ctx->serial;
1212 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001215/**
1216 * audit_set_loginuid - set a task's audit_context loginuid
1217 * @task: task whose audit context is being modified
1218 * @loginuid: loginuid value
1219 *
1220 * Returns 0.
1221 *
1222 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1223 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001224int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Steve Grubb456be6c2005-04-29 17:30:07 +01001226 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001227 struct audit_buffer *ab;
1228
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001229 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001230 if (ab) {
1231 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001232 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001233 task->pid, task->uid,
1234 task->audit_context->loginuid, loginuid);
1235 audit_log_end(ab);
1236 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001237 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239 return 0;
1240}
1241
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001242/**
1243 * audit_get_loginuid - get the loginuid for an audit_context
1244 * @ctx: the audit_context
1245 *
1246 * Returns the context's loginuid or -1 if @ctx is NULL.
1247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248uid_t audit_get_loginuid(struct audit_context *ctx)
1249{
1250 return ctx ? ctx->loginuid : -1;
1251}
1252
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001253/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05001254 * __audit_mq_open - record audit data for a POSIX MQ open
1255 * @oflag: open flag
1256 * @mode: mode bits
1257 * @u_attr: queue attributes
1258 *
1259 * Returns 0 for success or NULL context or < 0 on error.
1260 */
1261int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1262{
1263 struct audit_aux_data_mq_open *ax;
1264 struct audit_context *context = current->audit_context;
1265
1266 if (!audit_enabled)
1267 return 0;
1268
1269 if (likely(!context))
1270 return 0;
1271
1272 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1273 if (!ax)
1274 return -ENOMEM;
1275
1276 if (u_attr != NULL) {
1277 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1278 kfree(ax);
1279 return -EFAULT;
1280 }
1281 } else
1282 memset(&ax->attr, 0, sizeof(ax->attr));
1283
1284 ax->oflag = oflag;
1285 ax->mode = mode;
1286
1287 ax->d.type = AUDIT_MQ_OPEN;
1288 ax->d.next = context->aux;
1289 context->aux = (void *)ax;
1290 return 0;
1291}
1292
1293/**
1294 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1295 * @mqdes: MQ descriptor
1296 * @msg_len: Message length
1297 * @msg_prio: Message priority
1298 * @abs_timeout: Message timeout in absolute time
1299 *
1300 * Returns 0 for success or NULL context or < 0 on error.
1301 */
1302int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1303 const struct timespec __user *u_abs_timeout)
1304{
1305 struct audit_aux_data_mq_sendrecv *ax;
1306 struct audit_context *context = current->audit_context;
1307
1308 if (!audit_enabled)
1309 return 0;
1310
1311 if (likely(!context))
1312 return 0;
1313
1314 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1315 if (!ax)
1316 return -ENOMEM;
1317
1318 if (u_abs_timeout != NULL) {
1319 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1320 kfree(ax);
1321 return -EFAULT;
1322 }
1323 } else
1324 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1325
1326 ax->mqdes = mqdes;
1327 ax->msg_len = msg_len;
1328 ax->msg_prio = msg_prio;
1329
1330 ax->d.type = AUDIT_MQ_SENDRECV;
1331 ax->d.next = context->aux;
1332 context->aux = (void *)ax;
1333 return 0;
1334}
1335
1336/**
1337 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1338 * @mqdes: MQ descriptor
1339 * @msg_len: Message length
1340 * @msg_prio: Message priority
1341 * @abs_timeout: Message timeout in absolute time
1342 *
1343 * Returns 0 for success or NULL context or < 0 on error.
1344 */
1345int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1346 unsigned int __user *u_msg_prio,
1347 const struct timespec __user *u_abs_timeout)
1348{
1349 struct audit_aux_data_mq_sendrecv *ax;
1350 struct audit_context *context = current->audit_context;
1351
1352 if (!audit_enabled)
1353 return 0;
1354
1355 if (likely(!context))
1356 return 0;
1357
1358 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1359 if (!ax)
1360 return -ENOMEM;
1361
1362 if (u_msg_prio != NULL) {
1363 if (get_user(ax->msg_prio, u_msg_prio)) {
1364 kfree(ax);
1365 return -EFAULT;
1366 }
1367 } else
1368 ax->msg_prio = 0;
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
1381 ax->d.type = AUDIT_MQ_SENDRECV;
1382 ax->d.next = context->aux;
1383 context->aux = (void *)ax;
1384 return 0;
1385}
1386
1387/**
1388 * __audit_mq_notify - record audit data for a POSIX MQ notify
1389 * @mqdes: MQ descriptor
1390 * @u_notification: Notification event
1391 *
1392 * Returns 0 for success or NULL context or < 0 on error.
1393 */
1394
1395int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1396{
1397 struct audit_aux_data_mq_notify *ax;
1398 struct audit_context *context = current->audit_context;
1399
1400 if (!audit_enabled)
1401 return 0;
1402
1403 if (likely(!context))
1404 return 0;
1405
1406 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1407 if (!ax)
1408 return -ENOMEM;
1409
1410 if (u_notification != NULL) {
1411 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1412 kfree(ax);
1413 return -EFAULT;
1414 }
1415 } else
1416 memset(&ax->notification, 0, sizeof(ax->notification));
1417
1418 ax->mqdes = mqdes;
1419
1420 ax->d.type = AUDIT_MQ_NOTIFY;
1421 ax->d.next = context->aux;
1422 context->aux = (void *)ax;
1423 return 0;
1424}
1425
1426/**
1427 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1428 * @mqdes: MQ descriptor
1429 * @mqstat: MQ flags
1430 *
1431 * Returns 0 for success or NULL context or < 0 on error.
1432 */
1433int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1434{
1435 struct audit_aux_data_mq_getsetattr *ax;
1436 struct audit_context *context = current->audit_context;
1437
1438 if (!audit_enabled)
1439 return 0;
1440
1441 if (likely(!context))
1442 return 0;
1443
1444 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1445 if (!ax)
1446 return -ENOMEM;
1447
1448 ax->mqdes = mqdes;
1449 ax->mqstat = *mqstat;
1450
1451 ax->d.type = AUDIT_MQ_GETSETATTR;
1452 ax->d.next = context->aux;
1453 context->aux = (void *)ax;
1454 return 0;
1455}
1456
1457/**
Steve Grubb073115d2006-04-02 17:07:33 -04001458 * audit_ipc_obj - record audit data for ipc object
1459 * @ipcp: ipc permissions
1460 *
1461 * Returns 0 for success or NULL context or < 0 on error.
1462 */
Al Virod8945bb52006-05-18 16:01:30 -04001463int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04001464{
1465 struct audit_aux_data_ipcctl *ax;
1466 struct audit_context *context = current->audit_context;
1467
Steve Grubb073115d2006-04-02 17:07:33 -04001468 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1469 if (!ax)
1470 return -ENOMEM;
1471
1472 ax->uid = ipcp->uid;
1473 ax->gid = ipcp->gid;
1474 ax->mode = ipcp->mode;
1475 selinux_get_ipc_sid(ipcp, &ax->osid);
1476
1477 ax->d.type = AUDIT_IPC;
1478 ax->d.next = context->aux;
1479 context->aux = (void *)ax;
1480 return 0;
1481}
1482
1483/**
1484 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001485 * @qbytes: msgq bytes
1486 * @uid: msgq user id
1487 * @gid: msgq group id
1488 * @mode: msgq mode (permissions)
1489 *
1490 * Returns 0 for success or NULL context or < 0 on error.
1491 */
Al Virod8945bb52006-05-18 16:01:30 -04001492int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
1494 struct audit_aux_data_ipcctl *ax;
1495 struct audit_context *context = current->audit_context;
1496
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001497 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (!ax)
1499 return -ENOMEM;
1500
1501 ax->qbytes = qbytes;
1502 ax->uid = uid;
1503 ax->gid = gid;
1504 ax->mode = mode;
1505
Steve Grubb073115d2006-04-02 17:07:33 -04001506 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 ax->d.next = context->aux;
1508 context->aux = (void *)ax;
1509 return 0;
1510}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001511
Al Viro473ae302006-04-26 14:04:08 -04001512int audit_bprm(struct linux_binprm *bprm)
1513{
1514 struct audit_aux_data_execve *ax;
1515 struct audit_context *context = current->audit_context;
1516 unsigned long p, next;
1517 void *to;
1518
1519 if (likely(!audit_enabled || !context))
1520 return 0;
1521
1522 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1523 GFP_KERNEL);
1524 if (!ax)
1525 return -ENOMEM;
1526
1527 ax->argc = bprm->argc;
1528 ax->envc = bprm->envc;
1529 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1530 struct page *page = bprm->page[p / PAGE_SIZE];
1531 void *kaddr = kmap(page);
1532 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1533 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1534 to += next - p;
1535 kunmap(page);
1536 }
1537
1538 ax->d.type = AUDIT_EXECVE;
1539 ax->d.next = context->aux;
1540 context->aux = (void *)ax;
1541 return 0;
1542}
1543
1544
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001545/**
1546 * audit_socketcall - record audit data for sys_socketcall
1547 * @nargs: number of args
1548 * @args: args array
1549 *
1550 * Returns 0 for success or NULL context or < 0 on error.
1551 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001552int audit_socketcall(int nargs, unsigned long *args)
1553{
1554 struct audit_aux_data_socketcall *ax;
1555 struct audit_context *context = current->audit_context;
1556
1557 if (likely(!context))
1558 return 0;
1559
1560 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1561 if (!ax)
1562 return -ENOMEM;
1563
1564 ax->nargs = nargs;
1565 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1566
1567 ax->d.type = AUDIT_SOCKETCALL;
1568 ax->d.next = context->aux;
1569 context->aux = (void *)ax;
1570 return 0;
1571}
1572
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001573/**
1574 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1575 * @len: data length in user space
1576 * @a: data address in kernel space
1577 *
1578 * Returns 0 for success or NULL context or < 0 on error.
1579 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001580int audit_sockaddr(int len, void *a)
1581{
1582 struct audit_aux_data_sockaddr *ax;
1583 struct audit_context *context = current->audit_context;
1584
1585 if (likely(!context))
1586 return 0;
1587
1588 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1589 if (!ax)
1590 return -ENOMEM;
1591
1592 ax->len = len;
1593 memcpy(ax->a, a, len);
1594
1595 ax->d.type = AUDIT_SOCKADDR;
1596 ax->d.next = context->aux;
1597 context->aux = (void *)ax;
1598 return 0;
1599}
1600
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001601/**
1602 * audit_avc_path - record the granting or denial of permissions
1603 * @dentry: dentry to record
1604 * @mnt: mnt to record
1605 *
1606 * Returns 0 for success or NULL context or < 0 on error.
1607 *
1608 * Called from security/selinux/avc.c::avc_audit()
1609 */
Stephen Smalley01116102005-05-21 00:15:52 +01001610int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1611{
1612 struct audit_aux_data_path *ax;
1613 struct audit_context *context = current->audit_context;
1614
1615 if (likely(!context))
1616 return 0;
1617
1618 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1619 if (!ax)
1620 return -ENOMEM;
1621
1622 ax->dentry = dget(dentry);
1623 ax->mnt = mntget(mnt);
1624
1625 ax->d.type = AUDIT_AVC_PATH;
1626 ax->d.next = context->aux;
1627 context->aux = (void *)ax;
1628 return 0;
1629}
1630
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001631/**
1632 * audit_signal_info - record signal info for shutting down audit subsystem
1633 * @sig: signal value
1634 * @t: task being signaled
1635 *
1636 * If the audit subsystem is being terminated, record the task (pid)
1637 * and uid that is doing that.
1638 */
Al Viroe1396062006-05-25 10:19:47 -04001639void __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001640{
1641 extern pid_t audit_sig_pid;
1642 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04001643 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001644
Al Viroe1396062006-05-25 10:19:47 -04001645 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1646 struct task_struct *tsk = current;
1647 struct audit_context *ctx = tsk->audit_context;
1648 audit_sig_pid = tsk->pid;
1649 if (ctx)
1650 audit_sig_uid = ctx->loginuid;
1651 else
1652 audit_sig_uid = tsk->uid;
1653 selinux_get_task_sid(tsk, &audit_sig_sid);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001654 }
1655}