Skip to content

Commit 40cf471

Browse files
authored
Fix: DOGTAG-4608 [RHCS 9.7.z] TPS Not closing connections. (#5425)
This simple fix prevents a malformed compressed PKCS11 Object on the token from going into an endless loop. Minor fix added to apease reviews and be more defensive.
1 parent 08afde2 commit 40cf471

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

base/tps/src/main/java/org/dogtagpki/server/tps/main/PKCS11Obj.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -502,21 +502,30 @@ static private TPSBuffer uncompress(TPSBuffer compressedData) throws TPSExceptio
502502
byte[] data = compressedData.toBytesArray();
503503

504504
Inflater inflater = new Inflater();
505-
inflater.setInput(data);
506-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
507-
byte[] buffer = new byte[1024];
508-
while (!inflater.finished()) {
509-
int count = inflater.inflate(buffer);
510-
outputStream.write(buffer, 0, count);
511-
}
512-
outputStream.close();
513-
byte[] output = outputStream.toByteArray();
514-
logger.debug("Original: " + data.length);
515-
logger.debug("Uncompressed: " + output.length);
516505

517-
TPSBuffer result = new TPSBuffer(output);
506+
try {
507+
inflater.setInput(data);
508+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
509+
byte[] buffer = new byte[1024];
510+
while (!inflater.finished()) {
511+
int count = inflater.inflate(buffer);
512+
//Prevent possible spinning on corrupted data.
513+
if (count == 0 && !inflater.finished()) {
514+
throw new TPSException("PKCS11Obj.uncompress: incomplete or corrupted compressed data");
515+
}
516+
outputStream.write(buffer, 0, count);
517+
}
518+
outputStream.close();
519+
byte[] output = outputStream.toByteArray();
520+
logger.debug("Original: " + data.length);
521+
logger.debug("Uncompressed: " + output.length);
518522

519-
return result;
523+
TPSBuffer result = new TPSBuffer(output);
524+
525+
return result;
526+
} finally {
527+
inflater.end();
528+
}
520529
}
521530

522531
public static void main(String[] args) throws TPSException, DataFormatException, IOException {

0 commit comments

Comments
 (0)