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] 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 cf6dd3c0b7..8bbe9e7d61 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) {