Skip to content

Commit

Permalink
将创建/bin/false文件移入程序
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkzky committed Oct 21, 2024
1 parent eca8a40 commit b1dbb1b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
8 changes: 1 addition & 7 deletions user/apps/test-chown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

### 由于symlink系统调用还未实现,目前只测试chown和fchown

### 测试前需要手动添加nogroup用户组和nobody用户
### 测试前需要手动添加nogroup用户组和nobody用户(程序里加不了)
```groupadd -g 65534 nogroup
useradd -d /nonexistent -g 65534 -u 65534 -s /bin/false nobody
```

### /bin/false是个不可执行文件,用于测试chown系列系统调用时,文件权限的修改不会生效。
```
#!/bin/bash
exit 1
```
33 changes: 27 additions & 6 deletions user/apps/test-chown/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use core::ffi::{c_char, c_void};
use libc::{
chown, fchown, fchownat, getgrnam, getpwnam, lchown, mount, umount,
gid_t, uid_t, AT_FDCWD, AT_SYMLINK_NOFOLLOW,
chown, fchown, fchownat, getgrnam, getpwnam, gid_t, lchown, mount, uid_t, umount, AT_FDCWD,
AT_SYMLINK_NOFOLLOW,
};
use nix::errno::Errno;
use std::{
ffi::CString,
fs::{self, File},
io::{Error, Write},
os::unix::{fs::MetadataExt, io::AsRawFd},
fs::{self, metadata, File},
io::{self, Error, Write},
os::unix::{
fs::{MetadataExt, PermissionsExt},
io::AsRawFd,
},
path::Path,
};


fn print_file_owner_group(filename: &str) -> Result<(), Error> {
let metadata = std::fs::metadata(filename)?;
let uid = metadata.uid();
Expand Down Expand Up @@ -68,6 +70,7 @@ fn test_lchown(symlink_name: &str, new_uid: uid_t, new_gid: gid_t) -> Result<(),
}

fn main() -> Result<(), Error> {
create_false_file()?;
mount_test_ramfs();

let filename = "/mnt/myramfs/testfile.txt";
Expand Down Expand Up @@ -152,3 +155,21 @@ fn umount_test_ramfs() {
assert_eq!(result, 0, "Umount myramfs failed");
println!("Umount myramfs for test success!");
}

fn create_false_file() -> io::Result<()> {
let path = Path::new("/bin/false");
if metadata(path).is_ok() {
return Ok(());
}

let mut file = File::create(path)?;

file.write_all(b"#!/bin/bash\nexit 1\n")?;

let mut permissions = file.metadata()?.permissions();
permissions.set_mode(0o755); // 设置权限为755
std::fs::set_permissions(path, permissions)?;

println!("File created successfully at {:?}", path);
Ok(())
}

0 comments on commit b1dbb1b

Please sign in to comment.