blob: fa4bf9625456e89cfbddeb57a0f242a06ff465f9 [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.
Dustin Kirklandb63862f2005-11-03 15:41:46 +00006 * Copyright (C) 2005 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 *
Dustin Kirklandb63862f2005-11-03 15:41:46 +000032 * The support of additional filter rules compares (>, <, >=, <=) was
33 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
34 *
Amy Griffis73241cc2005-11-03 16:00:25 +000035 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36 * filesystem information.
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000037 *
38 * Subject and object context labeling support added by <danjones@us.ibm.com>
39 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
42#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/types.h>
Alan Cox715b49e2006-01-18 17:44:07 -080044#include <asm/atomic.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000045#include <asm/types.h>
46#include <linux/fs.h>
47#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mm.h>
49#include <linux/module.h>
Stephen Smalley01116102005-05-21 00:15:52 +010050#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010051#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/audit.h>
53#include <linux/personality.h>
54#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010055#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010056#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000058#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000059#include <linux/list.h>
Steve Grubba6c043a2006-01-01 14:07:00 -050060#include <linux/tty.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060061#include <linux/selinux.h>
Al Viro473ae302006-04-26 14:04:08 -040062#include <linux/binfmts.h>
Al Virof46038f2006-05-06 08:22:52 -040063#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
David Woodhousefe7752b2005-12-15 18:33:52 +000065#include "audit.h"
66
67extern struct list_head audit_filter_list[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* No syscall auditing will take place unless audit_enabled != 0. */
70extern int audit_enabled;
71
72/* AUDIT_NAMES is the number of slots we reserve in the audit_context
73 * for saving names from getname(). */
74#define AUDIT_NAMES 20
75
76/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
77 * audit_context from being used for nameless inodes from
78 * path_lookup. */
79#define AUDIT_NAMES_RESERVED 7
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* When fs/namei.c:getname() is called, we store the pointer in name and
82 * we don't let putname() free it (instead we free all of the saved
83 * pointers at syscall exit time).
84 *
85 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
86struct audit_names {
87 const char *name;
88 unsigned long ino;
Amy Griffis73241cc2005-11-03 16:00:25 +000089 unsigned long pino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 dev_t dev;
91 umode_t mode;
92 uid_t uid;
93 gid_t gid;
94 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -040095 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98struct audit_aux_data {
99 struct audit_aux_data *next;
100 int type;
101};
102
103#define AUDIT_AUX_IPCPERM 0
104
105struct audit_aux_data_ipcctl {
106 struct audit_aux_data d;
107 struct ipc_perm p;
108 unsigned long qbytes;
109 uid_t uid;
110 gid_t gid;
111 mode_t mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500112 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Al Viro473ae302006-04-26 14:04:08 -0400115struct audit_aux_data_execve {
116 struct audit_aux_data d;
117 int argc;
118 int envc;
119 char mem[0];
120};
121
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100122struct audit_aux_data_socketcall {
123 struct audit_aux_data d;
124 int nargs;
125 unsigned long args[0];
126};
127
128struct audit_aux_data_sockaddr {
129 struct audit_aux_data d;
130 int len;
131 char a[0];
132};
133
Stephen Smalley01116102005-05-21 00:15:52 +0100134struct audit_aux_data_path {
135 struct audit_aux_data d;
136 struct dentry *dentry;
137 struct vfsmount *mnt;
138};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140/* The per-task audit context. */
141struct audit_context {
142 int in_syscall; /* 1 if task is in a syscall */
143 enum audit_state state;
144 unsigned int serial; /* serial number for record */
145 struct timespec ctime; /* time of syscall entry */
146 uid_t loginuid; /* login uid (identity) */
147 int major; /* syscall number */
148 unsigned long argv[4]; /* syscall arguments */
149 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100150 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 int auditable; /* 1 if record should be written */
152 int name_count;
153 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100154 struct dentry * pwd;
155 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 struct audit_context *previous; /* For nested syscalls */
157 struct audit_aux_data *aux;
158
159 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400160 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 uid_t uid, euid, suid, fsuid;
162 gid_t gid, egid, sgid, fsgid;
163 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100164 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166#if AUDIT_DEBUG
167 int put_count;
168 int ino_count;
169#endif
170};
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* Compare a task_struct with an audit_rule. Return 1 on match, 0
174 * otherwise. */
175static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500176 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct audit_context *ctx,
178 enum audit_state *state)
179{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400180 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600181 u32 sid;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500184 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int result = 0;
186
Amy Griffis93315ed2006-02-07 12:05:27 -0500187 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500189 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 break;
Al Viro3c662512006-05-06 08:26:27 -0400191 case AUDIT_PPID:
192 if (ctx)
193 result = audit_comparator(ctx->ppid, f->op, f->val);
194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500196 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500199 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500202 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
204 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500205 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
207 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500208 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 break;
210 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500211 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 break;
213 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500214 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500217 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500220 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
2fd6f582005-04-29 16:08:28 +0100222 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000223 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500224 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 case AUDIT_EXIT:
228 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500229 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 break;
231 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100232 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500233 if (f->val)
234 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100235 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500236 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
239 case AUDIT_DEVMAJOR:
240 if (ctx) {
241 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500242 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 ++result;
244 break;
245 }
246 }
247 }
248 break;
249 case AUDIT_DEVMINOR:
250 if (ctx) {
251 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500252 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ++result;
254 break;
255 }
256 }
257 }
258 break;
259 case AUDIT_INODE:
260 if (ctx) {
261 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500262 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
263 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ++result;
265 break;
266 }
267 }
268 }
269 break;
270 case AUDIT_LOGINUID:
271 result = 0;
272 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500273 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600275 case AUDIT_SE_USER:
276 case AUDIT_SE_ROLE:
277 case AUDIT_SE_TYPE:
278 case AUDIT_SE_SEN:
279 case AUDIT_SE_CLR:
280 /* NOTE: this may return negative values indicating
281 a temporary error. We simply treat this as a
282 match for now to avoid losing information that
283 may be wanted. An error message will also be
284 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400285 if (f->se_rule) {
286 if (need_sid) {
287 selinux_task_ctxid(tsk, &sid);
288 need_sid = 0;
289 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600290 result = selinux_audit_rule_match(sid, f->type,
291 f->op,
292 f->se_rule,
293 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400294 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 case AUDIT_ARG0:
297 case AUDIT_ARG1:
298 case AUDIT_ARG2:
299 case AUDIT_ARG3:
300 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500301 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303 }
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (!result)
306 return 0;
307 }
308 switch (rule->action) {
309 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
310 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
311 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
312 }
313 return 1;
314}
315
316/* At process creation time, we can determine if system-call auditing is
317 * completely disabled for this task. Since we only have the task
318 * structure at this point, we can only check uid and gid.
319 */
320static enum audit_state audit_filter_task(struct task_struct *tsk)
321{
322 struct audit_entry *e;
323 enum audit_state state;
324
325 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100326 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
328 rcu_read_unlock();
329 return state;
330 }
331 }
332 rcu_read_unlock();
333 return AUDIT_BUILD_CONTEXT;
334}
335
336/* At syscall entry and exit time, this filter is called if the
337 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100338 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700339 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
341static enum audit_state audit_filter_syscall(struct task_struct *tsk,
342 struct audit_context *ctx,
343 struct list_head *list)
344{
345 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100346 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
David Woodhouse351bb722005-07-14 14:40:06 +0100348 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100349 return AUDIT_DISABLED;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100352 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000353 int word = AUDIT_WORD(ctx->major);
354 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100355
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000356 list_for_each_entry_rcu(e, list, list) {
357 if ((e->rule.mask[word] & bit) == bit
358 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
359 rcu_read_unlock();
360 return state;
361 }
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 rcu_read_unlock();
365 return AUDIT_BUILD_CONTEXT;
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static inline struct audit_context *audit_get_context(struct task_struct *tsk,
369 int return_valid,
370 int return_code)
371{
372 struct audit_context *context = tsk->audit_context;
373
374 if (likely(!context))
375 return NULL;
376 context->return_valid = return_valid;
377 context->return_code = return_code;
378
David Woodhouse21af6c42005-07-02 14:10:46 +0100379 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100381 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (state == AUDIT_RECORD_CONTEXT)
383 context->auditable = 1;
384 }
385
386 context->pid = tsk->pid;
Al Virof46038f2006-05-06 08:22:52 -0400387 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 context->uid = tsk->uid;
389 context->gid = tsk->gid;
390 context->euid = tsk->euid;
391 context->suid = tsk->suid;
392 context->fsuid = tsk->fsuid;
393 context->egid = tsk->egid;
394 context->sgid = tsk->sgid;
395 context->fsgid = tsk->fsgid;
396 context->personality = tsk->personality;
397 tsk->audit_context = NULL;
398 return context;
399}
400
401static inline void audit_free_names(struct audit_context *context)
402{
403 int i;
404
405#if AUDIT_DEBUG == 2
406 if (context->auditable
407 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000408 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 " name_count=%d put_count=%d"
410 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000411 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 context->serial, context->major, context->in_syscall,
413 context->name_count, context->put_count,
414 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000415 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 printk(KERN_ERR "names[%d] = %p = %s\n", i,
417 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000418 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 dump_stack();
421 return;
422 }
423#endif
424#if AUDIT_DEBUG
425 context->put_count = 0;
426 context->ino_count = 0;
427#endif
428
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000429 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (context->names[i].name)
431 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100434 if (context->pwd)
435 dput(context->pwd);
436 if (context->pwdmnt)
437 mntput(context->pwdmnt);
438 context->pwd = NULL;
439 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442static inline void audit_free_aux(struct audit_context *context)
443{
444 struct audit_aux_data *aux;
445
446 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100447 if (aux->type == AUDIT_AVC_PATH) {
448 struct audit_aux_data_path *axi = (void *)aux;
449 dput(axi->dentry);
450 mntput(axi->mnt);
451 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 context->aux = aux->next;
454 kfree(aux);
455 }
456}
457
458static inline void audit_zero_context(struct audit_context *context,
459 enum audit_state state)
460{
461 uid_t loginuid = context->loginuid;
462
463 memset(context, 0, sizeof(*context));
464 context->state = state;
465 context->loginuid = loginuid;
466}
467
468static inline struct audit_context *audit_alloc_context(enum audit_state state)
469{
470 struct audit_context *context;
471
472 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
473 return NULL;
474 audit_zero_context(context, state);
475 return context;
476}
477
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700478/**
479 * audit_alloc - allocate an audit context block for a task
480 * @tsk: task
481 *
482 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * if necessary. Doing so turns on system call auditing for the
484 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700485 * needed.
486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487int audit_alloc(struct task_struct *tsk)
488{
489 struct audit_context *context;
490 enum audit_state state;
491
492 if (likely(!audit_enabled))
493 return 0; /* Return if not auditing. */
494
495 state = audit_filter_task(tsk);
496 if (likely(state == AUDIT_DISABLED))
497 return 0;
498
499 if (!(context = audit_alloc_context(state))) {
500 audit_log_lost("out of memory in audit_alloc");
501 return -ENOMEM;
502 }
503
504 /* Preserve login uid */
505 context->loginuid = -1;
506 if (current->audit_context)
507 context->loginuid = current->audit_context->loginuid;
508
509 tsk->audit_context = context;
510 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
511 return 0;
512}
513
514static inline void audit_free_context(struct audit_context *context)
515{
516 struct audit_context *previous;
517 int count = 0;
518
519 do {
520 previous = context->previous;
521 if (previous || (count && count < 10)) {
522 ++count;
523 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
524 " freeing multiple contexts (%d)\n",
525 context->serial, context->major,
526 context->name_count, count);
527 }
528 audit_free_names(context);
529 audit_free_aux(context);
530 kfree(context);
531 context = previous;
532 } while (context);
533 if (count >= 10)
534 printk(KERN_ERR "audit: freed %d contexts\n", count);
535}
536
Al Viroe4951492006-03-29 20:17:10 -0500537static void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000538{
539 char *ctx = NULL;
540 ssize_t len = 0;
541
542 len = security_getprocattr(current, "current", NULL, 0);
543 if (len < 0) {
544 if (len != -EINVAL)
545 goto error_path;
546 return;
547 }
548
Al Viroe4951492006-03-29 20:17:10 -0500549 ctx = kmalloc(len, GFP_KERNEL);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000550 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000551 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000552
553 len = security_getprocattr(current, "current", ctx, len);
554 if (len < 0 )
555 goto error_path;
556
557 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000558 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000559
560error_path:
561 if (ctx)
562 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000563 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000564 return;
565}
566
Al Viroe4951492006-03-29 20:17:10 -0500567static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700568{
Al Viro45d9bb02006-03-29 20:02:55 -0500569 char name[sizeof(tsk->comm)];
570 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700571 struct vm_area_struct *vma;
572
Al Viroe4951492006-03-29 20:17:10 -0500573 /* tsk == current */
574
Al Viro45d9bb02006-03-29 20:02:55 -0500575 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100576 audit_log_format(ab, " comm=");
577 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700578
Al Viroe4951492006-03-29 20:17:10 -0500579 if (mm) {
580 down_read(&mm->mmap_sem);
581 vma = mm->mmap;
582 while (vma) {
583 if ((vma->vm_flags & VM_EXECUTABLE) &&
584 vma->vm_file) {
585 audit_log_d_path(ab, "exe=",
586 vma->vm_file->f_dentry,
587 vma->vm_file->f_vfsmnt);
588 break;
589 }
590 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700591 }
Al Viroe4951492006-03-29 20:17:10 -0500592 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700593 }
Al Viroe4951492006-03-29 20:17:10 -0500594 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700595}
596
Al Viroe4951492006-03-29 20:17:10 -0500597static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500599 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100601 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500602 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Al Viroe4951492006-03-29 20:17:10 -0500604 /* tsk == current */
605
606 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!ab)
608 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100609 audit_log_format(ab, "arch=%x syscall=%d",
610 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (context->personality != PER_LINUX)
612 audit_log_format(ab, " per=%lx", context->personality);
613 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100614 audit_log_format(ab, " success=%s exit=%ld",
615 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
616 context->return_code);
Al Viro45d9bb02006-03-29 20:02:55 -0500617 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
618 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500619 else
620 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 audit_log_format(ab,
622 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -0400623 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +0100624 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500625 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 context->argv[0],
627 context->argv[1],
628 context->argv[2],
629 context->argv[3],
630 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -0400631 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 context->pid,
633 context->loginuid,
634 context->uid,
635 context->gid,
636 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500637 context->egid, context->sgid, context->fsgid, tty);
Al Viroe4951492006-03-29 20:17:10 -0500638 audit_log_task_info(ab, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
David Woodhouse7551ced2005-05-26 12:04:57 +0100641 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100642
Al Viroe4951492006-03-29 20:17:10 -0500643 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!ab)
645 continue; /* audit_panic has been called */
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100648 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct audit_aux_data_ipcctl *axi = (void *)aux;
650 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400651 "ouid=%u ogid=%u mode=%x",
652 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500653 if (axi->osid != 0) {
654 char *ctx = NULL;
655 u32 len;
656 if (selinux_ctxid_to_string(
657 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500658 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500659 axi->osid);
660 call_panic = 1;
661 } else
662 audit_log_format(ab, " obj=%s", ctx);
663 kfree(ctx);
664 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100665 break; }
666
Steve Grubb073115d2006-04-02 17:07:33 -0400667 case AUDIT_IPC_SET_PERM: {
668 struct audit_aux_data_ipcctl *axi = (void *)aux;
669 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400670 "qbytes=%lx ouid=%u ogid=%u mode=%x",
Steve Grubb073115d2006-04-02 17:07:33 -0400671 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400672 break; }
Linda Knippersac032212006-05-16 22:03:48 -0400673
Al Viro473ae302006-04-26 14:04:08 -0400674 case AUDIT_EXECVE: {
675 struct audit_aux_data_execve *axi = (void *)aux;
676 int i;
677 const char *p;
678 for (i = 0, p = axi->mem; i < axi->argc; i++) {
679 audit_log_format(ab, "a%d=", i);
680 p = audit_log_untrustedstring(ab, p);
681 audit_log_format(ab, "\n");
682 }
683 break; }
Steve Grubb073115d2006-04-02 17:07:33 -0400684
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100685 case AUDIT_SOCKETCALL: {
686 int i;
687 struct audit_aux_data_socketcall *axs = (void *)aux;
688 audit_log_format(ab, "nargs=%d", axs->nargs);
689 for (i=0; i<axs->nargs; i++)
690 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
691 break; }
692
693 case AUDIT_SOCKADDR: {
694 struct audit_aux_data_sockaddr *axs = (void *)aux;
695
696 audit_log_format(ab, "saddr=");
697 audit_log_hex(ab, axs->a, axs->len);
698 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100699
700 case AUDIT_AVC_PATH: {
701 struct audit_aux_data_path *axi = (void *)aux;
702 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100703 break; }
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708
David Woodhouse8f37d472005-05-27 12:17:28 +0100709 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -0500710 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100711 if (ab) {
712 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
713 audit_log_end(ab);
714 }
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000717 unsigned long ino = context->names[i].ino;
718 unsigned long pino = context->names[i].pino;
719
Al Viroe4951492006-03-29 20:17:10 -0500720 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!ab)
722 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000725
726 audit_log_format(ab, " name=");
727 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100728 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000729 else
730 audit_log_format(ab, "(null)");
731
732 if (pino != (unsigned long)-1)
733 audit_log_format(ab, " parent=%lu", pino);
734 if (ino != (unsigned long)-1)
735 audit_log_format(ab, " inode=%lu", ino);
736 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
737 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
738 " ouid=%u ogid=%u rdev=%02x:%02x",
739 MAJOR(context->names[i].dev),
740 MINOR(context->names[i].dev),
741 context->names[i].mode,
742 context->names[i].uid,
743 context->names[i].gid,
744 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 MINOR(context->names[i].rdev));
Steve Grubb1b50eed2006-04-03 14:06:13 -0400746 if (context->names[i].osid != 0) {
747 char *ctx = NULL;
748 u32 len;
749 if (selinux_ctxid_to_string(
750 context->names[i].osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500751 audit_log_format(ab, " osid=%u",
Steve Grubb1b50eed2006-04-03 14:06:13 -0400752 context->names[i].osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500753 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400754 } else
755 audit_log_format(ab, " obj=%s", ctx);
756 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000757 }
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 audit_log_end(ab);
760 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500761 if (call_panic)
762 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700765/**
766 * audit_free - free a per-task audit context
767 * @tsk: task whose audit context block to free
768 *
Al Virofa84cb92006-03-29 20:30:19 -0500769 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700770 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771void audit_free(struct task_struct *tsk)
772{
773 struct audit_context *context;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (likely(!context))
777 return;
778
779 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100780 * function (e.g., exit_group), then free context block.
781 * We use GFP_ATOMIC here because we might be doing this
782 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -0500783 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +0100784 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500785 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 audit_free_context(context);
788}
789
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700790/**
791 * audit_syscall_entry - fill in an audit record at syscall entry
792 * @tsk: task being audited
793 * @arch: architecture type
794 * @major: major syscall type (function)
795 * @a1: additional syscall register 1
796 * @a2: additional syscall register 2
797 * @a3: additional syscall register 3
798 * @a4: additional syscall register 4
799 *
800 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 * audit context was created when the task was created and the state or
802 * filters demand the audit context be built. If the state from the
803 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
804 * then the record will be written at syscall exit time (otherwise, it
805 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700806 * be written).
807 */
Al Viro5411be52006-03-29 20:23:36 -0500808void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 unsigned long a1, unsigned long a2,
810 unsigned long a3, unsigned long a4)
811{
Al Viro5411be52006-03-29 20:23:36 -0500812 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 struct audit_context *context = tsk->audit_context;
814 enum audit_state state;
815
816 BUG_ON(!context);
817
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700818 /*
819 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 * calls in kernel_thread via the entry.S interface, instead of
821 * with direct calls. (If you are porting to a new
822 * architecture, hitting this condition can indicate that you
823 * got the _exit/_leave calls backward in entry.S.)
824 *
825 * i386 no
826 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -0600827 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 *
829 * This also happens with vm86 emulation in a non-nested manner
830 * (entries without exits), so this case must be caught.
831 */
832 if (context->in_syscall) {
833 struct audit_context *newctx;
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835#if AUDIT_DEBUG
836 printk(KERN_ERR
837 "audit(:%d) pid=%d in syscall=%d;"
838 " entering syscall=%d\n",
839 context->serial, tsk->pid, context->major, major);
840#endif
841 newctx = audit_alloc_context(context->state);
842 if (newctx) {
843 newctx->previous = context;
844 context = newctx;
845 tsk->audit_context = newctx;
846 } else {
847 /* If we can't alloc a new context, the best we
848 * can do is to leak memory (any pending putname
849 * will be lost). The only other alternative is
850 * to abandon auditing. */
851 audit_zero_context(context, context->state);
852 }
853 }
854 BUG_ON(context->in_syscall || context->name_count);
855
856 if (!audit_enabled)
857 return;
858
2fd6f582005-04-29 16:08:28 +0100859 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 context->major = major;
861 context->argv[0] = a1;
862 context->argv[1] = a2;
863 context->argv[2] = a3;
864 context->argv[3] = a4;
865
866 state = context->state;
867 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100868 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (likely(state == AUDIT_DISABLED))
870 return;
871
David Woodhousece625a82005-07-18 14:24:46 -0400872 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 context->ctime = CURRENT_TIME;
874 context->in_syscall = 1;
875 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
876}
877
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700878/**
879 * audit_syscall_exit - deallocate audit context after a system call
880 * @tsk: task being audited
881 * @valid: success/failure flag
882 * @return_code: syscall return value
883 *
884 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
886 * filtering, or because some other part of the kernel write an audit
887 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700888 * free the names stored from getname().
889 */
Al Viro5411be52006-03-29 20:23:36 -0500890void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Al Viro5411be52006-03-29 20:23:36 -0500892 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct audit_context *context;
894
2fd6f582005-04-29 16:08:28 +0100895 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -0500898 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
David Woodhousef7056d62005-06-20 16:07:33 +0100900 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500901 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 context->in_syscall = 0;
904 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (context->previous) {
907 struct audit_context *new_context = context->previous;
908 context->previous = NULL;
909 audit_free_context(context);
910 tsk->audit_context = new_context;
911 } else {
912 audit_free_names(context);
913 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 tsk->audit_context = context;
915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700918/**
919 * audit_getname - add a name to the list
920 * @name: name to add
921 *
922 * Add a name to the list of audit names for this context.
923 * Called from fs/namei.c:getname().
924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925void audit_getname(const char *name)
926{
927 struct audit_context *context = current->audit_context;
928
929 if (!context || IS_ERR(name) || !name)
930 return;
931
932 if (!context->in_syscall) {
933#if AUDIT_DEBUG == 2
934 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
935 __FILE__, __LINE__, context->serial, name);
936 dump_stack();
937#endif
938 return;
939 }
940 BUG_ON(context->name_count >= AUDIT_NAMES);
941 context->names[context->name_count].name = name;
942 context->names[context->name_count].ino = (unsigned long)-1;
943 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100944 if (!context->pwd) {
945 read_lock(&current->fs->lock);
946 context->pwd = dget(current->fs->pwd);
947 context->pwdmnt = mntget(current->fs->pwdmnt);
948 read_unlock(&current->fs->lock);
949 }
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700953/* audit_putname - intercept a putname request
954 * @name: name to intercept and delay for putname
955 *
956 * If we have stored the name from getname in the audit context,
957 * then we delay the putname until syscall exit.
958 * Called from include/linux/fs.h:putname().
959 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960void audit_putname(const char *name)
961{
962 struct audit_context *context = current->audit_context;
963
964 BUG_ON(!context);
965 if (!context->in_syscall) {
966#if AUDIT_DEBUG == 2
967 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
968 __FILE__, __LINE__, context->serial, name);
969 if (context->name_count) {
970 int i;
971 for (i = 0; i < context->name_count; i++)
972 printk(KERN_ERR "name[%d] = %p = %s\n", i,
973 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000974 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976#endif
977 __putname(name);
978 }
979#if AUDIT_DEBUG
980 else {
981 ++context->put_count;
982 if (context->put_count > context->name_count) {
983 printk(KERN_ERR "%s:%d(:%d): major=%d"
984 " in_syscall=%d putname(%p) name_count=%d"
985 " put_count=%d\n",
986 __FILE__, __LINE__,
987 context->serial, context->major,
988 context->in_syscall, name, context->name_count,
989 context->put_count);
990 dump_stack();
991 }
992 }
993#endif
994}
995
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500996static void audit_inode_context(int idx, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000997{
998 struct audit_context *context = current->audit_context;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000999
Steve Grubb1b50eed2006-04-03 14:06:13 -04001000 selinux_get_inode_sid(inode, &context->names[idx].osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001001}
1002
1003
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001004/**
1005 * audit_inode - store the inode and device from a lookup
1006 * @name: name being audited
1007 * @inode: inode being audited
1008 * @flags: lookup flags (as used in path_lookup())
1009 *
1010 * Called from fs/namei.c:path_lookup().
1011 */
Amy Griffis73241cc2005-11-03 16:00:25 +00001012void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 int idx;
1015 struct audit_context *context = current->audit_context;
1016
1017 if (!context->in_syscall)
1018 return;
1019 if (context->name_count
1020 && context->names[context->name_count-1].name
1021 && context->names[context->name_count-1].name == name)
1022 idx = context->name_count - 1;
1023 else if (context->name_count > 1
1024 && context->names[context->name_count-2].name
1025 && context->names[context->name_count-2].name == name)
1026 idx = context->name_count - 2;
1027 else {
1028 /* FIXME: how much do we care about inodes that have no
1029 * associated name? */
1030 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1031 return;
1032 idx = context->name_count++;
1033 context->names[idx].name = NULL;
1034#if AUDIT_DEBUG
1035 ++context->ino_count;
1036#endif
1037 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001038 context->names[idx].dev = inode->i_sb->s_dev;
1039 context->names[idx].mode = inode->i_mode;
1040 context->names[idx].uid = inode->i_uid;
1041 context->names[idx].gid = inode->i_gid;
1042 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001043 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001044 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1045 (strcmp(name, ".") != 0)) {
1046 context->names[idx].ino = (unsigned long)-1;
1047 context->names[idx].pino = inode->i_ino;
1048 } else {
1049 context->names[idx].ino = inode->i_ino;
1050 context->names[idx].pino = (unsigned long)-1;
1051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Amy Griffis73241cc2005-11-03 16:00:25 +00001054/**
1055 * audit_inode_child - collect inode info for created/removed objects
1056 * @dname: inode's dentry name
1057 * @inode: inode being audited
1058 * @pino: inode number of dentry parent
1059 *
1060 * For syscalls that create or remove filesystem objects, audit_inode
1061 * can only collect information for the filesystem object's parent.
1062 * This call updates the audit context with the child's information.
1063 * Syscalls that create a new filesystem object must be hooked after
1064 * the object is created. Syscalls that remove a filesystem object
1065 * must be hooked prior, in order to capture the target inode during
1066 * unsuccessful attempts.
1067 */
1068void __audit_inode_child(const char *dname, const struct inode *inode,
1069 unsigned long pino)
1070{
1071 int idx;
1072 struct audit_context *context = current->audit_context;
1073
1074 if (!context->in_syscall)
1075 return;
1076
1077 /* determine matching parent */
1078 if (dname)
1079 for (idx = 0; idx < context->name_count; idx++)
1080 if (context->names[idx].pino == pino) {
1081 const char *n;
1082 const char *name = context->names[idx].name;
1083 int dlen = strlen(dname);
1084 int nlen = name ? strlen(name) : 0;
1085
1086 if (nlen < dlen)
1087 continue;
1088
1089 /* disregard trailing slashes */
1090 n = name + nlen - 1;
1091 while ((*n == '/') && (n > name))
1092 n--;
1093
1094 /* find last path component */
1095 n = n - dlen + 1;
1096 if (n < name)
1097 continue;
1098 else if (n > name) {
1099 if (*--n != '/')
1100 continue;
1101 else
1102 n++;
1103 }
1104
1105 if (strncmp(n, dname, dlen) == 0)
1106 goto update_context;
1107 }
1108
1109 /* catch-all in case match not found */
1110 idx = context->name_count++;
1111 context->names[idx].name = NULL;
1112 context->names[idx].pino = pino;
1113#if AUDIT_DEBUG
1114 context->ino_count++;
1115#endif
1116
1117update_context:
1118 if (inode) {
1119 context->names[idx].ino = inode->i_ino;
1120 context->names[idx].dev = inode->i_sb->s_dev;
1121 context->names[idx].mode = inode->i_mode;
1122 context->names[idx].uid = inode->i_uid;
1123 context->names[idx].gid = inode->i_gid;
1124 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001125 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001129/**
1130 * auditsc_get_stamp - get local copies of audit_context values
1131 * @ctx: audit_context for the task
1132 * @t: timespec to store time recorded in the audit_context
1133 * @serial: serial value that is recorded in the audit_context
1134 *
1135 * Also sets the context as auditable.
1136 */
David Woodhousebfb44962005-05-21 21:08:09 +01001137void auditsc_get_stamp(struct audit_context *ctx,
1138 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
David Woodhousece625a82005-07-18 14:24:46 -04001140 if (!ctx->serial)
1141 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001142 t->tv_sec = ctx->ctime.tv_sec;
1143 t->tv_nsec = ctx->ctime.tv_nsec;
1144 *serial = ctx->serial;
1145 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001148/**
1149 * audit_set_loginuid - set a task's audit_context loginuid
1150 * @task: task whose audit context is being modified
1151 * @loginuid: loginuid value
1152 *
1153 * Returns 0.
1154 *
1155 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1156 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001157int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Steve Grubb456be6c2005-04-29 17:30:07 +01001159 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001160 struct audit_buffer *ab;
1161
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001162 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001163 if (ab) {
1164 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001165 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001166 task->pid, task->uid,
1167 task->audit_context->loginuid, loginuid);
1168 audit_log_end(ab);
1169 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001170 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 return 0;
1173}
1174
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001175/**
1176 * audit_get_loginuid - get the loginuid for an audit_context
1177 * @ctx: the audit_context
1178 *
1179 * Returns the context's loginuid or -1 if @ctx is NULL.
1180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181uid_t audit_get_loginuid(struct audit_context *ctx)
1182{
1183 return ctx ? ctx->loginuid : -1;
1184}
1185
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001186/**
Steve Grubb073115d2006-04-02 17:07:33 -04001187 * audit_ipc_obj - record audit data for ipc object
1188 * @ipcp: ipc permissions
1189 *
1190 * Returns 0 for success or NULL context or < 0 on error.
1191 */
1192int audit_ipc_obj(struct kern_ipc_perm *ipcp)
1193{
1194 struct audit_aux_data_ipcctl *ax;
1195 struct audit_context *context = current->audit_context;
1196
1197 if (likely(!context))
1198 return 0;
1199
1200 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1201 if (!ax)
1202 return -ENOMEM;
1203
1204 ax->uid = ipcp->uid;
1205 ax->gid = ipcp->gid;
1206 ax->mode = ipcp->mode;
1207 selinux_get_ipc_sid(ipcp, &ax->osid);
1208
1209 ax->d.type = AUDIT_IPC;
1210 ax->d.next = context->aux;
1211 context->aux = (void *)ax;
1212 return 0;
1213}
1214
1215/**
1216 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001217 * @qbytes: msgq bytes
1218 * @uid: msgq user id
1219 * @gid: msgq group id
1220 * @mode: msgq mode (permissions)
1221 *
1222 * Returns 0 for success or NULL context or < 0 on error.
1223 */
Linda Knippersac032212006-05-16 22:03:48 -04001224int audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 struct audit_aux_data_ipcctl *ax;
1227 struct audit_context *context = current->audit_context;
1228
1229 if (likely(!context))
1230 return 0;
1231
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001232 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (!ax)
1234 return -ENOMEM;
1235
1236 ax->qbytes = qbytes;
1237 ax->uid = uid;
1238 ax->gid = gid;
1239 ax->mode = mode;
1240
Steve Grubb073115d2006-04-02 17:07:33 -04001241 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 ax->d.next = context->aux;
1243 context->aux = (void *)ax;
1244 return 0;
1245}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001246
Al Viro473ae302006-04-26 14:04:08 -04001247int audit_bprm(struct linux_binprm *bprm)
1248{
1249 struct audit_aux_data_execve *ax;
1250 struct audit_context *context = current->audit_context;
1251 unsigned long p, next;
1252 void *to;
1253
1254 if (likely(!audit_enabled || !context))
1255 return 0;
1256
1257 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1258 GFP_KERNEL);
1259 if (!ax)
1260 return -ENOMEM;
1261
1262 ax->argc = bprm->argc;
1263 ax->envc = bprm->envc;
1264 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1265 struct page *page = bprm->page[p / PAGE_SIZE];
1266 void *kaddr = kmap(page);
1267 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1268 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1269 to += next - p;
1270 kunmap(page);
1271 }
1272
1273 ax->d.type = AUDIT_EXECVE;
1274 ax->d.next = context->aux;
1275 context->aux = (void *)ax;
1276 return 0;
1277}
1278
1279
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001280/**
1281 * audit_socketcall - record audit data for sys_socketcall
1282 * @nargs: number of args
1283 * @args: args array
1284 *
1285 * Returns 0 for success or NULL context or < 0 on error.
1286 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001287int audit_socketcall(int nargs, unsigned long *args)
1288{
1289 struct audit_aux_data_socketcall *ax;
1290 struct audit_context *context = current->audit_context;
1291
1292 if (likely(!context))
1293 return 0;
1294
1295 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1296 if (!ax)
1297 return -ENOMEM;
1298
1299 ax->nargs = nargs;
1300 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1301
1302 ax->d.type = AUDIT_SOCKETCALL;
1303 ax->d.next = context->aux;
1304 context->aux = (void *)ax;
1305 return 0;
1306}
1307
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001308/**
1309 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1310 * @len: data length in user space
1311 * @a: data address in kernel space
1312 *
1313 * Returns 0 for success or NULL context or < 0 on error.
1314 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001315int audit_sockaddr(int len, void *a)
1316{
1317 struct audit_aux_data_sockaddr *ax;
1318 struct audit_context *context = current->audit_context;
1319
1320 if (likely(!context))
1321 return 0;
1322
1323 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1324 if (!ax)
1325 return -ENOMEM;
1326
1327 ax->len = len;
1328 memcpy(ax->a, a, len);
1329
1330 ax->d.type = AUDIT_SOCKADDR;
1331 ax->d.next = context->aux;
1332 context->aux = (void *)ax;
1333 return 0;
1334}
1335
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001336/**
1337 * audit_avc_path - record the granting or denial of permissions
1338 * @dentry: dentry to record
1339 * @mnt: mnt to record
1340 *
1341 * Returns 0 for success or NULL context or < 0 on error.
1342 *
1343 * Called from security/selinux/avc.c::avc_audit()
1344 */
Stephen Smalley01116102005-05-21 00:15:52 +01001345int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1346{
1347 struct audit_aux_data_path *ax;
1348 struct audit_context *context = current->audit_context;
1349
1350 if (likely(!context))
1351 return 0;
1352
1353 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1354 if (!ax)
1355 return -ENOMEM;
1356
1357 ax->dentry = dget(dentry);
1358 ax->mnt = mntget(mnt);
1359
1360 ax->d.type = AUDIT_AVC_PATH;
1361 ax->d.next = context->aux;
1362 context->aux = (void *)ax;
1363 return 0;
1364}
1365
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001366/**
1367 * audit_signal_info - record signal info for shutting down audit subsystem
1368 * @sig: signal value
1369 * @t: task being signaled
1370 *
1371 * If the audit subsystem is being terminated, record the task (pid)
1372 * and uid that is doing that.
1373 */
Al Viroe1396062006-05-25 10:19:47 -04001374void __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001375{
1376 extern pid_t audit_sig_pid;
1377 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04001378 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001379
Al Viroe1396062006-05-25 10:19:47 -04001380 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1381 struct task_struct *tsk = current;
1382 struct audit_context *ctx = tsk->audit_context;
1383 audit_sig_pid = tsk->pid;
1384 if (ctx)
1385 audit_sig_uid = ctx->loginuid;
1386 else
1387 audit_sig_uid = tsk->uid;
1388 selinux_get_task_sid(tsk, &audit_sig_sid);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001389 }
1390}