Skip to contentSkip to conversation

crypto: qat - bound the live migration import parser

PATCH New Michael Bommarito 1 participant 3 messages Jun 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover crypto: qat - bound the live migration import parser

adf_mstate_mgr_init_from_remote() sets the section-walk cursor to mgr->buf + preh_len from a remote-supplied preh_len, and the default preamble checker only rejects preh_len > mgr->size. A remote preamble with preh_len == mgr->size moves the cursor one region past the allocation while n_sects is still honoured, so adf_mstate_sect_validate() reads sect->size before the section header is proven in bounds. The remote stream reaches this parser from the destination-host VFIO migration path (qat_vf_resume_write), so a malformed import reads out of bounds in the destination host kernel (fatal under KASAN / panic_on_warn).

Patch 1 rejects section headers not fully contained in the state buffer. Patch 2 adds KUnit coverage and is offered separately so it can be taken or dropped on its own. The parser was driven on QEMU x86_64 under KASAN via the patch 2 suite (Level-2: buggy code unchanged, surrounding VFIO/PF environment synthesized); the boundary trigger reports the out-of-bounds read on the unfixed parser and is gone after patch 1, with two benign controls passing on both trees.

Michael Bommarito (2):
crypto: qat - validate migration section header is in bounds
crypto: qat - add KUnit coverage for the migration import parser

 drivers/crypto/intel/qat/Kconfig              | 16 ++++
 .../intel/qat/qat_common/adf_mstate_mgr.c     | 18 ++++-
 .../qat/qat_common/adf_mstate_mgr_test.c      | 81 +++++++++++++++++++
 3 files changed, 113 insertions(+), 2 deletions(-)
 create mode 100644 drivers/crypto/intel/qat/qat_common/adf_mstate_mgr_test.c
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/2 crypto: qat - validate migration section header is in bounds

adf_mstate_mgr_init_from_remote() sets the section-walk cursor to mgr->buf + preh_len from the remote migration preamble. The default preamble checker only rejects preh_len > mgr->size, so preh_len == mgr->size (the 4096-byte QAT VF state buffer) puts mgr->state one region past the allocation while n_sects is still honoured. adf_mstate_sect_validate() then reads sect->size from that cursor before proving the section header is in the buffer. The remote stream reaches this parser from the destination-host VFIO migration path (qat_vf_resume_write), so a malformed import reads out of bounds.

Reject section headers not fully contained in the state buffer before dereferencing any of their fields.

Reproduced under KASAN on QEMU via the KUnit case in patch 2; the slab-out-of-bounds read is gone after this change.

Fixes f0bbfc391aa7 ("crypto: qat - implement interface for live migration")
Assisted-by Claude:claude-opus-4-8

---
The patch 2 KUnit case drives the real parser on a kmalloc(4096) buffer under KASAN on QEMU x86_64. Trigger {preh_len=4096, n_sects=1}: stock tree reports

  BUG: KASAN: slab-out-of-bounds in qat_mstate_remote_run

reading sect->size 8 bytes past the allocation; patched it returns -EINVAL and KASAN is silent. Two benign controls (empty preamble, in-bounds section header) drive the same path with no OOB and pass on both trees. No in-tree selftest exercises adf_mstate_mgr.c; patch 2 is the coverage offered. KASAN build of the touched object is warning clean.

 .../crypto/intel/qat/qat_common/adf_mstate_mgr.c   | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/crypto/intel/qat/qat_common/adf_mstate_mgr.c +12 -2
