DEV Community

Unicorn Developer
Unicorn Developer

Posted on

Classic C and errors of GmSSL project

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.

1427_GmSSL/image1.png

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.

Potential null pointer dereference

static int tls12_select_parameters(TLS_CONNECT *conn,
  const int *common_cipher_suites, ....)
{
  const uint8_t *cert_chains = conn->ctx->cert_chains;
  size_t cert_chains_len = conn->ctx->cert_chains_len;
  size_t cert_chain_idx;

  if (!conn || !common_cipher_suites || !common_cipher_suites_cnt
    || !common_supported_groups || !common_supported_groups_cnt
    || !common_signature_algorithms || !common_signature_algorithms_cnt)
  {
    error_print();
    return -1;
  }
  ....
}
Enter fullscreen mode Exit fullscreen mode

The PVS-Studio warning: V595 The 'conn' pointer was utilized before it was verified against nullptr. Check lines: 807, 811. tls12.c 807

Because of the conn pointer check, we can assume that this pointer may be NULL. However, the pointer is dereferenced before the check:

const uint8_t *cert_chains = conn->ctx->cert_chains;
....
if (!conn
Enter fullscreen mode Exit fullscreen mode

The input argument check should be placed at the beginning of the function.

Potential memory leaks

In C, manual memory management often leads to cases where developers forget to free memory in internal error handlers. GmSSL is not unique here.

static int test_sm4_ctr_test_vectors(void)
{
  ....
  uint8_t *plaintext;
  ....
  if ((plaintext =
         (uint8_t *)malloc(strlen(tests[i].plaintext)/2)) == NULL)
  {
    error_print();
    return -1;
  }
  if ((ciphertext =
         (uint8_t *)malloc(strlen(tests[i].ciphertext)/2)) == NULL)
  {
    error_print();
    return -1;
  }
  ....
}
Enter fullscreen mode Exit fullscreen mode

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:
  1. V773 The function was exited without releasing the 'aad' pointer. A memory leak is possible. sm4_ccmtest.c 128
  2. V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_ctrtest.c 115
  3. V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_ctrtest.c 123
  4. V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_cbctest.c 124
  5. V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_ofbtest.c 113
  6. V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_ofbtest.c 127
  7. V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_ofbtest.c 135
  8. V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_cfbtest.c 122
  9. V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_xtstest.c 115
  10. V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_xtstest.c 129
  11. V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_xtstest.c 137
  12. V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_ccmtest.c 132
  13. V773 The function was exited without releasing the 'encrypted' pointer. A memory leak is possible. sm4_ccmtest.c 148
  14. V773 The function was exited without releasing the 'decrypted' pointer. A memory leak is possible. sm4_ccmtest.c 155

Typos

int skfutil_main(int argc, char **argv)
{
  int ret = 1;
  ....
  } else if (!strcmp(*argv, "-lib")) {      // <=
    if (--argc < 1) goto bad;
    lib = *(++argv);
  } else if (!strcmp(*argv, "-lib")) {      // <=
    if (--argc < 1) goto bad;
    devname = *(++argv);
  } else if (!strcmp(*argv, "-devinfo")) {
  ....
}
Enter fullscreen mode Exit fullscreen mode

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.

int sdfencrypt_main(int argc, char **argv)
{
  ....
  if (!strcmp(*argv, "-help")) {
    printf("usage: gmssl %s %s\n", prog, usage);
    printf("%s\n", options);
    ret = 0;
    goto end;
    goto end;
  } else if (!strcmp(*argv, "-lib")) {
  ....
}
Enter fullscreen mode Exit fullscreen mode

The PVS-Studio warning: V779 Unreachable code detected. It is possible that an error is present. sdfencrypt.c 86

In this case, the typo has no consequences. However, this doesn't mean such typos are always harmless: CVE-2014-1266 — Finding More Than One Worm in the Apple: If you see something, say something comes to mind.

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:

void sha224_finish(SHA224_CTX *ctx, unsigned char dgst[SHA224_DIGEST_SIZE])
{
  uint8_t buf[SHA256_DIGEST_SIZE];
  sha256_finish((SHA256_CTX *)ctx, buf);
  memcpy(dgst, buf, SHA224_DIGEST_SIZE);
  memset(buf, 0, sizeof(buf));
}
Enter fullscreen mode Exit fullscreen mode

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:
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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 error of creating a 64-bit mask

int sm2_z256_get_booth(const sm2_z256_t a,
                       unsigned int window_size, int i)
{
  uint64_t mask = (1 << window_size) - 1;
  ....
}
Enter fullscreen mode Exit fullscreen mode

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_t mask = (1ui64 << window_size) - 1;
Enter fullscreen mode Exit fullscreen mode

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

A logic error

int tls13_process_client_pre_shared_key_from_ticket(
  TLS_CONNECT *conn, const uint8_t *ext_data, size_t ext_datalen)
{
  int ret;
  ....
  for (i = 0; identitieslen; i++) {
  ....
    if ((ret = tls13_psk_binder_verify(conn->digest,
      pre_shared_key, sizeof(pre_shared_key),
      &dgst_ctx, binder, binderlen)) != 1) {
      error_print();
      return -1;
    } else if (ret == 0) {
      continue;
    }
  ....
}
Enter fullscreen mode Exit fullscreen mode

The PVS-Studio warning: V547 Expression 'ret == 0' is always false. tls_psk.c 1572

The continue statement is never executed because the ret variable always equals 1 here. The code has been shortened for readability:

if ((ret = foo()) != 1) {
  ....
} else if (ret == 0) {  // ret equals 1 here
Enter fullscreen mode Exit fullscreen mode

The code contains a logic error or a typo.

There are two other similar errors:

  1. V547 Expression 'ret == 0' is always false. tls_psk.c 1897
  2. V547 Expression 'ret == 0' is always false. tlcp.c 2042

An uninitialized variable is used

int tls12_cert_chains_select(....)
{
  ....
  for (i = 1; cert_chains_len; i++) {
    const uint8_t *cert_chain;
    size_t cert_chain_len;
    int sig_alg;
    int ret;

    if (tls_uint24array_from_bytes(&cert_chain, &cert_chain_len,
      &cert_chains, &cert_chains_len) != 1) {
      error_print();
      return -1;
    }

    if (certs) *certs = cert_chain;
    if (certs_len) *certs_len = cert_chain_len;
    if (certs_idx) *certs_idx = i;
    if (prefered_sig_alg) *prefered_sig_alg = sig_alg;       // <=
    return 1;
  }
  ....
}
Enter fullscreen mode Exit fullscreen mode

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:
#define GETU32(p) \
  ((uint32_t)(p)[0] << 24 | \
   (uint32_t)(p)[1] << 16 | \
   (uint32_t)(p)[2] <<  8 | \
   (uint32_t)(p)[3])

#define PUTU32(p,V) \
  ((p)[0] = (uint8_t)((V) >> 24), \
   (p)[1] = (uint8_t)((V) >> 16), \
   (p)[2] = (uint8_t)((V) >>  8), \
   (p)[3] = (uint8_t)(V))

void sm4_encrypt(const SM4_KEY *key,
                 const uint8_t in[16], uint8_t out[16])
{
  uint32_t X0, X1, X2, X3, X4;
  int i;

  X0 = GETU32(in     );
  X1 = GETU32(in +  4);
  X2 = GETU32(in +  8);
  X3 = GETU32(in + 12);

  for (i = 0; i < 32; i++) {

    X4 = X1 ^ X2 ^ X3 ^ key->rk[i];
    X4 = S32(X4);
    X4 = X0 ^ L32(X4);

    X0 = X1;
    X1 = X2;
    X2 = X3;
    X3 = X4;
  }

  PUTU32(out     , X3);
  PUTU32(out +  4, X2);
  PUTU32(out +  8, X1);
  PUTU32(out + 12, X0);
}

static int speed_sm4_encrypt(void)
{
  SM4_KEY sm4_key;
  uint8_t key[16] = {0};
  uint8_t buf[16];
  size_t nbytes = 16 * 1024 * 1024;
  clock_t begin, end;
  double seconds;
  size_t i;

  sm4_set_encrypt_key(&sm4_key, key);
  for (i = 0; i < nbytes/sizeof(buf); i++) {
    sm4_encrypt(&sm4_key, buf, buf);      // <= buf is uninitialized
  }
  ....
}
Enter fullscreen mode Exit fullscreen mode

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:

  1. V614 Uninitialized buffer 'data' used. Consider checking the third actual argument of the 'tls_record_set_application_data' function. tlstest.c 404
  2. V614 Uninitialized buffer 'verify_data' used. Consider checking the third actual argument of the 'tls_record_set_handshake_finished' function. tlstest.c 342
  3. V614 Uninitialized buffer 'sig' used. Consider checking the third actual argument of the 'tls_record_set_handshake_certificate_verify' function. tlstest.c 320
  4. V614 Uninitialized buffer 'random' used. Consider checking the fourth actual argument of the 'tls_record_set_handshake_server_hello' function. tlstest.c 254
  5. V614 Uninitialized buffer 'random' used. Consider checking the fourth actual argument of the 'tls_record_set_handshake_client_hello' function. tlstest.c 229
  6. V614 Uninitialized buffer 'iv' used. Consider checking the third actual argument of the 'zuc_init' function. zuctest.c 508
  7. V614 Uninitialized buffer 'key' used. Consider checking the second actual argument of the 'zuc_init' function. zuctest.c 508
  8. V614 Uninitialized buffer 'iv' used. Consider checking the third actual argument of the 'zuc_init' function. zuctest.c 480
  9. V614 Uninitialized buffer 'key' used. Consider checking the second actual argument of the 'zuc_init' function. zuctest.c 480
  10. V614 Uninitialized buffer 'blocks' used. Consider checking the second actual argument of the 'ghash_update' function. ghashtest.c 169
  11. V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'hss_sign_update' function. lmstest.c 895
  12. V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'hss_sign_update' function. lmstest.c 845
  13. V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'hss_sign_update' function. lmstest.c 796
  14. V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'lms_sign_update' function. lmstest.c 417
  15. V614 Uninitialized buffer 'serial_buf' used. Consider checking the eighth actual argument of the 'cms_signer_infos_add_signer_info' function. cmstest.c 494
  16. V614 Uninitialized buffer 'sig_buf' used. Consider checking the tenth actual argument of the 'cms_signer_info_to_der' function. cmstest.c 331
  17. V614 Uninitialized buffer 'serial_buf' used. Consider checking the fourth actual argument of the 'cms_signer_info_to_der' function. cmstest.c 327
  18. V614 Uninitialized buffer 'c3' used. Consider checking the fourth actual argument of the 'sm9_ciphertext_to_der' function. sm9test.c 858
  19. V614 Uninitialized buffer 'c2' used. Consider checking the second actual argument of the 'sm9_ciphertext_to_der' function. sm9test.c 858
  20. V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the sixth actual argument of the 'sm4_ccm_encrypt' function. sm4_ccmtest.c 382
  21. V614 Uninitialized buffer 'aad' used. Consider checking the fourth actual argument of the 'sm4_ccm_encrypt' function. sm4_ccmtest.c 382
  22. V614 Uninitialized buffer 'iv' used. Consider checking the second actual argument of the 'sm4_ccm_encrypt' function. sm4_ccmtest.c 382
  23. V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'x509_sign_update' function. x509_keytest.c 434
  24. V614 Uninitialized buffer 'attrs' used. Consider checking the fifth actual argument of the 'x509_req_sign_to_der' function. x509_reqtest.c 161
  25. V614 Uninitialized buffer 'attrs_buf' used. Consider checking the fifth actual argument of the 'x509_request_info_to_der' function. x509_reqtest.c 52
  26. V614 Uninitialized buffer 'iv' used. Consider checking the seventh actual argument of the 'pbes2_algor_to_der' function. pkcs8test.c 234
  27. V614 Uninitialized buffer 'iv' used. Consider checking the seventh actual argument of the 'pbes2_params_to_der' function. pkcs8test.c 185
  28. V614 Uninitialized variable 'saltlen' used. Consider checking the sixth actual argument of the 'format_bytes' function. pkcs8test.c 115
  29. V614 Uninitialized pointer 'psalt' used. Consider checking the fifth actual argument of the 'format_bytes' function. pkcs8test.c 115
  30. V614 Uninitialized buffer 'plaintext' used. Consider checking the second actual argument of the 'sm2_encrypt_update' function. sm2_enctest.c 338
  31. V614 Uninitialized buffer 'msg' used. Consider checking the second actual argument of the 'sm2_sign_update' function. sm2_signtest.c 196
  32. V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the sixth actual argument of the 'sm4_gcm_encrypt' function. sm4_gcmtest.c 547
  33. V614 Uninitialized buffer 'aad' used. Consider checking the fourth actual argument of the 'sm4_gcm_encrypt' function. sm4_gcmtest.c 547
  34. V614 Uninitialized buffer 'iv' used. Consider checking the second actual argument of the 'sm4_gcm_encrypt' function. sm4_gcmtest.c 547
  35. V614 Uninitialized buffer 'blocks' used. Consider checking the second actual argument of the 'sm3_update' function. sm3test.c 189
  36. V614 Uninitialized buffer 'buf1' used. Consider checking the third actual argument of the 'sm4_cbc_padding_encrypt' function. sm4_cbctest.c 169
  37. V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_ctr32_encrypt_blocks' function. sm4test.c 336
  38. V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_ctr_encrypt_blocks' function. sm4test.c 308
  39. V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_cbc_decrypt_blocks' function. sm4test.c 280
  40. V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the third actual argument of the 'sm4_cbc_encrypt_blocks' function. sm4test.c 253
  41. V614 Uninitialized buffer '(uint8_t *) buf' used. Consider checking the second actual argument of the 'sm4_encrypt_blocks' function. sm4test.c 225
  42. V614 Uninitialized buffer 'attrs' used. Consider checking the fifth actual argument of the 'x509_req_sign_to_der' function. reqgen.c 245
  43. V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2451
  44. V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2349
  45. V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2250
  46. V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2167
  47. V614 Uninitialized buffer 'data' used. Consider checking the second actual argument of the 'cms_content_info_to_der' function. cms.c 2099
  48. 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)