Skip to content

Conversation

@wdfk-prog
Copy link
Contributor

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

你的解决方案是什么 (what is your solution)

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:close
  • 设置PR number为 \ Set the PR number to:11119
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 close 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the close branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/dfs/dfs_v1/src/dfs.c
  • components/dfs/dfs_v1/src/dfs_file.c

📊 Current Review Status (Last Updated: 2026-01-05 10:46 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@wdfk-prog
Copy link
Contributor Author

  • 又被坑了一次,不得不修复一下了😭

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR attempts to fix critical reference counting bugs in the DFS v1 filesystem that could lead to vnode reference count underflow and potential double-free vulnerabilities (as reported in issues #10847 and #10848). The changes focus on adding NULL checks and reordering operations to prevent improper vnode cleanup.

Key changes:

  • Added NULL check for vnode before accessing ref_count in dfs_file_close
  • Reordered vnode ref_count decrement and cleanup logic in dfs_file_close
  • Added underflow protection and moved vnode NULL assignment earlier in fdt_fd_release

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
components/dfs/dfs_v1/src/dfs_file.c Modified dfs_file_close to add NULL check for vnode, decrement ref_count before checking if zero, and moved fd->vnode = NULL assignment before cleanup
components/dfs/dfs_v1/src/dfs.c Modified fdt_fd_release to add underflow guard, move vnode NULL assignment earlier, and add ref_count check before decrement

Comment on lines +418 to +421
if (vnode->ref_count > 0)
{
vnode->ref_count--;
}
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Concurrency/并发]: Insufficient synchronization between dfs_file_close and fdt_fd_release / dfs_file_close 和 fdt_fd_release 之间的同步不足

English: The dfs_file_close function uses dfs_fm_lock() (vnode manager lock) while fdt_fd_release uses dfs_file_lock() (fd table lock). These are two different locks protecting different resources. This creates a race condition where:

  1. Thread A calls dfs_file_close, decrements vnode->ref_count to 0, and starts cleaning up
  2. Thread B calls fdt_fd_release concurrently and also tries to decrement the same vnode's ref_count
  3. Both threads might try to free the same vnode, or one might access freed memory

The guard if (vnode->ref_count > 0) at line 418 only partially mitigates this - if the race occurs after the check but before the decrement, underflow can still happen.

中文:dfs_file_close 函数使用 dfs_fm_lock()(vnode 管理器锁),而 fdt_fd_release 使用 dfs_file_lock()(fd 表锁)。这是两个不同的锁保护不同的资源。这会创建一个竞争条件:

  1. 线程 A 调用 dfs_file_close,将 vnode->ref_count 递减到 0,并开始清理
  2. 线程 B 同时调用 fdt_fd_release,也尝试递减同一个 vnode 的 ref_count
  3. 两个线程可能都尝试释放同一个 vnode,或者一个可能访问已释放的内存

第 418 行的保护条件 if (vnode->ref_count > 0) 只能部分缓解这个问题 - 如果竞争发生在检查之后但递减之前,仍然可能发生下溢。

A proper fix would require using the same lock (likely dfs_fm_lock) in both functions when accessing vnode ref_count.

Copilot uses AI. Check for mistakes.
Comment on lines +351 to +353
vnode->ref_count--;
fd->vnode = NULL;
if (vnode->ref_count == 0)
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Bug/错误]: Reference count decrement and vnode cleanup order may cause race condition / 引用计数递减和vnode清理顺序可能导致竞争条件

English: The current implementation decrements vnode->ref_count and sets fd->vnode = NULL before checking if ref_count == 0. This creates a race condition window where another thread could access the vnode with ref_count=0 but before it's cleaned up. The check should be performed on the original ref_count value before decrementing, not after.

中文:当前实现在检查 ref_count == 0 之前先递减 vnode->ref_count 并设置 fd->vnode = NULL。这会创建一个竞争条件窗口,其中另一个线程可能在清理之前访问 ref_count=0 的 vnode。应该在递减之前对原始 ref_count 值执行检查,而不是之后。

