[parted-devel] [PATCH 1/2] libparted: gpt: compare first and last usable LBAs

Jim Meyering jim at meyering.net
Wed Sep 12 09:04:57 UTC 2012


Davidlohr Bueso wrote:
> When verifying GPT header integrity, make sure that
> first usable LBA is smaller than last usable LBA.
> ---
>  libparted/labels/gpt.c |    5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
> index 6032e3f..6dfaf3e 100644
> --- a/libparted/labels/gpt.c
> +++ b/libparted/labels/gpt.c
> @@ -650,6 +650,11 @@ _header_is_valid (PedDisk const *disk, GuidPartitionTableHeader_t *gpt,
>      return 0;
>
>    PedSector first_usable = PED_LE64_TO_CPU (gpt->FirstUsableLBA);
> +  PedSector last_usable = PED_LE64_TO_CPU (gpt->LastUsableLBA);
> +
> +  if (last_usable < first_usable)
> +    return 0;
> +
>    if (first_usable < 3)
>      return 0;

Thank you for the patch.  That is an improvement.
Normally I would require a test that exercises this new code,
(the bar for GPT is particularly high) but I don't have time
to write it now, and it wouldn't be fair to ask you to do it.

I've made a minor correctness change to the log (s/smaller/no larger than/)
since they may be equal.


>From 46d9108009ccb9ac567cc285a15efb05864932d5 Mon Sep 17 00:00:00 2001
From: Davidlohr Bueso <dave at gnu.org>
Date: Tue, 11 Sep 2012 19:22:32 +0200
Subject: [PATCH] gpt: require first_usable_LBA <= last_usable_LBA

When verifying GPT header integrity, ensure that the
first usable LBA is no larger than the last usable LBA.
* libparted/labels/gpt.c (_header_is_valid): Reject a header
with last_usable < first_usable.
---
 libparted/labels/gpt.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libparted/labels/gpt.c b/libparted/labels/gpt.c
index 6032e3f..83e518f 100644
--- a/libparted/labels/gpt.c
+++ b/libparted/labels/gpt.c
@@ -653,6 +653,10 @@ _header_is_valid (PedDisk const *disk, GuidPartitionTableHeader_t *gpt,
   if (first_usable < 3)
     return 0;

+  PedSector last_usable = PED_LE64_TO_CPU (gpt->LastUsableLBA);
+  if (last_usable < first_usable)
+    return 0;
+
   origcrc = gpt->HeaderCRC32;
   gpt->HeaderCRC32 = 0;
   if (pth_crc32 (dev, gpt, &crc) != 0)
--
1.7.12.363.g53284de



More information about the parted-devel mailing list