crypto/aes.c: array overrun
PATCH 1/1 crypto/aes.c: array overrun
The Coverity checker spotted the following in crypto/aes.c:
<-- snip -->
...
struct aes_ctx {
int key_length;
u32 E[60];
u32 D[60];
};#define E_KEY ctx->E
...
#define loop8(i) \
{ t = ror32(t, 8); ; t = ls_box(t) ^ rco_tab[i]; \
t ^= E_KEY[8 * i]; E_KEY[8 * i + 8] = t; \
t ^= E_KEY[8 * i + 1]; E_KEY[8 * i + 9] = t; \
t ^= E_KEY[8 * i + 2]; E_KEY[8 * i + 10] = t; \
t ^= E_KEY[8 * i + 3]; E_KEY[8 * i + 11] = t; \
t = E_KEY[8 * i + 4] ^ ls_box(t); \
E_KEY[8 * i + 12] = t; \
t ^= E_KEY[8 * i + 5]; E_KEY[8 * i + 13] = t; \
t ^= E_KEY[8 * i + 6]; E_KEY[8 * i + 14] = t; \
t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \
}static intaes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags) {
...
case 32:
...
for (i = 0; i < 7; ++i)
loop8 (i);...
<-- snip -->
The problem is:
8 * 6 + 15 = 63 > 59
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedHerbert Xu
maintainerQuoted 3 lines · Adrian Bunk
...
#define loop8(i) \
...
Quoted 11 lines
t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \ }static int
aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags){
...case 32: ... for (i = 0; i < 7; ++i) loop8 (i);
OK this is not pretty but it is actually correct. Notice how we only overstep the mark for E_KEY but never for D_KEY. Since D_KEY is only initialised after this, it is OK for us to trash the start of D_KEY.
It's just a trick that makes the code slightly nicer (and no I didn't write this nor am I necessarily condoning it :)
Thanks for reporting this though.
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.txtQuoted 19 lines · Herbert Xu
Quoted 3 lines · Adrian Bunk
...
#define loop8(i) \...
Quoted 11 lines
t ^= E_KEY[8 * i + 7]; E_KEY[8 * i + 15] = t; \ }static int
aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags){
...case 32: ... for (i = 0; i < 7; ++i) loop8 (i);
Quoted 1 line
OK this is not pretty but it is actually correct. Notice how we only
~~~~~~~~~~~~~~~~~
Quoted 5 lines
overstep the mark for
E_KEYbut never forD_KEY. SinceD_KEYis only initialised after this, it is OK for us to trash the start ofD_KEY.It's just a trick that makes the code slightly nicer (and no I didn't write this nor am I necessarily condoning it :)
Overstepping array is not correct C. Even if gcc lays it out in order where array-to-be-thrashed is after it, so it works in practice, it is not okay. [Some kind of security-hardened-gcc may stop this as buffer overflow, for example]
Pavel
Signature · 1 line
161: {Quoted 3 lines · Herbert Xu
OK this is not pretty but it is actually correct. Notice how we only overstep the mark for
E_KEYbut never forD_KEY. SinceD_KEYis only initialised after this, it is OK for us to trash the start ofD_KEY.
I think a big comment block describing this behavior is called for, as it carries an implicit requirement that D_KEY and E_KEY remain adjacent in memory. Anybody allocating space between them is in for a rude awakening....
Jivin [email protected] lays it down ...
Quoted 10 lines · Herbert Xu
Quoted 4 lines
OK this is not pretty but it is actually correct. Notice how we only overstep the mark for
E_KEYbut never forD_KEY. SinceD_KEYis only initialised after this, it is OK for us to trash the start ofD_KEY.I think a big comment block describing this behavior is called for, as it carries an implicit requirement that
D_KEYandE_KEYremain adjacent in memory. Anybody allocating space between them is in for a rude awakening....
Sounds like a bug waiting to happen to me.
Why not do something like the attached patch.
Cheers,
Davidm
Signature · 2 lines
David McCullough, [email protected], Ph:+61 734352815 Secure Computing - SnapGear http://www.uCdot.org http://www.cyberguard.com
Index: linux-2.6.x/crypto/aes.c
===================================================================
RCS file: linux-2.6.x/crypto/aes.c,v
retrieving revision 1.1.1.6
crypto/aes.c +3 -4
@@ -78,12 +78,11 @@struct aes_ctx {int key_length; -u32 E[60 ]; -u32 D[60 ]; +u32 _KEYS[120 ]; }; -#define E_KEY ctx->E -#define D_KEY ctx->D +#define E_KEY (&ctx->_KEYS[0]) +#define D_KEY (&ctx->_KEYS[60])static u8 pow_tab[256 ]__initdata ;static u8 log_tab[256 ]__initdata ;
Herbert Xu
maintainercrypto/aes.c line 82
Quoted 7 lines · David McCullough
struct
aes_ctx{crypto/aes.c +1
-2int key_length; -u32 E [60 ]; -u32 D [60 ]; +u32 _KEYS[120 ]; };
Looks good. Thanks for this David.
Could you please change the name from _KEYS to buf and patch the x86-64 version as well?
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.txtJivin Herbert Xu lays it down ...
Quoted 11 lines · David McCullough
Review
Quoted 7 lines
struct
aes_ctx{(quoted diff) +1
-2int key_length; - u32 E[60]; - u32 D[60]; + u32 _KEYS[120]; };Looks good. Thanks for this David.
Quoted 2 lines
Could you please change the name from
_KEYSto buf and patch the x86-64 version as well?
No problems, attached.
Cheers,
Davidm
Signature · 2 lines
David McCullough, [email protected], Ph:+61 734352815 Secure Computing - SnapGear http://www.uCdot.org http://www.cyberguard.com
Index: linux-2.6.x/crypto/aes.c
===================================================================
RCS file: linux-2.6.x/crypto/aes.c,v
retrieving revision 1.1.1.6
crypto/aes.c +3 -4
@@ -78,12 +78,11 @@struct aes_ctx {int key_length; -u32 E[60 ]; -u32 D[60 ]; +u32 buf[120 ]; }; -#define E_KEY ctx->E -#define D_KEY ctx->D +#define E_KEY (&ctx->buf[0]) +#define D_KEY (&ctx->buf[60])static u8 pow_tab[256 ]__initdata ;static u8 log_tab[256 ]__initdata ;
Index: linux-2.6.x/arch/x86_64/crypto/aes.c
===================================================================
RCS file: linux-2.6.x/arch/x86_64/crypto/aes.c,v
retrieving revision 1.1.1.1
arch/x86_64/crypto/aes.c +3 -4
@@ -79,12 +79,11 @@struct aes_ctx {u32 key_length; -u32 E[60 ]; -u32 D[60 ]; +u32 buf[120 ]; }; -#define E_KEY ctx->E -#define D_KEY ctx->D +#define E_KEY (&ctx->buf[0]) +#define D_KEY (&ctx->buf[60])static u8 pow_tab[256 ]__initdata ;static u8 log_tab[256 ]__initdata ;
Herbert Xu
maintainerQuoted 2 lines · David McCullough
No problems, attached.
Patch applied. BTW, please attach a Signed-off-by line for your next patch submission. Thanks a lot.
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