Мы используем куки, чтобы пользоваться сайтом было удобно.
Хорошо
to the top

Вебинар: Классические VS тропические решения в статических анализаторах - 25.11

>
>
>
Классический C, классические ошибки...

Классический C, классические ошибки проекта GmSSL

17 Ноя 2026

Пока ИИ осваивает новые способы ошибиться, люди по-прежнему верны старым добрым опечаткам и ошибкам при работе с памятью. Давайте взглянем на эту классику.

Один из читателей предложил нам проверить проект GmSSL. Так как мы редко проверяем проекты, созданные в Китае, решили уделить ему время.

GmSSL — это разработанная Пекинским университетом открытая криптографическая библиотека на C, которая полностью реализует китайские коммерческие стандарты шифрования (SM2/SM3/SM4/SM9) и безопасные протоколы связи (TLCP, TLS 1.2/1.3). Отличается сверхлёгкостью и может использоваться даже в маломощных встраиваемых системах без динамической памяти, поддерживает аппаратные модули SDF/SKF.

На этот раз особенно критичных или необычных ошибок не обнаружилось. Зато нашлись самые типичные и привычные. Впрочем, из-за "стандартности" баги не перестают быть багами. Давайте посмотрим на них.

Потенциальное разыменование нулевого указателя

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

Предупреждение PVS-Studio: V595 The 'conn' pointer was utilized before it was verified against nullptr. Check lines: 807, 811. tls12.c 807

Из наличия проверки указателя conn можно сделать вывод, что он может оказаться равным NULL. Однако до проверки этот указатель разыменовывается:

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

Проверка входных аргументов явно должна быть расположена выше — в самом начале функции.

Потенциальные утечки памяти

Ручное управление памятью в C часто приводит к сценарию, когда во внутренних обработчиках ошибок забывают освободить память. GmSSL здесь не исключение.

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

Предупреждение PVS-Studio: V773 The function was exited without releasing the 'plaintext' pointer. A memory leak is possible. sm4_ctrtest.c 101

Если второй вызов malloc закончится неудачей, то произойдёт выход из функции без предварительной очистки памяти, на которую указывает plaintext. Т.е. не будет освобождена память, выделенная ранее под другой буфер.

  • 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

Опечатки

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

Предупреждение PVS-Studio: 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

В двух ветках, где проверяется наличие ключа -lib, выполняются разные действия. Это явная ошибка, так как вторая ветка недостижима. Во втором случае должен проверяться ключ -dev.

Рассмотри другую опечатку — дублирование оператора goto.

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

Предупреждение PVS-Studio: V779 Unreachable code detected. It is possible that an error is present. sdfencrypt.c 86

В данном случае эта опечатка не приводит к каким-либо последствиям. Но это не значит, что подобные опечатки всегда безобидны: вспоминается CVE-2014-1266 — Finding More Than One Worm in the Apple: If you see something, say something.

Ещё одно такое срабатывание: V779 Unreachable code detected. It is possible that an error is present. sdfdecrypt.c 86

Ошибка затирания приватных данных

В криптографических библиотеках принято "убирать" за собой, затирая более неиспользуемые приватные данные. Часто для этого применяют memset, и это ошибка. Оптимизирующий компилятор видит, что после вызова функции memset память больше не используется, и убирает этот вызов. Подробнее: CWE-14: Compiler Removal of Code to Clear Buffers.

Рассмотрим самую короткую функцию с такой ошибкой:

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

Предупреждение PVS-Studio: 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

  • 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

Ошибка формирования 64-битной маски

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

Предупреждение PVS-Studio: 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

В функции формируется 64-битная маска. Однако не получится выставить старшие биты, так как числовой литерал 1 имеет тип int. Будет возникать переполнение. Правильный вариант кода:

uint64_t mask = (1ui64 << window_size) - 1;

Схожее срабатывание: 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

Ошибка в логике

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

Предупреждение PVS-Studio: V547 Expression 'ret == 0' is always false. tls_psk.c 1572

Оператор continue никогда не выполнится, потому что в этом месте переменная ret всегда равна 1. Сокращённый код для наглядности:

if ((ret = foo()) != 1) {
  ....
} else if (ret == 0) {  // Здесь ret равен 1

Здесь какая-то логическая ошибка или опечатка.

Есть ещё две схожих ошибки:

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

Использование неинициализированной переменной

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

Предупреждение PVS-Studio: V614 Uninitialized variable 'sig_alg' used. tls_cert.c 623

Используется неинициализированная переменная sig_alg. Видимо, в этом коде пропущена какая-то логика, меняющая значение этой переменной.

P.S. В юнит-тестах библиотеки GmSSL часто используются буферы с неинициализированными данными. Т.е. выполняются различные операции над мусорными данными. Не берусь судить о корректности этого подхода, так как он явно выбран осознано. В любом случае, такой код юнит-тестов "пахнет", поэтому я решил упомянуть этом в статье.

#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 не инициализирован
  }
  ....
}

Предупреждение PVS-Studio: V614 Uninitialized buffer 'buf' used. Consider checking the second actual argument of the 'sm4_encrypt' function. sm4test.c 199

Другие аналогичные срабатывания PVS-Studio:

  • 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

Заключение

Как ошибаются люди — хорошо известно. Скачивайте и используйте PVS-Studio, чтобы найти такие ошибки и потенциальные уязвимости как можно раньше.

Как ошибается ИИ — мы изучаем и уже вырисовывается, куда надо двигаться в развитии анализатора. Например, это выявление дубликатов/схожего кода. В любом случае, анализатор PVS-Studio уже может помочь вам присмотреть за ИИ.

Подписаться на рассылку
Хотите раз в месяц получать от нас подборку вышедших в этот период самых интересных статей и новостей? Подписывайтесь!
Популярные статьи по теме

Комментарии (0)

Следующие комментарии next comments
close comment form