Linux cryptographic layer development
 help / color / mirror / Atom feed
* [PATCH] crypto: padlock: fix for non-64byte aligned data
@ 2010-11-04 17:50 Phil Sutter
  2010-11-04 18:46 ` Herbert Xu
  0 siblings, 1 reply; 9+ messages in thread
From: Phil Sutter @ 2010-11-04 17:50 UTC (permalink / raw)
  To: linux-crypto; +Cc: Chuck Ebbert, Nico Erfurth

Using cryptodev-linux with it's zero-copy functionality, one is mighty
enough to pass various misaligned/mis-sized buffers unmodified to the
engine's driver, which occured to be an easy way to break padlock
equipped machines:

On one hand, the original code is broken in padlock_xcrypt_cbc(): when
passing the "initial" bytes to xcryptcbc, 'count' is incorrectly used as
length. This may trigger prefetch-related issues, but will definitely
lead to data corruption as xcryptcbc is called again afterwards,
altering (count - initial) * AES_BLOCK_SIZE bytes after the end of
'output' in memory.

Another problem occurs when passing non-64byte aligned buffers, which
leads to memory corruption in userspace (running applications crash
randomly). This problem is too subtile for me to have more than vague
assumptions about it's origin. Anyways, this patch fixes them:

Instead of handling the "odd" bytes (i.e., the remainder when dividing
into prefetch blocks of 64bytes) at the beginning, go for them in the
end, copying the data out if prefetching would run beyond the page
boundary.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Nico Erfurth <ne@erfurth.eu>
---
 drivers/crypto/padlock-aes.c |   29 +++++++++++------------------
 1 files changed, 11 insertions(+), 18 deletions(-)

diff --git a/drivers/crypto/padlock-aes.c b/drivers/crypto/padlock-aes.c
index 2e992bc..b3f3382 100644
--- a/drivers/crypto/padlock-aes.c
+++ b/drivers/crypto/padlock-aes.c
@@ -260,19 +260,14 @@ static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
 {
 	u32 initial = count & (ecb_fetch_blocks - 1);
 
-	if (count < ecb_fetch_blocks) {
-		ecb_crypt(input, output, key, control_word, count);
-		return;
-	}
-
-	if (initial)
+	if (count > initial) {
 		asm volatile (".byte 0xf3,0x0f,0xa7,0xc8"	/* rep xcryptecb */
 			      : "+S"(input), "+D"(output)
-			      : "d"(control_word), "b"(key), "c"(initial));
+			      : "d"(control_word), "b"(key), "c"(count - initial));
+	}
 
-	asm volatile (".byte 0xf3,0x0f,0xa7,0xc8"	/* rep xcryptecb */
-		      : "+S"(input), "+D"(output)
-		      : "d"(control_word), "b"(key), "c"(count - initial));
+	if (initial)
+		ecb_crypt(input, output, key, control_word, initial);
 }
 
 static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
@@ -280,17 +275,15 @@ static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
 {
 	u32 initial = count & (cbc_fetch_blocks - 1);
 
-	if (count < cbc_fetch_blocks)
-		return cbc_crypt(input, output, key, iv, control_word, count);
-
-	if (initial)
+	if (count > initial) {
 		asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"	/* rep xcryptcbc */
 			      : "+S" (input), "+D" (output), "+a" (iv)
-			      : "d" (control_word), "b" (key), "c" (count));
+			      : "d" (control_word), "b" (key), "c" (count - initial));
+	}
+
+	if (initial)
+		return cbc_crypt(input, output, key, iv, control_word, initial);
 
-	asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"	/* rep xcryptcbc */
-		      : "+S" (input), "+D" (output), "+a" (iv)
-		      : "d" (control_word), "b" (key), "c" (count-initial));
 	return iv;
 }
 
-- 
1.7.3.2

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

end of thread, other threads:[~2010-12-08  1:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-04 17:50 [PATCH] crypto: padlock: fix for non-64byte aligned data Phil Sutter
2010-11-04 18:46 ` Herbert Xu
2010-11-05 14:12   ` Phil Sutter
2010-12-02  6:14     ` Herbert Xu
2010-12-07 10:41       ` Phil Sutter
2010-12-07 11:39         ` Herbert Xu
2010-12-07 13:40           ` Phil Sutter
2010-12-08  0:08             ` Harald Welte
2010-12-08  1:52               ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox