blob: 40722e26de98e285c1f90ff810b20aff6d3cb9a2 [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.
George C. Wilson20ca73b2006-05-24 16:09:55 -05006 * Copyright (C) 2005, 2006 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 *
George C. Wilson20ca73b2006-05-24 16:09:55 -050032 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
33 * 2006.
34 *
Dustin Kirklandb63862f2005-11-03 15:41:46 +000035 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
37 *
Amy Griffis73241cc2005-11-03 16:00:25 +000038 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000040 *
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
44
45#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/types.h>
Alan Cox715b49e2006-01-18 17:44:07 -080047#include <asm/atomic.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000048#include <asm/types.h>
49#include <linux/fs.h>
50#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/mm.h>
52#include <linux/module.h>
Stephen Smalley01116102005-05-21 00:15:52 +010053#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010054#include <linux/socket.h>
George C. Wilson20ca73b2006-05-24 16:09:55 -050055#include <linux/mqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/audit.h>
57#include <linux/personality.h>
58#include <linux/time.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010059#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010060#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/unistd.h>
Dustin Kirkland8c8570f2005-11-03 17:15:16 +000062#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000063#include <linux/list.h>
Steve Grubba6c043a2006-01-01 14:07:00 -050064#include <linux/tty.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060065#include <linux/selinux.h>
Al Viro473ae302006-04-26 14:04:08 -040066#include <linux/binfmts.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040067#include <linux/highmem.h>
Al Virof46038f2006-05-06 08:22:52 -040068#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
David Woodhousefe7752b2005-12-15 18:33:52 +000070#include "audit.h"
71
72extern struct list_head audit_filter_list[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/* No syscall auditing will take place unless audit_enabled != 0. */
75extern int audit_enabled;
76
77/* AUDIT_NAMES is the number of slots we reserve in the audit_context
78 * for saving names from getname(). */
79#define AUDIT_NAMES 20
80
81/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
82 * audit_context from being used for nameless inodes from
83 * path_lookup. */
84#define AUDIT_NAMES_RESERVED 7
85
Amy Griffis9c937dc2006-06-08 23:19:31 -040086/* Indicates that audit should log the full pathname. */
87#define AUDIT_NAME_FULL -1
88
Al Viro471a5c72006-07-10 08:29:24 -040089/* number of audit rules */
90int audit_n_rules;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* When fs/namei.c:getname() is called, we store the pointer in name and
93 * we don't let putname() free it (instead we free all of the saved
94 * pointers at syscall exit time).
95 *
96 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
97struct audit_names {
98 const char *name;
Amy Griffis9c937dc2006-06-08 23:19:31 -040099 int name_len; /* number of name's characters to log */
100 unsigned name_put; /* call __putname() for this name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 unsigned long ino;
102 dev_t dev;
103 umode_t mode;
104 uid_t uid;
105 gid_t gid;
106 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400107 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110struct audit_aux_data {
111 struct audit_aux_data *next;
112 int type;
113};
114
115#define AUDIT_AUX_IPCPERM 0
116
George C. Wilson20ca73b2006-05-24 16:09:55 -0500117struct audit_aux_data_mq_open {
118 struct audit_aux_data d;
119 int oflag;
120 mode_t mode;
121 struct mq_attr attr;
122};
123
124struct audit_aux_data_mq_sendrecv {
125 struct audit_aux_data d;
126 mqd_t mqdes;
127 size_t msg_len;
128 unsigned int msg_prio;
129 struct timespec abs_timeout;
130};
131
132struct audit_aux_data_mq_notify {
133 struct audit_aux_data d;
134 mqd_t mqdes;
135 struct sigevent notification;
136};
137
138struct audit_aux_data_mq_getsetattr {
139 struct audit_aux_data d;
140 mqd_t mqdes;
141 struct mq_attr mqstat;
142};
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144struct audit_aux_data_ipcctl {
145 struct audit_aux_data d;
146 struct ipc_perm p;
147 unsigned long qbytes;
148 uid_t uid;
149 gid_t gid;
150 mode_t mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500151 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
Al Viro473ae302006-04-26 14:04:08 -0400154struct audit_aux_data_execve {
155 struct audit_aux_data d;
156 int argc;
157 int envc;
158 char mem[0];
159};
160
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100161struct audit_aux_data_socketcall {
162 struct audit_aux_data d;
163 int nargs;
164 unsigned long args[0];
165};
166
167struct audit_aux_data_sockaddr {
168 struct audit_aux_data d;
169 int len;
170 char a[0];
171};
172
Stephen Smalley01116102005-05-21 00:15:52 +0100173struct audit_aux_data_path {
174 struct audit_aux_data d;
175 struct dentry *dentry;
176 struct vfsmount *mnt;
177};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/* The per-task audit context. */
180struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400181 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int in_syscall; /* 1 if task is in a syscall */
183 enum audit_state state;
184 unsigned int serial; /* serial number for record */
185 struct timespec ctime; /* time of syscall entry */
186 uid_t loginuid; /* login uid (identity) */
187 int major; /* syscall number */
188 unsigned long argv[4]; /* syscall arguments */
189 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100190 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int auditable; /* 1 if record should be written */
192 int name_count;
193 struct audit_names names[AUDIT_NAMES];
Amy Griffis5adc8a62006-06-14 18:45:21 -0400194 char * filterkey; /* key for rule that triggered record */
David Woodhouse8f37d472005-05-27 12:17:28 +0100195 struct dentry * pwd;
196 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct audit_context *previous; /* For nested syscalls */
198 struct audit_aux_data *aux;
199
200 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400201 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 uid_t uid, euid, suid, fsuid;
203 gid_t gid, egid, sgid, fsgid;
204 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100205 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207#if AUDIT_DEBUG
208 int put_count;
209 int ino_count;
210#endif
211};
212
Al Viro55669bf2006-08-31 19:26:40 -0400213#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
214static inline int open_arg(int flags, int mask)
215{
216 int n = ACC_MODE(flags);
217 if (flags & (O_TRUNC | O_CREAT))
218 n |= AUDIT_PERM_WRITE;
219 return n & mask;
220}
221
222static int audit_match_perm(struct audit_context *ctx, int mask)
223{
224 unsigned n = ctx->major;
225 switch (audit_classify_syscall(ctx->arch, n)) {
226 case 0: /* native */
227 if ((mask & AUDIT_PERM_WRITE) &&
228 audit_match_class(AUDIT_CLASS_WRITE, n))
229 return 1;
230 if ((mask & AUDIT_PERM_READ) &&
231 audit_match_class(AUDIT_CLASS_READ, n))
232 return 1;
233 if ((mask & AUDIT_PERM_ATTR) &&
234 audit_match_class(AUDIT_CLASS_CHATTR, n))
235 return 1;
236 return 0;
237 case 1: /* 32bit on biarch */
238 if ((mask & AUDIT_PERM_WRITE) &&
239 audit_match_class(AUDIT_CLASS_WRITE_32, n))
240 return 1;
241 if ((mask & AUDIT_PERM_READ) &&
242 audit_match_class(AUDIT_CLASS_READ_32, n))
243 return 1;
244 if ((mask & AUDIT_PERM_ATTR) &&
245 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
246 return 1;
247 return 0;
248 case 2: /* open */
249 return mask & ACC_MODE(ctx->argv[1]);
250 case 3: /* openat */
251 return mask & ACC_MODE(ctx->argv[2]);
252 case 4: /* socketcall */
253 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
254 case 5: /* execve */
255 return mask & AUDIT_PERM_EXEC;
256 default:
257 return 0;
258 }
259}
260
Amy Griffisf368c07d2006-04-07 16:55:56 -0400261/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/* Compare a task_struct with an audit_rule. Return 1 on match, 0
263 * otherwise. */
264static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500265 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400267 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 enum audit_state *state)
269{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400270 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600271 u32 sid;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500274 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int result = 0;
276
Amy Griffis93315ed2006-02-07 12:05:27 -0500277 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500279 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
Al Viro3c662512006-05-06 08:26:27 -0400281 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400282 if (ctx) {
283 if (!ctx->ppid)
284 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400285 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400286 }
Al Viro3c662512006-05-06 08:26:27 -0400287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500289 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 break;
291 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500292 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 break;
294 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500295 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500298 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 break;
300 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500301 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500304 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
306 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500307 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 break;
309 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500310 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500313 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 break;
2fd6f582005-04-29 16:08:28 +0100315 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000316 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500317 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100318 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 case AUDIT_EXIT:
321 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500322 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
324 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100325 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500326 if (f->val)
327 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100328 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500329 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 break;
332 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400333 if (name)
334 result = audit_comparator(MAJOR(name->dev),
335 f->op, f->val);
336 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500338 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 ++result;
340 break;
341 }
342 }
343 }
344 break;
345 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400346 if (name)
347 result = audit_comparator(MINOR(name->dev),
348 f->op, f->val);
349 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500351 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ++result;
353 break;
354 }
355 }
356 }
357 break;
358 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400359 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400360 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400361 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400363 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 ++result;
365 break;
366 }
367 }
368 }
369 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400370 case AUDIT_WATCH:
371 if (name && rule->watch->ino != (unsigned long)-1)
372 result = (name->dev == rule->watch->dev &&
Amy Griffis9c937dc2006-06-08 23:19:31 -0400373 name->ino == rule->watch->ino);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400374 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 case AUDIT_LOGINUID:
376 result = 0;
377 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500378 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500380 case AUDIT_SUBJ_USER:
381 case AUDIT_SUBJ_ROLE:
382 case AUDIT_SUBJ_TYPE:
383 case AUDIT_SUBJ_SEN:
384 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600385 /* NOTE: this may return negative values indicating
386 a temporary error. We simply treat this as a
387 match for now to avoid losing information that
388 may be wanted. An error message will also be
389 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400390 if (f->se_rule) {
391 if (need_sid) {
Stephen Smalley62bac012006-09-25 23:31:56 -0700392 selinux_get_task_sid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400393 need_sid = 0;
394 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600395 result = selinux_audit_rule_match(sid, f->type,
396 f->op,
397 f->se_rule,
398 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400399 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600400 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500401 case AUDIT_OBJ_USER:
402 case AUDIT_OBJ_ROLE:
403 case AUDIT_OBJ_TYPE:
404 case AUDIT_OBJ_LEV_LOW:
405 case AUDIT_OBJ_LEV_HIGH:
406 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
407 also applies here */
408 if (f->se_rule) {
409 /* Find files that match */
410 if (name) {
411 result = selinux_audit_rule_match(
412 name->osid, f->type, f->op,
413 f->se_rule, ctx);
414 } else if (ctx) {
415 for (j = 0; j < ctx->name_count; j++) {
416 if (selinux_audit_rule_match(
417 ctx->names[j].osid,
418 f->type, f->op,
419 f->se_rule, ctx)) {
420 ++result;
421 break;
422 }
423 }
424 }
425 /* Find ipc objects that match */
426 if (ctx) {
427 struct audit_aux_data *aux;
428 for (aux = ctx->aux; aux;
429 aux = aux->next) {
430 if (aux->type == AUDIT_IPC) {
431 struct audit_aux_data_ipcctl *axi = (void *)aux;
432 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
433 ++result;
434 break;
435 }
436 }
437 }
438 }
439 }
440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 case AUDIT_ARG0:
442 case AUDIT_ARG1:
443 case AUDIT_ARG2:
444 case AUDIT_ARG3:
445 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500446 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400448 case AUDIT_FILTERKEY:
449 /* ignore this field for filtering */
450 result = 1;
451 break;
Al Viro55669bf2006-08-31 19:26:40 -0400452 case AUDIT_PERM:
453 result = audit_match_perm(ctx, f->val);
454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!result)
458 return 0;
459 }
Amy Griffis5adc8a62006-06-14 18:45:21 -0400460 if (rule->filterkey)
461 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 switch (rule->action) {
463 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
465 }
466 return 1;
467}
468
469/* At process creation time, we can determine if system-call auditing is
470 * completely disabled for this task. Since we only have the task
471 * structure at this point, we can only check uid and gid.
472 */
473static enum audit_state audit_filter_task(struct task_struct *tsk)
474{
475 struct audit_entry *e;
476 enum audit_state state;
477
478 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100479 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400480 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 rcu_read_unlock();
482 return state;
483 }
484 }
485 rcu_read_unlock();
486 return AUDIT_BUILD_CONTEXT;
487}
488
489/* At syscall entry and exit time, this filter is called if the
490 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100491 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700492 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 */
494static enum audit_state audit_filter_syscall(struct task_struct *tsk,
495 struct audit_context *ctx,
496 struct list_head *list)
497{
498 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100499 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
David Woodhouse351bb722005-07-14 14:40:06 +0100501 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100502 return AUDIT_DISABLED;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100505 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000506 int word = AUDIT_WORD(ctx->major);
507 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100508
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000509 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400510 if ((e->rule.mask[word] & bit) == bit &&
511 audit_filter_rules(tsk, &e->rule, ctx, NULL,
512 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000513 rcu_read_unlock();
514 return state;
515 }
516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 rcu_read_unlock();
519 return AUDIT_BUILD_CONTEXT;
520}
521
Amy Griffisf368c07d2006-04-07 16:55:56 -0400522/* At syscall exit time, this filter is called if any audit_names[] have been
523 * collected during syscall processing. We only check rules in sublists at hash
524 * buckets applicable to the inode numbers in audit_names[].
525 * Regarding audit_state, same rules apply as for audit_filter_syscall().
526 */
527enum audit_state audit_filter_inodes(struct task_struct *tsk,
528 struct audit_context *ctx)
529{
530 int i;
531 struct audit_entry *e;
532 enum audit_state state;
533
534 if (audit_pid && tsk->tgid == audit_pid)
535 return AUDIT_DISABLED;
536
537 rcu_read_lock();
538 for (i = 0; i < ctx->name_count; i++) {
539 int word = AUDIT_WORD(ctx->major);
540 int bit = AUDIT_BIT(ctx->major);
541 struct audit_names *n = &ctx->names[i];
542 int h = audit_hash_ino((u32)n->ino);
543 struct list_head *list = &audit_inode_hash[h];
544
545 if (list_empty(list))
546 continue;
547
548 list_for_each_entry_rcu(e, list, list) {
549 if ((e->rule.mask[word] & bit) == bit &&
550 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
551 rcu_read_unlock();
552 return state;
553 }
554 }
555 }
556 rcu_read_unlock();
557 return AUDIT_BUILD_CONTEXT;
558}
559
560void audit_set_auditable(struct audit_context *ctx)
561{
562 ctx->auditable = 1;
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static inline struct audit_context *audit_get_context(struct task_struct *tsk,
566 int return_valid,
567 int return_code)
568{
569 struct audit_context *context = tsk->audit_context;
570
571 if (likely(!context))
572 return NULL;
573 context->return_valid = return_valid;
574 context->return_code = return_code;
575
Al Virod51374a2006-08-03 10:59:26 -0400576 if (context->in_syscall && !context->dummy && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400578
David Woodhouse0f45aa12005-06-19 19:35:50 +0100579 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400580 if (state == AUDIT_RECORD_CONTEXT) {
581 context->auditable = 1;
582 goto get_context;
583 }
584
585 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (state == AUDIT_RECORD_CONTEXT)
587 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Amy Griffisf368c07d2006-04-07 16:55:56 -0400591get_context:
Al Viro3f2792f2006-07-16 06:43:48 -0400592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 tsk->audit_context = NULL;
594 return context;
595}
596
597static inline void audit_free_names(struct audit_context *context)
598{
599 int i;
600
601#if AUDIT_DEBUG == 2
602 if (context->auditable
603 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000604 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 " name_count=%d put_count=%d"
606 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000607 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 context->serial, context->major, context->in_syscall,
609 context->name_count, context->put_count,
610 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000611 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 printk(KERN_ERR "names[%d] = %p = %s\n", i,
613 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000614 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 dump_stack();
617 return;
618 }
619#endif
620#if AUDIT_DEBUG
621 context->put_count = 0;
622 context->ino_count = 0;
623#endif
624
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000625 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400626 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100630 if (context->pwd)
631 dput(context->pwd);
632 if (context->pwdmnt)
633 mntput(context->pwdmnt);
634 context->pwd = NULL;
635 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638static inline void audit_free_aux(struct audit_context *context)
639{
640 struct audit_aux_data *aux;
641
642 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100643 if (aux->type == AUDIT_AVC_PATH) {
644 struct audit_aux_data_path *axi = (void *)aux;
645 dput(axi->dentry);
646 mntput(axi->mnt);
647 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 context->aux = aux->next;
650 kfree(aux);
651 }
652}
653
654static inline void audit_zero_context(struct audit_context *context,
655 enum audit_state state)
656{
657 uid_t loginuid = context->loginuid;
658
659 memset(context, 0, sizeof(*context));
660 context->state = state;
661 context->loginuid = loginuid;
662}
663
664static inline struct audit_context *audit_alloc_context(enum audit_state state)
665{
666 struct audit_context *context;
667
668 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
669 return NULL;
670 audit_zero_context(context, state);
671 return context;
672}
673
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700674/**
675 * audit_alloc - allocate an audit context block for a task
676 * @tsk: task
677 *
678 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * if necessary. Doing so turns on system call auditing for the
680 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700681 * needed.
682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683int audit_alloc(struct task_struct *tsk)
684{
685 struct audit_context *context;
686 enum audit_state state;
687
688 if (likely(!audit_enabled))
689 return 0; /* Return if not auditing. */
690
691 state = audit_filter_task(tsk);
692 if (likely(state == AUDIT_DISABLED))
693 return 0;
694
695 if (!(context = audit_alloc_context(state))) {
696 audit_log_lost("out of memory in audit_alloc");
697 return -ENOMEM;
698 }
699
700 /* Preserve login uid */
701 context->loginuid = -1;
702 if (current->audit_context)
703 context->loginuid = current->audit_context->loginuid;
704
705 tsk->audit_context = context;
706 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
707 return 0;
708}
709
710static inline void audit_free_context(struct audit_context *context)
711{
712 struct audit_context *previous;
713 int count = 0;
714
715 do {
716 previous = context->previous;
717 if (previous || (count && count < 10)) {
718 ++count;
719 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
720 " freeing multiple contexts (%d)\n",
721 context->serial, context->major,
722 context->name_count, count);
723 }
724 audit_free_names(context);
725 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400726 kfree(context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 kfree(context);
728 context = previous;
729 } while (context);
730 if (count >= 10)
731 printk(KERN_ERR "audit: freed %d contexts\n", count);
732}
733
Joy Latten161a09e2006-11-27 13:11:54 -0600734void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000735{
736 char *ctx = NULL;
737 ssize_t len = 0;
738
739 len = security_getprocattr(current, "current", NULL, 0);
740 if (len < 0) {
741 if (len != -EINVAL)
742 goto error_path;
743 return;
744 }
745
Al Viroe4951492006-03-29 20:17:10 -0500746 ctx = kmalloc(len, GFP_KERNEL);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000747 if (!ctx)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000748 goto error_path;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000749
750 len = security_getprocattr(current, "current", ctx, len);
751 if (len < 0 )
752 goto error_path;
753
754 audit_log_format(ab, " subj=%s", ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000755 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000756
757error_path:
Jesper Juhl9a66a532006-06-27 02:55:05 -0700758 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000759 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000760 return;
761}
762
Joy Latten161a09e2006-11-27 13:11:54 -0600763EXPORT_SYMBOL(audit_log_task_context);
764
Al Viroe4951492006-03-29 20:17:10 -0500765static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700766{
Al Viro45d9bb02006-03-29 20:02:55 -0500767 char name[sizeof(tsk->comm)];
768 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700769 struct vm_area_struct *vma;
770
Al Viroe4951492006-03-29 20:17:10 -0500771 /* tsk == current */
772
Al Viro45d9bb02006-03-29 20:02:55 -0500773 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100774 audit_log_format(ab, " comm=");
775 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700776
Al Viroe4951492006-03-29 20:17:10 -0500777 if (mm) {
778 down_read(&mm->mmap_sem);
779 vma = mm->mmap;
780 while (vma) {
781 if ((vma->vm_flags & VM_EXECUTABLE) &&
782 vma->vm_file) {
783 audit_log_d_path(ab, "exe=",
784 vma->vm_file->f_dentry,
785 vma->vm_file->f_vfsmnt);
786 break;
787 }
788 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700789 }
Al Viroe4951492006-03-29 20:17:10 -0500790 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700791 }
Al Viroe4951492006-03-29 20:17:10 -0500792 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700793}
794
Al Viroe4951492006-03-29 20:17:10 -0500795static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500797 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100799 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500800 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Al Viroe4951492006-03-29 20:17:10 -0500802 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -0400803 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -0400804 if (!context->ppid)
805 context->ppid = sys_getppid();
Al Viro3f2792f2006-07-16 06:43:48 -0400806 context->uid = tsk->uid;
807 context->gid = tsk->gid;
808 context->euid = tsk->euid;
809 context->suid = tsk->suid;
810 context->fsuid = tsk->fsuid;
811 context->egid = tsk->egid;
812 context->sgid = tsk->sgid;
813 context->fsgid = tsk->fsgid;
814 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -0500815
816 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!ab)
818 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100819 audit_log_format(ab, "arch=%x syscall=%d",
820 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (context->personality != PER_LINUX)
822 audit_log_format(ab, " per=%lx", context->personality);
823 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100824 audit_log_format(ab, " success=%s exit=%ld",
825 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
826 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -0700827
828 mutex_lock(&tty_mutex);
Al Viro45d9bb02006-03-29 20:02:55 -0500829 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
830 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500831 else
832 tty = "(none)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 audit_log_format(ab,
834 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -0400835 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +0100836 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500837 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 context->argv[0],
839 context->argv[1],
840 context->argv[2],
841 context->argv[3],
842 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -0400843 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 context->pid,
845 context->loginuid,
846 context->uid,
847 context->gid,
848 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500849 context->egid, context->sgid, context->fsgid, tty);
Alan Coxeb84a202006-09-29 02:01:41 -0700850
851 mutex_unlock(&tty_mutex);
852
Al Viroe4951492006-03-29 20:17:10 -0500853 audit_log_task_info(ab, tsk);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400854 if (context->filterkey) {
855 audit_log_format(ab, " key=");
856 audit_log_untrustedstring(ab, context->filterkey);
857 } else
858 audit_log_format(ab, " key=(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
David Woodhouse7551ced2005-05-26 12:04:57 +0100861 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100862
Al Viroe4951492006-03-29 20:17:10 -0500863 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (!ab)
865 continue; /* audit_panic has been called */
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -0500868 case AUDIT_MQ_OPEN: {
869 struct audit_aux_data_mq_open *axi = (void *)aux;
870 audit_log_format(ab,
871 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
872 "mq_msgsize=%ld mq_curmsgs=%ld",
873 axi->oflag, axi->mode, axi->attr.mq_flags,
874 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
875 axi->attr.mq_curmsgs);
876 break; }
877
878 case AUDIT_MQ_SENDRECV: {
879 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
880 audit_log_format(ab,
881 "mqdes=%d msg_len=%zd msg_prio=%u "
882 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
883 axi->mqdes, axi->msg_len, axi->msg_prio,
884 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
885 break; }
886
887 case AUDIT_MQ_NOTIFY: {
888 struct audit_aux_data_mq_notify *axi = (void *)aux;
889 audit_log_format(ab,
890 "mqdes=%d sigev_signo=%d",
891 axi->mqdes,
892 axi->notification.sigev_signo);
893 break; }
894
895 case AUDIT_MQ_GETSETATTR: {
896 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
897 audit_log_format(ab,
898 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
899 "mq_curmsgs=%ld ",
900 axi->mqdes,
901 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
902 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
903 break; }
904
Steve Grubbc0404992005-05-13 18:17:42 +0100905 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct audit_aux_data_ipcctl *axi = (void *)aux;
907 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400908 "ouid=%u ogid=%u mode=%x",
909 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500910 if (axi->osid != 0) {
911 char *ctx = NULL;
912 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -0700913 if (selinux_sid_to_string(
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500914 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500915 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500916 axi->osid);
917 call_panic = 1;
918 } else
919 audit_log_format(ab, " obj=%s", ctx);
920 kfree(ctx);
921 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100922 break; }
923
Steve Grubb073115d2006-04-02 17:07:33 -0400924 case AUDIT_IPC_SET_PERM: {
925 struct audit_aux_data_ipcctl *axi = (void *)aux;
926 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400927 "qbytes=%lx ouid=%u ogid=%u mode=%x",
Steve Grubb073115d2006-04-02 17:07:33 -0400928 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400929 break; }
Linda Knippersac032212006-05-16 22:03:48 -0400930
Al Viro473ae302006-04-26 14:04:08 -0400931 case AUDIT_EXECVE: {
932 struct audit_aux_data_execve *axi = (void *)aux;
933 int i;
934 const char *p;
935 for (i = 0, p = axi->mem; i < axi->argc; i++) {
936 audit_log_format(ab, "a%d=", i);
937 p = audit_log_untrustedstring(ab, p);
938 audit_log_format(ab, "\n");
939 }
940 break; }
Steve Grubb073115d2006-04-02 17:07:33 -0400941
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100942 case AUDIT_SOCKETCALL: {
943 int i;
944 struct audit_aux_data_socketcall *axs = (void *)aux;
945 audit_log_format(ab, "nargs=%d", axs->nargs);
946 for (i=0; i<axs->nargs; i++)
947 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
948 break; }
949
950 case AUDIT_SOCKADDR: {
951 struct audit_aux_data_sockaddr *axs = (void *)aux;
952
953 audit_log_format(ab, "saddr=");
954 audit_log_hex(ab, axs->a, axs->len);
955 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100956
957 case AUDIT_AVC_PATH: {
958 struct audit_aux_data_path *axi = (void *)aux;
959 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100960 break; }
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965
David Woodhouse8f37d472005-05-27 12:17:28 +0100966 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -0500967 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100968 if (ab) {
969 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
970 audit_log_end(ab);
971 }
972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400974 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +0000975
Al Viroe4951492006-03-29 20:17:10 -0500976 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!ab)
978 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000981
Amy Griffis9c937dc2006-06-08 23:19:31 -0400982 if (n->name) {
983 switch(n->name_len) {
984 case AUDIT_NAME_FULL:
985 /* log the full path */
986 audit_log_format(ab, " name=");
987 audit_log_untrustedstring(ab, n->name);
988 break;
989 case 0:
990 /* name was specified as a relative path and the
991 * directory component is the cwd */
992 audit_log_d_path(ab, " name=", context->pwd,
993 context->pwdmnt);
994 break;
995 default:
996 /* log the name's directory component */
997 audit_log_format(ab, " name=");
998 audit_log_n_untrustedstring(ab, n->name_len,
999 n->name);
1000 }
1001 } else
1002 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001003
Amy Griffis9c937dc2006-06-08 23:19:31 -04001004 if (n->ino != (unsigned long)-1) {
1005 audit_log_format(ab, " inode=%lu"
1006 " dev=%02x:%02x mode=%#o"
1007 " ouid=%u ogid=%u rdev=%02x:%02x",
1008 n->ino,
1009 MAJOR(n->dev),
1010 MINOR(n->dev),
1011 n->mode,
1012 n->uid,
1013 n->gid,
1014 MAJOR(n->rdev),
1015 MINOR(n->rdev));
1016 }
1017 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001018 char *ctx = NULL;
1019 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -07001020 if (selinux_sid_to_string(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001021 n->osid, &ctx, &len)) {
1022 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001023 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -04001024 } else
1025 audit_log_format(ab, " obj=%s", ctx);
1026 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001027 }
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 audit_log_end(ab);
1030 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001031 if (call_panic)
1032 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001035/**
1036 * audit_free - free a per-task audit context
1037 * @tsk: task whose audit context block to free
1038 *
Al Virofa84cb92006-03-29 20:30:19 -05001039 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041void audit_free(struct task_struct *tsk)
1042{
1043 struct audit_context *context;
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (likely(!context))
1047 return;
1048
1049 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +01001050 * function (e.g., exit_group), then free context block.
1051 * We use GFP_ATOMIC here because we might be doing this
1052 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001053 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +01001054 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001055 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 audit_free_context(context);
1058}
1059
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001060/**
1061 * audit_syscall_entry - fill in an audit record at syscall entry
1062 * @tsk: task being audited
1063 * @arch: architecture type
1064 * @major: major syscall type (function)
1065 * @a1: additional syscall register 1
1066 * @a2: additional syscall register 2
1067 * @a3: additional syscall register 3
1068 * @a4: additional syscall register 4
1069 *
1070 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * audit context was created when the task was created and the state or
1072 * filters demand the audit context be built. If the state from the
1073 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1074 * then the record will be written at syscall exit time (otherwise, it
1075 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001076 * be written).
1077 */
Al Viro5411be52006-03-29 20:23:36 -05001078void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 unsigned long a1, unsigned long a2,
1080 unsigned long a3, unsigned long a4)
1081{
Al Viro5411be52006-03-29 20:23:36 -05001082 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct audit_context *context = tsk->audit_context;
1084 enum audit_state state;
1085
1086 BUG_ON(!context);
1087
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001088 /*
1089 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * calls in kernel_thread via the entry.S interface, instead of
1091 * with direct calls. (If you are porting to a new
1092 * architecture, hitting this condition can indicate that you
1093 * got the _exit/_leave calls backward in entry.S.)
1094 *
1095 * i386 no
1096 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001097 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 *
1099 * This also happens with vm86 emulation in a non-nested manner
1100 * (entries without exits), so this case must be caught.
1101 */
1102 if (context->in_syscall) {
1103 struct audit_context *newctx;
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105#if AUDIT_DEBUG
1106 printk(KERN_ERR
1107 "audit(:%d) pid=%d in syscall=%d;"
1108 " entering syscall=%d\n",
1109 context->serial, tsk->pid, context->major, major);
1110#endif
1111 newctx = audit_alloc_context(context->state);
1112 if (newctx) {
1113 newctx->previous = context;
1114 context = newctx;
1115 tsk->audit_context = newctx;
1116 } else {
1117 /* If we can't alloc a new context, the best we
1118 * can do is to leak memory (any pending putname
1119 * will be lost). The only other alternative is
1120 * to abandon auditing. */
1121 audit_zero_context(context, context->state);
1122 }
1123 }
1124 BUG_ON(context->in_syscall || context->name_count);
1125
1126 if (!audit_enabled)
1127 return;
1128
2fd6f582005-04-29 16:08:28 +01001129 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 context->major = major;
1131 context->argv[0] = a1;
1132 context->argv[1] = a2;
1133 context->argv[2] = a3;
1134 context->argv[3] = a4;
1135
1136 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001137 context->dummy = !audit_n_rules;
1138 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
David Woodhouse0f45aa12005-06-19 19:35:50 +01001139 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (likely(state == AUDIT_DISABLED))
1141 return;
1142
David Woodhousece625a82005-07-18 14:24:46 -04001143 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 context->ctime = CURRENT_TIME;
1145 context->in_syscall = 1;
1146 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
Alexander Viro419c58f2006-09-29 00:08:50 -04001147 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001150/**
1151 * audit_syscall_exit - deallocate audit context after a system call
1152 * @tsk: task being audited
1153 * @valid: success/failure flag
1154 * @return_code: syscall return value
1155 *
1156 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1158 * filtering, or because some other part of the kernel write an audit
1159 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001160 * free the names stored from getname().
1161 */
Al Viro5411be52006-03-29 20:23:36 -05001162void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Al Viro5411be52006-03-29 20:23:36 -05001164 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct audit_context *context;
1166
2fd6f582005-04-29 16:08:28 +01001167 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001170 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
David Woodhousef7056d62005-06-20 16:07:33 +01001172 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001173 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 context->in_syscall = 0;
1176 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (context->previous) {
1179 struct audit_context *new_context = context->previous;
1180 context->previous = NULL;
1181 audit_free_context(context);
1182 tsk->audit_context = new_context;
1183 } else {
1184 audit_free_names(context);
1185 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001186 kfree(context->filterkey);
1187 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 tsk->audit_context = context;
1189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001192/**
1193 * audit_getname - add a name to the list
1194 * @name: name to add
1195 *
1196 * Add a name to the list of audit names for this context.
1197 * Called from fs/namei.c:getname().
1198 */
Al Virod8945bb52006-05-18 16:01:30 -04001199void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct audit_context *context = current->audit_context;
1202
Al Virod8945bb52006-05-18 16:01:30 -04001203 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return;
1205
1206 if (!context->in_syscall) {
1207#if AUDIT_DEBUG == 2
1208 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1209 __FILE__, __LINE__, context->serial, name);
1210 dump_stack();
1211#endif
1212 return;
1213 }
1214 BUG_ON(context->name_count >= AUDIT_NAMES);
1215 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001216 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1217 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 context->names[context->name_count].ino = (unsigned long)-1;
1219 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001220 if (!context->pwd) {
1221 read_lock(&current->fs->lock);
1222 context->pwd = dget(current->fs->pwd);
1223 context->pwdmnt = mntget(current->fs->pwdmnt);
1224 read_unlock(&current->fs->lock);
1225 }
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001229/* audit_putname - intercept a putname request
1230 * @name: name to intercept and delay for putname
1231 *
1232 * If we have stored the name from getname in the audit context,
1233 * then we delay the putname until syscall exit.
1234 * Called from include/linux/fs.h:putname().
1235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236void audit_putname(const char *name)
1237{
1238 struct audit_context *context = current->audit_context;
1239
1240 BUG_ON(!context);
1241 if (!context->in_syscall) {
1242#if AUDIT_DEBUG == 2
1243 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1244 __FILE__, __LINE__, context->serial, name);
1245 if (context->name_count) {
1246 int i;
1247 for (i = 0; i < context->name_count; i++)
1248 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1249 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001250 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252#endif
1253 __putname(name);
1254 }
1255#if AUDIT_DEBUG
1256 else {
1257 ++context->put_count;
1258 if (context->put_count > context->name_count) {
1259 printk(KERN_ERR "%s:%d(:%d): major=%d"
1260 " in_syscall=%d putname(%p) name_count=%d"
1261 " put_count=%d\n",
1262 __FILE__, __LINE__,
1263 context->serial, context->major,
1264 context->in_syscall, name, context->name_count,
1265 context->put_count);
1266 dump_stack();
1267 }
1268 }
1269#endif
1270}
1271
Amy Griffis3e2efce2006-07-13 13:16:02 -04001272/* Copy inode data into an audit_names. */
1273static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001274{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001275 name->ino = inode->i_ino;
1276 name->dev = inode->i_sb->s_dev;
1277 name->mode = inode->i_mode;
1278 name->uid = inode->i_uid;
1279 name->gid = inode->i_gid;
1280 name->rdev = inode->i_rdev;
1281 selinux_get_inode_sid(inode, &name->osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001282}
1283
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001284/**
1285 * audit_inode - store the inode and device from a lookup
1286 * @name: name being audited
1287 * @inode: inode being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001288 *
1289 * Called from fs/namei.c:path_lookup().
1290 */
Amy Griffis9c937dc2006-06-08 23:19:31 -04001291void __audit_inode(const char *name, const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 int idx;
1294 struct audit_context *context = current->audit_context;
1295
1296 if (!context->in_syscall)
1297 return;
1298 if (context->name_count
1299 && context->names[context->name_count-1].name
1300 && context->names[context->name_count-1].name == name)
1301 idx = context->name_count - 1;
1302 else if (context->name_count > 1
1303 && context->names[context->name_count-2].name
1304 && context->names[context->name_count-2].name == name)
1305 idx = context->name_count - 2;
1306 else {
1307 /* FIXME: how much do we care about inodes that have no
1308 * associated name? */
1309 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1310 return;
1311 idx = context->name_count++;
1312 context->names[idx].name = NULL;
1313#if AUDIT_DEBUG
1314 ++context->ino_count;
1315#endif
1316 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001317 audit_copy_inode(&context->names[idx], inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
Amy Griffis73241cc2005-11-03 16:00:25 +00001320/**
1321 * audit_inode_child - collect inode info for created/removed objects
1322 * @dname: inode's dentry name
1323 * @inode: inode being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04001324 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00001325 *
1326 * For syscalls that create or remove filesystem objects, audit_inode
1327 * can only collect information for the filesystem object's parent.
1328 * This call updates the audit context with the child's information.
1329 * Syscalls that create a new filesystem object must be hooked after
1330 * the object is created. Syscalls that remove a filesystem object
1331 * must be hooked prior, in order to capture the target inode during
1332 * unsuccessful attempts.
1333 */
1334void __audit_inode_child(const char *dname, const struct inode *inode,
Amy Griffis73d3ec52006-07-13 13:16:39 -04001335 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00001336{
1337 int idx;
1338 struct audit_context *context = current->audit_context;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001339 const char *found_name = NULL;
1340 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00001341
1342 if (!context->in_syscall)
1343 return;
1344
1345 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001346 if (!dname)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001347 goto update_context;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001348 for (idx = 0; idx < context->name_count; idx++)
Amy Griffis73d3ec52006-07-13 13:16:39 -04001349 if (context->names[idx].ino == parent->i_ino) {
Amy Griffisf368c07d2006-04-07 16:55:56 -04001350 const char *name = context->names[idx].name;
Amy Griffis73241cc2005-11-03 16:00:25 +00001351
Amy Griffisf368c07d2006-04-07 16:55:56 -04001352 if (!name)
1353 continue;
Amy Griffis73241cc2005-11-03 16:00:25 +00001354
Amy Griffis9c937dc2006-06-08 23:19:31 -04001355 if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
1356 context->names[idx].name_len = dirlen;
1357 found_name = name;
1358 break;
1359 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001360 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001361
Amy Griffis9c937dc2006-06-08 23:19:31 -04001362update_context:
Steve Grubbac9910c2006-09-28 14:31:32 -04001363 idx = context->name_count;
1364 if (context->name_count == AUDIT_NAMES) {
1365 printk(KERN_DEBUG "name_count maxed and losing %s\n",
1366 found_name ?: "(null)");
1367 return;
1368 }
1369 context->name_count++;
Amy Griffis73241cc2005-11-03 16:00:25 +00001370#if AUDIT_DEBUG
1371 context->ino_count++;
1372#endif
Amy Griffis9c937dc2006-06-08 23:19:31 -04001373 /* Re-use the name belonging to the slot for a matching parent directory.
1374 * All names for this context are relinquished in audit_free_names() */
1375 context->names[idx].name = found_name;
1376 context->names[idx].name_len = AUDIT_NAME_FULL;
1377 context->names[idx].name_put = 0; /* don't call __putname() */
Amy Griffis73241cc2005-11-03 16:00:25 +00001378
Amy Griffis3e2efce2006-07-13 13:16:02 -04001379 if (!inode)
1380 context->names[idx].ino = (unsigned long)-1;
1381 else
1382 audit_copy_inode(&context->names[idx], inode);
Amy Griffis73d3ec52006-07-13 13:16:39 -04001383
1384 /* A parent was not found in audit_names, so copy the inode data for the
1385 * provided parent. */
1386 if (!found_name) {
Steve Grubbac9910c2006-09-28 14:31:32 -04001387 idx = context->name_count;
1388 if (context->name_count == AUDIT_NAMES) {
1389 printk(KERN_DEBUG
1390 "name_count maxed and losing parent inode data: dev=%02x:%02x, inode=%lu",
1391 MAJOR(parent->i_sb->s_dev),
1392 MINOR(parent->i_sb->s_dev),
1393 parent->i_ino);
1394 return;
1395 }
1396 context->name_count++;
Amy Griffis73d3ec52006-07-13 13:16:39 -04001397#if AUDIT_DEBUG
1398 context->ino_count++;
1399#endif
1400 audit_copy_inode(&context->names[idx], parent);
1401 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001402}
1403
1404/**
1405 * audit_inode_update - update inode info for last collected name
1406 * @inode: inode being audited
1407 *
1408 * When open() is called on an existing object with the O_CREAT flag, the inode
1409 * data audit initially collects is incorrect. This additional hook ensures
1410 * audit has the inode data for the actual object to be opened.
1411 */
1412void __audit_inode_update(const struct inode *inode)
1413{
1414 struct audit_context *context = current->audit_context;
1415 int idx;
1416
1417 if (!context->in_syscall || !inode)
1418 return;
1419
1420 if (context->name_count == 0) {
1421 context->name_count++;
1422#if AUDIT_DEBUG
1423 context->ino_count++;
1424#endif
1425 }
1426 idx = context->name_count - 1;
1427
1428 audit_copy_inode(&context->names[idx], inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001431/**
1432 * auditsc_get_stamp - get local copies of audit_context values
1433 * @ctx: audit_context for the task
1434 * @t: timespec to store time recorded in the audit_context
1435 * @serial: serial value that is recorded in the audit_context
1436 *
1437 * Also sets the context as auditable.
1438 */
David Woodhousebfb44962005-05-21 21:08:09 +01001439void auditsc_get_stamp(struct audit_context *ctx,
1440 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
David Woodhousece625a82005-07-18 14:24:46 -04001442 if (!ctx->serial)
1443 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001444 t->tv_sec = ctx->ctime.tv_sec;
1445 t->tv_nsec = ctx->ctime.tv_nsec;
1446 *serial = ctx->serial;
1447 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
1449
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001450/**
1451 * audit_set_loginuid - set a task's audit_context loginuid
1452 * @task: task whose audit context is being modified
1453 * @loginuid: loginuid value
1454 *
1455 * Returns 0.
1456 *
1457 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1458 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001459int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Steve Grubb41757102006-06-12 07:48:28 -04001461 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01001462
Steve Grubb41757102006-06-12 07:48:28 -04001463 if (context) {
1464 /* Only log if audit is enabled */
1465 if (context->in_syscall) {
1466 struct audit_buffer *ab;
1467
1468 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1469 if (ab) {
1470 audit_log_format(ab, "login pid=%d uid=%u "
1471 "old auid=%u new auid=%u",
1472 task->pid, task->uid,
1473 context->loginuid, loginuid);
1474 audit_log_end(ab);
1475 }
Steve Grubbc0404992005-05-13 18:17:42 +01001476 }
Steve Grubb41757102006-06-12 07:48:28 -04001477 context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479 return 0;
1480}
1481
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001482/**
1483 * audit_get_loginuid - get the loginuid for an audit_context
1484 * @ctx: the audit_context
1485 *
1486 * Returns the context's loginuid or -1 if @ctx is NULL.
1487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488uid_t audit_get_loginuid(struct audit_context *ctx)
1489{
1490 return ctx ? ctx->loginuid : -1;
1491}
1492
Joy Latten161a09e2006-11-27 13:11:54 -06001493EXPORT_SYMBOL(audit_get_loginuid);
1494
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001495/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05001496 * __audit_mq_open - record audit data for a POSIX MQ open
1497 * @oflag: open flag
1498 * @mode: mode bits
1499 * @u_attr: queue attributes
1500 *
1501 * Returns 0 for success or NULL context or < 0 on error.
1502 */
1503int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1504{
1505 struct audit_aux_data_mq_open *ax;
1506 struct audit_context *context = current->audit_context;
1507
1508 if (!audit_enabled)
1509 return 0;
1510
1511 if (likely(!context))
1512 return 0;
1513
1514 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1515 if (!ax)
1516 return -ENOMEM;
1517
1518 if (u_attr != NULL) {
1519 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1520 kfree(ax);
1521 return -EFAULT;
1522 }
1523 } else
1524 memset(&ax->attr, 0, sizeof(ax->attr));
1525
1526 ax->oflag = oflag;
1527 ax->mode = mode;
1528
1529 ax->d.type = AUDIT_MQ_OPEN;
1530 ax->d.next = context->aux;
1531 context->aux = (void *)ax;
1532 return 0;
1533}
1534
1535/**
1536 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1537 * @mqdes: MQ descriptor
1538 * @msg_len: Message length
1539 * @msg_prio: Message priority
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001540 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001541 *
1542 * Returns 0 for success or NULL context or < 0 on error.
1543 */
1544int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1545 const struct timespec __user *u_abs_timeout)
1546{
1547 struct audit_aux_data_mq_sendrecv *ax;
1548 struct audit_context *context = current->audit_context;
1549
1550 if (!audit_enabled)
1551 return 0;
1552
1553 if (likely(!context))
1554 return 0;
1555
1556 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1557 if (!ax)
1558 return -ENOMEM;
1559
1560 if (u_abs_timeout != NULL) {
1561 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1562 kfree(ax);
1563 return -EFAULT;
1564 }
1565 } else
1566 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1567
1568 ax->mqdes = mqdes;
1569 ax->msg_len = msg_len;
1570 ax->msg_prio = msg_prio;
1571
1572 ax->d.type = AUDIT_MQ_SENDRECV;
1573 ax->d.next = context->aux;
1574 context->aux = (void *)ax;
1575 return 0;
1576}
1577
1578/**
1579 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1580 * @mqdes: MQ descriptor
1581 * @msg_len: Message length
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001582 * @u_msg_prio: Message priority
1583 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001584 *
1585 * Returns 0 for success or NULL context or < 0 on error.
1586 */
1587int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1588 unsigned int __user *u_msg_prio,
1589 const struct timespec __user *u_abs_timeout)
1590{
1591 struct audit_aux_data_mq_sendrecv *ax;
1592 struct audit_context *context = current->audit_context;
1593
1594 if (!audit_enabled)
1595 return 0;
1596
1597 if (likely(!context))
1598 return 0;
1599
1600 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1601 if (!ax)
1602 return -ENOMEM;
1603
1604 if (u_msg_prio != NULL) {
1605 if (get_user(ax->msg_prio, u_msg_prio)) {
1606 kfree(ax);
1607 return -EFAULT;
1608 }
1609 } else
1610 ax->msg_prio = 0;
1611
1612 if (u_abs_timeout != NULL) {
1613 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1614 kfree(ax);
1615 return -EFAULT;
1616 }
1617 } else
1618 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1619
1620 ax->mqdes = mqdes;
1621 ax->msg_len = msg_len;
1622
1623 ax->d.type = AUDIT_MQ_SENDRECV;
1624 ax->d.next = context->aux;
1625 context->aux = (void *)ax;
1626 return 0;
1627}
1628
1629/**
1630 * __audit_mq_notify - record audit data for a POSIX MQ notify
1631 * @mqdes: MQ descriptor
1632 * @u_notification: Notification event
1633 *
1634 * Returns 0 for success or NULL context or < 0 on error.
1635 */
1636
1637int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1638{
1639 struct audit_aux_data_mq_notify *ax;
1640 struct audit_context *context = current->audit_context;
1641
1642 if (!audit_enabled)
1643 return 0;
1644
1645 if (likely(!context))
1646 return 0;
1647
1648 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1649 if (!ax)
1650 return -ENOMEM;
1651
1652 if (u_notification != NULL) {
1653 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1654 kfree(ax);
1655 return -EFAULT;
1656 }
1657 } else
1658 memset(&ax->notification, 0, sizeof(ax->notification));
1659
1660 ax->mqdes = mqdes;
1661
1662 ax->d.type = AUDIT_MQ_NOTIFY;
1663 ax->d.next = context->aux;
1664 context->aux = (void *)ax;
1665 return 0;
1666}
1667
1668/**
1669 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1670 * @mqdes: MQ descriptor
1671 * @mqstat: MQ flags
1672 *
1673 * Returns 0 for success or NULL context or < 0 on error.
1674 */
1675int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1676{
1677 struct audit_aux_data_mq_getsetattr *ax;
1678 struct audit_context *context = current->audit_context;
1679
1680 if (!audit_enabled)
1681 return 0;
1682
1683 if (likely(!context))
1684 return 0;
1685
1686 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1687 if (!ax)
1688 return -ENOMEM;
1689
1690 ax->mqdes = mqdes;
1691 ax->mqstat = *mqstat;
1692
1693 ax->d.type = AUDIT_MQ_GETSETATTR;
1694 ax->d.next = context->aux;
1695 context->aux = (void *)ax;
1696 return 0;
1697}
1698
1699/**
Steve Grubb073115d2006-04-02 17:07:33 -04001700 * audit_ipc_obj - record audit data for ipc object
1701 * @ipcp: ipc permissions
1702 *
1703 * Returns 0 for success or NULL context or < 0 on error.
1704 */
Al Virod8945bb52006-05-18 16:01:30 -04001705int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04001706{
1707 struct audit_aux_data_ipcctl *ax;
1708 struct audit_context *context = current->audit_context;
1709
Steve Grubb073115d2006-04-02 17:07:33 -04001710 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1711 if (!ax)
1712 return -ENOMEM;
1713
1714 ax->uid = ipcp->uid;
1715 ax->gid = ipcp->gid;
1716 ax->mode = ipcp->mode;
1717 selinux_get_ipc_sid(ipcp, &ax->osid);
1718
1719 ax->d.type = AUDIT_IPC;
1720 ax->d.next = context->aux;
1721 context->aux = (void *)ax;
1722 return 0;
1723}
1724
1725/**
1726 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001727 * @qbytes: msgq bytes
1728 * @uid: msgq user id
1729 * @gid: msgq group id
1730 * @mode: msgq mode (permissions)
1731 *
1732 * Returns 0 for success or NULL context or < 0 on error.
1733 */
Al Virod8945bb52006-05-18 16:01:30 -04001734int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 struct audit_aux_data_ipcctl *ax;
1737 struct audit_context *context = current->audit_context;
1738
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001739 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (!ax)
1741 return -ENOMEM;
1742
1743 ax->qbytes = qbytes;
1744 ax->uid = uid;
1745 ax->gid = gid;
1746 ax->mode = mode;
1747
Steve Grubb073115d2006-04-02 17:07:33 -04001748 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 ax->d.next = context->aux;
1750 context->aux = (void *)ax;
1751 return 0;
1752}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001753
Al Viro473ae302006-04-26 14:04:08 -04001754int audit_bprm(struct linux_binprm *bprm)
1755{
1756 struct audit_aux_data_execve *ax;
1757 struct audit_context *context = current->audit_context;
1758 unsigned long p, next;
1759 void *to;
1760
Al Viro5ac3a9c2006-07-16 06:38:45 -04001761 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04001762 return 0;
1763
1764 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1765 GFP_KERNEL);
1766 if (!ax)
1767 return -ENOMEM;
1768
1769 ax->argc = bprm->argc;
1770 ax->envc = bprm->envc;
1771 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1772 struct page *page = bprm->page[p / PAGE_SIZE];
1773 void *kaddr = kmap(page);
1774 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1775 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1776 to += next - p;
1777 kunmap(page);
1778 }
1779
1780 ax->d.type = AUDIT_EXECVE;
1781 ax->d.next = context->aux;
1782 context->aux = (void *)ax;
1783 return 0;
1784}
1785
1786
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001787/**
1788 * audit_socketcall - record audit data for sys_socketcall
1789 * @nargs: number of args
1790 * @args: args array
1791 *
1792 * Returns 0 for success or NULL context or < 0 on error.
1793 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001794int audit_socketcall(int nargs, unsigned long *args)
1795{
1796 struct audit_aux_data_socketcall *ax;
1797 struct audit_context *context = current->audit_context;
1798
Al Viro5ac3a9c2006-07-16 06:38:45 -04001799 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001800 return 0;
1801
1802 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1803 if (!ax)
1804 return -ENOMEM;
1805
1806 ax->nargs = nargs;
1807 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1808
1809 ax->d.type = AUDIT_SOCKETCALL;
1810 ax->d.next = context->aux;
1811 context->aux = (void *)ax;
1812 return 0;
1813}
1814
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001815/**
1816 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1817 * @len: data length in user space
1818 * @a: data address in kernel space
1819 *
1820 * Returns 0 for success or NULL context or < 0 on error.
1821 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001822int audit_sockaddr(int len, void *a)
1823{
1824 struct audit_aux_data_sockaddr *ax;
1825 struct audit_context *context = current->audit_context;
1826
Al Viro5ac3a9c2006-07-16 06:38:45 -04001827 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001828 return 0;
1829
1830 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1831 if (!ax)
1832 return -ENOMEM;
1833
1834 ax->len = len;
1835 memcpy(ax->a, a, len);
1836
1837 ax->d.type = AUDIT_SOCKADDR;
1838 ax->d.next = context->aux;
1839 context->aux = (void *)ax;
1840 return 0;
1841}
1842
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001843/**
1844 * audit_avc_path - record the granting or denial of permissions
1845 * @dentry: dentry to record
1846 * @mnt: mnt to record
1847 *
1848 * Returns 0 for success or NULL context or < 0 on error.
1849 *
1850 * Called from security/selinux/avc.c::avc_audit()
1851 */
Stephen Smalley01116102005-05-21 00:15:52 +01001852int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1853{
1854 struct audit_aux_data_path *ax;
1855 struct audit_context *context = current->audit_context;
1856
1857 if (likely(!context))
1858 return 0;
1859
1860 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1861 if (!ax)
1862 return -ENOMEM;
1863
1864 ax->dentry = dget(dentry);
1865 ax->mnt = mntget(mnt);
1866
1867 ax->d.type = AUDIT_AVC_PATH;
1868 ax->d.next = context->aux;
1869 context->aux = (void *)ax;
1870 return 0;
1871}
1872
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001873/**
1874 * audit_signal_info - record signal info for shutting down audit subsystem
1875 * @sig: signal value
1876 * @t: task being signaled
1877 *
1878 * If the audit subsystem is being terminated, record the task (pid)
1879 * and uid that is doing that.
1880 */
Al Viroe1396062006-05-25 10:19:47 -04001881void __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001882{
1883 extern pid_t audit_sig_pid;
1884 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04001885 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001886
Al Viroe1396062006-05-25 10:19:47 -04001887 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1888 struct task_struct *tsk = current;
1889 struct audit_context *ctx = tsk->audit_context;
1890 audit_sig_pid = tsk->pid;
1891 if (ctx)
1892 audit_sig_uid = ctx->loginuid;
1893 else
1894 audit_sig_uid = tsk->uid;
1895 selinux_get_task_sid(tsk, &audit_sig_sid);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001896 }
1897}