[PATCH] disk: gpt: verify alternate LBA points to last usable LBA

Heinrich Schuchardt xypron.glpk at gmx.de
Mon Mar 8 18:38:00 CET 2021


On 08.03.21 17:07, Stefan Herbrechtsmeier wrote:
> From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier at weidmueller.com>
>
> The gpt command require the GPT backup header at the standard location
> at the end of the device.Check the alternate LBA value before reading
> the GPT backup header from the last usable LBA of the device.

If there is a bug in the gpt command, please, fix it instead of
introducing constraints that don't exist in the UEFI specification.

The UEFI specification has:

"The backup GPT Partition Entry Array must be located after the Last
Usable LBA and end before the backup GPT Header."

And of course the backup GPT header has to reside within the disk size.

There may be good reasons not to place the GPT header on the last LBA.
E.g. if the last sector is defective.

>
> Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier at weidmueller.com>
>
> ---
>
>  disk/part_efi.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/disk/part_efi.c b/disk/part_efi.c
> index e5636ea7e6..0fb7ff0b6b 100644
> --- a/disk/part_efi.c
> +++ b/disk/part_efi.c
> @@ -692,6 +692,15 @@ int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
>  	/* Free pte before allocating again */
>  	free(*gpt_pte);
>
> +	/*
> +	 * Check that the alternate_lba entry points to the last LBA
> +	 */
> +	if (le64_to_cpu(gpt_head->alternate_lba) != (dev_desc->lba - 1)) {

This is wrong. You may check:

le64_to_cpu(gpt_head->alternate_lba) < dev_desc->lba

and

AlternateLBA > LastUsableLBA +
ceil(NumberOfPartitionEntries * SizeOfPartitionEntry / SizeOfLBA)

> +		printf("%s: *** ERROR: Misplaced Backup GPT ***\n",
> +		       __func__);
> +		return -1;
> +	}
> +
>  	if (is_gpt_valid(dev_desc, (dev_desc->lba - 1),

This seems to be one of the buggy lines. You must use the alternate_lba
field here.

find_valid_gpt() should be corrected, too.

Best regards

Heinrich

>  			 gpt_head, gpt_pte) != 1) {
>  		printf("%s: *** ERROR: Invalid Backup GPT ***\n",
>



More information about the U-Boot mailing list