Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/LexicalPath.h>
#include <LibCore/System.h>
#include <LibSandbox/Sandbox.h>
#include <LibSandbox/Seccomp.h>
#include <MediaServer/Sandbox.h>
namespace MediaServer {
ErrorOr<void> apply_sandbox(StringView)
{
TRY(Sandbox::install_no_new_privileges());
TRY(Sandbox::configure_runtime());
auto executable_path = TRY(Core::System::current_executable_path());
auto build_root = LexicalPath::dirname(LexicalPath::dirname(executable_path));
Vector<Sandbox::PathRule> paths;
// cpptrace opens loaded ELF objects when symbolizing in-process stack traces.
TRY(Sandbox::add_path_if_exists(paths, executable_path, Sandbox::PathRule::Access::ReadOnly));
TRY(Sandbox::add_path_if_exists(paths, LexicalPath::join(build_root, "lib"sv).string(), Sandbox::PathRule::Access::ReadOnly));
TRY(Sandbox::add_loaded_library_directories(paths, build_root));
TRY(Sandbox::add_path_if_exists(paths, "/proc/self"sv, Sandbox::PathRule::Access::ReadOnly));
TRY(Sandbox::restrict_filesystem(paths.span()));
Sandbox::SeccompPolicy policy;
policy.allow_readonly_file_opens();
policy.allow_filesystem_metadata_queries();
policy.allow_file_descriptor_operations();
policy.allow_ipc();
policy.allow_socket_pairs();
policy.allow_common_runtime();
TRY(policy.install());
return {};
}
}