rollback_index2_tests.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Tests for rollback_index functions
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "2crc8.h"
  12. #include "rollback_index.h"
  13. #include "test_common.h"
  14. #include "tlcl.h"
  15. #include "utility.h"
  16. #include "vboot_common.h"
  17. /*
  18. * Buffer to hold accumulated list of calls to mocked Tlcl functions.
  19. * Each function appends itself to the buffer and updates mock_cnext.
  20. *
  21. * Size of mock_calls[] should be big enough to handle all expected
  22. * call sequences; 16KB should be plenty since none of the sequences
  23. * below is more than a few hundred bytes. We could be more clever
  24. * and use snprintf() with length checking below, at the expense of
  25. * making all the mock implementations bigger. If this were code used
  26. * outside of unit tests we'd want to do that, but here if we did
  27. * overrun the buffer the worst that's likely to happen is we'll crash
  28. * the test, and crash = failure anyway.
  29. */
  30. static char mock_calls[16384];
  31. static char *mock_cnext = mock_calls;
  32. /*
  33. * Variables to support mocked error values from Tlcl functions. Each
  34. * call, mock_count is incremented. If mock_count==fail_at_count, return
  35. * fail_with_error instead of the normal return value.
  36. */
  37. static int mock_count = 0;
  38. static int fail_at_count = 0;
  39. static uint32_t fail_with_error = TPM_SUCCESS;
  40. /* Similar, to determine when to inject noise during reads & writes */
  41. #define MAX_NOISE_COUNT 64 /* no noise after this many */
  42. static int noise_count = 0; /* read/write attempt (zero-based) */
  43. static int noise_on[MAX_NOISE_COUNT]; /* calls to inject noise on */
  44. /* Params / backing store for mocked Tlcl functions. */
  45. static TPM_PERMANENT_FLAGS mock_pflags;
  46. static RollbackSpaceFirmware mock_rsf;
  47. static RollbackSpaceKernel mock_rsk;
  48. static union {
  49. struct RollbackSpaceFwmp fwmp;
  50. uint8_t buf[FWMP_NV_MAX_SIZE];
  51. } mock_fwmp;
  52. static uint32_t mock_permissions;
  53. /* Recalculate CRC of FWMP data */
  54. static void RecalcFwmpCrc(void)
  55. {
  56. mock_fwmp.fwmp.crc = vb2_crc8(mock_fwmp.buf + 2,
  57. mock_fwmp.fwmp.struct_size - 2);
  58. }
  59. /* Reset the variables for the Tlcl mock functions. */
  60. static void ResetMocks(int fail_on_call, uint32_t fail_with_err)
  61. {
  62. *mock_calls = 0;
  63. mock_cnext = mock_calls;
  64. mock_count = 0;
  65. fail_at_count = fail_on_call;
  66. fail_with_error = fail_with_err;
  67. noise_count = 0;
  68. memset(&noise_on, 0, sizeof(noise_on));
  69. memset(&mock_pflags, 0, sizeof(mock_pflags));
  70. memset(&mock_rsf, 0, sizeof(mock_rsf));
  71. memset(&mock_rsk, 0, sizeof(mock_rsk));
  72. mock_permissions = 0;
  73. memset(mock_fwmp.buf, 0, sizeof(mock_fwmp.buf));
  74. mock_fwmp.fwmp.struct_size = sizeof(mock_fwmp.fwmp);
  75. mock_fwmp.fwmp.struct_version = ROLLBACK_SPACE_FWMP_VERSION;
  76. mock_fwmp.fwmp.flags = 0x1234;
  77. /* Put some data in the hash */
  78. mock_fwmp.fwmp.dev_key_hash[0] = 0xaa;
  79. mock_fwmp.fwmp.dev_key_hash[FWMP_HASH_SIZE - 1] = 0xbb;
  80. RecalcFwmpCrc();
  81. }
  82. /****************************************************************************/
  83. /* Function to garble data on its way to or from the TPM */
  84. static void MaybeInjectNoise(void *data, uint32_t length)
  85. {
  86. if (noise_count < MAX_NOISE_COUNT && noise_on[noise_count]) {
  87. uint8_t *val = data;
  88. val[length - 1]++;
  89. }
  90. noise_count++;
  91. }
  92. /****************************************************************************/
  93. /* Mocks for tlcl functions which log the calls made to mock_calls[]. */
  94. uint32_t TlclLibInit(void)
  95. {
  96. mock_cnext += sprintf(mock_cnext, "TlclLibInit()\n");
  97. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  98. }
  99. uint32_t TlclStartup(void)
  100. {
  101. mock_cnext += sprintf(mock_cnext, "TlclStartup()\n");
  102. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  103. }
  104. uint32_t TlclResume(void)
  105. {
  106. mock_cnext += sprintf(mock_cnext, "TlclResume()\n");
  107. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  108. }
  109. uint32_t TlclForceClear(void)
  110. {
  111. mock_cnext += sprintf(mock_cnext, "TlclForceClear()\n");
  112. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  113. }
  114. uint32_t TlclSetEnable(void)
  115. {
  116. mock_cnext += sprintf(mock_cnext, "TlclSetEnable()\n");
  117. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  118. }
  119. uint32_t TlclSetDeactivated(uint8_t flag)
  120. {
  121. mock_cnext += sprintf(mock_cnext, "TlclSetDeactivated(%d)\n", flag);
  122. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  123. }
  124. uint32_t TlclRead(uint32_t index, void* data, uint32_t length)
  125. {
  126. mock_cnext += sprintf(mock_cnext, "TlclRead(0x%x, %d)\n",
  127. index, length);
  128. if (FIRMWARE_NV_INDEX == index) {
  129. TEST_EQ(length, sizeof(mock_rsf), "TlclRead rsf size");
  130. memcpy(data, &mock_rsf, length);
  131. MaybeInjectNoise(data, length);
  132. } else if (KERNEL_NV_INDEX == index) {
  133. TEST_EQ(length, sizeof(mock_rsk), "TlclRead rsk size");
  134. memcpy(data, &mock_rsk, length);
  135. MaybeInjectNoise(data, length);
  136. } else if (FWMP_NV_INDEX == index) {
  137. memset(data, 0, length);
  138. if (length > sizeof(mock_fwmp))
  139. length = sizeof(mock_fwmp);
  140. memcpy(data, &mock_fwmp, length);
  141. MaybeInjectNoise(data, length);
  142. } else {
  143. memset(data, 0, length);
  144. }
  145. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  146. }
  147. uint32_t TlclWrite(uint32_t index, const void *data, uint32_t length)
  148. {
  149. mock_cnext += sprintf(mock_cnext, "TlclWrite(0x%x, %d)\n",
  150. index, length);
  151. if (FIRMWARE_NV_INDEX == index) {
  152. TEST_EQ(length, sizeof(mock_rsf), "TlclWrite rsf size");
  153. memcpy(&mock_rsf, data, length);
  154. MaybeInjectNoise(&mock_rsf, length);
  155. } else if (KERNEL_NV_INDEX == index) {
  156. TEST_EQ(length, sizeof(mock_rsk), "TlclWrite rsk size");
  157. memcpy(&mock_rsk, data, length);
  158. MaybeInjectNoise(&mock_rsk, length);
  159. }
  160. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  161. }
  162. uint32_t TlclDefineSpace(uint32_t index, uint32_t perm, uint32_t size)
  163. {
  164. mock_cnext += sprintf(mock_cnext, "TlclDefineSpace(0x%x, 0x%x, %d)\n",
  165. index, perm, size);
  166. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  167. }
  168. uint32_t TlclSelfTestFull(void)
  169. {
  170. mock_cnext += sprintf(mock_cnext, "TlclSelfTestFull()\n");
  171. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  172. }
  173. uint32_t TlclContinueSelfTest(void)
  174. {
  175. mock_cnext += sprintf(mock_cnext, "TlclContinueSelfTest()\n");
  176. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  177. }
  178. uint32_t TlclGetPermanentFlags(TPM_PERMANENT_FLAGS *pflags)
  179. {
  180. mock_cnext += sprintf(mock_cnext, "TlclGetPermanentFlags()\n");
  181. memcpy(pflags, &mock_pflags, sizeof(mock_pflags));
  182. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  183. }
  184. /* TlclGetFlags() doesn't need mocking; it calls TlclGetPermanentFlags() */
  185. uint32_t TlclAssertPhysicalPresence(void)
  186. {
  187. mock_cnext += sprintf(mock_cnext, "TlclAssertPhysicalPresence()\n");
  188. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  189. }
  190. uint32_t TlclFinalizePhysicalPresence(void)
  191. {
  192. mock_cnext += sprintf(mock_cnext, "TlclFinalizePhysicalPresence()\n");
  193. mock_pflags.physicalPresenceLifetimeLock = 1;
  194. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  195. }
  196. uint32_t TlclPhysicalPresenceCMDEnable(void)
  197. {
  198. mock_cnext += sprintf(mock_cnext, "TlclPhysicalPresenceCMDEnable()\n");
  199. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  200. }
  201. uint32_t TlclSetNvLocked(void)
  202. {
  203. mock_cnext += sprintf(mock_cnext, "TlclSetNvLocked()\n");
  204. mock_pflags.nvLocked = 1;
  205. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  206. }
  207. uint32_t TlclSetGlobalLock(void)
  208. {
  209. mock_cnext += sprintf(mock_cnext, "TlclSetGlobalLock()\n");
  210. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  211. }
  212. uint32_t TlclLockPhysicalPresence(void)
  213. {
  214. mock_cnext += sprintf(mock_cnext, "TlclLockPhysicalPresence()\n");
  215. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  216. }
  217. uint32_t TlclGetPermissions(uint32_t index, uint32_t* permissions)
  218. {
  219. mock_cnext += sprintf(mock_cnext, "TlclGetPermissions(0x%x)\n", index);
  220. *permissions = mock_permissions;
  221. return (++mock_count == fail_at_count) ? fail_with_error : TPM_SUCCESS;
  222. }
  223. /****************************************************************************/
  224. /* Tests for CRC errors */
  225. extern uint32_t ReadSpaceFirmware(RollbackSpaceFirmware *rsf);
  226. extern uint32_t WriteSpaceFirmware(RollbackSpaceFirmware *rsf);
  227. static void CrcTestFirmware(void)
  228. {
  229. RollbackSpaceFirmware rsf;
  230. /* Noise on reading, shouldn't matter here because version == 0 */
  231. ResetMocks(0, 0);
  232. noise_on[0] = 1;
  233. TEST_EQ(ReadSpaceFirmware(&rsf), 0, "ReadSpaceFirmware(), v0");
  234. TEST_STR_EQ(mock_calls,
  235. "TlclRead(0x1007, 10)\n",
  236. "tlcl calls");
  237. /*
  238. * But if the version >= 2, it will try three times and fail because
  239. * the CRC is no good.
  240. */
  241. ResetMocks(0, 0);
  242. mock_rsf.struct_version = 2;
  243. TEST_EQ(ReadSpaceFirmware(&rsf), TPM_E_CORRUPTED_STATE,
  244. "ReadSpaceFirmware(), v2, bad CRC");
  245. TEST_STR_EQ(mock_calls,
  246. "TlclRead(0x1007, 10)\n"
  247. "TlclRead(0x1007, 10)\n"
  248. "TlclRead(0x1007, 10)\n",
  249. "tlcl calls");
  250. /* If the CRC is good and some noise happens, it should recover. */
  251. ResetMocks(0, 0);
  252. mock_rsf.struct_version = 2;
  253. mock_rsf.crc8 = vb2_crc8(&mock_rsf,
  254. offsetof(RollbackSpaceFirmware, crc8));
  255. noise_on[0] = 1;
  256. TEST_EQ(ReadSpaceFirmware(&rsf), 0,
  257. "ReadSpaceFirmware(), v2, good CRC");
  258. TEST_STR_EQ(mock_calls,
  259. "TlclRead(0x1007, 10)\n"
  260. "TlclRead(0x1007, 10)\n",
  261. "tlcl calls");
  262. /* A write with version < 2 should convert to v2 and create the CRC */
  263. ResetMocks(0, 0);
  264. memset(&rsf, 0, sizeof(rsf));
  265. TEST_EQ(WriteSpaceFirmware(&rsf), 0, "WriteSpaceFirmware(), v0");
  266. TEST_EQ(mock_rsf.struct_version, 2, "WriteSpaceFirmware(), check v2");
  267. TEST_STR_EQ(mock_calls,
  268. "TlclWrite(0x1007, 10)\n"
  269. "TlclRead(0x1007, 10)\n",
  270. "tlcl calls");
  271. /* Same as above, but with some noise during the readback */
  272. ResetMocks(0, 0);
  273. memset(&rsf, 0, sizeof(rsf));
  274. noise_on[1] = 1;
  275. noise_on[2] = 1;
  276. TEST_EQ(WriteSpaceFirmware(&rsf), 0,
  277. "WriteSpaceFirmware(), read noise");
  278. TEST_STR_EQ(mock_calls,
  279. "TlclWrite(0x1007, 10)\n"
  280. "TlclRead(0x1007, 10)\n"
  281. "TlclRead(0x1007, 10)\n"
  282. "TlclRead(0x1007, 10)\n",
  283. "tlcl calls");
  284. /* With noise during the write, we'll try the write again */
  285. ResetMocks(0, 0);
  286. memset(&rsf, 0, sizeof(rsf));
  287. noise_on[0] = 1;
  288. TEST_EQ(WriteSpaceFirmware(&rsf), 0,
  289. "WriteSpaceFirmware(), write noise");
  290. TEST_EQ(mock_rsf.struct_version, 2, "WriteSpaceFirmware(), check v2");
  291. TEST_STR_EQ(mock_calls,
  292. "TlclWrite(0x1007, 10)\n"
  293. "TlclRead(0x1007, 10)\n"
  294. "TlclRead(0x1007, 10)\n"
  295. "TlclRead(0x1007, 10)\n"
  296. "TlclWrite(0x1007, 10)\n"
  297. "TlclRead(0x1007, 10)\n",
  298. "tlcl calls");
  299. /* Only if it just keeps on failing forever do we eventually give up */
  300. ResetMocks(0, 0);
  301. memset(&rsf, 0, sizeof(rsf));
  302. memset(noise_on, 1, sizeof(noise_on));
  303. TEST_EQ(WriteSpaceFirmware(&rsf), TPM_E_CORRUPTED_STATE,
  304. "WriteSpaceFirmware(), always noise");
  305. TEST_STR_EQ(mock_calls,
  306. "TlclWrite(0x1007, 10)\n"
  307. "TlclRead(0x1007, 10)\n"
  308. "TlclRead(0x1007, 10)\n"
  309. "TlclRead(0x1007, 10)\n"
  310. "TlclWrite(0x1007, 10)\n"
  311. "TlclRead(0x1007, 10)\n"
  312. "TlclRead(0x1007, 10)\n"
  313. "TlclRead(0x1007, 10)\n"
  314. "TlclWrite(0x1007, 10)\n"
  315. "TlclRead(0x1007, 10)\n"
  316. "TlclRead(0x1007, 10)\n"
  317. "TlclRead(0x1007, 10)\n",
  318. "tlcl calls");
  319. }
  320. extern uint32_t ReadSpaceKernel(RollbackSpaceKernel *rsk);
  321. extern uint32_t WriteSpaceKernel(RollbackSpaceKernel *rsk);
  322. static void CrcTestKernel(void)
  323. {
  324. RollbackSpaceKernel rsk;
  325. /* Noise on reading shouldn't matter here because version == 0 */
  326. ResetMocks(0, 0);
  327. noise_on[0] = 1;
  328. TEST_EQ(ReadSpaceKernel(&rsk), 0, "ReadSpaceKernel(), v0");
  329. TEST_STR_EQ(mock_calls,
  330. "TlclRead(0x1008, 13)\n",
  331. "tlcl calls");
  332. /*
  333. * But if the version >= 2, it will try three times and fail because
  334. * the CRC is no good.
  335. */
  336. ResetMocks(0, 0);
  337. mock_rsk.struct_version = 2;
  338. TEST_EQ(ReadSpaceKernel(&rsk), TPM_E_CORRUPTED_STATE,
  339. "ReadSpaceKernel(), v2, bad CRC");
  340. TEST_STR_EQ(mock_calls,
  341. "TlclRead(0x1008, 13)\n"
  342. "TlclRead(0x1008, 13)\n"
  343. "TlclRead(0x1008, 13)\n",
  344. "tlcl calls");
  345. /* If the CRC is good and some noise happens, it should recover. */
  346. ResetMocks(0, 0);
  347. mock_rsk.struct_version = 2;
  348. mock_rsk.crc8 = vb2_crc8(&mock_rsk,
  349. offsetof(RollbackSpaceKernel, crc8));
  350. noise_on[0] = 1;
  351. TEST_EQ(ReadSpaceKernel(&rsk), 0, "ReadSpaceKernel(), v2, good CRC");
  352. TEST_STR_EQ(mock_calls,
  353. "TlclRead(0x1008, 13)\n"
  354. "TlclRead(0x1008, 13)\n",
  355. "tlcl calls");
  356. /* A write with version < 2 should convert to v2 and create the CRC */
  357. ResetMocks(0, 0);
  358. memset(&rsk, 0, sizeof(rsk));
  359. TEST_EQ(WriteSpaceKernel(&rsk), 0, "WriteSpaceKernel(), v0");
  360. TEST_EQ(mock_rsk.struct_version, 2, "WriteSpaceKernel(), check v2");
  361. TEST_STR_EQ(mock_calls,
  362. "TlclWrite(0x1008, 13)\n"
  363. "TlclRead(0x1008, 13)\n",
  364. "tlcl calls");
  365. /* Same as above, but with some noise during the readback */
  366. ResetMocks(0, 0);
  367. memset(&rsk, 0, sizeof(rsk));
  368. noise_on[1] = 1;
  369. noise_on[2] = 1;
  370. TEST_EQ(WriteSpaceKernel(&rsk), 0, "WriteSpaceKernel(), read noise");
  371. TEST_STR_EQ(mock_calls,
  372. "TlclWrite(0x1008, 13)\n"
  373. "TlclRead(0x1008, 13)\n"
  374. "TlclRead(0x1008, 13)\n"
  375. "TlclRead(0x1008, 13)\n",
  376. "tlcl calls");
  377. /* With noise during the write, we'll try the write again */
  378. ResetMocks(0, 0);
  379. memset(&rsk, 0, sizeof(rsk));
  380. noise_on[0] = 1;
  381. TEST_EQ(WriteSpaceKernel(&rsk), 0, "WriteSpaceKernel(), write noise");
  382. TEST_EQ(mock_rsk.struct_version, 2, "WriteSpaceKernel(), check v2");
  383. TEST_STR_EQ(mock_calls,
  384. "TlclWrite(0x1008, 13)\n"
  385. "TlclRead(0x1008, 13)\n"
  386. "TlclRead(0x1008, 13)\n"
  387. "TlclRead(0x1008, 13)\n"
  388. "TlclWrite(0x1008, 13)\n"
  389. "TlclRead(0x1008, 13)\n",
  390. "tlcl calls");
  391. /* Only if it just keeps on failing forever do we eventually give up */
  392. ResetMocks(0, 0);
  393. memset(&rsk, 0, sizeof(rsk));
  394. memset(noise_on, 1, sizeof(noise_on));
  395. TEST_EQ(WriteSpaceKernel(&rsk), TPM_E_CORRUPTED_STATE,
  396. "WriteSpaceKernel(), always noise");
  397. TEST_STR_EQ(mock_calls,
  398. "TlclWrite(0x1008, 13)\n"
  399. "TlclRead(0x1008, 13)\n"
  400. "TlclRead(0x1008, 13)\n"
  401. "TlclRead(0x1008, 13)\n"
  402. "TlclWrite(0x1008, 13)\n"
  403. "TlclRead(0x1008, 13)\n"
  404. "TlclRead(0x1008, 13)\n"
  405. "TlclRead(0x1008, 13)\n"
  406. "TlclWrite(0x1008, 13)\n"
  407. "TlclRead(0x1008, 13)\n"
  408. "TlclRead(0x1008, 13)\n"
  409. "TlclRead(0x1008, 13)\n",
  410. "tlcl calls");
  411. }
  412. /****************************************************************************/
  413. /* Tests for misc helper functions */
  414. static void MiscTest(void)
  415. {
  416. uint8_t buf[8];
  417. ResetMocks(0, 0);
  418. TEST_EQ(TPMClearAndReenable(), 0, "TPMClearAndReenable()");
  419. TEST_STR_EQ(mock_calls,
  420. "TlclForceClear()\n"
  421. "TlclSetEnable()\n"
  422. "TlclSetDeactivated(0)\n",
  423. "tlcl calls");
  424. ResetMocks(0, 0);
  425. TEST_EQ(SafeWrite(0x123, buf, 8), 0, "SafeWrite()");
  426. TEST_STR_EQ(mock_calls,
  427. "TlclWrite(0x123, 8)\n",
  428. "tlcl calls");
  429. ResetMocks(1, TPM_E_BADINDEX);
  430. TEST_EQ(SafeWrite(0x123, buf, 8), TPM_E_BADINDEX, "SafeWrite() bad");
  431. TEST_STR_EQ(mock_calls,
  432. "TlclWrite(0x123, 8)\n",
  433. "tlcl calls");
  434. ResetMocks(1, TPM_E_MAXNVWRITES);
  435. TEST_EQ(SafeWrite(0x123, buf, 8), 0, "SafeWrite() retry max writes");
  436. TEST_STR_EQ(mock_calls,
  437. "TlclWrite(0x123, 8)\n"
  438. "TlclForceClear()\n"
  439. "TlclSetEnable()\n"
  440. "TlclSetDeactivated(0)\n"
  441. "TlclWrite(0x123, 8)\n",
  442. "tlcl calls");
  443. }
  444. /****************************************************************************/
  445. /* Tests for RollbackKernel() calls */
  446. static void RollbackKernelTest(void)
  447. {
  448. uint32_t version = 0;
  449. /* Normal read */
  450. ResetMocks(0, 0);
  451. mock_rsk.uid = ROLLBACK_SPACE_KERNEL_UID;
  452. mock_permissions = TPM_NV_PER_PPWRITE;
  453. mock_rsk.kernel_versions = 0x87654321;
  454. TEST_EQ(RollbackKernelRead(&version), 0, "RollbackKernelRead()");
  455. TEST_STR_EQ(mock_calls,
  456. "TlclRead(0x1008, 13)\n"
  457. "TlclGetPermissions(0x1008)\n",
  458. "tlcl calls");
  459. TEST_EQ(version, 0x87654321, "RollbackKernelRead() version");
  460. /* Read error */
  461. ResetMocks(1, TPM_E_IOERROR);
  462. TEST_EQ(RollbackKernelRead(&version), TPM_E_IOERROR,
  463. "RollbackKernelRead() error");
  464. TEST_STR_EQ(mock_calls,
  465. "TlclRead(0x1008, 13)\n",
  466. "tlcl calls");
  467. /* Wrong permission or UID will return error */
  468. ResetMocks(0, 0);
  469. mock_rsk.uid = ROLLBACK_SPACE_KERNEL_UID + 1;
  470. mock_permissions = TPM_NV_PER_PPWRITE;
  471. TEST_EQ(RollbackKernelRead(&version), TPM_E_CORRUPTED_STATE,
  472. "RollbackKernelRead() bad uid");
  473. ResetMocks(0, 0);
  474. mock_rsk.uid = ROLLBACK_SPACE_KERNEL_UID;
  475. mock_permissions = TPM_NV_PER_PPWRITE + 1;
  476. TEST_EQ(RollbackKernelRead(&version), TPM_E_CORRUPTED_STATE,
  477. "RollbackKernelRead() bad permissions");
  478. /* Test write */
  479. ResetMocks(0, 0);
  480. TEST_EQ(RollbackKernelWrite(0xBEAD4321), 0, "RollbackKernelWrite()");
  481. TEST_EQ(mock_rsk.kernel_versions, 0xBEAD4321,
  482. "RollbackKernelWrite() version");
  483. TEST_STR_EQ(mock_calls,
  484. "TlclRead(0x1008, 13)\n"
  485. "TlclWrite(0x1008, 13)\n"
  486. "TlclRead(0x1008, 13)\n",
  487. "tlcl calls");
  488. ResetMocks(1, TPM_E_IOERROR);
  489. TEST_EQ(RollbackKernelWrite(123), TPM_E_IOERROR,
  490. "RollbackKernelWrite() error");
  491. /* Test lock (recovery off) */
  492. ResetMocks(1, TPM_E_IOERROR);
  493. TEST_EQ(RollbackKernelLock(0), TPM_E_IOERROR,
  494. "RollbackKernelLock() error");
  495. /* Test lock with recovery on; shouldn't lock PP */
  496. ResetMocks(0, 0);
  497. TEST_EQ(RollbackKernelLock(1), 0, "RollbackKernelLock() in recovery");
  498. TEST_STR_EQ(mock_calls, "", "no tlcl calls");
  499. ResetMocks(0, 0);
  500. TEST_EQ(RollbackKernelLock(0), 0, "RollbackKernelLock()");
  501. TEST_STR_EQ(mock_calls,
  502. "TlclLockPhysicalPresence()\n",
  503. "tlcl calls");
  504. }
  505. /****************************************************************************/
  506. /* Tests for RollbackFwmpRead() calls */
  507. static void RollbackFwmpTest(void)
  508. {
  509. struct RollbackSpaceFwmp fwmp;
  510. struct RollbackSpaceFwmp fwmp_zero = {0};
  511. /* Normal read */
  512. ResetMocks(0, 0);
  513. TEST_EQ(RollbackFwmpRead(&fwmp), 0, "RollbackFwmpRead()");
  514. TEST_STR_EQ(mock_calls,
  515. "TlclRead(0x100a, 40)\n",
  516. " tlcl calls");
  517. TEST_EQ(0, memcmp(&fwmp, &mock_fwmp, sizeof(fwmp)), " data");
  518. /* Read error */
  519. ResetMocks(1, TPM_E_IOERROR);
  520. TEST_EQ(RollbackFwmpRead(&fwmp), TPM_E_IOERROR,
  521. "RollbackFwmpRead() error");
  522. TEST_STR_EQ(mock_calls,
  523. "TlclRead(0x100a, 40)\n",
  524. " tlcl calls");
  525. TEST_EQ(0, memcmp(&fwmp, &fwmp_zero, sizeof(fwmp)), " data clear");
  526. /* Not present isn't an error; just returns empty data */
  527. ResetMocks(1, TPM_E_BADINDEX);
  528. TEST_EQ(RollbackFwmpRead(&fwmp), 0, "RollbackFwmpRead() not present");
  529. TEST_STR_EQ(mock_calls,
  530. "TlclRead(0x100a, 40)\n",
  531. " tlcl calls");
  532. TEST_EQ(0, memcmp(&fwmp, &fwmp_zero, sizeof(fwmp)), " data clear");
  533. /* Struct size too small */
  534. ResetMocks(0, 0);
  535. mock_fwmp.fwmp.struct_size--;
  536. TEST_EQ(RollbackFwmpRead(&fwmp), TPM_E_STRUCT_SIZE,
  537. "RollbackFwmpRead() too small");
  538. /* Struct size too large with good CRC */
  539. ResetMocks(0, 0);
  540. mock_fwmp.fwmp.struct_size += 4;
  541. RecalcFwmpCrc();
  542. TEST_EQ(RollbackFwmpRead(&fwmp), 0, "RollbackFwmpRead() bigger");
  543. TEST_STR_EQ(mock_calls,
  544. "TlclRead(0x100a, 40)\n"
  545. "TlclRead(0x100a, 44)\n",
  546. " tlcl calls");
  547. TEST_EQ(0, memcmp(&fwmp, &mock_fwmp, sizeof(fwmp)), " data");
  548. /* Bad CRC causes retry, then eventual failure */
  549. ResetMocks(0, 0);
  550. mock_fwmp.fwmp.crc++;
  551. TEST_EQ(RollbackFwmpRead(&fwmp), TPM_E_CORRUPTED_STATE,
  552. "RollbackFwmpRead() crc");
  553. TEST_STR_EQ(mock_calls,
  554. "TlclRead(0x100a, 40)\n"
  555. "TlclRead(0x100a, 40)\n"
  556. "TlclRead(0x100a, 40)\n",
  557. " tlcl calls");
  558. /* Struct size too large with bad CRC */
  559. ResetMocks(0, 0);
  560. mock_fwmp.fwmp.struct_size += 4;
  561. RecalcFwmpCrc();
  562. mock_fwmp.fwmp.crc++;
  563. TEST_EQ(RollbackFwmpRead(&fwmp), TPM_E_CORRUPTED_STATE,
  564. "RollbackFwmpRead() bigger crc");
  565. TEST_STR_EQ(mock_calls,
  566. "TlclRead(0x100a, 40)\n"
  567. "TlclRead(0x100a, 44)\n"
  568. "TlclRead(0x100a, 40)\n"
  569. "TlclRead(0x100a, 44)\n"
  570. "TlclRead(0x100a, 40)\n"
  571. "TlclRead(0x100a, 44)\n",
  572. " tlcl calls");
  573. TEST_EQ(0, memcmp(&fwmp, &fwmp_zero, sizeof(fwmp)), " data");
  574. /* Minor version difference ok */
  575. ResetMocks(0, 0);
  576. mock_fwmp.fwmp.struct_version++;
  577. RecalcFwmpCrc();
  578. TEST_EQ(RollbackFwmpRead(&fwmp), 0, "RollbackFwmpRead() minor version");
  579. TEST_EQ(0, memcmp(&fwmp, &mock_fwmp, sizeof(fwmp)), " data");
  580. /* Major version difference not ok */
  581. ResetMocks(0, 0);
  582. mock_fwmp.fwmp.struct_version += 0x10;
  583. RecalcFwmpCrc();
  584. TEST_EQ(RollbackFwmpRead(&fwmp), TPM_E_STRUCT_VERSION,
  585. "RollbackFwmpRead() major version");
  586. }
  587. int main(int argc, char* argv[])
  588. {
  589. CrcTestFirmware();
  590. CrcTestKernel();
  591. MiscTest();
  592. RollbackKernelTest();
  593. RollbackFwmpTest();
  594. return gTestSuccess ? 0 : 255;
  595. }