mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-09-21 05:41:26 +02:00
BUG/MINOR: jwt: Memory leak if same key is used in multiple jwt_verify calls
If the same filename was specified in multiple calls of the jwt_verify converter, we would have parsed the contents of the file every time it was used instead of checking if the entry already existed in the tree. This lead to memory leaks because we would not insert the duplicated entry and we would not free it (as well as the EVP_PKEY it referenced). We now check the return value of ebst_insert and free the current entry if it is a duplicate of an existing entry. The order in which the tree insert and the pkey parsing happen was also switched in order to avoid parsing key files in case of duplicates. Should be backported to 2.5.
This commit is contained in:
parent
2b5a655946
commit
d544d33e10
28
src/jwt.c
28
src/jwt.c
@ -133,6 +133,18 @@ int jwt_tree_load_cert(char *path, int pathlen, char **err)
|
||||
EVP_PKEY *pkey = NULL;
|
||||
BIO *bio = NULL;
|
||||
|
||||
entry = calloc(1, sizeof(*entry) + pathlen + 1);
|
||||
if (!entry) {
|
||||
memprintf(err, "%sunable to allocate memory (jwt_cert_tree_entry).\n", err && *err ? *err : "");
|
||||
return -1;
|
||||
}
|
||||
memcpy(entry->path, path, pathlen + 1);
|
||||
|
||||
if (ebst_insert(&jwt_cert_tree, &entry->node) != &entry->node) {
|
||||
free(entry);
|
||||
return 0; /* Entry already in the tree */
|
||||
}
|
||||
|
||||
bio = BIO_new(BIO_s_file());
|
||||
if (!bio) {
|
||||
memprintf(err, "%sunable to allocate memory (BIO).\n", err && *err ? *err : "");
|
||||
@ -148,20 +160,18 @@ int jwt_tree_load_cert(char *path, int pathlen, char **err)
|
||||
goto end;
|
||||
}
|
||||
|
||||
entry = calloc(1, sizeof(*entry) + pathlen + 1);
|
||||
if (!entry) {
|
||||
memprintf(err, "%sunable to allocate memory (jwt_cert_tree_entry).\n", err && *err ? *err : "");
|
||||
goto end;
|
||||
}
|
||||
|
||||
memcpy(entry->path, path, pathlen + 1);
|
||||
entry->pkey = pkey;
|
||||
|
||||
ebst_insert(&jwt_cert_tree, &entry->node);
|
||||
retval = 0;
|
||||
}
|
||||
|
||||
end:
|
||||
if (retval) {
|
||||
/* Some error happened during pkey parsing, remove the already
|
||||
* inserted node from the tree and free it.
|
||||
*/
|
||||
ebmb_delete(&entry->node);
|
||||
free(entry);
|
||||
}
|
||||
BIO_free(bio);
|
||||
return retval;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user