Logic Flaw: SM4_ENCRYPT/DECRYPT functions return plaintext without warning in Community Edition

Open
#35,349 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
c, sql

Research direction

Start in source/libs/crypt/src/crypt.c, especially Builtin_CBC_EncryptImpl and the non-TD_ENTERPRISE path, then reproduce the behavior with ./taos using the SM4_ENCRYPT query shown. Trace how the routine's return value reaches the SQL function and verify that unsupported Community Edition calls report an explicit error instead of returning plaintext with Query OK.

Written by the indexing model from the issue text.

Description

bug

Bug Description
In the Community Edition build, the SM4 cryptographic routines are stubbed out using a memcpy operation, but the system processes the functions with a silent success status (Query OK). This creates a critical logical flaw and a false sense of security, as users are led to believe their data is encrypted when it is actually processed and stored as raw plaintext.

To Reproduce
Steps to reproduce the behavior:

  1. Open the interactive client terminal by running ./taos.
  2. Select or use any database context (USE test_vuln;).
  3. Execute the SM4 encryption query followed by the AES encryption query to see the plaintext leak vs expected behavior:
taos> SELECT SM4_ENCRYPT('HelloGhost12345', 'my_sm4_key_16_ch') FROM secure_data;
 sm4_encrypt('HelloGhost12345', 'my_sm4_key_16_ch') |
=====================================================
 HelloGhost12345                                    |
Query OK, 1 row(s) in set (0.066803s)

taos> SELECT AES_ENCRYPT('HelloGhost12345', 'my_aes_key_16_ch') FROM secure_data;
 aes_encrypt('HelloGhost12345', 'my_aes_key_16_ch') |
=====================================================
                                                    |
Query OK, 1 row(s) in set (0.011350s)

  1. Notice that the SM4 output returns the raw, unencrypted plaintext string HelloGhost12345 while Query OK is advertised.

Expected Behavior
If the SM4 algorithm is restricted or unimplemented in the Community Edition, the engine should explicitly fail and throw a proper error code (e.g., Function not supported in this edition) instead of failing open, silently copying plaintext, and misleading the operator.

Environment:

  • OS: Debian
  • TDengine Version: 3.4.1.6

Additional Context
The root cause lies in source/libs/crypt/src/crypt.c. When TD_ENTERPRISE is not defined, the core routines fall back to Builtin_CBC_EncryptImpl, which performs a direct data copy:

int32_t Builtin_CBC_EncryptImpl(SCryptOpts *opts) {
    memcpy(opts->result, opts->source, opts->len);
    return opts->len;
}

While commercial gating is understandable, performing a silent memcpy without a runtime warning propagates unencrypted data into architectural call sites (like WAL logs and local configurations) while advertising them as secure.

Dominant language
C
Stars
25.1k
Forks
5k
Avg merge
4d 59m
Merged PRs (30d)
7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from taosdata/TDengine

All issues in taosdata/TDengine

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.