From: Remi Tricot-Le Breton Date: Wed, 18 Nov 2026 14:09:29 +0000 (+0100) Subject: MINOR: jwt: Add rwlock to jwt_cert_tree X-Git-Url: https://git.kaiwu.me/http/%22../static/gitweb.js?a=commitdiff_plain;h=620a94b8f496f3f476c62e5d1b97ef826f9bfad8;p=haproxy.git MINOR: jwt: Add rwlock to jwt_cert_tree The jwt_cert_tree that stores pure public keys loaded during init and is used by the jwt_verify converter was never locked since we only appended entries during init and never modified them during runtime. In future commits its usage will be extended so it will need a locking mechanism. The tree will only change following CLI updates so a rwlock should do the job since mos of the work will be done under a read lock only. --- diff --git a/include/haproxy/thread-t.h b/include/haproxy/thread-t.h index 8c363b20c..5665824dd 100644 --- a/include/haproxy/thread-t.h +++ b/include/haproxy/thread-t.h @@ -217,6 +217,7 @@ enum lock_label { CACHE_LOCK, GUID_LOCK, PROXIES_DEL_LOCK, + JWT_LOCK, OTHER_LOCK, /* WT: make sure never to use these ones outside of development, * we need them for lock profiling! diff --git a/src/jwt.c b/src/jwt.c index 3848ba52f..dcece6f9f 100644 --- a/src/jwt.c +++ b/src/jwt.c @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -29,6 +30,8 @@ /* Tree into which the public certificates used to validate JWTs will be stored. */ static struct eb_root jwt_cert_tree = EB_ROOT_UNIQUE; +/* Lock protecting the jwt_cert_tree. */ +__decl_thread(HA_RWLOCK_T jwt_tree_lock); /* Set whether "none" algorithm should be allowed or not */ static int jwt_verify_allow_none_alg = 0; @@ -147,13 +150,15 @@ int jwt_tree_load_cert(char *path, int pathlen, int tryload_cert, const char *fi EVP_PKEY *pubkey = NULL; BIO *bio = NULL; struct stat buf; - struct ebmb_node *eb = NULL; struct ckch_store *store = NULL; - eb = ebst_lookup(&jwt_cert_tree, path); - - if (eb) - return 0; /* Entry already in the tree, nothing to do. */ + HA_RWLOCK_RDLOCK(JWT_LOCK, &jwt_tree_lock); + if (ebst_lookup(&jwt_cert_tree, path) != NULL) { + /* Entry already in the tree, nothing to do. */ + HA_RWLOCK_RDUNLOCK(JWT_LOCK, &jwt_tree_lock); + return 0; + } + HA_RWLOCK_RDUNLOCK(JWT_LOCK, &jwt_tree_lock); if (stat(path, &buf) == 0) { bio = BIO_new(BIO_s_file()); @@ -179,9 +184,18 @@ int jwt_tree_load_cert(char *path, int pathlen, int tryload_cert, const char *fi entry->pubkey = pubkey; - /* The entry insertion can't fail, we already - * checked if the entry existed. */ - ebst_insert(&jwt_cert_tree, &entry->node); + HA_RWLOCK_WRLOCK(JWT_LOCK, &jwt_tree_lock); + if (ebst_insert(&jwt_cert_tree, &entry->node) != &entry->node) { + /* Another thread may have inserted the + * same entry in the meantime. It should + * not happen for now since this + * function is only called during conf + * parsing but better safe than sorry. */ + EVP_PKEY_free(pubkey); + pubkey = NULL; + ha_free(&entry); + } + HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock); retval = 0; goto end; @@ -375,7 +389,6 @@ jwt_jwsverify_rsa_ecdsa(const struct jwt_ctx *ctx, struct buffer *decoded_signat EVP_MD_CTX *evp_md_ctx; EVP_PKEY_CTX *pkey_ctx = NULL; enum jwt_vrfy_status retval = JWT_VRFY_KO; - struct ebmb_node *eb = NULL; struct jwt_cert_tree_entry *entry = NULL; int is_ecdsa = 0; int padding = RSA_PKCS1_PADDING; @@ -441,7 +454,12 @@ jwt_jwsverify_rsa_ecdsa(const struct jwt_ctx *ctx, struct buffer *decoded_signat HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); } } else { - /* Look for a public key in the JWT tree */ + struct ebmb_node *eb = NULL; + /* Look for a public key in the JWT tree. We can keep a read + * lock because the tree itself is not changed and the only + * operation on the entry is EVP_PKEY_up_ref called on the pkey + * field which is atomic. */ + HA_RWLOCK_RDLOCK(JWT_LOCK, &jwt_tree_lock); eb = ebst_lookup(&jwt_cert_tree, ctx->key); if (eb) { @@ -451,6 +469,7 @@ jwt_jwsverify_rsa_ecdsa(const struct jwt_ctx *ctx, struct buffer *decoded_signat if (pubkey) EVP_PKEY_up_ref(pubkey); } + HA_RWLOCK_RDUNLOCK(JWT_LOCK, &jwt_tree_lock); } if (!pubkey) { @@ -611,6 +630,8 @@ static void jwt_deinit(void) struct ebmb_node *node = NULL; struct jwt_cert_tree_entry *entry = NULL; + HA_RWLOCK_WRLOCK(JWT_LOCK, &jwt_tree_lock); + node = ebmb_first(&jwt_cert_tree); while (node) { entry = ebmb_entry(node, struct jwt_cert_tree_entry, node); @@ -619,6 +640,8 @@ static void jwt_deinit(void) ha_free(&entry); node = ebmb_first(&jwt_cert_tree); } + + HA_RWLOCK_WRUNLOCK(JWT_LOCK, &jwt_tree_lock); } REGISTER_POST_DEINIT(jwt_deinit); diff --git a/src/thread.c b/src/thread.c index 4ca60a591..4bfd452e0 100644 --- a/src/thread.c +++ b/src/thread.c @@ -507,6 +507,7 @@ const char *lock_label(enum lock_label label) case CACHE_LOCK: return "CACHE"; case GUID_LOCK: return "GUID"; case PROXIES_DEL_LOCK: return "PROXIES_DEL"; + case JWT_LOCK: return "JWT"; case OTHER_LOCK: return "OTHER"; case DEBUG1_LOCK: return "DEBUG1"; case DEBUG2_LOCK: return "DEBUG2";