All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] crypto: ahash - Fix handling of unaligned buffers
@ 2010-02-23 11:50 Szilveszter Ordog
  2010-03-02 14:37 ` Herbert Xu
  0 siblings, 1 reply; 5+ messages in thread
From: Szilveszter Ordog @ 2010-02-23 11:50 UTC (permalink / raw)
  To: linux-crypto

The correct way to calculate the start of the aligned part of an
unaligned buffer is:

  offset = ALIGN(offset, alignmask + 1);

However, crypto_hash_walk_done() has:

  offset += alignmask - 1;
  offset = ALIGN(offset, alignmask + 1);

which actually skips a whole block unless offset % (alignmask + 1) == 1.

This patch fixes the problem.

Signed-off-by: Szilveszter Ördög <slipszi@gmail.com>
---
 crypto/ahash.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)

diff --git a/crypto/ahash.c b/crypto/ahash.c
index f347637..db42202 100644
--- a/crypto/ahash.c
+++ b/crypto/ahash.c
@@ -65,7 +65,6 @@ int crypto_hash_walk_done(struct crypto_hash_walk
*walk, int err)
 	walk->data -= walk->offset;

 	if (nbytes && walk->offset & alignmask && !err) {
-		walk->offset += alignmask - 1;
 		walk->offset = ALIGN(walk->offset, alignmask + 1);
 		walk->data += walk->offset;

-- 
1.5.5.6
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] crypto: ahash - Fix handling of unaligned buffers
  2010-02-23 11:50 [PATCH] crypto: ahash - Fix handling of unaligned buffers Szilveszter Ordog
@ 2010-03-02 14:37 ` Herbert Xu
  2010-03-02 23:10   ` Szilveszter Ordog
  0 siblings, 1 reply; 5+ messages in thread
From: Herbert Xu @ 2010-03-02 14:37 UTC (permalink / raw)
  To: Szilveszter Ordog; +Cc: linux-crypto

On Tue, Feb 23, 2010 at 11:50:01AM +0000, Szilveszter Ordog wrote:
> The correct way to calculate the start of the aligned part of an
> unaligned buffer is:
> 
>   offset = ALIGN(offset, alignmask + 1);
> 
> However, crypto_hash_walk_done() has:
> 
>   offset += alignmask - 1;
>   offset = ALIGN(offset, alignmask + 1);
> 
> which actually skips a whole block unless offset % (alignmask + 1) == 1.
> 
> This patch fixes the problem.
> 
> Signed-off-by: Szilveszter Ördög <slipszi@gmail.com>

I think you did find a bug, but it's not what you think it is :)

When we get an unaligned buffer, we first process the bit from the
start to the first aligned address.  Once we get to the aligned
address everything happens as usual.

So where this code is, we're trying to move to the next aligned
address, and as ALIGN rounds down, we need to add alignmask.
So the bug is the fact that we're adding alignmask - 1.

Were you able to reproduce this? If so please give this patch a
spin.

commit 483b84aa69382d581f272e882158b91387fa2b7a
Author: Herbert Xu <herbert@gondor.apana.org.au>
Date:   Tue Mar 2 22:36:33 2010 +0800

    crypto: hash - Fix SG walk on unaligned addresses
    
    When we do an SG walk on an unaligned address that is exactly 1
    modulo the alignment, we end up hashing some of the data twice.
    
    Reported-by: Szilveszter Ordog <slipszi@gmail.com>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/crypto/ahash.c b/crypto/ahash.c
index 33a4ff4..b52eb6d 100644
--- a/crypto/ahash.c
+++ b/crypto/ahash.c
@@ -78,7 +78,7 @@ int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err)
 	walk->data -= walk->offset;
 
 	if (nbytes && walk->offset & alignmask && !err) {
-		walk->offset += alignmask - 1;
+		walk->offset += alignmask;
 		walk->offset = ALIGN(walk->offset, alignmask + 1);
 		walk->data += walk->offset;
 
Thanks,
-- 
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] crypto: ahash - Fix handling of unaligned buffers
  2010-03-02 14:37 ` Herbert Xu
@ 2010-03-02 23:10   ` Szilveszter Ordog
  2010-03-03  0:04     ` Herbert Xu
  0 siblings, 1 reply; 5+ messages in thread
From: Szilveszter Ordog @ 2010-03-02 23:10 UTC (permalink / raw)
  To: Herbert Xu; +Cc: linux-crypto

> When we get an unaligned buffer, we first process the bit from the
> start to the first aligned address.  Once we get to the aligned
> address everything happens as usual.
>
> So where this code is, we're trying to move to the next aligned
> address, and as ALIGN rounds down, we need to add alignmask.
> So the bug is the fact that we're adding alignmask - 1.

No, ALIGN rounds *up* (as it should). See:

http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=blob;f=include/linux/kernel.h#l40

Best regards,
Szilveszter
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] crypto: ahash - Fix handling of unaligned buffers
  2010-03-02 23:10   ` Szilveszter Ordog
@ 2010-03-03  0:04     ` Herbert Xu
  2010-03-04  0:48       ` Szilveszter Ordog
  0 siblings, 1 reply; 5+ messages in thread
From: Herbert Xu @ 2010-03-03  0:04 UTC (permalink / raw)
  To: Szilveszter Ordog; +Cc: linux-crypto

Szilveszter Ordog <slipszi@gmail.com> wrote:
>> When we get an unaligned buffer, we first process the bit from the
>> start to the first aligned address.  Once we get to the aligned
>> address everything happens as usual.
>>
>> So where this code is, we're trying to move to the next aligned
>> address, and as ALIGN rounds down, we need to add alignmask.
>> So the bug is the fact that we're adding alignmask - 1.
> 
> No, ALIGN rounds *up* (as it should). See:

Ah, of course it does.  I've applied your patch.  Thanks a lot!
-- 
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
To unsubscribe from this list: send the line "unsubscribe linux-crypto" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] crypto: ahash - Fix handling of unaligned buffers
  2010-03-03  0:04     ` Herbert Xu
@ 2010-03-04  0:48       ` Szilveszter Ordog
  0 siblings, 0 replies; 5+ messages in thread
From: Szilveszter Ordog @ 2010-03-04  0:48 UTC (permalink / raw)
  To: Herbert Xu; +Cc: linux-crypto

> I've applied your patch.

Great! Thank you!

Best regards,
Szilveszter

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-03-04  0:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-23 11:50 [PATCH] crypto: ahash - Fix handling of unaligned buffers Szilveszter Ordog
2010-03-02 14:37 ` Herbert Xu
2010-03-02 23:10   ` Szilveszter Ordog
2010-03-03  0:04     ` Herbert Xu
2010-03-04  0:48       ` Szilveszter Ordog

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.