| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628 |
- Subject: Libluksmeta: fix gap allocator bounds checking
- Origin: upstream, commit v10-1-gba140c8 <https://github.com/latchset/luksmeta/commit/v10-1-gba140c8>
- Author: Sergio Correia <scorreia@redhat.com>
- Date: Thu Oct 8 06:23:06 2026 -0300
- Forwarded: not-needed
- The hard-limit passed to overlap() included slots[0].offset, shifting
- the boundary past the gap whenever slot 0 was occupied. Pass the bare
- gap length instead.
- The overlap predicate missed a candidate range fully contained inside
- an existing slot. Replace the two partial checks with the canonical
- half-open interval intersection (start < e && s->offset < end) and
- widen the slot endpoint to uint64_t as defense-in-depth.
- Add test-lm-gap covering both regressions, zero-length metadata,
- exact-fit and over-capacity boundaries, adjacent allocation, and
- space reuse after wipe.
- Signed-off-by: Sergio Correia <scorreia@redhat.com>
- --- a/Makefile.am
- +++ b/Makefile.am
- @@ -34,13 +34,14 @@
- check_LTLIBRARIES = libtest.la
- libtest_la_SOURCES = test.c test.h
-
- -check_PROGRAMS = test-crc32c test-lm-assumptions test-lm-init test-lm-one test-lm-two test-lm-big
- +check_PROGRAMS = test-crc32c test-lm-assumptions test-lm-init test-lm-one test-lm-two test-lm-big test-lm-gap
- test_crc32c_LDADD = libcrc32c.la
- test_lm_assumptions_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
- test_lm_init_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
- test_lm_one_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
- test_lm_two_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
- test_lm_big_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
- +test_lm_gap_LDADD = libtest.la libluksmeta.la @cryptsetup_LIBS@
-
- EXTRA_DIST = $(man_ADOC_FILES) test-luksmeta
- TESTS = $(check_PROGRAMS) test-luksmeta
- --- a/libluksmeta.c
- +++ b/libluksmeta.c
- @@ -77,12 +77,13 @@
-
- for (int i = 0; i < LUKS_NSLOTS; i++) {
- const lm_slot_t *s = &lm->slots[i];
- - uint32_t e = s->offset + s->length;
-
- - if (start <= s->offset && s->offset < end)
- - return true;
- + if (s->length == 0)
- + continue;
- +
- + uint64_t e = (uint64_t) s->offset + s->length;
-
- - if (start < e && e <= end)
- + if (start < e && s->offset < end)
- return true;
- }
-
- @@ -100,7 +101,7 @@
- return 0;
-
- for (uint32_t off = ALIGN(1, true); off < length; off += ALIGN(1, true)) {
- - if (!overlap(lm, off, off + size, lm->slots[0].offset + length))
- + if (!overlap(lm, off, off + size, length))
- return off;
- }
-
- --- /dev/null
- +++ b/test-lm-gap.c
- @@ -0,0 +1,557 @@
- +/* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
- +/*
- + * Copyright (c) 2026 Red Hat, Inc.
- + * Author: Sergio Correia <scorreia@redhat.com>
- + *
- + * This program is free software: you can redistribute it and/or modify it
- + * under the terms of the GNU Lesser General Public License as published by
- + * the Free Software Foundation, either version 2.1 of the License, or
- + * (at your option) any later version.
- + *
- + * This program is distributed in the hope that it will be useful,
- + * but WITHOUT ANY WARRANTY; without even the implied warranty of
- + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- + * GNU Lesser General Public License for more details.
- + *
- + * You should have received a copy of the GNU Lesser General Public License
- + * along with this program. If not, see <http://www.gnu.org/licenses/>.
- + */
- +
- +#include "test.h"
- +#include <error.h>
- +#include <errno.h>
- +#include <fcntl.h>
- +#include <stdlib.h>
- +#include <string.h>
- +#include <sys/stat.h>
- +
- +static const luksmeta_uuid_t UUID0 = {
- + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
- + 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11
- +};
- +
- +static const luksmeta_uuid_t UUID1 = {
- + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
- + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
- +};
- +
- +static const luksmeta_uuid_t UUID2 = {
- + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
- + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33
- +};
- +
- +/* Reset the filename template so mkstemp() in test_format() works on
- + * each call. The existing tests only call test_format() once, but we
- + * call it for every scenario. */
- +static void
- +reset_filename(void)
- +{
- + strcpy(filename, "/tmp/luksmetaXXXXXX");
- +}
- +
- +/* Read a region of the image file into a freshly allocated buffer. */
- +static uint8_t *
- +read_image(size_t off, size_t len)
- +{
- + uint8_t *buf = NULL;
- + int fd;
- +
- + buf = malloc(len);
- + if (!buf)
- + error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
- +
- + fd = open(filename, O_RDONLY);
- + if (fd < 0)
- + error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
- +
- + if (lseek(fd, off, SEEK_SET) == -1) {
- + free(buf);
- + close(fd);
- + error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
- + }
- +
- + if (read(fd, buf, len) != (ssize_t) len) {
- + free(buf);
- + close(fd);
- + error(EXIT_FAILURE, errno, "%s:%d", __FILE__, __LINE__);
- + }
- +
- + close(fd);
- + return buf;
- +}
- +
- +/*
- + * Bug 1 regression: occupied slot 0 shifts the hard limit, allowing a
- + * save to write past the gap into the encrypted payload area.
- + */
- +static void
- +test_bug1(void)
- +{
- + struct crypt_device *cd = NULL;
- + luksmeta_uuid_t uuid = {};
- + uint32_t offset = 0;
- + uint32_t length = 0;
- + uint8_t *before = NULL;
- + uint8_t *after = NULL;
- + uint8_t small[16];
- + uint8_t *big = NULL;
- + size_t capacity;
- + int r;
- +
- + reset_filename();
- + crypt_free(test_format());
- + cd = test_init();
- + test_hole(cd, &offset, &length);
- +
- + assert(length % 4096 == 0);
- + assert(length >= 12288);
- +
- + /* Usable data area: gap minus the header page. */
- + capacity = length - ALIGN(1, true);
- +
- + /* Fill small data with a known pattern. */
- + memset(small, 'S', sizeof(small));
- +
- + /* Save a small entry in slot 0. */
- + r = luksmeta_save(cd, 0, UUID0, small, sizeof(small));
- + assert(r == 0);
- +
- + /* Seed the payload boundary so we can detect overwrites. */
- + {
- + int fd = open(filename, O_RDWR);
- + uint8_t sentinel[4096];
- + assert(fd >= 0);
- + memset(sentinel, 'P', sizeof(sentinel));
- + assert(lseek(fd, offset + length, SEEK_SET) != -1);
- + assert(write(fd, sentinel, sizeof(sentinel)) == sizeof(sentinel));
- + close(fd);
- + }
- +
- + /* Snapshot the image before the rejected save. */
- + before = read_image(offset, length + 4096);
- +
- + /* Attempt to save capacity bytes — must fail, slot 0 already occupies
- + * space in the gap. */
- + big = calloc(1, capacity);
- + assert(big != NULL);
- + memset(big, 'B', capacity);
- +
- + r = luksmeta_save(cd, 1, UUID1, big, capacity);
- + assert(r == -ENOSPC);
- + free(big);
- +
- + /* Image must be unchanged. */
- + after = read_image(offset, length + 4096);
- + assert(memcmp(before, after, length + 4096) == 0);
- + free(before);
- + free(after);
- +
- + /* Original entry still loads correctly. */
- + {
- + uint8_t out[sizeof(small)] = {};
- + r = luksmeta_load(cd, 0, uuid, out, sizeof(out));
- + assert(r == (int) sizeof(small));
- + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
- + assert(memcmp(out, small, sizeof(small)) == 0);
- + }
- +
- + /* Destination slot is still empty. */
- + r = luksmeta_load(cd, 1, uuid, NULL, 0);
- + assert(r == -ENODATA);
- +
- + crypt_free(cd);
- + unlink(filename);
- +}
- +
- +/*
- + * Control: same sizes as Bug 1 but with the small entry in slot 1
- + * instead of slot 0. Verifies the rejection is not slot-0-specific.
- + */
- +static void
- +test_control(void)
- +{
- + struct crypt_device *cd = NULL;
- + luksmeta_uuid_t uuid = {};
- + uint32_t offset = 0;
- + uint32_t length = 0;
- + uint8_t *before = NULL;
- + uint8_t *after = NULL;
- + uint8_t small[16];
- + uint8_t *big = NULL;
- + size_t capacity;
- + int r;
- +
- + reset_filename();
- + crypt_free(test_format());
- + cd = test_init();
- + test_hole(cd, &offset, &length);
- +
- + capacity = length - ALIGN(1, true);
- + memset(small, 'S', sizeof(small));
- +
- + /* Save a small entry in slot 1 (not slot 0). */
- + r = luksmeta_save(cd, 1, UUID1, small, sizeof(small));
- + assert(r == 1);
- +
- + /* Seed payload boundary. */
- + {
- + int fd = open(filename, O_RDWR);
- + uint8_t sentinel[4096];
- + assert(fd >= 0);
- + memset(sentinel, 'P', sizeof(sentinel));
- + assert(lseek(fd, offset + length, SEEK_SET) != -1);
- + assert(write(fd, sentinel, sizeof(sentinel)) == sizeof(sentinel));
- + close(fd);
- + }
- +
- + before = read_image(offset, length + 4096);
- +
- + big = calloc(1, capacity);
- + assert(big != NULL);
- + memset(big, 'B', capacity);
- +
- + r = luksmeta_save(cd, 2, UUID2, big, capacity);
- + assert(r == -ENOSPC);
- + free(big);
- +
- + after = read_image(offset, length + 4096);
- + assert(memcmp(before, after, length + 4096) == 0);
- + free(before);
- + free(after);
- +
- + /* Original entry still loads. */
- + {
- + uint8_t out[sizeof(small)] = {};
- + r = luksmeta_load(cd, 1, uuid, out, sizeof(out));
- + assert(r == (int) sizeof(small));
- + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
- + assert(memcmp(out, small, sizeof(small)) == 0);
- + }
- +
- + r = luksmeta_load(cd, 2, uuid, NULL, 0);
- + assert(r == -ENODATA);
- +
- + crypt_free(cd);
- + unlink(filename);
- +}
- +
- +/*
- + * Bug 2 regression: a new small entry whose candidate position falls
- + * entirely inside an existing entry longer than two pages. The allocator
- + * must skip the existing range and place the new entry after it.
- + */
- +static void
- +test_bug2(void)
- +{
- + struct crypt_device *cd = NULL;
- + luksmeta_uuid_t uuid = {};
- + uint32_t offset = 0;
- + uint32_t length = 0;
- + uint8_t big[9000];
- + uint8_t small[100];
- + int r;
- +
- + reset_filename();
- + crypt_free(test_format());
- + cd = test_init();
- + test_hole(cd, &offset, &length);
- +
- + /* Need room for the header page + 9000 bytes (3 aligned pages) +
- + * the second entry (1 aligned page) = 5 pages minimum. */
- + assert(length >= 24576);
- +
- + memset(big, 'A', sizeof(big));
- + memset(small, 'X', sizeof(small));
- +
- + /* Slot 1: 9000 bytes, occupies gap-relative [4096, 13096). */
- + r = luksmeta_save(cd, 1, UUID1, big, sizeof(big));
- + assert(r == 1);
- +
- + /* Slot 2: 100 bytes. Must skip the existing range and succeed. */
- + r = luksmeta_save(cd, 2, UUID2, small, sizeof(small));
- + assert(r == 2);
- +
- + /* Both entries load with their original data. */
- + {
- + uint8_t out_big[sizeof(big)] = {};
- + r = luksmeta_load(cd, 1, uuid, out_big, sizeof(out_big));
- + assert(r == (int) sizeof(big));
- + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
- + assert(memcmp(out_big, big, sizeof(big)) == 0);
- + }
- +
- + {
- + uint8_t out_small[sizeof(small)] = {};
- + r = luksmeta_load(cd, 2, uuid, out_small, sizeof(out_small));
- + assert(r == (int) sizeof(small));
- + assert(memcmp(uuid, UUID2, sizeof(UUID2)) == 0);
- + assert(memcmp(out_small, small, sizeof(small)) == 0);
- + }
- +
- + crypt_free(cd);
- + unlink(filename);
- +}
- +
- +/*
- + * Zero-length metadata: a zero-length entry coexists with a normal entry.
- + */
- +static void
- +test_zero_length(void)
- +{
- + struct crypt_device *cd = NULL;
- + luksmeta_uuid_t uuid = {};
- + uint8_t dummy = 0;
- + uint8_t normal[64];
- + int r;
- +
- + reset_filename();
- + crypt_free(test_format());
- + cd = test_init();
- +
- + /* Save a zero-length entry. Use a non-NULL buffer to avoid UB. */
- + r = luksmeta_save(cd, 0, UUID0, &dummy, 0);
- + assert(r == 0);
- +
- + /* It loads back with length 0 and the correct UUID. */
- + r = luksmeta_load(cd, 0, uuid, NULL, 0);
- + assert(r == 0);
- + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
- +
- + /* Save a normal entry in the next slot. */
- + memset(normal, 'N', sizeof(normal));
- + r = luksmeta_save(cd, 1, UUID1, normal, sizeof(normal));
- + assert(r == 1);
- +
- + /* Both coexist: zero-length still loads. */
- + memset(uuid, 0, sizeof(uuid));
- + r = luksmeta_load(cd, 0, uuid, NULL, 0);
- + assert(r == 0);
- + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
- +
- + /* Normal entry loads with full contents. */
- + {
- + uint8_t out[sizeof(normal)] = {};
- + r = luksmeta_load(cd, 1, uuid, out, sizeof(out));
- + assert(r == (int) sizeof(normal));
- + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
- + assert(memcmp(out, normal, sizeof(normal)) == 0);
- + }
- +
- + crypt_free(cd);
- + unlink(filename);
- +}
- +
- +/*
- + * Exact-fit boundary: fill the entire usable data area.
- + */
- +static void
- +test_exact_fit(void)
- +{
- + struct crypt_device *cd = NULL;
- + luksmeta_uuid_t uuid = {};
- + uint32_t offset = 0;
- + uint32_t length = 0;
- + size_t capacity;
- + uint8_t *data = NULL;
- + int r;
- +
- + reset_filename();
- + crypt_free(test_format());
- + cd = test_init();
- + test_hole(cd, &offset, &length);
- +
- + assert(length % 4096 == 0);
- + capacity = length - ALIGN(1, true);
- +
- + data = malloc(capacity);
- + assert(data != NULL);
- + memset(data, 'F', capacity);
- +
- + /* Save exactly capacity bytes — should succeed. */
- + r = luksmeta_save(cd, 0, UUID0, data, capacity);
- + assert(r == 0);
- +
- + /* Verify full readback. */
- + {
- + uint8_t *out = malloc(capacity);
- + assert(out != NULL);
- + r = luksmeta_load(cd, 0, uuid, out, capacity);
- + assert(r == (int) capacity);
- + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
- + assert(memcmp(out, data, capacity) == 0);
- + free(out);
- + }
- +
- + free(data);
- + crypt_free(cd);
- + unlink(filename);
- +}
- +
- +/*
- + * One byte over capacity: must be rejected without changing the image.
- + */
- +static void
- +test_over_capacity(void)
- +{
- + struct crypt_device *cd = NULL;
- + luksmeta_uuid_t uuid = {};
- + uint32_t offset = 0;
- + uint32_t length = 0;
- + size_t capacity;
- + uint8_t *data = NULL;
- + uint8_t *before = NULL;
- + uint8_t *after = NULL;
- + int r;
- +
- + reset_filename();
- + crypt_free(test_format());
- + cd = test_init();
- + test_hole(cd, &offset, &length);
- +
- + assert(length % 4096 == 0);
- + capacity = length - ALIGN(1, true);
- +
- + data = calloc(1, capacity + 1);
- + assert(data != NULL);
- + memset(data, 'O', capacity + 1);
- +
- + before = read_image(offset, length);
- +
- + r = luksmeta_save(cd, 0, UUID0, data, capacity + 1);
- + assert(r == -ENOSPC);
- + free(data);
- +
- + after = read_image(offset, length);
- + assert(memcmp(before, after, length) == 0);
- + free(before);
- + free(after);
- +
- + /* Slot remains empty. */
- + r = luksmeta_load(cd, 0, uuid, NULL, 0);
- + assert(r == -ENODATA);
- +
- + crypt_free(cd);
- + unlink(filename);
- +}
- +
- +/*
- + * Adjacent allocation: second entry starts at the first's endpoint.
- + */
- +static void
- +test_adjacent(void)
- +{
- + struct crypt_device *cd = NULL;
- + luksmeta_uuid_t uuid = {};
- + uint32_t offset = 0;
- + uint32_t length = 0;
- + uint8_t data0[4096];
- + uint8_t data1[64];
- + int r;
- +
- + reset_filename();
- + crypt_free(test_format());
- + cd = test_init();
- + test_hole(cd, &offset, &length);
- +
- + memset(data0, 'A', sizeof(data0));
- + memset(data1, 'B', sizeof(data1));
- +
- + /* First entry: exactly one aligned page. */
- + r = luksmeta_save(cd, 0, UUID0, data0, sizeof(data0));
- + assert(r == 0);
- +
- + /* Second entry: should start immediately after. */
- + r = luksmeta_save(cd, 1, UUID1, data1, sizeof(data1));
- + assert(r == 1);
- +
- + /* Both load correctly. */
- + {
- + uint8_t out[sizeof(data0)] = {};
- + r = luksmeta_load(cd, 0, uuid, out, sizeof(out));
- + assert(r == (int) sizeof(data0));
- + assert(memcmp(uuid, UUID0, sizeof(UUID0)) == 0);
- + assert(memcmp(out, data0, sizeof(data0)) == 0);
- + }
- +
- + {
- + uint8_t out[sizeof(data1)] = {};
- + r = luksmeta_load(cd, 1, uuid, out, sizeof(out));
- + assert(r == (int) sizeof(data1));
- + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
- + assert(memcmp(out, data1, sizeof(data1)) == 0);
- + }
- +
- + crypt_free(cd);
- + unlink(filename);
- +}
- +
- +/*
- + * Space reuse: wipe an earlier entry, save a replacement into the freed
- + * hole, and verify both the replacement and the untouched later entry.
- + */
- +static void
- +test_reuse(void)
- +{
- + struct crypt_device *cd = NULL;
- + luksmeta_uuid_t uuid = {};
- + uint8_t data0[64];
- + uint8_t data1[64];
- + uint8_t data_new[64];
- + int r;
- +
- + reset_filename();
- + crypt_free(test_format());
- + cd = test_init();
- +
- + memset(data0, 'A', sizeof(data0));
- + memset(data1, 'B', sizeof(data1));
- + memset(data_new, 'C', sizeof(data_new));
- +
- + /* Two entries. */
- + r = luksmeta_save(cd, 0, UUID0, data0, sizeof(data0));
- + assert(r == 0);
- + r = luksmeta_save(cd, 1, UUID1, data1, sizeof(data1));
- + assert(r == 1);
- +
- + /* Wipe the first. */
- + assert(luksmeta_wipe(cd, 0, UUID0) == 0);
- +
- + /* Save a replacement into the freed slot. */
- + r = luksmeta_save(cd, 0, UUID2, data_new, sizeof(data_new));
- + assert(r == 0);
- +
- + /* Replacement loads correctly. */
- + {
- + uint8_t out[sizeof(data_new)] = {};
- + r = luksmeta_load(cd, 0, uuid, out, sizeof(out));
- + assert(r == (int) sizeof(data_new));
- + assert(memcmp(uuid, UUID2, sizeof(UUID2)) == 0);
- + assert(memcmp(out, data_new, sizeof(data_new)) == 0);
- + }
- +
- + /* Later entry is untouched. */
- + {
- + uint8_t out[sizeof(data1)] = {};
- + r = luksmeta_load(cd, 1, uuid, out, sizeof(out));
- + assert(r == (int) sizeof(data1));
- + assert(memcmp(uuid, UUID1, sizeof(UUID1)) == 0);
- + assert(memcmp(out, data1, sizeof(data1)) == 0);
- + }
- +
- + crypt_free(cd);
- + unlink(filename);
- +}
- +
- +int
- +main(int argc, char *argv[])
- +{
- + test_bug1();
- + test_control();
- + test_bug2();
- + test_zero_length();
- + test_exact_fit();
- + test_over_capacity();
- + test_adjacent();
- + test_reuse();
- + return 0;
- +}
|