Skip to content

Commit 84c93e8

Browse files
ardbiesheuvelgregkh
authored andcommitted
crypto: michael_mic - fix broken misalignment handling
commit e1b2d98 upstream. The Michael MIC driver uses the cra_alignmask to ensure that pointers presented to its update and finup/final methods are 32-bit aligned. However, due to the way the shash API works, this is no guarantee that the 32-bit reads occurring in the update method are also aligned, as the size of the buffer presented to update may be of uneven length. For instance, an update() of 3 bytes followed by a misaligned update() of 4 or more bytes will result in a misaligned access using an accessor that is not suitable for this. On most architectures, this does not matter, and so setting the cra_alignmask is pointless. On architectures where this does matter, setting the cra_alignmask does not actually solve the problem. So let's get rid of the cra_alignmask, and use unaligned accessors instead, where appropriate. Cc: <stable@vger.kernel.org> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6e3b671 commit 84c93e8

1 file changed

Lines changed: 12 additions & 19 deletions

File tree

‎crypto/michael_mic.c‎

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Copyright (c) 2004 Jouni Malinen <j@w1.fi>
88
*/
99
#include <crypto/internal/hash.h>
10-
#include <asm/byteorder.h>
10+
#include <asm/unaligned.h>
1111
#include <linux/init.h>
1212
#include <linux/module.h>
1313
#include <linux/string.h>
@@ -19,7 +19,7 @@ struct michael_mic_ctx {
1919
};
2020

2121
struct michael_mic_desc_ctx {
22-
u8 pending[4];
22+
__le32 pending;
2323
size_t pending_len;
2424

2525
u32 l, r;
@@ -60,37 +60,34 @@ static int michael_update(struct shash_desc *desc, const u8 *data,
6060
unsigned int len)
6161
{
6262
struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
63-
const __le32 *src;
6463

6564
if (mctx->pending_len) {
6665
int flen = 4 - mctx->pending_len;
6766
if (flen > len)
6867
flen = len;
69-
memcpy(&mctx->pending[mctx->pending_len], data, flen);
68+
memcpy((u8 *)&mctx->pending + mctx->pending_len, data, flen);
7069
mctx->pending_len += flen;
7170
data += flen;
7271
len -= flen;
7372

7473
if (mctx->pending_len < 4)
7574
return 0;
7675

77-
src = (const __le32 *)mctx->pending;
78-
mctx->l ^= le32_to_cpup(src);
76+
mctx->l ^= le32_to_cpu(mctx->pending);
7977
michael_block(mctx->l, mctx->r);
8078
mctx->pending_len = 0;
8179
}
8280

83-
src = (const __le32 *)data;
84-
8581
while (len >= 4) {
86-
mctx->l ^= le32_to_cpup(src++);
82+
mctx->l ^= get_unaligned_le32(data);
8783
michael_block(mctx->l, mctx->r);
84+
data += 4;
8885
len -= 4;
8986
}
9087

9188
if (len > 0) {
9289
mctx->pending_len = len;
93-
memcpy(mctx->pending, src, len);
90+
memcpy(&mctx->pending, data, len);
9491
}
9592

9693
return 0;
@@ -100,8 +97,7 @@ static int michael_update(struct shash_desc *desc, const u8 *data,
10097
static int michael_final(struct shash_desc *desc, u8 *out)
10198
{
10299
struct michael_mic_desc_ctx *mctx = shash_desc_ctx(desc);
103-
u8 *data = mctx->pending;
104-
__le32 *dst = (__le32 *)out;
100+
u8 *data = (u8 *)&mctx->pending;
105101

106102
/* Last block and padding (0x5a, 4..7 x 0) */
107103
switch (mctx->pending_len) {
@@ -123,8 +119,8 @@ static int michael_final(struct shash_desc *desc, u8 *out)
123119
/* l ^= 0; */
124120
michael_block(mctx->l, mctx->r);
125121

126-
dst[0] = cpu_to_le32(mctx->l);
127-
dst[1] = cpu_to_le32(mctx->r);
122+
put_unaligned_le32(mctx->l, out);
123+
put_unaligned_le32(mctx->r, out + 4);
128124

129125
return 0;
130126
}
@@ -135,13 +131,11 @@ static int michael_setkey(struct crypto_shash *tfm, const u8 *key,
135131
{
136132
struct michael_mic_ctx *mctx = crypto_shash_ctx(tfm);
137133

138-
const __le32 *data = (const __le32 *)key;
139-
140134
if (keylen != 8)
141135
return -EINVAL;
142136

143-
mctx->l = le32_to_cpu(data[0]);
144-
mctx->r = le32_to_cpu(data[1]);
137+
mctx->l = get_unaligned_le32(key);
138+
mctx->r = get_unaligned_le32(key + 4);
145139
return 0;
146140
}
147141

@@ -156,7 +150,6 @@ static struct shash_alg alg = {
156150
.cra_name = "michael_mic",
157151
.cra_driver_name = "michael_mic-generic",
158152
.cra_blocksize = 8,
159-
.cra_alignmask = 3,
160153
.cra_ctxsize = sizeof(struct michael_mic_ctx),
161154
.cra_module = THIS_MODULE,
162155
}

0 commit comments

Comments
 (0)