Bug 12471

Summary: fdisk for mediums with a block size != 512 bytes
Product: Busybox Reporter: Frank Mehnert <frank.mehnert>
Component: OtherAssignee: unassigned
Status: NEW ---    
Severity: normal CC: busybox-cvs
Priority: P5    
Version: 1.33.x   
Target Milestone: ---   
Hardware: All   
OS: Linux   
Host: Target:
Build:

Description Frank Mehnert 2020-01-14 19:20:04 UTC
util-linux/fdisk.c: Function bb_BLKGETSIZE_sectors() returns the number of sectors and always assumes that a sector size is 512 bytes, at least if ioctl(BLKGETSIZE64) works. This isn't necessarily true. Fdisk can also deal with sector sizes of 1024, 2048 or 4096 bytes and the case for ioctl(BLKGETSIZE) does it right: It divides the size by the sector_size as opposed to ">> 9" in the first case.

Suggested fix:

diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index e58cb0fd1..c1350539b 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -501,7 +501,7 @@ static sector_t bb_BLKGETSIZE_sectors(int fd)

        if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
                /* Got bytes, convert to 512 byte sectors */
-               v64 >>= 9;
+               v64 /= sector_size;
                if (v64 != (sector_t)v64) {
  ret_trunc:
                        /* Not only DOS, but all other partition tables


There are other places in util-linux/fdisk.c which assume that sector_size is 512 bytes, for example list_disk_geometry() where total_number_of_sectors is used