Skip to content

Commit

Permalink
8275811: Incorrect instance to dispose
Browse files Browse the repository at this point in the history
Reviewed-by: xuelei
  • Loading branch information
djelinski authored and XueleiFan committed Nov 16, 2021
1 parent b0a463f commit cddc6ce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void changeReadCiphers(SSLReadCipher readCipher) {
* Since MAC's doFinal() is called for every SSL/TLS packet, it's
* not necessary to do the same with MAC's.
*/
readCipher.dispose();
this.readCipher.dispose();

this.readCipher = readCipher;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ abstract void encodeHandshake(byte[] buffer,
// SSLEngine and SSLSocket
abstract void encodeChangeCipherSpec() throws IOException;

// SSLEngine and SSLSocket
void disposeWriteCipher() {
throw new UnsupportedOperationException();
}

// apply to SSLEngine only
Ciphertext encode(
ByteBuffer[] srcs, int srcsOffset, int srcsLength,
Expand Down Expand Up @@ -190,7 +195,7 @@ void changeWriteCiphers(SSLWriteCipher writeCipher,
* Since MAC's doFinal() is called for every SSL/TLS packet, it's
* not necessary to do the same with MAC's.
*/
writeCipher.dispose();
disposeWriteCipher();

this.writeCipher = writeCipher;
this.isFirstAppOutputRecord = true;
Expand Down Expand Up @@ -219,7 +224,7 @@ void changeWriteCiphers(SSLWriteCipher writeCipher,
flush();

// Dispose of any intermediate state in the underlying cipher.
writeCipher.dispose();
disposeWriteCipher();

this.writeCipher = writeCipher;
this.isFirstAppOutputRecord = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ void encodeChangeCipherSpec() throws IOException {
fragmenter.queueUpChangeCipherSpec();
}

@Override
void disposeWriteCipher() {
if (fragmenter == null) {
writeCipher.dispose();
} else {
fragmenter.queueUpCipherDispose();
}
}

@Override
void encodeV2NoCipher() throws IOException {
isTalkingToV2 = true;
Expand Down Expand Up @@ -361,6 +370,7 @@ private static class RecordMemo {
byte majorVersion;
byte minorVersion;
SSLWriteCipher encodeCipher;
boolean disposeCipher;

byte[] fragment;
}
Expand Down Expand Up @@ -422,6 +432,15 @@ void queueUpAlert(byte level, byte description) {
handshakeMemos.add(memo);
}

void queueUpCipherDispose() {
RecordMemo lastMemo = handshakeMemos.peekLast();
if (lastMemo != null) {
lastMemo.disposeCipher = true;
} else {
writeCipher.dispose();
}
}

Ciphertext acquireCiphertext(ByteBuffer dstBuf) throws IOException {
if (isEmpty()) {
return null;
Expand Down Expand Up @@ -521,6 +540,9 @@ Ciphertext acquireCiphertext(ByteBuffer dstBuf) throws IOException {
dstPos, dstLim, headerSize,
ProtocolVersion.valueOf(memo.majorVersion,
memo.minorVersion));
if (memo.disposeCipher) {
memo.encodeCipher.dispose();
}

if (SSLLogger.isOn && SSLLogger.isOn("packet")) {
ByteBuffer temporary = dstBuf.duplicate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ void encodeChangeCipherSpec() throws IOException {
}
}

@Override
void disposeWriteCipher() {
writeCipher.dispose();
}

@Override
public void flush() throws IOException {
recordLock.lock();
Expand Down

1 comment on commit cddc6ce

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.