blob: b097ccb4eb7ea8e10145a29b6f2e1fc14120e0d5 [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
Amy Griffis9c937dc2006-06-08 23:19:31 -040085/* Indicates that audit should log the full pathname. */
86#define AUDIT_NAME_FULL -1
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* When fs/namei.c:getname() is called, we store the pointer in name and
89 * we don't let putname() free it (instead we free all of the saved
90 * pointers at syscall exit time).
91 *
92 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
93struct audit_names {
94 const char *name;
Amy Griffis9c937dc2006-06-08 23:19:31 -040095 int name_len; /* number of name's characters to log */
96 unsigned name_put; /* call __putname() for this name */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unsigned long ino;
98 dev_t dev;
99 umode_t mode;
100 uid_t uid;
101 gid_t gid;
102 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400103 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106struct audit_aux_data {
107 struct audit_aux_data *next;
108 int type;
109};
110
111#define AUDIT_AUX_IPCPERM 0
112
George C. Wilson20ca73b2006-05-24 16:09:55 -0500113struct audit_aux_data_mq_open {
114 struct audit_aux_data d;
115 int oflag;
116 mode_t mode;
117 struct mq_attr attr;
118};
119
120struct audit_aux_data_mq_sendrecv {
121 struct audit_aux_data d;
122 mqd_t mqdes;
123 size_t msg_len;
124 unsigned int msg_prio;
125 struct timespec abs_timeout;
126};
127
128struct audit_aux_data_mq_notify {
129 struct audit_aux_data d;
130 mqd_t mqdes;
131 struct sigevent notification;
132};
133
134struct audit_aux_data_mq_getsetattr {
135 struct audit_aux_data d;
136 mqd_t mqdes;
137 struct mq_attr mqstat;
138};
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct audit_aux_data_ipcctl {
141 struct audit_aux_data d;
142 struct ipc_perm p;
143 unsigned long qbytes;
144 uid_t uid;
145 gid_t gid;
146 mode_t mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500147 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Al Viro473ae302006-04-26 14:04:08 -0400150struct audit_aux_data_execve {
151 struct audit_aux_data d;
152 int argc;
153 int envc;
154 char mem[0];
155};
156
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100157struct audit_aux_data_socketcall {
158 struct audit_aux_data d;
159 int nargs;
160 unsigned long args[0];
161};
162
163struct audit_aux_data_sockaddr {
164 struct audit_aux_data d;
165 int len;
166 char a[0];
167};
168
Stephen Smalley01116102005-05-21 00:15:52 +0100169struct audit_aux_data_path {
170 struct audit_aux_data d;
171 struct dentry *dentry;
172 struct vfsmount *mnt;
173};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175/* The per-task audit context. */
176struct audit_context {
177 int in_syscall; /* 1 if task is in a syscall */
178 enum audit_state state;
179 unsigned int serial; /* serial number for record */
180 struct timespec ctime; /* time of syscall entry */
181 uid_t loginuid; /* login uid (identity) */
182 int major; /* syscall number */
183 unsigned long argv[4]; /* syscall arguments */
184 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100185 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int auditable; /* 1 if record should be written */
187 int name_count;
188 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100189 struct dentry * pwd;
190 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct audit_context *previous; /* For nested syscalls */
192 struct audit_aux_data *aux;
193
194 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400195 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 uid_t uid, euid, suid, fsuid;
197 gid_t gid, egid, sgid, fsgid;
198 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100199 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201#if AUDIT_DEBUG
202 int put_count;
203 int ino_count;
204#endif
205};
206
Amy Griffisf368c07d2006-04-07 16:55:56 -0400207/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* Compare a task_struct with an audit_rule. Return 1 on match, 0
209 * otherwise. */
210static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500211 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400213 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 enum audit_state *state)
215{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400216 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600217 u32 sid;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500220 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int result = 0;
222
Amy Griffis93315ed2006-02-07 12:05:27 -0500223 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500225 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 break;
Al Viro3c662512006-05-06 08:26:27 -0400227 case AUDIT_PPID:
228 if (ctx)
229 result = audit_comparator(ctx->ppid, f->op, f->val);
230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500232 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500235 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 break;
237 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500238 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 break;
240 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500241 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 break;
243 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500244 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500247 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 break;
249 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500250 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
252 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500253 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 break;
255 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500256 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 break;
2fd6f582005-04-29 16:08:28 +0100258 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000259 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500260 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 case AUDIT_EXIT:
264 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500265 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100268 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500269 if (f->val)
270 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100271 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500272 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400276 if (name)
277 result = audit_comparator(MAJOR(name->dev),
278 f->op, f->val);
279 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500281 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 ++result;
283 break;
284 }
285 }
286 }
287 break;
288 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400289 if (name)
290 result = audit_comparator(MINOR(name->dev),
291 f->op, f->val);
292 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500294 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 ++result;
296 break;
297 }
298 }
299 }
300 break;
301 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400302 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400303 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400304 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400306 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ++result;
308 break;
309 }
310 }
311 }
312 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400313 case AUDIT_WATCH:
314 if (name && rule->watch->ino != (unsigned long)-1)
315 result = (name->dev == rule->watch->dev &&
Amy Griffis9c937dc2006-06-08 23:19:31 -0400316 name->ino == rule->watch->ino);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 case AUDIT_LOGINUID:
319 result = 0;
320 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500321 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600323 case AUDIT_SE_USER:
324 case AUDIT_SE_ROLE:
325 case AUDIT_SE_TYPE:
326 case AUDIT_SE_SEN:
327 case AUDIT_SE_CLR:
328 /* NOTE: this may return negative values indicating
329 a temporary error. We simply treat this as a
330 match for now to avoid losing information that
331 may be wanted. An error message will also be
332 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400333 if (f->se_rule) {
334 if (need_sid) {
335 selinux_task_ctxid(tsk, &sid);
336 need_sid = 0;
337 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600338 result = selinux_audit_rule_match(sid, f->type,
339 f->op,
340 f->se_rule,
341 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400342 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 case AUDIT_ARG0:
345 case AUDIT_ARG1:
346 case AUDIT_ARG2:
347 case AUDIT_ARG3:
348 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500349 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351 }
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!result)
354 return 0;
355 }
356 switch (rule->action) {
357 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
359 }
360 return 1;
361}
362
363/* At process creation time, we can determine if system-call auditing is
364 * completely disabled for this task. Since we only have the task
365 * structure at this point, we can only check uid and gid.
366 */
367static enum audit_state audit_filter_task(struct task_struct *tsk)
368{
369 struct audit_entry *e;
370 enum audit_state state;
371
372 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100373 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400374 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 rcu_read_unlock();
376 return state;
377 }
378 }
379 rcu_read_unlock();
380 return AUDIT_BUILD_CONTEXT;
381}
382
383/* At syscall entry and exit time, this filter is called if the
384 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100385 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700386 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 */
388static enum audit_state audit_filter_syscall(struct task_struct *tsk,
389 struct audit_context *ctx,
390 struct list_head *list)
391{
392 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100393 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David Woodhouse351bb722005-07-14 14:40:06 +0100395 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100396 return AUDIT_DISABLED;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100399 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000400 int word = AUDIT_WORD(ctx->major);
401 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100402
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000403 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400404 if ((e->rule.mask[word] & bit) == bit &&
405 audit_filter_rules(tsk, &e->rule, ctx, NULL,
406 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000407 rcu_read_unlock();
408 return state;
409 }
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 rcu_read_unlock();
413 return AUDIT_BUILD_CONTEXT;
414}
415
Amy Griffisf368c07d2006-04-07 16:55:56 -0400416/* At syscall exit time, this filter is called if any audit_names[] have been
417 * collected during syscall processing. We only check rules in sublists at hash
418 * buckets applicable to the inode numbers in audit_names[].
419 * Regarding audit_state, same rules apply as for audit_filter_syscall().
420 */
421enum audit_state audit_filter_inodes(struct task_struct *tsk,
422 struct audit_context *ctx)
423{
424 int i;
425 struct audit_entry *e;
426 enum audit_state state;
427
428 if (audit_pid && tsk->tgid == audit_pid)
429 return AUDIT_DISABLED;
430
431 rcu_read_lock();
432 for (i = 0; i < ctx->name_count; i++) {
433 int word = AUDIT_WORD(ctx->major);
434 int bit = AUDIT_BIT(ctx->major);
435 struct audit_names *n = &ctx->names[i];
436 int h = audit_hash_ino((u32)n->ino);
437 struct list_head *list = &audit_inode_hash[h];
438
439 if (list_empty(list))
440 continue;
441
442 list_for_each_entry_rcu(e, list, list) {
443 if ((e->rule.mask[word] & bit) == bit &&
444 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
445 rcu_read_unlock();
446 return state;
447 }
448 }
449 }
450 rcu_read_unlock();
451 return AUDIT_BUILD_CONTEXT;
452}
453
454void audit_set_auditable(struct audit_context *ctx)
455{
456 ctx->auditable = 1;
457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459static inline struct audit_context *audit_get_context(struct task_struct *tsk,
460 int return_valid,
461 int return_code)
462{
463 struct audit_context *context = tsk->audit_context;
464
465 if (likely(!context))
466 return NULL;
467 context->return_valid = return_valid;
468 context->return_code = return_code;
469
David Woodhouse21af6c42005-07-02 14:10:46 +0100470 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400472
David Woodhouse0f45aa12005-06-19 19:35:50 +0100473 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400474 if (state == AUDIT_RECORD_CONTEXT) {
475 context->auditable = 1;
476 goto get_context;
477 }
478
479 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (state == AUDIT_RECORD_CONTEXT)
481 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484
Amy Griffisf368c07d2006-04-07 16:55:56 -0400485get_context:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 context->pid = tsk->pid;
Al Virof46038f2006-05-06 08:22:52 -0400487 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 context->uid = tsk->uid;
489 context->gid = tsk->gid;
490 context->euid = tsk->euid;
491 context->suid = tsk->suid;
492 context->fsuid = tsk->fsuid;
493 context->egid = tsk->egid;
494 context->sgid = tsk->sgid;
495 context->fsgid = tsk->fsgid;
496 context->personality = tsk->personality;
497 tsk->audit_context = NULL;
498 return context;
499}
500
501static inline void audit_free_names(struct audit_context *context)
502{
503 int i;
504
505#if AUDIT_DEBUG == 2
506 if (context->auditable
507 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000508 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 " name_count=%d put_count=%d"
510 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000511 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 context->serial, context->major, context->in_syscall,
513 context->name_count, context->put_count,
514 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000515 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 printk(KERN_ERR "names[%d] = %p = %s\n", i,
517 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000518 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 dump_stack();
521 return;
522 }
523#endif
524#if AUDIT_DEBUG
525 context->put_count = 0;
526 context->ino_count = 0;
527#endif
528
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000529 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400530 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100534 if (context->pwd)
535 dput(context->pwd);
536 if (context->pwdmnt)
537 mntput(context->pwdmnt);
538 context->pwd = NULL;
539 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542static inline void audit_free_aux(struct audit_context *context)
543{
544 struct audit_aux_data *aux;
545
546 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100547 if (aux->type == AUDIT_AVC_PATH) {
548 struct audit_aux_data_path *axi = (void *)aux;
549 dput(axi->dentry);
550 mntput(axi->mnt);
551 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 context->aux = aux->next;
554 kfree(aux);
555 }
556}
557
558static inline void audit_zero_context(struct audit_context *context,
559 enum audit_state state)
560{
561 uid_t loginuid = context->loginuid;
562
563 memset(context, 0, sizeof(*context));
564 context->state = state;
565 context->loginuid = loginuid;
566}
567
568static inline struct audit_context *audit_alloc_context(enum audit_state state)
569{
570 struct audit_context *context;
571
572 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
573 return NULL;
574 audit_zero_context(context, state);
575 return context;
576}
577
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700578/**
579 * audit_alloc - allocate an audit context block for a task
580 * @tsk: task
581 *
582 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * if necessary. Doing so turns on system call auditing for the
584 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700585 * needed.
586 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587int audit_alloc(struct task_struct *tsk)
588{
589 struct audit_context *context;
590 enum audit_state state;
591
592 if (likely(!audit_enabled))
593 return 0; /* Return if not auditing. */
594
595 state = audit_filter_task(tsk);
596 if (likely(state == AUDIT_DISABLED))
597 return 0;
598
599 if (!(context = audit_alloc_context(state))) {
600 audit_log_lost("out of memory in audit_alloc");
601 return -ENOMEM;
602 }
603
604 /* Preserve login uid */
605 context->loginuid = -1;
606 if (current->audit_context)
607 context->loginuid = current->audit_context->loginuid;
608
609 tsk->audit_context = context;
610 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
611 return 0;
612}
613
614static inline void audit_free_context(struct audit_context *context)
615{
616 struct audit_context *previous;
617 int count = 0;
618
619 do {
620 previous = context->previous;
621 if (previous || (count && count < 10)) {
622 ++count;
623 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
624 " freeing multiple contexts (%d)\n",
625 context->serial, context->major,
626 context->name_count, count);
627 }
628 audit_free_names(context);
629 audit_free_aux(context);
630 kfree(context);
631 context = previous;
632 } while (context);
633 if (count >= 10)
634 printk(KERN_ERR "audit: freed %d contexts\n", count);
635}
636
Al Viroe4951492006-03-29 20:17:10 -0500637static void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000638{
639 char *ctx = NULL;
640 ssize_t len = 0;
641
642 len = security_getprocattr(current, "current", NULL, 0);
643 if (len < 0) {
644 if (len != -EINVAL)
645 goto error_path;
646 return;
647 }
648
Al Viroe4951492006-03-29 20:17:10 -0500649 ctx = kmalloc(len, GFP_KERNEL);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000650 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000651 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000652
653 len = security_getprocattr(current, "current", ctx, len);
654 if (len < 0 )
655 goto error_path;
656
657 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000658 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000659
660error_path:
661 if (ctx)
662 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000663 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000664 return;
665}
666
Al Viroe4951492006-03-29 20:17:10 -0500667static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700668{
Al Viro45d9bb02006-03-29 20:02:55 -0500669 char name[sizeof(tsk->comm)];
670 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700671 struct vm_area_struct *vma;
672
Al Viroe4951492006-03-29 20:17:10 -0500673 /* tsk == current */
674
Al Viro45d9bb02006-03-29 20:02:55 -0500675 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100676 audit_log_format(ab, " comm=");
677 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700678
Al Viroe4951492006-03-29 20:17:10 -0500679 if (mm) {
680 down_read(&mm->mmap_sem);
681 vma = mm->mmap;
682 while (vma) {
683 if ((vma->vm_flags & VM_EXECUTABLE) &&
684 vma->vm_file) {
685 audit_log_d_path(ab, "exe=",
686 vma->vm_file->f_dentry,
687 vma->vm_file->f_vfsmnt);
688 break;
689 }
690 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700691 }
Al Viroe4951492006-03-29 20:17:10 -0500692 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700693 }
Al Viroe4951492006-03-29 20:17:10 -0500694 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700695}
696
Al Viroe4951492006-03-29 20:17:10 -0500697static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500699 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100701 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500702 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Al Viroe4951492006-03-29 20:17:10 -0500704 /* tsk == current */
705
706 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!ab)
708 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100709 audit_log_format(ab, "arch=%x syscall=%d",
710 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (context->personality != PER_LINUX)
712 audit_log_format(ab, " per=%lx", context->personality);
713 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100714 audit_log_format(ab, " success=%s exit=%ld",
715 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
716 context->return_code);
Al Viro45d9bb02006-03-29 20:02:55 -0500717 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
718 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500719 else
720 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 audit_log_format(ab,
722 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -0400723 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +0100724 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500725 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 context->argv[0],
727 context->argv[1],
728 context->argv[2],
729 context->argv[3],
730 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -0400731 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 context->pid,
733 context->loginuid,
734 context->uid,
735 context->gid,
736 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500737 context->egid, context->sgid, context->fsgid, tty);
Al Viroe4951492006-03-29 20:17:10 -0500738 audit_log_task_info(ab, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
David Woodhouse7551ced2005-05-26 12:04:57 +0100741 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100742
Al Viroe4951492006-03-29 20:17:10 -0500743 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!ab)
745 continue; /* audit_panic has been called */
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -0500748 case AUDIT_MQ_OPEN: {
749 struct audit_aux_data_mq_open *axi = (void *)aux;
750 audit_log_format(ab,
751 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
752 "mq_msgsize=%ld mq_curmsgs=%ld",
753 axi->oflag, axi->mode, axi->attr.mq_flags,
754 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
755 axi->attr.mq_curmsgs);
756 break; }
757
758 case AUDIT_MQ_SENDRECV: {
759 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
760 audit_log_format(ab,
761 "mqdes=%d msg_len=%zd msg_prio=%u "
762 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
763 axi->mqdes, axi->msg_len, axi->msg_prio,
764 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
765 break; }
766
767 case AUDIT_MQ_NOTIFY: {
768 struct audit_aux_data_mq_notify *axi = (void *)aux;
769 audit_log_format(ab,
770 "mqdes=%d sigev_signo=%d",
771 axi->mqdes,
772 axi->notification.sigev_signo);
773 break; }
774
775 case AUDIT_MQ_GETSETATTR: {
776 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
777 audit_log_format(ab,
778 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
779 "mq_curmsgs=%ld ",
780 axi->mqdes,
781 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
782 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
783 break; }
784
Steve Grubbc0404992005-05-13 18:17:42 +0100785 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct audit_aux_data_ipcctl *axi = (void *)aux;
787 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400788 "ouid=%u ogid=%u mode=%x",
789 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500790 if (axi->osid != 0) {
791 char *ctx = NULL;
792 u32 len;
793 if (selinux_ctxid_to_string(
794 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500795 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500796 axi->osid);
797 call_panic = 1;
798 } else
799 audit_log_format(ab, " obj=%s", ctx);
800 kfree(ctx);
801 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100802 break; }
803
Steve Grubb073115d2006-04-02 17:07:33 -0400804 case AUDIT_IPC_SET_PERM: {
805 struct audit_aux_data_ipcctl *axi = (void *)aux;
806 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400807 "qbytes=%lx ouid=%u ogid=%u mode=%x",
Steve Grubb073115d2006-04-02 17:07:33 -0400808 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400809 break; }
Linda Knippersac032212006-05-16 22:03:48 -0400810
Al Viro473ae302006-04-26 14:04:08 -0400811 case AUDIT_EXECVE: {
812 struct audit_aux_data_execve *axi = (void *)aux;
813 int i;
814 const char *p;
815 for (i = 0, p = axi->mem; i < axi->argc; i++) {
816 audit_log_format(ab, "a%d=", i);
817 p = audit_log_untrustedstring(ab, p);
818 audit_log_format(ab, "\n");
819 }
820 break; }
Steve Grubb073115d2006-04-02 17:07:33 -0400821
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100822 case AUDIT_SOCKETCALL: {
823 int i;
824 struct audit_aux_data_socketcall *axs = (void *)aux;
825 audit_log_format(ab, "nargs=%d", axs->nargs);
826 for (i=0; i<axs->nargs; i++)
827 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
828 break; }
829
830 case AUDIT_SOCKADDR: {
831 struct audit_aux_data_sockaddr *axs = (void *)aux;
832
833 audit_log_format(ab, "saddr=");
834 audit_log_hex(ab, axs->a, axs->len);
835 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100836
837 case AUDIT_AVC_PATH: {
838 struct audit_aux_data_path *axi = (void *)aux;
839 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100840 break; }
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
David Woodhouse8f37d472005-05-27 12:17:28 +0100846 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -0500847 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100848 if (ab) {
849 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
850 audit_log_end(ab);
851 }
852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400854 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +0000855
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
Amy Griffis9c937dc2006-06-08 23:19:31 -0400862 if (n->name) {
863 switch(n->name_len) {
864 case AUDIT_NAME_FULL:
865 /* log the full path */
866 audit_log_format(ab, " name=");
867 audit_log_untrustedstring(ab, n->name);
868 break;
869 case 0:
870 /* name was specified as a relative path and the
871 * directory component is the cwd */
872 audit_log_d_path(ab, " name=", context->pwd,
873 context->pwdmnt);
874 break;
875 default:
876 /* log the name's directory component */
877 audit_log_format(ab, " name=");
878 audit_log_n_untrustedstring(ab, n->name_len,
879 n->name);
880 }
881 } else
882 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +0000883
Amy Griffis9c937dc2006-06-08 23:19:31 -0400884 if (n->ino != (unsigned long)-1) {
885 audit_log_format(ab, " inode=%lu"
886 " dev=%02x:%02x mode=%#o"
887 " ouid=%u ogid=%u rdev=%02x:%02x",
888 n->ino,
889 MAJOR(n->dev),
890 MINOR(n->dev),
891 n->mode,
892 n->uid,
893 n->gid,
894 MAJOR(n->rdev),
895 MINOR(n->rdev));
896 }
897 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -0400898 char *ctx = NULL;
899 u32 len;
900 if (selinux_ctxid_to_string(
Amy Griffis9c937dc2006-06-08 23:19:31 -0400901 n->osid, &ctx, &len)) {
902 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500903 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400904 } else
905 audit_log_format(ab, " obj=%s", ctx);
906 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000907 }
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 audit_log_end(ab);
910 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500911 if (call_panic)
912 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700915/**
916 * audit_free - free a per-task audit context
917 * @tsk: task whose audit context block to free
918 *
Al Virofa84cb92006-03-29 20:30:19 -0500919 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921void audit_free(struct task_struct *tsk)
922{
923 struct audit_context *context;
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (likely(!context))
927 return;
928
929 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100930 * function (e.g., exit_group), then free context block.
931 * We use GFP_ATOMIC here because we might be doing this
932 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -0500933 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +0100934 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500935 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 audit_free_context(context);
938}
939
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700940/**
941 * audit_syscall_entry - fill in an audit record at syscall entry
942 * @tsk: task being audited
943 * @arch: architecture type
944 * @major: major syscall type (function)
945 * @a1: additional syscall register 1
946 * @a2: additional syscall register 2
947 * @a3: additional syscall register 3
948 * @a4: additional syscall register 4
949 *
950 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * audit context was created when the task was created and the state or
952 * filters demand the audit context be built. If the state from the
953 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
954 * then the record will be written at syscall exit time (otherwise, it
955 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700956 * be written).
957 */
Al Viro5411be52006-03-29 20:23:36 -0500958void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 unsigned long a1, unsigned long a2,
960 unsigned long a3, unsigned long a4)
961{
Al Viro5411be52006-03-29 20:23:36 -0500962 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 struct audit_context *context = tsk->audit_context;
964 enum audit_state state;
965
966 BUG_ON(!context);
967
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700968 /*
969 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * calls in kernel_thread via the entry.S interface, instead of
971 * with direct calls. (If you are porting to a new
972 * architecture, hitting this condition can indicate that you
973 * got the _exit/_leave calls backward in entry.S.)
974 *
975 * i386 no
976 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -0600977 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *
979 * This also happens with vm86 emulation in a non-nested manner
980 * (entries without exits), so this case must be caught.
981 */
982 if (context->in_syscall) {
983 struct audit_context *newctx;
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985#if AUDIT_DEBUG
986 printk(KERN_ERR
987 "audit(:%d) pid=%d in syscall=%d;"
988 " entering syscall=%d\n",
989 context->serial, tsk->pid, context->major, major);
990#endif
991 newctx = audit_alloc_context(context->state);
992 if (newctx) {
993 newctx->previous = context;
994 context = newctx;
995 tsk->audit_context = newctx;
996 } else {
997 /* If we can't alloc a new context, the best we
998 * can do is to leak memory (any pending putname
999 * will be lost). The only other alternative is
1000 * to abandon auditing. */
1001 audit_zero_context(context, context->state);
1002 }
1003 }
1004 BUG_ON(context->in_syscall || context->name_count);
1005
1006 if (!audit_enabled)
1007 return;
1008
2fd6f582005-04-29 16:08:28 +01001009 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 context->major = major;
1011 context->argv[0] = a1;
1012 context->argv[1] = a2;
1013 context->argv[2] = a3;
1014 context->argv[3] = a4;
1015
1016 state = context->state;
1017 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +01001018 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (likely(state == AUDIT_DISABLED))
1020 return;
1021
David Woodhousece625a82005-07-18 14:24:46 -04001022 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 context->ctime = CURRENT_TIME;
1024 context->in_syscall = 1;
1025 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1026}
1027
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001028/**
1029 * audit_syscall_exit - deallocate audit context after a system call
1030 * @tsk: task being audited
1031 * @valid: success/failure flag
1032 * @return_code: syscall return value
1033 *
1034 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1036 * filtering, or because some other part of the kernel write an audit
1037 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001038 * free the names stored from getname().
1039 */
Al Viro5411be52006-03-29 20:23:36 -05001040void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Al Viro5411be52006-03-29 20:23:36 -05001042 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct audit_context *context;
1044
2fd6f582005-04-29 16:08:28 +01001045 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001048 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
David Woodhousef7056d62005-06-20 16:07:33 +01001050 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001051 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 context->in_syscall = 0;
1054 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (context->previous) {
1057 struct audit_context *new_context = context->previous;
1058 context->previous = NULL;
1059 audit_free_context(context);
1060 tsk->audit_context = new_context;
1061 } else {
1062 audit_free_names(context);
1063 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 tsk->audit_context = context;
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001068/**
1069 * audit_getname - add a name to the list
1070 * @name: name to add
1071 *
1072 * Add a name to the list of audit names for this context.
1073 * Called from fs/namei.c:getname().
1074 */
Al Virod8945bb52006-05-18 16:01:30 -04001075void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 struct audit_context *context = current->audit_context;
1078
Al Virod8945bb52006-05-18 16:01:30 -04001079 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return;
1081
1082 if (!context->in_syscall) {
1083#if AUDIT_DEBUG == 2
1084 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1085 __FILE__, __LINE__, context->serial, name);
1086 dump_stack();
1087#endif
1088 return;
1089 }
1090 BUG_ON(context->name_count >= AUDIT_NAMES);
1091 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001092 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1093 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 context->names[context->name_count].ino = (unsigned long)-1;
1095 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001096 if (!context->pwd) {
1097 read_lock(&current->fs->lock);
1098 context->pwd = dget(current->fs->pwd);
1099 context->pwdmnt = mntget(current->fs->pwdmnt);
1100 read_unlock(&current->fs->lock);
1101 }
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001105/* audit_putname - intercept a putname request
1106 * @name: name to intercept and delay for putname
1107 *
1108 * If we have stored the name from getname in the audit context,
1109 * then we delay the putname until syscall exit.
1110 * Called from include/linux/fs.h:putname().
1111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112void audit_putname(const char *name)
1113{
1114 struct audit_context *context = current->audit_context;
1115
1116 BUG_ON(!context);
1117 if (!context->in_syscall) {
1118#if AUDIT_DEBUG == 2
1119 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1120 __FILE__, __LINE__, context->serial, name);
1121 if (context->name_count) {
1122 int i;
1123 for (i = 0; i < context->name_count; i++)
1124 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1125 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001126 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128#endif
1129 __putname(name);
1130 }
1131#if AUDIT_DEBUG
1132 else {
1133 ++context->put_count;
1134 if (context->put_count > context->name_count) {
1135 printk(KERN_ERR "%s:%d(:%d): major=%d"
1136 " in_syscall=%d putname(%p) name_count=%d"
1137 " put_count=%d\n",
1138 __FILE__, __LINE__,
1139 context->serial, context->major,
1140 context->in_syscall, name, context->name_count,
1141 context->put_count);
1142 dump_stack();
1143 }
1144 }
1145#endif
1146}
1147
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001148static void audit_inode_context(int idx, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001149{
1150 struct audit_context *context = current->audit_context;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001151
Steve Grubb1b50eed2006-04-03 14:06:13 -04001152 selinux_get_inode_sid(inode, &context->names[idx].osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001153}
1154
1155
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001156/**
1157 * audit_inode - store the inode and device from a lookup
1158 * @name: name being audited
1159 * @inode: inode being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001160 *
1161 * Called from fs/namei.c:path_lookup().
1162 */
Amy Griffis9c937dc2006-06-08 23:19:31 -04001163void __audit_inode(const char *name, const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 int idx;
1166 struct audit_context *context = current->audit_context;
1167
1168 if (!context->in_syscall)
1169 return;
1170 if (context->name_count
1171 && context->names[context->name_count-1].name
1172 && context->names[context->name_count-1].name == name)
1173 idx = context->name_count - 1;
1174 else if (context->name_count > 1
1175 && context->names[context->name_count-2].name
1176 && context->names[context->name_count-2].name == name)
1177 idx = context->name_count - 2;
1178 else {
1179 /* FIXME: how much do we care about inodes that have no
1180 * associated name? */
1181 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1182 return;
1183 idx = context->name_count++;
1184 context->names[idx].name = NULL;
1185#if AUDIT_DEBUG
1186 ++context->ino_count;
1187#endif
1188 }
Amy Griffis9c937dc2006-06-08 23:19:31 -04001189 context->names[idx].ino = inode->i_ino;
David Woodhouseae7b9612005-06-20 16:11:05 +01001190 context->names[idx].dev = inode->i_sb->s_dev;
1191 context->names[idx].mode = inode->i_mode;
1192 context->names[idx].uid = inode->i_uid;
1193 context->names[idx].gid = inode->i_gid;
1194 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001195 audit_inode_context(idx, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Amy Griffis73241cc2005-11-03 16:00:25 +00001198/**
1199 * audit_inode_child - collect inode info for created/removed objects
1200 * @dname: inode's dentry name
1201 * @inode: inode being audited
1202 * @pino: inode number of dentry parent
1203 *
1204 * For syscalls that create or remove filesystem objects, audit_inode
1205 * can only collect information for the filesystem object's parent.
1206 * This call updates the audit context with the child's information.
1207 * Syscalls that create a new filesystem object must be hooked after
1208 * the object is created. Syscalls that remove a filesystem object
1209 * must be hooked prior, in order to capture the target inode during
1210 * unsuccessful attempts.
1211 */
1212void __audit_inode_child(const char *dname, const struct inode *inode,
1213 unsigned long pino)
1214{
1215 int idx;
1216 struct audit_context *context = current->audit_context;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001217 const char *found_name = NULL;
1218 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00001219
1220 if (!context->in_syscall)
1221 return;
1222
1223 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001224 if (!dname)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001225 goto update_context;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001226 for (idx = 0; idx < context->name_count; idx++)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001227 if (context->names[idx].ino == pino) {
Amy Griffisf368c07d2006-04-07 16:55:56 -04001228 const char *name = context->names[idx].name;
Amy Griffis73241cc2005-11-03 16:00:25 +00001229
Amy Griffisf368c07d2006-04-07 16:55:56 -04001230 if (!name)
1231 continue;
Amy Griffis73241cc2005-11-03 16:00:25 +00001232
Amy Griffis9c937dc2006-06-08 23:19:31 -04001233 if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
1234 context->names[idx].name_len = dirlen;
1235 found_name = name;
1236 break;
1237 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001238 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001239
Amy Griffis9c937dc2006-06-08 23:19:31 -04001240update_context:
Amy Griffis73241cc2005-11-03 16:00:25 +00001241 idx = context->name_count++;
Amy Griffis73241cc2005-11-03 16:00:25 +00001242#if AUDIT_DEBUG
1243 context->ino_count++;
1244#endif
Amy Griffis9c937dc2006-06-08 23:19:31 -04001245 /* Re-use the name belonging to the slot for a matching parent directory.
1246 * All names for this context are relinquished in audit_free_names() */
1247 context->names[idx].name = found_name;
1248 context->names[idx].name_len = AUDIT_NAME_FULL;
1249 context->names[idx].name_put = 0; /* don't call __putname() */
Amy Griffis73241cc2005-11-03 16:00:25 +00001250
Amy Griffis73241cc2005-11-03 16:00:25 +00001251 if (inode) {
1252 context->names[idx].ino = inode->i_ino;
1253 context->names[idx].dev = inode->i_sb->s_dev;
1254 context->names[idx].mode = inode->i_mode;
1255 context->names[idx].uid = inode->i_uid;
1256 context->names[idx].gid = inode->i_gid;
1257 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001258 audit_inode_context(idx, inode);
Amy Griffis9c937dc2006-06-08 23:19:31 -04001259 } else
1260 context->names[idx].ino = (unsigned long)-1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001263/**
1264 * auditsc_get_stamp - get local copies of audit_context values
1265 * @ctx: audit_context for the task
1266 * @t: timespec to store time recorded in the audit_context
1267 * @serial: serial value that is recorded in the audit_context
1268 *
1269 * Also sets the context as auditable.
1270 */
David Woodhousebfb44962005-05-21 21:08:09 +01001271void auditsc_get_stamp(struct audit_context *ctx,
1272 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
David Woodhousece625a82005-07-18 14:24:46 -04001274 if (!ctx->serial)
1275 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001276 t->tv_sec = ctx->ctime.tv_sec;
1277 t->tv_nsec = ctx->ctime.tv_nsec;
1278 *serial = ctx->serial;
1279 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001282/**
1283 * audit_set_loginuid - set a task's audit_context loginuid
1284 * @task: task whose audit context is being modified
1285 * @loginuid: loginuid value
1286 *
1287 * Returns 0.
1288 *
1289 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1290 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001291int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Steve Grubb41757102006-06-12 07:48:28 -04001293 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01001294
Steve Grubb41757102006-06-12 07:48:28 -04001295 if (context) {
1296 /* Only log if audit is enabled */
1297 if (context->in_syscall) {
1298 struct audit_buffer *ab;
1299
1300 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1301 if (ab) {
1302 audit_log_format(ab, "login pid=%d uid=%u "
1303 "old auid=%u new auid=%u",
1304 task->pid, task->uid,
1305 context->loginuid, loginuid);
1306 audit_log_end(ab);
1307 }
Steve Grubbc0404992005-05-13 18:17:42 +01001308 }
Steve Grubb41757102006-06-12 07:48:28 -04001309 context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311 return 0;
1312}
1313
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001314/**
1315 * audit_get_loginuid - get the loginuid for an audit_context
1316 * @ctx: the audit_context
1317 *
1318 * Returns the context's loginuid or -1 if @ctx is NULL.
1319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320uid_t audit_get_loginuid(struct audit_context *ctx)
1321{
1322 return ctx ? ctx->loginuid : -1;
1323}
1324
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001325/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05001326 * __audit_mq_open - record audit data for a POSIX MQ open
1327 * @oflag: open flag
1328 * @mode: mode bits
1329 * @u_attr: queue attributes
1330 *
1331 * Returns 0 for success or NULL context or < 0 on error.
1332 */
1333int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1334{
1335 struct audit_aux_data_mq_open *ax;
1336 struct audit_context *context = current->audit_context;
1337
1338 if (!audit_enabled)
1339 return 0;
1340
1341 if (likely(!context))
1342 return 0;
1343
1344 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1345 if (!ax)
1346 return -ENOMEM;
1347
1348 if (u_attr != NULL) {
1349 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1350 kfree(ax);
1351 return -EFAULT;
1352 }
1353 } else
1354 memset(&ax->attr, 0, sizeof(ax->attr));
1355
1356 ax->oflag = oflag;
1357 ax->mode = mode;
1358
1359 ax->d.type = AUDIT_MQ_OPEN;
1360 ax->d.next = context->aux;
1361 context->aux = (void *)ax;
1362 return 0;
1363}
1364
1365/**
1366 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1367 * @mqdes: MQ descriptor
1368 * @msg_len: Message length
1369 * @msg_prio: Message priority
1370 * @abs_timeout: Message timeout in absolute time
1371 *
1372 * Returns 0 for success or NULL context or < 0 on error.
1373 */
1374int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1375 const struct timespec __user *u_abs_timeout)
1376{
1377 struct audit_aux_data_mq_sendrecv *ax;
1378 struct audit_context *context = current->audit_context;
1379
1380 if (!audit_enabled)
1381 return 0;
1382
1383 if (likely(!context))
1384 return 0;
1385
1386 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1387 if (!ax)
1388 return -ENOMEM;
1389
1390 if (u_abs_timeout != NULL) {
1391 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1392 kfree(ax);
1393 return -EFAULT;
1394 }
1395 } else
1396 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1397
1398 ax->mqdes = mqdes;
1399 ax->msg_len = msg_len;
1400 ax->msg_prio = msg_prio;
1401
1402 ax->d.type = AUDIT_MQ_SENDRECV;
1403 ax->d.next = context->aux;
1404 context->aux = (void *)ax;
1405 return 0;
1406}
1407
1408/**
1409 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1410 * @mqdes: MQ descriptor
1411 * @msg_len: Message length
1412 * @msg_prio: Message priority
1413 * @abs_timeout: Message timeout in absolute time
1414 *
1415 * Returns 0 for success or NULL context or < 0 on error.
1416 */
1417int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1418 unsigned int __user *u_msg_prio,
1419 const struct timespec __user *u_abs_timeout)
1420{
1421 struct audit_aux_data_mq_sendrecv *ax;
1422 struct audit_context *context = current->audit_context;
1423
1424 if (!audit_enabled)
1425 return 0;
1426
1427 if (likely(!context))
1428 return 0;
1429
1430 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1431 if (!ax)
1432 return -ENOMEM;
1433
1434 if (u_msg_prio != NULL) {
1435 if (get_user(ax->msg_prio, u_msg_prio)) {
1436 kfree(ax);
1437 return -EFAULT;
1438 }
1439 } else
1440 ax->msg_prio = 0;
1441
1442 if (u_abs_timeout != NULL) {
1443 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1444 kfree(ax);
1445 return -EFAULT;
1446 }
1447 } else
1448 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1449
1450 ax->mqdes = mqdes;
1451 ax->msg_len = msg_len;
1452
1453 ax->d.type = AUDIT_MQ_SENDRECV;
1454 ax->d.next = context->aux;
1455 context->aux = (void *)ax;
1456 return 0;
1457}
1458
1459/**
1460 * __audit_mq_notify - record audit data for a POSIX MQ notify
1461 * @mqdes: MQ descriptor
1462 * @u_notification: Notification event
1463 *
1464 * Returns 0 for success or NULL context or < 0 on error.
1465 */
1466
1467int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1468{
1469 struct audit_aux_data_mq_notify *ax;
1470 struct audit_context *context = current->audit_context;
1471
1472 if (!audit_enabled)
1473 return 0;
1474
1475 if (likely(!context))
1476 return 0;
1477
1478 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1479 if (!ax)
1480 return -ENOMEM;
1481
1482 if (u_notification != NULL) {
1483 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1484 kfree(ax);
1485 return -EFAULT;
1486 }
1487 } else
1488 memset(&ax->notification, 0, sizeof(ax->notification));
1489
1490 ax->mqdes = mqdes;
1491
1492 ax->d.type = AUDIT_MQ_NOTIFY;
1493 ax->d.next = context->aux;
1494 context->aux = (void *)ax;
1495 return 0;
1496}
1497
1498/**
1499 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1500 * @mqdes: MQ descriptor
1501 * @mqstat: MQ flags
1502 *
1503 * Returns 0 for success or NULL context or < 0 on error.
1504 */
1505int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1506{
1507 struct audit_aux_data_mq_getsetattr *ax;
1508 struct audit_context *context = current->audit_context;
1509
1510 if (!audit_enabled)
1511 return 0;
1512
1513 if (likely(!context))
1514 return 0;
1515
1516 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1517 if (!ax)
1518 return -ENOMEM;
1519
1520 ax->mqdes = mqdes;
1521 ax->mqstat = *mqstat;
1522
1523 ax->d.type = AUDIT_MQ_GETSETATTR;
1524 ax->d.next = context->aux;
1525 context->aux = (void *)ax;
1526 return 0;
1527}
1528
1529/**
Steve Grubb073115d2006-04-02 17:07:33 -04001530 * audit_ipc_obj - record audit data for ipc object
1531 * @ipcp: ipc permissions
1532 *
1533 * Returns 0 for success or NULL context or < 0 on error.
1534 */
Al Virod8945bb52006-05-18 16:01:30 -04001535int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04001536{
1537 struct audit_aux_data_ipcctl *ax;
1538 struct audit_context *context = current->audit_context;
1539
Steve Grubb073115d2006-04-02 17:07:33 -04001540 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1541 if (!ax)
1542 return -ENOMEM;
1543
1544 ax->uid = ipcp->uid;
1545 ax->gid = ipcp->gid;
1546 ax->mode = ipcp->mode;
1547 selinux_get_ipc_sid(ipcp, &ax->osid);
1548
1549 ax->d.type = AUDIT_IPC;
1550 ax->d.next = context->aux;
1551 context->aux = (void *)ax;
1552 return 0;
1553}
1554
1555/**
1556 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001557 * @qbytes: msgq bytes
1558 * @uid: msgq user id
1559 * @gid: msgq group id
1560 * @mode: msgq mode (permissions)
1561 *
1562 * Returns 0 for success or NULL context or < 0 on error.
1563 */
Al Virod8945bb52006-05-18 16:01:30 -04001564int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
1566 struct audit_aux_data_ipcctl *ax;
1567 struct audit_context *context = current->audit_context;
1568
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001569 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!ax)
1571 return -ENOMEM;
1572
1573 ax->qbytes = qbytes;
1574 ax->uid = uid;
1575 ax->gid = gid;
1576 ax->mode = mode;
1577
Steve Grubb073115d2006-04-02 17:07:33 -04001578 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ax->d.next = context->aux;
1580 context->aux = (void *)ax;
1581 return 0;
1582}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001583
Al Viro473ae302006-04-26 14:04:08 -04001584int audit_bprm(struct linux_binprm *bprm)
1585{
1586 struct audit_aux_data_execve *ax;
1587 struct audit_context *context = current->audit_context;
1588 unsigned long p, next;
1589 void *to;
1590
1591 if (likely(!audit_enabled || !context))
1592 return 0;
1593
1594 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1595 GFP_KERNEL);
1596 if (!ax)
1597 return -ENOMEM;
1598
1599 ax->argc = bprm->argc;
1600 ax->envc = bprm->envc;
1601 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1602 struct page *page = bprm->page[p / PAGE_SIZE];
1603 void *kaddr = kmap(page);
1604 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1605 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1606 to += next - p;
1607 kunmap(page);
1608 }
1609
1610 ax->d.type = AUDIT_EXECVE;
1611 ax->d.next = context->aux;
1612 context->aux = (void *)ax;
1613 return 0;
1614}
1615
1616
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001617/**
1618 * audit_socketcall - record audit data for sys_socketcall
1619 * @nargs: number of args
1620 * @args: args array
1621 *
1622 * Returns 0 for success or NULL context or < 0 on error.
1623 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001624int audit_socketcall(int nargs, unsigned long *args)
1625{
1626 struct audit_aux_data_socketcall *ax;
1627 struct audit_context *context = current->audit_context;
1628
1629 if (likely(!context))
1630 return 0;
1631
1632 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1633 if (!ax)
1634 return -ENOMEM;
1635
1636 ax->nargs = nargs;
1637 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1638
1639 ax->d.type = AUDIT_SOCKETCALL;
1640 ax->d.next = context->aux;
1641 context->aux = (void *)ax;
1642 return 0;
1643}
1644
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001645/**
1646 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1647 * @len: data length in user space
1648 * @a: data address in kernel space
1649 *
1650 * Returns 0 for success or NULL context or < 0 on error.
1651 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001652int audit_sockaddr(int len, void *a)
1653{
1654 struct audit_aux_data_sockaddr *ax;
1655 struct audit_context *context = current->audit_context;
1656
1657 if (likely(!context))
1658 return 0;
1659
1660 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1661 if (!ax)
1662 return -ENOMEM;
1663
1664 ax->len = len;
1665 memcpy(ax->a, a, len);
1666
1667 ax->d.type = AUDIT_SOCKADDR;
1668 ax->d.next = context->aux;
1669 context->aux = (void *)ax;
1670 return 0;
1671}
1672
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001673/**
1674 * audit_avc_path - record the granting or denial of permissions
1675 * @dentry: dentry to record
1676 * @mnt: mnt to record
1677 *
1678 * Returns 0 for success or NULL context or < 0 on error.
1679 *
1680 * Called from security/selinux/avc.c::avc_audit()
1681 */
Stephen Smalley01116102005-05-21 00:15:52 +01001682int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1683{
1684 struct audit_aux_data_path *ax;
1685 struct audit_context *context = current->audit_context;
1686
1687 if (likely(!context))
1688 return 0;
1689
1690 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1691 if (!ax)
1692 return -ENOMEM;
1693
1694 ax->dentry = dget(dentry);
1695 ax->mnt = mntget(mnt);
1696
1697 ax->d.type = AUDIT_AVC_PATH;
1698 ax->d.next = context->aux;
1699 context->aux = (void *)ax;
1700 return 0;
1701}
1702
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001703/**
1704 * audit_signal_info - record signal info for shutting down audit subsystem
1705 * @sig: signal value
1706 * @t: task being signaled
1707 *
1708 * If the audit subsystem is being terminated, record the task (pid)
1709 * and uid that is doing that.
1710 */
Al Viroe1396062006-05-25 10:19:47 -04001711void __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001712{
1713 extern pid_t audit_sig_pid;
1714 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04001715 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001716
Al Viroe1396062006-05-25 10:19:47 -04001717 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1718 struct task_struct *tsk = current;
1719 struct audit_context *ctx = tsk->audit_context;
1720 audit_sig_pid = tsk->pid;
1721 if (ctx)
1722 audit_sig_uid = ctx->loginuid;
1723 else
1724 audit_sig_uid = tsk->uid;
1725 selinux_get_task_sid(tsk, &audit_sig_sid);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001726 }
1727}