Skip to content

Commit 2a84a98

Browse files
authored
Fix a bunch of places we forget to aws_raise_error() (#622)
1 parent 4c65ce5 commit 2a84a98

File tree

8 files changed

+75
-45
lines changed

8 files changed

+75
-45
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- 'main'
77

88
env:
9-
BUILDER_VERSION: v0.9.48
9+
BUILDER_VERSION: v0.9.55
1010
BUILDER_SOURCE: releases
1111
BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net
1212
PACKAGE_NAME: aws-c-io

source/channel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ int aws_channel_slot_insert_end(struct aws_channel *channel, struct aws_channel_
743743
}
744744

745745
AWS_ASSERT(0);
746-
return AWS_OP_ERR;
746+
return aws_raise_error(AWS_ERROR_INVALID_STATE);
747747
}
748748

749749
int aws_channel_slot_insert_left(struct aws_channel_slot *slot, struct aws_channel_slot *to_add) {

source/darwin/darwin_pki_utils.c

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,8 @@ int aws_import_public_and_private_keys_to_identity(
101101

102102
int result = AWS_OP_ERR;
103103

104-
CFDataRef cert_data = CFDataCreate(cf_alloc, public_cert_chain->ptr, public_cert_chain->len);
105-
CFDataRef key_data = CFDataCreate(cf_alloc, private_key->ptr, private_key->len);
106-
107-
if (!cert_data || !key_data) {
108-
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
109-
}
104+
CFDataRef cert_data = NULL;
105+
CFDataRef key_data = NULL;
110106

111107
CFArrayRef cert_import_output = NULL;
112108
CFArrayRef key_import_output = NULL;
@@ -118,9 +114,26 @@ int aws_import_public_and_private_keys_to_identity(
118114
import_params.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
119115
import_params.passphrase = CFSTR("");
120116

117+
struct aws_array_list cert_chain_list;
118+
AWS_ZERO_STRUCT(cert_chain_list);
119+
CFDataRef root_cert_data = NULL;
121120
SecCertificateRef certificate_ref = NULL;
122121
SecKeychainRef import_keychain = NULL;
123122

123+
cert_data = CFDataCreate(cf_alloc, public_cert_chain->ptr, public_cert_chain->len);
124+
if (!cert_data) {
125+
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: failed creating public cert chain data.");
126+
result = aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
127+
goto done;
128+
}
129+
130+
key_data = CFDataCreate(cf_alloc, private_key->ptr, private_key->len);
131+
if (!key_data) {
132+
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: failed creating private key data.");
133+
result = aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
134+
goto done;
135+
}
136+
124137
# pragma clang diagnostic push
125138
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
126139
/* SecKeychain functions are marked as deprecated.
@@ -134,7 +147,8 @@ int aws_import_public_and_private_keys_to_identity(
134147
"static: error opening keychain \"%s\" with OSStatus %d",
135148
aws_string_c_str(keychain_path),
136149
keychain_status);
137-
return AWS_OP_ERR;
150+
result = aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
151+
goto done;
138152
}
139153
keychain_status = SecKeychainUnlock(import_keychain, 0, "", true);
140154
if (keychain_status != errSecSuccess) {
@@ -143,14 +157,16 @@ int aws_import_public_and_private_keys_to_identity(
143157
"static: error unlocking keychain \"%s\" with OSStatus %d",
144158
aws_string_c_str(keychain_path),
145159
keychain_status);
146-
return AWS_OP_ERR;
160+
result = aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
161+
goto done;
147162
}
148163
} else {
149164
OSStatus keychain_status = SecKeychainCopyDefault(&import_keychain);
150165
if (keychain_status != errSecSuccess) {
151166
AWS_LOGF_ERROR(
152167
AWS_LS_IO_PKI, "static: error opening the default keychain with OSStatus %d", keychain_status);
153-
return AWS_OP_ERR;
168+
result = aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
169+
goto done;
154170
}
155171
}
156172

@@ -168,9 +184,6 @@ int aws_import_public_and_private_keys_to_identity(
168184
OSStatus key_status =
169185
SecItemImport(key_data, NULL, &format, &item_type, 0, &import_params, import_keychain, &key_import_output);
170186

171-
CFRelease(cert_data);
172-
CFRelease(key_data);
173-
174187
if (cert_status != errSecSuccess && cert_status != errSecDuplicateItem) {
175188
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: error importing certificate with OSStatus %d", (int)cert_status);
176189
result = aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE);
@@ -201,11 +214,8 @@ int aws_import_public_and_private_keys_to_identity(
201214
AWS_LS_IO_PKI,
202215
"static: certificate has an existing certificate-key pair that was previously imported into the Keychain. "
203216
"Using key from Keychain instead of the one provided.");
204-
struct aws_array_list cert_chain_list;
205-
206217
if (aws_pem_objects_init_from_file_contents(&cert_chain_list, alloc, *public_cert_chain)) {
207218
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: decoding certificate PEM failed.");
208-
aws_pem_objects_clean_up(&cert_chain_list);
209219
result = AWS_OP_ERR;
210220
goto done;
211221
}
@@ -214,36 +224,46 @@ int aws_import_public_and_private_keys_to_identity(
214224
aws_array_list_get_at_ptr(&cert_chain_list, (void **)&root_cert_ptr, 0);
215225
AWS_ASSERT(root_cert_ptr);
216226
CFDataRef root_cert_data = CFDataCreate(cf_alloc, root_cert_ptr->data.buffer, root_cert_ptr->data.len);
217-
218-
if (root_cert_data) {
219-
certificate_ref = SecCertificateCreateWithData(cf_alloc, root_cert_data);
220-
CFRelease(root_cert_data);
227+
if (!root_cert_data) {
228+
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: failed creating root cert data.");
229+
result = aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
230+
goto done;
221231
}
222232

223-
aws_pem_objects_clean_up(&cert_chain_list);
233+
certificate_ref = SecCertificateCreateWithData(cf_alloc, root_cert_data);
234+
if (!certificate_ref) {
235+
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: failed to create certificate.");
236+
result = aws_raise_error(AWS_IO_FILE_VALIDATION_FAILURE);
237+
goto done;
238+
}
224239
} else {
225240
certificate_ref = (SecCertificateRef)CFArrayGetValueAtIndex(cert_import_output, 0);
226241
/* SecCertificateCreateWithData returns an object with +1 retain, so we need to match that behavior here */
227242
CFRetain(certificate_ref);
228243
}
229244

230-
/* if we got a cert one way or the other, create the identity and return it */
231-
if (certificate_ref) {
232-
SecIdentityRef identity_output;
233-
OSStatus status = SecIdentityCreateWithCertificate(import_keychain, certificate_ref, &identity_output);
234-
if (status == errSecSuccess) {
235-
CFTypeRef certs[] = {identity_output};
236-
*identity = CFArrayCreate(cf_alloc, (const void **)certs, 1L, &kCFTypeArrayCallBacks);
237-
result = AWS_OP_SUCCESS;
238-
goto done;
239-
}
245+
/* we got a cert one way or the other, create the identity and return it */
246+
AWS_ASSERT(certificate_ref);
247+
SecIdentityRef identity_output;
248+
OSStatus status = SecIdentityCreateWithCertificate(import_keychain, certificate_ref, &identity_output);
249+
if (status != errSecSuccess) {
250+
AWS_LOGF_ERROR(AWS_LS_IO_PKI, "static: error creating identity with OSStatus %d", key_status);
251+
result = aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
252+
goto done;
240253
}
241254

255+
CFTypeRef certs[] = {identity_output};
256+
*identity = CFArrayCreate(cf_alloc, (const void **)certs, 1L, &kCFTypeArrayCallBacks);
257+
result = AWS_OP_SUCCESS;
258+
242259
done:
243260
aws_mutex_unlock(&s_sec_mutex);
244261
if (certificate_ref) {
245262
CFRelease(certificate_ref);
246263
}
264+
if (root_cert_data) {
265+
CFRelease(root_cert_data);
266+
}
247267
if (cert_import_output) {
248268
CFRelease(cert_import_output);
249269
}
@@ -253,6 +273,13 @@ int aws_import_public_and_private_keys_to_identity(
253273
if (import_keychain) {
254274
CFRelease(import_keychain);
255275
}
276+
if (cert_data) {
277+
CFRelease(cert_data);
278+
}
279+
if (key_data) {
280+
CFRelease(key_data);
281+
}
282+
aws_pem_objects_clean_up(&cert_chain_list);
256283

257284
return result;
258285
}

source/darwin/secure_transport_tls_channel_handler.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ static int s_drive_negotiation(struct aws_channel_handler *handler) {
335335
&secure_transport_handler->protocol, handler->alloc, (size_t)CFStringGetLength(protocol) + 1)) {
336336
CFRelease(protocol);
337337
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
338-
return AWS_OP_ERR;
338+
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
339339
}
340340

341341
memset(secure_transport_handler->protocol.buffer, 0, secure_transport_handler->protocol.capacity);
@@ -399,15 +399,15 @@ static int s_drive_negotiation(struct aws_channel_handler *handler) {
399399
if (secure_transport_handler->verify_peer) {
400400
if (!secure_transport_handler->ca_certs) {
401401
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
402-
return AWS_OP_ERR;
402+
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
403403
}
404404

405405
SecTrustRef trust;
406406
status = SSLCopyPeerTrust(secure_transport_handler->ctx, &trust);
407407

408408
if (status != errSecSuccess) {
409409
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
410-
return AWS_OP_ERR;
410+
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
411411
}
412412

413413
SecPolicyRef policy;
@@ -428,7 +428,7 @@ static int s_drive_negotiation(struct aws_channel_handler *handler) {
428428
AWS_LOGF_ERROR(AWS_LS_IO_TLS, "id=%p: Failed to set trust policy %d\n", (void *)handler, (int)status);
429429
CFRelease(trust);
430430
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
431-
return AWS_OP_ERR;
431+
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
432432
}
433433

434434
status = SecTrustSetAnchorCertificates(trust, secure_transport_handler->ca_certs);
@@ -440,7 +440,7 @@ static int s_drive_negotiation(struct aws_channel_handler *handler) {
440440
(int)status);
441441
CFRelease(trust);
442442
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
443-
return AWS_OP_ERR;
443+
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
444444
}
445445

446446
/* Use ONLY the custom CA bundle (ignoring system anchors) */
@@ -453,7 +453,7 @@ static int s_drive_negotiation(struct aws_channel_handler *handler) {
453453
(int)status);
454454
CFRelease(trust);
455455
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
456-
return AWS_OP_ERR;
456+
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
457457
}
458458

459459
SecTrustResultType trust_eval = 0;
@@ -471,17 +471,16 @@ static int s_drive_negotiation(struct aws_channel_handler *handler) {
471471
(void *)handler,
472472
(int)status,
473473
(int)trust_eval);
474-
return AWS_OP_ERR;
474+
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
475475
}
476476
return s_drive_negotiation(handler);
477477
/* if this is here, everything went wrong. */
478478
} else if (status != errSSLWouldBlock) {
479479
secure_transport_handler->negotiation_finished = false;
480480

481481
AWS_LOGF_WARN(AWS_LS_IO_TLS, "id=%p: negotiation failed with OSStatus %d.", (void *)handler, (int)status);
482-
aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
483482
s_invoke_negotiation_callback(handler, AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
484-
return AWS_OP_ERR;
483+
return aws_raise_error(AWS_IO_TLS_ERROR_NEGOTIATION_FAILURE);
485484
}
486485

487486
return AWS_OP_SUCCESS;

source/event_loop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ int aws_event_loop_fetch_local_object(
402402
return AWS_OP_SUCCESS;
403403
}
404404

405-
return AWS_OP_ERR;
405+
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
406406
}
407407

