Skip to content

Commit

Permalink
fix: libc++ buffer overflow in string_view ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Apr 18, 2023
1 parent dd0e744 commit c1b8515
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions patches/node/.patches
Expand Up @@ -31,3 +31,4 @@ fix_prevent_changing_functiontemplateinfo_after_publish.patch
enable_crashpad_linux_node_processes.patch
test_mark_cpu_prof_tests_as_flaky_in_electron.patch
fix_adapt_debugger_tests_for_upstream_v8_changes.patch
fix_libc_buffer_overflow_in_string_view_ctor.patch
37 changes: 37 additions & 0 deletions patches/node/fix_libc_buffer_overflow_in_string_view_ctor.patch
@@ -0,0 +1,37 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shelley Vohr <shelley.vohr@gmail.com>
Date: Tue, 18 Apr 2023 11:24:59 +0200
Subject: fix: libc++ buffer overflow in string_view ctor

Refs https://github.com/nodejs/node/pull/46410
Refs https://github.com/llvm/llvm-project/issues/61100.

Fixes a buffer overflow crash in std::string view constructor. This
happens as a result of a limitation of libc++'s implementation of the
string_view constructor. If string_view receives a pointer to a string start point,
and then a length as a size_t, the spec says that a size_t that exceeds the length
of the null terminated string found at pointer only be read up until the null terminator.

However, Chromium's implementation however does a hard check that this length is less than
or equal to static_cast<size_t>(std::numeric_limits<std::ptrdiff_t>::max()):
https://source.chromium.org/chromium/chromium/src/+/main:buildtools/third_party/libc++/trunk/include/string_view;drc=1718a75513d114e6b9aa4474e5c55d8dabee92fb;l=309

This doesn't break in Node.js right now because that hard assert is missing, but will
break in the next version of Xcode that's shipped and this Node.js too.

This should be upstreamed to ada.

diff --git a/deps/ada/ada.cpp b/deps/ada/ada.cpp
index 8b2cdd38ad0bb1e5757cdf5724c5a5917fc8e577..da5a04fa18f3268fc23558da63bb4837d9860d4f 100644
--- a/deps/ada/ada.cpp
+++ b/deps/ada/ada.cpp
@@ -826,7 +826,8 @@ namespace ada::helpers {
// Let path be url’s path.
// If url’s scheme is "file", path’s size is 1, and path[0] is a normalized Windows drive letter, then return.
if (type == ada::scheme::type::FILE && first_delimiter == std::string_view::npos) {
- if (checkers::is_normalized_windows_drive_letter(std::string_view(path.data() + 1, first_delimiter - 1))) {
+ if (checkers::is_normalized_windows_drive_letter(
+ std::string_view(path.data() + 1, path.length() - 1))) {
return;
}
}

0 comments on commit c1b8515

Please sign in to comment.