Skip to content

Conversation

@xboHodx
Copy link
Contributor

@xboHodx xboHodx commented Nov 6, 2025

  • 新增permission模块处理inode权限检查
  • 在路径遍历和chdir/fchdir系统调用中添加权限验证
  • 实现do_fchmodat文件权限修改功能

Note

Add UNIX-style permission checks to path traversal and chdir/fchdir, implement chmod via fchmodat, and only fallback arch_prctl to common handler on EINVAL; update tests.

  • VFS / Permissions:
    • Add vfs/permission.rs with PermissionMask and Cred::{inode_permission,check_chdir_permission} including capability overrides.
    • Enforce execute/search permission during path traversal in IndexNode::do_lookup_follow_symlink.
  • Syscalls:
    • chdir/fchdir: validate directory and permissions via check_chdir_permission before switching CWD.
    • Implement do_fchmodat: update only permission bits, preserve file type, and apply via set_metadata.
  • Arch:
    • arch_prctl: call x86_64 handler first; fallback to common handler only on EINVAL.
  • Tests:
    • Enable chdir_test, fchdir_test; blocklist FchdirTest.PermissionDenied.

Written by Cursor Bugbot for commit 8faea7f. This will update automatically on new commits. Configure here.

- 新增permission模块处理inode权限检查
- 在路径遍历和chdir/fchdir系统调用中添加权限验证
Copilot AI review requested due to automatic review settings November 6, 2025 14:00
@github-actions github-actions bot added the enhancement New feature or request label Nov 6, 2025
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 implements VFS permission checking infrastructure for DragonOS, adding UNIX DAC (Discretionary Access Control) permission checking capabilities. The changes enable proper permission validation for directory access operations and file mode changes.

  • Introduces a new permission.rs module with core permission checking functions (inode_permission, check_chdir_permission)
  • Integrates permission checks into chdir and fchdir syscalls to validate directory access rights
  • Implements the fchmodat syscall functionality to modify file permissions while preserving file type bits

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
kernel/src/filesystem/vfs/permission.rs New module providing UNIX DAC permission checking with capability override support
kernel/src/filesystem/vfs/syscall/sys_chdir.rs Adds permission validation using check_chdir_permission and fixes typo in error code documentation
kernel/src/filesystem/vfs/syscall/sys_fchdir.rs Adds permission validation for fchdir syscall
kernel/src/filesystem/vfs/open.rs Implements fchmodat functionality to modify file permissions
kernel/src/filesystem/vfs/mod.rs Integrates permission checks during path traversal and reorganizes imports
Comments suppressed due to low confidence (1)

kernel/src/filesystem/vfs/syscall/sys_chdir.rs:106

  • The directory type check is redundant. The check_chdir_permission function on line 98 already performs this validation on line 176 of permission.rs, returning ENOTDIR if the file type is not a directory. This duplicate check should be removed to avoid redundant validation and improve code maintainability.
        if metadata.file_type == FileType::Dir {
            proc.basic_mut().set_cwd(new_path);
            proc.fs_struct_mut().set_pwd(inode);
            return Ok(0);
        } else {
            return Err(SystemError::ENOTDIR);
        }

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

Comment on lines 49 to 51
if metadata.file_type != FileType::Dir {
return Err(SystemError::ENOTDIR);
}
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

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

The directory type check is redundant. The check_chdir_permission function on line 47 already performs this validation on line 176 of permission.rs, returning ENOTDIR if the file type is not a directory. This duplicate check should be removed to avoid redundant validation and improve code maintainability.

Suggested change
if metadata.file_type != FileType::Dir {
return Err(SystemError::ENOTDIR);
}

Copilot uses AI. Check for mistakes.
@fslongjin
Copy link
Member

这里需要fmt一下

Comment on lines 56 to 91
pub fn inode_permission(
metadata: &Metadata,
cred: &Arc<Cred>,
mask: u32,
) -> Result<(), SystemError> {
// 从 mode 中提取权限位
let mode_bits = metadata.mode.bits();
let file_mode = mode_bits & 0o777; // 只保留 rwxrwxrwx

// 确定要检查哪组权限位
let perm = if is_owner(metadata, cred) {
// 所有者权限(第 6-8 位)
(file_mode >> 6) & 7
} else if in_group(metadata, cred) {
// 组权限(第 3-5 位)
(file_mode >> 3) & 7
} else {
// 其他用户权限(第 0-2 位)
file_mode & 7
};

// PermissionMask 的低 3 位已经是 Unix 权限位格式 (rwx)
let need = mask & 0b111;

// 检查权限位是否满足请求
if (need & !perm) == 0 {
return Ok(()); // 通过普通检查,权限允许
}

// 尝试 capability 覆盖(类似 Linux 的 capable_wrt_inode_uidgid)
if try_capability_override(metadata, cred, mask) {
return Ok(());
}

// 所有检查都失败
Err(SystemError::EACCES)
Copy link
Member

Choose a reason for hiding this comment

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

权限相关的bit,本身就定义了mask,我觉得这个在这里写这么多位运算不是很好。and..我印象中好像内核有地方也写了类似的逻辑?可以再检查一下,完善一下这里。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

类似的逻辑指的是?是在内核中有相同作用的模块了吗?还是说这些位运算的逻辑?

Copy link
Member

Choose a reason for hiding this comment

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

类似的逻辑指的是?是在内核中有相同作用的模块了吗?还是说这些位运算的逻辑?

指的是位运算。这些mask之类的,
Uploading image.png…
这个结构体里面就有

- 重构权限位计算逻辑使用ModeType常量
- 更新gvisor测试白名单添加chdir相关测试
@fslongjin
Copy link
Member

失败的测试用例:
fchdir_test

[1970-01-01T00:02:34Z INFO runner::lib_sync]
[1970-01-01T00:02:34Z INFO runner::lib_sync] ===============================================
[INFO] 测试完成
[1970-01-01T00:02:34Z INFO runner::lib_sync] 27 / 28 测试用例通过
[1970-01-01T00:02:34Z INFO runner::lib_sync] 1 个测试用例失败:
[1970-01-01T00:02:34Z INFO runner::lib_sync] [X] fchdir_test

xboHodx and others added 4 commits November 14, 2025 17:04
- 将inode_permission及相关辅助函数从独立函数重构为Cred结构体方法
- 更新sys_chdir和sys_fchdir调用权限检查的新接口
- 优化代码组织结构,减少模块间依赖

Signed-off-by: longjin <longjin@DragonOS.org>
@fslongjin fslongjin merged commit 95dd6f7 into DragonOS-Community:master Nov 17, 2025
12 of 13 checks passed
@github-actions github-actions bot added the test Unitest/User space test label Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request test Unitest/User space test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants