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