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