Skip to content

Commit

Permalink
Kernel+LibC+LibCore+UE: Implement fchmodat(2)
Browse files Browse the repository at this point in the history
This function is an extended version of `chmod(2)` that lets one control
whether to dereference symlinks, and specify a file descriptor to a
directory that will be used as the base for relative paths.
  • Loading branch information
BertalanD authored and awesomekling committed Jan 12, 2022
1 parent 5f71925 commit 182016d
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 14 deletions.
7 changes: 7 additions & 0 deletions Kernel/API/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,13 @@ struct SC_statvfs_params {
struct statvfs* buf;
};

struct SC_chmod_params {
int dirfd;
StringArgument path;
u16 mode;
int follow_symlinks;
};

void initialize();
int sync();

Expand Down
4 changes: 2 additions & 2 deletions Kernel/FileSystem/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ ErrorOr<void> VirtualFileSystem::chmod(Custody& custody, mode_t mode)
return inode.chmod(mode);
}

ErrorOr<void> VirtualFileSystem::chmod(StringView path, mode_t mode, Custody& base)
ErrorOr<void> VirtualFileSystem::chmod(StringView path, mode_t mode, Custody& base, int options)
{
auto custody = TRY(resolve_path(path, base));
auto custody = TRY(resolve_path(path, base, nullptr, options));
return chmod(custody, mode);
}

Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/VirtualFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class VirtualFileSystem {
ErrorOr<void> unlink(StringView path, Custody& base);
ErrorOr<void> symlink(StringView target, StringView linkpath, Custody& base);
ErrorOr<void> rmdir(StringView path, Custody& base);
ErrorOr<void> chmod(StringView path, mode_t, Custody& base);
ErrorOr<void> chmod(StringView path, mode_t, Custody& base, int options = 0);
ErrorOr<void> chmod(Custody&, mode_t);
ErrorOr<void> chown(StringView path, UserID, GroupID, Custody& base, int options);
ErrorOr<void> chown(Custody&, UserID, GroupID);
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ class Process final
ErrorOr<FlatPtr> sys$rmdir(Userspace<const char*> pathname, size_t path_length);
ErrorOr<FlatPtr> sys$mount(Userspace<const Syscall::SC_mount_params*>);
ErrorOr<FlatPtr> sys$umount(Userspace<const char*> mountpoint, size_t mountpoint_length);
ErrorOr<FlatPtr> sys$chmod(Userspace<const char*> pathname, size_t path_length, mode_t);
ErrorOr<FlatPtr> sys$chmod(Userspace<Syscall::SC_chmod_params const*>);
ErrorOr<FlatPtr> sys$fchmod(int fd, mode_t);
ErrorOr<FlatPtr> sys$chown(Userspace<const Syscall::SC_chown_params*>);
ErrorOr<FlatPtr> sys$fchown(int fd, UserID, GroupID);
Expand Down
19 changes: 16 additions & 3 deletions Kernel/Syscalls/chmod.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2022, Daniel Bertalan <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand All @@ -10,12 +11,24 @@

namespace Kernel {

ErrorOr<FlatPtr> Process::sys$chmod(Userspace<const char*> user_path, size_t path_length, mode_t mode)
ErrorOr<FlatPtr> Process::sys$chmod(Userspace<Syscall::SC_chmod_params const*> user_params)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
TRY(require_promise(Pledge::fattr));
auto path = TRY(get_syscall_path_argument(user_path, path_length));
TRY(VirtualFileSystem::the().chmod(path->view(), mode, current_directory()));
auto params = TRY(copy_typed_from_user(user_params));
auto path = TRY(get_syscall_path_argument(params.path));

RefPtr<Custody> base;
if (params.dirfd == AT_FDCWD) {
base = current_directory();
} else {
auto base_description = TRY(fds().open_file_description(params.dirfd));
if (!base_description->custody())
return EINVAL;
base = base_description->custody();
}

TRY(VirtualFileSystem::the().chmod(path->view(), params.mode, *base, params.follow_symlinks ? 0 : O__NOERROR));
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion Userland/DevTools/UserspaceEmulator/Emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Emulator {
int virt$beep();
int virt$bind(int sockfd, FlatPtr address, socklen_t address_length);
int virt$chdir(FlatPtr, size_t);
int virt$chmod(FlatPtr, size_t, mode_t);
int virt$chmod(FlatPtr);
int virt$chown(FlatPtr);
int virt$clock_gettime(int, FlatPtr);
int virt$clock_nanosleep(FlatPtr);
Expand Down
13 changes: 9 additions & 4 deletions Userland/DevTools/UserspaceEmulator/Emulator_syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3)
case SC_chdir:
return virt$chdir(arg1, arg2);
case SC_chmod:
return virt$chmod(arg1, arg2, arg3);
return virt$chmod(arg1);
case SC_chown:
return virt$chown(arg1);
case SC_clock_gettime:
Expand Down Expand Up @@ -418,10 +418,15 @@ int Emulator::virt$dbgputstr(FlatPtr characters, int length)
return 0;
}

int Emulator::virt$chmod(FlatPtr path_addr, size_t path_length, mode_t mode)
int Emulator::virt$chmod(FlatPtr params_addr)
{
auto path = mmu().copy_buffer_from_vm(path_addr, path_length);
return syscall(SC_chmod, path.data(), path.size(), mode);
Syscall::SC_chmod_params params;
mmu().copy_from_vm(&params, params_addr, sizeof(params));

auto path = mmu().copy_buffer_from_vm((FlatPtr)params.path.characters, params.path.length);
params.path.characters = (char const*)path.data();
params.path.length = path.size();
return syscall(SC_chmod, &params);
}

int Emulator::virt$chown(FlatPtr params_addr)
Expand Down
20 changes: 19 additions & 1 deletion Userland/Libraries/LibC/stat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,30 @@ int mkdir(const char* pathname, mode_t mode)

// https://pubs.opengroup.org/onlinepubs/9699919799/functions/chmod.html
int chmod(const char* pathname, mode_t mode)
{
return fchmodat(AT_FDCWD, pathname, mode, 0);
}

// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmodat.html
int fchmodat(int dirfd, char const* pathname, mode_t mode, int flags)
{
if (!pathname) {
errno = EFAULT;
return -1;
}
int rc = syscall(SC_chmod, pathname, strlen(pathname), mode);

if (flags & ~AT_SYMLINK_) {
errno = EINVAL;
return -1;
}

Syscall::SC_chmod_params params {
dirfd,
{ pathname, strlen(pathname) },
mode,
!(flags & AT_SYMLINK_)
};
int rc = syscall(SC_chmod, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);
}

Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibC/sys/stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ __BEGIN_DECLS

mode_t umask(mode_t);
int chmod(const char* pathname, mode_t);
int fchmodat(int fd, char const* path, mode_t mode, int flag);
int fchmod(int fd, mode_t);
int mkdir(const char* pathname, mode_t);
int mkfifo(const char* pathname, mode_t);
Expand Down
8 changes: 7 additions & 1 deletion Userland/Libraries/LibCore/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,13 @@ ErrorOr<void> chmod(StringView pathname, mode_t mode)
return Error::from_syscall("chmod"sv, -EFAULT);

#ifdef __serenity__
int rc = syscall(SC_chmod, pathname.characters_without_null_termination(), pathname.length(), mode);
Syscall::SC_chmod_params params {
AT_FDCWD,
{ pathname.characters_without_null_termination(), pathname.length() },
mode,
true
};
int rc = syscall(SC_chmod, &params);
HANDLE_SYSCALL_RETURN_VALUE("chmod"sv, rc, {});
#else
String path = pathname;
Expand Down

0 comments on commit 182016d

Please sign in to comment.