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