Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions projects/dcmtk/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################


FROM gcr.io/oss-fuzz-base/base-builder

RUN apt-get update && apt-get install -y --no-install-recommends cmake ninja-build make pkg-config zlib1g-dev python3 && rm -rf /var/lib/apt/lists/*

RUN git clone --depth 1 https://github.com/DCMTK/dcmtk $SRC/dcmtk
RUN git clone --depth 1 https://github.com/DCMTK/dcmtk-fuzzers $SRC/dcmtk-fuzzers

COPY build.sh $SRC/
COPY dcmtk_dicom_fuzzer.dict make_seed_corpus.py $SRC/dcmtk-fuzzers/
COPY dcmtk_dicom_fuzzer.cc dcmtk_meta_fuzzer.cc $SRC/dcmtk-fuzzers/

WORKDIR $SRC/dcmtk-fuzzers
89 changes: 89 additions & 0 deletions projects/dcmtk/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash -eu
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
####################################################################################################################################

FUZZERS=(
dcmtk_dicom_fuzzer
dcmtk_meta_fuzzer
)

# Build DCMTK (static) with iconv enabled so liboficonv is present.
cd "$SRC"
cmake -S dcmtk -B dcmtk-build -DBUILD_SHARED_LIBS=OFF -DDCMTK_WITH_OPENSSL=OFF -DDCMTK_WITH_PNG=OFF -DDCMTK_WITH_TIFF=OFF -DDCMTK_WITH_XML=OFF -DDCMTK_WITH_ICONV=ON -DDCMTK_WITH_ZLIB=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$WORK/dcmtk-install"
cmake --build dcmtk-build -j"$(nproc)"
cmake --install dcmtk-build

# Ship the DICOM dictionary (for cleaner logs at runtime).
DICT_SRC=$(ls "$WORK"/dcmtk-install/share/dcmtk-*/dicom.dic 2>/dev/null || true)
if [ -n "$DICT_SRC" ]; then
cp "$DICT_SRC" "$OUT/dicom.dic" || true
fi

cd "$SRC/dcmtk-fuzzers"
DCMTK_INC="$WORK/dcmtk-install/include"
DCMTK_LIBDIR="$WORK/dcmtk-install/lib"

# Derive robust link set from pkg-config and filter to installed libs.
export PKG_CONFIG_PATH="$DCMTK_LIBDIR/pkgconfig:${PKG_CONFIG_PATH:-}"
RAW_LIBS="$(pkg-config --static --libs dcmtk 2>/dev/null || true)"
FILTERED_LIBS=""
for tok in $RAW_LIBS; do
if [[ "$tok" == -l* ]]; then
lib="${tok#-l}"
if [ -f "$DCMTK_LIBDIR/lib${lib}.a" ] || [ -f "$DCMTK_LIBDIR/lib${lib}.so" ]; then
FILTERED_LIBS+=" $tok"
fi
else
FILTERED_LIBS+=" $tok"
fi
done
[ -z "$FILTERED_LIBS" ] && FILTERED_LIBS="-ldcmdata -loflog -lofstd -loficonv -lz"
DCMTK_LIBS="-Wl,--start-group ${FILTERED_LIBS} -Wl,--end-group -lpthread -ldl"

build_one() {
local src="$1"
local out="$2"
"$CXX" $CXXFLAGS -std=c++17 -I"$DCMTK_INC" "$src" -o "$OUT/$out" $LIB_FUZZING_ENGINE -L"$DCMTK_LIBDIR" ${DCMTK_LIBS}
}

for fz in "${FUZZERS[@]}"; do
echo "Building $fz..."
build_one "${fz}.cc" "$fz"
done

# .options: use *relative* dictionary path so check_build works after files are copied.
cat > "$OUT/dcmtk_dicom_fuzzer.options" << 'EOF'
[libfuzzer]
max_len = 131072
timeout = 25
rss_limit_mb = 2560
dict = dcmtk_dicom_fuzzer.dict
EOF

cat > "$OUT/dcmtk_meta_fuzzer.options" << 'EOF'
[libfuzzer]
max_len = 65536
timeout = 25
rss_limit_mb = 2560
dict = dcmtk_dicom_fuzzer.dict
EOF

# Seed corpus next to binaries
python3 "$SRC/dcmtk-fuzzers/make_seed_corpus.py"

