linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 RESEND] virtio-crypto: fix memory-leak
@ 2022-09-19  7:51 Lei He
  2022-09-30  6:14 ` Herbert Xu
  0 siblings, 1 reply; 3+ messages in thread
From: Lei He @ 2022-09-19  7:51 UTC (permalink / raw)
  To: herbert; +Cc: mst, arei.gonglei, linux-crypto, linux-kernel, pizhenwei, lei he

From: lei he <helei.sig11@bytedance.com>

Fix memory-leak for virtio-crypto akcipher request, this problem is
introduced by 59ca6c93387d3(virtio-crypto: implement RSA algorithm).
The leak can be reproduced and tested with the following script
inside virtual machine:

#!/bin/bash

LOOP_TIMES=10000

# required module: pkcs8_key_parser, virtio_crypto
modprobe pkcs8_key_parser # if CONFIG_PKCS8_PRIVATE_KEY_PARSER=m
modprobe virtio_crypto # if CONFIG_CRYPTO_DEV_VIRTIO=m
rm -rf /tmp/data
dd if=/dev/random of=/tmp/data count=1 bs=230

# generate private key and self-signed cert
openssl req -nodes -x509 -newkey rsa:2048 -keyout key.pem \
		-outform der -out cert.der  \
		-subj "/C=CN/ST=GD/L=SZ/O=vihoo/OU=dev/CN=always.com/emailAddress=yy@always.com"
# convert private key from pem to der
openssl pkcs8 -in key.pem -topk8 -nocrypt -outform DER -out key.der

# add key
PRIV_KEY_ID=`cat key.der | keyctl padd asymmetric test_priv_key @s`
echo "priv key id = "$PRIV_KEY_ID
PUB_KEY_ID=`cat cert.der | keyctl padd asymmetric test_pub_key @s`
echo "pub key id = "$PUB_KEY_ID

# query key
keyctl pkey_query $PRIV_KEY_ID 0
keyctl pkey_query $PUB_KEY_ID 0

# here we only run pkey_encrypt becasuse it is the fastest interface
function bench_pub() {
	keyctl pkey_encrypt $PUB_KEY_ID 0 /tmp/data enc=pkcs1 >/tmp/enc.pub
}

# do bench_pub in loop to obtain the memory leak
for (( i = 0; i < ${LOOP_TIMES}; ++i )); do
	bench_pub
done

Signed-off-by: lei he <helei.sig11@bytedance.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Gonglei <arei.gonglei@huawei.com>
---
 drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
index 2a60d0525cde..168195672e2e 100644
--- a/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
+++ b/drivers/crypto/virtio/virtio_crypto_akcipher_algs.c
@@ -56,6 +56,10 @@ static void virtio_crypto_akcipher_finalize_req(
 	struct virtio_crypto_akcipher_request *vc_akcipher_req,
 	struct akcipher_request *req, int err)
 {
+	kfree(vc_akcipher_req->src_buf);
+	kfree(vc_akcipher_req->dst_buf);
+	vc_akcipher_req->src_buf = NULL;
+	vc_akcipher_req->dst_buf = NULL;
 	virtcrypto_clear_request(&vc_akcipher_req->base);
 
 	crypto_finalize_akcipher_request(vc_akcipher_req->base.dataq->engine, req, err);

base-commit: 568035b01cfb107af8d2e4bd2fb9aea22cf5b868
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2 RESEND] virtio-crypto: fix memory-leak
  2022-09-19  7:51 [PATCH v2 RESEND] virtio-crypto: fix memory-leak Lei He
@ 2022-09-30  6:14 ` Herbert Xu
  2022-09-30  6:31   ` zhenwei pi
  0 siblings, 1 reply; 3+ messages in thread
From: Herbert Xu @ 2022-09-30  6:14 UTC (permalink / raw)
  To: Lei He; +Cc: mst, arei.gonglei, linux-crypto, linux-kernel, pizhenwei

On Mon, Sep 19, 2022 at 03:51:58PM +0800, Lei He wrote:
> From: lei he <helei.sig11@bytedance.com>
> 
> Fix memory-leak for virtio-crypto akcipher request, this problem is
> introduced by 59ca6c93387d3(virtio-crypto: implement RSA algorithm).
> The leak can be reproduced and tested with the following script
> inside virtual machine:
> 
> #!/bin/bash
> 
> LOOP_TIMES=10000
> 
> # required module: pkcs8_key_parser, virtio_crypto
> modprobe pkcs8_key_parser # if CONFIG_PKCS8_PRIVATE_KEY_PARSER=m
> modprobe virtio_crypto # if CONFIG_CRYPTO_DEV_VIRTIO=m
> rm -rf /tmp/data
> dd if=/dev/random of=/tmp/data count=1 bs=230
> 
> # generate private key and self-signed cert
> openssl req -nodes -x509 -newkey rsa:2048 -keyout key.pem \
> 		-outform der -out cert.der  \
> 		-subj "/C=CN/ST=GD/L=SZ/O=vihoo/OU=dev/CN=always.com/emailAddress=yy@always.com"
> # convert private key from pem to der
> openssl pkcs8 -in key.pem -topk8 -nocrypt -outform DER -out key.der
> 
> # add key
> PRIV_KEY_ID=`cat key.der | keyctl padd asymmetric test_priv_key @s`
> echo "priv key id = "$PRIV_KEY_ID
> PUB_KEY_ID=`cat cert.der | keyctl padd asymmetric test_pub_key @s`
> echo "pub key id = "$PUB_KEY_ID
> 
> # query key
> keyctl pkey_query $PRIV_KEY_ID 0
> keyctl pkey_query $PUB_KEY_ID 0
> 
> # here we only run pkey_encrypt becasuse it is the fastest interface
> function bench_pub() {
> 	keyctl pkey_encrypt $PUB_KEY_ID 0 /tmp/data enc=pkcs1 >/tmp/enc.pub
> }
> 
> # do bench_pub in loop to obtain the memory leak
> for (( i = 0; i < ${LOOP_TIMES}; ++i )); do
> 	bench_pub
> done
> 
> Signed-off-by: lei he <helei.sig11@bytedance.com>
> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> Reviewed-by: Gonglei <arei.gonglei@huawei.com>
> ---
>  drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 4 ++++
>  1 file changed, 4 insertions(+)

Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Re: [PATCH v2 RESEND] virtio-crypto: fix memory-leak
  2022-09-30  6:14 ` Herbert Xu
