﻿# 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](https://import.viva64.com/docx/blog/1427_GmSSL/image1.png)

One of our readers suggested that we check the [GmSSL](https://github.com/guanzhi/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

```cpp
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;
  }
  ....
}
```

The PVS\-Studio warning: [V595](https://pvs-studio.com/en/docs/warnings/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:

```cpp
const uint8_t *cert_chains = conn->ctx->cert_chains;
....
if (!conn
```

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\.

```cpp
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;
  }
  ....
}
```

The PVS\-Studio warning: [V773](https://pvs-studio.com/en/docs/warnings/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\.

<details>
   <summary>Here are some other code snippets where memory leaks may occur:</summary>

1. V773 The function was exited without releasing the 'aad' pointer\. A memory leak is possible\. sm4\_ccmtest\.c 128
1. V773 The function was exited without releasing the 'encrypted' pointer\. A memory leak is possible\. sm4\_ctrtest\.c 115
1. V773 The function was exited without releasing the 'decrypted' pointer\. A memory leak is possible\. sm4\_ctrtest\.c 123
1. V773 The function was exited without releasing the 'plaintext' pointer\. A memory leak is possible\. sm4\_cbctest\.c 124
1. V773 The function was exited without releasing the 'plaintext' pointer\. A memory leak is possible\. sm4\_ofbtest\.c 113
1. V773 The function was exited without releasing the 'encrypted' pointer\. A memory leak is possible\. sm4\_ofbtest\.c 127
1. V773 The function was exited without releasing the 'decrypted' pointer\. A memory leak is possible\. sm4\_ofbtest\.c 135
1. V773 The function was exited without releasing the 'plaintext' pointer\. A memory leak is possible\. sm4\_cfbtest\.c 122
1. V773 The function was exited without releasing the 'plaintext' pointer\. A memory leak is possible\. sm4\_xtstest\.c 115
1. V773 The function was exited without releasing the 'encrypted' pointer\. A memory leak is possible\. sm4\_xtstest\.c 129
1. V773 The function was exited without releasing the 'decrypted' pointer\. A memory leak is possible\. sm4\_xtstest\.c 137
1. V773 The function was exited without releasing the 'plaintext' pointer\. A memory leak is possible\. sm4\_ccmtest\.c 132
1. V773 The function was exited without releasing the 'encrypted' pointer\. A memory leak is possible\. sm4\_ccmtest\.c 148
1. V773 The function was exited without releasing the 'decrypted' pointer\. A memory leak is possible\. sm4\_ccmtest\.c 155


</details>
## Typos

```cpp
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")) {
  ....
}
```

The PVS\-Studio warning: [V517](https://pvs-studio.com/en/docs/warnings/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\.

```cpp
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")) {
  ....
}
```

The PVS\-Studio warning: [V779](https://pvs-studio.com/en/docs/warnings/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](https://queue.acm.org/doi/10.1145/2620660.2620662) 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](https://cwe.mitre.org/data/definitions/14.html)\.

Let's look at the shortest possible function that contains this error:

```cpp
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));
}
```

The PVS\-Studio warning: [V597](https://pvs-studio.com/en/docs/warnings/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

<details>
   <summary>Here are other functions that incorrectly clear memory: </summary>

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
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 402
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 423
1. 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
1. 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
1. 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
1. 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
1. 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
1. 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
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\. sm3\.c 210
1. 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
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\. sm3\_hmac\.c 164
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\. ghash\.c 178
1. 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
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\. sdfdigest\.c 244
1. 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


</details>
## The error of creating a 64\-bit mask

```cpp
int sm2_z256_get_booth(const sm2_z256_t a,
                       unsigned int window_size, int i)
{
  uint64_t mask = (1 << window_size) - 1;
  ....
}
```

The PVS\-Studio warning: [V629](https://pvs-studio.com/en/docs/warnings/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:

```cpp
uint64_t mask = (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

## A logic error

```cpp
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;
    }
  ....
}
```

The PVS\-Studio warning: [V547](https://pvs-studio.com/en/docs/warnings/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:

```cpp
if ((ret = foo()) != 1) {
  ....
} else if (ret == 0) {  // ret equals 1 here
```

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
1. V547 Expression 'ret \=\= 0' is always false\. tlcp\.c 2042

## An uninitialized variable is used

```cpp
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;
  }
  ....
}
```

The PVS\-Studio warning: [V614](https://pvs-studio.com/en/docs/warnings/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\.

<details>
   <summary>This is an example that shows how to carefully use an uninitialized array:</summary>

```cpp
#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
  }
  ....
}
```

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
1. V614 Uninitialized buffer 'verify\_data' used\. Consider checking the third actual argument of the 'tls\_record\_set\_handshake\_finished' function\. tlstest\.c 342
1. V614 Uninitialized buffer 'sig' used\. Consider checking the third actual argument of the 'tls\_record\_set\_handshake\_certificate\_verify' function\. tlstest\.c 320
1. V614 Uninitialized buffer 'random' used\. Consider checking the fourth actual argument of the 'tls\_record\_set\_handshake\_server\_hello' function\. tlstest\.c 254
1. V614 Uninitialized buffer 'random' used\. Consider checking the fourth actual argument of the 'tls\_record\_set\_handshake\_client\_hello' function\. tlstest\.c 229
1. V614 Uninitialized buffer 'iv' used\. Consider checking the third actual argument of the 'zuc\_init' function\. zuctest\.c 508
1. V614 Uninitialized buffer 'key' used\. Consider checking the second actual argument of the 'zuc\_init' function\. zuctest\.c 508
1. V614 Uninitialized buffer 'iv' used\. Consider checking the third actual argument of the 'zuc\_init' function\. zuctest\.c 480
1. V614 Uninitialized buffer 'key' used\. Consider checking the second actual argument of the 'zuc\_init' function\. zuctest\.c 480
1. V614 Uninitialized buffer 'blocks' used\. Consider checking the second actual argument of the 'ghash\_update' function\. ghashtest\.c 169
1. V614 Uninitialized buffer 'msg' used\. Consider checking the second actual argument of the 'hss\_sign\_update' function\. lmstest\.c 895
1. V614 Uninitialized buffer 'msg' used\. Consider checking the second actual argument of the 'hss\_sign\_update' function\. lmstest\.c 845
1. V614 Uninitialized buffer 'msg' used\. Consider checking the second actual argument of the 'hss\_sign\_update' function\. lmstest\.c 796
1. V614 Uninitialized buffer 'msg' used\. Consider checking the second actual argument of the 'lms\_sign\_update' function\. lmstest\.c 417
1. V614 Uninitialized buffer 'serial\_buf' used\. Consider checking the eighth actual argument of the 'cms\_signer\_infos\_add\_signer\_info' function\. cmstest\.c 494
1. V614 Uninitialized buffer 'sig\_buf' used\. Consider checking the tenth actual argument of the 'cms\_signer\_info\_to\_der' function\. cmstest\.c 331
1. V614 Uninitialized buffer 'serial\_buf' used\. Consider checking the fourth actual argument of the 'cms\_signer\_info\_to\_der' function\. cmstest\.c 327
1. V614 Uninitialized buffer 'c3' used\. Consider checking the fourth actual argument of the 'sm9\_ciphertext\_to\_der' function\. sm9test\.c 858
1. V614 Uninitialized buffer 'c2' used\. Consider checking the second actual argument of the 'sm9\_ciphertext\_to\_der' function\. sm9test\.c 858
1. V614 Uninitialized buffer '\(uint8\_t \*\) buf' used\. Consider checking the sixth actual argument of the 'sm4\_ccm\_encrypt' function\. sm4\_ccmtest\.c 382
1. V614 Uninitialized buffer 'aad' used\. Consider checking the fourth actual argument of the 'sm4\_ccm\_encrypt' function\. sm4\_ccmtest\.c 382
1. V614 Uninitialized buffer 'iv' used\. Consider checking the second actual argument of the 'sm4\_ccm\_encrypt' function\. sm4\_ccmtest\.c 382
1. V614 Uninitialized buffer 'msg' used\. Consider checking the second actual argument of the 'x509\_sign\_update' function\. x509\_keytest\.c 434
1. V614 Uninitialized buffer 'attrs' used\. Consider checking the fifth actual argument of the 'x509\_req\_sign\_to\_der' function\. x509\_reqtest\.c 161
1. V614 Uninitialized buffer 'attrs\_buf' used\. Consider checking the fifth actual argument of the 'x509\_request\_info\_to\_der' function\. x509\_reqtest\.c 52
1. V614 Uninitialized buffer 'iv' used\. Consider checking the seventh actual argument of the 'pbes2\_algor\_to\_der' function\. pkcs8test\.c 234
1. V614 Uninitialized buffer 'iv' used\. Consider checking the seventh actual argument of the 'pbes2\_params\_to\_der' function\. pkcs8test\.c 185
1. V614 Uninitialized variable 'saltlen' used\. Consider checking the sixth actual argument of the 'format\_bytes' function\. pkcs8test\.c 115
1. V614 Uninitialized pointer 'psalt' used\. Consider checking the fifth actual argument of the 'format\_bytes' function\. pkcs8test\.c 115
1. V614 Uninitialized buffer 'plaintext' used\. Consider checking the second actual argument of the 'sm2\_encrypt\_update' function\. sm2\_enctest\.c 338
1. V614 Uninitialized buffer 'msg' used\. Consider checking the second actual argument of the 'sm2\_sign\_update' function\. sm2\_signtest\.c 196
1. V614 Uninitialized buffer '\(uint8\_t \*\) buf' used\. Consider checking the sixth actual argument of the 'sm4\_gcm\_encrypt' function\. sm4\_gcmtest\.c 547
1. V614 Uninitialized buffer 'aad' used\. Consider checking the fourth actual argument of the 'sm4\_gcm\_encrypt' function\. sm4\_gcmtest\.c 547
1. V614 Uninitialized buffer 'iv' used\. Consider checking the second actual argument of the 'sm4\_gcm\_encrypt' function\. sm4\_gcmtest\.c 547
1. V614 Uninitialized buffer 'blocks' used\. Consider checking the second actual argument of the 'sm3\_update' function\. sm3test\.c 189
1. V614 Uninitialized buffer 'buf1' used\. Consider checking the third actual argument of the 'sm4\_cbc\_padding\_encrypt' function\. sm4\_cbctest\.c 169
1. V614 Uninitialized buffer '\(uint8\_t \*\) buf' used\. Consider checking the third actual argument of the 'sm4\_ctr32\_encrypt\_blocks' function\. sm4test\.c 336
1. V614 Uninitialized buffer '\(uint8\_t \*\) buf' used\. Consider checking the third actual argument of the 'sm4\_ctr\_encrypt\_blocks' function\. sm4test\.c 308
1. V614 Uninitialized buffer '\(uint8\_t \*\) buf' used\. Consider checking the third actual argument of the 'sm4\_cbc\_decrypt\_blocks' function\. sm4test\.c 280
1. V614 Uninitialized buffer '\(uint8\_t \*\) buf' used\. Consider checking the third actual argument of the 'sm4\_cbc\_encrypt\_blocks' function\. sm4test\.c 253
1. V614 Uninitialized buffer '\(uint8\_t \*\) buf' used\. Consider checking the second actual argument of the 'sm4\_encrypt\_blocks' function\. sm4test\.c 225
1. V614 Uninitialized buffer 'attrs' used\. Consider checking the fifth actual argument of the 'x509\_req\_sign\_to\_der' function\. reqgen\.c 245
1. V614 Uninitialized buffer 'data' used\. Consider checking the second actual argument of the 'cms\_content\_info\_to\_der' function\. cms\.c 2451
1. V614 Uninitialized buffer 'data' used\. Consider checking the second actual argument of the 'cms\_content\_info\_to\_der' function\. cms\.c 2349
1. V614 Uninitialized buffer 'data' used\. Consider checking the second actual argument of the 'cms\_content\_info\_to\_der' function\. cms\.c 2250
1. V614 Uninitialized buffer 'data' used\. Consider checking the second actual argument of the 'cms\_content\_info\_to\_der' function\. cms\.c 2167
1. V614 Uninitialized buffer 'data' used\. Consider checking the second actual argument of the 'cms\_content\_info\_to\_der' function\. cms\.c 2099
1. V614 Uninitialized buffer 'data' used\. Consider checking the second actual argument of the 'cms\_content\_info\_to\_der' function\. cms\.c 2064


</details>
## Conclusion

We already know how people make mistakes\. Download and use [PVS\-Studio](https://pvs-studio.com/en/pvs-studio/download/) to catch such errors and potential vulnerabilities as early as possible\.

We're [still learning](https://pvs-studio.com/en/blog/posts/cpp/1402/) 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\.