Add the check of pathname in rename syscall

This commit is contained in:
LI Qing 2021-10-21 11:47:03 +08:00 committed by Zongmin.Gu
parent 1eb58a5eb3
commit 9f763f84b1
3 changed files with 35 additions and 1 deletions

2
deps/sefs vendored

@ -1 +1 @@
Subproject commit ca5dabb6ef3d4e9fdd704c40b85098cee8b63c5f
Subproject commit 5fd86adbfa0b173d10d5afebd871a237bf25f688

@ -8,6 +8,14 @@ pub fn do_renameat(old_fs_path: &FsPath, new_fs_path: &FsPath) -> Result<()> {
let oldpath = old_fs_path.to_abs_path()?;
let newpath = new_fs_path.to_abs_path()?;
let old_path = Path::new(&oldpath);
let new_path = Path::new(&newpath);
// Limitation: only compare the whole path components, cannot handle symlink or ".."
if new_path.starts_with(old_path) && new_path != old_path {
return_errno!(EINVAL, "newpath contains a path prefix of the oldpath");
}
let current = current!();
let fs = current.fs().read().unwrap();

@ -178,6 +178,31 @@ static int test_rename_dir() {
return 0;
}
static int test_rename_dir_to_subdir() {
const char *old_dir = "/root/test_old_dir";
mode_t mode = 00775;
int ret;
char sub_dir[PATH_MAX] = { 0 };
ret = snprintf(sub_dir, sizeof(sub_dir), "%s/test_new_dir", old_dir);
if (ret >= sizeof(sub_dir) || ret < 0) {
THROW_ERROR("failed to init new dir path");
}
if (mkdir(old_dir, mode) < 0) {
THROW_ERROR("failed to mkdir");
}
ret = rename(old_dir, sub_dir);
if (ret == 0 || errno != EINVAL) {
THROW_ERROR("failed to check rename dir to subdir");
}
if (rmdir(old_dir) < 0) {
THROW_ERROR("failed to rmdir");
}
return 0;
}
// ============================================================================
// Test suite main
// ============================================================================
@ -188,6 +213,7 @@ static test_case_t test_cases[] = {
TEST_CASE(test_rename_with_target_exist),
TEST_CASE(test_renameat),
TEST_CASE(test_rename_dir),
TEST_CASE(test_rename_dir_to_subdir),
};
int main(int argc, const char *argv[]) {