While AI is discovering new ways to make mistakes, human developers stay true to the classics: typos and memory errors. Let's take a look at them.
One of our readers suggested that we check the GmSSL project. Since we rarely cover projects from China, we decided to spend some time exploring this one.
GmSSL is an open-source cryptographic library written in C and developed by Peking University. It fully implements China's enterprise cryptographic standards (SM2/SM3/SM4/SM9) and secure communication protocols (TLCP, TLS 1.2/1.3). The library is extremely lightweight and even works in resource-constrained embedded systems that don't support dynamic memory allocation. It also supports SDF/SKF hardware security modules.
We didn't find any particularly critical or unusual bugs there. Instead, we encountered some of the most common and familiar types of errors. However, even though they're "standard," bugs are still bugs. Let's take a look.
The PVS-Studio warning: V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_ctrtest.c 101
If the second call to malloc fails, the function exits without first freeing the memory that plaintext points to. In other words, the memory previously allocated for another buffer isn't freed.
Here are some other code snippets where memory leaks may occur:
V773 The function was exited without releasing the 'aad' pointer. A memory leak is possible. sm4_ccmtest.c 128
V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_ctrtest.c 115
V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_ctrtest.c 123
V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_cbctest.c 124
V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_ofbtest.c 113
V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_ofbtest.c 127
V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_ofbtest.c 135
V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_cfbtest.c 122
V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_xtstest.c 115
V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_xtstest.c 129
V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_xtstest.c 137
V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_ccmtest.c 132
V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_ccmtest.c 148
V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_ccmtest.c 155
The PVS-Studio warning: V517 The use of 'if (A) {...} else if (A) {...}' pattern was detected. There is a probability of logical error presence. Check lines: 85, 88. skfutil.c 85
The two branches that check for the -lib flag perform different actions. This is clearly an error because the second branch can never execute. Instead, it should check for the -dev flag.
Let's look at another typo: a duplicated goto statement.
Here's another analyzer warning: V779 Unreachable code detected. It is possible that an error is present. sdfdecrypt.c 86
The failure to erase sensitive data
Cryptographic libraries usually delete data after using it to prevent sensitive information from being compromised. Developers often use memset for this purpose, which is a mistake. An optimizing compiler may determine that the program never uses the memory again after the memset call and remove the call entirely. Learn more here: CWE-14: Compiler Removal of Code to Clear Buffers.
Let's look at the shortest possible function that contains this error:
The PVS-Studio warning: V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sha2.c 196
Here are other functions that incorrectly clear memory:
V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sha2.c 380
V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sha2.c 402
V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sha2.c 423
V597 The compiler could delete the 'memset' function call, which is used to flush 'dgst' buffer. The memset_s() function should be used to erase the private data. sm3_kdf.c 52
V597 The compiler could delete the 'memset' function call, which is used to flush 'ctx' object. The memset_s() function should be used to erase the private data. sm3_pbkdf2.c 142
V597 The compiler could delete the 'memset' function call, which is used to flush 'key_block' buffer. The memset_s() function should be used to erase the private data. sm3_pbkdf2.c 143
V597 The compiler could delete the 'memset' function call, which is used to flush 'tmp_block' buffer. The memset_s() function should be used to erase the private data. sm3_pbkdf2.c 144
V597 The compiler could delete the 'memset' function call, which is used to flush 'ctx' object. The memset_s() function should be used to erase the private data. sm2_enc.c 56
V597 The compiler could delete the 'memset' function call, which is used to flush 'dgst' buffer. The memset_s() function should be used to erase the private data. sm2_enc.c 57
V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sm3.c 210
V597 The compiler could delete the 'memset' function call, which is used to flush 'sm3_ctx' object. The memset_s() function should be used to erase the private data. sm3.c 213
V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sm3_hmac.c 164
V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. ghash.c 178
V597 The compiler could delete the 'memset' function call, which is used to flush 'ghash_ctx' object. The memset_s() function should be used to erase the private data. ghash.c 181
V597 The compiler could delete the 'memset' function call, which is used to flush 'buf' buffer. The memset_s() function should be used to erase the private data. sdfdigest.c 244
V597 The compiler could delete the 'memset' function call, which is used to flush 'sm3_ctx' object. The memset_s() function should be used to erase the private data. sm3_kdf.c 51
The PVS-Studio warning: V629 Consider inspecting the '1 << window_size' expression. Bit shifting of the 32-bit value with a subsequent expansion to the 64-bit type. sm2_z256.c 336
The function generates a 64-bit mask. However, it's impossible to set the most significant bits since the 1 numeric literal is of the int type. An overflow will occur. The fixed code:
uint64_tmask=(1ui64<<window_size)-1;
Here's a similar warning: V629 Consider inspecting the '1 << window_size' expression. Bit shifting of the 32-bit value with a subsequent expansion to the 64-bit type. sm9_z256.c 344
The PVS-Studio warning: V614 Uninitialized variable 'sig_alg' used. tls_cert.c 623
The code uses the sig_alg uninitialized variable. It looks like the logic necessary to update this variable's value is missing.
P.S. GmSSL's unit tests often use buffers containing uninitialized data. In other words, they perform various operations on garbage data. I won't judge whether this is a sound approach, since the developers clearly chose it deliberately. Still, such unit-test code "smells", so I decided to mention it in the article.
This is an example that shows how to carefully use an uninitialized array:
The PVS-Studio warning: V614 Uninitialized buffer 'buf' used. Consider checking the second actual argument of the 'sm4_encrypt' function. sm4test.c 199
Other similar PVS-Studio warnings:
V614 Uninitialized buffer 'data' used. Consider checking the third actual argument of the 'tls_record_set_application_data' function. tlstest.c 404
V614 Uninitialized buffer 'verify_data' used. Consider checking the third actual argument of the 'tls_record_set_handshake_finished' function. tlstest.c 342
V614 Uninitialized buffer 'sig' used. Consider checking the third actual argument of the 'tls_record_set_handshake_certificate_verify' function. tlstest.c 320
V614 Uninitialized buffer 'random' used. Consider checking the fourth actual argument of the 'tls_record_set_handshake_server_hello' function. tlstest.c 254
V614 Uninitialized buffer 'random' used. Consider checking the fourth actual argument of the 'tls_record_set_handshake_client_hello' function. tlstest.c 229
V614 Uninitialized buffer 'iv' used. Consider checking the third actual argument of the 'zuc_init' function. zuctest.c 508
V614 Uninitialized buffer 'key' used. Consider checking the second actual argument of the 'zuc_init' function. zuctest.c 508
V614 Uninitialized buffer 'iv' used. Consider checking the third actual argument of the 'zuc_init' function. zuctest.c 480
V614 Uninitialized buffer 'key' used. Consider checking the second actual argument of the 'zuc_init' function. zuctest.c 480
V614 Uninitialized buffer 'blocks' used. Consider checking the second actual argument of the 'ghash_update' function. ghashtest.c 169
V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'hss_sign_update' function. lmstest.c 895
V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'hss_sign_update' function. lmstest.c 845
V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'hss_sign_update' function. lmstest.c 796
V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'lms_sign_update' function. lmstest.c 417
V614 Uninitialized buffer 'serial_buf' used. Consider checking the eighth actual argument of the 'cms_signer_infos_add_signer_info' function. cmstest.c 494
V614 Uninitialized buffer 'sig_buf' used. Consider checking the tenth actual argument of the 'cms_signer_info_to_der' function. cmstest.c 331
V614 Uninitialized buffer 'serial_buf' used. Consider checking the fourth actual argument of the 'cms_signer_info_to_der' function. cmstest.c 327
V614 Uninitialized buffer 'c3' used. Consider checking the fourth actual argument of the 'sm9_ciphertext_to_der' function. sm9test.c 858
V614 Uninitialized buffer 'c2' used. Consider checking the second actual argument of the 'sm9_ciphertext_to_der' function. sm9test.c 858
V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the sixth actual argument of the 'sm4_ccm_encrypt' function. sm4_ccmtest.c 382
V614 Uninitialized buffer 'aad' used. Consider checking the fourth actual argument of the 'sm4_ccm_encrypt' function. sm4_ccmtest.c 382
V614 Uninitialized buffer 'iv' used. Consider checking the second actual argument of the 'sm4_ccm_encrypt' function. sm4_ccmtest.c 382
V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'x509_sign_update' function. x509_keytest.c 434
V614 Uninitialized buffer 'attrs' used. Consider checking the fifth actual argument of the 'x509_req_sign_to_der' function. x509_reqtest.c 161
V614 Uninitialized buffer 'attrs_buf' used. Consider checking the fifth actual argument of the 'x509_request_info_to_der' function. x509_reqtest.c 52
V614 Uninitialized buffer 'iv' used. Consider checking the seventh actual argument of the 'pbes2_algor_to_der' function. pkcs8test.c 234
V614 Uninitialized buffer 'iv' used. Consider checking the seventh actual argument of the 'pbes2_params_to_der' function. pkcs8test.c 185
V614 Uninitialized variable 'saltlen' used. Consider checking the sixth actual argument of the 'format_bytes' function. pkcs8test.c 115
V614 Uninitialized pointer 'psalt' used. Consider checking the fifth actual argument of the 'format_bytes' function. pkcs8test.c 115
V614 Uninitialized buffer 'plaintext' used. Consider checking the second actual argument of the 'sm2_encrypt_update' function. sm2_enctest.c 338
V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'sm2_sign_update' function. sm2_signtest.c 196
V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the sixth actual argument of the 'sm4_gcm_encrypt' function. sm4_gcmtest.c 547
V614 Uninitialized buffer 'aad' used. Consider checking the fourth actual argument of the 'sm4_gcm_encrypt' function. sm4_gcmtest.c 547
V614 Uninitialized buffer 'iv' used. Consider checking the second actual argument of the 'sm4_gcm_encrypt' function. sm4_gcmtest.c 547
V614 Uninitialized buffer 'blocks' used. Consider checking the second actual argument of the 'sm3_update' function. sm3test.c 189
V614 Uninitialized buffer 'buf1' used. Consider checking the third actual argument of the 'sm4_cbc_padding_encrypt' function. sm4_cbctest.c 169
V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_ctr32_encrypt_blocks' function. sm4test.c 336
V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_ctr_encrypt_blocks' function. sm4test.c 308
V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_cbc_decrypt_blocks' function. sm4test.c 280
V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_cbc_encrypt_blocks' function. sm4test.c 253
V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the second actual argument of the 'sm4_encrypt_blocks' function. sm4test.c 225
V614 Uninitialized buffer 'attrs' used. Consider checking the fifth actual argument of the 'x509_req_sign_to_der' function. reqgen.c 245
V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2451
V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2349
V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2250
V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2167
V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2099
V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2064
Conclusion
We already know how people make mistakes. Download and use PVS-Studio to catch such errors and potential vulnerabilities as early as possible.
We're still learning how AI makes mistakes, but we're starting to see where to take our analyzer next. One promising direction, for example, is detecting duplicate and similar code. Either way, PVS-Studio can already help you check AI-generated code.
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)