@ 2022-09-30  6:31   ` zhenwei pi
  0 siblings, 0 replies; 3+ messages in thread
From: zhenwei pi @ 2022-09-30  6:31 UTC (permalink / raw)
  To: Herbert Xu; +Cc: mst, arei.gonglei, linux-crypto, linux-kernel



On 9/30/22 14:14, Herbert Xu wrote:
> On Mon, Sep 19, 2022 at 03:51:58PM +0800, Lei He wrote:
>> From: lei he <helei.sig11@bytedance.com>
>>
>> Fix memory-leak for virtio-crypto akcipher request, this problem is
>> introduced by 59ca6c93387d3(virtio-crypto: implement RSA algorithm).
>> The leak can be reproduced and tested with the following script
>> inside virtual machine:
>>
>> #!/bin/bash
>>
>> LOOP_TIMES=10000
>>
>> # required module: pkcs8_key_parser, virtio_crypto
>> modprobe pkcs8_key_parser # if CONFIG_PKCS8_PRIVATE_KEY_PARSER=m
>> modprobe virtio_crypto # if CONFIG_CRYPTO_DEV_VIRTIO=m
>> rm -rf /tmp/data
>> dd if=/dev/random of=/tmp/data count=1 bs=230
>>
>> # generate private key and self-signed cert
>> openssl req -nodes -x509 -newkey rsa:2048 -keyout key.pem \
>> 		-outform der -out cert.der  \
>> 		-subj "/C=CN/ST=GD/L=SZ/O=vihoo/OU=dev/CN=always.com/emailAddress=yy@always.com"
>> # convert private key from pem to der
>> openssl pkcs8 -in key.pem -topk8 -nocrypt -outform DER -out key.der
>>
>> # add key
>> PRIV_KEY_ID=`cat key.der | keyctl padd asymmetric test_priv_key @s`
>> echo "priv key id = "$PRIV_KEY_ID
>> PUB_KEY_ID=`cat cert.der | keyctl padd asymmetric test_pub_key @s`
>> echo "pub key id = "$PUB_KEY_ID
>>
>> # query key
>> keyctl pkey_query $PRIV_KEY_ID 0
>> keyctl pkey_query $PUB_KEY_ID 0
>>
>> # here we only run pkey_encrypt becasuse it is the fastest interface
>> function bench_pub() {
>> 	keyctl pkey_encrypt $PUB_KEY_ID 0 /tmp/data enc=pkcs1 >/tmp/enc.pub
>> }
>>
>> # do bench_pub in loop to obtain the memory leak
>> for (( i = 0; i < ${LOOP_TIMES}; ++i )); do
>> 	bench_pub
>> done
>>
>> Signed-off-by: lei he <helei.sig11@bytedance.com>
>> Acked-by: Michael S. Tsirkin <mst@redhat.com>
>> Reviewed-by: Gonglei <arei.gonglei@huawei.com>
>> ---
>>   drivers/crypto/virtio/virtio_crypto_akcipher_algs.c | 4 ++++
>>   1 file changed, 4 insertions(+)
> 
> Patch applied.  Thanks.

Hi,

I noticed that MST has already applied on vhost branch.
https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git/commit/?h=vhost&id=1bedcf22c081a6e9943f09937b2da8d3ef52d20d

Thanks.

-- 
zhenwei pi

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-09-30  6:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-19  7:51 [PATCH v2 RESEND] virtio-crypto: fix memory-leak Lei He
2022-09-30  6:14 ` Herbert Xu
2022-09-30  6:31   ` zhenwei pi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).