Skip to contentSkip to conversation

fdisk: sgi: abort on HDIO_GETGEO failure

PATCH Applied Davidlohr Bueso 3 participants 4 messages May 6, 2012 – May 10, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 5/5 fdisk: sgi: abort on HDIO_GETGEO failure

From: Davidlohr Bueso <[email protected]>

When the HDIO_GETGEO ioctl fails on non IRIX/MIPS platforms (ie: inappropriate ioctl for device) the variables that describe the geometry are compromissed. One clear example is a division by 0 bug because the cylinder size is 0 is verify_sgi():

$> fdisk sgi.img
Welcome to fdisk (util-linux 2.21.392-4b1c).

...

Command (m for help): x

Expert command (m for help): g
Building a new SGI disklabel.
Partition 11 of type SGI volume and of size 7.9 MiB is set Floating point exception

Fix this by simply exiting the program instead of leaving it in a vulnerable state.

---
 fdisk/fdisksgilabel.c |   39 ++++++++++++++++++++-------------------
 1 files changed, 20 insertions(+), 19 deletions(-)
fdisk/fdisksgilabel.c +20 -19
index 822f55d..b61f102 100644
@@ -705,25 +705,26 @@ create_sgilabel(void)
 	res = blkdev_get_sectors(fd, &llsectors);
 
 #ifdef HDIO_GETGEO
