public inbox for linux-arm-kernel@lists.infradead.org 
 help / color / mirror / Atom feed
From: Mostafa Saleh <smostafa@google•com>
To: op-tee@lists•trustedfirmware.org, linux-kernel@vger•kernel.org,
	 kvmarm@lists•linux.dev, linux-arm-kernel@lists•infradead.org
Cc: maz@kernel•org, oupton@kernel•org, joey.gouly@arm•com,
	 suzuki.poulose@arm•com, catalin.marinas@arm•com,
	jens.wiklander@linaro•org,  sumit.garg@kernel•org,
	sebastianene@google•com, vdonnefort@google•com,
	 sudeep.holla@kernel•org, Mostafa Saleh <smostafa@google•com>
Subject: [PATCH v4 3/5] firmware: arm_ffa: Fix Endpoint Memory Access Descriptor offset calculation
Date: Wed, 20 May 2026 20:49:46 +0000	[thread overview]
Message-ID: <20260520204948.2440882-4-smostafa@google.com> (raw)
In-Reply-To: <20260520204948.2440882-1-smostafa@google.com>

From: Sebastian Ene <sebastianene@google•com>

Use the descriptor's `ep_mem_offset` to calculate the start of the endpoint
memory access array and to comply with the FF-A spec instead of defaulting
to `sizeof(struct ffa_mem_region)`.
This requires moving `ffa_mem_region_additional_setup()` earlier in the setup
flow.
Also, add sanity checks to ensure the calculated descriptor offsets do not
exceed `max_fragsize`.

Signed-off-by: Sebastian Ene <sebastianene@google•com>
Signed-off-by: Mostafa Saleh <smostafa@google•com>
---
 drivers/firmware/arm_ffa/driver.c | 16 +++++++++++-----
 include/linux/arm_ffa.h           |  2 +-
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/firmware/arm_ffa/driver.c b/drivers/firmware/arm_ffa/driver.c
index b700b2e93e72..8573a7a6556e 100644
--- a/drivers/firmware/arm_ffa/driver.c
+++ b/drivers/firmware/arm_ffa/driver.c
@@ -685,19 +685,26 @@ ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
 	struct ffa_composite_mem_region *composite;
 	struct ffa_mem_region_addr_range *constituents;
 	struct ffa_mem_region_attributes *ep_mem_access;
-	u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg);
+	u32 idx, frag_len, length, buf_sz = 0, num_entries = sg_nents(args->sg), ep_offset;
+	u32 emad_size = ffa_emad_size_get(drv_info->version);
 
 	mem_region->tag = args->tag;
 	mem_region->flags = args->flags;
 	mem_region->sender_id = drv_info->vm_id;
 	mem_region->attributes = ffa_memory_attributes_get(func_id);
+
+	ffa_mem_region_additional_setup(drv_info->version, mem_region);
 	composite_offset = ffa_mem_desc_offset(buffer, args->nattrs,
 					       drv_info->version);
+	if (composite_offset + sizeof(*composite) > max_fragsize)
+		return -ENXIO;
 
 	for (idx = 0; idx < args->nattrs; idx++) {
-		ep_mem_access = buffer +
-			ffa_mem_desc_offset(buffer, idx, drv_info->version);
-		memset(ep_mem_access, 0, ffa_emad_size_get(drv_info->version));
+		ep_offset = ffa_mem_desc_offset(buffer, idx, drv_info->version);
+		if (ep_offset + emad_size > max_fragsize)
+			return -ENXIO;
+		ep_mem_access = buffer + ep_offset;
+		memset(ep_mem_access, 0, emad_size);
 		ep_mem_access->receiver = args->attrs[idx].receiver;
 		ep_mem_access->attrs = args->attrs[idx].attrs;
 		ep_mem_access->composite_off = composite_offset;
@@ -707,7 +714,6 @@ ffa_setup_and_transmit(u32 func_id, void *buffer, u32 max_fragsize,
 	}
 	mem_region->handle = 0;
 	mem_region->ep_count = args->nattrs;
-	ffa_mem_region_additional_setup(drv_info->version, mem_region);
 
 	composite = buffer + composite_offset;
 	composite->total_pg_cnt = ffa_get_num_pages_sg(args->sg);
diff --git a/include/linux/arm_ffa.h b/include/linux/arm_ffa.h
index 81e603839c4a..62d67dae8b70 100644
--- a/include/linux/arm_ffa.h
+++ b/include/linux/arm_ffa.h
@@ -445,7 +445,7 @@ ffa_mem_desc_offset(struct ffa_mem_region *buf, int count, u32 ffa_version)
 	if (!FFA_MEM_REGION_HAS_EP_MEM_OFFSET(ffa_version))
 		offset += offsetof(struct ffa_mem_region, ep_mem_offset);
 	else
-		offset += sizeof(struct ffa_mem_region);
+		offset += buf->ep_mem_offset;
 
 	return offset;
 }
-- 
2.54.0.669.g59709faab0-goog



  parent reply	other threads:[~2026-05-20 20:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-20 20:49 [PATCH v4 0/5] arm_ffa, KVM: Fix FF-A emad offset calculations Mostafa Saleh
2026-05-20 20:49 ` [PATCH v4 1/5] optee: ffa: Add NULL check in optee_ffa_lend_protmem Mostafa Saleh
2026-05-20 20:49 ` [PATCH v4 2/5] firmware: arm_ffa: Fix out-of-bound writes in ffa_setup_and_transmit() Mostafa Saleh
2026-05-21 12:51   ` Sudeep Holla
2026-05-20 20:49 ` Mostafa Saleh [this message]
2026-05-21 12:55   ` [PATCH v4 3/5] firmware: arm_ffa: Fix Endpoint Memory Access Descriptor offset calculation Sudeep Holla
2026-05-20 20:49 ` [PATCH v4 4/5] KVM: arm64: Fix bounds checking in do_ffa_mem_reclaim() Mostafa Saleh
2026-05-21  8:28   ` Marc Zyngier
2026-05-21 10:30     ` Mostafa Saleh
2026-05-21 10:43       ` Mostafa Saleh
2026-05-21 12:12         ` Marc Zyngier
2026-05-20 20:49 ` [PATCH v4 5/5] KVM: arm64: Validate the offset to the mem access descriptor Mostafa Saleh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260520204948.2440882-4-smostafa@google.com \
    --to=smostafa@google$(echo .)com \
    --cc=catalin.marinas@arm$(echo .)com \
    --cc=jens.wiklander@linaro$(echo .)org \
    --cc=joey.gouly@arm$(echo .)com \
    --cc=kvmarm@lists$(echo .)linux.dev \
    --cc=linux-arm-kernel@lists$(echo .)infradead.org \
    --cc=linux-kernel@vger$(echo .)kernel.org \
    --cc=maz@kernel$(echo .)org \
    --cc=op-tee@lists$(echo .)trustedfirmware.org \
    --cc=oupton@kernel$(echo .)org \
    --cc=sebastianene@google$(echo .)com \
    --cc=sudeep.holla@kernel$(echo .)org \
    --cc=sumit.garg@kernel$(echo .)org \
    --cc=suzuki.poulose@arm$(echo .)com \
    --cc=vdonnefort@google$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox