From 28d00d008f99a9588f172c236ad1cbe17e42c69c Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Thu, 23 Apr 2015 12:32:36 +0200 Subject: [PATCH] libfdisk: (gpt) add extra check for First/Last LBA Signed-off-by: Karel Zak --- disk-utils/sfdisk.c | 5 +++-- libfdisk/src/gpt.c | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index d8428d1f37..cb77c47f6f 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -1164,7 +1164,8 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) if (!sf->quiet) fputs(_("Checking that no-one is using this disk right now ..."), stdout); if (is_device_used(sf)) { - fputs(_(" FAILED\n\n"), stdout); + if (!sf->quiet) + fputs(_(" FAILED\n\n"), stdout); fdisk_warnx(sf->cxt, _( "This disk is currently in use - repartitioning is probably a bad idea.\n" @@ -1173,7 +1174,7 @@ static int command_fdisk(struct sfdisk *sf, int argc, char **argv) if (!sf->force) errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks.")); - } else + } else if (!sf->quiet) fputs(_(" OK\n\n"), stdout); } diff --git a/libfdisk/src/gpt.c b/libfdisk/src/gpt.c index fd49c1349b..a7766da604 100644 --- a/libfdisk/src/gpt.c +++ b/libfdisk/src/gpt.c @@ -2182,6 +2182,21 @@ static int gpt_add_partition( goto done; } + /* Be paranoid and check agains on-disk setting rather than against libfdisk cxt */ + if (user_l > le64_to_cpu(pheader->last_usable_lba)) { + fdisk_warnx(cxt, _("The last usable GPT sector is %ju, but %ju is requested."), + le64_to_cpu(pheader->last_usable_lba), user_l); + rc = -EINVAL; + goto done; + } + + if (user_f < le64_to_cpu(pheader->first_usable_lba)) { + fdisk_warnx(cxt, _("The first usable GPT sector is %ju, but %ju is requested."), + le64_to_cpu(pheader->first_usable_lba), user_f); + rc = -EINVAL; + goto done; + } + assert(!FDISK_IS_UNDEF(user_l)); assert(!FDISK_IS_UNDEF(user_f)); -- 2.47.2