Skip to content

Commit 5b5a6fd

Browse files
committed
Sweep every registered algorithm for signature-buffer bounds
The buffer-bounds group in test_security_audit.c named eleven algorithms explicitly, which is the same weakness that let the SNOVA and CROSS overflow through in the first place: test_sig_all.c already allocates calloc(1, max_signature_size()) and signs into it, exactly the shape that catches this, but the affected schemes sat outside its allow list. The underlying problem is that one list was gating two different expectations. Round-tripping is a correctness property that several algorithms here legitimately fail today. Staying inside max_signature_size() is a memory-safety property that every registered algorithm owes its caller whether or not it is otherwise correct, so it must not be gated on brokenness. The sweep now enumerates pqc_sig_algorithm_count() rather than a fixed list: 54 algorithms considered, 47 signed, 93 checks in that group where there were 22. The suite reports 125 checks in total. Remaining skips are for key-generation cost only -- XMSS-16/20 and LMS-H15/20/25 rebuild a 2^h-leaf tree per key, and XMSS-20 does not finish at all -- and the code says so rather than implying the algorithm is at fault. A newly added scheme is now covered for memory safety automatically, while still broken. Enumerating everything immediately found a second defect. Stateful schemes leave .sign NULL and are signed through pqc_sig_sign_stateful(), but pqc_sig_sign() never checked, so calling the stateless entry point on LMS or XMSS dereferenced a NULL function pointer. No test had done that before. It now returns PQC_ERROR_NOT_SUPPORTED. The allow-list observation is Conner's; both fixes above follow from it. Reported-by: Conner (Spartan8806) <https://github.com/spartan8806>
1 parent f9c2892 commit 5b5a6fd

2 files changed

Lines changed: 59 additions & 17 deletions

File tree

‎src/core/sig/sig.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ pqc_status_t pqc_sig_sign(const PQC_SIG *sig,
137137
const uint8_t *secret_key) {
138138
if (!sig || !signature || !signature_len || !message || !secret_key)
139139
return PQC_ERROR_INVALID_ARGUMENT;
140+
/*
141+
* Stateful schemes (LMS, XMSS) leave .sign NULL and are signed through
142+
* pqc_sig_sign_stateful() instead. Without this guard, calling the
143+
* stateless entry point on one of them dereferences a NULL function
144+
* pointer rather than reporting the misuse.
145+
*/
146+
if (!sig->vtable->sign)
147+
return PQC_ERROR_NOT_SUPPORTED;
140148
return sig->vtable->sign(signature, signature_len, message, message_len,
141149
secret_key);
142150
}

‎tests/unit/test_security_audit.c‎

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,42 @@ static void check(int cond, const char *what)
4242
/* 1. SNOVA / CROSS: declared signature size vs. what sign() emits */
4343
/* ------------------------------------------------------------------ */
4444

