1 /* auditsc.c -- System-call auditing support
2 * Handles all system-call specific auditing features.
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
5 * Copyright 2005 Hewlett-Packard Development Company, L.P.
6 * Copyright (C) 2005, 2006 IBM Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
32 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
35 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
38 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
45 #include <linux/init.h>
46 #include <asm/types.h>
47 #include <asm/atomic.h>
48 #include <asm/types.h>
50 #include <linux/namei.h>
52 #include <linux/module.h>
53 #include <linux/mount.h>
54 #include <linux/socket.h>
55 #include <linux/mqueue.h>
56 #include <linux/audit.h>
57 #include <linux/personality.h>
58 #include <linux/time.h>
59 #include <linux/netlink.h>
60 #include <linux/compiler.h>
61 #include <asm/unistd.h>
62 #include <linux/security.h>
63 #include <linux/list.h>
64 #include <linux/tty.h>
65 #include <linux/selinux.h>
66 #include <linux/binfmts.h>
67 #include <linux/syscalls.h>
71 extern struct list_head audit_filter_list[];
73 /* No syscall auditing will take place unless audit_enabled != 0. */
74 extern int audit_enabled;
76 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
77 * for saving names from getname(). */
78 #define AUDIT_NAMES 20
80 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
81 * audit_context from being used for nameless inodes from
83 #define AUDIT_NAMES_RESERVED 7
85 /* Indicates that audit should log the full pathname. */
86 #define AUDIT_NAME_FULL -1
88 /* number of audit rules */
91 /* When fs/namei.c:getname() is called, we store the pointer in name and
92 * we don't let putname() free it (instead we free all of the saved
93 * pointers at syscall exit time).
95 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
98 int name_len; /* number of name's characters to log */
99 unsigned name_put; /* call __putname() for this name */
109 struct audit_aux_data {
110 struct audit_aux_data *next;
114 #define AUDIT_AUX_IPCPERM 0
116 struct audit_aux_data_mq_open {
117 struct audit_aux_data d;
123 struct audit_aux_data_mq_sendrecv {
124 struct audit_aux_data d;
127 unsigned int msg_prio;
128 struct timespec abs_timeout;
131 struct audit_aux_data_mq_notify {
132 struct audit_aux_data d;
134 struct sigevent notification;
137 struct audit_aux_data_mq_getsetattr {
138 struct audit_aux_data d;
140 struct mq_attr mqstat;
143 struct audit_aux_data_ipcctl {
144 struct audit_aux_data d;
146 unsigned long qbytes;
153 struct audit_aux_data_execve {
154 struct audit_aux_data d;
160 struct audit_aux_data_socketcall {
161 struct audit_aux_data d;
163 unsigned long args[0];
166 struct audit_aux_data_sockaddr {
167 struct audit_aux_data d;
172 struct audit_aux_data_path {
173 struct audit_aux_data d;
174 struct dentry *dentry;
175 struct vfsmount *mnt;
178 /* The per-task audit context. */
179 struct audit_context {
180 int dummy; /* must be the first element */
181 int in_syscall; /* 1 if task is in a syscall */
182 enum audit_state state;
183 unsigned int serial; /* serial number for record */
184 struct timespec ctime; /* time of syscall entry */
185 uid_t loginuid; /* login uid (identity) */
186 int major; /* syscall number */
187 unsigned long argv[4]; /* syscall arguments */
188 int return_valid; /* return code is valid */
189 long return_code;/* syscall return code */
190 int auditable; /* 1 if record should be written */
192 struct audit_names names[AUDIT_NAMES];
193 char * filterkey; /* key for rule that triggered record */
195 struct vfsmount * pwdmnt;
196 struct audit_context *previous; /* For nested syscalls */
197 struct audit_aux_data *aux;
199 /* Save things to print about task_struct */
201 uid_t uid, euid, suid, fsuid;
202 gid_t gid, egid, sgid, fsgid;
203 unsigned long personality;
212 /* Determine if any context name data matches a rule's watch data */
213 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
215 static int audit_filter_rules(struct task_struct *tsk,
216 struct audit_krule *rule,
217 struct audit_context *ctx,
218 struct audit_names *name,
219 enum audit_state *state)
221 int i, j, need_sid = 1;
224 for (i = 0; i < rule->field_count; i++) {
225 struct audit_field *f = &rule->fields[i];
230 result = audit_comparator(tsk->pid, f->op, f->val);
234 result = audit_comparator(ctx->ppid, f->op, f->val);
237 result = audit_comparator(tsk->uid, f->op, f->val);
240 result = audit_comparator(tsk->euid, f->op, f->val);
243 result = audit_comparator(tsk->suid, f->op, f->val);
246 result = audit_comparator(tsk->fsuid, f->op, f->val);
249 result = audit_comparator(tsk->gid, f->op, f->val);
252 result = audit_comparator(tsk->egid, f->op, f->val);
255 result = audit_comparator(tsk->sgid, f->op, f->val);
258 result = audit_comparator(tsk->fsgid, f->op, f->val);
261 result = audit_comparator(tsk->personality, f->op, f->val);
265 result = audit_comparator(ctx->arch, f->op, f->val);
269 if (ctx && ctx->return_valid)
270 result = audit_comparator(ctx->return_code, f->op, f->val);
273 if (ctx && ctx->return_valid) {
275 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
277 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
282 result = audit_comparator(MAJOR(name->dev),
285 for (j = 0; j < ctx->name_count; j++) {
286 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
295 result = audit_comparator(MINOR(name->dev),
298 for (j = 0; j < ctx->name_count; j++) {
299 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
308 result = (name->ino == f->val);
310 for (j = 0; j < ctx->name_count; j++) {
311 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
319 if (name && rule->watch->ino != (unsigned long)-1)
320 result = (name->dev == rule->watch->dev &&
321 name->ino == rule->watch->ino);
326 result = audit_comparator(ctx->loginuid, f->op, f->val);
328 case AUDIT_SUBJ_USER:
329 case AUDIT_SUBJ_ROLE:
330 case AUDIT_SUBJ_TYPE:
333 /* NOTE: this may return negative values indicating
334 a temporary error. We simply treat this as a
335 match for now to avoid losing information that
336 may be wanted. An error message will also be
340 selinux_task_ctxid(tsk, &sid);
343 result = selinux_audit_rule_match(sid, f->type,
352 case AUDIT_OBJ_LEV_LOW:
353 case AUDIT_OBJ_LEV_HIGH:
354 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
357 /* Find files that match */
359 result = selinux_audit_rule_match(
360 name->osid, f->type, f->op,
363 for (j = 0; j < ctx->name_count; j++) {
364 if (selinux_audit_rule_match(
373 /* Find ipc objects that match */
375 struct audit_aux_data *aux;
376 for (aux = ctx->aux; aux;
378 if (aux->type == AUDIT_IPC) {
379 struct audit_aux_data_ipcctl *axi = (void *)aux;
380 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
394 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
396 case AUDIT_FILTERKEY:
397 /* ignore this field for filtering */
406 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
407 switch (rule->action) {
408 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
409 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
414 /* At process creation time, we can determine if system-call auditing is
415 * completely disabled for this task. Since we only have the task
416 * structure at this point, we can only check uid and gid.
418 static enum audit_state audit_filter_task(struct task_struct *tsk)
420 struct audit_entry *e;
421 enum audit_state state;
424 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
425 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
431 return AUDIT_BUILD_CONTEXT;
434 /* At syscall entry and exit time, this filter is called if the
435 * audit_state is not low enough that auditing cannot take place, but is
436 * also not high enough that we already know we have to write an audit
437 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
439 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
440 struct audit_context *ctx,
441 struct list_head *list)
443 struct audit_entry *e;
444 enum audit_state state;
446 if (audit_pid && tsk->tgid == audit_pid)
447 return AUDIT_DISABLED;
450 if (!list_empty(list)) {
451 int word = AUDIT_WORD(ctx->major);
452 int bit = AUDIT_BIT(ctx->major);
454 list_for_each_entry_rcu(e, list, list) {
455 if ((e->rule.mask[word] & bit) == bit &&
456 audit_filter_rules(tsk, &e->rule, ctx, NULL,
464 return AUDIT_BUILD_CONTEXT;
467 /* At syscall exit time, this filter is called if any audit_names[] have been
468 * collected during syscall processing. We only check rules in sublists at hash
469 * buckets applicable to the inode numbers in audit_names[].
470 * Regarding audit_state, same rules apply as for audit_filter_syscall().
472 enum audit_state audit_filter_inodes(struct task_struct *tsk,
473 struct audit_context *ctx)
476 struct audit_entry *e;
477 enum audit_state state;
479 if (audit_pid && tsk->tgid == audit_pid)
480 return AUDIT_DISABLED;
483 for (i = 0; i < ctx->name_count; i++) {
484 int word = AUDIT_WORD(ctx->major);
485 int bit = AUDIT_BIT(ctx->major);
486 struct audit_names *n = &ctx->names[i];
487 int h = audit_hash_ino((u32)n->ino);
488 struct list_head *list = &audit_inode_hash[h];
490 if (list_empty(list))
493 list_for_each_entry_rcu(e, list, list) {
494 if ((e->rule.mask[word] & bit) == bit &&
495 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
502 return AUDIT_BUILD_CONTEXT;
505 void audit_set_auditable(struct audit_context *ctx)
510 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
514 struct audit_context *context = tsk->audit_context;
516 if (likely(!context))
518 context->return_valid = return_valid;
519 context->return_code = return_code;
521 if (context->in_syscall && !context->dummy && !context->auditable) {
522 enum audit_state state;
524 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
525 if (state == AUDIT_RECORD_CONTEXT) {
526 context->auditable = 1;
530 state = audit_filter_inodes(tsk, context);
531 if (state == AUDIT_RECORD_CONTEXT)
532 context->auditable = 1;
537 context->pid = tsk->pid;
538 context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
539 context->uid = tsk->uid;
540 context->gid = tsk->gid;
541 context->euid = tsk->euid;
542 context->suid = tsk->suid;
543 context->fsuid = tsk->fsuid;
544 context->egid = tsk->egid;
545 context->sgid = tsk->sgid;
546 context->fsgid = tsk->fsgid;
547 context->personality = tsk->personality;
548 tsk->audit_context = NULL;
552 static inline void audit_free_names(struct audit_context *context)
557 if (context->auditable
558 ||context->put_count + context->ino_count != context->name_count) {
559 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
560 " name_count=%d put_count=%d"
561 " ino_count=%d [NOT freeing]\n",
563 context->serial, context->major, context->in_syscall,
564 context->name_count, context->put_count,
566 for (i = 0; i < context->name_count; i++) {
567 printk(KERN_ERR "names[%d] = %p = %s\n", i,
568 context->names[i].name,
569 context->names[i].name ?: "(null)");
576 context->put_count = 0;
577 context->ino_count = 0;
580 for (i = 0; i < context->name_count; i++) {
581 if (context->names[i].name && context->names[i].name_put)
582 __putname(context->names[i].name);
584 context->name_count = 0;
588 mntput(context->pwdmnt);
590 context->pwdmnt = NULL;
593 static inline void audit_free_aux(struct audit_context *context)
595 struct audit_aux_data *aux;
597 while ((aux = context->aux)) {
598 if (aux->type == AUDIT_AVC_PATH) {
599 struct audit_aux_data_path *axi = (void *)aux;
604 context->aux = aux->next;
609 static inline void audit_zero_context(struct audit_context *context,
610 enum audit_state state)
612 uid_t loginuid = context->loginuid;
614 memset(context, 0, sizeof(*context));
615 context->state = state;
616 context->loginuid = loginuid;
619 static inline struct audit_context *audit_alloc_context(enum audit_state state)
621 struct audit_context *context;
623 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
625 audit_zero_context(context, state);
630 * audit_alloc - allocate an audit context block for a task
633 * Filter on the task information and allocate a per-task audit context
634 * if necessary. Doing so turns on system call auditing for the
635 * specified task. This is called from copy_process, so no lock is
638 int audit_alloc(struct task_struct *tsk)
640 struct audit_context *context;
641 enum audit_state state;
643 if (likely(!audit_enabled))
644 return 0; /* Return if not auditing. */
646 state = audit_filter_task(tsk);
647 if (likely(state == AUDIT_DISABLED))
650 if (!(context = audit_alloc_context(state))) {
651 audit_log_lost("out of memory in audit_alloc");
655 /* Preserve login uid */
656 context->loginuid = -1;
657 if (current->audit_context)
658 context->loginuid = current->audit_context->loginuid;
660 tsk->audit_context = context;
661 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
665 static inline void audit_free_context(struct audit_context *context)
667 struct audit_context *previous;
671 previous = context->previous;
672 if (previous || (count && count < 10)) {
674 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
675 " freeing multiple contexts (%d)\n",
676 context->serial, context->major,
677 context->name_count, count);
679 audit_free_names(context);
680 audit_free_aux(context);
681 kfree(context->filterkey);
686 printk(KERN_ERR "audit: freed %d contexts\n", count);
689 static void audit_log_task_context(struct audit_buffer *ab)
694 len = security_getprocattr(current, "current", NULL, 0);
701 ctx = kmalloc(len, GFP_KERNEL);
705 len = security_getprocattr(current, "current", ctx, len);
709 audit_log_format(ab, " subj=%s", ctx);
714 audit_panic("error in audit_log_task_context");
718 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
720 char name[sizeof(tsk->comm)];
721 struct mm_struct *mm = tsk->mm;
722 struct vm_area_struct *vma;
726 get_task_comm(name, tsk);
727 audit_log_format(ab, " comm=");
728 audit_log_untrustedstring(ab, name);
731 down_read(&mm->mmap_sem);
734 if ((vma->vm_flags & VM_EXECUTABLE) &&
736 audit_log_d_path(ab, "exe=",
737 vma->vm_file->f_dentry,
738 vma->vm_file->f_vfsmnt);
743 up_read(&mm->mmap_sem);
745 audit_log_task_context(ab);
748 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
750 int i, call_panic = 0;
751 struct audit_buffer *ab;
752 struct audit_aux_data *aux;
757 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
759 return; /* audit_panic has been called */
760 audit_log_format(ab, "arch=%x syscall=%d",
761 context->arch, context->major);
762 if (context->personality != PER_LINUX)
763 audit_log_format(ab, " per=%lx", context->personality);
764 if (context->return_valid)
765 audit_log_format(ab, " success=%s exit=%ld",
766 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
767 context->return_code);
768 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
769 tty = tsk->signal->tty->name;
773 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
774 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
775 " euid=%u suid=%u fsuid=%u"
776 " egid=%u sgid=%u fsgid=%u tty=%s",
787 context->euid, context->suid, context->fsuid,
788 context->egid, context->sgid, context->fsgid, tty);
789 audit_log_task_info(ab, tsk);
790 if (context->filterkey) {
791 audit_log_format(ab, " key=");
792 audit_log_untrustedstring(ab, context->filterkey);
794 audit_log_format(ab, " key=(null)");
797 for (aux = context->aux; aux; aux = aux->next) {
799 ab = audit_log_start(context, GFP_KERNEL, aux->type);
801 continue; /* audit_panic has been called */
804 case AUDIT_MQ_OPEN: {
805 struct audit_aux_data_mq_open *axi = (void *)aux;
807 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
808 "mq_msgsize=%ld mq_curmsgs=%ld",
809 axi->oflag, axi->mode, axi->attr.mq_flags,
810 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
811 axi->attr.mq_curmsgs);
814 case AUDIT_MQ_SENDRECV: {
815 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
817 "mqdes=%d msg_len=%zd msg_prio=%u "
818 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
819 axi->mqdes, axi->msg_len, axi->msg_prio,
820 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
823 case AUDIT_MQ_NOTIFY: {
824 struct audit_aux_data_mq_notify *axi = (void *)aux;
826 "mqdes=%d sigev_signo=%d",
828 axi->notification.sigev_signo);
831 case AUDIT_MQ_GETSETATTR: {
832 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
834 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
837 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
838 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
842 struct audit_aux_data_ipcctl *axi = (void *)aux;
844 "ouid=%u ogid=%u mode=%x",
845 axi->uid, axi->gid, axi->mode);
846 if (axi->osid != 0) {
849 if (selinux_ctxid_to_string(
850 axi->osid, &ctx, &len)) {
851 audit_log_format(ab, " osid=%u",
855 audit_log_format(ab, " obj=%s", ctx);
860 case AUDIT_IPC_SET_PERM: {
861 struct audit_aux_data_ipcctl *axi = (void *)aux;
863 "qbytes=%lx ouid=%u ogid=%u mode=%x",
864 axi->qbytes, axi->uid, axi->gid, axi->mode);
868 struct audit_aux_data_execve *axi = (void *)aux;
871 for (i = 0, p = axi->mem; i < axi->argc; i++) {
872 audit_log_format(ab, "a%d=", i);
873 p = audit_log_untrustedstring(ab, p);
874 audit_log_format(ab, "\n");
878 case AUDIT_SOCKETCALL: {
880 struct audit_aux_data_socketcall *axs = (void *)aux;
881 audit_log_format(ab, "nargs=%d", axs->nargs);
882 for (i=0; i<axs->nargs; i++)
883 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
886 case AUDIT_SOCKADDR: {
887 struct audit_aux_data_sockaddr *axs = (void *)aux;
889 audit_log_format(ab, "saddr=");
890 audit_log_hex(ab, axs->a, axs->len);
893 case AUDIT_AVC_PATH: {
894 struct audit_aux_data_path *axi = (void *)aux;
895 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
902 if (context->pwd && context->pwdmnt) {
903 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
905 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
909 for (i = 0; i < context->name_count; i++) {
910 struct audit_names *n = &context->names[i];
912 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
914 continue; /* audit_panic has been called */
916 audit_log_format(ab, "item=%d", i);
919 switch(n->name_len) {
920 case AUDIT_NAME_FULL:
921 /* log the full path */
922 audit_log_format(ab, " name=");
923 audit_log_untrustedstring(ab, n->name);
926 /* name was specified as a relative path and the
927 * directory component is the cwd */
928 audit_log_d_path(ab, " name=", context->pwd,
932 /* log the name's directory component */
933 audit_log_format(ab, " name=");
934 audit_log_n_untrustedstring(ab, n->name_len,
938 audit_log_format(ab, " name=(null)");
940 if (n->ino != (unsigned long)-1) {
941 audit_log_format(ab, " inode=%lu"
942 " dev=%02x:%02x mode=%#o"
943 " ouid=%u ogid=%u rdev=%02x:%02x",
956 if (selinux_ctxid_to_string(
957 n->osid, &ctx, &len)) {
958 audit_log_format(ab, " osid=%u", n->osid);
961 audit_log_format(ab, " obj=%s", ctx);
968 audit_panic("error converting sid to string");
972 * audit_free - free a per-task audit context
973 * @tsk: task whose audit context block to free
975 * Called from copy_process and do_exit
977 void audit_free(struct task_struct *tsk)
979 struct audit_context *context;
981 context = audit_get_context(tsk, 0, 0);
982 if (likely(!context))
985 /* Check for system calls that do not go through the exit
986 * function (e.g., exit_group), then free context block.
987 * We use GFP_ATOMIC here because we might be doing this
988 * in the context of the idle thread */
989 /* that can happen only if we are called from do_exit() */
990 if (context->in_syscall && context->auditable)
991 audit_log_exit(context, tsk);
993 audit_free_context(context);
997 * audit_syscall_entry - fill in an audit record at syscall entry
998 * @tsk: task being audited
999 * @arch: architecture type
1000 * @major: major syscall type (function)
1001 * @a1: additional syscall register 1
1002 * @a2: additional syscall register 2
1003 * @a3: additional syscall register 3
1004 * @a4: additional syscall register 4
1006 * Fill in audit context at syscall entry. This only happens if the
1007 * audit context was created when the task was created and the state or
1008 * filters demand the audit context be built. If the state from the
1009 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1010 * then the record will be written at syscall exit time (otherwise, it
1011 * will only be written if another part of the kernel requests that it
1014 void audit_syscall_entry(int arch, int major,
1015 unsigned long a1, unsigned long a2,
1016 unsigned long a3, unsigned long a4)
1018 struct task_struct *tsk = current;
1019 struct audit_context *context = tsk->audit_context;
1020 enum audit_state state;
1025 * This happens only on certain architectures that make system
1026 * calls in kernel_thread via the entry.S interface, instead of
1027 * with direct calls. (If you are porting to a new
1028 * architecture, hitting this condition can indicate that you
1029 * got the _exit/_leave calls backward in entry.S.)
1033 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1035 * This also happens with vm86 emulation in a non-nested manner
1036 * (entries without exits), so this case must be caught.
1038 if (context->in_syscall) {
1039 struct audit_context *newctx;
1043 "audit(:%d) pid=%d in syscall=%d;"
1044 " entering syscall=%d\n",
1045 context->serial, tsk->pid, context->major, major);
1047 newctx = audit_alloc_context(context->state);
1049 newctx->previous = context;
1051 tsk->audit_context = newctx;
1053 /* If we can't alloc a new context, the best we
1054 * can do is to leak memory (any pending putname
1055 * will be lost). The only other alternative is
1056 * to abandon auditing. */
1057 audit_zero_context(context, context->state);
1060 BUG_ON(context->in_syscall || context->name_count);
1065 context->arch = arch;
1066 context->major = major;
1067 context->argv[0] = a1;
1068 context->argv[1] = a2;
1069 context->argv[2] = a3;
1070 context->argv[3] = a4;
1072 state = context->state;
1073 context->dummy = !audit_n_rules;
1074 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1075 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1076 if (likely(state == AUDIT_DISABLED))
1079 context->serial = 0;
1080 context->ctime = CURRENT_TIME;
1081 context->in_syscall = 1;
1082 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1086 * audit_syscall_exit - deallocate audit context after a system call
1087 * @tsk: task being audited
1088 * @valid: success/failure flag
1089 * @return_code: syscall return value
1091 * Tear down after system call. If the audit context has been marked as
1092 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1093 * filtering, or because some other part of the kernel write an audit
1094 * message), then write out the syscall information. In call cases,
1095 * free the names stored from getname().
1097 void audit_syscall_exit(int valid, long return_code)
1099 struct task_struct *tsk = current;
1100 struct audit_context *context;
1102 context = audit_get_context(tsk, valid, return_code);
1104 if (likely(!context))
1107 if (context->in_syscall && context->auditable)
1108 audit_log_exit(context, tsk);
1110 context->in_syscall = 0;
1111 context->auditable = 0;
1113 if (context->previous) {
1114 struct audit_context *new_context = context->previous;
1115 context->previous = NULL;
1116 audit_free_context(context);
1117 tsk->audit_context = new_context;
1119 audit_free_names(context);
1120 audit_free_aux(context);
1121 kfree(context->filterkey);
1122 context->filterkey = NULL;
1123 tsk->audit_context = context;
1128 * audit_getname - add a name to the list
1129 * @name: name to add
1131 * Add a name to the list of audit names for this context.
1132 * Called from fs/namei.c:getname().
1134 void __audit_getname(const char *name)
1136 struct audit_context *context = current->audit_context;
1138 if (IS_ERR(name) || !name)
1141 if (!context->in_syscall) {
1142 #if AUDIT_DEBUG == 2
1143 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1144 __FILE__, __LINE__, context->serial, name);
1149 BUG_ON(context->name_count >= AUDIT_NAMES);
1150 context->names[context->name_count].name = name;
1151 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1152 context->names[context->name_count].name_put = 1;
1153 context->names[context->name_count].ino = (unsigned long)-1;
1154 ++context->name_count;
1155 if (!context->pwd) {
1156 read_lock(¤t->fs->lock);
1157 context->pwd = dget(current->fs->pwd);
1158 context->pwdmnt = mntget(current->fs->pwdmnt);
1159 read_unlock(¤t->fs->lock);
1164 /* audit_putname - intercept a putname request
1165 * @name: name to intercept and delay for putname
1167 * If we have stored the name from getname in the audit context,
1168 * then we delay the putname until syscall exit.
1169 * Called from include/linux/fs.h:putname().
1171 void audit_putname(const char *name)
1173 struct audit_context *context = current->audit_context;
1176 if (!context->in_syscall) {
1177 #if AUDIT_DEBUG == 2
1178 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1179 __FILE__, __LINE__, context->serial, name);
1180 if (context->name_count) {
1182 for (i = 0; i < context->name_count; i++)
1183 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1184 context->names[i].name,
1185 context->names[i].name ?: "(null)");
1192 ++context->put_count;
1193 if (context->put_count > context->name_count) {
1194 printk(KERN_ERR "%s:%d(:%d): major=%d"
1195 " in_syscall=%d putname(%p) name_count=%d"
1198 context->serial, context->major,
1199 context->in_syscall, name, context->name_count,
1200 context->put_count);
1207 /* Copy inode data into an audit_names. */
1208 static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
1210 name->ino = inode->i_ino;
1211 name->dev = inode->i_sb->s_dev;
1212 name->mode = inode->i_mode;
1213 name->uid = inode->i_uid;
1214 name->gid = inode->i_gid;
1215 name->rdev = inode->i_rdev;
1216 selinux_get_inode_sid(inode, &name->osid);
1220 * audit_inode - store the inode and device from a lookup
1221 * @name: name being audited
1222 * @inode: inode being audited
1224 * Called from fs/namei.c:path_lookup().
1226 void __audit_inode(const char *name, const struct inode *inode)
1229 struct audit_context *context = current->audit_context;
1231 if (!context->in_syscall)
1233 if (context->name_count
1234 && context->names[context->name_count-1].name
1235 && context->names[context->name_count-1].name == name)
1236 idx = context->name_count - 1;
1237 else if (context->name_count > 1
1238 && context->names[context->name_count-2].name
1239 && context->names[context->name_count-2].name == name)
1240 idx = context->name_count - 2;
1242 /* FIXME: how much do we care about inodes that have no
1243 * associated name? */
1244 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1246 idx = context->name_count++;
1247 context->names[idx].name = NULL;
1249 ++context->ino_count;
1252 audit_copy_inode(&context->names[idx], inode);
1256 * audit_inode_child - collect inode info for created/removed objects
1257 * @dname: inode's dentry name
1258 * @inode: inode being audited
1259 * @parent: inode of dentry parent
1261 * For syscalls that create or remove filesystem objects, audit_inode
1262 * can only collect information for the filesystem object's parent.
1263 * This call updates the audit context with the child's information.
1264 * Syscalls that create a new filesystem object must be hooked after
1265 * the object is created. Syscalls that remove a filesystem object
1266 * must be hooked prior, in order to capture the target inode during
1267 * unsuccessful attempts.
1269 void __audit_inode_child(const char *dname, const struct inode *inode,
1270 const struct inode *parent)
1273 struct audit_context *context = current->audit_context;
1274 const char *found_name = NULL;
1277 if (!context->in_syscall)
1280 /* determine matching parent */
1282 goto update_context;
1283 for (idx = 0; idx < context->name_count; idx++)
1284 if (context->names[idx].ino == parent->i_ino) {
1285 const char *name = context->names[idx].name;
1290 if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
1291 context->names[idx].name_len = dirlen;
1298 idx = context->name_count++;
1300 context->ino_count++;
1302 /* Re-use the name belonging to the slot for a matching parent directory.
1303 * All names for this context are relinquished in audit_free_names() */
1304 context->names[idx].name = found_name;
1305 context->names[idx].name_len = AUDIT_NAME_FULL;
1306 context->names[idx].name_put = 0; /* don't call __putname() */
1309 context->names[idx].ino = (unsigned long)-1;
1311 audit_copy_inode(&context->names[idx], inode);
1313 /* A parent was not found in audit_names, so copy the inode data for the
1314 * provided parent. */
1316 idx = context->name_count++;
1318 context->ino_count++;
1320 audit_copy_inode(&context->names[idx], parent);
1325 * audit_inode_update - update inode info for last collected name
1326 * @inode: inode being audited
1328 * When open() is called on an existing object with the O_CREAT flag, the inode
1329 * data audit initially collects is incorrect. This additional hook ensures
1330 * audit has the inode data for the actual object to be opened.
1332 void __audit_inode_update(const struct inode *inode)
1334 struct audit_context *context = current->audit_context;
1337 if (!context->in_syscall || !inode)
1340 if (context->name_count == 0) {
1341 context->name_count++;
1343 context->ino_count++;
1346 idx = context->name_count - 1;
1348 audit_copy_inode(&context->names[idx], inode);
1352 * auditsc_get_stamp - get local copies of audit_context values
1353 * @ctx: audit_context for the task
1354 * @t: timespec to store time recorded in the audit_context
1355 * @serial: serial value that is recorded in the audit_context
1357 * Also sets the context as auditable.
1359 void auditsc_get_stamp(struct audit_context *ctx,
1360 struct timespec *t, unsigned int *serial)
1363 ctx->serial = audit_serial();
1364 t->tv_sec = ctx->ctime.tv_sec;
1365 t->tv_nsec = ctx->ctime.tv_nsec;
1366 *serial = ctx->serial;
1371 * audit_set_loginuid - set a task's audit_context loginuid
1372 * @task: task whose audit context is being modified
1373 * @loginuid: loginuid value
1377 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1379 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1381 struct audit_context *context = task->audit_context;
1384 /* Only log if audit is enabled */
1385 if (context->in_syscall) {
1386 struct audit_buffer *ab;
1388 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1390 audit_log_format(ab, "login pid=%d uid=%u "
1391 "old auid=%u new auid=%u",
1392 task->pid, task->uid,
1393 context->loginuid, loginuid);
1397 context->loginuid = loginuid;
1403 * audit_get_loginuid - get the loginuid for an audit_context
1404 * @ctx: the audit_context
1406 * Returns the context's loginuid or -1 if @ctx is NULL.
1408 uid_t audit_get_loginuid(struct audit_context *ctx)
1410 return ctx ? ctx->loginuid : -1;
1414 * __audit_mq_open - record audit data for a POSIX MQ open
1417 * @u_attr: queue attributes
1419 * Returns 0 for success or NULL context or < 0 on error.
1421 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1423 struct audit_aux_data_mq_open *ax;
1424 struct audit_context *context = current->audit_context;
1429 if (likely(!context))
1432 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1436 if (u_attr != NULL) {
1437 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1442 memset(&ax->attr, 0, sizeof(ax->attr));
1447 ax->d.type = AUDIT_MQ_OPEN;
1448 ax->d.next = context->aux;
1449 context->aux = (void *)ax;
1454 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1455 * @mqdes: MQ descriptor
1456 * @msg_len: Message length
1457 * @msg_prio: Message priority
1458 * @u_abs_timeout: Message timeout in absolute time
1460 * Returns 0 for success or NULL context or < 0 on error.
1462 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1463 const struct timespec __user *u_abs_timeout)
1465 struct audit_aux_data_mq_sendrecv *ax;
1466 struct audit_context *context = current->audit_context;
1471 if (likely(!context))
1474 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1478 if (u_abs_timeout != NULL) {
1479 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1484 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1487 ax->msg_len = msg_len;
1488 ax->msg_prio = msg_prio;
1490 ax->d.type = AUDIT_MQ_SENDRECV;
1491 ax->d.next = context->aux;
1492 context->aux = (void *)ax;
1497 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1498 * @mqdes: MQ descriptor
1499 * @msg_len: Message length
1500 * @u_msg_prio: Message priority
1501 * @u_abs_timeout: Message timeout in absolute time
1503 * Returns 0 for success or NULL context or < 0 on error.
1505 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1506 unsigned int __user *u_msg_prio,
1507 const struct timespec __user *u_abs_timeout)
1509 struct audit_aux_data_mq_sendrecv *ax;
1510 struct audit_context *context = current->audit_context;
1515 if (likely(!context))
1518 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1522 if (u_msg_prio != NULL) {
1523 if (get_user(ax->msg_prio, u_msg_prio)) {
1530 if (u_abs_timeout != NULL) {
1531 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1536 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1539 ax->msg_len = msg_len;
1541 ax->d.type = AUDIT_MQ_SENDRECV;
1542 ax->d.next = context->aux;
1543 context->aux = (void *)ax;
1548 * __audit_mq_notify - record audit data for a POSIX MQ notify
1549 * @mqdes: MQ descriptor
1550 * @u_notification: Notification event
1552 * Returns 0 for success or NULL context or < 0 on error.
1555 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1557 struct audit_aux_data_mq_notify *ax;
1558 struct audit_context *context = current->audit_context;
1563 if (likely(!context))
1566 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1570 if (u_notification != NULL) {
1571 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1576 memset(&ax->notification, 0, sizeof(ax->notification));
1580 ax->d.type = AUDIT_MQ_NOTIFY;
1581 ax->d.next = context->aux;
1582 context->aux = (void *)ax;
1587 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1588 * @mqdes: MQ descriptor
1591 * Returns 0 for success or NULL context or < 0 on error.
1593 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1595 struct audit_aux_data_mq_getsetattr *ax;
1596 struct audit_context *context = current->audit_context;
1601 if (likely(!context))
1604 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1609 ax->mqstat = *mqstat;
1611 ax->d.type = AUDIT_MQ_GETSETATTR;
1612 ax->d.next = context->aux;
1613 context->aux = (void *)ax;
1618 * audit_ipc_obj - record audit data for ipc object
1619 * @ipcp: ipc permissions
1621 * Returns 0 for success or NULL context or < 0 on error.
1623 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
1625 struct audit_aux_data_ipcctl *ax;
1626 struct audit_context *context = current->audit_context;
1628 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1632 ax->uid = ipcp->uid;
1633 ax->gid = ipcp->gid;
1634 ax->mode = ipcp->mode;
1635 selinux_get_ipc_sid(ipcp, &ax->osid);
1637 ax->d.type = AUDIT_IPC;
1638 ax->d.next = context->aux;
1639 context->aux = (void *)ax;
1644 * audit_ipc_set_perm - record audit data for new ipc permissions
1645 * @qbytes: msgq bytes
1646 * @uid: msgq user id
1647 * @gid: msgq group id
1648 * @mode: msgq mode (permissions)
1650 * Returns 0 for success or NULL context or < 0 on error.
1652 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1654 struct audit_aux_data_ipcctl *ax;
1655 struct audit_context *context = current->audit_context;
1657 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1661 ax->qbytes = qbytes;
1666 ax->d.type = AUDIT_IPC_SET_PERM;
1667 ax->d.next = context->aux;
1668 context->aux = (void *)ax;
1672 int audit_bprm(struct linux_binprm *bprm)
1674 struct audit_aux_data_execve *ax;
1675 struct audit_context *context = current->audit_context;
1676 unsigned long p, next;
1679 if (likely(!audit_enabled || !context || context->dummy))
1682 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1687 ax->argc = bprm->argc;
1688 ax->envc = bprm->envc;
1689 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1690 struct page *page = bprm->page[p / PAGE_SIZE];
1691 void *kaddr = kmap(page);
1692 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1693 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1698 ax->d.type = AUDIT_EXECVE;
1699 ax->d.next = context->aux;
1700 context->aux = (void *)ax;
1706 * audit_socketcall - record audit data for sys_socketcall
1707 * @nargs: number of args
1710 * Returns 0 for success or NULL context or < 0 on error.
1712 int audit_socketcall(int nargs, unsigned long *args)
1714 struct audit_aux_data_socketcall *ax;
1715 struct audit_context *context = current->audit_context;
1717 if (likely(!context || context->dummy))
1720 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1725 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1727 ax->d.type = AUDIT_SOCKETCALL;
1728 ax->d.next = context->aux;
1729 context->aux = (void *)ax;
1734 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1735 * @len: data length in user space
1736 * @a: data address in kernel space
1738 * Returns 0 for success or NULL context or < 0 on error.
1740 int audit_sockaddr(int len, void *a)
1742 struct audit_aux_data_sockaddr *ax;
1743 struct audit_context *context = current->audit_context;
1745 if (likely(!context || context->dummy))
1748 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1753 memcpy(ax->a, a, len);
1755 ax->d.type = AUDIT_SOCKADDR;
1756 ax->d.next = context->aux;
1757 context->aux = (void *)ax;
1762 * audit_avc_path - record the granting or denial of permissions
1763 * @dentry: dentry to record
1764 * @mnt: mnt to record
1766 * Returns 0 for success or NULL context or < 0 on error.
1768 * Called from security/selinux/avc.c::avc_audit()
1770 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1772 struct audit_aux_data_path *ax;
1773 struct audit_context *context = current->audit_context;
1775 if (likely(!context))
1778 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1782 ax->dentry = dget(dentry);
1783 ax->mnt = mntget(mnt);
1785 ax->d.type = AUDIT_AVC_PATH;
1786 ax->d.next = context->aux;
1787 context->aux = (void *)ax;
1792 * audit_signal_info - record signal info for shutting down audit subsystem
1793 * @sig: signal value
1794 * @t: task being signaled
1796 * If the audit subsystem is being terminated, record the task (pid)
1797 * and uid that is doing that.
1799 void __audit_signal_info(int sig, struct task_struct *t)
1801 extern pid_t audit_sig_pid;
1802 extern uid_t audit_sig_uid;
1803 extern u32 audit_sig_sid;
1805 if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
1806 struct task_struct *tsk = current;
1807 struct audit_context *ctx = tsk->audit_context;
1808 audit_sig_pid = tsk->pid;
1810 audit_sig_uid = ctx->loginuid;
1812 audit_sig_uid = tsk->uid;
1813 selinux_get_task_sid(tsk, &audit_sig_sid);