Debian Codegraph @

crt/s2n/tests/testlib/s2n_seccomp.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "testlib/s2n_testlib.h"
17 #include "utils/s2n_safety.h"
18
19 #ifdef SECCOMP
20
21 #include <seccomp.h>
22
23 DEFINE_POINTER_CLEANUP_FUNC(scmp_filter_ctx, seccomp_release);
24
25 extern bool s2n_use_color_in_output;
26
27 bool s2n_is_seccomp_supported()
28 {
29 return true;
30 }
31
32 /* "seccomp" allows the kernel to control what system calls an application
33 * is allowed to make based on a provided filter.
34 *
35 * seccomp is commonly used for "sandboxing" programs for security reasons.
36 */
37 S2N_RESULT s2n_seccomp_init()
38 {
39 /* Using SCMP_ACT_TRAP instead of SCMP_ACT_KILL as the default action
40 * makes this test easier to debug. GDB can be used to debug failures caused
41 * by SCMP_ACT_TRAP, but not caused by SCMP_ACT_KILL.
42 */
43 DEFER_CLEANUP(scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_TRAP),
44 seccomp_release_pointer);
45 RESULT_ENSURE_REF(ctx);
46
47 /* Basic requirements: s2n-tls is known to need these system calls in order
48 * to operate. Adding a new system call to this list means that any application
49 * using s2n-tls with seccomp will potentially also need to update its filter rules.
50 *
51 * Do not add any variation of "open" to this list. One of the primary reasons
52 * that an application would choose to use seccomp is to prevent opening files,
53 * similar to chroot.
54 */
55 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(access), 0));
56 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk), 0));
57 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(clock_gettime), 0));
58 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0));
59 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0));
60 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(fstat), 0));
61 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(futex), 0));
62 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getrandom), 0));
63 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0));
64 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0));
65
66 /* Ubuntu22 uses "newfstatat" instead of "fstat" */
67 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(newfstatat), 0));
68
69 /* See https://github.com/aws/aws-lc/blob/main/SANDBOXING.md#fork-protection:
70 * We can just cause the madavise call to fail rather than blocking it entirely. */
71 RESULT_GUARD_POSIX(seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EINVAL), SCMP_SYS(madvise), 0));
72
73 /* Checking whether the terminal supports color requires an additional
74 * system call. Preemptively disable color.
75 */
76 s2n_use_color_in_output = false;
77
78 RESULT_GUARD_POSIX(seccomp_load(ctx));
79 return S2N_RESULT_OK;
80 }
81
82 #else
83
84 bool s2n_is_seccomp_supported()
85 {
86 return false;
87 }
88
89 S2N_RESULT s2n_seccomp_init()
90 {
91 return S2N_RESULT_OK;
92 }
93
94 #endif