2010-08-02 12:45:38

by Szilveszter Ördög

[permalink] [raw]
Subject: [PATCH] crypto: hash - Fix handling of small unaligned buffers

If a scatterwalk chain contains an entry with an unaligned offset then
hash_walk_next() will cut off the next step at the next alignment point.

However, if the entry ends before the next alignment point then we will want to
process more data than it is available.

Fix this by checking whether the next aligment point is before the end of the
current entry.
---
crypto/ahash.c | 7 +++++--
1 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/crypto/ahash.c b/crypto/ahash.c
index b8c59b8..f669822 100644
--- a/crypto/ahash.c
+++ b/crypto/ahash.c
@@ -47,8 +47,11 @@ static int hash_walk_next(struct crypto_hash_walk *walk)
walk->data = crypto_kmap(walk->pg, 0);
walk->data += offset;

- if (offset & alignmask)
- nbytes = alignmask + 1 - (offset & alignmask);
+ if (offset & alignmask) {
+ unsigned int unaligned = alignmask + 1 - (offset & alignmask);
+ if (nbytes > unaligned)
+ nbytes = unaligned;
+ }

walk->entrylen -= nbytes;
return nbytes;
--
1.5.5.6


2010-08-04 13:48:54

by Herbert Xu

[permalink] [raw]
Subject: Re: [PATCH] crypto: hash - Fix handling of small unaligned buffers

On Mon, Aug 02, 2010 at 02:45:37PM +0200, Szilveszter ?rd?g wrote:
> If a scatterwalk chain contains an entry with an unaligned offset then
> hash_walk_next() will cut off the next step at the next alignment point.
>
> However, if the entry ends before the next alignment point then we will want to
> process more data than it is available.
>
> Fix this by checking whether the next aligment point is before the end of the
> current entry.
> ---
> crypto/ahash.c | 7 +++++--
> 1 files changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/crypto/ahash.c b/crypto/ahash.c
> index b8c59b8..f669822 100644
> --- a/crypto/ahash.c
> +++ b/crypto/ahash.c
> @@ -47,8 +47,11 @@ static int hash_walk_next(struct crypto_hash_walk *walk)
> walk->data = crypto_kmap(walk->pg, 0);
> walk->data += offset;
>
> - if (offset & alignmask)
> - nbytes = alignmask + 1 - (offset & alignmask);
> + if (offset & alignmask) {
> + unsigned int unaligned = alignmask + 1 - (offset & alignmask);
> + if (nbytes > unaligned)
> + nbytes = unaligned;
> + }
>
> walk->entrylen -= nbytes;
> return nbytes;

The patch looks OK to me. Dave, what do you think?

But you forgot to attach a Signed-off-by line. Please resend
with one attached.

Thanks!
--
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

2010-08-04 20:59:37

by David Miller

[permalink] [raw]
Subject: Re: [PATCH] crypto: hash - Fix handling of small unaligned buffers

From: Herbert Xu <[email protected]>
Date: Wed, 4 Aug 2010 21:48:51 +0800

> On Mon, Aug 02, 2010 at 02:45:37PM +0200, Szilveszter ?rd?g wrote:
>> If a scatterwalk chain contains an entry with an unaligned offset then
>> hash_walk_next() will cut off the next step at the next alignment point.
>>
>> However, if the entry ends before the next alignment point then we will want to
>> process more data than it is available.
>>
>> Fix this by checking whether the next aligment point is before the end of the
>> current entry.
>> ---
>> crypto/ahash.c | 7 +++++--
>> 1 files changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/crypto/ahash.c b/crypto/ahash.c
>> index b8c59b8..f669822 100644
>> --- a/crypto/ahash.c
>> +++ b/crypto/ahash.c
>> @@ -47,8 +47,11 @@ static int hash_walk_next(struct crypto_hash_walk *walk)
>> walk->data = crypto_kmap(walk->pg, 0);
>> walk->data += offset;
>>
>> - if (offset & alignmask)
>> - nbytes = alignmask + 1 - (offset & alignmask);
>> + if (offset & alignmask) {
>> + unsigned int unaligned = alignmask + 1 - (offset & alignmask);
>> + if (nbytes > unaligned)
>> + nbytes = unaligned;
>> + }
>>
>> walk->entrylen -= nbytes;
>> return nbytes;
>
> The patch looks OK to me. Dave, what do you think?

Looks good, thanks Szilveszter:

Acked-by: David S. Miller <[email protected]>