Skip to content

Commit

Permalink
fix: test case ut_lind_fs_open_existing_parentdirectory_and_nonexisti…
Browse files Browse the repository at this point in the history
…ng_file
  • Loading branch information
lind committed Sep 28, 2024
1 parent 215463e commit 34a8a98
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/safeposix/syscalls/fs_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,18 @@ impl Cage {
* Then return virtual fd
*/
pub fn open_syscall(&self, path: &str, oflag: i32, mode: u32) -> i32 {
// Validate oflag for unsupported or invalid flags
if (oflag & 0o20000) != 0 {
// Check for an invalid combination like using S_IFCHR which indicates
// character device creation but is being used with O_CREAT.
return syscall_error(Errno::EINVAL, "open", "Invalid oflag specified for file opening");
}

// Validate mode for invalid permission bits (only valid ones are allowed)
if (mode & !0o777) != 0 {
// If there are invalid bits in mode, return error
return syscall_error(Errno::EPERM, "open", "Invalid permission bits specified in mode");
}
// Convert data type from &str into *const i8
let relpath = normpath(convpath(path), self);
let relative_path = relpath.to_str().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src/tests/fs_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3241,6 +3241,8 @@ pub mod fs_tests {
cage.open_syscall(path, O_CREAT, invalid_mode),
-(Errno::EPERM as i32)
);
let _ = cage.unlink_syscall("/dir/file");
let _ = cage.rmdir_syscall("/dir");

assert_eq!(cage.exit_syscall(libc::EXIT_SUCCESS), libc::EXIT_SUCCESS);
lindrustfinalize();
Expand Down

0 comments on commit 34a8a98

Please sign in to comment.