Possible alignment issue in kernel's sha256 implementation
Hi,
Yesterday a bug was discovered in Debian's application, which uses the sha256 implementation virtually identical to the one available in kernel [0]. An unaligned access in sha256 code was causing a bus error on sparc. The offending code also present in the 2.6.17-rc3 crypto/sha256.c in the following form:
60 static inline void LOAD_OP(int I, u32 *W, const u8 *input) 61 {
62 W[I] = __be32_to_cpu( ((__be32*)(input))[I] );
63 }
This function is only called from sha256_transform(), which is called only from sha256_update(), which, in turn, is exposed to the kernel's cryptographic API layer. I believe that if a u8 array not aligned on 4-byte boundary is passed as a second argument ('data') to that function, it will cause an unaligned memory access in LOAD_OP.
I cannot really determine how dangerous this is. It might be that the cryptographic layer takes care of alignment, or the kernel will just handle the unaligned access gracefully. In any case, I think it's worth checking out by someone with more knowledge on the matter than myself.
[0] http://bugs.debian.org/366717
Best regards,
Jurij Smakov [email protected]
Key: http://www.wooyd.org/pgpkey/ KeyID: C99E03CC
David S. Miller
maintainerFrom: Jurij Smakov <[email protected]>
Date: Fri, 12 May 2006 19:59:10 -0700 (PDT)
Quoted 4 lines
60 static inline void
LOAD_OP(int I, u32 *W, const u8 *input)61 {
62 W[I] =__be32_to_cpu( ((__be32*)(input))[I] );
63 }
...
Quoted 6 lines
I cannot really determine how dangerous this is. It might be that the cryptographic layer takes care of alignment, or the kernel will just handle the unaligned access gracefully. In any case, I think it's worth checking out by someone with more knowledge on the matter than myself.
We allow kernel unaligned accesses to be fixed up transparently by a relatively expensive trap handler, but we don't allow it for user accesses.
Maybe the unaligned case cannot happen in the kernel for some reason.
Herbert, is there anything which ensures 32-bit alignment for the data accessed by LOAD_OP()?
Herbert Xu
maintainerQuoted 6 lines · David S. Miller
Maybe the unaligned case cannot happen in the kernel for some reason.
Herbert, is there anything which ensures 32-bit alignment for the data accessed by
LOAD_OP()?
It can happen but it's pretty unusual with today's users.
Atsushi Nemoto has a patch which fixes the issue for all digest algorithms by copying unaligned data where necessary. This is currently being tested in mm and should be in 2.6.18.
I've attached the patch if anyone wants to test it right now.
Cheers,
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.txta2be755965f42ddc16a22d5dea68e8765435abf7
crypto/digest.c +28 -16
@@ -27,6 +27,7 @@ struct scatterlist *sg, unsigned int nsg) {unsigned int i; +unsigned int alignmask =crypto_tfm_alg_alignmask (tfm);for (i =0 ; i < nsg; i++) {
@@ -38,12 +39,24 @@unsigned int bytes_from_page =min (l, ((unsigned int ) (PAGE_SIZE )) - offset); -char *p =crypto_kmap (pg,0 ) + offset; - +char *src =crypto_kmap (pg,0 ); +char *p = src + offset; + +if (unlikely (offset & alignmask)) { +unsigned int bytes = + alignmask +1 - (offset & alignmask); + bytes =min (bytes, bytes_from_page); + tfm->__crt_alg->cra_digest.dia_update + (crypto_tfm_ctx (tfm), p, + bytes); + p += bytes; + bytes_from_page -= bytes; + l -= bytes; + } tfm->__crt_alg->cra_digest.dia_update (crypto_tfm_ctx (tfm), p, bytes_from_page); -crypto_kunmap (p,0 ); +crypto_kunmap (src,0 );crypto_yield (tfm); offset =0 ; pg++;
@@ -54,7 +67,15 @@static void final (struct crypto_tfm *tfm,u8 *out) { - tfm->__crt_alg->cra_digest.dia_final (crypto_tfm_ctx (tfm), out); +unsigned long alignmask =crypto_tfm_alg_alignmask (tfm); +if (unlikely ((unsigned long )out & alignmask)) { +unsigned int size =crypto_tfm_alg_digestsize (tfm); +u8 buffer[size + alignmask]; +u8 *dst = (u8 *)ALIGN ((unsigned long )buffer, alignmask +1 ); + tfm->__crt_alg->cra_digest.dia_final (crypto_tfm_ctx (tfm), dst); +memcpy (out, dst, size); + }else + tfm->__crt_alg->cra_digest.dia_final (crypto_tfm_ctx (tfm), out); }static int setkey (struct crypto_tfm *tfm,const u8 *key,unsigned int keylen)
@@ -69,18 +90,9 @@static void digest (struct crypto_tfm *tfm,struct scatterlist *sg,unsigned int nsg,u8 *out) { -unsigned int i; - - tfm->crt_digest.dit_init (tfm); - -for (i =0 ; i < nsg; i++) { -char *p =crypto_kmap (sg[i].page,0 ) + sg[i].offset; - tfm->__crt_alg->cra_digest.dia_update (crypto_tfm_ctx (tfm), - p, sg[i].length); -crypto_kunmap (p,0 ); -crypto_yield (tfm); - } -crypto_digest_final (tfm, out); +init (tfm); +update (tfm, sg, nsg); +final (tfm, out); }int crypto_init_digest_flags (struct crypto_tfm *tfm,u32 flags)
crypto/michael_mic.c +1 -0
@@ -145,6 +145,7 @@ .cra_blocksize = 8, .cra_ctxsize = sizeof(struct michael_mic_ctx), .cra_module = THIS_MODULE, + .cra_alignmask = 3, .cra_list = LIST_HEAD_INIT(michael_mic_alg.cra_list), .cra_u = { .digest = { .dia_digestsize = 8,
crypto/sha1.c +1 -0
@@ -112,6 +112,7 @@ .cra_blocksize = SHA1_HMAC_BLOCK_SIZE, .cra_ctxsize = sizeof(struct sha1_ctx), .cra_module = THIS_MODULE, + .cra_alignmask = 3, .cra_list = LIST_HEAD_INIT(alg.cra_list), .cra_u = { .digest = { .dia_digestsize = SHA1_DIGEST_SIZE,
crypto/sha256.c +1 -0
@@ -313,6 +313,7 @@ .cra_blocksize = SHA256_HMAC_BLOCK_SIZE, .cra_ctxsize = sizeof(struct sha256_ctx), .cra_module = THIS_MODULE, + .cra_alignmask = 3, .cra_list = LIST_HEAD_INIT(alg.cra_list), .cra_u = { .digest = { .dia_digestsize = SHA256_DIGEST_SIZE,
crypto/sha512.c +2 -0
@@ -281,6 +281,7 @@ .cra_blocksize = SHA512_HMAC_BLOCK_SIZE, .cra_ctxsize = sizeof(struct sha512_ctx), .cra_module = THIS_MODULE, + .cra_alignmask = 3, .cra_list = LIST_HEAD_INIT(sha512.cra_list), .cra_u = { .digest = { .dia_digestsize = SHA512_DIGEST_SIZE,
@@ -295,6 +296,7 @@ .cra_flags = CRYPTO_ALG_TYPE_DIGEST, .cra_blocksize = SHA384_HMAC_BLOCK_SIZE, .cra_ctxsize = sizeof(struct sha512_ctx), + .cra_alignmask = 3, .cra_module = THIS_MODULE, .cra_list = LIST_HEAD_INIT(sha384.cra_list), .cra_u = { .digest = {
crypto/tgr192.c +3 -0
@@ -627,6 +627,7 @@ .cra_blocksize = TGR192_BLOCK_SIZE, .cra_ctxsize = sizeof(struct tgr192_ctx), .cra_module = THIS_MODULE, + .cra_alignmask = 7, .cra_list = LIST_HEAD_INIT(tgr192.cra_list), .cra_u = {.digest = { .dia_digestsize = TGR192_DIGEST_SIZE,
@@ -641,6 +642,7 @@ .cra_blocksize = TGR192_BLOCK_SIZE, .cra_ctxsize = sizeof(struct tgr192_ctx), .cra_module = THIS_MODULE, + .cra_alignmask = 7, .cra_list = LIST_HEAD_INIT(tgr160.cra_list), .cra_u = {.digest = { .dia_digestsize = TGR160_DIGEST_SIZE,
@@ -655,6 +657,7 @@ .cra_blocksize = TGR192_BLOCK_SIZE, .cra_ctxsize = sizeof(struct tgr192_ctx), .cra_module = THIS_MODULE, + .cra_alignmask = 7, .cra_list = LIST_HEAD_INIT(tgr128.cra_list), .cra_u = {.digest = { .dia_digestsize = TGR128_DIGEST_SIZE,