From 7e206a90db1254222be56053dc136b344160b937 Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Wed, 14 Jun 2023 23:29:38 +0200 Subject: [PATCH 1/2] Handle negative values passed to close() Calling close() with negative numbers causes out-of-bounds indexing of the filehandles array. For example, this can happen if open() returns an error and the value is later passed to close(). --- platform/source/mbed_retarget.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/platform/source/mbed_retarget.cpp b/platform/source/mbed_retarget.cpp index cf6dd3c0b76..8bbe9e7d611 100644 --- a/platform/source/mbed_retarget.cpp +++ b/platform/source/mbed_retarget.cpp @@ -656,6 +656,11 @@ extern "C" int PREFIX(_close)(FILEHANDLE fh) #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY extern "C" int close(int fildes) { + if (fildes < 0) + { + errno = EBADF; + return -1; + } FileHandle *fhc = mbed_file_handle(fildes); filehandles[fildes] = NULL; if (fhc == NULL) { From 56ca532ef1aade86a27e1519ff395f506b6aaa99 Mon Sep 17 00:00:00 2001 From: alrvid <126816223+alrvid@users.noreply.github.com> Date: Thu, 15 Jun 2023 15:38:55 +0200 Subject: [PATCH 2/2] Moved a { to the same line as if Moved a { to the same line as if --- platform/source/mbed_retarget.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/platform/source/mbed_retarget.cpp b/platform/source/mbed_retarget.cpp index 8bbe9e7d611..aa5dfa8abfb 100644 --- a/platform/source/mbed_retarget.cpp +++ b/platform/source/mbed_retarget.cpp @@ -656,8 +656,7 @@ extern "C" int PREFIX(_close)(FILEHANDLE fh) #if !MBED_CONF_PLATFORM_STDIO_MINIMAL_CONSOLE_ONLY extern "C" int close(int fildes) { - if (fildes < 0) - { + if (fildes < 0) { errno = EBADF; return -1; }