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