Truly independent web browser
LibWeb: Protect process-wide loader and crypto state
Andreas Kling32342aeff352
diff --git a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Libraries/LibWeb/Crypto/SubtleCrypto.cppindex 62b28c70e4b..f8b3f032265 100644--- a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp+++ b/Libraries/LibWeb/Crypto/SubtleCrypto.cpp@@ -36,6 +36,7 @@ using SupportedAlgorithmsMap = HashMap<String, RegisteredAlgorithms>;static SupportedAlgorithmsMap& supported_algorithms_internal();static SupportedAlgorithmsMap const& supported_algorithms();+static void define_supported_algorithms();template<typename Methods, typename Param = AlgorithmParams>static void define_an_algorithm(String op, StringView algorithm);@@ -1876,14 +1877,20 @@ SupportedAlgorithmsMap& supported_algorithms_internal()return *supported_algorithms;}-// https://w3c.github.io/webcrypto/#algorithm-normalization-internalSupportedAlgorithmsMap const& supported_algorithms(){- auto& internal_object = supported_algorithms_internal();+ // NB: Every agent of the process reads this map, so it is defined once and never changes after that.+ [[maybe_unused]] static bool const is_defined = [] {+ define_supported_algorithms();+ return true;+ }();+ return supported_algorithms_internal();+}- if (!internal_object.is_empty()) {- return internal_object;- }+// https://w3c.github.io/webcrypto/#algorithm-normalization-internal+void define_supported_algorithms()+{+ auto& internal_object = supported_algorithms_internal();// 1. For each value, v in the List of supported operations,// set the v key of the internal object supportedAlgorithms to a new associative container.@@ -2087,8 +2094,6 @@ SupportedAlgorithmsMap const& supported_algorithms()define_an_algorithm<KMAC>("exportKey"_string, algorithm);define_an_algorithm<KMAC, KmacImportParams>("get key length"_string, algorithm);}-- return internal_object;}// https://w3c.github.io/webcrypto/#concept-define-an-algorithmdiff --git a/Libraries/LibWeb/Fetch/Infrastructure/PreflightCache.cpp b/Libraries/LibWeb/Fetch/Infrastructure/PreflightCache.cppindex 99f9b3002df..c630df54f90 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/PreflightCache.cpp+++ b/Libraries/LibWeb/Fetch/Infrastructure/PreflightCache.cpp@@ -70,7 +70,8 @@ void PreflightCache::create_a_new_cache_entry(Request const& request, AK::Durati// https://fetch.spec.whatwg.org/#concept-cache-match-methodbool PreflightCache::is_a_method_cache_entry_match(Request const& request, ByteString const& method){- evict_expired_entries();+ MutexLocker locker(m_mutex);+ remove_expired_entries();// There is a method cache entry match for method using request when there is a cache entry in the user agent’s// CORS-preflight cache for which there is a cache entry match with request and its method is method or `*`.@@ -90,7 +91,8 @@ PreflightCache::Entry* PreflightCache::method_cache_entry_match(Request const& rvoid PreflightCache::cache_method(Request const& request, ByteString const& method, AK::Duration max_age){- evict_expired_entries();+ MutexLocker locker(m_mutex);+ remove_expired_entries();if (auto* entry = method_cache_entry_match(request, method); entry != nullptr) {entry->update_max_age(max_age);@@ -103,7 +105,8 @@ void PreflightCache::cache_method(Request const& request, ByteString const& meth// https://fetch.spec.whatwg.org/#concept-cache-match-headerbool PreflightCache::is_a_header_name_cache_entry_match(Request const& request, ByteString const& header_name){- evict_expired_entries();+ MutexLocker locker(m_mutex);+ remove_expired_entries();// There is a header-name cache entry match for headerName using request when there is a cache entry in the user// agent’s CORS-preflight cache for which there is a cache entry match with request and one of:@@ -135,7 +138,8 @@ PreflightCache::Entry* PreflightCache::header_name_cache_entry_match(Request convoid PreflightCache::cache_header_name(Request const& request, ByteString const& header_name, AK::Duration max_age){- evict_expired_entries();+ MutexLocker locker(m_mutex);+ remove_expired_entries();if (auto* entry = header_name_cache_entry_match(request, header_name); entry != nullptr) {entry->update_max_age(max_age);@@ -171,6 +175,12 @@ bool PreflightCache::is_cache_entry_match(Entry const& entry, Request const& req}void PreflightCache::evict_expired_entries()+{+ MutexLocker locker(m_mutex);+ remove_expired_entries();+}++void PreflightCache::remove_expired_entries(){m_entries.remove_all_matching([](Entry const& entry) {return entry.has_expired();@@ -181,6 +191,7 @@ void PreflightCache::evict_expired_entries()void PreflightCache::clear_cache_entries(Request const& request){auto request_key = determine_the_network_partition_key(request);+ MutexLocker locker(m_mutex);// To clear cache entries, given a request, remove any cache entries in the user agent’s CORS-preflight cache whosem_entries.remove_all_matching([&](Entry const& entry) {diff --git a/Libraries/LibWeb/Fetch/Infrastructure/PreflightCache.h b/Libraries/LibWeb/Fetch/Infrastructure/PreflightCache.hindex 142ba30f08f..ff3ff8c46a3 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/PreflightCache.h+++ b/Libraries/LibWeb/Fetch/Infrastructure/PreflightCache.h@@ -63,11 +63,15 @@ private:void update_max_age(AK::Duration max_age);};+ void remove_expired_entries();void create_a_new_cache_entry(Request const&, AK::Duration max_age, Optional<ByteString> method, Optional<ByteString> header_name);Entry* method_cache_entry_match(Request const&, ByteString const& method);Entry* header_name_cache_entry_match(Request const&, ByteString const& header_name);static bool is_cache_entry_match(Entry const&, Request const&);+ // The agents of a process share this cache.+ Mutex m_mutex;+// A CORS-preflight cache is a list of cache entries.Vector<Entry> m_entries;};diff --git a/Libraries/LibWeb/Loader/ContentBlocker.cpp b/Libraries/LibWeb/Loader/ContentBlocker.cppindex bccd55ddf64..6b634caf30b 100644--- a/Libraries/LibWeb/Loader/ContentBlocker.cpp+++ b/Libraries/LibWeb/Loader/ContentBlocker.cpp@@ -23,7 +23,7 @@ ContentBlocker::ContentBlocker() = default;ContentBlocker::~ContentBlocker(){- ContentBlocking::FFI::rust_content_blocker_free(m_engine);+ ContentBlocking::FFI::rust_content_blocker_free(m_engine.load());}static StringView resource_type_to_adblock_request_type(ContentBlocker::ResourceType type)@@ -155,8 +155,8 @@ ErrorOr<void> ContentBlocker::set_rules_from_bytes(ReadonlyBytes rules_bytes)// rules to put in it, and every caller already treats a missing engine as "nothing is blocked".// Drop the engine instead of rebuilding an empty one.if (rules_bytes.is_empty()) {- ContentBlocking::FFI::rust_content_blocker_free(m_engine);- m_engine = nullptr;+ MutexLocker locker(m_mutex);+ ContentBlocking::FFI::rust_content_blocker_free(m_engine.exchange(nullptr));m_has_cosmetic_rules = false;return {};}@@ -167,8 +167,8 @@ ErrorOr<void> ContentBlocker::set_rules_from_bytes(ReadonlyBytes rules_bytes)auto has_cosmetic_rules = rules_contain_cosmetic_rules(rules_bytes);- ContentBlocking::FFI::rust_content_blocker_free(m_engine);- m_engine = engine;+ MutexLocker locker(m_mutex);+ ContentBlocking::FFI::rust_content_blocker_free(m_engine.exchange(engine));m_has_cosmetic_rules = has_cosmetic_rules;return {};}@@ -185,7 +185,7 @@ bool ContentBlocker::is_filtered(URL::URL const& url, URL::URL const& source_urlbool ContentBlocker::is_filtered(URL::URL const& url, URL::URL const& source_url, ResourceType resource_type) const{- if (!m_engine)+ if (!has_rules())return false;if (url.scheme() == "data"sv)@@ -196,8 +196,12 @@ bool ContentBlocker::is_filtered(URL::URL const& url, URL::URL const& source_urlauto source_url_string = serialized_url_for_matching(normalized_source_url);auto request_type = resource_type_to_adblock_request_type(resource_type);+ MutexLocker locker(m_mutex);+ auto* engine = m_engine.load();+ if (!engine)+ return false;return ContentBlocking::FFI::rust_content_blocker_matches(- m_engine,+ engine,url_string.bytes(),source_url_string.bytes(),request_type.bytes());@@ -210,7 +214,7 @@ Utf16String ContentBlocker::cosmetic_style_sheet_for_url(URL::URL const& url) coUtf16String ContentBlocker::cosmetic_style_sheet_for_url(URL::URL const& url, ReadonlySpan<Utf16FlyString> classes, ReadonlySpan<Utf16FlyString> ids) const{- if (!m_engine || !m_has_cosmetic_rules)+ if (!has_cosmetic_rules())return {};auto url_string = serialized_url(url);@@ -225,8 +229,12 @@ Utf16String ContentBlocker::cosmetic_style_sheet_for_url(URL::URL const& url, Reauto classes_bytes = classes_string.value().bytes_as_string_view();auto ids_bytes = ids_string.value().bytes_as_string_view();+ MutexLocker locker(m_mutex);+ auto* engine = m_engine.load();+ if (!engine)+ return {};return take_rust_utf16_string(ContentBlocking::FFI::rust_content_blocker_cosmetic_css(- m_engine,+ engine,url_string.bytes(),classes_bytes.bytes(),ids_bytes.bytes()));@@ -234,7 +242,7 @@ Utf16String ContentBlocker::cosmetic_style_sheet_for_url(URL::URL const& url, Rebool ContentBlocker::has_generic_cosmetic_selectors_for_url(URL::URL const& url, ReadonlySpan<Utf16FlyString> classes, ReadonlySpan<Utf16FlyString> ids) const{- if (!m_engine || !m_has_cosmetic_rules)+ if (!has_cosmetic_rules())return false;auto url_string = serialized_url(url);@@ -249,8 +257,12 @@ bool ContentBlocker::has_generic_cosmetic_selectors_for_url(URL::URL const& url,auto classes_bytes = classes_string.value().bytes_as_string_view();auto ids_bytes = ids_string.value().bytes_as_string_view();+ MutexLocker locker(m_mutex);+ auto* engine = m_engine.load();+ if (!engine)+ return false;return ContentBlocking::FFI::rust_content_blocker_has_generic_cosmetic_selectors(- m_engine,+ engine,url_string.bytes(),classes_bytes.bytes(),ids_bytes.bytes());diff --git a/Libraries/LibWeb/Loader/ContentBlocker.h b/Libraries/LibWeb/Loader/ContentBlocker.hindex be3aa176401..630535d0f1f 100644--- a/Libraries/LibWeb/Loader/ContentBlocker.h+++ b/Libraries/LibWeb/Loader/ContentBlocker.h@@ -43,8 +43,8 @@ public:static ContentBlocker& the();- bool has_rules() const { return m_engine != nullptr; }- bool has_cosmetic_rules() const { return m_has_cosmetic_rules; }+ bool has_rules() const { return m_engine.load(AK::MemoryOrder::memory_order_relaxed) != nullptr; }+ bool has_cosmetic_rules() const { return m_has_cosmetic_rules.load(AK::MemoryOrder::memory_order_relaxed); }bool is_filtered(URL::URL const&) const;bool is_filtered(URL::URL const&, URL::URL const& source_url, ResourceType) const;@@ -63,8 +63,11 @@ private:ContentBlocker();~ContentBlocker();- bool m_has_cosmetic_rules { false };- void* m_engine { nullptr };+ // Agents on any thread ask, and the engine is not one that several threads can use at once. The atomics let a check+ // for no rules at all skip the lock.+ mutable Mutex m_mutex;+ Atomic<bool> m_has_cosmetic_rules { false };+ Atomic<void*> m_engine { nullptr };};}diff --git a/Libraries/LibWeb/Loader/LoaderConfig.cpp b/Libraries/LibWeb/Loader/LoaderConfig.cppindex fcb70078fea..a3442d9e4bc 100644--- a/Libraries/LibWeb/Loader/LoaderConfig.cpp+++ b/Libraries/LibWeb/Loader/LoaderConfig.cpp@@ -4,6 +4,8 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/MutexProtected.h>+#include <AK/NeverDestroyed.h>#include <AK/RefPtr.h>#include <LibWeb/Loader/LoaderConfig.h>#include <LibWebCommon/Loader/UserAgent.h>@@ -20,23 +22,25 @@ static NonnullRefPtr<LoaderConfigSnapshot const> default_loader_config()return make_ref_counted<LoaderConfigSnapshot>(move(config));}-static RefPtr<LoaderConfigSnapshot const>& loader_config()+static MutexProtected<NonnullRefPtr<LoaderConfigSnapshot const>>& loader_config(){- static auto& loader_config = *new RefPtr<LoaderConfigSnapshot const>(default_loader_config());- return loader_config;+ static NeverDestroyed<MutexProtected<NonnullRefPtr<LoaderConfigSnapshot const>>> loader_config(default_loader_config());+ return *loader_config;}NonnullRefPtr<LoaderConfigSnapshot const> current_loader_config(){- return *loader_config();+ return loader_config().with_locked([](auto const& snapshot) { return snapshot; });}void update_loader_config(Function<void(LoaderConfig&)> const& change){- auto config = loader_config()->config;- change(config);- VERIFY(!config.preferred_languages.is_empty());- loader_config() = make_ref_counted<LoaderConfigSnapshot>(move(config));+ loader_config().with_locked([&](auto& snapshot) {+ auto config = snapshot->config;+ change(config);+ VERIFY(!config.preferred_languages.is_empty());+ snapshot = make_ref_counted<LoaderConfigSnapshot>(move(config));+ });}void set_site_compatibility_data(SiteCompatibilityData data)diff --git a/Libraries/LibWeb/Loader/LoaderConfig.h b/Libraries/LibWeb/Loader/LoaderConfig.hindex 32edec12933..13cb2c79732 100644--- a/Libraries/LibWeb/Loader/LoaderConfig.h+++ b/Libraries/LibWeb/Loader/LoaderConfig.h@@ -13,7 +13,7 @@namespace Web {-// The process's loader configuration, read and changed on the main thread. A snapshot already taken never changes.+// The process's loader configuration, which any thread may read and change. A snapshot already taken never changes.WEB_API NonnullRefPtr<LoaderConfigSnapshot const> current_loader_config();WEB_API void update_loader_config(Function<void(LoaderConfig&)> const& change);WEB_API void set_site_compatibility_data(SiteCompatibilityData);