diff --git a/swift-ci/sdks/static-linux/resources/patches/musl/0001-iconv-fix-erroneous-input-validation-in-EUC-KR-decod.patch b/swift-ci/sdks/static-linux/resources/patches/musl/0001-iconv-fix-erroneous-input-validation-in-EUC-KR-decod.patch new file mode 100644 index 00000000..e62d28d4 --- /dev/null +++ b/swift-ci/sdks/static-linux/resources/patches/musl/0001-iconv-fix-erroneous-input-validation-in-EUC-KR-decod.patch @@ -0,0 +1,38 @@ +>From e5adcd97b5196e29991b524237381a0202a60659 Mon Sep 17 00:00:00 2001 +From: Rich Felker +Date: Sun, 9 Feb 2025 10:07:19 -0500 +Subject: [PATCH] iconv: fix erroneous input validation in EUC-KR decoder + +as a result of incorrect bounds checking on the lead byte being +decoded, certain invalid inputs which should produce an encoding +error, such as "\xc8\x41", instead produced out-of-bounds loads from +the ksc table. + +in a worst case, the loaded value may not be a valid unicode scalar +value, in which case, if the output encoding was UTF-8, wctomb would +return (size_t)-1, causing an overflow in the output pointer and +remaining buffer size which could clobber memory outside of the output +buffer. + +bug report was submitted in private by Nick Wellnhofer on account of +potential security implications. +--- + src/locale/iconv.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/locale/iconv.c b/src/locale/iconv.c +index 9605c8e9..008c93f0 100644 +--- a/src/locale/iconv.c ++++ b/src/locale/iconv.c +@@ -502,7 +502,7 @@ size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restri + if (c >= 93 || d >= 94) { + c += (0xa1-0x81); + d += 0xa1; +- if (c >= 93 || c>=0xc6-0x81 && d>0x52) ++ if (c > 0xc6-0x81 || c==0xc6-0x81 && d>0x52) + goto ilseq; + if (d-'A'<26) d = d-'A'; + else if (d-'a'<26) d = d-'a'+26; +-- +2.21.0 + diff --git a/swift-ci/sdks/static-linux/resources/patches/musl/0002-iconv-harden-UTF-8-output-code-path-against-input-de.patch b/swift-ci/sdks/static-linux/resources/patches/musl/0002-iconv-harden-UTF-8-output-code-path-against-input-de.patch new file mode 100644 index 00000000..9b64508c --- /dev/null +++ b/swift-ci/sdks/static-linux/resources/patches/musl/0002-iconv-harden-UTF-8-output-code-path-against-input-de.patch @@ -0,0 +1,38 @@ +>From c47ad25ea3b484e10326f933e927c0bc8cded3da Mon Sep 17 00:00:00 2001 +From: Rich Felker +Date: Wed, 12 Feb 2025 17:06:30 -0500 +Subject: [PATCH] iconv: harden UTF-8 output code path against input decoder + bugs + +the UTF-8 output code was written assuming an invariant that iconv's +decoders only emit valid Unicode Scalar Values which wctomb can encode +successfully, thereby always returning a value between 1 and 4. + +if this invariant is not satisfied, wctomb returns (size_t)-1, and the +subsequent adjustments to the output buffer pointer and remaining +output byte count overflow, moving the output position backwards, +potentially past the beginning of the buffer, without storing any +bytes. +--- + src/locale/iconv.c | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/src/locale/iconv.c b/src/locale/iconv.c +index 008c93f0..52178950 100644 +--- a/src/locale/iconv.c ++++ b/src/locale/iconv.c +@@ -545,6 +545,10 @@ size_t iconv(iconv_t cd, char **restrict in, size_t *restrict inb, char **restri + if (*outb < k) goto toobig; + memcpy(*out, tmp, k); + } else k = wctomb_utf8(*out, c); ++ /* This failure condition should be unreachable, but ++ * is included to prevent decoder bugs from translating ++ * into advancement outside the output buffer range. */ ++ if (k>4) goto ilseq; + *out += k; + *outb -= k; + break; +-- +2.21.0 + + diff --git a/swift-ci/sdks/static-linux/scripts/build.sh b/swift-ci/sdks/static-linux/scripts/build.sh index b7da4346..73e8795b 100755 --- a/swift-ci/sdks/static-linux/scripts/build.sh +++ b/swift-ci/sdks/static-linux/scripts/build.sh @@ -97,7 +97,7 @@ function declare_package } declare_package static_linux_sdk \ - "Swift statically linked SDK for Linux" \ + "Swift Static SDK for Linux" \ "Apache-2.0" "https://swift.org/install/sdk" declare_package swift "swift" "Apache-2.0" "https://swift.org" declare_package musl "musl" "MIT" "https://musl.org" @@ -109,9 +109,13 @@ declare_package curl "curl" "MIT" "https://curl.se" declare_package boringssl "boringssl" "OpenSSL AND ISC AND MIT" \ "https://boringssl.googlesource.com/boringssl/" declare_package zlib "zlib" "Zlib" "https://zlib.net" +declare_package bzip2 "bzip2" "bzip2-1.0.6" "https://sourceware.org/bzip2/" +declare_package xz "XZ Utils" "0BSD" "https://tukaani.org/xz" +declare_package libarchive "libarchive" "BSD-2-Clause" "https://www.libarchive.org" +declare_package mimalloc "mimalloc" "MIT" "https://microsoft.github.io/mimalloc/" # Parse command line arguments -static_linux_sdk_version=0.0.1 +static_linux_sdk_version=0.1.0 sdk_name= archs=x86_64,aarch64 build_type=RelWithDebInfo @@ -208,6 +212,15 @@ boringssl_version=$(describe ${source_dir}/boringssl) zlib_version=$(versionFromTag ${source_dir}/zlib) +bzip2_desc=$(describe ${source_dir}/bzip2) +bzip2_version=${bzip2_desc#bzip2-} + +libarchive_version=$(versionFromTag ${source_dir}/libarchive) + +mimalloc_version=$(versionFromTag ${source_dir}/mimalloc) + +xz_version=$(versionFromTag ${source_dir}/xz) + function quiet_pushd { pushd "$1" >/dev/null 2>&1 } @@ -233,6 +246,10 @@ echo " - libxml2 ${libxml2_version}" echo " - curl ${curl_version}" echo " - BoringSSL ${boringssl_version}" echo " - zlib ${zlib_version}" +echo " - bzip2 ${bzip2_version}" +echo " - xz ${xz_version}" +echo " - libarchive ${libarchive_version}" +echo " - mimalloc ${mimalloc_version}" function run() { echo "$@" @@ -268,6 +285,19 @@ else exit 1 fi +echo "Applying Musl security patches... " +for patch in $(realpath "${resource_dir}/patches/musl")/*; do + echo -n " $(basename $patch)..." + if git -C ${source_dir}/musl apply --reverse --check "$patch" >/dev/null 2>&1; then + echo "already patched" + elif git -C ${source_dir}/musl apply "$patch" >/dev/null 2>&1; then + echo "done" + else + echo "failed" + exit 1 + fi +done + for arch in $archs; do # Fix architecture names @@ -299,7 +329,7 @@ for arch in $archs; do cat > $sdk_root/SDKSettings.json < info.json <|--swift-tag |--swift-version ] - [--musl-version ] [--libxml2-version ] - [--curl-version ] [--boringssl-version ] + [--bzip2-version ] + [--curl-version ] + [--libarchive-version ] + [--libxml2-version ] + [--mimalloc-version ] + [--musl-version ] + [--xz-version ] [--zlib-version ] [--clone-with-ssh] [--source-dir ] @@ -72,10 +77,14 @@ SDK for Swift. Options are: If starts with "scheme:" or "tag:", it will select a scheme or tag; otherwise it will be treated as a version number. - --musl-version - --libxml2-version - --curl-version --boringssl-version + --bzip2-version + --curl-version + --libarchive-version + --libxml2-version + --mimalloc-version + --musl-version + --xz-version --zlib-version Select the versions of other dependencies. EOF @@ -89,17 +98,29 @@ if [[ -z "${MUSL_VERSION}" ]]; then MUSL_VERSION=1.2.5 fi if [[ -z "${LIBXML2_VERSION}" ]]; then - LIBXML2_VERSION=2.12.7 + LIBXML2_VERSION=2.14.5 fi if [[ -z "${CURL_VERSION}" ]]; then - CURL_VERSION=8.7.1 + CURL_VERSION=8.15.0 fi if [[ -z "${BORINGSSL_VERSION}" ]]; then - BORINGSSL_VERSION=fips-20220613 + BORINGSSL_VERSION=817ab07ebb53da35afea409ab9328f578492832d fi if [[ -z "${ZLIB_VERSION}" ]]; then ZLIB_VERSION=1.3.1 fi +if [[ -z "${BZIP2_VERSION}" ]]; then + BZIP2_VERSION=1.0.8 +fi +if [[ -z "${LIBARCHIVE_VERSION}" ]]; then + LIBARCHIVE_VERSION=3.8.1 +fi +if [[ -z "${MIMALLOC_VERSION}" ]]; then + MIMALLOC_VERSION=2.2.4 +fi +if [[ -z "${XZ_VERSION}" ]]; then + XZ_VERSION=5.8.1 +fi clone_with_ssh=false while [ "$#" -gt 0 ]; do @@ -120,6 +141,14 @@ while [ "$#" -gt 0 ]; do BORINGSSL_VERSION="$2"; shift ;; --zlib-version) ZLIB_VERSION="$2"; shift ;; + --bzip2-version) + BZIP2_VERSION="$2"; shift ;; + --libarchive-version) + LIBARCHIVE_VERSION="$2"; shift ;; + --mimalloc-version) + MIMALLOC_VERSION="$2"; shift ;; + --xz-version) + XZ_VERSION="$2"; shift ;; --clone-with-ssh) clone_with_ssh=true ;; --source-dir) @@ -208,3 +237,35 @@ header "Fetching zlib" pushd zlib >/dev/null 2>&1 git checkout v${ZLIB_VERSION} popd >/dev/null 2>&1 + +# Fetch bzip2 +header "Fetching bzip2" + +[[ -d bzip2 ]] | git clone git://sourceware.org/git/bzip2.git +pushd bzip2 >/dev/null 2>&1 +git checkout bzip2-${BZIP2_VERSION} +popd >/dev/null 2>&1 + +# Fetch libarchive +header "Fetching libarchive" + +[[ -d libarchive ]] | git clone ${github}libarchive/libarchive.git +pushd libarchive >/dev/null 2>&1 +git checkout v${LIBARCHIVE_VERSION} +popd >/dev/null 2>&1 + +# Fetch mimalloc +header "Fetching mimalloc" + +[[ -d mimalloc ]] | git clone ${github}microsoft/mimalloc.git +pushd mimalloc >/dev/null 2>&1 +git checkout v${MIMALLOC_VERSION} +popd + +# Fetch xz-utils +header "Fetching xz" + +[[ -d xz ]] | git clone ${github}tukaani-project/xz.git +pushd xz >/dev/null 2>&1 +git checkout v${XZ_VERSION} +popd