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