badsalttest.c 2.4 KB

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