blob: 4fc3867fa25afcaf4d6f3d795409d4573e0788c7 [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;
191 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500192 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 break;
194 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500195 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500198 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500201 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 break;
203 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500204 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500207 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
209 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500210 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
212 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500213 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 break;
215 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500216 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
2fd6f582005-04-29 16:08:28 +0100218 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000219 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500220 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 case AUDIT_EXIT:
224 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500225 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 break;
227 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100228 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500229 if (f->val)
230 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100231 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500232 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 break;
235 case AUDIT_DEVMAJOR:
236 if (ctx) {
237 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500238 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 ++result;
240 break;
241 }
242 }
243 }
244 break;
245 case AUDIT_DEVMINOR:
246 if (ctx) {
247 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500248 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 ++result;
250 break;
251 }
252 }
253 }
254 break;
255 case AUDIT_INODE:
256 if (ctx) {
257 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500258 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
259 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 ++result;
261 break;
262 }
263 }
264 }
265 break;
266 case AUDIT_LOGINUID:
267 result = 0;
268 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500269 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600271 case AUDIT_SE_USER:
272 case AUDIT_SE_ROLE:
273 case AUDIT_SE_TYPE:
274 case AUDIT_SE_SEN:
275 case AUDIT_SE_CLR:
276 /* NOTE: this may return negative values indicating
277 a temporary error. We simply treat this as a
278 match for now to avoid losing information that
279 may be wanted. An error message will also be
280 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400281 if (f->se_rule) {
282 if (need_sid) {
283 selinux_task_ctxid(tsk, &sid);
284 need_sid = 0;
285 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600286 result = selinux_audit_rule_match(sid, f->type,
287 f->op,
288 f->se_rule,
289 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400290 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case AUDIT_ARG0:
293 case AUDIT_ARG1:
294 case AUDIT_ARG2:
295 case AUDIT_ARG3:
296 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500297 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 break;
299 }
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!result)
302 return 0;
303 }
304 switch (rule->action) {
305 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
306 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
307 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
308 }
309 return 1;
310}
311
312/* At process creation time, we can determine if system-call auditing is
313 * completely disabled for this task. Since we only have the task
314 * structure at this point, we can only check uid and gid.
315 */
316static enum audit_state audit_filter_task(struct task_struct *tsk)
317{
318 struct audit_entry *e;
319 enum audit_state state;
320
321 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100322 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
324 rcu_read_unlock();
325 return state;
326 }
327 }
328 rcu_read_unlock();
329 return AUDIT_BUILD_CONTEXT;
330}
331
332/* At syscall entry and exit time, this filter is called if the
333 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100334 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700335 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
337static enum audit_state audit_filter_syscall(struct task_struct *tsk,
338 struct audit_context *ctx,
339 struct list_head *list)
340{
341 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100342 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
David Woodhouse351bb722005-07-14 14:40:06 +0100344 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100345 return AUDIT_DISABLED;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100348 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000349 int word = AUDIT_WORD(ctx->major);
350 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100351
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000352 list_for_each_entry_rcu(e, list, list) {
353 if ((e->rule.mask[word] & bit) == bit
354 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
355 rcu_read_unlock();
356 return state;
357 }
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 rcu_read_unlock();
361 return AUDIT_BUILD_CONTEXT;
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364static inline struct audit_context *audit_get_context(struct task_struct *tsk,
365 int return_valid,
366 int return_code)
367{
368 struct audit_context *context = tsk->audit_context;
369
370 if (likely(!context))
371 return NULL;
372 context->return_valid = return_valid;
373 context->return_code = return_code;
374
David Woodhouse21af6c42005-07-02 14:10:46 +0100375 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100377 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (state == AUDIT_RECORD_CONTEXT)
379 context->auditable = 1;
380 }
381
382 context->pid = tsk->pid;
Al Virof46038f2006-05-06 08:22:52 -0400383 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 context->uid = tsk->uid;
385 context->gid = tsk->gid;
386 context->euid = tsk->euid;
387 context->suid = tsk->suid;
388 context->fsuid = tsk->fsuid;
389 context->egid = tsk->egid;
390 context->sgid = tsk->sgid;
391 context->fsgid = tsk->fsgid;
392 context->personality = tsk->personality;
393 tsk->audit_context = NULL;
394 return context;
395}
396
397static inline void audit_free_names(struct audit_context *context)
398{
399 int i;
400
401#if AUDIT_DEBUG == 2
402 if (context->auditable
403 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000404 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 " name_count=%d put_count=%d"
406 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000407 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 context->serial, context->major, context->in_syscall,
409 context->name_count, context->put_count,
410 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000411 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 printk(KERN_ERR "names[%d] = %p = %s\n", i,
413 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000414 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 dump_stack();
417 return;
418 }
419#endif
420#if AUDIT_DEBUG
421 context->put_count = 0;
422 context->ino_count = 0;
423#endif
424
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000425 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (context->names[i].name)
427 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100430 if (context->pwd)
431 dput(context->pwd);
432 if (context->pwdmnt)
433 mntput(context->pwdmnt);
434 context->pwd = NULL;
435 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438static inline void audit_free_aux(struct audit_context *context)
439{
440 struct audit_aux_data *aux;
441
442 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100443 if (aux->type == AUDIT_AVC_PATH) {
444 struct audit_aux_data_path *axi = (void *)aux;
445 dput(axi->dentry);
446 mntput(axi->mnt);
447 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 context->aux = aux->next;
450 kfree(aux);
451 }
452}
453
454static inline void audit_zero_context(struct audit_context *context,
455 enum audit_state state)
456{
457 uid_t loginuid = context->loginuid;
458
459 memset(context, 0, sizeof(*context));
460 context->state = state;
461 context->loginuid = loginuid;
462}
463
464static inline struct audit_context *audit_alloc_context(enum audit_state state)
465{
466 struct audit_context *context;
467
468 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
469 return NULL;
470 audit_zero_context(context, state);
471 return context;
472}
473
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700474/**
475 * audit_alloc - allocate an audit context block for a task
476 * @tsk: task
477 *
478 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * if necessary. Doing so turns on system call auditing for the
480 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700481 * needed.
482 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483int audit_alloc(struct task_struct *tsk)
484{
485 struct audit_context *context;
486 enum audit_state state;
487
488 if (likely(!audit_enabled))
489 return 0; /* Return if not auditing. */
490
491 state = audit_filter_task(tsk);
492 if (likely(state == AUDIT_DISABLED))
493 return 0;
494
495 if (!(context = audit_alloc_context(state))) {
496 audit_log_lost("out of memory in audit_alloc");
497 return -ENOMEM;
498 }
499
500 /* Preserve login uid */
501 context->loginuid = -1;
502 if (current->audit_context)
503 context->loginuid = current->audit_context->loginuid;
504
505 tsk->audit_context = context;
506 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
507 return 0;
508}
509
510static inline void audit_free_context(struct audit_context *context)
511{
512 struct audit_context *previous;
513 int count = 0;
514
515 do {
516 previous = context->previous;
517 if (previous || (count && count < 10)) {
518 ++count;
519 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
520 " freeing multiple contexts (%d)\n",
521 context->serial, context->major,
522 context->name_count, count);
523 }
524 audit_free_names(context);
525 audit_free_aux(context);
526 kfree(context);
527 context = previous;
528 } while (context);
529 if (count >= 10)
530 printk(KERN_ERR "audit: freed %d contexts\n", count);
531}
532
Al Viroe4951492006-03-29 20:17:10 -0500533static void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000534{
535 char *ctx = NULL;
536 ssize_t len = 0;
537
538 len = security_getprocattr(current, "current", NULL, 0);
539 if (len < 0) {
540 if (len != -EINVAL)
541 goto error_path;
542 return;
543 }
544
Al Viroe4951492006-03-29 20:17:10 -0500545 ctx = kmalloc(len, GFP_KERNEL);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000546 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000547 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000548
549 len = security_getprocattr(current, "current", ctx, len);
550 if (len < 0 )
551 goto error_path;
552
553 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000554 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000555
556error_path:
557 if (ctx)
558 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000559 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000560 return;
561}
562
Al Viroe4951492006-03-29 20:17:10 -0500563static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700564{
Al Viro45d9bb02006-03-29 20:02:55 -0500565 char name[sizeof(tsk->comm)];
566 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700567 struct vm_area_struct *vma;
568
Al Viroe4951492006-03-29 20:17:10 -0500569 /* tsk == current */
570
Al Viro45d9bb02006-03-29 20:02:55 -0500571 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100572 audit_log_format(ab, " comm=");
573 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700574
Al Viroe4951492006-03-29 20:17:10 -0500575 if (mm) {
576 down_read(&mm->mmap_sem);
577 vma = mm->mmap;
578 while (vma) {
579 if ((vma->vm_flags & VM_EXECUTABLE) &&
580 vma->vm_file) {
581 audit_log_d_path(ab, "exe=",
582 vma->vm_file->f_dentry,
583 vma->vm_file->f_vfsmnt);
584 break;
585 }
586 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700587 }
Al Viroe4951492006-03-29 20:17:10 -0500588 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700589 }
Al Viroe4951492006-03-29 20:17:10 -0500590 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700591}
592
Al Viroe4951492006-03-29 20:17:10 -0500593static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500595 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100597 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500598 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Al Viroe4951492006-03-29 20:17:10 -0500600 /* tsk == current */
601
602 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!ab)
604 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100605 audit_log_format(ab, "arch=%x syscall=%d",
606 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (context->personality != PER_LINUX)
608 audit_log_format(ab, " per=%lx", context->personality);
609 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100610 audit_log_format(ab, " success=%s exit=%ld",
611 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
612 context->return_code);
Al Viro45d9bb02006-03-29 20:02:55 -0500613 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
614 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500615 else
616 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 audit_log_format(ab,
618 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -0400619 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +0100620 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500621 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 context->argv[0],
623 context->argv[1],
624 context->argv[2],
625 context->argv[3],
626 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -0400627 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 context->pid,
629 context->loginuid,
630 context->uid,
631 context->gid,
632 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500633 context->egid, context->sgid, context->fsgid, tty);
Al Viroe4951492006-03-29 20:17:10 -0500634 audit_log_task_info(ab, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
David Woodhouse7551ced2005-05-26 12:04:57 +0100637 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100638
Al Viroe4951492006-03-29 20:17:10 -0500639 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!ab)
641 continue; /* audit_panic has been called */
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100644 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct audit_aux_data_ipcctl *axi = (void *)aux;
646 audit_log_format(ab,
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500647 " qbytes=%lx iuid=%u igid=%u mode=%x",
648 axi->qbytes, axi->uid, axi->gid, axi->mode);
649 if (axi->osid != 0) {
650 char *ctx = NULL;
651 u32 len;
652 if (selinux_ctxid_to_string(
653 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500654 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500655 axi->osid);
656 call_panic = 1;
657 } else
658 audit_log_format(ab, " obj=%s", ctx);
659 kfree(ctx);
660 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100661 break; }
662
Steve Grubb073115d2006-04-02 17:07:33 -0400663 case AUDIT_IPC_SET_PERM: {
664 struct audit_aux_data_ipcctl *axi = (void *)aux;
665 audit_log_format(ab,
666 " new qbytes=%lx new iuid=%u new igid=%u new mode=%x",
667 axi->qbytes, axi->uid, axi->gid, axi->mode);
668 if (axi->osid != 0) {
669 char *ctx = NULL;
670 u32 len;
671 if (selinux_ctxid_to_string(
672 axi->osid, &ctx, &len)) {
673 audit_log_format(ab, " osid=%u",
674 axi->osid);
675 call_panic = 1;
676 } else
677 audit_log_format(ab, " obj=%s", ctx);
678 kfree(ctx);
679 }
680 break; }
Al Viro473ae302006-04-26 14:04:08 -0400681 case AUDIT_EXECVE: {
682 struct audit_aux_data_execve *axi = (void *)aux;
683 int i;
684 const char *p;
685 for (i = 0, p = axi->mem; i < axi->argc; i++) {
686 audit_log_format(ab, "a%d=", i);
687 p = audit_log_untrustedstring(ab, p);
688 audit_log_format(ab, "\n");
689 }
690 break; }
Steve Grubb073115d2006-04-02 17:07:33 -0400691
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100692 case AUDIT_SOCKETCALL: {
693 int i;
694 struct audit_aux_data_socketcall *axs = (void *)aux;
695 audit_log_format(ab, "nargs=%d", axs->nargs);
696 for (i=0; i<axs->nargs; i++)
697 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
698 break; }
699
700 case AUDIT_SOCKADDR: {
701 struct audit_aux_data_sockaddr *axs = (void *)aux;
702
703 audit_log_format(ab, "saddr=");
704 audit_log_hex(ab, axs->a, axs->len);
705 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100706
707 case AUDIT_AVC_PATH: {
708 struct audit_aux_data_path *axi = (void *)aux;
709 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100710 break; }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
David Woodhouse8f37d472005-05-27 12:17:28 +0100716 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -0500717 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100718 if (ab) {
719 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
720 audit_log_end(ab);
721 }
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000724 unsigned long ino = context->names[i].ino;
725 unsigned long pino = context->names[i].pino;
726
Al Viroe4951492006-03-29 20:17:10 -0500727 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!ab)
729 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000732
733 audit_log_format(ab, " name=");
734 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100735 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000736 else
737 audit_log_format(ab, "(null)");
738
739 if (pino != (unsigned long)-1)
740 audit_log_format(ab, " parent=%lu", pino);
741 if (ino != (unsigned long)-1)
742 audit_log_format(ab, " inode=%lu", ino);
743 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
744 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
745 " ouid=%u ogid=%u rdev=%02x:%02x",
746 MAJOR(context->names[i].dev),
747 MINOR(context->names[i].dev),
748 context->names[i].mode,
749 context->names[i].uid,
750 context->names[i].gid,
751 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 MINOR(context->names[i].rdev));
Steve Grubb1b50eed2006-04-03 14:06:13 -0400753 if (context->names[i].osid != 0) {
754 char *ctx = NULL;
755 u32 len;
756 if (selinux_ctxid_to_string(
757 context->names[i].osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500758 audit_log_format(ab, " osid=%u",
Steve Grubb1b50eed2006-04-03 14:06:13 -0400759 context->names[i].osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500760 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400761 } else
762 audit_log_format(ab, " obj=%s", ctx);
763 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000764 }
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 audit_log_end(ab);
767 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500768 if (call_panic)
769 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700772/**
773 * audit_free - free a per-task audit context
774 * @tsk: task whose audit context block to free
775 *
Al Virofa84cb92006-03-29 20:30:19 -0500776 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700777 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778void audit_free(struct task_struct *tsk)
779{
780 struct audit_context *context;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (likely(!context))
784 return;
785
786 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100787 * function (e.g., exit_group), then free context block.
788 * We use GFP_ATOMIC here because we might be doing this
789 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -0500790 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +0100791 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500792 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 audit_free_context(context);
795}
796
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700797/**
798 * audit_syscall_entry - fill in an audit record at syscall entry
799 * @tsk: task being audited
800 * @arch: architecture type
801 * @major: major syscall type (function)
802 * @a1: additional syscall register 1
803 * @a2: additional syscall register 2
804 * @a3: additional syscall register 3
805 * @a4: additional syscall register 4
806 *
807 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * audit context was created when the task was created and the state or
809 * filters demand the audit context be built. If the state from the
810 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
811 * then the record will be written at syscall exit time (otherwise, it
812 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700813 * be written).
814 */
Al Viro5411be52006-03-29 20:23:36 -0500815void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 unsigned long a1, unsigned long a2,
817 unsigned long a3, unsigned long a4)
818{
Al Viro5411be52006-03-29 20:23:36 -0500819 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 struct audit_context *context = tsk->audit_context;
821 enum audit_state state;
822
823 BUG_ON(!context);
824
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700825 /*
826 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * calls in kernel_thread via the entry.S interface, instead of
828 * with direct calls. (If you are porting to a new
829 * architecture, hitting this condition can indicate that you
830 * got the _exit/_leave calls backward in entry.S.)
831 *
832 * i386 no
833 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -0600834 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 *
836 * This also happens with vm86 emulation in a non-nested manner
837 * (entries without exits), so this case must be caught.
838 */
839 if (context->in_syscall) {
840 struct audit_context *newctx;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842#if AUDIT_DEBUG
843 printk(KERN_ERR
844 "audit(:%d) pid=%d in syscall=%d;"
845 " entering syscall=%d\n",
846 context->serial, tsk->pid, context->major, major);
847#endif
848 newctx = audit_alloc_context(context->state);
849 if (newctx) {
850 newctx->previous = context;
851 context = newctx;
852 tsk->audit_context = newctx;
853 } else {
854 /* If we can't alloc a new context, the best we
855 * can do is to leak memory (any pending putname
856 * will be lost). The only other alternative is
857 * to abandon auditing. */
858 audit_zero_context(context, context->state);
859 }
860 }
861 BUG_ON(context->in_syscall || context->name_count);
862
863 if (!audit_enabled)
864 return;
865
2fd6f582005-04-29 16:08:28 +0100866 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 context->major = major;
868 context->argv[0] = a1;
869 context->argv[1] = a2;
870 context->argv[2] = a3;
871 context->argv[3] = a4;
872
873 state = context->state;
874 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100875 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (likely(state == AUDIT_DISABLED))
877 return;
878
David Woodhousece625a82005-07-18 14:24:46 -0400879 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 context->ctime = CURRENT_TIME;
881 context->in_syscall = 1;
882 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
883}
884
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700885/**
886 * audit_syscall_exit - deallocate audit context after a system call
887 * @tsk: task being audited
888 * @valid: success/failure flag
889 * @return_code: syscall return value
890 *
891 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
893 * filtering, or because some other part of the kernel write an audit
894 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700895 * free the names stored from getname().
896 */
Al Viro5411be52006-03-29 20:23:36 -0500897void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Al Viro5411be52006-03-29 20:23:36 -0500899 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct audit_context *context;
901
2fd6f582005-04-29 16:08:28 +0100902 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -0500905 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
David Woodhousef7056d62005-06-20 16:07:33 +0100907 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500908 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 context->in_syscall = 0;
911 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (context->previous) {
914 struct audit_context *new_context = context->previous;
915 context->previous = NULL;
916 audit_free_context(context);
917 tsk->audit_context = new_context;
918 } else {
919 audit_free_names(context);
920 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 tsk->audit_context = context;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700925/**
926 * audit_getname - add a name to the list
927 * @name: name to add
928 *
929 * Add a name to the list of audit names for this context.
930 * Called from fs/namei.c:getname().
931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932void audit_getname(const char *name)
933{
934 struct audit_context *context = current->audit_context;
935
936 if (!context || IS_ERR(name) || !name)
937 return;
938
939 if (!context->in_syscall) {
940#if AUDIT_DEBUG == 2
941 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
942 __FILE__, __LINE__, context->serial, name);
943 dump_stack();
944#endif
945 return;
946 }
947 BUG_ON(context->name_count >= AUDIT_NAMES);
948 context->names[context->name_count].name = name;
949 context->names[context->name_count].ino = (unsigned long)-1;
950 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100951 if (!context->pwd) {
952 read_lock(&current->fs->lock);
953 context->pwd = dget(current->fs->pwd);
954 context->pwdmnt = mntget(current->fs->pwdmnt);
955 read_unlock(&current->fs->lock);
956 }
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700960/* audit_putname - intercept a putname request
961 * @name: name to intercept and delay for putname
962 *
963 * If we have stored the name from getname in the audit context,
964 * then we delay the putname until syscall exit.
965 * Called from include/linux/fs.h:putname().
966 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967void audit_putname(const char *name)
968{
969 struct audit_context *context = current->audit_context;
970
971 BUG_ON(!context);
972 if (!context->in_syscall) {
973#if AUDIT_DEBUG == 2
974 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
975 __FILE__, __LINE__, context->serial, name);
976 if (context->name_count) {
977 int i;
978 for (i = 0; i < context->name_count; i++)
979 printk(KERN_ERR "name[%d] = %p = %s\n", i,
980 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000981 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983#endif
984 __putname(name);
985 }
986#if AUDIT_DEBUG
987 else {
988 ++context->put_count;
989 if (context->put_count > context->name_count) {
990 printk(KERN_ERR "%s:%d(:%d): major=%d"
991 " in_syscall=%d putname(%p) name_count=%d"
992 " put_count=%d\n",
993 __FILE__, __LINE__,
994 context->serial, context->major,
995 context->in_syscall, name, context->name_count,
996 context->put_count);
997 dump_stack();
998 }
999 }
1000#endif
1001}
1002
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001003static void audit_inode_context(int idx, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001004{
1005 struct audit_context *context = current->audit_context;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001006
Steve Grubb1b50eed2006-04-03 14:06:13 -04001007 selinux_get_inode_sid(inode, &context->names[idx].osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001008}
1009
1010
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001011/**
1012 * audit_inode - store the inode and device from a lookup
1013 * @name: name being audited
1014 * @inode: inode being audited
1015 * @flags: lookup flags (as used in path_lookup())
1016 *
1017 * Called from fs/namei.c:path_lookup().
1018 */
Amy Griffis73241cc2005-11-03 16:00:25 +00001019void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 int idx;
1022 struct audit_context *context = current->audit_context;
1023
1024 if (!context->in_syscall)
1025 return;
1026 if (context->name_count
1027 && context->names[context->name_count-1].name
1028 && context->names[context->name_count-1].name == name)
1029 idx = context->name_count - 1;
1030 else if (context->name_count > 1
1031 && context->names[context->name_count-2].name
1032 && context->names[context->name_count-2].name == name)
1033 idx = context->name_count - 2;
1034 else {
1035 /* FIXME: how much do we care about inodes that have no
1036 * associated name? */
1037 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1038 return;
1039 idx = context->name_count++;
1040 context->names[idx].name = NULL;
1041#if AUDIT_DEBUG
1042 ++context->ino_count;
1043#endif
1044 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001045 context->names[idx].dev = inode->i_sb->s_dev;
1046 context->names[idx].mode = inode->i_mode;
1047 context->names[idx].uid = inode->i_uid;
1048 context->names[idx].gid = inode->i_gid;
1049 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001050 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001051 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1052 (strcmp(name, ".") != 0)) {
1053 context->names[idx].ino = (unsigned long)-1;
1054 context->names[idx].pino = inode->i_ino;
1055 } else {
1056 context->names[idx].ino = inode->i_ino;
1057 context->names[idx].pino = (unsigned long)-1;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Amy Griffis73241cc2005-11-03 16:00:25 +00001061/**
1062 * audit_inode_child - collect inode info for created/removed objects
1063 * @dname: inode's dentry name
1064 * @inode: inode being audited
1065 * @pino: inode number of dentry parent
1066 *
1067 * For syscalls that create or remove filesystem objects, audit_inode
1068 * can only collect information for the filesystem object's parent.
1069 * This call updates the audit context with the child's information.
1070 * Syscalls that create a new filesystem object must be hooked after
1071 * the object is created. Syscalls that remove a filesystem object
1072 * must be hooked prior, in order to capture the target inode during
1073 * unsuccessful attempts.
1074 */
1075void __audit_inode_child(const char *dname, const struct inode *inode,
1076 unsigned long pino)
1077{
1078 int idx;
1079 struct audit_context *context = current->audit_context;
1080
1081 if (!context->in_syscall)
1082 return;
1083
1084 /* determine matching parent */
1085 if (dname)
1086 for (idx = 0; idx < context->name_count; idx++)
1087 if (context->names[idx].pino == pino) {
1088 const char *n;
1089 const char *name = context->names[idx].name;
1090 int dlen = strlen(dname);
1091 int nlen = name ? strlen(name) : 0;
1092
1093 if (nlen < dlen)
1094 continue;
1095
1096 /* disregard trailing slashes */
1097 n = name + nlen - 1;
1098 while ((*n == '/') && (n > name))
1099 n--;
1100
1101 /* find last path component */
1102 n = n - dlen + 1;
1103 if (n < name)
1104 continue;
1105 else if (n > name) {
1106 if (*--n != '/')
1107 continue;
1108 else
1109 n++;
1110 }
1111
1112 if (strncmp(n, dname, dlen) == 0)
1113 goto update_context;
1114 }
1115
1116 /* catch-all in case match not found */
1117 idx = context->name_count++;
1118 context->names[idx].name = NULL;
1119 context->names[idx].pino = pino;
1120#if AUDIT_DEBUG
1121 context->ino_count++;
1122#endif
1123
1124update_context:
1125 if (inode) {
1126 context->names[idx].ino = inode->i_ino;
1127 context->names[idx].dev = inode->i_sb->s_dev;
1128 context->names[idx].mode = inode->i_mode;
1129 context->names[idx].uid = inode->i_uid;
1130 context->names[idx].gid = inode->i_gid;
1131 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001132 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001136/**
1137 * auditsc_get_stamp - get local copies of audit_context values
1138 * @ctx: audit_context for the task
1139 * @t: timespec to store time recorded in the audit_context
1140 * @serial: serial value that is recorded in the audit_context
1141 *
1142 * Also sets the context as auditable.
1143 */
David Woodhousebfb44962005-05-21 21:08:09 +01001144void auditsc_get_stamp(struct audit_context *ctx,
1145 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
David Woodhousece625a82005-07-18 14:24:46 -04001147 if (!ctx->serial)
1148 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001149 t->tv_sec = ctx->ctime.tv_sec;
1150 t->tv_nsec = ctx->ctime.tv_nsec;
1151 *serial = ctx->serial;
1152 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001155/**
1156 * audit_set_loginuid - set a task's audit_context loginuid
1157 * @task: task whose audit context is being modified
1158 * @loginuid: loginuid value
1159 *
1160 * Returns 0.
1161 *
1162 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1163 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001164int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Steve Grubb456be6c2005-04-29 17:30:07 +01001166 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001167 struct audit_buffer *ab;
1168
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001169 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001170 if (ab) {
1171 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001172 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001173 task->pid, task->uid,
1174 task->audit_context->loginuid, loginuid);
1175 audit_log_end(ab);
1176 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001177 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179 return 0;
1180}
1181
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001182/**
1183 * audit_get_loginuid - get the loginuid for an audit_context
1184 * @ctx: the audit_context
1185 *
1186 * Returns the context's loginuid or -1 if @ctx is NULL.
1187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188uid_t audit_get_loginuid(struct audit_context *ctx)
1189{
1190 return ctx ? ctx->loginuid : -1;
1191}
1192
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001193/**
Steve Grubb073115d2006-04-02 17:07:33 -04001194 * audit_ipc_obj - record audit data for ipc object
1195 * @ipcp: ipc permissions
1196 *
1197 * Returns 0 for success or NULL context or < 0 on error.
1198 */
1199int audit_ipc_obj(struct kern_ipc_perm *ipcp)
1200{
1201 struct audit_aux_data_ipcctl *ax;
1202 struct audit_context *context = current->audit_context;
1203
1204 if (likely(!context))
1205 return 0;
1206
1207 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1208 if (!ax)
1209 return -ENOMEM;
1210
1211 ax->uid = ipcp->uid;
1212 ax->gid = ipcp->gid;
1213 ax->mode = ipcp->mode;
1214 selinux_get_ipc_sid(ipcp, &ax->osid);
1215
1216 ax->d.type = AUDIT_IPC;
1217 ax->d.next = context->aux;
1218 context->aux = (void *)ax;
1219 return 0;
1220}
1221
1222/**
1223 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001224 * @qbytes: msgq bytes
1225 * @uid: msgq user id
1226 * @gid: msgq group id
1227 * @mode: msgq mode (permissions)
1228 *
1229 * Returns 0 for success or NULL context or < 0 on error.
1230 */
Steve Grubb073115d2006-04-02 17:07:33 -04001231int audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct audit_aux_data_ipcctl *ax;
1234 struct audit_context *context = current->audit_context;
1235
1236 if (likely(!context))
1237 return 0;
1238
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001239 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (!ax)
1241 return -ENOMEM;
1242
1243 ax->qbytes = qbytes;
1244 ax->uid = uid;
1245 ax->gid = gid;
1246 ax->mode = mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001247 selinux_get_ipc_sid(ipcp, &ax->osid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Steve Grubb073115d2006-04-02 17:07:33 -04001249 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ax->d.next = context->aux;
1251 context->aux = (void *)ax;
1252 return 0;
1253}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001254
Al Viro473ae302006-04-26 14:04:08 -04001255int audit_bprm(struct linux_binprm *bprm)
1256{
1257 struct audit_aux_data_execve *ax;
1258 struct audit_context *context = current->audit_context;
1259 unsigned long p, next;
1260 void *to;
1261
1262 if (likely(!audit_enabled || !context))
1263 return 0;
1264
1265 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1266 GFP_KERNEL);
1267 if (!ax)
1268 return -ENOMEM;
1269
1270 ax->argc = bprm->argc;
1271 ax->envc = bprm->envc;
1272 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1273 struct page *page = bprm->page[p / PAGE_SIZE];
1274 void *kaddr = kmap(page);
1275 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1276 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1277 to += next - p;
1278 kunmap(page);
1279 }
1280
1281 ax->d.type = AUDIT_EXECVE;
1282 ax->d.next = context->aux;
1283 context->aux = (void *)ax;
1284 return 0;
1285}
1286
1287
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001288/**
1289 * audit_socketcall - record audit data for sys_socketcall
1290 * @nargs: number of args
1291 * @args: args array
1292 *
1293 * Returns 0 for success or NULL context or < 0 on error.
1294 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001295int audit_socketcall(int nargs, unsigned long *args)
1296{
1297 struct audit_aux_data_socketcall *ax;
1298 struct audit_context *context = current->audit_context;
1299
1300 if (likely(!context))
1301 return 0;
1302
1303 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1304 if (!ax)
1305 return -ENOMEM;
1306
1307 ax->nargs = nargs;
1308 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1309
1310 ax->d.type = AUDIT_SOCKETCALL;
1311 ax->d.next = context->aux;
1312 context->aux = (void *)ax;
1313 return 0;
1314}
1315
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001316/**
1317 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1318 * @len: data length in user space
1319 * @a: data address in kernel space
1320 *
1321 * Returns 0 for success or NULL context or < 0 on error.
1322 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001323int audit_sockaddr(int len, void *a)
1324{
1325 struct audit_aux_data_sockaddr *ax;
1326 struct audit_context *context = current->audit_context;
1327
1328 if (likely(!context))
1329 return 0;
1330
1331 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1332 if (!ax)
1333 return -ENOMEM;
1334
1335 ax->len = len;
1336 memcpy(ax->a, a, len);
1337
1338 ax->d.type = AUDIT_SOCKADDR;
1339 ax->d.next = context->aux;
1340 context->aux = (void *)ax;
1341 return 0;
1342}
1343
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001344/**
1345 * audit_avc_path - record the granting or denial of permissions
1346 * @dentry: dentry to record
1347 * @mnt: mnt to record
1348 *
1349 * Returns 0 for success or NULL context or < 0 on error.
1350 *
1351 * Called from security/selinux/avc.c::avc_audit()
1352 */
Stephen Smalley01116102005-05-21 00:15:52 +01001353int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1354{
1355 struct audit_aux_data_path *ax;
1356 struct audit_context *context = current->audit_context;
1357
1358 if (likely(!context))
1359 return 0;
1360
1361 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1362 if (!ax)
1363 return -ENOMEM;
1364
1365 ax->dentry = dget(dentry);
1366 ax->mnt = mntget(mnt);
1367
1368 ax->d.type = AUDIT_AVC_PATH;
1369 ax->d.next = context->aux;
1370 context->aux = (void *)ax;
1371 return 0;
1372}
1373
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001374/**
1375 * audit_signal_info - record signal info for shutting down audit subsystem
1376 * @sig: signal value
1377 * @t: task being signaled
1378 *
1379 * If the audit subsystem is being terminated, record the task (pid)
1380 * and uid that is doing that.
1381 */
Al Viroe1396062006-05-25 10:19:47 -04001382void __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001383{
1384 extern pid_t audit_sig_pid;
1385 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04001386 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001387
Al Viroe1396062006-05-25 10:19:47 -04001388 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1389 struct task_struct *tsk = current;
1390 struct audit_context *ctx = tsk->audit_context;
1391 audit_sig_pid = tsk->pid;
1392 if (ctx)
1393 audit_sig_uid = ctx->loginuid;
1394 else
1395 audit_sig_uid = tsk->uid;
1396 selinux_get_task_sid(tsk, &audit_sig_sid);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001397 }
1398}