Skip to content

Commit 9d07886

Browse files
authored
Add missing NULL checks (#871)
Motivation: We didn't have all the necessary NULL checks in place which could cause a segfault when an operation failed in native code due an OOME (for example). Modifications: Add missing NULL checks Result: Correctly handle error scenarios
1 parent c9b4b6a commit 9d07886

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

openssl-dynamic/src/main/c/cert_compress.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ static int compress(jobject compression_algorithm, jmethodID compress_method, SS
5454
return 0; // Unable to reserve space for compressed data
5555
}
5656
jbyte* resultData = (*e)->GetByteArrayElements(e, resultArray, NULL);
57+
if (resultData == NULL) {
58+
return 0;
59+
}
5760
memcpy(outData, resultData, resultLen);
5861
(*e)->ReleaseByteArrayElements(e, resultArray, resultData, JNI_ABORT);
5962
if (!CBB_did_write(out, resultLen)) {
@@ -102,6 +105,9 @@ static int decompress(jobject compression_algorithm, jmethodID decompress_method
102105
return 0; // Unable to allocate certificate decompression buffer
103106
}
104107
jbyte* resultData = (*e)->GetByteArrayElements(e, resultArray, NULL);
108+
if (resultData == NULL) {
109+
return 0;
110+
}
105111
memcpy(outData, resultData, uncompressed_len);
106112
(*e)->ReleaseByteArrayElements(e, resultArray, resultData, JNI_ABORT);
107113
return 1; // Success

openssl-dynamic/src/main/c/ssl.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,6 @@ TCN_IMPLEMENT_CALL(jbyteArray, SSL, getSessionId)(TCN_STDARGS, jlong ssl)
18491849
return NULL;
18501850
}
18511851

1852-
18531852
if ((bArray = (*e)->NewByteArray(e, len)) == NULL) {
18541853
return NULL;
18551854
}

openssl-dynamic/src/main/c/sslcontext.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,11 @@ TCN_IMPLEMENT_CALL(void, SSLContext, setSessionTicketKeys0)(TCN_STDARGS, jlong c
13631363
return;
13641364
}
13651365

1366-
b = (*e)->GetByteArrayElements(e, keys, NULL);
1366+
if ((b = (*e)->GetByteArrayElements(e, keys, NULL)) == NULL) {
1367+
tcn_ThrowException(e, "GetByteArrayElements() returned null");
1368+
return;
1369+
}
1370+
13671371
for (i = 0; i < cnt; ++i) {
13681372
key = b + (SSL_SESSION_TICKET_KEY_SIZE * i);
13691373
memcpy(ticket_keys[i].key_name, key, 16);
@@ -1495,7 +1499,7 @@ static jbyteArray get_certs(JNIEnv *e, SSL* ssl, STACK_OF(X509)* chain) {
14951499
length = i2d_X509(cert, &buf);
14961500
#endif // OPENSSL_IS_BORINGSSL
14971501

1498-
if (length <= 0 || (bArray = (*e)->NewByteArray(e, length)) == NULL ) {
1502+
if (length <= 0 || (bArray = (*e)->NewByteArray(e, length)) == NULL) {
14991503
NETTY_JNI_UTIL_DELETE_LOCAL(e, array);
15001504
array = NULL;
15011505
goto complete;
@@ -2173,7 +2177,11 @@ static enum ssl_private_key_result_t tcn_private_key_sign_java(SSL *ssl, uint8_t
21732177
} else {
21742178
arrayLen = (*e)->GetArrayLength(e, resultBytes);
21752179
if (max_out >= arrayLen) {
2176-
b = (*e)->GetByteArrayElements(e, resultBytes, NULL);
2180+
if ((b = (*e)->GetByteArrayElements(e, resultBytes, NULL)) == NULL) {
2181+
ret = ssl_private_key_failure;
2182+
goto complete;
2183+
}
2184+
21772185
memcpy(out, b, arrayLen);
21782186
(*e)->ReleaseByteArrayElements(e, resultBytes, b, JNI_ABORT);
21792187
*out_len = arrayLen;
@@ -2238,7 +2246,11 @@ static enum ssl_private_key_result_t tcn_private_key_decrypt_java(SSL *ssl, uint
22382246
} else {
22392247
arrayLen = (*e)->GetArrayLength(e, resultBytes);
22402248
if (max_out >= arrayLen) {
2241-
b = (*e)->GetByteArrayElements(e, resultBytes, NULL);
2249+
if ((b = (*e)->GetByteArrayElements(e, resultBytes, NULL)) == NULL) {
2250+
ret = ssl_private_key_failure;
2251+
goto complete;
2252+
}
2253+
22422254
memcpy(out, b, arrayLen);
22432255
(*e)->ReleaseByteArrayElements(e, resultBytes, b, JNI_ABORT);
22442256
*out_len = arrayLen;
@@ -2300,7 +2312,9 @@ static enum ssl_private_key_result_t tcn_private_key_complete_java(SSL *ssl, uin
23002312
// belong to us.
23012313
return ssl_private_key_failure;
23022314
}
2303-
b = (*e)->GetByteArrayElements(e, resultBytes, NULL);
2315+
if ((b = (*e)->GetByteArrayElements(e, resultBytes, NULL)) == NULL) {
2316+
return ssl_private_key_failure;
2317+
}
23042318
memcpy(out, b, arrayLen);
23052319
(*e)->ReleaseByteArrayElements(e, resultBytes, b, JNI_ABORT);
23062320
*out_len = arrayLen;

0 commit comments

Comments
 (0)