97 lines
3.7 KiB
Diff
97 lines
3.7 KiB
Diff
# UNDF: UNDF-2026-000000040
|
|
diff --git a/lib/cookie.h b/lib/cookie.h
|
|
index abc1234..def5678 100644
|
|
--- a/lib/cookie.h
|
|
+++ b/lib/cookie.h
|
|
@@ -52,6 +52,7 @@
|
|
#define COOKIE_HASH_SIZE 63
|
|
|
|
struct CookieInfo {
|
|
+ struct Curl_hash name2node[COOKIE_HASH_SIZE]; /* CWE-407 fix: name→node per bucket */
|
|
struct Curl_llist cookielist[COOKIE_HASH_SIZE]; /* hash-indexed cookie lists */
|
|
time_t next_expiration; /* the next cookie to expire */
|
|
int numcookies; /* number of cookies in the jar */
|
|
|
|
diff --git a/lib/cookie.c b/lib/cookie.c
|
|
index abc1234..def5678 100644
|
|
--- a/lib/cookie.c
|
|
+++ b/lib/cookie.c
|
|
@@ -818,17 +818,43 @@ psl_check_cookie_for_domain(struct Curl_easy *data,
|
|
/* returns TRUE when replaced */
|
|
static bool replace_existing(struct Curl_easy *data,
|
|
struct Cookie *co,
|
|
struct CookieInfo *ci,
|
|
bool secure,
|
|
bool *replacep)
|
|
{
|
|
bool replace_old = FALSE;
|
|
struct Curl_llist_node *replace_n = NULL;
|
|
- struct Curl_llist_node *n;
|
|
size_t myhash = cookiehash(co->domain);
|
|
- for(n = Curl_llist_head(&ci->cookielist[myhash]); n; n = Curl_node_next(n)) {
|
|
- struct Cookie *clist = Curl_node_elem(n);
|
|
- if(!strcmp(clist->name, co->name)) {
|
|
+
|
|
+ /*
|
|
+ * CWE-407 fix: look up the candidate node by name in O(1) via the per-bucket
|
|
+ * name→node hash index, instead of walking the full linked list O(C) per call.
|
|
+ *
|
|
+ * Old: for each of C cookies in bucket, strcmp(clist->name, co->name) → O(C)
|
|
+ * New: Curl_hash_pick(&ci->name2node[myhash], co->name, namelen) → O(1)
|
|
+ *
|
|
+ * The hash maps cookie name to the llist_node so we jump directly to the
|
|
+ * candidate and do the domain/path validation only for that node.
|
|
+ */
|
|
+ size_t namelen = strlen(co->name);
|
|
+ struct Curl_llist_node *candidate =
|
|
+ Curl_hash_pick(&ci->name2node[myhash], co->name, namelen + 1);
|
|
+
|
|
+ if(candidate) {
|
|
+ struct Curl_llist_node *n = candidate;
|
|
+ {
|
|
+ struct Cookie *clist = Curl_node_elem(n);
|
|
/* the names are identical */
|
|
bool matching_domains = FALSE;
|
|
|
|
@@ -920,6 +946,10 @@ replace_existing(struct Curl_easy *data,
|
|
if(replace_n) {
|
|
struct Cookie *repl = Curl_node_elem(replace_n);
|
|
|
|
+ /* Remove from name index before freeing */
|
|
+ size_t rnamelen = strlen(repl->name);
|
|
+ Curl_hash_delete(&ci->name2node[myhash], repl->name, rnamelen + 1);
|
|
+
|
|
/* when replacing, creationtime is kept from old */
|
|
co->creationtime = repl->creationtime;
|
|
|
|
@@ -1035,6 +1065,10 @@ Curl_cookie_add(struct Curl_easy *data,
|
|
/* add this cookie to the list */
|
|
myhash = cookiehash(co->domain);
|
|
Curl_llist_append(&ci->cookielist[myhash], co, &co->node);
|
|
+ /* CWE-407 fix: update name index with the new node */
|
|
+ size_t addnamelen = strlen(co->name);
|
|
+ Curl_hash_add(&ci->name2node[myhash], co->name, addnamelen + 1, &co->node);
|
|
|
|
if(ci->running)
|
|
/* Only show this when NOT reading the cookies from a file */
|
|
@@ -1145,6 +1179,10 @@ Curl_cookie_cleanup(struct CookieInfo *ci)
|
|
if(ci) {
|
|
for(i = 0; i < COOKIE_HASH_SIZE; i++) {
|
|
Curl_llist_destroy(&ci->cookielist[i], NULL);
|
|
+ /* CWE-407 fix: destroy per-bucket name hash */
|
|
+ Curl_hash_destroy(&ci->name2node[i]);
|
|
}
|
|
free(ci);
|
|
}
|
|
@@ -115,6 +115,10 @@ Curl_cookie_init(void)
|
|
struct CookieInfo *ci = calloc(1, sizeof(struct CookieInfo));
|
|
if(ci) {
|
|
int i;
|
|
+ /* CWE-407 fix: initialize per-bucket name→node hash indices */
|
|
+ for(i = 0; i < COOKIE_HASH_SIZE; i++) {
|
|
+ Curl_hash_init(&ci->name2node[i], 7, Curl_hash_str, Curl_str_key_compare,
|
|
+ NULL);
|
|
+ }
|
|
for(i = 0; i < COOKIE_HASH_SIZE; i++)
|
|
Curl_llist_init(&ci->cookielist[i], NULL);
|
|
}
|