Kernel: Pass correct permission flags when opening files

Right now, permission flags passed to VFS::open() are effectively ignored, but
that is going to change.

* O_RDONLY is 0, but it's still nicer to pass it explicitly
* POSIX says that binding a Unix socket to a symlink shall fail with EADDRINUSE
This commit is contained in:
Sergey Bugaev 2020-01-19 01:04:48 +03:00 committed by Andreas Kling
parent 7d4a267504
commit 6466c3d750
4 changed files with 35 additions and 4 deletions

View file

@ -182,7 +182,7 @@ void dump_backtrace()
void load_ksyms()
{
auto result = VFS::the().open("/res/kernel.map", 0, 0, VFS::the().root_custody());
auto result = VFS::the().open("/res/kernel.map", O_RDONLY, 0, VFS::the().root_custody());
ASSERT(!result.is_error());
auto description = result.value();
auto buffer = description->read_entire_file();

View file

@ -111,7 +111,7 @@ KResult LocalSocket::bind(const sockaddr* user_address, socklen_t address_size)
mode_t mode = S_IFSOCK | (m_prebind_mode & 04777);
UidAndGid owner { m_prebind_uid, m_prebind_gid };
auto result = VFS::the().open(path, O_CREAT | O_EXCL, mode, current->process().current_directory(), owner);
auto result = VFS::the().open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW_NOERROR, mode, current->process().current_directory(), owner);
if (result.is_error()) {
if (result.error() == -EEXIST)
return KResult(-EADDRINUSE);
@ -145,7 +145,7 @@ KResult LocalSocket::connect(FileDescription& description, const sockaddr* addre
kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
#endif
auto description_or_error = VFS::the().open(safe_address, 0, 0, current->process().current_directory());
auto description_or_error = VFS::the().open(safe_address, O_RDWR, 0, current->process().current_directory());
if (description_or_error.is_error())
return KResult(-ECONNREFUSED);

View file

@ -4222,7 +4222,7 @@ int Process::sys$module_load(const char* user_path, size_t path_length)
auto path = get_syscall_path_argument(user_path, path_length);
if (path.is_error())
return path.error();
auto description_or_error = VFS::the().open(path.value(), 0, 0, current_directory());
auto description_or_error = VFS::the().open(path.value(), O_RDONLY, 0, current_directory());
if (description_or_error.is_error())
return description_or_error.error();
auto& description = description_or_error.value();

View file

@ -0,0 +1,31 @@
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main(int, char**)
{
constexpr const char* path = "/tmp/foo";
int rc = symlink("bar", path);
if (rc < 0) {
perror("symlink");
return 1;
}
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
return 1;
}
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
rc = bind(fd, (struct sockaddr*)(&addr), sizeof(addr));
if (rc < 0 && errno == EADDRINUSE)
return 0;
return 1;
}