Skip to content

Commit

Permalink
crypt: Zeroize and initialize (re)allocated memory in crypt_ra.
Browse files Browse the repository at this point in the history
Also consolidate the (re)allocation logic.
  • Loading branch information
besser82 committed Jan 16, 2025
1 parent 2cb1a96 commit 043f5df
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,23 @@ SYMVER_crypt_rn;
char *
crypt_ra (const char *phrase, const char *setting, void **data, int *size)
{
if (!*data)
{
*data = malloc (sizeof (struct crypt_data));
if (!*data)
return 0;
*size = sizeof (struct crypt_data);
}
if (*size < 0 || (size_t)*size < sizeof (struct crypt_data))
if (!*data || *size < 0 || (size_t) *size < sizeof (struct crypt_data))
{
/* realloc gives us no way to zeroize the previous data,
if it happens to relocate it to a new memory address.
So let's do it right away. */
if (*data && *size > 0)
explicit_bzero (*data, (size_t) *size);

/* realloc called with *data == NULL is the same as a call
to malloc with the identical size parameter. */
void *rdata = realloc (*data, sizeof (struct crypt_data));
if (!rdata)
return 0;

*data = rdata;
*size = sizeof (struct crypt_data);
explicit_bzero (*data, (size_t) *size);
}

struct crypt_data *p = *data;
Expand Down

0 comments on commit 043f5df

Please sign in to comment.