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