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