Skip to content

Commit d37c711

Browse files
perseantperseant
authored andcommitted
Check partial-segment magic number when determining whether a header is valid.
Consider a superblock valid if it has an LFS64 magic number, not just an LFS32 magic number.
1 parent d008d7e commit d37c711

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

usr.sbin/dumplfs/dumplfs.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: dumplfs.c,v 1.67 2025/09/14 19:09:11 perseant Exp $ */
1+
/* $NetBSD: dumplfs.c,v 1.68 2025/10/17 02:47:33 perseant Exp $ */
22

33
/*-
44
* Copyright (c) 1991, 1993
@@ -40,7 +40,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
4040
#if 0
4141
static char sccsid[] = "@(#)dumplfs.c 8.5 (Berkeley) 5/24/95";
4242
#else
43-
__RCSID("$NetBSD: dumplfs.c,v 1.67 2025/09/14 19:09:11 perseant Exp $");
43+
__RCSID("$NetBSD: dumplfs.c,v 1.68 2025/10/17 02:47:33 perseant Exp $");
4444
#endif
4545
#endif /* not lint */
4646

@@ -773,16 +773,33 @@ dump_segment(int fd, int segnum, daddr_t addr, struct lfs *lfsp, int dump_sb)
773773
sb = 0;
774774
did_one = 0;
775775
do {
776+
const char *whyfailed = NULL;
777+
776778
get(fd, sum_offset, sumblock, lfs_sb_getsumsize(lfsp));
777779
sump = (SEGSUM *)sumblock;
778780
sumstart = lfs_ss_getsumstart(lfsp);
779-
if ((lfs_sb_getversion(lfsp) > 1 &&
780-
lfs_ss_getident(lfsp, sump) != lfs_sb_getident(lfsp)) ||
781-
lfs_ss_getsumsum(lfsp, sump) !=
782-
cksum((char *)sump + sumstart,
783-
lfs_sb_getsumsize(lfsp) - sumstart)) {
781+
if (whyfailed == NULL && lfs_ss_getmagic(lfsp, sump) !=
782+
SS_MAGIC) {
783+
whyfailed = "bad magic number";
784+
printf("Found magic number 0x%lx expecting 0x%lx\n",
785+
(unsigned long)lfs_ss_getmagic(lfsp, sump),
786+
(unsigned long)SS_MAGIC);
787+
}
788+
if (whyfailed == NULL && lfs_ss_getsumsum(lfsp, sump) !=
789+
cksum((char *)sump + sumstart,
790+
lfs_sb_getsumsize(lfsp) - sumstart))
791+
whyfailed = "bad sumsum";
792+
if (whyfailed == NULL && lfs_sb_getversion(lfsp) > 1 &&
793+
lfs_ss_getident(lfsp, sump) != lfs_sb_getident(lfsp))
794+
whyfailed = "bad ident";
795+
if (whyfailed != NULL) {
796+
uint32_t sbmagic;
797+
784798
sbp = (struct lfs *)sump;
785-
if ((sb = (sbp->lfs_dlfs_u.u_32.dlfs_magic == LFS_MAGIC))) {
799+
sbmagic = sbp->lfs_dlfs_u.u_32.dlfs_magic;
800+
801+
if ((sb = (!lfsp->lfs_is64 && sbmagic == LFS_MAGIC)
802+
|| (lfsp->lfs_is64 && sbmagic == LFS64_MAGIC))) {
786803
printf("Superblock at 0x%x\n",
787804
(unsigned)lfs_btofsb(lfsp, sum_offset));
788805
if (dump_sb) {
@@ -799,8 +816,8 @@ dump_segment(int fd, int segnum, daddr_t addr, struct lfs *lfsp, int dump_sb)
799816
} else if (did_one)
800817
break;
801818
else {
802-
printf("Segment at 0x%llx empty or corrupt\n",
803-
(long long)addr);
819+
printf("Segment at 0x%llx empty or corrupt (%s)\n",
820+
(long long)addr, whyfailed);
804821
break;
805822
}
806823
} else {

0 commit comments

Comments
 (0)