-	if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
-		heads = geometry.heads;
-		sectors = geometry.sectors;
-		if (res == 0) {
-			/* the get device size ioctl was successful */
-			unsigned long long llcyls;
-			llcyls = llsectors / (heads * sectors * sec_fac);
-			cylinders = llcyls;
-			if (cylinders != llcyls)	/* truncated? */
-				cylinders = ~0;
-		} else {
-			/* otherwise print error and use truncated version */
-			cylinders = geometry.cylinders;
-			fprintf(stderr,
-				_("Warning:  BLKGETSIZE ioctl failed on %s.  "
-				  "Using geometry cylinder value of %d.\n"
-				  "This value may be truncated for devices"
-				  " > 33.8 GB.\n"), disk_device, cylinders);
-		}
+	if (ioctl(fd, HDIO_GETGEO, &geometry) < 0)
+		err(EXIT_FAILURE, _("ioctl failed on %s"), disk_device);
+
+	heads = geometry.heads;
+	sectors = geometry.sectors;
+	if (res == 0) {
+		/* the get device size ioctl was successful */
+		unsigned long long llcyls;
+		llcyls = llsectors / (heads * sectors * sec_fac);
+		cylinders = llcyls;
+		if (cylinders != llcyls)	/* truncated? */
+			cylinders = ~0;
+	} else {
+		/* otherwise print error and use truncated version */
+		cylinders = geometry.cylinders;
+		fprintf(stderr,
+			_("Warning:  BLKGETSIZE ioctl failed on %s.  "
+			  "Using geometry cylinder value of %d.\n"
+			  "This value may be truncated for devices"
+			  " > 33.8 GB.\n"), disk_device, cylinders);
 	}
 #endif
 	for (i = 0; i < 4; i++) {
Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 6 lines · Davidlohr Bueso

From: Davidlohr Bueso <[email protected]>

When the HDIO_GETGEO ioctl fails on non IRIX/MIPS platforms (ie:
inappropriate ioctl for device) the variables that describe the geometry are compromissed. One clear example is a division by 0 bug because the cylinder size is 0 is verify_sgi():

I agree with the change, however I think that more descriptive error message should be printed; see below.

Quoted 39 lines

$> fdisk sgi.img
Welcome to fdisk (util-linux 2.21.392-4b1c).

...

Command (m for help): x

Expert command (m for help): g
Building a new SGI disklabel.
Partition 11 of type SGI volume and of size 7.9 MiB is set Floating point exception

Fix this by simply exiting the program instead of leaving it in a vulnerable state.

---
 fdisk/fdisksgilabel.c |   39 ++++++++++++++++++++-------------------
 1 files changed, 20 insertions(+), 19 deletions(-)
fdisk/fdisksgilabel.c +0 -11
index 822f55d..b61f102 100644
@@ -705,25 +705,26 @@ create_sgilabel(void)
 	res = blkdev_get_sectors(fd, &llsectors);
 
 #ifdef HDIO_GETGEO
-	if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
-		heads = geometry.heads;
-		sectors = geometry.sectors;
-		if (res == 0) {
-			/* the get device size ioctl was successful */
-			unsigned long long llcyls;
-			llcyls = llsectors / (heads * sectors * sec_fac);
-			cylinders = llcyls;
-			if (cylinders != llcyls)	/* truncated? */
-				cylinders = ~0;
-		} else {

fdisk/fdisksgilabel.c create_sgilabel(void) · line 709

Quoted 10 lines
fdisk/fdisksgilabel.c +2 -8
-			/* otherwise print error and use truncated version */
-			cylinders = geometry.cylinders;
-			fprintf(stderr,
-				_("Warning:  BLKGETSIZE ioctl failed on %s.  "
-				  "Using geometry cylinder value of %d.\n"
-				  "This value may be truncated for devices"
-				  " > 33.8 GB.\n"), disk_device, cylinders);
-		}
+	if (ioctl(fd, HDIO_GETGEO, &geometry) < 0)
+		err(EXIT_FAILURE, _("ioctl failed on %s"), disk_device);

s/"ioctl failed..."/"HDIO_GETGEO ioctl failed..."

[SNIP]

Petr

--
Petr Uzel
IRC: ptr_uzl @ freenode

#2 lore raw
Quoted 54 lines · Petr Uzel
Quoted 6 lines · Davidlohr Bueso

From: Davidlohr Bueso <[email protected]>

When the HDIO_GETGEO ioctl fails on non IRIX/MIPS platforms (ie:
inappropriate ioctl for device) the variables that describe the geometry are compromissed. One clear example is a division by 0 bug because the cylinder size is 0 is verify_sgi():

I agree with the change, however I think that more descriptive error message should be printed; see below.

Quoted 43 lines

$> fdisk sgi.img
Welcome to fdisk (util-linux 2.21.392-4b1c).

...

Command (m for help): x

Expert command (m for help): g
Building a new SGI disklabel.
Partition 11 of type SGI volume and of size 7.9 MiB is set Floating point exception

Fix this by simply exiting the program instead of leaving it in a vulnerable state.

---
 fdisk/fdisksgilabel.c |   39 ++++++++++++++++++++-------------------
 1 files changed, 20 insertions(+), 19 deletions(-)
fdisk/fdisksgilabel.c +0 -15
index 822f55d..b61f102 100644
@@ -705,25 +705,26 @@ create_sgilabel(void)
 	res = blkdev_get_sectors(fd, &llsectors);
 
 #ifdef HDIO_GETGEO
-	if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
-		heads = geometry.heads;
-		sectors = geometry.sectors;
-		if (res == 0) {
-			/* the get device size ioctl was successful */
-			unsigned long long llcyls;
-			llcyls = llsectors / (heads * sectors * sec_fac);
-			cylinders = llcyls;
-			if (cylinders != llcyls)	/* truncated? */
-				cylinders = ~0;
-		} else {
-			/* otherwise print error and use truncated version */
-			cylinders = geometry.cylinders;
-			fprintf(stderr,
-				_("Warning:  BLKGETSIZE ioctl failed on %s.  "
Quoted 8 lines

fdisk/fdisksgilabel.c create_sgilabel(void) · line 709

Quoted 6 lines
fdisk/fdisksgilabel.c +2 -4
-				  "Using geometry cylinder value of %d.\n"
-				  "This value may be truncated for devices"
-				  " > 33.8 GB.\n"), disk_device, cylinders);
-		}
+	if (ioctl(fd, HDIO_GETGEO, &geometry) < 0)
+		err(EXIT_FAILURE, _("ioctl failed on %s"), disk_device);

s/"ioctl failed..."/"HDIO_GETGEO ioctl failed..."

I don't care either way - perhaps even a "cannot get geometry" message. Karel, its up to you.

Thanks for looking into this patchet!

- Davidlohr

Quoted 8 lines

[SNIP]

Petr

--
Petr Uzel
IRC: ptr_uzl @ freenode

maintainer Applied
#3 lore raw
In reply to Davidlohr Bueso
Quoted 2 lines · Davidlohr Bueso
 fdisk/fdisksgilabel.c |   39 ++++++++++++++++++++-------------------
 1 files changed, 20 insertions(+), 19 deletions(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com