t_execregs.c revision 1.2 1 /* $NetBSD: t_execregs.c,v 1.2 2025/02/28 16:08:19 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.2 2025/02/28 16:08:19 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_REQUIRE_EQ_MSG(n, 0,
67 "truncated read, missing %zu bytes", n);
68 }
69
70 static void
71 checkregs(const register_t regs[static NEXECREGS])
72 {
73 unsigned i;
74
75 #ifdef __hppa__
76 if (isQEMU()) {
77 atf_tc_expect_fail("PR port-hppa/59114: hppa:"
78 " eager fpu switching for qemu and/or spectre mitigation");
79 }
80 #endif
81
82 #if defined(__hppa__) || \
83 defined(__ia64__) || \
84 defined(__vax__) || \
85 defined(__x86_64__)
86 atf_tc_expect_fail("PR kern/59084: exec/spawn leaks register content");
87 #endif
88
89 for (i = 0; i < NEXECREGS; i++) {
90 if (regs[i] != 0) {
91 for (i = 0; i < NEXECREGS; i++) {
92 fprintf(stderr, "[%s] %"PRIxREGISTER"\n",
93 regname[i], regs[i]);
94 }
95 fprintf(stderr, "\n");
96 atf_tc_fail("registers not zeroed");
97 }
98 }
99 }
100
101 static void
102 testregs(int child, const int pipefd[static 2],
103 register_t regs[static NEXECREGS])
104 {
105 int status;
106
107 RL(close(pipefd[1]));
108
109 readregs(pipefd[0], regs);
110
111 RL(waitpid(child, &status, 0));
112 ATF_CHECK_EQ_MSG(status, 0, "status=0x%x", status);
113
114 checkregs(regs);
115 }
116
117 #endif
118
119 ATF_TC(execregszero);
120 ATF_TC_HEAD(execregszero, tc)
121 {
122 atf_tc_set_md_var(tc, "descr",
123 "Test execve(2) zeroes registers");
124 }
125 ATF_TC_BODY(execregszero, tc)
126 {
127 #ifdef HAVE_EXECREGS_TEST
128 char h_execregs[PATH_MAX];
129 int pipefd[2];
130 register_t regs[NEXECREGS];
131 pid_t child;
132
133 RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
134 atf_tc_get_config_var(tc, "srcdir")));
135
136 RL(pipe(pipefd));
137 memset(regs, 0x5a, sizeof(regs));
138
139 RL(child = fork());
140 if (child == 0) {
141 if (dup2(pipefd[1], STDOUT_FILENO) == -1)
142 err(1, "dup2");
143 if (closefrom(STDERR_FILENO + 1) == -1)
144 err(1, "closefrom");
145 if (execregschild(h_execregs) == -1)
146 err(1, "execve");
147 _exit(2);
148 }
149
150 testregs(child, pipefd, regs);
151 #else
152 atf_tc_skip("missing test for PR kern/59084:"
153 " exec/spawn leaks register content");
154 #endif
155 }
156
157 ATF_TC(spawnregszero);
158 ATF_TC_HEAD(spawnregszero, tc)
159 {
160 atf_tc_set_md_var(tc, "descr",
161 "Test posix_spawn(2) zeroes registers");
162 }
163 ATF_TC_BODY(spawnregszero, tc)
164 {
165 #ifdef HAVE_EXECREGS_TEST
166 char h_execregs[PATH_MAX];
167 int pipefd[2];
168 register_t regs[NEXECREGS];
169 pid_t child;
170
171 RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
172 atf_tc_get_config_var(tc, "srcdir")));
173
174 RL(pipe(pipefd));
175 memset(regs, 0x5a, sizeof(regs));
176
177 RL(child = spawnregschild(h_execregs, pipefd[1]));
178
179 testregs(child, pipefd, regs);
180 #else
181 atf_tc_skip("missing test for PR kern/59084:"
182 " exec/spawn leaks register content");
183 #endif
184 }
185
186 ATF_TP_ADD_TCS(tp)
187 {
188
189 ATF_TP_ADD_TC(tp, execregszero);
190 ATF_TP_ADD_TC(tp, spawnregszero);
191
192 return atf_no_error();
193 }
194