Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions kernel/src/filesystem/vfs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,11 @@ impl File {
/// @brief 判断当前文件是否可读
#[inline]
pub fn readable(&self) -> Result<(), SystemError> {
// 暂时认为只要不是write only, 就可读
if *self.mode.read() == FileMode::O_WRONLY {
return Err(SystemError::EPERM);
let mode = *self.mode.read();

// 检查是否是O_PATH文件描述符
if mode == FileMode::O_WRONLY || mode.contains(FileMode::O_PATH) {
Copy link

Copilot AI Nov 24, 2025

Choose a reason for hiding this comment

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

Missing space after || operator. The condition should be mode == FileMode::O_WRONLY || mode.contains(FileMode::O_PATH) for consistency with Rust formatting conventions.

Copilot uses AI. Check for mistakes.
return Err(SystemError::EBADF);
}

return Ok(());
Expand Down
20 changes: 19 additions & 1 deletion kernel/src/filesystem/vfs/syscall/sys_pread64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use system_error::SystemError;

use crate::arch::interrupt::TrapFrame;
use crate::arch::syscall::nr::SYS_PREAD64;
use crate::filesystem::vfs::FileType;
use crate::process::ProcessManager;
use crate::syscall::table::FormattedSyscallParam;
use crate::syscall::table::Syscall;
Expand Down Expand Up @@ -35,7 +36,15 @@ impl Syscall for SysPread64Handle {
let len = Self::len(args);
let offset = Self::offset(args);

let mut user_buffer_writer = UserBufferWriter::new(buf_vaddr, len, frame.is_from_user())?;
// 检查offset + len是否溢出 同时检查offset是否为负数

let end_pos = offset.checked_add(len).ok_or(SystemError::EINVAL)?;
if offset > i64::MAX as usize || end_pos > i64::MAX as usize {
return Err(SystemError::EINVAL);
}

let mut user_buffer_writer =
UserBufferWriter::new_checked(buf_vaddr, len, frame.is_from_user())?;
Copy link
Member

Choose a reason for hiding this comment

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

要求用户缓冲区已经映射应该是不对的。可能是pagefault延迟映射。应该是使用protected的那个,校验vma

let user_buf = user_buffer_writer.buffer(0)?;

let binding = ProcessManager::current_pcb().fd_table();
Expand All @@ -48,6 +57,15 @@ impl Syscall for SysPread64Handle {
// Drop guard to avoid scheduling issues
drop(fd_table_guard);

// 检查是否是管道/Socket (ESPIPE)
let md = file.metadata()?;
if md.file_type == FileType::Pipe
|| md.file_type == FileType::Socket
|| md.file_type == FileType::CharDevice
{
return Err(SystemError::ESPIPE);
}
Copy link

Choose a reason for hiding this comment

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

Bug: CharDevice incorrectly included in ESPIPE file type check

The comment states this check is for pipes and sockets, but the code also includes FileType::CharDevice. According to POSIX, pread64 returns ESPIPE specifically for pipes, FIFOs, and sockets - not character devices. Many character devices like /dev/null and /dev/zero (which are FileType::CharDevice in this codebase) can legitimately support pread operations. Including CharDevice in this check will incorrectly return ESPIPE for valid pread64 calls on such devices.

Fix in Cursor Fix in Web


return file.pread(offset, len, user_buf);
}

Expand Down
4 changes: 4 additions & 0 deletions user/apps/tests/syscall/gvisor/blocklists/pread64_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SYS_MEMFD_CREATE未实现
Pread64Test.Overflow
Copy link

Copilot AI Nov 24, 2025

Choose a reason for hiding this comment

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

According to the PR description, the Overflow test case should now be fixed (listed among the fixed test cases). However, it's still present in the blocklist. If the test is indeed fixed, it should be removed from this blocklist. If it still needs to be blocked for a different reason (SYS_MEMFD_CREATE not implemented), the comment and PR description should be clarified to explain that only partial fixes were made.

Suggested change
Pread64Test.Overflow

Copilot uses AI. Check for mistakes.
# 会卡死
Pread64Test.CantReadPipe
1 change: 1 addition & 0 deletions user/apps/tests/syscall/gvisor/whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ sync_test
#chown_test
chdir_test
fchdir_test
pread64_test

# 进程相关测试
fork_test
Expand Down