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/highmem.h>
68 #include <linux/syscalls.h>
72 extern struct list_head audit_filter_list[];
74 /* No syscall auditing will take place unless audit_enabled != 0. */
75 extern int audit_enabled;
77 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
78 * for saving names from getname(). */
79 #define AUDIT_NAMES 20
81 /* Indicates that audit should log the full pathname. */
82 #define AUDIT_NAME_FULL -1
84 /* number of audit rules */
87 /* determines whether we collect data for signals sent */
90 /* When fs/namei.c:getname() is called, we store the pointer in name and
91 * we don't let putname() free it (instead we free all of the saved
92 * pointers at syscall exit time).
94 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
97 int name_len; /* number of name's characters to log */
98 unsigned name_put; /* call __putname() for this name */
108 struct audit_aux_data {
109 struct audit_aux_data *next;
113 #define AUDIT_AUX_IPCPERM 0
115 /* Number of target pids per aux struct. */
116 #define AUDIT_AUX_PIDS 16
118 struct audit_aux_data_mq_open {
119 struct audit_aux_data d;
125 struct audit_aux_data_mq_sendrecv {
126 struct audit_aux_data d;
129 unsigned int msg_prio;
130 struct timespec abs_timeout;
133 struct audit_aux_data_mq_notify {
134 struct audit_aux_data d;
136 struct sigevent notification;
139 struct audit_aux_data_mq_getsetattr {
140 struct audit_aux_data d;
142 struct mq_attr mqstat;
145 struct audit_aux_data_ipcctl {
146 struct audit_aux_data d;
148 unsigned long qbytes;
155 struct audit_aux_data_execve {
156 struct audit_aux_data d;
162 struct audit_aux_data_socketcall {
163 struct audit_aux_data d;
165 unsigned long args[0];
168 struct audit_aux_data_sockaddr {
169 struct audit_aux_data d;
174 struct audit_aux_data_fd_pair {
175 struct audit_aux_data d;
179 struct audit_aux_data_path {
180 struct audit_aux_data d;
181 struct dentry *dentry;
182 struct vfsmount *mnt;
185 struct audit_aux_data_pids {
186 struct audit_aux_data d;
187 pid_t target_pid[AUDIT_AUX_PIDS];
188 u32 target_sid[AUDIT_AUX_PIDS];
192 /* The per-task audit context. */
193 struct audit_context {
194 int dummy; /* must be the first element */
195 int in_syscall; /* 1 if task is in a syscall */
196 enum audit_state state;
197 unsigned int serial; /* serial number for record */
198 struct timespec ctime; /* time of syscall entry */
199 uid_t loginuid; /* login uid (identity) */
200 int major; /* syscall number */
201 unsigned long argv[4]; /* syscall arguments */
202 int return_valid; /* return code is valid */
203 long return_code;/* syscall return code */
204 int auditable; /* 1 if record should be written */
206 struct audit_names names[AUDIT_NAMES];
207 char * filterkey; /* key for rule that triggered record */
209 struct vfsmount * pwdmnt;
210 struct audit_context *previous; /* For nested syscalls */
211 struct audit_aux_data *aux;
212 struct audit_aux_data *aux_pids;
214 /* Save things to print about task_struct */
216 uid_t uid, euid, suid, fsuid;
217 gid_t gid, egid, sgid, fsgid;
218 unsigned long personality;
230 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
231 static inline int open_arg(int flags, int mask)
233 int n = ACC_MODE(flags);
234 if (flags & (O_TRUNC | O_CREAT))
235 n |= AUDIT_PERM_WRITE;
239 static int audit_match_perm(struct audit_context *ctx, int mask)
241 unsigned n = ctx->major;
242 switch (audit_classify_syscall(ctx->arch, n)) {
244 if ((mask & AUDIT_PERM_WRITE) &&
245 audit_match_class(AUDIT_CLASS_WRITE, n))
247 if ((mask & AUDIT_PERM_READ) &&
248 audit_match_class(AUDIT_CLASS_READ, n))
250 if ((mask & AUDIT_PERM_ATTR) &&
251 audit_match_class(AUDIT_CLASS_CHATTR, n))
254 case 1: /* 32bit on biarch */
255 if ((mask & AUDIT_PERM_WRITE) &&
256 audit_match_class(AUDIT_CLASS_WRITE_32, n))
258 if ((mask & AUDIT_PERM_READ) &&
259 audit_match_class(AUDIT_CLASS_READ_32, n))
261 if ((mask & AUDIT_PERM_ATTR) &&
262 audit_match_class(AUDIT_CLASS_CHATTR_32, n))
266 return mask & ACC_MODE(ctx->argv[1]);
268 return mask & ACC_MODE(ctx->argv[2]);
269 case 4: /* socketcall */
270 return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
272 return mask & AUDIT_PERM_EXEC;
278 /* Determine if any context name data matches a rule's watch data */
279 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
281 static int audit_filter_rules(struct task_struct *tsk,
282 struct audit_krule *rule,
283 struct audit_context *ctx,
284 struct audit_names *name,
285 enum audit_state *state)
287 int i, j, need_sid = 1;
290 for (i = 0; i < rule->field_count; i++) {
291 struct audit_field *f = &rule->fields[i];
296 result = audit_comparator(tsk->pid, f->op, f->val);
301 ctx->ppid = sys_getppid();
302 result = audit_comparator(ctx->ppid, f->op, f->val);
306 result = audit_comparator(tsk->uid, f->op, f->val);
309 result = audit_comparator(tsk->euid, f->op, f->val);
312 result = audit_comparator(tsk->suid, f->op, f->val);
315 result = audit_comparator(tsk->fsuid, f->op, f->val);
318 result = audit_comparator(tsk->gid, f->op, f->val);
321 result = audit_comparator(tsk->egid, f->op, f->val);
324 result = audit_comparator(tsk->sgid, f->op, f->val);
327 result = audit_comparator(tsk->fsgid, f->op, f->val);
330 result = audit_comparator(tsk->personality, f->op, f->val);
334 result = audit_comparator(ctx->arch, f->op, f->val);
338 if (ctx && ctx->return_valid)
339 result = audit_comparator(ctx->return_code, f->op, f->val);
342 if (ctx && ctx->return_valid) {
344 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
346 result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
351 result = audit_comparator(MAJOR(name->dev),
354 for (j = 0; j < ctx->name_count; j++) {
355 if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
364 result = audit_comparator(MINOR(name->dev),
367 for (j = 0; j < ctx->name_count; j++) {
368 if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
377 result = (name->ino == f->val);
379 for (j = 0; j < ctx->name_count; j++) {
380 if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
388 if (name && rule->watch->ino != (unsigned long)-1)
389 result = (name->dev == rule->watch->dev &&
390 name->ino == rule->watch->ino);
395 result = audit_comparator(ctx->loginuid, f->op, f->val);
397 case AUDIT_SUBJ_USER:
398 case AUDIT_SUBJ_ROLE:
399 case AUDIT_SUBJ_TYPE:
402 /* NOTE: this may return negative values indicating
403 a temporary error. We simply treat this as a
404 match for now to avoid losing information that
405 may be wanted. An error message will also be
409 selinux_get_task_sid(tsk, &sid);
412 result = selinux_audit_rule_match(sid, f->type,
421 case AUDIT_OBJ_LEV_LOW:
422 case AUDIT_OBJ_LEV_HIGH:
423 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
426 /* Find files that match */
428 result = selinux_audit_rule_match(
429 name->osid, f->type, f->op,
432 for (j = 0; j < ctx->name_count; j++) {
433 if (selinux_audit_rule_match(
442 /* Find ipc objects that match */
444 struct audit_aux_data *aux;
445 for (aux = ctx->aux; aux;
447 if (aux->type == AUDIT_IPC) {
448 struct audit_aux_data_ipcctl *axi = (void *)aux;
449 if (selinux_audit_rule_match(axi->osid, f->type, f->op, f->se_rule, ctx)) {
463 result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
465 case AUDIT_FILTERKEY:
466 /* ignore this field for filtering */
470 result = audit_match_perm(ctx, f->val);
478 ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
479 switch (rule->action) {
480 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
481 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
486 /* At process creation time, we can determine if system-call auditing is
487 * completely disabled for this task. Since we only have the task
488 * structure at this point, we can only check uid and gid.
490 static enum audit_state audit_filter_task(struct task_struct *tsk)
492 struct audit_entry *e;
493 enum audit_state state;
496 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
497 if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
503 return AUDIT_BUILD_CONTEXT;
506 /* At syscall entry and exit time, this filter is called if the
507 * audit_state is not low enough that auditing cannot take place, but is
508 * also not high enough that we already know we have to write an audit
509 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
511 static enum audit_state audit_filter_syscall(struct task_struct *tsk,
512 struct audit_context *ctx,
513 struct list_head *list)
515 struct audit_entry *e;
516 enum audit_state state;
518 if (audit_pid && tsk->tgid == audit_pid)
519 return AUDIT_DISABLED;
522 if (!list_empty(list)) {
523 int word = AUDIT_WORD(ctx->major);
524 int bit = AUDIT_BIT(ctx->major);
526 list_for_each_entry_rcu(e, list, list) {
527 if ((e->rule.mask[word] & bit) == bit &&
528 audit_filter_rules(tsk, &e->rule, ctx, NULL,
536 return AUDIT_BUILD_CONTEXT;
539 /* At syscall exit time, this filter is called if any audit_names[] have been
540 * collected during syscall processing. We only check rules in sublists at hash
541 * buckets applicable to the inode numbers in audit_names[].
542 * Regarding audit_state, same rules apply as for audit_filter_syscall().
544 enum audit_state audit_filter_inodes(struct task_struct *tsk,
545 struct audit_context *ctx)
548 struct audit_entry *e;
549 enum audit_state state;
551 if (audit_pid && tsk->tgid == audit_pid)
552 return AUDIT_DISABLED;
555 for (i = 0; i < ctx->name_count; i++) {
556 int word = AUDIT_WORD(ctx->major);
557 int bit = AUDIT_BIT(ctx->major);
558 struct audit_names *n = &ctx->names[i];
559 int h = audit_hash_ino((u32)n->ino);
560 struct list_head *list = &audit_inode_hash[h];
562 if (list_empty(list))
565 list_for_each_entry_rcu(e, list, list) {
566 if ((e->rule.mask[word] & bit) == bit &&
567 audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
574 return AUDIT_BUILD_CONTEXT;
577 void audit_set_auditable(struct audit_context *ctx)
582 static inline struct audit_context *audit_get_context(struct task_struct *tsk,
586 struct audit_context *context = tsk->audit_context;
588 if (likely(!context))
590 context->return_valid = return_valid;
591 context->return_code = return_code;
593 if (context->in_syscall && !context->dummy && !context->auditable) {
594 enum audit_state state;
596 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
597 if (state == AUDIT_RECORD_CONTEXT) {
598 context->auditable = 1;
602 state = audit_filter_inodes(tsk, context);
603 if (state == AUDIT_RECORD_CONTEXT)
604 context->auditable = 1;
610 tsk->audit_context = NULL;
614 static inline void audit_free_names(struct audit_context *context)
619 if (context->auditable
620 ||context->put_count + context->ino_count != context->name_count) {
621 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
622 " name_count=%d put_count=%d"
623 " ino_count=%d [NOT freeing]\n",
625 context->serial, context->major, context->in_syscall,
626 context->name_count, context->put_count,
628 for (i = 0; i < context->name_count; i++) {
629 printk(KERN_ERR "names[%d] = %p = %s\n", i,
630 context->names[i].name,
631 context->names[i].name ?: "(null)");
638 context->put_count = 0;
639 context->ino_count = 0;
642 for (i = 0; i < context->name_count; i++) {
643 if (context->names[i].name && context->names[i].name_put)
644 __putname(context->names[i].name);
646 context->name_count = 0;
650 mntput(context->pwdmnt);
652 context->pwdmnt = NULL;
655 static inline void audit_free_aux(struct audit_context *context)
657 struct audit_aux_data *aux;
659 while ((aux = context->aux)) {
660 if (aux->type == AUDIT_AVC_PATH) {
661 struct audit_aux_data_path *axi = (void *)aux;
666 context->aux = aux->next;
669 while ((aux = context->aux_pids)) {
670 context->aux_pids = aux->next;
675 static inline void audit_zero_context(struct audit_context *context,
676 enum audit_state state)
678 uid_t loginuid = context->loginuid;
680 memset(context, 0, sizeof(*context));
681 context->state = state;
682 context->loginuid = loginuid;
685 static inline struct audit_context *audit_alloc_context(enum audit_state state)
687 struct audit_context *context;
689 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
691 audit_zero_context(context, state);
696 * audit_alloc - allocate an audit context block for a task
699 * Filter on the task information and allocate a per-task audit context
700 * if necessary. Doing so turns on system call auditing for the
701 * specified task. This is called from copy_process, so no lock is
704 int audit_alloc(struct task_struct *tsk)
706 struct audit_context *context;
707 enum audit_state state;
709 if (likely(!audit_enabled))
710 return 0; /* Return if not auditing. */
712 state = audit_filter_task(tsk);
713 if (likely(state == AUDIT_DISABLED))
716 if (!(context = audit_alloc_context(state))) {
717 audit_log_lost("out of memory in audit_alloc");
721 /* Preserve login uid */
722 context->loginuid = -1;
723 if (current->audit_context)
724 context->loginuid = current->audit_context->loginuid;
726 tsk->audit_context = context;
727 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
731 static inline void audit_free_context(struct audit_context *context)
733 struct audit_context *previous;
737 previous = context->previous;
738 if (previous || (count && count < 10)) {
740 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
741 " freeing multiple contexts (%d)\n",
742 context->serial, context->major,
743 context->name_count, count);
745 audit_free_names(context);
746 audit_free_aux(context);
747 kfree(context->filterkey);
752 printk(KERN_ERR "audit: freed %d contexts\n", count);
755 void audit_log_task_context(struct audit_buffer *ab)
762 selinux_get_task_sid(current, &sid);
766 error = selinux_sid_to_string(sid, &ctx, &len);
768 if (error != -EINVAL)
773 audit_log_format(ab, " subj=%s", ctx);
778 audit_panic("error in audit_log_task_context");
782 EXPORT_SYMBOL(audit_log_task_context);
784 static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
786 char name[sizeof(tsk->comm)];
787 struct mm_struct *mm = tsk->mm;
788 struct vm_area_struct *vma;
792 get_task_comm(name, tsk);
793 audit_log_format(ab, " comm=");
794 audit_log_untrustedstring(ab, name);
797 down_read(&mm->mmap_sem);
800 if ((vma->vm_flags & VM_EXECUTABLE) &&
802 audit_log_d_path(ab, "exe=",
803 vma->vm_file->f_path.dentry,
804 vma->vm_file->f_path.mnt);
809 up_read(&mm->mmap_sem);
811 audit_log_task_context(ab);
814 static int audit_log_pid_context(struct audit_context *context, pid_t pid,
817 struct audit_buffer *ab;
822 ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
826 if (selinux_sid_to_string(sid, &s, &len)) {
827 audit_log_format(ab, "opid=%d obj=(none)", pid);
830 audit_log_format(ab, "opid=%d obj=%s", pid, s);
837 static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
839 int i, call_panic = 0;
840 struct audit_buffer *ab;
841 struct audit_aux_data *aux;
845 context->pid = tsk->pid;
847 context->ppid = sys_getppid();
848 context->uid = tsk->uid;
849 context->gid = tsk->gid;
850 context->euid = tsk->euid;
851 context->suid = tsk->suid;
852 context->fsuid = tsk->fsuid;
853 context->egid = tsk->egid;
854 context->sgid = tsk->sgid;
855 context->fsgid = tsk->fsgid;
856 context->personality = tsk->personality;
858 ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
860 return; /* audit_panic has been called */
861 audit_log_format(ab, "arch=%x syscall=%d",
862 context->arch, context->major);
863 if (context->personality != PER_LINUX)
864 audit_log_format(ab, " per=%lx", context->personality);
865 if (context->return_valid)
866 audit_log_format(ab, " success=%s exit=%ld",
867 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
868 context->return_code);
870 mutex_lock(&tty_mutex);
871 read_lock(&tasklist_lock);
872 if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
873 tty = tsk->signal->tty->name;
876 read_unlock(&tasklist_lock);
878 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
879 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
880 " euid=%u suid=%u fsuid=%u"
881 " egid=%u sgid=%u fsgid=%u tty=%s",
892 context->euid, context->suid, context->fsuid,
893 context->egid, context->sgid, context->fsgid, tty);
895 mutex_unlock(&tty_mutex);
897 audit_log_task_info(ab, tsk);
898 if (context->filterkey) {
899 audit_log_format(ab, " key=");
900 audit_log_untrustedstring(ab, context->filterkey);
902 audit_log_format(ab, " key=(null)");
905 for (aux = context->aux; aux; aux = aux->next) {
907 ab = audit_log_start(context, GFP_KERNEL, aux->type);
909 continue; /* audit_panic has been called */
912 case AUDIT_MQ_OPEN: {
913 struct audit_aux_data_mq_open *axi = (void *)aux;
915 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
916 "mq_msgsize=%ld mq_curmsgs=%ld",
917 axi->oflag, axi->mode, axi->attr.mq_flags,
918 axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
919 axi->attr.mq_curmsgs);
922 case AUDIT_MQ_SENDRECV: {
923 struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
925 "mqdes=%d msg_len=%zd msg_prio=%u "
926 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
927 axi->mqdes, axi->msg_len, axi->msg_prio,
928 axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
931 case AUDIT_MQ_NOTIFY: {
932 struct audit_aux_data_mq_notify *axi = (void *)aux;
934 "mqdes=%d sigev_signo=%d",
936 axi->notification.sigev_signo);
939 case AUDIT_MQ_GETSETATTR: {
940 struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
942 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
945 axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
946 axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
950 struct audit_aux_data_ipcctl *axi = (void *)aux;
952 "ouid=%u ogid=%u mode=%x",
953 axi->uid, axi->gid, axi->mode);
954 if (axi->osid != 0) {
957 if (selinux_sid_to_string(
958 axi->osid, &ctx, &len)) {
959 audit_log_format(ab, " osid=%u",
963 audit_log_format(ab, " obj=%s", ctx);
968 case AUDIT_IPC_SET_PERM: {
969 struct audit_aux_data_ipcctl *axi = (void *)aux;
971 "qbytes=%lx ouid=%u ogid=%u mode=%x",
972 axi->qbytes, axi->uid, axi->gid, axi->mode);
976 struct audit_aux_data_execve *axi = (void *)aux;
979 for (i = 0, p = axi->mem; i < axi->argc; i++) {
980 audit_log_format(ab, "a%d=", i);
981 p = audit_log_untrustedstring(ab, p);
982 audit_log_format(ab, "\n");
986 case AUDIT_SOCKETCALL: {
988 struct audit_aux_data_socketcall *axs = (void *)aux;
989 audit_log_format(ab, "nargs=%d", axs->nargs);
990 for (i=0; i<axs->nargs; i++)
991 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
994 case AUDIT_SOCKADDR: {
995 struct audit_aux_data_sockaddr *axs = (void *)aux;
997 audit_log_format(ab, "saddr=");
998 audit_log_hex(ab, axs->a, axs->len);
1001 case AUDIT_AVC_PATH: {
1002 struct audit_aux_data_path *axi = (void *)aux;
1003 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
1006 case AUDIT_FD_PAIR: {
1007 struct audit_aux_data_fd_pair *axs = (void *)aux;
1008 audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
1015 for (aux = context->aux_pids; aux; aux = aux->next) {
1016 struct audit_aux_data_pids *axs = (void *)aux;
1019 for (i = 0; i < axs->pid_count; i++)
1020 if (audit_log_pid_context(context, axs->target_pid[i],
1021 axs->target_sid[i]))
1025 if (context->target_pid &&
1026 audit_log_pid_context(context, context->target_pid,
1027 context->target_sid))
1030 if (context->pwd && context->pwdmnt) {
1031 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
1033 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
1037 for (i = 0; i < context->name_count; i++) {
1038 struct audit_names *n = &context->names[i];
1040 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1042 continue; /* audit_panic has been called */
1044 audit_log_format(ab, "item=%d", i);
1047 switch(n->name_len) {
1048 case AUDIT_NAME_FULL:
1049 /* log the full path */
1050 audit_log_format(ab, " name=");
1051 audit_log_untrustedstring(ab, n->name);
1054 /* name was specified as a relative path and the
1055 * directory component is the cwd */
1056 audit_log_d_path(ab, " name=", context->pwd,
1060 /* log the name's directory component */
1061 audit_log_format(ab, " name=");
1062 audit_log_n_untrustedstring(ab, n->name_len,
1066 audit_log_format(ab, " name=(null)");
1068 if (n->ino != (unsigned long)-1) {
1069 audit_log_format(ab, " inode=%lu"
1070 " dev=%02x:%02x mode=%#o"
1071 " ouid=%u ogid=%u rdev=%02x:%02x",
1084 if (selinux_sid_to_string(
1085 n->osid, &ctx, &len)) {
1086 audit_log_format(ab, " osid=%u", n->osid);
1089 audit_log_format(ab, " obj=%s", ctx);
1096 audit_panic("error converting sid to string");
1100 * audit_free - free a per-task audit context
1101 * @tsk: task whose audit context block to free
1103 * Called from copy_process and do_exit
1105 void audit_free(struct task_struct *tsk)
1107 struct audit_context *context;
1109 context = audit_get_context(tsk, 0, 0);
1110 if (likely(!context))
1113 /* Check for system calls that do not go through the exit
1114 * function (e.g., exit_group), then free context block.
1115 * We use GFP_ATOMIC here because we might be doing this
1116 * in the context of the idle thread */
1117 /* that can happen only if we are called from do_exit() */
1118 if (context->in_syscall && context->auditable)
1119 audit_log_exit(context, tsk);
1121 audit_free_context(context);
1125 * audit_syscall_entry - fill in an audit record at syscall entry
1126 * @tsk: task being audited
1127 * @arch: architecture type
1128 * @major: major syscall type (function)
1129 * @a1: additional syscall register 1
1130 * @a2: additional syscall register 2
1131 * @a3: additional syscall register 3
1132 * @a4: additional syscall register 4
1134 * Fill in audit context at syscall entry. This only happens if the
1135 * audit context was created when the task was created and the state or
1136 * filters demand the audit context be built. If the state from the
1137 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1138 * then the record will be written at syscall exit time (otherwise, it
1139 * will only be written if another part of the kernel requests that it
1142 void audit_syscall_entry(int arch, int major,
1143 unsigned long a1, unsigned long a2,
1144 unsigned long a3, unsigned long a4)
1146 struct task_struct *tsk = current;
1147 struct audit_context *context = tsk->audit_context;
1148 enum audit_state state;
1153 * This happens only on certain architectures that make system
1154 * calls in kernel_thread via the entry.S interface, instead of
1155 * with direct calls. (If you are porting to a new
1156 * architecture, hitting this condition can indicate that you
1157 * got the _exit/_leave calls backward in entry.S.)
1161 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1163 * This also happens with vm86 emulation in a non-nested manner
1164 * (entries without exits), so this case must be caught.
1166 if (context->in_syscall) {
1167 struct audit_context *newctx;
1171 "audit(:%d) pid=%d in syscall=%d;"
1172 " entering syscall=%d\n",
1173 context->serial, tsk->pid, context->major, major);
1175 newctx = audit_alloc_context(context->state);
1177 newctx->previous = context;
1179 tsk->audit_context = newctx;
1181 /* If we can't alloc a new context, the best we
1182 * can do is to leak memory (any pending putname
1183 * will be lost). The only other alternative is
1184 * to abandon auditing. */
1185 audit_zero_context(context, context->state);
1188 BUG_ON(context->in_syscall || context->name_count);
1193 context->arch = arch;
1194 context->major = major;
1195 context->argv[0] = a1;
1196 context->argv[1] = a2;
1197 context->argv[2] = a3;
1198 context->argv[3] = a4;
1200 state = context->state;
1201 context->dummy = !audit_n_rules;
1202 if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
1203 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1204 if (likely(state == AUDIT_DISABLED))
1207 context->serial = 0;
1208 context->ctime = CURRENT_TIME;
1209 context->in_syscall = 1;
1210 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1215 * audit_syscall_exit - deallocate audit context after a system call
1216 * @tsk: task being audited
1217 * @valid: success/failure flag
1218 * @return_code: syscall return value
1220 * Tear down after system call. If the audit context has been marked as
1221 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1222 * filtering, or because some other part of the kernel write an audit
1223 * message), then write out the syscall information. In call cases,
1224 * free the names stored from getname().
1226 void audit_syscall_exit(int valid, long return_code)
1228 struct task_struct *tsk = current;
1229 struct audit_context *context;
1231 context = audit_get_context(tsk, valid, return_code);
1233 if (likely(!context))
1236 if (context->in_syscall && context->auditable)
1237 audit_log_exit(context, tsk);
1239 context->in_syscall = 0;
1240 context->auditable = 0;
1242 if (context->previous) {
1243 struct audit_context *new_context = context->previous;
1244 context->previous = NULL;
1245 audit_free_context(context);
1246 tsk->audit_context = new_context;
1248 audit_free_names(context);
1249 audit_free_aux(context);
1250 context->aux = NULL;
1251 context->aux_pids = NULL;
1252 context->target_pid = 0;
1253 context->target_sid = 0;
1254 kfree(context->filterkey);
1255 context->filterkey = NULL;
1256 tsk->audit_context = context;
1261 * audit_getname - add a name to the list
1262 * @name: name to add
1264 * Add a name to the list of audit names for this context.
1265 * Called from fs/namei.c:getname().
1267 void __audit_getname(const char *name)
1269 struct audit_context *context = current->audit_context;
1271 if (IS_ERR(name) || !name)
1274 if (!context->in_syscall) {
1275 #if AUDIT_DEBUG == 2
1276 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1277 __FILE__, __LINE__, context->serial, name);
1282 BUG_ON(context->name_count >= AUDIT_NAMES);
1283 context->names[context->name_count].name = name;
1284 context->names[context->name_count].name_len = AUDIT_NAME_FULL;
1285 context->names[context->name_count].name_put = 1;
1286 context->names[context->name_count].ino = (unsigned long)-1;
1287 context->names[context->name_count].osid = 0;
1288 ++context->name_count;
1289 if (!context->pwd) {
1290 read_lock(¤t->fs->lock);
1291 context->pwd = dget(current->fs->pwd);
1292 context->pwdmnt = mntget(current->fs->pwdmnt);
1293 read_unlock(¤t->fs->lock);
1298 /* audit_putname - intercept a putname request
1299 * @name: name to intercept and delay for putname
1301 * If we have stored the name from getname in the audit context,
1302 * then we delay the putname until syscall exit.
1303 * Called from include/linux/fs.h:putname().
1305 void audit_putname(const char *name)
1307 struct audit_context *context = current->audit_context;
1310 if (!context->in_syscall) {
1311 #if AUDIT_DEBUG == 2
1312 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1313 __FILE__, __LINE__, context->serial, name);
1314 if (context->name_count) {
1316 for (i = 0; i < context->name_count; i++)
1317 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1318 context->names[i].name,
1319 context->names[i].name ?: "(null)");
1326 ++context->put_count;
1327 if (context->put_count > context->name_count) {
1328 printk(KERN_ERR "%s:%d(:%d): major=%d"
1329 " in_syscall=%d putname(%p) name_count=%d"
1332 context->serial, context->major,
1333 context->in_syscall, name, context->name_count,
1334 context->put_count);
1341 static int audit_inc_name_count(struct audit_context *context,
1342 const struct inode *inode)
1344 if (context->name_count >= AUDIT_NAMES) {
1346 printk(KERN_DEBUG "name_count maxed, losing inode data: "
1347 "dev=%02x:%02x, inode=%lu",
1348 MAJOR(inode->i_sb->s_dev),
1349 MINOR(inode->i_sb->s_dev),
1353 printk(KERN_DEBUG "name_count maxed, losing inode data");
1356 context->name_count++;
1358 context->ino_count++;
1363 /* Copy inode data into an audit_names. */
1364 static void audit_copy_inode(struct audit_names *name, const struct inode *inode)
1366 name->ino = inode->i_ino;
1367 name->dev = inode->i_sb->s_dev;
1368 name->mode = inode->i_mode;
1369 name->uid = inode->i_uid;
1370 name->gid = inode->i_gid;
1371 name->rdev = inode->i_rdev;
1372 selinux_get_inode_sid(inode, &name->osid);
1376 * audit_inode - store the inode and device from a lookup
1377 * @name: name being audited
1378 * @inode: inode being audited
1380 * Called from fs/namei.c:path_lookup().
1382 void __audit_inode(const char *name, const struct inode *inode)
1385 struct audit_context *context = current->audit_context;
1387 if (!context->in_syscall)
1389 if (context->name_count
1390 && context->names[context->name_count-1].name
1391 && context->names[context->name_count-1].name == name)
1392 idx = context->name_count - 1;
1393 else if (context->name_count > 1
1394 && context->names[context->name_count-2].name
1395 && context->names[context->name_count-2].name == name)
1396 idx = context->name_count - 2;
1398 /* FIXME: how much do we care about inodes that have no
1399 * associated name? */
1400 if (audit_inc_name_count(context, inode))
1402 idx = context->name_count - 1;
1403 context->names[idx].name = NULL;
1405 audit_copy_inode(&context->names[idx], inode);
1409 * audit_inode_child - collect inode info for created/removed objects
1410 * @dname: inode's dentry name
1411 * @inode: inode being audited
1412 * @parent: inode of dentry parent
1414 * For syscalls that create or remove filesystem objects, audit_inode
1415 * can only collect information for the filesystem object's parent.
1416 * This call updates the audit context with the child's information.
1417 * Syscalls that create a new filesystem object must be hooked after
1418 * the object is created. Syscalls that remove a filesystem object
1419 * must be hooked prior, in order to capture the target inode during
1420 * unsuccessful attempts.
1422 void __audit_inode_child(const char *dname, const struct inode *inode,
1423 const struct inode *parent)
1426 struct audit_context *context = current->audit_context;
1427 const char *found_parent = NULL, *found_child = NULL;
1430 if (!context->in_syscall)
1433 /* determine matching parent */
1437 /* parent is more likely, look for it first */
1438 for (idx = 0; idx < context->name_count; idx++) {
1439 struct audit_names *n = &context->names[idx];
1444 if (n->ino == parent->i_ino &&
1445 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1446 n->name_len = dirlen; /* update parent data in place */
1447 found_parent = n->name;
1452 /* no matching parent, look for matching child */
1453 for (idx = 0; idx < context->name_count; idx++) {
1454 struct audit_names *n = &context->names[idx];
1459 /* strcmp() is the more likely scenario */
1460 if (!strcmp(dname, n->name) ||
1461 !audit_compare_dname_path(dname, n->name, &dirlen)) {
1463 audit_copy_inode(n, inode);
1465 n->ino = (unsigned long)-1;
1466 found_child = n->name;
1472 if (!found_parent) {
1473 if (audit_inc_name_count(context, parent))
1475 idx = context->name_count - 1;
1476 context->names[idx].name = NULL;
1477 audit_copy_inode(&context->names[idx], parent);
1481 if (audit_inc_name_count(context, inode))
1483 idx = context->name_count - 1;
1485 /* Re-use the name belonging to the slot for a matching parent
1486 * directory. All names for this context are relinquished in
1487 * audit_free_names() */
1489 context->names[idx].name = found_parent;
1490 context->names[idx].name_len = AUDIT_NAME_FULL;
1491 /* don't call __putname() */
1492 context->names[idx].name_put = 0;
1494 context->names[idx].name = NULL;
1498 audit_copy_inode(&context->names[idx], inode);
1500 context->names[idx].ino = (unsigned long)-1;
1505 * auditsc_get_stamp - get local copies of audit_context values
1506 * @ctx: audit_context for the task
1507 * @t: timespec to store time recorded in the audit_context
1508 * @serial: serial value that is recorded in the audit_context
1510 * Also sets the context as auditable.
1512 void auditsc_get_stamp(struct audit_context *ctx,
1513 struct timespec *t, unsigned int *serial)
1516 ctx->serial = audit_serial();
1517 t->tv_sec = ctx->ctime.tv_sec;
1518 t->tv_nsec = ctx->ctime.tv_nsec;
1519 *serial = ctx->serial;
1524 * audit_set_loginuid - set a task's audit_context loginuid
1525 * @task: task whose audit context is being modified
1526 * @loginuid: loginuid value
1530 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1532 int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1534 struct audit_context *context = task->audit_context;
1537 /* Only log if audit is enabled */
1538 if (context->in_syscall) {
1539 struct audit_buffer *ab;
1541 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
1543 audit_log_format(ab, "login pid=%d uid=%u "
1544 "old auid=%u new auid=%u",
1545 task->pid, task->uid,
1546 context->loginuid, loginuid);
1550 context->loginuid = loginuid;
1556 * audit_get_loginuid - get the loginuid for an audit_context
1557 * @ctx: the audit_context
1559 * Returns the context's loginuid or -1 if @ctx is NULL.
1561 uid_t audit_get_loginuid(struct audit_context *ctx)
1563 return ctx ? ctx->loginuid : -1;
1566 EXPORT_SYMBOL(audit_get_loginuid);
1569 * __audit_mq_open - record audit data for a POSIX MQ open
1572 * @u_attr: queue attributes
1574 * Returns 0 for success or NULL context or < 0 on error.
1576 int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
1578 struct audit_aux_data_mq_open *ax;
1579 struct audit_context *context = current->audit_context;
1584 if (likely(!context))
1587 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1591 if (u_attr != NULL) {
1592 if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
1597 memset(&ax->attr, 0, sizeof(ax->attr));
1602 ax->d.type = AUDIT_MQ_OPEN;
1603 ax->d.next = context->aux;
1604 context->aux = (void *)ax;
1609 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
1610 * @mqdes: MQ descriptor
1611 * @msg_len: Message length
1612 * @msg_prio: Message priority
1613 * @u_abs_timeout: Message timeout in absolute time
1615 * Returns 0 for success or NULL context or < 0 on error.
1617 int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
1618 const struct timespec __user *u_abs_timeout)
1620 struct audit_aux_data_mq_sendrecv *ax;
1621 struct audit_context *context = current->audit_context;
1626 if (likely(!context))
1629 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1633 if (u_abs_timeout != NULL) {
1634 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1639 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1642 ax->msg_len = msg_len;
1643 ax->msg_prio = msg_prio;
1645 ax->d.type = AUDIT_MQ_SENDRECV;
1646 ax->d.next = context->aux;
1647 context->aux = (void *)ax;
1652 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
1653 * @mqdes: MQ descriptor
1654 * @msg_len: Message length
1655 * @u_msg_prio: Message priority
1656 * @u_abs_timeout: Message timeout in absolute time
1658 * Returns 0 for success or NULL context or < 0 on error.
1660 int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
1661 unsigned int __user *u_msg_prio,
1662 const struct timespec __user *u_abs_timeout)
1664 struct audit_aux_data_mq_sendrecv *ax;
1665 struct audit_context *context = current->audit_context;
1670 if (likely(!context))
1673 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1677 if (u_msg_prio != NULL) {
1678 if (get_user(ax->msg_prio, u_msg_prio)) {
1685 if (u_abs_timeout != NULL) {
1686 if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
1691 memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
1694 ax->msg_len = msg_len;
1696 ax->d.type = AUDIT_MQ_SENDRECV;
1697 ax->d.next = context->aux;
1698 context->aux = (void *)ax;
1703 * __audit_mq_notify - record audit data for a POSIX MQ notify
1704 * @mqdes: MQ descriptor
1705 * @u_notification: Notification event
1707 * Returns 0 for success or NULL context or < 0 on error.
1710 int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
1712 struct audit_aux_data_mq_notify *ax;
1713 struct audit_context *context = current->audit_context;
1718 if (likely(!context))
1721 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1725 if (u_notification != NULL) {
1726 if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
1731 memset(&ax->notification, 0, sizeof(ax->notification));
1735 ax->d.type = AUDIT_MQ_NOTIFY;
1736 ax->d.next = context->aux;
1737 context->aux = (void *)ax;
1742 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
1743 * @mqdes: MQ descriptor
1746 * Returns 0 for success or NULL context or < 0 on error.
1748 int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
1750 struct audit_aux_data_mq_getsetattr *ax;
1751 struct audit_context *context = current->audit_context;
1756 if (likely(!context))
1759 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1764 ax->mqstat = *mqstat;
1766 ax->d.type = AUDIT_MQ_GETSETATTR;
1767 ax->d.next = context->aux;
1768 context->aux = (void *)ax;
1773 * audit_ipc_obj - record audit data for ipc object
1774 * @ipcp: ipc permissions
1776 * Returns 0 for success or NULL context or < 0 on error.
1778 int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
1780 struct audit_aux_data_ipcctl *ax;
1781 struct audit_context *context = current->audit_context;
1783 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1787 ax->uid = ipcp->uid;
1788 ax->gid = ipcp->gid;
1789 ax->mode = ipcp->mode;
1790 selinux_get_ipc_sid(ipcp, &ax->osid);
1792 ax->d.type = AUDIT_IPC;
1793 ax->d.next = context->aux;
1794 context->aux = (void *)ax;
1799 * audit_ipc_set_perm - record audit data for new ipc permissions
1800 * @qbytes: msgq bytes
1801 * @uid: msgq user id
1802 * @gid: msgq group id
1803 * @mode: msgq mode (permissions)
1805 * Returns 0 for success or NULL context or < 0 on error.
1807 int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1809 struct audit_aux_data_ipcctl *ax;
1810 struct audit_context *context = current->audit_context;
1812 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1816 ax->qbytes = qbytes;
1821 ax->d.type = AUDIT_IPC_SET_PERM;
1822 ax->d.next = context->aux;
1823 context->aux = (void *)ax;
1827 int audit_bprm(struct linux_binprm *bprm)
1829 struct audit_aux_data_execve *ax;
1830 struct audit_context *context = current->audit_context;
1831 unsigned long p, next;
1834 if (likely(!audit_enabled || !context || context->dummy))
1837 ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
1842 ax->argc = bprm->argc;
1843 ax->envc = bprm->envc;
1844 for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
1845 struct page *page = bprm->page[p / PAGE_SIZE];
1846 void *kaddr = kmap(page);
1847 next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1848 memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
1853 ax->d.type = AUDIT_EXECVE;
1854 ax->d.next = context->aux;
1855 context->aux = (void *)ax;
1861 * audit_socketcall - record audit data for sys_socketcall
1862 * @nargs: number of args
1865 * Returns 0 for success or NULL context or < 0 on error.
1867 int audit_socketcall(int nargs, unsigned long *args)
1869 struct audit_aux_data_socketcall *ax;
1870 struct audit_context *context = current->audit_context;
1872 if (likely(!context || context->dummy))
1875 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1880 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1882 ax->d.type = AUDIT_SOCKETCALL;
1883 ax->d.next = context->aux;
1884 context->aux = (void *)ax;
1889 * __audit_fd_pair - record audit data for pipe and socketpair
1890 * @fd1: the first file descriptor
1891 * @fd2: the second file descriptor
1893 * Returns 0 for success or NULL context or < 0 on error.
1895 int __audit_fd_pair(int fd1, int fd2)
1897 struct audit_context *context = current->audit_context;
1898 struct audit_aux_data_fd_pair *ax;
1900 if (likely(!context)) {
1904 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1912 ax->d.type = AUDIT_FD_PAIR;
1913 ax->d.next = context->aux;
1914 context->aux = (void *)ax;
1919 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1920 * @len: data length in user space
1921 * @a: data address in kernel space
1923 * Returns 0 for success or NULL context or < 0 on error.
1925 int audit_sockaddr(int len, void *a)
1927 struct audit_aux_data_sockaddr *ax;
1928 struct audit_context *context = current->audit_context;
1930 if (likely(!context || context->dummy))
1933 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1938 memcpy(ax->a, a, len);
1940 ax->d.type = AUDIT_SOCKADDR;
1941 ax->d.next = context->aux;
1942 context->aux = (void *)ax;
1946 void __audit_ptrace(struct task_struct *t)
1948 struct audit_context *context = current->audit_context;
1950 context->target_pid = t->pid;
1951 selinux_get_task_sid(t, &context->target_sid);
1955 * audit_avc_path - record the granting or denial of permissions
1956 * @dentry: dentry to record
1957 * @mnt: mnt to record
1959 * Returns 0 for success or NULL context or < 0 on error.
1961 * Called from security/selinux/avc.c::avc_audit()
1963 int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1965 struct audit_aux_data_path *ax;
1966 struct audit_context *context = current->audit_context;
1968 if (likely(!context))
1971 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1975 ax->dentry = dget(dentry);
1976 ax->mnt = mntget(mnt);
1978 ax->d.type = AUDIT_AVC_PATH;
1979 ax->d.next = context->aux;
1980 context->aux = (void *)ax;
1985 * audit_signal_info - record signal info for shutting down audit subsystem
1986 * @sig: signal value
1987 * @t: task being signaled
1989 * If the audit subsystem is being terminated, record the task (pid)
1990 * and uid that is doing that.
1992 int __audit_signal_info(int sig, struct task_struct *t)
1994 struct audit_aux_data_pids *axp;
1995 struct task_struct *tsk = current;
1996 struct audit_context *ctx = tsk->audit_context;
1997 extern pid_t audit_sig_pid;
1998 extern uid_t audit_sig_uid;
1999 extern u32 audit_sig_sid;
2001 if (audit_pid && t->tgid == audit_pid &&
2002 (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1)) {
2003 audit_sig_pid = tsk->pid;
2005 audit_sig_uid = ctx->loginuid;
2007 audit_sig_uid = tsk->uid;
2008 selinux_get_task_sid(tsk, &audit_sig_sid);
2011 if (!audit_signals) /* audit_context checked in wrapper */
2014 /* optimize the common case by putting first signal recipient directly
2015 * in audit_context */
2016 if (!ctx->target_pid) {
2017 ctx->target_pid = t->tgid;
2018 selinux_get_task_sid(t, &ctx->target_sid);
2022 axp = (void *)ctx->aux_pids;
2023 if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
2024 axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
2028 axp->d.type = AUDIT_OBJ_PID;
2029 axp->d.next = ctx->aux_pids;
2030 ctx->aux_pids = (void *)axp;
2032 BUG_ON(axp->pid_count > AUDIT_AUX_PIDS);
2034 axp->target_pid[axp->pid_count] = t->tgid;
2035 selinux_get_task_sid(t, &axp->target_sid[axp->pid_count]);
2042 * audit_core_dumps - record information about processes that end abnormally
2043 * @sig: signal value
2045 * If a process ends with a core dump, something fishy is going on and we
2046 * should record the event for investigation.
2048 void audit_core_dumps(long signr)
2050 struct audit_buffer *ab;
2056 if (signr == SIGQUIT) /* don't care for those */
2059 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
2060 audit_log_format(ab, "auid=%u uid=%u gid=%u",
2061 audit_get_loginuid(current->audit_context),
2062 current->uid, current->gid);
2063 selinux_get_task_sid(current, &sid);
2068 if (selinux_sid_to_string(sid, &ctx, &len))
2069 audit_log_format(ab, " ssid=%u", sid);
2071 audit_log_format(ab, " subj=%s", ctx);
2074 audit_log_format(ab, " pid=%d comm=", current->pid);
2075 audit_log_untrustedstring(ab, current->comm);
2076 audit_log_format(ab, " sig=%ld", signr);