Skip to content

Truly independent web browser

RequestServer: Let the Linux sandbox read linked CA certificates

Andreas Kling2f1578f2c87e
diff --git a/Services/RequestServer/SandboxLinux.cpp b/Services/RequestServer/SandboxLinux.cpp
index 0a9742c5549..7c628e70fb0 100644
--- a/Services/RequestServer/SandboxLinux.cpp
+++ b/Services/RequestServer/SandboxLinux.cpp
@@ -4,10 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/AllOf.h>
#include <AK/CharacterTypes.h>
#include <AK/String.h>
+#include <LibCore/DirIterator.h>
#include <LibCore/Environment.h>
#include <LibCore/File.h>
+#include <LibCore/System.h>
#include <LibSandbox/Sandbox.h>
#include <LibSandbox/Seccomp.h>
#include <RequestServer/ResourceSubstitutionMap.h>
@@ -18,9 +21,63 @@
#include <openssl/crypto.h>
#include <openssl/x509.h>
#include <string.h>
+#include <sys/stat.h>
namespace RequestServer {
+// The links are named after eight hexadecimal digits of the hash and a decimal counter, such as 002c0b4f.0.
+static bool is_certificate_hash_link_name(StringView name)
+{
+ if (name.length() < 10 || name[8] != '.')
+ return false;
+ return all_of(name.substring_view(0, 8), is_ascii_hex_digit) && all_of(name.substring_view(9), is_ascii_digit);
+}
+
+// OpenSSL looks certificates up in a directory through links named after a hash of their subject, and distributions
+// point those links at files that live elsewhere, such as /usr/share/ca-certificates. A rule on the directory doesn't
+// reach those files, so grant each of them as well.
+static ErrorOr<void> add_certificate_directory(Vector<Sandbox::PathRule>& paths, StringView directory)
+{
+ // OpenSSL skips the directories in a list that it can't read, so one of those must not keep the sandbox from
+ // applying.
+ if (auto metadata = Core::System::stat(directory); metadata.is_error() || !S_ISDIR(metadata.value().st_mode))
+ return {};
+
+ // Nothing is added for a directory that an earlier list already named.
+ auto rule_count = paths.size();
+ TRY(Sandbox::add_path_if_exists(paths, directory, Sandbox::PathRule::Access::ReadOnly));
+ if (paths.size() == rule_count)
+ return {};
+
+ Core::DirIterator iterator { directory.to_byte_string(), Core::DirIterator::SkipParentAndBaseDir };
+ while (iterator.has_next()) {
+ auto entry = iterator.next();
+ if (entry->type != Core::DirectoryEntry::Type::SymbolicLink && entry->type != Core::DirectoryEntry::Type::Unknown)
+ continue;
+ if (!is_certificate_hash_link_name(entry->name))
+ continue;
+ auto link = ByteString::formatted("{}/{}", directory, entry->name);
+
+ // A link to a directory would grant everything below it, and a certificate is always a file.
+ auto target_metadata = Core::System::stat(link);
+ if (target_metadata.is_error() || !S_ISREG(target_metadata.value().st_mode))
+ continue;
+
+ // The rule is bound to whatever the link resolves to when the sandbox opens it.
+ TRY(Sandbox::add_path_if_exists(paths, link, Sandbox::PathRule::Access::ReadOnly));
+ }
+
+ return {};
+}
+
+// OpenSSL and libcurl both accept a list of certificate directories separated by colons.
+static ErrorOr<void> add_certificate_directories(Vector<Sandbox::PathRule>& paths, StringView directories)
+{
+ for (auto directory : directories.split_view(':'))
+ TRY(add_certificate_directory(paths, directory));
+ return {};
+}
+
// Landlock binds to inodes. Open each bundle path to grant its symlink target, including targets outside /etc/ssl.
static ErrorOr<void> add_certificate_store_paths(Vector<Sandbox::PathRule>& paths)
{
@@ -35,16 +92,21 @@ static ErrorOr<void> add_certificate_store_paths(Vector<Sandbox::PathRule>& path
if (version_info->cainfo)
TRY(Sandbox::add_path_if_exists(paths, ByteString { version_info->cainfo }, Sandbox::PathRule::Access::ReadOnly));
if (version_info->capath)
- TRY(Sandbox::add_path_if_exists(paths, ByteString { version_info->capath }, Sandbox::PathRule::Access::ReadOnly));
+ TRY(add_certificate_directories(paths, StringView { version_info->capath, strlen(version_info->capath) }));
}
// libcurl is built with CURL_CA_FALLBACK, so OpenSSL's default store is consulted when no bundle is set.
TRY(Sandbox::add_path_if_exists(paths, ByteString { X509_get_default_cert_file() }, Sandbox::PathRule::Access::ReadOnly));
- TRY(Sandbox::add_path_if_exists(paths, ByteString { X509_get_default_cert_dir() }, Sandbox::PathRule::Access::ReadOnly));
- for (auto variable : { X509_get_default_cert_file_env(), X509_get_default_cert_dir_env() }) {
- if (auto value = Core::Environment::get(StringView { variable, strlen(variable) }); value.has_value() && !value->is_empty())
- TRY(Sandbox::add_path_if_exists(paths, *value, Sandbox::PathRule::Access::ReadOnly));
- }
+ auto const* default_certificate_directory = X509_get_default_cert_dir();
+ TRY(add_certificate_directories(paths, StringView { default_certificate_directory, strlen(default_certificate_directory) }));
+
+ auto const* certificate_file_variable = X509_get_default_cert_file_env();
+ if (auto value = Core::Environment::get(StringView { certificate_file_variable, strlen(certificate_file_variable) }); value.has_value() && !value->is_empty())
+ TRY(Sandbox::add_path_if_exists(paths, *value, Sandbox::PathRule::Access::ReadOnly));
+
+ auto const* certificate_directory_variable = X509_get_default_cert_dir_env();
+ if (auto value = Core::Environment::get(StringView { certificate_directory_variable, strlen(certificate_directory_variable) }); value.has_value())
+ TRY(add_certificate_directories(paths, *value));
return {};
}
diff --git a/Tests/RequestServer/TestResourceMapSandbox.cpp b/Tests/RequestServer/TestResourceMapSandbox.cpp
index f1cea506c4f..c9fb8344d70 100644
--- a/Tests/RequestServer/TestResourceMapSandbox.cpp
+++ b/Tests/RequestServer/TestResourceMapSandbox.cpp
@@ -124,6 +124,68 @@ TEST_CASE(certificate_files_are_readable_without_granting_access_to_neighbors)
EXPECT_EQ(WEXITSTATUS(status), 0);
}
+TEST_CASE(certificate_directory_links_are_readable_without_granting_access_to_neighbors)
+{
+ if (!landlock_is_available())
+ return;
+
+ char directory_template[] = "/tmp/ladybird-certificate-directory-XXXXXX";
+ auto* directory = mkdtemp(directory_template);
+ VERIFY(directory);
+ ScopeGuard cleanup = [&] {
+ MUST(FileSystem::remove(ByteString { directory }, FileSystem::RecursionMode::Allowed));
+ };
+
+ // Distributions keep the certificates outside of the directory that OpenSSL searches, which only links to them.
+ auto store_path = ByteString::formatted("{}/store", directory);
+ auto shared_path = ByteString::formatted("{}/shared", directory);
+ auto private_path = ByteString::formatted("{}/private", directory);
+ for (auto const& path : { store_path, shared_path, private_path })
+ VERIFY(mkdir(path.characters(), 0700) == 0);
+
+ auto certificate_path = ByteString::formatted("{}/certificate.pem", shared_path);
+ auto neighbor_path = ByteString::formatted("{}/neighbor.pem", shared_path);
+ auto key_path = ByteString::formatted("{}/key.pem", private_path);
+ auto write_file = [](ByteString const& path, StringView content) {
+ auto file = MUST(Core::File::open(path, Core::File::OpenMode::Write));
+ MUST(file->write_until_depleted(content.bytes()));
+ };
+ write_file(certificate_path, "certificate"sv);
+ write_file(neighbor_path, "neighbor"sv);
+ write_file(key_path, "key"sv);
+
+ auto certificate_link_path = ByteString::formatted("{}/0a1b2c3d.0", store_path);
+ VERIFY(symlink("../shared/certificate.pem", certificate_link_path.characters()) == 0);
+ auto directory_link_path = ByteString::formatted("{}/0a1b2c3d.1", store_path);
+ auto unrelated_link_path = ByteString::formatted("{}/neighbor.pem", store_path);
+ VERIFY(symlink(private_path.characters(), directory_link_path.characters()) == 0);
+ VERIFY(symlink("../shared/neighbor.pem", unrelated_link_path.characters()) == 0);
+
+ auto child = fork();
+ VERIFY(child >= 0);
+ if (child == 0) {
+ // A list may name directories that don't exist or can't be searched, and OpenSSL skips those.
+ VERIFY(setenv("SSL_CERT_DIR", ByteString::formatted("{}/missing:{}/certificate.pem/store:{}", directory, shared_path, store_path).characters(), 1) == 0);
+ MUST(RequestServer::apply_sandbox({}, {}, {}));
+
+ auto certificate_file = MUST(Core::File::open(certificate_link_path, Core::File::OpenMode::Read));
+ auto contents = MUST(certificate_file->read_until_eof());
+ VERIFY(StringView(contents) == "certificate"sv);
+
+ VERIFY(open(neighbor_path.characters(), O_RDONLY) == -1);
+ VERIFY(errno == EACCES);
+ VERIFY(open(ByteString::formatted("{}/key.pem", directory_link_path).characters(), O_RDONLY) == -1);
+ VERIFY(errno == EACCES);
+ _exit(0);
+ }
+
+ int status = 0;
+ VERIFY(waitpid(child, &status, 0) == child);
+ EXPECT(WIFEXITED(status));
+ if (WIFEXITED(status))
+ EXPECT_EQ(WEXITSTATUS(status), 0);
+}
+
TEST_CASE(vectored_io_and_permission_changes_work_in_the_sandbox)
{
if (!landlock_is_available())