Skip to content

Truly independent web browser

LibSandbox+Services: Allow reading the directories of loaded libraries

Jelle Raaijmakers4ad3e4a56f15
diff --git a/Libraries/LibSandbox/Sandbox.cpp b/Libraries/LibSandbox/Sandbox.cpp
index 66bd082f6b0..a8f10dfc57b 100644
--- a/Libraries/LibSandbox/Sandbox.cpp
+++ b/Libraries/LibSandbox/Sandbox.cpp
@@ -13,6 +13,7 @@
# include <dirent.h>
# include <errno.h>
# include <fcntl.h>
+# include <link.h>
# include <linux/landlock.h>
# include <stdlib.h>
# include <string.h>
@@ -83,6 +84,28 @@ ErrorOr<void> add_landlock_path_if_exists(Vector<LandlockPath>& paths, StringVie
TRY(paths.try_append({ move(path_bytes), access, is_directory }));
return {};
}
+
+ErrorOr<void> add_loaded_library_directories(Vector<LandlockPath>& paths, Optional<StringView> root)
+{
+ Vector<ByteString> directories;
+ dl_iterate_phdr([](dl_phdr_info* info, size_t, void* data) {
+ auto& directories = *static_cast<Vector<ByteString>*>(data);
+ StringView name { info->dlpi_name, strlen(info->dlpi_name) };
+ if (name.starts_with('/')) {
+ auto directory = LexicalPath::dirname(name);
+ if (!directories.contains_slow(directory))
+ directories.append(move(directory));
+ }
+ return 0;
+ },
+ &directories);
+
+ for (auto const& directory : directories) {
+ if (!root.has_value() || LexicalPath { directory }.is_child_of(LexicalPath { root->to_byte_string() }))
+ TRY(add_landlock_path_if_exists(paths, directory, LandlockPath::Access::ReadOnly));
+ }
+ return {};
+}
#endif
#if defined(AK_OS_MACOS)
diff --git a/Libraries/LibSandbox/Sandbox.h b/Libraries/LibSandbox/Sandbox.h
index 984a9396b38..7d17e6de0da 100644
--- a/Libraries/LibSandbox/Sandbox.h
+++ b/Libraries/LibSandbox/Sandbox.h
@@ -83,6 +83,7 @@ struct SeatbeltProfile {
#if defined(AK_OS_LINUX)
[[nodiscard]] ErrorOr<void> add_landlock_path_if_exists(Vector<LandlockPath>& paths, StringView path, LandlockPath::Access);
+[[nodiscard]] ErrorOr<void> add_loaded_library_directories(Vector<LandlockPath>& paths, Optional<StringView> root = {});
[[nodiscard]] ErrorOr<void> restrict_filesystem_with_landlock(ReadonlySpan<LandlockPath> = {});
#endif
diff --git a/Services/AudioServer/SandboxLinux.cpp b/Services/AudioServer/SandboxLinux.cpp
index 25eb84e8945..5b68ba559e1 100644
--- a/Services/AudioServer/SandboxLinux.cpp
+++ b/Services/AudioServer/SandboxLinux.cpp
@@ -26,6 +26,7 @@ ErrorOr<void> apply_sandbox(StringView)
// cpptrace opens loaded ELF objects when symbolizing in-process stack traces.
TRY(Sandbox::add_landlock_path_if_exists(paths, executable_path, Sandbox::LandlockPath::Access::ReadOnly));
TRY(Sandbox::add_landlock_path_if_exists(paths, LexicalPath::join(build_root, "lib"sv).string(), Sandbox::LandlockPath::Access::ReadOnly));
+ TRY(Sandbox::add_loaded_library_directories(paths, build_root));
TRY(Sandbox::add_landlock_path_if_exists(paths, "/proc/self"sv, Sandbox::LandlockPath::Access::ReadOnly));
// libpulse still keeps its cookie and runtime files in these, so they stay readable.
diff --git a/Services/Compositor/SandboxLinux.cpp b/Services/Compositor/SandboxLinux.cpp
index e8c1f255b0c..b7d9a521af8 100644
--- a/Services/Compositor/SandboxLinux.cpp
+++ b/Services/Compositor/SandboxLinux.cpp
@@ -17,7 +17,6 @@
#include <LibCore/System.h>
#include <LibSandbox/Sandbox.h>
#include <LibSandbox/Seccomp.h>
-#include <link.h>
#include <string.h>
namespace Compositor {
@@ -128,28 +127,6 @@ static ErrorOr<void> add_loader_manifest_paths(Vector<Sandbox::LandlockPath>& pa
return {};
}
-// The driver loads more libraries after Landlock is in place, and they must come from where the libraries that we
-// already use came from. Otherwise the dynamic loader skips to another copy, such as the system's instead of our own.
-static ErrorOr<void> add_loaded_library_directories(Vector<Sandbox::LandlockPath>& paths)
-{
- Vector<ByteString> directories;
- dl_iterate_phdr([](dl_phdr_info* info, size_t, void* data) {
- auto& directories = *static_cast<Vector<ByteString>*>(data);
- StringView name { info->dlpi_name, strlen(info->dlpi_name) };
- if (name.starts_with('/')) {
- auto directory = LexicalPath::dirname(name);
- if (!directories.contains_slow(directory))
- directories.append(move(directory));
- }
- return 0;
- },
- &directories);
-
- for (auto const& directory : directories)
- TRY(Sandbox::add_landlock_path_if_exists(paths, directory, Sandbox::LandlockPath::Access::ReadOnly));
- return {};
-}
-
ErrorOr<void> restrict_filesystem(StringView resource_root)
{
TRY(Sandbox::install_no_new_privileges());
@@ -161,7 +138,9 @@ ErrorOr<void> restrict_filesystem(StringView resource_root)
TRY(Sandbox::add_landlock_path_if_exists(paths, "/lib64"sv, Sandbox::LandlockPath::Access::ReadOnly));
TRY(Sandbox::add_landlock_path_if_exists(paths, "/usr/lib"sv, Sandbox::LandlockPath::Access::ReadOnly));
TRY(Sandbox::add_landlock_path_if_exists(paths, "/usr/local/lib"sv, Sandbox::LandlockPath::Access::ReadOnly));
- TRY(add_loaded_library_directories(paths));
+ // The driver loads more libraries after Landlock is in place, and they must come from where the libraries that we
+ // already use came from. Otherwise the dynamic loader skips to another copy, such as the system's instead of our own.
+ TRY(Sandbox::add_loaded_library_directories(paths));
// cpptrace opens loaded ELF objects when symbolizing in-process stack traces, and drivers read it too.
TRY(Sandbox::add_landlock_path_if_exists(paths, TRY(Core::System::current_executable_path()), Sandbox::LandlockPath::Access::ReadOnly));
TRY(Sandbox::add_landlock_path_if_exists(paths, "/etc/glvnd"sv, Sandbox::LandlockPath::Access::ReadOnly));
diff --git a/Services/MediaServer/SandboxLinux.cpp b/Services/MediaServer/SandboxLinux.cpp
index 8a63cbf830f..e556900d248 100644
--- a/Services/MediaServer/SandboxLinux.cpp
+++ b/Services/MediaServer/SandboxLinux.cpp
@@ -24,6 +24,7 @@ ErrorOr<void> apply_sandbox(StringView)
// cpptrace opens loaded ELF objects when symbolizing in-process stack traces.
TRY(Sandbox::add_landlock_path_if_exists(paths, executable_path, Sandbox::LandlockPath::Access::ReadOnly));
TRY(Sandbox::add_landlock_path_if_exists(paths, LexicalPath::join(build_root, "lib"sv).string(), Sandbox::LandlockPath::Access::ReadOnly));
+ TRY(Sandbox::add_loaded_library_directories(paths, build_root));
TRY(Sandbox::add_landlock_path_if_exists(paths, "/proc/self"sv, Sandbox::LandlockPath::Access::ReadOnly));
TRY(Sandbox::restrict_filesystem_with_landlock(paths.span()));
diff --git a/Services/RendererSandboxLinux.cpp b/Services/RendererSandboxLinux.cpp
index 607f04053a2..b648ff0226c 100644
--- a/Services/RendererSandboxLinux.cpp
+++ b/Services/RendererSandboxLinux.cpp
@@ -26,6 +26,7 @@ ErrorOr<void> apply_sandbox(StringView)
// cpptrace opens loaded ELF objects when symbolizing in-process stack traces.
TRY(Sandbox::add_landlock_path_if_exists(paths, executable_path, Sandbox::LandlockPath::Access::ReadOnly));
TRY(Sandbox::add_landlock_path_if_exists(paths, LexicalPath::join(build_root, "lib"sv).string(), Sandbox::LandlockPath::Access::ReadOnly));
+ TRY(Sandbox::add_loaded_library_directories(paths, build_root));
TRY(Sandbox::add_landlock_path_if_exists(paths, "/proc/self"sv, Sandbox::LandlockPath::Access::ReadOnly));
TRY(Sandbox::restrict_filesystem_with_landlock(paths.span()));