Debian Codegraph @

test/crypt-badargs.c

raw
1 /* Test passing invalid arguments to crypt*().
2
3 Written by Zack Weinberg <zackw at panix.com> in 2018.
4 To the extent possible under law, Zack Weinberg has waived all
5 copyright and related or neighboring rights to this work.
6
7 See https://creativecommons.org/publicdomain/zero/1.0/ for further
8 details. */
9
10 #include "crypt-port.h"
11
12 #include <errno.h>
13 #include <setjmp.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <signal.h>
17 #include <sys/mman.h>
18
19 /* The behavior tested below should be consistent for all hashing
20 methods. */
21 static const char *settings[] =
22 {
23 #if INCLUDE_descrypt || INCLUDE_bigcrypt
24 "Mp",
25 #endif
26 #if INCLUDE_bsdicrypt
27 "_J9..MJHn",
28 #endif
29 #if INCLUDE_md5crypt
30 "$1$MJHnaAke",
31 #endif
32 #if INCLUDE_nt
33 "$3$",
34 #endif
35 #if INCLUDE_sunmd5
36 /* exercise all paths of the bug-compatibility logic */
37 "$md5,rounds=55349$BPm.fm03$",
38 "$md5,rounds=55349$BPm.fm03$x",
39 "$md5,rounds=55349$BPm.fm03$$",
40 "$md5,rounds=55349$BPm.fm03$$x",
41 "$md5$BPm.fm03$",
42 "$md5$BPm.fm03$x",
43 "$md5$BPm.fm03$$",
44 "$md5$BPm.fm03$$x",
45 #endif
46 #if INCLUDE_sm3crypt
47 "$sm3$MJHnaAkegEVYHsFK",
48 "$sm3$rounds=10191$MJHnaAkegEVYHsFK",
49 #endif
50 #if INCLUDE_sha1crypt
51 "$sha1$248488$ggu.H673kaZ5$",
52 #endif
53 #if INCLUDE_sha256crypt
54 "$5$MJHnaAkegEVYHsFK",
55 "$5$rounds=10191$MJHnaAkegEVYHsFK",
56 #endif
57 #if INCLUDE_sha512crypt
58 "$6$MJHnaAkegEVYHsFK",
59 "$6$rounds=10191$MJHnaAkegEVYHsFK",
60 #endif
61 #if INCLUDE_bcrypt_a
62 "$2a$05$UBVLHeMpJ/QQCv3XqJx8zO",
63 #endif
64 #if INCLUDE_bcrypt
65 "$2b$05$UBVLHeMpJ/QQCv3XqJx8zO",
66 #endif
67 #if INCLUDE_bcrypt_x
68 "$2x$05$UBVLHeMpJ/QQCv3XqJx8zO",
69 #endif
70 #if INCLUDE_bcrypt_y
71 "$2y$05$UBVLHeMpJ/QQCv3XqJx8zO",
72 #endif
73 #if INCLUDE_yescrypt
74 "$y$j9T$MJHnaAkegEVYHsFKkmfzJ1",
75 #endif
76 #if INCLUDE_scrypt
77 "$7$CU..../....MJHnaAkegEVYHsFKkmfzJ1",
78 #endif
79 #if INCLUDE_gost_yescrypt
80 "$gy$j9T$MJHnaAkegEVYHsFKkmfzJ1",
81 #endif
82 #if INCLUDE_sm3_yescrypt
83 "$sm3y$j9T$MJHnaAkegEVYHsFKkmfzJ1",
84 #endif
85 };
86
87 /* In some of the tests below, a segmentation fault is the expected result. */
88 static sigjmp_buf env;
89 static void
90 segv_handler (int sig)
91 {
92 siglongjmp (env, sig);
93 }
94
95 static bool error_occurred;
96
97 #ifndef XCRYPT_USE_ASAN /* see comments in do_tests */
98 static void
99 expect_no_fault (const char *tag,
100 const char *phrase, const char *setting, const char *expect,
101 void (*testfn) (const char *, const char *,
102 const char *, const char *))
103 {
104 int rv = sigsetjmp (env, 1);
105 if (!rv)
106 testfn (tag, phrase, setting, expect);
107 else
108 {
109 printf ("FAIL: %s: Unexpected %s\n", tag, strsignal (rv));
110 error_occurred = 1;
111 }
112 }
113 #endif
114
115 static void
116 expect_a_fault (const char *tag,
117 const char *phrase, const char *setting, const char *expect,
118 void (*testfn) (const char *, const char *,
119 const char *, const char *))
120 {
121 int rv = sigsetjmp (env, 1);
122 if (!rv)
123 {
124 testfn (tag, phrase, setting, expect);
125 printf ("ERROR: %s: No signal occurred\n", tag);
126 error_occurred = true;
127 }
128 }
129
130 static void
131 check (const char *tag, const char *expect, const char *got)
132 {
133 int err = errno;
134 if ((got == 0 && expect != 0)
135 || (got != 0 && expect == 0)
136 || (got != 0 && expect != 0 && strcmp(got, expect) != 0))
137 {
138 printf ("FAIL: %s: exp '%s' got '%s'\n",
139 tag, expect ? expect : "(nil)",
140 got ? got : "(nil)");
141 error_occurred = true;
142 }
143 if ((expect == 0 || expect[0] == '*') && err != EINVAL)
144 {
145 printf ("FAIL: %s: exp '%s' got '%s'\n",
146 tag, strerror (EINVAL), strerror (err));
147 error_occurred = true;
148 }
149 }
150
151 static void
152 test_crypt (const char *tag,
153 const char *phrase, const char *setting, const char *expect)
154 {
155 char *got = crypt (phrase, setting);
156 check (tag, expect, got);
157 }
158
159 static void
160 test_crypt_r (const char *tag,
161 const char *phrase, const char *setting, const char *expect)
162 {
163 struct crypt_data data;
164 memset (&data, 0, sizeof data);
165 char *got = crypt_r (phrase, setting, &data);
166 check (tag, expect, got);
167 }
168
169 static void
170 test_crypt_rn (const char *tag,
171 const char *phrase, const char *setting, const char *expect)
172 {
173 struct crypt_data data;
174 memset (&data, 0, sizeof data);
175
176 char *got = crypt_rn (phrase, setting, &data, (int) sizeof data);
177 check (tag, expect, got);
178 }
179
180 static void
181 test_crypt_ra (const char *tag,
182 const char *phrase, const char *setting, const char *expect)
183 {
184 /* cheat - crypt_ra doesn't actually care whether its scratch area
185 is on the heap as long as it's big enough */
186 struct crypt_data data;
187 memset (&data, 0, sizeof data);
188 void *datap = &data;
189 int datas = (int) sizeof data;
190
191 char *got = crypt_ra (phrase, setting, &datap, &datas);
192 check (tag, expect, got);
193 }
194
195 #if ENABLE_FAILURE_TOKENS
196 # define FT0 "*0"
197 # define FT1 "*1"
198 #else
199 # define FT0 0
200 # define FT1 0
201 #endif
202
203 /* PAGE should point to PAGESIZE bytes of read-write memory followed
204 by another PAGESIZE bytes of inaccessible memory. */
205
206 static void
207 do_tests(char *page, size_t pagesize)
208 {
209 static const char phrase[] =
210 "the ritual question of how much is two plus two";
211
212 /* This copy operation intentionally omits the NUL; 'p1' points to a
213 sequence of nonzero bytes followed immediately by inaccessible
214 memory. */
215 memcpy (page + pagesize - (sizeof phrase - 1), phrase, sizeof phrase - 1);
216 const char *p1 = page + pagesize - (sizeof phrase - 1);
217 const char *p2 = page + pagesize;
218 size_t i;
219
220 /* Our crypt*() functions return NULL / a failure token, with errno set
221 to EINVAL, when either the setting or the phrase argument is NULL.
222 ASan's interceptors for crypt*() instead crash the program when either
223 argument is NULL -- this is arguably a better choice, but for
224 compatibility's sake we can't change what our functions do. There is
225 no way to disable interception of specific functions as far as I can
226 tell. Therefore, these tests are skipped when compiled with ASan. */
227 #ifndef XCRYPT_USE_ASAN
228 /* When SETTING is null, it shouldn't matter what PHRASE is. */
229 expect_no_fault ("0.0.crypt", 0, 0, FT0, test_crypt);
230 expect_no_fault ("0.0.crypt_r", 0, 0, FT0, test_crypt_r);
231 expect_no_fault ("0.0.crypt_rn", 0, 0, 0, test_crypt_rn);
232 expect_no_fault ("0.0.crypt_ra", 0, 0, 0, test_crypt_ra);
233
234 expect_no_fault ("''.0.crypt", "", 0, FT0, test_crypt);
235 expect_no_fault ("''.0.crypt_r", "", 0, FT0, test_crypt_r);
236 expect_no_fault ("''.0.crypt_rn", "", 0, 0, test_crypt_rn);
237 expect_no_fault ("''.0.crypt_ra", "", 0, 0, test_crypt_ra);
238
239 expect_no_fault ("ph.0.crypt", phrase, 0, FT0, test_crypt);
240 expect_no_fault ("ph.0.crypt_r", phrase, 0, FT0, test_crypt_r);
241 expect_no_fault ("ph.0.crypt_rn", phrase, 0, 0, test_crypt_rn);
242 expect_no_fault ("ph.0.crypt_ra", phrase, 0, 0, test_crypt_ra);
243
244 expect_no_fault ("p1.0.crypt", p1, 0, FT0, test_crypt);
245 expect_no_fault ("p1.0.crypt_r", p1, 0, FT0, test_crypt_r);
246 expect_no_fault ("p1.0.crypt_rn", p1, 0, 0, test_crypt_rn);
247 expect_no_fault ("p1.0.crypt_ra", p1, 0, 0, test_crypt_ra);
248
249 expect_no_fault ("p2.0.crypt", p2, 0, FT0, test_crypt);
250 expect_no_fault ("p2.0.crypt_r", p2, 0, FT0, test_crypt_r);
251 expect_no_fault ("p2.0.crypt_rn", p2, 0, 0, test_crypt_rn);
252 expect_no_fault ("p2.0.crypt_ra", p2, 0, 0, test_crypt_ra);
253
254 /* Conversely, when PHRASE is null,
255 it shouldn't matter what SETTING is... */
256 expect_no_fault ("0.''.crypt", 0, "", FT0, test_crypt);
257 expect_no_fault ("0.''.crypt_r", 0, "", FT0, test_crypt_r);
258 expect_no_fault ("0.''.crypt_rn", 0, "", 0, test_crypt_rn);
259 expect_no_fault ("0.''.crypt_ra", 0, "", 0, test_crypt_ra);
260
261 expect_no_fault ("0.'*'.crypt", 0, "*", FT0, test_crypt);
262 expect_no_fault ("0.'*'.crypt_r", 0, "*", FT0, test_crypt_r);
263 expect_no_fault ("0.'*'.crypt_rn", 0, "*", 0, test_crypt_rn);
264 expect_no_fault ("0.'*'.crypt_ra", 0, "*", 0, test_crypt_ra);
265
266 expect_no_fault ("0.'*0'.crypt", 0, "*0", FT1, test_crypt);
267 expect_no_fault ("0.'*0'.crypt_r", 0, "*0", FT1, test_crypt_r);
268 expect_no_fault ("0.'*0'.crypt_rn", 0, "*0", 0, test_crypt_rn);
269 expect_no_fault ("0.'*0'.crypt_ra", 0, "*0", 0, test_crypt_ra);
270
271 expect_no_fault ("0.'*1'.crypt", 0, "*1", FT0, test_crypt);
272 expect_no_fault ("0.'*1'.crypt_r", 0, "*1", FT0, test_crypt_r);
273 expect_no_fault ("0.'*1'.crypt_rn", 0, "*1", 0, test_crypt_rn);
274 expect_no_fault ("0.'*1'.crypt_ra", 0, "*1", 0, test_crypt_ra);
275
276 expect_no_fault ("0.p1.crypt", 0, p1, FT0, test_crypt);
277 expect_no_fault ("0.p1.crypt_r", 0, p1, FT0, test_crypt_r);
278 expect_no_fault ("0.p1.crypt_rn", 0, p1, 0, test_crypt_rn);
279 expect_no_fault ("0.p1.crypt_ra", 0, p1, 0, test_crypt_ra);
280
281 /* ... except for the case where SETTING is nonnull but there are
282 fewer than 2 readable characters at SETTING, in which case we'll
283 crash before we get to the null check in do_crypt. This is a
284 bug, but it's impractical to fix without breaking the property
285 that 'crypt' _never_ creates a failure token that is equal to the
286 setting string, which is more important than this corner case. */
287 expect_a_fault ("0.p2.crypt", 0, p2, FT0, test_crypt);
288 expect_a_fault ("0.p2.crypt_r", 0, p2, FT0, test_crypt_r);
289 expect_a_fault ("0.p2.crypt_rn", 0, p2, 0, test_crypt_rn);
290 expect_a_fault ("0.p2.crypt_ra", 0, p2, 0, test_crypt_ra);
291 #endif /* no ASan */
292
293 /* When SETTING is valid, passing an invalid string as PHRASE should
294 crash reliably. */
295 for (i = 0; i < ARRAY_SIZE (settings); i++)
296 {
297 snprintf (page, pagesize, "p1.'%s'.crypt", settings[i]);
298 expect_a_fault (page, p1, settings[i], FT0, test_crypt);
299
300 snprintf (page, pagesize, "p1.'%s'.crypt_r", settings[i]);
301 expect_a_fault (page, p1, settings[i], FT0, test_crypt_r);
302
303 snprintf (page, pagesize, "p1.'%s'.crypt_rn", settings[i]);
304 expect_a_fault (page, p1, settings[i], 0, test_crypt_rn);
305
306 snprintf (page, pagesize, "p1.'%s'.crypt_ra", settings[i]);
307 expect_a_fault (page, p1, settings[i], 0, test_crypt_ra);
308
309 snprintf (page, pagesize, "p2.'%s'.crypt", settings[i]);
310 expect_a_fault (page, p2, settings[i], FT0, test_crypt);
311
312 snprintf (page, pagesize, "p2.'%s'.crypt_r", settings[i]);
313 expect_a_fault (page, p2, settings[i], FT0, test_crypt_r);
314
315 snprintf (page, pagesize, "p2.'%s'.crypt_rn", settings[i]);
316 expect_a_fault (page, p2, settings[i], 0, test_crypt_rn);
317
318 snprintf (page, pagesize, "p2.'%s'.crypt_ra", settings[i]);
319 expect_a_fault (page, p2, settings[i], 0, test_crypt_ra);
320 }
321
322 /* Conversely, when PHRASE is valid, passing an invalid string as SETTING
323 should crash reliably. */
324 expect_a_fault ("ph.p2.crypt", phrase, p2, FT0, test_crypt);
325 expect_a_fault ("ph.p2.crypt_r", phrase, p2, FT0, test_crypt_r);
326 expect_a_fault ("ph.p2.crypt_rn", phrase, p2, 0, test_crypt_rn);
327 expect_a_fault ("ph.p2.crypt_ra", phrase, p2, 0, test_crypt_ra);
328
329 for (i = 0; i < ARRAY_SIZE (settings); i++)
330 {
331 p1 = memcpy (page + pagesize - strlen (settings[i]),
332 settings[i], strlen (settings[i]));
333
334 snprintf (page, pagesize, "ph.'%s'.crypt", settings[i]);
335 expect_a_fault (page, phrase, p1, FT0, test_crypt);
336
337 snprintf (page, pagesize, "ph.'%s'.crypt_r", settings[i]);
338 expect_a_fault (page, phrase, p1, FT0, test_crypt_r);
339
340 snprintf (page, pagesize, "ph.'%s'.crypt_rn", settings[i]);
341 expect_a_fault (page, phrase, p1, 0, test_crypt_rn);
342
343 snprintf (page, pagesize, "ph.'%s'.crypt_ra", settings[i]);
344 expect_a_fault (page, phrase, p1, 0, test_crypt_ra);
345 }
346 }
347
348 int
349 main (void)
350 {
351 /* Set up a two-page region whose first page is read-write and
352 whose second page is inaccessible. */
353 long pagesize_l = sysconf (_SC_PAGESIZE);
354 if (pagesize_l < 256)
355 {
356 printf ("ERROR: pagesize of %ld is too small\n", pagesize_l);
357 return 99;
358 }
359
360 size_t pagesize = (size_t) pagesize_l;
361 char *page = mmap (0, pagesize * 2, PROT_READ|PROT_WRITE,
362 MAP_PRIVATE|MAP_ANON, -1, 0);
363 if (page == MAP_FAILED)
364 {
365 perror ("mmap");
366 return 99;
367 }
368 // coverity[overflow_sink]
369 memset (page, 'x', pagesize * 2);
370 if (mprotect (page + pagesize, pagesize, PROT_NONE))
371 {
372 perror ("mprotect");
373 return 99;
374 }
375
376 struct sigaction sa, os, ob;
377 sigfillset (&sa.sa_mask);
378 sa.sa_flags = SA_RESTART;
379 sa.sa_handler = segv_handler;
380 if (sigaction (SIGBUS, &sa, &ob) || sigaction (SIGSEGV, &sa, &os))
381 {
382 perror ("sigaction");
383 return 1;
384 }
385
386 do_tests (page, pagesize);
387
388 sigaction (SIGBUS, &ob, 0);
389 sigaction (SIGSEGV, &os, 0);
390
391 return error_occurred;
392 }