Skip to content

Commit 310da88

Browse files
committed
Support using the system version of brotli
This is mainly motivated by the March 2022 release of .NET 5. .NET 5 was found to be vulnerable to CVE-2020-8927, which was caused by the older version of brotli built into .NET. .NET was vulernable even in environments where a system-wide version of brotli was present and had already received fixes for this CVE. We could have avoided a Remote Code Execution vulnerability in such environments by using the system's version of brotli. This is similar to the existing support for disabling distro-agnostic OpenSSL (except no OpenSSL is embedded) and using the system libunwind (a copy of libunwind is embedded this repo). One small twist is the presence of entrypoint verification. In a system-brotli build, the verification fails, because the built library, libSystem.IO.Compression.Native.so, doesn't include the symbols for Brotli. Those symbols are instead used from the system brotli libraries.
1 parent 135e566 commit 310da88

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

src/native/external/brotli.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include_directories("${CMAKE_CURRENT_LIST_DIR}/brotli/include")
1+
include_directories(BEFORE "${CMAKE_CURRENT_LIST_DIR}/brotli/include")
22

33
set (BROTLI_SOURCES_BASE
44
common/constants.c

src/native/libs/System.IO.Compression.Native/CMakeLists.txt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ set(NATIVECOMPRESSION_SOURCES
77
)
88

99
if (NOT CLR_CMAKE_TARGET_BROWSER)
10-
include(${CLR_SRC_NATIVE_DIR}/external/brotli.cmake)
10+
11+
if (CLR_CMAKE_USE_SYSTEM_BROTLI)
12+
add_definitions(-DFEATURE_USE_SYSTEM_BROTLI)
13+
else ()
14+
include(${CLR_SRC_NATIVE_DIR}/external/brotli.cmake)
15+
16+
set (NATIVECOMPRESSION_SOURCES
17+
${NATIVECOMPRESSION_SOURCES}
18+
${BROTLI_SOURCES}
19+
)
20+
endif ()
1121

1222
set (NATIVECOMPRESSION_SOURCES
1323
${NATIVECOMPRESSION_SOURCES}
14-
${BROTLI_SOURCES}
1524
entrypoints.c
1625
)
1726
endif ()
@@ -60,14 +69,16 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER)
6069
set_property(TARGET System.IO.Compression.Native APPEND_STRING PROPERTY LINK_FLAGS ${EXPORTS_LINKER_OPTION})
6170
set_property(TARGET System.IO.Compression.Native APPEND_STRING PROPERTY LINK_DEPENDS ${EXPORTS_FILE})
6271

63-
add_custom_command(TARGET System.IO.Compression.Native POST_BUILD
64-
COMMENT "Verifying System.IO.Compression.Native entry points against entrypoints.c "
65-
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../verify-entrypoints.sh
66-
$<TARGET_FILE:System.IO.Compression.Native>
67-
${CMAKE_CURRENT_SOURCE_DIR}/entrypoints.c
68-
${CMAKE_NM}
69-
VERBATIM
70-
)
72+
if (NOT CLR_CMAKE_USE_SYSTEM_BROTLI)
73+
add_custom_command(TARGET System.IO.Compression.Native POST_BUILD
74+
COMMENT "Verifying System.IO.Compression.Native entry points against entrypoints.c "
75+
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../verify-entrypoints.sh
76+
$<TARGET_FILE:System.IO.Compression.Native>
77+
${CMAKE_CURRENT_SOURCE_DIR}/entrypoints.c
78+
${CMAKE_NM}
79+
VERBATIM
80+
)
81+
endif ()
7182
endif ()
7283

7384
install_with_stripped_symbols (System.IO.Compression.Native PROGRAMS .)

src/native/libs/System.IO.Compression.Native/entrypoints.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
// Include System.IO.Compression.Native headers
77
#include "pal_zlib.h"
8-
#include <external/brotli/include/brotli/decode.h>
9-
#include <external/brotli/include/brotli/encode.h>
10-
#include <external/brotli/include/brotli/port.h>
11-
#include <external/brotli/include/brotli/types.h>
8+
#include <brotli/decode.h>
9+
#include <brotli/encode.h>
10+
#include <brotli/port.h>
11+
#include <brotli/types.h>
1212

1313
static const Entry s_compressionNative[] =
1414
{

src/native/libs/System.IO.Compression.Native/extra_libs.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ macro(append_extra_compression_libs NativeLibsExtra)
1212
find_package(ZLIB REQUIRED)
1313
endif ()
1414
list(APPEND ${NativeLibsExtra} ${ZLIB_LIBRARIES})
15+
16+
if (CLR_CMAKE_USE_SYSTEM_BROTLI)
17+
find_library(BROTLIDEC brotlidec REQUIRED)
18+
find_library(BROTLIENC brotlienc REQUIRED)
19+
20+
list(APPEND ${NativeLibsExtra} ${BROTLIDEC} ${BROTLIENC})
21+
endif ()
1522
endmacro()

0 commit comments

Comments
 (0)