Kernel+LibC: Allow passing mount flags to chroot()

Since a chroot is in many ways similar to a separate root mount, we can also
apply mount flags to it as if it was an actual mount. These flags will apply
whenever the chrooted process accesses its root directory, but not when other
processes access this same directory for the outside. Since it's common to
chdir("/") immediately after chrooting (so that files accessed through the
current directory inherit the same mount flags), this effectively allows one to
apply additional limitations to a process confined inside a chroot.

To this effect, sys$chroot() gains a mount_flags argument (exposed as
chroot_with_mount_flags() in userspace) which can be set to all the same values
as the flags argument for sys$mount(), and additionally to -1 to keep the flags
set for that file system. Note that passing 0 as mount_flags will unset any
flags that may have been set for the file system, not keep them.
This commit is contained in:
Sergey Bugaev 2020-01-12 21:03:42 +03:00 committed by Andreas Kling
parent fee6d0a3a6
commit dd54d13d8d
Notes: sideshowbarker 2024-07-19 10:07:05 +09:00
4 changed files with 11 additions and 4 deletions

View file

@ -4337,8 +4337,9 @@ int Process::sys$chroot(const char* user_path, size_t path_length)
if (directory_or_error.is_error())
return directory_or_error.error();
auto directory = directory_or_error.value();
m_root_directory_for_procfs = directory;
set_root_directory(Custody::create(nullptr, "", directory->inode(), directory->mount_flags()));
m_root_directory_relative_to_global_root = directory;
int chroot_mount_flags = mount_flags == -1 ? directory->mount_flags() : mount_flags;
set_root_directory(Custody::create(nullptr, "", directory->inode(), chroot_mount_flags));
return 0;
}