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