45+
/*
46+
* Skipped only because key generation is too expensive for a unit test,
47+
* never because an algorithm is broken.
48+
*
49+
* The distinction matters. Staying inside max_signature_size() is a
50+
* memory-safety property that every registered algorithm owes its
51+
* caller, working or not -- so this sweep must not be gated on whether
52+
* an algorithm is otherwise correct. Excluding by name on grounds of
53+
* brokenness is exactly how the SNOVA and CROSS overflow survived: the
54+
* general round-trip test in test_sig_all.c already allocates
55+
* calloc(1, max_signature_size()) and signs into it, which is the shape
56+
* that catches this, but those schemes sat outside its allow list.
57+
*
58+
* These entries rebuild a Merkle tree of 2^h leaves per key: XMSS-20 is
59+
* 2^20 leaves and does not finish in a test run at all.
60+
*/
61+
static int bounds_sweep_too_slow(const char *name)
62+
{
63+
return strcmp(name, "XMSS-SHA2-16-256") == 0 ||
64+
strcmp(name, "XMSS-SHA2-20-256") == 0 ||
65+
strcmp(name, "LMS-SHA256-H15") == 0 ||
66+
strcmp(name, "LMS-SHA256-H20") == 0 ||
67+
strcmp(name, "LMS-SHA256-H25") == 0;
68+
}
69+
4570
static void test_sig_buffer_bounds(void)
4671
{
47-
static const char *algs[] = {
48-
"SNOVA-24-5-4", "SNOVA-25-8-3", "SNOVA-28-17-3",
49-
"CROSS-RSDP-128-fast", "CROSS-RSDP-128-small",
50-
"CROSS-RSDP-192-fast", "CROSS-RSDP-192-small",
51-
"CROSS-RSDP-256-fast", "CROSS-RSDP-256-small",
52-
"MAYO-1", "UOV-Is",
53-
NULL
54-
};
5572
const uint8_t msg[] = "audit regression message";
56-
int a;
73+
int a, total = pqc_sig_algorithm_count(), swept = 0;
5774

5875
printf("\n[1] sign() must not exceed max_signature_size\n");
76+
printf(" sweeping every registered signature algorithm (%d)\n", total);
5977

60-
for (a = 0; algs[a]; a++) {
61-
PQC_SIG *sig = pqc_sig_new(algs[a]);
78+
for (a = 0; a < total; a++) {
79+
const char *alg = pqc_sig_algorithm_name(a);
80+
PQC_SIG *sig;
6281
uint8_t *pk, *sk, *sigbuf;
6382
size_t maxlen, siglen = 0;
6483
pqc_status_t rc;
@@ -68,44 +87,59 @@ static void test_sig_buffer_bounds(void)
6887
size_t guard = 64, i;
6988
int clean = 1;
7089

71-
if (!sig) { printf(" SKIP %s (not registered)\n", algs[a]); continue; }
90+
if (!alg) continue;
91+
if (bounds_sweep_too_slow(alg)) {
92+
printf(" SKIP %s (keygen cost, not correctness)\n", alg);
93+
continue;
94+
}
95+
96+
sig = pqc_sig_new(alg);
97+
if (!sig) { printf(" SKIP %s (not registered)\n", alg); continue; }
7298

7399
maxlen = pqc_sig_max_signature_size(sig);
74100
pk = (uint8_t *)calloc(1, pqc_sig_public_key_size(sig));
75101
sk = (uint8_t *)calloc(1, pqc_sig_secret_key_size(sig));
76102
sigbuf = (uint8_t *)malloc(maxlen + guard);
77-
if (!pk || !sk || !sigbuf) { printf(" SKIP %s (alloc)\n", algs[a]);
103+
if (!pk || !sk || !sigbuf) { printf(" SKIP %s (alloc)\n", alg);
78104
free(pk); free(sk); free(sigbuf); pqc_sig_free(sig); continue; }
79105

80106
memset(sigbuf, 0, maxlen);
81107
memset(sigbuf + maxlen, CANARY, guard);
82108

83109
rc = pqc_sig_keygen(sig, pk, sk);
84110
if (rc != PQC_OK) {
85-
printf(" SKIP %s (keygen rc=%d)\n", algs[a], rc);
111+
printf(" SKIP %s (keygen rc=%d)\n", alg, rc);
86112
free(pk); free(sk); free(sigbuf); pqc_sig_free(sig); continue;
87113
}
88114

89-
rc = pqc_sig_sign(sig, sigbuf, &siglen, msg, sizeof(msg), sk);
115+
if (pqc_sig_is_stateful(sig)) {
116+
rc = pqc_sig_sign_stateful(sig, sigbuf, &siglen,
117+
msg, sizeof(msg), sk);
118+
} else {
119+
rc = pqc_sig_sign(sig, sigbuf, &siglen, msg, sizeof(msg), sk);
120+
}
121+
swept++;
90122

91123
for (i = 0; i < guard; i++) {
92124
if (sigbuf[maxlen + i] != CANARY) { clean = 0; break; }
93125
}
94126

95127
snprintf(label, sizeof(label),
96-
"%s: no write past max_signature_size (%zu)", algs[a], maxlen);
128+
"%s: no write past max_signature_size (%zu)", alg, maxlen);
97129
check(clean, label);
98130

99131
if (rc == PQC_OK) {
100132
snprintf(label, sizeof(label),
101133
"%s: reported siglen %zu <= max %zu",
102-
algs[a], siglen, maxlen);
134+
alg, siglen, maxlen);
103135
check(siglen <= maxlen, label);
104136
}
105137

106138
free(pk); free(sk); free(sigbuf);
107139
pqc_sig_free(sig);
108140
}
141+
142+
printf(" %d of %d algorithms actually signed\n", swept, total);
109143
}
110144

111145
/*

0 commit comments

Comments
 (0)