src: safely clear certain buffers

Credits-to: Daniel Gustafsson
Ref: #13589 (original attempt)

Follow-up to 066478f634 #21598

safe clear more password buffers

tool_cfgable.c more

drop using dropped wrappers

creds.h copy comment suggested by LLM
This commit is contained in:
Viktor Szakats 2026-05-14 01:36:22 +02:00
parent 70666b9be8
commit f600501916
No known key found for this signature in database
9 changed files with 37 additions and 4 deletions

View file

@ -46,6 +46,7 @@ CURLcode Curl_creds_create(const char *user,
size_t salen = sasl_authzid ? strlen(sasl_authzid) : 0;
size_t sslen = sasl_service ? strlen(sasl_service) : 0;
char *s, *buf;
size_t bufsize;
CURLcode result = CURLE_OK;
Curl_creds_unlink(pcreds);
@ -64,13 +65,14 @@ CURLcode Curl_creds_create(const char *user,
}
/* null-terminator for user already part of struct */
creds = curlx_calloc(1, sizeof(*creds) +
ulen + plen + 1 + olen + 1 + salen + 1 + sslen + 1);
bufsize = ulen + plen + 1 + olen + 1 + salen + 1 + sslen + 1;
creds = curlx_calloc(1, sizeof(*creds) + bufsize);
if(!creds) {
result = CURLE_OUT_OF_MEMORY;
goto out;
}
creds->bufsize = bufsize;
creds->refcount = 1;
creds->source = source;
/* Some compilers try to be too smart about our dynamic struct size */
@ -144,6 +146,7 @@ void Curl_creds_unlink(struct Curl_creds **pcreds)
if(creds->refcount)
creds->refcount--;
if(!creds->refcount) {
curlx_memzero(creds, sizeof(*creds) + creds->bufsize);
curlx_free(creds);
}
}

View file

@ -39,6 +39,7 @@ struct Curl_creds {
const char *sasl_service; /* non-NULL, maybe empty string */
uint32_t refcount;
uint8_t source; /* CREDS_* value */
size_t bufsize; /* extra bytes added to sizeof(struct Curl_creds) */
char buf[1];
};

View file