index f9017e03ec0f2..7370b87f72a2f 100644
@@ -231,8 +231,18 @@ static int adf_mstate_sect_validate(struct adf_mstate_mgr *mgr)
 
 	end = (uintptr_t)mgr->buf + mgr->size;
 	for (i = 0; i < mgr->n_sects; i++) {
-		uintptr_t s_start = (uintptr_t)sect->state;
-		uintptr_t s_end = s_start + sect->size;
+		uintptr_t s_start, s_end;
+
+		/* The section header must be in the buffer before it is read. */
+		if ((uintptr_t)sect < (uintptr_t)mgr->buf ||
+		    (uintptr_t)sect > end - sizeof(*sect)) {
+			pr_debug("QAT: LM - Section header out of bounds (index=%u) in state_mgr (size=%u, secs=%u)\n",
+				 i, mgr->size, mgr->n_sects);
+			return -EINVAL;
+		}
+
+		s_start = (uintptr_t)sect->state;
+		s_end = s_start + sect->size;
 
 		if (s_end < s_start || s_end > end) {
 			pr_debug("QAT: LM - Corrupted state section (index=%u, size=%u) in state_mgr (size=%u, secs=%u)\n",
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/2 crypto: qat - add KUnit coverage for the migration import parser

In reply to Michael Bommarito

Add KUnit coverage for the remote (migration import) path of the QAT live migration state manager. The cases drive the real adf_mstate_mgr_init_from_remote() -> adf_mstate_sect_validate() parser on a buffer sized as the GEN4 VFIO migration backend allocates it, and include the preh_len == buffer-size boundary case that is the regression oracle for the preceding fix. The test file is included from adf_mstate_mgr.c so it can reach the file-local preamble and section types, gated by CONFIG_CRYPTO_DEV_QAT_KUNIT_TEST. It is offered as a separate patch so it can be taken or dropped independently of the fix.

Reproduced under KASAN on QEMU; the boundary case reports the slab-out-of-bounds read on an unfixed tree and passes once the fix is in place.

Assisted-by Claude:claude-opus-4-8

---
Three cases under KASAN on QEMU x86_64: a valid empty preamble, a valid in-bounds section header, and the preh_len == buffer-size (4096) boundary case. The boundary case is the regression oracle for patch 1: it reports the slab-out-of-bounds read on an unfixed tree and returns -EINVAL with the fix in place. The two valid cases drive the same parser path with no out-of-bounds access and pass on both trees.

 drivers/crypto/intel/qat/Kconfig              | 16 ++++
 .../intel/qat/qat_common/adf_mstate_mgr.c     |  4 +
 .../qat/qat_common/adf_mstate_mgr_test.c      | 81 +++++++++++++++++++
 3 files changed, 101 insertions(+)
 create mode 100644 drivers/crypto/intel/qat/qat_common/adf_mstate_mgr_test.c
drivers/crypto/intel/qat/Kconfig +16 -0
index 9d6e6f52d2dcb..116f7f94c9a64 100644
@@ -133,3 +133,19 @@ config CRYPTO_DEV_QAT_ERROR_INJECTION
 
 	  This functionality is available via debugfs entry of the Intel(R)
 	  QuickAssist device
+
+config CRYPTO_DEV_QAT_KUNIT_TEST
+	bool "KUnit tests for Intel(R) QAT live migration state manager" if !KUNIT_ALL_TESTS
+	depends on CRYPTO_DEV_QAT && KUNIT=y
+	default KUNIT_ALL_TESTS
+	help
+	  Build KUnit tests for the Intel(R) QAT live migration state
+	  manager remote-import parser (adf_mstate_mgr.c). The tests drive
+	  the migration setup parser on a buffer sized as the QAT GEN4
+	  VFIO migration backend allocates it and check that a malformed
+	  remote preamble is rejected before any out-of-bounds access.
+
+	  For more information on KUnit and unit tests in general, please
+	  refer to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
drivers/crypto/intel/qat/qat_common/adf_mstate_mgr.c +4 -0
index 7370b87f72a2f..701279409c32c 100644
@@ -326,3 +326,7 @@ found:
 
 	return sect;
 }
+
+#if IS_ENABLED(CONFIG_CRYPTO_DEV_QAT_KUNIT_TEST)
+#include "adf_mstate_mgr_test.c"
+#endif
drivers/crypto/intel/qat/qat_common/adf_mstate_mgr_test.c new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
2.53.0