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