| 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 <linux/fs.h> |
| 49 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/mm.h> |
| 51 | #include <linux/module.h> |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 52 | #include <linux/mount.h> |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 53 | #include <linux/socket.h> |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 54 | #include <linux/mqueue.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <linux/audit.h> |
| 56 | #include <linux/personality.h> |
| 57 | #include <linux/time.h> |
David Woodhouse | 5bb289b | 2005-06-24 14:14:05 +0100 | [diff] [blame] | 58 | #include <linux/netlink.h> |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 59 | #include <linux/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <asm/unistd.h> |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 61 | #include <linux/security.h> |
David Woodhouse | fe7752b | 2005-12-15 18:33:52 +0000 | [diff] [blame] | 62 | #include <linux/list.h> |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 63 | #include <linux/tty.h> |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 64 | #include <linux/binfmts.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 65 | #include <linux/highmem.h> |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 66 | #include <linux/syscalls.h> |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 67 | #include <linux/inotify.h> |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 68 | #include <linux/capability.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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* AUDIT_NAMES is the number of slots we reserve in the audit_context |
| 73 | * for saving names from getname(). */ |
| 74 | #define AUDIT_NAMES 20 |
| 75 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 76 | /* Indicates that audit should log the full pathname. */ |
| 77 | #define AUDIT_NAME_FULL -1 |
| 78 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 79 | /* no execve audit message should be longer than this (userspace limits) */ |
| 80 | #define MAX_EXECVE_AUDIT_LEN 7500 |
| 81 | |
Al Viro | 471a5c7 | 2006-07-10 08:29:24 -0400 | [diff] [blame] | 82 | /* number of audit rules */ |
| 83 | int audit_n_rules; |
| 84 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 85 | /* determines whether we collect data for signals sent */ |
| 86 | int audit_signals; |
| 87 | |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 88 | struct audit_cap_data { |
| 89 | kernel_cap_t permitted; |
| 90 | kernel_cap_t inheritable; |
| 91 | union { |
| 92 | unsigned int fE; /* effective bit of a file capability */ |
| 93 | kernel_cap_t effective; /* effective set of a process */ |
| 94 | }; |
| 95 | }; |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* When fs/namei.c:getname() is called, we store the pointer in name and |
| 98 | * we don't let putname() free it (instead we free all of the saved |
| 99 | * pointers at syscall exit time). |
| 100 | * |
| 101 | * Further, in fs/namei.c:path_lookup() we store the inode and device. */ |
| 102 | struct audit_names { |
| 103 | const char *name; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 104 | int name_len; /* number of name's characters to log */ |
| 105 | unsigned name_put; /* call __putname() for this name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | unsigned long ino; |
| 107 | dev_t dev; |
| 108 | umode_t mode; |
| 109 | uid_t uid; |
| 110 | gid_t gid; |
| 111 | dev_t rdev; |
Steve Grubb | 1b50eed | 2006-04-03 14:06:13 -0400 | [diff] [blame] | 112 | u32 osid; |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 113 | struct audit_cap_data fcap; |
| 114 | unsigned int fcap_ver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | struct audit_aux_data { |
| 118 | struct audit_aux_data *next; |
| 119 | int type; |
| 120 | }; |
| 121 | |
| 122 | #define AUDIT_AUX_IPCPERM 0 |
| 123 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 124 | /* Number of target pids per aux struct. */ |
| 125 | #define AUDIT_AUX_PIDS 16 |
| 126 | |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 127 | struct audit_aux_data_mq_open { |
| 128 | struct audit_aux_data d; |
| 129 | int oflag; |
| 130 | mode_t mode; |
| 131 | struct mq_attr attr; |
| 132 | }; |
| 133 | |
| 134 | struct audit_aux_data_mq_sendrecv { |
| 135 | struct audit_aux_data d; |
| 136 | mqd_t mqdes; |
| 137 | size_t msg_len; |
| 138 | unsigned int msg_prio; |
| 139 | struct timespec abs_timeout; |
| 140 | }; |
| 141 | |
| 142 | struct audit_aux_data_mq_notify { |
| 143 | struct audit_aux_data d; |
| 144 | mqd_t mqdes; |
| 145 | struct sigevent notification; |
| 146 | }; |
| 147 | |
| 148 | struct audit_aux_data_mq_getsetattr { |
| 149 | struct audit_aux_data d; |
| 150 | mqd_t mqdes; |
| 151 | struct mq_attr mqstat; |
| 152 | }; |
| 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | struct audit_aux_data_ipcctl { |
| 155 | struct audit_aux_data d; |
| 156 | struct ipc_perm p; |
| 157 | unsigned long qbytes; |
| 158 | uid_t uid; |
| 159 | gid_t gid; |
| 160 | mode_t mode; |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 161 | u32 osid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 164 | struct audit_aux_data_execve { |
| 165 | struct audit_aux_data d; |
| 166 | int argc; |
| 167 | int envc; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 168 | struct mm_struct *mm; |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 169 | }; |
| 170 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 171 | struct audit_aux_data_socketcall { |
| 172 | struct audit_aux_data d; |
| 173 | int nargs; |
| 174 | unsigned long args[0]; |
| 175 | }; |
| 176 | |
| 177 | struct audit_aux_data_sockaddr { |
| 178 | struct audit_aux_data d; |
| 179 | int len; |
| 180 | char a[0]; |
| 181 | }; |
| 182 | |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 183 | struct audit_aux_data_fd_pair { |
| 184 | struct audit_aux_data d; |
| 185 | int fd[2]; |
| 186 | }; |
| 187 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 188 | struct audit_aux_data_pids { |
| 189 | struct audit_aux_data d; |
| 190 | pid_t target_pid[AUDIT_AUX_PIDS]; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 191 | uid_t target_auid[AUDIT_AUX_PIDS]; |
| 192 | uid_t target_uid[AUDIT_AUX_PIDS]; |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 193 | unsigned int target_sessionid[AUDIT_AUX_PIDS]; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 194 | u32 target_sid[AUDIT_AUX_PIDS]; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 195 | char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN]; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 196 | int pid_count; |
| 197 | }; |
| 198 | |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame^] | 199 | struct audit_aux_data_bprm_fcaps { |
| 200 | struct audit_aux_data d; |
| 201 | struct audit_cap_data fcap; |
| 202 | unsigned int fcap_ver; |
| 203 | struct audit_cap_data old_pcap; |
| 204 | struct audit_cap_data new_pcap; |
| 205 | }; |
| 206 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 207 | struct audit_tree_refs { |
| 208 | struct audit_tree_refs *next; |
| 209 | struct audit_chunk *c[31]; |
| 210 | }; |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /* The per-task audit context. */ |
| 213 | struct audit_context { |
Al Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 214 | int dummy; /* must be the first element */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | int in_syscall; /* 1 if task is in a syscall */ |
| 216 | enum audit_state state; |
| 217 | unsigned int serial; /* serial number for record */ |
| 218 | struct timespec ctime; /* time of syscall entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | int major; /* syscall number */ |
| 220 | unsigned long argv[4]; /* syscall arguments */ |
| 221 | int return_valid; /* return code is valid */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 222 | long return_code;/* syscall return code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | int auditable; /* 1 if record should be written */ |
| 224 | int name_count; |
| 225 | struct audit_names names[AUDIT_NAMES]; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 226 | char * filterkey; /* key for rule that triggered record */ |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 227 | struct path pwd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | struct audit_context *previous; /* For nested syscalls */ |
| 229 | struct audit_aux_data *aux; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 230 | struct audit_aux_data *aux_pids; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | /* Save things to print about task_struct */ |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 233 | pid_t pid, ppid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | uid_t uid, euid, suid, fsuid; |
| 235 | gid_t gid, egid, sgid, fsgid; |
| 236 | unsigned long personality; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 237 | int arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 239 | pid_t target_pid; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 240 | uid_t target_auid; |
| 241 | uid_t target_uid; |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 242 | unsigned int target_sessionid; |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 243 | u32 target_sid; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 244 | char target_comm[TASK_COMM_LEN]; |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 245 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 246 | struct audit_tree_refs *trees, *first_trees; |
| 247 | int tree_count; |
| 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | #if AUDIT_DEBUG |
| 250 | int put_count; |
| 251 | int ino_count; |
| 252 | #endif |
| 253 | }; |
| 254 | |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 255 | #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE]) |
| 256 | static inline int open_arg(int flags, int mask) |
| 257 | { |
| 258 | int n = ACC_MODE(flags); |
| 259 | if (flags & (O_TRUNC | O_CREAT)) |
| 260 | n |= AUDIT_PERM_WRITE; |
| 261 | return n & mask; |
| 262 | } |
| 263 | |
| 264 | static int audit_match_perm(struct audit_context *ctx, int mask) |
| 265 | { |
Cordelia | c4bacef | 2008-08-18 09:45:51 -0700 | [diff] [blame] | 266 | unsigned n; |
zhangxiliang | 1a61c88 | 2008-08-02 10:56:37 +0800 | [diff] [blame] | 267 | if (unlikely(!ctx)) |
| 268 | return 0; |
Cordelia | c4bacef | 2008-08-18 09:45:51 -0700 | [diff] [blame] | 269 | n = ctx->major; |
Alan Cox | dbda4c0 | 2008-10-13 10:40:53 +0100 | [diff] [blame] | 270 | |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 271 | switch (audit_classify_syscall(ctx->arch, n)) { |
| 272 | case 0: /* native */ |
| 273 | if ((mask & AUDIT_PERM_WRITE) && |
| 274 | audit_match_class(AUDIT_CLASS_WRITE, n)) |
| 275 | return 1; |
| 276 | if ((mask & AUDIT_PERM_READ) && |
| 277 | audit_match_class(AUDIT_CLASS_READ, n)) |
| 278 | return 1; |
| 279 | if ((mask & AUDIT_PERM_ATTR) && |
| 280 | audit_match_class(AUDIT_CLASS_CHATTR, n)) |
| 281 | return 1; |
| 282 | return 0; |
| 283 | case 1: /* 32bit on biarch */ |
| 284 | if ((mask & AUDIT_PERM_WRITE) && |
| 285 | audit_match_class(AUDIT_CLASS_WRITE_32, n)) |
| 286 | return 1; |
| 287 | if ((mask & AUDIT_PERM_READ) && |
| 288 | audit_match_class(AUDIT_CLASS_READ_32, n)) |
| 289 | return 1; |
| 290 | if ((mask & AUDIT_PERM_ATTR) && |
| 291 | audit_match_class(AUDIT_CLASS_CHATTR_32, n)) |
| 292 | return 1; |
| 293 | return 0; |
| 294 | case 2: /* open */ |
| 295 | return mask & ACC_MODE(ctx->argv[1]); |
| 296 | case 3: /* openat */ |
| 297 | return mask & ACC_MODE(ctx->argv[2]); |
| 298 | case 4: /* socketcall */ |
| 299 | return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND); |
| 300 | case 5: /* execve */ |
| 301 | return mask & AUDIT_PERM_EXEC; |
| 302 | default: |
| 303 | return 0; |
| 304 | } |
| 305 | } |
| 306 | |
Al Viro | 8b67dca | 2008-04-28 04:15:49 -0400 | [diff] [blame] | 307 | static int audit_match_filetype(struct audit_context *ctx, int which) |
| 308 | { |
| 309 | unsigned index = which & ~S_IFMT; |
| 310 | mode_t mode = which & S_IFMT; |
zhangxiliang | 1a61c88 | 2008-08-02 10:56:37 +0800 | [diff] [blame] | 311 | |
| 312 | if (unlikely(!ctx)) |
| 313 | return 0; |
| 314 | |
Al Viro | 8b67dca | 2008-04-28 04:15:49 -0400 | [diff] [blame] | 315 | if (index >= ctx->name_count) |
| 316 | return 0; |
| 317 | if (ctx->names[index].ino == -1) |
| 318 | return 0; |
| 319 | if ((ctx->names[index].mode ^ mode) & S_IFMT) |
| 320 | return 0; |
| 321 | return 1; |
| 322 | } |
| 323 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 324 | /* |
| 325 | * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *; |
| 326 | * ->first_trees points to its beginning, ->trees - to the current end of data. |
| 327 | * ->tree_count is the number of free entries in array pointed to by ->trees. |
| 328 | * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL, |
| 329 | * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously, |
| 330 | * it's going to remain 1-element for almost any setup) until we free context itself. |
| 331 | * References in it _are_ dropped - at the same time we free/drop aux stuff. |
| 332 | */ |
| 333 | |
| 334 | #ifdef CONFIG_AUDIT_TREE |
| 335 | static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk) |
| 336 | { |
| 337 | struct audit_tree_refs *p = ctx->trees; |
| 338 | int left = ctx->tree_count; |
| 339 | if (likely(left)) { |
| 340 | p->c[--left] = chunk; |
| 341 | ctx->tree_count = left; |
| 342 | return 1; |
| 343 | } |
| 344 | if (!p) |
| 345 | return 0; |
| 346 | p = p->next; |
| 347 | if (p) { |
| 348 | p->c[30] = chunk; |
| 349 | ctx->trees = p; |
| 350 | ctx->tree_count = 30; |
| 351 | return 1; |
| 352 | } |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static int grow_tree_refs(struct audit_context *ctx) |
| 357 | { |
| 358 | struct audit_tree_refs *p = ctx->trees; |
| 359 | ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL); |
| 360 | if (!ctx->trees) { |
| 361 | ctx->trees = p; |
| 362 | return 0; |
| 363 | } |
| 364 | if (p) |
| 365 | p->next = ctx->trees; |
| 366 | else |
| 367 | ctx->first_trees = ctx->trees; |
| 368 | ctx->tree_count = 31; |
| 369 | return 1; |
| 370 | } |
| 371 | #endif |
| 372 | |
| 373 | static void unroll_tree_refs(struct audit_context *ctx, |
| 374 | struct audit_tree_refs *p, int count) |
| 375 | { |
| 376 | #ifdef CONFIG_AUDIT_TREE |
| 377 | struct audit_tree_refs *q; |
| 378 | int n; |
| 379 | if (!p) { |
| 380 | /* we started with empty chain */ |
| 381 | p = ctx->first_trees; |
| 382 | count = 31; |
| 383 | /* if the very first allocation has failed, nothing to do */ |
| 384 | if (!p) |
| 385 | return; |
| 386 | } |
| 387 | n = count; |
| 388 | for (q = p; q != ctx->trees; q = q->next, n = 31) { |
| 389 | while (n--) { |
| 390 | audit_put_chunk(q->c[n]); |
| 391 | q->c[n] = NULL; |
| 392 | } |
| 393 | } |
| 394 | while (n-- > ctx->tree_count) { |
| 395 | audit_put_chunk(q->c[n]); |
| 396 | q->c[n] = NULL; |
| 397 | } |
| 398 | ctx->trees = p; |
| 399 | ctx->tree_count = count; |
| 400 | #endif |
| 401 | } |
| 402 | |
| 403 | static void free_tree_refs(struct audit_context *ctx) |
| 404 | { |
| 405 | struct audit_tree_refs *p, *q; |
| 406 | for (p = ctx->first_trees; p; p = q) { |
| 407 | q = p->next; |
| 408 | kfree(p); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree) |
| 413 | { |
| 414 | #ifdef CONFIG_AUDIT_TREE |
| 415 | struct audit_tree_refs *p; |
| 416 | int n; |
| 417 | if (!tree) |
| 418 | return 0; |
| 419 | /* full ones */ |
| 420 | for (p = ctx->first_trees; p != ctx->trees; p = p->next) { |
| 421 | for (n = 0; n < 31; n++) |
| 422 | if (audit_tree_match(p->c[n], tree)) |
| 423 | return 1; |
| 424 | } |
| 425 | /* partial */ |
| 426 | if (p) { |
| 427 | for (n = ctx->tree_count; n < 31; n++) |
| 428 | if (audit_tree_match(p->c[n], tree)) |
| 429 | return 1; |
| 430 | } |
| 431 | #endif |
| 432 | return 0; |
| 433 | } |
| 434 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 435 | /* Determine if any context name data matches a rule's watch data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 |
| 437 | * otherwise. */ |
| 438 | static int audit_filter_rules(struct task_struct *tsk, |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 439 | struct audit_krule *rule, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | struct audit_context *ctx, |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 441 | struct audit_names *name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | enum audit_state *state) |
| 443 | { |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 444 | int i, j, need_sid = 1; |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 445 | u32 sid; |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | for (i = 0; i < rule->field_count; i++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 448 | struct audit_field *f = &rule->fields[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | int result = 0; |
| 450 | |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 451 | switch (f->type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | case AUDIT_PID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 453 | result = audit_comparator(tsk->pid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | break; |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 455 | case AUDIT_PPID: |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 456 | if (ctx) { |
| 457 | if (!ctx->ppid) |
| 458 | ctx->ppid = sys_getppid(); |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 459 | result = audit_comparator(ctx->ppid, f->op, f->val); |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 460 | } |
Al Viro | 3c66251 | 2006-05-06 08:26:27 -0400 | [diff] [blame] | 461 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | case AUDIT_UID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 463 | result = audit_comparator(tsk->uid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | break; |
| 465 | case AUDIT_EUID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 466 | result = audit_comparator(tsk->euid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | break; |
| 468 | case AUDIT_SUID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 469 | result = audit_comparator(tsk->suid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | break; |
| 471 | case AUDIT_FSUID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 472 | result = audit_comparator(tsk->fsuid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | break; |
| 474 | case AUDIT_GID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 475 | result = audit_comparator(tsk->gid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | break; |
| 477 | case AUDIT_EGID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 478 | result = audit_comparator(tsk->egid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | break; |
| 480 | case AUDIT_SGID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 481 | result = audit_comparator(tsk->sgid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | break; |
| 483 | case AUDIT_FSGID: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 484 | result = audit_comparator(tsk->fsgid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | break; |
| 486 | case AUDIT_PERS: |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 487 | result = audit_comparator(tsk->personality, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | break; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 489 | case AUDIT_ARCH: |
Daniel Walker | 9f8dbe9 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 490 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 491 | result = audit_comparator(ctx->arch, f->op, f->val); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 492 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | case AUDIT_EXIT: |
| 495 | if (ctx && ctx->return_valid) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 496 | result = audit_comparator(ctx->return_code, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | break; |
| 498 | case AUDIT_SUCCESS: |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 499 | if (ctx && ctx->return_valid) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 500 | if (f->val) |
| 501 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 502 | else |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 503 | result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE); |
David Woodhouse | b01f2cc | 2005-08-27 10:25:43 +0100 | [diff] [blame] | 504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | break; |
| 506 | case AUDIT_DEVMAJOR: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 507 | if (name) |
| 508 | result = audit_comparator(MAJOR(name->dev), |
| 509 | f->op, f->val); |
| 510 | else if (ctx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 512 | if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | ++result; |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | break; |
| 519 | case AUDIT_DEVMINOR: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 520 | if (name) |
| 521 | result = audit_comparator(MINOR(name->dev), |
| 522 | f->op, f->val); |
| 523 | else if (ctx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 525 | if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | ++result; |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | break; |
| 532 | case AUDIT_INODE: |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 533 | if (name) |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 534 | result = (name->ino == f->val); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 535 | else if (ctx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | for (j = 0; j < ctx->name_count; j++) { |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 537 | if (audit_comparator(ctx->names[j].ino, f->op, f->val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | ++result; |
| 539 | break; |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | break; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 544 | case AUDIT_WATCH: |
| 545 | if (name && rule->watch->ino != (unsigned long)-1) |
| 546 | result = (name->dev == rule->watch->dev && |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 547 | name->ino == rule->watch->ino); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 548 | break; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 549 | case AUDIT_DIR: |
| 550 | if (ctx) |
| 551 | result = match_tree_refs(ctx, rule->tree); |
| 552 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | case AUDIT_LOGINUID: |
| 554 | result = 0; |
| 555 | if (ctx) |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 556 | result = audit_comparator(tsk->loginuid, f->op, f->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | break; |
Darrel Goeddel | 3a6b9f8 | 2006-06-29 16:56:39 -0500 | [diff] [blame] | 558 | case AUDIT_SUBJ_USER: |
| 559 | case AUDIT_SUBJ_ROLE: |
| 560 | case AUDIT_SUBJ_TYPE: |
| 561 | case AUDIT_SUBJ_SEN: |
| 562 | case AUDIT_SUBJ_CLR: |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 563 | /* NOTE: this may return negative values indicating |
| 564 | a temporary error. We simply treat this as a |
| 565 | match for now to avoid losing information that |
| 566 | may be wanted. An error message will also be |
| 567 | logged upon error */ |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 568 | if (f->lsm_rule) { |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 569 | if (need_sid) { |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 570 | security_task_getsecid(tsk, &sid); |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 571 | need_sid = 0; |
| 572 | } |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 573 | result = security_audit_rule_match(sid, f->type, |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 574 | f->op, |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 575 | f->lsm_rule, |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 576 | ctx); |
Steve Grubb | 2ad312d | 2006-04-11 08:50:56 -0400 | [diff] [blame] | 577 | } |
Darrel Goeddel | 3dc7e31 | 2006-03-10 18:14:06 -0600 | [diff] [blame] | 578 | break; |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 579 | case AUDIT_OBJ_USER: |
| 580 | case AUDIT_OBJ_ROLE: |
| 581 | case AUDIT_OBJ_TYPE: |
| 582 | case AUDIT_OBJ_LEV_LOW: |
| 583 | case AUDIT_OBJ_LEV_HIGH: |
| 584 | /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR |
| 585 | also applies here */ |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 586 | if (f->lsm_rule) { |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 587 | /* Find files that match */ |
| 588 | if (name) { |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 589 | result = security_audit_rule_match( |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 590 | name->osid, f->type, f->op, |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 591 | f->lsm_rule, ctx); |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 592 | } else if (ctx) { |
| 593 | for (j = 0; j < ctx->name_count; j++) { |
Ahmed S. Darwish | d7a96f3 | 2008-03-01 22:01:11 +0200 | [diff] [blame] | 594 | if (security_audit_rule_match( |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 595 | ctx->names[j].osid, |
| 596 | f->type, f->op, |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 597 | f->lsm_rule, ctx)) { |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 598 | ++result; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | /* Find ipc objects that match */ |
| 604 | if (ctx) { |
| 605 | struct audit_aux_data *aux; |
| 606 | for (aux = ctx->aux; aux; |
| 607 | aux = aux->next) { |
| 608 | if (aux->type == AUDIT_IPC) { |
| 609 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
Ahmed S. Darwish | 04305e4 | 2008-04-19 09:59:43 +1000 | [diff] [blame] | 610 | if (security_audit_rule_match(axi->osid, f->type, f->op, f->lsm_rule, ctx)) { |
Darrel Goeddel | 6e5a2d1 | 2006-06-29 16:57:08 -0500 | [diff] [blame] | 611 | ++result; |
| 612 | break; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | case AUDIT_ARG0: |
| 620 | case AUDIT_ARG1: |
| 621 | case AUDIT_ARG2: |
| 622 | case AUDIT_ARG3: |
| 623 | if (ctx) |
Amy Griffis | 93315ed | 2006-02-07 12:05:27 -0500 | [diff] [blame] | 624 | 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] | 625 | break; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 626 | case AUDIT_FILTERKEY: |
| 627 | /* ignore this field for filtering */ |
| 628 | result = 1; |
| 629 | break; |
Al Viro | 55669bf | 2006-08-31 19:26:40 -0400 | [diff] [blame] | 630 | case AUDIT_PERM: |
| 631 | result = audit_match_perm(ctx, f->val); |
| 632 | break; |
Al Viro | 8b67dca | 2008-04-28 04:15:49 -0400 | [diff] [blame] | 633 | case AUDIT_FILETYPE: |
| 634 | result = audit_match_filetype(ctx, f->val); |
| 635 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | if (!result) |
| 639 | return 0; |
| 640 | } |
zhangxiliang | 980dfb0 | 2008-08-01 19:15:47 +0800 | [diff] [blame] | 641 | if (rule->filterkey && ctx) |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 642 | ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | switch (rule->action) { |
| 644 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
| 646 | } |
| 647 | return 1; |
| 648 | } |
| 649 | |
| 650 | /* At process creation time, we can determine if system-call auditing is |
| 651 | * completely disabled for this task. Since we only have the task |
| 652 | * structure at this point, we can only check uid and gid. |
| 653 | */ |
| 654 | static enum audit_state audit_filter_task(struct task_struct *tsk) |
| 655 | { |
| 656 | struct audit_entry *e; |
| 657 | enum audit_state state; |
| 658 | |
| 659 | rcu_read_lock(); |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 660 | 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] | 661 | if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | rcu_read_unlock(); |
| 663 | return state; |
| 664 | } |
| 665 | } |
| 666 | rcu_read_unlock(); |
| 667 | return AUDIT_BUILD_CONTEXT; |
| 668 | } |
| 669 | |
| 670 | /* At syscall entry and exit time, this filter is called if the |
| 671 | * 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] | 672 | * 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] | 673 | * 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] | 674 | */ |
| 675 | static enum audit_state audit_filter_syscall(struct task_struct *tsk, |
| 676 | struct audit_context *ctx, |
| 677 | struct list_head *list) |
| 678 | { |
| 679 | struct audit_entry *e; |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 680 | enum audit_state state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
David Woodhouse | 351bb72 | 2005-07-14 14:40:06 +0100 | [diff] [blame] | 682 | if (audit_pid && tsk->tgid == audit_pid) |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 683 | return AUDIT_DISABLED; |
| 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | rcu_read_lock(); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 686 | if (!list_empty(list)) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 687 | int word = AUDIT_WORD(ctx->major); |
| 688 | int bit = AUDIT_BIT(ctx->major); |
David Woodhouse | c389649 | 2005-08-17 14:49:57 +0100 | [diff] [blame] | 689 | |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 690 | list_for_each_entry_rcu(e, list, list) { |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 691 | if ((e->rule.mask[word] & bit) == bit && |
| 692 | audit_filter_rules(tsk, &e->rule, ctx, NULL, |
| 693 | &state)) { |
Dustin Kirkland | b63862f | 2005-11-03 15:41:46 +0000 | [diff] [blame] | 694 | rcu_read_unlock(); |
| 695 | return state; |
| 696 | } |
| 697 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | rcu_read_unlock(); |
| 700 | return AUDIT_BUILD_CONTEXT; |
| 701 | } |
| 702 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 703 | /* At syscall exit time, this filter is called if any audit_names[] have been |
| 704 | * collected during syscall processing. We only check rules in sublists at hash |
| 705 | * buckets applicable to the inode numbers in audit_names[]. |
| 706 | * Regarding audit_state, same rules apply as for audit_filter_syscall(). |
| 707 | */ |
| 708 | enum audit_state audit_filter_inodes(struct task_struct *tsk, |
| 709 | struct audit_context *ctx) |
| 710 | { |
| 711 | int i; |
| 712 | struct audit_entry *e; |
| 713 | enum audit_state state; |
| 714 | |
| 715 | if (audit_pid && tsk->tgid == audit_pid) |
| 716 | return AUDIT_DISABLED; |
| 717 | |
| 718 | rcu_read_lock(); |
| 719 | for (i = 0; i < ctx->name_count; i++) { |
| 720 | int word = AUDIT_WORD(ctx->major); |
| 721 | int bit = AUDIT_BIT(ctx->major); |
| 722 | struct audit_names *n = &ctx->names[i]; |
| 723 | int h = audit_hash_ino((u32)n->ino); |
| 724 | struct list_head *list = &audit_inode_hash[h]; |
| 725 | |
| 726 | if (list_empty(list)) |
| 727 | continue; |
| 728 | |
| 729 | list_for_each_entry_rcu(e, list, list) { |
| 730 | if ((e->rule.mask[word] & bit) == bit && |
| 731 | audit_filter_rules(tsk, &e->rule, ctx, n, &state)) { |
| 732 | rcu_read_unlock(); |
| 733 | return state; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | rcu_read_unlock(); |
| 738 | return AUDIT_BUILD_CONTEXT; |
| 739 | } |
| 740 | |
| 741 | void audit_set_auditable(struct audit_context *ctx) |
| 742 | { |
| 743 | ctx->auditable = 1; |
| 744 | } |
| 745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | static inline struct audit_context *audit_get_context(struct task_struct *tsk, |
| 747 | int return_valid, |
| 748 | int return_code) |
| 749 | { |
| 750 | struct audit_context *context = tsk->audit_context; |
| 751 | |
| 752 | if (likely(!context)) |
| 753 | return NULL; |
| 754 | context->return_valid = return_valid; |
Eric Paris | f701b75 | 2008-01-07 13:34:51 -0500 | [diff] [blame] | 755 | |
| 756 | /* |
| 757 | * we need to fix up the return code in the audit logs if the actual |
| 758 | * return codes are later going to be fixed up by the arch specific |
| 759 | * signal handlers |
| 760 | * |
| 761 | * This is actually a test for: |
| 762 | * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) || |
| 763 | * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK) |
| 764 | * |
| 765 | * but is faster than a bunch of || |
| 766 | */ |
| 767 | if (unlikely(return_code <= -ERESTARTSYS) && |
| 768 | (return_code >= -ERESTART_RESTARTBLOCK) && |
| 769 | (return_code != -ENOIOCTLCMD)) |
| 770 | context->return_code = -EINTR; |
| 771 | else |
| 772 | context->return_code = return_code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
Al Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 774 | if (context->in_syscall && !context->dummy && !context->auditable) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | enum audit_state state; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 776 | |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 777 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]); |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 778 | if (state == AUDIT_RECORD_CONTEXT) { |
| 779 | context->auditable = 1; |
| 780 | goto get_context; |
| 781 | } |
| 782 | |
| 783 | state = audit_filter_inodes(tsk, context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | if (state == AUDIT_RECORD_CONTEXT) |
| 785 | context->auditable = 1; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 789 | get_context: |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | tsk->audit_context = NULL; |
| 792 | return context; |
| 793 | } |
| 794 | |
| 795 | static inline void audit_free_names(struct audit_context *context) |
| 796 | { |
| 797 | int i; |
| 798 | |
| 799 | #if AUDIT_DEBUG == 2 |
| 800 | if (context->auditable |
| 801 | ||context->put_count + context->ino_count != context->name_count) { |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 802 | printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | " name_count=%d put_count=%d" |
| 804 | " ino_count=%d [NOT freeing]\n", |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 805 | __FILE__, __LINE__, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | context->serial, context->major, context->in_syscall, |
| 807 | context->name_count, context->put_count, |
| 808 | context->ino_count); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 809 | for (i = 0; i < context->name_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | printk(KERN_ERR "names[%d] = %p = %s\n", i, |
| 811 | context->names[i].name, |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 812 | context->names[i].name ?: "(null)"); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | dump_stack(); |
| 815 | return; |
| 816 | } |
| 817 | #endif |
| 818 | #if AUDIT_DEBUG |
| 819 | context->put_count = 0; |
| 820 | context->ino_count = 0; |
| 821 | #endif |
| 822 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 823 | for (i = 0; i < context->name_count; i++) { |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 824 | if (context->names[i].name && context->names[i].name_put) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | __putname(context->names[i].name); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | context->name_count = 0; |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 828 | path_put(&context->pwd); |
| 829 | context->pwd.dentry = NULL; |
| 830 | context->pwd.mnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | static inline void audit_free_aux(struct audit_context *context) |
| 834 | { |
| 835 | struct audit_aux_data *aux; |
| 836 | |
| 837 | while ((aux = context->aux)) { |
| 838 | context->aux = aux->next; |
| 839 | kfree(aux); |
| 840 | } |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 841 | while ((aux = context->aux_pids)) { |
| 842 | context->aux_pids = aux->next; |
| 843 | kfree(aux); |
| 844 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static inline void audit_zero_context(struct audit_context *context, |
| 848 | enum audit_state state) |
| 849 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | memset(context, 0, sizeof(*context)); |
| 851 | context->state = state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static inline struct audit_context *audit_alloc_context(enum audit_state state) |
| 855 | { |
| 856 | struct audit_context *context; |
| 857 | |
| 858 | if (!(context = kmalloc(sizeof(*context), GFP_KERNEL))) |
| 859 | return NULL; |
| 860 | audit_zero_context(context, state); |
| 861 | return context; |
| 862 | } |
| 863 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 864 | /** |
| 865 | * audit_alloc - allocate an audit context block for a task |
| 866 | * @tsk: task |
| 867 | * |
| 868 | * Filter on the task information and allocate a per-task audit context |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | * if necessary. Doing so turns on system call auditing for the |
| 870 | * specified task. This is called from copy_process, so no lock is |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 871 | * needed. |
| 872 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | int audit_alloc(struct task_struct *tsk) |
| 874 | { |
| 875 | struct audit_context *context; |
| 876 | enum audit_state state; |
| 877 | |
Eric Paris | b593d38 | 2008-01-08 17:38:31 -0500 | [diff] [blame] | 878 | if (likely(!audit_ever_enabled)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return 0; /* Return if not auditing. */ |
| 880 | |
| 881 | state = audit_filter_task(tsk); |
| 882 | if (likely(state == AUDIT_DISABLED)) |
| 883 | return 0; |
| 884 | |
| 885 | if (!(context = audit_alloc_context(state))) { |
| 886 | audit_log_lost("out of memory in audit_alloc"); |
| 887 | return -ENOMEM; |
| 888 | } |
| 889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | tsk->audit_context = context; |
| 891 | set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static inline void audit_free_context(struct audit_context *context) |
| 896 | { |
| 897 | struct audit_context *previous; |
| 898 | int count = 0; |
| 899 | |
| 900 | do { |
| 901 | previous = context->previous; |
| 902 | if (previous || (count && count < 10)) { |
| 903 | ++count; |
| 904 | printk(KERN_ERR "audit(:%d): major=%d name_count=%d:" |
| 905 | " freeing multiple contexts (%d)\n", |
| 906 | context->serial, context->major, |
| 907 | context->name_count, count); |
| 908 | } |
| 909 | audit_free_names(context); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 910 | unroll_tree_refs(context, NULL, 0); |
| 911 | free_tree_refs(context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | audit_free_aux(context); |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 913 | kfree(context->filterkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | kfree(context); |
| 915 | context = previous; |
| 916 | } while (context); |
| 917 | if (count >= 10) |
| 918 | printk(KERN_ERR "audit: freed %d contexts\n", count); |
| 919 | } |
| 920 | |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 921 | void audit_log_task_context(struct audit_buffer *ab) |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 922 | { |
| 923 | char *ctx = NULL; |
Al Viro | c4823bc | 2007-03-12 16:17:42 +0000 | [diff] [blame] | 924 | unsigned len; |
| 925 | int error; |
| 926 | u32 sid; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 927 | |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 928 | security_task_getsecid(current, &sid); |
Al Viro | c4823bc | 2007-03-12 16:17:42 +0000 | [diff] [blame] | 929 | if (!sid) |
| 930 | return; |
| 931 | |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 932 | error = security_secid_to_secctx(sid, &ctx, &len); |
Al Viro | c4823bc | 2007-03-12 16:17:42 +0000 | [diff] [blame] | 933 | if (error) { |
| 934 | if (error != -EINVAL) |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 935 | goto error_path; |
| 936 | return; |
| 937 | } |
| 938 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 939 | audit_log_format(ab, " subj=%s", ctx); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 940 | security_release_secctx(ctx, len); |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame] | 941 | return; |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 942 | |
| 943 | error_path: |
Dustin Kirkland | 7306a0b | 2005-11-16 15:53:13 +0000 | [diff] [blame] | 944 | audit_panic("error in audit_log_task_context"); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | |
Joy Latten | 161a09e | 2006-11-27 13:11:54 -0600 | [diff] [blame] | 948 | EXPORT_SYMBOL(audit_log_task_context); |
| 949 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 950 | 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] | 951 | { |
Al Viro | 45d9bb0 | 2006-03-29 20:02:55 -0500 | [diff] [blame] | 952 | char name[sizeof(tsk->comm)]; |
| 953 | struct mm_struct *mm = tsk->mm; |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 954 | struct vm_area_struct *vma; |
| 955 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 956 | /* tsk == current */ |
| 957 | |
Al Viro | 45d9bb0 | 2006-03-29 20:02:55 -0500 | [diff] [blame] | 958 | get_task_comm(name, tsk); |
David Woodhouse | 99e45ee | 2005-05-23 21:57:41 +0100 | [diff] [blame] | 959 | audit_log_format(ab, " comm="); |
| 960 | audit_log_untrustedstring(ab, name); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 961 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 962 | if (mm) { |
| 963 | down_read(&mm->mmap_sem); |
| 964 | vma = mm->mmap; |
| 965 | while (vma) { |
| 966 | if ((vma->vm_flags & VM_EXECUTABLE) && |
| 967 | vma->vm_file) { |
| 968 | audit_log_d_path(ab, "exe=", |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 969 | &vma->vm_file->f_path); |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 970 | break; |
| 971 | } |
| 972 | vma = vma->vm_next; |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 973 | } |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 974 | up_read(&mm->mmap_sem); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 975 | } |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 976 | audit_log_task_context(ab); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 977 | } |
| 978 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 979 | static int audit_log_pid_context(struct audit_context *context, pid_t pid, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 980 | uid_t auid, uid_t uid, unsigned int sessionid, |
| 981 | u32 sid, char *comm) |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 982 | { |
| 983 | struct audit_buffer *ab; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 984 | char *ctx = NULL; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 985 | u32 len; |
| 986 | int rc = 0; |
| 987 | |
| 988 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID); |
| 989 | if (!ab) |
Eric Paris | 6246cca | 2008-01-07 14:01:18 -0500 | [diff] [blame] | 990 | return rc; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 991 | |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 992 | audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid, |
| 993 | uid, sessionid); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 994 | if (security_secid_to_secctx(sid, &ctx, &len)) { |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 995 | audit_log_format(ab, " obj=(none)"); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 996 | rc = 1; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 997 | } else { |
| 998 | audit_log_format(ab, " obj=%s", ctx); |
| 999 | security_release_secctx(ctx, len); |
| 1000 | } |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 1001 | audit_log_format(ab, " ocomm="); |
| 1002 | audit_log_untrustedstring(ab, comm); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1003 | audit_log_end(ab); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1004 | |
| 1005 | return rc; |
| 1006 | } |
| 1007 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1008 | /* |
| 1009 | * to_send and len_sent accounting are very loose estimates. We aren't |
| 1010 | * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being |
| 1011 | * within about 500 bytes (next page boundry) |
| 1012 | * |
| 1013 | * why snprintf? an int is up to 12 digits long. if we just assumed when |
| 1014 | * logging that a[%d]= was going to be 16 characters long we would be wasting |
| 1015 | * space in every audit message. In one 7500 byte message we can log up to |
| 1016 | * about 1000 min size arguments. That comes down to about 50% waste of space |
| 1017 | * if we didn't do the snprintf to find out how long arg_num_len was. |
| 1018 | */ |
| 1019 | static int audit_log_single_execve_arg(struct audit_context *context, |
| 1020 | struct audit_buffer **ab, |
| 1021 | int arg_num, |
| 1022 | size_t *len_sent, |
| 1023 | const char __user *p, |
| 1024 | char *buf) |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1025 | { |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1026 | char arg_num_len_buf[12]; |
| 1027 | const char __user *tmp_p = p; |
| 1028 | /* how many digits are in arg_num? 3 is the length of a=\n */ |
| 1029 | size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3; |
| 1030 | size_t len, len_left, to_send; |
| 1031 | size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN; |
| 1032 | unsigned int i, has_cntl = 0, too_long = 0; |
| 1033 | int ret; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1034 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1035 | /* strnlen_user includes the null we don't want to send */ |
| 1036 | len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1037 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1038 | /* |
| 1039 | * We just created this mm, if we can't find the strings |
| 1040 | * we just copied into it something is _very_ wrong. Similar |
| 1041 | * for strings that are too long, we should not have created |
| 1042 | * any. |
| 1043 | */ |
Eric Paris | b0abcfc | 2008-02-18 18:23:16 -0500 | [diff] [blame] | 1044 | if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) { |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1045 | WARN_ON(1); |
| 1046 | send_sig(SIGKILL, current, 0); |
Eric Paris | b0abcfc | 2008-02-18 18:23:16 -0500 | [diff] [blame] | 1047 | return -1; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1048 | } |
Peter Zijlstra | 040b3a2 | 2007-07-28 00:55:18 +0200 | [diff] [blame] | 1049 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1050 | /* walk the whole argument looking for non-ascii chars */ |
| 1051 | do { |
| 1052 | if (len_left > MAX_EXECVE_AUDIT_LEN) |
| 1053 | to_send = MAX_EXECVE_AUDIT_LEN; |
| 1054 | else |
| 1055 | to_send = len_left; |
| 1056 | ret = copy_from_user(buf, tmp_p, to_send); |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1057 | /* |
| 1058 | * There is no reason for this copy to be short. We just |
| 1059 | * copied them here, and the mm hasn't been exposed to user- |
| 1060 | * space yet. |
| 1061 | */ |
Peter Zijlstra | 040b3a2 | 2007-07-28 00:55:18 +0200 | [diff] [blame] | 1062 | if (ret) { |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1063 | WARN_ON(1); |
| 1064 | send_sig(SIGKILL, current, 0); |
Eric Paris | b0abcfc | 2008-02-18 18:23:16 -0500 | [diff] [blame] | 1065 | return -1; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1066 | } |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1067 | buf[to_send] = '\0'; |
| 1068 | has_cntl = audit_string_contains_control(buf, to_send); |
| 1069 | if (has_cntl) { |
| 1070 | /* |
| 1071 | * hex messages get logged as 2 bytes, so we can only |
| 1072 | * send half as much in each message |
| 1073 | */ |
| 1074 | max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2; |
| 1075 | break; |
| 1076 | } |
| 1077 | len_left -= to_send; |
| 1078 | tmp_p += to_send; |
| 1079 | } while (len_left > 0); |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1080 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1081 | len_left = len; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1082 | |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1083 | if (len > max_execve_audit_len) |
| 1084 | too_long = 1; |
| 1085 | |
| 1086 | /* rewalk the argument actually logging the message */ |
| 1087 | for (i = 0; len_left > 0; i++) { |
| 1088 | int room_left; |
| 1089 | |
| 1090 | if (len_left > max_execve_audit_len) |
| 1091 | to_send = max_execve_audit_len; |
| 1092 | else |
| 1093 | to_send = len_left; |
| 1094 | |
| 1095 | /* do we have space left to send this argument in this ab? */ |
| 1096 | room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent; |
| 1097 | if (has_cntl) |
| 1098 | room_left -= (to_send * 2); |
| 1099 | else |
| 1100 | room_left -= to_send; |
| 1101 | if (room_left < 0) { |
| 1102 | *len_sent = 0; |
| 1103 | audit_log_end(*ab); |
| 1104 | *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE); |
| 1105 | if (!*ab) |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * first record needs to say how long the original string was |
| 1111 | * so we can be sure nothing was lost. |
| 1112 | */ |
| 1113 | if ((i == 0) && (too_long)) |
Paul Moore | 422b03c | 2008-02-27 10:39:22 -0500 | [diff] [blame] | 1114 | audit_log_format(*ab, "a%d_len=%zu ", arg_num, |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1115 | has_cntl ? 2*len : len); |
| 1116 | |
| 1117 | /* |
| 1118 | * normally arguments are small enough to fit and we already |
| 1119 | * filled buf above when we checked for control characters |
| 1120 | * so don't bother with another copy_from_user |
| 1121 | */ |
| 1122 | if (len >= max_execve_audit_len) |
| 1123 | ret = copy_from_user(buf, p, to_send); |
| 1124 | else |
| 1125 | ret = 0; |
| 1126 | if (ret) { |
| 1127 | WARN_ON(1); |
| 1128 | send_sig(SIGKILL, current, 0); |
Eric Paris | b0abcfc | 2008-02-18 18:23:16 -0500 | [diff] [blame] | 1129 | return -1; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1130 | } |
| 1131 | buf[to_send] = '\0'; |
| 1132 | |
| 1133 | /* actually log it */ |
| 1134 | audit_log_format(*ab, "a%d", arg_num); |
| 1135 | if (too_long) |
| 1136 | audit_log_format(*ab, "[%d]", i); |
| 1137 | audit_log_format(*ab, "="); |
| 1138 | if (has_cntl) |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1139 | audit_log_n_hex(*ab, buf, to_send); |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1140 | else |
| 1141 | audit_log_format(*ab, "\"%s\"", buf); |
| 1142 | audit_log_format(*ab, "\n"); |
| 1143 | |
| 1144 | p += to_send; |
| 1145 | len_left -= to_send; |
| 1146 | *len_sent += arg_num_len; |
| 1147 | if (has_cntl) |
| 1148 | *len_sent += to_send * 2; |
| 1149 | else |
| 1150 | *len_sent += to_send; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1151 | } |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1152 | /* include the null we didn't log */ |
| 1153 | return len + 1; |
| 1154 | } |
| 1155 | |
| 1156 | static void audit_log_execve_info(struct audit_context *context, |
| 1157 | struct audit_buffer **ab, |
| 1158 | struct audit_aux_data_execve *axi) |
| 1159 | { |
| 1160 | int i; |
| 1161 | size_t len, len_sent = 0; |
| 1162 | const char __user *p; |
| 1163 | char *buf; |
| 1164 | |
| 1165 | if (axi->mm != current->mm) |
| 1166 | return; /* execve failed, no additional info */ |
| 1167 | |
| 1168 | p = (const char __user *)axi->mm->arg_start; |
| 1169 | |
| 1170 | audit_log_format(*ab, "argc=%d ", axi->argc); |
| 1171 | |
| 1172 | /* |
| 1173 | * we need some kernel buffer to hold the userspace args. Just |
| 1174 | * allocate one big one rather than allocating one of the right size |
| 1175 | * for every single argument inside audit_log_single_execve_arg() |
| 1176 | * should be <8k allocation so should be pretty safe. |
| 1177 | */ |
| 1178 | buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL); |
| 1179 | if (!buf) { |
| 1180 | audit_panic("out of memory for argv string\n"); |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | for (i = 0; i < axi->argc; i++) { |
| 1185 | len = audit_log_single_execve_arg(context, ab, i, |
| 1186 | &len_sent, p, buf); |
| 1187 | if (len <= 0) |
| 1188 | break; |
| 1189 | p += len; |
| 1190 | } |
| 1191 | kfree(buf); |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 1194 | static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap) |
| 1195 | { |
| 1196 | int i; |
| 1197 | |
| 1198 | audit_log_format(ab, " %s=", prefix); |
| 1199 | CAP_FOR_EACH_U32(i) { |
| 1200 | audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name) |
| 1205 | { |
| 1206 | kernel_cap_t *perm = &name->fcap.permitted; |
| 1207 | kernel_cap_t *inh = &name->fcap.inheritable; |
| 1208 | int log = 0; |
| 1209 | |
| 1210 | if (!cap_isclear(*perm)) { |
| 1211 | audit_log_cap(ab, "cap_fp", perm); |
| 1212 | log = 1; |
| 1213 | } |
| 1214 | if (!cap_isclear(*inh)) { |
| 1215 | audit_log_cap(ab, "cap_fi", inh); |
| 1216 | log = 1; |
| 1217 | } |
| 1218 | |
| 1219 | if (log) |
| 1220 | audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver); |
| 1221 | } |
| 1222 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1223 | 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] | 1224 | { |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1225 | int i, call_panic = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | struct audit_buffer *ab; |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 1227 | struct audit_aux_data *aux; |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 1228 | const char *tty; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1230 | /* tsk == current */ |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 1231 | context->pid = tsk->pid; |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 1232 | if (!context->ppid) |
| 1233 | context->ppid = sys_getppid(); |
Al Viro | 3f2792f | 2006-07-16 06:43:48 -0400 | [diff] [blame] | 1234 | context->uid = tsk->uid; |
| 1235 | context->gid = tsk->gid; |
| 1236 | context->euid = tsk->euid; |
| 1237 | context->suid = tsk->suid; |
| 1238 | context->fsuid = tsk->fsuid; |
| 1239 | context->egid = tsk->egid; |
| 1240 | context->sgid = tsk->sgid; |
| 1241 | context->fsgid = tsk->fsgid; |
| 1242 | context->personality = tsk->personality; |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1243 | |
| 1244 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | if (!ab) |
| 1246 | return; /* audit_panic has been called */ |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 1247 | audit_log_format(ab, "arch=%x syscall=%d", |
| 1248 | context->arch, context->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | if (context->personality != PER_LINUX) |
| 1250 | audit_log_format(ab, " per=%lx", context->personality); |
| 1251 | if (context->return_valid) |
Daniel Walker | 9f8dbe9 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 1252 | audit_log_format(ab, " success=%s exit=%ld", |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1253 | (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", |
| 1254 | context->return_code); |
Alan Cox | eb84a20 | 2006-09-29 02:01:41 -0700 | [diff] [blame] | 1255 | |
Alan Cox | dbda4c0 | 2008-10-13 10:40:53 +0100 | [diff] [blame] | 1256 | spin_lock_irq(&tsk->sighand->siglock); |
Al Viro | 45d9bb0 | 2006-03-29 20:02:55 -0500 | [diff] [blame] | 1257 | if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name) |
| 1258 | tty = tsk->signal->tty->name; |
Steve Grubb | a6c043a | 2006-01-01 14:07:00 -0500 | [diff] [blame] | 1259 | else |
| 1260 | tty = "(none)"; |
Alan Cox | dbda4c0 | 2008-10-13 10:40:53 +0100 | [diff] [blame] | 1261 | spin_unlock_irq(&tsk->sighand->siglock); |
| 1262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | audit_log_format(ab, |
| 1264 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 1265 | " ppid=%d pid=%d auid=%u uid=%u gid=%u" |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 1266 | " euid=%u suid=%u fsuid=%u" |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 1267 | " egid=%u sgid=%u fsgid=%u tty=%s ses=%u", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | context->argv[0], |
| 1269 | context->argv[1], |
| 1270 | context->argv[2], |
| 1271 | context->argv[3], |
| 1272 | context->name_count, |
Al Viro | f46038f | 2006-05-06 08:22:52 -0400 | [diff] [blame] | 1273 | context->ppid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | context->pid, |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 1275 | tsk->loginuid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | context->uid, |
| 1277 | context->gid, |
| 1278 | context->euid, context->suid, context->fsuid, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 1279 | context->egid, context->sgid, context->fsgid, tty, |
| 1280 | tsk->sessionid); |
Alan Cox | eb84a20 | 2006-09-29 02:01:41 -0700 | [diff] [blame] | 1281 | |
Alan Cox | eb84a20 | 2006-09-29 02:01:41 -0700 | [diff] [blame] | 1282 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1283 | audit_log_task_info(ab, tsk); |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 1284 | if (context->filterkey) { |
| 1285 | audit_log_format(ab, " key="); |
| 1286 | audit_log_untrustedstring(ab, context->filterkey); |
| 1287 | } else |
| 1288 | audit_log_format(ab, " key=(null)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 1291 | for (aux = context->aux; aux; aux = aux->next) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1292 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1293 | ab = audit_log_start(context, GFP_KERNEL, aux->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | if (!ab) |
| 1295 | continue; /* audit_panic has been called */ |
| 1296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | switch (aux->type) { |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 1298 | case AUDIT_MQ_OPEN: { |
| 1299 | struct audit_aux_data_mq_open *axi = (void *)aux; |
| 1300 | audit_log_format(ab, |
| 1301 | "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld " |
| 1302 | "mq_msgsize=%ld mq_curmsgs=%ld", |
| 1303 | axi->oflag, axi->mode, axi->attr.mq_flags, |
| 1304 | axi->attr.mq_maxmsg, axi->attr.mq_msgsize, |
| 1305 | axi->attr.mq_curmsgs); |
| 1306 | break; } |
| 1307 | |
| 1308 | case AUDIT_MQ_SENDRECV: { |
| 1309 | struct audit_aux_data_mq_sendrecv *axi = (void *)aux; |
| 1310 | audit_log_format(ab, |
| 1311 | "mqdes=%d msg_len=%zd msg_prio=%u " |
| 1312 | "abs_timeout_sec=%ld abs_timeout_nsec=%ld", |
| 1313 | axi->mqdes, axi->msg_len, axi->msg_prio, |
| 1314 | axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec); |
| 1315 | break; } |
| 1316 | |
| 1317 | case AUDIT_MQ_NOTIFY: { |
| 1318 | struct audit_aux_data_mq_notify *axi = (void *)aux; |
| 1319 | audit_log_format(ab, |
| 1320 | "mqdes=%d sigev_signo=%d", |
| 1321 | axi->mqdes, |
| 1322 | axi->notification.sigev_signo); |
| 1323 | break; } |
| 1324 | |
| 1325 | case AUDIT_MQ_GETSETATTR: { |
| 1326 | struct audit_aux_data_mq_getsetattr *axi = (void *)aux; |
| 1327 | audit_log_format(ab, |
| 1328 | "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld " |
| 1329 | "mq_curmsgs=%ld ", |
| 1330 | axi->mqdes, |
| 1331 | axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg, |
| 1332 | axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs); |
| 1333 | break; } |
| 1334 | |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1335 | case AUDIT_IPC: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 1337 | audit_log_format(ab, |
Steve Grubb | 5b9a426 | 2007-05-29 10:38:18 -0400 | [diff] [blame] | 1338 | "ouid=%u ogid=%u mode=%#o", |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 1339 | axi->uid, axi->gid, axi->mode); |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1340 | if (axi->osid != 0) { |
| 1341 | char *ctx = NULL; |
| 1342 | u32 len; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 1343 | if (security_secid_to_secctx( |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1344 | axi->osid, &ctx, &len)) { |
Steve Grubb | ce29b68 | 2006-04-01 18:29:34 -0500 | [diff] [blame] | 1345 | audit_log_format(ab, " osid=%u", |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1346 | axi->osid); |
| 1347 | call_panic = 1; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 1348 | } else { |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1349 | audit_log_format(ab, " obj=%s", ctx); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 1350 | security_release_secctx(ctx, len); |
| 1351 | } |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1352 | } |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1353 | break; } |
| 1354 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1355 | case AUDIT_IPC_SET_PERM: { |
| 1356 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 1357 | audit_log_format(ab, |
Steve Grubb | 5b9a426 | 2007-05-29 10:38:18 -0400 | [diff] [blame] | 1358 | "qbytes=%lx ouid=%u ogid=%u mode=%#o", |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1359 | axi->qbytes, axi->uid, axi->gid, axi->mode); |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1360 | break; } |
Linda Knippers | ac03221 | 2006-05-16 22:03:48 -0400 | [diff] [blame] | 1361 | |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1362 | case AUDIT_EXECVE: { |
| 1363 | struct audit_aux_data_execve *axi = (void *)aux; |
Eric Paris | de6bbd1 | 2008-01-07 14:31:58 -0500 | [diff] [blame] | 1364 | audit_log_execve_info(context, &ab, axi); |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 1365 | break; } |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 1366 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1367 | case AUDIT_SOCKETCALL: { |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1368 | struct audit_aux_data_socketcall *axs = (void *)aux; |
| 1369 | audit_log_format(ab, "nargs=%d", axs->nargs); |
| 1370 | for (i=0; i<axs->nargs; i++) |
| 1371 | audit_log_format(ab, " a%d=%lx", i, axs->args[i]); |
| 1372 | break; } |
| 1373 | |
| 1374 | case AUDIT_SOCKADDR: { |
| 1375 | struct audit_aux_data_sockaddr *axs = (void *)aux; |
| 1376 | |
| 1377 | audit_log_format(ab, "saddr="); |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1378 | audit_log_n_hex(ab, axs->a, axs->len); |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1379 | break; } |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1380 | |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 1381 | case AUDIT_FD_PAIR: { |
| 1382 | struct audit_aux_data_fd_pair *axs = (void *)aux; |
| 1383 | audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]); |
| 1384 | break; } |
| 1385 | |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame^] | 1386 | case AUDIT_BPRM_FCAPS: { |
| 1387 | struct audit_aux_data_bprm_fcaps *axs = (void *)aux; |
| 1388 | audit_log_format(ab, "fver=%x", axs->fcap_ver); |
| 1389 | audit_log_cap(ab, "fp", &axs->fcap.permitted); |
| 1390 | audit_log_cap(ab, "fi", &axs->fcap.inheritable); |
| 1391 | audit_log_format(ab, " fe=%d", axs->fcap.fE); |
| 1392 | audit_log_cap(ab, "old_pp", &axs->old_pcap.permitted); |
| 1393 | audit_log_cap(ab, "old_pi", &axs->old_pcap.inheritable); |
| 1394 | audit_log_cap(ab, "old_pe", &axs->old_pcap.effective); |
| 1395 | audit_log_cap(ab, "new_pp", &axs->new_pcap.permitted); |
| 1396 | audit_log_cap(ab, "new_pi", &axs->new_pcap.inheritable); |
| 1397 | audit_log_cap(ab, "new_pe", &axs->new_pcap.effective); |
| 1398 | break; } |
| 1399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | } |
| 1401 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1404 | for (aux = context->aux_pids; aux; aux = aux->next) { |
| 1405 | struct audit_aux_data_pids *axs = (void *)aux; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1406 | |
| 1407 | for (i = 0; i < axs->pid_count; i++) |
| 1408 | if (audit_log_pid_context(context, axs->target_pid[i], |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 1409 | axs->target_auid[i], |
| 1410 | axs->target_uid[i], |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 1411 | axs->target_sessionid[i], |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 1412 | axs->target_sid[i], |
| 1413 | axs->target_comm[i])) |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1414 | call_panic = 1; |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 1415 | } |
| 1416 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1417 | if (context->target_pid && |
| 1418 | audit_log_pid_context(context, context->target_pid, |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 1419 | context->target_auid, context->target_uid, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 1420 | context->target_sessionid, |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 1421 | context->target_sid, context->target_comm)) |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1422 | call_panic = 1; |
| 1423 | |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1424 | if (context->pwd.dentry && context->pwd.mnt) { |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1425 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD); |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1426 | if (ab) { |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1427 | audit_log_d_path(ab, "cwd=", &context->pwd); |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1428 | audit_log_end(ab); |
| 1429 | } |
| 1430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | for (i = 0; i < context->name_count; i++) { |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1432 | struct audit_names *n = &context->names[i]; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1433 | |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1434 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | if (!ab) |
| 1436 | continue; /* audit_panic has been called */ |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | audit_log_format(ab, "item=%d", i); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1439 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1440 | if (n->name) { |
| 1441 | switch(n->name_len) { |
| 1442 | case AUDIT_NAME_FULL: |
| 1443 | /* log the full path */ |
| 1444 | audit_log_format(ab, " name="); |
| 1445 | audit_log_untrustedstring(ab, n->name); |
| 1446 | break; |
| 1447 | case 0: |
| 1448 | /* name was specified as a relative path and the |
| 1449 | * directory component is the cwd */ |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1450 | audit_log_d_path(ab, " name=", &context->pwd); |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1451 | break; |
| 1452 | default: |
| 1453 | /* log the name's directory component */ |
| 1454 | audit_log_format(ab, " name="); |
Eric Paris | b556f8a | 2008-04-18 10:12:59 -0400 | [diff] [blame] | 1455 | audit_log_n_untrustedstring(ab, n->name, |
| 1456 | n->name_len); |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1457 | } |
| 1458 | } else |
| 1459 | audit_log_format(ab, " name=(null)"); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1460 | |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1461 | if (n->ino != (unsigned long)-1) { |
| 1462 | audit_log_format(ab, " inode=%lu" |
| 1463 | " dev=%02x:%02x mode=%#o" |
| 1464 | " ouid=%u ogid=%u rdev=%02x:%02x", |
| 1465 | n->ino, |
| 1466 | MAJOR(n->dev), |
| 1467 | MINOR(n->dev), |
| 1468 | n->mode, |
| 1469 | n->uid, |
| 1470 | n->gid, |
| 1471 | MAJOR(n->rdev), |
| 1472 | MINOR(n->rdev)); |
| 1473 | } |
| 1474 | if (n->osid != 0) { |
Steve Grubb | 1b50eed | 2006-04-03 14:06:13 -0400 | [diff] [blame] | 1475 | char *ctx = NULL; |
| 1476 | u32 len; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 1477 | if (security_secid_to_secctx( |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1478 | n->osid, &ctx, &len)) { |
| 1479 | audit_log_format(ab, " osid=%u", n->osid); |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1480 | call_panic = 2; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 1481 | } else { |
Steve Grubb | 1b50eed | 2006-04-03 14:06:13 -0400 | [diff] [blame] | 1482 | audit_log_format(ab, " obj=%s", ctx); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 1483 | security_release_secctx(ctx, len); |
| 1484 | } |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 1487 | audit_log_fcaps(ab, n); |
| 1488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | audit_log_end(ab); |
| 1490 | } |
Eric Paris | c0641f2 | 2008-01-07 13:49:15 -0500 | [diff] [blame] | 1491 | |
| 1492 | /* Send end of event record to help user space know we are finished */ |
| 1493 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE); |
| 1494 | if (ab) |
| 1495 | audit_log_end(ab); |
Steve Grubb | 9c7aa6a | 2006-03-31 15:22:49 -0500 | [diff] [blame] | 1496 | if (call_panic) |
| 1497 | audit_panic("error converting sid to string"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1500 | /** |
| 1501 | * audit_free - free a per-task audit context |
| 1502 | * @tsk: task whose audit context block to free |
| 1503 | * |
Al Viro | fa84cb9 | 2006-03-29 20:30:19 -0500 | [diff] [blame] | 1504 | * Called from copy_process and do_exit |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1505 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | void audit_free(struct task_struct *tsk) |
| 1507 | { |
| 1508 | struct audit_context *context; |
| 1509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | context = audit_get_context(tsk, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | if (likely(!context)) |
| 1512 | return; |
| 1513 | |
| 1514 | /* Check for system calls that do not go through the exit |
Daniel Walker | 9f8dbe9 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 1515 | * function (e.g., exit_group), then free context block. |
| 1516 | * We use GFP_ATOMIC here because we might be doing this |
David Woodhouse | f556196 | 2005-07-13 22:47:07 +0100 | [diff] [blame] | 1517 | * in the context of the idle thread */ |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1518 | /* that can happen only if we are called from do_exit() */ |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 1519 | if (context->in_syscall && context->auditable) |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1520 | audit_log_exit(context, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
| 1522 | audit_free_context(context); |
| 1523 | } |
| 1524 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1525 | /** |
| 1526 | * audit_syscall_entry - fill in an audit record at syscall entry |
| 1527 | * @tsk: task being audited |
| 1528 | * @arch: architecture type |
| 1529 | * @major: major syscall type (function) |
| 1530 | * @a1: additional syscall register 1 |
| 1531 | * @a2: additional syscall register 2 |
| 1532 | * @a3: additional syscall register 3 |
| 1533 | * @a4: additional syscall register 4 |
| 1534 | * |
| 1535 | * Fill in audit context at syscall entry. This only happens if the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | * audit context was created when the task was created and the state or |
| 1537 | * filters demand the audit context be built. If the state from the |
| 1538 | * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT, |
| 1539 | * then the record will be written at syscall exit time (otherwise, it |
| 1540 | * 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] | 1541 | * be written). |
| 1542 | */ |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1543 | void audit_syscall_entry(int arch, int major, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | unsigned long a1, unsigned long a2, |
| 1545 | unsigned long a3, unsigned long a4) |
| 1546 | { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1547 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | struct audit_context *context = tsk->audit_context; |
| 1549 | enum audit_state state; |
| 1550 | |
Roland McGrath | 86a1c34 | 2008-06-23 15:37:04 -0700 | [diff] [blame] | 1551 | if (unlikely(!context)) |
| 1552 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1554 | /* |
| 1555 | * This happens only on certain architectures that make system |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | * calls in kernel_thread via the entry.S interface, instead of |
| 1557 | * with direct calls. (If you are porting to a new |
| 1558 | * architecture, hitting this condition can indicate that you |
| 1559 | * got the _exit/_leave calls backward in entry.S.) |
| 1560 | * |
| 1561 | * i386 no |
| 1562 | * x86_64 no |
Jon Mason | 2ef9481 | 2006-01-23 10:58:20 -0600 | [diff] [blame] | 1563 | * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | * |
| 1565 | * This also happens with vm86 emulation in a non-nested manner |
| 1566 | * (entries without exits), so this case must be caught. |
| 1567 | */ |
| 1568 | if (context->in_syscall) { |
| 1569 | struct audit_context *newctx; |
| 1570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | #if AUDIT_DEBUG |
| 1572 | printk(KERN_ERR |
| 1573 | "audit(:%d) pid=%d in syscall=%d;" |
| 1574 | " entering syscall=%d\n", |
| 1575 | context->serial, tsk->pid, context->major, major); |
| 1576 | #endif |
| 1577 | newctx = audit_alloc_context(context->state); |
| 1578 | if (newctx) { |
| 1579 | newctx->previous = context; |
| 1580 | context = newctx; |
| 1581 | tsk->audit_context = newctx; |
| 1582 | } else { |
| 1583 | /* If we can't alloc a new context, the best we |
| 1584 | * can do is to leak memory (any pending putname |
| 1585 | * will be lost). The only other alternative is |
| 1586 | * to abandon auditing. */ |
| 1587 | audit_zero_context(context, context->state); |
| 1588 | } |
| 1589 | } |
| 1590 | BUG_ON(context->in_syscall || context->name_count); |
| 1591 | |
| 1592 | if (!audit_enabled) |
| 1593 | return; |
| 1594 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1595 | context->arch = arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | context->major = major; |
| 1597 | context->argv[0] = a1; |
| 1598 | context->argv[1] = a2; |
| 1599 | context->argv[2] = a3; |
| 1600 | context->argv[3] = a4; |
| 1601 | |
| 1602 | state = context->state; |
Al Viro | d51374a | 2006-08-03 10:59:26 -0400 | [diff] [blame] | 1603 | context->dummy = !audit_n_rules; |
| 1604 | if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)) |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame] | 1605 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | if (likely(state == AUDIT_DISABLED)) |
| 1607 | return; |
| 1608 | |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 1609 | context->serial = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | context->ctime = CURRENT_TIME; |
| 1611 | context->in_syscall = 1; |
| 1612 | context->auditable = !!(state == AUDIT_RECORD_CONTEXT); |
Alexander Viro | 419c58f | 2006-09-29 00:08:50 -0400 | [diff] [blame] | 1613 | context->ppid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1616 | /** |
| 1617 | * audit_syscall_exit - deallocate audit context after a system call |
| 1618 | * @tsk: task being audited |
| 1619 | * @valid: success/failure flag |
| 1620 | * @return_code: syscall return value |
| 1621 | * |
| 1622 | * 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] | 1623 | * auditable (either because of the AUDIT_RECORD_CONTEXT state from |
| 1624 | * filtering, or because some other part of the kernel write an audit |
| 1625 | * message), then write out the syscall information. In call cases, |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1626 | * free the names stored from getname(). |
| 1627 | */ |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1628 | void audit_syscall_exit(int valid, long return_code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | { |
Al Viro | 5411be5 | 2006-03-29 20:23:36 -0500 | [diff] [blame] | 1630 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | struct audit_context *context; |
| 1632 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1633 | context = audit_get_context(tsk, valid, return_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | if (likely(!context)) |
Al Viro | 97e94c4 | 2006-03-29 20:26:24 -0500 | [diff] [blame] | 1636 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | |
David Woodhouse | f7056d6 | 2005-06-20 16:07:33 +0100 | [diff] [blame] | 1638 | if (context->in_syscall && context->auditable) |
Al Viro | e495149 | 2006-03-29 20:17:10 -0500 | [diff] [blame] | 1639 | audit_log_exit(context, tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | |
| 1641 | context->in_syscall = 0; |
| 1642 | context->auditable = 0; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 1643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | if (context->previous) { |
| 1645 | struct audit_context *new_context = context->previous; |
| 1646 | context->previous = NULL; |
| 1647 | audit_free_context(context); |
| 1648 | tsk->audit_context = new_context; |
| 1649 | } else { |
| 1650 | audit_free_names(context); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1651 | unroll_tree_refs(context, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | audit_free_aux(context); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1653 | context->aux = NULL; |
| 1654 | context->aux_pids = NULL; |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 1655 | context->target_pid = 0; |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 1656 | context->target_sid = 0; |
Amy Griffis | 5adc8a6 | 2006-06-14 18:45:21 -0400 | [diff] [blame] | 1657 | kfree(context->filterkey); |
| 1658 | context->filterkey = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | tsk->audit_context = context; |
| 1660 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1663 | static inline void handle_one(const struct inode *inode) |
| 1664 | { |
| 1665 | #ifdef CONFIG_AUDIT_TREE |
| 1666 | struct audit_context *context; |
| 1667 | struct audit_tree_refs *p; |
| 1668 | struct audit_chunk *chunk; |
| 1669 | int count; |
| 1670 | if (likely(list_empty(&inode->inotify_watches))) |
| 1671 | return; |
| 1672 | context = current->audit_context; |
| 1673 | p = context->trees; |
| 1674 | count = context->tree_count; |
| 1675 | rcu_read_lock(); |
| 1676 | chunk = audit_tree_lookup(inode); |
| 1677 | rcu_read_unlock(); |
| 1678 | if (!chunk) |
| 1679 | return; |
| 1680 | if (likely(put_tree_ref(context, chunk))) |
| 1681 | return; |
| 1682 | if (unlikely(!grow_tree_refs(context))) { |
Eric Paris | 436c405 | 2008-04-18 10:01:04 -0400 | [diff] [blame] | 1683 | printk(KERN_WARNING "out of memory, audit has lost a tree reference\n"); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1684 | audit_set_auditable(context); |
| 1685 | audit_put_chunk(chunk); |
| 1686 | unroll_tree_refs(context, p, count); |
| 1687 | return; |
| 1688 | } |
| 1689 | put_tree_ref(context, chunk); |
| 1690 | #endif |
| 1691 | } |
| 1692 | |
| 1693 | static void handle_path(const struct dentry *dentry) |
| 1694 | { |
| 1695 | #ifdef CONFIG_AUDIT_TREE |
| 1696 | struct audit_context *context; |
| 1697 | struct audit_tree_refs *p; |
| 1698 | const struct dentry *d, *parent; |
| 1699 | struct audit_chunk *drop; |
| 1700 | unsigned long seq; |
| 1701 | int count; |
| 1702 | |
| 1703 | context = current->audit_context; |
| 1704 | p = context->trees; |
| 1705 | count = context->tree_count; |
| 1706 | retry: |
| 1707 | drop = NULL; |
| 1708 | d = dentry; |
| 1709 | rcu_read_lock(); |
| 1710 | seq = read_seqbegin(&rename_lock); |
| 1711 | for(;;) { |
| 1712 | struct inode *inode = d->d_inode; |
| 1713 | if (inode && unlikely(!list_empty(&inode->inotify_watches))) { |
| 1714 | struct audit_chunk *chunk; |
| 1715 | chunk = audit_tree_lookup(inode); |
| 1716 | if (chunk) { |
| 1717 | if (unlikely(!put_tree_ref(context, chunk))) { |
| 1718 | drop = chunk; |
| 1719 | break; |
| 1720 | } |
| 1721 | } |
| 1722 | } |
| 1723 | parent = d->d_parent; |
| 1724 | if (parent == d) |
| 1725 | break; |
| 1726 | d = parent; |
| 1727 | } |
| 1728 | if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */ |
| 1729 | rcu_read_unlock(); |
| 1730 | if (!drop) { |
| 1731 | /* just a race with rename */ |
| 1732 | unroll_tree_refs(context, p, count); |
| 1733 | goto retry; |
| 1734 | } |
| 1735 | audit_put_chunk(drop); |
| 1736 | if (grow_tree_refs(context)) { |
| 1737 | /* OK, got more space */ |
| 1738 | unroll_tree_refs(context, p, count); |
| 1739 | goto retry; |
| 1740 | } |
| 1741 | /* too bad */ |
| 1742 | printk(KERN_WARNING |
Eric Paris | 436c405 | 2008-04-18 10:01:04 -0400 | [diff] [blame] | 1743 | "out of memory, audit has lost a tree reference\n"); |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1744 | unroll_tree_refs(context, p, count); |
| 1745 | audit_set_auditable(context); |
| 1746 | return; |
| 1747 | } |
| 1748 | rcu_read_unlock(); |
| 1749 | #endif |
| 1750 | } |
| 1751 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1752 | /** |
| 1753 | * audit_getname - add a name to the list |
| 1754 | * @name: name to add |
| 1755 | * |
| 1756 | * Add a name to the list of audit names for this context. |
| 1757 | * Called from fs/namei.c:getname(). |
| 1758 | */ |
Al Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1759 | void __audit_getname(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | { |
| 1761 | struct audit_context *context = current->audit_context; |
| 1762 | |
Al Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 1763 | if (IS_ERR(name) || !name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | return; |
| 1765 | |
| 1766 | if (!context->in_syscall) { |
| 1767 | #if AUDIT_DEBUG == 2 |
| 1768 | printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n", |
| 1769 | __FILE__, __LINE__, context->serial, name); |
| 1770 | dump_stack(); |
| 1771 | #endif |
| 1772 | return; |
| 1773 | } |
| 1774 | BUG_ON(context->name_count >= AUDIT_NAMES); |
| 1775 | context->names[context->name_count].name = name; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1776 | context->names[context->name_count].name_len = AUDIT_NAME_FULL; |
| 1777 | context->names[context->name_count].name_put = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | context->names[context->name_count].ino = (unsigned long)-1; |
Amy Griffis | e41e8bd | 2007-02-13 14:14:09 -0500 | [diff] [blame] | 1779 | context->names[context->name_count].osid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | ++context->name_count; |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1781 | if (!context->pwd.dentry) { |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1782 | read_lock(¤t->fs->lock); |
Jan Blunck | 44707fd | 2008-02-14 19:38:33 -0800 | [diff] [blame] | 1783 | context->pwd = current->fs->pwd; |
| 1784 | path_get(¤t->fs->pwd); |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 1785 | read_unlock(¤t->fs->lock); |
| 1786 | } |
Daniel Walker | 9f8dbe9 | 2007-10-18 03:06:09 -0700 | [diff] [blame] | 1787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | } |
| 1789 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1790 | /* audit_putname - intercept a putname request |
| 1791 | * @name: name to intercept and delay for putname |
| 1792 | * |
| 1793 | * If we have stored the name from getname in the audit context, |
| 1794 | * then we delay the putname until syscall exit. |
| 1795 | * Called from include/linux/fs.h:putname(). |
| 1796 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | void audit_putname(const char *name) |
| 1798 | { |
| 1799 | struct audit_context *context = current->audit_context; |
| 1800 | |
| 1801 | BUG_ON(!context); |
| 1802 | if (!context->in_syscall) { |
| 1803 | #if AUDIT_DEBUG == 2 |
| 1804 | printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n", |
| 1805 | __FILE__, __LINE__, context->serial, name); |
| 1806 | if (context->name_count) { |
| 1807 | int i; |
| 1808 | for (i = 0; i < context->name_count; i++) |
| 1809 | printk(KERN_ERR "name[%d] = %p = %s\n", i, |
| 1810 | context->names[i].name, |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1811 | context->names[i].name ?: "(null)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
| 1813 | #endif |
| 1814 | __putname(name); |
| 1815 | } |
| 1816 | #if AUDIT_DEBUG |
| 1817 | else { |
| 1818 | ++context->put_count; |
| 1819 | if (context->put_count > context->name_count) { |
| 1820 | printk(KERN_ERR "%s:%d(:%d): major=%d" |
| 1821 | " in_syscall=%d putname(%p) name_count=%d" |
| 1822 | " put_count=%d\n", |
| 1823 | __FILE__, __LINE__, |
| 1824 | context->serial, context->major, |
| 1825 | context->in_syscall, name, context->name_count, |
| 1826 | context->put_count); |
| 1827 | dump_stack(); |
| 1828 | } |
| 1829 | } |
| 1830 | #endif |
| 1831 | } |
| 1832 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1833 | static int audit_inc_name_count(struct audit_context *context, |
| 1834 | const struct inode *inode) |
| 1835 | { |
| 1836 | if (context->name_count >= AUDIT_NAMES) { |
| 1837 | if (inode) |
| 1838 | printk(KERN_DEBUG "name_count maxed, losing inode data: " |
Eric Paris | 436c405 | 2008-04-18 10:01:04 -0400 | [diff] [blame] | 1839 | "dev=%02x:%02x, inode=%lu\n", |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1840 | MAJOR(inode->i_sb->s_dev), |
| 1841 | MINOR(inode->i_sb->s_dev), |
| 1842 | inode->i_ino); |
| 1843 | |
| 1844 | else |
Eric Paris | 436c405 | 2008-04-18 10:01:04 -0400 | [diff] [blame] | 1845 | printk(KERN_DEBUG "name_count maxed, losing inode data\n"); |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1846 | return 1; |
| 1847 | } |
| 1848 | context->name_count++; |
| 1849 | #if AUDIT_DEBUG |
| 1850 | context->ino_count++; |
| 1851 | #endif |
| 1852 | return 0; |
| 1853 | } |
| 1854 | |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 1855 | |
| 1856 | static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry) |
| 1857 | { |
| 1858 | struct cpu_vfs_cap_data caps; |
| 1859 | int rc; |
| 1860 | |
| 1861 | memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t)); |
| 1862 | memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t)); |
| 1863 | name->fcap.fE = 0; |
| 1864 | name->fcap_ver = 0; |
| 1865 | |
| 1866 | if (!dentry) |
| 1867 | return 0; |
| 1868 | |
| 1869 | rc = get_vfs_caps_from_disk(dentry, &caps); |
| 1870 | if (rc) |
| 1871 | return rc; |
| 1872 | |
| 1873 | name->fcap.permitted = caps.permitted; |
| 1874 | name->fcap.inheritable = caps.inheritable; |
| 1875 | name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE); |
| 1876 | name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT; |
| 1877 | |
| 1878 | return 0; |
| 1879 | } |
| 1880 | |
| 1881 | |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1882 | /* Copy inode data into an audit_names. */ |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 1883 | static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry, |
| 1884 | const struct inode *inode) |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1885 | { |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 1886 | name->ino = inode->i_ino; |
| 1887 | name->dev = inode->i_sb->s_dev; |
| 1888 | name->mode = inode->i_mode; |
| 1889 | name->uid = inode->i_uid; |
| 1890 | name->gid = inode->i_gid; |
| 1891 | name->rdev = inode->i_rdev; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 1892 | security_inode_getsecid(inode, &name->osid); |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 1893 | audit_copy_fcaps(name, dentry); |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1896 | /** |
| 1897 | * audit_inode - store the inode and device from a lookup |
| 1898 | * @name: name being audited |
Randy Dunlap | 481968f | 2007-10-21 20:59:53 -0700 | [diff] [blame] | 1899 | * @dentry: dentry being audited |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 1900 | * |
| 1901 | * Called from fs/namei.c:path_lookup(). |
| 1902 | */ |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 1903 | void __audit_inode(const char *name, const struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | { |
| 1905 | int idx; |
| 1906 | struct audit_context *context = current->audit_context; |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1907 | const struct inode *inode = dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | |
| 1909 | if (!context->in_syscall) |
| 1910 | return; |
| 1911 | if (context->name_count |
| 1912 | && context->names[context->name_count-1].name |
| 1913 | && context->names[context->name_count-1].name == name) |
| 1914 | idx = context->name_count - 1; |
| 1915 | else if (context->name_count > 1 |
| 1916 | && context->names[context->name_count-2].name |
| 1917 | && context->names[context->name_count-2].name == name) |
| 1918 | idx = context->name_count - 2; |
| 1919 | else { |
| 1920 | /* FIXME: how much do we care about inodes that have no |
| 1921 | * associated name? */ |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1922 | if (audit_inc_name_count(context, inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | return; |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1924 | idx = context->name_count - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | context->names[idx].name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | } |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1927 | handle_path(dentry); |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 1928 | audit_copy_inode(&context->names[idx], dentry, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1931 | /** |
| 1932 | * audit_inode_child - collect inode info for created/removed objects |
| 1933 | * @dname: inode's dentry name |
Randy Dunlap | 481968f | 2007-10-21 20:59:53 -0700 | [diff] [blame] | 1934 | * @dentry: dentry being audited |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1935 | * @parent: inode of dentry parent |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1936 | * |
| 1937 | * For syscalls that create or remove filesystem objects, audit_inode |
| 1938 | * can only collect information for the filesystem object's parent. |
| 1939 | * This call updates the audit context with the child's information. |
| 1940 | * Syscalls that create a new filesystem object must be hooked after |
| 1941 | * the object is created. Syscalls that remove a filesystem object |
| 1942 | * must be hooked prior, in order to capture the target inode during |
| 1943 | * unsuccessful attempts. |
| 1944 | */ |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 1945 | void __audit_inode_child(const char *dname, const struct dentry *dentry, |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1946 | const struct inode *parent) |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1947 | { |
| 1948 | int idx; |
| 1949 | struct audit_context *context = current->audit_context; |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1950 | const char *found_parent = NULL, *found_child = NULL; |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 1951 | const struct inode *inode = dentry->d_inode; |
Amy Griffis | 9c937dc | 2006-06-08 23:19:31 -0400 | [diff] [blame] | 1952 | int dirlen = 0; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1953 | |
| 1954 | if (!context->in_syscall) |
| 1955 | return; |
| 1956 | |
Al Viro | 74c3cbe | 2007-07-22 08:04:18 -0400 | [diff] [blame] | 1957 | if (inode) |
| 1958 | handle_one(inode); |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1959 | /* determine matching parent */ |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1960 | if (!dname) |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1961 | goto add_names; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1962 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1963 | /* parent is more likely, look for it first */ |
| 1964 | for (idx = 0; idx < context->name_count; idx++) { |
| 1965 | struct audit_names *n = &context->names[idx]; |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1966 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1967 | if (!n->name) |
| 1968 | continue; |
| 1969 | |
| 1970 | if (n->ino == parent->i_ino && |
| 1971 | !audit_compare_dname_path(dname, n->name, &dirlen)) { |
| 1972 | n->name_len = dirlen; /* update parent data in place */ |
| 1973 | found_parent = n->name; |
| 1974 | goto add_names; |
Amy Griffis | f368c07d | 2006-04-07 16:55:56 -0400 | [diff] [blame] | 1975 | } |
Steve Grubb | ac9910c | 2006-09-28 14:31:32 -0400 | [diff] [blame] | 1976 | } |
Amy Griffis | 73241cc | 2005-11-03 16:00:25 +0000 | [diff] [blame] | 1977 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1978 | /* no matching parent, look for matching child */ |
| 1979 | for (idx = 0; idx < context->name_count; idx++) { |
| 1980 | struct audit_names *n = &context->names[idx]; |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 1981 | |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1982 | if (!n->name) |
| 1983 | continue; |
| 1984 | |
| 1985 | /* strcmp() is the more likely scenario */ |
| 1986 | if (!strcmp(dname, n->name) || |
| 1987 | !audit_compare_dname_path(dname, n->name, &dirlen)) { |
| 1988 | if (inode) |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 1989 | audit_copy_inode(n, NULL, inode); |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1990 | else |
| 1991 | n->ino = (unsigned long)-1; |
| 1992 | found_child = n->name; |
| 1993 | goto add_names; |
Steve Grubb | ac9910c | 2006-09-28 14:31:32 -0400 | [diff] [blame] | 1994 | } |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | add_names: |
| 1998 | if (!found_parent) { |
| 1999 | if (audit_inc_name_count(context, parent)) |
| 2000 | return; |
| 2001 | idx = context->name_count - 1; |
| 2002 | context->names[idx].name = NULL; |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 2003 | audit_copy_inode(&context->names[idx], NULL, parent); |
Amy Griffis | 73d3ec5 | 2006-07-13 13:16:39 -0400 | [diff] [blame] | 2004 | } |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 2005 | |
| 2006 | if (!found_child) { |
| 2007 | if (audit_inc_name_count(context, inode)) |
| 2008 | return; |
| 2009 | idx = context->name_count - 1; |
| 2010 | |
| 2011 | /* Re-use the name belonging to the slot for a matching parent |
| 2012 | * directory. All names for this context are relinquished in |
| 2013 | * audit_free_names() */ |
| 2014 | if (found_parent) { |
| 2015 | context->names[idx].name = found_parent; |
| 2016 | context->names[idx].name_len = AUDIT_NAME_FULL; |
| 2017 | /* don't call __putname() */ |
| 2018 | context->names[idx].name_put = 0; |
| 2019 | } else { |
| 2020 | context->names[idx].name = NULL; |
| 2021 | } |
| 2022 | |
| 2023 | if (inode) |
Eric Paris | 851f7ff | 2008-11-11 21:48:14 +1100 | [diff] [blame] | 2024 | audit_copy_inode(&context->names[idx], NULL, inode); |
Amy Griffis | 5712e88 | 2007-02-13 14:15:22 -0500 | [diff] [blame] | 2025 | else |
| 2026 | context->names[idx].ino = (unsigned long)-1; |
| 2027 | } |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 2028 | } |
Trond Myklebust | 50e437d | 2007-06-07 22:44:34 -0400 | [diff] [blame] | 2029 | EXPORT_SYMBOL_GPL(__audit_inode_child); |
Amy Griffis | 3e2efce | 2006-07-13 13:16:02 -0400 | [diff] [blame] | 2030 | |
| 2031 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2032 | * auditsc_get_stamp - get local copies of audit_context values |
| 2033 | * @ctx: audit_context for the task |
| 2034 | * @t: timespec to store time recorded in the audit_context |
| 2035 | * @serial: serial value that is recorded in the audit_context |
| 2036 | * |
| 2037 | * Also sets the context as auditable. |
| 2038 | */ |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 2039 | void auditsc_get_stamp(struct audit_context *ctx, |
| 2040 | struct timespec *t, unsigned int *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | { |
David Woodhouse | ce625a8 | 2005-07-18 14:24:46 -0400 | [diff] [blame] | 2042 | if (!ctx->serial) |
| 2043 | ctx->serial = audit_serial(); |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 2044 | t->tv_sec = ctx->ctime.tv_sec; |
| 2045 | t->tv_nsec = ctx->ctime.tv_nsec; |
| 2046 | *serial = ctx->serial; |
| 2047 | ctx->auditable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2050 | /* global counter which is incremented every time something logs in */ |
| 2051 | static atomic_t session_id = ATOMIC_INIT(0); |
| 2052 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2053 | /** |
| 2054 | * audit_set_loginuid - set a task's audit_context loginuid |
| 2055 | * @task: task whose audit context is being modified |
| 2056 | * @loginuid: loginuid value |
| 2057 | * |
| 2058 | * Returns 0. |
| 2059 | * |
| 2060 | * Called (set) from fs/proc/base.c::proc_loginuid_write(). |
| 2061 | */ |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 2062 | int audit_set_loginuid(struct task_struct *task, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | { |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2064 | unsigned int sessionid = atomic_inc_return(&session_id); |
Steve Grubb | 4175710 | 2006-06-12 07:48:28 -0400 | [diff] [blame] | 2065 | struct audit_context *context = task->audit_context; |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 2066 | |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 2067 | if (context && context->in_syscall) { |
| 2068 | struct audit_buffer *ab; |
Steve Grubb | 4175710 | 2006-06-12 07:48:28 -0400 | [diff] [blame] | 2069 | |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 2070 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN); |
| 2071 | if (ab) { |
| 2072 | audit_log_format(ab, "login pid=%d uid=%u " |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2073 | "old auid=%u new auid=%u" |
| 2074 | " old ses=%u new ses=%u", |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 2075 | task->pid, task->uid, |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2076 | task->loginuid, loginuid, |
| 2077 | task->sessionid, sessionid); |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 2078 | audit_log_end(ab); |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 2079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | } |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2081 | task->sessionid = sessionid; |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 2082 | task->loginuid = loginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | return 0; |
| 2084 | } |
| 2085 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2086 | /** |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2087 | * __audit_mq_open - record audit data for a POSIX MQ open |
| 2088 | * @oflag: open flag |
| 2089 | * @mode: mode bits |
| 2090 | * @u_attr: queue attributes |
| 2091 | * |
| 2092 | * Returns 0 for success or NULL context or < 0 on error. |
| 2093 | */ |
| 2094 | int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr) |
| 2095 | { |
| 2096 | struct audit_aux_data_mq_open *ax; |
| 2097 | struct audit_context *context = current->audit_context; |
| 2098 | |
| 2099 | if (!audit_enabled) |
| 2100 | return 0; |
| 2101 | |
| 2102 | if (likely(!context)) |
| 2103 | return 0; |
| 2104 | |
| 2105 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 2106 | if (!ax) |
| 2107 | return -ENOMEM; |
| 2108 | |
| 2109 | if (u_attr != NULL) { |
| 2110 | if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) { |
| 2111 | kfree(ax); |
| 2112 | return -EFAULT; |
| 2113 | } |
| 2114 | } else |
| 2115 | memset(&ax->attr, 0, sizeof(ax->attr)); |
| 2116 | |
| 2117 | ax->oflag = oflag; |
| 2118 | ax->mode = mode; |
| 2119 | |
| 2120 | ax->d.type = AUDIT_MQ_OPEN; |
| 2121 | ax->d.next = context->aux; |
| 2122 | context->aux = (void *)ax; |
| 2123 | return 0; |
| 2124 | } |
| 2125 | |
| 2126 | /** |
| 2127 | * __audit_mq_timedsend - record audit data for a POSIX MQ timed send |
| 2128 | * @mqdes: MQ descriptor |
| 2129 | * @msg_len: Message length |
| 2130 | * @msg_prio: Message priority |
Randy Dunlap | 1dbe83c | 2006-06-27 02:54:01 -0700 | [diff] [blame] | 2131 | * @u_abs_timeout: Message timeout in absolute time |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2132 | * |
| 2133 | * Returns 0 for success or NULL context or < 0 on error. |
| 2134 | */ |
| 2135 | int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio, |
| 2136 | const struct timespec __user *u_abs_timeout) |
| 2137 | { |
| 2138 | struct audit_aux_data_mq_sendrecv *ax; |
| 2139 | struct audit_context *context = current->audit_context; |
| 2140 | |
| 2141 | if (!audit_enabled) |
| 2142 | return 0; |
| 2143 | |
| 2144 | if (likely(!context)) |
| 2145 | return 0; |
| 2146 | |
| 2147 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 2148 | if (!ax) |
| 2149 | return -ENOMEM; |
| 2150 | |
| 2151 | if (u_abs_timeout != NULL) { |
| 2152 | if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) { |
| 2153 | kfree(ax); |
| 2154 | return -EFAULT; |
| 2155 | } |
| 2156 | } else |
| 2157 | memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout)); |
| 2158 | |
| 2159 | ax->mqdes = mqdes; |
| 2160 | ax->msg_len = msg_len; |
| 2161 | ax->msg_prio = msg_prio; |
| 2162 | |
| 2163 | ax->d.type = AUDIT_MQ_SENDRECV; |
| 2164 | ax->d.next = context->aux; |
| 2165 | context->aux = (void *)ax; |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
| 2169 | /** |
| 2170 | * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive |
| 2171 | * @mqdes: MQ descriptor |
| 2172 | * @msg_len: Message length |
Randy Dunlap | 1dbe83c | 2006-06-27 02:54:01 -0700 | [diff] [blame] | 2173 | * @u_msg_prio: Message priority |
| 2174 | * @u_abs_timeout: Message timeout in absolute time |
George C. Wilson | 20ca73b | 2006-05-24 16:09:55 -0500 | [diff] [blame] | 2175 | * |
| 2176 | * Returns 0 for success or NULL context or < 0 on error. |
| 2177 | */ |
| 2178 | int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len, |
| 2179 | unsigned int __user *u_msg_prio, |
| 2180 | const struct timespec __user *u_abs_timeout) |
| 2181 | { |
| 2182 | struct audit_aux_data_mq_sendrecv *ax; |
| 2183 | struct audit_context *context = current->audit_context; |
| 2184 | |
| 2185 | if (!audit_enabled) |
| 2186 | return 0; |
| 2187 | |
| 2188 | if (likely(!context)) |
| 2189 | return 0; |
| 2190 | |
| 2191 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 2192 | if (!ax) |
| 2193 | return -ENOMEM; |
| 2194 | |
| 2195 | if (u_msg_prio != NULL) { |
| 2196 | if (get_user(ax->msg_prio, u_msg_prio)) { |
| 2197 | kfree(ax); |
| 2198 | return -EFAULT; |
| 2199 | } |
| 2200 | } else |
| 2201 | ax->msg_prio = 0; |
| 2202 | |
| 2203 | if (u_abs_timeout != NULL) { |
| 2204 | if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) { |
| 2205 | kfree(ax); |
| 2206 | return -EFAULT; |
| 2207 | } |
| 2208 | } else |
| 2209 | memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout)); |
| 2210 | |
| 2211 | ax->mqdes = mqdes; |
| 2212 | ax->msg_len = msg_len; |
| 2213 | |
| 2214 | ax->d.type = AUDIT_MQ_SENDRECV; |
| 2215 | ax->d.next = context->aux; |
| 2216 | context->aux = (void *)ax; |
| 2217 | return 0; |
| 2218 | } |
| 2219 | |
| 2220 | /** |
| 2221 | * __audit_mq_notify - record audit data for a POSIX MQ notify |
| 2222 | * @mqdes: MQ descriptor |
| 2223 | * @u_notification: Notification event |
| 2224 | * |
| 2225 | * Returns 0 for success or NULL context or < 0 on error. |
| 2226 | */ |
| 2227 | |
| 2228 | int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification) |
| 2229 | { |
| 2230 | struct audit_aux_data_mq_notify *ax; |
| 2231 | struct audit_context *context = current->audit_context; |
| 2232 | |
| 2233 | if (!audit_enabled) |
| 2234 | return 0; |
| 2235 | |
| 2236 | if (likely(!context)) |
| 2237 | return 0; |
| 2238 | |
| 2239 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 2240 | if (!ax) |
| 2241 | return -ENOMEM; |
| 2242 | |
| 2243 | if (u_notification != NULL) { |
| 2244 | if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) { |
| 2245 | kfree(ax); |
| 2246 | return -EFAULT; |
| 2247 | } |
| 2248 | } else |
| 2249 | memset(&ax->notification, 0, sizeof(ax->notification)); |
| 2250 | |
| 2251 | ax->mqdes = mqdes; |
| 2252 | |
| 2253 | ax->d.type = AUDIT_MQ_NOTIFY; |
| 2254 | ax->d.next = context->aux; |
| 2255 | context->aux = (void *)ax; |
| 2256 | return 0; |
| 2257 | } |
| 2258 | |
| 2259 | /** |
| 2260 | * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute |
| 2261 | * @mqdes: MQ descriptor |
| 2262 | * @mqstat: MQ flags |
| 2263 | * |
| 2264 | * Returns 0 for success or NULL context or < 0 on error. |
| 2265 | */ |
| 2266 | int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat) |
| 2267 | { |
| 2268 | struct audit_aux_data_mq_getsetattr *ax; |
| 2269 | struct audit_context *context = current->audit_context; |
| 2270 | |
| 2271 | if (!audit_enabled) |
| 2272 | return 0; |
| 2273 | |
| 2274 | if (likely(!context)) |
| 2275 | return 0; |
| 2276 | |
| 2277 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 2278 | if (!ax) |
| 2279 | return -ENOMEM; |
| 2280 | |
| 2281 | ax->mqdes = mqdes; |
| 2282 | ax->mqstat = *mqstat; |
| 2283 | |
| 2284 | ax->d.type = AUDIT_MQ_GETSETATTR; |
| 2285 | ax->d.next = context->aux; |
| 2286 | context->aux = (void *)ax; |
| 2287 | return 0; |
| 2288 | } |
| 2289 | |
| 2290 | /** |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2291 | * audit_ipc_obj - record audit data for ipc object |
| 2292 | * @ipcp: ipc permissions |
| 2293 | * |
| 2294 | * Returns 0 for success or NULL context or < 0 on error. |
| 2295 | */ |
Al Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 2296 | int __audit_ipc_obj(struct kern_ipc_perm *ipcp) |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2297 | { |
| 2298 | struct audit_aux_data_ipcctl *ax; |
| 2299 | struct audit_context *context = current->audit_context; |
| 2300 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2301 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 2302 | if (!ax) |
| 2303 | return -ENOMEM; |
| 2304 | |
| 2305 | ax->uid = ipcp->uid; |
| 2306 | ax->gid = ipcp->gid; |
| 2307 | ax->mode = ipcp->mode; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2308 | security_ipc_getsecid(ipcp, &ax->osid); |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2309 | ax->d.type = AUDIT_IPC; |
| 2310 | ax->d.next = context->aux; |
| 2311 | context->aux = (void *)ax; |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
| 2315 | /** |
| 2316 | * audit_ipc_set_perm - record audit data for new ipc permissions |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2317 | * @qbytes: msgq bytes |
| 2318 | * @uid: msgq user id |
| 2319 | * @gid: msgq group id |
| 2320 | * @mode: msgq mode (permissions) |
| 2321 | * |
| 2322 | * Returns 0 for success or NULL context or < 0 on error. |
| 2323 | */ |
Al Viro | d8945bb5 | 2006-05-18 16:01:30 -0400 | [diff] [blame] | 2324 | 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] | 2325 | { |
| 2326 | struct audit_aux_data_ipcctl *ax; |
| 2327 | struct audit_context *context = current->audit_context; |
| 2328 | |
Dustin Kirkland | 8c8570f | 2005-11-03 17:15:16 +0000 | [diff] [blame] | 2329 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | if (!ax) |
| 2331 | return -ENOMEM; |
| 2332 | |
| 2333 | ax->qbytes = qbytes; |
| 2334 | ax->uid = uid; |
| 2335 | ax->gid = gid; |
| 2336 | ax->mode = mode; |
| 2337 | |
Steve Grubb | 073115d | 2006-04-02 17:07:33 -0400 | [diff] [blame] | 2338 | ax->d.type = AUDIT_IPC_SET_PERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | ax->d.next = context->aux; |
| 2340 | context->aux = (void *)ax; |
| 2341 | return 0; |
| 2342 | } |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2343 | |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 2344 | int audit_bprm(struct linux_binprm *bprm) |
| 2345 | { |
| 2346 | struct audit_aux_data_execve *ax; |
| 2347 | struct audit_context *context = current->audit_context; |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 2348 | |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 2349 | if (likely(!audit_enabled || !context || context->dummy)) |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 2350 | return 0; |
| 2351 | |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 2352 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 2353 | if (!ax) |
| 2354 | return -ENOMEM; |
| 2355 | |
| 2356 | ax->argc = bprm->argc; |
| 2357 | ax->envc = bprm->envc; |
Peter Zijlstra | bdf4c48 | 2007-07-19 01:48:15 -0700 | [diff] [blame] | 2358 | ax->mm = bprm->mm; |
Al Viro | 473ae30 | 2006-04-26 14:04:08 -0400 | [diff] [blame] | 2359 | ax->d.type = AUDIT_EXECVE; |
| 2360 | ax->d.next = context->aux; |
| 2361 | context->aux = (void *)ax; |
| 2362 | return 0; |
| 2363 | } |
| 2364 | |
| 2365 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2366 | /** |
| 2367 | * audit_socketcall - record audit data for sys_socketcall |
| 2368 | * @nargs: number of args |
| 2369 | * @args: args array |
| 2370 | * |
| 2371 | * Returns 0 for success or NULL context or < 0 on error. |
| 2372 | */ |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2373 | int audit_socketcall(int nargs, unsigned long *args) |
| 2374 | { |
| 2375 | struct audit_aux_data_socketcall *ax; |
| 2376 | struct audit_context *context = current->audit_context; |
| 2377 | |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 2378 | if (likely(!context || context->dummy)) |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2379 | return 0; |
| 2380 | |
| 2381 | ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL); |
| 2382 | if (!ax) |
| 2383 | return -ENOMEM; |
| 2384 | |
| 2385 | ax->nargs = nargs; |
| 2386 | memcpy(ax->args, args, nargs * sizeof(unsigned long)); |
| 2387 | |
| 2388 | ax->d.type = AUDIT_SOCKETCALL; |
| 2389 | ax->d.next = context->aux; |
| 2390 | context->aux = (void *)ax; |
| 2391 | return 0; |
| 2392 | } |
| 2393 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2394 | /** |
Al Viro | db34950 | 2007-02-07 01:48:00 -0500 | [diff] [blame] | 2395 | * __audit_fd_pair - record audit data for pipe and socketpair |
| 2396 | * @fd1: the first file descriptor |
| 2397 | * @fd2: the second file descriptor |
| 2398 | * |
| 2399 | * Returns 0 for success or NULL context or < 0 on error. |
| 2400 | */ |
| 2401 | int __audit_fd_pair(int fd1, int fd2) |
| 2402 | { |
| 2403 | struct audit_context *context = current->audit_context; |
| 2404 | struct audit_aux_data_fd_pair *ax; |
| 2405 | |
| 2406 | if (likely(!context)) { |
| 2407 | return 0; |
| 2408 | } |
| 2409 | |
| 2410 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); |
| 2411 | if (!ax) { |
| 2412 | return -ENOMEM; |
| 2413 | } |
| 2414 | |
| 2415 | ax->fd[0] = fd1; |
| 2416 | ax->fd[1] = fd2; |
| 2417 | |
| 2418 | ax->d.type = AUDIT_FD_PAIR; |
| 2419 | ax->d.next = context->aux; |
| 2420 | context->aux = (void *)ax; |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2425 | * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto |
| 2426 | * @len: data length in user space |
| 2427 | * @a: data address in kernel space |
| 2428 | * |
| 2429 | * Returns 0 for success or NULL context or < 0 on error. |
| 2430 | */ |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2431 | int audit_sockaddr(int len, void *a) |
| 2432 | { |
| 2433 | struct audit_aux_data_sockaddr *ax; |
| 2434 | struct audit_context *context = current->audit_context; |
| 2435 | |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 2436 | if (likely(!context || context->dummy)) |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 2437 | return 0; |
| 2438 | |
| 2439 | ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL); |
| 2440 | if (!ax) |
| 2441 | return -ENOMEM; |
| 2442 | |
| 2443 | ax->len = len; |
| 2444 | memcpy(ax->a, a, len); |
| 2445 | |
| 2446 | ax->d.type = AUDIT_SOCKADDR; |
| 2447 | ax->d.next = context->aux; |
| 2448 | context->aux = (void *)ax; |
| 2449 | return 0; |
| 2450 | } |
| 2451 | |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 2452 | void __audit_ptrace(struct task_struct *t) |
| 2453 | { |
| 2454 | struct audit_context *context = current->audit_context; |
| 2455 | |
| 2456 | context->target_pid = t->pid; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2457 | context->target_auid = audit_get_loginuid(t); |
| 2458 | context->target_uid = t->uid; |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2459 | context->target_sessionid = audit_get_sessionid(t); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2460 | security_task_getsecid(t, &context->target_sid); |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2461 | memcpy(context->target_comm, t->comm, TASK_COMM_LEN); |
Al Viro | a5cb013 | 2007-03-20 13:58:35 -0400 | [diff] [blame] | 2462 | } |
| 2463 | |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2464 | /** |
Randy Dunlap | b0dd25a | 2005-09-13 12:47:11 -0700 | [diff] [blame] | 2465 | * audit_signal_info - record signal info for shutting down audit subsystem |
| 2466 | * @sig: signal value |
| 2467 | * @t: task being signaled |
| 2468 | * |
| 2469 | * If the audit subsystem is being terminated, record the task (pid) |
| 2470 | * and uid that is doing that. |
| 2471 | */ |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2472 | int __audit_signal_info(int sig, struct task_struct *t) |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2473 | { |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2474 | struct audit_aux_data_pids *axp; |
| 2475 | struct task_struct *tsk = current; |
| 2476 | struct audit_context *ctx = tsk->audit_context; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2477 | |
Al Viro | 175fc48 | 2007-08-08 00:01:46 +0100 | [diff] [blame] | 2478 | if (audit_pid && t->tgid == audit_pid) { |
Eric Paris | ee1d315 | 2008-07-07 10:49:45 -0400 | [diff] [blame] | 2479 | if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) { |
Al Viro | 175fc48 | 2007-08-08 00:01:46 +0100 | [diff] [blame] | 2480 | audit_sig_pid = tsk->pid; |
Al Viro | bfef93a | 2008-01-10 04:53:18 -0500 | [diff] [blame] | 2481 | if (tsk->loginuid != -1) |
| 2482 | audit_sig_uid = tsk->loginuid; |
Al Viro | 175fc48 | 2007-08-08 00:01:46 +0100 | [diff] [blame] | 2483 | else |
| 2484 | audit_sig_uid = tsk->uid; |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2485 | security_task_getsecid(tsk, &audit_sig_sid); |
Al Viro | 175fc48 | 2007-08-08 00:01:46 +0100 | [diff] [blame] | 2486 | } |
| 2487 | if (!audit_signals || audit_dummy_context()) |
| 2488 | return 0; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2489 | } |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2490 | |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2491 | /* optimize the common case by putting first signal recipient directly |
| 2492 | * in audit_context */ |
| 2493 | if (!ctx->target_pid) { |
| 2494 | ctx->target_pid = t->tgid; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2495 | ctx->target_auid = audit_get_loginuid(t); |
| 2496 | ctx->target_uid = t->uid; |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2497 | ctx->target_sessionid = audit_get_sessionid(t); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2498 | security_task_getsecid(t, &ctx->target_sid); |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2499 | memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2500 | return 0; |
| 2501 | } |
| 2502 | |
| 2503 | axp = (void *)ctx->aux_pids; |
| 2504 | if (!axp || axp->pid_count == AUDIT_AUX_PIDS) { |
| 2505 | axp = kzalloc(sizeof(*axp), GFP_ATOMIC); |
| 2506 | if (!axp) |
| 2507 | return -ENOMEM; |
| 2508 | |
| 2509 | axp->d.type = AUDIT_OBJ_PID; |
| 2510 | axp->d.next = ctx->aux_pids; |
| 2511 | ctx->aux_pids = (void *)axp; |
| 2512 | } |
Adrian Bunk | 88ae704 | 2007-08-22 14:01:05 -0700 | [diff] [blame] | 2513 | BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2514 | |
| 2515 | axp->target_pid[axp->pid_count] = t->tgid; |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2516 | axp->target_auid[axp->pid_count] = audit_get_loginuid(t); |
| 2517 | axp->target_uid[axp->pid_count] = t->uid; |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2518 | axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2519 | security_task_getsecid(t, &axp->target_sid[axp->pid_count]); |
Eric Paris | c2a7780 | 2008-01-07 13:40:17 -0500 | [diff] [blame] | 2520 | memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN); |
Amy Griffis | e54dc24 | 2007-03-29 18:01:04 -0400 | [diff] [blame] | 2521 | axp->pid_count++; |
| 2522 | |
| 2523 | return 0; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 2524 | } |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2525 | |
| 2526 | /** |
Eric Paris | 3fc689e | 2008-11-11 21:48:18 +1100 | [diff] [blame^] | 2527 | * __audit_log_bprm_fcaps - store information about a loading bprm and relevant fcaps |
| 2528 | * @bprm pointer to the bprm being processed |
| 2529 | * @caps the caps read from the disk |
| 2530 | * |
| 2531 | * Simply check if the proc already has the caps given by the file and if not |
| 2532 | * store the priv escalation info for later auditing at the end of the syscall |
| 2533 | * |
| 2534 | * this can fail and we don't care. See the note in audit.h for |
| 2535 | * audit_log_bprm_fcaps() for my explaination.... |
| 2536 | * |
| 2537 | * -Eric |
| 2538 | */ |
| 2539 | void __audit_log_bprm_fcaps(struct linux_binprm *bprm, kernel_cap_t *pP, kernel_cap_t *pE) |
| 2540 | { |
| 2541 | struct audit_aux_data_bprm_fcaps *ax; |
| 2542 | struct audit_context *context = current->audit_context; |
| 2543 | struct cpu_vfs_cap_data vcaps; |
| 2544 | struct dentry *dentry; |
| 2545 | |
| 2546 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); |
| 2547 | if (!ax) |
| 2548 | return; |
| 2549 | |
| 2550 | ax->d.type = AUDIT_BPRM_FCAPS; |
| 2551 | ax->d.next = context->aux; |
| 2552 | context->aux = (void *)ax; |
| 2553 | |
| 2554 | dentry = dget(bprm->file->f_dentry); |
| 2555 | get_vfs_caps_from_disk(dentry, &vcaps); |
| 2556 | dput(dentry); |
| 2557 | |
| 2558 | ax->fcap.permitted = vcaps.permitted; |
| 2559 | ax->fcap.inheritable = vcaps.inheritable; |
| 2560 | ax->fcap.fE = !!(vcaps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE); |
| 2561 | ax->fcap_ver = (vcaps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT; |
| 2562 | |
| 2563 | ax->old_pcap.permitted = *pP; |
| 2564 | ax->old_pcap.inheritable = current->cap_inheritable; |
| 2565 | ax->old_pcap.effective = *pE; |
| 2566 | |
| 2567 | ax->new_pcap.permitted = current->cap_permitted; |
| 2568 | ax->new_pcap.inheritable = current->cap_inheritable; |
| 2569 | ax->new_pcap.effective = current->cap_effective; |
| 2570 | } |
| 2571 | |
| 2572 | /** |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2573 | * audit_core_dumps - record information about processes that end abnormally |
Henrik Kretzschmar | 6d9525b | 2007-07-15 23:41:10 -0700 | [diff] [blame] | 2574 | * @signr: signal value |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2575 | * |
| 2576 | * If a process ends with a core dump, something fishy is going on and we |
| 2577 | * should record the event for investigation. |
| 2578 | */ |
| 2579 | void audit_core_dumps(long signr) |
| 2580 | { |
| 2581 | struct audit_buffer *ab; |
| 2582 | u32 sid; |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2583 | uid_t auid = audit_get_loginuid(current); |
| 2584 | unsigned int sessionid = audit_get_sessionid(current); |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2585 | |
| 2586 | if (!audit_enabled) |
| 2587 | return; |
| 2588 | |
| 2589 | if (signr == SIGQUIT) /* don't care for those */ |
| 2590 | return; |
| 2591 | |
| 2592 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND); |
Eric Paris | 4746ec5 | 2008-01-08 10:06:53 -0500 | [diff] [blame] | 2593 | audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u", |
| 2594 | auid, current->uid, current->gid, sessionid); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2595 | security_task_getsecid(current, &sid); |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2596 | if (sid) { |
| 2597 | char *ctx = NULL; |
| 2598 | u32 len; |
| 2599 | |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2600 | if (security_secid_to_secctx(sid, &ctx, &len)) |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2601 | audit_log_format(ab, " ssid=%u", sid); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2602 | else { |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2603 | audit_log_format(ab, " subj=%s", ctx); |
Ahmed S. Darwish | 2a862b3 | 2008-03-01 21:54:38 +0200 | [diff] [blame] | 2604 | security_release_secctx(ctx, len); |
| 2605 | } |
Steve Grubb | 0a4ff8c | 2007-04-19 10:28:21 -0400 | [diff] [blame] | 2606 | } |
| 2607 | audit_log_format(ab, " pid=%d comm=", current->pid); |
| 2608 | audit_log_untrustedstring(ab, current->comm); |
| 2609 | audit_log_format(ab, " sig=%ld", signr); |
| 2610 | audit_log_end(ab); |
| 2611 | } |