blob: 2e123a8a0d6030b14d43d60ed9ff658b2c00255a [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* auditsc.c -- System-call auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handles all system-call specific auditing features.
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
Amy Griffis73241cc2005-11-03 16:00:25 +00005 * Copyright 2005 Hewlett-Packard Development Company, L.P.
Dustin Kirklandb63862f2005-11-03 15:41:46 +00006 * Copyright (C) 2005 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * All Rights Reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
24 *
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
27 *
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
31 *
Dustin Kirklandb63862f2005-11-03 15:41:46 +000032 * The support of additional filter rules compares (>, <, >=, <=) was
33 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
34 *
Amy Griffis73241cc2005-11-03 16:00:25 +000035 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36 * filesystem information.
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000037 *
38 * Subject and object context labeling support added by <danjones@us.ibm.com>
39 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
42#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/types.h>
Alan Cox715b49e2006-01-18 17:44:07 -080044#include <asm/atomic.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000045#include <asm/types.h>
46#include <linux/fs.h>
47#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/mm.h>
49#include <linux/module.h>
Stephen Smalley01116102005-05-21 00:15:52 +010050#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010051#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/audit.h>
53#include <linux/personality.h>
54#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010055#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010056#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000058#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000059#include <linux/list.h>
Steve Grubba6c043a2006-01-01 14:07:00 -050060#include <linux/tty.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060061#include <linux/selinux.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
David Woodhousefe7752b2005-12-15 18:33:52 +000063#include "audit.h"
64
65extern struct list_head audit_filter_list[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* No syscall auditing will take place unless audit_enabled != 0. */
68extern int audit_enabled;
69
70/* AUDIT_NAMES is the number of slots we reserve in the audit_context
71 * for saving names from getname(). */
72#define AUDIT_NAMES 20
73
74/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
75 * audit_context from being used for nameless inodes from
76 * path_lookup. */
77#define AUDIT_NAMES_RESERVED 7
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* When fs/namei.c:getname() is called, we store the pointer in name and
80 * we don't let putname() free it (instead we free all of the saved
81 * pointers at syscall exit time).
82 *
83 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
84struct audit_names {
85 const char *name;
86 unsigned long ino;
Amy Griffis73241cc2005-11-03 16:00:25 +000087 unsigned long pino;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 dev_t dev;
89 umode_t mode;
90 uid_t uid;
91 gid_t gid;
92 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -040093 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
96struct audit_aux_data {
97 struct audit_aux_data *next;
98 int type;
99};
100
101#define AUDIT_AUX_IPCPERM 0
102
103struct audit_aux_data_ipcctl {
104 struct audit_aux_data d;
105 struct ipc_perm p;
106 unsigned long qbytes;
107 uid_t uid;
108 gid_t gid;
109 mode_t mode;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000110 char *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100113struct audit_aux_data_socketcall {
114 struct audit_aux_data d;
115 int nargs;
116 unsigned long args[0];
117};
118
119struct audit_aux_data_sockaddr {
120 struct audit_aux_data d;
121 int len;
122 char a[0];
123};
124
Stephen Smalley01116102005-05-21 00:15:52 +0100125struct audit_aux_data_path {
126 struct audit_aux_data d;
127 struct dentry *dentry;
128 struct vfsmount *mnt;
129};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/* The per-task audit context. */
132struct audit_context {
133 int in_syscall; /* 1 if task is in a syscall */
134 enum audit_state state;
135 unsigned int serial; /* serial number for record */
136 struct timespec ctime; /* time of syscall entry */
137 uid_t loginuid; /* login uid (identity) */
138 int major; /* syscall number */
139 unsigned long argv[4]; /* syscall arguments */
140 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100141 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int auditable; /* 1 if record should be written */
143 int name_count;
144 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100145 struct dentry * pwd;
146 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct audit_context *previous; /* For nested syscalls */
148 struct audit_aux_data *aux;
149
150 /* Save things to print about task_struct */
151 pid_t pid;
152 uid_t uid, euid, suid, fsuid;
153 gid_t gid, egid, sgid, fsgid;
154 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100155 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157#if AUDIT_DEBUG
158 int put_count;
159 int ino_count;
160#endif
161};
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164/* Compare a task_struct with an audit_rule. Return 1 on match, 0
165 * otherwise. */
166static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500167 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct audit_context *ctx,
169 enum audit_state *state)
170{
171 int i, j;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600172 u32 sid;
173
174 selinux_task_ctxid(tsk, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500177 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int result = 0;
179
Amy Griffis93315ed2006-02-07 12:05:27 -0500180 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500182 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
184 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500185 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500188 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500191 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 break;
193 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500194 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 break;
196 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500197 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 break;
199 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500200 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500203 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 break;
205 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500206 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
208 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500209 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
2fd6f582005-04-29 16:08:28 +0100211 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000212 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500213 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 case AUDIT_EXIT:
217 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500218 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 break;
220 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100221 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500222 if (f->val)
223 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100224 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500225 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
228 case AUDIT_DEVMAJOR:
229 if (ctx) {
230 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500231 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 ++result;
233 break;
234 }
235 }
236 }
237 break;
238 case AUDIT_DEVMINOR:
239 if (ctx) {
240 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500241 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ++result;
243 break;
244 }
245 }
246 }
247 break;
248 case AUDIT_INODE:
249 if (ctx) {
250 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500251 if (audit_comparator(ctx->names[j].ino, f->op, f->val) ||
252 audit_comparator(ctx->names[j].pino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ++result;
254 break;
255 }
256 }
257 }
258 break;
259 case AUDIT_LOGINUID:
260 result = 0;
261 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500262 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 break;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600264 case AUDIT_SE_USER:
265 case AUDIT_SE_ROLE:
266 case AUDIT_SE_TYPE:
267 case AUDIT_SE_SEN:
268 case AUDIT_SE_CLR:
269 /* NOTE: this may return negative values indicating
270 a temporary error. We simply treat this as a
271 match for now to avoid losing information that
272 may be wanted. An error message will also be
273 logged upon error */
274 if (f->se_rule)
275 result = selinux_audit_rule_match(sid, f->type,
276 f->op,
277 f->se_rule,
278 ctx);
279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 case AUDIT_ARG0:
281 case AUDIT_ARG1:
282 case AUDIT_ARG2:
283 case AUDIT_ARG3:
284 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500285 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 break;
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!result)
290 return 0;
291 }
292 switch (rule->action) {
293 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
294 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
295 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
296 }
297 return 1;
298}
299
300/* At process creation time, we can determine if system-call auditing is
301 * completely disabled for this task. Since we only have the task
302 * structure at this point, we can only check uid and gid.
303 */
304static enum audit_state audit_filter_task(struct task_struct *tsk)
305{
306 struct audit_entry *e;
307 enum audit_state state;
308
309 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100310 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
312 rcu_read_unlock();
313 return state;
314 }
315 }
316 rcu_read_unlock();
317 return AUDIT_BUILD_CONTEXT;
318}
319
320/* At syscall entry and exit time, this filter is called if the
321 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100322 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700323 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 */
325static enum audit_state audit_filter_syscall(struct task_struct *tsk,
326 struct audit_context *ctx,
327 struct list_head *list)
328{
329 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100330 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David Woodhouse351bb722005-07-14 14:40:06 +0100332 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100333 return AUDIT_DISABLED;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100336 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000337 int word = AUDIT_WORD(ctx->major);
338 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100339
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000340 list_for_each_entry_rcu(e, list, list) {
341 if ((e->rule.mask[word] & bit) == bit
342 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
343 rcu_read_unlock();
344 return state;
345 }
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 rcu_read_unlock();
349 return AUDIT_BUILD_CONTEXT;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static inline struct audit_context *audit_get_context(struct task_struct *tsk,
353 int return_valid,
354 int return_code)
355{
356 struct audit_context *context = tsk->audit_context;
357
358 if (likely(!context))
359 return NULL;
360 context->return_valid = return_valid;
361 context->return_code = return_code;
362
David Woodhouse21af6c42005-07-02 14:10:46 +0100363 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100365 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (state == AUDIT_RECORD_CONTEXT)
367 context->auditable = 1;
368 }
369
370 context->pid = tsk->pid;
371 context->uid = tsk->uid;
372 context->gid = tsk->gid;
373 context->euid = tsk->euid;
374 context->suid = tsk->suid;
375 context->fsuid = tsk->fsuid;
376 context->egid = tsk->egid;
377 context->sgid = tsk->sgid;
378 context->fsgid = tsk->fsgid;
379 context->personality = tsk->personality;
380 tsk->audit_context = NULL;
381 return context;
382}
383
384static inline void audit_free_names(struct audit_context *context)
385{
386 int i;
387
388#if AUDIT_DEBUG == 2
389 if (context->auditable
390 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000391 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 " name_count=%d put_count=%d"
393 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000394 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 context->serial, context->major, context->in_syscall,
396 context->name_count, context->put_count,
397 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000398 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 printk(KERN_ERR "names[%d] = %p = %s\n", i,
400 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000401 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dump_stack();
404 return;
405 }
406#endif
407#if AUDIT_DEBUG
408 context->put_count = 0;
409 context->ino_count = 0;
410#endif
411
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000412 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (context->names[i].name)
414 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100417 if (context->pwd)
418 dput(context->pwd);
419 if (context->pwdmnt)
420 mntput(context->pwdmnt);
421 context->pwd = NULL;
422 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425static inline void audit_free_aux(struct audit_context *context)
426{
427 struct audit_aux_data *aux;
428
429 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100430 if (aux->type == AUDIT_AVC_PATH) {
431 struct audit_aux_data_path *axi = (void *)aux;
432 dput(axi->dentry);
433 mntput(axi->mnt);
434 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000435 if ( aux->type == AUDIT_IPC ) {
436 struct audit_aux_data_ipcctl *axi = (void *)aux;
437 if (axi->ctx)
438 kfree(axi->ctx);
439 }
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 context->aux = aux->next;
442 kfree(aux);
443 }
444}
445
446static inline void audit_zero_context(struct audit_context *context,
447 enum audit_state state)
448{
449 uid_t loginuid = context->loginuid;
450
451 memset(context, 0, sizeof(*context));
452 context->state = state;
453 context->loginuid = loginuid;
454}
455
456static inline struct audit_context *audit_alloc_context(enum audit_state state)
457{
458 struct audit_context *context;
459
460 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
461 return NULL;
462 audit_zero_context(context, state);
463 return context;
464}
465
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700466/**
467 * audit_alloc - allocate an audit context block for a task
468 * @tsk: task
469 *
470 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * if necessary. Doing so turns on system call auditing for the
472 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700473 * needed.
474 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475int audit_alloc(struct task_struct *tsk)
476{
477 struct audit_context *context;
478 enum audit_state state;
479
480 if (likely(!audit_enabled))
481 return 0; /* Return if not auditing. */
482
483 state = audit_filter_task(tsk);
484 if (likely(state == AUDIT_DISABLED))
485 return 0;
486
487 if (!(context = audit_alloc_context(state))) {
488 audit_log_lost("out of memory in audit_alloc");
489 return -ENOMEM;
490 }
491
492 /* Preserve login uid */
493 context->loginuid = -1;
494 if (current->audit_context)
495 context->loginuid = current->audit_context->loginuid;
496
497 tsk->audit_context = context;
498 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
499 return 0;
500}
501
502static inline void audit_free_context(struct audit_context *context)
503{
504 struct audit_context *previous;
505 int count = 0;
506
507 do {
508 previous = context->previous;
509 if (previous || (count && count < 10)) {
510 ++count;
511 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
512 " freeing multiple contexts (%d)\n",
513 context->serial, context->major,
514 context->name_count, count);
515 }
516 audit_free_names(context);
517 audit_free_aux(context);
518 kfree(context);
519 context = previous;
520 } while (context);
521 if (count >= 10)
522 printk(KERN_ERR "audit: freed %d contexts\n", count);
523}
524
Al Viroe4951492006-03-29 20:17:10 -0500525static void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000526{
527 char *ctx = NULL;
528 ssize_t len = 0;
529
530 len = security_getprocattr(current, "current", NULL, 0);
531 if (len < 0) {
532 if (len != -EINVAL)
533 goto error_path;
534 return;
535 }
536
Al Viroe4951492006-03-29 20:17:10 -0500537 ctx = kmalloc(len, GFP_KERNEL);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000538 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000539 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000540
541 len = security_getprocattr(current, "current", ctx, len);
542 if (len < 0 )
543 goto error_path;
544
545 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000546 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000547
548error_path:
549 if (ctx)
550 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000551 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000552 return;
553}
554
Al Viroe4951492006-03-29 20:17:10 -0500555static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700556{
Al Viro45d9bb02006-03-29 20:02:55 -0500557 char name[sizeof(tsk->comm)];
558 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700559 struct vm_area_struct *vma;
560
Al Viroe4951492006-03-29 20:17:10 -0500561 /* tsk == current */
562
Al Viro45d9bb02006-03-29 20:02:55 -0500563 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100564 audit_log_format(ab, " comm=");
565 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700566
Al Viroe4951492006-03-29 20:17:10 -0500567 if (mm) {
568 down_read(&mm->mmap_sem);
569 vma = mm->mmap;
570 while (vma) {
571 if ((vma->vm_flags & VM_EXECUTABLE) &&
572 vma->vm_file) {
573 audit_log_d_path(ab, "exe=",
574 vma->vm_file->f_dentry,
575 vma->vm_file->f_vfsmnt);
576 break;
577 }
578 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700579 }
Al Viroe4951492006-03-29 20:17:10 -0500580 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700581 }
Al Viroe4951492006-03-29 20:17:10 -0500582 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700583}
584
Al Viroe4951492006-03-29 20:17:10 -0500585static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 int i;
588 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100589 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500590 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Al Viroe4951492006-03-29 20:17:10 -0500592 /* tsk == current */
593
594 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!ab)
596 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100597 audit_log_format(ab, "arch=%x syscall=%d",
598 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (context->personality != PER_LINUX)
600 audit_log_format(ab, " per=%lx", context->personality);
601 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100602 audit_log_format(ab, " success=%s exit=%ld",
603 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
604 context->return_code);
Al Viro45d9bb02006-03-29 20:02:55 -0500605 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
606 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500607 else
608 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 audit_log_format(ab,
610 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100611 " pid=%d auid=%u uid=%u gid=%u"
612 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500613 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 context->argv[0],
615 context->argv[1],
616 context->argv[2],
617 context->argv[3],
618 context->name_count,
619 context->pid,
620 context->loginuid,
621 context->uid,
622 context->gid,
623 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500624 context->egid, context->sgid, context->fsgid, tty);
Al Viroe4951492006-03-29 20:17:10 -0500625 audit_log_task_info(ab, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
David Woodhouse7551ced2005-05-26 12:04:57 +0100628 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100629
Al Viroe4951492006-03-29 20:17:10 -0500630 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!ab)
632 continue; /* audit_panic has been called */
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100635 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct audit_aux_data_ipcctl *axi = (void *)aux;
637 audit_log_format(ab,
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000638 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
639 axi->qbytes, axi->uid, axi->gid, axi->mode, axi->ctx);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100640 break; }
641
642 case AUDIT_SOCKETCALL: {
643 int i;
644 struct audit_aux_data_socketcall *axs = (void *)aux;
645 audit_log_format(ab, "nargs=%d", axs->nargs);
646 for (i=0; i<axs->nargs; i++)
647 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
648 break; }
649
650 case AUDIT_SOCKADDR: {
651 struct audit_aux_data_sockaddr *axs = (void *)aux;
652
653 audit_log_format(ab, "saddr=");
654 audit_log_hex(ab, axs->a, axs->len);
655 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100656
657 case AUDIT_AVC_PATH: {
658 struct audit_aux_data_path *axi = (void *)aux;
659 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100660 break; }
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
David Woodhouse8f37d472005-05-27 12:17:28 +0100666 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -0500667 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100668 if (ab) {
669 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
670 audit_log_end(ab);
671 }
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 for (i = 0; i < context->name_count; i++) {
Steve Grubb1b50eed2006-04-03 14:06:13 -0400674 int call_panic = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +0000675 unsigned long ino = context->names[i].ino;
676 unsigned long pino = context->names[i].pino;
677
Al Viroe4951492006-03-29 20:17:10 -0500678 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (!ab)
680 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000683
684 audit_log_format(ab, " name=");
685 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100686 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000687 else
688 audit_log_format(ab, "(null)");
689
690 if (pino != (unsigned long)-1)
691 audit_log_format(ab, " parent=%lu", pino);
692 if (ino != (unsigned long)-1)
693 audit_log_format(ab, " inode=%lu", ino);
694 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
695 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
696 " ouid=%u ogid=%u rdev=%02x:%02x",
697 MAJOR(context->names[i].dev),
698 MINOR(context->names[i].dev),
699 context->names[i].mode,
700 context->names[i].uid,
701 context->names[i].gid,
702 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 MINOR(context->names[i].rdev));
Steve Grubb1b50eed2006-04-03 14:06:13 -0400704 if (context->names[i].osid != 0) {
705 char *ctx = NULL;
706 u32 len;
707 if (selinux_ctxid_to_string(
708 context->names[i].osid, &ctx, &len)) {
709 audit_log_format(ab, " obj=%u",
710 context->names[i].osid);
711 call_panic = 1;
712 } else
713 audit_log_format(ab, " obj=%s", ctx);
714 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000715 }
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 audit_log_end(ab);
Steve Grubb1b50eed2006-04-03 14:06:13 -0400718 if (call_panic)
719 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721}
722
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700723/**
724 * audit_free - free a per-task audit context
725 * @tsk: task whose audit context block to free
726 *
Al Virofa84cb92006-03-29 20:30:19 -0500727 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729void audit_free(struct task_struct *tsk)
730{
731 struct audit_context *context;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (likely(!context))
735 return;
736
737 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +0100738 * function (e.g., exit_group), then free context block.
739 * We use GFP_ATOMIC here because we might be doing this
740 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -0500741 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +0100742 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500743 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 audit_free_context(context);
746}
747
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700748/**
749 * audit_syscall_entry - fill in an audit record at syscall entry
750 * @tsk: task being audited
751 * @arch: architecture type
752 * @major: major syscall type (function)
753 * @a1: additional syscall register 1
754 * @a2: additional syscall register 2
755 * @a3: additional syscall register 3
756 * @a4: additional syscall register 4
757 *
758 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * audit context was created when the task was created and the state or
760 * filters demand the audit context be built. If the state from the
761 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
762 * then the record will be written at syscall exit time (otherwise, it
763 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700764 * be written).
765 */
Al Viro5411be52006-03-29 20:23:36 -0500766void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 unsigned long a1, unsigned long a2,
768 unsigned long a3, unsigned long a4)
769{
Al Viro5411be52006-03-29 20:23:36 -0500770 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct audit_context *context = tsk->audit_context;
772 enum audit_state state;
773
774 BUG_ON(!context);
775
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700776 /*
777 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * calls in kernel_thread via the entry.S interface, instead of
779 * with direct calls. (If you are porting to a new
780 * architecture, hitting this condition can indicate that you
781 * got the _exit/_leave calls backward in entry.S.)
782 *
783 * i386 no
784 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -0600785 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 *
787 * This also happens with vm86 emulation in a non-nested manner
788 * (entries without exits), so this case must be caught.
789 */
790 if (context->in_syscall) {
791 struct audit_context *newctx;
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793#if AUDIT_DEBUG
794 printk(KERN_ERR
795 "audit(:%d) pid=%d in syscall=%d;"
796 " entering syscall=%d\n",
797 context->serial, tsk->pid, context->major, major);
798#endif
799 newctx = audit_alloc_context(context->state);
800 if (newctx) {
801 newctx->previous = context;
802 context = newctx;
803 tsk->audit_context = newctx;
804 } else {
805 /* If we can't alloc a new context, the best we
806 * can do is to leak memory (any pending putname
807 * will be lost). The only other alternative is
808 * to abandon auditing. */
809 audit_zero_context(context, context->state);
810 }
811 }
812 BUG_ON(context->in_syscall || context->name_count);
813
814 if (!audit_enabled)
815 return;
816
2fd6f582005-04-29 16:08:28 +0100817 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 context->major = major;
819 context->argv[0] = a1;
820 context->argv[1] = a2;
821 context->argv[2] = a3;
822 context->argv[3] = a4;
823
824 state = context->state;
825 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100826 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (likely(state == AUDIT_DISABLED))
828 return;
829
David Woodhousece625a82005-07-18 14:24:46 -0400830 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 context->ctime = CURRENT_TIME;
832 context->in_syscall = 1;
833 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
834}
835
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700836/**
837 * audit_syscall_exit - deallocate audit context after a system call
838 * @tsk: task being audited
839 * @valid: success/failure flag
840 * @return_code: syscall return value
841 *
842 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
844 * filtering, or because some other part of the kernel write an audit
845 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700846 * free the names stored from getname().
847 */
Al Viro5411be52006-03-29 20:23:36 -0500848void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Al Viro5411be52006-03-29 20:23:36 -0500850 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct audit_context *context;
852
2fd6f582005-04-29 16:08:28 +0100853 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -0500856 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David Woodhousef7056d62005-06-20 16:07:33 +0100858 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -0500859 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 context->in_syscall = 0;
862 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (context->previous) {
865 struct audit_context *new_context = context->previous;
866 context->previous = NULL;
867 audit_free_context(context);
868 tsk->audit_context = new_context;
869 } else {
870 audit_free_names(context);
871 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 tsk->audit_context = context;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700876/**
877 * audit_getname - add a name to the list
878 * @name: name to add
879 *
880 * Add a name to the list of audit names for this context.
881 * Called from fs/namei.c:getname().
882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883void audit_getname(const char *name)
884{
885 struct audit_context *context = current->audit_context;
886
887 if (!context || IS_ERR(name) || !name)
888 return;
889
890 if (!context->in_syscall) {
891#if AUDIT_DEBUG == 2
892 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
893 __FILE__, __LINE__, context->serial, name);
894 dump_stack();
895#endif
896 return;
897 }
898 BUG_ON(context->name_count >= AUDIT_NAMES);
899 context->names[context->name_count].name = name;
900 context->names[context->name_count].ino = (unsigned long)-1;
901 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100902 if (!context->pwd) {
903 read_lock(&current->fs->lock);
904 context->pwd = dget(current->fs->pwd);
905 context->pwdmnt = mntget(current->fs->pwdmnt);
906 read_unlock(&current->fs->lock);
907 }
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700911/* audit_putname - intercept a putname request
912 * @name: name to intercept and delay for putname
913 *
914 * If we have stored the name from getname in the audit context,
915 * then we delay the putname until syscall exit.
916 * Called from include/linux/fs.h:putname().
917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918void audit_putname(const char *name)
919{
920 struct audit_context *context = current->audit_context;
921
922 BUG_ON(!context);
923 if (!context->in_syscall) {
924#if AUDIT_DEBUG == 2
925 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
926 __FILE__, __LINE__, context->serial, name);
927 if (context->name_count) {
928 int i;
929 for (i = 0; i < context->name_count; i++)
930 printk(KERN_ERR "name[%d] = %p = %s\n", i,
931 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000932 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934#endif
935 __putname(name);
936 }
937#if AUDIT_DEBUG
938 else {
939 ++context->put_count;
940 if (context->put_count > context->name_count) {
941 printk(KERN_ERR "%s:%d(:%d): major=%d"
942 " in_syscall=%d putname(%p) name_count=%d"
943 " put_count=%d\n",
944 __FILE__, __LINE__,
945 context->serial, context->major,
946 context->in_syscall, name, context->name_count,
947 context->put_count);
948 dump_stack();
949 }
950 }
951#endif
952}
953
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000954void audit_inode_context(int idx, const struct inode *inode)
955{
956 struct audit_context *context = current->audit_context;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000957
Steve Grubb1b50eed2006-04-03 14:06:13 -0400958 selinux_get_inode_sid(inode, &context->names[idx].osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000959}
960
961
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700962/**
963 * audit_inode - store the inode and device from a lookup
964 * @name: name being audited
965 * @inode: inode being audited
966 * @flags: lookup flags (as used in path_lookup())
967 *
968 * Called from fs/namei.c:path_lookup().
969 */
Amy Griffis73241cc2005-11-03 16:00:25 +0000970void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 int idx;
973 struct audit_context *context = current->audit_context;
974
975 if (!context->in_syscall)
976 return;
977 if (context->name_count
978 && context->names[context->name_count-1].name
979 && context->names[context->name_count-1].name == name)
980 idx = context->name_count - 1;
981 else if (context->name_count > 1
982 && context->names[context->name_count-2].name
983 && context->names[context->name_count-2].name == name)
984 idx = context->name_count - 2;
985 else {
986 /* FIXME: how much do we care about inodes that have no
987 * associated name? */
988 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
989 return;
990 idx = context->name_count++;
991 context->names[idx].name = NULL;
992#if AUDIT_DEBUG
993 ++context->ino_count;
994#endif
995 }
David Woodhouseae7b9612005-06-20 16:11:05 +0100996 context->names[idx].dev = inode->i_sb->s_dev;
997 context->names[idx].mode = inode->i_mode;
998 context->names[idx].uid = inode->i_uid;
999 context->names[idx].gid = inode->i_gid;
1000 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001001 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001002 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1003 (strcmp(name, ".") != 0)) {
1004 context->names[idx].ino = (unsigned long)-1;
1005 context->names[idx].pino = inode->i_ino;
1006 } else {
1007 context->names[idx].ino = inode->i_ino;
1008 context->names[idx].pino = (unsigned long)-1;
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Amy Griffis73241cc2005-11-03 16:00:25 +00001012/**
1013 * audit_inode_child - collect inode info for created/removed objects
1014 * @dname: inode's dentry name
1015 * @inode: inode being audited
1016 * @pino: inode number of dentry parent
1017 *
1018 * For syscalls that create or remove filesystem objects, audit_inode
1019 * can only collect information for the filesystem object's parent.
1020 * This call updates the audit context with the child's information.
1021 * Syscalls that create a new filesystem object must be hooked after
1022 * the object is created. Syscalls that remove a filesystem object
1023 * must be hooked prior, in order to capture the target inode during
1024 * unsuccessful attempts.
1025 */
1026void __audit_inode_child(const char *dname, const struct inode *inode,
1027 unsigned long pino)
1028{
1029 int idx;
1030 struct audit_context *context = current->audit_context;
1031
1032 if (!context->in_syscall)
1033 return;
1034
1035 /* determine matching parent */
1036 if (dname)
1037 for (idx = 0; idx < context->name_count; idx++)
1038 if (context->names[idx].pino == pino) {
1039 const char *n;
1040 const char *name = context->names[idx].name;
1041 int dlen = strlen(dname);
1042 int nlen = name ? strlen(name) : 0;
1043
1044 if (nlen < dlen)
1045 continue;
1046
1047 /* disregard trailing slashes */
1048 n = name + nlen - 1;
1049 while ((*n == '/') && (n > name))
1050 n--;
1051
1052 /* find last path component */
1053 n = n - dlen + 1;
1054 if (n < name)
1055 continue;
1056 else if (n > name) {
1057 if (*--n != '/')
1058 continue;
1059 else
1060 n++;
1061 }
1062
1063 if (strncmp(n, dname, dlen) == 0)
1064 goto update_context;
1065 }
1066
1067 /* catch-all in case match not found */
1068 idx = context->name_count++;
1069 context->names[idx].name = NULL;
1070 context->names[idx].pino = pino;
1071#if AUDIT_DEBUG
1072 context->ino_count++;
1073#endif
1074
1075update_context:
1076 if (inode) {
1077 context->names[idx].ino = inode->i_ino;
1078 context->names[idx].dev = inode->i_sb->s_dev;
1079 context->names[idx].mode = inode->i_mode;
1080 context->names[idx].uid = inode->i_uid;
1081 context->names[idx].gid = inode->i_gid;
1082 context->names[idx].rdev = inode->i_rdev;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001083 audit_inode_context(idx, inode);
Amy Griffis73241cc2005-11-03 16:00:25 +00001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001087/**
1088 * auditsc_get_stamp - get local copies of audit_context values
1089 * @ctx: audit_context for the task
1090 * @t: timespec to store time recorded in the audit_context
1091 * @serial: serial value that is recorded in the audit_context
1092 *
1093 * Also sets the context as auditable.
1094 */
David Woodhousebfb44962005-05-21 21:08:09 +01001095void auditsc_get_stamp(struct audit_context *ctx,
1096 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
David Woodhousece625a82005-07-18 14:24:46 -04001098 if (!ctx->serial)
1099 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001100 t->tv_sec = ctx->ctime.tv_sec;
1101 t->tv_nsec = ctx->ctime.tv_nsec;
1102 *serial = ctx->serial;
1103 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001106/**
1107 * audit_set_loginuid - set a task's audit_context loginuid
1108 * @task: task whose audit context is being modified
1109 * @loginuid: loginuid value
1110 *
1111 * Returns 0.
1112 *
1113 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1114 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001115int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Steve Grubb456be6c2005-04-29 17:30:07 +01001117 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001118 struct audit_buffer *ab;
1119
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001120 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001121 if (ab) {
1122 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001123 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001124 task->pid, task->uid,
1125 task->audit_context->loginuid, loginuid);
1126 audit_log_end(ab);
1127 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001128 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 return 0;
1131}
1132
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001133/**
1134 * audit_get_loginuid - get the loginuid for an audit_context
1135 * @ctx: the audit_context
1136 *
1137 * Returns the context's loginuid or -1 if @ctx is NULL.
1138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139uid_t audit_get_loginuid(struct audit_context *ctx)
1140{
1141 return ctx ? ctx->loginuid : -1;
1142}
1143
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001144static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
1145{
1146 struct audit_context *context = current->audit_context;
1147 char *ctx = NULL;
1148 int len = 0;
1149
1150 if (likely(!context))
1151 return NULL;
1152
1153 len = security_ipc_getsecurity(ipcp, NULL, 0);
1154 if (len == -EOPNOTSUPP)
1155 goto ret;
1156 if (len < 0)
1157 goto error_path;
1158
1159 ctx = kmalloc(len, GFP_ATOMIC);
1160 if (!ctx)
1161 goto error_path;
1162
1163 len = security_ipc_getsecurity(ipcp, ctx, len);
1164 if (len < 0)
1165 goto error_path;
1166
1167 return ctx;
1168
1169error_path:
1170 kfree(ctx);
1171 audit_panic("error in audit_ipc_context");
1172ret:
1173 return NULL;
1174}
1175
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001176/**
1177 * audit_ipc_perms - record audit data for ipc
1178 * @qbytes: msgq bytes
1179 * @uid: msgq user id
1180 * @gid: msgq group id
1181 * @mode: msgq mode (permissions)
1182 *
1183 * Returns 0 for success or NULL context or < 0 on error.
1184 */
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001185int 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 -07001186{
1187 struct audit_aux_data_ipcctl *ax;
1188 struct audit_context *context = current->audit_context;
1189
1190 if (likely(!context))
1191 return 0;
1192
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001193 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (!ax)
1195 return -ENOMEM;
1196
1197 ax->qbytes = qbytes;
1198 ax->uid = uid;
1199 ax->gid = gid;
1200 ax->mode = mode;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001201 ax->ctx = audit_ipc_context(ipcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Steve Grubbc0404992005-05-13 18:17:42 +01001203 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ax->d.next = context->aux;
1205 context->aux = (void *)ax;
1206 return 0;
1207}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001208
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001209/**
1210 * audit_socketcall - record audit data for sys_socketcall
1211 * @nargs: number of args
1212 * @args: args array
1213 *
1214 * Returns 0 for success or NULL context or < 0 on error.
1215 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001216int audit_socketcall(int nargs, unsigned long *args)
1217{
1218 struct audit_aux_data_socketcall *ax;
1219 struct audit_context *context = current->audit_context;
1220
1221 if (likely(!context))
1222 return 0;
1223
1224 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1225 if (!ax)
1226 return -ENOMEM;
1227
1228 ax->nargs = nargs;
1229 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1230
1231 ax->d.type = AUDIT_SOCKETCALL;
1232 ax->d.next = context->aux;
1233 context->aux = (void *)ax;
1234 return 0;
1235}
1236
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001237/**
1238 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1239 * @len: data length in user space
1240 * @a: data address in kernel space
1241 *
1242 * Returns 0 for success or NULL context or < 0 on error.
1243 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001244int audit_sockaddr(int len, void *a)
1245{
1246 struct audit_aux_data_sockaddr *ax;
1247 struct audit_context *context = current->audit_context;
1248
1249 if (likely(!context))
1250 return 0;
1251
1252 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1253 if (!ax)
1254 return -ENOMEM;
1255
1256 ax->len = len;
1257 memcpy(ax->a, a, len);
1258
1259 ax->d.type = AUDIT_SOCKADDR;
1260 ax->d.next = context->aux;
1261 context->aux = (void *)ax;
1262 return 0;
1263}
1264
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001265/**
1266 * audit_avc_path - record the granting or denial of permissions
1267 * @dentry: dentry to record
1268 * @mnt: mnt to record
1269 *
1270 * Returns 0 for success or NULL context or < 0 on error.
1271 *
1272 * Called from security/selinux/avc.c::avc_audit()
1273 */
Stephen Smalley01116102005-05-21 00:15:52 +01001274int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1275{
1276 struct audit_aux_data_path *ax;
1277 struct audit_context *context = current->audit_context;
1278
1279 if (likely(!context))
1280 return 0;
1281
1282 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1283 if (!ax)
1284 return -ENOMEM;
1285
1286 ax->dentry = dget(dentry);
1287 ax->mnt = mntget(mnt);
1288
1289 ax->d.type = AUDIT_AVC_PATH;
1290 ax->d.next = context->aux;
1291 context->aux = (void *)ax;
1292 return 0;
1293}
1294
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001295/**
1296 * audit_signal_info - record signal info for shutting down audit subsystem
1297 * @sig: signal value
1298 * @t: task being signaled
1299 *
1300 * If the audit subsystem is being terminated, record the task (pid)
1301 * and uid that is doing that.
1302 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001303void audit_signal_info(int sig, struct task_struct *t)
1304{
1305 extern pid_t audit_sig_pid;
1306 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001307
David Woodhouse582edda2005-07-13 22:39:34 +01001308 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001309 if (sig == SIGTERM || sig == SIGHUP) {
1310 struct audit_context *ctx = current->audit_context;
1311 audit_sig_pid = current->pid;
1312 if (ctx)
1313 audit_sig_uid = ctx->loginuid;
1314 else
1315 audit_sig_uid = current->uid;
1316 }
1317 }
1318}