blob: 4ca913daa7da48807451a53a6396047a29b5b516 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
David Woodhousefe7752b2005-12-15 18:33:52 +000064#include "audit.h"
65
66extern struct list_head audit_filter_list[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* No syscall auditing will take place unless audit_enabled != 0. */
69extern int audit_enabled;
70
71/* AUDIT_NAMES is the number of slots we reserve in the audit_context
72 * for saving names from getname(). */
73#define AUDIT_NAMES 20
74
75/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
76 * audit_context from being used for nameless inodes from
77 * path_lookup. */
78#define AUDIT_NAMES_RESERVED 7
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* When fs/namei.c:getname() is called, we store the pointer in name and
81 * we don't let putname() free it (instead we free all of the saved
82 * pointers at syscall exit time).
83 *
84 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
85struct audit_names {
86 const char *name;
87 unsigned long ino;
Amy Griffis73241cc2005-11-03 16:00:25 +000088 unsigned long pino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 dev_t dev;
90 umode_t mode;
91 uid_t uid;
92 gid_t gid;
93 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -040094 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97struct audit_aux_data {
98 struct audit_aux_data *next;
99 int type;
100};
101
102#define AUDIT_AUX_IPCPERM 0
103
104struct audit_aux_data_ipcctl {
105 struct audit_aux_data d;
106 struct ipc_perm p;
107 unsigned long qbytes;
108 uid_t uid;
109 gid_t gid;
110 mode_t mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500111 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Al Viro473ae302006-04-26 14:04:08 -0400114struct audit_aux_data_execve {
115 struct audit_aux_data d;
116 int argc;
117 int envc;
118 char mem[0];
119};
120
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100121struct audit_aux_data_socketcall {
122 struct audit_aux_data d;
123 int nargs;
124 unsigned long args[0];
125};
126
127struct audit_aux_data_sockaddr {
128 struct audit_aux_data d;
129 int len;
130 char a[0];
131};
132
Stephen Smalley01116102005-05-21 00:15:52 +0100133struct audit_aux_data_path {
134 struct audit_aux_data d;
135 struct dentry *dentry;
136 struct vfsmount *mnt;
137};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/* The per-task audit context. */
140struct audit_context {
141 int in_syscall; /* 1 if task is in a syscall */
142 enum audit_state state;
143 unsigned int serial; /* serial number for record */
144 struct timespec ctime; /* time of syscall entry */
145 uid_t loginuid; /* login uid (identity) */
146 int major; /* syscall number */
147 unsigned long argv[4]; /* syscall arguments */
148 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100149 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 int auditable; /* 1 if record should be written */
151 int name_count;
152 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100153 struct dentry * pwd;
154 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct audit_context *previous; /* For nested syscalls */
156 struct audit_aux_data *aux;
157
158 /* Save things to print about task_struct */
159 pid_t pid;
160 uid_t uid, euid, suid, fsuid;
161 gid_t gid, egid, sgid, fsgid;
162 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100163 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165#if AUDIT_DEBUG
166 int put_count;
167 int ino_count;
168#endif
169};
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/* Compare a task_struct with an audit_rule. Return 1 on match, 0
173 * otherwise. */
174static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500175 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct audit_context *ctx,
177 enum audit_state *state)
178{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400179 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600180 u32 sid;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500183 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int result = 0;
185
Amy Griffis93315ed2006-02-07 12:05:27 -0500186 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500188 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500191 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500194 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 break;
196 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500197 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500200 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500203 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500206 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
208 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500209 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
211 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500212 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500215 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
2fd6f582005-04-29 16:08:28 +0100217 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000218 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500219 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 case AUDIT_EXIT:
223 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500224 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
226 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100227 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500228 if (f->val)
229 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100230 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500231 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234 case AUDIT_DEVMAJOR:
235 if (ctx) {
236 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500237 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ++result;
239 break;
240 }
241 }
242 }
243 break;
244 case AUDIT_DEVMINOR:
245 if (ctx) {
246 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500247 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ++result;
249 break;
250 }
251 }
252 }
253 break;
254 case AUDIT_INODE:
255 if (ctx) {
256 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500257 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
258 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 ++result;
260 break;
261 }
262 }
263 }
264 break;
265 case AUDIT_LOGINUID:
266 result = 0;
267 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500268 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 break;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600270 case AUDIT_SE_USER:
271 case AUDIT_SE_ROLE:
272 case AUDIT_SE_TYPE:
273 case AUDIT_SE_SEN:
274 case AUDIT_SE_CLR:
275 /* NOTE: this may return negative values indicating
276 a temporary error. We simply treat this as a
277 match for now to avoid losing information that
278 may be wanted. An error message will also be
279 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400280 if (f->se_rule) {
281 if (need_sid) {
282 selinux_task_ctxid(tsk, &sid);
283 need_sid = 0;
284 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600285 result = selinux_audit_rule_match(sid, f->type,
286 f->op,
287 f->se_rule,
288 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400289 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 case AUDIT_ARG0:
292 case AUDIT_ARG1:
293 case AUDIT_ARG2:
294 case AUDIT_ARG3:
295 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500296 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!result)
301 return 0;
302 }
303 switch (rule->action) {
304 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
305 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
306 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
307 }
308 return 1;
309}
310
311/* At process creation time, we can determine if system-call auditing is
312 * completely disabled for this task. Since we only have the task
313 * structure at this point, we can only check uid and gid.
314 */
315static enum audit_state audit_filter_task(struct task_struct *tsk)
316{
317 struct audit_entry *e;
318 enum audit_state state;
319
320 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100321 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
323 rcu_read_unlock();
324 return state;
325 }
326 }
327 rcu_read_unlock();
328 return AUDIT_BUILD_CONTEXT;
329}
330
331/* At syscall entry and exit time, this filter is called if the
332 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100333 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700334 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336static enum audit_state audit_filter_syscall(struct task_struct *tsk,
337 struct audit_context *ctx,
338 struct list_head *list)
339{
340 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100341 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
David Woodhouse351bb722005-07-14 14:40:06 +0100343 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100344 return AUDIT_DISABLED;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100347 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000348 int word = AUDIT_WORD(ctx->major);
349 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100350
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000351 list_for_each_entry_rcu(e, list, list) {
352 if ((e->rule.mask[word] & bit) == bit
353 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
354 rcu_read_unlock();
355 return state;
356 }
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359 rcu_read_unlock();
360 return AUDIT_BUILD_CONTEXT;
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static inline struct audit_context *audit_get_context(struct task_struct *tsk,
364 int return_valid,
365 int return_code)
366{
367 struct audit_context *context = tsk->audit_context;
368
369 if (likely(!context))
370 return NULL;
371 context->return_valid = return_valid;
372 context->return_code = return_code;
373
David Woodhouse21af6c42005-07-02 14:10:46 +0100374 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100376 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (state == AUDIT_RECORD_CONTEXT)
378 context->auditable = 1;
379 }
380
381 context->pid = tsk->pid;
382 context->uid = tsk->uid;
383 context->gid = tsk->gid;
384 context->euid = tsk->euid;
385 context->suid = tsk->suid;
386 context->fsuid = tsk->fsuid;
387 context->egid = tsk->egid;
388 context->sgid = tsk->sgid;
389 context->fsgid = tsk->fsgid;
390 context->personality = tsk->personality;
391 tsk->audit_context = NULL;
392 return context;
393}
394
395static inline void audit_free_names(struct audit_context *context)
396{
397 int i;
398
399#if AUDIT_DEBUG == 2
400 if (context->auditable
401 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000402 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 " name_count=%d put_count=%d"
404 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000405 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 context->serial, context->major, context->in_syscall,
407 context->name_count, context->put_count,
408 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000409 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 printk(KERN_ERR "names[%d] = %p = %s\n", i,
411 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000412 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 dump_stack();
415 return;
416 }
417#endif
418#if AUDIT_DEBUG
419 context->put_count = 0;
420 context->ino_count = 0;
421#endif
422
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000423 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (context->names[i].name)
425 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100428 if (context->pwd)
429 dput(context->pwd);
430 if (context->pwdmnt)
431 mntput(context->pwdmnt);
432 context->pwd = NULL;
433 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436static inline void audit_free_aux(struct audit_context *context)
437{
438 struct audit_aux_data *aux;
439
440 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100441 if (aux->type == AUDIT_AVC_PATH) {
442 struct audit_aux_data_path *axi = (void *)aux;
443 dput(axi->dentry);
444 mntput(axi->mnt);
445 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 context->aux = aux->next;
448 kfree(aux);
449 }
450}
451
452static inline void audit_zero_context(struct audit_context *context,
453 enum audit_state state)
454{
455 uid_t loginuid = context->loginuid;
456
457 memset(context, 0, sizeof(*context));
458 context->state = state;
459 context->loginuid = loginuid;
460}
461
462static inline struct audit_context *audit_alloc_context(enum audit_state state)
463{
464 struct audit_context *context;
465
466 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
467 return NULL;
468 audit_zero_context(context, state);
469 return context;
470}
471
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700472/**
473 * audit_alloc - allocate an audit context block for a task
474 * @tsk: task
475 *
476 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 * if necessary. Doing so turns on system call auditing for the
478 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700479 * needed.
480 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481int audit_alloc(struct task_struct *tsk)
482{
483 struct audit_context *context;
484 enum audit_state state;
485
486 if (likely(!audit_enabled))
487 return 0; /* Return if not auditing. */
488
489 state = audit_filter_task(tsk);
490 if (likely(state == AUDIT_DISABLED))
491 return 0;
492
493 if (!(context = audit_alloc_context(state))) {
494 audit_log_lost("out of memory in audit_alloc");
495 return -ENOMEM;
496 }
497
498 /* Preserve login uid */
499 context->loginuid = -1;
500 if (current->audit_context)
501 context->loginuid = current->audit_context->loginuid;
502
503 tsk->audit_context = context;
504 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
505 return 0;
506}
507
508static inline void audit_free_context(struct audit_context *context)
509{
510 struct audit_context *previous;
511 int count = 0;
512
513 do {
514 previous = context->previous;
515 if (previous || (count && count < 10)) {
516 ++count;
517 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
518 " freeing multiple contexts (%d)\n",
519 context->serial, context->major,
520 context->name_count, count);
521 }
522 audit_free_names(context);
523 audit_free_aux(context);
524 kfree(context);
525 context = previous;
526 } while (context);
527 if (count >= 10)
528 printk(KERN_ERR "audit: freed %d contexts\n", count);
529}
530
Al Viroe4951492006-03-29 20:17:10 -0500531static void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000532{
533 char *ctx = NULL;
534 ssize_t len = 0;
535
536 len = security_getprocattr(current, "current", NULL, 0);
537 if (len < 0) {
538 if (len != -EINVAL)
539 goto error_path;
540 return;
541 }
542
Al Viroe4951492006-03-29 20:17:10 -0500543 ctx = kmalloc(len, GFP_KERNEL);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000544 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000545 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000546
547 len = security_getprocattr(current, "current", ctx, len);
548 if (len < 0 )
549 goto error_path;
550
551 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000552 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000553
554error_path:
555 if (ctx)
556 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000557 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000558 return;
559}
560
Al Viroe4951492006-03-29 20:17:10 -0500561static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700562{
Al Viro45d9bb02006-03-29 20:02:55 -0500563 char name[sizeof(tsk->comm)];
564 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700565 struct vm_area_struct *vma;
566
Al Viroe4951492006-03-29 20:17:10 -0500567 /* tsk == current */
568
Al Viro45d9bb02006-03-29 20:02:55 -0500569 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100570 audit_log_format(ab, " comm=");
571 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700572
Al Viroe4951492006-03-29 20:17:10 -0500573 if (mm) {
574 down_read(&mm->mmap_sem);
575 vma = mm->mmap;
576 while (vma) {
577 if ((vma->vm_flags & VM_EXECUTABLE) &&
578 vma->vm_file) {
579 audit_log_d_path(ab, "exe=",
580 vma->vm_file->f_dentry,
581 vma->vm_file->f_vfsmnt);
582 break;
583 }
584 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700585 }
Al Viroe4951492006-03-29 20:17:10 -0500586 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700587 }
Al Viroe4951492006-03-29 20:17:10 -0500588 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700589}
590
Al Viroe4951492006-03-29 20:17:10 -0500591static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500593 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100595 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500596 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Al Viroe4951492006-03-29 20:17:10 -0500598 /* tsk == current */
599
600 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!ab)
602 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100603 audit_log_format(ab, "arch=%x syscall=%d",
604 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (context->personality != PER_LINUX)
606 audit_log_format(ab, " per=%lx", context->personality);
607 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100608 audit_log_format(ab, " success=%s exit=%ld",
609 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
610 context->return_code);
Al Viro45d9bb02006-03-29 20:02:55 -0500611 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
612 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500613 else
614 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 audit_log_format(ab,
616 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100617 " pid=%d auid=%u uid=%u gid=%u"
618 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500619 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 context->argv[0],
621 context->argv[1],
622 context->argv[2],
623 context->argv[3],
624 context->name_count,
625 context->pid,
626 context->loginuid,
627 context->uid,
628 context->gid,
629 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500630 context->egid, context->sgid, context->fsgid, tty);
Al Viroe4951492006-03-29 20:17:10 -0500631 audit_log_task_info(ab, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
David Woodhouse7551ced2005-05-26 12:04:57 +0100634 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100635
Al Viroe4951492006-03-29 20:17:10 -0500636 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (!ab)
638 continue; /* audit_panic has been called */
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100641 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 struct audit_aux_data_ipcctl *axi = (void *)aux;
643 audit_log_format(ab,
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500644 " qbytes=%lx iuid=%u igid=%u mode=%x",
645 axi->qbytes, axi->uid, axi->gid, axi->mode);
646 if (axi->osid != 0) {
647 char *ctx = NULL;
648 u32 len;
649 if (selinux_ctxid_to_string(
650 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500651 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500652 axi->osid);
653 call_panic = 1;
654 } else
655 audit_log_format(ab, " obj=%s", ctx);
656 kfree(ctx);
657 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100658 break; }
659
Steve Grubb073115d2006-04-02 17:07:33 -0400660 case AUDIT_IPC_SET_PERM: {
661 struct audit_aux_data_ipcctl *axi = (void *)aux;
662 audit_log_format(ab,
663 " new qbytes=%lx new iuid=%u new igid=%u new mode=%x",
664 axi->qbytes, axi->uid, axi->gid, axi->mode);
665 if (axi->osid != 0) {
666 char *ctx = NULL;
667 u32 len;
668 if (selinux_ctxid_to_string(
669 axi->osid, &ctx, &len)) {
670 audit_log_format(ab, " osid=%u",
671 axi->osid);
672 call_panic = 1;
673 } else
674 audit_log_format(ab, " obj=%s", ctx);
675 kfree(ctx);
676 }
677 break; }
Al Viro473ae302006-04-26 14:04:08 -0400678 case AUDIT_EXECVE: {
679 struct audit_aux_data_execve *axi = (void *)aux;
680 int i;
681 const char *p;
682 for (i = 0, p = axi->mem; i < axi->argc; i++) {
683 audit_log_format(ab, "a%d=", i);
684 p = audit_log_untrustedstring(ab, p);
685 audit_log_format(ab, "\n");
686 }
687 break; }
Steve Grubb073115d2006-04-02 17:07:33 -0400688
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100689 case AUDIT_SOCKETCALL: {
690 int i;
691 struct audit_aux_data_socketcall *axs = (void *)aux;
692 audit_log_format(ab, "nargs=%d", axs->nargs);
693 for (i=0; i<axs->nargs; i++)
694 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
695 break; }
696
697 case AUDIT_SOCKADDR: {
698 struct audit_aux_data_sockaddr *axs = (void *)aux;
699
700 audit_log_format(ab, "saddr=");
701 audit_log_hex(ab, axs->a, axs->len);
702 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100703
704 case AUDIT_AVC_PATH: {
705 struct audit_aux_data_path *axi = (void *)aux;
706 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100707 break; }
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
David Woodhouse8f37d472005-05-27 12:17:28 +0100713 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -0500714 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100715 if (ab) {
716 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
717 audit_log_end(ab);
718 }
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000721 unsigned long ino = context->names[i].ino;
722 unsigned long pino = context->names[i].pino;
723
Al Viroe4951492006-03-29 20:17:10 -0500724 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!ab)
726 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000729
730 audit_log_format(ab, " name=");
731 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100732 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000733 else
734 audit_log_format(ab, "(null)");
735
736 if (pino != (unsigned long)-1)
737 audit_log_format(ab, " parent=%lu", pino);
738 if (ino != (unsigned long)-1)
739 audit_log_format(ab, " inode=%lu", ino);
740 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
741 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
742 " ouid=%u ogid=%u rdev=%02x:%02x",
743 MAJOR(context->names[i].dev),
744 MINOR(context->names[i].dev),
745 context->names[i].mode,
746 context->names[i].uid,
747 context->names[i].gid,
748 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 MINOR(context->names[i].rdev));
Steve Grubb1b50eed2006-04-03 14:06:13 -0400750 if (context->names[i].osid != 0) {
751 char *ctx = NULL;
752 u32 len;
753 if (selinux_ctxid_to_string(
754 context->names[i].osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500755 audit_log_format(ab, " osid=%u",
Steve Grubb1b50eed2006-04-03 14:06:13 -0400756 context->names[i].osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500757 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400758 } else
759 audit_log_format(ab, " obj=%s", ctx);
760 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000761 }
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 audit_log_end(ab);
764 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500765 if (call_panic)
766 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700769/**
770 * audit_free - free a per-task audit context
771 * @tsk: task whose audit context block to free
772 *
Al Virofa84cb92006-03-29 20:30:19 -0500773 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700774 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775void audit_free(struct task_struct *tsk)
776{
777 struct audit_context *context;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (likely(!context))
781 return;
782
783 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100784 * function (e.g., exit_group), then free context block.
785 * We use GFP_ATOMIC here because we might be doing this
786 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -0500787 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +0100788 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500789 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 audit_free_context(context);
792}
793
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700794/**
795 * audit_syscall_entry - fill in an audit record at syscall entry
796 * @tsk: task being audited
797 * @arch: architecture type
798 * @major: major syscall type (function)
799 * @a1: additional syscall register 1
800 * @a2: additional syscall register 2
801 * @a3: additional syscall register 3
802 * @a4: additional syscall register 4
803 *
804 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 * audit context was created when the task was created and the state or
806 * filters demand the audit context be built. If the state from the
807 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
808 * then the record will be written at syscall exit time (otherwise, it
809 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700810 * be written).
811 */
Al Viro5411be52006-03-29 20:23:36 -0500812void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 unsigned long a1, unsigned long a2,
814 unsigned long a3, unsigned long a4)
815{
Al Viro5411be52006-03-29 20:23:36 -0500816 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 struct audit_context *context = tsk->audit_context;
818 enum audit_state state;
819
820 BUG_ON(!context);
821
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700822 /*
823 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 * calls in kernel_thread via the entry.S interface, instead of
825 * with direct calls. (If you are porting to a new
826 * architecture, hitting this condition can indicate that you
827 * got the _exit/_leave calls backward in entry.S.)
828 *
829 * i386 no
830 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -0600831 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 *
833 * This also happens with vm86 emulation in a non-nested manner
834 * (entries without exits), so this case must be caught.
835 */
836 if (context->in_syscall) {
837 struct audit_context *newctx;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839#if AUDIT_DEBUG
840 printk(KERN_ERR
841 "audit(:%d) pid=%d in syscall=%d;"
842 " entering syscall=%d\n",
843 context->serial, tsk->pid, context->major, major);
844#endif
845 newctx = audit_alloc_context(context->state);
846 if (newctx) {
847 newctx->previous = context;
848 context = newctx;
849 tsk->audit_context = newctx;
850 } else {
851 /* If we can't alloc a new context, the best we
852 * can do is to leak memory (any pending putname
853 * will be lost). The only other alternative is
854 * to abandon auditing. */
855 audit_zero_context(context, context->state);
856 }
857 }
858 BUG_ON(context->in_syscall || context->name_count);
859
860 if (!audit_enabled)
861 return;
862
2fd6f582005-04-29 16:08:28 +0100863 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 context->major = major;
865 context->argv[0] = a1;
866 context->argv[1] = a2;
867 context->argv[2] = a3;
868 context->argv[3] = a4;
869
870 state = context->state;
871 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100872 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (likely(state == AUDIT_DISABLED))
874 return;
875
David Woodhousece625a82005-07-18 14:24:46 -0400876 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 context->ctime = CURRENT_TIME;
878 context->in_syscall = 1;
879 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
880}
881
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700882/**
883 * audit_syscall_exit - deallocate audit context after a system call
884 * @tsk: task being audited
885 * @valid: success/failure flag
886 * @return_code: syscall return value
887 *
888 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
890 * filtering, or because some other part of the kernel write an audit
891 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700892 * free the names stored from getname().
893 */
Al Viro5411be52006-03-29 20:23:36 -0500894void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Al Viro5411be52006-03-29 20:23:36 -0500896 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct audit_context *context;
898
2fd6f582005-04-29 16:08:28 +0100899 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -0500902 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
David Woodhousef7056d62005-06-20 16:07:33 +0100904 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500905 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 context->in_syscall = 0;
908 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (context->previous) {
911 struct audit_context *new_context = context->previous;
912 context->previous = NULL;
913 audit_free_context(context);
914 tsk->audit_context = new_context;
915 } else {
916 audit_free_names(context);
917 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 tsk->audit_context = context;
919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700922/**
923 * audit_getname - add a name to the list
924 * @name: name to add
925 *
926 * Add a name to the list of audit names for this context.
927 * Called from fs/namei.c:getname().
928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929void audit_getname(const char *name)
930{
931 struct audit_context *context = current->audit_context;
932
933 if (!context || IS_ERR(name) || !name)
934 return;
935
936 if (!context->in_syscall) {
937#if AUDIT_DEBUG == 2
938 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
939 __FILE__, __LINE__, context->serial, name);
940 dump_stack();
941#endif
942 return;
943 }
944 BUG_ON(context->name_count >= AUDIT_NAMES);
945 context->names[context->name_count].name = name;
946 context->names[context->name_count].ino = (unsigned long)-1;
947 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100948 if (!context->pwd) {
949 read_lock(&current->fs->lock);
950 context->pwd = dget(current->fs->pwd);
951 context->pwdmnt = mntget(current->fs->pwdmnt);
952 read_unlock(&current->fs->lock);
953 }
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700957/* audit_putname - intercept a putname request
958 * @name: name to intercept and delay for putname
959 *
960 * If we have stored the name from getname in the audit context,
961 * then we delay the putname until syscall exit.
962 * Called from include/linux/fs.h:putname().
963 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964void audit_putname(const char *name)
965{
966 struct audit_context *context = current->audit_context;
967
968 BUG_ON(!context);
969 if (!context->in_syscall) {
970#if AUDIT_DEBUG == 2
971 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
972 __FILE__, __LINE__, context->serial, name);
973 if (context->name_count) {
974 int i;
975 for (i = 0; i < context->name_count; i++)
976 printk(KERN_ERR "name[%d] = %p = %s\n", i,
977 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000978 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980#endif
981 __putname(name);
982 }
983#if AUDIT_DEBUG
984 else {
985 ++context->put_count;
986 if (context->put_count > context->name_count) {
987 printk(KERN_ERR "%s:%d(:%d): major=%d"
988 " in_syscall=%d putname(%p) name_count=%d"
989 " put_count=%d\n",
990 __FILE__, __LINE__,
991 context->serial, context->major,
992 context->in_syscall, name, context->name_count,
993 context->put_count);
994 dump_stack();
995 }
996 }
997#endif
998}
999
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001000static void audit_inode_context(int idx, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001001{
1002 struct audit_context *context = current->audit_context;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001003
Steve Grubb1b50eed2006-04-03 14:06:13 -04001004 selinux_get_inode_sid(inode, &context->names[idx].osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001005}
1006
1007
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001008/**
1009 * audit_inode - store the inode and device from a lookup
1010 * @name: name being audited
1011 * @inode: inode being audited
1012 * @flags: lookup flags (as used in path_lookup())
1013 *
1014 * Called from fs/namei.c:path_lookup().
1015 */
Amy Griffis73241cc2005-11-03 16:00:25 +00001016void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 int idx;
1019 struct audit_context *context = current->audit_context;
1020
1021 if (!context->in_syscall)
1022 return;
1023 if (context->name_count
1024 && context->names[context->name_count-1].name
1025 && context->names[context->name_count-1].name == name)
1026 idx = context->name_count - 1;
1027 else if (context->name_count > 1
1028 && context->names[context->name_count-2].name
1029 && context->names[context->name_count-2].name == name)
1030 idx = context->name_count - 2;
1031 else {
1032 /* FIXME: how much do we care about inodes that have no
1033 * associated name? */
1034 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1035 return;
1036 idx = context->name_count++;
1037 context->names[idx].name = NULL;
1038#if AUDIT_DEBUG
1039 ++context->ino_count;
1040#endif
1041 }
David Woodhouseae7b961b2005-06-20 16:11:05 +01001042 context->names[idx].dev = inode->i_sb->s_dev;
1043 context->names[idx].mode = inode->i_mode;
1044 context->names[idx].uid = inode->i_uid;
1045 context->names[idx].gid = inode->i_gid;
1046 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001047 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001048 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1049 (strcmp(name, ".") != 0)) {
1050 context->names[idx].ino = (unsigned long)-1;
1051 context->names[idx].pino = inode->i_ino;
1052 } else {
1053 context->names[idx].ino = inode->i_ino;
1054 context->names[idx].pino = (unsigned long)-1;
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Amy Griffis73241cc2005-11-03 16:00:25 +00001058/**
1059 * audit_inode_child - collect inode info for created/removed objects
1060 * @dname: inode's dentry name
1061 * @inode: inode being audited
1062 * @pino: inode number of dentry parent
1063 *
1064 * For syscalls that create or remove filesystem objects, audit_inode
1065 * can only collect information for the filesystem object's parent.
1066 * This call updates the audit context with the child's information.
1067 * Syscalls that create a new filesystem object must be hooked after
1068 * the object is created. Syscalls that remove a filesystem object
1069 * must be hooked prior, in order to capture the target inode during
1070 * unsuccessful attempts.
1071 */
1072void __audit_inode_child(const char *dname, const struct inode *inode,
1073 unsigned long pino)
1074{
1075 int idx;
1076 struct audit_context *context = current->audit_context;
1077
1078 if (!context->in_syscall)
1079 return;
1080
1081 /* determine matching parent */
1082 if (dname)
1083 for (idx = 0; idx < context->name_count; idx++)
1084 if (context->names[idx].pino == pino) {
1085 const char *n;
1086 const char *name = context->names[idx].name;
1087 int dlen = strlen(dname);
1088 int nlen = name ? strlen(name) : 0;
1089
1090 if (nlen < dlen)
1091 continue;
1092
1093 /* disregard trailing slashes */
1094 n = name + nlen - 1;
1095 while ((*n == '/') && (n > name))
1096 n--;
1097
1098 /* find last path component */
1099 n = n - dlen + 1;
1100 if (n < name)
1101 continue;
1102 else if (n > name) {
1103 if (*--n != '/')
1104 continue;
1105 else
1106 n++;
1107 }
1108
1109 if (strncmp(n, dname, dlen) == 0)
1110 goto update_context;
1111 }
1112
1113 /* catch-all in case match not found */
1114 idx = context->name_count++;
1115 context->names[idx].name = NULL;
1116 context->names[idx].pino = pino;
1117#if AUDIT_DEBUG
1118 context->ino_count++;
1119#endif
1120
1121update_context:
1122 if (inode) {
1123 context->names[idx].ino = inode->i_ino;
1124 context->names[idx].dev = inode->i_sb->s_dev;
1125 context->names[idx].mode = inode->i_mode;
1126 context->names[idx].uid = inode->i_uid;
1127 context->names[idx].gid = inode->i_gid;
1128 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001129 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001133/**
1134 * auditsc_get_stamp - get local copies of audit_context values
1135 * @ctx: audit_context for the task
1136 * @t: timespec to store time recorded in the audit_context
1137 * @serial: serial value that is recorded in the audit_context
1138 *
1139 * Also sets the context as auditable.
1140 */
David Woodhousebfb44962005-05-21 21:08:09 +01001141void auditsc_get_stamp(struct audit_context *ctx,
1142 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
David Woodhousece625a82005-07-18 14:24:46 -04001144 if (!ctx->serial)
1145 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001146 t->tv_sec = ctx->ctime.tv_sec;
1147 t->tv_nsec = ctx->ctime.tv_nsec;
1148 *serial = ctx->serial;
1149 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001152/**
1153 * audit_set_loginuid - set a task's audit_context loginuid
1154 * @task: task whose audit context is being modified
1155 * @loginuid: loginuid value
1156 *
1157 * Returns 0.
1158 *
1159 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1160 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001161int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Steve Grubb456be6c2005-04-29 17:30:07 +01001163 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001164 struct audit_buffer *ab;
1165
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001166 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001167 if (ab) {
1168 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001169 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001170 task->pid, task->uid,
1171 task->audit_context->loginuid, loginuid);
1172 audit_log_end(ab);
1173 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001174 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176 return 0;
1177}
1178
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001179/**
1180 * audit_get_loginuid - get the loginuid for an audit_context
1181 * @ctx: the audit_context
1182 *
1183 * Returns the context's loginuid or -1 if @ctx is NULL.
1184 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185uid_t audit_get_loginuid(struct audit_context *ctx)
1186{
1187 return ctx ? ctx->loginuid : -1;
1188}
1189
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001190/**
Steve Grubb073115d2006-04-02 17:07:33 -04001191 * audit_ipc_obj - record audit data for ipc object
1192 * @ipcp: ipc permissions
1193 *
1194 * Returns 0 for success or NULL context or < 0 on error.
1195 */
1196int audit_ipc_obj(struct kern_ipc_perm *ipcp)
1197{
1198 struct audit_aux_data_ipcctl *ax;
1199 struct audit_context *context = current->audit_context;
1200
1201 if (likely(!context))
1202 return 0;
1203
1204 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1205 if (!ax)
1206 return -ENOMEM;
1207
1208 ax->uid = ipcp->uid;
1209 ax->gid = ipcp->gid;
1210 ax->mode = ipcp->mode;
1211 selinux_get_ipc_sid(ipcp, &ax->osid);
1212
1213 ax->d.type = AUDIT_IPC;
1214 ax->d.next = context->aux;
1215 context->aux = (void *)ax;
1216 return 0;
1217}
1218
1219/**
1220 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001221 * @qbytes: msgq bytes
1222 * @uid: msgq user id
1223 * @gid: msgq group id
1224 * @mode: msgq mode (permissions)
1225 *
1226 * Returns 0 for success or NULL context or < 0 on error.
1227 */
Steve Grubb073115d2006-04-02 17:07:33 -04001228int 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 -07001229{
1230 struct audit_aux_data_ipcctl *ax;
1231 struct audit_context *context = current->audit_context;
1232
1233 if (likely(!context))
1234 return 0;
1235
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001236 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!ax)
1238 return -ENOMEM;
1239
1240 ax->qbytes = qbytes;
1241 ax->uid = uid;
1242 ax->gid = gid;
1243 ax->mode = mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001244 selinux_get_ipc_sid(ipcp, &ax->osid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Steve Grubb073115d2006-04-02 17:07:33 -04001246 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 ax->d.next = context->aux;
1248 context->aux = (void *)ax;
1249 return 0;
1250}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001251
Al Viro473ae302006-04-26 14:04:08 -04001252int audit_bprm(struct linux_binprm *bprm)
1253{
1254 struct audit_aux_data_execve *ax;
1255 struct audit_context *context = current->audit_context;
1256 unsigned long p, next;
1257 void *to;
1258
1259 if (likely(!audit_enabled || !context))
1260 return 0;
1261
1262 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1263 GFP_KERNEL);
1264 if (!ax)
1265 return -ENOMEM;
1266
1267 ax->argc = bprm->argc;
1268 ax->envc = bprm->envc;
1269 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1270 struct page *page = bprm->page[p / PAGE_SIZE];
1271 void *kaddr = kmap(page);
1272 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1273 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1274 to += next - p;
1275 kunmap(page);
1276 }
1277
1278 ax->d.type = AUDIT_EXECVE;
1279 ax->d.next = context->aux;
1280 context->aux = (void *)ax;
1281 return 0;
1282}
1283
1284
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001285/**
1286 * audit_socketcall - record audit data for sys_socketcall
1287 * @nargs: number of args
1288 * @args: args array
1289 *
1290 * Returns 0 for success or NULL context or < 0 on error.
1291 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001292int audit_socketcall(int nargs, unsigned long *args)
1293{
1294 struct audit_aux_data_socketcall *ax;
1295 struct audit_context *context = current->audit_context;
1296
1297 if (likely(!context))
1298 return 0;
1299
1300 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1301 if (!ax)
1302 return -ENOMEM;
1303
1304 ax->nargs = nargs;
1305 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1306
1307 ax->d.type = AUDIT_SOCKETCALL;
1308 ax->d.next = context->aux;
1309 context->aux = (void *)ax;
1310 return 0;
1311}
1312
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001313/**
1314 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1315 * @len: data length in user space
1316 * @a: data address in kernel space
1317 *
1318 * Returns 0 for success or NULL context or < 0 on error.
1319 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001320int audit_sockaddr(int len, void *a)
1321{
1322 struct audit_aux_data_sockaddr *ax;
1323 struct audit_context *context = current->audit_context;
1324
1325 if (likely(!context))
1326 return 0;
1327
1328 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1329 if (!ax)
1330 return -ENOMEM;
1331
1332 ax->len = len;
1333 memcpy(ax->a, a, len);
1334
1335 ax->d.type = AUDIT_SOCKADDR;
1336 ax->d.next = context->aux;
1337 context->aux = (void *)ax;
1338 return 0;
1339}
1340
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001341/**
1342 * audit_avc_path - record the granting or denial of permissions
1343 * @dentry: dentry to record
1344 * @mnt: mnt to record
1345 *
1346 * Returns 0 for success or NULL context or < 0 on error.
1347 *
1348 * Called from security/selinux/avc.c::avc_audit()
1349 */
Stephen Smalley01116102005-05-21 00:15:52 +01001350int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1351{
1352 struct audit_aux_data_path *ax;
1353 struct audit_context *context = current->audit_context;
1354
1355 if (likely(!context))
1356 return 0;
1357
1358 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1359 if (!ax)
1360 return -ENOMEM;
1361
1362 ax->dentry = dget(dentry);
1363 ax->mnt = mntget(mnt);
1364
1365 ax->d.type = AUDIT_AVC_PATH;
1366 ax->d.next = context->aux;
1367 context->aux = (void *)ax;
1368 return 0;
1369}
1370
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001371/**
1372 * audit_signal_info - record signal info for shutting down audit subsystem
1373 * @sig: signal value
1374 * @t: task being signaled
1375 *
1376 * If the audit subsystem is being terminated, record the task (pid)
1377 * and uid that is doing that.
1378 */
Al Viroe1396062006-05-25 10:19:47 -04001379void __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001380{
1381 extern pid_t audit_sig_pid;
1382 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04001383 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001384
Al Viroe1396062006-05-25 10:19:47 -04001385 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1386 struct task_struct *tsk = current;
1387 struct audit_context *ctx = tsk->audit_context;
1388 audit_sig_pid = tsk->pid;
1389 if (ctx)
1390 audit_sig_uid = ctx->loginuid;
1391 else
1392 audit_sig_uid = tsk->uid;
1393 selinux_get_task_sid(tsk, &audit_sig_sid);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001394 }
1395}