Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/fdisk Avoid crashes with invalid or tiny sector sizes.



details:   https://anonhg.NetBSD.org/src/rev/2da888beb44f
branches:  trunk
changeset: 364646:2da888beb44f
user:      mlelstv <mlelstv%NetBSD.org@localhost>
date:      Sat Apr 02 19:15:09 2022 +0000

description:
Avoid crashes with invalid or tiny sector sizes.

diffstat:

 sbin/fdisk/fdisk.c |  11 +++++++----
 1 files changed, 7 insertions(+), 4 deletions(-)

diffs (39 lines):

diff -r 2b91af8a3183 -r 2da888beb44f sbin/fdisk/fdisk.c
--- a/sbin/fdisk/fdisk.c        Sat Apr 02 18:15:43 2022 +0000
+++ b/sbin/fdisk/fdisk.c        Sat Apr 02 19:15:09 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fdisk.c,v 1.160 2021/11/03 14:30:04 nia Exp $ */
+/*     $NetBSD: fdisk.c,v 1.161 2022/04/02 19:15:09 mlelstv Exp $ */
 
 /*
  * Mach Operating System
@@ -39,7 +39,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: fdisk.c,v 1.160 2021/11/03 14:30:04 nia Exp $");
+__RCSID("$NetBSD: fdisk.c,v 1.161 2022/04/02 19:15:09 mlelstv Exp $");
 #endif /* not lint */
 
 #define MBRPTYPENAMES
@@ -2551,6 +2551,9 @@
        if (*rfd == -1)
                errx(1, "read_disk(); fd == -1");
 
+       if (secsize <= 0)
+               errx(1, "read_disk(); secsize invalid");
+
        off_t offs = sector * (off_t)secsize;
        off_t mod = offs & (secsize - 1);
        off_t rnd = offs & ~(secsize - 1);
@@ -2558,8 +2561,8 @@
        if (lseek(*rfd, rnd, SEEK_SET) == (off_t)-1)
                return -1;
 
-       if (secsize == 512)
-               return read(*rfd, buf, 512);
+       if (secsize <= 512)
+               return read(*rfd, buf, secsize);
 
        if ((nr = read(*rfd, iobuf, secsize)) != secsize)
                return nr;



Home | Main Index | Thread Index | Old Index