From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A9E9466B47 for ; Wed, 17 Jun 2026 14:51:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781707913; cv=none; b=m9f60jx+ylCbZdbpwpzDT/WNHedr6pRU2zi0idcfwVPnBMOmeI2X7Eru1wefYTztEflrdqjFYzeubFo6ycOBJidiLhkOGRNU7uB5S6tT5kcz1rBfO121EqdLLw16VJ5TmKScnu/2hT41lcAQq/QCmpzOeCmoiW12KQ57Q6HBXbo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781707913; c=relaxed/simple; bh=0Gnoj5mVkYvfotoTTL4oke5ZLGjP0C1k2XZl/76sd5A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qTOedXHT6QWEKFSW2xPVOqM+PudTfYIMZFZ0d8/TnWgJ5eu10MwaXKh9D918pkloTbY0088OZZEgNRDse3xvnqJS/t4WaLWijf/25JW1LsxQ3o6x4SDEteyHrce9yUSmjBuZfSsbtIRFrfbhNvEZi3yTpTwZ3JOhHzW2/oRntFQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=u2hvVmvk; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="u2hvVmvk" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-4629f312a67so1056842f8f.2 for ; Wed, 17 Jun 2026 07:51:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781707909; x=1782312709; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qkRBbhHB4iuN7xSPIITChDuTjwYpXvbOBnslFsQsEpc=; b=u2hvVmvkiRnz7I8lIEyN9eYnApQ5JHXQcCdod097sN3hl1pTbWtx/EE0SrR3soItXy LA9urRrzpQXFkBrYaCkmEDzpfy3atmEZ77bfy0KNFL4dihqbJ1w5/of92ql+MCe7DVdT F+/mG+P2N7AyZtJ1srEB/2csRrkZhyPsZdxl4Xpbax93qJlW/O8VA6HSPgEsbggqghHP ArE1AfYUZshzSPrMrAM96/faJsxQsWhCOizfvO9xZwv+u3maujYXlDQJapGnrL+BCdNf JxPlUKtc0SEujUvrqn257SiSREvsfZQxCnhxp352Vbs3Qofq7W3PVwuc0DtZrgZU0uzt mI5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781707909; x=1782312709; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qkRBbhHB4iuN7xSPIITChDuTjwYpXvbOBnslFsQsEpc=; b=rX6J3J1Qqmt5dw4bSVsCOlK1YeQHAaG1FqS0QH9l+bw4U90I78VejM+vuF6E8RvMQQ +y113HwZMzuclNiGXwG3dX0KznqbRc0y9NxQUA8FNAbJt5GuK/VGOLLXB+DDyxd2KMiw otOPMNtFaKpUPOQYCGVqTYrrPfUY63TueHd6v5+z9WMyF8qg+lc7NmLOEe+hd0vDELWR JxOPcgf3uhTBeesPpkq6S3+Cq/KNqv0Q7XfJsFMtUpdF5YukIMhGkBJLVUzc24QHWyBM Px+AskTHMR8uaB0+RGGPGeaowlTZpBD7t4J1mQp4OYEB3FCVXzncGjcJNJV29QCPK5Ae 1R+A== X-Forwarded-Encrypted: i=1; AFNElJ+TJYIWfrrmhwC0Y8QXevONk5vgGe5hD5xCob3OfHgraCgYaXffABO5j0NOdyNx+jshrmhJ7o6G/UdpbQ8=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7HkzHBmWqZ3IOhbobWG0OWRskE7RRFO8jg5CIEN6VvLp7JzCt NICBRqDe8ivYk5SxnvFj16z1r01G67M2pKJEjXE6B4y4O+XcZUnoowGqqw84GDHcKHKhVzeR//U 1snwYUNqCGiA40bq1wWRxwwU1+ynDcQ== X-Received: from wmbc22.prod.google.com ([2002:a7b:c016:0:b0:491:41a:40d4]) (user=sebastianene job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:37ce:b0:490:bd66:e526 with SMTP id 5b1f17b1804b1-49236791faemr29047845e9.32.1781707908701; Wed, 17 Jun 2026 07:51:48 -0700 (PDT) Date: Wed, 17 Jun 2026 14:51:28 +0000 In-Reply-To: <20260617145130.3729015-1-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260617145130.3729015-1-sebastianene@google.com> X-Mailer: git-send-email 2.54.0.1136.gdb2ca164c4-goog Message-ID: <20260617145130.3729015-6-sebastianene@google.com> Subject: [PATCH v7 5/7] KVM: arm64: Validate the offset to the mem access descriptor From: Sebastian Ene To: catalin.marinas@arm.com, oupton@kernel.org, sudeep.holla@kernel.org, will@kernel.org Cc: jens.wiklander@linaro.org, joey.gouly@arm.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, android-kvm@google.com, maz@kernel.org, mrigendra.chaubey@gmail.com, op-tee@lists.trustedfirmware.org, perlarsen@google.com, sebastianene@google.com, seiden@linux.ibm.com, smostafa@google.com, sumit.garg@kernel.org, suzuki.poulose@arm.com, vdonnefort@google.com, yuzenghui@huawei.com Content-Type: text/plain; charset="UTF-8" Prevent the pKVM hypervisor from making assumptions that the endpoint memory access descriptor (EMAD) comes right after the FF-A memory region header. Prior to FF-A version 1.1 the header of the memory region didn't contain an offset to the endpoint memory access descriptor. The layout of a memory transaction looks like this from 1.1 onward: Type | Field name | Offset [ Header | ffa_mem_region | 0 EMAD 1 | ffa_mem_region_attributes) | ffa_mem_region.ep_mem_offset ] Verify that the offset to the first endpoint memory access descriptor is within the mailbox buffer bounds. Also, fix one hardcoded sizeof(struct ffa_mem_region_attributes) that should be replaced ffa_emad_size_get() for compatibility with FFA v1.0. Fixes: 42fb33dde42b ("KVM: arm64: Use FF-A 1.1 with pKVM") Signed-off-by: Sebastian Ene Signed-off-by: Mostafa Saleh --- arch/arm64/kvm/hyp/nvhe/ffa.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/ffa.c b/arch/arm64/kvm/hyp/nvhe/ffa.c index 2d211661952e..1a2abd0154c6 100644 --- a/arch/arm64/kvm/hyp/nvhe/ffa.c +++ b/arch/arm64/kvm/hyp/nvhe/ffa.c @@ -476,11 +476,14 @@ static void __do_ffa_mem_xfer(const u64 func_id, DECLARE_REG(u32, fraglen, ctxt, 2); DECLARE_REG(u64, addr_mbz, ctxt, 3); DECLARE_REG(u32, npages_mbz, ctxt, 4); + u32 offset, nr_ranges, checked_offset, em_mem_access_off; struct ffa_mem_region_attributes *ep_mem_access; struct ffa_composite_mem_region *reg; struct ffa_mem_region *buf; - u32 offset, nr_ranges, checked_offset; int ret = 0; + size_t mem_region_len = !FFA_MEM_REGION_HAS_EP_MEM_OFFSET(hyp_ffa_version) ? + offsetof(struct ffa_mem_region, ep_mem_offset) : + sizeof(struct ffa_mem_region); if (addr_mbz || npages_mbz || fraglen > len || fraglen > KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE) { @@ -488,8 +491,7 @@ static void __do_ffa_mem_xfer(const u64 func_id, goto out; } - if (fraglen < sizeof(struct ffa_mem_region) + - sizeof(struct ffa_mem_region_attributes)) { + if (fraglen < mem_region_len + ffa_emad_size_get(hyp_ffa_version)) { ret = FFA_RET_INVALID_PARAMETERS; goto out; } @@ -508,8 +510,13 @@ static void __do_ffa_mem_xfer(const u64 func_id, buf = hyp_buffers.tx; memcpy(buf, host_buffers.tx, fraglen); - ep_mem_access = (void *)buf + - ffa_mem_desc_offset(buf, 0, hyp_ffa_version); + em_mem_access_off = ffa_mem_desc_offset(buf, 0, hyp_ffa_version); + if ((u64)em_mem_access_off + ffa_emad_size_get(hyp_ffa_version) > fraglen) { + ret = FFA_RET_INVALID_PARAMETERS; + goto out_unlock; + } + + ep_mem_access = (void *)buf + em_mem_access_off; offset = ep_mem_access->composite_off; if (!offset || buf->ep_count != 1 || buf->sender_id != HOST_FFA_ID) { ret = FFA_RET_INVALID_PARAMETERS; @@ -574,9 +581,9 @@ static void do_ffa_mem_reclaim(struct arm_smccc_1_2_regs *res, DECLARE_REG(u32, handle_lo, ctxt, 1); DECLARE_REG(u32, handle_hi, ctxt, 2); DECLARE_REG(u32, flags, ctxt, 3); + u32 offset, len, fraglen, fragoff, em_mem_access_off; struct ffa_mem_region_attributes *ep_mem_access; struct ffa_composite_mem_region *reg; - u32 offset, len, fraglen, fragoff; struct ffa_mem_region *buf; int ret = 0; u64 handle; @@ -599,8 +606,14 @@ static void do_ffa_mem_reclaim(struct arm_smccc_1_2_regs *res, len = res->a1; fraglen = res->a2; - ep_mem_access = (void *)buf + - ffa_mem_desc_offset(buf, 0, hyp_ffa_version); + em_mem_access_off = ffa_mem_desc_offset(buf, 0, hyp_ffa_version); + if ((u64)em_mem_access_off + ffa_emad_size_get(hyp_ffa_version) > fraglen) { + ret = FFA_RET_INVALID_PARAMETERS; + ffa_rx_release(res); + goto out_unlock; + } + + ep_mem_access = (void *)buf + em_mem_access_off; offset = ep_mem_access->composite_off; /* * We can trust the SPMD to get this right, but let's at least -- 2.54.0.1136.gdb2ca164c4-goog