| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Test program for bad DES salt detection in crypt. | 
|  | 2 | Copyright (C) 2012-2016 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 | <http://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, pagesize * 2, PROT_READ | PROT_WRITE, | 
|  | 48 | MAP_PRIVATE | MAP_ANON, -1, 0); | 
|  | 49 | if (page == MAP_FAILED) | 
|  | 50 | { | 
|  | 51 | perror ("mmap"); | 
|  | 52 | n--; | 
|  | 53 | } | 
|  | 54 | else | 
|  | 55 | { | 
|  | 56 | if (mmap (page + pagesize, pagesize, 0, | 
|  | 57 | MAP_PRIVATE | MAP_ANON | MAP_FIXED, | 
|  | 58 | -1, 0) != page + pagesize) | 
|  | 59 | perror ("mmap 2"); | 
|  | 60 | page[pagesize - 1] = '*'; | 
|  | 61 | tests[n - 1][1] = &page[pagesize - 1]; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | for (size_t i = 0; i < n; i++) | 
|  | 65 | { | 
|  | 66 | if (crypt (tests[i][0], tests[i][1])) | 
|  | 67 | { | 
|  | 68 | result++; | 
|  | 69 | printf ("%s: crypt returned non-NULL with salt \"%s\"\n", | 
|  | 70 | tests[i][0], tests[i][1]); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | if (crypt_r (tests[i][0], tests[i][1], &cd)) | 
|  | 74 | { | 
|  | 75 | result++; | 
|  | 76 | printf ("%s: crypt_r returned non-NULL with salt \"%s\"\n", | 
|  | 77 | tests[i][0], tests[i][1]); | 
|  | 78 | } | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | return result; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | #define TIMEOUT 5 | 
|  | 85 | #define TEST_FUNCTION do_test () | 
|  | 86 | #include "../test-skeleton.c" |