This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit b426084a0afe60a38666bc8dd9060003848ad52f Author: liaoao <[email protected]> AuthorDate: Wed Sep 9 16:25:53 2026 +0800 libc/stdio: bypass fdsan for standard streams in freopen The stdin/stdout/stderr descriptors are created by the boot-time console open + dup2, which does not set an fdsan owner tag. The descriptor is left unowned (tag == 0) while the FILE object logically owns it, so the close_with_tag() in freopen() trips the fdsan "fd N is unowned" assertion (lib_fdsan.c) when CONFIG_FDSAN is enabled. Bypass fdsan entirely for the three standard streams: close their old fd with a plain close() and do not re-own the reused fd, keeping them unowned throughout. This is consistent with the rest of libc stdio: fopen()/fdopen() only exchange the owner tag on their regular-stream path and leave the standard streams (the sl_std[] else branch) unowned, and fclose() special-cases stdin/stdout/stderr. Regular streams (fd >= 3) keep close_with_tag() and the owner tag so fdsan can still catch genuine ownership violations. Signed-off-by: liaoao <[email protected]> Signed-off-by: pangzhen1 <[email protected]> --- libs/libc/stdio/lib_freopen.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/libs/libc/stdio/lib_freopen.c b/libs/libc/stdio/lib_freopen.c index 8489b9b6fb2..2b506d5ad03 100644 --- a/libs/libc/stdio/lib_freopen.c +++ b/libs/libc/stdio/lib_freopen.c @@ -32,6 +32,7 @@ #include <unistd.h> #ifdef CONFIG_FDSAN +# include <stdint.h> # include <android/fdsan.h> #endif @@ -118,9 +119,18 @@ FAR FILE *freopen(FAR const char *path, FAR const char *mode, /* close the old fd */ #ifdef CONFIG_FDSAN - android_fdsan_close_with_tag(fileno(stream), + /* Standard streams are fdsan-unowned; bypass fdsan as fclose() does. */ + + if (stream == stdin || stream == stdout || stream == stderr) + { + close(fileno(stream)); + } + else + { + android_fdsan_close_with_tag(fileno(stream), android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE, (uintptr_t)stream)); + } #else close(fileno(stream)); #endif @@ -129,9 +139,13 @@ FAR FILE *freopen(FAR const char *path, FAR const char *mode, fd = open(path, oflags, 0666); #ifdef CONFIG_FDSAN - android_fdsan_exchange_owner_tag(fd, 0, - android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE, - (uintptr_t)stream)); + if (stream != stdin && stream != stdout && stream != stderr) + { + android_fdsan_exchange_owner_tag(fd, 0, + android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_FILE, + (uintptr_t)stream)); + } + #endif flockfile(stream); stream->fs_cookie = (FAR void *)(intptr_t)fd;
