Skip to content

Conversation

@GUIDINGLI
Copy link
Contributor

@GUIDINGLI GUIDINGLI commented Jan 14, 2026

Summary

This PR fixes the freopen() implementation:

  1. Close old fd before reopening: The previous implementation opened the new file first, then used dup2() to replace the old fd. This approach has issues:

    • The old fd remains open until dup2() closes it implicitly
    • Race condition: another thread could use the old fd between open() and dup2()

    The new implementation closes the old fd first, then opens the new file. This allows the kernel to reuse the same fd number, avoiding the need for dup2().

  2. Add fdsan support: When CONFIG_FDSAN is enabled, properly handle fd ownership tags:

    • Use android_fdsan_close_with_tag() to close the old fd with correct ownership
    • Use android_fdsan_exchange_owner_tag() to set ownership on the new fd

Impact

  • Fixes potential fd leak and race condition in freopen()
  • Enables fdsan to correctly track fd ownership through freopen() calls
  • No changes to the public API

Testing

  • Verified freopen() works correctly with and without CONFIG_FDSAN
  • fdsan no longer reports ownership errors when using freopen()
  • testcase: test_nuttx_fs_stream01

XMPengYinjie and others added 2 commits January 14, 2026 23:29
Updated freopen function in libc stdio to close the old file descriptor before reopening the file.

Signed-off-by: pengyinjie <[email protected]>
Signed-off-by: ligd <[email protected]>
[<0x60ddce>] __assert+0x1d/0x5c
[<0x60f2be>] android_fdsan_exchange_owner_tag+0x71/0xc0
[<0x60a14e>] close+0x19/0x64
[<0xb77b1c>] freopen+0x47/0xd8
[<0x86fe54>] test_nuttx_fs_stream01+0x4f/0x1a4
[<0xcef54e>] cmocka_run_one_test_or_fixture+0xe5/0x3cc
[<0xcefd26>] _cmocka_run_group_tests+0x41d/0xbd8
[<0x86cff2>] cmocka_fs_test_main+0x35/0x58
[<0x6a9d1a>] nxtask_startup+0x15/0x30
[<0x641e92>] nxtask_start+0x75/0x94

Signed-off-by: ligd <[email protected]>
@github-actions github-actions bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Jan 14, 2026
@GUIDINGLI GUIDINGLI changed the title Mine1 libc/stdio: fix freopen fd handling and add fdsan support Jan 14, 2026
@acassis
Copy link
Contributor

acassis commented Jan 14, 2026

@GUIDINGLI could you please provide a test example that exercise this issue? Maybe this test should be included in our ostest or NUT test

@GUIDINGLI
Copy link
Contributor Author

@GUIDINGLI could you please provide a test example that exercise this issue? Maybe this test should be included in our ostest or NUT test

[<0x60ddce>] __assert+0x1d/0x5c
[<0x60f2be>] android_fdsan_exchange_owner_tag+0x71/0xc0
[<0x60a14e>] close+0x19/0x64
[<0xb77b1c>] freopen+0x47/0xd8
[<0x86fe54>] test_nuttx_fs_stream01+0x4f/0x1a4
[<0xcef54e>] cmocka_run_one_test_or_fixture+0xe5/0x3cc
[<0xcefd26>] _cmocka_run_group_tests+0x41d/0xbd8
[<0x86cff2>] cmocka_fs_test_main+0x35/0x58
[<0x6a9d1a>] nxtask_startup+0x15/0x30
[<0x641e92>] nxtask_start+0x75/0x94

That say case is here test_nuttx_fs_stream01

@Donny9 Donny9 merged commit 8c95e9e into apache:master Jan 15, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants