Security.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "Backend.h"
  6. void
  7. Security::EncodeLargeConstants()
  8. {
  9. #pragma prefast(suppress:6236 6285, "logical-or of constants is by design")
  10. if (PHASE_OFF(Js::EncodeConstantsPhase, this->func) || CONFIG_ISENABLED(Js::DebugFlag) || !MD_ENCODE_LG_CONSTS)
  11. {
  12. return;
  13. }
  14. uint prevInstrConstSize = 0;
  15. FOREACH_INSTR_IN_FUNC_EDITING(instr, instrNext, this->func)
  16. {
  17. if (!instr->IsRealInstr())
  18. {
  19. if (instr->IsLabelInstr())
  20. {
  21. IR::LabelInstr * label = instr->AsLabelInstr();
  22. if (label->labelRefs.Count() > 1 || (label->labelRefs.Count() == 1 && label->labelRefs.Head() != label->m_prev))
  23. {
  24. if (this->cookieOpnd != nullptr)
  25. {
  26. this->cookieOpnd->Free(this->func);
  27. }
  28. this->baseOpnd = nullptr;
  29. this->cookieOpnd = nullptr;
  30. this->basePlusCookieOpnd = nullptr;
  31. }
  32. }
  33. continue;
  34. }
  35. IR::Opnd *src1 = instr->GetSrc1();
  36. IR::Opnd *src2 = instr->GetSrc2();
  37. IR::Opnd *dst = instr->GetDst();
  38. if (dst && this->baseOpnd && dst->IsEqual(this->baseOpnd))
  39. {
  40. if (this->cookieOpnd != nullptr)
  41. {
  42. this->cookieOpnd->Free(this->func);
  43. }
  44. this->baseOpnd = nullptr;
  45. this->cookieOpnd = nullptr;
  46. this->basePlusCookieOpnd = nullptr;
  47. }
  48. uint currInstrConstSize = 0;
  49. uint dstSize = dst ? CalculateConstSize(dst) : 0;
  50. uint src1Size = 0;
  51. uint src2Size = 0;
  52. if (src1)
  53. {
  54. src1Size = CalculateConstSize(src1);
  55. if (src2)
  56. {
  57. src2Size = CalculateConstSize(src2);
  58. }
  59. }
  60. prevInstrConstSize = currInstrConstSize;
  61. currInstrConstSize = dstSize + src1Size + src2Size;
  62. // we don't need to blind constants if user controlled byte size < 3
  63. if (currInstrConstSize + prevInstrConstSize <= 2 && !PHASE_FORCE1(Js::EncodeConstantsPhase))
  64. {
  65. continue;
  66. }
  67. bool isSrc1EqualDst = false;
  68. if (dstSize >= 2)
  69. {
  70. // don't count instrs where dst == src1 against size
  71. if (src1 && dstSize == src1Size && src1->IsEqual(dst))
  72. {
  73. currInstrConstSize -= dstSize;
  74. isSrc1EqualDst = true;
  75. if (currInstrConstSize + prevInstrConstSize <= 2 && !PHASE_FORCE1(Js::EncodeConstantsPhase))
  76. {
  77. continue;
  78. }
  79. }
  80. this->EncodeOpnd(instr, dst);
  81. if (isSrc1EqualDst)
  82. {
  83. instr->ReplaceSrc1(dst);
  84. }
  85. currInstrConstSize -= dstSize;
  86. if (currInstrConstSize + prevInstrConstSize <= 2 && !PHASE_FORCE1(Js::EncodeConstantsPhase))
  87. {
  88. continue;
  89. }
  90. }
  91. if (src1Size >= 2 && !isSrc1EqualDst)
  92. {
  93. this->EncodeOpnd(instr, src1);
  94. currInstrConstSize -= src1Size;
  95. if (currInstrConstSize + prevInstrConstSize <= 2 && !PHASE_FORCE1(Js::EncodeConstantsPhase))
  96. {
  97. continue;
  98. }
  99. }
  100. if (src2Size >= 2)
  101. {
  102. this->EncodeOpnd(instr, src2);
  103. currInstrConstSize -= src2Size;
  104. }
  105. } NEXT_INSTR_IN_FUNC_EDITING;
  106. }
  107. int
  108. Security::GetNextNOPInsertPoint()
  109. {
  110. uint frequency = (1 << CONFIG_FLAG(NopFrequency)) - 1;
  111. return (Math::Rand() & frequency) + 1;
  112. }
  113. void
  114. Security::InsertRandomFunctionPad(IR::Instr * instrBeforeInstr)
  115. {
  116. if (PHASE_OFF(Js::InsertNOPsPhase, instrBeforeInstr->m_func->GetTopFunc())
  117. || CONFIG_ISENABLED(Js::DebugFlag) || CONFIG_ISENABLED(Js::BenchmarkFlag))
  118. {
  119. return;
  120. }
  121. DWORD randomPad = Math::Rand() & ((0 - INSTR_ALIGNMENT) & 0xF);
  122. #ifndef _M_ARM
  123. if (randomPad == 1)
  124. {
  125. InsertSmallNOP(instrBeforeInstr, 1);
  126. return;
  127. }
  128. if (randomPad & 1)
  129. {
  130. InsertSmallNOP(instrBeforeInstr, 3);
  131. randomPad -= 3;
  132. }
  133. #endif
  134. Assert((randomPad & 1) == 0);
  135. while (randomPad >= 4)
  136. {
  137. InsertSmallNOP(instrBeforeInstr, 4);
  138. randomPad -= 4;
  139. }
  140. Assert(randomPad == 2 || randomPad == 0);
  141. if (randomPad == 2)
  142. {
  143. InsertSmallNOP(instrBeforeInstr, 2);
  144. }
  145. }
  146. void
  147. Security::InsertNOPs()
  148. {
  149. if (PHASE_OFF(Js::InsertNOPsPhase, this->func) || CONFIG_ISENABLED(Js::DebugFlag) || CONFIG_ISENABLED(Js::BenchmarkFlag))
  150. {
  151. return;
  152. }
  153. int count = 0;
  154. IR::Instr *instr = this->func->m_headInstr;
  155. while (true)
  156. {
  157. count = this->GetNextNOPInsertPoint();
  158. while (instr && count--)
  159. {
  160. instr = instr->GetNextRealInstr();
  161. }
  162. if (instr == nullptr)
  163. {
  164. break;
  165. }
  166. this->InsertNOPBefore(instr);
  167. };
  168. }
  169. void
  170. Security::InsertNOPBefore(IR::Instr *instr)
  171. {
  172. InsertSmallNOP(instr, (Math::Rand() & 0x3) + 1);
  173. }
  174. void
  175. Security::InsertSmallNOP(IR::Instr * instr, DWORD nopSize)
  176. {
  177. #if defined(_M_IX86) || defined(_M_X64)
  178. #ifdef _M_IX86
  179. if (AutoSystemInfo::Data.SSE2Available())
  180. { // on x86 system that has SSE2, encode fast NOPs as x64 does
  181. #endif
  182. Assert(nopSize >= 1 || nopSize <= 4);
  183. IR::Instr *nop = IR::Instr::New(Js::OpCode::NOP, instr->m_func);
  184. // Let the encoder know what the size of the NOP needs to be.
  185. if (nopSize > 1)
  186. {
  187. // 2, 3 or 4 byte NOP.
  188. IR::IntConstOpnd *nopSizeOpnd = IR::IntConstOpnd::New(nopSize, TyInt8, instr->m_func);
  189. nop->SetSrc1(nopSizeOpnd);
  190. }
  191. instr->InsertBefore(nop);
  192. #ifdef _M_IX86
  193. }
  194. else
  195. {
  196. IR::Instr *nopInstr = nullptr;
  197. IR::RegOpnd *regOpnd;
  198. IR::IndirOpnd *indirOpnd;
  199. switch (nopSize)
  200. {
  201. case 1:
  202. // nop
  203. nopInstr = IR::Instr::New(Js::OpCode::NOP, instr->m_func);
  204. break;
  205. case 2:
  206. // mov edi, edi ; 2 bytes
  207. regOpnd = IR::RegOpnd::New(nullptr, RegEDI, TyInt32, instr->m_func);
  208. nopInstr = IR::Instr::New(Js::OpCode::MOV, regOpnd, regOpnd, instr->m_func);
  209. break;
  210. case 3:
  211. // lea ecx, [ecx+00] ; 3 bytes
  212. regOpnd = IR::RegOpnd::New(nullptr, RegECX, TyInt32, instr->m_func);
  213. indirOpnd = IR::IndirOpnd::New(regOpnd, (int32)0, TyInt32, instr->m_func);
  214. nopInstr = IR::Instr::New(Js::OpCode::LEA, regOpnd, indirOpnd, instr->m_func);
  215. break;
  216. case 4:
  217. // lea esp, [esp+00] ; 4 bytes
  218. regOpnd = IR::RegOpnd::New(nullptr, RegESP, TyInt32, instr->m_func);
  219. indirOpnd = IR::IndirOpnd::New(regOpnd, (int32)0, TyInt32, instr->m_func);
  220. nopInstr = IR::Instr::New(Js::OpCode::LEA, regOpnd, indirOpnd, instr->m_func);
  221. break;
  222. default:
  223. Assert(false);
  224. break;
  225. }
  226. instr->InsertBefore(nopInstr);
  227. }
  228. #endif
  229. #elif defined(_M_ARM)
  230. // Can't insert 3 bytes, must choose between 2 and 4.
  231. IR::Instr *nopInstr = nullptr;
  232. switch (nopSize)
  233. {
  234. case 1:
  235. case 2:
  236. nopInstr = IR::Instr::New(Js::OpCode::NOP, instr->m_func);
  237. break;
  238. case 3:
  239. case 4:
  240. nopInstr = IR::Instr::New(Js::OpCode::NOP_W, instr->m_func);
  241. break;
  242. default:
  243. Assert(false);
  244. break;
  245. }
  246. instr->InsertBefore(nopInstr);
  247. #else
  248. AssertMsg(false, "Unimplemented");
  249. #endif
  250. }
  251. bool
  252. Security::DontEncode(IR::Opnd *opnd)
  253. {
  254. switch (opnd->GetKind())
  255. {
  256. case IR::OpndKindIntConst:
  257. {
  258. IR::IntConstOpnd *intConstOpnd = opnd->AsIntConstOpnd();
  259. return intConstOpnd->m_dontEncode;
  260. }
  261. case IR::OpndKindAddr:
  262. {
  263. IR::AddrOpnd *addrOpnd = opnd->AsAddrOpnd();
  264. return (addrOpnd->m_dontEncode ||
  265. !addrOpnd->IsVar() ||
  266. addrOpnd->m_address == nullptr ||
  267. !Js::TaggedNumber::Is(addrOpnd->m_address));
  268. }
  269. case IR::OpndKindIndir:
  270. {
  271. IR::IndirOpnd *indirOpnd = opnd->AsIndirOpnd();
  272. return indirOpnd->m_dontEncode || indirOpnd->GetOffset() == 0;
  273. }
  274. case IR::OpndKindInt64Const:
  275. return false;
  276. default:
  277. return true;
  278. }
  279. }
  280. uint
  281. Security::CalculateConstSize(IR::Opnd *opnd)
  282. {
  283. if (DontEncode(opnd))
  284. {
  285. return 0;
  286. }
  287. switch (opnd->GetKind())
  288. {
  289. #if TARGET_64
  290. case IR::OpndKindInt64Const:
  291. {
  292. IR::Int64ConstOpnd *intConstOpnd = opnd->AsInt64ConstOpnd();
  293. return GetByteCount(intConstOpnd->GetValue());
  294. }
  295. #endif
  296. case IR::OpndKindIntConst:
  297. {
  298. IR::IntConstOpnd *intConstOpnd = opnd->AsIntConstOpnd();
  299. return GetByteCount(intConstOpnd->GetValue());
  300. }
  301. case IR::OpndKindAddr:
  302. {
  303. IR::AddrOpnd *addrOpnd = opnd->AsAddrOpnd();
  304. return Js::TaggedInt::Is(addrOpnd->m_address) ? GetByteCount(Js::TaggedInt::ToInt32(addrOpnd->m_address)) : GetByteCount((intptr_t)addrOpnd->m_address);
  305. }
  306. case IR::OpndKindIndir:
  307. {
  308. IR::IndirOpnd * indirOpnd = opnd->AsIndirOpnd();
  309. return GetByteCount(indirOpnd->GetOffset());
  310. }
  311. default:
  312. Assume(UNREACHED);
  313. }
  314. return 0;
  315. }
  316. bool
  317. Security::EncodeOpnd(IR::Instr * instr, IR::Opnd *opnd)
  318. {
  319. IR::RegOpnd *newOpnd;
  320. bool isSrc2 = false;
  321. const auto unlinkSrc = [&]() {
  322. if (opnd != instr->GetSrc1())
  323. {
  324. Assert(opnd == instr->GetSrc2());
  325. isSrc2 = true;
  326. instr->UnlinkSrc2();
  327. }
  328. else
  329. {
  330. instr->UnlinkSrc1();
  331. }
  332. };
  333. switch (opnd->GetKind())
  334. {
  335. case IR::OpndKindIntConst:
  336. {
  337. IR::IntConstOpnd *intConstOpnd = opnd->AsIntConstOpnd();
  338. unlinkSrc();
  339. intConstOpnd->SetEncodedValue(EncodeValue(instr, intConstOpnd, intConstOpnd->GetValue(), &newOpnd));
  340. }
  341. break;
  342. case IR::OpndKindAddr:
  343. {
  344. IR::AddrOpnd *addrOpnd = opnd->AsAddrOpnd();
  345. unlinkSrc();
  346. addrOpnd->SetEncodedValue((Js::Var)this->EncodeValue(instr, addrOpnd, (IntConstType)addrOpnd->m_address, &newOpnd), addrOpnd->GetAddrOpndKind());
  347. }
  348. break;
  349. case IR::OpndKindIndir:
  350. {
  351. IR::IndirOpnd *indirOpnd = opnd->AsIndirOpnd();
  352. // Using 32 bit cookie causes major perf loss on the subsequent indirs, so only support this path for 16 bit offset
  353. // It's relatively rare for base to be null or to have index + offset, so fall back to the more generic xor method for these
  354. if (indirOpnd->GetBaseOpnd() && indirOpnd->GetIndexOpnd() == nullptr && Math::FitsInWord(indirOpnd->GetOffset()))
  355. {
  356. if (!this->baseOpnd || !this->baseOpnd->IsEqual(indirOpnd->GetBaseOpnd()))
  357. {
  358. if (this->cookieOpnd != nullptr)
  359. {
  360. this->cookieOpnd->Free(this->func);
  361. }
  362. this->cookieOpnd = BuildCookieOpnd(TyInt16, instr->m_func);
  363. this->basePlusCookieOpnd = IR::RegOpnd::New(TyMachReg, instr->m_func);
  364. this->baseOpnd = indirOpnd->GetBaseOpnd();
  365. IR::IndirOpnd * indir = IR::IndirOpnd::New(this->baseOpnd, this->cookieOpnd->AsInt32(), TyMachReg, instr->m_func);
  366. Lowerer::InsertLea(this->basePlusCookieOpnd, indir, instr);
  367. }
  368. int32 diff = indirOpnd->GetOffset() - this->cookieOpnd->AsInt32();
  369. indirOpnd->SetOffset((int32)diff);
  370. indirOpnd->SetBaseOpnd(this->basePlusCookieOpnd);
  371. return true;
  372. }
  373. IR::IntConstOpnd *indexOpnd = IR::IntConstOpnd::New(indirOpnd->GetOffset(), TyMachReg, instr->m_func);
  374. indexOpnd->SetValue(EncodeValue(instr, indexOpnd, indexOpnd->GetValue(), &newOpnd));
  375. indirOpnd->SetOffset(0);
  376. if (indirOpnd->GetIndexOpnd() != nullptr)
  377. {
  378. // update the base rather than the index, because index might have scale
  379. if (indirOpnd->GetBaseOpnd() != nullptr)
  380. {
  381. IR::RegOpnd * newBaseOpnd = IR::RegOpnd::New(TyMachReg, instr->m_func);
  382. Lowerer::InsertAdd(false, newBaseOpnd, newOpnd, indirOpnd->GetBaseOpnd(), instr);
  383. indirOpnd->ReplaceBaseOpnd(newBaseOpnd);
  384. }
  385. else
  386. {
  387. indirOpnd->SetBaseOpnd(newOpnd);
  388. }
  389. }
  390. else
  391. {
  392. indirOpnd->SetIndexOpnd(newOpnd);
  393. }
  394. }
  395. return true;
  396. default:
  397. return false;
  398. }
  399. IR::Opnd *dst = instr->GetDst();
  400. if (dst)
  401. {
  402. #if TARGET_64
  403. // Ensure the left and right operand has the same type (that might not be true for constants on x64)
  404. newOpnd = (IR::RegOpnd *)newOpnd->UseWithNewType(dst->GetType(), instr->m_func);
  405. #endif
  406. if (dst->IsRegOpnd())
  407. {
  408. IR::RegOpnd *dstRegOpnd = dst->AsRegOpnd();
  409. StackSym *dstSym = dstRegOpnd->m_sym;
  410. if (dstSym)
  411. {
  412. dstSym->m_isConst = false;
  413. dstSym->m_isIntConst = false;
  414. dstSym->m_isInt64Const = false;
  415. dstSym->m_isTaggableIntConst = false;
  416. dstSym->m_isFltConst = false;
  417. }
  418. }
  419. }
  420. LowererMD::ImmedSrcToReg(instr, newOpnd, isSrc2 ? 2 : 1);
  421. return true;
  422. }
  423. IR::IntConstOpnd *
  424. Security::BuildCookieOpnd(IRType type, Func * func)
  425. {
  426. IntConstType cookie = 0;
  427. switch (type)
  428. {
  429. case TyInt8:
  430. cookie = (int8)Math::Rand();
  431. break;
  432. case TyUint8:
  433. cookie = (uint8)Math::Rand();
  434. break;
  435. case TyInt16:
  436. cookie = (int16)Math::Rand();
  437. break;
  438. case TyUint16:
  439. cookie = (uint16)Math::Rand();
  440. break;
  441. #if TARGET_32
  442. case TyVar:
  443. #endif
  444. case TyInt32:
  445. cookie = (int32)Math::Rand();
  446. break;
  447. case TyUint32:
  448. cookie = (uint32)Math::Rand();
  449. break;
  450. #if TARGET_64
  451. case TyVar:
  452. case TyInt64:
  453. case TyUint64:
  454. cookie = Math::Rand();
  455. break;
  456. #endif
  457. default:
  458. Assume(UNREACHED);
  459. }
  460. IR::IntConstOpnd * cookieOpnd = IR::IntConstOpnd::New(cookie, type, func);
  461. #if DBG_DUMP
  462. cookieOpnd->SetName(_u("cookie"));
  463. #endif
  464. return cookieOpnd;
  465. }
  466. IntConstType
  467. Security::EncodeValue(IR::Instr * instr, IR::Opnd *opnd, IntConstType constValue, _Out_ IR::RegOpnd **pNewOpnd)
  468. {
  469. if (opnd->GetType() == TyInt32 || opnd->GetType() == TyInt16 || opnd->GetType() == TyInt8
  470. #if TARGET_32
  471. || opnd->GetType() == TyVar
  472. #endif
  473. )
  474. {
  475. IR::RegOpnd *regOpnd = IR::RegOpnd::New(StackSym::New(opnd->GetType(), instr->m_func), opnd->GetType(), instr->m_func);
  476. IR::Instr * instrNew = LowererMD::CreateAssign(regOpnd, opnd, instr);
  477. IR::IntConstOpnd * cookieOpnd = BuildCookieOpnd(opnd->GetType(), instr->m_func);
  478. instrNew = IR::Instr::New(LowererMD::MDXorOpcode, regOpnd, regOpnd, cookieOpnd, instr->m_func);
  479. instr->InsertBefore(instrNew);
  480. LowererMD::Legalize(instrNew);
  481. StackSym * stackSym = regOpnd->m_sym;
  482. Assert(!stackSym->m_isSingleDef);
  483. Assert(stackSym->m_instrDef == nullptr);
  484. stackSym->m_isEncodedConstant = true;
  485. stackSym->constantValue = (int32)constValue;
  486. *pNewOpnd = regOpnd;
  487. int32 value = (int32)constValue;
  488. value = value ^ cookieOpnd->AsInt32();
  489. return value;
  490. }
  491. else if (opnd->GetType() == TyUint32 || opnd->GetType() == TyUint16 || opnd->GetType() == TyUint8)
  492. {
  493. IR::RegOpnd *regOpnd = IR::RegOpnd::New(StackSym::New(opnd->GetType(), instr->m_func), opnd->GetType(), instr->m_func);
  494. IR::Instr * instrNew = LowererMD::CreateAssign(regOpnd, opnd, instr);
  495. IR::IntConstOpnd * cookieOpnd = BuildCookieOpnd(opnd->GetType(), instr->m_func);
  496. instrNew = IR::Instr::New(LowererMD::MDXorOpcode, regOpnd, regOpnd, cookieOpnd, instr->m_func);
  497. instr->InsertBefore(instrNew);
  498. LowererMD::Legalize(instrNew);
  499. StackSym * stackSym = regOpnd->m_sym;
  500. Assert(!stackSym->m_isSingleDef);
  501. Assert(stackSym->m_instrDef == nullptr);
  502. stackSym->m_isEncodedConstant = true;
  503. stackSym->constantValue = (uint32)constValue;
  504. *pNewOpnd = regOpnd;
  505. uint32 value = (uint32)constValue;
  506. value = value ^ cookieOpnd->AsUint32();
  507. return (IntConstType)value;
  508. }
  509. else
  510. {
  511. #if TARGET_64
  512. return this->EncodeAddress(instr, opnd, constValue, pNewOpnd);
  513. #else
  514. Assert(false);
  515. // (Prefast warning on failure to assign *pNewOpnd.)
  516. *pNewOpnd = nullptr;
  517. return 0;
  518. #endif
  519. }
  520. }
  521. #if TARGET_64
  522. size_t
  523. Security::EncodeAddress(IR::Instr * instr, IR::Opnd *opnd, size_t value, _Out_ IR::RegOpnd **pNewOpnd)
  524. {
  525. IR::Instr *instrNew = nullptr;
  526. IR::RegOpnd *regOpnd = IR::RegOpnd::New(TyMachReg, instr->m_func);
  527. instrNew = LowererMD::CreateAssign(regOpnd, opnd, instr);
  528. IR::IntConstOpnd *cookieOpnd = BuildCookieOpnd(TyMachReg, instr->m_func);
  529. instrNew = IR::Instr::New(LowererMD::MDXorOpcode, regOpnd, regOpnd, cookieOpnd, instr->m_func);
  530. instr->InsertBefore(instrNew);
  531. LowererMD::Legalize(instrNew);
  532. StackSym * stackSym = regOpnd->m_sym;
  533. Assert(!stackSym->m_isSingleDef);
  534. Assert(stackSym->m_instrDef == nullptr);
  535. stackSym->m_isEncodedConstant = true;
  536. stackSym->constantValue = value;
  537. *pNewOpnd = regOpnd;
  538. return value ^ cookieOpnd->GetValue();
  539. }
  540. #endif