Skip to content

Truly independent web browser

3.4 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/ByteString.h>
#include <AK/EnumBits.h>
#include <AK/Error.h>
#include <AK/Optional.h>
#include <AK/Platform.h>
#include <AK/Span.h>
#include <AK/StringView.h>
#include <AK/Vector.h>

namespace Sandbox {

#if defined(AK_OS_LINUX)
struct PathRule {
    enum class Access {
        ReadOnly,
        ReadAndExecute,
        ReadWrite,
    };

    ByteString path;
    Access access { Access::ReadOnly };
    bool is_directory { false };
};
#endif

#if defined(AK_OS_MACOS)
struct SeatbeltPath {
    enum class Access {
        ReadOnly,
        ReadAndExecute,
        ReadWrite,
    };

    ByteString path;
    Access access { Access::ReadOnly };
    bool is_directory { false };
};

enum class NetworkAccess {
    Denied,
    Allowed,
    // Only UDP datagrams, sent to any host and received on sockets the helper binds itself, as ICE and mDNS need.
    Datagrams,
};

// System services that a helper may use, in addition to its own Browser endpoint.
enum class SystemService : u8 {
    None = 0,
    Fonts = 1 << 0,
    Audio = 1 << 1,
    VideoDecoding = 1 << 2,
    GPU = 1 << 3,
    IOSurface = 1 << 4,
    // Mapping MAP_JIT memory, for the code that JIT compilers generate.
    JIT = 1 << 5,
    // Enumerating displays and following their refresh, which CoreGraphics and CoreVideo do through the window server.
    Display = 1 << 6,
    // Decoding audio with the platform's codecs, in addition to playing it.
    AudioDecoding = 1 << 7,
};
AK_ENUM_BITWISE_OPERATORS(SystemService);

struct SeatbeltProfile {
    ReadonlySpan<SeatbeltPath> paths {};
    NetworkAccess network_access { NetworkAccess::Denied };
    ReadonlySpan<ByteString> executable_paths {};
    ReadonlySpan<StringView> iokit_user_client_classes {};

    // The bootstrap name of the Browser endpoint that the helper connects to after installing the sandbox.
    StringView mach_server_name {};
    SystemService system_services { SystemService::None };
};
#endif

[[nodiscard]] ErrorOr<void> install_no_new_privileges();
[[nodiscard]] ErrorOr<void> configure_runtime();

#if defined(AK_OS_LINUX)
[[nodiscard]] ErrorOr<void> add_path_if_exists(Vector<PathRule>& paths, StringView path, PathRule::Access);
[[nodiscard]] ErrorOr<void> add_loaded_library_directories(Vector<PathRule>& paths, Optional<StringView> root = {});
[[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

#if defined(AK_OS_MACOS)
[[nodiscard]] ErrorOr<void> add_seatbelt_path_if_exists(Vector<SeatbeltPath>& paths, StringView path, SeatbeltPath::Access);
[[nodiscard]] ErrorOr<void> apply_macos_sandbox(SeatbeltProfile const&);

// Returns the .app bundle that contains the executable, if the executable is in the bundle's Contents/MacOS directory.
[[nodiscard]] Optional<ByteString> application_bundle_for_executable(StringView executable_path);
#endif

}