Skip to content

Commit c823115

Browse files
committed
Avoid integer overflow in MemoryBIO
1 parent 57adc7f commit c823115

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ssl/MemoryBIO.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import java.nio.ByteBuffer;
4444

45+
import com.oracle.graal.python.util.OverflowException;
4546
import com.oracle.graal.python.util.PythonUtils;
4647

4748
/**
@@ -103,11 +104,11 @@ public void applyWrite(ByteBuffer buffer) {
103104
*
104105
* @param capacity Required capacity in bytes
105106
*/
106-
public void ensureWriteCapacity(int capacity) {
107+
public void ensureWriteCapacity(int capacity) throws OverflowException {
107108
if (bytes.length - writePosition < capacity) {
108109
int pending = getPending();
109110
if (bytes.length - pending < capacity) {
110-
byte[] newBytes = new byte[capacity + pending];
111+
byte[] newBytes = new byte[PythonUtils.addExact(capacity, pending)];
111112
PythonUtils.arraycopy(bytes, readPosition, newBytes, 0, pending);
112113
bytes = newBytes;
113114
} else {
@@ -138,7 +139,7 @@ public byte[] read(int length) {
138139
* @param from Data to be written
139140
* @param length Lenght of data to be written
140141
*/
141-
public void write(byte[] from, int length) {
142+
public void write(byte[] from, int length) throws OverflowException {
142143
ensureWriteCapacity(length);
143144
PythonUtils.arraycopy(from, 0, bytes, writePosition, Math.min(length, from.length));
144145
writePosition += length;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ssl/MemoryBIOBuiltins.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.ssl;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.MemoryError;
4344
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.SSLError;
4445
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
4546

@@ -59,6 +60,7 @@
5960
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode;
6061
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
6162
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
63+
import com.oracle.graal.python.util.OverflowException;
6264
import com.oracle.truffle.api.CompilerDirectives;
6365
import com.oracle.truffle.api.dsl.Fallback;
6466
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -131,6 +133,8 @@ int write(PMemoryBIO self, Object buffer,
131133
int len = lib.getBufferLength(buffer);
132134
self.getBio().write(bytes, len);
133135
return len;
136+
} catch (OverflowException e) {
137+
throw raise(MemoryError);
134138
} catch (UnsupportedMessageException e) {
135139
throw CompilerDirectives.shouldNotReachHere();
136140
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ssl/SSLEngineHelper.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects.ssl;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.MemoryError;
4344
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.OSError;
4445

4546
import java.io.IOException;
@@ -57,7 +58,9 @@
5758
import com.oracle.graal.python.builtins.objects.socket.SocketUtils.TimeoutHelper;
5859
import com.oracle.graal.python.nodes.ErrorMessages;
5960
import com.oracle.graal.python.nodes.PNodeWithRaise;
61+
import com.oracle.graal.python.nodes.PRaiseNode;
6062
import com.oracle.graal.python.runtime.exception.PException;
63+
import com.oracle.graal.python.util.OverflowException;
6164
import com.oracle.truffle.api.CompilerDirectives;
6265
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6366

@@ -308,13 +311,14 @@ private static void loop(PNodeWithRaise node, PSSLSocket socket, ByteBuffer appI
308311
} catch (IOException e) {
309312
// TODO better error handling, distinguish SSL errors and socket errors
310313
throw PRaiseSSLErrorNode.raiseUncached(node, SSLErrorCode.ERROR_SYSCALL, e.toString());
314+
} catch (OverflowException | OutOfMemoryError e) {
315+
throw PRaiseNode.raiseUncached(node, MemoryError);
311316
}
312317
// TODO handle other socket errors (NotYetConnected)
313-
// TODO handle OOM
314318
}
315319

316320
private static SSLEngineResult doUnwrap(SSLEngine engine, MemoryBIO networkInboundBIO, ByteBuffer targetBuffer, MemoryBIO applicationInboundBIO, boolean writeDirectlyToTarget)
317-
throws SSLException {
321+
throws SSLException, OverflowException {
318322
ByteBuffer readBuffer = networkInboundBIO.getBufferForReading();
319323
try {
320324
if (writeDirectlyToTarget) {
@@ -334,7 +338,7 @@ private static SSLEngineResult doUnwrap(SSLEngine engine, MemoryBIO networkInbou
334338
}
335339
}
336340

337-
private static SSLEngineResult doWrap(SSLEngine engine, ByteBuffer appInput, MemoryBIO networkOutboundBIO, int netBufferSize) throws SSLException {
341+
private static SSLEngineResult doWrap(SSLEngine engine, ByteBuffer appInput, MemoryBIO networkOutboundBIO, int netBufferSize) throws SSLException, OverflowException {
338342
networkOutboundBIO.ensureWriteCapacity(netBufferSize);
339343
ByteBuffer writeBuffer = networkOutboundBIO.getBufferForWriting();
340344
try {
@@ -351,7 +355,7 @@ private static PException handleSSLException(PNodeWithRaise node, SSLException e
351355
throw PRaiseSSLErrorNode.raiseUncached(node, SSLErrorCode.ERROR_SSL, e.toString());
352356
}
353357

354-
private static int obtainMoreInput(PNodeWithRaise node, SSLEngine engine, MemoryBIO networkInboundBIO, PSocket socket, TimeoutHelper timeoutHelper) throws IOException {
358+
private static int obtainMoreInput(PNodeWithRaise node, SSLEngine engine, MemoryBIO networkInboundBIO, PSocket socket, TimeoutHelper timeoutHelper) throws IOException, OverflowException {
355359
if (socket != null) {
356360
if (socket.getSocket() == null) {
357361
// TODO use raiseOsError with ENOTCONN

0 commit comments

Comments
 (0)