Avoid unsequenced accesses to volatile variables
What changed, and why it matters
This commit fixes a subtle C language rule violation in Bitcoin Core's libsecp256k1 cryptographic library. The code was reading a special 'volatile' variable multiple times within the same expression, which modern C standards treat as undefined behavior. The fix copies the value to a regular variable first, then uses it. This is a defensive, code-correctness fix in a security-critical constant-time algorithm, but the commit itself does not demonstrate any actual exploit or real-world vulnerability.
Treat as a low-risk hardening fix. Merge the patch and include it in the next maintenance release. No immediate incident response is warranted because no exploit is demonstrated and the practical security impact is theoretical. Consider adding a CI build with Clang 24 and -Wunsequenced/-Werror to catch similar issues.
Security signals we found
Undefined behavior in cryptographic constant-time code
Compiler warning (-Wunsequenced) in security-critical path
Volatile variable used as constant-time execution barrier
Modular inversion normalization path affected
No demonstrated exploit or test case in commit
Evidence from the diff
The patch addresses unsequenced side effects on volatile scalar objects in secp256k1_modinv32_normalize_30 and secp256k1_modinv64_normalize_62. In C11, two side effects on the same scalar object without an intervening sequence point are undefined behavior. The expressions (rN ^ vcond_negate) - vcond_negate read the volatile variable twice in one full expression, which Clang 24’s -Wunsequenced flags. The fix introduces a non-volatile local copy (cond_negate = vcond_negate;) and uses that in the arithmetic, preserving the constant-time branchless negation pattern while removing the unsequenced access. The volatile write remains to inhibit the compiler from optimizing the value into a branch.
Changed components
src/modinv32_impl.hsrc/modinv64_impl.hsecp256k1_modinv32_normalize_30secp256k1_modinv64_normalize_62Inspect captured patch +20 / −14
### src/modinv32_impl.h
@@ -64,6 +64,7 @@ static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int3
const int32_t M30 = (int32_t)(UINT32_MAX >> 2);
int32_t r0 = r->v[0], r1 = r->v[1], r2 = r->v[2], r3 = r->v[3], r4 = r->v[4],
r5 = r->v[5], r6 = r->v[6], r7 = r->v[7], r8 = r->v[8];
+ int32_t cond_negate;
volatile int32_t vcond_add, vcond_negate;
#ifdef VERIFY
@@ -93,15 +94,17 @@ static void secp256k1_modinv32_normalize_30(secp256k1_modinv32_signed30 *r, int3
r7 += modinfo->modulus.v[7] & vcond_add;
r8 += modinfo->modulus.v[8] & vcond_add;
vcond_negate = sign >> 31;
- r0 = (r0 ^ vcond_negate) - vcond_negate;
- r1 = (r1 ^ vcond_negate) - vcond_negate;
- r2 = (r2 ^ vcond_negate) - vcond_negate;
- r3 = (r3 ^ vcond_negate) - vcond_negate;
- r4 = (r4 ^ vcond_negate) - vcond_negate;
- r5 = (r5 ^ vcond_negate) - vcond_negate;
- r6 = (r6 ^ vcond_negate) - vcond_negate;
- r7 = (r7 ^ vcond_negate) - vcond_negate;
- r8 = (r8 ^ vcond_negate) - vcond_negate;
+ /* Unsequenced reads to volatile variables are UB, so copy to a non-volatile variable first.*/
+ cond_negate = vcond_negate;
+ r0 = (r0 ^ cond_negate) - cond_negate;
+ r1 = (r1 ^ cond_negate) - cond_negate;
+ r2 = (r2 ^ cond_negate) - cond_negate;
+ r3 = (r3 ^ cond_negate) - cond_negate;
+ r4 = (r4 ^ cond_negate) - cond_negate;
+ r5 = (r5 ^ cond_negate) - cond_negate;
+ r6 = (r6 ^ cond_negate) - cond_negate;
+ r7 = (r7 ^ cond_negate) - cond_negate;
+ r8 = (r8 ^ cond_negate) - cond_negate;
/* Propagate the top bits, to bring limbs back to range (-2^30,2^30). */
r1 += r0 >> 30; r0 &= M30;
r2 += r1 >> 30; r1 &= M30;
### src/modinv64_impl.h
@@ -88,6 +88,7 @@ static int secp256k1_modinv64_det_check_pow2(const secp256k1_modinv64_trans2x2 *
static void secp256k1_modinv64_normalize_62(secp256k1_modinv64_signed62 *r, int64_t sign, const secp256k1_modinv64_modinfo *modinfo) {
const int64_t M62 = (int64_t)(UINT64_MAX >> 2);
int64_t r0 = r->v[0], r1 = r->v[1], r2 = r->v[2], r3 = r->v[3], r4 = r->v[4];
+ int64_t cond_negate;
volatile int64_t vcond_add, vcond_negate;
#ifdef VERIFY
@@ -113,11 +114,13 @@ static void secp256k1_modinv64_normalize_62(secp256k1_modinv64_signed62 *r, int6
r3 += modinfo->modulus.v[3] & vcond_add;
r4 += modinfo->modulus.v[4] & vcond_add;
vcond_negate = sign >> 63;
- r0 = (r0 ^ vcond_negate) - vcond_negate;
- r1 = (r1 ^ vcond_negate) - vcond_negate;
- r2 = (r2 ^ vcond_negate) - vcond_negate;
- r3 = (r3 ^ vcond_negate) - vcond_negate;
- r4 = (r4 ^ vcond_negate) - vcond_negate;
+ /* Unsequenced reads to volatile variables are UB, so copy to a non-volatile variable first.*/
+ cond_negate = vcond_negate;
+ r0 = (r0 ^ cond_negate) - cond_negate;
+ r1 = (r1 ^ cond_negate) - cond_negate;
+ r2 = (r2 ^ cond_negate) - cond_negate;
+ r3 = (r3 ^ cond_negate) - cond_negate;
+ r4 = (r4 ^ cond_negate) - cond_negate;
/* Propagate the top bits, to bring limbs back to range (-2^62,2^62). */
r1 += r0 >> 62; r0 &= M62;
r2 += r1 >> 62; r1 &= M62;Why this scored 32/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.