• Kees Cook's avatar
    treewide: Use array_size() in sock_kmalloc() · 76e43e37
    Kees Cook authored
    The sock_kmalloc() function has no 2-factor argument form, so
    multiplication factors need to be wrapped in array_size(). This patch
    replaces cases of:
    
            sock_kmalloc(handle, a * b, gfp)
    
    with:
            sock_kmalloc(handle, array_size(a, b), gfp)
    
    as well as handling cases of:
    
            sock_kmalloc(handle, a * b * c, gfp)
    
    with:
    
            sock_kmalloc(handle, array3_size(a, b, c), gfp)
    
    This does, however, attempt to ignore constant size factors like:
    
            sock_kmalloc(handle, 4 * 1024, gfp)
    
    though any constants defined via macros get caught up in the conversion.
    
    Any factors with a sizeof() of "unsigned char", "char", and "u8" were
    dropped, since they're redundant.
    
    The Coccinelle script used for this was:
    
    // Fix redundant parens around sizeof().
    @@
    expression HANDLE;
    type TYPE;
    expression THING, E;
    @@
    
    (
      sock_kmalloc(HANDLE,
    -	(sizeof(TYPE)) * E
    +	sizeof(TYPE) * E
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	(sizeof(THING)) * E
    +	sizeof(THING) * E
      , ...)
    )
    
    // Drop single-byte sizes and redundant parens.
    @@
    expression HANDLE;
    expression COUNT;
    typedef u8;
    typedef __u8;
    @@
    
    (
      sock_kmalloc(HANDLE,
    -	sizeof(u8) * (COUNT)
    +	COUNT
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(__u8) * (COUNT)
    +	COUNT
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(char) * (COUNT)
    +	COUNT
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(unsigned char) * (COUNT)
    +	COUNT
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(u8) * COUNT
    +	COUNT
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(__u8) * COUNT
    +	COUNT
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(char) * COUNT
    +	COUNT
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(unsigned char) * COUNT
    +	COUNT
      , ...)
    )
    
    // 2-factor product with sizeof(type/expression) and identifier or constant.
    @@
    expression HANDLE;
    type TYPE;
    expression THING;
    identifier COUNT_ID;
    constant COUNT_CONST;
    @@
    
    (
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE) * (COUNT_ID)
    +	array_size(COUNT_ID, sizeof(TYPE))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE) * COUNT_ID
    +	array_size(COUNT_ID, sizeof(TYPE))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE) * (COUNT_CONST)
    +	array_size(COUNT_CONST, sizeof(TYPE))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE) * COUNT_CONST
    +	array_size(COUNT_CONST, sizeof(TYPE))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING) * (COUNT_ID)
    +	array_size(COUNT_ID, sizeof(THING))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING) * COUNT_ID
    +	array_size(COUNT_ID, sizeof(THING))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING) * (COUNT_CONST)
    +	array_size(COUNT_CONST, sizeof(THING))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING) * COUNT_CONST
    +	array_size(COUNT_CONST, sizeof(THING))
      , ...)
    )
    
    // 2-factor product, only identifiers.
    @@
    expression HANDLE;
    identifier SIZE, COUNT;
    @@
    
      sock_kmalloc(HANDLE,
    -	SIZE * COUNT
    +	array_size(COUNT, SIZE)
      , ...)
    
    // 3-factor product with 1 sizeof(type) or sizeof(expression), with
    // redundant parens removed.
    @@
    expression HANDLE;
    expression THING;
    identifier STRIDE, COUNT;
    type TYPE;
    @@
    
    (
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE) * (COUNT) * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE) * (COUNT) * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE) * COUNT * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE) * COUNT * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(TYPE))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING) * (COUNT) * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING) * (COUNT) * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING) * COUNT * (STRIDE)
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING) * COUNT * STRIDE
    +	array3_size(COUNT, STRIDE, sizeof(THING))
      , ...)
    )
    
    // 3-factor product with 2 sizeof(variable), with redundant parens removed.
    @@
    expression HANDLE;
    expression THING1, THING2;
    identifier COUNT;
    type TYPE1, TYPE2;
    @@
    
    (
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE1) * sizeof(TYPE2) * COUNT
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING1) * sizeof(THING2) * COUNT
    +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(THING1) * sizeof(THING2) * (COUNT)
    +	array3_size(COUNT, sizeof(THING1), sizeof(THING2))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE1) * sizeof(THING2) * COUNT
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	sizeof(TYPE1) * sizeof(THING2) * (COUNT)
    +	array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
      , ...)
    )
    
    // 3-factor product, only identifiers, with redundant parens removed.
    @@
    expression HANDLE;
    identifier STRIDE, SIZE, COUNT;
    @@
    
    (
      sock_kmalloc(HANDLE,
    -	(COUNT) * STRIDE * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	COUNT * (STRIDE) * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	COUNT * STRIDE * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	(COUNT) * (STRIDE) * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	COUNT * (STRIDE) * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	(COUNT) * STRIDE * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	(COUNT) * (STRIDE) * (SIZE)
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    |
      sock_kmalloc(HANDLE,
    -	COUNT * STRIDE * SIZE
    +	array3_size(COUNT, STRIDE, SIZE)
      , ...)
    )
    
    // Any remaining multi-factor products, first at least 3-factor products
    // when they're not all constants...
    @@
    expression HANDLE;
    expression E1, E2, E3;
    constant C1, C2, C3;
    @@
    
    (
      sock_kmalloc(HANDLE, C1 * C2 * C3, ...)
    |
      sock_kmalloc(HANDLE,
    -	E1 * E2 * E3
    +	array3_size(E1, E2, E3)
      , ...)
    )
    
    // And then all remaining 2 factors products when they're not all constants.
    @@
    expression HANDLE;
    expression E1, E2;
    constant C1, C2;
    @@
    
    (
      sock_kmalloc(HANDLE, C1 * C2, ...)
    |
      sock_kmalloc(HANDLE,
    -	E1 * E2
    +	array_size(E1, E2)
      , ...)
    )
    Signed-off-by: 's avatarKees Cook <keescook@chromium.org>
    76e43e37
Name
Last commit
Last update
..
asymmetric_keys Loading commit data...
async_tx Loading commit data...
842.c Loading commit data...
Kconfig Loading commit data...
Makefile Loading commit data...
ablkcipher.c Loading commit data...
acompress.c Loading commit data...
aead.c Loading commit data...
aegis.h Loading commit data...
aegis128.c Loading commit data...
aegis128l.c Loading commit data...
aegis256.c Loading commit data...
aes_generic.c Loading commit data...
aes_ti.c Loading commit data...
af_alg.c Loading commit data...
ahash.c Loading commit data...
akcipher.c Loading commit data...
algapi.c Loading commit data...
algboss.c Loading commit data...
algif_aead.c Loading commit data...
algif_hash.c Loading commit data...
algif_rng.c Loading commit data...
algif_skcipher.c Loading commit data...
ansi_cprng.c Loading commit data...
anubis.c Loading commit data...
api.c Loading commit data...
arc4.c Loading commit data...
authenc.c Loading commit data...
authencesn.c Loading commit data...
blkcipher.c Loading commit data...
blowfish_common.c Loading commit data...
blowfish_generic.c Loading commit data...
camellia_generic.c Loading commit data...
cast5_generic.c Loading commit data...
cast6_generic.c Loading commit data...
cast_common.c Loading commit data...
cbc.c Loading commit data...
ccm.c Loading commit data...
cfb.c Loading commit data...
chacha20_generic.c Loading commit data...
chacha20poly1305.c Loading commit data...
cipher.c Loading commit data...
cmac.c Loading commit data...
compress.c Loading commit data...
crc32_generic.c Loading commit data...
crc32c_generic.c Loading commit data...
crct10dif_common.c Loading commit data...
crct10dif_generic.c Loading commit data...
cryptd.c Loading commit data...
crypto_engine.c Loading commit data...
crypto_null.c Loading commit data...
crypto_user.c Loading commit data...
crypto_wq.c Loading commit data...
ctr.c Loading commit data...
cts.c Loading commit data...
deflate.c Loading commit data...
des_generic.c Loading commit data...
dh.c Loading commit data...
dh_helper.c Loading commit data...
drbg.c Loading commit data...
ecb.c Loading commit data...
ecc.c Loading commit data...
ecc.h Loading commit data...
ecc_curve_defs.h Loading commit data...
ecdh.c Loading commit data...
ecdh_helper.c Loading commit data...
echainiv.c Loading commit data...
fcrypt.c Loading commit data...
fips.c Loading commit data...
gcm.c Loading commit data...
gf128mul.c Loading commit data...
ghash-generic.c Loading commit data...
hash_info.c Loading commit data...
hmac.c Loading commit data...
internal.h Loading commit data...
jitterentropy-kcapi.c Loading commit data...
jitterentropy.c Loading commit data...
keywrap.c Loading commit data...
khazad.c Loading commit data...
kpp.c Loading commit data...
lrw.c Loading commit data...
lz4.c Loading commit data...
lz4hc.c Loading commit data...
lzo.c Loading commit data...
mcryptd.c Loading commit data...
md4.c Loading commit data...
md5.c Loading commit data...
memneq.c Loading commit data...
michael_mic.c Loading commit data...
morus1280.c Loading commit data...
morus640.c Loading commit data...
pcbc.c Loading commit data...
pcrypt.c Loading commit data...
poly1305_generic.c Loading commit data...
proc.c Loading commit data...
ripemd.h Loading commit data...
rmd128.c Loading commit data...
rmd160.c Loading commit data...
rmd256.c Loading commit data...
rmd320.c Loading commit data...
rng.c Loading commit data...
rsa-pkcs1pad.c Loading commit data...
rsa.c Loading commit data...
rsa_helper.c Loading commit data...
rsaprivkey.asn1 Loading commit data...
rsapubkey.asn1 Loading commit data...
salsa20_generic.c Loading commit data...
scatterwalk.c Loading commit data...
scompress.c Loading commit data...
seed.c Loading commit data...
seqiv.c Loading commit data...
serpent_generic.c Loading commit data...
sha1_generic.c Loading commit data...
sha256_generic.c Loading commit data...
sha3_generic.c Loading commit data...
sha512_generic.c Loading commit data...
shash.c Loading commit data...
simd.c Loading commit data...
skcipher.c Loading commit data...
sm3_generic.c Loading commit data...
sm4_generic.c Loading commit data...
speck.c Loading commit data...
tcrypt.c Loading commit data...
tcrypt.h Loading commit data...
tea.c Loading commit data...
testmgr.c Loading commit data...
testmgr.h Loading commit data...
tgr192.c Loading commit data...
twofish_common.c Loading commit data...
twofish_generic.c Loading commit data...
vmac.c Loading commit data...
wp512.c Loading commit data...
xcbc.c Loading commit data...
xor.c Loading commit data...
xts.c Loading commit data...
zstd.c Loading commit data...