os: avoid closing null fd at Fopen

In `Fopen` function variable `iop` may store NULL as a result of `fopen`
call. In this case, if later privileges couldn't be restored (`seteuid`
call fails), further `fclose(iop)` call will cause runtime error.

This commit adds check `iop` for NULL before calling `fclose` to prevent
potential NULL pointer dereference.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Mikhail Dmitrichenko <m.dmitrichenko222@gmail.com>
Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/2115>
This commit is contained in:
Mikhail Dmitrichenko 2025-12-17 11:52:16 +03:00 committed by Marge Bot
parent 7fb5e00ad8
commit f83807647e

View file

@ -1215,7 +1215,9 @@ Fopen(const char *file, const char *type)
iop = fopen(file, type); iop = fopen(file, type);
if (seteuid(euid) == -1) { if (seteuid(euid) == -1) {
fclose(iop); if (iop) {
fclose(iop);
}
return NULL; return NULL;
} }
return iop; return iop;