blob: e36481ed61b4d0b138fa43e7662c0b65cfb021a5 [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
Amy Griffis9c937dc2006-06-08 23:19:31 -040081/* Indicates that audit should log the full pathname. */
82#define AUDIT_NAME_FULL -1
83
Al Viro471a5c72006-07-10 08:29:24 -040084/* number of audit rules */
85int audit_n_rules;
86
Amy Griffise54dc242007-03-29 18:01:04 -040087/* determines whether we collect data for signals sent */
88int audit_signals;
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* When fs/namei.c:getname() is called, we store the pointer in name and
91 * we don't let putname() free it (instead we free all of the saved
92 * pointers at syscall exit time).
93 *
94 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
95struct audit_names {
96 const char *name;
Amy Griffis9c937dc2006-06-08 23:19:31 -040097 int name_len; /* number of name's characters to log */
98 unsigned name_put; /* call __putname() for this name */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 unsigned long ino;
100 dev_t dev;
101 umode_t mode;
102 uid_t uid;
103 gid_t gid;
104 dev_t rdev;
Steve Grubb1b50eed2006-04-03 14:06:13 -0400105 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108struct audit_aux_data {
109 struct audit_aux_data *next;
110 int type;
111};
112
113#define AUDIT_AUX_IPCPERM 0
114
Amy Griffise54dc242007-03-29 18:01:04 -0400115/* Number of target pids per aux struct. */
116#define AUDIT_AUX_PIDS 16
117
George C. Wilson20ca73b2006-05-24 16:09:55 -0500118struct audit_aux_data_mq_open {
119 struct audit_aux_data d;
120 int oflag;
121 mode_t mode;
122 struct mq_attr attr;
123};
124
125struct audit_aux_data_mq_sendrecv {
126 struct audit_aux_data d;
127 mqd_t mqdes;
128 size_t msg_len;
129 unsigned int msg_prio;
130 struct timespec abs_timeout;
131};
132
133struct audit_aux_data_mq_notify {
134 struct audit_aux_data d;
135 mqd_t mqdes;
136 struct sigevent notification;
137};
138
139struct audit_aux_data_mq_getsetattr {
140 struct audit_aux_data d;
141 mqd_t mqdes;
142 struct mq_attr mqstat;
143};
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145struct audit_aux_data_ipcctl {
146 struct audit_aux_data d;
147 struct ipc_perm p;
148 unsigned long qbytes;
149 uid_t uid;
150 gid_t gid;
151 mode_t mode;
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500152 u32 osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153};
154
Al Viro473ae302006-04-26 14:04:08 -0400155struct audit_aux_data_execve {
156 struct audit_aux_data d;
157 int argc;
158 int envc;
159 char mem[0];
160};
161
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100162struct audit_aux_data_socketcall {
163 struct audit_aux_data d;
164 int nargs;
165 unsigned long args[0];
166};
167
168struct audit_aux_data_sockaddr {
169 struct audit_aux_data d;
170 int len;
171 char a[0];
172};
173
Al Virodb349502007-02-07 01:48:00 -0500174struct audit_aux_data_fd_pair {
175 struct audit_aux_data d;
176 int fd[2];
177};
178
Stephen Smalley01116102005-05-21 00:15:52 +0100179struct audit_aux_data_path {
180 struct audit_aux_data d;
181 struct dentry *dentry;
182 struct vfsmount *mnt;
183};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Amy Griffise54dc242007-03-29 18:01:04 -0400185struct audit_aux_data_pids {
186 struct audit_aux_data d;
187 pid_t target_pid[AUDIT_AUX_PIDS];
188 u32 target_sid[AUDIT_AUX_PIDS];
189 int pid_count;
190};
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/* The per-task audit context. */
193struct audit_context {
Al Virod51374a2006-08-03 10:59:26 -0400194 int dummy; /* must be the first element */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int in_syscall; /* 1 if task is in a syscall */
196 enum audit_state state;
197 unsigned int serial; /* serial number for record */
198 struct timespec ctime; /* time of syscall entry */
199 uid_t loginuid; /* login uid (identity) */
200 int major; /* syscall number */
201 unsigned long argv[4]; /* syscall arguments */
202 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100203 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int auditable; /* 1 if record should be written */
205 int name_count;
206 struct audit_names names[AUDIT_NAMES];
Amy Griffis5adc8a62006-06-14 18:45:21 -0400207 char * filterkey; /* key for rule that triggered record */
David Woodhouse8f37d472005-05-27 12:17:28 +0100208 struct dentry * pwd;
209 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct audit_context *previous; /* For nested syscalls */
211 struct audit_aux_data *aux;
Amy Griffise54dc242007-03-29 18:01:04 -0400212 struct audit_aux_data *aux_pids;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Save things to print about task_struct */
Al Virof46038f2006-05-06 08:22:52 -0400215 pid_t pid, ppid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 uid_t uid, euid, suid, fsuid;
217 gid_t gid, egid, sgid, fsgid;
218 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100219 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Al Viroa5cb0132007-03-20 13:58:35 -0400221 pid_t target_pid;
222 u32 target_sid;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224#if AUDIT_DEBUG
225 int put_count;
226 int ino_count;
227#endif
228};
229
Al Viro55669bf2006-08-31 19:26:40 -0400230#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
231static inline int open_arg(int flags, int mask)
232{
233 int n = ACC_MODE(flags);
234 if (flags & (O_TRUNC | O_CREAT))
235 n |= AUDIT_PERM_WRITE;
236 return n & mask;
237}
238
239static int audit_match_perm(struct audit_context *ctx, int mask)
240{
241 unsigned n = ctx->major;
242 switch (audit_classify_syscall(ctx->arch, n)) {
243 case 0: /* native */
244 if ((mask & AUDIT_PERM_WRITE) &&
245 audit_match_class(AUDIT_CLASS_WRITE, n))
246 return 1;
247 if ((mask & AUDIT_PERM_READ) &&
248 audit_match_class(AUDIT_CLASS_READ, n))
249 return 1;
250 if ((mask & AUDIT_PERM_ATTR) &&
251 audit_match_class(AUDIT_CLASS_CHATTR, n))
252 return 1;
253 return 0;
254 case 1: /* 32bit on biarch */
255 if ((mask & AUDIT_PERM_WRITE) &&
256 audit_match_class(AUDIT_CLASS_WRITE_32, n))
257 return 1;
258 if ((mask & AUDIT_PERM_READ) &&
259 audit_match_class(AUDIT_CLASS_READ_32, n))
260 return 1;
261 if ((mask & AUDIT_PERM_ATTR) &&
262 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
263 return 1;
264 return 0;
265 case 2: /* open */
266 return mask & ACC_MODE(ctx->argv[1]);
267 case 3: /* openat */
268 return mask & ACC_MODE(ctx->argv[2]);
269 case 4: /* socketcall */
270 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
271 case 5: /* execve */
272 return mask & AUDIT_PERM_EXEC;
273 default:
274 return 0;
275 }
276}
277
Amy Griffisf368c07d2006-04-07 16:55:56 -0400278/* Determine if any context name data matches a rule's watch data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/* Compare a task_struct with an audit_rule. Return 1 on match, 0
280 * otherwise. */
281static int audit_filter_rules(struct task_struct *tsk,
Amy Griffis93315ed2006-02-07 12:05:27 -0500282 struct audit_krule *rule,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 struct audit_context *ctx,
Amy Griffisf368c07d2006-04-07 16:55:56 -0400284 struct audit_names *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 enum audit_state *state)
286{
Steve Grubb2ad312d2006-04-11 08:50:56 -0400287 int i, j, need_sid = 1;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600288 u32 sid;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500291 struct audit_field *f = &rule->fields[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int result = 0;
293
Amy Griffis93315ed2006-02-07 12:05:27 -0500294 switch (f->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500296 result = audit_comparator(tsk->pid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
Al Viro3c662512006-05-06 08:26:27 -0400298 case AUDIT_PPID:
Alexander Viro419c58f2006-09-29 00:08:50 -0400299 if (ctx) {
300 if (!ctx->ppid)
301 ctx->ppid = sys_getppid();
Al Viro3c662512006-05-06 08:26:27 -0400302 result = audit_comparator(ctx->ppid, f->op, f->val);
Alexander Viro419c58f2006-09-29 00:08:50 -0400303 }
Al Viro3c662512006-05-06 08:26:27 -0400304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500306 result = audit_comparator(tsk->uid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 case AUDIT_EUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500309 result = audit_comparator(tsk->euid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break;
311 case AUDIT_SUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500312 result = audit_comparator(tsk->suid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
314 case AUDIT_FSUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500315 result = audit_comparator(tsk->fsuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500318 result = audit_comparator(tsk->gid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 break;
320 case AUDIT_EGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500321 result = audit_comparator(tsk->egid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
323 case AUDIT_SGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500324 result = audit_comparator(tsk->sgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 break;
326 case AUDIT_FSGID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500327 result = audit_comparator(tsk->fsgid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 break;
329 case AUDIT_PERS:
Amy Griffis93315ed2006-02-07 12:05:27 -0500330 result = audit_comparator(tsk->personality, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 break;
2fd6f582005-04-29 16:08:28 +0100332 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000333 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500334 result = audit_comparator(ctx->arch, f->op, f->val);
2fd6f582005-04-29 16:08:28 +0100335 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 case AUDIT_EXIT:
338 if (ctx && ctx->return_valid)
Amy Griffis93315ed2006-02-07 12:05:27 -0500339 result = audit_comparator(ctx->return_code, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 break;
341 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100342 if (ctx && ctx->return_valid) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500343 if (f->val)
344 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100345 else
Amy Griffis93315ed2006-02-07 12:05:27 -0500346 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
349 case AUDIT_DEVMAJOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400350 if (name)
351 result = audit_comparator(MAJOR(name->dev),
352 f->op, f->val);
353 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500355 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 ++result;
357 break;
358 }
359 }
360 }
361 break;
362 case AUDIT_DEVMINOR:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400363 if (name)
364 result = audit_comparator(MINOR(name->dev),
365 f->op, f->val);
366 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500368 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 ++result;
370 break;
371 }
372 }
373 }
374 break;
375 case AUDIT_INODE:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400376 if (name)
Amy Griffis9c937dc2006-06-08 23:19:31 -0400377 result = (name->ino == f->val);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400378 else if (ctx) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400380 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 ++result;
382 break;
383 }
384 }
385 }
386 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400387 case AUDIT_WATCH:
388 if (name && rule->watch->ino != (unsigned long)-1)
389 result = (name->dev == rule->watch->dev &&
Amy Griffis9c937dc2006-06-08 23:19:31 -0400390 name->ino == rule->watch->ino);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400391 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 case AUDIT_LOGINUID:
393 result = 0;
394 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500395 result = audit_comparator(ctx->loginuid, f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500397 case AUDIT_SUBJ_USER:
398 case AUDIT_SUBJ_ROLE:
399 case AUDIT_SUBJ_TYPE:
400 case AUDIT_SUBJ_SEN:
401 case AUDIT_SUBJ_CLR:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600402 /* NOTE: this may return negative values indicating
403 a temporary error. We simply treat this as a
404 match for now to avoid losing information that
405 may be wanted. An error message will also be
406 logged upon error */
Steve Grubb2ad312d2006-04-11 08:50:56 -0400407 if (f->se_rule) {
408 if (need_sid) {
Stephen Smalley62bac012006-09-25 23:31:56 -0700409 selinux_get_task_sid(tsk, &sid);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400410 need_sid = 0;
411 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600412 result = selinux_audit_rule_match(sid, f->type,
413 f->op,
414 f->se_rule,
415 ctx);
Steve Grubb2ad312d2006-04-11 08:50:56 -0400416 }
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600417 break;
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500418 case AUDIT_OBJ_USER:
419 case AUDIT_OBJ_ROLE:
420 case AUDIT_OBJ_TYPE:
421 case AUDIT_OBJ_LEV_LOW:
422 case AUDIT_OBJ_LEV_HIGH:
423 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
424 also applies here */
425 if (f->se_rule) {
426 /* Find files that match */
427 if (name) {
428 result = selinux_audit_rule_match(
429 name->osid, f->type, f->op,
430 f->se_rule, ctx);
431 } else if (ctx) {
432 for (j = 0; j < ctx->name_count; j++) {
433 if (selinux_audit_rule_match(
434 ctx->names[j].osid,
435 f->type, f->op,
436 f->se_rule, ctx)) {
437 ++result;
438 break;
439 }
440 }
441 }
442 /* Find ipc objects that match */
443 if (ctx) {
444 struct audit_aux_data *aux;
445 for (aux = ctx->aux; aux;
446 aux = aux->next) {
447 if (aux->type == AUDIT_IPC) {
448 struct audit_aux_data_ipcctl *axi = (void *)aux;
449 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
450 ++result;
451 break;
452 }
453 }
454 }
455 }
456 }
457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 case AUDIT_ARG0:
459 case AUDIT_ARG1:
460 case AUDIT_ARG2:
461 case AUDIT_ARG3:
462 if (ctx)
Amy Griffis93315ed2006-02-07 12:05:27 -0500463 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400465 case AUDIT_FILTERKEY:
466 /* ignore this field for filtering */
467 result = 1;
468 break;
Al Viro55669bf2006-08-31 19:26:40 -0400469 case AUDIT_PERM:
470 result = audit_match_perm(ctx, f->val);
471 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!result)
475 return 0;
476 }
Amy Griffis5adc8a62006-06-14 18:45:21 -0400477 if (rule->filterkey)
478 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 switch (rule->action) {
480 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
482 }
483 return 1;
484}
485
486/* At process creation time, we can determine if system-call auditing is
487 * completely disabled for this task. Since we only have the task
488 * structure at this point, we can only check uid and gid.
489 */
490static enum audit_state audit_filter_task(struct task_struct *tsk)
491{
492 struct audit_entry *e;
493 enum audit_state state;
494
495 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100496 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400497 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 rcu_read_unlock();
499 return state;
500 }
501 }
502 rcu_read_unlock();
503 return AUDIT_BUILD_CONTEXT;
504}
505
506/* At syscall entry and exit time, this filter is called if the
507 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100508 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700509 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
511static enum audit_state audit_filter_syscall(struct task_struct *tsk,
512 struct audit_context *ctx,
513 struct list_head *list)
514{
515 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100516 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
David Woodhouse351bb722005-07-14 14:40:06 +0100518 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100519 return AUDIT_DISABLED;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100522 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000523 int word = AUDIT_WORD(ctx->major);
524 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100525
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000526 list_for_each_entry_rcu(e, list, list) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400527 if ((e->rule.mask[word] & bit) == bit &&
528 audit_filter_rules(tsk, &e->rule, ctx, NULL,
529 &state)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000530 rcu_read_unlock();
531 return state;
532 }
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 rcu_read_unlock();
536 return AUDIT_BUILD_CONTEXT;
537}
538
Amy Griffisf368c07d2006-04-07 16:55:56 -0400539/* At syscall exit time, this filter is called if any audit_names[] have been
540 * collected during syscall processing. We only check rules in sublists at hash
541 * buckets applicable to the inode numbers in audit_names[].
542 * Regarding audit_state, same rules apply as for audit_filter_syscall().
543 */
544enum audit_state audit_filter_inodes(struct task_struct *tsk,
545 struct audit_context *ctx)
546{
547 int i;
548 struct audit_entry *e;
549 enum audit_state state;
550
551 if (audit_pid && tsk->tgid == audit_pid)
552 return AUDIT_DISABLED;
553
554 rcu_read_lock();
555 for (i = 0; i < ctx->name_count; i++) {
556 int word = AUDIT_WORD(ctx->major);
557 int bit = AUDIT_BIT(ctx->major);
558 struct audit_names *n = &ctx->names[i];
559 int h = audit_hash_ino((u32)n->ino);
560 struct list_head *list = &audit_inode_hash[h];
561
562 if (list_empty(list))
563 continue;
564
565 list_for_each_entry_rcu(e, list, list) {
566 if ((e->rule.mask[word] & bit) == bit &&
567 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
568 rcu_read_unlock();
569 return state;
570 }
571 }
572 }
573 rcu_read_unlock();
574 return AUDIT_BUILD_CONTEXT;
575}
576
577void audit_set_auditable(struct audit_context *ctx)
578{
579 ctx->auditable = 1;
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static inline struct audit_context *audit_get_context(struct task_struct *tsk,
583 int return_valid,
584 int return_code)
585{
586 struct audit_context *context = tsk->audit_context;
587
588 if (likely(!context))
589 return NULL;
590 context->return_valid = return_valid;
591 context->return_code = return_code;
592
Al Virod51374a2006-08-03 10:59:26 -0400593 if (context->in_syscall && !context->dummy && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 enum audit_state state;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400595
David Woodhouse0f45aa12005-06-19 19:35:50 +0100596 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400597 if (state == AUDIT_RECORD_CONTEXT) {
598 context->auditable = 1;
599 goto get_context;
600 }
601
602 state = audit_filter_inodes(tsk, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (state == AUDIT_RECORD_CONTEXT)
604 context->auditable = 1;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
Amy Griffisf368c07d2006-04-07 16:55:56 -0400608get_context:
Al Viro3f2792f2006-07-16 06:43:48 -0400609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 tsk->audit_context = NULL;
611 return context;
612}
613
614static inline void audit_free_names(struct audit_context *context)
615{
616 int i;
617
618#if AUDIT_DEBUG == 2
619 if (context->auditable
620 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000621 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 " name_count=%d put_count=%d"
623 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000624 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 context->serial, context->major, context->in_syscall,
626 context->name_count, context->put_count,
627 context->ino_count);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000628 for (i = 0; i < context->name_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 printk(KERN_ERR "names[%d] = %p = %s\n", i,
630 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000631 context->names[i].name ?: "(null)");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 dump_stack();
634 return;
635 }
636#endif
637#if AUDIT_DEBUG
638 context->put_count = 0;
639 context->ino_count = 0;
640#endif
641
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000642 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400643 if (context->names[i].name && context->names[i].name_put)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 __putname(context->names[i].name);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100647 if (context->pwd)
648 dput(context->pwd);
649 if (context->pwdmnt)
650 mntput(context->pwdmnt);
651 context->pwd = NULL;
652 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655static inline void audit_free_aux(struct audit_context *context)
656{
657 struct audit_aux_data *aux;
658
659 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100660 if (aux->type == AUDIT_AVC_PATH) {
661 struct audit_aux_data_path *axi = (void *)aux;
662 dput(axi->dentry);
663 mntput(axi->mnt);
664 }
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 context->aux = aux->next;
667 kfree(aux);
668 }
Amy Griffise54dc242007-03-29 18:01:04 -0400669 while ((aux = context->aux_pids)) {
670 context->aux_pids = aux->next;
671 kfree(aux);
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675static inline void audit_zero_context(struct audit_context *context,
676 enum audit_state state)
677{
678 uid_t loginuid = context->loginuid;
679
680 memset(context, 0, sizeof(*context));
681 context->state = state;
682 context->loginuid = loginuid;
683}
684
685static inline struct audit_context *audit_alloc_context(enum audit_state state)
686{
687 struct audit_context *context;
688
689 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
690 return NULL;
691 audit_zero_context(context, state);
692 return context;
693}
694
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700695/**
696 * audit_alloc - allocate an audit context block for a task
697 * @tsk: task
698 *
699 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 * if necessary. Doing so turns on system call auditing for the
701 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700702 * needed.
703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704int audit_alloc(struct task_struct *tsk)
705{
706 struct audit_context *context;
707 enum audit_state state;
708
709 if (likely(!audit_enabled))
710 return 0; /* Return if not auditing. */
711
712 state = audit_filter_task(tsk);
713 if (likely(state == AUDIT_DISABLED))
714 return 0;
715
716 if (!(context = audit_alloc_context(state))) {
717 audit_log_lost("out of memory in audit_alloc");
718 return -ENOMEM;
719 }
720
721 /* Preserve login uid */
722 context->loginuid = -1;
723 if (current->audit_context)
724 context->loginuid = current->audit_context->loginuid;
725
726 tsk->audit_context = context;
727 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
728 return 0;
729}
730
731static inline void audit_free_context(struct audit_context *context)
732{
733 struct audit_context *previous;
734 int count = 0;
735
736 do {
737 previous = context->previous;
738 if (previous || (count && count < 10)) {
739 ++count;
740 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
741 " freeing multiple contexts (%d)\n",
742 context->serial, context->major,
743 context->name_count, count);
744 }
745 audit_free_names(context);
746 audit_free_aux(context);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400747 kfree(context->filterkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 kfree(context);
749 context = previous;
750 } while (context);
751 if (count >= 10)
752 printk(KERN_ERR "audit: freed %d contexts\n", count);
753}
754
Joy Latten161a09e2006-11-27 13:11:54 -0600755void audit_log_task_context(struct audit_buffer *ab)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000756{
757 char *ctx = NULL;
Al Viroc4823bc2007-03-12 16:17:42 +0000758 unsigned len;
759 int error;
760 u32 sid;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000761
Al Viroc4823bc2007-03-12 16:17:42 +0000762 selinux_get_task_sid(current, &sid);
763 if (!sid)
764 return;
765
766 error = selinux_sid_to_string(sid, &ctx, &len);
767 if (error) {
768 if (error != -EINVAL)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000769 goto error_path;
770 return;
771 }
772
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000773 audit_log_format(ab, " subj=%s", ctx);
Al Viroc4823bc2007-03-12 16:17:42 +0000774 kfree(ctx);
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000775 return;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000776
777error_path:
Dustin Kirkland7306a0b2005-11-16 15:53:13 +0000778 audit_panic("error in audit_log_task_context");
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000779 return;
780}
781
Joy Latten161a09e2006-11-27 13:11:54 -0600782EXPORT_SYMBOL(audit_log_task_context);
783
Al Viroe4951492006-03-29 20:17:10 -0500784static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
Stephen Smalley219f0812005-04-18 10:47:35 -0700785{
Al Viro45d9bb02006-03-29 20:02:55 -0500786 char name[sizeof(tsk->comm)];
787 struct mm_struct *mm = tsk->mm;
Stephen Smalley219f0812005-04-18 10:47:35 -0700788 struct vm_area_struct *vma;
789
Al Viroe4951492006-03-29 20:17:10 -0500790 /* tsk == current */
791
Al Viro45d9bb02006-03-29 20:02:55 -0500792 get_task_comm(name, tsk);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100793 audit_log_format(ab, " comm=");
794 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700795
Al Viroe4951492006-03-29 20:17:10 -0500796 if (mm) {
797 down_read(&mm->mmap_sem);
798 vma = mm->mmap;
799 while (vma) {
800 if ((vma->vm_flags & VM_EXECUTABLE) &&
801 vma->vm_file) {
802 audit_log_d_path(ab, "exe=",
Josef Sipeka7a005f2006-12-08 02:37:17 -0800803 vma->vm_file->f_path.dentry,
804 vma->vm_file->f_path.mnt);
Al Viroe4951492006-03-29 20:17:10 -0500805 break;
806 }
807 vma = vma->vm_next;
Stephen Smalley219f0812005-04-18 10:47:35 -0700808 }
Al Viroe4951492006-03-29 20:17:10 -0500809 up_read(&mm->mmap_sem);
Stephen Smalley219f0812005-04-18 10:47:35 -0700810 }
Al Viroe4951492006-03-29 20:17:10 -0500811 audit_log_task_context(ab);
Stephen Smalley219f0812005-04-18 10:47:35 -0700812}
813
Amy Griffise54dc242007-03-29 18:01:04 -0400814static int audit_log_pid_context(struct audit_context *context, pid_t pid,
815 u32 sid)
816{
817 struct audit_buffer *ab;
818 char *s = NULL;
819 u32 len;
820 int rc = 0;
821
822 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
823 if (!ab)
824 return 1;
825
826 if (selinux_sid_to_string(sid, &s, &len)) {
827 audit_log_format(ab, "opid=%d obj=(none)", pid);
828 rc = 1;
829 } else
830 audit_log_format(ab, "opid=%d obj=%s", pid, s);
831 audit_log_end(ab);
832 kfree(s);
833
834 return rc;
835}
836
Al Viroe4951492006-03-29 20:17:10 -0500837static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500839 int i, call_panic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100841 struct audit_aux_data *aux;
Steve Grubba6c043a2006-01-01 14:07:00 -0500842 const char *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Al Viroe4951492006-03-29 20:17:10 -0500844 /* tsk == current */
Al Viro3f2792f2006-07-16 06:43:48 -0400845 context->pid = tsk->pid;
Alexander Viro419c58f2006-09-29 00:08:50 -0400846 if (!context->ppid)
847 context->ppid = sys_getppid();
Al Viro3f2792f2006-07-16 06:43:48 -0400848 context->uid = tsk->uid;
849 context->gid = tsk->gid;
850 context->euid = tsk->euid;
851 context->suid = tsk->suid;
852 context->fsuid = tsk->fsuid;
853 context->egid = tsk->egid;
854 context->sgid = tsk->sgid;
855 context->fsgid = tsk->fsgid;
856 context->personality = tsk->personality;
Al Viroe4951492006-03-29 20:17:10 -0500857
858 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!ab)
860 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100861 audit_log_format(ab, "arch=%x syscall=%d",
862 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (context->personality != PER_LINUX)
864 audit_log_format(ab, " per=%lx", context->personality);
865 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100866 audit_log_format(ab, " success=%s exit=%ld",
867 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
868 context->return_code);
Alan Coxeb84a202006-09-29 02:01:41 -0700869
870 mutex_lock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800871 read_lock(&tasklist_lock);
Al Viro45d9bb02006-03-29 20:02:55 -0500872 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
873 tty = tsk->signal->tty->name;
Steve Grubba6c043a2006-01-01 14:07:00 -0500874 else
875 tty = "(none)";
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800876 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 audit_log_format(ab,
878 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Al Virof46038f2006-05-06 08:22:52 -0400879 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
Steve Grubb326e9c82005-05-21 00:22:31 +0100880 " euid=%u suid=%u fsuid=%u"
Steve Grubba6c043a2006-01-01 14:07:00 -0500881 " egid=%u sgid=%u fsgid=%u tty=%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 context->argv[0],
883 context->argv[1],
884 context->argv[2],
885 context->argv[3],
886 context->name_count,
Al Virof46038f2006-05-06 08:22:52 -0400887 context->ppid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 context->pid,
889 context->loginuid,
890 context->uid,
891 context->gid,
892 context->euid, context->suid, context->fsuid,
Steve Grubba6c043a2006-01-01 14:07:00 -0500893 context->egid, context->sgid, context->fsgid, tty);
Alan Coxeb84a202006-09-29 02:01:41 -0700894
895 mutex_unlock(&tty_mutex);
896
Al Viroe4951492006-03-29 20:17:10 -0500897 audit_log_task_info(ab, tsk);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400898 if (context->filterkey) {
899 audit_log_format(ab, " key=");
900 audit_log_untrustedstring(ab, context->filterkey);
901 } else
902 audit_log_format(ab, " key=(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
David Woodhouse7551ced2005-05-26 12:04:57 +0100905 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100906
Al Viroe4951492006-03-29 20:17:10 -0500907 ab = audit_log_start(context, GFP_KERNEL, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (!ab)
909 continue; /* audit_panic has been called */
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 switch (aux->type) {
George C. Wilson20ca73b2006-05-24 16:09:55 -0500912 case AUDIT_MQ_OPEN: {
913 struct audit_aux_data_mq_open *axi = (void *)aux;
914 audit_log_format(ab,
915 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
916 "mq_msgsize=%ld mq_curmsgs=%ld",
917 axi->oflag, axi->mode, axi->attr.mq_flags,
918 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
919 axi->attr.mq_curmsgs);
920 break; }
921
922 case AUDIT_MQ_SENDRECV: {
923 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
924 audit_log_format(ab,
925 "mqdes=%d msg_len=%zd msg_prio=%u "
926 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
927 axi->mqdes, axi->msg_len, axi->msg_prio,
928 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
929 break; }
930
931 case AUDIT_MQ_NOTIFY: {
932 struct audit_aux_data_mq_notify *axi = (void *)aux;
933 audit_log_format(ab,
934 "mqdes=%d sigev_signo=%d",
935 axi->mqdes,
936 axi->notification.sigev_signo);
937 break; }
938
939 case AUDIT_MQ_GETSETATTR: {
940 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
941 audit_log_format(ab,
942 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
943 "mq_curmsgs=%ld ",
944 axi->mqdes,
945 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
946 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
947 break; }
948
Steve Grubbc0404992005-05-13 18:17:42 +0100949 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 struct audit_aux_data_ipcctl *axi = (void *)aux;
951 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400952 "ouid=%u ogid=%u mode=%x",
953 axi->uid, axi->gid, axi->mode);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500954 if (axi->osid != 0) {
955 char *ctx = NULL;
956 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -0700957 if (selinux_sid_to_string(
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500958 axi->osid, &ctx, &len)) {
Steve Grubbce29b682006-04-01 18:29:34 -0500959 audit_log_format(ab, " osid=%u",
Steve Grubb9c7aa6a2006-03-31 15:22:49 -0500960 axi->osid);
961 call_panic = 1;
962 } else
963 audit_log_format(ab, " obj=%s", ctx);
964 kfree(ctx);
965 }
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100966 break; }
967
Steve Grubb073115d2006-04-02 17:07:33 -0400968 case AUDIT_IPC_SET_PERM: {
969 struct audit_aux_data_ipcctl *axi = (void *)aux;
970 audit_log_format(ab,
Linda Knippersac032212006-05-16 22:03:48 -0400971 "qbytes=%lx ouid=%u ogid=%u mode=%x",
Steve Grubb073115d2006-04-02 17:07:33 -0400972 axi->qbytes, axi->uid, axi->gid, axi->mode);
Steve Grubb073115d2006-04-02 17:07:33 -0400973 break; }
Linda Knippersac032212006-05-16 22:03:48 -0400974
Al Viro473ae302006-04-26 14:04:08 -0400975 case AUDIT_EXECVE: {
976 struct audit_aux_data_execve *axi = (void *)aux;
977 int i;
978 const char *p;
979 for (i = 0, p = axi->mem; i < axi->argc; i++) {
980 audit_log_format(ab, "a%d=", i);
981 p = audit_log_untrustedstring(ab, p);
982 audit_log_format(ab, "\n");
983 }
984 break; }
Steve Grubb073115d2006-04-02 17:07:33 -0400985
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100986 case AUDIT_SOCKETCALL: {
987 int i;
988 struct audit_aux_data_socketcall *axs = (void *)aux;
989 audit_log_format(ab, "nargs=%d", axs->nargs);
990 for (i=0; i<axs->nargs; i++)
991 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
992 break; }
993
994 case AUDIT_SOCKADDR: {
995 struct audit_aux_data_sockaddr *axs = (void *)aux;
996
997 audit_log_format(ab, "saddr=");
998 audit_log_hex(ab, axs->a, axs->len);
999 break; }
Stephen Smalley01116102005-05-21 00:15:52 +01001000
1001 case AUDIT_AVC_PATH: {
1002 struct audit_aux_data_path *axi = (void *)aux;
1003 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +01001004 break; }
1005
Al Virodb349502007-02-07 01:48:00 -05001006 case AUDIT_FD_PAIR: {
1007 struct audit_aux_data_fd_pair *axs = (void *)aux;
1008 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1009 break; }
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
Amy Griffise54dc242007-03-29 18:01:04 -04001015 for (aux = context->aux_pids; aux; aux = aux->next) {
1016 struct audit_aux_data_pids *axs = (void *)aux;
1017 int i;
1018
1019 for (i = 0; i < axs->pid_count; i++)
1020 if (audit_log_pid_context(context, axs->target_pid[i],
1021 axs->target_sid[i]))
1022 call_panic = 1;
Al Viroa5cb0132007-03-20 13:58:35 -04001023 }
1024
Amy Griffise54dc242007-03-29 18:01:04 -04001025 if (context->target_pid &&
1026 audit_log_pid_context(context, context->target_pid,
1027 context->target_sid))
1028 call_panic = 1;
1029
David Woodhouse8f37d472005-05-27 12:17:28 +01001030 if (context->pwd && context->pwdmnt) {
Al Viroe4951492006-03-29 20:17:10 -05001031 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +01001032 if (ab) {
1033 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1034 audit_log_end(ab);
1035 }
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 for (i = 0; i < context->name_count; i++) {
Amy Griffis9c937dc2006-06-08 23:19:31 -04001038 struct audit_names *n = &context->names[i];
Amy Griffis73241cc2005-11-03 16:00:25 +00001039
Al Viroe4951492006-03-29 20:17:10 -05001040 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!ab)
1042 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +01001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +00001045
Amy Griffis9c937dc2006-06-08 23:19:31 -04001046 if (n->name) {
1047 switch(n->name_len) {
1048 case AUDIT_NAME_FULL:
1049 /* log the full path */
1050 audit_log_format(ab, " name=");
1051 audit_log_untrustedstring(ab, n->name);
1052 break;
1053 case 0:
1054 /* name was specified as a relative path and the
1055 * directory component is the cwd */
1056 audit_log_d_path(ab, " name=", context->pwd,
1057 context->pwdmnt);
1058 break;
1059 default:
1060 /* log the name's directory component */
1061 audit_log_format(ab, " name=");
1062 audit_log_n_untrustedstring(ab, n->name_len,
1063 n->name);
1064 }
1065 } else
1066 audit_log_format(ab, " name=(null)");
Amy Griffis73241cc2005-11-03 16:00:25 +00001067
Amy Griffis9c937dc2006-06-08 23:19:31 -04001068 if (n->ino != (unsigned long)-1) {
1069 audit_log_format(ab, " inode=%lu"
1070 " dev=%02x:%02x mode=%#o"
1071 " ouid=%u ogid=%u rdev=%02x:%02x",
1072 n->ino,
1073 MAJOR(n->dev),
1074 MINOR(n->dev),
1075 n->mode,
1076 n->uid,
1077 n->gid,
1078 MAJOR(n->rdev),
1079 MINOR(n->rdev));
1080 }
1081 if (n->osid != 0) {
Steve Grubb1b50eed2006-04-03 14:06:13 -04001082 char *ctx = NULL;
1083 u32 len;
Stephen Smalley1a70cd42006-09-25 23:31:57 -07001084 if (selinux_sid_to_string(
Amy Griffis9c937dc2006-06-08 23:19:31 -04001085 n->osid, &ctx, &len)) {
1086 audit_log_format(ab, " osid=%u", n->osid);
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001087 call_panic = 2;
Steve Grubb1b50eed2006-04-03 14:06:13 -04001088 } else
1089 audit_log_format(ab, " obj=%s", ctx);
1090 kfree(ctx);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001091 }
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 audit_log_end(ab);
1094 }
Steve Grubb9c7aa6a2006-03-31 15:22:49 -05001095 if (call_panic)
1096 audit_panic("error converting sid to string");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001099/**
1100 * audit_free - free a per-task audit context
1101 * @tsk: task whose audit context block to free
1102 *
Al Virofa84cb92006-03-29 20:30:19 -05001103 * Called from copy_process and do_exit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105void audit_free(struct task_struct *tsk)
1106{
1107 struct audit_context *context;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 context = audit_get_context(tsk, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (likely(!context))
1111 return;
1112
1113 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +01001114 * function (e.g., exit_group), then free context block.
1115 * We use GFP_ATOMIC here because we might be doing this
1116 * in the context of the idle thread */
Al Viroe4951492006-03-29 20:17:10 -05001117 /* that can happen only if we are called from do_exit() */
David Woodhousef7056d62005-06-20 16:07:33 +01001118 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001119 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 audit_free_context(context);
1122}
1123
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001124/**
1125 * audit_syscall_entry - fill in an audit record at syscall entry
1126 * @tsk: task being audited
1127 * @arch: architecture type
1128 * @major: major syscall type (function)
1129 * @a1: additional syscall register 1
1130 * @a2: additional syscall register 2
1131 * @a3: additional syscall register 3
1132 * @a4: additional syscall register 4
1133 *
1134 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 * audit context was created when the task was created and the state or
1136 * filters demand the audit context be built. If the state from the
1137 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1138 * then the record will be written at syscall exit time (otherwise, it
1139 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001140 * be written).
1141 */
Al Viro5411be52006-03-29 20:23:36 -05001142void audit_syscall_entry(int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 unsigned long a1, unsigned long a2,
1144 unsigned long a3, unsigned long a4)
1145{
Al Viro5411be52006-03-29 20:23:36 -05001146 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 struct audit_context *context = tsk->audit_context;
1148 enum audit_state state;
1149
1150 BUG_ON(!context);
1151
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001152 /*
1153 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 * calls in kernel_thread via the entry.S interface, instead of
1155 * with direct calls. (If you are porting to a new
1156 * architecture, hitting this condition can indicate that you
1157 * got the _exit/_leave calls backward in entry.S.)
1158 *
1159 * i386 no
1160 * x86_64 no
Jon Mason2ef94812006-01-23 10:58:20 -06001161 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 *
1163 * This also happens with vm86 emulation in a non-nested manner
1164 * (entries without exits), so this case must be caught.
1165 */
1166 if (context->in_syscall) {
1167 struct audit_context *newctx;
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169#if AUDIT_DEBUG
1170 printk(KERN_ERR
1171 "audit(:%d) pid=%d in syscall=%d;"
1172 " entering syscall=%d\n",
1173 context->serial, tsk->pid, context->major, major);
1174#endif
1175 newctx = audit_alloc_context(context->state);
1176 if (newctx) {
1177 newctx->previous = context;
1178 context = newctx;
1179 tsk->audit_context = newctx;
1180 } else {
1181 /* If we can't alloc a new context, the best we
1182 * can do is to leak memory (any pending putname
1183 * will be lost). The only other alternative is
1184 * to abandon auditing. */
1185 audit_zero_context(context, context->state);
1186 }
1187 }
1188 BUG_ON(context->in_syscall || context->name_count);
1189
1190 if (!audit_enabled)
1191 return;
1192
2fd6f582005-04-29 16:08:28 +01001193 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 context->major = major;
1195 context->argv[0] = a1;
1196 context->argv[1] = a2;
1197 context->argv[2] = a3;
1198 context->argv[3] = a4;
1199
1200 state = context->state;
Al Virod51374a2006-08-03 10:59:26 -04001201 context->dummy = !audit_n_rules;
1202 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
David Woodhouse0f45aa12005-06-19 19:35:50 +01001203 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (likely(state == AUDIT_DISABLED))
1205 return;
1206
David Woodhousece625a82005-07-18 14:24:46 -04001207 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 context->ctime = CURRENT_TIME;
1209 context->in_syscall = 1;
1210 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
Alexander Viro419c58f2006-09-29 00:08:50 -04001211 context->ppid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001214/**
1215 * audit_syscall_exit - deallocate audit context after a system call
1216 * @tsk: task being audited
1217 * @valid: success/failure flag
1218 * @return_code: syscall return value
1219 *
1220 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1222 * filtering, or because some other part of the kernel write an audit
1223 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001224 * free the names stored from getname().
1225 */
Al Viro5411be52006-03-29 20:23:36 -05001226void audit_syscall_exit(int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Al Viro5411be52006-03-29 20:23:36 -05001228 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct audit_context *context;
1230
2fd6f582005-04-29 16:08:28 +01001231 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (likely(!context))
Al Viro97e94c42006-03-29 20:26:24 -05001234 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
David Woodhousef7056d62005-06-20 16:07:33 +01001236 if (context->in_syscall && context->auditable)
Al Viroe4951492006-03-29 20:17:10 -05001237 audit_log_exit(context, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 context->in_syscall = 0;
1240 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (context->previous) {
1243 struct audit_context *new_context = context->previous;
1244 context->previous = NULL;
1245 audit_free_context(context);
1246 tsk->audit_context = new_context;
1247 } else {
1248 audit_free_names(context);
1249 audit_free_aux(context);
Amy Griffise54dc242007-03-29 18:01:04 -04001250 context->aux = NULL;
1251 context->aux_pids = NULL;
Al Viroa5cb0132007-03-20 13:58:35 -04001252 context->target_pid = 0;
Amy Griffise54dc242007-03-29 18:01:04 -04001253 context->target_sid = 0;
Amy Griffis5adc8a62006-06-14 18:45:21 -04001254 kfree(context->filterkey);
1255 context->filterkey = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 tsk->audit_context = context;
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001260/**
1261 * audit_getname - add a name to the list
1262 * @name: name to add
1263 *
1264 * Add a name to the list of audit names for this context.
1265 * Called from fs/namei.c:getname().
1266 */
Al Virod8945bb52006-05-18 16:01:30 -04001267void __audit_getname(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
1269 struct audit_context *context = current->audit_context;
1270
Al Virod8945bb52006-05-18 16:01:30 -04001271 if (IS_ERR(name) || !name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return;
1273
1274 if (!context->in_syscall) {
1275#if AUDIT_DEBUG == 2
1276 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1277 __FILE__, __LINE__, context->serial, name);
1278 dump_stack();
1279#endif
1280 return;
1281 }
1282 BUG_ON(context->name_count >= AUDIT_NAMES);
1283 context->names[context->name_count].name = name;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001284 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1285 context->names[context->name_count].name_put = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 context->names[context->name_count].ino = (unsigned long)-1;
Amy Griffise41e8bd2007-02-13 14:14:09 -05001287 context->names[context->name_count].osid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001289 if (!context->pwd) {
1290 read_lock(&current->fs->lock);
1291 context->pwd = dget(current->fs->pwd);
1292 context->pwdmnt = mntget(current->fs->pwdmnt);
1293 read_unlock(&current->fs->lock);
1294 }
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001298/* audit_putname - intercept a putname request
1299 * @name: name to intercept and delay for putname
1300 *
1301 * If we have stored the name from getname in the audit context,
1302 * then we delay the putname until syscall exit.
1303 * Called from include/linux/fs.h:putname().
1304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305void audit_putname(const char *name)
1306{
1307 struct audit_context *context = current->audit_context;
1308
1309 BUG_ON(!context);
1310 if (!context->in_syscall) {
1311#if AUDIT_DEBUG == 2
1312 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1313 __FILE__, __LINE__, context->serial, name);
1314 if (context->name_count) {
1315 int i;
1316 for (i = 0; i < context->name_count; i++)
1317 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1318 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001319 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321#endif
1322 __putname(name);
1323 }
1324#if AUDIT_DEBUG
1325 else {
1326 ++context->put_count;
1327 if (context->put_count > context->name_count) {
1328 printk(KERN_ERR "%s:%d(:%d): major=%d"
1329 " in_syscall=%d putname(%p) name_count=%d"
1330 " put_count=%d\n",
1331 __FILE__, __LINE__,
1332 context->serial, context->major,
1333 context->in_syscall, name, context->name_count,
1334 context->put_count);
1335 dump_stack();
1336 }
1337 }
1338#endif
1339}
1340
Amy Griffis5712e882007-02-13 14:15:22 -05001341static int audit_inc_name_count(struct audit_context *context,
1342 const struct inode *inode)
1343{
1344 if (context->name_count >= AUDIT_NAMES) {
1345 if (inode)
1346 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1347 "dev=%02x:%02x, inode=%lu",
1348 MAJOR(inode->i_sb->s_dev),
1349 MINOR(inode->i_sb->s_dev),
1350 inode->i_ino);
1351
1352 else
1353 printk(KERN_DEBUG "name_count maxed, losing inode data");
1354 return 1;
1355 }
1356 context->name_count++;
1357#if AUDIT_DEBUG
1358 context->ino_count++;
1359#endif
1360 return 0;
1361}
1362
Amy Griffis3e2efce2006-07-13 13:16:02 -04001363/* Copy inode data into an audit_names. */
1364static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001365{
Amy Griffis3e2efce2006-07-13 13:16:02 -04001366 name->ino = inode->i_ino;
1367 name->dev = inode->i_sb->s_dev;
1368 name->mode = inode->i_mode;
1369 name->uid = inode->i_uid;
1370 name->gid = inode->i_gid;
1371 name->rdev = inode->i_rdev;
1372 selinux_get_inode_sid(inode, &name->osid);
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00001373}
1374
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001375/**
1376 * audit_inode - store the inode and device from a lookup
1377 * @name: name being audited
1378 * @inode: inode being audited
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001379 *
1380 * Called from fs/namei.c:path_lookup().
1381 */
Amy Griffis9c937dc2006-06-08 23:19:31 -04001382void __audit_inode(const char *name, const struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 int idx;
1385 struct audit_context *context = current->audit_context;
1386
1387 if (!context->in_syscall)
1388 return;
1389 if (context->name_count
1390 && context->names[context->name_count-1].name
1391 && context->names[context->name_count-1].name == name)
1392 idx = context->name_count - 1;
1393 else if (context->name_count > 1
1394 && context->names[context->name_count-2].name
1395 && context->names[context->name_count-2].name == name)
1396 idx = context->name_count - 2;
1397 else {
1398 /* FIXME: how much do we care about inodes that have no
1399 * associated name? */
Amy Griffis5712e882007-02-13 14:15:22 -05001400 if (audit_inc_name_count(context, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return;
Amy Griffis5712e882007-02-13 14:15:22 -05001402 idx = context->name_count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 context->names[idx].name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001405 audit_copy_inode(&context->names[idx], inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
Amy Griffis73241cc2005-11-03 16:00:25 +00001408/**
1409 * audit_inode_child - collect inode info for created/removed objects
1410 * @dname: inode's dentry name
1411 * @inode: inode being audited
Amy Griffis73d3ec52006-07-13 13:16:39 -04001412 * @parent: inode of dentry parent
Amy Griffis73241cc2005-11-03 16:00:25 +00001413 *
1414 * For syscalls that create or remove filesystem objects, audit_inode
1415 * can only collect information for the filesystem object's parent.
1416 * This call updates the audit context with the child's information.
1417 * Syscalls that create a new filesystem object must be hooked after
1418 * the object is created. Syscalls that remove a filesystem object
1419 * must be hooked prior, in order to capture the target inode during
1420 * unsuccessful attempts.
1421 */
1422void __audit_inode_child(const char *dname, const struct inode *inode,
Amy Griffis73d3ec52006-07-13 13:16:39 -04001423 const struct inode *parent)
Amy Griffis73241cc2005-11-03 16:00:25 +00001424{
1425 int idx;
1426 struct audit_context *context = current->audit_context;
Amy Griffis5712e882007-02-13 14:15:22 -05001427 const char *found_parent = NULL, *found_child = NULL;
Amy Griffis9c937dc2006-06-08 23:19:31 -04001428 int dirlen = 0;
Amy Griffis73241cc2005-11-03 16:00:25 +00001429
1430 if (!context->in_syscall)
1431 return;
1432
1433 /* determine matching parent */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001434 if (!dname)
Amy Griffis5712e882007-02-13 14:15:22 -05001435 goto add_names;
Amy Griffis73241cc2005-11-03 16:00:25 +00001436
Amy Griffis5712e882007-02-13 14:15:22 -05001437 /* parent is more likely, look for it first */
1438 for (idx = 0; idx < context->name_count; idx++) {
1439 struct audit_names *n = &context->names[idx];
Amy Griffis73241cc2005-11-03 16:00:25 +00001440
Amy Griffis5712e882007-02-13 14:15:22 -05001441 if (!n->name)
1442 continue;
1443
1444 if (n->ino == parent->i_ino &&
1445 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1446 n->name_len = dirlen; /* update parent data in place */
1447 found_parent = n->name;
1448 goto add_names;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001449 }
Steve Grubbac9910c2006-09-28 14:31:32 -04001450 }
Amy Griffis73241cc2005-11-03 16:00:25 +00001451
Amy Griffis5712e882007-02-13 14:15:22 -05001452 /* no matching parent, look for matching child */
1453 for (idx = 0; idx < context->name_count; idx++) {
1454 struct audit_names *n = &context->names[idx];
Amy Griffis73d3ec52006-07-13 13:16:39 -04001455
Amy Griffis5712e882007-02-13 14:15:22 -05001456 if (!n->name)
1457 continue;
1458
1459 /* strcmp() is the more likely scenario */
1460 if (!strcmp(dname, n->name) ||
1461 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1462 if (inode)
1463 audit_copy_inode(n, inode);
1464 else
1465 n->ino = (unsigned long)-1;
1466 found_child = n->name;
1467 goto add_names;
Steve Grubbac9910c2006-09-28 14:31:32 -04001468 }
Amy Griffis5712e882007-02-13 14:15:22 -05001469 }
1470
1471add_names:
1472 if (!found_parent) {
1473 if (audit_inc_name_count(context, parent))
1474 return;
1475 idx = context->name_count - 1;
1476 context->names[idx].name = NULL;
Amy Griffis73d3ec52006-07-13 13:16:39 -04001477 audit_copy_inode(&context->names[idx], parent);
1478 }
Amy Griffis5712e882007-02-13 14:15:22 -05001479
1480 if (!found_child) {
1481 if (audit_inc_name_count(context, inode))
1482 return;
1483 idx = context->name_count - 1;
1484
1485 /* Re-use the name belonging to the slot for a matching parent
1486 * directory. All names for this context are relinquished in
1487 * audit_free_names() */
1488 if (found_parent) {
1489 context->names[idx].name = found_parent;
1490 context->names[idx].name_len = AUDIT_NAME_FULL;
1491 /* don't call __putname() */
1492 context->names[idx].name_put = 0;
1493 } else {
1494 context->names[idx].name = NULL;
1495 }
1496
1497 if (inode)
1498 audit_copy_inode(&context->names[idx], inode);
1499 else
1500 context->names[idx].ino = (unsigned long)-1;
1501 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001502}
1503
1504/**
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001505 * 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}
Steve Grubb0a4ff8c2007-04-19 10:28:21 -04002040
2041/**
2042 * audit_core_dumps - record information about processes that end abnormally
2043 * @sig: signal value
2044 *
2045 * If a process ends with a core dump, something fishy is going on and we
2046 * should record the event for investigation.
2047 */
2048void audit_core_dumps(long signr)
2049{
2050 struct audit_buffer *ab;
2051 u32 sid;
2052
2053 if (!audit_enabled)
2054 return;
2055
2056 if (signr == SIGQUIT) /* don't care for those */
2057 return;
2058
2059 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2060 audit_log_format(ab, "auid=%u uid=%u gid=%u",
2061 audit_get_loginuid(current->audit_context),
2062 current->uid, current->gid);
2063 selinux_get_task_sid(current, &sid);
2064 if (sid) {
2065 char *ctx = NULL;
2066 u32 len;
2067
2068 if (selinux_sid_to_string(sid, &ctx, &len))
2069 audit_log_format(ab, " ssid=%u", sid);
2070 else
2071 audit_log_format(ab, " subj=%s", ctx);
2072 kfree(ctx);
2073 }
2074 audit_log_format(ab, " pid=%d comm=", current->pid);
2075 audit_log_untrustedstring(ab, current->comm);
2076 audit_log_format(ab, " sig=%ld", signr);
2077 audit_log_end(ab);
2078}