From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.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 7049A4779AA for ; Thu, 30 Apr 2026 16:02:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777564974; cv=none; b=cCIyPh4B6RVhV+WCX8cwX775JgaY/Jo0i0GvWfvqHsaOCW9OtKttK87MISnmBmaqxQrwT/lwjV/auzWgi4Cwh59vh21LJ/6y05JPk17myBTH3kddKXNlFL0rpiDqYusPBr4OJfLntJoCuOq0yYokX0hdcgPMi0eNACzjPLUFSD4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777564974; c=relaxed/simple; bh=nSYWLOgCg9cYKYP1SSRIy1jcqnUttFghlsW4Zh+PYH4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=o6GwjTvNZAh/JJyzaz74mxEAjQWIeSwnRU+sn5YuRK5rZB0pfK+r7YY5onsj0I1WFutuT46SrO+9J40nZttqb9toLKC3Q242iWrUmlyeeg2kBzVe+pGo4y3INuzTmff1GVCuBSTnhtouxuyLNxFVFsyuD9kIfYwgPztQTzFaXCc= 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=U1IUzs9W; arc=none smtp.client-ip=209.85.128.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="U1IUzs9W" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-488ba2f4094so8020605e9.1 for ; Thu, 30 Apr 2026 09:02:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777564971; x=1778169771; 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=4coEQbrBUmFUrjDBw9QngC1TQrHyKn5HQ1tDIlRoapE=; b=U1IUzs9WVgXlpHnutnz/7hWh1Itzt4FeOHH3rESDj8KzjXICkPOcoXb16O1kvfFG7o yOqVSGdfTHszUWMxzkeTYCcGFqvopGSNhQq7atiY5LEL4z9rkHAjYy7I78WQH6iO60rh fb0Yws0olfq5+W1n8uViSwRHi+R9Qj3G1n2VZucH271HniygdhIAClBmg9jk37jEyD39 Ctt5tONn6+jUnWKcssTCIA1jfnt930Blkz3EBXNTgb4Bx52Lzhvkaa7erKcZ9puaTuD/ V8ROX7MYP9bCCmP2t2BwwQn5J5Cnp4V/8TTbq25KDp8YItU5vkxD6fjKCVRK74c89S2n 7kew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777564971; x=1778169771; 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=4coEQbrBUmFUrjDBw9QngC1TQrHyKn5HQ1tDIlRoapE=; b=W4SotA3xQaDLIFp/DNYcLDto+IlgHsbI7/R/gfHonXHAMAs4qLRiIre08/vYPlOKEV 4CF6RLkvIx3OsYTVEw6/ipbob3Ycz78x7DcLM9OFCUrE/mbtD3I8bJOTKDw8Rt/ZOoJk 9ZUh8d/XNjQHOOVq08abAdROmGFh5rcqScALIC4h1xuLKtNx6198QmvuMhR7IrfgEExH lBoSsVcQdRTawiDiwKuX8MevFRJK2cDx6siBF38If0KYQV6sZ2m3MOTxLX2L9W5ALhXC cCvVjW6vR2yxA7RHsE5MS9W3XZtCGjHU3xL9LCphJM8VlllxYmLgMxygiYb95cCmD4nS sl7w== X-Forwarded-Encrypted: i=1; AFNElJ+vQcs3IKGBj/8F7ZiBWeZSHTa4U/drlOT2N4jFFImm8Ncn1zTmeekm4FcL/DGVceC79hxmQtDsN2uCF1Q=@vger.kernel.org X-Gm-Message-State: AOJu0Ywfzvcpyhzku85r9j/v2PkPNUiw15n65MExqHu2NKHC2wZZsvly mLj0SIPxrEpktTXk6K8LZM5hjvHCaP7p/zqrn+mRY0AEeM9Rl7d6+qNoXatN1bnte5CQF1rcSJH P6J/rb6MMjqlTRQWIhFA3++4tpe1mjw== X-Received: from wmjx19.prod.google.com ([2002:a05:600c:21d3:b0:487:2186:fcf7]) (user=sebastianene job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:a14:b0:488:af7f:7707 with SMTP id 5b1f17b1804b1-48a84451bccmr53589865e9.18.1777564970647; Thu, 30 Apr 2026 09:02:50 -0700 (PDT) Date: Thu, 30 Apr 2026 16:02:41 +0000 In-Reply-To: <20260430160241.1934777-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: <20260430160241.1934777-1-sebastianene@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260430160241.1934777-3-sebastianene@google.com> Subject: [PATCH v2 2/2] KVM: arm64: Validate the offset to the mem access descriptor From: Sebastian Ene To: catalin.marinas@arm.com, maz@kernel.org, oupton@kernel.org, sudeep.holla@kernel.org, will@kernel.org Cc: joey.gouly@arm.com, korneld@google.com, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, android-kvm@google.com, mrigendra.chaubey@gmail.com, perlarsen@google.com, sebastianene@google.com, 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. Signed-off-by: Sebastian Ene --- v1 -> v2: * don't enforce a strict placement for the ep_mem_offset since this is not compliant with the spec and we should not make assumptions about the driver. Link to v1: https://lore.kernel.org/all/ae9KN9nkOgDYJcGP@google.com/T/#t --- --- arch/arm64/kvm/hyp/nvhe/ffa.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/ffa.c b/arch/arm64/kvm/hyp/nvhe/ffa.c index 1af722771178..8e9017fc8e39 100644 --- a/arch/arm64/kvm/hyp/nvhe/ffa.c +++ b/arch/arm64/kvm/hyp/nvhe/ffa.c @@ -479,7 +479,7 @@ static void __do_ffa_mem_xfer(const u64 func_id, 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; + u32 offset, nr_ranges, checked_offset, em_mem_access_off; int ret = 0; if (addr_mbz || npages_mbz || fraglen > len || @@ -508,8 +508,14 @@ 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 (em_mem_access_off > + KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE - (struct ffa_mem_region_attributes)) { + 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; @@ -576,7 +582,7 @@ static void do_ffa_mem_reclaim(struct arm_smccc_1_2_regs *res, DECLARE_REG(u32, flags, ctxt, 3); struct ffa_mem_region_attributes *ep_mem_access; struct ffa_composite_mem_region *reg; - u32 offset, len, fraglen, fragoff; + u32 offset, len, fraglen, fragoff, em_mem_access_off; struct ffa_mem_region *buf; int ret = 0; u64 handle; @@ -599,8 +605,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 (em_mem_access_off > + KVM_FFA_MBOX_NR_PAGES * PAGE_SIZE - (struct ffa_mem_region_attributes)) { + ret = FFA_RET_INVALID_PARAMETERS; + 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.545.g6539524ca2-goog