]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: (dos) fix logical partition start
authorMartin Jungblut Schreiner <martinjungblut@gmail.com>
Mon, 29 Dec 2025 22:52:56 +0000 (19:52 -0300)
committerMartin Jungblut Schreiner <martinjungblut@gmail.com>
Mon, 29 Dec 2025 22:52:56 +0000 (19:52 -0300)
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.

libfdisk/src/dos.c

index c88d2a4f21308e1e3041cfe4c9a4b5478bf7a80c..f85104b2828f0baebd3d0ed033ba2e3cb531b45c 100644 (file)
@@ -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;