@ -100,6 +100,7 @@ CURLcode Curl_create_sspi_identity(const char *userp, const char *passwdp,
xcharp_u domain, dup_domain;
xcharp_u passwd, dup_passwd;
size_t domlen = 0;
size_t pwlen;
domain.const_tchar_ptr = TEXT("");
@ -155,17 +156,20 @@ CURLcode Curl_create_sspi_identity(const char *userp, const char *passwdp,
curlx_free(dup_domain.tchar_ptr);
return CURLE_OUT_OF_MEMORY;
}
pwlen = _tcslen(passwd.tchar_ptr);
dup_passwd.tchar_ptr = curlx_tcsdup(passwd.tchar_ptr);
if(!dup_passwd.tchar_ptr) {
curlx_free(dup_user.tchar_ptr);
curlx_free(dup_domain.tchar_ptr);
curlx_memzero(passwd.tchar_ptr, pwlen * sizeof(*passwd.tchar_ptr));
curlx_free(passwd.tchar_ptr);
return CURLE_OUT_OF_MEMORY;
}
identity->Password = dup_passwd.tbyte_ptr;
identity->PasswordLength = curlx_uztoul(_tcslen(dup_passwd.tchar_ptr));
identity->PasswordLength = curlx_uztoul(pwlen);
dup_passwd.tchar_ptr = NULL;
curlx_memzero(passwd.tchar_ptr, pwlen * sizeof(*passwd.tchar_ptr));
curlx_free(passwd.tchar_ptr);
identity->User = dup_user.tbyte_ptr;
@ -199,6 +203,8 @@ void Curl_sspi_free_identity(SEC_WINNT_AUTH_IDENTITY *identity)
{
if(identity) {
curlx_safefree(identity->User);
curlx_memzero(identity->Password,
identity->PasswordLength * sizeof(*identity->Password));
curlx_safefree(identity->Password);
curlx_safefree(identity->Domain);
}

View file

@ -613,6 +613,7 @@ static CURLcode imap_perform_login(struct Curl_easy *data,
passwd ? passwd : "");
curlx_free(user);
curlx_strzero(passwd);
curlx_free(passwd);
if(!result)

View file

@ -347,8 +347,10 @@ static CURLcode mqtt_connect(struct Curl_easy *data)
result = mqtt_send(data, packet, packetlen);
end:
if(packet)
if(packet) {
curlx_memzero(packet, packetlen);
curlx_free(packet);
}
Curl_creds_unlink(&data->state.creds);
return result;
}

View file

@ -154,6 +154,7 @@ static CURLcode setstropt_userpwd(const char *option, char **userp,
curlx_free(*userp);
*userp = user;
curlx_strzero(*passwdp);
curlx_free(*passwdp);
*passwdp = passwd;
@ -1657,6 +1658,7 @@ static CURLcode setopt_cptr_proxy(struct Curl_easy *data, CURLoption option,
result = Curl_urldecode(p, 0, &s->str[STRING_PROXYPASSWORD], NULL,
REJECT_ZERO);
curlx_free(u);
curlx_strzero(p);
curlx_free(p);
break;
}

View file

@ -153,6 +153,15 @@ void Curl_freeset(struct Curl_easy *data)
enum dupblob j;
for(i = (enum dupstring)0; i < STRING_LAST; i++) {
if(i == STRING_PASSWORD ||
i == STRING_KEY_PASSWD ||
#ifndef CURL_DISABLE_PROXY
i == STRING_PROXYPASSWORD ||
i == STRING_KEY_PASSWD_PROXY ||
#endif
i == STRING_BEARER) {
curlx_strzero(data->set.str[i]);
}
curlx_safefree(data->set.str[i]);
}

View file

@ -69,6 +69,7 @@ static void free_urlhandle(struct Curl_URL *u)
{
curlx_free(u->scheme);
curlx_free(u->user);
curlx_strzero(u->password);
curlx_free(u->password);
curlx_free(u->options);
curlx_free(u->host);
@ -322,6 +323,7 @@ UNITTEST CURLUcode parse_hostname_login(struct Curl_URL *u,
}
if(passwdp) {
curlx_strzero(u->password);
curlx_free(u->password);
u->password = passwdp;
}
@ -338,6 +340,7 @@ UNITTEST CURLUcode parse_hostname_login(struct Curl_URL *u,
out:
curlx_free(userp);
curlx_strzero(passwdp);
curlx_free(passwdp);
curlx_free(optionsp);
curlx_safefree(u->user);
@ -1783,6 +1786,7 @@ static CURLUcode urlset_clear(CURLU *u, CURLUPart what)
curlx_safefree(u->user);
break;
case CURLUPART_PASSWORD:
curlx_strzero(u->password);
curlx_safefree(u->password);
break;
case CURLUPART_OPTIONS:
@ -2029,6 +2033,8 @@ nomem:
}
}
if(what == CURLUPART_PASSWORD)
curlx_strzero(*storep);
curlx_free(*storep);
*storep = (char *)CURL_UNCONST(newp);
}

View file

@ -145,7 +145,9 @@ static void free_config_fields(struct OperationConfig *config)
curlx_safefree(config->proxy_key);
curlx_safefree(config->key_type);
curlx_safefree(config->proxy_key_type);
curlx_strzero(config->key_passwd);
curlx_safefree(config->key_passwd);
curlx_strzero(config->proxy_key_passwd);
curlx_safefree(config->proxy_key_passwd);
curlx_safefree(config->pubkey);
curlx_safefree(config->hostpubmd5);
@ -158,6 +160,7 @@ static void free_config_fields(struct OperationConfig *config)
curlx_safefree(config->request_target);
curlx_safefree(config->customrequest);
curlx_safefree(config->krblevel);
curlx_strzero(config->oauth_bearer);
curlx_safefree(config->oauth_bearer);
curlx_safefree(config->sasl_authzid);
curlx_safefree(config->unix_socket_path);