408408
int aws_event_loop_put_local_object(struct aws_event_loop *event_loop, struct aws_event_loop_local_object *obj) {

source/posix/socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,7 @@ int aws_socket_get_error(struct aws_socket *socket) {
18921892
socklen_t result_length = sizeof(connect_result);
18931893

18941894
if (getsockopt(socket->io_handle.data.fd, SOL_SOCKET, SO_ERROR, &connect_result, &result_length) < 0) {
1895-
return AWS_OP_ERR;
1895+
return s_determine_socket_error(errno);
18961896
}
18971897

18981898
if (connect_result) {

source/windows/iocp/iocp_event_loop.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ static int s_unsubscribe_from_io_events(struct aws_event_loop *event_loop, struc
635635
"id=%p: failed to un-subscribe from events on handle %p",
636636
(void *)event_loop,
637637
(void *)handle->data.handle);
638-
return AWS_OP_ERR;
638+
return aws_raise_error(AWS_ERROR_SYS_CALL_FAILURE);
639639
}
640640

641641
static void s_free_io_event_resources(void *user_data) {

source/windows/secure_channel_tls_handler.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,9 +1367,13 @@ static int s_process_write_message(
13671367
struct aws_io_message *outgoing_message =
13681368
aws_channel_acquire_message_from_pool(slot->channel, AWS_IO_MESSAGE_APPLICATION_DATA, to_write);
13691369

1370-
if (!outgoing_message || outgoing_message->message_data.capacity <= upstream_overhead) {
1370+
if (!outgoing_message) {
13711371
return AWS_OP_ERR;
13721372
}
1373+
if (outgoing_message->message_data.capacity <= upstream_overhead) {
1374+
aws_mem_release(outgoing_message->allocator, outgoing_message);
1375+
return aws_raise_error(AWS_ERROR_INVALID_STATE);
1376+
}
13731377

13741378
/* what if message is larger than one record? */
13751379
size_t original_message_fragment_to_process = outgoing_message->message_data.capacity - upstream_overhead;

0 commit comments

Comments
 (0)