NetBSD Problem Report #57410

From www@netbsd.org  Tue May 16 00:29:51 2023
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id EA4E21A923B
	for <gnats-bugs@gnats.NetBSD.org>; Tue, 16 May 2023 00:29:50 +0000 (UTC)
Message-Id: <20230516002949.690CB1A923C@mollari.NetBSD.org>
Date: Tue, 16 May 2023 00:29:49 +0000 (UTC)
From: mp@petermann-it.de
Reply-To: mp@petermann-it.de
To: gnats-bugs@NetBSD.org
Subject: gpt silently exits if file isn't an integral multiple of 512 bytes long
X-Send-Pr-Version: www-1.0

>Number:         57410
>Category:       bin
>Synopsis:       gpt silently exits if file isn't an integral multiple of 512 bytes long
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue May 16 00:30:00 +0000 2023
>Last-Modified:  Tue May 16 05:25:01 +0000 2023
>Originator:     Matthias Petermann
>Release:        NetBSD-10.0_BETA
>Organization:
>Environment:
NetBSD vhost2.local 10.0_BETA NetBSD 10.0_BETA (XEN3_DOM0) #0: Mon May  8 07:40:40 UTC 2023  root@ws.local:/build/netbsd-10/obj/sys/arch/amd64/compile/XEN3_DOM0 amd64
>Description:
This is a follow up to a discussion on IRC #netbsd. Once again, many thanks to Riastradh :-)

In a file image there is a GPT. When I try to display it with "gpt show file.img", gpt exits with error code 1 without any further output.

Riastradh could conclude that it might be because the size of my image file is not a multiple of 512. The file was exactly one byte too big with 17179869185 bytes. After a truncate to 17179869184 the GPT was output correctly.
>How-To-Repeat:
vhost2$ doas dd if=/dev/zero of=test3.img bs=1 count=1 seek=16g
vhost2$ stat -s test3.img                                                                                   
st_dev=43011 st_ino=8479504 st_mode=0100644 st_nlink=1 st_uid=0 st_gid=0 st_rdev=-1 st_size=17179869185 st_atime=1684224662 st_mtime=1684224651 st_ctime=1684224651 st_birthtime=1684224587 st_blksize=32768 st_blocks=320 st_flags=0
vhost2$ doas vnconfig vnd0 test3.img                                                                        
vhost2$ doas gpt create vnd0
vhost2$ doas gpt show vnd0
     start      size  index  contents
         0         1         PMBR
         1         1         Pri GPT header
         2        32         Pri GPT table
        34  33554365         Unused
  33554399        32         Sec GPT table
  33554431         1         Sec GPT header
vhost2$ doas vnconfig -u vnd0
vhost2$ doas gpt show test3.img                                                                             
vhost2$ echo $?
1
vhost2$ cat truncate.c                                                                                      
#include <unistd.h> 

int main(void)
{
   truncate("test3.img", 17179869184); 
}
vhost2$ doas truncate 
vhost2$ doas gpt show test3.img
     start      size  index  contents
         0         1         PMBR
         1         1         Pri GPT header
         2        32         Pri GPT table
        34  33554365         Unused
  33554399        32         Sec GPT table
  33554431         1         Sec GPT header
>Fix:
First, I should improve my method of creating the file images. I suspect that the byte overage is due to my dd command. I would have to specify the seek parameter in bytes instead of the human readable unit and subtract one from that.

In addition, it would of course be nice if gpt could handle such a case similarly to when the secondary GPT table is not present (I seem to recall that a meaningful error message appears in that case?).

>Audit-Trail:
From: mlelstv@serpens.de (Michael van Elst)
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: bin/57410: gpt silently exits if file isn't an integral multiple of 512 bytes long
Date: Tue, 16 May 2023 05:24:32 -0000 (UTC)

 mp@petermann-it.de writes:

 >In a file image there is a GPT. When I try to display it with "gpt show file.img", gpt exits with error code 1 without any further output.

 >Riastradh could conclude that it might be because the size of my image file is not a multiple of 512. The file was exactly one byte too big with 17179869185 bytes. After a truncate to 17179869184 the GPT was output correctly.


 Indeed. There are many cases, where the gpt tool fails silently when it cannot
 read the GPT, some problems are printed with -v. This seems to be intentional.


 Maybe this (untested):

 ndex: gpt.c
 ===================================================================
 RCS file: /cvsroot/src/sbin/gpt/gpt.c,v
 retrieving revision 1.84
 diff -p -u -r1.84 gpt.c
 --- gpt.c       22 Nov 2022 00:25:52 -0000      1.84
 +++ gpt.c       16 May 2023 05:23:40 -0000
 @@ -386,8 +386,10 @@ gpt_gpt(gpt_t gpt, off_t lba, int found)
         uint32_t crc;

         hdr = gpt_read(gpt, lba, 1);
 -       if (hdr == NULL)
 +       if (hdr == NULL) {
 +               gpt_warn(gpt, "Read failed");
                 return -1;
 +       }

         if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)))
                 goto fail_hdr;
 @@ -540,6 +542,7 @@ gpt_open(const char *dev, int flags, int
                         gpt->secsz = 512;       /* Fixed size for files. */
                 if (gpt->mediasz == 0) {
                         if (gpt->sb.st_size % gpt->secsz) {
 +                               gpt_warn(gpt, "Media size not a multiple of sector size (%u)\n", gpt->secsz);
                                 errno = EINVAL;
                                 goto close;
                         }
 @@ -602,6 +605,8 @@ gpt_open(const char *dev, int flags, int
         if (gpt->fd != -1)
                 close(gpt->fd);
         free(gpt);
 +       if (!(flags & GPT_QUIET))
 +               gpt_warn("No GPT found");
         return NULL;
  }

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2023 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.