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