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