blob: 4052f0aec1d3bbae594eb4624373eb14f25d1880 [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* auditsc.c -- System-call auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handles all system-call specific auditing features.
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
Amy Griffis73241cc2005-11-03 16:00:25 +00005 * Copyright 2005 Hewlett-Packard Development Company, L.P.
Dustin Kirklandb63862f2005-11-03 15:41:46 +00006 * Copyright (C) 2005 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * All Rights Reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
24 *
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
27 *
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
31 *
Dustin Kirklandb63862f2005-11-03 15:41:46 +000032 * The support of additional filter rules compares (>, <, >=, <=) was
33 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
34 *
Amy Griffis73241cc2005-11-03 16:00:25 +000035 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36 * filesystem information.
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000037 *
38 * Subject and object context labeling support added by <danjones@us.ibm.com>
39 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
42#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/types.h>
Alan Cox715b49e2006-01-18 17:44:07 -080044#include <asm/atomic.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000045#include <asm/types.h>
46#include <linux/fs.h>
47#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mm.h>
49#include <linux/module.h>
Stephen Smalley01116102005-05-21 00:15:52 +010050#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010051#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/audit.h>
53#include <linux/personality.h>
54#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010055#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010056#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000058#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000059#include <linux/list.h>
Steve Grubba6c043a2006-01-01 14:07:00 -050060#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
David Woodhousefe7752b2005-12-15 18:33:52 +000062#include "audit.h"
63
64extern struct list_head audit_filter_list[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* No syscall auditing will take place unless audit_enabled != 0. */
67extern int audit_enabled;
68
69/* AUDIT_NAMES is the number of slots we reserve in the audit_context
70 * for saving names from getname(). */
71#define AUDIT_NAMES 20
72
73/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
74 * audit_context from being used for nameless inodes from
75 * path_lookup. */
76#define AUDIT_NAMES_RESERVED 7
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* When fs/namei.c:getname() is called, we store the pointer in name and
79 * we don't let putname() free it (instead we free all of the saved
80 * pointers at syscall exit time).
81 *
82 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
83struct audit_names {
84 const char *name;
85 unsigned long ino;
Amy Griffis73241cc2005-11-03 16:00:25 +000086 unsigned long pino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 dev_t dev;
88 umode_t mode;
89 uid_t uid;
90 gid_t gid;
91 dev_t rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000092 char *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95struct audit_aux_data {
96 struct audit_aux_data *next;
97 int type;
98};
99
100#define AUDIT_AUX_IPCPERM 0
101
102struct audit_aux_data_ipcctl {
103 struct audit_aux_data d;
104 struct ipc_perm p;
105 unsigned long qbytes;
106 uid_t uid;
107 gid_t gid;
108 mode_t mode;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000109 char *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110};
111
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100112struct audit_aux_data_socketcall {
113 struct audit_aux_data d;
114 int nargs;
115 unsigned long args[0];
116};
117
118struct audit_aux_data_sockaddr {
119 struct audit_aux_data d;
120 int len;
121 char a[0];
122};
123
Stephen Smalley01116102005-05-21 00:15:52 +0100124struct audit_aux_data_path {
125 struct audit_aux_data d;
126 struct dentry *dentry;
127 struct vfsmount *mnt;
128};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* The per-task audit context. */
131struct audit_context {
132 int in_syscall; /* 1 if task is in a syscall */
133 enum audit_state state;
134 unsigned int serial; /* serial number for record */
135 struct timespec ctime; /* time of syscall entry */
136 uid_t loginuid; /* login uid (identity) */
137 int major; /* syscall number */
138 unsigned long argv[4]; /* syscall arguments */
139 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100140 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 int auditable; /* 1 if record should be written */
142 int name_count;
143 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100144 struct dentry * pwd;
145 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct audit_context *previous; /* For nested syscalls */
147 struct audit_aux_data *aux;
148
149 /* Save things to print about task_struct */
150 pid_t pid;
151 uid_t uid, euid, suid, fsuid;
152 gid_t gid, egid, sgid, fsgid;
153 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100154 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156#if AUDIT_DEBUG
157 int put_count;
158 int ino_count;
159#endif
160};
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163/* Compare a task_struct with an audit_rule. Return 1 on match, 0
164 * otherwise. */
165static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500166 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct audit_context *ctx,
168 enum audit_state *state)
169{
170 int i, j;
171
172 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500173 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 int result = 0;
175
Amy Griffis93315ed2006-02-07 12:05:27 -0500176 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500178 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
180 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500181 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
183 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500184 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 break;
186 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500187 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 break;
189 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500190 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 break;
192 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500193 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 break;
195 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500196 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 break;
198 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500199 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 break;
201 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500202 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
204 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500205 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
2fd6f582005-04-29 16:08:28 +0100207 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000208 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500209 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 case AUDIT_EXIT:
213 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500214 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100217 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500218 if (f->val)
219 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100220 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500221 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 break;
224 case AUDIT_DEVMAJOR:
225 if (ctx) {
226 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500227 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 ++result;
229 break;
230 }
231 }
232 }
233 break;
234 case AUDIT_DEVMINOR:
235 if (ctx) {
236 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500237 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ++result;
239 break;
240 }
241 }
242 }
243 break;
244 case AUDIT_INODE:
245 if (ctx) {
246 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500247 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
248 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 ++result;
250 break;
251 }
252 }
253 }
254 break;
255 case AUDIT_LOGINUID:
256 result = 0;
257 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500258 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 break;
260 case AUDIT_ARG0:
261 case AUDIT_ARG1:
262 case AUDIT_ARG2:
263 case AUDIT_ARG3:
264 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500265 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267 }
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (!result)
270 return 0;
271 }
272 switch (rule->action) {
273 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
274 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
275 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
276 }
277 return 1;
278}
279
280/* At process creation time, we can determine if system-call auditing is
281 * completely disabled for this task. Since we only have the task
282 * structure at this point, we can only check uid and gid.
283 */
284static enum audit_state audit_filter_task(struct task_struct *tsk)
285{
286 struct audit_entry *e;
287 enum audit_state state;
288
289 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100290 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
292 rcu_read_unlock();
293 return state;
294 }
295 }
296 rcu_read_unlock();
297 return AUDIT_BUILD_CONTEXT;
298}
299
300/* At syscall entry and exit time, this filter is called if the
301 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100302 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700303 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305static enum audit_state audit_filter_syscall(struct task_struct *tsk,
306 struct audit_context *ctx,
307 struct list_head *list)
308{
309 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100310 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
David Woodhouse351bb722005-07-14 14:40:06 +0100312 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100313 return AUDIT_DISABLED;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100316 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000317 int word = AUDIT_WORD(ctx->major);
318 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100319
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000320 list_for_each_entry_rcu(e, list, list) {
321 if ((e->rule.mask[word] & bit) == bit
322 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
323 rcu_read_unlock();
324 return state;
325 }
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328 rcu_read_unlock();
329 return AUDIT_BUILD_CONTEXT;
330}
331
332/* This should be called with task_lock() held. */
333static inline struct audit_context *audit_get_context(struct task_struct *tsk,
334 int return_valid,
335 int return_code)
336{
337 struct audit_context *context = tsk->audit_context;
338
339 if (likely(!context))
340 return NULL;
341 context->return_valid = return_valid;
342 context->return_code = return_code;
343
David Woodhouse21af6c42005-07-02 14:10:46 +0100344 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100346 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 if (state == AUDIT_RECORD_CONTEXT)
348 context->auditable = 1;
349 }
350
351 context->pid = tsk->pid;
352 context->uid = tsk->uid;
353 context->gid = tsk->gid;
354 context->euid = tsk->euid;
355 context->suid = tsk->suid;
356 context->fsuid = tsk->fsuid;
357 context->egid = tsk->egid;
358 context->sgid = tsk->sgid;
359 context->fsgid = tsk->fsgid;
360 context->personality = tsk->personality;
361 tsk->audit_context = NULL;
362 return context;
363}
364
365static inline void audit_free_names(struct audit_context *context)
366{
367 int i;
368
369#if AUDIT_DEBUG == 2
370 if (context->auditable
371 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000372 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 " name_count=%d put_count=%d"
374 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000375 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 context->serial, context->major, context->in_syscall,
377 context->name_count, context->put_count,
378 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000379 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 printk(KERN_ERR "names[%d] = %p = %s\n", i,
381 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000382 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dump_stack();
385 return;
386 }
387#endif
388#if AUDIT_DEBUG
389 context->put_count = 0;
390 context->ino_count = 0;
391#endif
392
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000393 for (i = 0; i < context->name_count; i++) {
394 char *p = context->names[i].ctx;
395 context->names[i].ctx = NULL;
396 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (context->names[i].name)
398 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100401 if (context->pwd)
402 dput(context->pwd);
403 if (context->pwdmnt)
404 mntput(context->pwdmnt);
405 context->pwd = NULL;
406 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static inline void audit_free_aux(struct audit_context *context)
410{
411 struct audit_aux_data *aux;
412
413 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100414 if (aux->type == AUDIT_AVC_PATH) {
415 struct audit_aux_data_path *axi = (void *)aux;
416 dput(axi->dentry);
417 mntput(axi->mnt);
418 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000419 if ( aux->type == AUDIT_IPC ) {
420 struct audit_aux_data_ipcctl *axi = (void *)aux;
421 if (axi->ctx)
422 kfree(axi->ctx);
423 }
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 context->aux = aux->next;
426 kfree(aux);
427 }
428}
429
430static inline void audit_zero_context(struct audit_context *context,
431 enum audit_state state)
432{
433 uid_t loginuid = context->loginuid;
434
435 memset(context, 0, sizeof(*context));
436 context->state = state;
437 context->loginuid = loginuid;
438}
439
440static inline struct audit_context *audit_alloc_context(enum audit_state state)
441{
442 struct audit_context *context;
443
444 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
445 return NULL;
446 audit_zero_context(context, state);
447 return context;
448}
449
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700450/**
451 * audit_alloc - allocate an audit context block for a task
452 * @tsk: task
453 *
454 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * if necessary. Doing so turns on system call auditing for the
456 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700457 * needed.
458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459int audit_alloc(struct task_struct *tsk)
460{
461 struct audit_context *context;
462 enum audit_state state;
463
464 if (likely(!audit_enabled))
465 return 0; /* Return if not auditing. */
466
467 state = audit_filter_task(tsk);
468 if (likely(state == AUDIT_DISABLED))
469 return 0;
470
471 if (!(context = audit_alloc_context(state))) {
472 audit_log_lost("out of memory in audit_alloc");
473 return -ENOMEM;
474 }
475
476 /* Preserve login uid */
477 context->loginuid = -1;
478 if (current->audit_context)
479 context->loginuid = current->audit_context->loginuid;
480
481 tsk->audit_context = context;
482 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
483 return 0;
484}
485
486static inline void audit_free_context(struct audit_context *context)
487{
488 struct audit_context *previous;
489 int count = 0;
490
491 do {
492 previous = context->previous;
493 if (previous || (count && count < 10)) {
494 ++count;
495 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
496 " freeing multiple contexts (%d)\n",
497 context->serial, context->major,
498 context->name_count, count);
499 }
500 audit_free_names(context);
501 audit_free_aux(context);
502 kfree(context);
503 context = previous;
504 } while (context);
505 if (count >= 10)
506 printk(KERN_ERR "audit: freed %d contexts\n", count);
507}
508
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000509static void audit_log_task_context(struct audit_buffer *ab, gfp_t gfp_mask)
510{
511 char *ctx = NULL;
512 ssize_t len = 0;
513
514 len = security_getprocattr(current, "current", NULL, 0);
515 if (len < 0) {
516 if (len != -EINVAL)
517 goto error_path;
518 return;
519 }
520
521 ctx = kmalloc(len, gfp_mask);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000522 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000523 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000524
525 len = security_getprocattr(current, "current", ctx, len);
526 if (len < 0 )
527 goto error_path;
528
529 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000530 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000531
532error_path:
533 if (ctx)
534 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000535 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000536 return;
537}
538
Al Viro45d9bb02006-03-29 20:02:55 -0500539static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk, gfp_t gfp_mask)
Stephen Smalley219f0812005-04-18 10:47:35 -0700540{
Al Viro45d9bb02006-03-29 20:02:55 -0500541 char name[sizeof(tsk->comm)];
542 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700543 struct vm_area_struct *vma;
544
Al Viro45d9bb02006-03-29 20:02:55 -0500545 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100546 audit_log_format(ab, " comm=");
547 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700548
549 if (!mm)
550 return;
551
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000552 /*
553 * this is brittle; all callers that pass GFP_ATOMIC will have
Al Viro45d9bb02006-03-29 20:02:55 -0500554 * NULL tsk->mm and we won't get here.
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000555 */
Stephen Smalley219f0812005-04-18 10:47:35 -0700556 down_read(&mm->mmap_sem);
557 vma = mm->mmap;
558 while (vma) {
559 if ((vma->vm_flags & VM_EXECUTABLE) &&
560 vma->vm_file) {
561 audit_log_d_path(ab, "exe=",
562 vma->vm_file->f_dentry,
563 vma->vm_file->f_vfsmnt);
564 break;
565 }
566 vma = vma->vm_next;
567 }
568 up_read(&mm->mmap_sem);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000569 audit_log_task_context(ab, gfp_mask);
Stephen Smalley219f0812005-04-18 10:47:35 -0700570}
571
Al Viro45d9bb02006-03-29 20:02:55 -0500572static void audit_log_exit(struct audit_context *context, struct task_struct *tsk, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 int i;
575 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100576 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500577 const char *tty;
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);
Al Viro45d9bb02006-03-29 20:02:55 -0500590 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
591 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500592 else
593 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 audit_log_format(ab,
595 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100596 " pid=%d auid=%u uid=%u gid=%u"
597 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500598 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 context->argv[0],
600 context->argv[1],
601 context->argv[2],
602 context->argv[3],
603 context->name_count,
604 context->pid,
605 context->loginuid,
606 context->uid,
607 context->gid,
608 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500609 context->egid, context->sgid, context->fsgid, tty);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000610 audit_log_task_info(ab, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
David Woodhouse7551ced2005-05-26 12:04:57 +0100613 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100614
Al Viroef20c8c2006-02-18 15:41:50 -0500615 ab = audit_log_start(context, gfp_mask, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!ab)
617 continue; /* audit_panic has been called */
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100620 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 struct audit_aux_data_ipcctl *axi = (void *)aux;
622 audit_log_format(ab,
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000623 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
624 axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100625 break; }
626
627 case AUDIT_SOCKETCALL: {
628 int i;
629 struct audit_aux_data_socketcall *axs = (void *)aux;
630 audit_log_format(ab, "nargs=%d", axs->nargs);
631 for (i=0; i<axs->nargs; i++)
632 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
633 break; }
634
635 case AUDIT_SOCKADDR: {
636 struct audit_aux_data_sockaddr *axs = (void *)aux;
637
638 audit_log_format(ab, "saddr=");
639 audit_log_hex(ab, axs->a, axs->len);
640 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100641
642 case AUDIT_AVC_PATH: {
643 struct audit_aux_data_path *axi = (void *)aux;
644 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100645 break; }
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
David Woodhouse8f37d472005-05-27 12:17:28 +0100651 if (context->pwd && context->pwdmnt) {
Al Viroef20c8c2006-02-18 15:41:50 -0500652 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100653 if (ab) {
654 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
655 audit_log_end(ab);
656 }
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000659 unsigned long ino = context->names[i].ino;
660 unsigned long pino = context->names[i].pino;
661
Al Viroef20c8c2006-02-18 15:41:50 -0500662 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (!ab)
664 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000667
668 audit_log_format(ab, " name=");
669 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100670 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000671 else
672 audit_log_format(ab, "(null)");
673
674 if (pino != (unsigned long)-1)
675 audit_log_format(ab, " parent=%lu", pino);
676 if (ino != (unsigned long)-1)
677 audit_log_format(ab, " inode=%lu", ino);
678 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
679 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
680 " ouid=%u ogid=%u rdev=%02x:%02x",
681 MAJOR(context->names[i].dev),
682 MINOR(context->names[i].dev),
683 context->names[i].mode,
684 context->names[i].uid,
685 context->names[i].gid,
686 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 MINOR(context->names[i].rdev));
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000688 if (context->names[i].ctx) {
689 audit_log_format(ab, " obj=%s",
690 context->names[i].ctx);
691 }
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 audit_log_end(ab);
694 }
695}
696
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700697/**
698 * audit_free - free a per-task audit context
699 * @tsk: task whose audit context block to free
700 *
701 * Called from copy_process and __put_task_struct.
702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703void audit_free(struct task_struct *tsk)
704{
705 struct audit_context *context;
706
Ingo Molnar4023e022006-03-07 23:51:39 -0800707 /*
708 * No need to lock the task - when we execute audit_free()
709 * then the task has no external references anymore, and
710 * we are tearing it down. (The locking also confuses
711 * DEBUG_LOCKDEP - this freeing may occur in softirq
712 * contexts as well, via RCU.)
713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (likely(!context))
716 return;
717
718 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100719 * function (e.g., exit_group), then free context block.
720 * We use GFP_ATOMIC here because we might be doing this
721 * in the context of the idle thread */
David Woodhousef7056d62005-06-20 16:07:33 +0100722 if (context->in_syscall && context->auditable)
Al Viro45d9bb02006-03-29 20:02:55 -0500723 audit_log_exit(context, tsk, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 audit_free_context(context);
726}
727
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700728/**
729 * audit_syscall_entry - fill in an audit record at syscall entry
730 * @tsk: task being audited
731 * @arch: architecture type
732 * @major: major syscall type (function)
733 * @a1: additional syscall register 1
734 * @a2: additional syscall register 2
735 * @a3: additional syscall register 3
736 * @a4: additional syscall register 4
737 *
738 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * audit context was created when the task was created and the state or
740 * filters demand the audit context be built. If the state from the
741 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
742 * then the record will be written at syscall exit time (otherwise, it
743 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700744 * be written).
745 */
2fd6f582005-04-29 16:08:28 +0100746void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 unsigned long a1, unsigned long a2,
748 unsigned long a3, unsigned long a4)
749{
750 struct audit_context *context = tsk->audit_context;
751 enum audit_state state;
752
753 BUG_ON(!context);
754
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700755 /*
756 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * calls in kernel_thread via the entry.S interface, instead of
758 * with direct calls. (If you are porting to a new
759 * architecture, hitting this condition can indicate that you
760 * got the _exit/_leave calls backward in entry.S.)
761 *
762 * i386 no
763 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -0600764 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
766 * This also happens with vm86 emulation in a non-nested manner
767 * (entries without exits), so this case must be caught.
768 */
769 if (context->in_syscall) {
770 struct audit_context *newctx;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772#if AUDIT_DEBUG
773 printk(KERN_ERR
774 "audit(:%d) pid=%d in syscall=%d;"
775 " entering syscall=%d\n",
776 context->serial, tsk->pid, context->major, major);
777#endif
778 newctx = audit_alloc_context(context->state);
779 if (newctx) {
780 newctx->previous = context;
781 context = newctx;
782 tsk->audit_context = newctx;
783 } else {
784 /* If we can't alloc a new context, the best we
785 * can do is to leak memory (any pending putname
786 * will be lost). The only other alternative is
787 * to abandon auditing. */
788 audit_zero_context(context, context->state);
789 }
790 }
791 BUG_ON(context->in_syscall || context->name_count);
792
793 if (!audit_enabled)
794 return;
795
2fd6f582005-04-29 16:08:28 +0100796 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 context->major = major;
798 context->argv[0] = a1;
799 context->argv[1] = a2;
800 context->argv[2] = a3;
801 context->argv[3] = a4;
802
803 state = context->state;
804 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100805 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (likely(state == AUDIT_DISABLED))
807 return;
808
David Woodhousece625a82005-07-18 14:24:46 -0400809 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 context->ctime = CURRENT_TIME;
811 context->in_syscall = 1;
812 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
813}
814
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700815/**
816 * audit_syscall_exit - deallocate audit context after a system call
817 * @tsk: task being audited
818 * @valid: success/failure flag
819 * @return_code: syscall return value
820 *
821 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
823 * filtering, or because some other part of the kernel write an audit
824 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700825 * free the names stored from getname().
826 */
2fd6f582005-04-29 16:08:28 +0100827void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct audit_context *context;
830
831 get_task_struct(tsk);
832 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +0100833 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 task_unlock(tsk);
835
836 /* Not having a context here is ok, since the parent may have
837 * called __put_task_struct. */
838 if (likely(!context))
David Woodhouse413a1c72005-08-17 14:45:55 +0100839 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
David Woodhousef7056d62005-06-20 16:07:33 +0100841 if (context->in_syscall && context->auditable)
Al Viro45d9bb02006-03-29 20:02:55 -0500842 audit_log_exit(context, tsk, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 context->in_syscall = 0;
845 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (context->previous) {
848 struct audit_context *new_context = context->previous;
849 context->previous = NULL;
850 audit_free_context(context);
851 tsk->audit_context = new_context;
852 } else {
853 audit_free_names(context);
854 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 tsk->audit_context = context;
856 }
David Woodhouse413a1c72005-08-17 14:45:55 +0100857 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 put_task_struct(tsk);
859}
860
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700861/**
862 * audit_getname - add a name to the list
863 * @name: name to add
864 *
865 * Add a name to the list of audit names for this context.
866 * Called from fs/namei.c:getname().
867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868void audit_getname(const char *name)
869{
870 struct audit_context *context = current->audit_context;
871
872 if (!context || IS_ERR(name) || !name)
873 return;
874
875 if (!context->in_syscall) {
876#if AUDIT_DEBUG == 2
877 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
878 __FILE__, __LINE__, context->serial, name);
879 dump_stack();
880#endif
881 return;
882 }
883 BUG_ON(context->name_count >= AUDIT_NAMES);
884 context->names[context->name_count].name = name;
885 context->names[context->name_count].ino = (unsigned long)-1;
886 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100887 if (!context->pwd) {
888 read_lock(&current->fs->lock);
889 context->pwd = dget(current->fs->pwd);
890 context->pwdmnt = mntget(current->fs->pwdmnt);
891 read_unlock(&current->fs->lock);
892 }
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700896/* audit_putname - intercept a putname request
897 * @name: name to intercept and delay for putname
898 *
899 * If we have stored the name from getname in the audit context,
900 * then we delay the putname until syscall exit.
901 * Called from include/linux/fs.h:putname().
902 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903void audit_putname(const char *name)
904{
905 struct audit_context *context = current->audit_context;
906
907 BUG_ON(!context);
908 if (!context->in_syscall) {
909#if AUDIT_DEBUG == 2
910 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
911 __FILE__, __LINE__, context->serial, name);
912 if (context->name_count) {
913 int i;
914 for (i = 0; i < context->name_count; i++)
915 printk(KERN_ERR "name[%d] = %p = %s\n", i,
916 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000917 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919#endif
920 __putname(name);
921 }
922#if AUDIT_DEBUG
923 else {
924 ++context->put_count;
925 if (context->put_count > context->name_count) {
926 printk(KERN_ERR "%s:%d(:%d): major=%d"
927 " in_syscall=%d putname(%p) name_count=%d"
928 " put_count=%d\n",
929 __FILE__, __LINE__,
930 context->serial, context->major,
931 context->in_syscall, name, context->name_count,
932 context->put_count);
933 dump_stack();
934 }
935 }
936#endif
937}
938
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000939void audit_inode_context(int idx, const struct inode *inode)
940{
941 struct audit_context *context = current->audit_context;
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000942 const char *suffix = security_inode_xattr_getsuffix();
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000943 char *ctx = NULL;
944 int len = 0;
945
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000946 if (!suffix)
947 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000948
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000949 len = security_inode_getsecurity(inode, suffix, NULL, 0, 0);
950 if (len == -EOPNOTSUPP)
951 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000952 if (len < 0)
953 goto error_path;
954
955 ctx = kmalloc(len, GFP_KERNEL);
956 if (!ctx)
957 goto error_path;
958
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000959 len = security_inode_getsecurity(inode, suffix, ctx, len, 0);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000960 if (len < 0)
961 goto error_path;
962
963 kfree(context->names[idx].ctx);
964 context->names[idx].ctx = ctx;
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000965 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000966
967error_path:
968 if (ctx)
969 kfree(ctx);
970 audit_panic("error in audit_inode_context");
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000971ret:
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000972 return;
973}
974
975
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700976/**
977 * audit_inode - store the inode and device from a lookup
978 * @name: name being audited
979 * @inode: inode being audited
980 * @flags: lookup flags (as used in path_lookup())
981 *
982 * Called from fs/namei.c:path_lookup().
983 */
Amy Griffis73241cc2005-11-03 16:00:25 +0000984void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 int idx;
987 struct audit_context *context = current->audit_context;
988
989 if (!context->in_syscall)
990 return;
991 if (context->name_count
992 && context->names[context->name_count-1].name
993 && context->names[context->name_count-1].name == name)
994 idx = context->name_count - 1;
995 else if (context->name_count > 1
996 && context->names[context->name_count-2].name
997 && context->names[context->name_count-2].name == name)
998 idx = context->name_count - 2;
999 else {
1000 /* FIXME: how much do we care about inodes that have no
1001 * associated name? */
1002 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1003 return;
1004 idx = context->name_count++;
1005 context->names[idx].name = NULL;
1006#if AUDIT_DEBUG
1007 ++context->ino_count;
1008#endif
1009 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001010 context->names[idx].dev = inode->i_sb->s_dev;
1011 context->names[idx].mode = inode->i_mode;
1012 context->names[idx].uid = inode->i_uid;
1013 context->names[idx].gid = inode->i_gid;
1014 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001015 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001016 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1017 (strcmp(name, ".") != 0)) {
1018 context->names[idx].ino = (unsigned long)-1;
1019 context->names[idx].pino = inode->i_ino;
1020 } else {
1021 context->names[idx].ino = inode->i_ino;
1022 context->names[idx].pino = (unsigned long)-1;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
Amy Griffis73241cc2005-11-03 16:00:25 +00001026/**
1027 * audit_inode_child - collect inode info for created/removed objects
1028 * @dname: inode's dentry name
1029 * @inode: inode being audited
1030 * @pino: inode number of dentry parent
1031 *
1032 * For syscalls that create or remove filesystem objects, audit_inode
1033 * can only collect information for the filesystem object's parent.
1034 * This call updates the audit context with the child's information.
1035 * Syscalls that create a new filesystem object must be hooked after
1036 * the object is created. Syscalls that remove a filesystem object
1037 * must be hooked prior, in order to capture the target inode during
1038 * unsuccessful attempts.
1039 */
1040void __audit_inode_child(const char *dname, const struct inode *inode,
1041 unsigned long pino)
1042{
1043 int idx;
1044 struct audit_context *context = current->audit_context;
1045
1046 if (!context->in_syscall)
1047 return;
1048
1049 /* determine matching parent */
1050 if (dname)
1051 for (idx = 0; idx < context->name_count; idx++)
1052 if (context->names[idx].pino == pino) {
1053 const char *n;
1054 const char *name = context->names[idx].name;
1055 int dlen = strlen(dname);
1056 int nlen = name ? strlen(name) : 0;
1057
1058 if (nlen < dlen)
1059 continue;
1060
1061 /* disregard trailing slashes */
1062 n = name + nlen - 1;
1063 while ((*n == '/') && (n > name))
1064 n--;
1065
1066 /* find last path component */
1067 n = n - dlen + 1;
1068 if (n < name)
1069 continue;
1070 else if (n > name) {
1071 if (*--n != '/')
1072 continue;
1073 else
1074 n++;
1075 }
1076
1077 if (strncmp(n, dname, dlen) == 0)
1078 goto update_context;
1079 }
1080
1081 /* catch-all in case match not found */
1082 idx = context->name_count++;
1083 context->names[idx].name = NULL;
1084 context->names[idx].pino = pino;
1085#if AUDIT_DEBUG
1086 context->ino_count++;
1087#endif
1088
1089update_context:
1090 if (inode) {
1091 context->names[idx].ino = inode->i_ino;
1092 context->names[idx].dev = inode->i_sb->s_dev;
1093 context->names[idx].mode = inode->i_mode;
1094 context->names[idx].uid = inode->i_uid;
1095 context->names[idx].gid = inode->i_gid;
1096 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001097 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001101/**
1102 * auditsc_get_stamp - get local copies of audit_context values
1103 * @ctx: audit_context for the task
1104 * @t: timespec to store time recorded in the audit_context
1105 * @serial: serial value that is recorded in the audit_context
1106 *
1107 * Also sets the context as auditable.
1108 */
David Woodhousebfb44962005-05-21 21:08:09 +01001109void auditsc_get_stamp(struct audit_context *ctx,
1110 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
David Woodhousece625a82005-07-18 14:24:46 -04001112 if (!ctx->serial)
1113 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001114 t->tv_sec = ctx->ctime.tv_sec;
1115 t->tv_nsec = ctx->ctime.tv_nsec;
1116 *serial = ctx->serial;
1117 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001120/**
1121 * audit_set_loginuid - set a task's audit_context loginuid
1122 * @task: task whose audit context is being modified
1123 * @loginuid: loginuid value
1124 *
1125 * Returns 0.
1126 *
1127 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1128 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001129int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Steve Grubb456be6c2005-04-29 17:30:07 +01001131 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001132 struct audit_buffer *ab;
1133
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001134 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001135 if (ab) {
1136 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001137 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001138 task->pid, task->uid,
1139 task->audit_context->loginuid, loginuid);
1140 audit_log_end(ab);
1141 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001142 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 return 0;
1145}
1146
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001147/**
1148 * audit_get_loginuid - get the loginuid for an audit_context
1149 * @ctx: the audit_context
1150 *
1151 * Returns the context's loginuid or -1 if @ctx is NULL.
1152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153uid_t audit_get_loginuid(struct audit_context *ctx)
1154{
1155 return ctx ? ctx->loginuid : -1;
1156}
1157
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001158static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
1159{
1160 struct audit_context *context = current->audit_context;
1161 char *ctx = NULL;
1162 int len = 0;
1163
1164 if (likely(!context))
1165 return NULL;
1166
1167 len = security_ipc_getsecurity(ipcp, NULL, 0);
1168 if (len == -EOPNOTSUPP)
1169 goto ret;
1170 if (len < 0)
1171 goto error_path;
1172
1173 ctx = kmalloc(len, GFP_ATOMIC);
1174 if (!ctx)
1175 goto error_path;
1176
1177 len = security_ipc_getsecurity(ipcp, ctx, len);
1178 if (len < 0)
1179 goto error_path;
1180
1181 return ctx;
1182
1183error_path:
1184 kfree(ctx);
1185 audit_panic("error in audit_ipc_context");
1186ret:
1187 return NULL;
1188}
1189
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001190/**
1191 * audit_ipc_perms - record audit data for ipc
1192 * @qbytes: msgq bytes
1193 * @uid: msgq user id
1194 * @gid: msgq group id
1195 * @mode: msgq mode (permissions)
1196 *
1197 * Returns 0 for success or NULL context or < 0 on error.
1198 */
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001199int 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 -07001200{
1201 struct audit_aux_data_ipcctl *ax;
1202 struct audit_context *context = current->audit_context;
1203
1204 if (likely(!context))
1205 return 0;
1206
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001207 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (!ax)
1209 return -ENOMEM;
1210
1211 ax->qbytes = qbytes;
1212 ax->uid = uid;
1213 ax->gid = gid;
1214 ax->mode = mode;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001215 ax->ctx = audit_ipc_context(ipcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Steve Grubbc0404992005-05-13 18:17:42 +01001217 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 ax->d.next = context->aux;
1219 context->aux = (void *)ax;
1220 return 0;
1221}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001222
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001223/**
1224 * audit_socketcall - record audit data for sys_socketcall
1225 * @nargs: number of args
1226 * @args: args array
1227 *
1228 * Returns 0 for success or NULL context or < 0 on error.
1229 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001230int audit_socketcall(int nargs, unsigned long *args)
1231{
1232 struct audit_aux_data_socketcall *ax;
1233 struct audit_context *context = current->audit_context;
1234
1235 if (likely(!context))
1236 return 0;
1237
1238 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1239 if (!ax)
1240 return -ENOMEM;
1241
1242 ax->nargs = nargs;
1243 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1244
1245 ax->d.type = AUDIT_SOCKETCALL;
1246 ax->d.next = context->aux;
1247 context->aux = (void *)ax;
1248 return 0;
1249}
1250
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001251/**
1252 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1253 * @len: data length in user space
1254 * @a: data address in kernel space
1255 *
1256 * Returns 0 for success or NULL context or < 0 on error.
1257 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001258int audit_sockaddr(int len, void *a)
1259{
1260 struct audit_aux_data_sockaddr *ax;
1261 struct audit_context *context = current->audit_context;
1262
1263 if (likely(!context))
1264 return 0;
1265
1266 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1267 if (!ax)
1268 return -ENOMEM;
1269
1270 ax->len = len;
1271 memcpy(ax->a, a, len);
1272
1273 ax->d.type = AUDIT_SOCKADDR;
1274 ax->d.next = context->aux;
1275 context->aux = (void *)ax;
1276 return 0;
1277}
1278
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001279/**
1280 * audit_avc_path - record the granting or denial of permissions
1281 * @dentry: dentry to record
1282 * @mnt: mnt to record
1283 *
1284 * Returns 0 for success or NULL context or < 0 on error.
1285 *
1286 * Called from security/selinux/avc.c::avc_audit()
1287 */
Stephen Smalley01116102005-05-21 00:15:52 +01001288int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1289{
1290 struct audit_aux_data_path *ax;
1291 struct audit_context *context = current->audit_context;
1292
1293 if (likely(!context))
1294 return 0;
1295
1296 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1297 if (!ax)
1298 return -ENOMEM;
1299
1300 ax->dentry = dget(dentry);
1301 ax->mnt = mntget(mnt);
1302
1303 ax->d.type = AUDIT_AVC_PATH;
1304 ax->d.next = context->aux;
1305 context->aux = (void *)ax;
1306 return 0;
1307}
1308
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001309/**
1310 * audit_signal_info - record signal info for shutting down audit subsystem
1311 * @sig: signal value
1312 * @t: task being signaled
1313 *
1314 * If the audit subsystem is being terminated, record the task (pid)
1315 * and uid that is doing that.
1316 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001317void audit_signal_info(int sig, struct task_struct *t)
1318{
1319 extern pid_t audit_sig_pid;
1320 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001321
David Woodhouse582edda2005-07-13 22:39:34 +01001322 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001323 if (sig == SIGTERM || sig == SIGHUP) {
1324 struct audit_context *ctx = current->audit_context;
1325 audit_sig_pid = current->pid;
1326 if (ctx)
1327 audit_sig_uid = ctx->loginuid;
1328 else
1329 audit_sig_uid = current->uid;
1330 }
1331 }
1332}