Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: virtio/akcipher - Fix stack overflow on memcpy

sizeof(struct virtio_crypto_akcipher_session_para) is less than
sizeof(struct virtio_crypto_op_ctrl_req::u), copying more bytes from
stack variable leads stack overflow. Clang reports this issue by
commands:
make -j CC=clang-14 mrproper >/dev/null 2>&1
make -j O=/tmp/crypto-build CC=clang-14 allmodconfig >/dev/null 2>&1
make -j O=/tmp/crypto-build W=1 CC=clang-14 drivers/crypto/virtio/
virtio_crypto_akcipher_algs.o

Fixes: 59ca6c93387d ("virtio-crypto: implement RSA algorithm")
Link: https://lore.kernel.org/all/0a194a79-e3a3-45e7-be98-83abd3e1cb7e@roeck-us.net/
Cc: <stable@vger.kernel.org>
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
Tested-by: Nathan Chancellor <nathan@kernel.org> # build
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

zhenwei pi and committed by
Herbert Xu
c0ec2a71 24c890dd

+3 -2
+3 -2
drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
··· 104 104 } 105 105 106 106 static int virtio_crypto_alg_akcipher_init_session(struct virtio_crypto_akcipher_ctx *ctx, 107 - struct virtio_crypto_ctrl_header *header, void *para, 107 + struct virtio_crypto_ctrl_header *header, 108 + struct virtio_crypto_akcipher_session_para *para, 108 109 const uint8_t *key, unsigned int keylen) 109 110 { 110 111 struct scatterlist outhdr_sg, key_sg, inhdr_sg, *sgs[3]; ··· 129 128 130 129 ctrl = &vc_ctrl_req->ctrl; 131 130 memcpy(&ctrl->header, header, sizeof(ctrl->header)); 132 - memcpy(&ctrl->u, para, sizeof(ctrl->u)); 131 + memcpy(&ctrl->u.akcipher_create_session.para, para, sizeof(*para)); 133 132 input = &vc_ctrl_req->input; 134 133 input->status = cpu_to_le32(VIRTIO_CRYPTO_ERR); 135 134