blob: 17719b30363870fe9b50f3d44f455fd073b6514b [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,
165 struct audit_rule *rule,
166 struct audit_context *ctx,
167 enum audit_state *state)
168{
169 int i, j;
170
171 for (i = 0; i < rule->field_count; i++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000172 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
173 u32 op = rule->fields[i] & AUDIT_OPERATORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 u32 value = rule->values[i];
175 int result = 0;
176
177 switch (field) {
178 case AUDIT_PID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000179 result = audit_comparator(tsk->pid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
181 case AUDIT_UID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000182 result = audit_comparator(tsk->uid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
184 case AUDIT_EUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000185 result = audit_comparator(tsk->euid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187 case AUDIT_SUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000188 result = audit_comparator(tsk->suid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190 case AUDIT_FSUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000191 result = audit_comparator(tsk->fsuid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193 case AUDIT_GID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000194 result = audit_comparator(tsk->gid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 break;
196 case AUDIT_EGID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000197 result = audit_comparator(tsk->egid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 case AUDIT_SGID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000200 result = audit_comparator(tsk->sgid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202 case AUDIT_FSGID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000203 result = audit_comparator(tsk->fsgid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205 case AUDIT_PERS:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000206 result = audit_comparator(tsk->personality, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
2fd6f582005-04-29 16:08:28 +0100208 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000209 if (ctx)
210 result = audit_comparator(ctx->arch, op, value);
2fd6f582005-04-29 16:08:28 +0100211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 case AUDIT_EXIT:
214 if (ctx && ctx->return_valid)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000215 result = audit_comparator(ctx->return_code, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 break;
217 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100218 if (ctx && ctx->return_valid) {
219 if (value)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000220 result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100221 else
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000222 result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
225 case AUDIT_DEVMAJOR:
226 if (ctx) {
227 for (j = 0; j < ctx->name_count; j++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000228 if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 ++result;
230 break;
231 }
232 }
233 }
234 break;
235 case AUDIT_DEVMINOR:
236 if (ctx) {
237 for (j = 0; j < ctx->name_count; j++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000238 if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 ++result;
240 break;
241 }
242 }
243 }
244 break;
245 case AUDIT_INODE:
246 if (ctx) {
247 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000248 if (audit_comparator(ctx->names[j].ino, op, value) ||
249 audit_comparator(ctx->names[j].pino, op, value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ++result;
251 break;
252 }
253 }
254 }
255 break;
256 case AUDIT_LOGINUID:
257 result = 0;
258 if (ctx)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000259 result = audit_comparator(ctx->loginuid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 break;
261 case AUDIT_ARG0:
262 case AUDIT_ARG1:
263 case AUDIT_ARG2:
264 case AUDIT_ARG3:
265 if (ctx)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000266 result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
268 }
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (!result)
271 return 0;
272 }
273 switch (rule->action) {
274 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
275 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
276 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
277 }
278 return 1;
279}
280
281/* At process creation time, we can determine if system-call auditing is
282 * completely disabled for this task. Since we only have the task
283 * structure at this point, we can only check uid and gid.
284 */
285static enum audit_state audit_filter_task(struct task_struct *tsk)
286{
287 struct audit_entry *e;
288 enum audit_state state;
289
290 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100291 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
293 rcu_read_unlock();
294 return state;
295 }
296 }
297 rcu_read_unlock();
298 return AUDIT_BUILD_CONTEXT;
299}
300
301/* At syscall entry and exit time, this filter is called if the
302 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100303 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700304 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
306static enum audit_state audit_filter_syscall(struct task_struct *tsk,
307 struct audit_context *ctx,
308 struct list_head *list)
309{
310 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100311 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
David Woodhouse351bb722005-07-14 14:40:06 +0100313 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100314 return AUDIT_DISABLED;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100317 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000318 int word = AUDIT_WORD(ctx->major);
319 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100320
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000321 list_for_each_entry_rcu(e, list, list) {
322 if ((e->rule.mask[word] & bit) == bit
323 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
324 rcu_read_unlock();
325 return state;
326 }
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 rcu_read_unlock();
330 return AUDIT_BUILD_CONTEXT;
331}
332
333/* This should be called with task_lock() held. */
334static inline struct audit_context *audit_get_context(struct task_struct *tsk,
335 int return_valid,
336 int return_code)
337{
338 struct audit_context *context = tsk->audit_context;
339
340 if (likely(!context))
341 return NULL;
342 context->return_valid = return_valid;
343 context->return_code = return_code;
344
David Woodhouse21af6c42005-07-02 14:10:46 +0100345 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100347 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (state == AUDIT_RECORD_CONTEXT)
349 context->auditable = 1;
350 }
351
352 context->pid = tsk->pid;
353 context->uid = tsk->uid;
354 context->gid = tsk->gid;
355 context->euid = tsk->euid;
356 context->suid = tsk->suid;
357 context->fsuid = tsk->fsuid;
358 context->egid = tsk->egid;
359 context->sgid = tsk->sgid;
360 context->fsgid = tsk->fsgid;
361 context->personality = tsk->personality;
362 tsk->audit_context = NULL;
363 return context;
364}
365
366static inline void audit_free_names(struct audit_context *context)
367{
368 int i;
369
370#if AUDIT_DEBUG == 2
371 if (context->auditable
372 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000373 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 " name_count=%d put_count=%d"
375 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000376 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 context->serial, context->major, context->in_syscall,
378 context->name_count, context->put_count,
379 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000380 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 printk(KERN_ERR "names[%d] = %p = %s\n", i,
382 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000383 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dump_stack();
386 return;
387 }
388#endif
389#if AUDIT_DEBUG
390 context->put_count = 0;
391 context->ino_count = 0;
392#endif
393
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000394 for (i = 0; i < context->name_count; i++) {
395 char *p = context->names[i].ctx;
396 context->names[i].ctx = NULL;
397 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (context->names[i].name)
399 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100402 if (context->pwd)
403 dput(context->pwd);
404 if (context->pwdmnt)
405 mntput(context->pwdmnt);
406 context->pwd = NULL;
407 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410static inline void audit_free_aux(struct audit_context *context)
411{
412 struct audit_aux_data *aux;
413
414 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100415 if (aux->type == AUDIT_AVC_PATH) {
416 struct audit_aux_data_path *axi = (void *)aux;
417 dput(axi->dentry);
418 mntput(axi->mnt);
419 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000420 if ( aux->type == AUDIT_IPC ) {
421 struct audit_aux_data_ipcctl *axi = (void *)aux;
422 if (axi->ctx)
423 kfree(axi->ctx);
424 }
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 context->aux = aux->next;
427 kfree(aux);
428 }
429}
430
431static inline void audit_zero_context(struct audit_context *context,
432 enum audit_state state)
433{
434 uid_t loginuid = context->loginuid;
435
436 memset(context, 0, sizeof(*context));
437 context->state = state;
438 context->loginuid = loginuid;
439}
440
441static inline struct audit_context *audit_alloc_context(enum audit_state state)
442{
443 struct audit_context *context;
444
445 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
446 return NULL;
447 audit_zero_context(context, state);
448 return context;
449}
450
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700451/**
452 * audit_alloc - allocate an audit context block for a task
453 * @tsk: task
454 *
455 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * if necessary. Doing so turns on system call auditing for the
457 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700458 * needed.
459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460int audit_alloc(struct task_struct *tsk)
461{
462 struct audit_context *context;
463 enum audit_state state;
464
465 if (likely(!audit_enabled))
466 return 0; /* Return if not auditing. */
467
468 state = audit_filter_task(tsk);
469 if (likely(state == AUDIT_DISABLED))
470 return 0;
471
472 if (!(context = audit_alloc_context(state))) {
473 audit_log_lost("out of memory in audit_alloc");
474 return -ENOMEM;
475 }
476
477 /* Preserve login uid */
478 context->loginuid = -1;
479 if (current->audit_context)
480 context->loginuid = current->audit_context->loginuid;
481
482 tsk->audit_context = context;
483 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
484 return 0;
485}
486
487static inline void audit_free_context(struct audit_context *context)
488{
489 struct audit_context *previous;
490 int count = 0;
491
492 do {
493 previous = context->previous;
494 if (previous || (count && count < 10)) {
495 ++count;
496 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
497 " freeing multiple contexts (%d)\n",
498 context->serial, context->major,
499 context->name_count, count);
500 }
501 audit_free_names(context);
502 audit_free_aux(context);
503 kfree(context);
504 context = previous;
505 } while (context);
506 if (count >= 10)
507 printk(KERN_ERR "audit: freed %d contexts\n", count);
508}
509
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000510static void audit_log_task_context(struct audit_buffer *ab, gfp_t gfp_mask)
511{
512 char *ctx = NULL;
513 ssize_t len = 0;
514
515 len = security_getprocattr(current, "current", NULL, 0);
516 if (len < 0) {
517 if (len != -EINVAL)
518 goto error_path;
519 return;
520 }
521
522 ctx = kmalloc(len, gfp_mask);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000523 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000524 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000525
526 len = security_getprocattr(current, "current", ctx, len);
527 if (len < 0 )
528 goto error_path;
529
530 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000531 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000532
533error_path:
534 if (ctx)
535 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000536 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000537 return;
538}
539
540static void audit_log_task_info(struct audit_buffer *ab, gfp_t gfp_mask)
Stephen Smalley219f0812005-04-18 10:47:35 -0700541{
542 char name[sizeof(current->comm)];
543 struct mm_struct *mm = current->mm;
544 struct vm_area_struct *vma;
545
546 get_task_comm(name, current);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100547 audit_log_format(ab, " comm=");
548 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700549
550 if (!mm)
551 return;
552
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000553 /*
554 * this is brittle; all callers that pass GFP_ATOMIC will have
555 * NULL current->mm and we won't get here.
556 */
Stephen Smalley219f0812005-04-18 10:47:35 -0700557 down_read(&mm->mmap_sem);
558 vma = mm->mmap;
559 while (vma) {
560 if ((vma->vm_flags & VM_EXECUTABLE) &&
561 vma->vm_file) {
562 audit_log_d_path(ab, "exe=",
563 vma->vm_file->f_dentry,
564 vma->vm_file->f_vfsmnt);
565 break;
566 }
567 vma = vma->vm_next;
568 }
569 up_read(&mm->mmap_sem);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000570 audit_log_task_context(ab, gfp_mask);
Stephen Smalley219f0812005-04-18 10:47:35 -0700571}
572
Al Viro9796fdd2005-10-21 03:22:03 -0400573static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 int i;
576 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100577 struct audit_aux_data *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
David Woodhousef5561962005-07-13 22:47:07 +0100579 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (!ab)
581 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100582 audit_log_format(ab, "arch=%x syscall=%d",
583 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (context->personality != PER_LINUX)
585 audit_log_format(ab, " per=%lx", context->personality);
586 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100587 audit_log_format(ab, " success=%s exit=%ld",
588 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
589 context->return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 audit_log_format(ab,
591 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100592 " pid=%d auid=%u uid=%u gid=%u"
593 " euid=%u suid=%u fsuid=%u"
594 " egid=%u sgid=%u fsgid=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 context->argv[0],
596 context->argv[1],
597 context->argv[2],
598 context->argv[3],
599 context->name_count,
600 context->pid,
601 context->loginuid,
602 context->uid,
603 context->gid,
604 context->euid, context->suid, context->fsuid,
605 context->egid, context->sgid, context->fsgid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000606 audit_log_task_info(ab, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
David Woodhouse7551ced2005-05-26 12:04:57 +0100609 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100610
Al Viroef20c8c2006-02-18 15:41:50 -0500611 ab = audit_log_start(context, gfp_mask, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!ab)
613 continue; /* audit_panic has been called */
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100616 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct audit_aux_data_ipcctl *axi = (void *)aux;
618 audit_log_format(ab,
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000619 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
620 axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100621 break; }
622
623 case AUDIT_SOCKETCALL: {
624 int i;
625 struct audit_aux_data_socketcall *axs = (void *)aux;
626 audit_log_format(ab, "nargs=%d", axs->nargs);
627 for (i=0; i<axs->nargs; i++)
628 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
629 break; }
630
631 case AUDIT_SOCKADDR: {
632 struct audit_aux_data_sockaddr *axs = (void *)aux;
633
634 audit_log_format(ab, "saddr=");
635 audit_log_hex(ab, axs->a, axs->len);
636 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100637
638 case AUDIT_AVC_PATH: {
639 struct audit_aux_data_path *axi = (void *)aux;
640 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100641 break; }
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
David Woodhouse8f37d472005-05-27 12:17:28 +0100647 if (context->pwd && context->pwdmnt) {
Al Viroef20c8c2006-02-18 15:41:50 -0500648 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100649 if (ab) {
650 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
651 audit_log_end(ab);
652 }
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000655 unsigned long ino = context->names[i].ino;
656 unsigned long pino = context->names[i].pino;
657
Al Viroef20c8c2006-02-18 15:41:50 -0500658 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (!ab)
660 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000663
664 audit_log_format(ab, " name=");
665 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100666 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000667 else
668 audit_log_format(ab, "(null)");
669
670 if (pino != (unsigned long)-1)
671 audit_log_format(ab, " parent=%lu", pino);
672 if (ino != (unsigned long)-1)
673 audit_log_format(ab, " inode=%lu", ino);
674 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
675 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
676 " ouid=%u ogid=%u rdev=%02x:%02x",
677 MAJOR(context->names[i].dev),
678 MINOR(context->names[i].dev),
679 context->names[i].mode,
680 context->names[i].uid,
681 context->names[i].gid,
682 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 MINOR(context->names[i].rdev));
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000684 if (context->names[i].ctx) {
685 audit_log_format(ab, " obj=%s",
686 context->names[i].ctx);
687 }
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 audit_log_end(ab);
690 }
691}
692
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700693/**
694 * audit_free - free a per-task audit context
695 * @tsk: task whose audit context block to free
696 *
697 * Called from copy_process and __put_task_struct.
698 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699void audit_free(struct task_struct *tsk)
700{
701 struct audit_context *context;
702
703 task_lock(tsk);
704 context = audit_get_context(tsk, 0, 0);
705 task_unlock(tsk);
706
707 if (likely(!context))
708 return;
709
710 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100711 * function (e.g., exit_group), then free context block.
712 * We use GFP_ATOMIC here because we might be doing this
713 * in the context of the idle thread */
David Woodhousef7056d62005-06-20 16:07:33 +0100714 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +0100715 audit_log_exit(context, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 audit_free_context(context);
718}
719
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700720/**
721 * audit_syscall_entry - fill in an audit record at syscall entry
722 * @tsk: task being audited
723 * @arch: architecture type
724 * @major: major syscall type (function)
725 * @a1: additional syscall register 1
726 * @a2: additional syscall register 2
727 * @a3: additional syscall register 3
728 * @a4: additional syscall register 4
729 *
730 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * audit context was created when the task was created and the state or
732 * filters demand the audit context be built. If the state from the
733 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
734 * then the record will be written at syscall exit time (otherwise, it
735 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700736 * be written).
737 */
2fd6f582005-04-29 16:08:28 +0100738void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 unsigned long a1, unsigned long a2,
740 unsigned long a3, unsigned long a4)
741{
742 struct audit_context *context = tsk->audit_context;
743 enum audit_state state;
744
745 BUG_ON(!context);
746
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700747 /*
748 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * calls in kernel_thread via the entry.S interface, instead of
750 * with direct calls. (If you are porting to a new
751 * architecture, hitting this condition can indicate that you
752 * got the _exit/_leave calls backward in entry.S.)
753 *
754 * i386 no
755 * x86_64 no
756 * ppc64 yes (see arch/ppc64/kernel/misc.S)
757 *
758 * This also happens with vm86 emulation in a non-nested manner
759 * (entries without exits), so this case must be caught.
760 */
761 if (context->in_syscall) {
762 struct audit_context *newctx;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764#if AUDIT_DEBUG
765 printk(KERN_ERR
766 "audit(:%d) pid=%d in syscall=%d;"
767 " entering syscall=%d\n",
768 context->serial, tsk->pid, context->major, major);
769#endif
770 newctx = audit_alloc_context(context->state);
771 if (newctx) {
772 newctx->previous = context;
773 context = newctx;
774 tsk->audit_context = newctx;
775 } else {
776 /* If we can't alloc a new context, the best we
777 * can do is to leak memory (any pending putname
778 * will be lost). The only other alternative is
779 * to abandon auditing. */
780 audit_zero_context(context, context->state);
781 }
782 }
783 BUG_ON(context->in_syscall || context->name_count);
784
785 if (!audit_enabled)
786 return;
787
2fd6f582005-04-29 16:08:28 +0100788 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 context->major = major;
790 context->argv[0] = a1;
791 context->argv[1] = a2;
792 context->argv[2] = a3;
793 context->argv[3] = a4;
794
795 state = context->state;
796 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100797 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (likely(state == AUDIT_DISABLED))
799 return;
800
David Woodhousece625a82005-07-18 14:24:46 -0400801 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 context->ctime = CURRENT_TIME;
803 context->in_syscall = 1;
804 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
805}
806
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700807/**
808 * audit_syscall_exit - deallocate audit context after a system call
809 * @tsk: task being audited
810 * @valid: success/failure flag
811 * @return_code: syscall return value
812 *
813 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
815 * filtering, or because some other part of the kernel write an audit
816 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700817 * free the names stored from getname().
818 */
2fd6f582005-04-29 16:08:28 +0100819void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 struct audit_context *context;
822
823 get_task_struct(tsk);
824 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +0100825 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 task_unlock(tsk);
827
828 /* Not having a context here is ok, since the parent may have
829 * called __put_task_struct. */
830 if (likely(!context))
David Woodhouse413a1c72005-08-17 14:45:55 +0100831 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
David Woodhousef7056d62005-06-20 16:07:33 +0100833 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +0100834 audit_log_exit(context, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 context->in_syscall = 0;
837 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (context->previous) {
840 struct audit_context *new_context = context->previous;
841 context->previous = NULL;
842 audit_free_context(context);
843 tsk->audit_context = new_context;
844 } else {
845 audit_free_names(context);
846 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 tsk->audit_context = context;
848 }
David Woodhouse413a1c72005-08-17 14:45:55 +0100849 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 put_task_struct(tsk);
851}
852
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700853/**
854 * audit_getname - add a name to the list
855 * @name: name to add
856 *
857 * Add a name to the list of audit names for this context.
858 * Called from fs/namei.c:getname().
859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860void audit_getname(const char *name)
861{
862 struct audit_context *context = current->audit_context;
863
864 if (!context || IS_ERR(name) || !name)
865 return;
866
867 if (!context->in_syscall) {
868#if AUDIT_DEBUG == 2
869 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
870 __FILE__, __LINE__, context->serial, name);
871 dump_stack();
872#endif
873 return;
874 }
875 BUG_ON(context->name_count >= AUDIT_NAMES);
876 context->names[context->name_count].name = name;
877 context->names[context->name_count].ino = (unsigned long)-1;
878 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100879 if (!context->pwd) {
880 read_lock(&current->fs->lock);
881 context->pwd = dget(current->fs->pwd);
882 context->pwdmnt = mntget(current->fs->pwdmnt);
883 read_unlock(&current->fs->lock);
884 }
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700888/* audit_putname - intercept a putname request
889 * @name: name to intercept and delay for putname
890 *
891 * If we have stored the name from getname in the audit context,
892 * then we delay the putname until syscall exit.
893 * Called from include/linux/fs.h:putname().
894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895void audit_putname(const char *name)
896{
897 struct audit_context *context = current->audit_context;
898
899 BUG_ON(!context);
900 if (!context->in_syscall) {
901#if AUDIT_DEBUG == 2
902 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
903 __FILE__, __LINE__, context->serial, name);
904 if (context->name_count) {
905 int i;
906 for (i = 0; i < context->name_count; i++)
907 printk(KERN_ERR "name[%d] = %p = %s\n", i,
908 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000909 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911#endif
912 __putname(name);
913 }
914#if AUDIT_DEBUG
915 else {
916 ++context->put_count;
917 if (context->put_count > context->name_count) {
918 printk(KERN_ERR "%s:%d(:%d): major=%d"
919 " in_syscall=%d putname(%p) name_count=%d"
920 " put_count=%d\n",
921 __FILE__, __LINE__,
922 context->serial, context->major,
923 context->in_syscall, name, context->name_count,
924 context->put_count);
925 dump_stack();
926 }
927 }
928#endif
929}
930
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000931void audit_inode_context(int idx, const struct inode *inode)
932{
933 struct audit_context *context = current->audit_context;
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000934 const char *suffix = security_inode_xattr_getsuffix();
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000935 char *ctx = NULL;
936 int len = 0;
937
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000938 if (!suffix)
939 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000940
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000941 len = security_inode_getsecurity(inode, suffix, NULL, 0, 0);
942 if (len == -EOPNOTSUPP)
943 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000944 if (len < 0)
945 goto error_path;
946
947 ctx = kmalloc(len, GFP_KERNEL);
948 if (!ctx)
949 goto error_path;
950
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000951 len = security_inode_getsecurity(inode, suffix, ctx, len, 0);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000952 if (len < 0)
953 goto error_path;
954
955 kfree(context->names[idx].ctx);
956 context->names[idx].ctx = ctx;
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000957 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000958
959error_path:
960 if (ctx)
961 kfree(ctx);
962 audit_panic("error in audit_inode_context");
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000963ret:
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000964 return;
965}
966
967
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700968/**
969 * audit_inode - store the inode and device from a lookup
970 * @name: name being audited
971 * @inode: inode being audited
972 * @flags: lookup flags (as used in path_lookup())
973 *
974 * Called from fs/namei.c:path_lookup().
975 */
Amy Griffis73241cc2005-11-03 16:00:25 +0000976void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 int idx;
979 struct audit_context *context = current->audit_context;
980
981 if (!context->in_syscall)
982 return;
983 if (context->name_count
984 && context->names[context->name_count-1].name
985 && context->names[context->name_count-1].name == name)
986 idx = context->name_count - 1;
987 else if (context->name_count > 1
988 && context->names[context->name_count-2].name
989 && context->names[context->name_count-2].name == name)
990 idx = context->name_count - 2;
991 else {
992 /* FIXME: how much do we care about inodes that have no
993 * associated name? */
994 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
995 return;
996 idx = context->name_count++;
997 context->names[idx].name = NULL;
998#if AUDIT_DEBUG
999 ++context->ino_count;
1000#endif
1001 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001002 context->names[idx].dev = inode->i_sb->s_dev;
1003 context->names[idx].mode = inode->i_mode;
1004 context->names[idx].uid = inode->i_uid;
1005 context->names[idx].gid = inode->i_gid;
1006 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001007 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001008 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1009 (strcmp(name, ".") != 0)) {
1010 context->names[idx].ino = (unsigned long)-1;
1011 context->names[idx].pino = inode->i_ino;
1012 } else {
1013 context->names[idx].ino = inode->i_ino;
1014 context->names[idx].pino = (unsigned long)-1;
1015 }
1016}
1017
1018/**
1019 * audit_inode_child - collect inode info for created/removed objects
1020 * @dname: inode's dentry name
1021 * @inode: inode being audited
1022 * @pino: inode number of dentry parent
1023 *
1024 * For syscalls that create or remove filesystem objects, audit_inode
1025 * can only collect information for the filesystem object's parent.
1026 * This call updates the audit context with the child's information.
1027 * Syscalls that create a new filesystem object must be hooked after
1028 * the object is created. Syscalls that remove a filesystem object
1029 * must be hooked prior, in order to capture the target inode during
1030 * unsuccessful attempts.
1031 */
1032void __audit_inode_child(const char *dname, const struct inode *inode,
1033 unsigned long pino)
1034{
1035 int idx;
1036 struct audit_context *context = current->audit_context;
1037
1038 if (!context->in_syscall)
1039 return;
1040
1041 /* determine matching parent */
1042 if (dname)
1043 for (idx = 0; idx < context->name_count; idx++)
1044 if (context->names[idx].pino == pino) {
1045 const char *n;
1046 const char *name = context->names[idx].name;
1047 int dlen = strlen(dname);
1048 int nlen = name ? strlen(name) : 0;
1049
1050 if (nlen < dlen)
1051 continue;
1052
1053 /* disregard trailing slashes */
1054 n = name + nlen - 1;
1055 while ((*n == '/') && (n > name))
1056 n--;
1057
1058 /* find last path component */
1059 n = n - dlen + 1;
1060 if (n < name)
1061 continue;
1062 else if (n > name) {
1063 if (*--n != '/')
1064 continue;
1065 else
1066 n++;
1067 }
1068
1069 if (strncmp(n, dname, dlen) == 0)
1070 goto update_context;
1071 }
1072
1073 /* catch-all in case match not found */
1074 idx = context->name_count++;
1075 context->names[idx].name = NULL;
1076 context->names[idx].pino = pino;
1077#if AUDIT_DEBUG
1078 context->ino_count++;
1079#endif
1080
1081update_context:
1082 if (inode) {
1083 context->names[idx].ino = inode->i_ino;
1084 context->names[idx].dev = inode->i_sb->s_dev;
1085 context->names[idx].mode = inode->i_mode;
1086 context->names[idx].uid = inode->i_uid;
1087 context->names[idx].gid = inode->i_gid;
1088 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001089 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001093/**
1094 * auditsc_get_stamp - get local copies of audit_context values
1095 * @ctx: audit_context for the task
1096 * @t: timespec to store time recorded in the audit_context
1097 * @serial: serial value that is recorded in the audit_context
1098 *
1099 * Also sets the context as auditable.
1100 */
David Woodhousebfb44962005-05-21 21:08:09 +01001101void auditsc_get_stamp(struct audit_context *ctx,
1102 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
David Woodhousece625a82005-07-18 14:24:46 -04001104 if (!ctx->serial)
1105 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001106 t->tv_sec = ctx->ctime.tv_sec;
1107 t->tv_nsec = ctx->ctime.tv_nsec;
1108 *serial = ctx->serial;
1109 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001112/**
1113 * audit_set_loginuid - set a task's audit_context loginuid
1114 * @task: task whose audit context is being modified
1115 * @loginuid: loginuid value
1116 *
1117 * Returns 0.
1118 *
1119 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1120 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001121int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Steve Grubb456be6c2005-04-29 17:30:07 +01001123 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001124 struct audit_buffer *ab;
1125
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001126 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001127 if (ab) {
1128 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001129 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001130 task->pid, task->uid,
1131 task->audit_context->loginuid, loginuid);
1132 audit_log_end(ab);
1133 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001134 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 return 0;
1137}
1138
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001139/**
1140 * audit_get_loginuid - get the loginuid for an audit_context
1141 * @ctx: the audit_context
1142 *
1143 * Returns the context's loginuid or -1 if @ctx is NULL.
1144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145uid_t audit_get_loginuid(struct audit_context *ctx)
1146{
1147 return ctx ? ctx->loginuid : -1;
1148}
1149
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001150static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
1151{
1152 struct audit_context *context = current->audit_context;
1153 char *ctx = NULL;
1154 int len = 0;
1155
1156 if (likely(!context))
1157 return NULL;
1158
1159 len = security_ipc_getsecurity(ipcp, NULL, 0);
1160 if (len == -EOPNOTSUPP)
1161 goto ret;
1162 if (len < 0)
1163 goto error_path;
1164
1165 ctx = kmalloc(len, GFP_ATOMIC);
1166 if (!ctx)
1167 goto error_path;
1168
1169 len = security_ipc_getsecurity(ipcp, ctx, len);
1170 if (len < 0)
1171 goto error_path;
1172
1173 return ctx;
1174
1175error_path:
1176 kfree(ctx);
1177 audit_panic("error in audit_ipc_context");
1178ret:
1179 return NULL;
1180}
1181
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001182/**
1183 * audit_ipc_perms - record audit data for ipc
1184 * @qbytes: msgq bytes
1185 * @uid: msgq user id
1186 * @gid: msgq group id
1187 * @mode: msgq mode (permissions)
1188 *
1189 * Returns 0 for success or NULL context or < 0 on error.
1190 */
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001191int 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 -07001192{
1193 struct audit_aux_data_ipcctl *ax;
1194 struct audit_context *context = current->audit_context;
1195
1196 if (likely(!context))
1197 return 0;
1198
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001199 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (!ax)
1201 return -ENOMEM;
1202
1203 ax->qbytes = qbytes;
1204 ax->uid = uid;
1205 ax->gid = gid;
1206 ax->mode = mode;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001207 ax->ctx = audit_ipc_context(ipcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Steve Grubbc0404992005-05-13 18:17:42 +01001209 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ax->d.next = context->aux;
1211 context->aux = (void *)ax;
1212 return 0;
1213}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001214
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001215/**
1216 * audit_socketcall - record audit data for sys_socketcall
1217 * @nargs: number of args
1218 * @args: args array
1219 *
1220 * Returns 0 for success or NULL context or < 0 on error.
1221 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001222int audit_socketcall(int nargs, unsigned long *args)
1223{
1224 struct audit_aux_data_socketcall *ax;
1225 struct audit_context *context = current->audit_context;
1226
1227 if (likely(!context))
1228 return 0;
1229
1230 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1231 if (!ax)
1232 return -ENOMEM;
1233
1234 ax->nargs = nargs;
1235 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1236
1237 ax->d.type = AUDIT_SOCKETCALL;
1238 ax->d.next = context->aux;
1239 context->aux = (void *)ax;
1240 return 0;
1241}
1242
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001243/**
1244 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1245 * @len: data length in user space
1246 * @a: data address in kernel space
1247 *
1248 * Returns 0 for success or NULL context or < 0 on error.
1249 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001250int audit_sockaddr(int len, void *a)
1251{
1252 struct audit_aux_data_sockaddr *ax;
1253 struct audit_context *context = current->audit_context;
1254
1255 if (likely(!context))
1256 return 0;
1257
1258 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1259 if (!ax)
1260 return -ENOMEM;
1261
1262 ax->len = len;
1263 memcpy(ax->a, a, len);
1264
1265 ax->d.type = AUDIT_SOCKADDR;
1266 ax->d.next = context->aux;
1267 context->aux = (void *)ax;
1268 return 0;
1269}
1270
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001271/**
1272 * audit_avc_path - record the granting or denial of permissions
1273 * @dentry: dentry to record
1274 * @mnt: mnt to record
1275 *
1276 * Returns 0 for success or NULL context or < 0 on error.
1277 *
1278 * Called from security/selinux/avc.c::avc_audit()
1279 */
Stephen Smalley01116102005-05-21 00:15:52 +01001280int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1281{
1282 struct audit_aux_data_path *ax;
1283 struct audit_context *context = current->audit_context;
1284
1285 if (likely(!context))
1286 return 0;
1287
1288 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1289 if (!ax)
1290 return -ENOMEM;
1291
1292 ax->dentry = dget(dentry);
1293 ax->mnt = mntget(mnt);
1294
1295 ax->d.type = AUDIT_AVC_PATH;
1296 ax->d.next = context->aux;
1297 context->aux = (void *)ax;
1298 return 0;
1299}
1300
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001301/**
1302 * audit_signal_info - record signal info for shutting down audit subsystem
1303 * @sig: signal value
1304 * @t: task being signaled
1305 *
1306 * If the audit subsystem is being terminated, record the task (pid)
1307 * and uid that is doing that.
1308 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001309void audit_signal_info(int sig, struct task_struct *t)
1310{
1311 extern pid_t audit_sig_pid;
1312 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001313
David Woodhouse582edda2005-07-13 22:39:34 +01001314 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001315 if (sig == SIGTERM || sig == SIGHUP) {
1316 struct audit_context *ctx = current->audit_context;
1317 audit_sig_pid = current->pid;
1318 if (ctx)
1319 audit_sig_uid = ctx->loginuid;
1320 else
1321 audit_sig_uid = current->uid;
1322 }
1323 }
1324}