From a0d0763d4307d6f9a6957ea43198619e96abe715 Mon Sep 17 00:00:00 2001 From: "W. Felix Handte" Date: Mon, 6 Dec 2021 13:47:18 -0500 Subject: [PATCH] Reject Irregular Dictionary Files I hadn't seen #2890, so I wrote my own version. I like this approach a little better, since it does an explicit check for a regular file, rather than passing a magic value. Addresses #2874. --- programs/fileio.c | 21 ++++++++++++++++----- tests/playTests.sh | 5 +++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/programs/fileio.c b/programs/fileio.c index 29a2d66aac6..a7f0db5ffa4 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -732,22 +732,33 @@ static size_t FIO_createDictBuffer(void** bufferPtr, const char* fileName, FIO_p { FILE* fileHandle; U64 fileSize; + stat_t statbuf; assert(bufferPtr != NULL); *bufferPtr = NULL; if (fileName == NULL) return 0; DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName); + + if (!UTIL_stat(fileName, &statbuf)) { + EXM_THROW(31, "Stat failed on dictionary file %s.", fileName); + } + + if (!UTIL_isRegularFileStat(&statbuf)) { + EXM_THROW(32, "Dictionary %s must be a regular file.", fileName); + } + fileHandle = fopen(fileName, "rb"); - if (fileHandle==NULL) EXM_THROW(31, "%s: %s", fileName, strerror(errno)); - fileSize = UTIL_getFileSize(fileName); - if (fileSize == UTIL_FILESIZE_UNKNOWN) - EXM_THROW(32, "This file format is not supported : Dictionary file %s\n", fileName); + if (fileHandle == NULL) { + EXM_THROW(33, "Couldn't open dictionary %s: %s", fileName, strerror(errno)); + } + + fileSize = UTIL_getFileSizeStat(&statbuf); { size_t const dictSizeMax = prefs->patchFromMode ? prefs->memLimit : DICTSIZE_MAX; if (fileSize > dictSizeMax) { - EXM_THROW(32, "Dictionary file %s is too large (> %u bytes)", + EXM_THROW(34, "Dictionary file %s is too large (> %u bytes)", fileName, (unsigned)dictSizeMax); /* avoid extreme cases */ } } diff --git a/tests/playTests.sh b/tests/playTests.sh index 6dbfffca209..4a5ff4259c7 100755 --- a/tests/playTests.sh +++ b/tests/playTests.sh @@ -974,6 +974,11 @@ println "- Dictionary compression with btlazy2 strategy" zstd -f tmp -D tmpDict --zstd=strategy=6 zstd -d tmp.zst -D tmpDict -fo result $DIFF "$TESTFILE" result +if [ -e /dev/stdin ]; then + println "- Test rejecting irregular dictionary file" + cat tmpDict | zstd -f tmp -D /dev/stdin 2>&1 | grep 'regular file' || die "Correct error message not detected!" + cat tmpDict | zstd -d tmp.zst -D /dev/stdin -f 2>&1 | grep 'regular file' || die "Correct error message not detected!" +fi if [ -n "$hasMT" ] then println "- Test dictionary compression with multithreading "