blob: ba087885477786ab1a076cb7202e9fb62d485d5c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
David Woodhousefe7752b2005-12-15 18:33:52 +000061#include "audit.h"
62
63extern struct list_head audit_filter_list[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* No syscall auditing will take place unless audit_enabled != 0. */
66extern int audit_enabled;
67
68/* AUDIT_NAMES is the number of slots we reserve in the audit_context
69 * for saving names from getname(). */
70#define AUDIT_NAMES 20
71
72/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
73 * audit_context from being used for nameless inodes from
74 * path_lookup. */
75#define AUDIT_NAMES_RESERVED 7
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* When fs/namei.c:getname() is called, we store the pointer in name and
78 * we don't let putname() free it (instead we free all of the saved
79 * pointers at syscall exit time).
80 *
81 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
82struct audit_names {
83 const char *name;
84 unsigned long ino;
Amy Griffis73241cc2005-11-03 16:00:25 +000085 unsigned long pino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dev_t dev;
87 umode_t mode;
88 uid_t uid;
89 gid_t gid;
90 dev_t rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000091 char *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94struct audit_aux_data {
95 struct audit_aux_data *next;
96 int type;
97};
98
99#define AUDIT_AUX_IPCPERM 0
100
101struct audit_aux_data_ipcctl {
102 struct audit_aux_data d;
103 struct ipc_perm p;
104 unsigned long qbytes;
105 uid_t uid;
106 gid_t gid;
107 mode_t mode;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000108 char *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100111struct audit_aux_data_socketcall {
112 struct audit_aux_data d;
113 int nargs;
114 unsigned long args[0];
115};
116
117struct audit_aux_data_sockaddr {
118 struct audit_aux_data d;
119 int len;
120 char a[0];
121};
122
Stephen Smalley01116102005-05-21 00:15:52 +0100123struct audit_aux_data_path {
124 struct audit_aux_data d;
125 struct dentry *dentry;
126 struct vfsmount *mnt;
127};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/* The per-task audit context. */
130struct audit_context {
131 int in_syscall; /* 1 if task is in a syscall */
132 enum audit_state state;
133 unsigned int serial; /* serial number for record */
134 struct timespec ctime; /* time of syscall entry */
135 uid_t loginuid; /* login uid (identity) */
136 int major; /* syscall number */
137 unsigned long argv[4]; /* syscall arguments */
138 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100139 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int auditable; /* 1 if record should be written */
141 int name_count;
142 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100143 struct dentry * pwd;
144 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct audit_context *previous; /* For nested syscalls */
146 struct audit_aux_data *aux;
147
148 /* Save things to print about task_struct */
149 pid_t pid;
150 uid_t uid, euid, suid, fsuid;
151 gid_t gid, egid, sgid, fsgid;
152 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100153 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155#if AUDIT_DEBUG
156 int put_count;
157 int ino_count;
158#endif
159};
160
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* Compare a task_struct with an audit_rule. Return 1 on match, 0
163 * otherwise. */
164static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500165 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct audit_context *ctx,
167 enum audit_state *state)
168{
169 int i, j;
170
171 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500172 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 int result = 0;
174
Amy Griffis93315ed2006-02-07 12:05:27 -0500175 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500177 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500180 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500183 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500186 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 break;
188 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500189 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 break;
191 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500192 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 break;
194 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500195 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500198 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 break;
200 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500201 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 break;
203 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500204 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
2fd6f582005-04-29 16:08:28 +0100206 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000207 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500208 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 case AUDIT_EXIT:
212 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500213 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 break;
215 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100216 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500217 if (f->val)
218 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100219 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500220 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 break;
223 case AUDIT_DEVMAJOR:
224 if (ctx) {
225 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500226 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ++result;
228 break;
229 }
230 }
231 }
232 break;
233 case AUDIT_DEVMINOR:
234 if (ctx) {
235 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500236 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 ++result;
238 break;
239 }
240 }
241 }
242 break;
243 case AUDIT_INODE:
244 if (ctx) {
245 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500246 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
247 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ++result;
249 break;
250 }
251 }
252 }
253 break;
254 case AUDIT_LOGINUID:
255 result = 0;
256 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500257 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
259 case AUDIT_ARG0:
260 case AUDIT_ARG1:
261 case AUDIT_ARG2:
262 case AUDIT_ARG3:
263 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500264 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 break;
266 }
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!result)
269 return 0;
270 }
271 switch (rule->action) {
272 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
273 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
274 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
275 }
276 return 1;
277}
278
279/* At process creation time, we can determine if system-call auditing is
280 * completely disabled for this task. Since we only have the task
281 * structure at this point, we can only check uid and gid.
282 */
283static enum audit_state audit_filter_task(struct task_struct *tsk)
284{
285 struct audit_entry *e;
286 enum audit_state state;
287
288 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100289 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
291 rcu_read_unlock();
292 return state;
293 }
294 }
295 rcu_read_unlock();
296 return AUDIT_BUILD_CONTEXT;
297}
298
299/* At syscall entry and exit time, this filter is called if the
300 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100301 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700302 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304static enum audit_state audit_filter_syscall(struct task_struct *tsk,
305 struct audit_context *ctx,
306 struct list_head *list)
307{
308 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100309 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
David Woodhouse351bb722005-07-14 14:40:06 +0100311 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100312 return AUDIT_DISABLED;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100315 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000316 int word = AUDIT_WORD(ctx->major);
317 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100318
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000319 list_for_each_entry_rcu(e, list, list) {
320 if ((e->rule.mask[word] & bit) == bit
321 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
322 rcu_read_unlock();
323 return state;
324 }
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 rcu_read_unlock();
328 return AUDIT_BUILD_CONTEXT;
329}
330
331/* This should be called with task_lock() held. */
332static inline struct audit_context *audit_get_context(struct task_struct *tsk,
333 int return_valid,
334 int return_code)
335{
336 struct audit_context *context = tsk->audit_context;
337
338 if (likely(!context))
339 return NULL;
340 context->return_valid = return_valid;
341 context->return_code = return_code;
342
David Woodhouse21af6c42005-07-02 14:10:46 +0100343 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100345 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (state == AUDIT_RECORD_CONTEXT)
347 context->auditable = 1;
348 }
349
350 context->pid = tsk->pid;
351 context->uid = tsk->uid;
352 context->gid = tsk->gid;
353 context->euid = tsk->euid;
354 context->suid = tsk->suid;
355 context->fsuid = tsk->fsuid;
356 context->egid = tsk->egid;
357 context->sgid = tsk->sgid;
358 context->fsgid = tsk->fsgid;
359 context->personality = tsk->personality;
360 tsk->audit_context = NULL;
361 return context;
362}
363
364static inline void audit_free_names(struct audit_context *context)
365{
366 int i;
367
368#if AUDIT_DEBUG == 2
369 if (context->auditable
370 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000371 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 " name_count=%d put_count=%d"
373 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000374 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 context->serial, context->major, context->in_syscall,
376 context->name_count, context->put_count,
377 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000378 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 printk(KERN_ERR "names[%d] = %p = %s\n", i,
380 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000381 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dump_stack();
384 return;
385 }
386#endif
387#if AUDIT_DEBUG
388 context->put_count = 0;
389 context->ino_count = 0;
390#endif
391
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000392 for (i = 0; i < context->name_count; i++) {
393 char *p = context->names[i].ctx;
394 context->names[i].ctx = NULL;
395 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (context->names[i].name)
397 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100400 if (context->pwd)
401 dput(context->pwd);
402 if (context->pwdmnt)
403 mntput(context->pwdmnt);
404 context->pwd = NULL;
405 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408static inline void audit_free_aux(struct audit_context *context)
409{
410 struct audit_aux_data *aux;
411
412 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100413 if (aux->type == AUDIT_AVC_PATH) {
414 struct audit_aux_data_path *axi = (void *)aux;
415 dput(axi->dentry);
416 mntput(axi->mnt);
417 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000418 if ( aux->type == AUDIT_IPC ) {
419 struct audit_aux_data_ipcctl *axi = (void *)aux;
420 if (axi->ctx)
421 kfree(axi->ctx);
422 }
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 context->aux = aux->next;
425 kfree(aux);
426 }
427}
428
429static inline void audit_zero_context(struct audit_context *context,
430 enum audit_state state)
431{
432 uid_t loginuid = context->loginuid;
433
434 memset(context, 0, sizeof(*context));
435 context->state = state;
436 context->loginuid = loginuid;
437}
438
439static inline struct audit_context *audit_alloc_context(enum audit_state state)
440{
441 struct audit_context *context;
442
443 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
444 return NULL;
445 audit_zero_context(context, state);
446 return context;
447}
448
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700449/**
450 * audit_alloc - allocate an audit context block for a task
451 * @tsk: task
452 *
453 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * if necessary. Doing so turns on system call auditing for the
455 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700456 * needed.
457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458int audit_alloc(struct task_struct *tsk)
459{
460 struct audit_context *context;
461 enum audit_state state;
462
463 if (likely(!audit_enabled))
464 return 0; /* Return if not auditing. */
465
466 state = audit_filter_task(tsk);
467 if (likely(state == AUDIT_DISABLED))
468 return 0;
469
470 if (!(context = audit_alloc_context(state))) {
471 audit_log_lost("out of memory in audit_alloc");
472 return -ENOMEM;
473 }
474
475 /* Preserve login uid */
476 context->loginuid = -1;
477 if (current->audit_context)
478 context->loginuid = current->audit_context->loginuid;
479
480 tsk->audit_context = context;
481 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
482 return 0;
483}
484
485static inline void audit_free_context(struct audit_context *context)
486{
487 struct audit_context *previous;
488 int count = 0;
489
490 do {
491 previous = context->previous;
492 if (previous || (count && count < 10)) {
493 ++count;
494 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
495 " freeing multiple contexts (%d)\n",
496 context->serial, context->major,
497 context->name_count, count);
498 }
499 audit_free_names(context);
500 audit_free_aux(context);
501 kfree(context);
502 context = previous;
503 } while (context);
504 if (count >= 10)
505 printk(KERN_ERR "audit: freed %d contexts\n", count);
506}
507
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000508static void audit_log_task_context(struct audit_buffer *ab, gfp_t gfp_mask)
509{
510 char *ctx = NULL;
511 ssize_t len = 0;
512
513 len = security_getprocattr(current, "current", NULL, 0);
514 if (len < 0) {
515 if (len != -EINVAL)
516 goto error_path;
517 return;
518 }
519
520 ctx = kmalloc(len, gfp_mask);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000521 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000522 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000523
524 len = security_getprocattr(current, "current", ctx, len);
525 if (len < 0 )
526 goto error_path;
527
528 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000529 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000530
531error_path:
532 if (ctx)
533 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000534 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000535 return;
536}
537
538static void audit_log_task_info(struct audit_buffer *ab, gfp_t gfp_mask)
Stephen Smalley219f0812005-04-18 10:47:35 -0700539{
540 char name[sizeof(current->comm)];
541 struct mm_struct *mm = current->mm;
542 struct vm_area_struct *vma;
543
544 get_task_comm(name, current);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100545 audit_log_format(ab, " comm=");
546 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700547
548 if (!mm)
549 return;
550
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000551 /*
552 * this is brittle; all callers that pass GFP_ATOMIC will have
553 * NULL current->mm and we won't get here.
554 */
Stephen Smalley219f0812005-04-18 10:47:35 -0700555 down_read(&mm->mmap_sem);
556 vma = mm->mmap;
557 while (vma) {
558 if ((vma->vm_flags & VM_EXECUTABLE) &&
559 vma->vm_file) {
560 audit_log_d_path(ab, "exe=",
561 vma->vm_file->f_dentry,
562 vma->vm_file->f_vfsmnt);
563 break;
564 }
565 vma = vma->vm_next;
566 }
567 up_read(&mm->mmap_sem);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000568 audit_log_task_context(ab, gfp_mask);
Stephen Smalley219f0812005-04-18 10:47:35 -0700569}
570
Al Viro9796fdd2005-10-21 03:22:03 -0400571static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 int i;
574 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100575 struct audit_aux_data *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
David Woodhousef5561962005-07-13 22:47:07 +0100577 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!ab)
579 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100580 audit_log_format(ab, "arch=%x syscall=%d",
581 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (context->personality != PER_LINUX)
583 audit_log_format(ab, " per=%lx", context->personality);
584 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100585 audit_log_format(ab, " success=%s exit=%ld",
586 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
587 context->return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 audit_log_format(ab,
589 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100590 " pid=%d auid=%u uid=%u gid=%u"
591 " euid=%u suid=%u fsuid=%u"
592 " egid=%u sgid=%u fsgid=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 context->argv[0],
594 context->argv[1],
595 context->argv[2],
596 context->argv[3],
597 context->name_count,
598 context->pid,
599 context->loginuid,
600 context->uid,
601 context->gid,
602 context->euid, context->suid, context->fsuid,
603 context->egid, context->sgid, context->fsgid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000604 audit_log_task_info(ab, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
David Woodhouse7551ced2005-05-26 12:04:57 +0100607 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100608
Al Viroef20c8c2006-02-18 15:41:50 -0500609 ab = audit_log_start(context, gfp_mask, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (!ab)
611 continue; /* audit_panic has been called */
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100614 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 struct audit_aux_data_ipcctl *axi = (void *)aux;
616 audit_log_format(ab,
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000617 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
618 axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100619 break; }
620
621 case AUDIT_SOCKETCALL: {
622 int i;
623 struct audit_aux_data_socketcall *axs = (void *)aux;
624 audit_log_format(ab, "nargs=%d", axs->nargs);
625 for (i=0; i<axs->nargs; i++)
626 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
627 break; }
628
629 case AUDIT_SOCKADDR: {
630 struct audit_aux_data_sockaddr *axs = (void *)aux;
631
632 audit_log_format(ab, "saddr=");
633 audit_log_hex(ab, axs->a, axs->len);
634 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100635
636 case AUDIT_AVC_PATH: {
637 struct audit_aux_data_path *axi = (void *)aux;
638 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100639 break; }
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
David Woodhouse8f37d472005-05-27 12:17:28 +0100645 if (context->pwd && context->pwdmnt) {
Al Viroef20c8c2006-02-18 15:41:50 -0500646 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100647 if (ab) {
648 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
649 audit_log_end(ab);
650 }
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000653 unsigned long ino = context->names[i].ino;
654 unsigned long pino = context->names[i].pino;
655
Al Viroef20c8c2006-02-18 15:41:50 -0500656 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!ab)
658 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000661
662 audit_log_format(ab, " name=");
663 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100664 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000665 else
666 audit_log_format(ab, "(null)");
667
668 if (pino != (unsigned long)-1)
669 audit_log_format(ab, " parent=%lu", pino);
670 if (ino != (unsigned long)-1)
671 audit_log_format(ab, " inode=%lu", ino);
672 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
673 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
674 " ouid=%u ogid=%u rdev=%02x:%02x",
675 MAJOR(context->names[i].dev),
676 MINOR(context->names[i].dev),
677 context->names[i].mode,
678 context->names[i].uid,
679 context->names[i].gid,
680 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 MINOR(context->names[i].rdev));
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000682 if (context->names[i].ctx) {
683 audit_log_format(ab, " obj=%s",
684 context->names[i].ctx);
685 }
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 audit_log_end(ab);
688 }
689}
690
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700691/**
692 * audit_free - free a per-task audit context
693 * @tsk: task whose audit context block to free
694 *
695 * Called from copy_process and __put_task_struct.
696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697void audit_free(struct task_struct *tsk)
698{
699 struct audit_context *context;
700
701 task_lock(tsk);
702 context = audit_get_context(tsk, 0, 0);
703 task_unlock(tsk);
704
705 if (likely(!context))
706 return;
707
708 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100709 * function (e.g., exit_group), then free context block.
710 * We use GFP_ATOMIC here because we might be doing this
711 * in the context of the idle thread */
David Woodhousef7056d62005-06-20 16:07:33 +0100712 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +0100713 audit_log_exit(context, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 audit_free_context(context);
716}
717
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700718/**
719 * audit_syscall_entry - fill in an audit record at syscall entry
720 * @tsk: task being audited
721 * @arch: architecture type
722 * @major: major syscall type (function)
723 * @a1: additional syscall register 1
724 * @a2: additional syscall register 2
725 * @a3: additional syscall register 3
726 * @a4: additional syscall register 4
727 *
728 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * audit context was created when the task was created and the state or
730 * filters demand the audit context be built. If the state from the
731 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
732 * then the record will be written at syscall exit time (otherwise, it
733 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700734 * be written).
735 */
2fd6f582005-04-29 16:08:28 +0100736void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 unsigned long a1, unsigned long a2,
738 unsigned long a3, unsigned long a4)
739{
740 struct audit_context *context = tsk->audit_context;
741 enum audit_state state;
742
743 BUG_ON(!context);
744
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700745 /*
746 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * calls in kernel_thread via the entry.S interface, instead of
748 * with direct calls. (If you are porting to a new
749 * architecture, hitting this condition can indicate that you
750 * got the _exit/_leave calls backward in entry.S.)
751 *
752 * i386 no
753 * x86_64 no
754 * ppc64 yes (see arch/ppc64/kernel/misc.S)
755 *
756 * This also happens with vm86 emulation in a non-nested manner
757 * (entries without exits), so this case must be caught.
758 */
759 if (context->in_syscall) {
760 struct audit_context *newctx;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762#if AUDIT_DEBUG
763 printk(KERN_ERR
764 "audit(:%d) pid=%d in syscall=%d;"
765 " entering syscall=%d\n",
766 context->serial, tsk->pid, context->major, major);
767#endif
768 newctx = audit_alloc_context(context->state);
769 if (newctx) {
770 newctx->previous = context;
771 context = newctx;
772 tsk->audit_context = newctx;
773 } else {
774 /* If we can't alloc a new context, the best we
775 * can do is to leak memory (any pending putname
776 * will be lost). The only other alternative is
777 * to abandon auditing. */
778 audit_zero_context(context, context->state);
779 }
780 }
781 BUG_ON(context->in_syscall || context->name_count);
782
783 if (!audit_enabled)
784 return;
785
2fd6f582005-04-29 16:08:28 +0100786 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 context->major = major;
788 context->argv[0] = a1;
789 context->argv[1] = a2;
790 context->argv[2] = a3;
791 context->argv[3] = a4;
792
793 state = context->state;
794 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100795 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (likely(state == AUDIT_DISABLED))
797 return;
798
David Woodhousece625a82005-07-18 14:24:46 -0400799 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 context->ctime = CURRENT_TIME;
801 context->in_syscall = 1;
802 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
803}
804
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700805/**
806 * audit_syscall_exit - deallocate audit context after a system call
807 * @tsk: task being audited
808 * @valid: success/failure flag
809 * @return_code: syscall return value
810 *
811 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
813 * filtering, or because some other part of the kernel write an audit
814 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700815 * free the names stored from getname().
816 */
2fd6f582005-04-29 16:08:28 +0100817void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct audit_context *context;
820
821 get_task_struct(tsk);
822 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +0100823 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 task_unlock(tsk);
825
826 /* Not having a context here is ok, since the parent may have
827 * called __put_task_struct. */
828 if (likely(!context))
David Woodhouse413a1c72005-08-17 14:45:55 +0100829 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
David Woodhousef7056d62005-06-20 16:07:33 +0100831 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +0100832 audit_log_exit(context, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 context->in_syscall = 0;
835 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (context->previous) {
838 struct audit_context *new_context = context->previous;
839 context->previous = NULL;
840 audit_free_context(context);
841 tsk->audit_context = new_context;
842 } else {
843 audit_free_names(context);
844 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 tsk->audit_context = context;
846 }
David Woodhouse413a1c72005-08-17 14:45:55 +0100847 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 put_task_struct(tsk);
849}
850
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700851/**
852 * audit_getname - add a name to the list
853 * @name: name to add
854 *
855 * Add a name to the list of audit names for this context.
856 * Called from fs/namei.c:getname().
857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858void audit_getname(const char *name)
859{
860 struct audit_context *context = current->audit_context;
861
862 if (!context || IS_ERR(name) || !name)
863 return;
864
865 if (!context->in_syscall) {
866#if AUDIT_DEBUG == 2
867 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
868 __FILE__, __LINE__, context->serial, name);
869 dump_stack();
870#endif
871 return;
872 }
873 BUG_ON(context->name_count >= AUDIT_NAMES);
874 context->names[context->name_count].name = name;
875 context->names[context->name_count].ino = (unsigned long)-1;
876 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100877 if (!context->pwd) {
878 read_lock(&current->fs->lock);
879 context->pwd = dget(current->fs->pwd);
880 context->pwdmnt = mntget(current->fs->pwdmnt);
881 read_unlock(&current->fs->lock);
882 }
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700886/* audit_putname - intercept a putname request
887 * @name: name to intercept and delay for putname
888 *
889 * If we have stored the name from getname in the audit context,
890 * then we delay the putname until syscall exit.
891 * Called from include/linux/fs.h:putname().
892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893void audit_putname(const char *name)
894{
895 struct audit_context *context = current->audit_context;
896
897 BUG_ON(!context);
898 if (!context->in_syscall) {
899#if AUDIT_DEBUG == 2
900 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
901 __FILE__, __LINE__, context->serial, name);
902 if (context->name_count) {
903 int i;
904 for (i = 0; i < context->name_count; i++)
905 printk(KERN_ERR "name[%d] = %p = %s\n", i,
906 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000907 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909#endif
910 __putname(name);
911 }
912#if AUDIT_DEBUG
913 else {
914 ++context->put_count;
915 if (context->put_count > context->name_count) {
916 printk(KERN_ERR "%s:%d(:%d): major=%d"
917 " in_syscall=%d putname(%p) name_count=%d"
918 " put_count=%d\n",
919 __FILE__, __LINE__,
920 context->serial, context->major,
921 context->in_syscall, name, context->name_count,
922 context->put_count);
923 dump_stack();
924 }
925 }
926#endif
927}
928
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000929void audit_inode_context(int idx, const struct inode *inode)
930{
931 struct audit_context *context = current->audit_context;
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000932 const char *suffix = security_inode_xattr_getsuffix();
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000933 char *ctx = NULL;
934 int len = 0;
935
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000936 if (!suffix)
937 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000938
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000939 len = security_inode_getsecurity(inode, suffix, NULL, 0, 0);
940 if (len == -EOPNOTSUPP)
941 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000942 if (len < 0)
943 goto error_path;
944
945 ctx = kmalloc(len, GFP_KERNEL);
946 if (!ctx)
947 goto error_path;
948
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000949 len = security_inode_getsecurity(inode, suffix, ctx, len, 0);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000950 if (len < 0)
951 goto error_path;
952
953 kfree(context->names[idx].ctx);
954 context->names[idx].ctx = ctx;
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000955 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000956
957error_path:
958 if (ctx)
959 kfree(ctx);
960 audit_panic("error in audit_inode_context");
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000961ret:
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000962 return;
963}
964
965
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700966/**
967 * audit_inode - store the inode and device from a lookup
968 * @name: name being audited
969 * @inode: inode being audited
970 * @flags: lookup flags (as used in path_lookup())
971 *
972 * Called from fs/namei.c:path_lookup().
973 */
Amy Griffis73241cc2005-11-03 16:00:25 +0000974void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 int idx;
977 struct audit_context *context = current->audit_context;
978
979 if (!context->in_syscall)
980 return;
981 if (context->name_count
982 && context->names[context->name_count-1].name
983 && context->names[context->name_count-1].name == name)
984 idx = context->name_count - 1;
985 else if (context->name_count > 1
986 && context->names[context->name_count-2].name
987 && context->names[context->name_count-2].name == name)
988 idx = context->name_count - 2;
989 else {
990 /* FIXME: how much do we care about inodes that have no
991 * associated name? */
992 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
993 return;
994 idx = context->name_count++;
995 context->names[idx].name = NULL;
996#if AUDIT_DEBUG
997 ++context->ino_count;
998#endif
999 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001000 context->names[idx].dev = inode->i_sb->s_dev;
1001 context->names[idx].mode = inode->i_mode;
1002 context->names[idx].uid = inode->i_uid;
1003 context->names[idx].gid = inode->i_gid;
1004 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001005 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001006 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1007 (strcmp(name, ".") != 0)) {
1008 context->names[idx].ino = (unsigned long)-1;
1009 context->names[idx].pino = inode->i_ino;
1010 } else {
1011 context->names[idx].ino = inode->i_ino;
1012 context->names[idx].pino = (unsigned long)-1;
1013 }
1014}
1015
1016/**
1017 * audit_inode_child - collect inode info for created/removed objects
1018 * @dname: inode's dentry name
1019 * @inode: inode being audited
1020 * @pino: inode number of dentry parent
1021 *
1022 * For syscalls that create or remove filesystem objects, audit_inode
1023 * can only collect information for the filesystem object's parent.
1024 * This call updates the audit context with the child's information.
1025 * Syscalls that create a new filesystem object must be hooked after
1026 * the object is created. Syscalls that remove a filesystem object
1027 * must be hooked prior, in order to capture the target inode during
1028 * unsuccessful attempts.
1029 */
1030void __audit_inode_child(const char *dname, const struct inode *inode,
1031 unsigned long pino)
1032{
1033 int idx;
1034 struct audit_context *context = current->audit_context;
1035
1036 if (!context->in_syscall)
1037 return;
1038
1039 /* determine matching parent */
1040 if (dname)
1041 for (idx = 0; idx < context->name_count; idx++)
1042 if (context->names[idx].pino == pino) {
1043 const char *n;
1044 const char *name = context->names[idx].name;
1045 int dlen = strlen(dname);
1046 int nlen = name ? strlen(name) : 0;
1047
1048 if (nlen < dlen)
1049 continue;
1050
1051 /* disregard trailing slashes */
1052 n = name + nlen - 1;
1053 while ((*n == '/') && (n > name))
1054 n--;
1055
1056 /* find last path component */
1057 n = n - dlen + 1;
1058 if (n < name)
1059 continue;
1060 else if (n > name) {
1061 if (*--n != '/')
1062 continue;
1063 else
1064 n++;
1065 }
1066
1067 if (strncmp(n, dname, dlen) == 0)
1068 goto update_context;
1069 }
1070
1071 /* catch-all in case match not found */
1072 idx = context->name_count++;
1073 context->names[idx].name = NULL;
1074 context->names[idx].pino = pino;
1075#if AUDIT_DEBUG
1076 context->ino_count++;
1077#endif
1078
1079update_context:
1080 if (inode) {
1081 context->names[idx].ino = inode->i_ino;
1082 context->names[idx].dev = inode->i_sb->s_dev;
1083 context->names[idx].mode = inode->i_mode;
1084 context->names[idx].uid = inode->i_uid;
1085 context->names[idx].gid = inode->i_gid;
1086 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001087 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001091/**
1092 * auditsc_get_stamp - get local copies of audit_context values
1093 * @ctx: audit_context for the task
1094 * @t: timespec to store time recorded in the audit_context
1095 * @serial: serial value that is recorded in the audit_context
1096 *
1097 * Also sets the context as auditable.
1098 */
David Woodhousebfb44962005-05-21 21:08:09 +01001099void auditsc_get_stamp(struct audit_context *ctx,
1100 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
David Woodhousece625a82005-07-18 14:24:46 -04001102 if (!ctx->serial)
1103 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001104 t->tv_sec = ctx->ctime.tv_sec;
1105 t->tv_nsec = ctx->ctime.tv_nsec;
1106 *serial = ctx->serial;
1107 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001110/**
1111 * audit_set_loginuid - set a task's audit_context loginuid
1112 * @task: task whose audit context is being modified
1113 * @loginuid: loginuid value
1114 *
1115 * Returns 0.
1116 *
1117 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1118 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001119int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Steve Grubb456be6c2005-04-29 17:30:07 +01001121 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001122 struct audit_buffer *ab;
1123
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001124 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001125 if (ab) {
1126 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001127 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001128 task->pid, task->uid,
1129 task->audit_context->loginuid, loginuid);
1130 audit_log_end(ab);
1131 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001132 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 return 0;
1135}
1136
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001137/**
1138 * audit_get_loginuid - get the loginuid for an audit_context
1139 * @ctx: the audit_context
1140 *
1141 * Returns the context's loginuid or -1 if @ctx is NULL.
1142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143uid_t audit_get_loginuid(struct audit_context *ctx)
1144{
1145 return ctx ? ctx->loginuid : -1;
1146}
1147
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001148static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
1149{
1150 struct audit_context *context = current->audit_context;
1151 char *ctx = NULL;
1152 int len = 0;
1153
1154 if (likely(!context))
1155 return NULL;
1156
1157 len = security_ipc_getsecurity(ipcp, NULL, 0);
1158 if (len == -EOPNOTSUPP)
1159 goto ret;
1160 if (len < 0)
1161 goto error_path;
1162
1163 ctx = kmalloc(len, GFP_ATOMIC);
1164 if (!ctx)
1165 goto error_path;
1166
1167 len = security_ipc_getsecurity(ipcp, ctx, len);
1168 if (len < 0)
1169 goto error_path;
1170
1171 return ctx;
1172
1173error_path:
1174 kfree(ctx);
1175 audit_panic("error in audit_ipc_context");
1176ret:
1177 return NULL;
1178}
1179
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001180/**
1181 * audit_ipc_perms - record audit data for ipc
1182 * @qbytes: msgq bytes
1183 * @uid: msgq user id
1184 * @gid: msgq group id
1185 * @mode: msgq mode (permissions)
1186 *
1187 * Returns 0 for success or NULL context or < 0 on error.
1188 */
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001189int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct audit_aux_data_ipcctl *ax;
1192 struct audit_context *context = current->audit_context;
1193
1194 if (likely(!context))
1195 return 0;
1196
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001197 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (!ax)
1199 return -ENOMEM;
1200
1201 ax->qbytes = qbytes;
1202 ax->uid = uid;
1203 ax->gid = gid;
1204 ax->mode = mode;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001205 ax->ctx = audit_ipc_context(ipcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Steve Grubbc0404992005-05-13 18:17:42 +01001207 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 ax->d.next = context->aux;
1209 context->aux = (void *)ax;
1210 return 0;
1211}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001212
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001213/**
1214 * audit_socketcall - record audit data for sys_socketcall
1215 * @nargs: number of args
1216 * @args: args array
1217 *
1218 * Returns 0 for success or NULL context or < 0 on error.
1219 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001220int audit_socketcall(int nargs, unsigned long *args)
1221{
1222 struct audit_aux_data_socketcall *ax;
1223 struct audit_context *context = current->audit_context;
1224
1225 if (likely(!context))
1226 return 0;
1227
1228 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1229 if (!ax)
1230 return -ENOMEM;
1231
1232 ax->nargs = nargs;
1233 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1234
1235 ax->d.type = AUDIT_SOCKETCALL;
1236 ax->d.next = context->aux;
1237 context->aux = (void *)ax;
1238 return 0;
1239}
1240
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001241/**
1242 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1243 * @len: data length in user space
1244 * @a: data address in kernel space
1245 *
1246 * Returns 0 for success or NULL context or < 0 on error.
1247 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001248int audit_sockaddr(int len, void *a)
1249{
1250 struct audit_aux_data_sockaddr *ax;
1251 struct audit_context *context = current->audit_context;
1252
1253 if (likely(!context))
1254 return 0;
1255
1256 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1257 if (!ax)
1258 return -ENOMEM;
1259
1260 ax->len = len;
1261 memcpy(ax->a, a, len);
1262
1263 ax->d.type = AUDIT_SOCKADDR;
1264 ax->d.next = context->aux;
1265 context->aux = (void *)ax;
1266 return 0;
1267}
1268
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001269/**
1270 * audit_avc_path - record the granting or denial of permissions
1271 * @dentry: dentry to record
1272 * @mnt: mnt to record
1273 *
1274 * Returns 0 for success or NULL context or < 0 on error.
1275 *
1276 * Called from security/selinux/avc.c::avc_audit()
1277 */
Stephen Smalley01116102005-05-21 00:15:52 +01001278int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1279{
1280 struct audit_aux_data_path *ax;
1281 struct audit_context *context = current->audit_context;
1282
1283 if (likely(!context))
1284 return 0;
1285
1286 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1287 if (!ax)
1288 return -ENOMEM;
1289
1290 ax->dentry = dget(dentry);
1291 ax->mnt = mntget(mnt);
1292
1293 ax->d.type = AUDIT_AVC_PATH;
1294 ax->d.next = context->aux;
1295 context->aux = (void *)ax;
1296 return 0;
1297}
1298
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001299/**
1300 * audit_signal_info - record signal info for shutting down audit subsystem
1301 * @sig: signal value
1302 * @t: task being signaled
1303 *
1304 * If the audit subsystem is being terminated, record the task (pid)
1305 * and uid that is doing that.
1306 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001307void audit_signal_info(int sig, struct task_struct *t)
1308{
1309 extern pid_t audit_sig_pid;
1310 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001311
David Woodhouse582edda2005-07-13 22:39:34 +01001312 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001313 if (sig == SIGTERM || sig == SIGHUP) {
1314 struct audit_context *ctx = current->audit_context;
1315 audit_sig_pid = current->pid;
1316 if (ctx)
1317 audit_sig_uid = ctx->loginuid;
1318 else
1319 audit_sig_uid = current->uid;
1320 }
1321 }
1322}