Package: snac2
Version: 2.75-2
Severity: important
Tags: patch upstream fixed-upstream
Dear Maintainer,
In snac2 2.75-2 (trixie), `snac adduser` can create an account whose
public key, as stored in user/<uid>/key.json and published in the actor's
publicKeyPem, has stray bytes after the PEM footer. On my system it ended in:
-----END PUBLIC KEY-----\nk\xecZ
Remote servers (tested: mastodon.social, a Mastodon/Hometown 4.6 instance)
then fail to parse the key and reject every HTTP-signed request from that
account with 401. So the account can't follow anyone, can't fetch from
servers that require authorized fetch, and its deliveries fail. The only
log line is e.g. "cannot get actor to follow <url> 401".
Cause
-----
xs_openssl.h, xs_evp_genkey():
PEM_write_bio_PUBKEY(bp, pkey);
BIO_get_mem_ptr(bp, &pptr);
...
keypair = xs_dict_append(keypair, "secret", sptr->data);
keypair = xs_dict_append(keypair, "public", pptr->data);
BUF_MEM->data isn't NUL-terminated, but it's passed as a C string, so
xs_dict_append() -> xs_size() -> strlen() reads past ->length into
uninitialised memory until it hits a zero byte. Whatever bytes it finds
there end up in the key. Whether garbage appears depends on the heap
contents, so it is intermittent. The private key can be affected the
same way.
valgrind on a small harness that calls xs_evp_genkey(2048) from the 2.75
sources (with OpenSSL 3.x / 4.x):
Conditional jump or move depends on uninitialised value(s)
at strlen
by xs_size (xs.h:312)
by xs_dict_set (xs.h:1145)
by xs_dict_append (xs.h:1214)
by xs_evp_genkey (xs_openssl.h:145)
ERROR SUMMARY: 10 errors from 9 contexts
With the patch below: ERROR SUMMARY: 0 errors.
Fix
---
Fixed upstream in 2.91 by commit e11cb70003e3 ("fix: memory leak and oob
read in "adduser"", which copies exactly ->length bytes using
xs_str_new_sz() and frees the EVP objects). The follow-up commits
c3a8147 ("Fixed compilation error in OpenBSD") and a3cf0db ("Deleted
unused goto label") are needed too, because e11cb70 alone makes `goto end`
jump past the new cleanup-attributed declarations. The attached patch
is those three commits combined; it applies cleanly to 2.75. 2.96-1 in
testing/unstable and trixie-backports already contains the fix, so
only stable (trixie) and bookworm-backports (2.75-2~bpo12+1) are
affected.
Please consider fixing this in a trixie point release.
Workaround for users with an affected account: rebuild "public" from
"secret" (`openssl pkey -pubout`), write it back to key.json, delete
snac's cached copy of its own actor under object/, and restart snac.
-- System Information:
Debian Release: 13 (trixie)
Architecture: amd64
Versions of packages snac2 depends on:
ii libssl3t64 3.5.6-1~deb13u2
diff --git a/xs_openssl.h b/xs_openssl.h
index f215bcc..d44aa4c 100644
--- a/xs_openssl.h
+++ b/xs_openssl.h
@@ -38,7 +38,7 @@ xs_str *xs_base64_enc(const xs_val *data, int sz)
{
BIO *mem, *b64;
BUF_MEM *bptr;
-
+
b64 = BIO_new(BIO_f_base64());
mem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, mem);
@@ -118,16 +118,16 @@ xs_dict *xs_evp_genkey(int bits)
/* generates an RSA keypair using the EVP interface */
{
xs_dict *keypair = NULL;
- EVP_PKEY_CTX *ctx;
+ EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY *pkey = NULL;
if ((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)) == NULL)
- goto end;
+ return NULL;
if (EVP_PKEY_keygen_init(ctx) <= 0 ||
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) <= 0 ||
EVP_PKEY_keygen(ctx, &pkey) <= 0)
- goto end;
+ return NULL;
BIO *bs = BIO_new(BIO_s_mem());
BIO *bp = BIO_new(BIO_s_mem());
@@ -142,13 +142,17 @@ xs_dict *xs_evp_genkey(int bits)
keypair = xs_dict_new();
- keypair = xs_dict_append(keypair, "secret", sptr->data);
- keypair = xs_dict_append(keypair, "public", pptr->data);
+ xs *secret = xs_str_new_sz(sptr->data, sptr->length);
+ xs *public = xs_str_new_sz(pptr->data, pptr->length);
+ keypair = xs_dict_append(keypair, "secret", secret);
+ keypair = xs_dict_append(keypair, "public", public);
BIO_free(bs);
BIO_free(bp);
-end:
+ EVP_PKEY_free(pkey);
+ EVP_PKEY_CTX_free(ctx);
+
return keypair;
}