Skip to content

Commit

Permalink
Use Machine cwd for readlinkat /proc/self/exe, fix getcwd return value
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Jun 28, 2024
1 parent fa9f605 commit b65ac4d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
9 changes: 2 additions & 7 deletions emulator/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ static void run_program(
machine.fds().permit_filesystem = !cli_args.sandbox;
machine.fds().permit_sockets = !cli_args.sandbox;
// Rewrite certain links to masquerade and simplify some interactions (eg. /proc/self/exe)
machine.fds().filter_readlink = [=] (void* user, std::string& path) {
machine.fds().filter_readlink = [&] (void* user, std::string& path) {
if (path == "/proc/self/exe") {
path = "/program";
path = machine.fds().cwd + "/program";
return true;
}
fprintf(stderr, "Guest wanted to readlink: %s (denied)\n", path.c_str());
Expand All @@ -189,11 +189,6 @@ static void run_program(
path = args.at(0); // Sneakily open the real program instead
return true;
}
if (path == "/home"
|| path == "lib"
|| path == "lib/zig"
|| path == "std/std.zig"
) return true;
if (path == "/etc/ssl/certs/ca-certificates.crt")
return true;
// ld-linux
Expand Down
22 changes: 13 additions & 9 deletions lib/libriscv/linux/system_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ static void syscall_openat(Machine<W>& machine)
// filter_open() can modify the path
if (!machine.fds().filter_open(machine.template get_userdata<void>(), path)) {
machine.set_result(-EPERM);
SYSPRINT("SYSCALL openat(path: %s) => %d\n",
path.c_str(), machine.template return_value<int>());
return;
}
}
Expand Down Expand Up @@ -493,15 +495,17 @@ void syscall_readlinkat(Machine<W>& machine)
SYSPRINT("SYSCALL readlinkat, fd: %d path: %s (filter => %s) buffer: 0x%lX size: %zu >= %ld\n",
vfd, original_path.c_str(), path.c_str(), (long)g_buf, (size_t)bufsize, (long)machine.return_value());
}
const int real_fd = machine.fds().translate(vfd);
else
{
const int real_fd = machine.fds().translate(vfd);

const int res = readlinkat(real_fd, original_path.c_str(), buffer, bufsize);
if (res > 0) {
// TODO: Only necessary if g_buf is not sequential.
machine.copy_to_guest(g_buf, buffer, res);
const int res = readlinkat(real_fd, original_path.c_str(), buffer, bufsize);
if (res > 0) {
// TODO: Only necessary if g_buf is not sequential.
machine.copy_to_guest(g_buf, buffer, res);
}
machine.set_result_or_error(res);
}

machine.set_result_or_error(res);
} else {
machine.set_result(-ENOSYS);
}
Expand Down Expand Up @@ -556,12 +560,12 @@ template <int W>
static void syscall_getcwd(Machine<W>& machine)
{
const auto g_buf = machine.sysarg(0);
const auto size = machine.sysarg(1);
[[maybe_unused]] const auto size = machine.sysarg(1);

auto& cwd = machine.fds().cwd;
if (!cwd.empty()) {
machine.copy_to_guest(g_buf, cwd.c_str(), cwd.size()+1);
machine.set_result(0);
machine.set_result(cwd.size()+1);
} else {
machine.set_result(-1);
}
Expand Down

0 comments on commit b65ac4d

Please sign in to comment.