|
Packit |
6c4009 |
/* Test program for bad DES salt detection in crypt.
|
|
Packit |
6c4009 |
Copyright (C) 2012-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <sys/mman.h>
|
|
Packit |
6c4009 |
#include <crypt.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static const char *tests[][2] =
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
{ "no salt", "" },
|
|
Packit |
6c4009 |
{ "single char", "/" },
|
|
Packit |
6c4009 |
{ "first char bad", "!x" },
|
|
Packit |
6c4009 |
{ "second char bad", "Z%" },
|
|
Packit |
6c4009 |
{ "both chars bad", ":@" },
|
|
Packit |
6c4009 |
{ "un$upported algorithm", "$2$" },
|
|
Packit |
6c4009 |
{ "unsupported_algorithm", "_1" },
|
|
Packit |
6c4009 |
{ "end of page", NULL }
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int result = 0;
|
|
Packit |
6c4009 |
struct crypt_data cd;
|
|
Packit |
6c4009 |
size_t n = sizeof (tests) / sizeof (*tests);
|
|
Packit |
6c4009 |
size_t pagesize = (size_t) sysconf (_SC_PAGESIZE);
|
|
Packit |
6c4009 |
char *page;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check that crypt won't look at the second character if the first
|
|
Packit |
6c4009 |
one is invalid. */
|
|
Packit |
6c4009 |
page = mmap (NULL, pagesize * 2, PROT_READ | PROT_WRITE,
|
|
Packit |
6c4009 |
MAP_PRIVATE | MAP_ANON, -1, 0);
|
|
Packit |
6c4009 |
if (page == MAP_FAILED)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
perror ("mmap");
|
|
Packit |
6c4009 |
n--;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (mmap (page + pagesize, pagesize, 0,
|
|
Packit |
6c4009 |
MAP_PRIVATE | MAP_ANON | MAP_FIXED,
|
|
Packit |
6c4009 |
-1, 0) != page + pagesize)
|
|
Packit |
6c4009 |
perror ("mmap 2");
|
|
Packit |
6c4009 |
page[pagesize - 1] = '*';
|
|
Packit |
6c4009 |
tests[n - 1][1] = &page[pagesize - 1];
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Mark cd as initialized before first call to crypt_r. */
|
|
Packit |
6c4009 |
cd.initialized = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (size_t i = 0; i < n; i++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (crypt (tests[i][0], tests[i][1]))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
result++;
|
|
Packit |
6c4009 |
printf ("%s: crypt returned non-NULL with salt \"%s\"\n",
|
|
Packit |
6c4009 |
tests[i][0], tests[i][1]);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (crypt_r (tests[i][0], tests[i][1], &cd))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
result++;
|
|
Packit |
6c4009 |
printf ("%s: crypt_r returned non-NULL with salt \"%s\"\n",
|
|
Packit |
6c4009 |
tests[i][0], tests[i][1]);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define TIMEOUT 5
|
|
Packit |
6c4009 |
#define TEST_FUNCTION do_test ()
|
|
Packit |
6c4009 |
#include "../test-skeleton.c"
|