blob: 8aca4ab4aa2793e1c211ecbe21751d541cd4ca39 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static inline struct audit_context *audit_get_context(struct task_struct *tsk,
333 int return_valid,
334 int return_code)
335{
336 struct audit_context *context = tsk->audit_context;
337
338 if (likely(!context))
339 return NULL;
340 context->return_valid = return_valid;
341 context->return_code = return_code;
342
David Woodhouse21af6c42005-07-02 14:10:46 +0100343 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100345 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (state == AUDIT_RECORD_CONTEXT)
347 context->auditable = 1;
348 }
349
350 context->pid = tsk->pid;
351 context->uid = tsk->uid;
352 context->gid = tsk->gid;
353 context->euid = tsk->euid;
354 context->suid = tsk->suid;
355 context->fsuid = tsk->fsuid;
356 context->egid = tsk->egid;
357 context->sgid = tsk->sgid;
358 context->fsgid = tsk->fsgid;
359 context->personality = tsk->personality;
360 tsk->audit_context = NULL;
361 return context;
362}
363
364static inline void audit_free_names(struct audit_context *context)
365{
366 int i;
367
368#if AUDIT_DEBUG == 2
369 if (context->auditable
370 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000371 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 " name_count=%d put_count=%d"
373 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000374 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 context->serial, context->major, context->in_syscall,
376 context->name_count, context->put_count,
377 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000378 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 printk(KERN_ERR "names[%d] = %p = %s\n", i,
380 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000381 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 dump_stack();
384 return;
385 }
386#endif
387#if AUDIT_DEBUG
388 context->put_count = 0;
389 context->ino_count = 0;
390#endif
391
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000392 for (i = 0; i < context->name_count; i++) {
393 char *p = context->names[i].ctx;
394 context->names[i].ctx = NULL;
395 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (context->names[i].name)
397 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100400 if (context->pwd)
401 dput(context->pwd);
402 if (context->pwdmnt)
403 mntput(context->pwdmnt);
404 context->pwd = NULL;
405 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408static inline void audit_free_aux(struct audit_context *context)
409{
410 struct audit_aux_data *aux;
411
412 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100413 if (aux->type == AUDIT_AVC_PATH) {
414 struct audit_aux_data_path *axi = (void *)aux;
415 dput(axi->dentry);
416 mntput(axi->mnt);
417 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000418 if ( aux->type == AUDIT_IPC ) {
419 struct audit_aux_data_ipcctl *axi = (void *)aux;
420 if (axi->ctx)
421 kfree(axi->ctx);
422 }
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 context->aux = aux->next;
425 kfree(aux);
426 }
427}
428
429static inline void audit_zero_context(struct audit_context *context,
430 enum audit_state state)
431{
432 uid_t loginuid = context->loginuid;
433
434 memset(context, 0, sizeof(*context));
435 context->state = state;
436 context->loginuid = loginuid;
437}
438
439static inline struct audit_context *audit_alloc_context(enum audit_state state)
440{
441 struct audit_context *context;
442
443 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
444 return NULL;
445 audit_zero_context(context, state);
446 return context;
447}
448
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700449/**
450 * audit_alloc - allocate an audit context block for a task
451 * @tsk: task
452 *
453 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * if necessary. Doing so turns on system call auditing for the
455 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700456 * needed.
457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458int audit_alloc(struct task_struct *tsk)
459{
460 struct audit_context *context;
461 enum audit_state state;
462
463 if (likely(!audit_enabled))
464 return 0; /* Return if not auditing. */
465
466 state = audit_filter_task(tsk);
467 if (likely(state == AUDIT_DISABLED))
468 return 0;
469
470 if (!(context = audit_alloc_context(state))) {
471 audit_log_lost("out of memory in audit_alloc");
472 return -ENOMEM;
473 }
474
475 /* Preserve login uid */
476 context->loginuid = -1;
477 if (current->audit_context)
478 context->loginuid = current->audit_context->loginuid;
479
480 tsk->audit_context = context;
481 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
482 return 0;
483}
484
485static inline void audit_free_context(struct audit_context *context)
486{
487 struct audit_context *previous;
488 int count = 0;
489
490 do {
491 previous = context->previous;
492 if (previous || (count && count < 10)) {
493 ++count;
494 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
495 " freeing multiple contexts (%d)\n",
496 context->serial, context->major,
497 context->name_count, count);
498 }
499 audit_free_names(context);
500 audit_free_aux(context);
501 kfree(context);
502 context = previous;
503 } while (context);
504 if (count >= 10)
505 printk(KERN_ERR "audit: freed %d contexts\n", count);
506}
507
Al Viroe4951492006-03-29 20:17:10 -0500508static void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000509{
510 char *ctx = NULL;
511 ssize_t len = 0;
512
513 len = security_getprocattr(current, "current", NULL, 0);
514 if (len < 0) {
515 if (len != -EINVAL)
516 goto error_path;
517 return;
518 }
519
Al Viroe4951492006-03-29 20:17:10 -0500520 ctx = kmalloc(len, GFP_KERNEL);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000521 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000522 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000523
524 len = security_getprocattr(current, "current", ctx, len);
525 if (len < 0 )
526 goto error_path;
527
528 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000529 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000530
531error_path:
532 if (ctx)
533 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000534 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000535 return;
536}
537
Al Viroe4951492006-03-29 20:17:10 -0500538static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700539{
Al Viro45d9bb02006-03-29 20:02:55 -0500540 char name[sizeof(tsk->comm)];
541 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700542 struct vm_area_struct *vma;
543
Al Viroe4951492006-03-29 20:17:10 -0500544 /* tsk == current */
545
Al Viro45d9bb02006-03-29 20:02:55 -0500546 get_task_comm(name, tsk);
David Woodhouse99e45eea2005-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
Al Viroe4951492006-03-29 20:17:10 -0500550 if (mm) {
551 down_read(&mm->mmap_sem);
552 vma = mm->mmap;
553 while (vma) {
554 if ((vma->vm_flags & VM_EXECUTABLE) &&
555 vma->vm_file) {
556 audit_log_d_path(ab, "exe=",
557 vma->vm_file->f_dentry,
558 vma->vm_file->f_vfsmnt);
559 break;
560 }
561 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700562 }
Al Viroe4951492006-03-29 20:17:10 -0500563 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700564 }
Al Viroe4951492006-03-29 20:17:10 -0500565 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700566}
567
Al Viroe4951492006-03-29 20:17:10 -0500568static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 int i;
571 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100572 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500573 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Al Viroe4951492006-03-29 20:17:10 -0500575 /* tsk == current */
576
577 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (!ab)
579 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100580 audit_log_format(ab, "arch=%x syscall=%d",
581 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (context->personality != PER_LINUX)
583 audit_log_format(ab, " per=%lx", context->personality);
584 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100585 audit_log_format(ab, " success=%s exit=%ld",
586 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
587 context->return_code);
Al Viro45d9bb02006-03-29 20:02:55 -0500588 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
589 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500590 else
591 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 audit_log_format(ab,
593 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100594 " pid=%d auid=%u uid=%u gid=%u"
595 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500596 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 context->argv[0],
598 context->argv[1],
599 context->argv[2],
600 context->argv[3],
601 context->name_count,
602 context->pid,
603 context->loginuid,
604 context->uid,
605 context->gid,
606 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500607 context->egid, context->sgid, context->fsgid, tty);
Al Viroe4951492006-03-29 20:17:10 -0500608 audit_log_task_info(ab, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
David Woodhouse7551ced2005-05-26 12:04:57 +0100611 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100612
Al Viroe4951492006-03-29 20:17:10 -0500613 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (!ab)
615 continue; /* audit_panic has been called */
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100618 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct audit_aux_data_ipcctl *axi = (void *)aux;
620 audit_log_format(ab,
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000621 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
622 axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100623 break; }
624
625 case AUDIT_SOCKETCALL: {
626 int i;
627 struct audit_aux_data_socketcall *axs = (void *)aux;
628 audit_log_format(ab, "nargs=%d", axs->nargs);
629 for (i=0; i<axs->nargs; i++)
630 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
631 break; }
632
633 case AUDIT_SOCKADDR: {
634 struct audit_aux_data_sockaddr *axs = (void *)aux;
635
636 audit_log_format(ab, "saddr=");
637 audit_log_hex(ab, axs->a, axs->len);
638 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100639
640 case AUDIT_AVC_PATH: {
641 struct audit_aux_data_path *axi = (void *)aux;
642 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100643 break; }
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
David Woodhouse8f37d472005-05-27 12:17:28 +0100649 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -0500650 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100651 if (ab) {
652 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
653 audit_log_end(ab);
654 }
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000657 unsigned long ino = context->names[i].ino;
658 unsigned long pino = context->names[i].pino;
659
Al Viroe4951492006-03-29 20:17:10 -0500660 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!ab)
662 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000665
666 audit_log_format(ab, " name=");
667 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100668 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000669 else
670 audit_log_format(ab, "(null)");
671
672 if (pino != (unsigned long)-1)
673 audit_log_format(ab, " parent=%lu", pino);
674 if (ino != (unsigned long)-1)
675 audit_log_format(ab, " inode=%lu", ino);
676 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
677 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
678 " ouid=%u ogid=%u rdev=%02x:%02x",
679 MAJOR(context->names[i].dev),
680 MINOR(context->names[i].dev),
681 context->names[i].mode,
682 context->names[i].uid,
683 context->names[i].gid,
684 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 MINOR(context->names[i].rdev));
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000686 if (context->names[i].ctx) {
687 audit_log_format(ab, " obj=%s",
688 context->names[i].ctx);
689 }
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 audit_log_end(ab);
692 }
693}
694
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700695/**
696 * audit_free - free a per-task audit context
697 * @tsk: task whose audit context block to free
698 *
Al Virofa84cb92006-03-29 20:30:19 -0500699 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700700 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701void audit_free(struct task_struct *tsk)
702{
703 struct audit_context *context;
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (likely(!context))
707 return;
708
709 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100710 * function (e.g., exit_group), then free context block.
711 * We use GFP_ATOMIC here because we might be doing this
712 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -0500713 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +0100714 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500715 audit_log_exit(context, tsk);
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 */
Al Viro5411be52006-03-29 20:23:36 -0500738void audit_syscall_entry(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{
Al Viro5411be52006-03-29 20:23:36 -0500742 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct audit_context *context = tsk->audit_context;
744 enum audit_state state;
745
746 BUG_ON(!context);
747
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700748 /*
749 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 * calls in kernel_thread via the entry.S interface, instead of
751 * with direct calls. (If you are porting to a new
752 * architecture, hitting this condition can indicate that you
753 * got the _exit/_leave calls backward in entry.S.)
754 *
755 * i386 no
756 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -0600757 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 *
759 * This also happens with vm86 emulation in a non-nested manner
760 * (entries without exits), so this case must be caught.
761 */
762 if (context->in_syscall) {
763 struct audit_context *newctx;
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765#if AUDIT_DEBUG
766 printk(KERN_ERR
767 "audit(:%d) pid=%d in syscall=%d;"
768 " entering syscall=%d\n",
769 context->serial, tsk->pid, context->major, major);
770#endif
771 newctx = audit_alloc_context(context->state);
772 if (newctx) {
773 newctx->previous = context;
774 context = newctx;
775 tsk->audit_context = newctx;
776 } else {
777 /* If we can't alloc a new context, the best we
778 * can do is to leak memory (any pending putname
779 * will be lost). The only other alternative is
780 * to abandon auditing. */
781 audit_zero_context(context, context->state);
782 }
783 }
784 BUG_ON(context->in_syscall || context->name_count);
785
786 if (!audit_enabled)
787 return;
788
2fd6f582005-04-29 16:08:28 +0100789 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 context->major = major;
791 context->argv[0] = a1;
792 context->argv[1] = a2;
793 context->argv[2] = a3;
794 context->argv[3] = a4;
795
796 state = context->state;
797 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100798 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (likely(state == AUDIT_DISABLED))
800 return;
801
David Woodhousece625a82005-07-18 14:24:46 -0400802 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 context->ctime = CURRENT_TIME;
804 context->in_syscall = 1;
805 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
806}
807
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700808/**
809 * audit_syscall_exit - deallocate audit context after a system call
810 * @tsk: task being audited
811 * @valid: success/failure flag
812 * @return_code: syscall return value
813 *
814 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
816 * filtering, or because some other part of the kernel write an audit
817 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700818 * free the names stored from getname().
819 */
Al Viro5411be52006-03-29 20:23:36 -0500820void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Al Viro5411be52006-03-29 20:23:36 -0500822 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct audit_context *context;
824
2fd6f582005-04-29 16:08:28 +0100825 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -0500828 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
David Woodhousef7056d62005-06-20 16:07:33 +0100830 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500831 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 context->in_syscall = 0;
834 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (context->previous) {
837 struct audit_context *new_context = context->previous;
838 context->previous = NULL;
839 audit_free_context(context);
840 tsk->audit_context = new_context;
841 } else {
842 audit_free_names(context);
843 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 tsk->audit_context = context;
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700848/**
849 * audit_getname - add a name to the list
850 * @name: name to add
851 *
852 * Add a name to the list of audit names for this context.
853 * Called from fs/namei.c:getname().
854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855void audit_getname(const char *name)
856{
857 struct audit_context *context = current->audit_context;
858
859 if (!context || IS_ERR(name) || !name)
860 return;
861
862 if (!context->in_syscall) {
863#if AUDIT_DEBUG == 2
864 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
865 __FILE__, __LINE__, context->serial, name);
866 dump_stack();
867#endif
868 return;
869 }
870 BUG_ON(context->name_count >= AUDIT_NAMES);
871 context->names[context->name_count].name = name;
872 context->names[context->name_count].ino = (unsigned long)-1;
873 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100874 if (!context->pwd) {
875 read_lock(&current->fs->lock);
876 context->pwd = dget(current->fs->pwd);
877 context->pwdmnt = mntget(current->fs->pwdmnt);
878 read_unlock(&current->fs->lock);
879 }
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700883/* audit_putname - intercept a putname request
884 * @name: name to intercept and delay for putname
885 *
886 * If we have stored the name from getname in the audit context,
887 * then we delay the putname until syscall exit.
888 * Called from include/linux/fs.h:putname().
889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890void audit_putname(const char *name)
891{
892 struct audit_context *context = current->audit_context;
893
894 BUG_ON(!context);
895 if (!context->in_syscall) {
896#if AUDIT_DEBUG == 2
897 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
898 __FILE__, __LINE__, context->serial, name);
899 if (context->name_count) {
900 int i;
901 for (i = 0; i < context->name_count; i++)
902 printk(KERN_ERR "name[%d] = %p = %s\n", i,
903 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000904 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906#endif
907 __putname(name);
908 }
909#if AUDIT_DEBUG
910 else {
911 ++context->put_count;
912 if (context->put_count > context->name_count) {
913 printk(KERN_ERR "%s:%d(:%d): major=%d"
914 " in_syscall=%d putname(%p) name_count=%d"
915 " put_count=%d\n",
916 __FILE__, __LINE__,
917 context->serial, context->major,
918 context->in_syscall, name, context->name_count,
919 context->put_count);
920 dump_stack();
921 }
922 }
923#endif
924}
925
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000926void audit_inode_context(int idx, const struct inode *inode)
927{
928 struct audit_context *context = current->audit_context;
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000929 const char *suffix = security_inode_xattr_getsuffix();
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000930 char *ctx = NULL;
931 int len = 0;
932
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000933 if (!suffix)
934 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000935
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000936 len = security_inode_getsecurity(inode, suffix, NULL, 0, 0);
937 if (len == -EOPNOTSUPP)
938 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000939 if (len < 0)
940 goto error_path;
941
942 ctx = kmalloc(len, GFP_KERNEL);
943 if (!ctx)
944 goto error_path;
945
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000946 len = security_inode_getsecurity(inode, suffix, ctx, len, 0);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000947 if (len < 0)
948 goto error_path;
949
950 kfree(context->names[idx].ctx);
951 context->names[idx].ctx = ctx;
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000952 goto ret;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000953
954error_path:
955 if (ctx)
956 kfree(ctx);
957 audit_panic("error in audit_inode_context");
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000958ret:
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000959 return;
960}
961
962
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700963/**
964 * audit_inode - store the inode and device from a lookup
965 * @name: name being audited
966 * @inode: inode being audited
967 * @flags: lookup flags (as used in path_lookup())
968 *
969 * Called from fs/namei.c:path_lookup().
970 */
Amy Griffis73241cc2005-11-03 16:00:25 +0000971void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 int idx;
974 struct audit_context *context = current->audit_context;
975
976 if (!context->in_syscall)
977 return;
978 if (context->name_count
979 && context->names[context->name_count-1].name
980 && context->names[context->name_count-1].name == name)
981 idx = context->name_count - 1;
982 else if (context->name_count > 1
983 && context->names[context->name_count-2].name
984 && context->names[context->name_count-2].name == name)
985 idx = context->name_count - 2;
986 else {
987 /* FIXME: how much do we care about inodes that have no
988 * associated name? */
989 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
990 return;
991 idx = context->name_count++;
992 context->names[idx].name = NULL;
993#if AUDIT_DEBUG
994 ++context->ino_count;
995#endif
996 }
David Woodhouseae7b9612005-06-20 16:11:05 +0100997 context->names[idx].dev = inode->i_sb->s_dev;
998 context->names[idx].mode = inode->i_mode;
999 context->names[idx].uid = inode->i_uid;
1000 context->names[idx].gid = inode->i_gid;
1001 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001002 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001003 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1004 (strcmp(name, ".") != 0)) {
1005 context->names[idx].ino = (unsigned long)-1;
1006 context->names[idx].pino = inode->i_ino;
1007 } else {
1008 context->names[idx].ino = inode->i_ino;
1009 context->names[idx].pino = (unsigned long)-1;
1010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Amy Griffis73241cc2005-11-03 16:00:25 +00001013/**
1014 * audit_inode_child - collect inode info for created/removed objects
1015 * @dname: inode's dentry name
1016 * @inode: inode being audited
1017 * @pino: inode number of dentry parent
1018 *
1019 * For syscalls that create or remove filesystem objects, audit_inode
1020 * can only collect information for the filesystem object's parent.
1021 * This call updates the audit context with the child's information.
1022 * Syscalls that create a new filesystem object must be hooked after
1023 * the object is created. Syscalls that remove a filesystem object
1024 * must be hooked prior, in order to capture the target inode during
1025 * unsuccessful attempts.
1026 */
1027void __audit_inode_child(const char *dname, const struct inode *inode,
1028 unsigned long pino)
1029{
1030 int idx;
1031 struct audit_context *context = current->audit_context;
1032
1033 if (!context->in_syscall)
1034 return;
1035
1036 /* determine matching parent */
1037 if (dname)
1038 for (idx = 0; idx < context->name_count; idx++)
1039 if (context->names[idx].pino == pino) {
1040 const char *n;
1041 const char *name = context->names[idx].name;
1042 int dlen = strlen(dname);
1043 int nlen = name ? strlen(name) : 0;
1044
1045 if (nlen < dlen)
1046 continue;
1047
1048 /* disregard trailing slashes */
1049 n = name + nlen - 1;
1050 while ((*n == '/') && (n > name))
1051 n--;
1052
1053 /* find last path component */
1054 n = n - dlen + 1;
1055 if (n < name)
1056 continue;
1057 else if (n > name) {
1058 if (*--n != '/')
1059 continue;
1060 else
1061 n++;
1062 }
1063
1064 if (strncmp(n, dname, dlen) == 0)
1065 goto update_context;
1066 }
1067
1068 /* catch-all in case match not found */
1069 idx = context->name_count++;
1070 context->names[idx].name = NULL;
1071 context->names[idx].pino = pino;
1072#if AUDIT_DEBUG
1073 context->ino_count++;
1074#endif
1075
1076update_context:
1077 if (inode) {
1078 context->names[idx].ino = inode->i_ino;
1079 context->names[idx].dev = inode->i_sb->s_dev;
1080 context->names[idx].mode = inode->i_mode;
1081 context->names[idx].uid = inode->i_uid;
1082 context->names[idx].gid = inode->i_gid;
1083 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001084 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001088/**
1089 * auditsc_get_stamp - get local copies of audit_context values
1090 * @ctx: audit_context for the task
1091 * @t: timespec to store time recorded in the audit_context
1092 * @serial: serial value that is recorded in the audit_context
1093 *
1094 * Also sets the context as auditable.
1095 */
David Woodhousebfb44962005-05-21 21:08:09 +01001096void auditsc_get_stamp(struct audit_context *ctx,
1097 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
David Woodhousece625a82005-07-18 14:24:46 -04001099 if (!ctx->serial)
1100 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001101 t->tv_sec = ctx->ctime.tv_sec;
1102 t->tv_nsec = ctx->ctime.tv_nsec;
1103 *serial = ctx->serial;
1104 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001107/**
1108 * audit_set_loginuid - set a task's audit_context loginuid
1109 * @task: task whose audit context is being modified
1110 * @loginuid: loginuid value
1111 *
1112 * Returns 0.
1113 *
1114 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1115 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001116int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Steve Grubb456be6c2005-04-29 17:30:07 +01001118 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001119 struct audit_buffer *ab;
1120
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001121 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001122 if (ab) {
1123 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001124 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001125 task->pid, task->uid,
1126 task->audit_context->loginuid, loginuid);
1127 audit_log_end(ab);
1128 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001129 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 return 0;
1132}
1133
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001134/**
1135 * audit_get_loginuid - get the loginuid for an audit_context
1136 * @ctx: the audit_context
1137 *
1138 * Returns the context's loginuid or -1 if @ctx is NULL.
1139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140uid_t audit_get_loginuid(struct audit_context *ctx)
1141{
1142 return ctx ? ctx->loginuid : -1;
1143}
1144
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001145static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
1146{
1147 struct audit_context *context = current->audit_context;
1148 char *ctx = NULL;
1149 int len = 0;
1150
1151 if (likely(!context))
1152 return NULL;
1153
1154 len = security_ipc_getsecurity(ipcp, NULL, 0);
1155 if (len == -EOPNOTSUPP)
1156 goto ret;
1157 if (len < 0)
1158 goto error_path;
1159
1160 ctx = kmalloc(len, GFP_ATOMIC);
1161 if (!ctx)
1162 goto error_path;
1163
1164 len = security_ipc_getsecurity(ipcp, ctx, len);
1165 if (len < 0)
1166 goto error_path;
1167
1168 return ctx;
1169
1170error_path:
1171 kfree(ctx);
1172 audit_panic("error in audit_ipc_context");
1173ret:
1174 return NULL;
1175}
1176
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001177/**
1178 * audit_ipc_perms - record audit data for ipc
1179 * @qbytes: msgq bytes
1180 * @uid: msgq user id
1181 * @gid: msgq group id
1182 * @mode: msgq mode (permissions)
1183 *
1184 * Returns 0 for success or NULL context or < 0 on error.
1185 */
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001186int 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 -07001187{
1188 struct audit_aux_data_ipcctl *ax;
1189 struct audit_context *context = current->audit_context;
1190
1191 if (likely(!context))
1192 return 0;
1193
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001194 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (!ax)
1196 return -ENOMEM;
1197
1198 ax->qbytes = qbytes;
1199 ax->uid = uid;
1200 ax->gid = gid;
1201 ax->mode = mode;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001202 ax->ctx = audit_ipc_context(ipcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Steve Grubbc0404992005-05-13 18:17:42 +01001204 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 ax->d.next = context->aux;
1206 context->aux = (void *)ax;
1207 return 0;
1208}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001209
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001210/**
1211 * audit_socketcall - record audit data for sys_socketcall
1212 * @nargs: number of args
1213 * @args: args array
1214 *
1215 * Returns 0 for success or NULL context or < 0 on error.
1216 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001217int audit_socketcall(int nargs, unsigned long *args)
1218{
1219 struct audit_aux_data_socketcall *ax;
1220 struct audit_context *context = current->audit_context;
1221
1222 if (likely(!context))
1223 return 0;
1224
1225 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1226 if (!ax)
1227 return -ENOMEM;
1228
1229 ax->nargs = nargs;
1230 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1231
1232 ax->d.type = AUDIT_SOCKETCALL;
1233 ax->d.next = context->aux;
1234 context->aux = (void *)ax;
1235 return 0;
1236}
1237
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001238/**
1239 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1240 * @len: data length in user space
1241 * @a: data address in kernel space
1242 *
1243 * Returns 0 for success or NULL context or < 0 on error.
1244 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001245int audit_sockaddr(int len, void *a)
1246{
1247 struct audit_aux_data_sockaddr *ax;
1248 struct audit_context *context = current->audit_context;
1249
1250 if (likely(!context))
1251 return 0;
1252
1253 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1254 if (!ax)
1255 return -ENOMEM;
1256
1257 ax->len = len;
1258 memcpy(ax->a, a, len);
1259
1260 ax->d.type = AUDIT_SOCKADDR;
1261 ax->d.next = context->aux;
1262 context->aux = (void *)ax;
1263 return 0;
1264}
1265
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001266/**
1267 * audit_avc_path - record the granting or denial of permissions
1268 * @dentry: dentry to record
1269 * @mnt: mnt to record
1270 *
1271 * Returns 0 for success or NULL context or < 0 on error.
1272 *
1273 * Called from security/selinux/avc.c::avc_audit()
1274 */
Stephen Smalley01116102005-05-21 00:15:52 +01001275int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1276{
1277 struct audit_aux_data_path *ax;
1278 struct audit_context *context = current->audit_context;
1279
1280 if (likely(!context))
1281 return 0;
1282
1283 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1284 if (!ax)
1285 return -ENOMEM;
1286
1287 ax->dentry = dget(dentry);
1288 ax->mnt = mntget(mnt);
1289
1290 ax->d.type = AUDIT_AVC_PATH;
1291 ax->d.next = context->aux;
1292 context->aux = (void *)ax;
1293 return 0;
1294}
1295
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001296/**
1297 * audit_signal_info - record signal info for shutting down audit subsystem
1298 * @sig: signal value
1299 * @t: task being signaled
1300 *
1301 * If the audit subsystem is being terminated, record the task (pid)
1302 * and uid that is doing that.
1303 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001304void audit_signal_info(int sig, struct task_struct *t)
1305{
1306 extern pid_t audit_sig_pid;
1307 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001308
David Woodhouse582edda2005-07-13 22:39:34 +01001309 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001310 if (sig == SIGTERM || sig == SIGHUP) {
1311 struct audit_context *ctx = current->audit_context;
1312 audit_sig_pid = current->pid;
1313 if (ctx)
1314 audit_sig_uid = ctx->loginuid;
1315 else
1316 audit_sig_uid = current->uid;
1317 }
1318 }
1319}