t_execregs.c revision 1.1 1 /* $NetBSD: t_execregs.c,v 1.1 2025/02/27 00:55:31 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.1 2025/02/27 00:55:31 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 "h_macros.h"
46
47 static void
48 readregs(int rfd, register_t regs[static NEXECREGS])
49 {
50 uint8_t *p;
51 size_t n;
52 ssize_t nread;
53
54 p = (void *)regs;
55 n = NEXECREGS*sizeof(regs[0]);
56 while (n) {
57 RL(nread = read(rfd, p, n));
58 ATF_CHECK_MSG((size_t)nread <= n,
59 "overlong read: %zu > %zu", (size_t)nread, n);
60 if (nread == 0)
61 break;
62 p += (size_t)nread;
63 n -= (size_t)nread;
64 }
65 ATF_REQUIRE_EQ_MSG(n, 0,
66 "truncated read, missing %zu bytes", n);
67 }
68
69 static void
70 checkregs(const register_t regs[static NEXECREGS])
71 {
72 unsigned i;
73
74 #if defined(__hppa__) || \
75 defined(__ia64__) || \
76 defined(__vax__) || \
77 defined(__x86_64__)
78 atf_tc_expect_fail("PR kern/59084: exec/spawn leaks register content");
79 #endif
80
81 for (i = 0; i < NEXECREGS; i++) {
82 if (regs[i] != 0) {
83 for (i = 0; i < NEXECREGS; i++) {
84 fprintf(stderr, "[%s] %"PRIxREGISTER"\n",
85 regname[i], regs[i]);
86 }
87 fprintf(stderr, "\n");
88 atf_tc_fail("registers not zeroed");
89 }
90 }
91 }
92
93 static void
94 testregs(int child, const int pipefd[static 2],
95 register_t regs[static NEXECREGS])
96 {
97 int status;
98
99 RL(close(pipefd[1]));
100
101 readregs(pipefd[0], regs);
102
103 RL(waitpid(child, &status, 0));
104 ATF_CHECK_EQ_MSG(status, 0, "status=0x%x", status);
105
106 checkregs(regs);
107 }
108
109 #endif
110
111 ATF_TC(execregszero);
112 ATF_TC_HEAD(execregszero, tc)
113 {
114 atf_tc_set_md_var(tc, "descr",
115 "Test execve(2) zeroes registers");
116 }
117 ATF_TC_BODY(execregszero, tc)
118 {
119 #ifdef HAVE_EXECREGS_TEST
120 char h_execregs[PATH_MAX];
121 int pipefd[2];
122 register_t regs[NEXECREGS];
123 pid_t child;
124
125 RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
126 atf_tc_get_config_var(tc, "srcdir")));
127
128 RL(pipe(pipefd));
129 memset(regs, 0x5a, sizeof(regs));
130
131 RL(child = fork());
132 if (child == 0) {
133 if (dup2(pipefd[1], STDOUT_FILENO) == -1)
134 err(1, "dup2");
135 if (closefrom(STDERR_FILENO + 1) == -1)
136 err(1, "closefrom");
137 if (execregschild(h_execregs) == -1)
138 err(1, "execve");
139 _exit(2);
140 }
141
142 testregs(child, pipefd, regs);
143 #else
144 atf_tc_skip("missing test for PR kern/59084:"
145 " exec/spawn leaks register content");
146 #endif
147 }
148
149 ATF_TC(spawnregszero);
150 ATF_TC_HEAD(spawnregszero, tc)
151 {
152 atf_tc_set_md_var(tc, "descr",
153 "Test posix_spawn(2) zeroes registers");
154 }
155 ATF_TC_BODY(spawnregszero, tc)
156 {
157 #ifdef HAVE_EXECREGS_TEST
158 char h_execregs[PATH_MAX];
159 int pipefd[2];
160 register_t regs[NEXECREGS];
161 pid_t child;
162
163 RL(snprintf(h_execregs, sizeof(h_execregs), "%s/h_execregs",
164 atf_tc_get_config_var(tc, "srcdir")));
165
166 RL(pipe(pipefd));
167 memset(regs, 0x5a, sizeof(regs));
168
169 RL(child = spawnregschild(h_execregs, pipefd[1]));
170
171 testregs(child, pipefd, regs);
172 #else
173 atf_tc_skip("missing test for PR kern/59084:"
174 " exec/spawn leaks register content");
175 #endif
176 }
177
178 ATF_TP_ADD_TCS(tp)
179 {
180
181 ATF_TP_ADD_TC(tp, execregszero);
182 ATF_TP_ADD_TC(tp, spawnregszero);
183
184 return atf_no_error();
185 }
186