| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | |
| 3 * Use of this source code is governed by a BSD-style license that can be | |
| 4 * found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 /* | |
| 8 * Exercise object proxy mapping. Don't actually use real objects, | |
| 9 * but just made-up values. | |
| 10 * | |
| 11 * Use a simple array to hold object / name tuples to check that the | |
| 12 * object proxy can do lookups properly of all entered values as well | |
| 13 * as negative examples of objects that have never been entered / | |
| 14 * names that had never been issued. This array is unsorted, so | |
| 15 * searching it is inefficient, but should be obviously correct. | |
| 16 */ | |
| 17 | |
| 18 #include "native_client/src/include/portability.h" | |
| 19 | |
| 20 #include <stdio.h> | |
| 21 #include <stdlib.h> | |
| 22 #include <string.h> | |
| 23 | |
| 24 #include "native_client/src/shared/platform/nacl_log.h" | |
| 25 #include "native_client/src/shared/platform/nacl_secure_random.h" | |
| 26 #include "native_client/src/trusted/service_runtime/nacl_all_modules.h" | |
| 27 | |
| 28 #include "native_client/src/trusted/service_runtime/fs/obj_proxy.h" | |
| 29 | |
| 30 #define SEED_BYTES 16 | |
| 31 | |
| 32 int verbose; | |
| 33 | |
| 34 struct TestObj { | |
| 35 void *obj; | |
| 36 uint8_t const *name; | |
| 37 }; | |
| 38 | |
| 39 struct TestData { | |
| 40 struct TestObj *obj_tbl; | |
| 41 int obj_tbl_size; | |
| 42 int obj_tbl_used; | |
| 43 int namelen; | |
| 44 }; | |
| 45 | |
| 46 void TestDataCtor(struct TestData *self, int namelen) { | |
| 47 self->obj_tbl = NULL; | |
| 48 self->obj_tbl_size = 0; | |
| 49 self->obj_tbl_used = 0; | |
| 50 self->namelen = namelen; | |
| 51 } | |
| 52 | |
| 53 void TestDataDtor(struct TestData *self) { | |
| 54 free(self->obj_tbl); | |
| 55 } | |
| 56 | |
| 57 void TestDataEnterObj(struct TestData *self, void *obj, uint8_t const *name) { | |
| 58 if (self->obj_tbl_used >= self->obj_tbl_size) { | |
| 59 if (self->obj_tbl_size) { | |
| 60 self->obj_tbl_size *= 2; | |
| 61 } else { | |
| 62 self->obj_tbl_size = 16; | |
| 63 } | |
| 64 self->obj_tbl = realloc(self->obj_tbl, | |
| 65 self->obj_tbl_size * sizeof *self->obj_tbl); | |
| 66 if (NULL == self->obj_tbl) { | |
| 67 fprintf(stderr, "obj_tbl_enter: out of memory\n"); | |
| 68 exit(1); | |
| 69 } | |
| 70 } | |
| 71 self->obj_tbl[self->obj_tbl_used].obj = obj; | |
| 72 self->obj_tbl[self->obj_tbl_used].name = name; | |
| 73 ++self->obj_tbl_used; | |
| 74 } | |
| 75 | |
| 76 uint8_t const *TestDataFindName(struct TestData *self, void *obj) { | |
| 77 int i; | |
| 78 | |
| 79 for (i = 0; i < self->obj_tbl_used; ++i) { | |
| 80 if (self->obj_tbl[i].obj == obj) { | |
| 81 return self->obj_tbl[i].name; | |
| 82 } | |
| 83 } | |
| 84 return NULL; | |
| 85 } | |
| 86 | |
| 87 void *TestDataFindObj(struct TestData *self, uint8_t const *name) { | |
| 88 int i; | |
| 89 | |
| 90 /* the dirt simple, slow, but obviously correct algorithm */ | |
| 91 for (i = 0; i < self->obj_tbl_used; ++i) { | |
| 92 if (!memcmp(self->obj_tbl[i].name, name, self->namelen)) { | |
| 93 return self->obj_tbl[i].obj; | |
| 94 } | |
| 95 } | |
| 96 return NULL; | |
| 97 } | |
| 98 | |
| 99 void print_name(FILE *ostr, uint8_t const *name, int namelen) { | |
| 100 int i; | |
| 101 int v; | |
| 102 | |
| 103 for (i = 0; i < namelen; ++i) { | |
| 104 v = name[i] & 0xff; | |
| 105 if (0 == (i & 0x7)) { | |
| 106 putc(' ', ostr); | |
| 107 } | |
| 108 fprintf(ostr, "%02x", v); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 | |
| 113 int test_for_size(int namelen, int num_samples_init, int num_bogus_init, | |
| 114 struct NaClSecureRngIf *rng) { | |
| 115 struct TestData td; | |
| 116 struct NaClObjProxy nop; | |
| 117 uintptr_t samp; | |
| 118 void *obj; | |
| 119 uint8_t const *name; | |
| 120 uint8_t const *true_name; | |
| 121 void *found_obj; | |
| 122 int passed = 0; | |
| 123 uint8_t *bogus_name; | |
| 124 uintptr_t num_samples = (uintptr_t)num_samples_init; | |
| 125 uintptr_t num_bogus = (uintptr_t)num_bogus_init; | |
| 126 | |
| 127 bogus_name = malloc(namelen); | |
| 128 printf("Testing for name length %d, %"NACL_PRIxPTR" legit samples, " | |
| 129 "%"NACL_PRIxPTR" bogus lookups\n", | |
| 130 namelen, num_samples, num_bogus); | |
| 131 TestDataCtor(&td, namelen); | |
| 132 | |
| 133 NaClObjProxyCtor(&nop, rng, namelen); | |
| 134 | |
| 135 printf("insertion\n"); | |
| 136 for (samp = 0; samp < num_samples; ++samp) { | |
| 137 obj = (void *) samp; | |
| 138 name = NaClObjProxyInsert(&nop, obj); | |
| 139 | |
| 140 if (verbose) { | |
| 141 printf("%4"NACL_PRIxPTR": ", samp); | |
| 142 print_name(stdout, name, namelen); | |
| 143 putchar('\n'); | |
| 144 } | |
| 145 | |
| 146 TestDataEnterObj(&td, obj, name); | |
| 147 } | |
| 148 | |
| 149 printf("lookup\n"); | |
| 150 /* now look up every one */ | |
| 151 for (samp = 0; samp < num_samples; ++samp) { | |
| 152 obj = (void *) samp; | |
| 153 | |
| 154 true_name = TestDataFindName(&td, obj); | |
| 155 if (NULL == true_name) { | |
| 156 printf("`True name' for object %"NACL_PRIxPTR" not found\n", samp); | |
| 157 goto cleanup; | |
| 158 } | |
| 159 if (verbose) { | |
| 160 printf("[ %4"NACL_PRIxPTR": ", samp); | |
| 161 print_name(stdout, true_name, namelen); | |
| 162 printf(" ]\n"); | |
| 163 } | |
| 164 | |
| 165 name = NaClObjProxyFindNameByObj(&nop, obj); | |
| 166 if (NULL == name) { | |
| 167 printf("Object %"NACL_PRIxPTR" not found!\n", samp); | |
| 168 goto cleanup; | |
| 169 } | |
| 170 | |
| 171 if (memcmp(name, true_name, namelen)) { | |
| 172 printf("Name mismatch! Expected "); | |
| 173 print_name(stdout, true_name, namelen); | |
| 174 printf(" got "); | |
| 175 print_name(stdout, name, namelen); | |
| 176 printf("\n"); | |
| 177 goto cleanup; | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 /* now look up every one, backwards */ | |
| 182 printf("order reversed lookup\n"); | |
| 183 for (samp = num_samples; --samp > 0; ) { | |
| 184 obj = (void *) samp; | |
| 185 | |
| 186 true_name = TestDataFindName(&td, obj); | |
| 187 if (NULL == true_name) { | |
| 188 printf("`True name' for object %"NACL_PRIxPTR" not found\n", samp); | |
| 189 goto cleanup; | |
| 190 } | |
| 191 if (verbose) { | |
| 192 printf("[ %4"NACL_PRIxPTR": ", samp); | |
| 193 print_name(stdout, true_name, namelen); | |
| 194 printf(" ]\n"); | |
| 195 } | |
| 196 | |
| 197 name = NaClObjProxyFindNameByObj(&nop, obj); | |
| 198 if (NULL == name) { | |
| 199 printf("Object %"NACL_PRIxPTR" not found!\n", samp); | |
| 200 goto cleanup; | |
| 201 } | |
| 202 | |
| 203 if (memcmp(name, true_name, namelen)) { | |
| 204 printf("Name mismatch! Expected "); | |
| 205 print_name(stdout, true_name, namelen); | |
| 206 printf(" got "); | |
| 207 print_name(stdout, name, namelen); | |
| 208 printf("\n"); | |
| 209 goto cleanup; | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 printf("lookup by name\n"); | |
| 214 /* now look up every one */ | |
| 215 for (samp = 0; samp < num_samples; ++samp) { | |
| 216 obj = (void *) samp; | |
| 217 | |
| 218 true_name = TestDataFindName(&td, obj); | |
| 219 if (NULL == true_name) { | |
| 220 printf("`True name' for object %"NACL_PRIxPTR" not found\n", samp); | |
| 221 goto cleanup; | |
| 222 } | |
| 223 if (verbose) { | |
| 224 printf("[ %4"NACL_PRIxPTR": ", samp); | |
| 225 print_name(stdout, true_name, namelen); | |
| 226 printf(" ]\n"); | |
| 227 } | |
| 228 | |
| 229 found_obj = (void *) 0xdeadbeef; | |
| 230 if (!NaClObjProxyFindObjByName(&nop, true_name, &found_obj)) { | |
| 231 printf("Object %"NACL_PRIxPTR" not found, name ", samp); | |
| 232 print_name(stdout, true_name, namelen); | |
| 233 printf("!\n"); | |
| 234 goto cleanup; | |
| 235 } | |
| 236 | |
| 237 if (found_obj != obj) { | |
| 238 printf("Object mismatch! Expected 0x%08" | |
| 239 NACL_PRIxPTR" got 0x%08"NACL_PRIxPTR"\n", | |
| 240 (uintptr_t) obj, (uintptr_t) found_obj); | |
| 241 goto cleanup; | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 printf("lookup by name, rev order\n"); | |
| 246 /* now look up every one */ | |
| 247 for (samp = num_samples; --samp > 0; ) { | |
| 248 obj = (void *) samp; | |
| 249 | |
| 250 true_name = TestDataFindName(&td, obj); | |
| 251 if (NULL == true_name) { | |
| 252 printf("`True name' for object %"NACL_PRIxPTR" not found\n", samp); | |
| 253 goto cleanup; | |
| 254 } | |
| 255 if (verbose) { | |
| 256 printf("[ %4"NACL_PRIxPTR": ", samp); | |
| 257 print_name(stdout, true_name, namelen); | |
| 258 printf(" ]\n"); | |
| 259 } | |
| 260 | |
| 261 if (!NaClObjProxyFindObjByName(&nop, true_name, &found_obj)) { | |
| 262 printf("Object %"NACL_PRIxPTR" not found!\n", samp); | |
| 263 goto cleanup; | |
| 264 } | |
| 265 | |
| 266 if (found_obj != obj) { | |
| 267 printf("Object mismatch! Expected 0x%08"NACL_PRIxPTR | |
| 268 " got 0x%08"NACL_PRIxPTR, | |
| 269 (uintptr_t) obj, (uintptr_t) found_obj); | |
| 270 goto cleanup; | |
| 271 } | |
| 272 } | |
| 273 | |
| 274 printf("Bogus objects\n"); | |
| 275 for (samp = num_samples; samp < num_samples + num_bogus; ++samp) { | |
| 276 obj = (void *) samp; | |
| 277 name = NaClObjProxyFindNameByObj(&nop, obj); | |
| 278 if (NULL != name) { | |
| 279 printf("Bogus object %"NACL_PRIxPTR" FOUND\n", samp); | |
| 280 goto cleanup; | |
| 281 } | |
| 282 } | |
| 283 | |
| 284 printf("Bogus names\n"); | |
| 285 for (samp = 0; samp < num_bogus; ++samp) { | |
| 286 (*rng->vtbl->GenBytes)(rng, bogus_name, namelen); | |
| 287 if (NaClObjProxyFindObjByName(&nop, bogus_name, &obj)) { | |
| 288 printf("Bogus name"); | |
| 289 print_name(stdout, bogus_name, namelen); | |
| 290 printf(" FOUND!\n"); | |
| 291 goto cleanup; | |
| 292 } | |
| 293 } | |
| 294 | |
| 295 printf("OK\n"); | |
| 296 passed = 1; | |
| 297 | |
| 298 cleanup: | |
| 299 NaClObjProxyDtor(&nop); | |
| 300 TestDataDtor(&td); | |
| 301 free(bogus_name); | |
| 302 return passed; | |
| 303 } | |
| 304 | |
| 305 | |
| 306 /* | |
| 307 * A very weak linear congruential generator. Only use for testing. | |
| 308 */ | |
| 309 struct WeakRng { | |
| 310 struct NaClSecureRngIf base; | |
| 311 uint32_t x; | |
| 312 }; | |
| 313 | |
| 314 static struct NaClSecureRngIfVtbl const kWeakRngVtbl; | |
| 315 | |
| 316 int WeakRngCtor(struct WeakRng *self) { | |
| 317 self->base.vtbl = &kWeakRngVtbl; | |
| 318 self->x = 263; | |
| 319 return 1; | |
| 320 } | |
| 321 | |
| 322 int WeakRngTestingCtor(struct WeakRng *self, | |
| 323 uint8_t *seed_material, | |
| 324 size_t seed_bytes) { | |
| 325 self->x = 0; | |
| 326 while (seed_bytes > 0) { | |
| 327 self->x = (self->x * 263) ^ seed_material[--seed_bytes]; | |
| 328 } | |
| 329 if (0 == self->x) { | |
| 330 self->x = 263; | |
| 331 } | |
| 332 self->base.vtbl = &kWeakRngVtbl; | |
| 333 return 1; | |
| 334 } | |
| 335 | |
| 336 void WeakRngDtor(struct NaClSecureRngIf *vself) { | |
| 337 vself->vtbl = NULL; | |
| 338 } | |
| 339 | |
| 340 uint8_t WeakRngGenByte(struct NaClSecureRngIf *vself) { | |
| 341 struct WeakRng *self = (struct WeakRng *) vself; | |
| 342 uint64_t y; | |
| 343 | |
| 344 y = (uint64_t) self->x; | |
| 345 y *= 16807; | |
| 346 y = y + (y >> 31); | |
| 347 y = y & ((1u << 31) - 1); | |
| 348 self->x = (uint32_t) y; | |
| 349 return self->x; | |
| 350 } | |
| 351 | |
| 352 static struct NaClSecureRngIfVtbl const kWeakRngVtbl = { | |
| 353 WeakRngDtor, | |
| 354 WeakRngGenByte, | |
| 355 NaClSecureRngDefaultGenUint32, | |
| 356 NaClSecureRngDefaultGenBytes, | |
| 357 NaClSecureRngDefaultUniform, | |
| 358 }; | |
| 359 | |
| 360 | |
| 361 int RunTests(int namelen_start, int namelen_end, int namelen_inc, | |
| 362 int num_samples, int num_bogus, | |
| 363 struct NaClSecureRngIf *rng) { | |
| 364 int pass = 1; | |
| 365 int namelen; | |
| 366 | |
| 367 for (namelen = namelen_start; namelen < namelen_end; namelen += namelen_inc) { | |
| 368 pass = pass && test_for_size(namelen, num_samples, num_bogus, rng); | |
| 369 } | |
| 370 return pass; | |
| 371 } | |
| 372 | |
| 373 int main(int ac, char **av) { | |
| 374 int opt; | |
| 375 int namelen_start = 8; | |
| 376 int namelen_end = 32; | |
| 377 int namelen_inc = 1; | |
| 378 int num_samples = 1 << 16; | |
| 379 int num_bogus = 1024; | |
| 380 int pass = 1; | |
| 381 | |
| 382 int seed_provided = 0; | |
| 383 uint8_t seed_material[SEED_BYTES]; | |
| 384 size_t ix; | |
| 385 unsigned int seed_byte; | |
| 386 | |
| 387 struct NaClSecureRng rng; | |
| 388 struct NaClSecureRngIf *rp; | |
| 389 int use_weak_rng = 0; | |
| 390 struct WeakRng weak_rng; | |
| 391 int fallback_to_weak = 1; | |
| 392 | |
| 393 memset(seed_material, 0, sizeof seed_material); | |
| 394 | |
| 395 while (-1 != (opt = getopt(ac, av, "s:e:i:n:b:vS:fFwW"))) { | |
| 396 switch (opt) { | |
| 397 case 's': | |
| 398 namelen_start = strtol(optarg, (char **) 0, 0); | |
| 399 break; | |
| 400 case 'e': | |
| 401 namelen_end = strtol(optarg, (char **) 0, 0); | |
| 402 break; | |
| 403 case 'i': | |
| 404 namelen_inc = strtol(optarg, (char **) 0, 0); | |
| 405 break; | |
| 406 case 'n': | |
| 407 num_samples = strtol(optarg, (char **) 0, 0); | |
| 408 break; | |
| 409 case 'b': | |
| 410 num_bogus = strtol(optarg, (char **) 0, 0); | |
| 411 break; | |
| 412 case 'v': | |
| 413 ++verbose; | |
| 414 break; | |
| 415 case 'S': | |
| 416 for (ix = 0; | |
| 417 (1 == sscanf(optarg + 2*ix, "%2x", &seed_byte)) | |
| 418 && ix < sizeof seed_material; | |
| 419 ++ix) { | |
| 420 seed_material[ix] = seed_byte; | |
| 421 } | |
| 422 seed_provided = 1; | |
| 423 break; | |
| 424 case 'f': | |
| 425 fallback_to_weak = 1; | |
| 426 break; | |
| 427 case 'F': | |
| 428 fallback_to_weak = 0; | |
| 429 break; | |
| 430 case 'w': | |
| 431 use_weak_rng = 1; | |
| 432 break; | |
| 433 case 'W': | |
| 434 use_weak_rng = 0; | |
| 435 break; | |
| 436 default: | |
| 437 fprintf(stderr, | |
| 438 "Usage: obj_proxy_test [-s start_namelen] [-e end_namelen]\n" | |
| 439 " [-i incr_namelen] [-n num_samples]\n" | |
| 440 " [-b num_bogus_samples]\n" | |
| 441 " [-v]\n" | |
| 442 " [-S] hex-seed-material\n" | |
| 443 " [-fFwW]\n" | |
| 444 " -v verbose mode\n" | |
| 445 " -f fall back to a weak generator if deterministic seeding\n" | |
| 446 " is not possible (default) \n" | |
| 447 " -F fall back to non-deterministic seeding if\n" | |
| 448 " deterministic seeding is not possible\n" | |
| 449 " -w use a weak generator\n" | |
| 450 " -W use the standard secure generator (default)\n" | |
| 451 "\n" | |
| 452 " NB: it is bad if num_samples >= 2**(4*start_namelen)\n" | |
| 453 " since the birthday paradox will cause name collisions\n" | |
| 454 ); | |
| 455 exit(1); | |
| 456 } | |
| 457 } | |
| 458 NaClAllModulesInit(); | |
| 459 if (namelen_start > namelen_end || namelen_inc <= 0) { | |
| 460 fprintf(stderr, "obj_proxy_test: name length parameters are bogus\n"); | |
| 461 exit(1); | |
| 462 } | |
| 463 | |
| 464 if (!seed_provided) { | |
| 465 NaClSecureRngCtor(&rng); | |
| 466 | |
| 467 for (ix = 0; ix < sizeof seed_material; ++ix) { | |
| 468 seed_material[ix] = (*rng.base.vtbl->GenByte)(&rng.base); | |
| 469 } | |
| 470 | |
| 471 (*rng.base.vtbl->Dtor)(&rng.base); | |
| 472 } | |
| 473 | |
| 474 printf("TEST RNG SEED: "); | |
| 475 for (ix = 0; ix < sizeof seed_material; ++ix) { | |
| 476 printf("%02x", seed_material[ix]); | |
| 477 } | |
| 478 printf("\n"); | |
| 479 | |
| 480 if (use_weak_rng) { | |
| 481 if (verbose) { | |
| 482 printf("Using weak generator\n"); | |
| 483 } | |
| 484 WeakRngTestingCtor(&weak_rng, seed_material, sizeof seed_material); | |
| 485 rp = &weak_rng.base; | |
| 486 } else { | |
| 487 if (!NaClSecureRngTestingCtor(&rng, seed_material, sizeof seed_material)) { | |
| 488 if (fallback_to_weak) { | |
| 489 printf("Cannot set seed. Reverting to weak generator.\n"); | |
| 490 WeakRngTestingCtor(&weak_rng, seed_material, sizeof seed_material); | |
| 491 rp = &weak_rng.base; | |
| 492 } else { | |
| 493 printf("Cannot set seed. Reverting to nondeterministic generator.\n"); | |
| 494 NaClSecureRngCtor(&rng); | |
| 495 rp = &rng.base; | |
| 496 } | |
| 497 } else { | |
| 498 if (verbose) { | |
| 499 printf("Using standard generator\n"); | |
| 500 } | |
| 501 rp = &rng.base; | |
| 502 } | |
| 503 } | |
| 504 | |
| 505 pass = pass && RunTests(namelen_start, namelen_end, namelen_inc, | |
| 506 num_samples, num_bogus, rp); | |
| 507 | |
| 508 (*rp->vtbl->Dtor)(rp); | |
| 509 | |
| 510 NaClAllModulesFini(); | |
| 511 | |
| 512 printf("%s\n", pass ? "PASSED" : "FAILED"); | |
| 513 | |
| 514 return !pass; | |
| 515 } | |
| OLD | NEW |