| 1 |
/* Test rejection of ill-formed password hashes. |
| 2 |
Copyright (C) 2012-2018 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 "crypt-port.h" |
| 20 |
#undef yescrypt |
| 21 |
|
| 22 |
#include <errno.h> |
| 23 |
#include <setjmp.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
#include <signal.h> |
| 27 |
#include <sys/mman.h> |
| 28 |
|
| 29 |
/* If VERBOSE is true, passing testcases will be printed out as well |
| 30 |
as failing ones. */ |
| 31 |
static bool verbose = false; |
| 32 |
|
| 33 |
/* All hashes are hashes of this passphrase, an infamous error message |
| 34 |
used for some forgotten can't-happen condition in Unix V6; see |
| 35 |
<https://wiki.tuhs.org/doku.php?id=anecdotes:values_of_beta>. */ |
| 36 |
static const char phrase[] = "values of β will give rise to dom!"; |
| 37 |
|
| 38 |
/* Correct setting strings, from which we derive incorrect ones by |
| 39 |
replacing one character at a time with a character that cannot |
| 40 |
appear in a valid passphrase (namely ':') and/or truncating the |
| 41 |
string. */ |
| 42 |
struct valid_setting; |
| 43 |
|
| 44 |
/* Type of functions to use in is_valid_trunc. */ |
| 45 |
typedef bool (*valid_trunc_p)(const struct valid_setting *original, |
| 46 |
const char *truncated); |
| 47 |
|
| 48 |
struct valid_setting |
| 49 |
{ |
| 50 |
/* Human-readable name for this test */ |
| 51 |
const char *tag; |
| 52 |
|
| 53 |
/* The setting string */ |
| 54 |
const char *setting; |
| 55 |
|
| 56 |
/* Length of the actual setting, within the setting string. This is |
| 57 |
usually equal to strlen(setting), but a couple of the strings are |
| 58 |
padded on the right for hash-specific reasons. */ |
| 59 |
size_t setting_len; |
| 60 |
|
| 61 |
/* Given a truncation of a valid setting string, decide whether the |
| 62 |
truncation is also valid. */ |
| 63 |
valid_trunc_p is_valid_trunc; |
| 64 |
|
| 65 |
/* Numeric parameter for is_valid_trunc; usually the length of a |
| 66 |
subfield of the setting. */ |
| 67 |
uint16_t is_valid_trunc_param; |
| 68 |
|
| 69 |
/* Whether support for this hash was compiled into the library. */ |
| 70 |
bool enabled; |
| 71 |
|
| 72 |
}; |
| 73 |
|
| 74 |
/* is_valid_trunc functions -- forward declarations */ |
| 75 |
|
| 76 |
static bool vt_never(const struct valid_setting *, const char *); |
| 77 |
static bool vt_varsuffix(const struct valid_setting *, const char *); |
| 78 |
static bool vt_sunmd5(const struct valid_setting *, const char *); |
| 79 |
static bool vt_sm3(const struct valid_setting *, const char *); |
| 80 |
static bool vt_sha2gnu(const struct valid_setting *, const char *); |
| 81 |
static bool vt_yescrypt(const struct valid_setting *, const char *); |
| 82 |
|
| 83 |
/* shorthands for use in valid_cases */ |
| 84 |
|
| 85 |
#define V_( hash, setting, vt, vp) \ |
| 86 |
{ #hash, setting, sizeof setting - 1, vt, vp, INCLUDE_##hash } |
| 87 |
#define Vp_( hash, setting, vt, vp) \ |
| 88 |
{ #hash, setting, vp, vt, vp, INCLUDE_##hash } |
| 89 |
#define Vt_( hash, tag, setting, vt, vp) \ |
| 90 |
{ #hash " (" #tag ")", setting, sizeof setting - 1, vt, vp, INCLUDE_##hash } |
| 91 |
#define Vtp_(hash, tag, setting, vt, vp) \ |
| 92 |
{ #hash " (" #tag ")", setting, vp, vt, vp, INCLUDE_##hash } |
| 93 |
|
| 94 |
#define V( hash, setting) V_( hash, setting, vt_never, 0) |
| 95 |
#define Vn( hash, vt, setting) V_( hash, setting, vt_##vt, 0) |
| 96 |
#define Vp( hash, sl, setting) Vp_( hash, setting, vt_varsuffix, sl) |
| 97 |
#define Vv( hash, sl, setting) V_( hash, setting, vt_varsuffix, sl) |
| 98 |
#define Vt( hash, tag, setting) Vt_( hash, tag, setting, vt_never, 0) |
| 99 |
#define Vtn(hash, tag, vt, setting) Vt_( hash, tag, setting, vt_##vt, 0) |
| 100 |
#define Vtp(hash, tag, sl, setting) Vtp_(hash, tag, setting, vt_varsuffix, sl) |
| 101 |
#define Vtv(hash, tag, sl, setting) Vt_( hash, tag, setting, vt_varsuffix, sl) |
| 102 |
|
| 103 |
/* Each of these is a valid setting string for some algorithm, |
| 104 |
from which we will derive many invalid setting strings. |
| 105 |
This is an expensive test, so where possible, the number of |
| 106 |
"rounds" of the hash function has been set abnormally low. */ |
| 107 |
static const struct valid_setting valid_cases[] = |
| 108 |
{ |
| 109 |
V (descrypt, "Mp" ), |
| 110 |
/* bigcrypt is extra special: |
| 111 |
this salt is a valid descrypt salt when bigcrypt isn't enabled |
| 112 |
but descrypt is; |
| 113 |
truncations down to 2 are valid when descrypt is enabled, but |
| 114 |
if *only* bigcrypt is enabled, then truncations can only |
| 115 |
go down to 14. */ |
| 116 |
{ |
| 117 |
INCLUDE_bigcrypt ? "bigcrypt" : "descrypt (padded)", |
| 118 |
"Mp............", 2, vt_varsuffix, |
| 119 |
INCLUDE_descrypt ? 2 : 14, |
| 120 |
INCLUDE_descrypt || INCLUDE_bigcrypt |
| 121 |
}, |
| 122 |
V (bsdicrypt, "_J9..MJHn" ), |
| 123 |
Vv (md5crypt, 3, "$1$MJHnaAke$" ), |
| 124 |
Vtn(sunmd5, plain, sunmd5, "$md5$1xMeE.at$" ), |
| 125 |
Vtn(sunmd5, rounds, sunmd5, "$md5,rounds=123$1xMeE.at$" ), |
| 126 |
Vt (nt, plain, "$3$" ), |
| 127 |
Vtp(nt, fake salt, 3, "$3$__not_used__c809a450df09a3" ), |
| 128 |
Vv (sha1crypt, 11, "$sha1$123$GGXpNqoJvglVTkGU$" ), |
| 129 |
Vtn(sha256crypt, plain, sha2gnu, "$5$MJHnaAkegEVYHsFK$" ), |
| 130 |
Vtn(sha256crypt, rounds, sha2gnu, "$5$rounds=1000$MJHnaAkegEVYHsFK$" ), |
| 131 |
Vtn(sha512crypt, plain, sha2gnu, "$6$MJHnaAkegEVYHsFK$" ), |
| 132 |
Vtn(sha512crypt, rounds, sha2gnu, "$6$rounds=1000$MJHnaAkegEVYHsFK$" ), |
| 133 |
Vtn(sm3crypt, plain, sm3, "$sm3$MJHnaAkegEVYHsFK$" ), |
| 134 |
Vtn(sm3crypt, rounds, sm3, "$sm3$rounds=1000$MJHnaAkegEVYHsFK$" ), |
| 135 |
V (bcrypt, "$2b$04$UBVLHeMpJ/QQCv3XqJx8zO" ), |
| 136 |
V (bcrypt_a, "$2a$04$UBVLHeMpJ/QQCv3XqJx8zO" ), |
| 137 |
V (bcrypt_x, "$2x$04$UBVLHeMpJ/QQCv3XqJx8zO" ), |
| 138 |
V (bcrypt_y, "$2y$04$UBVLHeMpJ/QQCv3XqJx8zO" ), |
| 139 |
Vv (scrypt, 14, "$7$C6..../....SodiumChloride$" ), |
| 140 |
Vn (yescrypt, yescrypt, "$y$j9T$PKXc3hCOSyMqdaEQArI62/$" ), |
| 141 |
Vn (gost_yescrypt, yescrypt, "$gy$j9T$PKXc3hCOSyMqdaEQArI62/$" ), |
| 142 |
Vn (sm3_yescrypt, yescrypt, "$sm3y$j9T$PKXc3hCOSyMqdaEQArI62/$" ), |
| 143 |
}; |
| 144 |
|
| 145 |
#undef V_ |
| 146 |
#undef Vp_ |
| 147 |
#undef Vt_ |
| 148 |
#undef Vtp_ |
| 149 |
|
| 150 |
#undef V |
| 151 |
#undef Vn |
| 152 |
#undef Vp |
| 153 |
#undef Vv |
| 154 |
#undef Vt |
| 155 |
#undef Vtn |
| 156 |
#undef Vtp |
| 157 |
#undef Vtv |
| 158 |
|
| 159 |
/* Additional tests of manually constructed, invalid setting |
| 160 |
strings. */ |
| 161 |
struct invalid_setting |
| 162 |
{ |
| 163 |
const char *tag; |
| 164 |
const char *setting; |
| 165 |
}; |
| 166 |
static const struct invalid_setting invalid_cases[] = |
| 167 |
{ |
| 168 |
/* These strings are invalid regardless of the algorithm. */ |
| 169 |
{ "too short 1", "/" }, |
| 170 |
{ "too short 2", "M" }, |
| 171 |
{ "too short 3", "$" }, |
| 172 |
{ "too short 4", "_" }, |
| 173 |
{ "too short 5", "." }, |
| 174 |
{ "invalid char :", ":" }, |
| 175 |
{ "invalid char ;", ";" }, |
| 176 |
{ "invalid char *", "*" }, |
| 177 |
{ "invalid char !", "!" }, |
| 178 |
{ "invalid char \\", "\\" }, |
| 179 |
{ "invalid char SPC", " " }, |
| 180 |
{ "invalid char TAB", "\t" }, |
| 181 |
{ "invalid char ^M", "\r" }, |
| 182 |
{ "invalid char ^J", "\n" }, |
| 183 |
{ "invalid char ^L", "\f" }, |
| 184 |
{ "invalid char ^A", "\001" }, |
| 185 |
{ "invalid char DEL", "\177" }, |
| 186 |
{ "failure token 1", "*0" }, |
| 187 |
{ "failure token 2", "*1" }, |
| 188 |
{ "unsupported algorithm", "$un$upp0rt3d$" }, |
| 189 |
{ "empty string", "" }, |
| 190 |
|
| 191 |
/* These strings are invalid for specific algorithms, in ways |
| 192 |
that the generic error generator cannot produce. */ |
| 193 |
{ "sunmd5 absent rounds", "$md5,rounds=$1xMeE.at$" }, |
| 194 |
{ "sunmd5 low rounds", "$md5,rounds=0$1xMeE.at$" }, |
| 195 |
{ "sunmd5 octal rounds", "$md5,rounds=012$1xMeE.at$" }, |
| 196 |
{ "sunmd5 high rounds", "$md5,rounds=4294967296$1xMeE.at$" }, |
| 197 |
{ "sha256 absent rounds", "$5$rounds=$MJHnaAkegEVYHsFK$" }, |
| 198 |
{ "sha256 low rounds", "$5$rounds=0$MJHnaAkegEVYHsFK$" }, |
| 199 |
{ "sha256 octal rounds", "$5$rounds=0100$MJHnaAkegEVYHsFK$" }, |
| 200 |
{ "sha256 high rounds", "$5$rounds=4294967295$MJHnaAkegEVYHsFK$" }, |
| 201 |
{ "sha512 absent rounds", "$6$rounds=$MJHnaAkegEVYHsFK$" }, |
| 202 |
{ "sha512 low rounds", "$6$rounds=0$MJHnaAkegEVYHsFK$" }, |
| 203 |
{ "sha512 octal rounds", "$6$rounds=0100$MJHnaAkegEVYHsFK$" }, |
| 204 |
{ "sha512 high rounds", "$6$rounds=4294967295$MJHnaAkegEVYHsFK$" }, |
| 205 |
{ "sm3 absent rounds", "$sm3$rounds=$MJHnaAkegEVYHsFK$" }, |
| 206 |
{ "sm3 low rounds", "$sm3$rounds=0$MJHnaAkegEVYHsFK$" }, |
| 207 |
{ "sm3 octal rounds", "$sm3$rounds=0100$MJHnaAkegEVYHsFK$" }, |
| 208 |
{ "sm3 high rounds", "$sm3$rounds=4294967295$MJHnaAkegEVYHsFK$" }, |
| 209 |
{ "bcrypt no subtype", "$2$04$UBVLHeMpJ/QQCv3XqJx8zO" }, |
| 210 |
{ "bcrypt_b low rounds", "$2b$03$UBVLHeMpJ/QQCv3XqJx8zO" }, |
| 211 |
{ "bcrypt_b high rounds", "$2b$32$UBVLHeMpJ/QQCv3XqJx8zO" }, |
| 212 |
{ "bcrypt_a low rounds", "$2a$03$UBVLHeMpJ/QQCv3XqJx8zO" }, |
| 213 |
{ "bcrypt_a high rounds", "$2a$32$UBVLHeMpJ/QQCv3XqJx8zO" }, |
| 214 |
{ "bcrypt_x low rounds", "$2x$03$UBVLHeMpJ/QQCv3XqJx8zO" }, |
| 215 |
{ "bcrypt_x high rounds", "$2x$32$UBVLHeMpJ/QQCv3XqJx8zO" }, |
| 216 |
{ "bcrypt_y low rounds", "$2y$03$UBVLHeMpJ/QQCv3XqJx8zO" }, |
| 217 |
{ "bcrypt_y low rounds", "$2y$32$UBVLHeMpJ/QQCv3XqJx8zO" }, |
| 218 |
{ "yescrypt short params", "$y$j9$PKXc3hCOSyMqdaEQArI62/$" }, |
| 219 |
{ "gost-yescrypt short params", "$gy$j9$PKXc3hCOSyMqdaEQArI62/$" }, |
| 220 |
{ "sm3-yescrypt short params", "$sm3y$j9$PKXc3hCOSyMqdaEQArI62/$" }, |
| 221 |
}; |
| 222 |
|
| 223 |
/* is_valid_trunc functions -- definitions. |
| 224 |
Note: these only need to be correct for the patterns we actually test. */ |
| 225 |
|
| 226 |
/* All truncations of this setting string are invalid. */ |
| 227 |
static bool |
| 228 |
vt_never(const struct valid_setting * ARG_UNUSED(original), |
| 229 |
const char * ARG_UNUSED(truncated)) |
| 230 |
{ |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
/* This setting string has a variable-length suffix; truncations are |
| 235 |
valid as long as the result has at least `is_valid_trunc_param' |
| 236 |
characters. */ |
| 237 |
static bool |
| 238 |
vt_varsuffix(const struct valid_setting *original, |
| 239 |
const char *truncated) |
| 240 |
{ |
| 241 |
return strlen(truncated) >= original->is_valid_trunc_param; |
| 242 |
} |
| 243 |
|
| 244 |
/* Special validity rule for sunmd5, sha256crypt, and sha512crypt: ... */ |
| 245 |
static bool |
| 246 |
vt_roundseq(const char *truncated, size_t minlen, size_t roundslen, |
| 247 |
const char *roundstag1, const char *roundstag2) |
| 248 |
{ |
| 249 |
/* ... the setting cannot be valid if it's shorter than 'minlen' |
| 250 |
characters ... */ |
| 251 |
if (strlen(truncated) < minlen) |
| 252 |
return false; |
| 253 |
|
| 254 |
/* ... if it begins with roundstag1 or roundstag2 then a sequence of |
| 255 |
digits must follow, then a dollar sign; roundstag2 may be null; |
| 256 |
... */ |
| 257 |
if (!strncmp(truncated, roundstag1, roundslen) |
| 258 |
|| (roundstag2 && !strncmp(truncated, roundstag2, roundslen))) |
| 259 |
{ |
| 260 |
size_t i = roundslen; |
| 261 |
while (truncated[i] >= '0' && truncated[i] <= '9') |
| 262 |
i++; |
| 263 |
if (truncated[i] != '$') |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
/* ... otherwise it's ok. */ |
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
/* Special validity rule for sunmd5. */ |
| 272 |
static bool |
| 273 |
vt_sunmd5(const struct valid_setting *ARG_UNUSED(original), |
| 274 |
const char *truncated) |
| 275 |
{ |
| 276 |
return vt_roundseq(truncated, strlen("$md5$"), strlen("$md5,rounds="), |
| 277 |
"$md5,rounds=", 0); |
| 278 |
} |
| 279 |
|
| 280 |
/* Special validity rule for sm3. */ |
| 281 |
static bool |
| 282 |
vt_sm3(const struct valid_setting *ARG_UNUSED(original), |
| 283 |
const char *truncated) |
| 284 |
{ |
| 285 |
return vt_roundseq(truncated, strlen("$sm3$"), strlen("$sm3$rounds="), |
| 286 |
"$sm3$rounds=", 0); |
| 287 |
} |
| 288 |
|
| 289 |
/* Special validity rule for sha256crypt and sha512crypt. */ |
| 290 |
static bool |
| 291 |
vt_sha2gnu(const struct valid_setting *ARG_UNUSED(original), |
| 292 |
const char *truncated) |
| 293 |
{ |
| 294 |
return vt_roundseq(truncated, strlen("$5$"), strlen("$5$rounds="), |
| 295 |
"$5$rounds=", "$6$rounds="); |
| 296 |
} |
| 297 |
|
| 298 |
/* Special validity rule for yescrypt and gost_yescrypt: ... */ |
| 299 |
static bool |
| 300 |
vt_yescrypt(const struct valid_setting *ARG_UNUSED(original), |
| 301 |
const char *truncated) |
| 302 |
{ |
| 303 |
/* ... the setting string must begin with "$y$j9T$" or "$gy$j9T$" |
| 304 |
(other introductory sequences are possible but those are the |
| 305 |
only ones we use); ... */ |
| 306 |
size_t y_intro_len = strlen("$y$j9T$"); |
| 307 |
size_t gy_intro_len = strlen("$gy$j9T$"); |
| 308 |
size_t sm3y_intro_len = strlen("$sm3y$j9T$"); |
| 309 |
size_t intro_len; |
| 310 |
if (!strncmp(truncated, "$y$j9T$", y_intro_len)) |
| 311 |
intro_len = y_intro_len; |
| 312 |
else if (!strncmp(truncated, "$gy$j9T$", gy_intro_len)) |
| 313 |
intro_len = gy_intro_len; |
| 314 |
else if (!strncmp(truncated, "$sm3y$j9T$", sm3y_intro_len)) |
| 315 |
intro_len = sm3y_intro_len; |
| 316 |
else |
| 317 |
return false; |
| 318 |
|
| 319 |
/* ... and the remainder must be one of these lengths. (I do not |
| 320 |
see a pattern.) */ |
| 321 |
switch (strlen(truncated) - intro_len) |
| 322 |
{ |
| 323 |
case 0: |
| 324 |
case 4: |
| 325 |
case 7: |
| 326 |
case 8: |
| 327 |
case 12: |
| 328 |
case 16: |
| 329 |
case 20: |
| 330 |
case 22: |
| 331 |
case 23: |
| 332 |
return true; |
| 333 |
default: |
| 334 |
return false; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
/* Some of the test setting strings contain unprintable characters, |
| 340 |
which we print as hex escapes. For readability, whenever we print |
| 341 |
out a setting string we pad it on the right with spaces to the |
| 342 |
length of the longest setting string we have. (There is always |
| 343 |
something after that on the line.) */ |
| 344 |
static size_t longest_setting; |
| 345 |
|
| 346 |
static void |
| 347 |
print_setting (const char *setting, bool pad) |
| 348 |
{ |
| 349 |
size_t n = 0; |
| 350 |
for (; *setting; setting++) |
| 351 |
{ |
| 352 |
unsigned int c = (unsigned int)(unsigned char) *setting; |
| 353 |
if (0x20 <= c && c <= 0x7e) |
| 354 |
{ |
| 355 |
putchar ((int)c); |
| 356 |
n += 1; |
| 357 |
} |
| 358 |
else |
| 359 |
{ |
| 360 |
printf ("\\x%02x", c); |
| 361 |
n += 4; |
| 362 |
} |
| 363 |
} |
| 364 |
if (!pad) |
| 365 |
return; |
| 366 |
while (n < longest_setting) |
| 367 |
{ |
| 368 |
putchar (' '); |
| 369 |
n += 1; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
static size_t |
| 374 |
measure_setting (const char *setting) |
| 375 |
{ |
| 376 |
size_t n = 0; |
| 377 |
for (; *setting; setting++) |
| 378 |
{ |
| 379 |
unsigned int c = (unsigned int)(unsigned char) *setting; |
| 380 |
if (0x20 <= c && c <= 0x7e) |
| 381 |
n += 1; |
| 382 |
else |
| 383 |
n += 4; |
| 384 |
} |
| 385 |
return n; |
| 386 |
} |
| 387 |
|
| 388 |
static void |
| 389 |
measure_settings (void) |
| 390 |
{ |
| 391 |
size_t ls = 0; |
| 392 |
for (size_t i = 0; i < ARRAY_SIZE (valid_cases); i++) |
| 393 |
ls = MAX (ls, measure_setting(valid_cases[i].setting)); |
| 394 |
|
| 395 |
for (size_t i = 0; i < ARRAY_SIZE (invalid_cases); i++) |
| 396 |
ls = MAX (ls, measure_setting(invalid_cases[i].setting)); |
| 397 |
|
| 398 |
longest_setting = ls; |
| 399 |
} |
| 400 |
|
| 401 |
static void |
| 402 |
print_result (const char *result, const char *setting, |
| 403 |
const char *tag, bool expected_valid) |
| 404 |
{ |
| 405 |
printf ("%s: ", result); |
| 406 |
print_setting (setting, true); |
| 407 |
printf (" (%s, %s)", tag, expected_valid ? "valid" : "invalid"); |
| 408 |
} |
| 409 |
|
| 410 |
/* Part of what we're testing, is whether any of the hashing methods |
| 411 |
can read past the end of a properly terminated C string that |
| 412 |
happens to contain an invalid setting. We do this by placing the |
| 413 |
invalid setting right next to a page of inaccessible memory and |
| 414 |
trapping the fault. */ |
| 415 |
static volatile sig_atomic_t signal_loop = 0; |
| 416 |
static sigjmp_buf env; |
| 417 |
static void |
| 418 |
segv_handler (int sig) |
| 419 |
{ |
| 420 |
if (signal_loop == 0) |
| 421 |
{ |
| 422 |
signal_loop = 1; |
| 423 |
siglongjmp (env, sig); |
| 424 |
} |
| 425 |
else |
| 426 |
{ |
| 427 |
signal (sig, SIG_DFL); |
| 428 |
raise (sig); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
/* We use only crypt_rn in this test, because it only exercises the |
| 433 |
error handling logic within the hashing methods, not the |
| 434 |
higher-level error handling logic that varies slightly among the |
| 435 |
entry points (that's all taken care of in crypt-badargs.c). We use |
| 436 |
crypt_rn instead of crypt_r so that this test does not need to vary |
| 437 |
any of its logic based on --enable-failure-tokens. */ |
| 438 |
static bool |
| 439 |
test_one_setting (const char *setting, size_t l_setting, |
| 440 |
const char *tag, bool expected_valid, |
| 441 |
struct crypt_data *cd) |
| 442 |
{ |
| 443 |
volatile bool fail = false; |
| 444 |
signal_loop = 0; |
| 445 |
int sig = sigsetjmp (env, 1); |
| 446 |
if (!sig) |
| 447 |
{ |
| 448 |
char *retval = crypt_rn (phrase, setting, cd, (int) sizeof *cd); |
| 449 |
if (expected_valid) |
| 450 |
{ |
| 451 |
if (!retval) |
| 452 |
{ |
| 453 |
fail = true; |
| 454 |
print_result ("FAIL", setting, tag, expected_valid); |
| 455 |
puts(": returned NULL"); |
| 456 |
} |
| 457 |
else if (retval != cd->output) |
| 458 |
{ |
| 459 |
fail = true; |
| 460 |
print_result ("FAIL", setting, tag, expected_valid); |
| 461 |
printf(": returned %p, should be %p\n", |
| 462 |
(const void *)retval, (const void *)cd->output); |
| 463 |
} |
| 464 |
else if (strncmp (retval, setting, l_setting)) |
| 465 |
{ |
| 466 |
fail = true; |
| 467 |
print_result("FAIL", setting, tag, expected_valid); |
| 468 |
fputs(": got non-matching ", stdout); |
| 469 |
print_setting(retval, false); |
| 470 |
putchar('\n'); |
| 471 |
} |
| 472 |
} |
| 473 |
else |
| 474 |
{ |
| 475 |
if (retval) |
| 476 |
{ |
| 477 |
fail = true; |
| 478 |
print_result ("FAIL", setting, tag, expected_valid); |
| 479 |
fputs(": expected NULL, got ", stdout); |
| 480 |
print_setting (retval, false); |
| 481 |
putchar('\n'); |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
else |
| 486 |
{ |
| 487 |
fail = true; |
| 488 |
print_result("FAIL", setting, tag, expected_valid); |
| 489 |
printf(": %s\n", strsignal (sig)); |
| 490 |
} |
| 491 |
|
| 492 |
if (verbose && !fail) |
| 493 |
{ |
| 494 |
print_result("PASS", setting, tag, expected_valid); |
| 495 |
putchar('\n'); |
| 496 |
} |
| 497 |
|
| 498 |
return fail; |
| 499 |
} |
| 500 |
|
| 501 |
static bool |
| 502 |
test_one_valid(const struct valid_setting *tc, |
| 503 |
char *page, size_t pagesize, struct crypt_data *cd) |
| 504 |
{ |
| 505 |
/* Caution: tc->setting_len is _not_ always equal to strlen(tc->setting). |
| 506 |
Sometimes it is smaller. */ |
| 507 |
size_t l_setting = strlen(tc->setting) + 1; |
| 508 |
char *setting = page + pagesize - l_setting; |
| 509 |
memcpy(setting, tc->setting, l_setting); |
| 510 |
|
| 511 |
/* crypt_rn() using this setting, unmodified, is expected to |
| 512 |
succeed, unless the hash function is disabled. */ |
| 513 |
if (test_one_setting (setting, tc->setting_len, tc->tag, tc->enabled, cd)) |
| 514 |
return true; |
| 515 |
|
| 516 |
/* Rechecking the hash with the full output should also succeed. |
| 517 |
In this subtest we expect to get the same _complete hash_ |
| 518 |
back out, not just the same setting. */ |
| 519 |
if (tc->enabled) |
| 520 |
{ |
| 521 |
size_t l_hash = strlen (cd->output); |
| 522 |
char *p = page + pagesize - (l_hash + 1); |
| 523 |
assert (l_hash + 1 <= CRYPT_OUTPUT_SIZE); |
| 524 |
memcpy (p, cd->output, l_hash + 1); |
| 525 |
|
| 526 |
if (test_one_setting (p, l_hash, tc->tag, true, cd)) |
| 527 |
return true; |
| 528 |
|
| 529 |
/* When crypt() is called with a complete hashed passphrase as the |
| 530 |
setting string, the hashing method must not look at the hash |
| 531 |
component of the setting _at all_. We test this by supplying a |
| 532 |
string with one extra character, an A, which _could_ be part of |
| 533 |
the hash component for all supported methods, but which is much |
| 534 |
too short by itself. This should produce the same complete hash |
| 535 |
as the previous test. (It has to be a character which _could_ |
| 536 |
appear, because the generic crypt() machinery rejects setting |
| 537 |
strings containing invalid characters in any position.) |
| 538 |
|
| 539 |
Super special case: Don't do this subtest for sunmd5, |
| 540 |
because, due to a bug in its original implementation, the |
| 541 |
first character after the end of the salt _does_ affect the |
| 542 |
hash output. We have to preserve this bug for compatibility |
| 543 |
with existing sunmd5 hashed passphrases. */ |
| 544 |
if (!INCLUDE_sunmd5 || strncmp(tc->setting, "$md5", 4)) |
| 545 |
{ |
| 546 |
p = page + pagesize - (l_hash + 1 + l_setting + 1); |
| 547 |
memcpy (p, cd->output, l_hash + 1); |
| 548 |
|
| 549 |
char *settingA = page + pagesize - (l_setting + 1); |
| 550 |
memcpy(settingA, tc->setting, l_setting - 1); |
| 551 |
settingA[l_setting - 1] = 'A'; |
| 552 |
settingA[l_setting - 0] = '\0'; |
| 553 |
if (test_one_setting (settingA, tc->setting_len, tc->tag, true, cd)) |
| 554 |
return true; |
| 555 |
if (strcmp (cd->output, p)) |
| 556 |
{ |
| 557 |
print_result ("FAIL", settingA, tc->tag, true); |
| 558 |
/* Since cd->output and p are both hashed passphrases, not |
| 559 |
handcrafted invalid setting strings, we can safely print |
| 560 |
them with %s. */ |
| 561 |
printf (": expected %s, got %s\n", p, cd->output); |
| 562 |
return true; |
| 563 |
} |
| 564 |
else if (verbose) |
| 565 |
{ |
| 566 |
print_result ("PASS", settingA, tc->tag, true); |
| 567 |
printf (": got %s, as expected\n", cd->output); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
/* Restore the original data at 'setting', as expected by code |
| 572 |
below. */ |
| 573 |
memcpy(setting, tc->setting, l_setting); |
| 574 |
} |
| 575 |
|
| 576 |
/* The rest of the subtests in this function are logically independent. */ |
| 577 |
bool failed = false; |
| 578 |
|
| 579 |
/* Replacing any one character of this setting with a ':', leaving |
| 580 |
the rest of the string intact, should cause crypt_rn to fail. */ |
| 581 |
for (size_t i = 0; i < l_setting - 1; i++) |
| 582 |
{ |
| 583 |
char saved = setting[i]; |
| 584 |
setting[i] = ':'; |
| 585 |
failed |= test_one_setting(setting, tc->setting_len, tc->tag, false, cd); |
| 586 |
setting[i] = saved; |
| 587 |
} |
| 588 |
|
| 589 |
/* Chop off the last character of the setting string and test that. |
| 590 |
Then, replace the new last character of the setting string with a |
| 591 |
colon, and test that. (This is different from the earlier test |
| 592 |
where we replaced each character in turn with a colon but kept |
| 593 |
the rest of the string intact, because the hashing method might |
| 594 |
be calling strlen() on the setting string.) Repeat these two |
| 595 |
steps until we have just one character left, then stop. |
| 596 |
|
| 597 |
For instance, if the original setting string is |
| 598 |
$1$MJHnaAke$ |
| 599 |
then we test |
| 600 |
$1$MJHnaAke |
| 601 |
$1$MJHnaAk: |
| 602 |
$1$MJHnaAk |
| 603 |
$1$MJHnaA: |
| 604 |
$1$MJHnaA |
| 605 |
... |
| 606 |
$1 |
| 607 |
$: |
| 608 |
|
| 609 |
($1$MJHnaAke: would have been tested by the loop above. All the |
| 610 |
single-character strings that can be a prefix of a setting string |
| 611 |
from valid_cases---"$", "_", "M"---are tested by invalid_cases, |
| 612 |
is ":".) |
| 613 |
|
| 614 |
Up till this point l_setting has been _one more than_ |
| 615 |
strlen(setting), but in this loop it is more convenient to have |
| 616 |
it be equal to strlen(setting). */ |
| 617 |
l_setting -= 1; |
| 618 |
|
| 619 |
while (l_setting > 2) |
| 620 |
{ |
| 621 |
memmove(setting + 1, setting, l_setting - 1); |
| 622 |
setting += 1; |
| 623 |
l_setting -= 1; |
| 624 |
failed |= test_one_setting(setting, MIN (l_setting, tc->setting_len), |
| 625 |
tc->tag, |
| 626 |
tc->enabled |
| 627 |
&& tc->is_valid_trunc(tc, setting), |
| 628 |
cd); |
| 629 |
page[pagesize - 2] = ':'; |
| 630 |
failed |= test_one_setting(setting, l_setting, tc->tag, false, cd); |
| 631 |
} |
| 632 |
|
| 633 |
return failed; |
| 634 |
} |
| 635 |
|
| 636 |
static bool |
| 637 |
test_one_invalid(const struct invalid_setting *tc, |
| 638 |
char *page, size_t pagesize, struct crypt_data *cd) |
| 639 |
{ |
| 640 |
size_t l_setting = strlen(tc->setting) + 1; |
| 641 |
char *setting = page + pagesize - l_setting; |
| 642 |
memcpy(setting, tc->setting, l_setting); |
| 643 |
return test_one_setting(setting, l_setting - 1, tc->tag, false, cd); |
| 644 |
} |
| 645 |
|
| 646 |
static bool |
| 647 |
do_tests(char *page, size_t pagesize) |
| 648 |
{ |
| 649 |
bool failed = false; |
| 650 |
|
| 651 |
struct crypt_data cd; |
| 652 |
memset (&cd, 0, sizeof cd); |
| 653 |
|
| 654 |
for (size_t i = 0; i < ARRAY_SIZE (valid_cases); i++) |
| 655 |
failed |= test_one_valid (&valid_cases[i], page, pagesize, &cd); |
| 656 |
|
| 657 |
for (size_t i = 0; i < ARRAY_SIZE (invalid_cases); i++) |
| 658 |
failed |= test_one_invalid (&invalid_cases[i], page, pagesize, &cd); |
| 659 |
|
| 660 |
return failed; |
| 661 |
} |
| 662 |
|
| 663 |
int |
| 664 |
main (int argc, char **argv) |
| 665 |
{ |
| 666 |
if (argc <= 1) |
| 667 |
; |
| 668 |
else if (argc == 2 |
| 669 |
&& (!strcmp(argv[1], "-v") |
| 670 |
|| !strcmp(argv[1], "--verbose"))) |
| 671 |
verbose = true; |
| 672 |
else |
| 673 |
{ |
| 674 |
fprintf(stderr, "usage: %s [-v | --verbose]\n", argv[0]); |
| 675 |
return 99; |
| 676 |
} |
| 677 |
|
| 678 |
if (setvbuf(stdout, 0, _IOLBF, 0) || setvbuf(stderr, 0, _IOLBF, 0)) |
| 679 |
{ |
| 680 |
perror ("setvbuf"); |
| 681 |
return 99; |
| 682 |
} |
| 683 |
|
| 684 |
/* Set up a two-page region whose first page is read-write and |
| 685 |
whose second page is inaccessible. */ |
| 686 |
long pagesize_l = sysconf (_SC_PAGESIZE); |
| 687 |
if (pagesize_l < (long) CRYPT_OUTPUT_SIZE) |
| 688 |
{ |
| 689 |
printf ("ERROR: pagesize of %ld is too small\n", pagesize_l); |
| 690 |
return 99; |
| 691 |
} |
| 692 |
|
| 693 |
size_t pagesize = (size_t) pagesize_l; |
| 694 |
char *page = mmap (0, pagesize * 2, PROT_READ|PROT_WRITE, |
| 695 |
MAP_PRIVATE|MAP_ANON, -1, 0); |
| 696 |
if (page == MAP_FAILED) |
| 697 |
{ |
| 698 |
perror ("mmap"); |
| 699 |
return 99; |
| 700 |
} |
| 701 |
// coverity[overflow_sink] |
| 702 |
memset (page, 'x', pagesize * 2); |
| 703 |
if (mprotect (page + pagesize, pagesize, PROT_NONE)) |
| 704 |
{ |
| 705 |
perror ("mprotect"); |
| 706 |
return 99; |
| 707 |
} |
| 708 |
|
| 709 |
struct sigaction sa, os, ob; |
| 710 |
sigfillset (&sa.sa_mask); |
| 711 |
sa.sa_flags = SA_RESTART; |
| 712 |
sa.sa_handler = segv_handler; |
| 713 |
if (sigaction (SIGBUS, &sa, &ob) || sigaction (SIGSEGV, &sa, &os)) |
| 714 |
{ |
| 715 |
perror ("sigaction"); |
| 716 |
return 1; |
| 717 |
} |
| 718 |
|
| 719 |
measure_settings(); |
| 720 |
bool failed = do_tests (page, pagesize); |
| 721 |
|
| 722 |
sigaction (SIGBUS, &ob, 0); |
| 723 |
sigaction (SIGSEGV, &os, 0); |
| 724 |
|
| 725 |
return failed; |
| 726 |
} |