| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* AUDIT_NAMES is the number of slots we reserve in the audit_context |
| 75 | * for saving names from getname(). */ |
| 76 | #define AUDIT_NAMES 20 |
| 77 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 78 | /* Indicates that audit should log the full pathname. */ |
| 79 | #define AUDIT_NAME_FULL -1 |
| 80 | |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 81 | /* number of audit rules */ |
| 82 | int audit_n_rules; |
| 83 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 84 | /* determines whether we collect data for signals sent */ |
| 85 | int audit_signals; |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* When fs/namei.c:getname() is called, we store the pointer in name and |
| 88 | * we don't let putname() free it (instead we free all of the saved |
| 89 | * pointers at syscall exit time). |
| 90 | * |
| 91 | * Further, in fs/namei.c:path_lookup() we store the inode and device. */ |
| 92 | struct audit_names { |
| 93 | const char *name; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 94 | int name_len; /* number of name's characters to log */ |
| 95 | unsigned name_put; /* call __putname() for this name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | unsigned long ino; |
| 97 | dev_t dev; |
| 98 | umode_t mode; |
| 99 | uid_t uid; |
| 100 | gid_t gid; |
| 101 | dev_t rdev; |
Steve Grubb | 1b50eed | 2006-04-03 14:06:13 -0400 | [diff] [blame] | 102 | u32 osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | struct audit_aux_data { |
| 106 | struct audit_aux_data *next; |
| 107 | int type; |
| 108 | }; |
| 109 | |
| 110 | #define AUDIT_AUX_IPCPERM 0 |
| 111 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 112 | /* Number of target pids per aux struct. */ |
| 113 | #define AUDIT_AUX_PIDS 16 |
| 114 | |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 115 | struct audit_aux_data_mq_open { |
| 116 | struct audit_aux_data d; |
| 117 | int oflag; |
| 118 | mode_t mode; |
| 119 | struct mq_attr attr; |
| 120 | }; |
| 121 | |
| 122 | struct audit_aux_data_mq_sendrecv { |
| 123 | struct audit_aux_data d; |
| 124 | mqd_t mqdes; |
| 125 | size_t msg_len; |
| 126 | unsigned int msg_prio; |
| 127 | struct timespec abs_timeout; |
| 128 | }; |
| 129 | |
| 130 | struct audit_aux_data_mq_notify { |
| 131 | struct audit_aux_data d; |
| 132 | mqd_t mqdes; |
| 133 | struct sigevent notification; |
| 134 | }; |
| 135 | |
| 136 | struct audit_aux_data_mq_getsetattr { |
| 137 | struct audit_aux_data d; |
| 138 | mqd_t mqdes; |
| 139 | struct mq_attr mqstat; |
| 140 | }; |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | struct audit_aux_data_ipcctl { |
| 143 | struct audit_aux_data d; |
| 144 | struct ipc_perm p; |
| 145 | unsigned long qbytes; |
| 146 | uid_t uid; |
| 147 | gid_t gid; |
| 148 | mode_t mode; |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 149 | u32 osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 152 | struct audit_aux_data_execve { |
| 153 | struct audit_aux_data d; |
| 154 | int argc; |
| 155 | int envc; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 156 | struct mm_struct *mm; |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 157 | }; |
| 158 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 159 | struct audit_aux_data_socketcall { |
| 160 | struct audit_aux_data d; |
| 161 | int nargs; |
| 162 | unsigned long args[0]; |
| 163 | }; |
| 164 | |
| 165 | struct audit_aux_data_sockaddr { |
| 166 | struct audit_aux_data d; |
| 167 | int len; |
| 168 | char a[0]; |
| 169 | }; |
| 170 | |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 171 | struct audit_aux_data_fd_pair { |
| 172 | struct audit_aux_data d; |
| 173 | int fd[2]; |
| 174 | }; |
| 175 | |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 176 | struct audit_aux_data_path { |
| 177 | struct audit_aux_data d; |
| 178 | struct dentry *dentry; |
| 179 | struct vfsmount *mnt; |
| 180 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 182 | struct audit_aux_data_pids { |
| 183 | struct audit_aux_data d; |
| 184 | pid_t target_pid[AUDIT_AUX_PIDS]; |
| 185 | u32 target_sid[AUDIT_AUX_PIDS]; |
| 186 | int pid_count; |
| 187 | }; |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* The per-task audit context. */ |
| 190 | struct audit_context { |
Al Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 191 | int dummy; /* must be the first element */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | int in_syscall; /* 1 if task is in a syscall */ |
| 193 | enum audit_state state; |
| 194 | unsigned int serial; /* serial number for record */ |
| 195 | struct timespec ctime; /* time of syscall entry */ |
| 196 | uid_t loginuid; /* login uid (identity) */ |
| 197 | int major; /* syscall number */ |
| 198 | unsigned long argv[4]; /* syscall arguments */ |
| 199 | int return_valid; /* return code is valid */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 200 | long return_code;/* syscall return code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | int auditable; /* 1 if record should be written */ |
| 202 | int name_count; |
| 203 | struct audit_names names[AUDIT_NAMES]; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 204 | char * filterkey; /* key for rule that triggered record */ |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 205 | struct dentry * pwd; |
| 206 | struct vfsmount * pwdmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | struct audit_context *previous; /* For nested syscalls */ |
| 208 | struct audit_aux_data *aux; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 209 | struct audit_aux_data *aux_pids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | /* Save things to print about task_struct */ |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 212 | pid_t pid, ppid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | uid_t uid, euid, suid, fsuid; |
| 214 | gid_t gid, egid, sgid, fsgid; |
| 215 | unsigned long personality; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 216 | int arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 218 | pid_t target_pid; |
| 219 | u32 target_sid; |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | #if AUDIT_DEBUG |
| 222 | int put_count; |
| 223 | int ino_count; |
| 224 | #endif |
| 225 | }; |
| 226 | |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 227 | #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE]) |
| 228 | static inline int open_arg(int flags, int mask) |
| 229 | { |
| 230 | int n = ACC_MODE(flags); |
| 231 | if (flags & (O_TRUNC | O_CREAT)) |
| 232 | n |= AUDIT_PERM_WRITE; |
| 233 | return n & mask; |
| 234 | } |
| 235 | |
| 236 | static int audit_match_perm(struct audit_context *ctx, int mask) |
| 237 | { |
| 238 | unsigned n = ctx->major; |
| 239 | switch (audit_classify_syscall(ctx->arch, n)) { |
| 240 | case 0: /* native */ |
| 241 | if ((mask & AUDIT_PERM_WRITE) && |
| 242 | audit_match_class(AUDIT_CLASS_WRITE, n)) |
| 243 | return 1; |
| 244 | if ((mask & AUDIT_PERM_READ) && |
| 245 | audit_match_class(AUDIT_CLASS_READ, n)) |
| 246 | return 1; |
| 247 | if ((mask & AUDIT_PERM_ATTR) && |
| 248 | audit_match_class(AUDIT_CLASS_CHATTR, n)) |
| 249 | return 1; |
| 250 | return 0; |
| 251 | case 1: /* 32bit on biarch */ |
| 252 | if ((mask & AUDIT_PERM_WRITE) && |
| 253 | audit_match_class(AUDIT_CLASS_WRITE_32, n)) |
| 254 | return 1; |
| 255 | if ((mask & AUDIT_PERM_READ) && |
| 256 | audit_match_class(AUDIT_CLASS_READ_32, n)) |
| 257 | return 1; |
| 258 | if ((mask & AUDIT_PERM_ATTR) && |
| 259 | audit_match_class(AUDIT_CLASS_CHATTR_32, n)) |
| 260 | return 1; |
| 261 | return 0; |
| 262 | case 2: /* open */ |
| 263 | return mask & ACC_MODE(ctx->argv[1]); |
| 264 | case 3: /* openat */ |
| 265 | return mask & ACC_MODE(ctx->argv[2]); |
| 266 | case 4: /* socketcall */ |
| 267 | return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND); |
| 268 | case 5: /* execve */ |
| 269 | return mask & AUDIT_PERM_EXEC; |
| 270 | default: |
| 271 | return 0; |
| 272 | } |
| 273 | } |
| 274 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 275 | /* Determine if any context name data matches a rule's watch data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 |
| 277 | * otherwise. */ |
| 278 | static int audit_filter_rules(struct task_struct *tsk, |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 279 | struct audit_krule *rule, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | struct audit_context *ctx, |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 281 | struct audit_names *name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | enum audit_state *state) |
| 283 | { |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 284 | int i, j, need_sid = 1; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 285 | u32 sid; |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | for (i = 0; i < rule->field_count; i++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 288 | struct audit_field *f = &rule->fields[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | int result = 0; |
| 290 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 291 | switch (f->type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | case AUDIT_PID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 293 | result = audit_comparator(tsk->pid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | break; |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 295 | case AUDIT_PPID: |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 296 | if (ctx) { |
| 297 | if (!ctx->ppid) |
| 298 | ctx->ppid = sys_getppid(); |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 299 | result = audit_comparator(ctx->ppid, f->op, f->val); |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 300 | } |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 301 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | case AUDIT_UID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 303 | result = audit_comparator(tsk->uid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | break; |
| 305 | case AUDIT_EUID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 306 | result = audit_comparator(tsk->euid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | break; |
| 308 | case AUDIT_SUID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 309 | result = audit_comparator(tsk->suid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | break; |
| 311 | case AUDIT_FSUID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 312 | result = audit_comparator(tsk->fsuid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | break; |
| 314 | case AUDIT_GID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 315 | result = audit_comparator(tsk->gid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | break; |
| 317 | case AUDIT_EGID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 318 | result = audit_comparator(tsk->egid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | break; |
| 320 | case AUDIT_SGID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 321 | result = audit_comparator(tsk->sgid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | break; |
| 323 | case AUDIT_FSGID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 324 | result = audit_comparator(tsk->fsgid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | break; |
| 326 | case AUDIT_PERS: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 327 | result = audit_comparator(tsk->personality, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | break; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 329 | case AUDIT_ARCH: |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 330 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 331 | result = audit_comparator(ctx->arch, f->op, f->val); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 332 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | case AUDIT_EXIT: |
| 335 | if (ctx && ctx->return_valid) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 336 | result = audit_comparator(ctx->return_code, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | break; |
| 338 | case AUDIT_SUCCESS: |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 339 | if (ctx && ctx->return_valid) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 340 | if (f->val) |
| 341 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 342 | else |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 343 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | break; |
| 346 | case AUDIT_DEVMAJOR: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 347 | if (name) |
| 348 | result = audit_comparator(MAJOR(name->dev), |
| 349 | f->op, f->val); |
| 350 | else if (ctx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 352 | if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | ++result; |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | break; |
| 359 | case AUDIT_DEVMINOR: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 360 | if (name) |
| 361 | result = audit_comparator(MINOR(name->dev), |
| 362 | f->op, f->val); |
| 363 | else if (ctx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 365 | if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | ++result; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | break; |
| 372 | case AUDIT_INODE: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 373 | if (name) |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 374 | result = (name->ino == f->val); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 375 | else if (ctx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 377 | if (audit_comparator(ctx->names[j].ino, f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | ++result; |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 384 | case AUDIT_WATCH: |
| 385 | if (name && rule->watch->ino != (unsigned long)-1) |
| 386 | result = (name->dev == rule->watch->dev && |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 387 | name->ino == rule->watch->ino); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 388 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | case AUDIT_LOGINUID: |
| 390 | result = 0; |
| 391 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 392 | result = audit_comparator(ctx->loginuid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 394 | case AUDIT_SUBJ_USER: |
| 395 | case AUDIT_SUBJ_ROLE: |
| 396 | case AUDIT_SUBJ_TYPE: |
| 397 | case AUDIT_SUBJ_SEN: |
| 398 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 399 | /* NOTE: this may return negative values indicating |
| 400 | a temporary error. We simply treat this as a |
| 401 | match for now to avoid losing information that |
| 402 | may be wanted. An error message will also be |
| 403 | logged upon error */ |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 404 | if (f->se_rule) { |
| 405 | if (need_sid) { |
Stephen Smalley | 62bac01 | 2006-09-25 23:31:56 -0700 | [diff] [blame] | 406 | selinux_get_task_sid(tsk, &sid); |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 407 | need_sid = 0; |
| 408 | } |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 409 | result = selinux_audit_rule_match(sid, f->type, |
| 410 | f->op, |
| 411 | f->se_rule, |
| 412 | ctx); |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 413 | } |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 414 | break; |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 415 | case AUDIT_OBJ_USER: |
| 416 | case AUDIT_OBJ_ROLE: |
| 417 | case AUDIT_OBJ_TYPE: |
| 418 | case AUDIT_OBJ_LEV_LOW: |
| 419 | case AUDIT_OBJ_LEV_HIGH: |
| 420 | /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR |
| 421 | also applies here */ |
| 422 | if (f->se_rule) { |
| 423 | /* Find files that match */ |
| 424 | if (name) { |
| 425 | result = selinux_audit_rule_match( |
| 426 | name->osid, f->type, f->op, |
| 427 | f->se_rule, ctx); |
| 428 | } else if (ctx) { |
| 429 | for (j = 0; j < ctx->name_count; j++) { |
| 430 | if (selinux_audit_rule_match( |
| 431 | ctx->names[j].osid, |
| 432 | f->type, f->op, |
| 433 | f->se_rule, ctx)) { |
| 434 | ++result; |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | /* Find ipc objects that match */ |
| 440 | if (ctx) { |
| 441 | struct audit_aux_data *aux; |
| 442 | for (aux = ctx->aux; aux; |
| 443 | aux = aux->next) { |
| 444 | if (aux->type == AUDIT_IPC) { |
| 445 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 446 | if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) { |
| 447 | ++result; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | case AUDIT_ARG0: |
| 456 | case AUDIT_ARG1: |
| 457 | case AUDIT_ARG2: |
| 458 | case AUDIT_ARG3: |
| 459 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 460 | 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] | 461 | break; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 462 | case AUDIT_FILTERKEY: |
| 463 | /* ignore this field for filtering */ |
| 464 | result = 1; |
| 465 | break; |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 466 | case AUDIT_PERM: |
| 467 | result = audit_match_perm(ctx, f->val); |
| 468 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | if (!result) |
| 472 | return 0; |
| 473 | } |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 474 | if (rule->filterkey) |
| 475 | ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | switch (rule->action) { |
| 477 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
| 479 | } |
| 480 | return 1; |
| 481 | } |
| 482 | |
| 483 | /* At process creation time, we can determine if system-call auditing is |
| 484 | * completely disabled for this task. Since we only have the task |
| 485 | * structure at this point, we can only check uid and gid. |
| 486 | */ |
| 487 | static enum audit_state audit_filter_task(struct task_struct *tsk) |
| 488 | { |
| 489 | struct audit_entry *e; |
| 490 | enum audit_state state; |
| 491 | |
| 492 | rcu_read_lock(); |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 493 | 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] | 494 | if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | rcu_read_unlock(); |
| 496 | return state; |
| 497 | } |
| 498 | } |
| 499 | rcu_read_unlock(); |
| 500 | return AUDIT_BUILD_CONTEXT; |
| 501 | } |
| 502 | |
| 503 | /* At syscall entry and exit time, this filter is called if the |
| 504 | * 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] | 505 | * 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] | 506 | * 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] | 507 | */ |
| 508 | static enum audit_state audit_filter_syscall(struct task_struct *tsk, |
| 509 | struct audit_context *ctx, |
| 510 | struct list_head *list) |
| 511 | { |
| 512 | struct audit_entry *e; |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 513 | enum audit_state state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
David Woodhouse | 351bb72 | 2005-07-14 14:40:06 +0100 | [diff] [blame] | 515 | if (audit_pid && tsk->tgid == audit_pid) |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 516 | return AUDIT_DISABLED; |
| 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | rcu_read_lock(); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 519 | if (!list_empty(list)) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 520 | int word = AUDIT_WORD(ctx->major); |
| 521 | int bit = AUDIT_BIT(ctx->major); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 522 | |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 523 | list_for_each_entry_rcu(e, list, list) { |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 524 | if ((e->rule.mask[word] & bit) == bit && |
| 525 | audit_filter_rules(tsk, &e->rule, ctx, NULL, |
| 526 | &state)) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 527 | rcu_read_unlock(); |
| 528 | return state; |
| 529 | } |
| 530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | rcu_read_unlock(); |
| 533 | return AUDIT_BUILD_CONTEXT; |
| 534 | } |
| 535 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 536 | /* At syscall exit time, this filter is called if any audit_names[] have been |
| 537 | * collected during syscall processing. We only check rules in sublists at hash |
| 538 | * buckets applicable to the inode numbers in audit_names[]. |
| 539 | * Regarding audit_state, same rules apply as for audit_filter_syscall(). |
| 540 | */ |
| 541 | enum audit_state audit_filter_inodes(struct task_struct *tsk, |
| 542 | struct audit_context *ctx) |
| 543 | { |
| 544 | int i; |
| 545 | struct audit_entry *e; |
| 546 | enum audit_state state; |
| 547 | |
| 548 | if (audit_pid && tsk->tgid == audit_pid) |
| 549 | return AUDIT_DISABLED; |
| 550 | |
| 551 | rcu_read_lock(); |
| 552 | for (i = 0; i < ctx->name_count; i++) { |
| 553 | int word = AUDIT_WORD(ctx->major); |
| 554 | int bit = AUDIT_BIT(ctx->major); |
| 555 | struct audit_names *n = &ctx->names[i]; |
| 556 | int h = audit_hash_ino((u32)n->ino); |
| 557 | struct list_head *list = &audit_inode_hash[h]; |
| 558 | |
| 559 | if (list_empty(list)) |
| 560 | continue; |
| 561 | |
| 562 | list_for_each_entry_rcu(e, list, list) { |
| 563 | if ((e->rule.mask[word] & bit) == bit && |
| 564 | audit_filter_rules(tsk, &e->rule, ctx, n, &state)) { |
| 565 | rcu_read_unlock(); |
| 566 | return state; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | rcu_read_unlock(); |
| 571 | return AUDIT_BUILD_CONTEXT; |
| 572 | } |
| 573 | |
| 574 | void audit_set_auditable(struct audit_context *ctx) |
| 575 | { |
| 576 | ctx->auditable = 1; |
| 577 | } |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | static inline struct audit_context *audit_get_context(struct task_struct *tsk, |
| 580 | int return_valid, |
| 581 | int return_code) |
| 582 | { |
| 583 | struct audit_context *context = tsk->audit_context; |
| 584 | |
| 585 | if (likely(!context)) |
| 586 | return NULL; |
| 587 | context->return_valid = return_valid; |
| 588 | context->return_code = return_code; |
| 589 | |
Al Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 590 | if (context->in_syscall && !context->dummy && !context->auditable) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | enum audit_state state; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 592 | |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 593 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 594 | if (state == AUDIT_RECORD_CONTEXT) { |
| 595 | context->auditable = 1; |
| 596 | goto get_context; |
| 597 | } |
| 598 | |
| 599 | state = audit_filter_inodes(tsk, context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | if (state == AUDIT_RECORD_CONTEXT) |
| 601 | context->auditable = 1; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 605 | get_context: |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | tsk->audit_context = NULL; |
| 608 | return context; |
| 609 | } |
| 610 | |
| 611 | static inline void audit_free_names(struct audit_context *context) |
| 612 | { |
| 613 | int i; |
| 614 | |
| 615 | #if AUDIT_DEBUG == 2 |
| 616 | if (context->auditable |
| 617 | ||context->put_count + context->ino_count != context->name_count) { |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 618 | printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | " name_count=%d put_count=%d" |
| 620 | " ino_count=%d [NOT freeing]\n", |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 621 | __FILE__, __LINE__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | context->serial, context->major, context->in_syscall, |
| 623 | context->name_count, context->put_count, |
| 624 | context->ino_count); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 625 | for (i = 0; i < context->name_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | printk(KERN_ERR "names[%d] = %p = %s\n", i, |
| 627 | context->names[i].name, |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 628 | context->names[i].name ?: "(null)"); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | dump_stack(); |
| 631 | return; |
| 632 | } |
| 633 | #endif |
| 634 | #if AUDIT_DEBUG |
| 635 | context->put_count = 0; |
| 636 | context->ino_count = 0; |
| 637 | #endif |
| 638 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 639 | for (i = 0; i < context->name_count; i++) { |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 640 | if (context->names[i].name && context->names[i].name_put) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | __putname(context->names[i].name); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 642 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | context->name_count = 0; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 644 | if (context->pwd) |
| 645 | dput(context->pwd); |
| 646 | if (context->pwdmnt) |
| 647 | mntput(context->pwdmnt); |
| 648 | context->pwd = NULL; |
| 649 | context->pwdmnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static inline void audit_free_aux(struct audit_context *context) |
| 653 | { |
| 654 | struct audit_aux_data *aux; |
| 655 | |
| 656 | while ((aux = context->aux)) { |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 657 | if (aux->type == AUDIT_AVC_PATH) { |
| 658 | struct audit_aux_data_path *axi = (void *)aux; |
| 659 | dput(axi->dentry); |
| 660 | mntput(axi->mnt); |
| 661 | } |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | context->aux = aux->next; |
| 664 | kfree(aux); |
| 665 | } |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 666 | while ((aux = context->aux_pids)) { |
| 667 | context->aux_pids = aux->next; |
| 668 | kfree(aux); |
| 669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static inline void audit_zero_context(struct audit_context *context, |
| 673 | enum audit_state state) |
| 674 | { |
| 675 | uid_t loginuid = context->loginuid; |
| 676 | |
| 677 | memset(context, 0, sizeof(*context)); |
| 678 | context->state = state; |
| 679 | context->loginuid = loginuid; |
| 680 | } |
| 681 | |
| 682 | static inline struct audit_context *audit_alloc_context(enum audit_state state) |
| 683 | { |
| 684 | struct audit_context *context; |
| 685 | |
| 686 | if (!(context = kmalloc(sizeof(*context), GFP_KERNEL))) |
| 687 | return NULL; |
| 688 | audit_zero_context(context, state); |
| 689 | return context; |
| 690 | } |
| 691 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 692 | /** |
| 693 | * audit_alloc - allocate an audit context block for a task |
| 694 | * @tsk: task |
| 695 | * |
| 696 | * Filter on the task information and allocate a per-task audit context |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | * if necessary. Doing so turns on system call auditing for the |
| 698 | * specified task. This is called from copy_process, so no lock is |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 699 | * needed. |
| 700 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | int audit_alloc(struct task_struct *tsk) |
| 702 | { |
| 703 | struct audit_context *context; |
| 704 | enum audit_state state; |
| 705 | |
| 706 | if (likely(!audit_enabled)) |
| 707 | return 0; /* Return if not auditing. */ |
| 708 | |
| 709 | state = audit_filter_task(tsk); |
| 710 | if (likely(state == AUDIT_DISABLED)) |
| 711 | return 0; |
| 712 | |
| 713 | if (!(context = audit_alloc_context(state))) { |
| 714 | audit_log_lost("out of memory in audit_alloc"); |
| 715 | return -ENOMEM; |
| 716 | } |
| 717 | |
| 718 | /* Preserve login uid */ |
| 719 | context->loginuid = -1; |
| 720 | if (current->audit_context) |
| 721 | context->loginuid = current->audit_context->loginuid; |
| 722 | |
| 723 | tsk->audit_context = context; |
| 724 | set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static inline void audit_free_context(struct audit_context *context) |
| 729 | { |
| 730 | struct audit_context *previous; |
| 731 | int count = 0; |
| 732 | |
| 733 | do { |
| 734 | previous = context->previous; |
| 735 | if (previous || (count && count < 10)) { |
| 736 | ++count; |
| 737 | printk(KERN_ERR "audit(:%d): major=%d name_count=%d:" |
| 738 | " freeing multiple contexts (%d)\n", |
| 739 | context->serial, context->major, |
| 740 | context->name_count, count); |
| 741 | } |
| 742 | audit_free_names(context); |
| 743 | audit_free_aux(context); |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 744 | kfree(context->filterkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | kfree(context); |
| 746 | context = previous; |
| 747 | } while (context); |
| 748 | if (count >= 10) |
| 749 | printk(KERN_ERR "audit: freed %d contexts\n", count); |
| 750 | } |
| 751 | |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 752 | void audit_log_task_context(struct audit_buffer *ab) |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 753 | { |
| 754 | char *ctx = NULL; |
Al Viro | c4823bc | 2007-03-12 16:17:42 +0000 | [diff] [blame] | 755 | unsigned len; |
| 756 | int error; |
| 757 | u32 sid; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 758 | |
Al Viro | c4823bc | 2007-03-12 16:17:42 +0000 | [diff] [blame] | 759 | selinux_get_task_sid(current, &sid); |
| 760 | if (!sid) |
| 761 | return; |
| 762 | |
| 763 | error = selinux_sid_to_string(sid, &ctx, &len); |
| 764 | if (error) { |
| 765 | if (error != -EINVAL) |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 766 | goto error_path; |
| 767 | return; |
| 768 | } |
| 769 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 770 | audit_log_format(ab, " subj=%s", ctx); |
Al Viro | c4823bc | 2007-03-12 16:17:42 +0000 | [diff] [blame] | 771 | kfree(ctx); |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame] | 772 | return; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 773 | |
| 774 | error_path: |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame] | 775 | audit_panic("error in audit_log_task_context"); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 779 | EXPORT_SYMBOL(audit_log_task_context); |
| 780 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 781 | 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] | 782 | { |
Al Viro | 45d9bb0 | 2006-03-29 20:02:55 -0500 | [diff] [blame] | 783 | char name[sizeof(tsk->comm)]; |
| 784 | struct mm_struct *mm = tsk->mm; |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 785 | struct vm_area_struct *vma; |
| 786 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 787 | /* tsk == current */ |
| 788 | |
Al Viro | 45d9bb0 | 2006-03-29 20:02:55 -0500 | [diff] [blame] | 789 | get_task_comm(name, tsk); |
David Woodhouse | 99e45ee | 2005-05-23 21:57:41 +0100 | [diff] [blame] | 790 | audit_log_format(ab, " comm="); |
| 791 | audit_log_untrustedstring(ab, name); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 792 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 793 | if (mm) { |
| 794 | down_read(&mm->mmap_sem); |
| 795 | vma = mm->mmap; |
| 796 | while (vma) { |
| 797 | if ((vma->vm_flags & VM_EXECUTABLE) && |
| 798 | vma->vm_file) { |
| 799 | audit_log_d_path(ab, "exe=", |
Josef Sipek | a7a005f | 2006-12-08 02:37:17 -0800 | [diff] [blame] | 800 | vma->vm_file->f_path.dentry, |
| 801 | vma->vm_file->f_path.mnt); |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 802 | break; |
| 803 | } |
| 804 | vma = vma->vm_next; |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 805 | } |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 806 | up_read(&mm->mmap_sem); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 807 | } |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 808 | audit_log_task_context(ab); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 811 | static int audit_log_pid_context(struct audit_context *context, pid_t pid, |
| 812 | u32 sid) |
| 813 | { |
| 814 | struct audit_buffer *ab; |
| 815 | char *s = NULL; |
| 816 | u32 len; |
| 817 | int rc = 0; |
| 818 | |
| 819 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID); |
| 820 | if (!ab) |
| 821 | return 1; |
| 822 | |
| 823 | if (selinux_sid_to_string(sid, &s, &len)) { |
| 824 | audit_log_format(ab, "opid=%d obj=(none)", pid); |
| 825 | rc = 1; |
| 826 | } else |
| 827 | audit_log_format(ab, "opid=%d obj=%s", pid, s); |
| 828 | audit_log_end(ab); |
| 829 | kfree(s); |
| 830 | |
| 831 | return rc; |
| 832 | } |
| 833 | |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 834 | static void audit_log_execve_info(struct audit_buffer *ab, |
| 835 | struct audit_aux_data_execve *axi) |
| 836 | { |
| 837 | int i; |
| 838 | long len, ret; |
| 839 | const char __user *p = (const char __user *)axi->mm->arg_start; |
| 840 | char *buf; |
| 841 | |
| 842 | if (axi->mm != current->mm) |
| 843 | return; /* execve failed, no additional info */ |
| 844 | |
| 845 | for (i = 0; i < axi->argc; i++, p += len) { |
Ollie Wild | b6a2fea | 2007-07-19 01:48:16 -0700 | [diff] [blame] | 846 | len = strnlen_user(p, MAX_ARG_STRLEN); |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 847 | /* |
| 848 | * We just created this mm, if we can't find the strings |
| 849 | * we just copied into it something is _very_ wrong. Similar |
| 850 | * for strings that are too long, we should not have created |
| 851 | * any. |
| 852 | */ |
| 853 | if (!len || len > MAX_ARG_STRLEN) { |
| 854 | WARN_ON(1); |
| 855 | send_sig(SIGKILL, current, 0); |
| 856 | } |
| 857 | |
| 858 | buf = kmalloc(len, GFP_KERNEL); |
| 859 | if (!buf) { |
| 860 | audit_panic("out of memory for argv string\n"); |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | ret = copy_from_user(buf, p, len); |
| 865 | /* |
| 866 | * There is no reason for this copy to be short. We just |
| 867 | * copied them here, and the mm hasn't been exposed to user- |
| 868 | * space yet. |
| 869 | */ |
| 870 | if (!ret) { |
| 871 | WARN_ON(1); |
| 872 | send_sig(SIGKILL, current, 0); |
| 873 | } |
| 874 | |
| 875 | audit_log_format(ab, "a%d=", i); |
| 876 | audit_log_untrustedstring(ab, buf); |
| 877 | audit_log_format(ab, "\n"); |
| 878 | |
| 879 | kfree(buf); |
| 880 | } |
| 881 | } |
| 882 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 883 | 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] | 884 | { |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 885 | int i, call_panic = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | struct audit_buffer *ab; |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 887 | struct audit_aux_data *aux; |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 888 | const char *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 890 | /* tsk == current */ |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 891 | context->pid = tsk->pid; |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 892 | if (!context->ppid) |
| 893 | context->ppid = sys_getppid(); |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 894 | context->uid = tsk->uid; |
| 895 | context->gid = tsk->gid; |
| 896 | context->euid = tsk->euid; |
| 897 | context->suid = tsk->suid; |
| 898 | context->fsuid = tsk->fsuid; |
| 899 | context->egid = tsk->egid; |
| 900 | context->sgid = tsk->sgid; |
| 901 | context->fsgid = tsk->fsgid; |
| 902 | context->personality = tsk->personality; |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 903 | |
| 904 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | if (!ab) |
| 906 | return; /* audit_panic has been called */ |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 907 | audit_log_format(ab, "arch=%x syscall=%d", |
| 908 | context->arch, context->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | if (context->personality != PER_LINUX) |
| 910 | audit_log_format(ab, " per=%lx", context->personality); |
| 911 | if (context->return_valid) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 912 | audit_log_format(ab, " success=%s exit=%ld", |
| 913 | (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", |
| 914 | context->return_code); |
Alan Cox | eb84a20 | 2006-09-29 02:01:41 -0700 | [diff] [blame] | 915 | |
| 916 | mutex_lock(&tty_mutex); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 917 | read_lock(&tasklist_lock); |
Al Viro | 45d9bb0 | 2006-03-29 20:02:55 -0500 | [diff] [blame] | 918 | if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name) |
| 919 | tty = tsk->signal->tty->name; |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 920 | else |
| 921 | tty = "(none)"; |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 922 | read_unlock(&tasklist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | audit_log_format(ab, |
| 924 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 925 | " ppid=%d pid=%d auid=%u uid=%u gid=%u" |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 926 | " euid=%u suid=%u fsuid=%u" |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 927 | " egid=%u sgid=%u fsgid=%u tty=%s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | context->argv[0], |
| 929 | context->argv[1], |
| 930 | context->argv[2], |
| 931 | context->argv[3], |
| 932 | context->name_count, |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 933 | context->ppid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | context->pid, |
| 935 | context->loginuid, |
| 936 | context->uid, |
| 937 | context->gid, |
| 938 | context->euid, context->suid, context->fsuid, |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 939 | context->egid, context->sgid, context->fsgid, tty); |
Alan Cox | eb84a20 | 2006-09-29 02:01:41 -0700 | [diff] [blame] | 940 | |
| 941 | mutex_unlock(&tty_mutex); |
| 942 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 943 | audit_log_task_info(ab, tsk); |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 944 | if (context->filterkey) { |
| 945 | audit_log_format(ab, " key="); |
| 946 | audit_log_untrustedstring(ab, context->filterkey); |
| 947 | } else |
| 948 | audit_log_format(ab, " key=(null)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 951 | for (aux = context->aux; aux; aux = aux->next) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 952 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 953 | ab = audit_log_start(context, GFP_KERNEL, aux->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | if (!ab) |
| 955 | continue; /* audit_panic has been called */ |
| 956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | switch (aux->type) { |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 958 | case AUDIT_MQ_OPEN: { |
| 959 | struct audit_aux_data_mq_open *axi = (void *)aux; |
| 960 | audit_log_format(ab, |
| 961 | "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld " |
| 962 | "mq_msgsize=%ld mq_curmsgs=%ld", |
| 963 | axi->oflag, axi->mode, axi->attr.mq_flags, |
| 964 | axi->attr.mq_maxmsg, axi->attr.mq_msgsize, |
| 965 | axi->attr.mq_curmsgs); |
| 966 | break; } |
| 967 | |
| 968 | case AUDIT_MQ_SENDRECV: { |
| 969 | struct audit_aux_data_mq_sendrecv *axi = (void *)aux; |
| 970 | audit_log_format(ab, |
| 971 | "mqdes=%d msg_len=%zd msg_prio=%u " |
| 972 | "abs_timeout_sec=%ld abs_timeout_nsec=%ld", |
| 973 | axi->mqdes, axi->msg_len, axi->msg_prio, |
| 974 | axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec); |
| 975 | break; } |
| 976 | |
| 977 | case AUDIT_MQ_NOTIFY: { |
| 978 | struct audit_aux_data_mq_notify *axi = (void *)aux; |
| 979 | audit_log_format(ab, |
| 980 | "mqdes=%d sigev_signo=%d", |
| 981 | axi->mqdes, |
| 982 | axi->notification.sigev_signo); |
| 983 | break; } |
| 984 | |
| 985 | case AUDIT_MQ_GETSETATTR: { |
| 986 | struct audit_aux_data_mq_getsetattr *axi = (void *)aux; |
| 987 | audit_log_format(ab, |
| 988 | "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld " |
| 989 | "mq_curmsgs=%ld ", |
| 990 | axi->mqdes, |
| 991 | axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg, |
| 992 | axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs); |
| 993 | break; } |
| 994 | |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 995 | case AUDIT_IPC: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 997 | audit_log_format(ab, |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 998 | "ouid=%u ogid=%u mode=%x", |
| 999 | axi->uid, axi->gid, axi->mode); |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1000 | if (axi->osid != 0) { |
| 1001 | char *ctx = NULL; |
| 1002 | u32 len; |
Stephen Smalley | 1a70cd4 | 2006-09-25 23:31:57 -0700 | [diff] [blame] | 1003 | if (selinux_sid_to_string( |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1004 | axi->osid, &ctx, &len)) { |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 1005 | audit_log_format(ab, " osid=%u", |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1006 | axi->osid); |
| 1007 | call_panic = 1; |
| 1008 | } else |
| 1009 | audit_log_format(ab, " obj=%s", ctx); |
| 1010 | kfree(ctx); |
| 1011 | } |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1012 | break; } |
| 1013 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1014 | case AUDIT_IPC_SET_PERM: { |
| 1015 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 1016 | audit_log_format(ab, |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 1017 | "qbytes=%lx ouid=%u ogid=%u mode=%x", |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1018 | axi->qbytes, axi->uid, axi->gid, axi->mode); |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1019 | break; } |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 1020 | |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1021 | case AUDIT_EXECVE: { |
| 1022 | struct audit_aux_data_execve *axi = (void *)aux; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1023 | audit_log_execve_info(ab, axi); |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1024 | break; } |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1025 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1026 | case AUDIT_SOCKETCALL: { |
| 1027 | int i; |
| 1028 | struct audit_aux_data_socketcall *axs = (void *)aux; |
| 1029 | audit_log_format(ab, "nargs=%d", axs->nargs); |
| 1030 | for (i=0; i<axs->nargs; i++) |
| 1031 | audit_log_format(ab, " a%d=%lx", i, axs->args[i]); |
| 1032 | break; } |
| 1033 | |
| 1034 | case AUDIT_SOCKADDR: { |
| 1035 | struct audit_aux_data_sockaddr *axs = (void *)aux; |
| 1036 | |
| 1037 | audit_log_format(ab, "saddr="); |
| 1038 | audit_log_hex(ab, axs->a, axs->len); |
| 1039 | break; } |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1040 | |
| 1041 | case AUDIT_AVC_PATH: { |
| 1042 | struct audit_aux_data_path *axi = (void *)aux; |
| 1043 | audit_log_d_path(ab, "path=", axi->dentry, axi->mnt); |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1044 | break; } |
| 1045 | |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 1046 | case AUDIT_FD_PAIR: { |
| 1047 | struct audit_aux_data_fd_pair *axs = (void *)aux; |
| 1048 | audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]); |
| 1049 | break; } |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | } |
| 1052 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1055 | for (aux = context->aux_pids; aux; aux = aux->next) { |
| 1056 | struct audit_aux_data_pids *axs = (void *)aux; |
| 1057 | int i; |
| 1058 | |
| 1059 | for (i = 0; i < axs->pid_count; i++) |
| 1060 | if (audit_log_pid_context(context, axs->target_pid[i], |
| 1061 | axs->target_sid[i])) |
| 1062 | call_panic = 1; |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 1063 | } |
| 1064 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1065 | if (context->target_pid && |
| 1066 | audit_log_pid_context(context, context->target_pid, |
| 1067 | context->target_sid)) |
| 1068 | call_panic = 1; |
| 1069 | |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1070 | if (context->pwd && context->pwdmnt) { |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1071 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD); |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1072 | if (ab) { |
| 1073 | audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt); |
| 1074 | audit_log_end(ab); |
| 1075 | } |
| 1076 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | for (i = 0; i < context->name_count; i++) { |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1078 | struct audit_names *n = &context->names[i]; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1079 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1080 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (!ab) |
| 1082 | continue; /* audit_panic has been called */ |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | audit_log_format(ab, "item=%d", i); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1085 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1086 | if (n->name) { |
| 1087 | switch(n->name_len) { |
| 1088 | case AUDIT_NAME_FULL: |
| 1089 | /* log the full path */ |
| 1090 | audit_log_format(ab, " name="); |
| 1091 | audit_log_untrustedstring(ab, n->name); |
| 1092 | break; |
| 1093 | case 0: |
| 1094 | /* name was specified as a relative path and the |
| 1095 | * directory component is the cwd */ |
| 1096 | audit_log_d_path(ab, " name=", context->pwd, |
| 1097 | context->pwdmnt); |
| 1098 | break; |
| 1099 | default: |
| 1100 | /* log the name's directory component */ |
| 1101 | audit_log_format(ab, " name="); |
| 1102 | audit_log_n_untrustedstring(ab, n->name_len, |
| 1103 | n->name); |
| 1104 | } |
| 1105 | } else |
| 1106 | audit_log_format(ab, " name=(null)"); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1107 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1108 | if (n->ino != (unsigned long)-1) { |
| 1109 | audit_log_format(ab, " inode=%lu" |
| 1110 | " dev=%02x:%02x mode=%#o" |
| 1111 | " ouid=%u ogid=%u rdev=%02x:%02x", |
| 1112 | n->ino, |
| 1113 | MAJOR(n->dev), |
| 1114 | MINOR(n->dev), |
| 1115 | n->mode, |
| 1116 | n->uid, |
| 1117 | n->gid, |
| 1118 | MAJOR(n->rdev), |
| 1119 | MINOR(n->rdev)); |
| 1120 | } |
| 1121 | if (n->osid != 0) { |
Steve Grubb | 1b50eed | 2006-04-03 14:06:13 -0400 | [diff] [blame] | 1122 | char *ctx = NULL; |
| 1123 | u32 len; |
Stephen Smalley | 1a70cd4 | 2006-09-25 23:31:57 -0700 | [diff] [blame] | 1124 | if (selinux_sid_to_string( |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1125 | n->osid, &ctx, &len)) { |
| 1126 | audit_log_format(ab, " osid=%u", n->osid); |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1127 | call_panic = 2; |
Steve Grubb | 1b50eed | 2006-04-03 14:06:13 -0400 | [diff] [blame] | 1128 | } else |
| 1129 | audit_log_format(ab, " obj=%s", ctx); |
| 1130 | kfree(ctx); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | audit_log_end(ab); |
| 1134 | } |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1135 | if (call_panic) |
| 1136 | audit_panic("error converting sid to string"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1139 | /** |
| 1140 | * audit_free - free a per-task audit context |
| 1141 | * @tsk: task whose audit context block to free |
| 1142 | * |
Al Viro | fa84cb9 | 2006-03-29 20:30:19 -0500 | [diff] [blame] | 1143 | * Called from copy_process and do_exit |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1144 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | void audit_free(struct task_struct *tsk) |
| 1146 | { |
| 1147 | struct audit_context *context; |
| 1148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | context = audit_get_context(tsk, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | if (likely(!context)) |
| 1151 | return; |
| 1152 | |
| 1153 | /* Check for system calls that do not go through the exit |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 1154 | * function (e.g., exit_group), then free context block. |
| 1155 | * We use GFP_ATOMIC here because we might be doing this |
| 1156 | * in the context of the idle thread */ |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1157 | /* that can happen only if we are called from do_exit() */ |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 1158 | if (context->in_syscall && context->auditable) |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1159 | audit_log_exit(context, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
| 1161 | audit_free_context(context); |
| 1162 | } |
| 1163 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1164 | /** |
| 1165 | * audit_syscall_entry - fill in an audit record at syscall entry |
| 1166 | * @tsk: task being audited |
| 1167 | * @arch: architecture type |
| 1168 | * @major: major syscall type (function) |
| 1169 | * @a1: additional syscall register 1 |
| 1170 | * @a2: additional syscall register 2 |
| 1171 | * @a3: additional syscall register 3 |
| 1172 | * @a4: additional syscall register 4 |
| 1173 | * |
| 1174 | * Fill in audit context at syscall entry. This only happens if the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | * audit context was created when the task was created and the state or |
| 1176 | * filters demand the audit context be built. If the state from the |
| 1177 | * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT, |
| 1178 | * then the record will be written at syscall exit time (otherwise, it |
| 1179 | * 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] | 1180 | * be written). |
| 1181 | */ |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1182 | void audit_syscall_entry(int arch, int major, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | unsigned long a1, unsigned long a2, |
| 1184 | unsigned long a3, unsigned long a4) |
| 1185 | { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1186 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | struct audit_context *context = tsk->audit_context; |
| 1188 | enum audit_state state; |
| 1189 | |
| 1190 | BUG_ON(!context); |
| 1191 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1192 | /* |
| 1193 | * This happens only on certain architectures that make system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | * calls in kernel_thread via the entry.S interface, instead of |
| 1195 | * with direct calls. (If you are porting to a new |
| 1196 | * architecture, hitting this condition can indicate that you |
| 1197 | * got the _exit/_leave calls backward in entry.S.) |
| 1198 | * |
| 1199 | * i386 no |
| 1200 | * x86_64 no |
Jon Mason | 2ef9481 | 2006-01-23 10:58:20 -0600 | [diff] [blame] | 1201 | * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | * |
| 1203 | * This also happens with vm86 emulation in a non-nested manner |
| 1204 | * (entries without exits), so this case must be caught. |
| 1205 | */ |
| 1206 | if (context->in_syscall) { |
| 1207 | struct audit_context *newctx; |
| 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | #if AUDIT_DEBUG |
| 1210 | printk(KERN_ERR |
| 1211 | "audit(:%d) pid=%d in syscall=%d;" |
| 1212 | " entering syscall=%d\n", |
| 1213 | context->serial, tsk->pid, context->major, major); |
| 1214 | #endif |
| 1215 | newctx = audit_alloc_context(context->state); |
| 1216 | if (newctx) { |
| 1217 | newctx->previous = context; |
| 1218 | context = newctx; |
| 1219 | tsk->audit_context = newctx; |
| 1220 | } else { |
| 1221 | /* If we can't alloc a new context, the best we |
| 1222 | * can do is to leak memory (any pending putname |
| 1223 | * will be lost). The only other alternative is |
| 1224 | * to abandon auditing. */ |
| 1225 | audit_zero_context(context, context->state); |
| 1226 | } |
| 1227 | } |
| 1228 | BUG_ON(context->in_syscall || context->name_count); |
| 1229 | |
| 1230 | if (!audit_enabled) |
| 1231 | return; |
| 1232 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1233 | context->arch = arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | context->major = major; |
| 1235 | context->argv[0] = a1; |
| 1236 | context->argv[1] = a2; |
| 1237 | context->argv[2] = a3; |
| 1238 | context->argv[3] = a4; |
| 1239 | |
| 1240 | state = context->state; |
Al Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 1241 | context->dummy = !audit_n_rules; |
| 1242 | if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)) |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 1243 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | if (likely(state == AUDIT_DISABLED)) |
| 1245 | return; |
| 1246 | |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 1247 | context->serial = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | context->ctime = CURRENT_TIME; |
| 1249 | context->in_syscall = 1; |
| 1250 | context->auditable = !!(state == AUDIT_RECORD_CONTEXT); |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 1251 | context->ppid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1254 | /** |
| 1255 | * audit_syscall_exit - deallocate audit context after a system call |
| 1256 | * @tsk: task being audited |
| 1257 | * @valid: success/failure flag |
| 1258 | * @return_code: syscall return value |
| 1259 | * |
| 1260 | * 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] | 1261 | * auditable (either because of the AUDIT_RECORD_CONTEXT state from |
| 1262 | * filtering, or because some other part of the kernel write an audit |
| 1263 | * message), then write out the syscall information. In call cases, |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1264 | * free the names stored from getname(). |
| 1265 | */ |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1266 | void audit_syscall_exit(int valid, long return_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1268 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | struct audit_context *context; |
| 1270 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1271 | context = audit_get_context(tsk, valid, return_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | if (likely(!context)) |
Al Viro | 97e94c4 | 2006-03-29 20:26:24 -0500 | [diff] [blame] | 1274 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 1276 | if (context->in_syscall && context->auditable) |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1277 | audit_log_exit(context, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
| 1279 | context->in_syscall = 0; |
| 1280 | context->auditable = 0; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | if (context->previous) { |
| 1283 | struct audit_context *new_context = context->previous; |
| 1284 | context->previous = NULL; |
| 1285 | audit_free_context(context); |
| 1286 | tsk->audit_context = new_context; |
| 1287 | } else { |
| 1288 | audit_free_names(context); |
| 1289 | audit_free_aux(context); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1290 | context->aux = NULL; |
| 1291 | context->aux_pids = NULL; |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 1292 | context->target_pid = 0; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1293 | context->target_sid = 0; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 1294 | kfree(context->filterkey); |
| 1295 | context->filterkey = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | tsk->audit_context = context; |
| 1297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1300 | /** |
| 1301 | * audit_getname - add a name to the list |
| 1302 | * @name: name to add |
| 1303 | * |
| 1304 | * Add a name to the list of audit names for this context. |
| 1305 | * Called from fs/namei.c:getname(). |
| 1306 | */ |
Al Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1307 | void __audit_getname(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | { |
| 1309 | struct audit_context *context = current->audit_context; |
| 1310 | |
Al Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1311 | if (IS_ERR(name) || !name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | return; |
| 1313 | |
| 1314 | if (!context->in_syscall) { |
| 1315 | #if AUDIT_DEBUG == 2 |
| 1316 | printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n", |
| 1317 | __FILE__, __LINE__, context->serial, name); |
| 1318 | dump_stack(); |
| 1319 | #endif |
| 1320 | return; |
| 1321 | } |
| 1322 | BUG_ON(context->name_count >= AUDIT_NAMES); |
| 1323 | context->names[context->name_count].name = name; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1324 | context->names[context->name_count].name_len = AUDIT_NAME_FULL; |
| 1325 | context->names[context->name_count].name_put = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | context->names[context->name_count].ino = (unsigned long)-1; |
Amy Griffis | e41e8bd | 2007-02-13 14:14:09 -0500 | [diff] [blame] | 1327 | context->names[context->name_count].osid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | ++context->name_count; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1329 | if (!context->pwd) { |
| 1330 | read_lock(¤t->fs->lock); |
| 1331 | context->pwd = dget(current->fs->pwd); |
| 1332 | context->pwdmnt = mntget(current->fs->pwdmnt); |
| 1333 | read_unlock(¤t->fs->lock); |
| 1334 | } |
| 1335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1338 | /* audit_putname - intercept a putname request |
| 1339 | * @name: name to intercept and delay for putname |
| 1340 | * |
| 1341 | * If we have stored the name from getname in the audit context, |
| 1342 | * then we delay the putname until syscall exit. |
| 1343 | * Called from include/linux/fs.h:putname(). |
| 1344 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | void audit_putname(const char *name) |
| 1346 | { |
| 1347 | struct audit_context *context = current->audit_context; |
| 1348 | |
| 1349 | BUG_ON(!context); |
| 1350 | if (!context->in_syscall) { |
| 1351 | #if AUDIT_DEBUG == 2 |
| 1352 | printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n", |
| 1353 | __FILE__, __LINE__, context->serial, name); |
| 1354 | if (context->name_count) { |
| 1355 | int i; |
| 1356 | for (i = 0; i < context->name_count; i++) |
| 1357 | printk(KERN_ERR "name[%d] = %p = %s\n", i, |
| 1358 | context->names[i].name, |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1359 | context->names[i].name ?: "(null)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | } |
| 1361 | #endif |
| 1362 | __putname(name); |
| 1363 | } |
| 1364 | #if AUDIT_DEBUG |
| 1365 | else { |
| 1366 | ++context->put_count; |
| 1367 | if (context->put_count > context->name_count) { |
| 1368 | printk(KERN_ERR "%s:%d(:%d): major=%d" |
| 1369 | " in_syscall=%d putname(%p) name_count=%d" |
| 1370 | " put_count=%d\n", |
| 1371 | __FILE__, __LINE__, |
| 1372 | context->serial, context->major, |
| 1373 | context->in_syscall, name, context->name_count, |
| 1374 | context->put_count); |
| 1375 | dump_stack(); |
| 1376 | } |
| 1377 | } |
| 1378 | #endif |
| 1379 | } |
| 1380 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1381 | static int audit_inc_name_count(struct audit_context *context, |
| 1382 | const struct inode *inode) |
| 1383 | { |
| 1384 | if (context->name_count >= AUDIT_NAMES) { |
| 1385 | if (inode) |
| 1386 | printk(KERN_DEBUG "name_count maxed, losing inode data: " |
| 1387 | "dev=%02x:%02x, inode=%lu", |
| 1388 | MAJOR(inode->i_sb->s_dev), |
| 1389 | MINOR(inode->i_sb->s_dev), |
| 1390 | inode->i_ino); |
| 1391 | |
| 1392 | else |
| 1393 | printk(KERN_DEBUG "name_count maxed, losing inode data"); |
| 1394 | return 1; |
| 1395 | } |
| 1396 | context->name_count++; |
| 1397 | #if AUDIT_DEBUG |
| 1398 | context->ino_count++; |
| 1399 | #endif |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1403 | /* Copy inode data into an audit_names. */ |
| 1404 | 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] | 1405 | { |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1406 | name->ino = inode->i_ino; |
| 1407 | name->dev = inode->i_sb->s_dev; |
| 1408 | name->mode = inode->i_mode; |
| 1409 | name->uid = inode->i_uid; |
| 1410 | name->gid = inode->i_gid; |
| 1411 | name->rdev = inode->i_rdev; |
| 1412 | selinux_get_inode_sid(inode, &name->osid); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1415 | /** |
| 1416 | * audit_inode - store the inode and device from a lookup |
| 1417 | * @name: name being audited |
| 1418 | * @inode: inode being audited |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1419 | * |
| 1420 | * Called from fs/namei.c:path_lookup(). |
| 1421 | */ |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1422 | void __audit_inode(const char *name, const struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | { |
| 1424 | int idx; |
| 1425 | struct audit_context *context = current->audit_context; |
| 1426 | |
| 1427 | if (!context->in_syscall) |
| 1428 | return; |
| 1429 | if (context->name_count |
| 1430 | && context->names[context->name_count-1].name |
| 1431 | && context->names[context->name_count-1].name == name) |
| 1432 | idx = context->name_count - 1; |
| 1433 | else if (context->name_count > 1 |
| 1434 | && context->names[context->name_count-2].name |
| 1435 | && context->names[context->name_count-2].name == name) |
| 1436 | idx = context->name_count - 2; |
| 1437 | else { |
| 1438 | /* FIXME: how much do we care about inodes that have no |
| 1439 | * associated name? */ |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1440 | if (audit_inc_name_count(context, inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | return; |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1442 | idx = context->name_count - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | context->names[idx].name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | } |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1445 | audit_copy_inode(&context->names[idx], inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1448 | /** |
| 1449 | * audit_inode_child - collect inode info for created/removed objects |
| 1450 | * @dname: inode's dentry name |
| 1451 | * @inode: inode being audited |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1452 | * @parent: inode of dentry parent |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1453 | * |
| 1454 | * For syscalls that create or remove filesystem objects, audit_inode |
| 1455 | * can only collect information for the filesystem object's parent. |
| 1456 | * This call updates the audit context with the child's information. |
| 1457 | * Syscalls that create a new filesystem object must be hooked after |
| 1458 | * the object is created. Syscalls that remove a filesystem object |
| 1459 | * must be hooked prior, in order to capture the target inode during |
| 1460 | * unsuccessful attempts. |
| 1461 | */ |
| 1462 | void __audit_inode_child(const char *dname, const struct inode *inode, |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1463 | const struct inode *parent) |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1464 | { |
| 1465 | int idx; |
| 1466 | struct audit_context *context = current->audit_context; |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1467 | const char *found_parent = NULL, *found_child = NULL; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1468 | int dirlen = 0; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (!context->in_syscall) |
| 1471 | return; |
| 1472 | |
| 1473 | /* determine matching parent */ |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1474 | if (!dname) |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1475 | goto add_names; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1476 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1477 | /* parent is more likely, look for it first */ |
| 1478 | for (idx = 0; idx < context->name_count; idx++) { |
| 1479 | struct audit_names *n = &context->names[idx]; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1480 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1481 | if (!n->name) |
| 1482 | continue; |
| 1483 | |
| 1484 | if (n->ino == parent->i_ino && |
| 1485 | !audit_compare_dname_path(dname, n->name, &dirlen)) { |
| 1486 | n->name_len = dirlen; /* update parent data in place */ |
| 1487 | found_parent = n->name; |
| 1488 | goto add_names; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1489 | } |
Steve Grubb | ac9910c | 2006-09-28 14:31:32 -0400 | [diff] [blame] | 1490 | } |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1491 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1492 | /* no matching parent, look for matching child */ |
| 1493 | for (idx = 0; idx < context->name_count; idx++) { |
| 1494 | struct audit_names *n = &context->names[idx]; |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1495 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1496 | if (!n->name) |
| 1497 | continue; |
| 1498 | |
| 1499 | /* strcmp() is the more likely scenario */ |
| 1500 | if (!strcmp(dname, n->name) || |
| 1501 | !audit_compare_dname_path(dname, n->name, &dirlen)) { |
| 1502 | if (inode) |
| 1503 | audit_copy_inode(n, inode); |
| 1504 | else |
| 1505 | n->ino = (unsigned long)-1; |
| 1506 | found_child = n->name; |
| 1507 | goto add_names; |
Steve Grubb | ac9910c | 2006-09-28 14:31:32 -0400 | [diff] [blame] | 1508 | } |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | add_names: |
| 1512 | if (!found_parent) { |
| 1513 | if (audit_inc_name_count(context, parent)) |
| 1514 | return; |
| 1515 | idx = context->name_count - 1; |
| 1516 | context->names[idx].name = NULL; |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1517 | audit_copy_inode(&context->names[idx], parent); |
| 1518 | } |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1519 | |
| 1520 | if (!found_child) { |
| 1521 | if (audit_inc_name_count(context, inode)) |
| 1522 | return; |
| 1523 | idx = context->name_count - 1; |
| 1524 | |
| 1525 | /* Re-use the name belonging to the slot for a matching parent |
| 1526 | * directory. All names for this context are relinquished in |
| 1527 | * audit_free_names() */ |
| 1528 | if (found_parent) { |
| 1529 | context->names[idx].name = found_parent; |
| 1530 | context->names[idx].name_len = AUDIT_NAME_FULL; |
| 1531 | /* don't call __putname() */ |
| 1532 | context->names[idx].name_put = 0; |
| 1533 | } else { |
| 1534 | context->names[idx].name = NULL; |
| 1535 | } |
| 1536 | |
| 1537 | if (inode) |
| 1538 | audit_copy_inode(&context->names[idx], inode); |
| 1539 | else |
| 1540 | context->names[idx].ino = (unsigned long)-1; |
| 1541 | } |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1545 | * auditsc_get_stamp - get local copies of audit_context values |
| 1546 | * @ctx: audit_context for the task |
| 1547 | * @t: timespec to store time recorded in the audit_context |
| 1548 | * @serial: serial value that is recorded in the audit_context |
| 1549 | * |
| 1550 | * Also sets the context as auditable. |
| 1551 | */ |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1552 | void auditsc_get_stamp(struct audit_context *ctx, |
| 1553 | struct timespec *t, unsigned int *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | { |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 1555 | if (!ctx->serial) |
| 1556 | ctx->serial = audit_serial(); |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1557 | t->tv_sec = ctx->ctime.tv_sec; |
| 1558 | t->tv_nsec = ctx->ctime.tv_nsec; |
| 1559 | *serial = ctx->serial; |
| 1560 | ctx->auditable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1563 | /** |
| 1564 | * audit_set_loginuid - set a task's audit_context loginuid |
| 1565 | * @task: task whose audit context is being modified |
| 1566 | * @loginuid: loginuid value |
| 1567 | * |
| 1568 | * Returns 0. |
| 1569 | * |
| 1570 | * Called (set) from fs/proc/base.c::proc_loginuid_write(). |
| 1571 | */ |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1572 | int audit_set_loginuid(struct task_struct *task, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | { |
Steve Grubb | 4175710 | 2006-06-12 07:48:28 -0400 | [diff] [blame] | 1574 | struct audit_context *context = task->audit_context; |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1575 | |
Steve Grubb | 4175710 | 2006-06-12 07:48:28 -0400 | [diff] [blame] | 1576 | if (context) { |
| 1577 | /* Only log if audit is enabled */ |
| 1578 | if (context->in_syscall) { |
| 1579 | struct audit_buffer *ab; |
| 1580 | |
| 1581 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN); |
| 1582 | if (ab) { |
| 1583 | audit_log_format(ab, "login pid=%d uid=%u " |
| 1584 | "old auid=%u new auid=%u", |
| 1585 | task->pid, task->uid, |
| 1586 | context->loginuid, loginuid); |
| 1587 | audit_log_end(ab); |
| 1588 | } |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1589 | } |
Steve Grubb | 4175710 | 2006-06-12 07:48:28 -0400 | [diff] [blame] | 1590 | context->loginuid = loginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | } |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1595 | /** |
| 1596 | * audit_get_loginuid - get the loginuid for an audit_context |
| 1597 | * @ctx: the audit_context |
| 1598 | * |
| 1599 | * Returns the context's loginuid or -1 if @ctx is NULL. |
| 1600 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | uid_t audit_get_loginuid(struct audit_context *ctx) |
| 1602 | { |
| 1603 | return ctx ? ctx->loginuid : -1; |
| 1604 | } |
| 1605 | |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 1606 | EXPORT_SYMBOL(audit_get_loginuid); |
| 1607 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1608 | /** |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1609 | * __audit_mq_open - record audit data for a POSIX MQ open |
| 1610 | * @oflag: open flag |
| 1611 | * @mode: mode bits |
| 1612 | * @u_attr: queue attributes |
| 1613 | * |
| 1614 | * Returns 0 for success or NULL context or < 0 on error. |
| 1615 | */ |
| 1616 | int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr) |
| 1617 | { |
| 1618 | struct audit_aux_data_mq_open *ax; |
| 1619 | struct audit_context *context = current->audit_context; |
| 1620 | |
| 1621 | if (!audit_enabled) |
| 1622 | return 0; |
| 1623 | |
| 1624 | if (likely(!context)) |
| 1625 | return 0; |
| 1626 | |
| 1627 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 1628 | if (!ax) |
| 1629 | return -ENOMEM; |
| 1630 | |
| 1631 | if (u_attr != NULL) { |
| 1632 | if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) { |
| 1633 | kfree(ax); |
| 1634 | return -EFAULT; |
| 1635 | } |
| 1636 | } else |
| 1637 | memset(&ax->attr, 0, sizeof(ax->attr)); |
| 1638 | |
| 1639 | ax->oflag = oflag; |
| 1640 | ax->mode = mode; |
| 1641 | |
| 1642 | ax->d.type = AUDIT_MQ_OPEN; |
| 1643 | ax->d.next = context->aux; |
| 1644 | context->aux = (void *)ax; |
| 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | /** |
| 1649 | * __audit_mq_timedsend - record audit data for a POSIX MQ timed send |
| 1650 | * @mqdes: MQ descriptor |
| 1651 | * @msg_len: Message length |
| 1652 | * @msg_prio: Message priority |
Randy Dunlap | 1dbe83c | 2006-06-27 02:54:01 -0700 | [diff] [blame] | 1653 | * @u_abs_timeout: Message timeout in absolute time |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1654 | * |
| 1655 | * Returns 0 for success or NULL context or < 0 on error. |
| 1656 | */ |
| 1657 | int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, |
| 1658 | const struct timespec __user *u_abs_timeout) |
| 1659 | { |
| 1660 | struct audit_aux_data_mq_sendrecv *ax; |
| 1661 | struct audit_context *context = current->audit_context; |
| 1662 | |
| 1663 | if (!audit_enabled) |
| 1664 | return 0; |
| 1665 | |
| 1666 | if (likely(!context)) |
| 1667 | return 0; |
| 1668 | |
| 1669 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 1670 | if (!ax) |
| 1671 | return -ENOMEM; |
| 1672 | |
| 1673 | if (u_abs_timeout != NULL) { |
| 1674 | if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) { |
| 1675 | kfree(ax); |
| 1676 | return -EFAULT; |
| 1677 | } |
| 1678 | } else |
| 1679 | memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout)); |
| 1680 | |
| 1681 | ax->mqdes = mqdes; |
| 1682 | ax->msg_len = msg_len; |
| 1683 | ax->msg_prio = msg_prio; |
| 1684 | |
| 1685 | ax->d.type = AUDIT_MQ_SENDRECV; |
| 1686 | ax->d.next = context->aux; |
| 1687 | context->aux = (void *)ax; |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | /** |
| 1692 | * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive |
| 1693 | * @mqdes: MQ descriptor |
| 1694 | * @msg_len: Message length |
Randy Dunlap | 1dbe83c | 2006-06-27 02:54:01 -0700 | [diff] [blame] | 1695 | * @u_msg_prio: Message priority |
| 1696 | * @u_abs_timeout: Message timeout in absolute time |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1697 | * |
| 1698 | * Returns 0 for success or NULL context or < 0 on error. |
| 1699 | */ |
| 1700 | int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len, |
| 1701 | unsigned int __user *u_msg_prio, |
| 1702 | const struct timespec __user *u_abs_timeout) |
| 1703 | { |
| 1704 | struct audit_aux_data_mq_sendrecv *ax; |
| 1705 | struct audit_context *context = current->audit_context; |
| 1706 | |
| 1707 | if (!audit_enabled) |
| 1708 | return 0; |
| 1709 | |
| 1710 | if (likely(!context)) |
| 1711 | return 0; |
| 1712 | |
| 1713 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 1714 | if (!ax) |
| 1715 | return -ENOMEM; |
| 1716 | |
| 1717 | if (u_msg_prio != NULL) { |
| 1718 | if (get_user(ax->msg_prio, u_msg_prio)) { |
| 1719 | kfree(ax); |
| 1720 | return -EFAULT; |
| 1721 | } |
| 1722 | } else |
| 1723 | ax->msg_prio = 0; |
| 1724 | |
| 1725 | if (u_abs_timeout != NULL) { |
| 1726 | if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) { |
| 1727 | kfree(ax); |
| 1728 | return -EFAULT; |
| 1729 | } |
| 1730 | } else |
| 1731 | memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout)); |
| 1732 | |
| 1733 | ax->mqdes = mqdes; |
| 1734 | ax->msg_len = msg_len; |
| 1735 | |
| 1736 | ax->d.type = AUDIT_MQ_SENDRECV; |
| 1737 | ax->d.next = context->aux; |
| 1738 | context->aux = (void *)ax; |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | /** |
| 1743 | * __audit_mq_notify - record audit data for a POSIX MQ notify |
| 1744 | * @mqdes: MQ descriptor |
| 1745 | * @u_notification: Notification event |
| 1746 | * |
| 1747 | * Returns 0 for success or NULL context or < 0 on error. |
| 1748 | */ |
| 1749 | |
| 1750 | int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification) |
| 1751 | { |
| 1752 | struct audit_aux_data_mq_notify *ax; |
| 1753 | struct audit_context *context = current->audit_context; |
| 1754 | |
| 1755 | if (!audit_enabled) |
| 1756 | return 0; |
| 1757 | |
| 1758 | if (likely(!context)) |
| 1759 | return 0; |
| 1760 | |
| 1761 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 1762 | if (!ax) |
| 1763 | return -ENOMEM; |
| 1764 | |
| 1765 | if (u_notification != NULL) { |
| 1766 | if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) { |
| 1767 | kfree(ax); |
| 1768 | return -EFAULT; |
| 1769 | } |
| 1770 | } else |
| 1771 | memset(&ax->notification, 0, sizeof(ax->notification)); |
| 1772 | |
| 1773 | ax->mqdes = mqdes; |
| 1774 | |
| 1775 | ax->d.type = AUDIT_MQ_NOTIFY; |
| 1776 | ax->d.next = context->aux; |
| 1777 | context->aux = (void *)ax; |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
| 1781 | /** |
| 1782 | * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute |
| 1783 | * @mqdes: MQ descriptor |
| 1784 | * @mqstat: MQ flags |
| 1785 | * |
| 1786 | * Returns 0 for success or NULL context or < 0 on error. |
| 1787 | */ |
| 1788 | int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat) |
| 1789 | { |
| 1790 | struct audit_aux_data_mq_getsetattr *ax; |
| 1791 | struct audit_context *context = current->audit_context; |
| 1792 | |
| 1793 | if (!audit_enabled) |
| 1794 | return 0; |
| 1795 | |
| 1796 | if (likely(!context)) |
| 1797 | return 0; |
| 1798 | |
| 1799 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 1800 | if (!ax) |
| 1801 | return -ENOMEM; |
| 1802 | |
| 1803 | ax->mqdes = mqdes; |
| 1804 | ax->mqstat = *mqstat; |
| 1805 | |
| 1806 | ax->d.type = AUDIT_MQ_GETSETATTR; |
| 1807 | ax->d.next = context->aux; |
| 1808 | context->aux = (void *)ax; |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
| 1812 | /** |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1813 | * audit_ipc_obj - record audit data for ipc object |
| 1814 | * @ipcp: ipc permissions |
| 1815 | * |
| 1816 | * Returns 0 for success or NULL context or < 0 on error. |
| 1817 | */ |
Al Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1818 | int __audit_ipc_obj(struct kern_ipc_perm *ipcp) |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1819 | { |
| 1820 | struct audit_aux_data_ipcctl *ax; |
| 1821 | struct audit_context *context = current->audit_context; |
| 1822 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1823 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 1824 | if (!ax) |
| 1825 | return -ENOMEM; |
| 1826 | |
| 1827 | ax->uid = ipcp->uid; |
| 1828 | ax->gid = ipcp->gid; |
| 1829 | ax->mode = ipcp->mode; |
| 1830 | selinux_get_ipc_sid(ipcp, &ax->osid); |
| 1831 | |
| 1832 | ax->d.type = AUDIT_IPC; |
| 1833 | ax->d.next = context->aux; |
| 1834 | context->aux = (void *)ax; |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | /** |
| 1839 | * audit_ipc_set_perm - record audit data for new ipc permissions |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1840 | * @qbytes: msgq bytes |
| 1841 | * @uid: msgq user id |
| 1842 | * @gid: msgq group id |
| 1843 | * @mode: msgq mode (permissions) |
| 1844 | * |
| 1845 | * Returns 0 for success or NULL context or < 0 on error. |
| 1846 | */ |
Al Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1847 | 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] | 1848 | { |
| 1849 | struct audit_aux_data_ipcctl *ax; |
| 1850 | struct audit_context *context = current->audit_context; |
| 1851 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1852 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | if (!ax) |
| 1854 | return -ENOMEM; |
| 1855 | |
| 1856 | ax->qbytes = qbytes; |
| 1857 | ax->uid = uid; |
| 1858 | ax->gid = gid; |
| 1859 | ax->mode = mode; |
| 1860 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1861 | ax->d.type = AUDIT_IPC_SET_PERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | ax->d.next = context->aux; |
| 1863 | context->aux = (void *)ax; |
| 1864 | return 0; |
| 1865 | } |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1866 | |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1867 | int audit_argv_kb = 32; |
| 1868 | |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1869 | int audit_bprm(struct linux_binprm *bprm) |
| 1870 | { |
| 1871 | struct audit_aux_data_execve *ax; |
| 1872 | struct audit_context *context = current->audit_context; |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1873 | |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 1874 | if (likely(!audit_enabled || !context || context->dummy)) |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1875 | return 0; |
| 1876 | |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1877 | /* |
| 1878 | * Even though the stack code doesn't limit the arg+env size any more, |
| 1879 | * the audit code requires that _all_ arguments be logged in a single |
| 1880 | * netlink skb. Hence cap it :-( |
| 1881 | */ |
| 1882 | if (bprm->argv_len > (audit_argv_kb << 10)) |
| 1883 | return -E2BIG; |
| 1884 | |
| 1885 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1886 | if (!ax) |
| 1887 | return -ENOMEM; |
| 1888 | |
| 1889 | ax->argc = bprm->argc; |
| 1890 | ax->envc = bprm->envc; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1891 | ax->mm = bprm->mm; |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1892 | ax->d.type = AUDIT_EXECVE; |
| 1893 | ax->d.next = context->aux; |
| 1894 | context->aux = (void *)ax; |
| 1895 | return 0; |
| 1896 | } |
| 1897 | |
| 1898 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1899 | /** |
| 1900 | * audit_socketcall - record audit data for sys_socketcall |
| 1901 | * @nargs: number of args |
| 1902 | * @args: args array |
| 1903 | * |
| 1904 | * Returns 0 for success or NULL context or < 0 on error. |
| 1905 | */ |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1906 | int audit_socketcall(int nargs, unsigned long *args) |
| 1907 | { |
| 1908 | struct audit_aux_data_socketcall *ax; |
| 1909 | struct audit_context *context = current->audit_context; |
| 1910 | |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 1911 | if (likely(!context || context->dummy)) |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1912 | return 0; |
| 1913 | |
| 1914 | ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL); |
| 1915 | if (!ax) |
| 1916 | return -ENOMEM; |
| 1917 | |
| 1918 | ax->nargs = nargs; |
| 1919 | memcpy(ax->args, args, nargs * sizeof(unsigned long)); |
| 1920 | |
| 1921 | ax->d.type = AUDIT_SOCKETCALL; |
| 1922 | ax->d.next = context->aux; |
| 1923 | context->aux = (void *)ax; |
| 1924 | return 0; |
| 1925 | } |
| 1926 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1927 | /** |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 1928 | * __audit_fd_pair - record audit data for pipe and socketpair |
| 1929 | * @fd1: the first file descriptor |
| 1930 | * @fd2: the second file descriptor |
| 1931 | * |
| 1932 | * Returns 0 for success or NULL context or < 0 on error. |
| 1933 | */ |
| 1934 | int __audit_fd_pair(int fd1, int fd2) |
| 1935 | { |
| 1936 | struct audit_context *context = current->audit_context; |
| 1937 | struct audit_aux_data_fd_pair *ax; |
| 1938 | |
| 1939 | if (likely(!context)) { |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
| 1943 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); |
| 1944 | if (!ax) { |
| 1945 | return -ENOMEM; |
| 1946 | } |
| 1947 | |
| 1948 | ax->fd[0] = fd1; |
| 1949 | ax->fd[1] = fd2; |
| 1950 | |
| 1951 | ax->d.type = AUDIT_FD_PAIR; |
| 1952 | ax->d.next = context->aux; |
| 1953 | context->aux = (void *)ax; |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1958 | * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto |
| 1959 | * @len: data length in user space |
| 1960 | * @a: data address in kernel space |
| 1961 | * |
| 1962 | * Returns 0 for success or NULL context or < 0 on error. |
| 1963 | */ |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1964 | int audit_sockaddr(int len, void *a) |
| 1965 | { |
| 1966 | struct audit_aux_data_sockaddr *ax; |
| 1967 | struct audit_context *context = current->audit_context; |
| 1968 | |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 1969 | if (likely(!context || context->dummy)) |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1970 | return 0; |
| 1971 | |
| 1972 | ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL); |
| 1973 | if (!ax) |
| 1974 | return -ENOMEM; |
| 1975 | |
| 1976 | ax->len = len; |
| 1977 | memcpy(ax->a, a, len); |
| 1978 | |
| 1979 | ax->d.type = AUDIT_SOCKADDR; |
| 1980 | ax->d.next = context->aux; |
| 1981 | context->aux = (void *)ax; |
| 1982 | return 0; |
| 1983 | } |
| 1984 | |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 1985 | void __audit_ptrace(struct task_struct *t) |
| 1986 | { |
| 1987 | struct audit_context *context = current->audit_context; |
| 1988 | |
| 1989 | context->target_pid = t->pid; |
| 1990 | selinux_get_task_sid(t, &context->target_sid); |
| 1991 | } |
| 1992 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1993 | /** |
| 1994 | * audit_avc_path - record the granting or denial of permissions |
| 1995 | * @dentry: dentry to record |
| 1996 | * @mnt: mnt to record |
| 1997 | * |
| 1998 | * Returns 0 for success or NULL context or < 0 on error. |
| 1999 | * |
| 2000 | * Called from security/selinux/avc.c::avc_audit() |
| 2001 | */ |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 2002 | int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt) |
| 2003 | { |
| 2004 | struct audit_aux_data_path *ax; |
| 2005 | struct audit_context *context = current->audit_context; |
| 2006 | |
| 2007 | if (likely(!context)) |
| 2008 | return 0; |
| 2009 | |
| 2010 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 2011 | if (!ax) |
| 2012 | return -ENOMEM; |
| 2013 | |
| 2014 | ax->dentry = dget(dentry); |
| 2015 | ax->mnt = mntget(mnt); |
| 2016 | |
| 2017 | ax->d.type = AUDIT_AVC_PATH; |
| 2018 | ax->d.next = context->aux; |
| 2019 | context->aux = (void *)ax; |
| 2020 | return 0; |
| 2021 | } |
| 2022 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2023 | /** |
| 2024 | * audit_signal_info - record signal info for shutting down audit subsystem |
| 2025 | * @sig: signal value |
| 2026 | * @t: task being signaled |
| 2027 | * |
| 2028 | * If the audit subsystem is being terminated, record the task (pid) |
| 2029 | * and uid that is doing that. |
| 2030 | */ |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2031 | int __audit_signal_info(int sig, struct task_struct *t) |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2032 | { |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2033 | struct audit_aux_data_pids *axp; |
| 2034 | struct task_struct *tsk = current; |
| 2035 | struct audit_context *ctx = tsk->audit_context; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2036 | extern pid_t audit_sig_pid; |
| 2037 | extern uid_t audit_sig_uid; |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 2038 | extern u32 audit_sig_sid; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2039 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2040 | if (audit_pid && t->tgid == audit_pid && |
| 2041 | (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1)) { |
Al Viro | e139606 | 2006-05-25 10:19:47 -0400 | [diff] [blame] | 2042 | audit_sig_pid = tsk->pid; |
| 2043 | if (ctx) |
| 2044 | audit_sig_uid = ctx->loginuid; |
| 2045 | else |
| 2046 | audit_sig_uid = tsk->uid; |
| 2047 | selinux_get_task_sid(tsk, &audit_sig_sid); |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2048 | } |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2049 | |
| 2050 | if (!audit_signals) /* audit_context checked in wrapper */ |
| 2051 | return 0; |
| 2052 | |
| 2053 | /* optimize the common case by putting first signal recipient directly |
| 2054 | * in audit_context */ |
| 2055 | if (!ctx->target_pid) { |
| 2056 | ctx->target_pid = t->tgid; |
| 2057 | selinux_get_task_sid(t, &ctx->target_sid); |
| 2058 | return 0; |
| 2059 | } |
| 2060 | |
| 2061 | axp = (void *)ctx->aux_pids; |
| 2062 | if (!axp || axp->pid_count == AUDIT_AUX_PIDS) { |
| 2063 | axp = kzalloc(sizeof(*axp), GFP_ATOMIC); |
| 2064 | if (!axp) |
| 2065 | return -ENOMEM; |
| 2066 | |
| 2067 | axp->d.type = AUDIT_OBJ_PID; |
| 2068 | axp->d.next = ctx->aux_pids; |
| 2069 | ctx->aux_pids = (void *)axp; |
| 2070 | } |
| 2071 | BUG_ON(axp->pid_count > AUDIT_AUX_PIDS); |
| 2072 | |
| 2073 | axp->target_pid[axp->pid_count] = t->tgid; |
| 2074 | selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]); |
| 2075 | axp->pid_count++; |
| 2076 | |
| 2077 | return 0; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2078 | } |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2079 | |
| 2080 | /** |
| 2081 | * audit_core_dumps - record information about processes that end abnormally |
Henrik Kretzschmar | 6d9525b | 2007-07-15 23:41:10 -0700 | [diff] [blame] | 2082 | * @signr: signal value |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2083 | * |
| 2084 | * If a process ends with a core dump, something fishy is going on and we |
| 2085 | * should record the event for investigation. |
| 2086 | */ |
| 2087 | void audit_core_dumps(long signr) |
| 2088 | { |
| 2089 | struct audit_buffer *ab; |
| 2090 | u32 sid; |
| 2091 | |
| 2092 | if (!audit_enabled) |
| 2093 | return; |
| 2094 | |
| 2095 | if (signr == SIGQUIT) /* don't care for those */ |
| 2096 | return; |
| 2097 | |
| 2098 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND); |
| 2099 | audit_log_format(ab, "auid=%u uid=%u gid=%u", |
| 2100 | audit_get_loginuid(current->audit_context), |
| 2101 | current->uid, current->gid); |
| 2102 | selinux_get_task_sid(current, &sid); |
| 2103 | if (sid) { |
| 2104 | char *ctx = NULL; |
| 2105 | u32 len; |
| 2106 | |
| 2107 | if (selinux_sid_to_string(sid, &ctx, &len)) |
| 2108 | audit_log_format(ab, " ssid=%u", sid); |
| 2109 | else |
| 2110 | audit_log_format(ab, " subj=%s", ctx); |
| 2111 | kfree(ctx); |
| 2112 | } |
| 2113 | audit_log_format(ab, " pid=%d comm=", current->pid); |
| 2114 | audit_log_untrustedstring(ab, current->comm); |
| 2115 | audit_log_format(ab, " sig=%ld", signr); |
| 2116 | audit_log_end(ab); |
| 2117 | } |