Skip to content

Commit 11176f8

Browse files
committed
fix stack buffer overflow in encryption setup
1 parent c2ecece commit 11176f8

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

src/crypto/clu_crypto_setup.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,10 @@ int wolfCLU_setup(int argc, char** argv, char action)
347347
WOLFCLU_LOG(WOLFCLU_L0,
348348
"-in flag was not set, please enter a string or"
349349
"file name to be encrypted: ");
350-
ret = (int) scanf("%s", inName);
350+
ret = (fgets(inName, sizeof(inName), stdin) != NULL) ? 1 : 0;
351+
if (ret > 0) {
352+
inName[strcspn(inName, "\n")] = '\0';
353+
}
351354
}
352355
in = inName;
353356
WOLFCLU_LOG(WOLFCLU_L0, "Encrypting :\"%s\"", inName);
@@ -397,7 +400,11 @@ int wolfCLU_setup(int argc, char** argv, char action)
397400
while (ret == 0) {
398401
WOLFCLU_LOG(WOLFCLU_L0,
399402
"Please enter a name for the output file: ");
400-
ret = (int) scanf("%s", outNameEnc);
403+
ret = (fgets(outNameEnc, sizeof(outNameEnc), stdin) != NULL)
404+
? 1 : 0;
405+
if (ret > 0) {
406+
outNameEnc[strcspn(outNameEnc, "\n")] = '\0';
407+
}
401408
out = (ret > 0) ? outNameEnc : '\0';
402409
}
403410
}
@@ -419,7 +426,11 @@ int wolfCLU_setup(int argc, char** argv, char action)
419426
while (ret == 0) {
420427
WOLFCLU_LOG(WOLFCLU_L0,
421428
"Please enter a name for the output file: ");
422-
ret = (int) scanf("%s", outNameDec);
429+
ret = (fgets(outNameDec, sizeof(outNameDec), stdin) != NULL)
430+
? 1 : 0;
431+
if (ret > 0) {
432+
outNameDec[strcspn(outNameDec, "\n")] = '\0';
433+
}
423434
out = (ret > 0) ? outNameDec : '\0';
424435
}
425436
}

tests/encrypt/enc-test.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,47 @@ fi
185185
rm -f test-dec.der
186186
rm -f test-enc.der
187187

188+
# Regression tests for stack buffer overflow fix (scanf -> fgets)
189+
190+
# Test: -in not provided, filename supplied via stdin (inName path, L344)
191+
printf "certs/crl.der\n" | ./wolfssl enc -aes-128-cbc -out test-stdin-in.enc -k "testpass" > /dev/null 2>&1
192+
if [ $? != 0 ]; then
193+
echo "Failed: enc with stdin input (no -in flag)"
194+
exit 99
195+
fi
196+
./wolfssl enc -d -aes-128-cbc -in test-stdin-in.enc -out test-stdin-in.dec -k "testpass" > /dev/null 2>&1
197+
diff certs/crl.der test-stdin-in.dec > /dev/null 2>&1
198+
if [ $? != 0 ]; then
199+
echo "Failed: stdin enc/dec roundtrip mismatch"
200+
exit 99
201+
fi
202+
rm -f test-stdin-in.enc test-stdin-in.dec
203+
204+
205+
# Test: outNameEnc/outNameDec via stdin (non-EVP path, Camellia)
206+
./wolfssl enc -camellia-128-cbc -in certs/crl.der -out test-cam-probe.enc -k "testpass" > /dev/null 2>&1
207+
if [ $? -eq 0 ]; then
208+
# outNameEnc: -out omitted, filename supplied via stdin
209+
printf "test-cam-stdin.enc\n" | ./wolfssl enc -camellia-128-cbc -in certs/crl.der -k "testpass" > /dev/null 2>&1
210+
if [ $? != 0 ]; then
211+
echo "Failed: Camellia enc with stdin output name (no -out flag)"
212+
exit 99
213+
fi
214+
215+
# outNameDec: -out omitted, filename supplied via stdin
216+
printf "test-cam-stdin.dec\n" | ./wolfssl enc -d -camellia-128-cbc -in test-cam-stdin.enc -k "testpass" > /dev/null 2>&1
217+
if [ $? != 0 ]; then
218+
echo "Failed: Camellia dec with stdin output name (no -out flag)"
219+
exit 99
220+
fi
221+
diff certs/crl.der test-cam-stdin.dec > /dev/null 2>&1
222+
if [ $? != 0 ]; then
223+
echo "Failed: Camellia stdin outName enc/dec roundtrip mismatch"
224+
exit 99
225+
fi
226+
227+
rm -f test-cam-probe.enc test-cam-stdin.enc test-cam-stdin.dec
228+
fi
229+
188230
echo "Done"
189231
exit 0

0 commit comments

Comments
 (0)