Skip to content

Commit f9fc1ec

Browse files
arndbherbertx
authored andcommitted
crypto: drivers - avoid memcpy size warning
Some configurations with gcc-12 or gcc-13 produce a warning for the source and destination of a memcpy() in atmel_sha_hmac_compute_ipad_hash() potentially overlapping: In file included from include/linux/string.h:254, from drivers/crypto/atmel-sha.c:15: drivers/crypto/atmel-sha.c: In function 'atmel_sha_hmac_compute_ipad_hash': include/linux/fortify-string.h:57:33: error: '__builtin_memcpy' accessing 129 or more bytes at offsets 408 and 280 overlaps 1 or more bytes at offset 408 [-Werror=restrict] 57 | #define __underlying_memcpy __builtin_memcpy | ^ include/linux/fortify-string.h:648:9: note: in expansion of macro '__underlying_memcpy' 648 | __underlying_##op(p, q, __fortify_size); \ | ^~~~~~~~~~~~~ include/linux/fortify-string.h:693:26: note: in expansion of macro '__fortify_memcpy_chk' 693 | #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \ | ^~~~~~~~~~~~~~~~~~~~ drivers/crypto/atmel-sha.c:1773:9: note: in expansion of macro 'memcpy' 1773 | memcpy(hmac->opad, hmac->ipad, bs); | ^~~~~~ The same thing happens in two more drivers that have the same logic: drivers/crypto/chelsio/chcr_algo.c: In function 'chcr_ahash_setkey': include/linux/fortify-string.h:57:33: error: '__builtin_memcpy' accessing 129 or more bytes at offsets 260 and 132 overlaps 1 or more bytes at offset 260 [-Werror=restrict] drivers/crypto/bcm/cipher.c: In function 'ahash_hmac_setkey': include/linux/fortify-string.h:57:33: error: '__builtin_memcpy' accessing between 129 and 4294967295 bytes at offsets 840 and 712 overlaps between 1 and 4294967167 bytes at offset 840 [-Werror=restrict] I don't think it can actually happen because the size is strictly bounded to the available block sizes, at most 128 bytes, though inlining decisions could lead gcc to not see that. Use the unsafe_memcpy() helper instead of memcpy(), with the only difference being that this skips the hardening checks that produce the warning. Suggested-by: Herbert Xu <[email protected]> Signed-off-by: Arnd Bergmann <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 8e03dd6 commit f9fc1ec

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

drivers/crypto/atmel-sha.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,8 @@ static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd)
17701770
size_t bs = ctx->block_size;
17711771
size_t i, num_words = bs / sizeof(u32);
17721772

1773-
memcpy(hmac->opad, hmac->ipad, bs);
1773+
unsafe_memcpy(hmac->opad, hmac->ipad, bs,
1774+
"fortified memcpy causes -Wrestrict warning");
17741775
for (i = 0; i < num_words; ++i) {
17751776
hmac->ipad[i] ^= 0x36363636;
17761777
hmac->opad[i] ^= 0x5c5c5c5c;

drivers/crypto/bcm/cipher.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2397,7 +2397,8 @@ static int ahash_hmac_setkey(struct crypto_ahash *ahash, const u8 *key,
23972397
memset(ctx->ipad + ctx->authkeylen, 0,
23982398
blocksize - ctx->authkeylen);
23992399
ctx->authkeylen = 0;
2400-
memcpy(ctx->opad, ctx->ipad, blocksize);
2400+
unsafe_memcpy(ctx->opad, ctx->ipad, blocksize,
2401+
"fortified memcpy causes -Wrestrict warning");
24012402

24022403
for (index = 0; index < blocksize; index++) {
24032404
ctx->ipad[index] ^= HMAC_IPAD_VALUE;

drivers/crypto/chelsio/chcr_algo.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,8 @@ static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
22162216
memcpy(hmacctx->ipad, key, keylen);
22172217
}
22182218
memset(hmacctx->ipad + keylen, 0, bs - keylen);
2219-
memcpy(hmacctx->opad, hmacctx->ipad, bs);
2219+
unsafe_memcpy(hmacctx->opad, hmacctx->ipad, bs,
2220+
"fortified memcpy causes -Wrestrict warning");
22202221

22212222
for (i = 0; i < bs / sizeof(int); i++) {
22222223
*((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA;

0 commit comments

Comments
 (0)