Skip to content

Conversation

@mistcoversmyeyes
Copy link
Contributor

@mistcoversmyeyes mistcoversmyeyes commented Nov 12, 2025

问题描述

gVisor 的测试用例 UnlinkTest.BadNamePtr 通过 unlink(reinterpret_cast<char*>(1)) 模拟用户传入无效指针。DragonOS 预期返回 EFAULT,但现状是内核不断陷入 copy_from_user 触发的缺页重试,最终将进程直接 kill,测试无法继续。

排查过程

  • 通过断点和日志确认 page fault 一直发生在 copy_from_user,复现地址形如 0x1
  • 对比 Linux 内核实现后,确认我们现有的 verify_area() 只验证指针是否落在用户空间地址范围,未校验该范围是否真的映射且具备访问权限。
  • do_user_addr_fault 中也看到由于缺少显式的 fail-fast,非法地址反复触发 fault handler,最终通过 SIGSEGV 结束进程。

修复方案

  • docs(syscall/user_access):记录问题定位过程,明确“仅做边界检查”的现有行为。
  • refactor(mm):将 verify_area() 重命名为 check_area_in_user_space(),突出它只负责判定是否落在用户虚拟地址空间这一事实。
  • fix:在 copy_from_user() 中引入新的映射校验,调用 is_user_accessable()(检测页表映射 + 用户/写权限),对未映射或权限不足的区域立即返回 -EFAULT,避免进入缺页处理循环。
  • refactor(syscall/user_access):将原先名为 check_user_access_by_page_table() 的布尔函数重命名为 is_user_accessable(),让调用语义更贴近布尔返回值。
  • style:顺手整理了一处代码格式问题,保持文件一致性。

TODO

  • 搭建最小复现用例并定位问题成因
  • 重命名指针校验相关工具函数,澄清职责
  • copy_from_user() 中新增页表级访问校验并返回 -EFAULT
  • is_user_accessable() 的拼写修正为 is_user_accessible()(待进一步提交)

测试验证

image

Copilot AI review requested due to automatic review settings November 12, 2025 10:43
@github-actions github-actions bot added Bug fix A bug is fixed in this pull request test Unitest/User space test labels Nov 12, 2025
Copilot finished reviewing on behalf of mistcoversmyeyes November 12, 2025 10:44
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 fixes the test case UnlinkTest.BadNamePtr by improving user space access validation. The main change is refactoring the user space address validation mechanism.

  • Renamed verify_area() to check_area_in_user_space() to better indicate its purpose
  • Renamed check_user_access_by_page_table() to is_user_accessable() for clarity
  • Updated copy_from_user() to use proper page table validation instead of simple address range checking

Reviewed Changes

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

Show a summary per file
File Description
kernel/src/syscall/user_access.rs Core refactoring: replaced address range checking with page table validation and renamed helper functions
kernel/src/syscall/mod.rs Updated socket syscall validation to use renamed function
kernel/src/process/syscall/sys_execve.rs Updated execve parameter validation to use renamed function
kernel/src/process/syscall/sys_clone.rs Updated clone syscall validation to use renamed function
kernel/src/process/fork.rs Updated fork TLS validation to use renamed function
kernel/src/net/socket/endpoint.rs Updated socket endpoint validation to use renamed function
kernel/src/mm/syscall/sys_munmap.rs Updated munmap validation to use renamed function
kernel/src/mm/syscall/sys_msync.rs Updated msync validation to use renamed function
kernel/src/mm/syscall/sys_mprotect.rs Updated mprotect validation to use renamed function
kernel/src/mm/syscall/sys_mmap.rs Updated mmap validation to use renamed function
kernel/src/mm/syscall/sys_mincore.rs Updated mincore validation to use renamed function
kernel/src/mm/syscall/sys_madvise.rs Updated madvise validation to use renamed function
kernel/src/mm/mod.rs Renamed verify_area to check_area_in_user_space
kernel/src/libs/futex/syscall/sys_robust_futex.rs Updated robust futex validation to use renamed function
kernel/src/libs/futex/syscall/sys_futex.rs Updated futex validation to use renamed function
kernel/src/filesystem/vfs/syscall/sys_getdents.rs Updated getdents validation to use renamed function
kernel/src/filesystem/vfs/syscall/sys_getcwd.rs Updated getcwd validation to use renamed function
kernel/src/driver/disk/ahci/ahcidisk.rs Updated AHCI disk buffer validation to use renamed function
kernel/src/arch/x86_64/process/syscall.rs Updated arch_prctl validation to use renamed function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/// Copy data from user space to kernel space
pub unsafe fn copy_from_user(dst: &mut [u8], src: VirtAddr) -> Result<usize, SystemError> {
verify_area(src, dst.len()).map_err(|_| SystemError::EFAULT)?;
// 这里使用的函数错误了, verify_area() 函数只简单的检查 `src` 是否存在于用户地址空间,而不检查 `src` 是否真的被分配给用户进程使用。
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

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

Corrected spacing in comment: '使用的函数错误了' should have proper punctuation. The comma should be a Chinese comma ','.

Suggested change
// 这里使用的函数错误了, verify_area() 函数只简单的检查 `src` 是否存在于用户地址空间,而不检查 `src` 是否真的被分配给用户进程使用。
// 这里使用的函数错误了verify_area() 函数只简单的检查 `src` 是否存在于用户地址空间,而不检查 `src` 是否真的被分配给用户进程使用。

Copilot uses AI. Check for mistakes.
/// * `true` if all pages are mapped and have required permissions
/// * `false` if any page is not mapped or lacks required permissions
fn check_user_access_by_page_table(addr: VirtAddr, size: usize, check_write: bool) -> bool {
fn is_user_accessable(addr: VirtAddr, size: usize, check_write: bool) -> bool {
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

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

Corrected spelling of 'accessable' to 'accessible'.

Suggested change
fn is_user_accessable(addr: VirtAddr, size: usize, check_write: bool) -> bool {
fn is_user_accessible(addr: VirtAddr, size: usize, check_write: bool) -> bool {

Copilot uses AI. Check for mistakes.
///
/// 否则返回Ok(())
pub fn verify_area(addr: VirtAddr, size: usize) -> Result<(), SystemError> {
pub fn check_area_in_user_space(addr: VirtAddr, size: usize) -> Result<(), SystemError> {
Copy link
Member

@fslongjin fslongjin Nov 12, 2025

Choose a reason for hiding this comment

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

这里重命名的意义在于?这种广泛使用的命名不要更改。

@mistcoversmyeyes mistcoversmyeyes force-pushed the bugfix/unlink_bad_ptr branch 2 times, most recently from f735b8b to e5034d7 Compare November 19, 2025 12:06
Copy link
Member

@fslongjin fslongjin left a comment

Choose a reason for hiding this comment

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

这个pr看起来没有实质的更改?

@mistcoversmyeyes
Copy link
Contributor Author

这个pr看起来没有实质的更改?

这几天一堆 ddl ,这个 PR 要delay一下 (┬┬﹏┬┬)

@mistcoversmyeyes mistcoversmyeyes marked this pull request as draft November 24, 2025 10:17
@mistcoversmyeyes mistcoversmyeyes changed the title fix(syscall): 修复测试用例 UnlinkTest.BadNamePtr fix(syscall): 修复 unlink 系统调用 Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug fix A bug is fixed in this pull request test Unitest/User space test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants