xref: /freebsd/tests/sys/kern/sigsys.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * Copyright (c) 2023 The FreeBSD Foundation
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * This software were developed by Konstantin Belousov <kib@FreeBSD.org>
7  * under sponsorship from the FreeBSD Foundation.
8  */
9 
10 #include <sys/param.h>
11 #include <sys/syscall.h>
12 #include <sys/sysctl.h>
13 
14 #include <atf-c.h>
15 #include <errno.h>
16 #include <signal.h>
17 #include <stdatomic.h>
18 #include <stdbool.h>
19 #include <stdio.h>
20 
21 static sig_atomic_t sigsys_cnt;
22 
23 #define	SAVEDVALUE	"savedsignosys"
24 
25 static void
26 sigsys_handler(int signo, siginfo_t *si, void *ucp)
27 {
28 	sigsys_cnt++;
29 }
30 
31 static void
32 sigsys_test(int knob)
33 {
34 	struct sigaction sa;
35 
36 	memset(&sa, 0, sizeof(sa));
37 	sa.sa_sigaction = sigsys_handler;
38 	sa.sa_flags = SA_SIGINFO;
39 	ATF_REQUIRE(sigaction(SIGSYS, &sa, NULL) == 0);
40 
41 	ATF_REQUIRE(syscall(273) == -1);	/* reserved */
42 	ATF_CHECK_ERRNO(ENOSYS, true);
43 	atomic_signal_fence(memory_order_seq_cst);
44 	ATF_CHECK_EQ(1 * knob, sigsys_cnt * knob);
45 
46 	ATF_REQUIRE(syscall(440) == -1);	/* SYS_kse_switchin */
47 	ATF_CHECK_ERRNO(ENOSYS, true);
48 	atomic_signal_fence(memory_order_seq_cst);
49 	ATF_CHECK_EQ(2 * knob, sigsys_cnt * knob);
50 
51 	/* Hope this is enough for say next two months */
52 	ATF_REQUIRE(syscall(3000000) == -1);
53 	ATF_CHECK_ERRNO(ENOSYS, true);
54 	atomic_signal_fence(memory_order_seq_cst);
55 	ATF_CHECK_EQ(3 * knob, sigsys_cnt * knob);
56 
57 	ATF_REQUIRE(syscall(SYS_afs3_syscall) == -1);
58 	ATF_CHECK_ERRNO(ENOSYS, true);
59 	atomic_signal_fence(memory_order_seq_cst);
60 	ATF_CHECK_EQ(4 * knob, sigsys_cnt * knob);
61 }
62 
63 static void
64 sysctlset(const char *name, int val)
65 {
66 	size_t oldlen;
67 	int oldval;
68 	char buf[80];
69 
70 	ATF_REQUIRE(sysctlbyname(name, &oldval, &oldlen, NULL, 0) == 0);
71 
72 	/* Store old %name in a symlink for cleanup */
73 	snprintf(buf, sizeof(buf), "%d", oldval);
74 	ATF_REQUIRE(symlink(buf, SAVEDVALUE) == 0);
75 
76 	ATF_REQUIRE(sysctlbyname(name, NULL, NULL, &val, sizeof(val)) == 0);
77 }
78 
79 static void
80 sysctlcleanup(const char *name)
81 {
82 	size_t oldlen;
83 	int n, oldval;
84 	char buf[80];
85 
86 	if ((n = readlink(SAVEDVALUE, buf, sizeof(buf))) > 0) {
87 		buf[MIN((size_t)n, sizeof(buf) - 1)] = '\0';
88 		if (sscanf(buf, "%d", &oldval) == 1) {
89 			oldlen = sizeof(oldval);
90 			(void)sysctlbyname(name, NULL, 0,
91 			    &oldval, oldlen);
92 		}
93 	}
94 	(void)unlink(SAVEDVALUE);
95 }
96 
97 ATF_TC_WITH_CLEANUP(sigsys_test_on);
98 ATF_TC_HEAD(sigsys_test_on, tc)
99 {
100 	atf_tc_set_md_var(tc, "require.user", "root");
101 	atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
102 	atf_tc_set_md_var(tc, "descr",
103 	    "Testing delivery of SIGSYS on invalid syscalls");
104 }
105 
106 ATF_TC_BODY(sigsys_test_on, tc)
107 {
108 	sysctlset("kern.signosys", 1);
109 	sigsys_test(1);
110 }
111 
112 ATF_TC_CLEANUP(sigsys_test_on, tc)
113 {
114 	sysctlcleanup("kern.signosys");
115 }
116 
117 ATF_TC_WITH_CLEANUP(sigsys_test_off);
118 ATF_TC_HEAD(sigsys_test_off, tc)
119 {
120 	atf_tc_set_md_var(tc, "require.user", "root");
121 	atf_tc_set_md_var(tc, "require.config", "allow_sysctl_side_effects");
122 	atf_tc_set_md_var(tc, "descr",
123 	    "Testing SIGSYS silence on invalid syscalls");
124 }
125 
126 ATF_TC_BODY(sigsys_test_off, tc)
127 {
128 	sysctlset("kern.signosys", 0);
129 	sigsys_test(0);
130 }
131 
132 ATF_TC_CLEANUP(sigsys_test_off, tc)
133 {
134 	sysctlcleanup("kern.signosys");
135 }
136 
137 ATF_TP_ADD_TCS(tp)
138 {
139 	ATF_TP_ADD_TC(tp, sigsys_test_on);
140 	ATF_TP_ADD_TC(tp, sigsys_test_off);
141 	return (atf_no_error());
142 }
143