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