Repository navigation
Description
Problem
fsck (elkscmd/disk_utils/fsck.c, and the copy in busyelks/cmd/fsck.c) computes
the disk offset of an inode block as
lseek(IN, (off_t)((inode_block) * BLOCK_SIZE), SEEK_CUR);
The multiplication is done in 16-bit int before the cast, so it overflows for
inode block 32 and above (inode numbers above 1024). Those inodes are read from,
and on repair written back to, the wrong place on disk.
unmap_inode_buffer() has a second bug: it tests mapped_block > 1 where
map_inode() tests inode_block > 0, so a repaired inode block 1 (inodes 33-64)
is written back over inode block 0, which holds the root inode.
Symptoms (QEMU, current master 3e6b940)
32 MB MINIX v1 filesystem made with mfs genfs from about 2,700 files (20 MB
used). The host mfsck reports it clean.
fsck -f /dev/hdareports false "Block 264 has been used before" errors,
prints thousands of "inode count too big" warnings, then aborts with
STACK OVERFLOWafter about 13 seconds of emulated time.- Subsets of the tree with fewer than 1024 inodes check clean; a subset with
1,227 inodes fails the same way. - Repair test: with inode 40's link count set to 5 on the host,
fsck -fa
on stock ELKS leaves the filesystem unusable (host mfsck afterwards:
"root inode isn't a directory").
Fix
Compute the offset in one SEEK_SET using long arithmetic, in both the read
and write paths, which also removes the > 1 / > 0 mismatch:
lseek(IN, ((off_t)(2 + IMAPS + ZMAPS) + inode_block) * BLOCK_SIZE, SEEK_SET);
Applied to both elkscmd/disk_utils/fsck.c and elkscmd/busyelks/cmd/fsck.c.
Results with the fix
- Same 32 MB filesystem:
fsck -fcompletes cleanly, no stack warnings. - Repair test: fsck reports "Inode 40 (mode = 0100644), i_nlinks=5, counted=1",
fixes it, and host mfsck confirms the filesystem is clean afterwards.
Floppy filesystems (fewer than 1024 inodes) were not affected.
Found and tested with the help of Claude (AI assistant), using a scripted QEMU
harness. Not yet tested on real hardware.
Patch
diff --git a/elkscmd/busyelks/cmd/fsck.c b/elkscmd/busyelks/cmd/fsck.c
index da2320d..4113a28 100644
--- a/elkscmd/busyelks/cmd/fsck.c
+++ b/elkscmd/busyelks/cmd/fsck.c
@@ -177,12 +177,11 @@ void unmap_inode_buffer()
printd("none mapped\n");
return;
}
- /* Seek to after the Superblock, inode maps and zone maps */
- lseek(IN, (off_t)((2 + IMAPS + ZMAPS) * BLOCK_SIZE), SEEK_SET);
- /* Seek to the block we have to write */
- if (mapped_block > 1) {
- lseek(IN, (off_t)((mapped_block) * BLOCK_SIZE), SEEK_CUR);
- }
+ /* Seek past the superblock, inode maps and zone maps to the inode block.
+ * Use long arithmetic: block * BLOCK_SIZE overflows a 16-bit int past
+ * inode block 31 (inode 1024).
+ */
+ lseek(IN, ((off_t)(2 + IMAPS + ZMAPS) + mapped_block) * BLOCK_SIZE, SEEK_SET);
if (BLOCK_SIZE != write(IN, inode_buffer, BLOCK_SIZE)) {
die("Unable to write inodes");
}
@@ -231,12 +230,8 @@ struct minix_inode * map_inode(unsigned int nr)
/* WRITE OLD INODE BLOCK BACK TO DISK */
unmap_inode_buffer();
}
- /* Seek to after the Superblock, inode maps and zone maps */
- lseek(IN, (off_t)((2 + IMAPS + ZMAPS) * BLOCK_SIZE), SEEK_SET);
- /* Seek to the block we have to write */
- if (inode_block > 0) {
- lseek(IN, (off_t)((inode_block) * BLOCK_SIZE), SEEK_CUR);
- }
+ /* Seek past the superblock, inode maps and zone maps to the inode block */
+ lseek(IN, ((off_t)(2 + IMAPS + ZMAPS) + inode_block) * BLOCK_SIZE, SEEK_SET);
/* READ In NEW BLOCK FROM DISK */
if (BLOCK_SIZE != read(IN, inode_buffer, BLOCK_SIZE)) {
die("Unable to read inodes");
diff --git a/elkscmd/disk_utils/fsck.c b/elkscmd/disk_utils/fsck.c
index b7fd2ac..0a28e39 100644
--- a/elkscmd/disk_utils/fsck.c
+++ b/elkscmd/disk_utils/fsck.c
@@ -222,12 +222,11 @@ void unmap_inode_buffer()
printd("none mapped\n");
return;
}
- /* Seek to after the Superblock, inode maps and zone maps */
- lseek(IN, (off_t)((2 + IMAPS + ZMAPS) * BLOCK_SIZE), SEEK_SET);
- /* Seek to the block we have to write */
- if (mapped_block > 1) {
- lseek(IN, (off_t)((mapped_block) * BLOCK_SIZE), SEEK_CUR);
- }
+ /* Seek past the superblock, inode maps and zone maps to the inode block.
+ * Use long arithmetic: block * BLOCK_SIZE overflows a 16-bit int past
+ * inode block 31 (inode 1024).
+ */
+ lseek(IN, ((off_t)(2 + IMAPS + ZMAPS) + mapped_block) * BLOCK_SIZE, SEEK_SET);
if (BLOCK_SIZE != write(IN, inode_buffer, BLOCK_SIZE)) {
die("Unable to write inodes");
}
@@ -277,12 +276,8 @@ struct minix_inode * map_inode(unsigned int nr)
/* WRITE OLD INODE BLOCK BACK TO DISK */
unmap_inode_buffer();
}
- /* Seek to after the Superblock, inode maps and zone maps */
- lseek(IN, (off_t)((2 + IMAPS + ZMAPS) * BLOCK_SIZE), SEEK_SET);
- /* Seek to the block we have to write */
- if (inode_block > 0) {
- lseek(IN, (off_t)((inode_block) * BLOCK_SIZE), SEEK_CUR);
- }
+ /* Seek past the superblock, inode maps and zone maps to the inode block */
+ lseek(IN, ((off_t)(2 + IMAPS + ZMAPS) + inode_block) * BLOCK_SIZE, SEEK_SET);
/* READ In NEW BLOCK FROM DISK */
if (BLOCK_SIZE != read(IN, inode_buffer, BLOCK_SIZE)) {
die("Unable to read inodes");
Activity
ghaerr commentedon Nov 15, 2026
Hello @proxybarrio,
Wow, thanks for this. Another integer overflow bug and in
fsckof all things!Super catch and thanks for the fix as well.
Do you happen to have a shell script to create a large number of inuse inodes using
mfs genfs? That would allow me to quickly duplicate this before fixing. If not, I'll create one.Geez, and a second bug found. In some ways amazing we've never seen this bug before, but it looks like possibly the "if > 0 / if > 1" tests were added after the fact to fix a signed vs unsigned int bug on overflow which wasn't identified as such. I'll be looking at git blame to see if this is what happened. I don't yet fully understand why the if test was even needed, other than guessing it prohibited problems (and introduced others) when inode_block or mapped_block was negative (i.e. > 32767).
Obviously all of this is also potentially problematic on 65M HDs where unsigned int block numbers can be interpreted as negative numbers when improperly handled. Do you think we need to test on larger 65M HDs also, with huge incorrect data or inode block numbers (e.g. > 32767)?
I'll create a PR from a review of the patch and post it.
Thank you!
alci/tests/fsck: large MINIX v1 fsck tests for ghaerr/elks ghaerr#2851 …
proxybarrio commentedon Nov 17, 2026
Hello Greg,
First of all, thank you, and genuinely so, for the speed and the warmth of your reply: I had barely closed the laptop on Sunday when your answer was already sitting there, and PR #2853 followed within the hour, which, for a bug that had been quietly lurking in fsck for who knows how many years, is a rather satisfying turn of events. I have since tested the head of the PR (2c2bf6b), and I can confirm that it fixes both problems, the 16-bit overflow and the
> 1/> 0mismatch, cleanly and completely.As for the script you asked for, here it is. One small trap worth knowing about, which cost me an attempt or two before I understood what was going on: if the filler file is made of zeros,
mfsvery sensibly stores it as holes, and the small files end up at the bottom of the disk instead of the top. Random data solves it. The filler goes in first withgenfs, then the small files are added one by one, so that their data lands at the high zone numbers we actually want to exercise:For instance,
mkinodes.sh many.img 3000 65535 40000produces a 64 MB filesystem, the ceiling for MINIX v1, with some 3,000 inodes in use and file data reaching all the way up to zone 43249.Your question about block numbers above 32767 was a very good one, and it seemed only fair to answer it with tests rather than opinions. So I ran four of them on 64 MB filesystems (65535 blocks) in QEMU, with the image mounted as a second disk, and then, for good measure and a second pair of eyes, checked every single result on the host with
fsck.minix -ffrom util-linux:fsck -ffsck -ffsck -fafsck -faThe picture, I think, is reassuring. Zone numbers above 32767 behave perfectly well on their own, in checking as in repair, since
test_bitandset_bittake unsigned arguments andread_blockandwrite_blockalready work with long offsets. Trouble begins only past 1024 inodes, which is precisely the door your PR closes. On the zone side I found no second overflow hiding in the shadows.Short version: the fix holds, even in the worst case.
All of this was tested with the help of Claude, using scripted QEMU and 86Box harnesses; real hardware is still to come, and I will report back once my Toshiba T1200 gets its turn.
Thanks again, and all the best, anzi, ciao!
Lele
ghaerr commentedon Nov 17, 2026
Hello @proxybarrio,
I just looked a little further into your alci branch, wow you have been busy! Nice name. Are you in Italy, where at? I've been over three times this summer, and trying to learn Italian. No parlo molto...
I like all the scripts you've put together in alci/tests. ALCI itself looks very interesting, along with its goals. I'll dive in further after the current activity and interest on ELKS dies down a bit!
Thanks for the location of the scripts. I haven't had time to run them yet, but appreciate all the testing you've done so I'm going to commit #2853 now, and the scripts later, as I'm still interested in playing with this. It sounds like I need to run
mkinodes.sh many.img 3000 65535 40000first, thencorrupt.py many.img 1024, then boot ELKS and fsck the image, right?Yes. I got caught up in that myself a while back when I forgot that MINIX optimizes all-zero blocks as a 16-bit zero in the inode block tables, which the kernel then artificially expands on reads or allocates an actual block on writes. For quite a while I couldn't figure out why so much disk space was left on my test MINIX volume when creating large files using /dev/zero... :)
Thanks for all your testing, it is very reassuring.
ALCI goal 1: ELKS speed ups and min RAM - I remain very interested in your thoughts!
Regarding your ALCI goals 2 and 3: you might check out my sister project Microwindows/Nano-X which we have running on ELKS; it's got a terminal emulator that could run
minitermto talk to Claude via the serial port. Very recently, someone wrote a Windows 16 NE loader that runs unmodified 16-bit Windows binaries on ELKS, discussed in more detail in #2855 which you might find interesting. I sure do. Or are you have some PC/GEOS source that you're thinking of porting?Thank you!
proxybarrio commentedon Nov 17, 2026
alci/tests/fsck: large MINIX v1 fsck tests for ghaerr/elks ghaerr#2851 …
ghaerr commentedon Nov 17, 2026
Hello @proxybarrio!
Thanks for your further explanations.
I've been looking through each commit - very interesting! I can see some are more oriented for eventual inclusion in upstream ELKS, while others are pretty much fast speed hacks, which of course is your goal. I find it all very interesting, especially learning where various speed bumps are. For instance, it makes sense, now that you've proved it, that running all TTY I/O through character queues is very time consuming on 8088 hardware, which is known slow for that kind of thing along with all the unnecessary function calls. More modern CPUs blaze through looping code without us programmers even thinking much of it.
Anyways, lots to learn and think about looking through your ALCI project, a lot of ground has been covered in the various commits. I'm super glad you submitted the fsck fix since I hadn't previously known about you or your project. I'll spend more time looking through the whole of it and let it settle, as there's also a number of other recent larger kernel enhancements others have been making, so being the old-fashioned human programmer that I am, I need more time to think about it :)
FYI:
nxtermis the actual terminal emulator - it runs a terminal shell as a Nano-X client and emulates lots of ANSI sequences.minitermis essentially a raw-mode keyboard <-> serial terminal, with no terminal emulation at all.Ciao!