From 3fda6e425aab23dcb18dd460348d27e98876307b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 4 Dec 2023 15:14:40 +0800 Subject: [PATCH] Global: never leak fd to child processes --- src/common/io/io_unix.c | 8 ++++---- src/detection/battery/battery_bsd.c | 4 ++-- src/detection/brightness/brightness_bsd.c | 2 +- src/logo/image/image.c | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/common/io/io_unix.c b/src/common/io/io_unix.c index c3f592e69..e4b9e62e1 100644 --- a/src/common/io/io_unix.c +++ b/src/common/io/io_unix.c @@ -27,7 +27,7 @@ static void createSubfolders(const char* fileName) bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data) { - int openFlagsModes = O_WRONLY | O_CREAT | O_TRUNC; + int openFlagsModes = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC; int openFlagsRights = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; int FF_AUTO_CLOSE_FD fd = open(fileName, openFlagsModes, openFlagsRights); @@ -87,7 +87,7 @@ bool ffAppendFDBuffer(int fd, FFstrbuf* buffer) ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data) { - int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY); + int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY | O_CLOEXEC); if(fd == -1) return -1; @@ -96,7 +96,7 @@ ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data) bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer) { - int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY); + int FF_AUTO_CLOSE_FD fd = open(fileName, O_RDONLY | O_CLOEXEC); if(fd == -1) return false; @@ -199,7 +199,7 @@ bool ffSuppressIO(bool suppress) origOut = dup(STDOUT_FILENO); origErr = dup(STDERR_FILENO); - nullFile = open("/dev/null", O_WRONLY); + nullFile = open("/dev/null", O_WRONLY | O_CLOEXEC); init = true; } diff --git a/src/detection/battery/battery_bsd.c b/src/detection/battery/battery_bsd.c index 52ce13d01..3edba2a62 100644 --- a/src/detection/battery/battery_bsd.c +++ b/src/detection/battery/battery_bsd.c @@ -20,9 +20,9 @@ const char* ffDetectBattery(FF_MAYBE_UNUSED FFBatteryOptions* options, FFlist* r if(units == 0) return NULL; - FF_AUTO_CLOSE_FD int acpifd = open("/dev/acpi", O_RDONLY); + FF_AUTO_CLOSE_FD int acpifd = open("/dev/acpi", O_RDONLY | O_CLOEXEC); if(acpifd < 0) - return "open(\"/dev/acpi\", O_RDONLY) failed"; + return "open(\"/dev/acpi\", O_RDONLY | O_CLOEXEC) failed"; for(int i = 0; i < units; ++i) { diff --git a/src/detection/brightness/brightness_bsd.c b/src/detection/brightness/brightness_bsd.c index 71bf8c910..34b91ab5c 100644 --- a/src/detection/brightness/brightness_bsd.c +++ b/src/detection/brightness/brightness_bsd.c @@ -18,7 +18,7 @@ const char* ffDetectBrightness(FF_MAYBE_UNUSED FFBrightnessOptions* options, FFl { path[sizeof(path) - 2] = i; - FF_AUTO_CLOSE_FD int blfd = open(path, O_RDONLY); + FF_AUTO_CLOSE_FD int blfd = open(path, O_RDONLY | O_CLOEXEC); if (blfd < 0) continue; diff --git a/src/logo/image/image.c b/src/logo/image/image.c index 18a33e3a6..7a513ec3a 100644 --- a/src/logo/image/image.c +++ b/src/logo/image/image.c @@ -620,7 +620,7 @@ static int getCacheFD(FFLogoRequestData* requestData, const char* fileName) { uint32_t cacheDirLength = requestData->cacheDir.length; ffStrbufAppendS(&requestData->cacheDir, fileName); - int fd = open(requestData->cacheDir.chars, O_RDONLY); + int fd = open(requestData->cacheDir.chars, O_RDONLY | O_CLOEXEC); ffStrbufSubstrBefore(&requestData->cacheDir, cacheDirLength); return fd; }