aajisaka commented on code in PR #58909:
URL: https://github.com/apache/spark/pull/58909#discussion_r4553600430


##########
common/network-common/src/main/java/org/apache/spark/network/crypto/GcmTransportCipher.java:
##########
@@ -442,8 +444,8 @@ public void channelRead(ChannelHandlerContext ctx, Object 
ciphertextMessage)
                         int readableBytes = Math.min(
                                 nettyBufReadableBytes,
                                 ciphertextBuffer.remaining());
-                        int expectedRemaining = (int) (expectedLength - 
ciphertextRead);
-                        int bytesToRead = Math.min(readableBytes, 
expectedRemaining);
+                        long expectedRemaining = expectedLength - 
ciphertextRead;
+                        int bytesToRead = (int) Math.min(readableBytes, 
expectedRemaining);

Review Comment:
   Fixed by 
https://github.com/apache/spark/commit/7512339b1f18b8b748a749cfb9b8b4a4befead33



##########
common/network-common/src/test/java/org/apache/spark/network/crypto/GcmAuthEngineSuite.java:
##########
@@ -570,6 +573,60 @@ public void testSplitLengthPrefix() throws Exception {
     }
   }
 
+  @Test
+  public void testCiphertextLengthLargerThanMaxInt() throws Exception {
+    TransportConf gcmConf = getConf(2, false);
+    try (AuthEngine client = new AuthEngine("appId", "secret", gcmConf);
+         AuthEngine server = new AuthEngine("appId", "secret", gcmConf)) {
+      AuthMessage clientChallenge = client.challenge();
+      AuthMessage serverResponse = server.response(clientChallenge);
+      client.deriveSessionCipher(clientChallenge, serverResponse);
+      GcmTransportCipher cipher = (GcmTransportCipher) server.sessionCipher();
+      GcmTransportCipher.DecryptionHandler decryptionHandler = 
cipher.getDecryptionHandler();
+      AesGcmHkdfStreaming streaming = cipher.getAesGcmHkdfStreaming();
+
+      // remaining ciphertext length is Integer.MAX_VALUE + 1,
+      // which becomes Integer.MIN_VALUE when cast to int.
+      long expectedLength = (long) GcmTransportCipher.LENGTH_HEADER_BYTES +
+              streaming.getHeaderLength() + Integer.MAX_VALUE + 1L;
+      StreamSegmentEncrypter encrypter = streaming.newStreamSegmentEncrypter(
+              Longs.toByteArray(expectedLength));
+      ByteBuffer header = encrypter.getHeader();
+      ByteBuf ciphertext = 
Unpooled.buffer(GcmTransportCipher.LENGTH_HEADER_BYTES +
+              header.remaining() + 1)
+              .writeLong(expectedLength)
+              .writeBytes(header)
+              .writeByte(0);
+      ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
+
+      decryptionHandler.channelRead(ctx, ciphertext);
+
+      verify(ctx, never()).fireChannelRead(any());
+    }
+  }
+
+  @Test
+  public void testInvalidExpectedCiphertextLength() throws Exception {
+    TransportConf gcmConf = getConf(2, false);
+    try (AuthEngine client = new AuthEngine("appId", "secret", gcmConf);
+         AuthEngine server = new AuthEngine("appId", "secret", gcmConf)) {
+      AuthMessage clientChallenge = client.challenge();
+      AuthMessage serverResponse = server.response(clientChallenge);
+      client.deriveSessionCipher(clientChallenge, serverResponse);
+      GcmTransportCipher cipher = (GcmTransportCipher) server.sessionCipher();
+      GcmTransportCipher.DecryptionHandler decryptionHandler = 
cipher.getDecryptionHandler();
+      long invalidLength = (long) GcmTransportCipher.LENGTH_HEADER_BYTES +
+              cipher.getAesGcmHkdfStreaming().getHeaderLength() - 1;
+      ByteBuf ciphertext = Unpooled.buffer(8).writeLong(invalidLength);

Review Comment:
   Fixed by 
https://github.com/apache/spark/commit/7512339b1f18b8b748a749cfb9b8b4a4befead33



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to