# Copy dictionary next to binaries under both names (defensive)
cp "$SRC/dcmtk-fuzzers/dcmtk_dicom_fuzzer.dict" "$OUT/dcmtk_dicom_fuzzer.dict" || true
cp "$SRC/dcmtk-fuzzers/dcmtk_dicom_fuzzer.dict" "$OUT/dcmtk_meta_fuzzer.dict" || true
76 changes: 76 additions & 0 deletions projects/dcmtk/dcmtk_dicom_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////
#include <cstdint>
#include <cstddef>
#include <cstdlib>
#include <new>
#include <string>

#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmdata/dcistrmb.h"
#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcxfer.h"

static constexpr std::size_t kNewNothrowCap = 8 * 1024 * 1024;

void* operator new(std::size_t n, const std::nothrow_t&) noexcept {
if (n > kNewNothrowCap) return nullptr;
try { return ::operator new(n); } catch (...) { return nullptr; }
}
void* operator new[](std::size_t n, const std::nothrow_t&) noexcept {
if (n > kNewNothrowCap) return nullptr;
try { return ::operator new[](n); } catch (...) { return nullptr; }
}

static void walkDataset(DcmItem* item) {
if (!item) return;
DcmStack stack;
if (item->nextObject(stack, OFTrue).good()) {
do {
DcmObject* obj = stack.top();
if (!obj) break;
(void)obj->ident();
(void)obj->getTag();
} while (item->nextObject(stack, OFFalse).good());
}
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
static bool dict_set = (setenv("DCMDICTPATH", "/out/dicom.dic", 0), true);
(void)dict_set;

DcmInputBufferStream in;
in.setBuffer((void*)data, size);
in.setEos();

DcmFileFormat file;
const Uint32 kMaxReadLen = 256 * 1024;

if (file.read(in, EXS_Unknown, EGL_noChange, kMaxReadLen).good()) {
if (auto* ds = file.getDataset()) {
(void)ds->chooseRepresentation(EXS_LittleEndianExplicit, nullptr);
(void)ds->calcElementLength(EXS_LittleEndianExplicit, EET_ExplicitLength);

OFString s;
(void)ds->findAndGetOFString(DCM_PatientName, s);
(void)ds->findAndGetOFString(DCM_StudyInstanceUID, s);
(void)ds->findAndGetOFString(DCM_SOPClassUID, s);

walkDataset(ds);
}
}
return 0;
}
45 changes: 45 additions & 0 deletions projects/dcmtk/dcmtk_dicom_fuzzer.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"DICM"
"AE"
"AS"
"AT"
"CS"
"DA"
"DS"
"DT"
"FL"
"FD"
"IS"
"LO"
"LT"
"OB"
"OD"
"OF"
"OL"
"OW"
"PN"
"SH"
"SL"
"SQ"
"SS"
"ST"
"TM"
"UC"
"UI"
"UL"
"UN"
"UR"
"US"
"UT"
"ISO_IR 100"
"ISO_IR 192"
"ISO 2022 IR 87"
"ISO 2022 IR 149"
"1.2.840.10008.1.2"
"1.2.840.10008.1.2.1"
"1.2.840.10008.1.2.2"
"1.2.840.10008.5.1.4.1.1.2"
"1.2.840.10008.5.1.4.1.1.4"
"1.2.840.10008.5.1.4.1.1.128"
"\xFE\xFF\x00\xE0"
"\xFE\xFF\x0D\xE0"
"\xFE\xFF\xDD\xE0"
49 changes: 49 additions & 0 deletions projects/dcmtk/dcmtk_meta_fuzzer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////

#include <cstdint>
#include <cstddef>
#include <new>

#include "dcmtk/dcmdata/dcmetinf.h"
#include "dcmtk/dcmdata/dcistrmb.h"
#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcxfer.h"

static constexpr std::size_t kNewCap = 2 * 1024 * 1024;
void* operator new(std::size_t n, const std::nothrow_t&) noexcept {
if (n > kNewCap) return nullptr;
try { return ::operator new(n); } catch (...) { return nullptr; }
}
void* operator new[](std::size_t n, const std::nothrow_t&) noexcept {
if (n > kNewCap) return nullptr;
try { return ::operator new[](n); } catch (...) { return nullptr; }
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
DcmInputBufferStream in;
in.setBuffer((void*)data, size);
in.setEos();

DcmMetaInfo mi;
const Uint32 kMaxReadLen = 128 * 1024;
if (mi.read(in, EXS_LittleEndianExplicit, EGL_noChange, kMaxReadLen).good()) {
OFString s;
(void)mi.findAndGetOFString(DCM_TransferSyntaxUID, s);
(void)mi.findAndGetOFString(DCM_SourceApplicationEntityTitle, s);
}
return 0;
}
Loading