1791451386.v10-1-gba140c8.libluksmeta-fix-gap-allocator-bounds-checking.patch 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. Subject: Libluksmeta: fix gap allocator bounds checking
  2. Origin: upstream, commit v10-1-gba140c8 <https://github.com/latchset/luksmeta/commit/v10-1-gba140c8>
  3. Author: Sergio Correia <scorreia@redhat.com>
  4. Date: Thu Oct 8 06:23:06 2026 -0300
  5. Forwarded: not-needed
  6. The hard-limit passed to overlap() included slots[0].offset, shifting
  7. the boundary past the gap whenever slot 0 was occupied. Pass the bare
  8. gap length instead.
  9. The overlap predicate missed a candidate range fully contained inside
  10. an existing slot. Replace the two partial checks with the canonical
  11. half-open interval intersection (start < e && s->offset < end) and
  12. widen the slot endpoint to uint64_t as defense-in-depth.
  13. Add test-lm-gap covering both regressions, zero-length metadata,
  14. exact-fit and over-capacity boundaries, adjacent allocation, and
  15. space reuse after wipe.
  16. Signed-off-by: Sergio Correia <scorreia@redhat.com>
  17. --- a/Makefile.am
  18. +++ b/Makefile.am
  19. @@ -34,13 +34,14 @@
  20. check_LTLIBRARIES = libtest.la
  21. libtest_la_SOURCES = test.c test.h
  22. -check_PROGRAMS = test-crc32c test-lm-assumptions test-lm-init test-lm-one test-lm-two test-lm-big
  23. +check_PROGRAMS = test-crc32c test-lm-assumptions test-lm-init test-lm-one test-lm-two test-lm-big test-lm-gap
  24. test_crc32c_LDADD = libcrc32c.la
  25. test_lm_assumptions_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
  26. test_lm_init_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
  27. test_lm_one_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
  28. test_lm_two_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
  29. test_lm_big_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
  30. +test_lm_gap_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
  31. EXTRA_DIST = $(man_ADOC_FILES) test-luksmeta
  32. TESTS = $(check_PROGRAMS) test-luksmeta
  33. --- a/libluksmeta.c
  34. +++ b/libluksmeta.c
  35. @@ -77,12 +77,13 @@
  36. for (int i = 0; i < LUKS_NSLOTS; i++) {
  37. const lm_slot_t *s = &lm->slots[i];
  38. - uint32_t e = s->offset + s->length;
  39. - if (start <= s->offset && s->offset < end)
  40. - return true;
  41. + if (s->length == 0)
  42. + continue;
  43. +
  44. + uint64_t e = (uint64_t) s->offset + s->length;
  45. - if (start < e && e <= end)
  46. + if (start < e && s->offset < end)
  47. return true;
  48. }
  49. @@ -100,7 +101,7 @@
  50. return 0;
  51. for (uint32_t off = ALIGN(1, true); off < length; off += ALIGN(1, true)) {
  52. - if (!overlap(lm, off, off + size, lm->slots[0].offset + length))
  53. + if (!overlap(lm, off, off + size, length))
  54. return off;
  55. }
  56. --- /dev/null
  57. +++ b/test-lm-gap.c
  58. @@ -0,0 +1,557 @@
  59. +/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  60. +/*
  61. + * Copyright (c) 2026 Red Hat, Inc.
  62. + * Author: Sergio Correia <scorreia@redhat.com>
  63. + *
  64. + * This program is free software: you can redistribute it and/or modify it
  65. + * under the terms of the GNU Lesser General Public License as published by
  66. + * the Free Software Foundation, either version 2.1 of the License, or
  67. + * (at your option) any later version.
  68. + *
  69. + * This program is distributed in the hope that it will be useful,
  70. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  71. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  72. + * GNU Lesser General Public License for more details.
  73. + *
  74. + * You should have received a copy of the GNU Lesser General Public License
  75. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  76. + */
  77. +
  78. +#include "test.h"
  79. +#include <error.h>
  80. +#include <errno.h>
  81. +#include <fcntl.h>
  82. +#include <stdlib.h>
  83. +#include <string.h>
  84. +#include <sys/stat.h>
  85. +
  86. +static const luksmeta_uuid_t UUID0 = {
  87. + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  88. + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11
  89. +};
  90. +
  91. +static const luksmeta_uuid_t UUID1 = {
  92. + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  93. + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
  94. +};
  95. +
  96. +static const luksmeta_uuid_t UUID2 = {
  97. + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  98. + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33
  99. +};
  100. +
  101. +/* Reset the filename template so mkstemp() in test_format() works on
  102. + * each call. The existing tests only call test_format() once, but we
  103. + * call it for every scenario. */
  104. +static void
  105. +reset_filename(void)
  106. +{
  107. + strcpy(filename, "/tmp/luksmetaXXXXXX");
  108. +}
  109. +
  110. +/* Read a region of the image file into a freshly allocated buffer. */
  111. +static uint8_t *
  112. +read_image(size_t off, size_t len)
  113. +{
  114. + uint8_t *buf = NULL;
  115. + int fd;
  116. +
  117. + buf = malloc(len);
  118. + if (!buf)
  119. + error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  120. +
  121. + fd = open(filename, O_RDONLY);
  122. + if (fd < 0)
  123. + error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  124. +
  125. + if (lseek(fd, off, SEEK_SET) == -1) {
  126. + free(buf);
  127. + close(fd);
  128. + error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  129. + }
  130. +
  131. + if (read(fd, buf, len) != (ssize_t) len) {
  132. + free(buf);
  133. + close(fd);
  134. + error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
  135. + }
  136. +
  137. + close(fd);
  138. + return buf;
  139. +}
  140. +
  141. +/*
  142. + * Bug 1 regression: occupied slot 0 shifts the hard limit, allowing a
  143. + * save to write past the gap into the encrypted payload area.
  144. + */
  145. +static void
  146. +test_bug1(void)
  147. +{
  148. + struct crypt_device *cd = NULL;
  149. + luksmeta_uuid_t uuid = {};
  150. + uint32_t offset = 0;
  151. + uint32_t length = 0;
  152. + uint8_t *before = NULL;
  153. + uint8_t *after = NULL;
  154. + uint8_t small[16];
  155. + uint8_t *big = NULL;
  156. + size_t capacity;
  157. + int r;
  158. +
  159. + reset_filename();
  160. + crypt_free(test_format());
  161. + cd = test_init();
  162. + test_hole(cd, &offset, &length);
  163. +
  164. + assert(length % 4096 == 0);
  165. + assert(length >= 12288);
  166. +
  167. + /* Usable data area: gap minus the header page. */
  168. + capacity = length - ALIGN(1, true);
  169. +
  170. + /* Fill small data with a known pattern. */
  171. + memset(small, 'S', sizeof(small));
  172. +
  173. + /* Save a small entry in slot 0. */
  174. + r = luksmeta_save(cd, 0, UUID0, small, sizeof(small));
  175. + assert(r == 0);
  176. +
  177. + /* Seed the payload boundary so we can detect overwrites. */
  178. + {
  179. + int fd = open(filename, O_RDWR);
  180. + uint8_t sentinel[4096];
  181. + assert(fd >= 0);
  182. + memset(sentinel, 'P', sizeof(sentinel));
  183. + assert(lseek(fd, offset + length, SEEK_SET) != -1);
  184. + assert(write(fd, sentinel, sizeof(sentinel)) == sizeof(sentinel));
  185. + close(fd);
  186. + }
  187. +
  188. + /* Snapshot the image before the rejected save. */
  189. + before = read_image(offset, length + 4096);
  190. +
  191. + /* Attempt to save capacity bytes — must fail, slot 0 already occupies
  192. + * space in the gap. */
  193. + big = calloc(1, capacity);
  194. + assert(big != NULL);
  195. + memset(big, 'B', capacity);
  196. +
  197. + r = luksmeta_save(cd, 1, UUID1, big, capacity);
  198. + assert(r == -ENOSPC);
  199. + free(big);
  200. +
  201. + /* Image must be unchanged. */
  202. + after = read_image(offset, length + 4096);
  203. + assert(memcmp(before, after, length + 4096) == 0);
  204. + free(before);
  205. + free(after);
  206. +
  207. + /* Original entry still loads correctly. */
  208. + {
  209. + uint8_t out[sizeof(small)] = {};
  210. + r = luksmeta_load(cd, 0, uuid, out, sizeof(out));
  211. + assert(r == (int) sizeof(small));
  212. + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
  213. + assert(memcmp(out, small, sizeof(small)) == 0);
  214. + }
  215. +
  216. + /* Destination slot is still empty. */
  217. + r = luksmeta_load(cd, 1, uuid, NULL, 0);
  218. + assert(r == -ENODATA);
  219. +
  220. + crypt_free(cd);
  221. + unlink(filename);
  222. +}
  223. +
  224. +/*
  225. + * Control: same sizes as Bug 1 but with the small entry in slot 1
  226. + * instead of slot 0. Verifies the rejection is not slot-0-specific.
  227. + */
  228. +static void
  229. +test_control(void)
  230. +{
  231. + struct crypt_device *cd = NULL;
  232. + luksmeta_uuid_t uuid = {};
  233. + uint32_t offset = 0;
  234. + uint32_t length = 0;
  235. + uint8_t *before = NULL;
  236. + uint8_t *after = NULL;
  237. + uint8_t small[16];
  238. + uint8_t *big = NULL;
  239. + size_t capacity;
  240. + int r;
  241. +
  242. + reset_filename();
  243. + crypt_free(test_format());
  244. + cd = test_init();
  245. + test_hole(cd, &offset, &length);
  246. +
  247. + capacity = length - ALIGN(1, true);
  248. + memset(small, 'S', sizeof(small));
  249. +
  250. + /* Save a small entry in slot 1 (not slot 0). */
  251. + r = luksmeta_save(cd, 1, UUID1, small, sizeof(small));
  252. + assert(r == 1);
  253. +
  254. + /* Seed payload boundary. */
  255. + {
  256. + int fd = open(filename, O_RDWR);
  257. + uint8_t sentinel[4096];
  258. + assert(fd >= 0);
  259. + memset(sentinel, 'P', sizeof(sentinel));
  260. + assert(lseek(fd, offset + length, SEEK_SET) != -1);
  261. + assert(write(fd, sentinel, sizeof(sentinel)) == sizeof(sentinel));
  262. + close(fd);
  263. + }
  264. +
  265. + before = read_image(offset, length + 4096);
  266. +
  267. + big = calloc(1, capacity);
  268. + assert(big != NULL);
  269. + memset(big, 'B', capacity);
  270. +
  271. + r = luksmeta_save(cd, 2, UUID2, big, capacity);
  272. + assert(r == -ENOSPC);
  273. + free(big);
  274. +
  275. + after = read_image(offset, length + 4096);
  276. + assert(memcmp(before, after, length + 4096) == 0);
  277. + free(before);
  278. + free(after);
  279. +
  280. + /* Original entry still loads. */
  281. + {
  282. + uint8_t out[sizeof(small)] = {};
  283. + r = luksmeta_load(cd, 1, uuid, out, sizeof(out));
  284. + assert(r == (int) sizeof(small));
  285. + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
  286. + assert(memcmp(out, small, sizeof(small)) == 0);
  287. + }
  288. +
  289. + r = luksmeta_load(cd, 2, uuid, NULL, 0);
  290. + assert(r == -ENODATA);
  291. +
  292. + crypt_free(cd);
  293. + unlink(filename);
  294. +}
  295. +
  296. +/*
  297. + * Bug 2 regression: a new small entry whose candidate position falls
  298. + * entirely inside an existing entry longer than two pages. The allocator
  299. + * must skip the existing range and place the new entry after it.
  300. + */
  301. +static void
  302. +test_bug2(void)
  303. +{
  304. + struct crypt_device *cd = NULL;
  305. + luksmeta_uuid_t uuid = {};
  306. + uint32_t offset = 0;
  307. + uint32_t length = 0;
  308. + uint8_t big[9000];
  309. + uint8_t small[100];
  310. + int r;
  311. +
  312. + reset_filename();
  313. + crypt_free(test_format());
  314. + cd = test_init();
  315. + test_hole(cd, &offset, &length);
  316. +
  317. + /* Need room for the header page + 9000 bytes (3 aligned pages) +
  318. + * the second entry (1 aligned page) = 5 pages minimum. */
  319. + assert(length >= 24576);
  320. +
  321. + memset(big, 'A', sizeof(big));
  322. + memset(small, 'X', sizeof(small));
  323. +
  324. + /* Slot 1: 9000 bytes, occupies gap-relative [4096, 13096). */
  325. + r = luksmeta_save(cd, 1, UUID1, big, sizeof(big));
  326. + assert(r == 1);
  327. +
  328. + /* Slot 2: 100 bytes. Must skip the existing range and succeed. */
  329. + r = luksmeta_save(cd, 2, UUID2, small, sizeof(small));
  330. + assert(r == 2);
  331. +
  332. + /* Both entries load with their original data. */
  333. + {
  334. + uint8_t out_big[sizeof(big)] = {};
  335. + r = luksmeta_load(cd, 1, uuid, out_big, sizeof(out_big));
  336. + assert(r == (int) sizeof(big));
  337. + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
  338. + assert(memcmp(out_big, big, sizeof(big)) == 0);
  339. + }
  340. +
  341. + {
  342. + uint8_t out_small[sizeof(small)] = {};
  343. + r = luksmeta_load(cd, 2, uuid, out_small, sizeof(out_small));
  344. + assert(r == (int) sizeof(small));
  345. + assert(memcmp(uuid, UUID2, sizeof(UUID2)) == 0);
  346. + assert(memcmp(out_small, small, sizeof(small)) == 0);
  347. + }
  348. +
  349. + crypt_free(cd);
  350. + unlink(filename);
  351. +}
  352. +
  353. +/*
  354. + * Zero-length metadata: a zero-length entry coexists with a normal entry.
  355. + */
  356. +static void
  357. +test_zero_length(void)
  358. +{
  359. + struct crypt_device *cd = NULL;
  360. + luksmeta_uuid_t uuid = {};
  361. + uint8_t dummy = 0;
  362. + uint8_t normal[64];
  363. + int r;
  364. +
  365. + reset_filename();
  366. + crypt_free(test_format());
  367. + cd = test_init();
  368. +
  369. + /* Save a zero-length entry. Use a non-NULL buffer to avoid UB. */
  370. + r = luksmeta_save(cd, 0, UUID0, &dummy, 0);
  371. + assert(r == 0);
  372. +
  373. + /* It loads back with length 0 and the correct UUID. */
  374. + r = luksmeta_load(cd, 0, uuid, NULL, 0);
  375. + assert(r == 0);
  376. + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
  377. +
  378. + /* Save a normal entry in the next slot. */
  379. + memset(normal, 'N', sizeof(normal));
  380. + r = luksmeta_save(cd, 1, UUID1, normal, sizeof(normal));
  381. + assert(r == 1);
  382. +
  383. + /* Both coexist: zero-length still loads. */
  384. + memset(uuid, 0, sizeof(uuid));
  385. + r = luksmeta_load(cd, 0, uuid, NULL, 0);
  386. + assert(r == 0);
  387. + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
  388. +
  389. + /* Normal entry loads with full contents. */
  390. + {
  391. + uint8_t out[sizeof(normal)] = {};
  392. + r = luksmeta_load(cd, 1, uuid, out, sizeof(out));
  393. + assert(r == (int) sizeof(normal));
  394. + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
  395. + assert(memcmp(out, normal, sizeof(normal)) == 0);
  396. + }
  397. +
  398. + crypt_free(cd);
  399. + unlink(filename);
  400. +}
  401. +
  402. +/*
  403. + * Exact-fit boundary: fill the entire usable data area.
  404. + */
  405. +static void
  406. +test_exact_fit(void)
  407. +{
  408. + struct crypt_device *cd = NULL;
  409. + luksmeta_uuid_t uuid = {};
  410. + uint32_t offset = 0;
  411. + uint32_t length = 0;
  412. + size_t capacity;
  413. + uint8_t *data = NULL;
  414. + int r;
  415. +
  416. + reset_filename();
  417. + crypt_free(test_format());
  418. + cd = test_init();
  419. + test_hole(cd, &offset, &length);
  420. +
  421. + assert(length % 4096 == 0);
  422. + capacity = length - ALIGN(1, true);
  423. +
  424. + data = malloc(capacity);
  425. + assert(data != NULL);
  426. + memset(data, 'F', capacity);
  427. +
  428. + /* Save exactly capacity bytes — should succeed. */
  429. + r = luksmeta_save(cd, 0, UUID0, data, capacity);
  430. + assert(r == 0);
  431. +
  432. + /* Verify full readback. */
  433. + {
  434. + uint8_t *out = malloc(capacity);
  435. + assert(out != NULL);
  436. + r = luksmeta_load(cd, 0, uuid, out, capacity);
  437. + assert(r == (int) capacity);
  438. + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
  439. + assert(memcmp(out, data, capacity) == 0);
  440. + free(out);
  441. + }
  442. +
  443. + free(data);
  444. + crypt_free(cd);
  445. + unlink(filename);
  446. +}
  447. +
  448. +/*
  449. + * One byte over capacity: must be rejected without changing the image.
  450. + */
  451. +static void
  452. +test_over_capacity(void)
  453. +{
  454. + struct crypt_device *cd = NULL;
  455. + luksmeta_uuid_t uuid = {};
  456. + uint32_t offset = 0;
  457. + uint32_t length = 0;
  458. + size_t capacity;
  459. + uint8_t *data = NULL;
  460. + uint8_t *before = NULL;
  461. + uint8_t *after = NULL;
  462. + int r;
  463. +
  464. + reset_filename();
  465. + crypt_free(test_format());
  466. + cd = test_init();
  467. + test_hole(cd, &offset, &length);
  468. +
  469. + assert(length % 4096 == 0);
  470. + capacity = length - ALIGN(1, true);
  471. +
  472. + data = calloc(1, capacity + 1);
  473. + assert(data != NULL);
  474. + memset(data, 'O', capacity + 1);
  475. +
  476. + before = read_image(offset, length);
  477. +
  478. + r = luksmeta_save(cd, 0, UUID0, data, capacity + 1);
  479. + assert(r == -ENOSPC);
  480. + free(data);
  481. +
  482. + after = read_image(offset, length);
  483. + assert(memcmp(before, after, length) == 0);
  484. + free(before);
  485. + free(after);
  486. +
  487. + /* Slot remains empty. */
  488. + r = luksmeta_load(cd, 0, uuid, NULL, 0);
  489. + assert(r == -ENODATA);
  490. +
  491. + crypt_free(cd);
  492. + unlink(filename);
  493. +}
  494. +
  495. +/*
  496. + * Adjacent allocation: second entry starts at the first's endpoint.
  497. + */
  498. +static void
  499. +test_adjacent(void)
  500. +{
  501. + struct crypt_device *cd = NULL;
  502. + luksmeta_uuid_t uuid = {};
  503. + uint32_t offset = 0;
  504. + uint32_t length = 0;
  505. + uint8_t data0[4096];
  506. + uint8_t data1[64];
  507. + int r;
  508. +
  509. + reset_filename();
  510. + crypt_free(test_format());
  511. + cd = test_init();
  512. + test_hole(cd, &offset, &length);
  513. +
  514. + memset(data0, 'A', sizeof(data0));
  515. + memset(data1, 'B', sizeof(data1));
  516. +
  517. + /* First entry: exactly one aligned page. */
  518. + r = luksmeta_save(cd, 0, UUID0, data0, sizeof(data0));
  519. + assert(r == 0);
  520. +
  521. + /* Second entry: should start immediately after. */
  522. + r = luksmeta_save(cd, 1, UUID1, data1, sizeof(data1));
  523. + assert(r == 1);
  524. +
  525. + /* Both load correctly. */
  526. + {
  527. + uint8_t out[sizeof(data0)] = {};
  528. + r = luksmeta_load(cd, 0, uuid, out, sizeof(out));
  529. + assert(r == (int) sizeof(data0));
  530. + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
  531. + assert(memcmp(out, data0, sizeof(data0)) == 0);
  532. + }
  533. +
  534. + {
  535. + uint8_t out[sizeof(data1)] = {};
  536. + r = luksmeta_load(cd, 1, uuid, out, sizeof(out));
  537. + assert(r == (int) sizeof(data1));
  538. + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
  539. + assert(memcmp(out, data1, sizeof(data1)) == 0);
  540. + }
  541. +
  542. + crypt_free(cd);
  543. + unlink(filename);
  544. +}
  545. +
  546. +/*
  547. + * Space reuse: wipe an earlier entry, save a replacement into the freed
  548. + * hole, and verify both the replacement and the untouched later entry.
  549. + */
  550. +static void
  551. +test_reuse(void)
  552. +{
  553. + struct crypt_device *cd = NULL;
  554. + luksmeta_uuid_t uuid = {};
  555. + uint8_t data0[64];
  556. + uint8_t data1[64];
  557. + uint8_t data_new[64];
  558. + int r;
  559. +
  560. + reset_filename();
  561. + crypt_free(test_format());
  562. + cd = test_init();
  563. +
  564. + memset(data0, 'A', sizeof(data0));
  565. + memset(data1, 'B', sizeof(data1));
  566. + memset(data_new, 'C', sizeof(data_new));
  567. +
  568. + /* Two entries. */
  569. + r = luksmeta_save(cd, 0, UUID0, data0, sizeof(data0));
  570. + assert(r == 0);
  571. + r = luksmeta_save(cd, 1, UUID1, data1, sizeof(data1));
  572. + assert(r == 1);
  573. +
  574. + /* Wipe the first. */
  575. + assert(luksmeta_wipe(cd, 0, UUID0) == 0);
  576. +
  577. + /* Save a replacement into the freed slot. */
  578. + r = luksmeta_save(cd, 0, UUID2, data_new, sizeof(data_new));
  579. + assert(r == 0);
  580. +
  581. + /* Replacement loads correctly. */
  582. + {
  583. + uint8_t out[sizeof(data_new)] = {};
  584. + r = luksmeta_load(cd, 0, uuid, out, sizeof(out));
  585. + assert(r == (int) sizeof(data_new));
  586. + assert(memcmp(uuid, UUID2, sizeof(UUID2)) == 0);
  587. + assert(memcmp(out, data_new, sizeof(data_new)) == 0);
  588. + }
  589. +
  590. + /* Later entry is untouched. */
  591. + {
  592. + uint8_t out[sizeof(data1)] = {};
  593. + r = luksmeta_load(cd, 1, uuid, out, sizeof(out));
  594. + assert(r == (int) sizeof(data1));
  595. + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
  596. + assert(memcmp(out, data1, sizeof(data1)) == 0);
  597. + }
  598. +
  599. + crypt_free(cd);
  600. + unlink(filename);
  601. +}
  602. +
  603. +int
  604. +main(int argc, char *argv[])
  605. +{
  606. + test_bug1();
  607. + test_control();
  608. + test_bug2();
  609. + test_zero_length();
  610. + test_exact_fit();
  611. + test_over_capacity();
  612. + test_adjacent();
  613. + test_reuse();
  614. + return 0;
  615. +}