46 #include <asm/types.h>
51 #include <linux/export.h>
52 #include <linux/slab.h>
54 #include <linux/socket.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>
63 #include <linux/list.h>
64 #include <linux/tty.h>
65 #include <linux/binfmts.h>
68 #include <linux/capability.h>
75 #define AUDITSC_INVALID 0
76 #define AUDITSC_SUCCESS 1
77 #define AUDITSC_FAILURE 2
85 #define MAX_EXECVE_AUDIT_LEN 7500
136 #define AUDIT_AUX_IPCPERM 0
139 #define AUDIT_AUX_PIDS 16
279 static inline int open_arg(
int flags,
int mask)
307 if ((mask & AUDIT_PERM_WRITE) &&
310 if ((mask & AUDIT_PERM_READ) &&
313 if ((mask & AUDIT_PERM_ATTR) &&
322 return ((mask & AUDIT_PERM_WRITE) && ctx->
argv[0] ==
SYS_BIND);
339 if ((n->
ino != -1) &&
357 #ifdef CONFIG_AUDIT_TREE
407 #ifdef CONFIG_AUDIT_TREE
419 for (q = p; q != ctx->
trees; q = q->
next, n = 31) {
445 #ifdef CONFIG_AUDIT_TREE
452 for (n = 0; n < 31; n++)
514 static int audit_field_compare(
struct task_struct *tsk,
523 return audit_compare_uid(cred->
uid, name, f, ctx);
525 return audit_compare_gid(cred->
gid, name, f, ctx);
527 return audit_compare_uid(cred->
euid, name, f, ctx);
529 return audit_compare_gid(cred->
egid, name, f, ctx);
531 return audit_compare_uid(tsk->loginuid, name, f, ctx);
533 return audit_compare_uid(cred->
suid, name, f, ctx);
535 return audit_compare_gid(cred->
sgid, name, f, ctx);
537 return audit_compare_uid(cred->
fsuid, name, f, ctx);
539 return audit_compare_gid(cred->
fsgid, name, f, ctx);
580 WARN(1,
"Missing AUDIT_COMPARE define. Report as a bug\n");
594 static int audit_filter_rules(
struct task_struct *tsk,
601 const struct cred *cred;
699 result = (name->
ino == f->
val);
739 result = match_tree_refs(ctx, rule->
tree);
761 result = security_audit_rule_match(sid, f->
type,
777 result = security_audit_rule_match(
782 if (security_audit_rule_match(n->
osid, f->
type,
793 if (security_audit_rule_match(ctx->
ipc.osid,
811 result = audit_match_perm(ctx, f->
val);
814 result = audit_match_filetype(ctx, f->
val);
817 result = audit_field_compare(tsk, cred, f, ctx, name);
879 if (!list_empty(list)) {
883 list_for_each_entry_rcu(e, list, list) {
884 if ((e->
rule.mask[word] & bit) == bit &&
885 audit_filter_rules(tsk, &e->
rule, ctx,
NULL,
901 static int audit_filter_inode_name(
struct task_struct *tsk,
905 int h = audit_hash_ino((
u32)n->
ino);
913 if (list_empty(list))
916 list_for_each_entry_rcu(e, list, list) {
917 if ((e->
rule.mask[word] & bit) == bit &&
918 audit_filter_rules(tsk, &e->
rule, ctx, n, &state,
false)) {
942 if (audit_filter_inode_name(tsk, n, ctx))
985 static inline void audit_free_names(
struct audit_context *context)
990 if (context->put_count + context->ino_count != context->
name_count) {
992 " name_count=%d put_count=%d"
993 " ino_count=%d [NOT freeing]\n",
1000 n->
name, n->
name->name ?:
"(null)");
1007 context->put_count = 0;
1008 context->ino_count = 0;
1024 static inline void audit_free_aux(
struct audit_context *context)
1028 while ((aux = context->
aux)) {
1032 while ((aux = context->
aux_pids)) {
1038 static inline void audit_zero_context(
struct audit_context *context,
1041 memset(context, 0,
sizeof(*context));
1042 context->state =
state;
1052 audit_zero_context(context, state);
1076 state = audit_filter_task(tsk, &key);
1080 if (!(context = audit_alloc_context(state))) {
1092 static inline void audit_free_context(
struct audit_context *context)
1099 if (previous || (count && count < 10)) {
1102 " freeing multiple contexts (%d)\n",
1106 audit_free_names(context);
1107 unroll_tree_refs(context,
NULL, 0);
1108 free_tree_refs(context);
1109 audit_free_aux(context);
1150 const struct cred *cred;
1151 char name[
sizeof(tsk->
comm)];
1161 spin_lock_irq(&tsk->
sighand->siglock);
1163 tty = tsk->
signal->tty->name;
1166 spin_unlock_irq(&tsk->
sighand->siglock);
1170 " ppid=%ld pid=%d auid=%u uid=%u gid=%u"
1171 " euid=%u suid=%u fsuid=%u"
1172 " egid=%u sgid=%u fsgid=%u ses=%u tty=%s",
1184 tsk->sessionid, tty);
1203 u32 sid,
char *comm)
1242 static int audit_log_single_execve_arg(
struct audit_context *context,
1246 const char __user *p,
1249 char arg_num_len_buf[12];
1252 size_t arg_num_len =
snprintf(arg_num_len_buf, 12,
"%d", arg_num) + 5;
1253 size_t len, len_left, to_send;
1255 unsigned int i, has_cntl = 0, too_long = 0;
1290 buf[to_send] =
'\0';
1300 len_left -= to_send;
1302 }
while (len_left > 0);
1306 if (len > max_execve_audit_len)
1310 for (i = 0; len_left > 0; i++) {
1313 if (len_left > max_execve_audit_len)
1314 to_send = max_execve_audit_len;
1321 room_left -= (to_send * 2);
1323 room_left -= to_send;
1324 if (room_left < 0) {
1336 if ((i == 0) && (too_long))
1338 has_cntl ? 2*len : len);
1345 if (len >= max_execve_audit_len)
1354 buf[to_send] =
'\0';
1364 audit_log_string(*ab, buf);
1367 len_left -= to_send;
1368 *len_sent += arg_num_len;
1370 *len_sent += to_send * 2;
1372 *len_sent += to_send;
1378 static void audit_log_execve_info(
struct audit_context *context,
1383 size_t len_sent = 0;
1390 p = (
const char __user *)axi->
mm->arg_start;
1406 for (i = 0; i < axi->
argc; i++) {
1407 len = audit_log_single_execve_arg(context, ab, i,
1432 if (!cap_isclear(*perm)) {
1433 audit_log_cap(ab,
"cap_fp", perm);
1436 if (!cap_isclear(*inh)) {
1437 audit_log_cap(ab,
"cap_fi", inh);
1445 static void show_special(
struct audit_context *context,
int *call_panic)
1454 switch (context->
type) {
1458 for (i = 0; i < nargs; i++)
1463 u32 osid = context->
ipc.osid;
1480 if (context->
ipc.has_perm) {
1485 "qbytes=%lx ouid=%u ogid=%u mode=%#ho",
1486 context->
ipc.qbytes,
1487 context->
ipc.perm_uid,
1488 context->
ipc.perm_gid,
1489 context->
ipc.perm_mode);
1496 "oflag=0x%x mode=%#ho mq_flags=0x%lx mq_maxmsg=%ld "
1497 "mq_msgsize=%ld mq_curmsgs=%ld",
1499 context->
mq_open.attr.mq_flags,
1500 context->
mq_open.attr.mq_maxmsg,
1501 context->
mq_open.attr.mq_msgsize,
1502 context->
mq_open.attr.mq_curmsgs);
1506 "mqdes=%d msg_len=%zd msg_prio=%u "
1507 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1522 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1530 audit_log_cap(ab,
"cap_pi", &context->
capset.cap.inheritable);
1531 audit_log_cap(ab,
"cap_pp", &context->
capset.cap.permitted);
1532 audit_log_cap(ab,
"cap_pe", &context->
capset.cap.effective);
1536 context->
mmap.flags);
1573 if (n->
ino != (
unsigned long)-1) {
1575 " dev=%02x:%02x mode=%#ho"
1576 " ouid=%u ogid=%u rdev=%02x:%02x",
1590 n->
osid, &ctx, &len)) {
1599 audit_log_fcaps(ab, n);
1606 int i, call_panic = 0;
1627 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d",
1638 for (aux = context->
aux; aux; aux = aux->
next) {
1644 switch (aux->
type) {
1648 audit_log_execve_info(context, &ab, axi);
1654 audit_log_cap(ab,
"fp", &axs->
fcap.permitted);
1655 audit_log_cap(ab,
"fi", &axs->
fcap.inheritable);
1657 audit_log_cap(ab,
"old_pp", &axs->
old_pcap.permitted);
1658 audit_log_cap(ab,
"old_pi", &axs->
old_pcap.inheritable);
1659 audit_log_cap(ab,
"old_pe", &axs->
old_pcap.effective);
1660 audit_log_cap(ab,
"new_pp", &axs->
new_pcap.permitted);
1661 audit_log_cap(ab,
"new_pi", &axs->
new_pcap.inheritable);
1662 audit_log_cap(ab,
"new_pe", &axs->
new_pcap.effective);
1670 show_special(context, &call_panic);
1672 if (context->
fds[0] >= 0) {
1676 context->
fds[0], context->
fds[1]);
1695 if (audit_log_pid_context(context, axs->
target_pid[i],
1705 audit_log_pid_context(context, context->
target_pid,
1711 if (context->
pwd.dentry && context->
pwd.mnt) {
1721 audit_log_name(context, n, i++, &call_panic);
1741 context = audit_get_context(tsk, 0, 0);
1751 audit_log_exit(context, tsk);
1755 audit_free_context(context);
1776 unsigned long a1,
unsigned long a2,
1777 unsigned long a3,
unsigned long a4)
1805 "audit(:%d) pid=%d in syscall=%d;"
1806 " entering syscall=%d\n",
1809 newctx = audit_alloc_context(context->state);
1819 audit_zero_context(context, context->state);
1834 state = context->state;
1871 context = audit_get_context(tsk, success, return_code);
1876 audit_log_exit(context, tsk);
1887 audit_free_context(context);
1890 audit_free_names(context);
1891 unroll_tree_refs(context,
NULL, 0);
1892 audit_free_aux(context);
1899 context->
fds[0] = -1;
1908 static inline void handle_one(
const struct inode *
inode)
1910 #ifdef CONFIG_AUDIT_TREE
1915 if (
likely(hlist_empty(&inode->i_fsnotify_marks)))
1917 context =
current->audit_context;
1925 if (
likely(put_tree_ref(context, chunk)))
1927 if (
unlikely(!grow_tree_refs(context))) {
1929 audit_set_auditable(context);
1931 unroll_tree_refs(context, p, count);
1934 put_tree_ref(context, chunk);
1940 #ifdef CONFIG_AUDIT_TREE
1943 const struct dentry *
d, *
parent;
1948 context =
current->audit_context;
1955 seq = read_seqbegin(&rename_lock);
1957 struct inode *inode = d->
d_inode;
1958 if (inode &&
unlikely(!hlist_empty(&inode->i_fsnotify_marks))) {
1962 if (
unlikely(!put_tree_ref(context, chunk))) {
1973 if (
unlikely(read_seqretry(&rename_lock, seq) || drop)) {
1977 unroll_tree_refs(context, p, count);
1981 if (grow_tree_refs(context)) {
1983 unroll_tree_refs(context, p, count);
1988 "out of memory, audit has lost a tree reference\n");
1989 unroll_tree_refs(context, p, count);
1990 audit_set_auditable(context);
2004 memset(aname, 0,
sizeof(*aname));
2006 aname = kzalloc(
sizeof(*aname),
GFP_NOFS);
2012 aname->
ino = (
unsigned long)-1;
2018 context->ino_count++;
2040 if (n->
name->uptr == uptr)
2059 #if AUDIT_DEBUG == 2
2061 __FILE__, __LINE__, context->
serial, name);
2081 if (!context->
pwd.dentry)
2098 #if AUDIT_DEBUG == 2
2100 __FILE__, __LINE__, context->
serial, name);
2107 n->
name, n->
name->name ?:
"(null)");
2114 ++context->put_count;
2115 if (context->put_count > context->
name_count) {
2117 " in_syscall=%d putname(%p) name_count=%d"
2129 static inline int audit_copy_fcaps(
struct audit_names *name,
const struct dentry *dentry)
2141 name->
fcap.permitted =
caps.permitted;
2142 name->
fcap.inheritable =
caps.inheritable;
2151 static void audit_copy_inode(
struct audit_names *name,
const struct dentry *dentry,
2152 const struct inode *inode)
2155 name->
dev = inode->
i_sb->s_dev;
2161 audit_copy_fcaps(name, dentry);
2171 unsigned int parent)
2174 const struct inode *inode = dentry->
d_inode;
2234 handle_path(dentry);
2235 audit_copy_inode(n, dentry, inode);
2253 const struct dentry *dentry,
2254 const unsigned char type)
2257 const struct inode *inode = dentry->
d_inode;
2258 const char *dname = dentry->
d_name.name;
2286 if (found_parent && (n->
name != found_parent->
name))
2299 if (!found_parent) {
2304 audit_copy_inode(n,
NULL, parent);
2308 found_child = audit_alloc_name(context, type);
2316 found_child->name = found_parent->
name;
2319 found_child->name_put =
false;
2323 audit_copy_inode(found_child, dentry, inode);
2325 found_child->ino = (
unsigned long)-1;
2369 unsigned int sessionid;
2371 #ifdef CONFIG_AUDIT_LOGINUID_IMMUTABLE
2372 if (uid_valid(task->loginuid))
2386 "old auid=%u new auid=%u"
2387 " old ses=%u new ses=%u",
2392 task->sessionid, sessionid);
2396 task->sessionid = sessionid;
2397 task->loginuid = loginuid;
2491 context->
ipc.uid = ipcp->
uid;
2492 context->
ipc.gid = ipcp->
gid;
2493 context->
ipc.mode = ipcp->
mode;
2494 context->
ipc.has_perm = 0;
2513 context->
ipc.perm_uid =
uid;
2514 context->
ipc.perm_gid =
gid;
2515 context->
ipc.perm_mode =
mode;
2516 context->
ipc.has_perm = 1;
2532 ax->
d.next = context->
aux;
2533 context->
aux = (
void *)ax;
2562 context->
fds[0] = fd1;
2563 context->
fds[1] = fd2;
2619 if (uid_valid(tsk->loginuid))
2676 const struct cred *
new,
const struct cred *old)
2681 struct dentry *dentry;
2688 ax->
d.next = context->
aux;
2689 context->
aux = (
void *)ax;
2691 dentry = dget(bprm->
file->f_dentry);
2704 ax->
new_pcap.permitted =
new->cap_permitted;
2705 ax->
new_pcap.inheritable =
new->cap_inheritable;
2706 ax->
new_pcap.effective =
new->cap_effective;
2720 const struct cred *
new,
const struct cred *old)
2724 context->
capset.cap.effective =
new->cap_effective;
2725 context->
capset.cap.inheritable =
new->cap_effective;
2726 context->
capset.cap.permitted =
new->cap_permitted;
2742 unsigned int sessionid;
2744 auid = audit_get_loginuid(
current);
2745 sessionid = audit_get_sessionid(
current);
2757 audit_log_string(ab, reason);
2778 audit_log_abend(ab,
"memory violation", signr);
2787 audit_log_abend(ab,
"seccomp", signr);