1 /*-
2 * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3 * Copyright (c) 2023, 2025 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Robert Clausecker
7 * <fuz@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/param.h>
32 #include <sys/mman.h>
33 #include <assert.h>
34 #include <dlfcn.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <atf-c.h>
40
41 static char *(*stpncpy_fn)(char *restrict, const char *restrict, size_t);
42
43 static char *
makebuf(size_t len,int guard_at_end)44 makebuf(size_t len, int guard_at_end)
45 {
46 char *buf;
47 size_t alloc_size, page_size;
48
49 page_size = getpagesize();
50 alloc_size = roundup2(len, page_size) + page_size;
51
52 buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
53 assert(buf);
54 if (guard_at_end) {
55 assert(mprotect(buf + alloc_size - page_size, page_size, PROT_NONE) == 0);
56 return (buf + alloc_size - page_size - len);
57 } else {
58 assert(mprotect(buf, page_size, PROT_NONE) == 0);
59 return (buf + page_size);
60 }
61 }
62
63 static void
freebuf(char * buf,size_t len,int guard_at_end)64 freebuf(char *buf, size_t len, int guard_at_end)
65 {
66 size_t alloc_size, page_size;
67
68 page_size = getpagesize();
69 alloc_size = roundup2(len, page_size) + page_size;
70
71 if (guard_at_end)
72 munmap(buf + len + page_size - alloc_size, alloc_size);
73 else
74 munmap(buf - page_size, alloc_size);
75 }
76
77 static void
test_stpncpy(const char * s,size_t size)78 test_stpncpy(const char *s, size_t size)
79 {
80 char *src, *dst, *expected;
81 size_t bufsize, x;
82 int i, j;
83
84 for (i = 0; i <= 1; i++) {
85 for (j = 0; j <= 1; j++) {
86 for (bufsize = 0; bufsize <= size + 32; bufsize++) {
87 dst = makebuf(bufsize, j);
88 if (bufsize < size) {
89 src = makebuf(bufsize, i);
90 memcpy(src, s, bufsize);
91 expected = dst + bufsize;
92 } else {
93 src = makebuf(size, i);
94 memcpy(src, s, size);
95 expected = dst + size - 1;
96 }
97
98 memset(dst, 'X', bufsize);
99 assert(stpncpy_fn(dst, src, bufsize) == expected);
100 assert(memcmp(src, dst, MIN(bufsize, size)) == 0);
101 for (x = size; x < bufsize; x++)
102 assert(dst[x] == '\0');
103
104 freebuf(dst, bufsize, j);
105 freebuf(src, MIN(bufsize, size), i);
106 }
107 }
108 }
109 }
110
111 static void
test_sentinel(char * dest,char * src,size_t destlen,size_t srclen)112 test_sentinel(char *dest, char *src, size_t destlen, size_t srclen)
113 {
114 size_t i;
115 const char *res, *wantres;
116 const char *fail = NULL;
117
118 for (i = 0; i < srclen; i++)
119 /* src will never include (){} */
120 src[i] = '0' + i;
121 src[srclen] = '\0';
122
123 /* source sentinels: not to be copied */
124 src[-1] = '(';
125 src[srclen+1] = ')';
126
127 memset(dest, 0xee, destlen);
128
129 /* destination sentinels: not to be touched */
130 dest[-1] = '{';
131 dest[destlen] = '}';
132
133 wantres = dest + (srclen > destlen ? destlen : srclen);
134 res = stpncpy_fn(dest, src, destlen);
135
136 if (dest[-1] != '{')
137 fail = "start sentinel overwritten";
138 else if (dest[destlen] != '}')
139 fail = "end sentinel overwritten";
140 else if (strncmp(src, dest, destlen) != 0)
141 fail = "string not copied correctly";
142 else if (res != wantres)
143 fail = "incorrect return value";
144 else for (i = srclen; i < destlen; i++)
145 if (dest[i] != '\0') {
146 fail = "incomplete NUL padding";
147 break;
148 }
149
150 if (fail)
151 atf_tc_fail_nonfatal("%s\n"
152 "stpncpy(%p \"%s\", %p \"%s\", %zu) = %p (want %p)\n",
153 fail, dest, dest, src, src, destlen, res, wantres);
154 }
155
156 ATF_TC_WITHOUT_HEAD(null);
ATF_TC_BODY(null,tc)157 ATF_TC_BODY(null, tc)
158 {
159 ATF_CHECK_EQ(stpncpy_fn(NULL, NULL, 0), NULL);
160 }
161
162 ATF_TC_WITHOUT_HEAD(bounds);
ATF_TC_BODY(bounds,tc)163 ATF_TC_BODY(bounds, tc)
164 {
165 size_t i;
166 char buf[64];
167
168 for (i = 0; i < sizeof(buf) - 1; i++) {
169 buf[i] = ' ' + i;
170 buf[i+1] = '\0';
171 test_stpncpy(buf, i + 2);
172 }
173 }
174
175 ATF_TC_WITHOUT_HEAD(alignments);
ATF_TC_BODY(alignments,tc)176 ATF_TC_BODY(alignments, tc)
177 {
178 size_t srcalign, destalign, srclen, destlen;
179 char src[15+3+64]; /* 15 offsets + 64 max length + NUL + sentinels */
180 char dest[15+2+64]; /* 15 offsets + 64 max length + sentinels */
181
182 for (srcalign = 0; srcalign < 16; srcalign++)
183 for (destalign = 0; destalign < 16; destalign++)
184 for (srclen = 0; srclen < 64; srclen++)
185 for (destlen = 0; destlen < 64; destlen++)
186 test_sentinel(dest+destalign+1,
187 src+srcalign+1, destlen, srclen);
188 }
189
ATF_TP_ADD_TCS(tp)190 ATF_TP_ADD_TCS(tp)
191 {
192 void *dl_handle;
193
194 dl_handle = dlopen(NULL, RTLD_LAZY);
195 stpncpy_fn = dlsym(dl_handle, "test_stpncpy");
196 if (stpncpy_fn == NULL)
197 stpncpy_fn = stpncpy;
198
199 ATF_TP_ADD_TC(tp, null);
200 ATF_TP_ADD_TC(tp, bounds);
201 ATF_TP_ADD_TC(tp, alignments);
202
203 return (atf_no_error());
204 }
205