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