NetBSD Problem Report #56530

From www@netbsd.org  Wed Dec  1 09:39:32 2021
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 0CF811A921F
	for <gnats-bugs@gnats.NetBSD.org>; Wed,  1 Dec 2021 09:39:32 +0000 (UTC)
Message-Id: <20211201093930.621CD1A9239@mollari.NetBSD.org>
Date: Wed,  1 Dec 2021 09:39:30 +0000 (UTC)
From: rvp@SDF.ORG
Reply-To: rvp@SDF.ORG
To: gnats-bugs@NetBSD.org
Subject: fstyp(8): Segmentation fault in the HAMMER2 filesystem check
X-Send-Pr-Version: www-1.0

>Number:         56530
>Category:       bin
>Synopsis:       fstyp(8): Segmentation fault in the HAMMER2 filesystem check
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Dec 01 09:40:00 +0000 2021
>Last-Modified:  Thu Dec 02 14:30:04 +0000 2021
>Originator:     RVP
>Release:        NetBSD/9.99.92
>Organization:
>Environment:
NetBSD x202e.localdomain 9.99.92 NetBSD 9.99.92 (MYKERNEL) #0: Wed Dec  1 00:05:59 UTC 2021  bld@x202e.localdomain:/usr/obj/usr/src/sys/arch/amd64/compile/MYKERNEL amd64
>Description:
fstyp(8) dumps core in the HAMMER2 filesystem check when read_voldata()
returns NULL--because ftell() returns 0 for /dev/dk* wedges and block
devices.

Before:

# dkctl wd0 listwedges | fgrep dk7
dk7: FreeBSD_swap, 8390656 blocks at 106973184, type: swap
# fstyp /dev/dk7
Segmentation fault (core dumped)
#

After:

# fstyp /dev/dk7
fstyp: /dev/dk7: filesystem not recognized: Device busy

Also affects NetBSD-9.2.
>How-To-Repeat:
See above.
>Fix:
diff -urN usr.sbin/fstyp.orig/hammer2.c usr.sbin/fstyp/hammer2.c
--- usr.sbin/fstyp.orig/hammer2.c	2021-01-10 13:44:57.000000000 +0000
+++ usr.sbin/fstyp/hammer2.c	2021-11-30 07:36:22.198347288 +0000
@@ -40,9 +40,40 @@
 #include "fstyp.h"
 #include "hammer2_disk.h"

+#ifdef __NetBSD__
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/disk.h>
+#include <sys/stat.h>
+
+static ssize_t
+get_nbsd_file_size(FILE* fp)
+{
+	struct dkwedge_info dkw;
+	ssize_t siz;
+	int fd;
+
+	if ((fd = fileno(fp)) == -1) {
+		warnx("hammer2: invalid file descriptor");
+		return -1;
+	}
+	if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
+		return (ssize_t)(dkw.dkw_size * DEV_BSIZE);
+	}
+	if ((siz = lseek(fd, 0, SEEK_END)) == -1) {
+		warnx("hammer2: lseek failed");
+		return -1;
+	}
+	return siz;
+}
+#endif
+
 static ssize_t
 get_file_size(FILE *fp)
 {
+#ifdef __NetBSD__
+	return get_nbsd_file_size(fp);
+#else
 	ssize_t siz;

 	if (fseek(fp, 0, SEEK_END) == -1) {
@@ -57,6 +88,7 @@
 	}

 	return (siz);
+#endif
 }

 static hammer2_volume_data_t *
@@ -386,6 +418,8 @@
 	hammer2_volume_data_t *voldata = read_voldata(fp, 0);
 	int error = 1;

+	if (voldata == NULL)
+		goto fail;
 	if (voldata->volu_id != HAMMER2_ROOT_VOLUME)
 		goto fail;
 	if (voldata->nvolumes != 0)

>Audit-Trail:
From: RVP <rvp@SDF.ORG>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: bin/56530: fstyp(8): Segmentation fault in the HAMMER2 filesystem
 check
Date: Wed, 1 Dec 2021 19:27:23 +0000 (UTC)

 One more fix:

 # dkctl wd0 listwedges | fgrep dk1:
 dk1: Basic data partition, 1228800 blocks at 616448, type:

 Before:

 # fstyp -l /dev/dk1
 fstyp: ntfs: Could not open iconv: Invalid argument
 ntfs
 #

 After:

 # fstyp -l /dev/dk1
 ntfs Recovery
 #

 ---START---
 diff -urN usr.sbin/fstyp.orig/ntfs.c usr.sbin/fstyp/ntfs.c
 --- usr.sbin/fstyp.orig/ntfs.c	2019-12-28 08:22:30.000000000 +0000
 +++ usr.sbin/fstyp/ntfs.c	2021-12-01 19:12:46.956462159 +0000
 @@ -39,6 +39,7 @@

   #include <err.h>
   #include <iconv.h>
 +#include <langinfo.h>
   #include <stdint.h>
   #include <stdio.h>
   #include <stdlib.h>
 @@ -108,8 +109,7 @@
   	iconv_t cd;
   	size_t rc;

 -	/* dstname="" means convert to the current locale. */
 -	cd = iconv_open("", NTFS_ENC);
 +	cd = iconv_open(nl_langinfo(CODESET), NTFS_ENC);
   	if (cd == (iconv_t)-1) {
   		warn("ntfs: Could not open iconv");
   		return;
 ---END---

 Thanks,
 -RVP

 PS. In the previous patch, there's no need to `#include <sys/stat.h>'.
 Please remove it.

From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56530 CVS commit: src/usr.sbin/fstyp
Date: Thu, 2 Dec 2021 09:26:13 -0500

 Module Name:	src
 Committed By:	christos
 Date:		Thu Dec  2 14:26:12 UTC 2021

 Modified Files:
 	src/usr.sbin/fstyp: ntfs.c

 Log Message:
 PR/56530: RVP: fix iconv open error.


 To generate a diff of this commit:
 cvs rdiff -u -r1.2 -r1.3 src/usr.sbin/fstyp/ntfs.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Christos Zoulas" <christos@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/56530 CVS commit: src/usr.sbin/fstyp
Date: Thu, 2 Dec 2021 09:26:42 -0500

 Module Name:	src
 Committed By:	christos
 Date:		Thu Dec  2 14:26:42 UTC 2021

 Modified Files:
 	src/usr.sbin/fstyp: hammer2.c

 Log Message:
 PR/56530: RVP: use ioctl to get the partition size if possible (avoids SEGV)


 To generate a diff of this commit:
 cvs rdiff -u -r1.8 -r1.9 src/usr.sbin/fstyp/hammer2.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.