|
| 1 | +cmake_minimum_required(VERSION 3.11) |
| 2 | +project(curl_fuzzer_deps) |
| 3 | + |
| 4 | +include(ExternalProject) |
| 5 | + |
| 6 | +# Install zlib |
| 7 | +# |
| 8 | +# renovate: datasource=github-tags depName=madler/zlib |
| 9 | +set(ZLIB_VERSION 1.3.1) |
| 10 | +set(ZLIB_URL https://zlib.net/zlib-${ZLIB_VERSION}.tar.gz) |
| 11 | +set(ZLIB_INSTALL_DIR ${CMAKE_BINARY_DIR}/zlib-install) |
| 12 | + |
| 13 | +ExternalProject_Add( |
| 14 | + zlib_external |
| 15 | + URL ${ZLIB_URL} |
| 16 | + PREFIX ${CMAKE_BINARY_DIR}/zlib |
| 17 | + SOURCE_SUBDIR . |
| 18 | + CONFIGURE_COMMAND <SOURCE_DIR>/configure --static --prefix=${ZLIB_INSTALL_DIR} |
| 19 | + BUILD_COMMAND $(MAKE) |
| 20 | + INSTALL_COMMAND $(MAKE) install |
| 21 | + BUILD_IN_SOURCE 1 |
| 22 | + DOWNLOAD_EXTRACT_TIMESTAMP TRUE |
| 23 | +) |
| 24 | +set(ZLIB_STATIC_LIB ${ZLIB_INSTALL_DIR}/lib/libz.a) |
| 25 | + |
| 26 | +# For the memory sanitizer build, turn off OpenSSL as it causes bugs we can't |
| 27 | +# affect (see 16697, 17624) |
| 28 | +if(NOT (DEFINED ENV{SANITIZER} AND "$ENV{SANITIZER}" STREQUAL "memory")) |
| 29 | + # Install openssl |
| 30 | + # |
| 31 | + # renovate: datasource=github-tags depName=openssl/openssl |
| 32 | + set(OPENSSL_VERSION 3.5.0) |
| 33 | + set(OPENSSL_URL https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz) |
| 34 | + set(OPENSSL_INSTALL_DIR ${CMAKE_BINARY_DIR}/openssl-install) |
| 35 | + set(OPENSSL_SRC_DIR ${CMAKE_BINARY_DIR}/openssl/src/openssl_external) |
| 36 | + |
| 37 | + # Architecture and sanitizer logic |
| 38 | + set(OPENSSL_ARCH_PROG "") |
| 39 | + set(OPENSSL_EC_FLAG "enable-ec_nistp_64_gcc_128") |
| 40 | + if(DEFINED ENV{ARCHITECTURE} AND "$ENV{ARCHITECTURE}" STREQUAL "i386") |
| 41 | + set(OPENSSL_ARCH_PROG "setarch i386") |
| 42 | + set(OPENSSL_EC_FLAG "no-threads") |
| 43 | + endif() |
| 44 | + |
| 45 | + set(OPENSSL_ASM_FLAG "") |
| 46 | + if(DEFINED ENV{SANITIZER} AND "$ENV{SANITIZER}" STREQUAL "memory") |
| 47 | + set(OPENSSL_ASM_FLAG "no-asm") |
| 48 | + endif() |
| 49 | + |
| 50 | + # Compose the config command |
| 51 | + set(OPENSSL_CONFIGURE_COMMAND |
| 52 | + ${OPENSSL_ARCH_PROG} ./config |
| 53 | + --prefix=${OPENSSL_INSTALL_DIR} |
| 54 | + --libdir=lib |
| 55 | + --debug |
| 56 | + -DPEDANTIC |
| 57 | + -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
| 58 | + no-shared |
| 59 | + ${OPENSSL_ASM_FLAG} |
| 60 | + enable-tls1_3 |
| 61 | + enable-rc5 |
| 62 | + enable-md2 |
| 63 | + enable-ssl3 |
| 64 | + ${OPENSSL_EC_FLAG} |
| 65 | + enable-ssl3-method |
| 66 | + enable-nextprotoneg |
| 67 | + enable-weak-ssl-ciphers |
| 68 | + --with-zlib-include=${ZLIB_INSTALL_DIR}/include |
| 69 | + --with-zlib-lib=${ZLIB_INSTALL_DIR}/lib |
| 70 | + $ENV{OPENSSLFLAGS} |
| 71 | + ) |
| 72 | + |
| 73 | + ExternalProject_Add( |
| 74 | + openssl_external |
| 75 | + URL ${OPENSSL_URL} |
| 76 | + PREFIX ${CMAKE_BINARY_DIR}/openssl |
| 77 | + SOURCE_SUBDIR . |
| 78 | + CONFIGURE_COMMAND ${OPENSSL_CONFIGURE_COMMAND} |
| 79 | + BUILD_COMMAND $(MAKE) |
| 80 | + INSTALL_COMMAND $(MAKE) install_sw |
| 81 | + BUILD_IN_SOURCE 1 |
| 82 | + DOWNLOAD_EXTRACT_TIMESTAMP TRUE |
| 83 | + ) |
| 84 | + |
| 85 | + # Build zlib before openssl |
| 86 | + add_dependencies(openssl_external zlib_external) |
| 87 | + |
| 88 | + # Set the OpenSSL option for nghttp2 |
| 89 | + set(NGHTTP2_OPENSSL_OPTION --with-openssl=${OPENSSL_INSTALL_DIR}) |
| 90 | + |
| 91 | + # Set the dependency option for openssl |
| 92 | + set(OPENSSL_DEP openssl_external) |
| 93 | + set(OPENSSL_STATIC_LIB ${OPENSSL_INSTALL_DIR}/lib/libssl.a ${OPENSSL_INSTALL_DIR}/lib/libcrypto.a) |
| 94 | +else() |
| 95 | + set(NGHTTP2_OPENSSL_OPTION --without-openssl) |
| 96 | + set(OPENSSL_DEP "") |
| 97 | + set(OPENSSL_STATIC_LIB "") |
| 98 | +endif() |
| 99 | + |
| 100 | +# Install nghttp2 |
| 101 | +# |
| 102 | +# renovate: datasource=github-tags depName=nghttp2/nghttp2 |
| 103 | +set(NGHTTP2_VERSION 1.61.0) |
| 104 | +set(NGHTTP2_URL https://github.com/nghttp2/nghttp2/releases/download/v${NGHTTP2_VERSION}/nghttp2-${NGHTTP2_VERSION}.tar.gz) |
| 105 | +set(NGHTTP2_INSTALL_DIR ${CMAKE_BINARY_DIR}/nghttp2-install) |
| 106 | + |
| 107 | +set(NGHTTP2_CONFIGURE_COMMAND |
| 108 | + autoreconf -i && |
| 109 | + ./configure --prefix=${NGHTTP2_INSTALL_DIR} |
| 110 | + --disable-shared |
| 111 | + --enable-static |
| 112 | + --disable-threads |
| 113 | + --enable-lib-only |
| 114 | + ${NGHTTP2_OPENSSL_OPTION} |
| 115 | +) |
| 116 | + |
| 117 | +ExternalProject_Add( |
| 118 | + nghttp2_external |
| 119 | + URL ${NGHTTP2_URL} |
| 120 | + PREFIX ${CMAKE_BINARY_DIR}/nghttp2 |
| 121 | + SOURCE_SUBDIR . |
| 122 | + CONFIGURE_COMMAND ${NGHTTP2_CONFIGURE_COMMAND} |
| 123 | + BUILD_COMMAND $(MAKE) |
| 124 | + INSTALL_COMMAND $(MAKE) install |
| 125 | + BUILD_IN_SOURCE 1 |
| 126 | + DOWNLOAD_EXTRACT_TIMESTAMP TRUE |
| 127 | +) |
| 128 | +set(NGHTTP2_STATIC_LIB ${NGHTTP2_INSTALL_DIR}/lib/libnghttp2.a) |
| 129 | + |
| 130 | +# Ensure zlib and openssl are built before nghttp2 |
| 131 | +add_dependencies(nghttp2_external ${OPENSSL_DEP} zlib_external) |
| 132 | + |
| 133 | +# Install GDB if GDBMODE is set |
| 134 | +set(GDB_VERSION 13.2) |
| 135 | +set(GDB_URL https://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.gz) |
| 136 | +set(GDB_INSTALL_DIR ${CMAKE_BINARY_DIR}/gdb-install) |
| 137 | + |
| 138 | +option(BUILD_GDB "Build GDB as an external project" OFF) |
| 139 | +if(BUILD_GDB) |
| 140 | + ExternalProject_Add( |
| 141 | + gdb_external |
| 142 | + URL ${GDB_URL} |
| 143 | + PREFIX ${CMAKE_BINARY_DIR}/gdb |
| 144 | + SOURCE_SUBDIR . |
| 145 | + CONFIGURE_COMMAND ./configure --prefix=${GDB_INSTALL_DIR} |
| 146 | + BUILD_COMMAND $(MAKE) |
| 147 | + INSTALL_COMMAND $(MAKE) install |
| 148 | + BUILD_IN_SOURCE 1 |
| 149 | + ) |
| 150 | + set(GDB_DEP gdb_external) |
| 151 | +else() |
| 152 | + set(GDB_DEP "") |
| 153 | +endif() |
| 154 | + |
| 155 | +# Group dependencies into a single target |
| 156 | +add_custom_target(deps |
| 157 | + DEPENDS zlib_external ${OPENSSL_DEP} nghttp2_external ${GDB_DEP} |
| 158 | +) |
| 159 | + |
| 160 | +# Now for the main dependencies! |
| 161 | +# |
| 162 | +# Compile and install curl. |
| 163 | +set(CURL_INSTALL_DIR ${CMAKE_BINARY_DIR}/curl-install) |
| 164 | + |
| 165 | +# Determine SSL and nghttp2 options |
| 166 | +if(TARGET openssl_external) |
| 167 | + set(CURL_SSL_OPTION "--with-ssl=${OPENSSL_INSTALL_DIR}") |
| 168 | +else() |
| 169 | + set(CURL_SSL_OPTION "--without-ssl") |
| 170 | +endif() |
| 171 | + |
| 172 | +set(CURL_NGHTTP2_OPTION "--with-nghttp2=${NGHTTP2_INSTALL_DIR}") |
| 173 | +set(CURL_CONFIGURE_COMMAND |
| 174 | + autoreconf -fi && |
| 175 | + ./configure |
| 176 | + --prefix=${CURL_INSTALL_DIR} |
| 177 | + --disable-shared |
| 178 | + --enable-debug |
| 179 | + --enable-maintainer-mode |
| 180 | + --disable-symbol-hiding |
| 181 | + --disable-docs |
| 182 | + --enable-ipv6 |
| 183 | + --enable-websockets |
| 184 | + --without-libpsl |
| 185 | + --with-random=/dev/null |
| 186 | + ${CURL_SSL_OPTION} |
| 187 | + ${CURL_NGHTTP2_OPTION} |
| 188 | +) |
| 189 | + |
| 190 | +set(CURL_POST_INSTALL_COMMAND |
| 191 | + COMMAND ${CMAKE_COMMAND} -E make_directory ${CURL_INSTALL_DIR}/utfuzzer |
| 192 | + COMMAND ${CMAKE_COMMAND} -E copy_if_different <SOURCE_DIR>/lib/curl_fnmatch.h ${CURL_INSTALL_DIR}/utfuzzer/ |
| 193 | + COMMAND ${CMAKE_COMMAND} -E copy_if_different <SOURCE_DIR>/lib/bufq.h ${CURL_INSTALL_DIR}/utfuzzer/ |
| 194 | + COMMAND ${CMAKE_COMMAND} -E touch ${CURL_INSTALL_DIR}/utfuzzer/curl_setup.h |
| 195 | +) |
| 196 | + |
| 197 | +# Conditionally check to see if there's a source directory or not. |
| 198 | +# If there is, use it. Otherwise, download the latest version. |
| 199 | +# |
| 200 | +if (DEFINED ENV{CURL_SOURCE_DIR}) |
| 201 | + ExternalProject_Add( |
| 202 | + curl_external |
| 203 | + SOURCE_DIR $ENV{CURL_SOURCE_DIR} |
| 204 | + CONFIGURE_COMMAND ${CURL_CONFIGURE_COMMAND} |
| 205 | + BUILD_COMMAND $(MAKE) |
| 206 | + INSTALL_COMMAND $(MAKE) install |
| 207 | + ${CURL_POST_INSTALL_COMMAND} |
| 208 | + BUILD_IN_SOURCE 1 |
| 209 | + ) |
| 210 | +else() |
| 211 | + set(CURL_VERSION 8.14.1) |
| 212 | + # The URL needs dots replaced with underscores for curl's release naming convention. |
| 213 | + string(REPLACE "." "_" CURL_VERSION_UNDERSCORE ${CURL_VERSION}) |
| 214 | + set(CURL_URL https://github.com/curl/curl/releases/download/curl-${CURL_VERSION_UNDERSCORE}/curl-${CURL_VERSION}.tar.gz) |
| 215 | + ExternalProject_Add( |
| 216 | + curl_external |
| 217 | + URL ${CURL_URL} |
| 218 | + PREFIX ${CMAKE_BINARY_DIR}/curl |
| 219 | + CONFIGURE_COMMAND ${CURL_CONFIGURE_COMMAND} |
| 220 | + BUILD_COMMAND $(MAKE) |
| 221 | + INSTALL_COMMAND $(MAKE) install |
| 222 | + ${CURL_POST_INSTALL_COMMAND} |
| 223 | + BUILD_IN_SOURCE 1 |
| 224 | + DOWNLOAD_EXTRACT_TIMESTAMP TRUE |
| 225 | + ) |
| 226 | +endif() |
| 227 | + |
| 228 | +set(CURL_STATIC_LIB ${CURL_INSTALL_DIR}/lib/libcurl.a) |
| 229 | + |
| 230 | +# Add dependencies for curl |
| 231 | +add_dependencies(curl_external nghttp2_external ${OPENSSL_DEP} zlib_external) |
| 232 | + |
| 233 | +# Now it's time for the main targets! |
| 234 | +# |
| 235 | +# Paths to curl install (adjust as needed) |
| 236 | +set(CURL_INCLUDE_DIRS |
| 237 | + ${CURL_INSTALL_DIR}/include |
| 238 | + ${CURL_INSTALL_DIR}/utfuzzer |
| 239 | +) |
| 240 | +set(CURL_LIB_DIR ${CURL_INSTALL_DIR}/lib) |
| 241 | + |
| 242 | +# Fuzzing engine |
| 243 | +if (DEFINED ENV{LIB_FUZZING_ENGINE}) |
| 244 | + set(LIB_FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE}) |
| 245 | +else() |
| 246 | + add_library(standaloneengine STATIC standalone_fuzz_target_runner.cc) |
| 247 | + set_target_properties(standaloneengine PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) |
| 248 | + set(LIB_FUZZING_ENGINE ${CMAKE_BINARY_DIR}/libstandaloneengine.a) |
| 249 | +endif() |
| 250 | + |
| 251 | +# Common sources and flags |
| 252 | +set(COMMON_SOURCES curl_fuzzer.cc curl_fuzzer_tlv.cc curl_fuzzer_callback.cc) |
| 253 | +set(COMMON_FLAGS -g -DCURL_DISABLE_DEPRECATION) |
| 254 | +set(COMMON_LINK_LIBS |
| 255 | + ${CURL_LIB_DIR}/libcurl.a |
| 256 | + ${NGHTTP2_STATIC_LIB} |
| 257 | + ${OPENSSL_STATIC_LIB} |
| 258 | + ${ZLIB_STATIC_LIB} |
| 259 | + ${LIB_FUZZING_ENGINE} |
| 260 | + pthread |
| 261 | + m |
| 262 | +) |
| 263 | + |
| 264 | +# Helper macro to define a fuzzer target |
| 265 | +macro(add_curl_fuzzer name proto) |
| 266 | + add_executable(${name} ${COMMON_SOURCES}) |
| 267 | + target_compile_options(${name} PRIVATE ${COMMON_FLAGS} -DFUZZ_PROTOCOLS_${proto}) |
| 268 | + target_include_directories(${name} PRIVATE ${CURL_INCLUDE_DIRS}) |
| 269 | + target_link_libraries(${name} PRIVATE ${COMMON_LINK_LIBS} ${LIB_FUZZING_ENGINE}) |
| 270 | +endmacro() |
| 271 | + |
| 272 | +# Main fuzzer (all protocols) |
| 273 | +add_executable(curl_fuzzer ${COMMON_SOURCES}) |
| 274 | +target_compile_options(curl_fuzzer PRIVATE ${COMMON_FLAGS} -DFUZZ_PROTOCOLS_ALL) |
| 275 | +target_include_directories(curl_fuzzer PRIVATE ${CURL_INCLUDE_DIRS}) |
| 276 | +target_link_libraries(curl_fuzzer PRIVATE ${COMMON_LINK_LIBS} ${LIB_FUZZING_ENGINE}) |
| 277 | + |
| 278 | +# Protocol-specific fuzzers |
| 279 | +add_curl_fuzzer(curl_fuzzer_dict DICT) |
| 280 | +add_curl_fuzzer(curl_fuzzer_file FILE) |
| 281 | +add_curl_fuzzer(curl_fuzzer_ftp FTP) |
| 282 | +add_curl_fuzzer(curl_fuzzer_gopher GOPHER) |
| 283 | +add_curl_fuzzer(curl_fuzzer_http HTTP) |
| 284 | +add_curl_fuzzer(curl_fuzzer_https HTTPS) |
| 285 | +add_curl_fuzzer(curl_fuzzer_imap IMAP) |
| 286 | +add_curl_fuzzer(curl_fuzzer_mqtt MQTT) |
| 287 | +add_curl_fuzzer(curl_fuzzer_pop3 POP3) |
| 288 | +add_curl_fuzzer(curl_fuzzer_rtsp RTSP) |
| 289 | +add_curl_fuzzer(curl_fuzzer_smb SMB) |
| 290 | +add_curl_fuzzer(curl_fuzzer_smtp SMTP) |
| 291 | +add_curl_fuzzer(curl_fuzzer_tftp TFTP) |
| 292 | +add_curl_fuzzer(curl_fuzzer_ws WS) |
| 293 | + |
| 294 | +# BUFQ fuzzer |
| 295 | +add_executable(curl_fuzzer_bufq fuzz_bufq.cc) |
| 296 | +target_compile_options(curl_fuzzer_bufq PRIVATE ${COMMON_FLAGS}) |
| 297 | +target_include_directories(curl_fuzzer_bufq PRIVATE ${CURL_INCLUDE_DIRS}) |
| 298 | +target_link_libraries(curl_fuzzer_bufq PRIVATE ${COMMON_LINK_LIBS}) |
| 299 | + |
| 300 | +# URL fuzzer |
| 301 | +add_executable(fuzz_url fuzz_url.cc) |
| 302 | +target_compile_options(fuzz_url PRIVATE ${COMMON_FLAGS}) |
| 303 | +target_include_directories(fuzz_url PRIVATE ${CURL_INCLUDE_DIRS}) |
| 304 | +target_link_libraries(fuzz_url PRIVATE ${COMMON_LINK_LIBS}) |
| 305 | + |
| 306 | +# Unit test fuzzer |
| 307 | +add_executable(curl_fuzzer_fnmatch fuzz_fnmatch.cc) |
| 308 | +target_compile_options(curl_fuzzer_fnmatch PRIVATE ${COMMON_FLAGS}) |
| 309 | +target_include_directories(curl_fuzzer_fnmatch PRIVATE ${CURL_INCLUDE_DIRS}) |
| 310 | +target_link_libraries(curl_fuzzer_fnmatch PRIVATE ${COMMON_LINK_LIBS}) |
| 311 | + |
| 312 | +# Add dependencies so fuzzers build after curl |
| 313 | +add_dependencies(curl_fuzzer curl_external) |
| 314 | +add_dependencies(curl_fuzzer_dict curl_external) |
| 315 | +add_dependencies(curl_fuzzer_file curl_external) |
| 316 | +add_dependencies(curl_fuzzer_ftp curl_external) |
| 317 | +add_dependencies(curl_fuzzer_gopher curl_external) |
| 318 | +add_dependencies(curl_fuzzer_http curl_external) |
| 319 | +add_dependencies(curl_fuzzer_https curl_external) |
| 320 | +add_dependencies(curl_fuzzer_imap curl_external) |
| 321 | +add_dependencies(curl_fuzzer_mqtt curl_external) |
| 322 | +add_dependencies(curl_fuzzer_pop3 curl_external) |
| 323 | +add_dependencies(curl_fuzzer_rtsp curl_external) |
| 324 | +add_dependencies(curl_fuzzer_smb curl_external) |
| 325 | +add_dependencies(curl_fuzzer_smtp curl_external) |
| 326 | +add_dependencies(curl_fuzzer_tftp curl_external) |
| 327 | +add_dependencies(curl_fuzzer_ws curl_external) |
| 328 | +add_dependencies(curl_fuzzer_bufq curl_external) |
| 329 | +add_dependencies(fuzz_url curl_external) |
| 330 | +add_dependencies(curl_fuzzer_fnmatch curl_external) |
| 331 | + |
| 332 | +# Create a custom target for all fuzzers |
| 333 | +add_custom_target(fuzz |
| 334 | + DEPENDS |
| 335 | + curl_fuzzer |
| 336 | + curl_fuzzer_dict |
| 337 | + curl_fuzzer_file |
| 338 | + curl_fuzzer_ftp |
| 339 | + curl_fuzzer_gopher |
| 340 | + curl_fuzzer_http |
| 341 | + curl_fuzzer_https |
| 342 | + curl_fuzzer_imap |
| 343 | + curl_fuzzer_mqtt |
| 344 | + curl_fuzzer_pop3 |
| 345 | + curl_fuzzer_rtsp |
| 346 | + curl_fuzzer_smb |
| 347 | + curl_fuzzer_smtp |
| 348 | + curl_fuzzer_tftp |
| 349 | + curl_fuzzer_ws |
| 350 | + curl_fuzzer_bufq |
| 351 | + fuzz_url |
| 352 | + curl_fuzzer_fnmatch |
| 353 | +) |
0 commit comments