Skip to contentSkip to conversation

Avoid QEMU OOM on huge request from guest

PATCH Superseded zhenwei pi 3 participants 6 messages Dec 14, 2025 – Dec 20, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
author
#1 lore raw

PATCH 1/2 hw/virtio/virtio-crypto: verify asym request size

In reply to zhenwei pi

The total lenght of request is limited by cryptodev config, verify it to avoid unexpected request from guest.

Fixes 0e660a6f90a ("crypto: Introduce RSA algorithm")
---
 hw/virtio/virtio-crypto.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/virtio/virtio-crypto.c +7 -0
index 517f2089c5..94dbf9d92d 100644
@@ -767,11 +767,18 @@ virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto,
     uint32_t len;
     uint8_t *src = NULL;
     uint8_t *dst = NULL;
+    uint64_t max_len;
 
     asym_op_info = g_new0(CryptoDevBackendAsymOpInfo, 1);
     src_len = ldl_le_p(&req->para.src_data_len);
     dst_len = ldl_le_p(&req->para.dst_data_len);
 
+    max_len = src_len + dst_len;
+    if (unlikely(max_len > vcrypto->conf.max_size)) {
+        virtio_error(vdev, "virtio-crypto asym too big length");
+        goto err;
+    }
+
     if (src_len > 0) {
         src = g_malloc0(src_len);
         len = iov_to_buf(iov, out_num, 0, src, src_len);
Signature · 1 line
2.43.0

zhenwei pi

author
#0 lore raw

cover Avoid QEMU OOM on huge request from guest

Fix two issues in this series:
- Verify asym request size from device level
- Limit the maximum size for cryptodev builtin driver

zhenwei pi (2):
hw/virtio/virtio-crypto: verify asym request size
cryptodev-builtin: Limit the maximum size

 backends/cryptodev-builtin.c | 9 +++------
 hw/virtio/virtio-crypto.c    | 7 +++++++
 2 files changed, 10 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH 2/2 cryptodev-builtin: Limit the maximum size

This backend driver is used for demonstration purposes only, unlimited size leads QEMU OOM.

Fixes 1653a5f3fc7 ("cryptodev: introduce a new cryptodev backend")
---
 backends/cryptodev-builtin.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
backends/cryptodev-builtin.c +3 -6
index 0414c01e06..55a3fbd27b 100644
@@ -53,6 +53,8 @@ typedef struct CryptoDevBackendBuiltinSession {
 
 #define CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN    512
 #define CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN  64
+/* demonstration purposes only, use a limited size to avoid QEMU OOM */
+#define CRYPTODEV_BUITLIN_MAX_REQUEST_SIZE  (1024 * 1024)
 
 struct CryptoDevBackendBuiltin {
     CryptoDevBackend parent_obj;
@@ -98,12 +100,7 @@ static void cryptodev_builtin_init(
                          1u << QCRYPTODEV_BACKEND_SERVICE_TYPE_MAC;
     backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
     backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
-    /*
-     * Set the Maximum length of crypto request.
-     * Why this value? Just avoid to overflow when
-     * memory allocation for each crypto request.
-     */
-    backend->conf.max_size = LONG_MAX - sizeof(CryptoDevBackendOpInfo);
+    backend->conf.max_size = CRYPTODEV_BUITLIN_MAX_REQUEST_SIZE;
     backend->conf.max_cipher_key_len = CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN;
     backend->conf.max_auth_key_len = CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN;
     cryptodev_builtin_init_akcipher(backend);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] hw/virtio/virtio-crypto: verify asym request size

In reply to zhenwei pi
Quoted 3 lines · zhenwei pi

The total lenght of request is limited by cryptodev config, verify it to avoid unexpected request from guest.

CVE-2025-14876 has been assigned to this bug.

Thanks,

Quoted 33 lines
Fixes 0e660a6f90a ("crypto: Introduce RSA algorithm")
---
 hw/virtio/virtio-crypto.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/virtio/virtio-crypto.c +7 -1
index 517f2089c5..94dbf9d92d 100644
@@ -767,11 +767,18 @@ virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto,
     uint32_t len;
     uint8_t *src = NULL;
     uint8_t *dst = NULL;
+    uint64_t max_len;
 
     asym_op_info = g_new0(CryptoDevBackendAsymOpInfo, 1);
     src_len = ldl_le_p(&req->para.src_data_len);
     dst_len = ldl_le_p(&req->para.dst_data_len);
 
+    max_len = src_len + dst_len;
+    if (unlikely(max_len > vcrypto->conf.max_size)) {
+        virtio_error(vdev, "virtio-crypto asym too big length");
+        goto err;
+    }
+
     if (src_len > 0) {
         src = g_malloc0(src_len);
         len = iov_to_buf(iov, out_num, 0, src, src_len);
--

2.43.0

Signature · 3 lines
Mauro Matteo Cascella
Red Hat Product Security
PGP-Key ID: BB3410B0
author
#3 lore raw

Re: [PATCH 1/2] hw/virtio/virtio-crypto: verify asym request size

Quoted 9 lines · Mauro Matteo Cascella
Quoted 3 lines · zhenwei pi

The total lenght of request is limited by cryptodev config, verify it to avoid unexpected request from guest.

CVE-2025-14876 has been assigned to this bug.

Thanks,

OK, I suggest the two patches are tagged with this CVE. This root reason of this issue:
- the lack of limitation from hw akcipher (this fix)
- so huge limitation (almost LONG_MAX bytes) from backend builtin driver (the next fix)

Quoted 35 lines
Quoted 33 lines
Fixes 0e660a6f90a ("crypto: Introduce RSA algorithm")
---
  hw/virtio/virtio-crypto.c | 7 +++++++
  1 file changed, 7 insertions(+)
hw/virtio/virtio-crypto.c +7 -1
index 517f2089c5..94dbf9d92d 100644
@@ -767,11 +767,18 @@ virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto,
      uint32_t len;
      uint8_t *src = NULL;
      uint8_t *dst = NULL;
+    uint64_t max_len;
 
      asym_op_info = g_new0(CryptoDevBackendAsymOpInfo, 1);
      src_len = ldl_le_p(&req->para.src_data_len);
      dst_len = ldl_le_p(&req->para.dst_data_len);
 
+    max_len = src_len + dst_len;
+    if (unlikely(max_len > vcrypto->conf.max_size)) {
+        virtio_error(vdev, "virtio-crypto asym too big length");
+        goto err;
+    }
+
      if (src_len > 0) {
          src = g_malloc0(src_len);
          len = iov_to_buf(iov, out_num, 0, src, src_len);
--

2.43.0

#4 lore raw

Re: [PATCH 1/2] hw/virtio/virtio-crypto: verify asym request size

In reply to zhenwei pi
Quoted 15 lines · zhenwei pi

The total lenght of request is limited by cryptodev config, verify it to avoid unexpected request from guest.

Fixes 0e660a6f90a ("crypto: Introduce RSA algorithm")
---
  hw/virtio/virtio-crypto.c | 7 +++++++
  1 file changed, 7 insertions(+)
hw/virtio/virtio-crypto.c +0 -0
index 517f2089c5..94dbf9d92d 100644

hw/virtio/virtio-crypto.c virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto, · line 776

Quoted 10 lines
hw/virtio/virtio-crypto.c +2 -0
@@ -767,11 +767,18 @@ virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto,
      uint32_t len;
      uint8_t *src = NULL;
      uint8_t *dst = NULL;
+    uint64_t max_len;
 
      asym_op_info = g_new0(CryptoDevBackendAsymOpInfo, 1);
      src_len = ldl_le_p(&req->para.src_data_len);
      dst_len = ldl_le_p(&req->para.dst_data_len);
 
+    max_len = src_len + dst_len;

I believe this can be overflown when calculating the sum, while both args are uint32_t.

      max_len = (uint64_t)src_len + dst_len;

might be better. This is what's used in other places in this file too.

I wonder if modern compilers can warn about such overflow possibilities, and what's the better way to write such expressions. Something like

max_len = src_len; max_len += dst_len

maybe? :)

hw/virtio/virtio-crypto.c virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto, · line 778

Quoted 2 lines
hw/virtio/virtio-crypto.c +2 -0
+    if (unlikely(max_len > vcrypto->conf.max_size)) {
+        virtio_error(vdev, "virtio-crypto asym too big length");

"virtio-crypto asym request is too large" ?

Thanks,

/mjt