The logic should be:

  1. Check if vnode->ref_count == 1 (last reference)
  2. If yes, perform cleanup (remove from hash, free memory)
  3. Otherwise, just decrement ref_count
  4. Finally, set fd->vnode = NULL

逻辑应该是:

  1. 检查 vnode->ref_count == 1(最后一个引用)
  2. 如果是,执行清理(从哈希中删除,释放内存)
  3. 否则,只需递减 ref_count
  4. 最后,设置 fd->vnode = NULL

Copilot uses AI. Check for mistakes.
Comment on lines +418 to 425
if (vnode->ref_count > 0)
{
vnode->ref_count--;
}
if(vnode->ref_count == 0)
{
rt_free(vnode);
fd_slot->vnode = RT_NULL;
}
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Bug/错误]: Potential vnode double-free vulnerability / 潜在的 vnode 双重释放漏洞

English: While the guard if (vnode->ref_count > 0) prevents underflow, the logic here is problematic. If fdt_fd_release is called when the vnode was already freed by dfs_file_close, this could lead to use-after-free. The vnode might have already been freed in dfs_file_close at line 363, but fd_slot->vnode still holds a dangling pointer until line 415 sets it to NULL. Additionally, freeing the vnode here at line 424 without proper cleanup (removing from hash list, freeing path/fullpath) is incomplete compared to the cleanup in dfs_file_close. This inconsistency suggests a design flaw in managing vnode lifecycle across two different code paths.

中文:虽然保护条件 if (vnode->ref_count > 0) 可以防止下溢,但这里的逻辑存在问题。如果在 vnode 已被 dfs_file_close 释放后调用 fdt_fd_release,这可能导致释放后使用。vnode 可能已在 dfs_file_close 的第 363 行被释放,但 fd_slot->vnode 仍持有悬空指针,直到第 415 行将其设置为 NULL。此外,在第 424 行释放 vnode 时没有适当的清理(从哈希列表中删除、释放 path/fullpath),与 dfs_file_close 中的清理相比是不完整的。这种不一致性表明在两个不同代码路径中管理 vnode 生命周期存在设计缺陷。

The correct approach should ensure that vnode cleanup is performed in only one place with proper synchronization, or the cleanup in both places should be identical and complete.

Copilot uses AI. Check for mistakes.
{
vnode->ref_count--;
}
if(vnode->ref_count == 0)
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Code Style/代码风格]: Missing space after 'if' keyword / 'if' 关键字后缺少空格

English: According to RT-Thread coding standards, there should be a space between 'if' and the opening parenthesis. The code should be if (vnode->ref_count == 0) instead of if(vnode->ref_count == 0).

中文:根据 RT-Thread 编码标准,'if' 和左括号之间应该有一个空格。代码应该是 if (vnode->ref_count == 0) 而不是 if(vnode->ref_count == 0)

Suggested change
if(vnode->ref_count == 0)
if (vnode->ref_count == 0)

Copilot uses AI. Check for mistakes.
Comment on lines +340 to 343
if (vnode == NULL || vnode->ref_count <= 0)
{
dfs_fm_unlock();
return -ENXIO;
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Bug/错误]: NULL check added but early return prevents vnode cleanup / 添加了 NULL 检查但提前返回阻止了 vnode 清理

English: While adding vnode == NULL check is good defensive programming, returning -ENXIO when vnode is NULL causes a potential resource leak. If vnode is somehow NULL but fd->ref_count == 1, the function returns early without decrementing fd->ref_count or cleaning up the fd structure. This could lead to fd resources not being properly released. Consider whether this is an assertion-worthy condition instead, since vnode should never be NULL when fd->ref_count == 1 in a correctly functioning system.

中文:虽然添加 vnode == NULL 检查是良好的防御性编程,但当 vnode 为 NULL 时返回 -ENXIO 会导致潜在的资源泄漏。如果 vnode 不知何故为 NULL 但 fd->ref_count == 1,函数会提前返回,而不递减 fd->ref_count 或清理 fd 结构。这可能导致 fd 资源无法正确释放。考虑这是否应该是断言级别的条件,因为在正常运行的系统中,当 fd->ref_count == 1vnode 不应该为 NULL。

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant