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