1 | /* Test program for bad DES salt detection in crypt. |
2 | Copyright (C) 2012-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <stdio.h> |
20 | #include <unistd.h> |
21 | #include <sys/mman.h> |
22 | #include <crypt.h> |
23 | |
24 | static const char *tests[][2] = |
25 | { |
26 | { "no salt" , "" }, |
27 | { "single char" , "/" }, |
28 | { "first char bad" , "!x" }, |
29 | { "second char bad" , "Z%" }, |
30 | { "both chars bad" , ":@" }, |
31 | { "un$upported algorithm" , "$2$" }, |
32 | { "unsupported_algorithm" , "_1" }, |
33 | { "end of page" , NULL } |
34 | }; |
35 | |
36 | static int |
37 | do_test (void) |
38 | { |
39 | int result = 0; |
40 | struct crypt_data cd; |
41 | size_t n = sizeof (tests) / sizeof (*tests); |
42 | size_t pagesize = (size_t) sysconf (_SC_PAGESIZE); |
43 | char *page; |
44 | |
45 | /* Check that crypt won't look at the second character if the first |
46 | one is invalid. */ |
47 | page = mmap (NULL, len: pagesize * 2, PROT_READ | PROT_WRITE, |
48 | MAP_PRIVATE | MAP_ANON, fd: -1, offset: 0); |
49 | if (page == MAP_FAILED) |
50 | { |
51 | perror (s: "mmap" ); |
52 | n--; |
53 | } |
54 | else |
55 | { |
56 | if (mmap (addr: page + pagesize, len: pagesize, prot: 0, |
57 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, |
58 | fd: -1, offset: 0) != page + pagesize) |
59 | perror (s: "mmap 2" ); |
60 | page[pagesize - 1] = '*'; |
61 | tests[n - 1][1] = &page[pagesize - 1]; |
62 | } |
63 | |
64 | /* Mark cd as initialized before first call to crypt_r. */ |
65 | cd.initialized = 0; |
66 | |
67 | for (size_t i = 0; i < n; i++) |
68 | { |
69 | if (crypt (phrase: tests[i][0], salt: tests[i][1])) |
70 | { |
71 | result++; |
72 | printf (format: "%s: crypt returned non-NULL with salt \"%s\"\n" , |
73 | tests[i][0], tests[i][1]); |
74 | } |
75 | |
76 | if (crypt_r (phrase: tests[i][0], salt: tests[i][1], data: &cd)) |
77 | { |
78 | result++; |
79 | printf (format: "%s: crypt_r returned non-NULL with salt \"%s\"\n" , |
80 | tests[i][0], tests[i][1]); |
81 | } |
82 | } |
83 | |
84 | return result; |
85 | } |
86 | |
87 | #define TEST_FUNCTION do_test () |
88 | #include "../test-skeleton.c" |
89 | |