Skip to contentSkip to conversation

2.6.13rc3: crypto horribly broken on all 64bit archs

PATCH Andreas Steinmetz 4 participants 4 messages Jul 17, 2005 – Jul 20, 2005 Crypto API

lore
Share & tools
lore
#0 lore raw

from include/linux/kernel.h:

#define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))

from crypto/cipher.c:

unsigned int alignmask = ...
u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
...
unsigned int alignmask = ...
u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
...
unsigned int align;
addr = ALIGN(addr, align);
addr += ALIGN(tfm->__crt_alg->cra_ctxsize, align);

The compiler first does ~((a)-1)) and then expands the unsigned int to unsigned long for the & operation. So we end up with only the lower 32 bits of the address. Who did smoke what to do this? Patch attached.

Signature · 1 line
Andreas Steinmetz                       SPAMmers use [email protected]
crypto/cipher.c +3 -3
@@ -41,7 +41,7 @@
 			       struct scatter_walk *in,
 			       struct scatter_walk *out, unsigned int bsize)
 {
-	unsigned int alignmask = crypto_tfm_alg_alignmask(desc->tfm);
+	unsigned long alignmask = crypto_tfm_alg_alignmask(desc->tfm);
 	u8 buffer[bsize * 2 + alignmask];
 	u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
 	u8 *dst = src + bsize;
@@ -160,7 +160,7 @@
 			      unsigned int nbytes)
 {
 	struct crypto_tfm *tfm = desc->tfm;
-	unsigned int alignmask = crypto_tfm_alg_alignmask(tfm);
+	unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
 	u8 *iv = desc->info;
 
 	if (unlikely(((unsigned long)iv & alignmask))) {
@@ -424,7 +424,7 @@
 	}
 	
 	if (ops->cit_mode == CRYPTO_TFM_MODE_CBC) {
-		unsigned int align;
+		unsigned long align;
 		unsigned long addr;
 	    	
 	    	switch (crypto_tfm_alg_blocksize(tfm)) {
maintainer
#1 lore raw
Quoted 4 lines · Andreas Steinmetz

The compiler first does ~((a)-1)) and then expands the unsigned int to unsigned long for the & operation. So we end up with only the lower 32 bits of the address. Who did smoke what to do this? Patch attached.

Thanks for the patch Andreas. A similar patch will be in the mainline kernel as soon as everybody is back home from Canada.

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#2 lore raw

From: Herbert Xu <[email protected]>
Date: Mon, 18 Jul 2005 06:45:54 +1000

Quoted 8 lines · Andreas Steinmetz
Quoted 4 lines

The compiler first does ~((a)-1)) and then expands the unsigned int to unsigned long for the & operation. So we end up with only the lower 32 bits of the address. Who did smoke what to do this? Patch attached.

Thanks for the patch Andreas. A similar patch will be in the mainline kernel as soon as everybody is back home from Canada.

Yes, the fix is in my tree and will make it's way upstream as soon as possible.

#3 lore raw
In reply to Andreas Steinmetz
Quoted 8 lines · Andreas Steinmetz

from include/linux/kernel.h:

#define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))

from crypto/cipher.c:

unsigned int alignmask = ...
u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);

The type foolery you suggested is un-obvious, especially at review time. How about the following instead?

-andy

# HG changeset patch
# User [email protected]
# Node ID ff4d8285dcb581b8340b133c22780c4fcc0dabb3
# Parent 2b0b2208676a22741fc3b1681ca9dd555ca40dfd
Add new PTR_ALIGN macro to encapsulate necessary casting, and use it in the places where ALIGN was used on pointers.

crypto/cipher.c +2 -2
diff -r 2b0b2208676a -r ff4d8285dcb5 crypto/cipher.c
@@ -43,7 +43,7 @@
 {
 	unsigned int alignmask = crypto_tfm_alg_alignmask(desc->tfm);
 	u8 buffer[bsize * 2 + alignmask];
-	u8 *src = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
+	u8 *src = PTR_ALIGN(buffer, alignmask + 1);
 	u8 *dst = src + bsize;
 	unsigned int n;
 
@@ -166,7 +166,7 @@
 	if (unlikely(((unsigned long)iv & alignmask))) {
 		unsigned int ivsize = tfm->crt_cipher.cit_ivsize;
 		u8 buffer[ivsize + alignmask];
-		u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
+		u8 *tmp = PTR_ALIGN(buffer, alignmask + 1);
 		int err;
 
 		desc->info = memcpy(tmp, iv, ivsize);
drivers/crypto/padlock-aes.c +1 -1
diff -r 2b0b2208676a -r ff4d8285dcb5 drivers/crypto/padlock-aes.c
@@ -287,7 +287,7 @@
 
 static inline struct aes_ctx *aes_ctx(void *ctx)
 {
-	return (struct aes_ctx *)ALIGN((unsigned long)ctx, PADLOCK_ALIGNMENT);
+	return PTR_ALIGN(ctx, PADLOCK_ALIGNMENT);
 }
 
 static int
include/linux/kernel.h +2 -0
diff -r 2b0b2208676a -r ff4d8285dcb5 include/linux/kernel.h
@@ -29,6 +29,8 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))
+#define PTR_ALIGN(p,a) \
+	((void *)(((unsigned long)(p)+(a)-1)&~(((unsigned long)(a)-1))))
 
 #define	KERN_EMERG	"<0>"	/* system is unusable			*/
 #define	KERN_ALERT	"<1>"	/* action must be taken immediately	*/