]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: (gpt) make sure device is large enough
authorKarel Zak <kzak@redhat.com>
Wed, 30 Sep 2020 09:44:03 +0000 (11:44 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 30 Sep 2020 09:44:03 +0000 (11:44 +0200)
The current code creates GPT header and partitions arrays (with 128
entries ...) although there is no space for all the stuff. This patch
forces fdisk_create_disklabel() to return -ENOSPC if the last and first
usable LBA calculation is out of device size.

Addresses: https://github.com/karelzak/util-linux/issues/1147
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/gpt.c

index 66520c51db60846226f3461dbcc5c27d035c532f..563c77073b87c2eb0187f14f58aba60e9e6cad5d 100644 (file)
@@ -417,9 +417,13 @@ static inline int gpt_calculate_alternative_entries_lba(
        uint64_t esects = 0;
        int rc = gpt_calculate_sectorsof_entries(hdr, nents, &esects, cxt);
 
-       if (rc == 0)
-               *sz = cxt->total_sectors - 1ULL - esects;
-       return rc;
+       if (rc)
+               return rc;
+       if (cxt->total_sectors < 1ULL + esects)
+               return -ENOSPC;
+
+       *sz = cxt->total_sectors - 1ULL - esects;
+       return 0;
 }
 
 static inline int gpt_calculate_last_lba(
@@ -431,9 +435,13 @@ static inline int gpt_calculate_last_lba(
        uint64_t esects = 0;
        int rc = gpt_calculate_sectorsof_entries(hdr, nents, &esects, cxt);
 
-       if (rc == 0)
-               *sz = cxt->total_sectors - 2ULL - esects;
-       return rc;
+       if (rc)
+               return rc;
+       if (cxt->total_sectors < 2ULL + esects)
+               return -ENOSPC;
+
+       *sz = cxt->total_sectors - 2ULL - esects;
+       return 0;
 }
 
 static inline int gpt_calculate_first_lba(
@@ -3082,7 +3090,6 @@ static int gpt_reset_alignment(struct fdisk_context *cxt)
                uint64_t first, last;
 
                count_first_last_lba(cxt, &first, &last);
-
                if (cxt->first_lba < first)
                        cxt->first_lba = first;
                if (cxt->last_lba > last)