1 /* auditsc.c -- System-call auditing support 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> 37 #include <linux/mount.h> 38 #include <linux/socket.h> 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 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 128 struct audit_aux_data_path { 129 struct audit_aux_data d; 130 struct dentry *dentry; 131 struct vfsmount *mnt; 132 }; 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 */ 144 long return_code;/* syscall return code */ 145 int auditable; /* 1 if record should be written */ 146 int name_count; 147 struct audit_names names[AUDIT_NAMES]; 148 struct dentry * pwd; 149 struct vfsmount * pwdmnt; 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; 158 int arch; 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. */ 170 static LIST_HEAD(audit_tsklist); 171 static LIST_HEAD(audit_entlist); 172 static LIST_HEAD(audit_extlist); 173 174 struct audit_entry { 175 struct list_head list; 176 struct rcu_head rcu; 177 struct audit_rule rule; 178 }; 179 180 extern int audit_pid; 181 182 /* Check to see if two rules are identical. It is called from 183 * audit_del_rule during AUDIT_DEL. */ 184 static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b) 185 { 186 int i; 187 188 if (a->flags != b->flags) 189 return 1; 190 191 if (a->action != b->action) 192 return 1; 193 194 if (a->field_count != b->field_count) 195 return 1; 196 197 for (i = 0; i < a->field_count; i++) { 198 if (a->fields[i] != b->fields[i] 199 || a->values[i] != b->values[i]) 200 return 1; 201 } 202 203 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) 204 if (a->mask[i] != b->mask[i]) 205 return 1; 206 207 return 0; 208 } 209 210 /* Note that audit_add_rule and audit_del_rule are called via 211 * audit_receive() in audit.c, and are protected by 212 * audit_netlink_sem. */ 213 static inline int audit_add_rule(struct audit_entry *entry, 214 struct list_head *list) 215 { 216 if (entry->rule.flags & AUDIT_PREPEND) { 217 entry->rule.flags &= ~AUDIT_PREPEND; 218 list_add_rcu(&entry->list, list); 219 } else { 220 list_add_tail_rcu(&entry->list, list); 221 } 222 return 0; 223 } 224 225 static void audit_free_rule(struct rcu_head *head) 226 { 227 struct audit_entry *e = container_of(head, struct audit_entry, rcu); 228 kfree(e); 229 } 230 231 /* Note that audit_add_rule and audit_del_rule are called via 232 * audit_receive() in audit.c, and are protected by 233 * audit_netlink_sem. */ 234 static inline int audit_del_rule(struct audit_rule *rule, 235 struct list_head *list) 236 { 237 struct audit_entry *e; 238 239 /* Do not use the _rcu iterator here, since this is the only 240 * deletion routine. */ 241 list_for_each_entry(e, list, list) { 242 if (!audit_compare_rule(rule, &e->rule)) { 243 list_del_rcu(&e->list); 244 call_rcu(&e->rcu, audit_free_rule); 245 return 0; 246 } 247 } 248 return -EFAULT; /* No matching rule */ 249 } 250 251 /* Copy rule from user-space to kernel-space. Called during 252 * AUDIT_ADD. */ 253 static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s) 254 { 255 int i; 256 257 if (s->action != AUDIT_NEVER 258 && s->action != AUDIT_POSSIBLE 259 && s->action != AUDIT_ALWAYS) 260 return -1; 261 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS) 262 return -1; 263 264 d->flags = s->flags; 265 d->action = s->action; 266 d->field_count = s->field_count; 267 for (i = 0; i < d->field_count; i++) { 268 d->fields[i] = s->fields[i]; 269 d->values[i] = s->values[i]; 270 } 271 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i]; 272 return 0; 273 } 274 275 int audit_receive_filter(int type, int pid, int uid, int seq, void *data, 276 uid_t loginuid) 277 { 278 u32 flags; 279 struct audit_entry *entry; 280 int err = 0; 281 282 switch (type) { 283 case AUDIT_LIST: 284 /* The *_rcu iterators not needed here because we are 285 always called with audit_netlink_sem held. */ 286 list_for_each_entry(entry, &audit_tsklist, list) 287 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1, 288 &entry->rule, sizeof(entry->rule)); 289 list_for_each_entry(entry, &audit_entlist, list) 290 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1, 291 &entry->rule, sizeof(entry->rule)); 292 list_for_each_entry(entry, &audit_extlist, list) 293 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1, 294 &entry->rule, sizeof(entry->rule)); 295 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0); 296 break; 297 case AUDIT_ADD: 298 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL))) 299 return -ENOMEM; 300 if (audit_copy_rule(&entry->rule, data)) { 301 kfree(entry); 302 return -EINVAL; 303 } 304 flags = entry->rule.flags; 305 if (!err && (flags & AUDIT_PER_TASK)) 306 err = audit_add_rule(entry, &audit_tsklist); 307 if (!err && (flags & AUDIT_AT_ENTRY)) 308 err = audit_add_rule(entry, &audit_entlist); 309 if (!err && (flags & AUDIT_AT_EXIT)) 310 err = audit_add_rule(entry, &audit_extlist); 311 audit_log(NULL, AUDIT_CONFIG_CHANGE, 312 "auid=%u added an audit rule\n", loginuid); 313 break; 314 case AUDIT_DEL: 315 flags =((struct audit_rule *)data)->flags; 316 if (!err && (flags & AUDIT_PER_TASK)) 317 err = audit_del_rule(data, &audit_tsklist); 318 if (!err && (flags & AUDIT_AT_ENTRY)) 319 err = audit_del_rule(data, &audit_entlist); 320 if (!err && (flags & AUDIT_AT_EXIT)) 321 err = audit_del_rule(data, &audit_extlist); 322 audit_log(NULL, AUDIT_CONFIG_CHANGE, 323 "auid=%u removed an audit rule\n", loginuid); 324 break; 325 default: 326 return -EINVAL; 327 } 328 329 return err; 330 } 331 332 /* Compare a task_struct with an audit_rule. Return 1 on match, 0 333 * otherwise. */ 334 static int audit_filter_rules(struct task_struct *tsk, 335 struct audit_rule *rule, 336 struct audit_context *ctx, 337 enum audit_state *state) 338 { 339 int i, j; 340 341 for (i = 0; i < rule->field_count; i++) { 342 u32 field = rule->fields[i] & ~AUDIT_NEGATE; 343 u32 value = rule->values[i]; 344 int result = 0; 345 346 switch (field) { 347 case AUDIT_PID: 348 result = (tsk->pid == value); 349 break; 350 case AUDIT_UID: 351 result = (tsk->uid == value); 352 break; 353 case AUDIT_EUID: 354 result = (tsk->euid == value); 355 break; 356 case AUDIT_SUID: 357 result = (tsk->suid == value); 358 break; 359 case AUDIT_FSUID: 360 result = (tsk->fsuid == value); 361 break; 362 case AUDIT_GID: 363 result = (tsk->gid == value); 364 break; 365 case AUDIT_EGID: 366 result = (tsk->egid == value); 367 break; 368 case AUDIT_SGID: 369 result = (tsk->sgid == value); 370 break; 371 case AUDIT_FSGID: 372 result = (tsk->fsgid == value); 373 break; 374 case AUDIT_PERS: 375 result = (tsk->personality == value); 376 break; 377 case AUDIT_ARCH: 378 if (ctx) 379 result = (ctx->arch == value); 380 break; 381 382 case AUDIT_EXIT: 383 if (ctx && ctx->return_valid) 384 result = (ctx->return_code == value); 385 break; 386 case AUDIT_SUCCESS: 387 if (ctx && ctx->return_valid) 388 result = (ctx->return_valid == AUDITSC_SUCCESS); 389 break; 390 case AUDIT_DEVMAJOR: 391 if (ctx) { 392 for (j = 0; j < ctx->name_count; j++) { 393 if (MAJOR(ctx->names[j].dev)==value) { 394 ++result; 395 break; 396 } 397 } 398 } 399 break; 400 case AUDIT_DEVMINOR: 401 if (ctx) { 402 for (j = 0; j < ctx->name_count; j++) { 403 if (MINOR(ctx->names[j].dev)==value) { 404 ++result; 405 break; 406 } 407 } 408 } 409 break; 410 case AUDIT_INODE: 411 if (ctx) { 412 for (j = 0; j < ctx->name_count; j++) { 413 if (ctx->names[j].ino == value) { 414 ++result; 415 break; 416 } 417 } 418 } 419 break; 420 case AUDIT_LOGINUID: 421 result = 0; 422 if (ctx) 423 result = (ctx->loginuid == value); 424 break; 425 case AUDIT_ARG0: 426 case AUDIT_ARG1: 427 case AUDIT_ARG2: 428 case AUDIT_ARG3: 429 if (ctx) 430 result = (ctx->argv[field-AUDIT_ARG0]==value); 431 break; 432 } 433 434 if (rule->fields[i] & AUDIT_NEGATE) 435 result = !result; 436 if (!result) 437 return 0; 438 } 439 switch (rule->action) { 440 case AUDIT_NEVER: *state = AUDIT_DISABLED; break; 441 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break; 442 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; 443 } 444 return 1; 445 } 446 447 /* At process creation time, we can determine if system-call auditing is 448 * completely disabled for this task. Since we only have the task 449 * structure at this point, we can only check uid and gid. 450 */ 451 static enum audit_state audit_filter_task(struct task_struct *tsk) 452 { 453 struct audit_entry *e; 454 enum audit_state state; 455 456 rcu_read_lock(); 457 list_for_each_entry_rcu(e, &audit_tsklist, list) { 458 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) { 459 rcu_read_unlock(); 460 return state; 461 } 462 } 463 rcu_read_unlock(); 464 return AUDIT_BUILD_CONTEXT; 465 } 466 467 /* At syscall entry and exit time, this filter is called if the 468 * audit_state is not low enough that auditing cannot take place, but is 469 * also not high enough that we already know we have to write an audit 470 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT). 471 */ 472 static enum audit_state audit_filter_syscall(struct task_struct *tsk, 473 struct audit_context *ctx, 474 struct list_head *list) 475 { 476 struct audit_entry *e; 477 enum audit_state state; 478 int word = AUDIT_WORD(ctx->major); 479 int bit = AUDIT_BIT(ctx->major); 480 481 rcu_read_lock(); 482 list_for_each_entry_rcu(e, list, list) { 483 if ((e->rule.mask[word] & bit) == bit 484 && audit_filter_rules(tsk, &e->rule, ctx, &state)) { 485 rcu_read_unlock(); 486 return state; 487 } 488 } 489 rcu_read_unlock(); 490 return AUDIT_BUILD_CONTEXT; 491 } 492 493 /* This should be called with task_lock() held. */ 494 static inline struct audit_context *audit_get_context(struct task_struct *tsk, 495 int return_valid, 496 int return_code) 497 { 498 struct audit_context *context = tsk->audit_context; 499 500 if (likely(!context)) 501 return NULL; 502 context->return_valid = return_valid; 503 context->return_code = return_code; 504 505 if (context->in_syscall && !context->auditable) { 506 enum audit_state state; 507 state = audit_filter_syscall(tsk, context, &audit_extlist); 508 if (state == AUDIT_RECORD_CONTEXT) 509 context->auditable = 1; 510 } 511 512 context->pid = tsk->pid; 513 context->uid = tsk->uid; 514 context->gid = tsk->gid; 515 context->euid = tsk->euid; 516 context->suid = tsk->suid; 517 context->fsuid = tsk->fsuid; 518 context->egid = tsk->egid; 519 context->sgid = tsk->sgid; 520 context->fsgid = tsk->fsgid; 521 context->personality = tsk->personality; 522 tsk->audit_context = NULL; 523 return context; 524 } 525 526 static inline void audit_free_names(struct audit_context *context) 527 { 528 int i; 529 530 #if AUDIT_DEBUG == 2 531 if (context->auditable 532 ||context->put_count + context->ino_count != context->name_count) { 533 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d" 534 " name_count=%d put_count=%d" 535 " ino_count=%d [NOT freeing]\n", 536 __LINE__, 537 context->serial, context->major, context->in_syscall, 538 context->name_count, context->put_count, 539 context->ino_count); 540 for (i = 0; i < context->name_count; i++) 541 printk(KERN_ERR "names[%d] = %p = %s\n", i, 542 context->names[i].name, 543 context->names[i].name); 544 dump_stack(); 545 return; 546 } 547 #endif 548 #if AUDIT_DEBUG 549 context->put_count = 0; 550 context->ino_count = 0; 551 #endif 552 553 for (i = 0; i < context->name_count; i++) 554 if (context->names[i].name) 555 __putname(context->names[i].name); 556 context->name_count = 0; 557 if (context->pwd) 558 dput(context->pwd); 559 if (context->pwdmnt) 560 mntput(context->pwdmnt); 561 context->pwd = NULL; 562 context->pwdmnt = NULL; 563 } 564 565 static inline void audit_free_aux(struct audit_context *context) 566 { 567 struct audit_aux_data *aux; 568 569 while ((aux = context->aux)) { 570 if (aux->type == AUDIT_AVC_PATH) { 571 struct audit_aux_data_path *axi = (void *)aux; 572 dput(axi->dentry); 573 mntput(axi->mnt); 574 } 575 context->aux = aux->next; 576 kfree(aux); 577 } 578 } 579 580 static inline void audit_zero_context(struct audit_context *context, 581 enum audit_state state) 582 { 583 uid_t loginuid = context->loginuid; 584 585 memset(context, 0, sizeof(*context)); 586 context->state = state; 587 context->loginuid = loginuid; 588 } 589 590 static inline struct audit_context *audit_alloc_context(enum audit_state state) 591 { 592 struct audit_context *context; 593 594 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL))) 595 return NULL; 596 audit_zero_context(context, state); 597 return context; 598 } 599 600 /* Filter on the task information and allocate a per-task audit context 601 * if necessary. Doing so turns on system call auditing for the 602 * specified task. This is called from copy_process, so no lock is 603 * needed. */ 604 int audit_alloc(struct task_struct *tsk) 605 { 606 struct audit_context *context; 607 enum audit_state state; 608 609 if (likely(!audit_enabled)) 610 return 0; /* Return if not auditing. */ 611 612 state = audit_filter_task(tsk); 613 if (likely(state == AUDIT_DISABLED)) 614 return 0; 615 616 if (!(context = audit_alloc_context(state))) { 617 audit_log_lost("out of memory in audit_alloc"); 618 return -ENOMEM; 619 } 620 621 /* Preserve login uid */ 622 context->loginuid = -1; 623 if (current->audit_context) 624 context->loginuid = current->audit_context->loginuid; 625 626 tsk->audit_context = context; 627 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT); 628 return 0; 629 } 630 631 static inline void audit_free_context(struct audit_context *context) 632 { 633 struct audit_context *previous; 634 int count = 0; 635 636 do { 637 previous = context->previous; 638 if (previous || (count && count < 10)) { 639 ++count; 640 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:" 641 " freeing multiple contexts (%d)\n", 642 context->serial, context->major, 643 context->name_count, count); 644 } 645 audit_free_names(context); 646 audit_free_aux(context); 647 kfree(context); 648 context = previous; 649 } while (context); 650 if (count >= 10) 651 printk(KERN_ERR "audit: freed %d contexts\n", count); 652 } 653 654 static void audit_log_task_info(struct audit_buffer *ab) 655 { 656 char name[sizeof(current->comm)]; 657 struct mm_struct *mm = current->mm; 658 struct vm_area_struct *vma; 659 660 get_task_comm(name, current); 661 audit_log_format(ab, " comm="); 662 audit_log_untrustedstring(ab, name); 663 664 if (!mm) 665 return; 666 667 down_read(&mm->mmap_sem); 668 vma = mm->mmap; 669 while (vma) { 670 if ((vma->vm_flags & VM_EXECUTABLE) && 671 vma->vm_file) { 672 audit_log_d_path(ab, "exe=", 673 vma->vm_file->f_dentry, 674 vma->vm_file->f_vfsmnt); 675 break; 676 } 677 vma = vma->vm_next; 678 } 679 up_read(&mm->mmap_sem); 680 } 681 682 static void audit_log_exit(struct audit_context *context) 683 { 684 int i; 685 struct audit_buffer *ab; 686 struct audit_aux_data *aux; 687 688 ab = audit_log_start(context, AUDIT_SYSCALL); 689 if (!ab) 690 return; /* audit_panic has been called */ 691 audit_log_format(ab, "arch=%x syscall=%d", 692 context->arch, context->major); 693 if (context->personality != PER_LINUX) 694 audit_log_format(ab, " per=%lx", context->personality); 695 if (context->return_valid) 696 audit_log_format(ab, " success=%s exit=%ld", 697 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no", 698 context->return_code); 699 audit_log_format(ab, 700 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d" 701 " pid=%d auid=%u uid=%u gid=%u" 702 " euid=%u suid=%u fsuid=%u" 703 " egid=%u sgid=%u fsgid=%u", 704 context->argv[0], 705 context->argv[1], 706 context->argv[2], 707 context->argv[3], 708 context->name_count, 709 context->pid, 710 context->loginuid, 711 context->uid, 712 context->gid, 713 context->euid, context->suid, context->fsuid, 714 context->egid, context->sgid, context->fsgid); 715 audit_log_task_info(ab); 716 audit_log_end(ab); 717 718 for (aux = context->aux; aux; aux = aux->next) { 719 720 ab = audit_log_start(context, aux->type); 721 if (!ab) 722 continue; /* audit_panic has been called */ 723 724 switch (aux->type) { 725 case AUDIT_IPC: { 726 struct audit_aux_data_ipcctl *axi = (void *)aux; 727 audit_log_format(ab, 728 " qbytes=%lx iuid=%u igid=%u mode=%x", 729 axi->qbytes, axi->uid, axi->gid, axi->mode); 730 break; } 731 732 case AUDIT_SOCKETCALL: { 733 int i; 734 struct audit_aux_data_socketcall *axs = (void *)aux; 735 audit_log_format(ab, "nargs=%d", axs->nargs); 736 for (i=0; i<axs->nargs; i++) 737 audit_log_format(ab, " a%d=%lx", i, axs->args[i]); 738 break; } 739 740 case AUDIT_SOCKADDR: { 741 struct audit_aux_data_sockaddr *axs = (void *)aux; 742 743 audit_log_format(ab, "saddr="); 744 audit_log_hex(ab, axs->a, axs->len); 745 break; } 746 747 case AUDIT_AVC_PATH: { 748 struct audit_aux_data_path *axi = (void *)aux; 749 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt); 750 break; } 751 752 } 753 audit_log_end(ab); 754 } 755 756 if (context->pwd && context->pwdmnt) { 757 ab = audit_log_start(context, AUDIT_CWD); 758 if (ab) { 759 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt); 760 audit_log_end(ab); 761 } 762 } 763 for (i = 0; i < context->name_count; i++) { 764 ab = audit_log_start(context, AUDIT_PATH); 765 if (!ab) 766 continue; /* audit_panic has been called */ 767 768 audit_log_format(ab, "item=%d", i); 769 if (context->names[i].name) { 770 audit_log_format(ab, " name="); 771 audit_log_untrustedstring(ab, context->names[i].name); 772 } 773 if (context->names[i].ino != (unsigned long)-1) 774 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o" 775 " ouid=%u ogid=%u rdev=%02x:%02x", 776 context->names[i].ino, 777 MAJOR(context->names[i].dev), 778 MINOR(context->names[i].dev), 779 context->names[i].mode, 780 context->names[i].uid, 781 context->names[i].gid, 782 MAJOR(context->names[i].rdev), 783 MINOR(context->names[i].rdev)); 784 audit_log_end(ab); 785 } 786 } 787 788 /* Free a per-task audit context. Called from copy_process and 789 * __put_task_struct. */ 790 void audit_free(struct task_struct *tsk) 791 { 792 struct audit_context *context; 793 794 task_lock(tsk); 795 context = audit_get_context(tsk, 0, 0); 796 task_unlock(tsk); 797 798 if (likely(!context)) 799 return; 800 801 /* Check for system calls that do not go through the exit 802 * function (e.g., exit_group), then free context block. */ 803 if (context->in_syscall && context->auditable && context->pid != audit_pid) 804 audit_log_exit(context); 805 806 audit_free_context(context); 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). */ 816 void audit_syscall_entry(struct task_struct *tsk, int arch, int major, 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 870 context->arch = arch; 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(). */ 894 void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code) 895 { 896 struct audit_context *context; 897 898 get_task_struct(tsk); 899 task_lock(tsk); 900 context = audit_get_context(tsk, valid, return_code); 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 908 if (context->in_syscall && context->auditable && context->pid != audit_pid) 909 audit_log_exit(context); 910 911 context->in_syscall = 0; 912 context->auditable = 0; 913 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 if (!context->pwd) { 949 read_lock(¤t->fs->lock); 950 context->pwd = dget(current->fs->pwd); 951 context->pwdmnt = mntget(current->fs->pwdmnt); 952 read_unlock(¤t->fs->lock); 953 } 954 955 } 956 957 /* Intercept a putname request. Called from 958 * include/linux/fs.h:putname(). If we have stored the name from 959 * getname in the audit context, then we delay the putname until syscall 960 * exit. */ 961 void audit_putname(const char *name) 962 { 963 struct audit_context *context = current->audit_context; 964 965 BUG_ON(!context); 966 if (!context->in_syscall) { 967 #if AUDIT_DEBUG == 2 968 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n", 969 __FILE__, __LINE__, context->serial, name); 970 if (context->name_count) { 971 int i; 972 for (i = 0; i < context->name_count; i++) 973 printk(KERN_ERR "name[%d] = %p = %s\n", i, 974 context->names[i].name, 975 context->names[i].name); 976 } 977 #endif 978 __putname(name); 979 } 980 #if AUDIT_DEBUG 981 else { 982 ++context->put_count; 983 if (context->put_count > context->name_count) { 984 printk(KERN_ERR "%s:%d(:%d): major=%d" 985 " in_syscall=%d putname(%p) name_count=%d" 986 " put_count=%d\n", 987 __FILE__, __LINE__, 988 context->serial, context->major, 989 context->in_syscall, name, context->name_count, 990 context->put_count); 991 dump_stack(); 992 } 993 } 994 #endif 995 } 996 997 /* Store the inode and device from a lookup. Called from 998 * fs/namei.c:path_lookup(). */ 999 void audit_inode(const char *name, const struct inode *inode) 1000 { 1001 int idx; 1002 struct audit_context *context = current->audit_context; 1003 1004 if (!context->in_syscall) 1005 return; 1006 if (context->name_count 1007 && context->names[context->name_count-1].name 1008 && context->names[context->name_count-1].name == name) 1009 idx = context->name_count - 1; 1010 else if (context->name_count > 1 1011 && context->names[context->name_count-2].name 1012 && context->names[context->name_count-2].name == name) 1013 idx = context->name_count - 2; 1014 else { 1015 /* FIXME: how much do we care about inodes that have no 1016 * associated name? */ 1017 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED) 1018 return; 1019 idx = context->name_count++; 1020 context->names[idx].name = NULL; 1021 #if AUDIT_DEBUG 1022 ++context->ino_count; 1023 #endif 1024 } 1025 context->names[idx].ino = inode->i_ino; 1026 context->names[idx].dev = inode->i_sb->s_dev; 1027 context->names[idx].mode = inode->i_mode; 1028 context->names[idx].uid = inode->i_uid; 1029 context->names[idx].gid = inode->i_gid; 1030 context->names[idx].rdev = inode->i_rdev; 1031 } 1032 1033 void auditsc_get_stamp(struct audit_context *ctx, 1034 struct timespec *t, unsigned int *serial) 1035 { 1036 t->tv_sec = ctx->ctime.tv_sec; 1037 t->tv_nsec = ctx->ctime.tv_nsec; 1038 *serial = ctx->serial; 1039 ctx->auditable = 1; 1040 } 1041 1042 int audit_set_loginuid(struct task_struct *task, uid_t loginuid) 1043 { 1044 if (task->audit_context) { 1045 struct audit_buffer *ab; 1046 1047 ab = audit_log_start(NULL, AUDIT_LOGIN); 1048 if (ab) { 1049 audit_log_format(ab, "login pid=%d uid=%u " 1050 "old auid=%u new auid=%u", 1051 task->pid, task->uid, 1052 task->audit_context->loginuid, loginuid); 1053 audit_log_end(ab); 1054 } 1055 task->audit_context->loginuid = loginuid; 1056 } 1057 return 0; 1058 } 1059 1060 uid_t audit_get_loginuid(struct audit_context *ctx) 1061 { 1062 return ctx ? ctx->loginuid : -1; 1063 } 1064 1065 int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode) 1066 { 1067 struct audit_aux_data_ipcctl *ax; 1068 struct audit_context *context = current->audit_context; 1069 1070 if (likely(!context)) 1071 return 0; 1072 1073 ax = kmalloc(sizeof(*ax), GFP_KERNEL); 1074 if (!ax) 1075 return -ENOMEM; 1076 1077 ax->qbytes = qbytes; 1078 ax->uid = uid; 1079 ax->gid = gid; 1080 ax->mode = mode; 1081 1082 ax->d.type = AUDIT_IPC; 1083 ax->d.next = context->aux; 1084 context->aux = (void *)ax; 1085 return 0; 1086 } 1087 1088 int audit_socketcall(int nargs, unsigned long *args) 1089 { 1090 struct audit_aux_data_socketcall *ax; 1091 struct audit_context *context = current->audit_context; 1092 1093 if (likely(!context)) 1094 return 0; 1095 1096 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL); 1097 if (!ax) 1098 return -ENOMEM; 1099 1100 ax->nargs = nargs; 1101 memcpy(ax->args, args, nargs * sizeof(unsigned long)); 1102 1103 ax->d.type = AUDIT_SOCKETCALL; 1104 ax->d.next = context->aux; 1105 context->aux = (void *)ax; 1106 return 0; 1107 } 1108 1109 int audit_sockaddr(int len, void *a) 1110 { 1111 struct audit_aux_data_sockaddr *ax; 1112 struct audit_context *context = current->audit_context; 1113 1114 if (likely(!context)) 1115 return 0; 1116 1117 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL); 1118 if (!ax) 1119 return -ENOMEM; 1120 1121 ax->len = len; 1122 memcpy(ax->a, a, len); 1123 1124 ax->d.type = AUDIT_SOCKADDR; 1125 ax->d.next = context->aux; 1126 context->aux = (void *)ax; 1127 return 0; 1128 } 1129 1130 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt) 1131 { 1132 struct audit_aux_data_path *ax; 1133 struct audit_context *context = current->audit_context; 1134 1135 if (likely(!context)) 1136 return 0; 1137 1138 ax = kmalloc(sizeof(*ax), GFP_ATOMIC); 1139 if (!ax) 1140 return -ENOMEM; 1141 1142 ax->dentry = dget(dentry); 1143 ax->mnt = mntget(mnt); 1144 1145 ax->d.type = AUDIT_AVC_PATH; 1146 ax->d.next = context->aux; 1147 context->aux = (void *)ax; 1148 return 0; 1149 } 1150 1151 void audit_signal_info(int sig, struct task_struct *t) 1152 { 1153 extern pid_t audit_sig_pid; 1154 extern uid_t audit_sig_uid; 1155 1156 if (unlikely(audit_pid && t->pid == audit_pid)) { 1157 if (sig == SIGTERM || sig == SIGHUP) { 1158 struct audit_context *ctx = current->audit_context; 1159 audit_sig_pid = current->pid; 1160 if (ctx) 1161 audit_sig_uid = ctx->loginuid; 1162 else 1163 audit_sig_uid = current->uid; 1164 } 1165 } 1166 } 1167 1168