From 2a03d5f7484c272d3c6c1c7a25b842678e463e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Omar=20Yi=C4=9Fitcan=20Meng=C3=BC?= Date: Mon, 23 Nov 2026 23:45:45 +0300 Subject: [PATCH] DosForceDup: validate NewHandle against ps_maxfiles and handle self-dup 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 #261 --- kernel/dosfns.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/dosfns.c b/kernel/dosfns.c index 312cbfa1..10588e7f 100644 --- a/kernel/dosfns.c +++ b/kernel/dosfns.c @@ -689,6 +689,12 @@ COUNT DosForceDup(unsigned OldHandle, unsigned NewHandle) if ((Sftp = get_sft(OldHandle)) == (sft FAR *) - 1) return DE_INVLDHNDL; + if (NewHandle >= p->ps_maxfiles) + return DE_INVLDHNDL; + + if (OldHandle == NewHandle) + return SUCCESS; + /* now close the new handle if it's open */ if ((UBYTE) p->ps_filetab[NewHandle] != 0xff) {