From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2201) id C062E3858410; Fri, 21 Jul 2023 12:26:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C062E3858410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689942387; bh=VuWdLn+V6VOLhWZdzaMq0K4YAzltFYxSuc5oFWJ5Axw=; h=From:To:Subject:Date:From; b=ZqxL3WorbKYyOw+JIApRCORAZiTwJ+PLJz2kQTHMmy6O398fzzCbsD7XxoqSf2JMH MT5/uZZbowaURJOR+p2cFh3z+CqXKD249yA360W5/Fe9R5eKruuWkwj/5N0xUPwcQ/ zJohpmKjrSxfqM8dla9YotMSNNL5QdhiyavZNl44= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Jon Turney To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: Fix Windows file handle leak in stat("file", -1) X-Act-Checkin: newlib-cygwin X-Git-Author: Jon Turney X-Git-Refname: refs/heads/master X-Git-Oldrev: 9fca983916a88ebb565654d639b79f2e5fdfd5a8 X-Git-Newrev: 42b44044b34d7c26abf84f8b24f820e04e1662f2 Message-Id: <20230721122627.C062E3858410@sourceware.org> Date: Fri, 21 Jul 2023 12:26:27 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D42b44044b34= d7c26abf84f8b24f820e04e1662f2 commit 42b44044b34d7c26abf84f8b24f820e04e1662f2 Author: Jon Turney Date: Mon Jul 17 16:05:01 2023 +0100 Cygwin: Fix Windows file handle leak in stat("file", -1) =20 Don't leak a Windows file handle if stat() is called with a valid filename, but invalid stat buffer pointer. =20 We do not destroy fh (which closes a Windows handle it has opened) if an exception happens in the __try block. =20 Avoid this by re-ordering things so that we don't construct the fhandler object until after we've attempted to use the struct stat buffer. =20 Fixes: 73151c54d581 ("syscalls.cc (stat_worker): Don't call build_fh_pc= with invalid pc.") Signed-off-by: Jon Turney Diff: --- winsup/cygwin/syscalls.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index 73343ecc1..32ace4d38 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -1967,12 +1967,13 @@ stat_worker (path_conv &pc, struct stat *buf) { fhandler_base *fh; =20 - if (!(fh =3D build_fh_pc (pc))) - __leave; - debug_printf ("(%S, %p, %p), file_attributes %d", pc.get_nt_native_path (), buf, fh, (DWORD) *fh); memset (buf, 0, sizeof (*buf)); + + if (!(fh =3D build_fh_pc (pc))) + __leave; + res =3D fh->fstat (buf); if (!res) fh->stat_fixup (buf);