Skip to content

Truly independent web browser

LibWebView: Open only ordinary files for a renderer

sideshowbarkerb20eb93cc9c5
diff --git a/Libraries/LibWebView/Utilities.cpp b/Libraries/LibWebView/Utilities.cpp
index 80101186c1b..aad0f13f5ae 100644
--- a/Libraries/LibWebView/Utilities.cpp
+++ b/Libraries/LibWebView/Utilities.cpp
@@ -20,6 +20,11 @@
# include <AK/Windows.h>
#endif
+#if defined(AK_OS_LINUX)
+# include <linux/magic.h>
+# include <sys/vfs.h>
+#endif
+
namespace WebView {
ErrorOr<Web::HTML::SelectedFile> create_selected_file(ByteString const& file_path)
@@ -38,6 +43,59 @@ ErrorOr<Web::HTML::SelectedFile> create_selected_file(ByteString const& file_pat
return Web::HTML::SelectedFile { move(name), IPC::File::adopt_file(move(file)) };
}
+#if !defined(AK_OS_WINDOWS)
+// The kernel's interface trees hold no file a page could have: What a pseudo-file there reads depends on the process
+// that opens it, which is the UI process here, and a device node or a descriptor link reaches whatever is behind it.
+static bool is_in_kernel_interface_tree(StringView canonical_path)
+{
+ for (auto tree : { "/proc"sv, "/sys"sv, "/dev"sv }) {
+ if (canonical_path == tree || (canonical_path.starts_with(tree) && canonical_path[tree.length()] == '/'))
+ return true;
+ }
+ return false;
+}
+
+# if defined(AK_OS_LINUX)
+// A process information or sysfs file system can be mounted anywhere, so the trees' paths alone don't rule them out.
+static ErrorOr<bool> is_on_kernel_interface_file_system(int fd)
+{
+ struct statfs file_system;
+ if (fstatfs(fd, &file_system) < 0)
+ return Error::from_syscall("fstatfs"sv, errno);
+ return file_system.f_type == PROC_SUPER_MAGIC || file_system.f_type == SYSFS_MAGIC;
+}
+# endif
+
+// Opens a canonical path one component at a time, refusing a symbolic link at any of them: The path had none when it
+// was resolved, so one there now was put in place since, and could lead anywhere.
+static ErrorOr<int> open_without_following_links(StringView canonical_path, int options)
+{
+ // Each directory on the way only needs to be searched, as path resolution would.
+# if defined(O_PATH)
+ static constexpr int directory_options = O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC;
+# elif defined(O_SEARCH)
+ static constexpr int directory_options = O_SEARCH | O_NOFOLLOW | O_CLOEXEC;
+# else
+ static constexpr int directory_options = O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC;
+# endif
+
+ LexicalPath lexical_path { canonical_path };
+ auto const& components = lexical_path.parts_view();
+ auto fd = TRY(Core::System::open("/"sv, components.is_empty() ? options : directory_options));
+
+ for (size_t i = 0; i < components.size(); ++i) {
+ ByteString name = components[i];
+ auto next_fd = ::openat(fd, name.characters(), i + 1 == components.size() ? options | O_NOFOLLOW : directory_options);
+ auto error = errno;
+ ::close(fd);
+ if (next_fd < 0)
+ return Error::from_syscall("openat"sv, error);
+ fd = next_fd;
+ }
+ return fd;
+}
+#endif
+
ErrorOr<IPC::File> open_local_file_for_renderer(ByteString const& path)
{
#if defined(AK_OS_WINDOWS)
@@ -48,13 +106,23 @@ ErrorOr<IPC::File> open_local_file_for_renderer(ByteString const& path)
return Error::from_errno(EACCES);
return IPC::File::adopt_file(move(file));
#else
- auto fd = TRY(Core::System::open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
+ // Decide on the file the path names once every symbolic link in it is resolved, so that no link leads elsewhere.
+ auto canonical_path = TRY(FileSystem::real_path(path));
+ if (is_in_kernel_interface_tree(canonical_path))
+ return Error::from_errno(EACCES);
+
+ auto fd = TRY(open_without_following_links(canonical_path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
auto file = IPC::File::adopt_fd(fd);
auto stat = TRY(Core::System::fstat(fd));
if (!S_ISREG(stat.st_mode) && !S_ISDIR(stat.st_mode))
return Error::from_errno(EACCES);
+# if defined(AK_OS_LINUX)
+ if (TRY(is_on_kernel_interface_file_system(fd)))
+ return Error::from_errno(EACCES);
+# endif
+
// The renderer reads the file as it would any other, so it gets a blocking descriptor.
auto flags = TRY(Core::System::fcntl(fd, F_GETFL));
TRY(Core::System::fcntl(fd, F_SETFL, flags & ~O_NONBLOCK));
diff --git a/Libraries/LibWebView/Utilities.h b/Libraries/LibWebView/Utilities.h
index f611c46d5e0..bf861525179 100644
--- a/Libraries/LibWebView/Utilities.h
+++ b/Libraries/LibWebView/Utilities.h
@@ -22,7 +22,9 @@ namespace WebView {
WEBVIEW_API ErrorOr<Web::HTML::SelectedFile> create_selected_file(ByteString const&);
// Opens a local file a renderer asked for, read-only. Only regular files and directories are opened, and the open
-// never blocks, so a path naming a FIFO or a device cannot stall the UI process.
+// never blocks, so a path naming a FIFO or a device can't stall the UI process. The path counts once its symbolic links
+// are resolved, and nothing under /proc, /sys, or /dev is opened, since what a file there reads depends on the process
+// that opens it.
WEBVIEW_API ErrorOr<IPC::File> open_local_file_for_renderer(ByteString const& path);
WEBVIEW_API ErrorOr<JsonObject> read_json_file(ByteString const& path);
diff --git a/Tests/LibWeb/Text/expected/Fetch/file-url-subresources-of-system-files.txt b/Tests/LibWeb/Text/expected/Fetch/file-url-subresources-of-system-files.txt
new file mode 100644
index 00000000000..1915e06a261
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Fetch/file-url-subresources-of-system-files.txt
@@ -0,0 +1,8 @@
+a regular file: loaded
+/proc/self/maps: refused
+/proc/self/environ: refused
+/proc/self/fd/0: refused
+/sys/kernel/mm/transparent_hugepage/enabled: refused
+/dev/fd/0: refused
+/dev/stdin: refused
+/dev/null: refused
diff --git a/Tests/LibWeb/Text/input/Fetch/file-url-subresources-of-system-files.html b/Tests/LibWeb/Text/input/Fetch/file-url-subresources-of-system-files.html
new file mode 100644
index 00000000000..4af693a2d67
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/file-url-subresources-of-system-files.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A file: document loads other local files, but none of the kernel's process, device, or system information files:
+ // What those read depends on the process opening them.
+ function load(url) {
+ return new Promise(resolve => {
+ const script = document.createElement("script");
+ script.src = url;
+ script.onload = () => resolve("loaded");
+ script.onerror = () => resolve("refused");
+ document.head.appendChild(script);
+ });
+ }
+
+ promiseTest(async () => {
+ println(`a regular file: ${await load(new URL("file-script-canary.js", window.location.href).href)}`);
+ for (const path of [
+ "/proc/self/maps",
+ "/proc/self/environ",
+ "/proc/self/fd/0",
+ "/sys/kernel/mm/transparent_hugepage/enabled",
+ "/dev/fd/0",
+ "/dev/stdin",
+ "/dev/null",
+ ])
+ println(`${path}: ${await load(`file://${path}`)}`);
+ });
+</script>
diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txt
index 43c166c4ffa..38dd2565a29 100644
--- a/Tests/LibWebView/CMakeLists.txt
+++ b/Tests/LibWebView/CMakeLists.txt
@@ -107,6 +107,7 @@ if (APPLE OR LINUX)
TestCrashReportReview.cpp
TestCrashReportStore.cpp
TestFontService.cpp
+ TestOpenLocalFileForRenderer.cpp
TestRendererFontService.cpp
)
endif()
diff --git a/Tests/LibWebView/TestOpenLocalFileForRenderer.cpp b/Tests/LibWebView/TestOpenLocalFileForRenderer.cpp
new file mode 100644
index 00000000000..5ecf0b8a75f
--- /dev/null
+++ b/Tests/LibWebView/TestOpenLocalFileForRenderer.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/LexicalPath.h>
+#include <LibCore/File.h>
+#include <LibCore/System.h>
+#include <LibFileSystem/FileSystem.h>
+#include <LibTest/TestCase.h>
+#include <LibWebView/Utilities.h>
+#include <stdlib.h>
+
+// A renderer names the file it wants by path, and the UI process opens it as itself. So a path that reaches one of this
+// process's own descriptors, or a file whose content depends on the process reading it, is refused, however it's named.
+
+namespace {
+
+class TemporaryDirectory {
+public:
+ TemporaryDirectory()
+ {
+ char path_template[] = "/tmp/TestOpenLocalFileForRenderer.XXXXXX";
+ VERIFY(mkdtemp(path_template));
+ m_path = path_template;
+ }
+
+ ~TemporaryDirectory()
+ {
+ (void)FileSystem::remove(m_path, FileSystem::RecursionMode::Allowed);
+ }
+
+ ByteString const& path() const { return m_path; }
+ ByteString join(StringView name) const { return LexicalPath::join(m_path, name).string(); }
+
+private:
+ ByteString m_path;
+};
+
+}
+
+static void expect_refused(ByteString const& path)
+{
+ if (!WebView::open_local_file_for_renderer(path).is_error())
+ FAIL(ByteString::formatted("{} was opened", path));
+}
+
+TEST_CASE(regular_files_and_directories_open)
+{
+ TemporaryDirectory directory;
+ auto file_path = directory.join("file.txt"sv);
+ auto file = TRY_OR_FAIL(Core::File::open(file_path, Core::File::OpenMode::Write));
+ TRY_OR_FAIL(file->write_until_depleted("content"sv));
+
+ TRY_OR_FAIL(WebView::open_local_file_for_renderer(file_path));
+ TRY_OR_FAIL(WebView::open_local_file_for_renderer(directory.path()));
+
+ // A symbolic link to a regular file reaches the file.
+ auto link_path = directory.join("link.txt"sv);
+ TRY_OR_FAIL(Core::System::symlink(file_path, link_path));
+ auto linked = TRY_OR_FAIL(WebView::open_local_file_for_renderer(link_path));
+ auto linked_file = TRY_OR_FAIL(Core::File::adopt_fd(linked.take_fd(), Core::File::OpenMode::Read));
+ auto content = TRY_OR_FAIL(linked_file->read_until_eof());
+ EXPECT_EQ(StringView { content }, "content"sv);
+}
+
+TEST_CASE(descriptor_links_are_refused)
+{
+ TemporaryDirectory directory;
+ auto file_path = directory.join("file.txt"sv);
+ auto file = TRY_OR_FAIL(Core::File::open(file_path, Core::File::OpenMode::ReadWrite));
+ TRY_OR_FAIL(file->write_until_depleted("content"sv));
+
+ // A descriptor link opens this process's own descriptor, even one for a file that's no longer at any path.
+ TRY_OR_FAIL(Core::System::unlink(file_path));
+ auto descriptor_path = ByteString::formatted("/dev/fd/{}", file->fd());
+ expect_refused(descriptor_path);
+
+ auto link_path = directory.join("link"sv);
+ TRY_OR_FAIL(Core::System::symlink(descriptor_path, link_path));
+ expect_refused(link_path);
+
+ expect_refused("/dev/null"sv);
+}
+
+#if defined(AK_OS_LINUX)
+TEST_CASE(process_and_system_information_is_refused)
+{
+ expect_refused("/proc/self/maps"sv);
+ expect_refused("/proc/self/environ"sv);
+ expect_refused("/proc/cpuinfo"sv);
+ expect_refused("/sys/kernel/mm/transparent_hugepage/enabled"sv);
+
+ TemporaryDirectory directory;
+ auto link_path = directory.join("link"sv);
+ TRY_OR_FAIL(Core::System::symlink("/proc/self/maps"sv, link_path));
+ expect_refused(link_path);
+}
+#endif