Skip to content

Commit

Permalink
dh key: get rid of stack allocated array for zeroes
Browse files Browse the repository at this point in the history
We're interested in getting rid of all of the stack allocated arrays in
the kernel: https://lkml.org/lkml/2018/3/7/621

This case is interesting, since we really just need an array of bytes that
are zero. The loop already ensures that if the array isn't exactly the
right size that enough zero bytes will be copied in. So, instead of
choosing this value to be the size of the hash, let's just choose it to be
32, since that is a common size, is not too big, and will not result in too
many extra iterations of the loop.

v2: split out from other patch, just hardcode array size instead of
    dynamically allocating something the right size
v3: fix typo of 256 -> 32

Signed-off-by: Tycho Andersen <tycho@tycho.ws>
Reviewed-by: Kees Cook <keescook@chromium.org>
CC: David Howells <dhowells@redhat.com>
CC: James Morris <jmorris@namei.org>
CC: "Serge E. Hallyn" <serge@hallyn.com>
CC: Eric Biggers <ebiggers3@gmail.com>
Signed-off-by: James Morris <james.morris@microsoft.com>
  • Loading branch information
Tycho Andersen authored and James Morris committed May 11, 2018
1 parent 383203e commit 890e2ab
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions security/keys/dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
goto err;

if (zlen && h) {
u8 tmpbuffer[h];
size_t chunk = min_t(size_t, zlen, h);
u8 tmpbuffer[32];
size_t chunk = min_t(size_t, zlen, sizeof(tmpbuffer));
memset(tmpbuffer, 0, chunk);

do {
Expand All @@ -173,7 +173,7 @@ static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
goto err;

zlen -= chunk;
chunk = min_t(size_t, zlen, h);
chunk = min_t(size_t, zlen, sizeof(tmpbuffer));
} while (zlen);
}

Expand Down

0 comments on commit 890e2ab

Please sign in to comment.