Skip to content

DosForceDup: validate NewHandle against ps_maxfiles and handle self-dup - #279

Open
CmdSuv wants to merge 1 commit into
FDOS:masterfrom
CmdSuv:fix-dosforcedup-newhandle-validation
Open

CmdSuv wants to merge 1 commit into
FDOS:masterfrom
CmdSuv:fix-dosforcedup-newhandle-validation

Conversation

@CmdSuv

@CmdSuv CmdSuv commented Nov 23, 2026

Copy link
Copy Markdown

Summary

DosForceDup() (INT 21h / AH=46h) in kernel/dosfns.c validated OldHandle via get_sft(), but never validated NewHandle against p->ps_maxfiles and did not short-circuit when OldHandle == NewHandle:

  1. Out-of-bounds JFT access: For any caller-supplied NewHandle >= p->ps_maxfiles, p->ps_filetab[NewHandle] reads out of bounds in the PSP segment. Whenever that out-of-bounds byte happens to be 0xff, DosForceDup() overwrites it with p->ps_filetab[OldHandle], leaks an SFT reference count (Sftp->sft_count += 1), and returns SUCCESS instead of DE_INVLDHNDL.
  2. dup2(h, h) closes open handles: When OldHandle == NewHandle on an open handle, DosClose(NewHandle) closes the handle (ps_filetab[h] = 0xff, decrements sft_count), after which DosForceDup() copies 0xff back onto itself and increments Sftp->sft_count on the closed SFT while returning SUCCESS.

Fix

  • Check if (NewHandle >= p->ps_maxfiles) return DE_INVLDHNDL; (matching get_sft_idx() and other JFT lookups in the kernel).
  • Check if (OldHandle == NewHandle) return SUCCESS; after get_sft(OldHandle) has verified that OldHandle is a valid open handle.

Verification

Tested locally with Open Watcom (COMPILER=owlinux) and a 16-bit x86 NASM .COM test program booted under qemu-system-i386:

Before fix (master):

T1  dup2(h,h): FAIL (handle closed! read AX=0x0006)
T2A dup2(h,0x6A OOB sentinel): FAIL (CF=0! OOB write corrupted [0x82] to 0x0005)

After fix:

T1  dup2(h,h): PASS (handle kept open & read AAAA)
T2A dup2(h,0x6A OOB sentinel): PASS (CF=1 AX=6, sentinel=0xFF intact)
T2B dup2(h,20 boundary): PASS (CF=1 AX=6)
T2C dup2(h,0xFFFF wrap): PASS (CF=1 AX=6)
T3  dup2(15,15 invalid): PASS (CF=1 AX=6)
T4A dup2(h1,10 free slot): PASS (read AAAA)
T4B dup2(h1,h2 open slot): PASS (closed B, read AAAA)
  • Official ./test/test.sh QEMU boot test: boot wc: success
  • Fork GitHub Actions CI (ia16-elf-gcc, OpenWatcom Linux, OpenWatcom DOS + ./ci_test.sh): passed

Fixes #261

DosForceDup() (INT 21h/AH=46h) validated OldHandle via get_sft(), but
never checked NewHandle against p->ps_maxfiles and did not short-circuit
when OldHandle == NewHandle.

1. For any caller-supplied NewHandle >= p->ps_maxfiles, indexing
   p->ps_filetab[NewHandle] read out of bounds in the PSP segment and,
   whenever that byte was 0xFF, wrote the SFT index out of bounds and
   leaked an SFT reference count while returning SUCCESS instead of
   DE_INVLDHNDL.
2. When OldHandle == NewHandle on an open handle, DosClose(NewHandle)
   closed the handle and decremented sft_count, after which the function
   copied 0xFF back onto itself and incremented sft_count on the closed
   SFT while returning SUCCESS.

Check NewHandle >= p->ps_maxfiles (returning DE_INVLDHNDL) and return
SUCCESS immediately when OldHandle == NewHandle (after OldHandle has
been validated by get_sft()).

Fixes FDOS#261

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DosForceDup (INT 21h/AH=46h) does not validate NewHandle: out-of-bounds JFT access + dup2(h,h) closes the handle

1 participant