blob: 6aff0df7556833c6a0f1f797dbbc3047355f6480 [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
Amy Griffise54dc242007-03-29 18:01:04 -040092/* determines whether we collect data for signals sent */
93int audit_signals;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* When fs/namei.c:getname() is called, we store the pointer in name and
96 * we don't let putname() free it (instead we free all of the saved
97 * pointers at syscall exit time).
98 *
99 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
100struct audit_names {
101 const char *name;
Amy Griffis9c937dc2006-06-08 23:19:31 -0400102 int name_len; /* number of name's characters to log */
103 unsigned name_put; /* call __putname() for this name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long ino;
105 dev_t dev;
106 umode_t mode;
107 uid_t uid;
108 gid_t gid;
109 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400110 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
113struct audit_aux_data {
114 struct audit_aux_data *next;
115 int type;
116};
117
118#define AUDIT_AUX_IPCPERM 0
119
Amy Griffise54dc242007-03-29 18:01:04 -0400120/* Number of target pids per aux struct. */
121#define AUDIT_AUX_PIDS 16
122
George C. Wilson20ca73b2006-05-24 16:09:55 -0500123struct audit_aux_data_mq_open {
124 struct audit_aux_data d;
125 int oflag;
126 mode_t mode;
127 struct mq_attr attr;
128};
129
130struct audit_aux_data_mq_sendrecv {
131 struct audit_aux_data d;
132 mqd_t mqdes;
133 size_t msg_len;
134 unsigned int msg_prio;
135 struct timespec abs_timeout;
136};
137
138struct audit_aux_data_mq_notify {
139 struct audit_aux_data d;
140 mqd_t mqdes;
141 struct sigevent notification;
142};
143
144struct audit_aux_data_mq_getsetattr {
145 struct audit_aux_data d;
146 mqd_t mqdes;
147 struct mq_attr mqstat;
148};
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150struct audit_aux_data_ipcctl {
151 struct audit_aux_data d;
152 struct ipc_perm p;
153 unsigned long qbytes;
154 uid_t uid;
155 gid_t gid;
156 mode_t mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500157 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
Al Viro473ae302006-04-26 14:04:08 -0400160struct audit_aux_data_execve {
161 struct audit_aux_data d;
162 int argc;
163 int envc;
164 char mem[0];
165};
166
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100167struct audit_aux_data_socketcall {
168 struct audit_aux_data d;
169 int nargs;
170 unsigned long args[0];
171};
172
173struct audit_aux_data_sockaddr {
174 struct audit_aux_data d;
175 int len;
176 char a[0];
177};
178
Al Virodb349502007-02-07 01:48:00 -0500179struct audit_aux_data_fd_pair {
180 struct audit_aux_data d;
181 int fd[2];
182};
183
Stephen Smalley01116102005-05-21 00:15:52 +0100184struct audit_aux_data_path {
185 struct audit_aux_data d;
186 struct dentry *dentry;
187 struct vfsmount *mnt;
188};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Amy Griffise54dc242007-03-29 18:01:04 -0400190struct audit_aux_data_pids {
191 struct audit_aux_data d;
192 pid_t target_pid[AUDIT_AUX_PIDS];
193 u32 target_sid[AUDIT_AUX_PIDS];
194 int pid_count;
195};
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* The per-task audit context. */
198struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400199 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int in_syscall; /* 1 if task is in a syscall */
201 enum audit_state state;
202 unsigned int serial; /* serial number for record */
203 struct timespec ctime; /* time of syscall entry */
204 uid_t loginuid; /* login uid (identity) */
205 int major; /* syscall number */
206 unsigned long argv[4]; /* syscall arguments */
207 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100208 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int auditable; /* 1 if record should be written */
210 int name_count;
211 struct audit_names names[AUDIT_NAMES];
Amy Griffis5adc8a62006-06-14 18:45:21 -0400212 char * filterkey; /* key for rule that triggered record */
David Woodhouse8f37d472005-05-27 12:17:28 +0100213 struct dentry * pwd;
214 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct audit_context *previous; /* For nested syscalls */
216 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400217 struct audit_aux_data *aux_pids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400220 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 uid_t uid, euid, suid, fsuid;
222 gid_t gid, egid, sgid, fsgid;
223 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100224 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Al Viroa5cb0132007-03-20 13:58:35 -0400226 pid_t target_pid;
227 u32 target_sid;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#if AUDIT_DEBUG
230 int put_count;
231 int ino_count;
232#endif
233};
234
Al Viro55669bf2006-08-31 19:26:40 -0400235#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
236static inline int open_arg(int flags, int mask)
237{
238 int n = ACC_MODE(flags);
239 if (flags & (O_TRUNC | O_CREAT))
240 n |= AUDIT_PERM_WRITE;
241 return n & mask;
242}
243
244static int audit_match_perm(struct audit_context *ctx, int mask)
245{
246 unsigned n = ctx->major;
247 switch (audit_classify_syscall(ctx->arch, n)) {
248 case 0: /* native */
249 if ((mask & AUDIT_PERM_WRITE) &&
250 audit_match_class(AUDIT_CLASS_WRITE, n))
251 return 1;
252 if ((mask & AUDIT_PERM_READ) &&
253 audit_match_class(AUDIT_CLASS_READ, n))
254 return 1;
255 if ((mask & AUDIT_PERM_ATTR) &&
256 audit_match_class(AUDIT_CLASS_CHATTR, n))
257 return 1;
258 return 0;
259 case 1: /* 32bit on biarch */
260 if ((mask & AUDIT_PERM_WRITE) &&
261 audit_match_class(AUDIT_CLASS_WRITE_32, n))
262 return 1;
263 if ((mask & AUDIT_PERM_READ) &&
264 audit_match_class(AUDIT_CLASS_READ_32, n))
265 return 1;
266 if ((mask & AUDIT_PERM_ATTR) &&
267 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
268 return 1;
269 return 0;
270 case 2: /* open */
271 return mask & ACC_MODE(ctx->argv[1]);
272 case 3: /* openat */
273 return mask & ACC_MODE(ctx->argv[2]);
274 case 4: /* socketcall */
275 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
276 case 5: /* execve */
277 return mask & AUDIT_PERM_EXEC;
278 default:
279 return 0;
280 }
281}
282
Amy Griffisf368c07d2006-04-07 16:55:56 -0400283/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/* Compare a task_struct with an audit_rule. Return 1 on match, 0
285 * otherwise. */
286static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500287 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400289 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 enum audit_state *state)
291{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400292 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600293 u32 sid;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500296 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int result = 0;
298
Amy Griffis93315ed2006-02-07 12:05:27 -0500299 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500301 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
Al Viro3c662512006-05-06 08:26:27 -0400303 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400304 if (ctx) {
305 if (!ctx->ppid)
306 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400307 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400308 }
Al Viro3c662512006-05-06 08:26:27 -0400309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500311 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break;
313 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500314 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
316 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500317 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500320 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 break;
322 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500323 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500326 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500329 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 break;
331 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500332 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 break;
334 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500335 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 break;
2fd6f582005-04-29 16:08:28 +0100337 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000338 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500339 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 case AUDIT_EXIT:
343 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500344 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 break;
346 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100347 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500348 if (f->val)
349 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100350 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500351 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400355 if (name)
356 result = audit_comparator(MAJOR(name->dev),
357 f->op, f->val);
358 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500360 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 ++result;
362 break;
363 }
364 }
365 }
366 break;
367 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400368 if (name)
369 result = audit_comparator(MINOR(name->dev),
370 f->op, f->val);
371 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500373 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ++result;
375 break;
376 }
377 }
378 }
379 break;
380 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400381 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400382 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400383 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400385 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 ++result;
387 break;
388 }
389 }
390 }
391 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400392 case AUDIT_WATCH:
393 if (name && rule->watch->ino != (unsigned long)-1)
394 result = (name->dev == rule->watch->dev &&
Amy Griffis9c937dc2006-06-08 23:19:31 -0400395 name->ino == rule->watch->ino);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 case AUDIT_LOGINUID:
398 result = 0;
399 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500400 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500402 case AUDIT_SUBJ_USER:
403 case AUDIT_SUBJ_ROLE:
404 case AUDIT_SUBJ_TYPE:
405 case AUDIT_SUBJ_SEN:
406 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600407 /* NOTE: this may return negative values indicating
408 a temporary error. We simply treat this as a
409 match for now to avoid losing information that
410 may be wanted. An error message will also be
411 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400412 if (f->se_rule) {
413 if (need_sid) {
Stephen Smalley62bac012006-09-25 23:31:56 -0700414 selinux_get_task_sid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400415 need_sid = 0;
416 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600417 result = selinux_audit_rule_match(sid, f->type,
418 f->op,
419 f->se_rule,
420 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400421 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600422 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500423 case AUDIT_OBJ_USER:
424 case AUDIT_OBJ_ROLE:
425 case AUDIT_OBJ_TYPE:
426 case AUDIT_OBJ_LEV_LOW:
427 case AUDIT_OBJ_LEV_HIGH:
428 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
429 also applies here */
430 if (f->se_rule) {
431 /* Find files that match */
432 if (name) {
433 result = selinux_audit_rule_match(
434 name->osid, f->type, f->op,
435 f->se_rule, ctx);
436 } else if (ctx) {
437 for (j = 0; j < ctx->name_count; j++) {
438 if (selinux_audit_rule_match(
439 ctx->names[j].osid,
440 f->type, f->op,
441 f->se_rule, ctx)) {
442 ++result;
443 break;
444 }
445 }
446 }
447 /* Find ipc objects that match */
448 if (ctx) {
449 struct audit_aux_data *aux;
450 for (aux = ctx->aux; aux;
451 aux = aux->next) {
452 if (aux->type == AUDIT_IPC) {
453 struct audit_aux_data_ipcctl *axi = (void *)aux;
454 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
455 ++result;
456 break;
457 }
458 }
459 }
460 }
461 }
462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 case AUDIT_ARG0:
464 case AUDIT_ARG1:
465 case AUDIT_ARG2:
466 case AUDIT_ARG3:
467 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500468 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400470 case AUDIT_FILTERKEY:
471 /* ignore this field for filtering */
472 result = 1;
473 break;
Al Viro55669bf2006-08-31 19:26:40 -0400474 case AUDIT_PERM:
475 result = audit_match_perm(ctx, f->val);
476 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (!result)
480 return 0;
481 }
Amy Griffis5adc8a62006-06-14 18:45:21 -0400482 if (rule->filterkey)
483 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 switch (rule->action) {
485 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
487 }
488 return 1;
489}
490
491/* At process creation time, we can determine if system-call auditing is
492 * completely disabled for this task. Since we only have the task
493 * structure at this point, we can only check uid and gid.
494 */
495static enum audit_state audit_filter_task(struct task_struct *tsk)
496{
497 struct audit_entry *e;
498 enum audit_state state;
499
500 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100501 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400502 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 rcu_read_unlock();
504 return state;
505 }
506 }
507 rcu_read_unlock();
508 return AUDIT_BUILD_CONTEXT;
509}
510
511/* At syscall entry and exit time, this filter is called if the
512 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100513 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700514 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
516static enum audit_state audit_filter_syscall(struct task_struct *tsk,
517 struct audit_context *ctx,
518 struct list_head *list)
519{
520 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100521 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
David Woodhouse351bb722005-07-14 14:40:06 +0100523 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100524 return AUDIT_DISABLED;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100527 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000528 int word = AUDIT_WORD(ctx->major);
529 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100530
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000531 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400532 if ((e->rule.mask[word] & bit) == bit &&
533 audit_filter_rules(tsk, &e->rule, ctx, NULL,
534 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000535 rcu_read_unlock();
536 return state;
537 }
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 rcu_read_unlock();
541 return AUDIT_BUILD_CONTEXT;
542}
543
Amy Griffisf368c07d2006-04-07 16:55:56 -0400544/* At syscall exit time, this filter is called if any audit_names[] have been
545 * collected during syscall processing. We only check rules in sublists at hash
546 * buckets applicable to the inode numbers in audit_names[].
547 * Regarding audit_state, same rules apply as for audit_filter_syscall().
548 */
549enum audit_state audit_filter_inodes(struct task_struct *tsk,
550 struct audit_context *ctx)
551{
552 int i;
553 struct audit_entry *e;
554 enum audit_state state;
555
556 if (audit_pid && tsk->tgid == audit_pid)
557 return AUDIT_DISABLED;
558
559 rcu_read_lock();
560 for (i = 0; i < ctx->name_count; i++) {
561 int word = AUDIT_WORD(ctx->major);
562 int bit = AUDIT_BIT(ctx->major);
563 struct audit_names *n = &ctx->names[i];
564 int h = audit_hash_ino((u32)n->ino);
565 struct list_head *list = &audit_inode_hash[h];
566
567 if (list_empty(list))
568 continue;
569
570 list_for_each_entry_rcu(e, list, list) {
571 if ((e->rule.mask[word] & bit) == bit &&
572 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
573 rcu_read_unlock();
574 return state;
575 }
576 }
577 }
578 rcu_read_unlock();
579 return AUDIT_BUILD_CONTEXT;
580}
581
582void audit_set_auditable(struct audit_context *ctx)
583{
584 ctx->auditable = 1;
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static inline struct audit_context *audit_get_context(struct task_struct *tsk,
588 int return_valid,
589 int return_code)
590{
591 struct audit_context *context = tsk->audit_context;
592
593 if (likely(!context))
594 return NULL;
595 context->return_valid = return_valid;
596 context->return_code = return_code;
597
Al Virod51374a2006-08-03 10:59:26 -0400598 if (context->in_syscall && !context->dummy && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400600
David Woodhouse0f45aa12005-06-19 19:35:50 +0100601 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400602 if (state == AUDIT_RECORD_CONTEXT) {
603 context->auditable = 1;
604 goto get_context;
605 }
606
607 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (state == AUDIT_RECORD_CONTEXT)
609 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Amy Griffisf368c07d2006-04-07 16:55:56 -0400613get_context:
Al Viro3f2792f2006-07-16 06:43:48 -0400614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 tsk->audit_context = NULL;
616 return context;
617}
618
619static inline void audit_free_names(struct audit_context *context)
620{
621 int i;
622
623#if AUDIT_DEBUG == 2
624 if (context->auditable
625 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000626 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 " name_count=%d put_count=%d"
628 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000629 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 context->serial, context->major, context->in_syscall,
631 context->name_count, context->put_count,
632 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000633 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 printk(KERN_ERR "names[%d] = %p = %s\n", i,
635 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000636 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 dump_stack();
639 return;
640 }
641#endif
642#if AUDIT_DEBUG
643 context->put_count = 0;
644 context->ino_count = 0;
645#endif
646
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000647 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400648 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100652 if (context->pwd)
653 dput(context->pwd);
654 if (context->pwdmnt)
655 mntput(context->pwdmnt);
656 context->pwd = NULL;
657 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660static inline void audit_free_aux(struct audit_context *context)
661{
662 struct audit_aux_data *aux;
663
664 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100665 if (aux->type == AUDIT_AVC_PATH) {
666 struct audit_aux_data_path *axi = (void *)aux;
667 dput(axi->dentry);
668 mntput(axi->mnt);
669 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 context->aux = aux->next;
672 kfree(aux);
673 }
Amy Griffise54dc242007-03-29 18:01:04 -0400674 while ((aux = context->aux_pids)) {
675 context->aux_pids = aux->next;
676 kfree(aux);
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680static inline void audit_zero_context(struct audit_context *context,
681 enum audit_state state)
682{
683 uid_t loginuid = context->loginuid;
684
685 memset(context, 0, sizeof(*context));
686 context->state = state;
687 context->loginuid = loginuid;
688}
689
690static inline struct audit_context *audit_alloc_context(enum audit_state state)
691{
692 struct audit_context *context;
693
694 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
695 return NULL;
696 audit_zero_context(context, state);
697 return context;
698}
699
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700700/**
701 * audit_alloc - allocate an audit context block for a task
702 * @tsk: task
703 *
704 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * if necessary. Doing so turns on system call auditing for the
706 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700707 * needed.
708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709int audit_alloc(struct task_struct *tsk)
710{
711 struct audit_context *context;
712 enum audit_state state;
713
714 if (likely(!audit_enabled))
715 return 0; /* Return if not auditing. */
716
717 state = audit_filter_task(tsk);
718 if (likely(state == AUDIT_DISABLED))
719 return 0;
720
721 if (!(context = audit_alloc_context(state))) {
722 audit_log_lost("out of memory in audit_alloc");
723 return -ENOMEM;
724 }
725
726 /* Preserve login uid */
727 context->loginuid = -1;
728 if (current->audit_context)
729 context->loginuid = current->audit_context->loginuid;
730
731 tsk->audit_context = context;
732 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
733 return 0;
734}
735
736static inline void audit_free_context(struct audit_context *context)
737{
738 struct audit_context *previous;
739 int count = 0;
740
741 do {
742 previous = context->previous;
743 if (previous || (count && count < 10)) {
744 ++count;
745 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
746 " freeing multiple contexts (%d)\n",
747 context->serial, context->major,
748 context->name_count, count);
749 }
750 audit_free_names(context);
751 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400752 kfree(context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 kfree(context);
754 context = previous;
755 } while (context);
756 if (count >= 10)
757 printk(KERN_ERR "audit: freed %d contexts\n", count);
758}
759
Joy Latten161a09e2006-11-27 13:11:54 -0600760void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000761{
762 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000763 unsigned len;
764 int error;
765 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000766
Al Viroc4823bc2007-03-12 16:17:42 +0000767 selinux_get_task_sid(current, &sid);
768 if (!sid)
769 return;
770
771 error = selinux_sid_to_string(sid, &ctx, &len);
772 if (error) {
773 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000774 goto error_path;
775 return;
776 }
777
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000778 audit_log_format(ab, " subj=%s", ctx);
Al Viroc4823bc2007-03-12 16:17:42 +0000779 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000780 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000781
782error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000783 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000784 return;
785}
786
Joy Latten161a09e2006-11-27 13:11:54 -0600787EXPORT_SYMBOL(audit_log_task_context);
788
Al Viroe4951492006-03-29 20:17:10 -0500789static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700790{
Al Viro45d9bb02006-03-29 20:02:55 -0500791 char name[sizeof(tsk->comm)];
792 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700793 struct vm_area_struct *vma;
794
Al Viroe4951492006-03-29 20:17:10 -0500795 /* tsk == current */
796
Al Viro45d9bb02006-03-29 20:02:55 -0500797 get_task_comm(name, tsk);
David Woodhouse99e45eea2005-05-23 21:57:41 +0100798 audit_log_format(ab, " comm=");
799 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700800
Al Viroe4951492006-03-29 20:17:10 -0500801 if (mm) {
802 down_read(&mm->mmap_sem);
803 vma = mm->mmap;
804 while (vma) {
805 if ((vma->vm_flags & VM_EXECUTABLE) &&
806 vma->vm_file) {
807 audit_log_d_path(ab, "exe=",
Josef Sipeka7a005f2006-12-08 02:37:17 -0800808 vma->vm_file->f_path.dentry,
809 vma->vm_file->f_path.mnt);
Al Viroe4951492006-03-29 20:17:10 -0500810 break;
811 }
812 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700813 }
Al Viroe4951492006-03-29 20:17:10 -0500814 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700815 }
Al Viroe4951492006-03-29 20:17:10 -0500816 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700817}
818
Amy Griffise54dc242007-03-29 18:01:04 -0400819static int audit_log_pid_context(struct audit_context *context, pid_t pid,
820 u32 sid)
821{
822 struct audit_buffer *ab;
823 char *s = NULL;
824 u32 len;
825 int rc = 0;
826
827 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
828 if (!ab)
829 return 1;
830
831 if (selinux_sid_to_string(sid, &s, &len)) {
832 audit_log_format(ab, "opid=%d obj=(none)", pid);
833 rc = 1;
834 } else
835 audit_log_format(ab, "opid=%d obj=%s", pid, s);
836 audit_log_end(ab);
837 kfree(s);
838
839 return rc;
840}
841
Al Viroe4951492006-03-29 20:17:10 -0500842static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500844 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100846 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500847 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Al Viroe4951492006-03-29 20:17:10 -0500849 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -0400850 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -0400851 if (!context->ppid)
852 context->ppid = sys_getppid();
Al Viro3f2792f2006-07-16 06:43:48 -0400853 context->uid = tsk->uid;
854 context->gid = tsk->gid;
855 context->euid = tsk->euid;
856 context->suid = tsk->suid;
857 context->fsuid = tsk->fsuid;
858 context->egid = tsk->egid;
859 context->sgid = tsk->sgid;
860 context->fsgid = tsk->fsgid;
861 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -0500862
863 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (!ab)
865 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100866 audit_log_format(ab, "arch=%x syscall=%d",
867 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (context->personality != PER_LINUX)
869 audit_log_format(ab, " per=%lx", context->personality);
870 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100871 audit_log_format(ab, " success=%s exit=%ld",
872 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
873 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -0700874
875 mutex_lock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800876 read_lock(&tasklist_lock);
Al Viro45d9bb02006-03-29 20:02:55 -0500877 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
878 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500879 else
880 tty = "(none)";
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800881 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 audit_log_format(ab,
883 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -0400884 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +0100885 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500886 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 context->argv[0],
888 context->argv[1],
889 context->argv[2],
890 context->argv[3],
891 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -0400892 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 context->pid,
894 context->loginuid,
895 context->uid,
896 context->gid,
897 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500898 context->egid, context->sgid, context->fsgid, tty);
Alan Coxeb84a202006-09-29 02:01:41 -0700899
900 mutex_unlock(&tty_mutex);
901
Al Viroe4951492006-03-29 20:17:10 -0500902 audit_log_task_info(ab, tsk);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400903 if (context->filterkey) {
904 audit_log_format(ab, " key=");
905 audit_log_untrustedstring(ab, context->filterkey);
906 } else
907 audit_log_format(ab, " key=(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
David Woodhouse7551ced2005-05-26 12:04:57 +0100910 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100911
Al Viroe4951492006-03-29 20:17:10 -0500912 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (!ab)
914 continue; /* audit_panic has been called */
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -0500917 case AUDIT_MQ_OPEN: {
918 struct audit_aux_data_mq_open *axi = (void *)aux;
919 audit_log_format(ab,
920 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
921 "mq_msgsize=%ld mq_curmsgs=%ld",
922 axi->oflag, axi->mode, axi->attr.mq_flags,
923 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
924 axi->attr.mq_curmsgs);
925 break; }
926
927 case AUDIT_MQ_SENDRECV: {
928 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
929 audit_log_format(ab,
930 "mqdes=%d msg_len=%zd msg_prio=%u "
931 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
932 axi->mqdes, axi->msg_len, axi->msg_prio,
933 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
934 break; }
935
936 case AUDIT_MQ_NOTIFY: {
937 struct audit_aux_data_mq_notify *axi = (void *)aux;
938 audit_log_format(ab,
939 "mqdes=%d sigev_signo=%d",
940 axi->mqdes,
941 axi->notification.sigev_signo);
942 break; }
943
944 case AUDIT_MQ_GETSETATTR: {
945 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
946 audit_log_format(ab,
947 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
948 "mq_curmsgs=%ld ",
949 axi->mqdes,
950 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
951 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
952 break; }
953
Steve Grubbc0404992005-05-13 18:17:42 +0100954 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 struct audit_aux_data_ipcctl *axi = (void *)aux;
956 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400957 "ouid=%u ogid=%u mode=%x",
958 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500959 if (axi->osid != 0) {
960 char *ctx = NULL;
961 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -0700962 if (selinux_sid_to_string(
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500963 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500964 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500965 axi->osid);
966 call_panic = 1;
967 } else
968 audit_log_format(ab, " obj=%s", ctx);
969 kfree(ctx);
970 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100971 break; }
972
Steve Grubb073115d2006-04-02 17:07:33 -0400973 case AUDIT_IPC_SET_PERM: {
974 struct audit_aux_data_ipcctl *axi = (void *)aux;
975 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400976 "qbytes=%lx ouid=%u ogid=%u mode=%x",
Steve Grubb073115d2006-04-02 17:07:33 -0400977 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400978 break; }
Linda Knippersac032212006-05-16 22:03:48 -0400979
Al Viro473ae302006-04-26 14:04:08 -0400980 case AUDIT_EXECVE: {
981 struct audit_aux_data_execve *axi = (void *)aux;
982 int i;
983 const char *p;
984 for (i = 0, p = axi->mem; i < axi->argc; i++) {
985 audit_log_format(ab, "a%d=", i);
986 p = audit_log_untrustedstring(ab, p);
987 audit_log_format(ab, "\n");
988 }
989 break; }
Steve Grubb073115d2006-04-02 17:07:33 -0400990
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100991 case AUDIT_SOCKETCALL: {
992 int i;
993 struct audit_aux_data_socketcall *axs = (void *)aux;
994 audit_log_format(ab, "nargs=%d", axs->nargs);
995 for (i=0; i<axs->nargs; i++)
996 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
997 break; }
998
999 case AUDIT_SOCKADDR: {
1000 struct audit_aux_data_sockaddr *axs = (void *)aux;
1001
1002 audit_log_format(ab, "saddr=");
1003 audit_log_hex(ab, axs->a, axs->len);
1004 break; }
Stephen Smalley01116102005-05-21 00:15:52 +01001005
1006 case AUDIT_AVC_PATH: {
1007 struct audit_aux_data_path *axi = (void *)aux;
1008 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +01001009 break; }
1010
Al Virodb349502007-02-07 01:48:00 -05001011 case AUDIT_FD_PAIR: {
1012 struct audit_aux_data_fd_pair *axs = (void *)aux;
1013 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1014 break; }
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
Amy Griffise54dc242007-03-29 18:01:04 -04001020 for (aux = context->aux_pids; aux; aux = aux->next) {
1021 struct audit_aux_data_pids *axs = (void *)aux;
1022 int i;
1023
1024 for (i = 0; i < axs->pid_count; i++)
1025 if (audit_log_pid_context(context, axs->target_pid[i],
1026 axs->target_sid[i]))
1027 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001028 }
1029
Amy Griffise54dc242007-03-29 18:01:04 -04001030 if (context->target_pid &&
1031 audit_log_pid_context(context, context->target_pid,
1032 context->target_sid))
1033 call_panic = 1;
1034
David Woodhouse8f37d472005-05-27 12:17:28 +01001035 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -05001036 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001037 if (ab) {
1038 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1039 audit_log_end(ab);
1040 }
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -04001043 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +00001044
Al Viroe4951492006-03-29 20:17:10 -05001045 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (!ab)
1047 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +01001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +00001050
Amy Griffis9c937dc2006-06-08 23:19:31 -04001051 if (n->name) {
1052 switch(n->name_len) {
1053 case AUDIT_NAME_FULL:
1054 /* log the full path */
1055 audit_log_format(ab, " name=");
1056 audit_log_untrustedstring(ab, n->name);
1057 break;
1058 case 0:
1059 /* name was specified as a relative path and the
1060 * directory component is the cwd */
1061 audit_log_d_path(ab, " name=", context->pwd,
1062 context->pwdmnt);
1063 break;
1064 default:
1065 /* log the name's directory component */
1066 audit_log_format(ab, " name=");
1067 audit_log_n_untrustedstring(ab, n->name_len,
1068 n->name);
1069 }
1070 } else
1071 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001072
Amy Griffis9c937dc2006-06-08 23:19:31 -04001073 if (n->ino != (unsigned long)-1) {
1074 audit_log_format(ab, " inode=%lu"
1075 " dev=%02x:%02x mode=%#o"
1076 " ouid=%u ogid=%u rdev=%02x:%02x",
1077 n->ino,
1078 MAJOR(n->dev),
1079 MINOR(n->dev),
1080 n->mode,
1081 n->uid,
1082 n->gid,
1083 MAJOR(n->rdev),
1084 MINOR(n->rdev));
1085 }
1086 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001087 char *ctx = NULL;
1088 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -07001089 if (selinux_sid_to_string(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001090 n->osid, &ctx, &len)) {
1091 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001092 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -04001093 } else
1094 audit_log_format(ab, " obj=%s", ctx);
1095 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001096 }
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 audit_log_end(ab);
1099 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001100 if (call_panic)
1101 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001104/**
1105 * audit_free - free a per-task audit context
1106 * @tsk: task whose audit context block to free
1107 *
Al Virofa84cb92006-03-29 20:30:19 -05001108 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110void audit_free(struct task_struct *tsk)
1111{
1112 struct audit_context *context;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (likely(!context))
1116 return;
1117
1118 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +01001119 * function (e.g., exit_group), then free context block.
1120 * We use GFP_ATOMIC here because we might be doing this
1121 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001122 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +01001123 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001124 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 audit_free_context(context);
1127}
1128
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001129/**
1130 * audit_syscall_entry - fill in an audit record at syscall entry
1131 * @tsk: task being audited
1132 * @arch: architecture type
1133 * @major: major syscall type (function)
1134 * @a1: additional syscall register 1
1135 * @a2: additional syscall register 2
1136 * @a3: additional syscall register 3
1137 * @a4: additional syscall register 4
1138 *
1139 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * audit context was created when the task was created and the state or
1141 * filters demand the audit context be built. If the state from the
1142 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1143 * then the record will be written at syscall exit time (otherwise, it
1144 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001145 * be written).
1146 */
Al Viro5411be52006-03-29 20:23:36 -05001147void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 unsigned long a1, unsigned long a2,
1149 unsigned long a3, unsigned long a4)
1150{
Al Viro5411be52006-03-29 20:23:36 -05001151 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct audit_context *context = tsk->audit_context;
1153 enum audit_state state;
1154
1155 BUG_ON(!context);
1156
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001157 /*
1158 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * calls in kernel_thread via the entry.S interface, instead of
1160 * with direct calls. (If you are porting to a new
1161 * architecture, hitting this condition can indicate that you
1162 * got the _exit/_leave calls backward in entry.S.)
1163 *
1164 * i386 no
1165 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001166 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 *
1168 * This also happens with vm86 emulation in a non-nested manner
1169 * (entries without exits), so this case must be caught.
1170 */
1171 if (context->in_syscall) {
1172 struct audit_context *newctx;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174#if AUDIT_DEBUG
1175 printk(KERN_ERR
1176 "audit(:%d) pid=%d in syscall=%d;"
1177 " entering syscall=%d\n",
1178 context->serial, tsk->pid, context->major, major);
1179#endif
1180 newctx = audit_alloc_context(context->state);
1181 if (newctx) {
1182 newctx->previous = context;
1183 context = newctx;
1184 tsk->audit_context = newctx;
1185 } else {
1186 /* If we can't alloc a new context, the best we
1187 * can do is to leak memory (any pending putname
1188 * will be lost). The only other alternative is
1189 * to abandon auditing. */
1190 audit_zero_context(context, context->state);
1191 }
1192 }
1193 BUG_ON(context->in_syscall || context->name_count);
1194
1195 if (!audit_enabled)
1196 return;
1197
2fd6f582005-04-29 16:08:28 +01001198 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 context->major = major;
1200 context->argv[0] = a1;
1201 context->argv[1] = a2;
1202 context->argv[2] = a3;
1203 context->argv[3] = a4;
1204
1205 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001206 context->dummy = !audit_n_rules;
1207 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
David Woodhouse0f45aa12005-06-19 19:35:50 +01001208 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (likely(state == AUDIT_DISABLED))
1210 return;
1211
David Woodhousece625a82005-07-18 14:24:46 -04001212 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 context->ctime = CURRENT_TIME;
1214 context->in_syscall = 1;
1215 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
Alexander Viro419c58f2006-09-29 00:08:50 -04001216 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001219/**
1220 * audit_syscall_exit - deallocate audit context after a system call
1221 * @tsk: task being audited
1222 * @valid: success/failure flag
1223 * @return_code: syscall return value
1224 *
1225 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1227 * filtering, or because some other part of the kernel write an audit
1228 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001229 * free the names stored from getname().
1230 */
Al Viro5411be52006-03-29 20:23:36 -05001231void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Al Viro5411be52006-03-29 20:23:36 -05001233 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct audit_context *context;
1235
2fd6f582005-04-29 16:08:28 +01001236 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001239 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
David Woodhousef7056d62005-06-20 16:07:33 +01001241 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001242 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 context->in_syscall = 0;
1245 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (context->previous) {
1248 struct audit_context *new_context = context->previous;
1249 context->previous = NULL;
1250 audit_free_context(context);
1251 tsk->audit_context = new_context;
1252 } else {
1253 audit_free_names(context);
1254 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001255 context->aux = NULL;
1256 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001257 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001258 context->target_sid = 0;
Amy Griffis5adc8a62006-06-14 18:45:21 -04001259 kfree(context->filterkey);
1260 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 tsk->audit_context = context;
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001265/**
1266 * audit_getname - add a name to the list
1267 * @name: name to add
1268 *
1269 * Add a name to the list of audit names for this context.
1270 * Called from fs/namei.c:getname().
1271 */
Al Virod8945bb52006-05-18 16:01:30 -04001272void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 struct audit_context *context = current->audit_context;
1275
Al Virod8945bb52006-05-18 16:01:30 -04001276 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return;
1278
1279 if (!context->in_syscall) {
1280#if AUDIT_DEBUG == 2
1281 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1282 __FILE__, __LINE__, context->serial, name);
1283 dump_stack();
1284#endif
1285 return;
1286 }
1287 BUG_ON(context->name_count >= AUDIT_NAMES);
1288 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001289 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1290 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 context->names[context->name_count].ino = (unsigned long)-1;
1292 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001293 if (!context->pwd) {
1294 read_lock(&current->fs->lock);
1295 context->pwd = dget(current->fs->pwd);
1296 context->pwdmnt = mntget(current->fs->pwdmnt);
1297 read_unlock(&current->fs->lock);
1298 }
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001302/* audit_putname - intercept a putname request
1303 * @name: name to intercept and delay for putname
1304 *
1305 * If we have stored the name from getname in the audit context,
1306 * then we delay the putname until syscall exit.
1307 * Called from include/linux/fs.h:putname().
1308 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309void audit_putname(const char *name)
1310{
1311 struct audit_context *context = current->audit_context;
1312
1313 BUG_ON(!context);
1314 if (!context->in_syscall) {
1315#if AUDIT_DEBUG == 2
1316 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1317 __FILE__, __LINE__, context->serial, name);
1318 if (context->name_count) {
1319 int i;
1320 for (i = 0; i < context->name_count; i++)
1321 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1322 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001323 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325#endif
1326 __putname(name);
1327 }
1328#if AUDIT_DEBUG
1329 else {
1330 ++context->put_count;
1331 if (context->put_count > context->name_count) {
1332 printk(KERN_ERR "%s:%d(:%d): major=%d"
1333 " in_syscall=%d putname(%p) name_count=%d"
1334 " put_count=%d\n",
1335 __FILE__, __LINE__,
1336 context->serial, context->major,
1337 context->in_syscall, name, context->name_count,
1338 context->put_count);
1339 dump_stack();
1340 }
1341 }
1342#endif
1343}
1344
Amy Griffis3e2efce2006-07-13 13:16:02 -04001345/* Copy inode data into an audit_names. */
1346static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001347{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001348 name->ino = inode->i_ino;
1349 name->dev = inode->i_sb->s_dev;
1350 name->mode = inode->i_mode;
1351 name->uid = inode->i_uid;
1352 name->gid = inode->i_gid;
1353 name->rdev = inode->i_rdev;
1354 selinux_get_inode_sid(inode, &name->osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001355}
1356
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001357/**
1358 * audit_inode - store the inode and device from a lookup
1359 * @name: name being audited
1360 * @inode: inode being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001361 *
1362 * Called from fs/namei.c:path_lookup().
1363 */
Amy Griffis9c937dc2006-06-08 23:19:31 -04001364void __audit_inode(const char *name, const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 int idx;
1367 struct audit_context *context = current->audit_context;
1368
1369 if (!context->in_syscall)
1370 return;
1371 if (context->name_count
1372 && context->names[context->name_count-1].name
1373 && context->names[context->name_count-1].name == name)
1374 idx = context->name_count - 1;
1375 else if (context->name_count > 1
1376 && context->names[context->name_count-2].name
1377 && context->names[context->name_count-2].name == name)
1378 idx = context->name_count - 2;
1379 else {
1380 /* FIXME: how much do we care about inodes that have no
1381 * associated name? */
1382 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1383 return;
1384 idx = context->name_count++;
1385 context->names[idx].name = NULL;
1386#if AUDIT_DEBUG
1387 ++context->ino_count;
1388#endif
1389 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001390 audit_copy_inode(&context->names[idx], inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Amy Griffis73241cc2005-11-03 16:00:25 +00001393/**
1394 * audit_inode_child - collect inode info for created/removed objects
1395 * @dname: inode's dentry name
1396 * @inode: inode being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04001397 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00001398 *
1399 * For syscalls that create or remove filesystem objects, audit_inode
1400 * can only collect information for the filesystem object's parent.
1401 * This call updates the audit context with the child's information.
1402 * Syscalls that create a new filesystem object must be hooked after
1403 * the object is created. Syscalls that remove a filesystem object
1404 * must be hooked prior, in order to capture the target inode during
1405 * unsuccessful attempts.
1406 */
1407void __audit_inode_child(const char *dname, const struct inode *inode,
Amy Griffis73d3ec52006-07-13 13:16:39 -04001408 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00001409{
1410 int idx;
1411 struct audit_context *context = current->audit_context;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001412 const char *found_name = NULL;
1413 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00001414
1415 if (!context->in_syscall)
1416 return;
1417
1418 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001419 if (!dname)
Amy Griffis9c937dc2006-06-08 23:19:31 -04001420 goto update_context;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001421 for (idx = 0; idx < context->name_count; idx++)
Amy Griffis73d3ec52006-07-13 13:16:39 -04001422 if (context->names[idx].ino == parent->i_ino) {
Amy Griffisf368c07d2006-04-07 16:55:56 -04001423 const char *name = context->names[idx].name;
Amy Griffis73241cc2005-11-03 16:00:25 +00001424
Amy Griffisf368c07d2006-04-07 16:55:56 -04001425 if (!name)
1426 continue;
Amy Griffis73241cc2005-11-03 16:00:25 +00001427
Amy Griffis9c937dc2006-06-08 23:19:31 -04001428 if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
1429 context->names[idx].name_len = dirlen;
1430 found_name = name;
1431 break;
1432 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001433 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001434
Amy Griffis9c937dc2006-06-08 23:19:31 -04001435update_context:
Steve Grubbac9910c2006-09-28 14:31:32 -04001436 idx = context->name_count;
1437 if (context->name_count == AUDIT_NAMES) {
1438 printk(KERN_DEBUG "name_count maxed and losing %s\n",
1439 found_name ?: "(null)");
1440 return;
1441 }
1442 context->name_count++;
Amy Griffis73241cc2005-11-03 16:00:25 +00001443#if AUDIT_DEBUG
1444 context->ino_count++;
1445#endif
Amy Griffis9c937dc2006-06-08 23:19:31 -04001446 /* Re-use the name belonging to the slot for a matching parent directory.
1447 * All names for this context are relinquished in audit_free_names() */
1448 context->names[idx].name = found_name;
1449 context->names[idx].name_len = AUDIT_NAME_FULL;
1450 context->names[idx].name_put = 0; /* don't call __putname() */
Amy Griffis73241cc2005-11-03 16:00:25 +00001451
Amy Griffis3e2efce2006-07-13 13:16:02 -04001452 if (!inode)
1453 context->names[idx].ino = (unsigned long)-1;
1454 else
1455 audit_copy_inode(&context->names[idx], inode);
Amy Griffis73d3ec52006-07-13 13:16:39 -04001456
1457 /* A parent was not found in audit_names, so copy the inode data for the
1458 * provided parent. */
1459 if (!found_name) {
Steve Grubbac9910c2006-09-28 14:31:32 -04001460 idx = context->name_count;
1461 if (context->name_count == AUDIT_NAMES) {
1462 printk(KERN_DEBUG
1463 "name_count maxed and losing parent inode data: dev=%02x:%02x, inode=%lu",
1464 MAJOR(parent->i_sb->s_dev),
1465 MINOR(parent->i_sb->s_dev),
1466 parent->i_ino);
1467 return;
1468 }
1469 context->name_count++;
Amy Griffis73d3ec52006-07-13 13:16:39 -04001470#if AUDIT_DEBUG
1471 context->ino_count++;
1472#endif
1473 audit_copy_inode(&context->names[idx], parent);
1474 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001475}
1476
1477/**
1478 * audit_inode_update - update inode info for last collected name
1479 * @inode: inode being audited
1480 *
1481 * When open() is called on an existing object with the O_CREAT flag, the inode
1482 * data audit initially collects is incorrect. This additional hook ensures
1483 * audit has the inode data for the actual object to be opened.
1484 */
1485void __audit_inode_update(const struct inode *inode)
1486{
1487 struct audit_context *context = current->audit_context;
1488 int idx;
1489
1490 if (!context->in_syscall || !inode)
1491 return;
1492
1493 if (context->name_count == 0) {
1494 context->name_count++;
1495#if AUDIT_DEBUG
1496 context->ino_count++;
1497#endif
1498 }
1499 idx = context->name_count - 1;
1500
1501 audit_copy_inode(&context->names[idx], inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001504/**
1505 * auditsc_get_stamp - get local copies of audit_context values
1506 * @ctx: audit_context for the task
1507 * @t: timespec to store time recorded in the audit_context
1508 * @serial: serial value that is recorded in the audit_context
1509 *
1510 * Also sets the context as auditable.
1511 */
David Woodhousebfb44962005-05-21 21:08:09 +01001512void auditsc_get_stamp(struct audit_context *ctx,
1513 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
David Woodhousece625a82005-07-18 14:24:46 -04001515 if (!ctx->serial)
1516 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001517 t->tv_sec = ctx->ctime.tv_sec;
1518 t->tv_nsec = ctx->ctime.tv_nsec;
1519 *serial = ctx->serial;
1520 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001523/**
1524 * audit_set_loginuid - set a task's audit_context loginuid
1525 * @task: task whose audit context is being modified
1526 * @loginuid: loginuid value
1527 *
1528 * Returns 0.
1529 *
1530 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1531 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001532int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Steve Grubb41757102006-06-12 07:48:28 -04001534 struct audit_context *context = task->audit_context;
Steve Grubbc0404992005-05-13 18:17:42 +01001535
Steve Grubb41757102006-06-12 07:48:28 -04001536 if (context) {
1537 /* Only log if audit is enabled */
1538 if (context->in_syscall) {
1539 struct audit_buffer *ab;
1540
1541 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1542 if (ab) {
1543 audit_log_format(ab, "login pid=%d uid=%u "
1544 "old auid=%u new auid=%u",
1545 task->pid, task->uid,
1546 context->loginuid, loginuid);
1547 audit_log_end(ab);
1548 }
Steve Grubbc0404992005-05-13 18:17:42 +01001549 }
Steve Grubb41757102006-06-12 07:48:28 -04001550 context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 return 0;
1553}
1554
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001555/**
1556 * audit_get_loginuid - get the loginuid for an audit_context
1557 * @ctx: the audit_context
1558 *
1559 * Returns the context's loginuid or -1 if @ctx is NULL.
1560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561uid_t audit_get_loginuid(struct audit_context *ctx)
1562{
1563 return ctx ? ctx->loginuid : -1;
1564}
1565
Joy Latten161a09e2006-11-27 13:11:54 -06001566EXPORT_SYMBOL(audit_get_loginuid);
1567
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001568/**
George C. Wilson20ca73b2006-05-24 16:09:55 -05001569 * __audit_mq_open - record audit data for a POSIX MQ open
1570 * @oflag: open flag
1571 * @mode: mode bits
1572 * @u_attr: queue attributes
1573 *
1574 * Returns 0 for success or NULL context or < 0 on error.
1575 */
1576int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1577{
1578 struct audit_aux_data_mq_open *ax;
1579 struct audit_context *context = current->audit_context;
1580
1581 if (!audit_enabled)
1582 return 0;
1583
1584 if (likely(!context))
1585 return 0;
1586
1587 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1588 if (!ax)
1589 return -ENOMEM;
1590
1591 if (u_attr != NULL) {
1592 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1593 kfree(ax);
1594 return -EFAULT;
1595 }
1596 } else
1597 memset(&ax->attr, 0, sizeof(ax->attr));
1598
1599 ax->oflag = oflag;
1600 ax->mode = mode;
1601
1602 ax->d.type = AUDIT_MQ_OPEN;
1603 ax->d.next = context->aux;
1604 context->aux = (void *)ax;
1605 return 0;
1606}
1607
1608/**
1609 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1610 * @mqdes: MQ descriptor
1611 * @msg_len: Message length
1612 * @msg_prio: Message priority
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001613 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001614 *
1615 * Returns 0 for success or NULL context or < 0 on error.
1616 */
1617int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1618 const struct timespec __user *u_abs_timeout)
1619{
1620 struct audit_aux_data_mq_sendrecv *ax;
1621 struct audit_context *context = current->audit_context;
1622
1623 if (!audit_enabled)
1624 return 0;
1625
1626 if (likely(!context))
1627 return 0;
1628
1629 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1630 if (!ax)
1631 return -ENOMEM;
1632
1633 if (u_abs_timeout != NULL) {
1634 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1635 kfree(ax);
1636 return -EFAULT;
1637 }
1638 } else
1639 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1640
1641 ax->mqdes = mqdes;
1642 ax->msg_len = msg_len;
1643 ax->msg_prio = msg_prio;
1644
1645 ax->d.type = AUDIT_MQ_SENDRECV;
1646 ax->d.next = context->aux;
1647 context->aux = (void *)ax;
1648 return 0;
1649}
1650
1651/**
1652 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1653 * @mqdes: MQ descriptor
1654 * @msg_len: Message length
Randy Dunlap1dbe83c2006-06-27 02:54:01 -07001655 * @u_msg_prio: Message priority
1656 * @u_abs_timeout: Message timeout in absolute time
George C. Wilson20ca73b2006-05-24 16:09:55 -05001657 *
1658 * Returns 0 for success or NULL context or < 0 on error.
1659 */
1660int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1661 unsigned int __user *u_msg_prio,
1662 const struct timespec __user *u_abs_timeout)
1663{
1664 struct audit_aux_data_mq_sendrecv *ax;
1665 struct audit_context *context = current->audit_context;
1666
1667 if (!audit_enabled)
1668 return 0;
1669
1670 if (likely(!context))
1671 return 0;
1672
1673 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1674 if (!ax)
1675 return -ENOMEM;
1676
1677 if (u_msg_prio != NULL) {
1678 if (get_user(ax->msg_prio, u_msg_prio)) {
1679 kfree(ax);
1680 return -EFAULT;
1681 }
1682 } else
1683 ax->msg_prio = 0;
1684
1685 if (u_abs_timeout != NULL) {
1686 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1687 kfree(ax);
1688 return -EFAULT;
1689 }
1690 } else
1691 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1692
1693 ax->mqdes = mqdes;
1694 ax->msg_len = msg_len;
1695
1696 ax->d.type = AUDIT_MQ_SENDRECV;
1697 ax->d.next = context->aux;
1698 context->aux = (void *)ax;
1699 return 0;
1700}
1701
1702/**
1703 * __audit_mq_notify - record audit data for a POSIX MQ notify
1704 * @mqdes: MQ descriptor
1705 * @u_notification: Notification event
1706 *
1707 * Returns 0 for success or NULL context or < 0 on error.
1708 */
1709
1710int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1711{
1712 struct audit_aux_data_mq_notify *ax;
1713 struct audit_context *context = current->audit_context;
1714
1715 if (!audit_enabled)
1716 return 0;
1717
1718 if (likely(!context))
1719 return 0;
1720
1721 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1722 if (!ax)
1723 return -ENOMEM;
1724
1725 if (u_notification != NULL) {
1726 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1727 kfree(ax);
1728 return -EFAULT;
1729 }
1730 } else
1731 memset(&ax->notification, 0, sizeof(ax->notification));
1732
1733 ax->mqdes = mqdes;
1734
1735 ax->d.type = AUDIT_MQ_NOTIFY;
1736 ax->d.next = context->aux;
1737 context->aux = (void *)ax;
1738 return 0;
1739}
1740
1741/**
1742 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1743 * @mqdes: MQ descriptor
1744 * @mqstat: MQ flags
1745 *
1746 * Returns 0 for success or NULL context or < 0 on error.
1747 */
1748int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1749{
1750 struct audit_aux_data_mq_getsetattr *ax;
1751 struct audit_context *context = current->audit_context;
1752
1753 if (!audit_enabled)
1754 return 0;
1755
1756 if (likely(!context))
1757 return 0;
1758
1759 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1760 if (!ax)
1761 return -ENOMEM;
1762
1763 ax->mqdes = mqdes;
1764 ax->mqstat = *mqstat;
1765
1766 ax->d.type = AUDIT_MQ_GETSETATTR;
1767 ax->d.next = context->aux;
1768 context->aux = (void *)ax;
1769 return 0;
1770}
1771
1772/**
Steve Grubb073115d2006-04-02 17:07:33 -04001773 * audit_ipc_obj - record audit data for ipc object
1774 * @ipcp: ipc permissions
1775 *
1776 * Returns 0 for success or NULL context or < 0 on error.
1777 */
Al Virod8945bb52006-05-18 16:01:30 -04001778int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
Steve Grubb073115d2006-04-02 17:07:33 -04001779{
1780 struct audit_aux_data_ipcctl *ax;
1781 struct audit_context *context = current->audit_context;
1782
Steve Grubb073115d2006-04-02 17:07:33 -04001783 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1784 if (!ax)
1785 return -ENOMEM;
1786
1787 ax->uid = ipcp->uid;
1788 ax->gid = ipcp->gid;
1789 ax->mode = ipcp->mode;
1790 selinux_get_ipc_sid(ipcp, &ax->osid);
1791
1792 ax->d.type = AUDIT_IPC;
1793 ax->d.next = context->aux;
1794 context->aux = (void *)ax;
1795 return 0;
1796}
1797
1798/**
1799 * audit_ipc_set_perm - record audit data for new ipc permissions
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001800 * @qbytes: msgq bytes
1801 * @uid: msgq user id
1802 * @gid: msgq group id
1803 * @mode: msgq mode (permissions)
1804 *
1805 * Returns 0 for success or NULL context or < 0 on error.
1806 */
Al Virod8945bb52006-05-18 16:01:30 -04001807int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 struct audit_aux_data_ipcctl *ax;
1810 struct audit_context *context = current->audit_context;
1811
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001812 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (!ax)
1814 return -ENOMEM;
1815
1816 ax->qbytes = qbytes;
1817 ax->uid = uid;
1818 ax->gid = gid;
1819 ax->mode = mode;
1820
Steve Grubb073115d2006-04-02 17:07:33 -04001821 ax->d.type = AUDIT_IPC_SET_PERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 ax->d.next = context->aux;
1823 context->aux = (void *)ax;
1824 return 0;
1825}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001826
Al Viro473ae302006-04-26 14:04:08 -04001827int audit_bprm(struct linux_binprm *bprm)
1828{
1829 struct audit_aux_data_execve *ax;
1830 struct audit_context *context = current->audit_context;
1831 unsigned long p, next;
1832 void *to;
1833
Al Viro5ac3a9c2006-07-16 06:38:45 -04001834 if (likely(!audit_enabled || !context || context->dummy))
Al Viro473ae302006-04-26 14:04:08 -04001835 return 0;
1836
1837 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1838 GFP_KERNEL);
1839 if (!ax)
1840 return -ENOMEM;
1841
1842 ax->argc = bprm->argc;
1843 ax->envc = bprm->envc;
1844 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1845 struct page *page = bprm->page[p / PAGE_SIZE];
1846 void *kaddr = kmap(page);
1847 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1848 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1849 to += next - p;
1850 kunmap(page);
1851 }
1852
1853 ax->d.type = AUDIT_EXECVE;
1854 ax->d.next = context->aux;
1855 context->aux = (void *)ax;
1856 return 0;
1857}
1858
1859
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001860/**
1861 * audit_socketcall - record audit data for sys_socketcall
1862 * @nargs: number of args
1863 * @args: args array
1864 *
1865 * Returns 0 for success or NULL context or < 0 on error.
1866 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001867int audit_socketcall(int nargs, unsigned long *args)
1868{
1869 struct audit_aux_data_socketcall *ax;
1870 struct audit_context *context = current->audit_context;
1871
Al Viro5ac3a9c2006-07-16 06:38:45 -04001872 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001873 return 0;
1874
1875 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1876 if (!ax)
1877 return -ENOMEM;
1878
1879 ax->nargs = nargs;
1880 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1881
1882 ax->d.type = AUDIT_SOCKETCALL;
1883 ax->d.next = context->aux;
1884 context->aux = (void *)ax;
1885 return 0;
1886}
1887
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001888/**
Al Virodb349502007-02-07 01:48:00 -05001889 * __audit_fd_pair - record audit data for pipe and socketpair
1890 * @fd1: the first file descriptor
1891 * @fd2: the second file descriptor
1892 *
1893 * Returns 0 for success or NULL context or < 0 on error.
1894 */
1895int __audit_fd_pair(int fd1, int fd2)
1896{
1897 struct audit_context *context = current->audit_context;
1898 struct audit_aux_data_fd_pair *ax;
1899
1900 if (likely(!context)) {
1901 return 0;
1902 }
1903
1904 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1905 if (!ax) {
1906 return -ENOMEM;
1907 }
1908
1909 ax->fd[0] = fd1;
1910 ax->fd[1] = fd2;
1911
1912 ax->d.type = AUDIT_FD_PAIR;
1913 ax->d.next = context->aux;
1914 context->aux = (void *)ax;
1915 return 0;
1916}
1917
1918/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001919 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1920 * @len: data length in user space
1921 * @a: data address in kernel space
1922 *
1923 * Returns 0 for success or NULL context or < 0 on error.
1924 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001925int audit_sockaddr(int len, void *a)
1926{
1927 struct audit_aux_data_sockaddr *ax;
1928 struct audit_context *context = current->audit_context;
1929
Al Viro5ac3a9c2006-07-16 06:38:45 -04001930 if (likely(!context || context->dummy))
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001931 return 0;
1932
1933 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1934 if (!ax)
1935 return -ENOMEM;
1936
1937 ax->len = len;
1938 memcpy(ax->a, a, len);
1939
1940 ax->d.type = AUDIT_SOCKADDR;
1941 ax->d.next = context->aux;
1942 context->aux = (void *)ax;
1943 return 0;
1944}
1945
Al Viroa5cb0132007-03-20 13:58:35 -04001946void __audit_ptrace(struct task_struct *t)
1947{
1948 struct audit_context *context = current->audit_context;
1949
1950 context->target_pid = t->pid;
1951 selinux_get_task_sid(t, &context->target_sid);
1952}
1953
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001954/**
1955 * audit_avc_path - record the granting or denial of permissions
1956 * @dentry: dentry to record
1957 * @mnt: mnt to record
1958 *
1959 * Returns 0 for success or NULL context or < 0 on error.
1960 *
1961 * Called from security/selinux/avc.c::avc_audit()
1962 */
Stephen Smalley01116102005-05-21 00:15:52 +01001963int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1964{
1965 struct audit_aux_data_path *ax;
1966 struct audit_context *context = current->audit_context;
1967
1968 if (likely(!context))
1969 return 0;
1970
1971 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1972 if (!ax)
1973 return -ENOMEM;
1974
1975 ax->dentry = dget(dentry);
1976 ax->mnt = mntget(mnt);
1977
1978 ax->d.type = AUDIT_AVC_PATH;
1979 ax->d.next = context->aux;
1980 context->aux = (void *)ax;
1981 return 0;
1982}
1983
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001984/**
1985 * audit_signal_info - record signal info for shutting down audit subsystem
1986 * @sig: signal value
1987 * @t: task being signaled
1988 *
1989 * If the audit subsystem is being terminated, record the task (pid)
1990 * and uid that is doing that.
1991 */
Amy Griffise54dc242007-03-29 18:01:04 -04001992int __audit_signal_info(int sig, struct task_struct *t)
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001993{
Amy Griffise54dc242007-03-29 18:01:04 -04001994 struct audit_aux_data_pids *axp;
1995 struct task_struct *tsk = current;
1996 struct audit_context *ctx = tsk->audit_context;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001997 extern pid_t audit_sig_pid;
1998 extern uid_t audit_sig_uid;
Al Viroe1396062006-05-25 10:19:47 -04001999 extern u32 audit_sig_sid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002000
Amy Griffise54dc242007-03-29 18:01:04 -04002001 if (audit_pid && t->tgid == audit_pid &&
2002 (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1)) {
Al Viroe1396062006-05-25 10:19:47 -04002003 audit_sig_pid = tsk->pid;
2004 if (ctx)
2005 audit_sig_uid = ctx->loginuid;
2006 else
2007 audit_sig_uid = tsk->uid;
2008 selinux_get_task_sid(tsk, &audit_sig_sid);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002009 }
Amy Griffise54dc242007-03-29 18:01:04 -04002010
2011 if (!audit_signals) /* audit_context checked in wrapper */
2012 return 0;
2013
2014 /* optimize the common case by putting first signal recipient directly
2015 * in audit_context */
2016 if (!ctx->target_pid) {
2017 ctx->target_pid = t->tgid;
2018 selinux_get_task_sid(t, &ctx->target_sid);
2019 return 0;
2020 }
2021
2022 axp = (void *)ctx->aux_pids;
2023 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2024 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2025 if (!axp)
2026 return -ENOMEM;
2027
2028 axp->d.type = AUDIT_OBJ_PID;
2029 axp->d.next = ctx->aux_pids;
2030 ctx->aux_pids = (void *)axp;
2031 }
2032 BUG_ON(axp->pid_count > AUDIT_AUX_PIDS);
2033
2034 axp->target_pid[axp->pid_count] = t->tgid;
2035 selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
2036 axp->pid_count++;
2037
2038 return 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01002039}