Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2009 The Native Client Authors. All rights reserved. | 2 * Copyright 2009 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
| 4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 /* | 7 /* |
| 8 * ncvalidate_iter.c | 8 * ncvalidate_iter.c |
| 9 * Validate x86 instructions for Native Client | 9 * Validate x86 instructions for Native Client |
| 10 * | 10 * |
| 11 */ | 11 */ |
| 12 | 12 |
| 13 #include <assert.h> | 13 #include <assert.h> |
| 14 #include <string.h> | 14 #include <string.h> |
| 15 | 15 |
| 16 #include "native_client/src/trusted/validator_x86/ncvalidate_iter.h" | 16 #include "native_client/src/trusted/validator_x86/ncvalidate_iter.h" |
| 17 | 17 #include "native_client/src/shared/platform/nacl_check.h" |
| 18 #include "native_client/src/shared/platform/nacl_log.h" | 18 #include "native_client/src/shared/platform/nacl_log.h" |
| 19 #include "native_client/src/trusted/validator_x86/nc_inst_iter.h" | 19 #include "native_client/src/trusted/validator_x86/nc_inst_iter.h" |
| 20 #include "native_client/src/trusted/validator_x86/nc_inst_state_internal.h" | 20 #include "native_client/src/trusted/validator_x86/nc_inst_state_internal.h" |
| 21 #include "native_client/src/trusted/validator_x86/nc_segment.h" | 21 #include "native_client/src/trusted/validator_x86/nc_segment.h" |
| 22 #include "native_client/src/trusted/validator_x86/ncop_exps.h" | 22 #include "native_client/src/trusted/validator_x86/ncop_exps.h" |
| 23 #include "native_client/src/trusted/validator_x86/ncvalidate_iter_internal.h" | 23 #include "native_client/src/trusted/validator_x86/ncvalidate_iter_internal.h" |
| 24 #include "native_client/src/trusted/validator_x86/ncvalidator_registry.h" | 24 #include "native_client/src/trusted/validator_x86/ncvalidator_registry.h" |
| 25 | 25 |
| 26 /* To turn on debugging of instruction decoding, change value of | 26 /* To turn on debugging of instruction decoding, change value of |
| 27 * DEBUGGING to 1. | 27 * DEBUGGING to 1. |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 NaClLogV_mu(level, format, ap); | 252 NaClLogV_mu(level, format, ap); |
| 253 va_end(ap); | 253 va_end(ap); |
| 254 NaClLogUnlock(); | 254 NaClLogUnlock(); |
| 255 NaClRecordErrorReported(state, level); | 255 NaClRecordErrorReported(state, level); |
| 256 } | 256 } |
| 257 if (state->do_stub_out && (level <= LOG_ERROR)) { | 257 if (state->do_stub_out && (level <= LOG_ERROR)) { |
| 258 memset(inst->mpc, kNaClFullStop, inst->length); | 258 memset(inst->mpc, kNaClFullStop, inst->length); |
| 259 } | 259 } |
| 260 } | 260 } |
| 261 | 261 |
| 262 void NaClValidatorTwoInstMessage(int level, | |
| 263 NaClValidatorState* state, | |
| 264 NaClInstState* inst1, | |
| 265 NaClInstState* inst2, | |
| 266 const char* format, | |
| 267 ...) { | |
| 268 level = NaClRecordIfValidatorError(state, level); | |
| 269 if (NaClPrintValidatorMessages(state, level)) { | |
| 270 va_list ap; | |
| 271 struct Gio* g = NaClLogGetGio(); | |
| 272 | |
| 273 NaClLogLock(); | |
| 274 /* TODO(karl) - Make printing of instruction state possible via format. */ | |
| 275 va_start(ap, format); | |
| 276 NaClLog_mu(level, "VALIDATOR: %s", NaClLogLevelLabel(level)); | |
| 277 NaClLogV_mu(level, format, ap); | |
| 278 va_end(ap); | |
| 279 | |
| 280 /* TODO(karl): empty fmt strings not allowed */ | |
| 281 NaClLog_mu(level, "\n%45s ", "VALIDATOR:"); | |
| 282 NaClInstStateInstPrint(g, inst1); | |
| 283 /* TODO(karl): empty fmt strings not allowed */ | |
| 284 NaClLog_mu(level, "%45s ", "VALIDATOR:"); | |
| 285 NaClInstStateInstPrint(g, inst2); | |
| 286 | |
| 287 NaClLogUnlock(); | |
| 288 NaClRecordErrorReported(state, level); | |
| 289 } | |
| 290 if (state->do_stub_out && (level <= LOG_ERROR)) { | |
| 291 memset(inst1->mpc, kNaClFullStop, inst1->length); | |
| 292 memset(inst2->mpc, kNaClFullStop, inst2->length); | |
| 293 } | |
| 294 } | |
| 295 | |
| 262 Bool NaClValidatorQuit(NaClValidatorState* state) { | 296 Bool NaClValidatorQuit(NaClValidatorState* state) { |
| 263 return !state->validates_ok && (state->quit_after_error_count == 0); | 297 return !state->validates_ok && (state->quit_after_error_count == 0); |
| 264 } | 298 } |
| 265 | 299 |
| 266 void NaClRegisterValidator( | 300 void NaClRegisterValidator( |
| 267 NaClValidatorState* state, | 301 NaClValidatorState* state, |
| 268 NaClValidator validator, | 302 NaClValidator validator, |
| 269 NaClValidatorPostValidate post_validate, | 303 NaClValidatorPostValidate post_validate, |
| 270 NaClValidatorPrintStats print_stats, | 304 NaClValidatorPrintStats print_stats, |
| 271 NaClValidatorMemoryCreate create_memory, | 305 NaClValidatorMemoryCreate create_memory, |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 443 void* NaClGetValidatorLocalMemory(NaClValidator validator, | 477 void* NaClGetValidatorLocalMemory(NaClValidator validator, |
| 444 const NaClValidatorState* state) { | 478 const NaClValidatorState* state) { |
| 445 int i; | 479 int i; |
| 446 for (i = 0; i < state->number_validators; ++i) { | 480 for (i = 0; i < state->number_validators; ++i) { |
| 447 if (state->validators[i].validator == validator) { | 481 if (state->validators[i].validator == validator) { |
| 448 return state->local_memory[i]; | 482 return state->local_memory[i]; |
| 449 } | 483 } |
| 450 } | 484 } |
| 451 return NULL; | 485 return NULL; |
| 452 } | 486 } |
| 487 | |
| 488 /* | |
| 489 * Check that iter_new is a valid replacement for iter_old. | |
| 490 * If a validation error occurs, state->validates_ok will be set to false by | |
| 491 * NaClValidatorInstMessage when it is given LOG_ERROR, see the end of this | |
| 492 * function. | |
| 493 */ | |
| 494 static void NaClValidateInstReplacement(NaClInstIter* iter_old, | |
| 495 NaClInstIter* iter_new, | |
| 496 struct NaClValidatorState* state) { | |
| 497 NaClInstState *istate_old, *istate_new; | |
| 498 NaClExpVector *exp_old, *exp_new; | |
| 499 uint32_t i; | |
| 500 | |
| 501 istate_old = NaClInstIterGetState(iter_old); | |
| 502 istate_new = NaClInstIterGetState(iter_new); | |
| 503 | |
| 504 /* Location/length must match */ | |
| 505 if (istate_new->vpc != istate_old->vpc || | |
| 506 istate_new->length != istate_old->length) { | |
| 507 NaClValidatorTwoInstMessage(LOG_ERROR, state, istate_old, istate_new, | |
| 508 "Code modification: instructions length/addresses do not match"); | |
| 509 return; | |
| 510 } | |
| 511 | |
| 512 | |
| 513 do { | |
| 514 /* fast check if the replacement is identical */ | |
| 515 if (!memcmp(istate_old->mpc, istate_new->mpc, istate_old->length)) | |
| 516 return; | |
| 517 | |
| 518 if (istate_old->num_prefix_bytes != istate_new->num_prefix_bytes) | |
| 519 break; | |
| 520 if (istate_old->num_rex_prefixes != istate_new->num_rex_prefixes) | |
| 521 break; | |
| 522 if (istate_old->rexprefix != istate_new->rexprefix) | |
| 523 break; | |
| 524 if (istate_old->modrm != istate_new->modrm) | |
| 525 break; | |
| 526 if (istate_old->has_sib != istate_new->has_sib) | |
| 527 break; | |
| 528 if (istate_old->has_sib && istate_old->sib != istate_new->sib) | |
| 529 break; | |
| 530 if (istate_old->operand_size != istate_new->operand_size) | |
| 531 break; | |
| 532 if (istate_old->address_size != istate_new->address_size) | |
| 533 break; | |
| 534 if (istate_old->prefix_mask != istate_new->prefix_mask) | |
| 535 break; | |
| 536 | |
| 537 /* | |
| 538 * these are pointers, but they reference entries in a static table, | |
| 539 * so if the two instructions are the same, then these pointers must | |
| 540 * reference the same entry | |
| 541 */ | |
| 542 if (istate_old->inst != istate_new->inst) | |
| 543 break; | |
| 544 | |
| 545 exp_old = NaClInstStateExpVector(istate_old); | |
| 546 exp_new = NaClInstStateExpVector(istate_new); | |
| 547 | |
| 548 /* check if the instruction operands are identical */ | |
| 549 if (exp_old->number_expr_nodes != exp_new->number_expr_nodes) | |
| 550 break; | |
| 551 | |
| 552 for (i = 0; i < exp_old->number_expr_nodes; i++) { | |
| 553 if (exp_old->node[i].kind != exp_new->node[i].kind) | |
| 554 break; | |
|
elijahtaylor (use chromium)
2010/12/10 20:01:40
are these breaks in this for loop correct? It see
Karl
2010/12/10 21:18:15
Good point. if they are different, an error shoul
| |
| 555 if (exp_old->node[i].flags != exp_new->node[i].flags) | |
| 556 break; | |
| 557 | |
| 558 /* | |
| 559 * allow some constants to be different; however it is important not to | |
| 560 * allow modification of sandboxing instructions. Note nether of the | |
|
elijahtaylor (use chromium)
2010/12/10 20:01:40
s/nether/neither/
Karl
2010/12/10 21:18:15
This correction should be made.
| |
| 561 * instructions allowed for modification is used for sandboxing | |
| 562 */ | |
| 563 if (exp_old->node[i].value != exp_new->node[i].value) { | |
| 564 if (exp_old->node[i].kind == ExprConstant) { | |
| 565 | |
| 566 /* allow different constants in direct calls */ | |
| 567 if (istate_old->inst->name == InstCall) | |
| 568 if (exp_old->node[i].flags & NACL_EFLAG(ExprJumpTarget)) | |
| 569 if (exp_old->node[NaClGetExpParentIndex(exp_old, i)].kind | |
| 570 == OperandReference) | |
| 571 continue; | |
| 572 | |
| 573 /* | |
| 574 * allow different constants in operand of mov | |
| 575 * e.g. mov $rax, 0xdeadbeef | |
| 576 */ | |
| 577 if (istate_old->inst->name == InstMov) | |
| 578 if (exp_old->node[i].flags & NACL_EFLAG(ExprUsed)) | |
| 579 if (exp_old->node[NaClGetExpParentIndex(exp_old, i)].kind | |
| 580 == OperandReference) | |
| 581 continue; | |
| 582 /* | |
| 583 * allow different displacements in memory reference of mov | |
| 584 * instructions e.g. mov $rax, [$r15+$rbx*2+0x7fff] | |
| 585 */ | |
| 586 if (istate_old->inst->name == InstMov) | |
| 587 if (exp_old->node[NaClGetExpParentIndex(exp_old, i)].kind | |
| 588 == ExprMemOffset) | |
| 589 /* displacement is the fourth node after ExprMemOffset node */ | |
| 590 if (i - NaClGetExpParentIndex(exp_old, i) == 4) | |
| 591 continue; | |
| 592 } | |
| 593 break; | |
| 594 } | |
| 595 } | |
| 596 | |
| 597 return; | |
| 598 } while (0); | |
| 599 | |
| 600 NaClValidatorTwoInstMessage(LOG_ERROR, state, istate_old, istate_new, | |
| 601 "Code modification: failed to modify instruction"); | |
| 602 } | |
| 603 | |
| 604 void NaClValidateSegmentPair(uint8_t* mbase_old, uint8_t* mbase_new, | |
| 605 NaClPcAddress vbase, size_t size, | |
| 606 struct NaClValidatorState* state) { | |
| 607 NaClSegment segment_old, segment_new; | |
| 608 NaClInstIter *iter_old, *iter_new; | |
| 609 | |
| 610 NaClValidatorStateInitializeValidators(state); | |
| 611 NaClSegmentInitialize(mbase_old, vbase, size, &segment_old); | |
| 612 NaClSegmentInitialize(mbase_new, vbase, size, &segment_new); | |
| 613 iter_old = NaClInstIterCreateWithLookback(&segment_old, kLookbackSize); | |
| 614 iter_new = NaClInstIterCreateWithLookback(&segment_new, kLookbackSize); | |
| 615 while (NaClInstIterHasNext(iter_old) && | |
| 616 NaClInstIterHasNext(iter_new)) { | |
| 617 state->cur_inst_state = NaClInstIterGetState(iter_new); | |
| 618 state->cur_inst = NaClInstStateInst(state->cur_inst_state); | |
| 619 state->cur_inst_vector = NaClInstStateExpVector(state->cur_inst_state); | |
| 620 NaClApplyValidators(state, iter_new); | |
| 621 NaClValidateInstReplacement(iter_old, iter_new, state); | |
| 622 if (state->quit) break; | |
| 623 NaClInstIterAdvance(iter_old); | |
| 624 NaClInstIterAdvance(iter_new); | |
| 625 } | |
| 626 | |
| 627 if (NaClInstIterHasNext(iter_old) || | |
| 628 NaClInstIterHasNext(iter_new)) { | |
| 629 NaClValidatorMessage(LOG_ERROR, state, | |
| 630 "Code modification: code segments have different number of instructions\n"); | |
| 631 } | |
| 632 | |
| 633 state->cur_inst_state = NULL; | |
| 634 state->cur_inst = NULL; | |
| 635 state->cur_inst_vector = NULL; | |
| 636 NaClApplyPostValidators(state, iter_new); | |
| 637 NaClInstIterDestroy(iter_old); | |
| 638 NaClInstIterDestroy(iter_new); | |
| 639 NaClValidatorStatePrintStats(state); | |
| 640 } | |
| 641 | |
| OLD | NEW |