Skip to content

Truly independent web browser

LibSandbox: Log the paths that a sandbox refuses when debugging

Andreas Klingba39d058e529
diff --git a/Libraries/LibSandbox/FileBroker.cpp b/Libraries/LibSandbox/FileBroker.cpp
index bd51355eeae..f7a5f2a47be 100644
--- a/Libraries/LibSandbox/FileBroker.cpp
+++ b/Libraries/LibSandbox/FileBroker.cpp
@@ -5,6 +5,8 @@
*/
#include <AK/ByteString.h>
+#include <AK/Format.h>
+#include <AK/HashTable.h>
#include <AK/Noncopyable.h>
#include <AK/ScopeGuard.h>
#include <AK/StringBuilder.h>
@@ -253,10 +255,12 @@ private:
class FileBrokerServer {
public:
- FileBrokerServer(Vector<ResolvedPathRule> rules, pid_t helper_pid, ByteString current_directory)
+ FileBrokerServer(Vector<ResolvedPathRule> rules, pid_t helper_pid, ByteString current_directory, ByteString helper_name)
: m_rules(move(rules))
, m_helper_pid(helper_pid)
, m_current_directory(move(current_directory))
+ , m_helper_name(move(helper_name))
+ , m_logs_refusals(sandbox_debugging_is_enabled())
{
}
@@ -268,12 +272,57 @@ private:
int fd { -1 };
};
- bool allows(StringView path, FileBrokerAccess access) const { return file_broker_allows(m_rules, path, access); }
+ bool allows(StringView path, FileBrokerAccess access) const
+ {
+ if (file_broker_allows(m_rules, path, access))
+ return true;
+ log_refusal(path, access);
+ return false;
+ }
+
+ static constexpr size_t max_logged_refusals = 4096;
- // A path outside of what the helper can see has to look the same whether or not it exists.
+ // Each path is logged once for each kind of access, since libraries tend to ask for the same files again and again.
+ void log_refusal(StringView path, FileBrokerAccess access) const
+ {
+ if (!m_logs_refusals)
+ return;
+ if (m_logged_too_many_refusals)
+ return;
+ auto key = ByteString::formatted("{}:{}", to_underlying(access), path);
+ if (m_logged_refusals.contains(key))
+ return;
+ if (m_logged_refusals.size() == max_logged_refusals) {
+ m_logged_too_many_refusals = true;
+ warnln("Sandbox refusal in {}: too many refusals, further refusals are not logged", m_helper_name);
+ return;
+ }
+ m_logged_refusals.set(key);
+ auto access_name = [&] {
+ switch (access) {
+ case FileBrokerAccess::Metadata:
+ return "metadata"sv;
+ case FileBrokerAccess::Read:
+ return "read"sv;
+ case FileBrokerAccess::Write:
+ return "write"sv;
+ case FileBrokerAccess::ChangeEntry:
+ return "entry change"sv;
+ }
+ VERIFY_NOT_REACHED();
+ }();
+ warnln("Sandbox refusal in {}: {} access to {}", m_helper_name, access_name, path);
+ }
+
+ // A path outside of what the helper can see has to look the same whether or not it exists. Hiding that a path is
+ // missing changes nothing for the helper, so only hiding one that exists is logged.
int failure_for(PathWalker const& walker, int error) const
{
- return allows(walker.failed_path(), FileBrokerAccess::Metadata) ? error : EACCES;
+ if (file_broker_allows(m_rules, walker.failed_path(), FileBrokerAccess::Metadata))
+ return error;
+ if (error != ENOENT)
+ log_refusal(walker.failed_path(), FileBrokerAccess::Metadata);
+ return EACCES;
}
ErrorOr<ByteString> absolute_path(StringView path, int directory_fd) const;
@@ -293,6 +342,10 @@ private:
Vector<ResolvedPathRule> m_rules;
pid_t m_helper_pid { 0 };
ByteString m_current_directory;
+ ByteString m_helper_name;
+ bool m_logs_refusals { false };
+ mutable HashTable<ByteString> m_logged_refusals;
+ mutable bool m_logged_too_many_refusals { false };
};
}
@@ -444,10 +497,11 @@ int FileBrokerServer::make_directory(StringView path, u32 mode)
// The kernel reports a directory that exists before it asks whether a new one could be made there, and libpulse
// depends on that for a runtime directory that it may only read.
auto target = walker.canonical_path_with_last_name();
- if (!allows(target, FileBrokerAccess::ChangeEntry)) {
+ if (!file_broker_allows(m_rules, target, FileBrokerAccess::ChangeEntry)) {
struct stat statbuf;
- if (allows(target, FileBrokerAccess::Metadata) && fstatat(walker.fd(), walker.last_name().characters(), &statbuf, AT_SYMLINK_NOFOLLOW) == 0)
+ if (file_broker_allows(m_rules, target, FileBrokerAccess::Metadata) && fstatat(walker.fd(), walker.last_name().characters(), &statbuf, AT_SYMLINK_NOFOLLOW) == 0)
return EEXIST;
+ log_refusal(target, FileBrokerAccess::ChangeEntry);
return EACCES;
}
return mkdirat(walker.fd(), walker.last_name().characters(), mode) < 0 ? errno : 0;
@@ -486,8 +540,11 @@ int FileBrokerServer::bind_socket(int fd, char const* address, u32 length)
if (fd < 0 || length < sizeof(sa_family_t) || length > sizeof(storage))
return EINVAL;
memcpy(&storage, address, length);
- if (storage.ss_family != AF_INET && storage.ss_family != AF_INET6 && storage.ss_family != AF_NETLINK)
+ if (storage.ss_family != AF_INET && storage.ss_family != AF_INET6 && storage.ss_family != AF_NETLINK) {
+ if (m_logs_refusals)
+ warnln("Sandbox refusal in {}: binding a socket of address family {}", m_helper_name, storage.ss_family);
return EACCES;
+ }
return bind(fd, reinterpret_cast<sockaddr const*>(&storage), length) < 0 ? errno : 0;
}
@@ -750,11 +807,13 @@ ErrorOr<void> start_file_broker(ReadonlySpan<PathRule> rules)
// Everything else the helper has open is more than a broker for its files should ever hold.
close_all_descriptors_except(fds[1]);
+ char helper_name[16] = {};
+ (void)prctl(PR_GET_NAME, helper_name, 0ul, 0ul, 0ul);
(void)prctl(PR_SET_NAME, "FileBroker", 0ul, 0ul, 0ul);
if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0ul, 0ul, 0ul) < 0 || getppid() != helper_pid)
_exit(1);
- FileBrokerServer server { move(resolved_rules), helper_pid, current_directory };
+ FileBrokerServer server { move(resolved_rules), helper_pid, current_directory, ByteString { helper_name, strnlen(helper_name, sizeof(helper_name)) } };
if (confine_broker().is_error())
_exit(1);
diff --git a/Libraries/LibSandbox/Sandbox.cpp b/Libraries/LibSandbox/Sandbox.cpp
index ae8a104f51d..784c011f2ca 100644
--- a/Libraries/LibSandbox/Sandbox.cpp
+++ b/Libraries/LibSandbox/Sandbox.cpp
@@ -799,6 +799,12 @@ static ErrorOr<size_t> count_threads()
static bool s_signals_stay_within_the_process_tree = false;
+bool sandbox_debugging_is_enabled()
+{
+ auto const* value = getenv("LADYBIRD_SANDBOX_DEBUG");
+ return value && *value && StringView { value, strlen(value) } != "0"sv;
+}
+
bool signals_stay_within_the_process_tree()
{
return s_signals_stay_within_the_process_tree;
@@ -932,7 +938,7 @@ ErrorOr<void> restrict_filesystem(ReadonlySpan<PathRule> paths)
// Any failure means we can't have Landlock here: a kernel without it says ENOSYS or EOPNOTSUPP, and a container's
// own seccomp filter can say anything.
auto landlock_abi = syscall(__NR_landlock_create_ruleset, nullptr, 0, LANDLOCK_CREATE_RULESET_VERSION);
- if (landlock_abi > 0)
+ if (landlock_abi > 0 && !sandbox_debugging_is_enabled())
return restrict_filesystem_with_landlock(paths, landlock_abi);
return start_file_broker(paths);
diff --git a/Libraries/LibSandbox/Sandbox.h b/Libraries/LibSandbox/Sandbox.h
index 1079746eea7..2dd72a0d58e 100644
--- a/Libraries/LibSandbox/Sandbox.h
+++ b/Libraries/LibSandbox/Sandbox.h
@@ -89,6 +89,9 @@ struct SeatbeltProfile {
[[nodiscard]] ErrorOr<void> restrict_filesystem(ReadonlySpan<PathRule> = {});
// Whether restrict_filesystem() also kept this process from signalling any process outside its own tree.
[[nodiscard]] bool signals_stay_within_the_process_tree();
+// Whether LADYBIRD_SANDBOX_DEBUG asks the sandbox to log what it refuses. The file broker then confines the file system
+// even where Landlock exists, because Landlock refuses a path without telling the process, and the broker can log it.
+[[nodiscard]] bool sandbox_debugging_is_enabled();
[[nodiscard]] ErrorOr<void> add_gpu_driver_paths(Vector<PathRule>& paths);
#endif
diff --git a/Meta/CMake/all_the_debug_macros.cmake b/Meta/CMake/all_the_debug_macros.cmake
index 4d4aee55079..79aeee6aea2 100644
--- a/Meta/CMake/all_the_debug_macros.cmake
+++ b/Meta/CMake/all_the_debug_macros.cmake
@@ -71,6 +71,8 @@ set(XML_PARSER_DEBUG ON)
# False positive: ANDROID_LOG_DEBUG is a log level, not a debug flag
# set(ANDROID_LOG_DEBUG ON)
+# False positive: LADYBIRD_SANDBOX_DEBUG is an environment variable, not a debug flag
+# set(LADYBIRD_SANDBOX_DEBUG ON)
# Third-party: skia vcpkg port overlay
# set(gn_defines_DEBUG ON)
# set(gn_include_dirs_DEBUG ON)
diff --git a/Tests/LibSandbox/TestFileBroker.cpp b/Tests/LibSandbox/TestFileBroker.cpp
index 7c5fd28b5b7..d35f926c6a5 100644
--- a/Tests/LibSandbox/TestFileBroker.cpp
+++ b/Tests/LibSandbox/TestFileBroker.cpp
@@ -5,6 +5,7 @@
*/
#include <AK/Platform.h>
+#include <AK/StringBuilder.h>
#include <LibSandbox/FileBroker.h>
#include <LibSandbox/Sandbox.h>
#include <LibSandbox/Seccomp.h>
@@ -277,6 +278,73 @@ TEST_CASE(the_broker_enforces_the_path_rules)
(void)rmdir(base.characters());
}
+// Runs the body in a child with LADYBIRD_SANDBOX_DEBUG set, and returns what the child and its broker wrote to stderr.
+template<typename Body>
+static ByteString stderr_of_debugged_child(Body body)
+{
+ int fds[2];
+ VERIFY(pipe(fds) == 0);
+ auto child = fork();
+ VERIFY(child >= 0);
+ if (child == 0) {
+ close(fds[0]);
+ check(60, dup2(fds[1], STDERR_FILENO) == STDERR_FILENO);
+ close(fds[1]);
+ check(61, setenv("LADYBIRD_SANDBOX_DEBUG", "1", 1) == 0);
+ body();
+ _exit(0);
+ }
+
+ close(fds[1]);
+ StringBuilder output;
+ char buffer[4096];
+ ssize_t received = 0;
+ while ((received = read(fds[0], buffer, sizeof(buffer))) > 0)
+ output.append(StringView { buffer, static_cast<size_t>(received) });
+ close(fds[0]);
+
+ int status = 0;
+ VERIFY(waitpid(child, &status, 0) == child);
+ EXPECT(WIFEXITED(status));
+ if (WIFEXITED(status))
+ EXPECT_EQ(WEXITSTATUS(status), 0);
+ return output.to_byte_string();
+}
+
+TEST_CASE(sandbox_debugging_logs_each_refused_path_once)
+{
+ char base_template[] = "/tmp/lb-file-broker-XXXXXX";
+ VERIFY(mkdtemp(base_template));
+ ByteString base { base_template };
+ auto read_only = ByteString::formatted("{}/read-only", base);
+ auto hidden = ByteString::formatted("{}/hidden", base);
+ auto hidden_file = ByteString::formatted("{}/file", hidden);
+ VERIFY(mkdir(read_only.characters(), 0700) == 0);
+ VERIFY(mkdir(hidden.characters(), 0700) == 0);
+ make_file(hidden_file);
+
+ auto output = stderr_of_debugged_child([&] {
+ Vector<PathRule> rules;
+ MUST(Sandbox::add_path_if_exists(rules, read_only, PathRule::Access::ReadOnly));
+ MUST(Sandbox::add_path_if_exists(rules, "/proc/self"sv, PathRule::Access::ReadOnly));
+ confine(rules);
+
+ check(1, error_opening(hidden_file, O_RDONLY) == EACCES);
+ check(2, error_opening(hidden_file, O_RDONLY) == EACCES);
+ // A directory that exists is reported as such, which is no refusal to log.
+ check(3, mkdir(read_only.characters(), 0700) < 0 && errno == EEXIST);
+ });
+
+ auto refusal = ByteString::formatted("read access to {}\n", hidden_file);
+ EXPECT_EQ(output.count(refusal), 1u);
+ EXPECT(!output.contains("entry change"sv));
+
+ (void)unlink(hidden_file.characters());
+ (void)rmdir(hidden.characters());
+ (void)rmdir(read_only.characters());
+ (void)rmdir(base.characters());
+}
+
TEST_CASE(the_broker_does_not_follow_a_link_out_of_a_writable_directory)
{
char base_template[] = "/tmp/lb-file-broker-XXXXXX";