From: Remi Tricot-Le Breton Date: Thu, 26 Nov 2026 10:11:55 +0000 (+0100) Subject: BUG/MEDIUM: jwt: Add refcount to jwt tree entry X-Git-Url: https://git.kaiwu.me/http/ngx_http_ssi_module.html?a=commitdiff_plain;h=a3aa2e20ac6af695faa85fce0edf878fa67cbd45;p=haproxy.git BUG/MEDIUM: jwt: Add refcount to jwt tree entry During the certificate update process via CLI commands, when calling 'ckch_store_replace', we were removing the previous ckch_store before recreating a jwt tree entry and inserting the new ckch_store. During the short time frame between the 'ckch_store_free' and the 'jwt_tree_create_entry_from_store' call we had no corresponding entry in the jwt tree so a 'jwt_verify' call at this exact moment would fail. Instead of rethinking the whole tree logic, a reference counter was added to tree entry so that we can update the JWT tree entry with the new ckch_store data (pubkey/privkey), increase the reference counter so that the ckch_store_free call on the old ckch_store does not fully remove the JWT tree entry. This patch should be backported to 3.4. --- diff --git a/include/haproxy/jwt-t.h b/include/haproxy/jwt-t.h index 97279e568..850aceb4c 100644 --- a/include/haproxy/jwt-t.h +++ b/include/haproxy/jwt-t.h @@ -72,6 +72,7 @@ enum jwt_elt { }; struct jwt_cert_tree_entry { + int refcount; EVP_PKEY *pubkey; /* Pubkey used in jwt_verify */ EVP_PKEY *privkey; /* Privkey used in jwt_decrypt */ struct ebmb_node node; diff --git a/include/haproxy/jwt.h b/include/haproxy/jwt.h index 074464e85..1aaf18526 100644 --- a/include/haproxy/jwt.h +++ b/include/haproxy/jwt.h @@ -35,8 +35,8 @@ enum jwt_vrfy_status jwt_verify(const struct buffer *token, const struct buffer const struct buffer *key, jwt_verify_mode mode); int jwt_tree_create_entry_from_store(struct ckch_store *store, char **err); +void jwt_tree_update_entry_from_store(struct ckch_store *store, int take_ref); int jwt_tree_delete_entry(char *path); -int jwt_tree_clear_entry(char *path); int jwt_tree_get_pkey(char *path, EVP_PKEY **pkey, int get_pubkey); #endif /* USE_OPENSSL */ diff --git a/reg-tests/jwt/jws_verify.vtc b/reg-tests/jwt/jws_verify.vtc index 8b508c857..317eec774 100644 --- a/reg-tests/jwt/jws_verify.vtc +++ b/reg-tests/jwt/jws_verify.vtc @@ -163,12 +163,14 @@ haproxy h1 -conf { backend dflt_be http-request set-var(txn.bearer) http_auth_bearer http-request set-var(txn.jwt_alg) var(txn.bearer),jwt_header_query('$.alg') + http-request set-var(txn.cert) req.hdr(x-jwt-cert) if { req.hdr(x-jwt-cert) -m found } http-request set-var(txn.jwt_verify) var(txn.bearer),jwt_verify(txn.jwt_alg,"unknown_cert.pem") http-response set-header x-jwt-token %[var(txn.bearer)] http-response set-header x-jwt-alg %[var(txn.jwt_alg)] http-response set-header x-jwt-verify %[var(txn.jwt_verify)] + http-response set-header x-jwt-cert-verify %[var(txn.bearer),jwt_verify_cert(txn.jwt_alg,txn.cert)] if { var(txn.cert) -m found } server s1 ${s1_addr}:${s1_port} @@ -636,3 +638,43 @@ client c30 -connect ${h1_binhmacfe_sock} { expect resp.status == 200 expect resp.http.x-int == "1" } -run + +# The JWT entry of a certificate created through "new ssl cert" must survive +# the "commit ssl cert" call even when the certificate does not use JWT. It is +# kept as a keyless entry so that jwt_verify_cert() reports +# JWT_VRFY_UNAVAIL_CERT (-7) instead of JWT_VRFY_UNKNOWN_CERT (-5). +shell { + echo "new ssl cert ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" - + printf "set ssl cert ${tmpdir}/keyless.pem <<\n$(cat ${testdir}/cert.rsa.pem)\n\n" | socat "${tmpdir}/h1/stats" - + echo "commit ssl cert ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" - +} + +client c31 -connect ${h1_mainfe_sock} { + txreq -url "/errors" \ + -hdr "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.AA" \ + -hdr "x-jwt-cert: ${tmpdir}/keyless.pem" + rxresp + expect resp.status == 200 + expect resp.http.x-jwt-cert-verify == "-7" +} -run + +# Repeated "add/del ssl jwt" on a certificate must not leak references on its +# JWT entry. Once the certificate is deleted, the entry must be removed and +# jwt_verify_cert() must return JWT_VRFY_UNKNOWN_CERT (-5). +shell { + echo "add ssl jwt ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" - + echo "del ssl jwt ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" - + echo "add ssl jwt ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" - + echo "del ssl jwt ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" - + + echo "del ssl cert ${tmpdir}/keyless.pem" | socat "${tmpdir}/h1/stats" - +} + +client c32 -connect ${h1_mainfe_sock} { + txreq -url "/errors" \ + -hdr "Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.AA" \ + -hdr "x-jwt-cert: ${tmpdir}/keyless.pem" + rxresp + expect resp.status == 200 + expect resp.http.x-jwt-cert-verify == "-5" +} -run diff --git a/src/jwt.c b/src/jwt.c index 4d2d7c953..ed70c5bd2 100644 --- a/src/jwt.c +++ b/src/jwt.c @@ -139,16 +139,21 @@ int jwt_tokenize(const struct buffer *jwt, struct jwt_item *items, unsigned int return (ptr != jwt_end); } +#define UPDATE_PKEY(dst, pkey) ({ \ + EVP_PKEY_free(dst); \ + dst = pkey; \ + if (pkey) \ + EVP_PKEY_up_ref(pkey); \ +}) + /* * Allocate a new entry for and insert it into the JWT certificate tree. * Both and are optional and may be NULL, which is used to * mark a certificate as known to the JWT tree while making it unusable by the * jwt_verify/jwt_decrypt converters. The keys are not stolen: a reference is * taken on each of them, so that the caller keeps the ownership of its own - * reference. If an entry already exists for this path (for instance because it - * was kept around by jwt_tree_clear_entry() after a "del ssl jwt" command), - * its keys are replaced in place instead of creating a duplicate entry, which - * also makes it possible to re-enable the "jwt" option on a certificate. + * reference. If an entry already exists for this path, its keys are replaced in + * place instead of creating a duplicate entry. * Returns 0 on success, -1 on allocation failure (and fills if provided). */ static int jwt_tree_create_entry(char *path, int pathlen, EVP_PKEY *pubkey, EVP_PKEY *privkey, char **err) @@ -175,18 +180,11 @@ static int jwt_tree_create_entry(char *path, int pathlen, EVP_PKEY *pubkey, EVP_ entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node); } - EVP_PKEY_free(entry->pubkey); - entry->pubkey = NULL; - if (pubkey) { - entry->pubkey = pubkey; - EVP_PKEY_up_ref(pubkey); - } - EVP_PKEY_free(entry->privkey); - entry->privkey = NULL; - if (privkey) { - entry->privkey = privkey; - EVP_PKEY_up_ref(privkey); - } + ++entry->refcount; + + UPDATE_PKEY(entry->pubkey, pubkey); + UPDATE_PKEY(entry->privkey, privkey); + HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock); return 0; @@ -225,31 +223,49 @@ int jwt_tree_create_entry_from_store(struct ckch_store *store, char **err) } /* - * Release the keys held by the JWT tree entry matching but keep the - * entry itself in the tree. This is used when the "jwt" option is disabled on - * a certificate ("del ssl jwt"): keeping an empty entry makes it possible for - * jwt_verify to return JWT_VRFY_UNAVAIL_CERT instead of - * JWT_VRFY_UNKNOWN_CERT. The keys can later be restored by - * jwt_tree_create_entry(). - * Returns 0 (the entry may legitimately not exist). + * Update the JWT tree entry matching 's path and take a new owner + * reference on it if is set, so that it can survive the release of + * the previous store. The entry is expected to already exist. + * This function is only meant to be called either with the "add/del ssl jwt" + * cli command or during the ckch_store_replace process where we update the + * corresponding JWT tree entry before deleting the previous ckch_store and + * inserting the new one in the ckch_store tree. */ -int jwt_tree_clear_entry(char *path) +void jwt_tree_update_entry_from_store(struct ckch_store *store, int take_ref) { + EVP_PKEY *pubkey = NULL; + EVP_PKEY *privkey = NULL; struct jwt_cert_tree_entry *entry = NULL; struct ebmb_node *eb = NULL; - HA_RWLOCK_WRLOCK(JWT_LOCK, &jwt_tree_lock); - eb = ebst_lookup(&jwt_cert_tree, path); - if (eb != NULL) { - entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node); - EVP_PKEY_free(entry->pubkey); - EVP_PKEY_free(entry->privkey); - entry->pubkey = NULL; - entry->privkey = NULL; + if (!store) + return; + + if (store->conf.jwt) { + /* X509_get_pubkey() returns an owned reference which is released + * below. Note that the entry creation takes its own reference on + * the key. */ + pubkey = X509_get_pubkey(store->data->cert); + privkey = store->data->key; } - HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock); - return 0; + HA_RWLOCK_WRLOCK(JWT_LOCK, &jwt_tree_lock); + eb = ebst_lookup(&jwt_cert_tree, store->path); + if (unlikely(!eb)) + /* Should never happen */ + goto end; + + entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node); + + if (take_ref) + ++entry->refcount; + + UPDATE_PKEY(entry->pubkey, pubkey); + UPDATE_PKEY(entry->privkey, privkey); + +end: + EVP_PKEY_free(pubkey); + HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock); } /* @@ -268,10 +284,15 @@ int jwt_tree_delete_entry(char *path) eb = ebst_lookup(&jwt_cert_tree, path); if (eb != NULL) { entry = ebmb_entry(eb, struct jwt_cert_tree_entry, node); - EVP_PKEY_free(entry->pubkey); - EVP_PKEY_free(entry->privkey); - ebmb_delete(eb); - free(entry); + + --entry->refcount; + + if (entry->refcount <= 0) { + EVP_PKEY_free(entry->pubkey); + EVP_PKEY_free(entry->privkey); + ebmb_delete(eb); + free(entry); + } } HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock); diff --git a/src/ssl_ckch.c b/src/ssl_ckch.c index 1389c7a72..77528283d 100644 --- a/src/ssl_ckch.c +++ b/src/ssl_ckch.c @@ -2556,7 +2556,6 @@ static inline int cli_set_ssl_jwt(char **args, struct appctx *appctx, int value) { struct ckch_store *ckchs; char *err = NULL; - int oldvalue; if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) return cli_err(appctx, "Can't allocate memory!\n"); @@ -2573,18 +2572,9 @@ static inline int cli_set_ssl_jwt(char **args, struct appctx *appctx, int value) if ((ckchs = ckchs_lookup(args[3])) == NULL) goto error; - oldvalue = ckchs->conf.jwt; ckchs->conf.jwt = value; - if (value) { - if (jwt_tree_create_entry_from_store(ckchs, &err)) { - ckchs->conf.jwt = oldvalue; - HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); - return cli_dynerr(appctx, memprintf(&err, "%sAdding certificate '%s' to JWT tree failed", err ? err : "\n", args[3])); - } - } else { - jwt_tree_clear_entry(args[3]); - } + jwt_tree_update_entry_from_store(ckchs, 0); HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); return 0; @@ -3102,11 +3092,14 @@ void ckch_store_replace(struct ckch_store *old_ckchs, struct ckch_store *new_ckc __ckch_inst_free_locked(ckchi); } - ckch_store_free(old_ckchs); + /* + * We update the JWT entry before calling ckch_store_free so that we + * don't end up with a short window during which the entry does not + * exist. + */ + jwt_tree_update_entry_from_store(new_ckchs, 1); - /* The old entry should have been removed in chck_store_free, create a - * new entry altogether. */ - jwt_tree_create_entry_from_store(new_ckchs, NULL); + ckch_store_free(old_ckchs); ebst_insert(&ckchs_tree, &new_ckchs->node); }