All of lore.kernel.org
 help / color / mirror / Atom feed
From: Itai Handler <itai.handler@gmail.com>
To: cryptsetup@lists.linux.dev, Milan Broz <gmazyland@gmail.com>
Subject: [PATCH] Reject LUKS2 header with out-of-range encryption sector size.
Date: Tue, 22 Sep 2026 13:05:54 +0300	[thread overview]
Message-ID: <20260922100554.28692-1-itai.handler@gmail.com> (raw)

LUKS2 keeps the encryption sector size in the segment metadata.
MAX_SECTOR_SIZE (4096 bytes, the smallest page size among the platforms
cryptsetup runs on) is the largest value the format allows, because a
LUKS device has to remain openable on all of them.

Writing a header enforces that, through LUKS2_check_encryption_sector(),
so cryptsetup never produces one that exceeds it. Reading a header does
not: hdr_validate_crypt_segment() only requires "sector_size" to be
non-zero and 512-aligned. A header carrying a larger value - after
metadata corruption, or from anything that wrote it other than
cryptsetup - therefore loads without complaint, and luksDump reports it
as a valid LUKS device:

	sector: 65536 [bytes]

Activating it then fails, but only because dm-crypt applies a 4096-byte
limit of its own. A constraint of the LUKS2 format is in effect being
enforced by an implementation detail of one kernel target, which is the
wrong place for it and leaves the tools reporting such a device as
valid.

Check the limit where the rest of the header is checked, so that the
same rule applies to a header being read as to one being written.

This cannot reject anything cryptsetup has written. LUKS1 is unaffected,
its encryption sector size is always 512 bytes.

Assisted-by: LLM
---
 lib/luks2/luks2_json_metadata.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/luks2/luks2_json_metadata.c b/lib/luks2/luks2_json_metadata.c
index c532576..bb88846 100644
--- a/lib/luks2/luks2_json_metadata.c
+++ b/lib/luks2/luks2_json_metadata.c
@@ -555,7 +555,7 @@ static int hdr_validate_crypt_segment(struct crypt_device *cd, json_object *jobj
 	}
 
 	sector_size = crypt_jobj_get_uint32(jobj_sector_size);
-	if (!sector_size || MISALIGNED_512(sector_size)) {
+	if (!sector_size || MISALIGNED_512(sector_size) || sector_size > MAX_SECTOR_SIZE) {
 		log_dbg(cd, "Illegal sector size: %" PRIu32, sector_size);
 		return 1;
 	}
-- 
2.34.1


             reply	other threads:[~2026-09-22 10:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-22 10:05 Itai Handler [this message]
2026-09-22 12:07 ` [PATCH] Reject LUKS2 header with out-of-range encryption sector size Ondrej Kozina
2026-09-22 12:21   ` Itai Handler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260922100554.28692-1-itai.handler@gmail.com \
    --to=itai.handler@gmail.com \
    --cc=cryptsetup@lists.linux.dev \
    --cc=gmazyland@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.