t_execregs.c revision 1.3 1 /* $NetBSD: t_execregs.c,v 1.3 2025/02/28 16:08:42 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: t_execregs.c,v 1.3 2025/02/28 16:08:42 riastradh Exp $");
31
32 #include <sys/wait.h>
33
34 #include <atf-c.h>
35 #include <err.h>
36 #include <inttypes.h>
37 #include <limits.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #ifdef HAVE_EXECREGS_TEST
43
44 #include "execregs.h"
45 #include "isqemu.h"
46 #include "h_macros.h"
47
48 static void
49 readregs(int rfd, register_t regs[static NEXECREGS])
50 {
51 uint8_t *p;
52 size_t n;
53 ssize_t nread;
54
55 p = (void *)regs;
56 n = NEXECREGS*sizeof(regs[0]);
57 while (n) {
58 RL(nread = read(rfd, p, n));
59 ATF_CHECK_MSG((size_t)nread <= n,
60 "overlong read: %zu > %zu", (size_t)nread, n);
61 if (nread == 0)
62 break;
63 p += (size_t)nread;
64 n -= (size_t)nread;
65 }
66 ATF_CHECK_EQ_MSG(n, 0,
67 "truncated read, missing %zu of %zu bytes",
68 n, NEXECREGS*sizeof(regs[0]));
69 }
70
71 static void
72 checkregs(const register_t regs[static NEXECREGS])
73 {
74 unsigned i;
75
76 #ifdef __hppa__
77 if (isQEMU()) {
78 atf_tc_expect_fail("PR port-hppa/59114: hppa:"
79 " eager fpu switching for qemu and/or spectre mitigation");
80 }
81 #endif
82
83 #if defined(__hppa__) || \
84 defined(__ia64__) || \
85 defined(__vax__) || \
86 defined(__x86_64__)
87 atf_tc_expect_fail("PR kern/59084: exec/spawn leaks register content");
88 #endif
89
90 for (i = 0; i < NEXECREGS; i++) {
91 if (regs[i] != 0) {
92 for (i = 0; i < NEXECREGS; i++) {
93 fprintf(stderr, "[%s] %"PRIxREGISTER"\n",
94 regname[i], regs[i]);
95 }
96 fprintf(stderr, "\n");
97 atf_tc_fail("registers not zeroed");
98 }
99 }
100 }
101
102 static void
103 testregs(int child, const int pipefd[static 2],
104 register_t regs[static NEXECREGS])
105 {
106 int status;
107
108 RL(close(pipefd[1]));
109
110 readregs(pipefd[0], regs);
111
112 RL(waitpid(child, &status, 0));
113 ATF_CHECK_EQ_MSG(status, 0, "status=0x%x", status);
114
115 checkregs(regs);
116 }
117
118 #endif
119
120 ATF_TC(execregszero);
121 ATF_TC_HEAD(execregszero, tc)
122 {
123 atf_tc_set_md_var(tc, "descr",
124 "Test execve(2) zeroes registers");
125 }
126 ATF_TC_BODY(execregszero, tc)
127 {
128 #ifdef HAVE_EXECREGS_TEST
129 char h_execregs[PATH_MAX];
130 int pipefd[2];
131 register_t regs[NEXECREGS];
132 pid_t child;
133
134 RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
135 atf_tc_get_config_var(tc, "srcdir")));
136
137 RL(pipe(pipefd));
138 memset(regs, 0x5a, sizeof(regs));
139
140 RL(child = fork());
141 if (child == 0) {
142 if (dup2(pipefd[1], STDOUT_FILENO) == -1)
143 err(1, "dup2");
144 if (closefrom(STDERR_FILENO + 1) == -1)
145 err(1, "closefrom");
146 if (execregschild(h_execregs) == -1)
147 err(1, "execve");
148 _exit(2);
149 }
150
151 testregs(child, pipefd, regs);
152 #else
153 atf_tc_skip("missing test for PR kern/59084:"
154 " exec/spawn leaks register content");
155 #endif
156 }
157
158 ATF_TC(spawnregszero);
159 ATF_TC_HEAD(spawnregszero, tc)
160 {
161 atf_tc_set_md_var(tc, "descr",
162 "Test posix_spawn(2) zeroes registers");
163 }
164 ATF_TC_BODY(spawnregszero, tc)
165 {
166 #ifdef HAVE_EXECREGS_TEST
167 char h_execregs[PATH_MAX];
168 int pipefd[2];
169 register_t regs[NEXECREGS];
170 pid_t child;
171
172 RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
173 atf_tc_get_config_var(tc, "srcdir")));
174
175 RL(pipe(pipefd));
176 memset(regs, 0x5a, sizeof(regs));
177
178 RL(child = spawnregschild(h_execregs, pipefd[1]));
179
180 testregs(child, pipefd, regs);
181 #else
182 atf_tc_skip("missing test for PR kern/59084:"
183 " exec/spawn leaks register content");
184 #endif
185 }
186
187 ATF_TP_ADD_TCS(tp)
188 {
189
190 ATF_TP_ADD_TC(tp, execregszero);
191 ATF_TP_ADD_TC(tp, spawnregszero);
192
193 return atf_no_error();
194 }
195