Skip to content

Commit

Permalink
crypto: hash - Zap unaligned buffers
Browse files Browse the repository at this point in the history
Some unaligned buffers on the stack weren't zapped properly which
may cause secret data to be leaked.  This patch fixes them by doing
a zero memset.

It is also possible for us to place random kernel stack contents
in the digest buffer if a digest operation fails.  This is fixed
by only copying if the operation succeeded.

Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
herbertx committed Jul 14, 2009
1 parent 500b3e3 commit 8c32c51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 1 addition & 2 deletions crypto/ahash.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
memcpy(alignbuffer, key, keylen);
ret = ahash->setkey(tfm, alignbuffer, keylen);
memset(alignbuffer, 0, keylen);
kfree(buffer);
kzfree(buffer);
return ret;
}

Expand Down
14 changes: 11 additions & 3 deletions crypto/shash.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
memcpy(alignbuffer, key, keylen);
err = shash->setkey(tfm, alignbuffer, keylen);
memset(alignbuffer, 0, keylen);
kfree(buffer);
kzfree(buffer);
return err;
}

Expand Down Expand Up @@ -79,13 +78,16 @@ static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
((unsigned long)data & alignmask);
u8 buf[shash_align_buffer_size(unaligned_len, alignmask)]
__attribute__ ((aligned));
int err;

if (unaligned_len > len)
unaligned_len = len;

memcpy(buf, data, unaligned_len);
err = shash->update(desc, buf, unaligned_len);
memset(buf, 0, unaligned_len);

return shash->update(desc, buf, unaligned_len) ?:
return err ?:
shash->update(desc, data + unaligned_len, len - unaligned_len);
}

Expand Down Expand Up @@ -114,7 +116,13 @@ static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
int err;

err = shash->final(desc, buf);
if (err)
goto out;

memcpy(out, buf, ds);

out:
memset(buf, 0, ds);
return err;
}

Expand Down

0 comments on commit 8c32c51

Please sign in to comment.