| 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. |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> |
| 22 | * |
| 23 | * Many of the ideas implemented here are from Stephen C. Tweedie, |
| 24 | * especially the idea of avoiding a copy by using getname. |
| 25 | * |
| 26 | * The method for actual interception of syscall entry and exit (not in |
| 27 | * this file -- see entry.S) is based on a GPL'd patch written by |
| 28 | * okir@suse.de and Copyright 2003 SuSE Linux AG. |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <linux/init.h> |
| 33 | #include <asm/atomic.h> |
| 34 | #include <asm/types.h> |
| 35 | #include <linux/mm.h> |
| 36 | #include <linux/module.h> |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 37 | #include <linux/mount.h> |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 38 | #include <linux/socket.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/audit.h> |
| 40 | #include <linux/personality.h> |
| 41 | #include <linux/time.h> |
| 42 | #include <asm/unistd.h> |
| 43 | |
| 44 | /* 0 = no checking |
| 45 | 1 = put_count checking |
| 46 | 2 = verbose put_count checking |
| 47 | */ |
| 48 | #define AUDIT_DEBUG 0 |
| 49 | |
| 50 | /* No syscall auditing will take place unless audit_enabled != 0. */ |
| 51 | extern int audit_enabled; |
| 52 | |
| 53 | /* AUDIT_NAMES is the number of slots we reserve in the audit_context |
| 54 | * for saving names from getname(). */ |
| 55 | #define AUDIT_NAMES 20 |
| 56 | |
| 57 | /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the |
| 58 | * audit_context from being used for nameless inodes from |
| 59 | * path_lookup. */ |
| 60 | #define AUDIT_NAMES_RESERVED 7 |
| 61 | |
| 62 | /* At task start time, the audit_state is set in the audit_context using |
| 63 | a per-task filter. At syscall entry, the audit_state is augmented by |
| 64 | the syscall filter. */ |
| 65 | enum audit_state { |
| 66 | AUDIT_DISABLED, /* Do not create per-task audit_context. |
| 67 | * No syscall-specific audit records can |
| 68 | * be generated. */ |
| 69 | AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context, |
| 70 | * but don't necessarily fill it in at |
| 71 | * syscall entry time (i.e., filter |
| 72 | * instead). */ |
| 73 | AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context, |
| 74 | * and always fill it in at syscall |
| 75 | * entry time. This makes a full |
| 76 | * syscall record available if some |
| 77 | * other part of the kernel decides it |
| 78 | * should be recorded. */ |
| 79 | AUDIT_RECORD_CONTEXT /* Create the per-task audit_context, |
| 80 | * always fill it in at syscall entry |
| 81 | * time, and always write out the audit |
| 82 | * record at syscall exit time. */ |
| 83 | }; |
| 84 | |
| 85 | /* When fs/namei.c:getname() is called, we store the pointer in name and |
| 86 | * we don't let putname() free it (instead we free all of the saved |
| 87 | * pointers at syscall exit time). |
| 88 | * |
| 89 | * Further, in fs/namei.c:path_lookup() we store the inode and device. */ |
| 90 | struct audit_names { |
| 91 | const char *name; |
| 92 | unsigned long ino; |
| 93 | dev_t dev; |
| 94 | umode_t mode; |
| 95 | uid_t uid; |
| 96 | gid_t gid; |
| 97 | dev_t rdev; |
| 98 | }; |
| 99 | |
| 100 | struct audit_aux_data { |
| 101 | struct audit_aux_data *next; |
| 102 | int type; |
| 103 | }; |
| 104 | |
| 105 | #define AUDIT_AUX_IPCPERM 0 |
| 106 | |
| 107 | struct audit_aux_data_ipcctl { |
| 108 | struct audit_aux_data d; |
| 109 | struct ipc_perm p; |
| 110 | unsigned long qbytes; |
| 111 | uid_t uid; |
| 112 | gid_t gid; |
| 113 | mode_t mode; |
| 114 | }; |
| 115 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 116 | struct audit_aux_data_socketcall { |
| 117 | struct audit_aux_data d; |
| 118 | int nargs; |
| 119 | unsigned long args[0]; |
| 120 | }; |
| 121 | |
| 122 | struct audit_aux_data_sockaddr { |
| 123 | struct audit_aux_data d; |
| 124 | int len; |
| 125 | char a[0]; |
| 126 | }; |
| 127 | |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 128 | struct audit_aux_data_path { |
| 129 | struct audit_aux_data d; |
| 130 | struct dentry *dentry; |
| 131 | struct vfsmount *mnt; |
| 132 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | /* The per-task audit context. */ |
| 135 | struct audit_context { |
| 136 | int in_syscall; /* 1 if task is in a syscall */ |
| 137 | enum audit_state state; |
| 138 | unsigned int serial; /* serial number for record */ |
| 139 | struct timespec ctime; /* time of syscall entry */ |
| 140 | uid_t loginuid; /* login uid (identity) */ |
| 141 | int major; /* syscall number */ |
| 142 | unsigned long argv[4]; /* syscall arguments */ |
| 143 | int return_valid; /* return code is valid */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 144 | long return_code;/* syscall return code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | int auditable; /* 1 if record should be written */ |
| 146 | int name_count; |
| 147 | struct audit_names names[AUDIT_NAMES]; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 148 | struct dentry * pwd; |
| 149 | struct vfsmount * pwdmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | struct audit_context *previous; /* For nested syscalls */ |
| 151 | struct audit_aux_data *aux; |
| 152 | |
| 153 | /* Save things to print about task_struct */ |
| 154 | pid_t pid; |
| 155 | uid_t uid, euid, suid, fsuid; |
| 156 | gid_t gid, egid, sgid, fsgid; |
| 157 | unsigned long personality; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 158 | int arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | #if AUDIT_DEBUG |
| 161 | int put_count; |
| 162 | int ino_count; |
| 163 | #endif |
| 164 | }; |
| 165 | |
| 166 | /* Public API */ |
| 167 | /* There are three lists of rules -- one to search at task creation |
| 168 | * time, one to search at syscall entry time, and another to search at |
| 169 | * syscall exit time. */ |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 170 | static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = { |
| 171 | LIST_HEAD_INIT(audit_filter_list[0]), |
| 172 | LIST_HEAD_INIT(audit_filter_list[1]), |
| 173 | LIST_HEAD_INIT(audit_filter_list[2]), |
| 174 | LIST_HEAD_INIT(audit_filter_list[3]), |
| 175 | LIST_HEAD_INIT(audit_filter_list[4]), |
| 176 | #if AUDIT_NR_FILTERS != 5 |
| 177 | #error Fix audit_filter_list initialiser |
| 178 | #endif |
| 179 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | struct audit_entry { |
| 182 | struct list_head list; |
| 183 | struct rcu_head rcu; |
| 184 | struct audit_rule rule; |
| 185 | }; |
| 186 | |
David Woodhouse | 7ca0026 | 2005-05-19 11:23:13 +0100 | [diff] [blame] | 187 | extern int audit_pid; |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* Check to see if two rules are identical. It is called from |
| 190 | * audit_del_rule during AUDIT_DEL. */ |
| 191 | static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b) |
| 192 | { |
| 193 | int i; |
| 194 | |
| 195 | if (a->flags != b->flags) |
| 196 | return 1; |
| 197 | |
| 198 | if (a->action != b->action) |
| 199 | return 1; |
| 200 | |
| 201 | if (a->field_count != b->field_count) |
| 202 | return 1; |
| 203 | |
| 204 | for (i = 0; i < a->field_count; i++) { |
| 205 | if (a->fields[i] != b->fields[i] |
| 206 | || a->values[i] != b->values[i]) |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) |
| 211 | if (a->mask[i] != b->mask[i]) |
| 212 | return 1; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | /* Note that audit_add_rule and audit_del_rule are called via |
| 218 | * audit_receive() in audit.c, and are protected by |
| 219 | * audit_netlink_sem. */ |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 220 | static inline void audit_add_rule(struct audit_entry *entry, |
| 221 | struct list_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 223 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) { |
| 224 | entry->rule.flags &= ~AUDIT_FILTER_PREPEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | list_add_rcu(&entry->list, list); |
| 226 | } else { |
| 227 | list_add_tail_rcu(&entry->list, list); |
| 228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void audit_free_rule(struct rcu_head *head) |
| 232 | { |
| 233 | struct audit_entry *e = container_of(head, struct audit_entry, rcu); |
| 234 | kfree(e); |
| 235 | } |
| 236 | |
| 237 | /* Note that audit_add_rule and audit_del_rule are called via |
| 238 | * audit_receive() in audit.c, and are protected by |
| 239 | * audit_netlink_sem. */ |
| 240 | static inline int audit_del_rule(struct audit_rule *rule, |
| 241 | struct list_head *list) |
| 242 | { |
| 243 | struct audit_entry *e; |
| 244 | |
| 245 | /* Do not use the _rcu iterator here, since this is the only |
| 246 | * deletion routine. */ |
| 247 | list_for_each_entry(e, list, list) { |
| 248 | if (!audit_compare_rule(rule, &e->rule)) { |
| 249 | list_del_rcu(&e->list); |
| 250 | call_rcu(&e->rcu, audit_free_rule); |
| 251 | return 0; |
| 252 | } |
| 253 | } |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 254 | return -ENOENT; /* No matching rule */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | /* Copy rule from user-space to kernel-space. Called during |
| 258 | * AUDIT_ADD. */ |
| 259 | static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s) |
| 260 | { |
| 261 | int i; |
| 262 | |
| 263 | if (s->action != AUDIT_NEVER |
| 264 | && s->action != AUDIT_POSSIBLE |
| 265 | && s->action != AUDIT_ALWAYS) |
| 266 | return -1; |
| 267 | if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS) |
| 268 | return -1; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 269 | if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS) |
| 270 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | d->flags = s->flags; |
| 273 | d->action = s->action; |
| 274 | d->field_count = s->field_count; |
| 275 | for (i = 0; i < d->field_count; i++) { |
| 276 | d->fields[i] = s->fields[i]; |
| 277 | d->values[i] = s->values[i]; |
| 278 | } |
| 279 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i]; |
| 280 | return 0; |
| 281 | } |
| 282 | |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 283 | int audit_receive_filter(int type, int pid, int uid, int seq, void *data, |
| 284 | uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | struct audit_entry *entry; |
| 287 | int err = 0; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 288 | int i; |
| 289 | unsigned listnr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | switch (type) { |
| 292 | case AUDIT_LIST: |
| 293 | /* The *_rcu iterators not needed here because we are |
| 294 | always called with audit_netlink_sem held. */ |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 295 | for (i=0; i<AUDIT_NR_FILTERS; i++) { |
| 296 | list_for_each_entry(entry, &audit_filter_list[i], list) |
| 297 | audit_send_reply(pid, seq, AUDIT_LIST, 0, 1, |
| 298 | &entry->rule, sizeof(entry->rule)); |
| 299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0); |
| 301 | break; |
| 302 | case AUDIT_ADD: |
| 303 | if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL))) |
| 304 | return -ENOMEM; |
| 305 | if (audit_copy_rule(&entry->rule, data)) { |
| 306 | kfree(entry); |
| 307 | return -EINVAL; |
| 308 | } |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 309 | listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND; |
| 310 | audit_add_rule(entry, &audit_filter_list[listnr]); |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 311 | audit_log(NULL, AUDIT_CONFIG_CHANGE, |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 312 | "auid=%u added an audit rule\n", loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | break; |
| 314 | case AUDIT_DEL: |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 315 | listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND; |
| 316 | if (listnr >= AUDIT_NR_FILTERS) |
| 317 | return -EINVAL; |
| 318 | |
| 319 | err = audit_del_rule(data, &audit_filter_list[listnr]); |
| 320 | if (!err) |
| 321 | audit_log(NULL, AUDIT_CONFIG_CHANGE, |
| 322 | "auid=%u removed an audit rule\n", loginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | break; |
| 324 | default: |
| 325 | return -EINVAL; |
| 326 | } |
| 327 | |
| 328 | return err; |
| 329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
| 331 | /* Compare a task_struct with an audit_rule. Return 1 on match, 0 |
| 332 | * otherwise. */ |
| 333 | static int audit_filter_rules(struct task_struct *tsk, |
| 334 | struct audit_rule *rule, |
| 335 | struct audit_context *ctx, |
| 336 | enum audit_state *state) |
| 337 | { |
| 338 | int i, j; |
| 339 | |
| 340 | for (i = 0; i < rule->field_count; i++) { |
| 341 | u32 field = rule->fields[i] & ~AUDIT_NEGATE; |
| 342 | u32 value = rule->values[i]; |
| 343 | int result = 0; |
| 344 | |
| 345 | switch (field) { |
| 346 | case AUDIT_PID: |
| 347 | result = (tsk->pid == value); |
| 348 | break; |
| 349 | case AUDIT_UID: |
| 350 | result = (tsk->uid == value); |
| 351 | break; |
| 352 | case AUDIT_EUID: |
| 353 | result = (tsk->euid == value); |
| 354 | break; |
| 355 | case AUDIT_SUID: |
| 356 | result = (tsk->suid == value); |
| 357 | break; |
| 358 | case AUDIT_FSUID: |
| 359 | result = (tsk->fsuid == value); |
| 360 | break; |
| 361 | case AUDIT_GID: |
| 362 | result = (tsk->gid == value); |
| 363 | break; |
| 364 | case AUDIT_EGID: |
| 365 | result = (tsk->egid == value); |
| 366 | break; |
| 367 | case AUDIT_SGID: |
| 368 | result = (tsk->sgid == value); |
| 369 | break; |
| 370 | case AUDIT_FSGID: |
| 371 | result = (tsk->fsgid == value); |
| 372 | break; |
| 373 | case AUDIT_PERS: |
| 374 | result = (tsk->personality == value); |
| 375 | break; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 376 | case AUDIT_ARCH: |
| 377 | if (ctx) |
| 378 | result = (ctx->arch == value); |
| 379 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | case AUDIT_EXIT: |
| 382 | if (ctx && ctx->return_valid) |
| 383 | result = (ctx->return_code == value); |
| 384 | break; |
| 385 | case AUDIT_SUCCESS: |
| 386 | if (ctx && ctx->return_valid) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 387 | result = (ctx->return_valid == AUDITSC_SUCCESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | break; |
| 389 | case AUDIT_DEVMAJOR: |
| 390 | if (ctx) { |
| 391 | for (j = 0; j < ctx->name_count; j++) { |
| 392 | if (MAJOR(ctx->names[j].dev)==value) { |
| 393 | ++result; |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | break; |
| 399 | case AUDIT_DEVMINOR: |
| 400 | if (ctx) { |
| 401 | for (j = 0; j < ctx->name_count; j++) { |
| 402 | if (MINOR(ctx->names[j].dev)==value) { |
| 403 | ++result; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | break; |
| 409 | case AUDIT_INODE: |
| 410 | if (ctx) { |
| 411 | for (j = 0; j < ctx->name_count; j++) { |
| 412 | if (ctx->names[j].ino == value) { |
| 413 | ++result; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | break; |
| 419 | case AUDIT_LOGINUID: |
| 420 | result = 0; |
| 421 | if (ctx) |
| 422 | result = (ctx->loginuid == value); |
| 423 | break; |
| 424 | case AUDIT_ARG0: |
| 425 | case AUDIT_ARG1: |
| 426 | case AUDIT_ARG2: |
| 427 | case AUDIT_ARG3: |
| 428 | if (ctx) |
| 429 | result = (ctx->argv[field-AUDIT_ARG0]==value); |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | if (rule->fields[i] & AUDIT_NEGATE) |
| 434 | result = !result; |
| 435 | if (!result) |
| 436 | return 0; |
| 437 | } |
| 438 | switch (rule->action) { |
| 439 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; |
| 440 | case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break; |
| 441 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
| 442 | } |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | /* At process creation time, we can determine if system-call auditing is |
| 447 | * completely disabled for this task. Since we only have the task |
| 448 | * structure at this point, we can only check uid and gid. |
| 449 | */ |
| 450 | static enum audit_state audit_filter_task(struct task_struct *tsk) |
| 451 | { |
| 452 | struct audit_entry *e; |
| 453 | enum audit_state state; |
| 454 | |
| 455 | rcu_read_lock(); |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 456 | 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] | 457 | if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { |
| 458 | rcu_read_unlock(); |
| 459 | return state; |
| 460 | } |
| 461 | } |
| 462 | rcu_read_unlock(); |
| 463 | return AUDIT_BUILD_CONTEXT; |
| 464 | } |
| 465 | |
| 466 | /* At syscall entry and exit time, this filter is called if the |
| 467 | * 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] | 468 | * also not high enough that we already know we have to write an audit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT). |
| 470 | */ |
| 471 | static enum audit_state audit_filter_syscall(struct task_struct *tsk, |
| 472 | struct audit_context *ctx, |
| 473 | struct list_head *list) |
| 474 | { |
| 475 | struct audit_entry *e; |
| 476 | enum audit_state state; |
| 477 | int word = AUDIT_WORD(ctx->major); |
| 478 | int bit = AUDIT_BIT(ctx->major); |
| 479 | |
| 480 | rcu_read_lock(); |
| 481 | list_for_each_entry_rcu(e, list, list) { |
| 482 | if ((e->rule.mask[word] & bit) == bit |
| 483 | && audit_filter_rules(tsk, &e->rule, ctx, &state)) { |
| 484 | rcu_read_unlock(); |
| 485 | return state; |
| 486 | } |
| 487 | } |
| 488 | rcu_read_unlock(); |
| 489 | return AUDIT_BUILD_CONTEXT; |
| 490 | } |
| 491 | |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 492 | int audit_filter_user(struct task_struct *tsk, int type) |
| 493 | { |
| 494 | struct audit_entry *e; |
| 495 | enum audit_state state; |
| 496 | |
| 497 | rcu_read_lock(); |
| 498 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) { |
| 499 | if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { |
| 500 | rcu_read_unlock(); |
| 501 | return state != AUDIT_DISABLED; |
| 502 | } |
| 503 | } |
| 504 | rcu_read_unlock(); |
| 505 | return 1; /* Audit by default */ |
| 506 | |
| 507 | } |
| 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | /* This should be called with task_lock() held. */ |
| 510 | static inline struct audit_context *audit_get_context(struct task_struct *tsk, |
| 511 | int return_valid, |
| 512 | int return_code) |
| 513 | { |
| 514 | struct audit_context *context = tsk->audit_context; |
| 515 | |
| 516 | if (likely(!context)) |
| 517 | return NULL; |
| 518 | context->return_valid = return_valid; |
| 519 | context->return_code = return_code; |
| 520 | |
| 521 | if (context->in_syscall && !context->auditable) { |
| 522 | enum audit_state state; |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 523 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (state == AUDIT_RECORD_CONTEXT) |
| 525 | context->auditable = 1; |
| 526 | } |
| 527 | |
| 528 | context->pid = tsk->pid; |
| 529 | context->uid = tsk->uid; |
| 530 | context->gid = tsk->gid; |
| 531 | context->euid = tsk->euid; |
| 532 | context->suid = tsk->suid; |
| 533 | context->fsuid = tsk->fsuid; |
| 534 | context->egid = tsk->egid; |
| 535 | context->sgid = tsk->sgid; |
| 536 | context->fsgid = tsk->fsgid; |
| 537 | context->personality = tsk->personality; |
| 538 | tsk->audit_context = NULL; |
| 539 | return context; |
| 540 | } |
| 541 | |
| 542 | static inline void audit_free_names(struct audit_context *context) |
| 543 | { |
| 544 | int i; |
| 545 | |
| 546 | #if AUDIT_DEBUG == 2 |
| 547 | if (context->auditable |
| 548 | ||context->put_count + context->ino_count != context->name_count) { |
| 549 | printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d" |
| 550 | " name_count=%d put_count=%d" |
| 551 | " ino_count=%d [NOT freeing]\n", |
| 552 | __LINE__, |
| 553 | context->serial, context->major, context->in_syscall, |
| 554 | context->name_count, context->put_count, |
| 555 | context->ino_count); |
| 556 | for (i = 0; i < context->name_count; i++) |
| 557 | printk(KERN_ERR "names[%d] = %p = %s\n", i, |
| 558 | context->names[i].name, |
| 559 | context->names[i].name); |
| 560 | dump_stack(); |
| 561 | return; |
| 562 | } |
| 563 | #endif |
| 564 | #if AUDIT_DEBUG |
| 565 | context->put_count = 0; |
| 566 | context->ino_count = 0; |
| 567 | #endif |
| 568 | |
| 569 | for (i = 0; i < context->name_count; i++) |
| 570 | if (context->names[i].name) |
| 571 | __putname(context->names[i].name); |
| 572 | context->name_count = 0; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 573 | if (context->pwd) |
| 574 | dput(context->pwd); |
| 575 | if (context->pwdmnt) |
| 576 | mntput(context->pwdmnt); |
| 577 | context->pwd = NULL; |
| 578 | context->pwdmnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static inline void audit_free_aux(struct audit_context *context) |
| 582 | { |
| 583 | struct audit_aux_data *aux; |
| 584 | |
| 585 | while ((aux = context->aux)) { |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 586 | if (aux->type == AUDIT_AVC_PATH) { |
| 587 | struct audit_aux_data_path *axi = (void *)aux; |
| 588 | dput(axi->dentry); |
| 589 | mntput(axi->mnt); |
| 590 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | context->aux = aux->next; |
| 592 | kfree(aux); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | static inline void audit_zero_context(struct audit_context *context, |
| 597 | enum audit_state state) |
| 598 | { |
| 599 | uid_t loginuid = context->loginuid; |
| 600 | |
| 601 | memset(context, 0, sizeof(*context)); |
| 602 | context->state = state; |
| 603 | context->loginuid = loginuid; |
| 604 | } |
| 605 | |
| 606 | static inline struct audit_context *audit_alloc_context(enum audit_state state) |
| 607 | { |
| 608 | struct audit_context *context; |
| 609 | |
| 610 | if (!(context = kmalloc(sizeof(*context), GFP_KERNEL))) |
| 611 | return NULL; |
| 612 | audit_zero_context(context, state); |
| 613 | return context; |
| 614 | } |
| 615 | |
| 616 | /* Filter on the task information and allocate a per-task audit context |
| 617 | * if necessary. Doing so turns on system call auditing for the |
| 618 | * specified task. This is called from copy_process, so no lock is |
| 619 | * needed. */ |
| 620 | int audit_alloc(struct task_struct *tsk) |
| 621 | { |
| 622 | struct audit_context *context; |
| 623 | enum audit_state state; |
| 624 | |
| 625 | if (likely(!audit_enabled)) |
| 626 | return 0; /* Return if not auditing. */ |
| 627 | |
| 628 | state = audit_filter_task(tsk); |
| 629 | if (likely(state == AUDIT_DISABLED)) |
| 630 | return 0; |
| 631 | |
| 632 | if (!(context = audit_alloc_context(state))) { |
| 633 | audit_log_lost("out of memory in audit_alloc"); |
| 634 | return -ENOMEM; |
| 635 | } |
| 636 | |
| 637 | /* Preserve login uid */ |
| 638 | context->loginuid = -1; |
| 639 | if (current->audit_context) |
| 640 | context->loginuid = current->audit_context->loginuid; |
| 641 | |
| 642 | tsk->audit_context = context; |
| 643 | set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static inline void audit_free_context(struct audit_context *context) |
| 648 | { |
| 649 | struct audit_context *previous; |
| 650 | int count = 0; |
| 651 | |
| 652 | do { |
| 653 | previous = context->previous; |
| 654 | if (previous || (count && count < 10)) { |
| 655 | ++count; |
| 656 | printk(KERN_ERR "audit(:%d): major=%d name_count=%d:" |
| 657 | " freeing multiple contexts (%d)\n", |
| 658 | context->serial, context->major, |
| 659 | context->name_count, count); |
| 660 | } |
| 661 | audit_free_names(context); |
| 662 | audit_free_aux(context); |
| 663 | kfree(context); |
| 664 | context = previous; |
| 665 | } while (context); |
| 666 | if (count >= 10) |
| 667 | printk(KERN_ERR "audit: freed %d contexts\n", count); |
| 668 | } |
| 669 | |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 670 | static void audit_log_task_info(struct audit_buffer *ab) |
| 671 | { |
| 672 | char name[sizeof(current->comm)]; |
| 673 | struct mm_struct *mm = current->mm; |
| 674 | struct vm_area_struct *vma; |
| 675 | |
| 676 | get_task_comm(name, current); |
David Woodhouse | 99e45ee | 2005-05-23 21:57:41 +0100 | [diff] [blame] | 677 | audit_log_format(ab, " comm="); |
| 678 | audit_log_untrustedstring(ab, name); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 679 | |
| 680 | if (!mm) |
| 681 | return; |
| 682 | |
| 683 | down_read(&mm->mmap_sem); |
| 684 | vma = mm->mmap; |
| 685 | while (vma) { |
| 686 | if ((vma->vm_flags & VM_EXECUTABLE) && |
| 687 | vma->vm_file) { |
| 688 | audit_log_d_path(ab, "exe=", |
| 689 | vma->vm_file->f_dentry, |
| 690 | vma->vm_file->f_vfsmnt); |
| 691 | break; |
| 692 | } |
| 693 | vma = vma->vm_next; |
| 694 | } |
| 695 | up_read(&mm->mmap_sem); |
| 696 | } |
| 697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | static void audit_log_exit(struct audit_context *context) |
| 699 | { |
| 700 | int i; |
| 701 | struct audit_buffer *ab; |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 702 | struct audit_aux_data *aux; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 704 | ab = audit_log_start(context, AUDIT_SYSCALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | if (!ab) |
| 706 | return; /* audit_panic has been called */ |
David Woodhouse | bccf6ae | 2005-05-23 21:35:28 +0100 | [diff] [blame] | 707 | audit_log_format(ab, "arch=%x syscall=%d", |
| 708 | context->arch, context->major); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | if (context->personality != PER_LINUX) |
| 710 | audit_log_format(ab, " per=%lx", context->personality); |
| 711 | if (context->return_valid) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 712 | audit_log_format(ab, " success=%s exit=%ld", |
| 713 | (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", |
| 714 | context->return_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | audit_log_format(ab, |
| 716 | " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 717 | " pid=%d auid=%u uid=%u gid=%u" |
| 718 | " euid=%u suid=%u fsuid=%u" |
| 719 | " egid=%u sgid=%u fsgid=%u", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | context->argv[0], |
| 721 | context->argv[1], |
| 722 | context->argv[2], |
| 723 | context->argv[3], |
| 724 | context->name_count, |
| 725 | context->pid, |
| 726 | context->loginuid, |
| 727 | context->uid, |
| 728 | context->gid, |
| 729 | context->euid, context->suid, context->fsuid, |
| 730 | context->egid, context->sgid, context->fsgid); |
Stephen Smalley | 219f081 | 2005-04-18 10:47:35 -0700 | [diff] [blame] | 731 | audit_log_task_info(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
David Woodhouse | 7551ced | 2005-05-26 12:04:57 +0100 | [diff] [blame] | 734 | for (aux = context->aux; aux; aux = aux->next) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 735 | |
| 736 | ab = audit_log_start(context, aux->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (!ab) |
| 738 | continue; /* audit_panic has been called */ |
| 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | switch (aux->type) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 741 | case AUDIT_IPC: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | struct audit_aux_data_ipcctl *axi = (void *)aux; |
| 743 | audit_log_format(ab, |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 744 | " qbytes=%lx iuid=%u igid=%u mode=%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | axi->qbytes, axi->uid, axi->gid, axi->mode); |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 746 | break; } |
| 747 | |
| 748 | case AUDIT_SOCKETCALL: { |
| 749 | int i; |
| 750 | struct audit_aux_data_socketcall *axs = (void *)aux; |
| 751 | audit_log_format(ab, "nargs=%d", axs->nargs); |
| 752 | for (i=0; i<axs->nargs; i++) |
| 753 | audit_log_format(ab, " a%d=%lx", i, axs->args[i]); |
| 754 | break; } |
| 755 | |
| 756 | case AUDIT_SOCKADDR: { |
| 757 | struct audit_aux_data_sockaddr *axs = (void *)aux; |
| 758 | |
| 759 | audit_log_format(ab, "saddr="); |
| 760 | audit_log_hex(ab, axs->a, axs->len); |
| 761 | break; } |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 762 | |
| 763 | case AUDIT_AVC_PATH: { |
| 764 | struct audit_aux_data_path *axi = (void *)aux; |
| 765 | audit_log_d_path(ab, "path=", axi->dentry, axi->mnt); |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 766 | break; } |
| 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | audit_log_end(ab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
| 771 | |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 772 | if (context->pwd && context->pwdmnt) { |
| 773 | ab = audit_log_start(context, AUDIT_CWD); |
| 774 | if (ab) { |
| 775 | audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt); |
| 776 | audit_log_end(ab); |
| 777 | } |
| 778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | for (i = 0; i < context->name_count; i++) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 780 | ab = audit_log_start(context, AUDIT_PATH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (!ab) |
| 782 | continue; /* audit_panic has been called */ |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | audit_log_format(ab, "item=%d", i); |
| 83c7d09 | 2005-04-29 15:54:44 +0100 | [diff] [blame] | 785 | if (context->names[i].name) { |
| 786 | audit_log_format(ab, " name="); |
| 787 | audit_log_untrustedstring(ab, context->names[i].name); |
| 788 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | if (context->names[i].ino != (unsigned long)-1) |
| 790 | audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o" |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 791 | " ouid=%u ogid=%u rdev=%02x:%02x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | context->names[i].ino, |
| 793 | MAJOR(context->names[i].dev), |
| 794 | MINOR(context->names[i].dev), |
| 795 | context->names[i].mode, |
| 796 | context->names[i].uid, |
| 797 | context->names[i].gid, |
| 798 | MAJOR(context->names[i].rdev), |
| 799 | MINOR(context->names[i].rdev)); |
| 800 | audit_log_end(ab); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* Free a per-task audit context. Called from copy_process and |
| 805 | * __put_task_struct. */ |
| 806 | void audit_free(struct task_struct *tsk) |
| 807 | { |
| 808 | struct audit_context *context; |
| 809 | |
| 810 | task_lock(tsk); |
| 811 | context = audit_get_context(tsk, 0, 0); |
| 812 | task_unlock(tsk); |
| 813 | |
| 814 | if (likely(!context)) |
| 815 | return; |
| 816 | |
| 817 | /* Check for system calls that do not go through the exit |
| 818 | * function (e.g., exit_group), then free context block. */ |
David Woodhouse | 7ca0026 | 2005-05-19 11:23:13 +0100 | [diff] [blame] | 819 | if (context->in_syscall && context->auditable && context->pid != audit_pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | audit_log_exit(context); |
| 821 | |
| 822 | audit_free_context(context); |
| 823 | } |
| 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | /* Fill in audit context at syscall entry. This only happens if the |
| 826 | * audit context was created when the task was created and the state or |
| 827 | * filters demand the audit context be built. If the state from the |
| 828 | * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT, |
| 829 | * then the record will be written at syscall exit time (otherwise, it |
| 830 | * will only be written if another part of the kernel requests that it |
| 831 | * be written). */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 832 | void audit_syscall_entry(struct task_struct *tsk, int arch, int major, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | unsigned long a1, unsigned long a2, |
| 834 | unsigned long a3, unsigned long a4) |
| 835 | { |
| 836 | struct audit_context *context = tsk->audit_context; |
| 837 | enum audit_state state; |
| 838 | |
| 839 | BUG_ON(!context); |
| 840 | |
| 841 | /* This happens only on certain architectures that make system |
| 842 | * calls in kernel_thread via the entry.S interface, instead of |
| 843 | * with direct calls. (If you are porting to a new |
| 844 | * architecture, hitting this condition can indicate that you |
| 845 | * got the _exit/_leave calls backward in entry.S.) |
| 846 | * |
| 847 | * i386 no |
| 848 | * x86_64 no |
| 849 | * ppc64 yes (see arch/ppc64/kernel/misc.S) |
| 850 | * |
| 851 | * This also happens with vm86 emulation in a non-nested manner |
| 852 | * (entries without exits), so this case must be caught. |
| 853 | */ |
| 854 | if (context->in_syscall) { |
| 855 | struct audit_context *newctx; |
| 856 | |
| 857 | #if defined(__NR_vm86) && defined(__NR_vm86old) |
| 858 | /* vm86 mode should only be entered once */ |
| 859 | if (major == __NR_vm86 || major == __NR_vm86old) |
| 860 | return; |
| 861 | #endif |
| 862 | #if AUDIT_DEBUG |
| 863 | printk(KERN_ERR |
| 864 | "audit(:%d) pid=%d in syscall=%d;" |
| 865 | " entering syscall=%d\n", |
| 866 | context->serial, tsk->pid, context->major, major); |
| 867 | #endif |
| 868 | newctx = audit_alloc_context(context->state); |
| 869 | if (newctx) { |
| 870 | newctx->previous = context; |
| 871 | context = newctx; |
| 872 | tsk->audit_context = newctx; |
| 873 | } else { |
| 874 | /* If we can't alloc a new context, the best we |
| 875 | * can do is to leak memory (any pending putname |
| 876 | * will be lost). The only other alternative is |
| 877 | * to abandon auditing. */ |
| 878 | audit_zero_context(context, context->state); |
| 879 | } |
| 880 | } |
| 881 | BUG_ON(context->in_syscall || context->name_count); |
| 882 | |
| 883 | if (!audit_enabled) |
| 884 | return; |
| 885 | |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 886 | context->arch = arch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | context->major = major; |
| 888 | context->argv[0] = a1; |
| 889 | context->argv[1] = a2; |
| 890 | context->argv[2] = a3; |
| 891 | context->argv[3] = a4; |
| 892 | |
| 893 | state = context->state; |
| 894 | if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT) |
David Woodhouse | 0f45aa1 | 2005-06-19 19:35:50 +0100 | [diff] [blame^] | 895 | state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | if (likely(state == AUDIT_DISABLED)) |
| 897 | return; |
| 898 | |
| 899 | context->serial = audit_serial(); |
| 900 | context->ctime = CURRENT_TIME; |
| 901 | context->in_syscall = 1; |
| 902 | context->auditable = !!(state == AUDIT_RECORD_CONTEXT); |
| 903 | } |
| 904 | |
| 905 | /* Tear down after system call. If the audit context has been marked as |
| 906 | * auditable (either because of the AUDIT_RECORD_CONTEXT state from |
| 907 | * filtering, or because some other part of the kernel write an audit |
| 908 | * message), then write out the syscall information. In call cases, |
| 909 | * free the names stored from getname(). */ |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 910 | 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] | 911 | { |
| 912 | struct audit_context *context; |
| 913 | |
| 914 | get_task_struct(tsk); |
| 915 | task_lock(tsk); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 916 | context = audit_get_context(tsk, valid, return_code); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | task_unlock(tsk); |
| 918 | |
| 919 | /* Not having a context here is ok, since the parent may have |
| 920 | * called __put_task_struct. */ |
| 921 | if (likely(!context)) |
| 922 | return; |
| 923 | |
David Woodhouse | 7ca0026 | 2005-05-19 11:23:13 +0100 | [diff] [blame] | 924 | if (context->in_syscall && context->auditable && context->pid != audit_pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | audit_log_exit(context); |
| 926 | |
| 927 | context->in_syscall = 0; |
| 928 | context->auditable = 0; |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | if (context->previous) { |
| 931 | struct audit_context *new_context = context->previous; |
| 932 | context->previous = NULL; |
| 933 | audit_free_context(context); |
| 934 | tsk->audit_context = new_context; |
| 935 | } else { |
| 936 | audit_free_names(context); |
| 937 | audit_free_aux(context); |
| 938 | audit_zero_context(context, context->state); |
| 939 | tsk->audit_context = context; |
| 940 | } |
| 941 | put_task_struct(tsk); |
| 942 | } |
| 943 | |
| 944 | /* Add a name to the list. Called from fs/namei.c:getname(). */ |
| 945 | void audit_getname(const char *name) |
| 946 | { |
| 947 | struct audit_context *context = current->audit_context; |
| 948 | |
| 949 | if (!context || IS_ERR(name) || !name) |
| 950 | return; |
| 951 | |
| 952 | if (!context->in_syscall) { |
| 953 | #if AUDIT_DEBUG == 2 |
| 954 | printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n", |
| 955 | __FILE__, __LINE__, context->serial, name); |
| 956 | dump_stack(); |
| 957 | #endif |
| 958 | return; |
| 959 | } |
| 960 | BUG_ON(context->name_count >= AUDIT_NAMES); |
| 961 | context->names[context->name_count].name = name; |
| 962 | context->names[context->name_count].ino = (unsigned long)-1; |
| 963 | ++context->name_count; |
David Woodhouse | 8f37d47 | 2005-05-27 12:17:28 +0100 | [diff] [blame] | 964 | if (!context->pwd) { |
| 965 | read_lock(¤t->fs->lock); |
| 966 | context->pwd = dget(current->fs->pwd); |
| 967 | context->pwdmnt = mntget(current->fs->pwdmnt); |
| 968 | read_unlock(¤t->fs->lock); |
| 969 | } |
| 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* Intercept a putname request. Called from |
| 974 | * include/linux/fs.h:putname(). If we have stored the name from |
| 975 | * getname in the audit context, then we delay the putname until syscall |
| 976 | * exit. */ |
| 977 | void audit_putname(const char *name) |
| 978 | { |
| 979 | struct audit_context *context = current->audit_context; |
| 980 | |
| 981 | BUG_ON(!context); |
| 982 | if (!context->in_syscall) { |
| 983 | #if AUDIT_DEBUG == 2 |
| 984 | printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n", |
| 985 | __FILE__, __LINE__, context->serial, name); |
| 986 | if (context->name_count) { |
| 987 | int i; |
| 988 | for (i = 0; i < context->name_count; i++) |
| 989 | printk(KERN_ERR "name[%d] = %p = %s\n", i, |
| 990 | context->names[i].name, |
| 991 | context->names[i].name); |
| 992 | } |
| 993 | #endif |
| 994 | __putname(name); |
| 995 | } |
| 996 | #if AUDIT_DEBUG |
| 997 | else { |
| 998 | ++context->put_count; |
| 999 | if (context->put_count > context->name_count) { |
| 1000 | printk(KERN_ERR "%s:%d(:%d): major=%d" |
| 1001 | " in_syscall=%d putname(%p) name_count=%d" |
| 1002 | " put_count=%d\n", |
| 1003 | __FILE__, __LINE__, |
| 1004 | context->serial, context->major, |
| 1005 | context->in_syscall, name, context->name_count, |
| 1006 | context->put_count); |
| 1007 | dump_stack(); |
| 1008 | } |
| 1009 | } |
| 1010 | #endif |
| 1011 | } |
| 1012 | |
| 1013 | /* Store the inode and device from a lookup. Called from |
| 1014 | * fs/namei.c:path_lookup(). */ |
| 1015 | void audit_inode(const char *name, const struct inode *inode) |
| 1016 | { |
| 1017 | int idx; |
| 1018 | struct audit_context *context = current->audit_context; |
| 1019 | |
| 1020 | if (!context->in_syscall) |
| 1021 | return; |
| 1022 | if (context->name_count |
| 1023 | && context->names[context->name_count-1].name |
| 1024 | && context->names[context->name_count-1].name == name) |
| 1025 | idx = context->name_count - 1; |
| 1026 | else if (context->name_count > 1 |
| 1027 | && context->names[context->name_count-2].name |
| 1028 | && context->names[context->name_count-2].name == name) |
| 1029 | idx = context->name_count - 2; |
| 1030 | else { |
| 1031 | /* FIXME: how much do we care about inodes that have no |
| 1032 | * associated name? */ |
| 1033 | if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED) |
| 1034 | return; |
| 1035 | idx = context->name_count++; |
| 1036 | context->names[idx].name = NULL; |
| 1037 | #if AUDIT_DEBUG |
| 1038 | ++context->ino_count; |
| 1039 | #endif |
| 1040 | } |
| 1041 | context->names[idx].ino = inode->i_ino; |
| 1042 | context->names[idx].dev = inode->i_sb->s_dev; |
| 1043 | context->names[idx].mode = inode->i_mode; |
| 1044 | context->names[idx].uid = inode->i_uid; |
| 1045 | context->names[idx].gid = inode->i_gid; |
| 1046 | context->names[idx].rdev = inode->i_rdev; |
| 1047 | } |
| 1048 | |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1049 | void auditsc_get_stamp(struct audit_context *ctx, |
| 1050 | struct timespec *t, unsigned int *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | { |
David Woodhouse | bfb4496 | 2005-05-21 21:08:09 +0100 | [diff] [blame] | 1052 | t->tv_sec = ctx->ctime.tv_sec; |
| 1053 | t->tv_nsec = ctx->ctime.tv_nsec; |
| 1054 | *serial = ctx->serial; |
| 1055 | ctx->auditable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1058 | int audit_set_loginuid(struct task_struct *task, uid_t loginuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1060 | if (task->audit_context) { |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1061 | struct audit_buffer *ab; |
| 1062 | |
| 1063 | ab = audit_log_start(NULL, AUDIT_LOGIN); |
| 1064 | if (ab) { |
| 1065 | audit_log_format(ab, "login pid=%d uid=%u " |
Steve Grubb | 326e9c8 | 2005-05-21 00:22:31 +0100 | [diff] [blame] | 1066 | "old auid=%u new auid=%u", |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1067 | task->pid, task->uid, |
| 1068 | task->audit_context->loginuid, loginuid); |
| 1069 | audit_log_end(ab); |
| 1070 | } |
Steve Grubb | 456be6c | 2005-04-29 17:30:07 +0100 | [diff] [blame] | 1071 | task->audit_context->loginuid = loginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | } |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | uid_t audit_get_loginuid(struct audit_context *ctx) |
| 1077 | { |
| 1078 | return ctx ? ctx->loginuid : -1; |
| 1079 | } |
| 1080 | |
| 1081 | int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode) |
| 1082 | { |
| 1083 | struct audit_aux_data_ipcctl *ax; |
| 1084 | struct audit_context *context = current->audit_context; |
| 1085 | |
| 1086 | if (likely(!context)) |
| 1087 | return 0; |
| 1088 | |
| 1089 | ax = kmalloc(sizeof(*ax), GFP_KERNEL); |
| 1090 | if (!ax) |
| 1091 | return -ENOMEM; |
| 1092 | |
| 1093 | ax->qbytes = qbytes; |
| 1094 | ax->uid = uid; |
| 1095 | ax->gid = gid; |
| 1096 | ax->mode = mode; |
| 1097 | |
Steve Grubb | c040499 | 2005-05-13 18:17:42 +0100 | [diff] [blame] | 1098 | ax->d.type = AUDIT_IPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | ax->d.next = context->aux; |
| 1100 | context->aux = (void *)ax; |
| 1101 | return 0; |
| 1102 | } |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1103 | |
David Woodhouse | 3ec3b2f | 2005-05-17 12:08:48 +0100 | [diff] [blame] | 1104 | int audit_socketcall(int nargs, unsigned long *args) |
| 1105 | { |
| 1106 | struct audit_aux_data_socketcall *ax; |
| 1107 | struct audit_context *context = current->audit_context; |
| 1108 | |
| 1109 | if (likely(!context)) |
| 1110 | return 0; |
| 1111 | |
| 1112 | ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL); |
| 1113 | if (!ax) |
| 1114 | return -ENOMEM; |
| 1115 | |
| 1116 | ax->nargs = nargs; |
| 1117 | memcpy(ax->args, args, nargs * sizeof(unsigned long)); |
| 1118 | |
| 1119 | ax->d.type = AUDIT_SOCKETCALL; |
| 1120 | ax->d.next = context->aux; |
| 1121 | context->aux = (void *)ax; |
| 1122 | return 0; |
| 1123 | } |
| 1124 | |
| 1125 | int audit_sockaddr(int len, void *a) |
| 1126 | { |
| 1127 | struct audit_aux_data_sockaddr *ax; |
| 1128 | struct audit_context *context = current->audit_context; |
| 1129 | |
| 1130 | if (likely(!context)) |
| 1131 | return 0; |
| 1132 | |
| 1133 | ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL); |
| 1134 | if (!ax) |
| 1135 | return -ENOMEM; |
| 1136 | |
| 1137 | ax->len = len; |
| 1138 | memcpy(ax->a, a, len); |
| 1139 | |
| 1140 | ax->d.type = AUDIT_SOCKADDR; |
| 1141 | ax->d.next = context->aux; |
| 1142 | context->aux = (void *)ax; |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
Stephen Smalley | 0111610 | 2005-05-21 00:15:52 +0100 | [diff] [blame] | 1146 | int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt) |
| 1147 | { |
| 1148 | struct audit_aux_data_path *ax; |
| 1149 | struct audit_context *context = current->audit_context; |
| 1150 | |
| 1151 | if (likely(!context)) |
| 1152 | return 0; |
| 1153 | |
| 1154 | ax = kmalloc(sizeof(*ax), GFP_ATOMIC); |
| 1155 | if (!ax) |
| 1156 | return -ENOMEM; |
| 1157 | |
| 1158 | ax->dentry = dget(dentry); |
| 1159 | ax->mnt = mntget(mnt); |
| 1160 | |
| 1161 | ax->d.type = AUDIT_AVC_PATH; |
| 1162 | ax->d.next = context->aux; |
| 1163 | context->aux = (void *)ax; |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1167 | void audit_signal_info(int sig, struct task_struct *t) |
| 1168 | { |
| 1169 | extern pid_t audit_sig_pid; |
| 1170 | extern uid_t audit_sig_uid; |
Steve Grubb | c2f0c7c | 2005-05-06 12:38:39 +0100 | [diff] [blame] | 1171 | |
| 1172 | if (unlikely(audit_pid && t->pid == audit_pid)) { |
| 1173 | if (sig == SIGTERM || sig == SIGHUP) { |
| 1174 | struct audit_context *ctx = current->audit_context; |
| 1175 | audit_sig_pid = current->pid; |
| 1176 | if (ctx) |
| 1177 | audit_sig_uid = ctx->loginuid; |
| 1178 | else |
| 1179 | audit_sig_uid = current->uid; |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | |