| 85c8721 | 2005-04-29 16:23:29 +0100 | [diff] [blame] | 1 | /* auditsc.c -- System-call auditing support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Handles all system-call specific auditing features. |
| 3 | * |
| 4 | * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina. |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 5 | * Copyright 2005 Hewlett-Packard Development Company, L.P. |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 6 | * Copyright (C) 2005, 2006 IBM Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 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. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 32 | * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>, |
| 33 | * 2006. |
| 34 | * |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 35 | * The support of additional filter rules compares (>, <, >=, <=) was |
| 36 | * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005. |
| 37 | * |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 38 | * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional |
| 39 | * filesystem information. |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 40 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | */ |
| 44 | |
| 45 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/types.h> |
Alan Cox | 715b49e | 2006-01-18 17:44:07 -0800 | [diff] [blame] | 47 | #include <asm/atomic.h> |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 48 | #include <asm/types.h> |
| 49 | #include <linux/fs.h> |
| 50 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/mm.h> |
| 52 | #include <linux/module.h> |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 53 | #include <linux/mount.h> |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 54 | #include <linux/socket.h> |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 55 | #include <linux/mqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/audit.h> |
| 57 | #include <linux/personality.h> |
| 58 | #include <linux/time.h> |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 59 | #include <linux/netlink.h> |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 60 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <asm/unistd.h> |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 62 | #include <linux/security.h> |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 63 | #include <linux/list.h> |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 64 | #include <linux/tty.h> |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 65 | #include <linux/selinux.h> |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 66 | #include <linux/binfmts.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 67 | #include <linux/highmem.h> |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 68 | #include <linux/syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 70 | #include "audit.h" |
| 71 | |
| 72 | extern struct list_head audit_filter_list[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | /* No syscall auditing will take place unless audit_enabled != 0. */ |
| 75 | extern 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 Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 81 | /* Indicates that audit should log the full pathname. */ |
| 82 | #define AUDIT_NAME_FULL -1 |
| 83 | |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 84 | /* number of audit rules */ |
| 85 | int audit_n_rules; |
| 86 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 87 | /* determines whether we collect data for signals sent */ |
| 88 | int audit_signals; |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* 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. */ |
| 95 | struct audit_names { |
| 96 | const char *name; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 97 | int name_len; /* number of name's characters to log */ |
| 98 | unsigned name_put; /* call __putname() for this name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | unsigned long ino; |
| 100 | dev_t dev; |
| 101 | umode_t mode; |
| 102 | uid_t uid; |
| 103 | gid_t gid; |
| 104 | dev_t rdev; |
Steve Grubb | 1b50eed | 2006-04-03 14:06:13 -0400 | [diff] [blame] | 105 | u32 osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | struct audit_aux_data { |
| 109 | struct audit_aux_data *next; |
| 110 | int type; |
| 111 | }; |
| 112 | |
| 113 | #define AUDIT_AUX_IPCPERM 0 |
| 114 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 115 | /* Number of target pids per aux struct. */ |
| 116 | #define AUDIT_AUX_PIDS 16 |
| 117 | |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 118 | struct 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 | |
| 125 | struct 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 | |
| 133 | struct audit_aux_data_mq_notify { |
| 134 | struct audit_aux_data d; |
| 135 | mqd_t mqdes; |
| 136 | struct sigevent notification; |
| 137 | }; |
| 138 | |
| 139 | struct audit_aux_data_mq_getsetattr { |
| 140 | struct audit_aux_data d; |
| 141 | mqd_t mqdes; |
| 142 | struct mq_attr mqstat; |
| 143 | }; |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | struct 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 Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 152 | u32 osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 155 | struct audit_aux_data_execve { |
| 156 | struct audit_aux_data d; |
| 157 | int argc; |
| 158 | int envc; |
| 159 | char mem[0]; |
| 160 | }; |
| 161 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 162 | struct audit_aux_data_socketcall { |
| 163 | struct audit_aux_data d; |
| 164 | int nargs; |
| 165 | unsigned long args[0]; |
| 166 | }; |
| 167 | |
| 168 | struct audit_aux_data_sockaddr { |
| 169 | struct audit_aux_data d; |
| 170 | int len; |
| 171 | char a[0]; |
| 172 | }; |
| 173 | |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 174 | struct audit_aux_data_fd_pair { |
| 175 | struct audit_aux_data d; |
| 176 | int fd[2]; |
| 177 | }; |
| 178 | |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 179 | struct audit_aux_data_path { |
| 180 | struct audit_aux_data d; |
| 181 | struct dentry *dentry; |
| 182 | struct vfsmount *mnt; |
| 183 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 185 | struct 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* The per-task audit context. */ |
| 193 | struct audit_context { |
Al Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 194 | int dummy; /* must be the first element */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | 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 */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 203 | long return_code;/* syscall return code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | int auditable; /* 1 if record should be written */ |
| 205 | int name_count; |
| 206 | struct audit_names names[AUDIT_NAMES]; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 207 | char * filterkey; /* key for rule that triggered record */ |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 208 | struct dentry * pwd; |
| 209 | struct vfsmount * pwdmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | struct audit_context *previous; /* For nested syscalls */ |
| 211 | struct audit_aux_data *aux; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 212 | struct audit_aux_data *aux_pids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | /* Save things to print about task_struct */ |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 215 | pid_t pid, ppid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | uid_t uid, euid, suid, fsuid; |
| 217 | gid_t gid, egid, sgid, fsgid; |
| 218 | unsigned long personality; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 219 | int arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 221 | pid_t target_pid; |
| 222 | u32 target_sid; |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | #if AUDIT_DEBUG |
| 225 | int put_count; |
| 226 | int ino_count; |
| 227 | #endif |
| 228 | }; |
| 229 | |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 230 | #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE]) |
| 231 | static 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 | |
| 239 | static 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 Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 278 | /* Determine if any context name data matches a rule's watch data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 |
| 280 | * otherwise. */ |
| 281 | static int audit_filter_rules(struct task_struct *tsk, |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 282 | struct audit_krule *rule, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | struct audit_context *ctx, |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 284 | struct audit_names *name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | enum audit_state *state) |
| 286 | { |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 287 | int i, j, need_sid = 1; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 288 | u32 sid; |
| 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | for (i = 0; i < rule->field_count; i++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 291 | struct audit_field *f = &rule->fields[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | int result = 0; |
| 293 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 294 | switch (f->type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | case AUDIT_PID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 296 | result = audit_comparator(tsk->pid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | break; |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 298 | case AUDIT_PPID: |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 299 | if (ctx) { |
| 300 | if (!ctx->ppid) |
| 301 | ctx->ppid = sys_getppid(); |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 302 | result = audit_comparator(ctx->ppid, f->op, f->val); |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 303 | } |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 304 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | case AUDIT_UID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 306 | result = audit_comparator(tsk->uid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | break; |
| 308 | case AUDIT_EUID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 309 | result = audit_comparator(tsk->euid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | break; |
| 311 | case AUDIT_SUID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 312 | result = audit_comparator(tsk->suid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | break; |
| 314 | case AUDIT_FSUID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 315 | result = audit_comparator(tsk->fsuid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | break; |
| 317 | case AUDIT_GID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 318 | result = audit_comparator(tsk->gid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | break; |
| 320 | case AUDIT_EGID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 321 | result = audit_comparator(tsk->egid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | break; |
| 323 | case AUDIT_SGID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 324 | result = audit_comparator(tsk->sgid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | break; |
| 326 | case AUDIT_FSGID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 327 | result = audit_comparator(tsk->fsgid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | break; |
| 329 | case AUDIT_PERS: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 330 | result = audit_comparator(tsk->personality, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | break; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 332 | case AUDIT_ARCH: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 333 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 334 | result = audit_comparator(ctx->arch, f->op, f->val); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 335 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | case AUDIT_EXIT: |
| 338 | if (ctx && ctx->return_valid) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 339 | result = audit_comparator(ctx->return_code, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | break; |
| 341 | case AUDIT_SUCCESS: |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 342 | if (ctx && ctx->return_valid) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 343 | if (f->val) |
| 344 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 345 | else |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 346 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | break; |
| 349 | case AUDIT_DEVMAJOR: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 350 | if (name) |
| 351 | result = audit_comparator(MAJOR(name->dev), |
| 352 | f->op, f->val); |
| 353 | else if (ctx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 355 | if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | ++result; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | break; |
| 362 | case AUDIT_DEVMINOR: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 363 | if (name) |
| 364 | result = audit_comparator(MINOR(name->dev), |
| 365 | f->op, f->val); |
| 366 | else if (ctx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 368 | if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | ++result; |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | break; |
| 375 | case AUDIT_INODE: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 376 | if (name) |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 377 | result = (name->ino == f->val); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 378 | else if (ctx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 380 | if (audit_comparator(ctx->names[j].ino, f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | ++result; |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 387 | case AUDIT_WATCH: |
| 388 | if (name && rule->watch->ino != (unsigned long)-1) |
| 389 | result = (name->dev == rule->watch->dev && |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 390 | name->ino == rule->watch->ino); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 391 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | case AUDIT_LOGINUID: |
| 393 | result = 0; |
| 394 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 395 | result = audit_comparator(ctx->loginuid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 397 | 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 Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 402 | /* 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 Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 407 | if (f->se_rule) { |
| 408 | if (need_sid) { |
Stephen Smalley | 62bac01 | 2006-09-25 23:31:56 -0700 | [diff] [blame] | 409 | selinux_get_task_sid(tsk, &sid); |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 410 | need_sid = 0; |
| 411 | } |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 412 | result = selinux_audit_rule_match(sid, f->type, |
| 413 | f->op, |
| 414 | f->se_rule, |
| 415 | ctx); |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 416 | } |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 417 | break; |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 418 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | case AUDIT_ARG0: |
| 459 | case AUDIT_ARG1: |
| 460 | case AUDIT_ARG2: |
| 461 | case AUDIT_ARG3: |
| 462 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 463 | result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | break; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 465 | case AUDIT_FILTERKEY: |
| 466 | /* ignore this field for filtering */ |
| 467 | result = 1; |
| 468 | break; |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 469 | case AUDIT_PERM: |
| 470 | result = audit_match_perm(ctx, f->val); |
| 471 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | if (!result) |
| 475 | return 0; |
| 476 | } |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 477 | if (rule->filterkey) |
| 478 | ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | switch (rule->action) { |
| 480 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | 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 | */ |
| 490 | static 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 Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 496 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) { |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 497 | if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | 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 Grubb | 23f32d1 | 2005-05-13 18:35:15 +0100 | [diff] [blame] | 508 | * also not high enough that we already know we have to write an audit |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 509 | * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | */ |
| 511 | static 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 Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 516 | enum audit_state state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
David Woodhouse | 351bb72 | 2005-07-14 14:40:06 +0100 | [diff] [blame] | 518 | if (audit_pid && tsk->tgid == audit_pid) |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 519 | return AUDIT_DISABLED; |
| 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | rcu_read_lock(); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 522 | if (!list_empty(list)) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 523 | int word = AUDIT_WORD(ctx->major); |
| 524 | int bit = AUDIT_BIT(ctx->major); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 525 | |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 526 | list_for_each_entry_rcu(e, list, list) { |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 527 | if ((e->rule.mask[word] & bit) == bit && |
| 528 | audit_filter_rules(tsk, &e->rule, ctx, NULL, |
| 529 | &state)) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 530 | rcu_read_unlock(); |
| 531 | return state; |
| 532 | } |
| 533 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | rcu_read_unlock(); |
| 536 | return AUDIT_BUILD_CONTEXT; |
| 537 | } |
| 538 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 539 | /* 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 | */ |
| 544 | enum 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 | |
| 577 | void audit_set_auditable(struct audit_context *ctx) |
| 578 | { |
| 579 | ctx->auditable = 1; |
| 580 | } |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | static 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 Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 593 | if (context->in_syscall && !context->dummy && !context->auditable) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | enum audit_state state; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 595 | |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 596 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 597 | if (state == AUDIT_RECORD_CONTEXT) { |
| 598 | context->auditable = 1; |
| 599 | goto get_context; |
| 600 | } |
| 601 | |
| 602 | state = audit_filter_inodes(tsk, context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | if (state == AUDIT_RECORD_CONTEXT) |
| 604 | context->auditable = 1; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 608 | get_context: |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | tsk->audit_context = NULL; |
| 611 | return context; |
| 612 | } |
| 613 | |
| 614 | static 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 Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 621 | printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | " name_count=%d put_count=%d" |
| 623 | " ino_count=%d [NOT freeing]\n", |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 624 | __FILE__, __LINE__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | context->serial, context->major, context->in_syscall, |
| 626 | context->name_count, context->put_count, |
| 627 | context->ino_count); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 628 | for (i = 0; i < context->name_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | printk(KERN_ERR "names[%d] = %p = %s\n", i, |
| 630 | context->names[i].name, |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 631 | context->names[i].name ?: "(null)"); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 632 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | 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 Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 642 | for (i = 0; i < context->name_count; i++) { |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 643 | if (context->names[i].name && context->names[i].name_put) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | __putname(context->names[i].name); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | context->name_count = 0; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 647 | if (context->pwd) |
| 648 | dput(context->pwd); |
| 649 | if (context->pwdmnt) |
| 650 | mntput(context->pwdmnt); |
| 651 | context->pwd = NULL; |
| 652 | context->pwdmnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static inline void audit_free_aux(struct audit_context *context) |
| 656 | { |
| 657 | struct audit_aux_data *aux; |
| 658 | |
| 659 | while ((aux = context->aux)) { |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 660 | 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 Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | context->aux = aux->next; |
| 667 | kfree(aux); |
| 668 | } |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 669 | while ((aux = context->aux_pids)) { |
| 670 | context->aux_pids = aux->next; |
| 671 | kfree(aux); |
| 672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static 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 | |
| 685 | static 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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 695 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | * 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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 702 | * needed. |
| 703 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | int 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 | |
| 731 | static 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 Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 747 | kfree(context->filterkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | 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 Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 755 | void audit_log_task_context(struct audit_buffer *ab) |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 756 | { |
| 757 | char *ctx = NULL; |
Al Viro | c4823bc | 2007-03-12 16:17:42 +0000 | [diff] [blame] | 758 | unsigned len; |
| 759 | int error; |
| 760 | u32 sid; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 761 | |
Al Viro | c4823bc | 2007-03-12 16:17:42 +0000 | [diff] [blame] | 762 | 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 Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 769 | goto error_path; |
| 770 | return; |
| 771 | } |
| 772 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 773 | audit_log_format(ab, " subj=%s", ctx); |
Al Viro | c4823bc | 2007-03-12 16:17:42 +0000 | [diff] [blame] | 774 | kfree(ctx); |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame] | 775 | return; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 776 | |
| 777 | error_path: |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame] | 778 | audit_panic("error in audit_log_task_context"); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 779 | return; |
| 780 | } |
| 781 | |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 782 | EXPORT_SYMBOL(audit_log_task_context); |
| 783 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 784 | static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk) |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 785 | { |
Al Viro | 45d9bb0 | 2006-03-29 20:02:55 -0500 | [diff] [blame] | 786 | char name[sizeof(tsk->comm)]; |
| 787 | struct mm_struct *mm = tsk->mm; |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 788 | struct vm_area_struct *vma; |
| 789 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 790 | /* tsk == current */ |
| 791 | |
Al Viro | 45d9bb0 | 2006-03-29 20:02:55 -0500 | [diff] [blame] | 792 | get_task_comm(name, tsk); |
David Woodhouse | 99e45ee | 2005-05-23 21:57:41 +0100 | [diff] [blame] | 793 | audit_log_format(ab, " comm="); |
| 794 | audit_log_untrustedstring(ab, name); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 795 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 796 | 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 Sipek | a7a005f | 2006-12-08 02:37:17 -0800 | [diff] [blame] | 803 | vma->vm_file->f_path.dentry, |
| 804 | vma->vm_file->f_path.mnt); |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 805 | break; |
| 806 | } |
| 807 | vma = vma->vm_next; |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 808 | } |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 809 | up_read(&mm->mmap_sem); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 810 | } |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 811 | audit_log_task_context(ab); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 814 | static 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 Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 837 | static void audit_log_exit(struct audit_context *context, struct task_struct *tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 839 | int i, call_panic = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | struct audit_buffer *ab; |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 841 | struct audit_aux_data *aux; |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 842 | const char *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 844 | /* tsk == current */ |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 845 | context->pid = tsk->pid; |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 846 | if (!context->ppid) |
| 847 | context->ppid = sys_getppid(); |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 848 | 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 Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 857 | |
| 858 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (!ab) |
| 860 | return; /* audit_panic has been called */ |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 861 | audit_log_format(ab, "arch=%x syscall=%d", |
| 862 | context->arch, context->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | if (context->personality != PER_LINUX) |
| 864 | audit_log_format(ab, " per=%lx", context->personality); |
| 865 | if (context->return_valid) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 866 | audit_log_format(ab, " success=%s exit=%ld", |
| 867 | (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", |
| 868 | context->return_code); |
Alan Cox | eb84a20 | 2006-09-29 02:01:41 -0700 | [diff] [blame] | 869 | |
| 870 | mutex_lock(&tty_mutex); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 871 | read_lock(&tasklist_lock); |
Al Viro | 45d9bb0 | 2006-03-29 20:02:55 -0500 | [diff] [blame] | 872 | if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name) |
| 873 | tty = tsk->signal->tty->name; |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 874 | else |
| 875 | tty = "(none)"; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 876 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | audit_log_format(ab, |
| 878 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 879 | " ppid=%d pid=%d auid=%u uid=%u gid=%u" |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 880 | " euid=%u suid=%u fsuid=%u" |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 881 | " egid=%u sgid=%u fsgid=%u tty=%s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | context->argv[0], |
| 883 | context->argv[1], |
| 884 | context->argv[2], |
| 885 | context->argv[3], |
| 886 | context->name_count, |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 887 | context->ppid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | context->pid, |
| 889 | context->loginuid, |
| 890 | context->uid, |
| 891 | context->gid, |
| 892 | context->euid, context->suid, context->fsuid, |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 893 | context->egid, context->sgid, context->fsgid, tty); |
Alan Cox | eb84a20 | 2006-09-29 02:01:41 -0700 | [diff] [blame] | 894 | |
| 895 | mutex_unlock(&tty_mutex); |
| 896 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 897 | audit_log_task_info(ab, tsk); |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 898 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 905 | for (aux = context->aux; aux; aux = aux->next) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 906 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 907 | ab = audit_log_start(context, GFP_KERNEL, aux->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | if (!ab) |
| 909 | continue; /* audit_panic has been called */ |
| 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | switch (aux->type) { |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 912 | 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 Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 949 | case AUDIT_IPC: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 951 | audit_log_format(ab, |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 952 | "ouid=%u ogid=%u mode=%x", |
| 953 | axi->uid, axi->gid, axi->mode); |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 954 | if (axi->osid != 0) { |
| 955 | char *ctx = NULL; |
| 956 | u32 len; |
Stephen Smalley | 1a70cd4 | 2006-09-25 23:31:57 -0700 | [diff] [blame] | 957 | if (selinux_sid_to_string( |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 958 | axi->osid, &ctx, &len)) { |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 959 | audit_log_format(ab, " osid=%u", |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 960 | axi->osid); |
| 961 | call_panic = 1; |
| 962 | } else |
| 963 | audit_log_format(ab, " obj=%s", ctx); |
| 964 | kfree(ctx); |
| 965 | } |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 966 | break; } |
| 967 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 968 | case AUDIT_IPC_SET_PERM: { |
| 969 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 970 | audit_log_format(ab, |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 971 | "qbytes=%lx ouid=%u ogid=%u mode=%x", |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 972 | axi->qbytes, axi->uid, axi->gid, axi->mode); |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 973 | break; } |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 974 | |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 975 | 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 Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 985 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 986 | 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 Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1000 | |
| 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 Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1004 | break; } |
| 1005 | |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 1006 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | } |
| 1012 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1015 | 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 Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 1023 | } |
| 1024 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1025 | if (context->target_pid && |
| 1026 | audit_log_pid_context(context, context->target_pid, |
| 1027 | context->target_sid)) |
| 1028 | call_panic = 1; |
| 1029 | |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1030 | if (context->pwd && context->pwdmnt) { |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1031 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD); |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1032 | if (ab) { |
| 1033 | audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt); |
| 1034 | audit_log_end(ab); |
| 1035 | } |
| 1036 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | for (i = 0; i < context->name_count; i++) { |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1038 | struct audit_names *n = &context->names[i]; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1039 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1040 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | if (!ab) |
| 1042 | continue; /* audit_panic has been called */ |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | audit_log_format(ab, "item=%d", i); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1045 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1046 | 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 Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1067 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1068 | 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 Grubb | 1b50eed | 2006-04-03 14:06:13 -0400 | [diff] [blame] | 1082 | char *ctx = NULL; |
| 1083 | u32 len; |
Stephen Smalley | 1a70cd4 | 2006-09-25 23:31:57 -0700 | [diff] [blame] | 1084 | if (selinux_sid_to_string( |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1085 | n->osid, &ctx, &len)) { |
| 1086 | audit_log_format(ab, " osid=%u", n->osid); |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1087 | call_panic = 2; |
Steve Grubb | 1b50eed | 2006-04-03 14:06:13 -0400 | [diff] [blame] | 1088 | } else |
| 1089 | audit_log_format(ab, " obj=%s", ctx); |
| 1090 | kfree(ctx); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | audit_log_end(ab); |
| 1094 | } |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1095 | if (call_panic) |
| 1096 | audit_panic("error converting sid to string"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1099 | /** |
| 1100 | * audit_free - free a per-task audit context |
| 1101 | * @tsk: task whose audit context block to free |
| 1102 | * |
Al Viro | fa84cb9 | 2006-03-29 20:30:19 -0500 | [diff] [blame] | 1103 | * Called from copy_process and do_exit |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1104 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | void audit_free(struct task_struct *tsk) |
| 1106 | { |
| 1107 | struct audit_context *context; |
| 1108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | context = audit_get_context(tsk, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | if (likely(!context)) |
| 1111 | return; |
| 1112 | |
| 1113 | /* Check for system calls that do not go through the exit |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 1114 | * 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 Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1117 | /* that can happen only if we are called from do_exit() */ |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 1118 | if (context->in_syscall && context->auditable) |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1119 | audit_log_exit(context, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
| 1121 | audit_free_context(context); |
| 1122 | } |
| 1123 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1124 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | * 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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1140 | * be written). |
| 1141 | */ |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1142 | void audit_syscall_entry(int arch, int major, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | unsigned long a1, unsigned long a2, |
| 1144 | unsigned long a3, unsigned long a4) |
| 1145 | { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1146 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | struct audit_context *context = tsk->audit_context; |
| 1148 | enum audit_state state; |
| 1149 | |
| 1150 | BUG_ON(!context); |
| 1151 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1152 | /* |
| 1153 | * This happens only on certain architectures that make system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | * 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 Mason | 2ef9481 | 2006-01-23 10:58:20 -0600 | [diff] [blame] | 1161 | * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | #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 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1193 | context->arch = arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | 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 Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 1201 | context->dummy = !audit_n_rules; |
| 1202 | if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)) |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 1203 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | if (likely(state == AUDIT_DISABLED)) |
| 1205 | return; |
| 1206 | |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 1207 | context->serial = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | context->ctime = CURRENT_TIME; |
| 1209 | context->in_syscall = 1; |
| 1210 | context->auditable = !!(state == AUDIT_RECORD_CONTEXT); |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 1211 | context->ppid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1214 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | * 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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1224 | * free the names stored from getname(). |
| 1225 | */ |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1226 | void audit_syscall_exit(int valid, long return_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1228 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | struct audit_context *context; |
| 1230 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1231 | context = audit_get_context(tsk, valid, return_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | if (likely(!context)) |
Al Viro | 97e94c4 | 2006-03-29 20:26:24 -0500 | [diff] [blame] | 1234 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 1236 | if (context->in_syscall && context->auditable) |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1237 | audit_log_exit(context, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | |
| 1239 | context->in_syscall = 0; |
| 1240 | context->auditable = 0; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | 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 Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1250 | context->aux = NULL; |
| 1251 | context->aux_pids = NULL; |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 1252 | context->target_pid = 0; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1253 | context->target_sid = 0; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 1254 | kfree(context->filterkey); |
| 1255 | context->filterkey = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | tsk->audit_context = context; |
| 1257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1260 | /** |
| 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 Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1267 | void __audit_getname(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | { |
| 1269 | struct audit_context *context = current->audit_context; |
| 1270 | |
Al Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1271 | if (IS_ERR(name) || !name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | 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 Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1284 | context->names[context->name_count].name_len = AUDIT_NAME_FULL; |
| 1285 | context->names[context->name_count].name_put = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | context->names[context->name_count].ino = (unsigned long)-1; |
Amy Griffis | e41e8bd | 2007-02-13 14:14:09 -0500 | [diff] [blame] | 1287 | context->names[context->name_count].osid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | ++context->name_count; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1289 | if (!context->pwd) { |
| 1290 | read_lock(¤t->fs->lock); |
| 1291 | context->pwd = dget(current->fs->pwd); |
| 1292 | context->pwdmnt = mntget(current->fs->pwdmnt); |
| 1293 | read_unlock(¤t->fs->lock); |
| 1294 | } |
| 1295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | } |
| 1297 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1298 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | void 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 Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1319 | context->names[i].name ?: "(null)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | } |
| 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 Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1341 | static 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 Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1363 | /* Copy inode data into an audit_names. */ |
| 1364 | static void audit_copy_inode(struct audit_names *name, const struct inode *inode) |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1365 | { |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1366 | 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 Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1375 | /** |
| 1376 | * audit_inode - store the inode and device from a lookup |
| 1377 | * @name: name being audited |
| 1378 | * @inode: inode being audited |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1379 | * |
| 1380 | * Called from fs/namei.c:path_lookup(). |
| 1381 | */ |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1382 | void __audit_inode(const char *name, const struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | { |
| 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 Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1400 | if (audit_inc_name_count(context, inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | return; |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1402 | idx = context->name_count - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | context->names[idx].name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1405 | audit_copy_inode(&context->names[idx], inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1408 | /** |
| 1409 | * audit_inode_child - collect inode info for created/removed objects |
| 1410 | * @dname: inode's dentry name |
| 1411 | * @inode: inode being audited |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1412 | * @parent: inode of dentry parent |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1413 | * |
| 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 | */ |
| 1422 | void __audit_inode_child(const char *dname, const struct inode *inode, |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1423 | const struct inode *parent) |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1424 | { |
| 1425 | int idx; |
| 1426 | struct audit_context *context = current->audit_context; |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1427 | const char *found_parent = NULL, *found_child = NULL; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1428 | int dirlen = 0; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1429 | |
| 1430 | if (!context->in_syscall) |
| 1431 | return; |
| 1432 | |
| 1433 | /* determine matching parent */ |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1434 | if (!dname) |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1435 | goto add_names; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1436 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1437 | /* 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 Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1440 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1441 | 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 Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1449 | } |
Steve Grubb | ac9910c | 2006-09-28 14:31:32 -0400 | [diff] [blame] | 1450 | } |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1451 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1452 | /* 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 Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1455 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1456 | 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 Grubb | ac9910c | 2006-09-28 14:31:32 -0400 | [diff] [blame] | 1468 | } |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | add_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 Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1477 | audit_copy_inode(&context->names[idx], parent); |
| 1478 | } |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1479 | |
| 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 Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 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 Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1512 | void auditsc_get_stamp(struct audit_context *ctx, |
| 1513 | struct timespec *t, unsigned int *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | { |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 1515 | if (!ctx->serial) |
| 1516 | ctx->serial = audit_serial(); |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1517 | t->tv_sec = ctx->ctime.tv_sec; |
| 1518 | t->tv_nsec = ctx->ctime.tv_nsec; |
| 1519 | *serial = ctx->serial; |
| 1520 | ctx->auditable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1523 | /** |
| 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 Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1532 | int audit_set_loginuid(struct task_struct *task, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | { |
Steve Grubb | 4175710 | 2006-06-12 07:48:28 -0400 | [diff] [blame] | 1534 | struct audit_context *context = task->audit_context; |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1535 | |
Steve Grubb | 4175710 | 2006-06-12 07:48:28 -0400 | [diff] [blame] | 1536 | 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 Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1549 | } |
Steve Grubb | 4175710 | 2006-06-12 07:48:28 -0400 | [diff] [blame] | 1550 | context->loginuid = loginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | } |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1555 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | uid_t audit_get_loginuid(struct audit_context *ctx) |
| 1562 | { |
| 1563 | return ctx ? ctx->loginuid : -1; |
| 1564 | } |
| 1565 | |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 1566 | EXPORT_SYMBOL(audit_get_loginuid); |
| 1567 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1568 | /** |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1569 | * __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 | */ |
| 1576 | int __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 Dunlap | 1dbe83c | 2006-06-27 02:54:01 -0700 | [diff] [blame] | 1613 | * @u_abs_timeout: Message timeout in absolute time |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1614 | * |
| 1615 | * Returns 0 for success or NULL context or < 0 on error. |
| 1616 | */ |
| 1617 | int __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 Dunlap | 1dbe83c | 2006-06-27 02:54:01 -0700 | [diff] [blame] | 1655 | * @u_msg_prio: Message priority |
| 1656 | * @u_abs_timeout: Message timeout in absolute time |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1657 | * |
| 1658 | * Returns 0 for success or NULL context or < 0 on error. |
| 1659 | */ |
| 1660 | int __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 | |
| 1710 | int __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 | */ |
| 1748 | int __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 Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1773 | * 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 Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1778 | int __audit_ipc_obj(struct kern_ipc_perm *ipcp) |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1779 | { |
| 1780 | struct audit_aux_data_ipcctl *ax; |
| 1781 | struct audit_context *context = current->audit_context; |
| 1782 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1783 | 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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1800 | * @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 Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1807 | int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | { |
| 1809 | struct audit_aux_data_ipcctl *ax; |
| 1810 | struct audit_context *context = current->audit_context; |
| 1811 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1812 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | if (!ax) |
| 1814 | return -ENOMEM; |
| 1815 | |
| 1816 | ax->qbytes = qbytes; |
| 1817 | ax->uid = uid; |
| 1818 | ax->gid = gid; |
| 1819 | ax->mode = mode; |
| 1820 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1821 | ax->d.type = AUDIT_IPC_SET_PERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | ax->d.next = context->aux; |
| 1823 | context->aux = (void *)ax; |
| 1824 | return 0; |
| 1825 | } |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1826 | |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1827 | int 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 Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 1834 | if (likely(!audit_enabled || !context || context->dummy)) |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1835 | 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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1860 | /** |
| 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 Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1867 | int 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 Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 1872 | if (likely(!context || context->dummy)) |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1873 | 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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1888 | /** |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 1889 | * __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 | */ |
| 1895 | int __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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1919 | * 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 Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1925 | int audit_sockaddr(int len, void *a) |
| 1926 | { |
| 1927 | struct audit_aux_data_sockaddr *ax; |
| 1928 | struct audit_context *context = current->audit_context; |
| 1929 | |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 1930 | if (likely(!context || context->dummy)) |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1931 | 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 Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 1946 | void __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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1954 | /** |
| 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 Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1963 | int 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 Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1984 | /** |
| 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 Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1992 | int __audit_signal_info(int sig, struct task_struct *t) |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1993 | { |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1994 | struct audit_aux_data_pids *axp; |
| 1995 | struct task_struct *tsk = current; |
| 1996 | struct audit_context *ctx = tsk->audit_context; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1997 | extern pid_t audit_sig_pid; |
| 1998 | extern uid_t audit_sig_uid; |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 1999 | extern u32 audit_sig_sid; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2000 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2001 | if (audit_pid && t->tgid == audit_pid && |
| 2002 | (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1)) { |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 2003 | 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 Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2009 | } |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2010 | |
| 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 Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2039 | } |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2040 | |
| 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 | */ |
| 2048 | void 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 | } |