diff --git a/.github/workflows/gha.yml b/.github/workflows/gha.yml index 41280bd0e..2f25c1412 100644 --- a/.github/workflows/gha.yml +++ b/.github/workflows/gha.yml @@ -59,7 +59,7 @@ jobs: strategy: fail-fast: false matrix: - pattern: [0, 1, 2, 3, 4, 5, 6, 7, 8] + pattern: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] steps: - uses: actions/checkout@v1 - name: install build depends @@ -148,6 +148,16 @@ jobs: ACTION="ci/build_cmake_embedded.sh" export ARCH="64" fi + if [ ${{ matrix.pattern }} == 9 ]; then + export CC=gcc + export CXX=g++ + ACTION="ci/build_cmake.sh" + export ARCH="64" + export SHARED="ON" + export SAN="-fsanitize=address -fno-omit-frame-pointer" + export CHAR_SIGN="signed" + LIMITS="-DMSGPACK_UNPACK_ARRAY_LIMIT=100000 -DMSGPACK_UNPACK_MAP_LIMIT=100000" + fi # install gtest wget https://github.com/google/googletest/archive/v1.13.0.zip -O googletest-1.13.0.zip @@ -164,7 +174,7 @@ jobs: fi # build and test - CMAKE_CXX_COMPILER="${CXX}" CMAKE_C_COMPILER="${CC}" GTEST_ROOT="${BASE}/usr" CFLAGS="-Werror -g -gdwarf-4 -fsanitize=undefined -fno-sanitize-recover=all" CXXFLAGS="-Werror -g -ggdb3 -gdwarf-4 -fsanitize=undefined -fno-sanitize-recover=all" ${ACTION} + CMAKE_CXX_COMPILER="${CXX}" CMAKE_C_COMPILER="${CC}" GTEST_ROOT="${BASE}/usr" CFLAGS="-Werror -g -gdwarf-4 -fsanitize=undefined -fno-sanitize-recover=all ${LIMITS}" CXXFLAGS="-Werror -g -ggdb3 -gdwarf-4 -fsanitize=undefined -fno-sanitize-recover=all" ${ACTION} windows: runs-on: windows-2022 diff --git a/src/unpack.c b/src/unpack.c index e47e1ec0f..943a25e89 100644 --- a/src/unpack.c +++ b/src/unpack.c @@ -187,6 +187,28 @@ static inline int template_callback_false(unpack_user* u, msgpack_object* o) return 0; } +/* + * Optional, compile-time hardening limits for the number of elements an + * array, or key/value pairs a map, may declare before its backing store is + * allocated. They bound the up-front allocation that a tiny message can + * request: a 5-byte array32/map32 header (e.g. 0xdf 0xff 0xff 0xff 0xff) can + * otherwise ask for ~100-200 GB before a single element byte is read, which + * makes a resource-exhaustion attack very cheap (see issue #1013). + * + * Both default to 0, which means "no limit" and preserves the previous + * behavior exactly: with the default the checks below compile out entirely. + * Define them to a non-zero value at build time + * (e.g. -DMSGPACK_UNPACK_ARRAY_LIMIT=1000000) to reject any array or map + * whose declared element count exceeds the limit with + * MSGPACK_UNPACK_PARSE_ERROR. + */ +#ifndef MSGPACK_UNPACK_ARRAY_LIMIT +#define MSGPACK_UNPACK_ARRAY_LIMIT 0 +#endif +#ifndef MSGPACK_UNPACK_MAP_LIMIT +#define MSGPACK_UNPACK_MAP_LIMIT 0 +#endif + static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o) { size_t size; @@ -199,6 +221,11 @@ static inline int template_callback_array(unpack_user* u, unsigned int n, msgpac return MSGPACK_UNPACK_NOMEM_ERROR; #endif +#if MSGPACK_UNPACK_ARRAY_LIMIT + if (n > MSGPACK_UNPACK_ARRAY_LIMIT) + return MSGPACK_UNPACK_PARSE_ERROR; +#endif + o->type = MSGPACK_OBJECT_ARRAY; o->via.array.size = 0; @@ -243,6 +270,11 @@ static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_ return MSGPACK_UNPACK_NOMEM_ERROR; #endif +#if MSGPACK_UNPACK_MAP_LIMIT + if (n > MSGPACK_UNPACK_MAP_LIMIT) + return MSGPACK_UNPACK_PARSE_ERROR; +#endif + o->type = MSGPACK_OBJECT_MAP; o->via.map.size = 0;