1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 12:55:09 +00:00

Kernel: Add a basic implementation of unveil()

This syscall is a complement to pledge() and adds the same sort of
incremental relinquishing of capabilities for filesystem access.

The first call to unveil() will "drop a veil" on the process, and from
now on, only unveiled parts of the filesystem are visible to it.

Each call to unveil() specifies a path to either a directory or a file
along with permissions for that path. The permissions are a combination
of the following:

- r: Read access (like the "rpath" promise)
- w: Write access (like the "wpath" promise)
- x: Execute access
- c: Create/remove access (like the "cpath" promise)

Attempts to open a path that has not been unveiled with fail with
ENOENT. If the unveiled path lacks sufficient permissions, it will fail
with EACCES.

Like pledge(), subsequent calls to unveil() with the same path can only
remove permissions, not add them.

Once you call unveil(nullptr, nullptr), the veil is locked, and it's no
longer possible to unveil any more paths for the process, ever.

This concept comes from OpenBSD, and their implementation does various
things differently, I'm sure. This is just a first implementation for
SerenityOS, and we'll keep improving on it as we go. :^)
This commit is contained in:
Andreas Kling 2020-01-20 22:12:04 +01:00
parent e711936c78
commit 0569123ad7
7 changed files with 188 additions and 3 deletions

View file

@ -1820,6 +1820,10 @@ bool Process::validate(const Syscall::ImmutableBufferArgument<DataType, SizeType
String Process::validate_and_copy_string_from_user(const char* user_characters, size_t user_length) const
{
if (!user_characters)
return {};
if (user_length == 0)
return String::empty();
if (!validate_read(user_characters, user_length))
return {};
SmapDisabler disabler;
@ -1933,6 +1937,9 @@ int Process::sys$open(const Syscall::SC_open_params* user_params)
if (options & O_NOFOLLOW_NOERROR)
return -EINVAL;
if (options & O_UNLINK_INTERNAL)
return -EINVAL;
if ((options & O_RDWR) || (options & O_WRONLY))
REQUIRE_PROMISE(wpath);
else
@ -4588,3 +4595,74 @@ Region& Process::add_region(NonnullOwnPtr<Region> region)
m_regions.append(move(region));
return *ptr;
}
int Process::sys$unveil(const Syscall::SC_unveil_params* user_params)
{
Syscall::SC_unveil_params params;
if (!validate_read_and_copy_typed(&params, user_params))
return -EFAULT;
if (!params.path.characters && !params.permissions.characters) {
m_unveil_state = UnveilState::VeilLocked;
return 0;
}
if (m_unveil_state == UnveilState::VeilLocked)
return -EPERM;
if (!params.path.characters || !params.permissions.characters)
return -EINVAL;
if (params.permissions.length > 4)
return -EINVAL;
auto path = get_syscall_path_argument(params.path);
if (path.is_error())
return path.error();
if (path.value().is_empty() || path.value().characters()[0] != '/')
return -EINVAL;
auto permissions = validate_and_copy_string_from_user(params.permissions);
if (permissions.is_null())
return -EFAULT;
unsigned new_permissions = 0;
for (size_t i = 0; i < permissions.length(); ++i) {
switch (permissions[i]) {
case 'r':
new_permissions |= UnveiledPath::Access::Read;
break;
case 'w':
new_permissions |= UnveiledPath::Access::Write;
break;
case 'x':
new_permissions |= UnveiledPath::Access::Execute;
break;
case 'c':
new_permissions |= UnveiledPath::Access::CreateOrRemove;
break;
default:
return -EINVAL;
}
}
for (int i = 0; i < m_unveiled_paths.size(); ++i) {
auto& unveiled_path = m_unveiled_paths[i];
if (unveiled_path.path == path.value()) {
if (new_permissions & ~unveiled_path.permissions)
return -EPERM;
if (!new_permissions) {
m_unveiled_paths.remove(i);
return 0;
}
unveiled_path.permissions = new_permissions;
return 0;
}
}
m_unveiled_paths.append({ path.value(), new_permissions });
ASSERT(m_unveil_state != UnveilState::VeilLocked);
m_unveil_state = UnveilState::VeilDropped;
return 0;
}