Skip to content

Truly independent web browser

LibCore+LibSandbox: Initialize IOSurface before sandboxing on macOS

Timothy Flynnaa6b30d6ecb3
diff --git a/Libraries/LibCore/IOSurface.cpp b/Libraries/LibCore/IOSurface.cpp
index c2a8bcfdc54..b4a1ab663ef 100644
--- a/Libraries/LibCore/IOSurface.cpp
+++ b/Libraries/LibCore/IOSurface.cpp
@@ -45,6 +45,12 @@ struct IOSurfaceHandle::IOSurfaceRefWrapper {
IOSurfaceRef ref;
};
+void IOSurfaceHandle::initialize()
+{
+ // Querying the row alignment initializes the IOSurface connection without allocating a surface.
+ (void)IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, 0);
+}
+
IOSurfaceHandle::IOSurfaceHandle(OwnPtr<IOSurfaceRefWrapper>&& ref_wrapper)
: m_ref_wrapper(move(ref_wrapper))
{
diff --git a/Libraries/LibCore/IOSurface.h b/Libraries/LibCore/IOSurface.h
index f74ee7b322b..e8cf031d241 100644
--- a/Libraries/LibCore/IOSurface.h
+++ b/Libraries/LibCore/IOSurface.h
@@ -18,6 +18,8 @@ class CORE_API IOSurfaceHandle {
AK_MAKE_NONCOPYABLE(IOSurfaceHandle);
public:
+ static void initialize();
+
IOSurfaceHandle(IOSurfaceHandle&& other);
IOSurfaceHandle& operator=(IOSurfaceHandle&& other);
diff --git a/Libraries/LibSandbox/Sandbox.cpp b/Libraries/LibSandbox/Sandbox.cpp
index 784c011f2ca..07eed74fbfc 100644
--- a/Libraries/LibSandbox/Sandbox.cpp
+++ b/Libraries/LibSandbox/Sandbox.cpp
@@ -27,6 +27,7 @@
#if defined(AK_OS_MACOS)
# include <AK/LexicalPath.h>
# include <AK/StringBuilder.h>
+# include <LibCore/IOSurface.h>
# include <LibCore/StatsLog.h>
# include <errno.h>
# include <limits.h>
@@ -763,6 +764,11 @@ ErrorOr<void> apply_macos_sandbox(SeatbeltProfile const& options)
auto profile_string = profile.to_byte_string();
+ // IOSurface loads Launch Services preferences on first use. Do this before sandboxing, since a denied read
+ // makes Launch Services log errors whose formatting produces CoreFoundation warnings about URLs without schemes.
+ if (has_flag(options.system_services, SystemService::IOSurface))
+ Core::IOSurfaceHandle::initialize();
+
char* errorbuf = nullptr;
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
diff --git a/Tests/LibSandbox/TestHelperSandboxes.cpp b/Tests/LibSandbox/TestHelperSandboxes.cpp
index 98c67a267b8..c360db98510 100644
--- a/Tests/LibSandbox/TestHelperSandboxes.cpp
+++ b/Tests/LibSandbox/TestHelperSandboxes.cpp
@@ -9,6 +9,7 @@
#include <AK/LexicalPath.h>
#include <AK/ScopeGuard.h>
#include <CoreGraphics/CoreGraphics.h>
+#include <LibCore/IOSurface.h>
#include <LibCore/System.h>
#include <LibTest/TestCase.h>
#include <Security/Security.h>
@@ -33,6 +34,10 @@ enum class Outcome {
static Outcome run_in_helper_sandbox(Function<ErrorOr<void>()> const& apply_sandbox, Function<bool()> const& operation)
{
+ // These tests fork without exec, so initialize the native runtime in the parent. Objective-C cannot initialize
+ // classes in a child forked from a multithreaded process, such as one that has already queried CoreGraphics.
+ Core::IOSurfaceHandle::initialize();
+
auto child = fork();
VERIFY(child >= 0);
if (child == 0) {