From: Martin Jungblut Schreiner Date: Mon, 29 Dec 2025 22:52:56 +0000 (-0300) Subject: libfdisk: (dos) fix logical partition start X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8b8da020f57414c90981371da71fdf32d2253ac7;p=thirdparty%2Futil-linux.git libfdisk: (dos) fix logical partition start fdisk could accept adjacent logical partitions, causing the EBR for the new logical partition to be written inside the previous partition’s data area. This can corrupt the EBR chain. Fix free-sector search to keep an EBR gap (first_lba) after logical partitions. --- diff --git a/libfdisk/src/dos.c b/libfdisk/src/dos.c index c88d2a4f2..f85104b28 100644 --- a/libfdisk/src/dos.c +++ b/libfdisk/src/dos.c @@ -1201,6 +1201,21 @@ static int find_first_free_sector_in_range( p_start -= cxt->first_lba; if (first < p_start) continue; + + /* if we're placing a logical partition start, ensure + there's room for the *next* EBR (stored at start - first_lba). + therefore enforce: + start(Lnew) >= end(Lprev) + first_lba + 1 */ + if (logical && first > p_end && (first - p_end) <= cxt->first_lba) { + first = p_end + 1 + cxt->first_lba; + first_moved = 1; + + if (first > end) + return -ENOSPC; + + continue; + } + if (first <= p_end) { first = p_end + 1 + (logical ? cxt->first_lba : 0); first_moved = 1;