aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/seccomp.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/seccomp.c')
-rw-r--r--kernel/seccomp.c344
1 files changed, 326 insertions, 18 deletions
diff --git a/kernel/seccomp.c b/kernel/seccomp.c
index 98b59b5db90b..bb3a38005b9c 100644
--- a/kernel/seccomp.c
+++ b/kernel/seccomp.c
@@ -17,11 +17,13 @@
17#include <linux/audit.h> 17#include <linux/audit.h>
18#include <linux/compat.h> 18#include <linux/compat.h>
19#include <linux/coredump.h> 19#include <linux/coredump.h>
20#include <linux/kmemleak.h>
20#include <linux/sched.h> 21#include <linux/sched.h>
21#include <linux/sched/task_stack.h> 22#include <linux/sched/task_stack.h>
22#include <linux/seccomp.h> 23#include <linux/seccomp.h>
23#include <linux/slab.h> 24#include <linux/slab.h>
24#include <linux/syscalls.h> 25#include <linux/syscalls.h>
26#include <linux/sysctl.h>
25 27
26#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER 28#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
27#include <asm/syscall.h> 29#include <asm/syscall.h>
@@ -42,6 +44,7 @@
42 * get/put helpers should be used when accessing an instance 44 * get/put helpers should be used when accessing an instance
43 * outside of a lifetime-guarded section. In general, this 45 * outside of a lifetime-guarded section. In general, this
44 * is only needed for handling filters shared across tasks. 46 * is only needed for handling filters shared across tasks.
47 * @log: true if all actions except for SECCOMP_RET_ALLOW should be logged
45 * @prev: points to a previously installed, or inherited, filter 48 * @prev: points to a previously installed, or inherited, filter
46 * @prog: the BPF program to evaluate 49 * @prog: the BPF program to evaluate
47 * 50 *
@@ -57,6 +60,7 @@
57 */ 60 */
58struct seccomp_filter { 61struct seccomp_filter {
59 refcount_t usage; 62 refcount_t usage;
63 bool log;
60 struct seccomp_filter *prev; 64 struct seccomp_filter *prev;
61 struct bpf_prog *prog; 65 struct bpf_prog *prog;
62}; 66};
@@ -171,10 +175,15 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
171/** 175/**
172 * seccomp_run_filters - evaluates all seccomp filters against @sd 176 * seccomp_run_filters - evaluates all seccomp filters against @sd
173 * @sd: optional seccomp data to be passed to filters 177 * @sd: optional seccomp data to be passed to filters
178 * @match: stores struct seccomp_filter that resulted in the return value,
179 * unless filter returned SECCOMP_RET_ALLOW, in which case it will
180 * be unchanged.
174 * 181 *
175 * Returns valid seccomp BPF response codes. 182 * Returns valid seccomp BPF response codes.
176 */ 183 */
177static u32 seccomp_run_filters(const struct seccomp_data *sd) 184#define ACTION_ONLY(ret) ((s32)((ret) & (SECCOMP_RET_ACTION_FULL)))
185static u32 seccomp_run_filters(const struct seccomp_data *sd,
186 struct seccomp_filter **match)
178{ 187{
179 struct seccomp_data sd_local; 188 struct seccomp_data sd_local;
180 u32 ret = SECCOMP_RET_ALLOW; 189 u32 ret = SECCOMP_RET_ALLOW;
@@ -184,7 +193,7 @@ static u32 seccomp_run_filters(const struct seccomp_data *sd)
184 193
185 /* Ensure unexpected behavior doesn't result in failing open. */ 194 /* Ensure unexpected behavior doesn't result in failing open. */
186 if (unlikely(WARN_ON(f == NULL))) 195 if (unlikely(WARN_ON(f == NULL)))
187 return SECCOMP_RET_KILL; 196 return SECCOMP_RET_KILL_PROCESS;
188 197
189 if (!sd) { 198 if (!sd) {
190 populate_seccomp_data(&sd_local); 199 populate_seccomp_data(&sd_local);
@@ -198,8 +207,10 @@ static u32 seccomp_run_filters(const struct seccomp_data *sd)
198 for (; f; f = f->prev) { 207 for (; f; f = f->prev) {
199 u32 cur_ret = BPF_PROG_RUN(f->prog, sd); 208 u32 cur_ret = BPF_PROG_RUN(f->prog, sd);
200 209
201 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION)) 210 if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
202 ret = cur_ret; 211 ret = cur_ret;
212 *match = f;
213 }
203 } 214 }
204 return ret; 215 return ret;
205} 216}
@@ -444,6 +455,10 @@ static long seccomp_attach_filter(unsigned int flags,
444 return ret; 455 return ret;
445 } 456 }
446 457
458 /* Set log flag, if present. */
459 if (flags & SECCOMP_FILTER_FLAG_LOG)
460 filter->log = true;
461
447 /* 462 /*
448 * If there is an existing filter, make it the prev and don't drop its 463 * If there is an existing filter, make it the prev and don't drop its
449 * task reference. 464 * task reference.
@@ -458,14 +473,19 @@ static long seccomp_attach_filter(unsigned int flags,
458 return 0; 473 return 0;
459} 474}
460 475
476void __get_seccomp_filter(struct seccomp_filter *filter)
477{
478 /* Reference count is bounded by the number of total processes. */
479 refcount_inc(&filter->usage);
480}
481
461/* get_seccomp_filter - increments the reference count of the filter on @tsk */ 482/* get_seccomp_filter - increments the reference count of the filter on @tsk */
462void get_seccomp_filter(struct task_struct *tsk) 483void get_seccomp_filter(struct task_struct *tsk)
463{ 484{
464 struct seccomp_filter *orig = tsk->seccomp.filter; 485 struct seccomp_filter *orig = tsk->seccomp.filter;
465 if (!orig) 486 if (!orig)
466 return; 487 return;
467 /* Reference count is bounded by the number of total processes. */ 488 __get_seccomp_filter(orig);
468 refcount_inc(&orig->usage);
469} 489}
470 490
471static inline void seccomp_filter_free(struct seccomp_filter *filter) 491static inline void seccomp_filter_free(struct seccomp_filter *filter)
@@ -476,10 +496,8 @@ static inline void seccomp_filter_free(struct seccomp_filter *filter)
476 } 496 }
477} 497}
478 498
479/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */ 499static void __put_seccomp_filter(struct seccomp_filter *orig)
480void put_seccomp_filter(struct task_struct *tsk)
481{ 500{
482 struct seccomp_filter *orig = tsk->seccomp.filter;
483 /* Clean up single-reference branches iteratively. */ 501 /* Clean up single-reference branches iteratively. */
484 while (orig && refcount_dec_and_test(&orig->usage)) { 502 while (orig && refcount_dec_and_test(&orig->usage)) {
485 struct seccomp_filter *freeme = orig; 503 struct seccomp_filter *freeme = orig;
@@ -488,6 +506,12 @@ void put_seccomp_filter(struct task_struct *tsk)
488 } 506 }
489} 507}
490 508
509/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
510void put_seccomp_filter(struct task_struct *tsk)
511{
512 __put_seccomp_filter(tsk->seccomp.filter);
513}
514
491static void seccomp_init_siginfo(siginfo_t *info, int syscall, int reason) 515static void seccomp_init_siginfo(siginfo_t *info, int syscall, int reason)
492{ 516{
493 memset(info, 0, sizeof(*info)); 517 memset(info, 0, sizeof(*info));
@@ -514,6 +538,65 @@ static void seccomp_send_sigsys(int syscall, int reason)
514} 538}
515#endif /* CONFIG_SECCOMP_FILTER */ 539#endif /* CONFIG_SECCOMP_FILTER */
516 540
541/* For use with seccomp_actions_logged */
542#define SECCOMP_LOG_KILL_PROCESS (1 << 0)
543#define SECCOMP_LOG_KILL_THREAD (1 << 1)
544#define SECCOMP_LOG_TRAP (1 << 2)
545#define SECCOMP_LOG_ERRNO (1 << 3)
546#define SECCOMP_LOG_TRACE (1 << 4)
547#define SECCOMP_LOG_LOG (1 << 5)
548#define SECCOMP_LOG_ALLOW (1 << 6)
549
550static u32 seccomp_actions_logged = SECCOMP_LOG_KILL_PROCESS |
551 SECCOMP_LOG_KILL_THREAD |
552 SECCOMP_LOG_TRAP |
553 SECCOMP_LOG_ERRNO |
554 SECCOMP_LOG_TRACE |
555 SECCOMP_LOG_LOG;
556
557static inline void seccomp_log(unsigned long syscall, long signr, u32 action,
558 bool requested)
559{
560 bool log = false;
561
562 switch (action) {
563 case SECCOMP_RET_ALLOW:
564 break;
565 case SECCOMP_RET_TRAP:
566 log = requested && seccomp_actions_logged & SECCOMP_LOG_TRAP;
567 break;
568 case SECCOMP_RET_ERRNO:
569 log = requested && seccomp_actions_logged & SECCOMP_LOG_ERRNO;
570 break;
571 case SECCOMP_RET_TRACE:
572 log = requested && seccomp_actions_logged & SECCOMP_LOG_TRACE;
573 break;
574 case SECCOMP_RET_LOG:
575 log = seccomp_actions_logged & SECCOMP_LOG_LOG;
576 break;
577 case SECCOMP_RET_KILL_THREAD:
578 log = seccomp_actions_logged & SECCOMP_LOG_KILL_THREAD;
579 break;
580 case SECCOMP_RET_KILL_PROCESS:
581 default:
582 log = seccomp_actions_logged & SECCOMP_LOG_KILL_PROCESS;
583 }
584
585 /*
586 * Force an audit message to be emitted when the action is RET_KILL_*,
587 * RET_LOG, or the FILTER_FLAG_LOG bit was set and the action is
588 * allowed to be logged by the admin.
589 */
590 if (log)
591 return __audit_seccomp(syscall, signr, action);
592
593 /*
594 * Let the audit subsystem decide if the action should be audited based
595 * on whether the current task itself is being audited.
596 */
597 return audit_seccomp(syscall, signr, action);
598}
599
517/* 600/*
518 * Secure computing mode 1 allows only read/write/exit/sigreturn. 601 * Secure computing mode 1 allows only read/write/exit/sigreturn.
519 * To be fully secure this must be combined with rlimit 602 * To be fully secure this must be combined with rlimit
@@ -539,7 +622,7 @@ static void __secure_computing_strict(int this_syscall)
539#ifdef SECCOMP_DEBUG 622#ifdef SECCOMP_DEBUG
540 dump_stack(); 623 dump_stack();
541#endif 624#endif
542 audit_seccomp(this_syscall, SIGKILL, SECCOMP_RET_KILL); 625 seccomp_log(this_syscall, SIGKILL, SECCOMP_RET_KILL_THREAD, true);
543 do_exit(SIGKILL); 626 do_exit(SIGKILL);
544} 627}
545 628
@@ -566,6 +649,7 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
566 const bool recheck_after_trace) 649 const bool recheck_after_trace)
567{ 650{
568 u32 filter_ret, action; 651 u32 filter_ret, action;
652 struct seccomp_filter *match = NULL;
569 int data; 653 int data;
570 654
571 /* 655 /*
@@ -574,9 +658,9 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
574 */ 658 */
575 rmb(); 659 rmb();
576 660
577 filter_ret = seccomp_run_filters(sd); 661 filter_ret = seccomp_run_filters(sd, &match);
578 data = filter_ret & SECCOMP_RET_DATA; 662 data = filter_ret & SECCOMP_RET_DATA;
579 action = filter_ret & SECCOMP_RET_ACTION; 663 action = filter_ret & SECCOMP_RET_ACTION_FULL;
580 664
581 switch (action) { 665 switch (action) {
582 case SECCOMP_RET_ERRNO: 666 case SECCOMP_RET_ERRNO:
@@ -637,14 +721,25 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
637 721
638 return 0; 722 return 0;
639 723
724 case SECCOMP_RET_LOG:
725 seccomp_log(this_syscall, 0, action, true);
726 return 0;
727
640 case SECCOMP_RET_ALLOW: 728 case SECCOMP_RET_ALLOW:
729 /*
730 * Note that the "match" filter will always be NULL for
731 * this action since SECCOMP_RET_ALLOW is the starting
732 * state in seccomp_run_filters().
733 */
641 return 0; 734 return 0;
642 735
643 case SECCOMP_RET_KILL: 736 case SECCOMP_RET_KILL_THREAD:
737 case SECCOMP_RET_KILL_PROCESS:
644 default: 738 default:
645 audit_seccomp(this_syscall, SIGSYS, action); 739 seccomp_log(this_syscall, SIGSYS, action, true);
646 /* Dump core only if this is the last remaining thread. */ 740 /* Dump core only if this is the last remaining thread. */
647 if (get_nr_threads(current) == 1) { 741 if (action == SECCOMP_RET_KILL_PROCESS ||
742 get_nr_threads(current) == 1) {
648 siginfo_t info; 743 siginfo_t info;
649 744
650 /* Show the original registers in the dump. */ 745 /* Show the original registers in the dump. */
@@ -653,13 +748,16 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
653 seccomp_init_siginfo(&info, this_syscall, data); 748 seccomp_init_siginfo(&info, this_syscall, data);
654 do_coredump(&info); 749 do_coredump(&info);
655 } 750 }
656 do_exit(SIGSYS); 751 if (action == SECCOMP_RET_KILL_PROCESS)
752 do_group_exit(SIGSYS);
753 else
754 do_exit(SIGSYS);
657 } 755 }
658 756
659 unreachable(); 757 unreachable();
660 758
661skip: 759skip:
662 audit_seccomp(this_syscall, 0, action); 760 seccomp_log(this_syscall, 0, action, match ? match->log : false);
663 return -1; 761 return -1;
664} 762}
665#else 763#else
@@ -794,6 +892,29 @@ static inline long seccomp_set_mode_filter(unsigned int flags,
794} 892}
795#endif 893#endif
796 894
895static long seccomp_get_action_avail(const char __user *uaction)
896{
897 u32 action;
898
899 if (copy_from_user(&action, uaction, sizeof(action)))
900 return -EFAULT;
901
902 switch (action) {
903 case SECCOMP_RET_KILL_PROCESS:
904 case SECCOMP_RET_KILL_THREAD:
905 case SECCOMP_RET_TRAP:
906 case SECCOMP_RET_ERRNO:
907 case SECCOMP_RET_TRACE:
908 case SECCOMP_RET_LOG:
909 case SECCOMP_RET_ALLOW:
910 break;
911 default:
912 return -EOPNOTSUPP;
913 }
914
915 return 0;
916}
917
797/* Common entry point for both prctl and syscall. */ 918/* Common entry point for both prctl and syscall. */
798static long do_seccomp(unsigned int op, unsigned int flags, 919static long do_seccomp(unsigned int op, unsigned int flags,
799 const char __user *uargs) 920 const char __user *uargs)
@@ -805,6 +926,11 @@ static long do_seccomp(unsigned int op, unsigned int flags,
805 return seccomp_set_mode_strict(); 926 return seccomp_set_mode_strict();
806 case SECCOMP_SET_MODE_FILTER: 927 case SECCOMP_SET_MODE_FILTER:
807 return seccomp_set_mode_filter(flags, uargs); 928 return seccomp_set_mode_filter(flags, uargs);
929 case SECCOMP_GET_ACTION_AVAIL:
930 if (flags != 0)
931 return -EINVAL;
932
933 return seccomp_get_action_avail(uargs);
808 default: 934 default:
809 return -EINVAL; 935 return -EINVAL;
810 } 936 }
@@ -908,13 +1034,13 @@ long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
908 if (!data) 1034 if (!data)
909 goto out; 1035 goto out;
910 1036
911 get_seccomp_filter(task); 1037 __get_seccomp_filter(filter);
912 spin_unlock_irq(&task->sighand->siglock); 1038 spin_unlock_irq(&task->sighand->siglock);
913 1039
914 if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog))) 1040 if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
915 ret = -EFAULT; 1041 ret = -EFAULT;
916 1042
917 put_seccomp_filter(task); 1043 __put_seccomp_filter(filter);
918 return ret; 1044 return ret;
919 1045
920out: 1046out:
@@ -922,3 +1048,185 @@ out:
922 return ret; 1048 return ret;
923} 1049}
924#endif 1050#endif
1051
1052#ifdef CONFIG_SYSCTL
1053
1054/* Human readable action names for friendly sysctl interaction */
1055#define SECCOMP_RET_KILL_PROCESS_NAME "kill_process"
1056#define SECCOMP_RET_KILL_THREAD_NAME "kill_thread"
1057#define SECCOMP_RET_TRAP_NAME "trap"
1058#define SECCOMP_RET_ERRNO_NAME "errno"
1059#define SECCOMP_RET_TRACE_NAME "trace"
1060#define SECCOMP_RET_LOG_NAME "log"
1061#define SECCOMP_RET_ALLOW_NAME "allow"
1062
1063static const char seccomp_actions_avail[] =
1064 SECCOMP_RET_KILL_PROCESS_NAME " "
1065 SECCOMP_RET_KILL_THREAD_NAME " "
1066 SECCOMP_RET_TRAP_NAME " "
1067 SECCOMP_RET_ERRNO_NAME " "
1068 SECCOMP_RET_TRACE_NAME " "
1069 SECCOMP_RET_LOG_NAME " "
1070 SECCOMP_RET_ALLOW_NAME;
1071
1072struct seccomp_log_name {
1073 u32 log;
1074 const char *name;
1075};
1076
1077static const struct seccomp_log_name seccomp_log_names[] = {
1078 { SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME },
1079 { SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME },
1080 { SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME },
1081 { SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME },
1082 { SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME },
1083 { SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME },
1084 { SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME },
1085 { }
1086};
1087
1088static bool seccomp_names_from_actions_logged(char *names, size_t size,
1089 u32 actions_logged)
1090{
1091 const struct seccomp_log_name *cur;
1092 bool append_space = false;
1093
1094 for (cur = seccomp_log_names; cur->name && size; cur++) {
1095 ssize_t ret;
1096
1097 if (!(actions_logged & cur->log))
1098 continue;
1099
1100 if (append_space) {
1101 ret = strscpy(names, " ", size);
1102 if (ret < 0)
1103 return false;
1104
1105 names += ret;
1106 size -= ret;
1107 } else
1108 append_space = true;
1109
1110 ret = strscpy(names, cur->name, size);
1111 if (ret < 0)
1112 return false;
1113
1114 names += ret;
1115 size -= ret;
1116 }
1117
1118 return true;
1119}
1120
1121static bool seccomp_action_logged_from_name(u32 *action_logged,
1122 const char *name)
1123{
1124 const struct seccomp_log_name *cur;
1125
1126 for (cur = seccomp_log_names; cur->name; cur++) {
1127 if (!strcmp(cur->name, name)) {
1128 *action_logged = cur->log;
1129 return true;
1130 }
1131 }
1132
1133 return false;
1134}
1135
1136static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names)
1137{
1138 char *name;
1139
1140 *actions_logged = 0;
1141 while ((name = strsep(&names, " ")) && *name) {
1142 u32 action_logged = 0;
1143
1144 if (!seccomp_action_logged_from_name(&action_logged, name))
1145 return false;
1146
1147 *actions_logged |= action_logged;
1148 }
1149
1150 return true;
1151}
1152
1153static int seccomp_actions_logged_handler(struct ctl_table *ro_table, int write,
1154 void __user *buffer, size_t *lenp,
1155 loff_t *ppos)
1156{
1157 char names[sizeof(seccomp_actions_avail)];
1158 struct ctl_table table;
1159 int ret;
1160
1161 if (write && !capable(CAP_SYS_ADMIN))
1162 return -EPERM;
1163
1164 memset(names, 0, sizeof(names));
1165
1166 if (!write) {
1167 if (!seccomp_names_from_actions_logged(names, sizeof(names),
1168 seccomp_actions_logged))
1169 return -EINVAL;
1170 }
1171
1172 table = *ro_table;
1173 table.data = names;
1174 table.maxlen = sizeof(names);
1175 ret = proc_dostring(&table, write, buffer, lenp, ppos);
1176 if (ret)
1177 return ret;
1178
1179 if (write) {
1180 u32 actions_logged;
1181
1182 if (!seccomp_actions_logged_from_names(&actions_logged,
1183 table.data))
1184 return -EINVAL;
1185
1186 if (actions_logged & SECCOMP_LOG_ALLOW)
1187 return -EINVAL;
1188
1189 seccomp_actions_logged = actions_logged;
1190 }
1191
1192 return 0;
1193}
1194
1195static struct ctl_path seccomp_sysctl_path[] = {
1196 { .procname = "kernel", },
1197 { .procname = "seccomp", },
1198 { }
1199};
1200
1201static struct ctl_table seccomp_sysctl_table[] = {
1202 {
1203 .procname = "actions_avail",
1204 .data = (void *) &seccomp_actions_avail,
1205 .maxlen = sizeof(seccomp_actions_avail),
1206 .mode = 0444,
1207 .proc_handler = proc_dostring,
1208 },
1209 {
1210 .procname = "actions_logged",
1211 .mode = 0644,
1212 .proc_handler = seccomp_actions_logged_handler,
1213 },
1214 { }
1215};
1216
1217static int __init seccomp_sysctl_init(void)
1218{
1219 struct ctl_table_header *hdr;
1220
1221 hdr = register_sysctl_paths(seccomp_sysctl_path, seccomp_sysctl_table);
1222 if (!hdr)
1223 pr_warn("seccomp: sysctl registration failed\n");
1224 else
1225 kmemleak_not_leak(hdr);
1226
1227 return 0;
1228}
1229
1230device_initcall(seccomp_sysctl_init)
1231
1232#endif /* CONFIG_SYSCTL */