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