Skip to contentSkip to conversation

KVM: arm64: Add memory length checks before it is xfered

PATCH In review Snehal Koukuntla 3 participants 4 messages Sep 6, 2024 – Sep 9, 2024

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

PATCH 1/1 KVM: arm64: Add memory length checks before it is xfered

From: Snehal <[email protected]>

Check size during allocation to fix discrepancy in memory reclaim path. Currently only happens during memory reclaim, inconsistent with mem_xfer

---
 arch/arm64/kvm/hyp/nvhe/ffa.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/kvm/hyp/nvhe/ffa.c +5 -0
index e715c157c2c4..e9223cc4f913 100644
@@ -461,6 +461,11 @@ static __always_inline void do_ffa_mem_xfer(const u64 func_id,
 		goto out_unlock;
 	}
 
+	if (len > ffa_desc_buf.len) {
+		ret = FFA_RET_NO_MEMORY;
+		goto out_unlock;
+	}
+
 	buf = hyp_buffers.tx;
 	memcpy(buf, host_buffers.tx, fraglen);
 
Signature · 1 line
2.46.0.469.g59c65b2a67-goog
maintainer
#1 lore raw

Hi Snehal,

Quoted 5 lines · Snehal Koukuntla

From: Snehal <[email protected]>

Check size during allocation to fix discrepancy in memory reclaim path. Currently only happens during memory reclaim, inconsistent with mem_xfer

Can you please elaborate? It doesn't seem to fail at allocation time here, as everything is pre-allocated. Some context would greatly help, as my FFA-foo is as basic as it gets (I did read the spec once and ran away screaming).

Quoted 2 lines

The From: and Signed-off-by: tags do not match. You may want to add a [user] section to your .gitconfig with your full name so that this issue is sorted once and for all.

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id,

Quoted 9 lines
---
 arch/arm64/kvm/hyp/nvhe/ffa.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm64/kvm/hyp/nvhe/ffa.c +0 -0
index e715c157c2c4..e9223cc4f913 100644
@@ -461,6 +461,11 @@ static __always_inline void do_ffa_mem_xfer(const u64 func_id,

/facepalm: why do we have this __always_inline here? Nothing to do with your patch, but definitely worth understanding why it is required.

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id, · line 468

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +5 -0
 		goto out_unlock;
 	}
 
+	if (len > ffa_desc_buf.len) {
+		ret = FFA_RET_NO_MEMORY;
+		goto out_unlock;
+	}
+

It took some digging to understand how the various queues are sized, and a comment explaining the relation between ffa_desc_buf and the other queues would be very welcome.

I also notice that we have other places (apparently dealing with fragments) that do not have such checks. Do they need anything else?

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id, · line 471

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +0 -0
 	buf = hyp_buffers.tx;
 	memcpy(buf, host_buffers.tx, fraglen);
 

Finally, this probably deserves a Fixes: tag and a Cc: stable so that it can be backported.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#2 lore raw

On Fri, Sep 06, 2024 at 05:35:39PM +0100, Marc Zyngier wrote:

Hi,

Quoted 7 lines

Hi Snehal,

Quoted 3 lines · Snehal Koukuntla

From: Snehal <[email protected]>

Quoted 8 lines
Quoted 2 lines

Check size during allocation to fix discrepancy in memory reclaim path. Currently only happens during memory reclaim, inconsistent with mem_xfer

Can you please elaborate? It doesn't seem to fail at allocation time here, as everything is pre-allocated. Some context would greatly help, as my FFA-foo is as basic as it gets (I did read the spec once and ran away screaming).

Right, I think what happens is that we use the fragmentation API to transfer memory to Trustzone that normally won't fit on the reclaim path where we use an auxiliary buffer to store the descriptors.

All the descriptors are identified by the same handle and the reclaim will try to store them into the ffa_desc_buf before nuking the FF-A annotation from the stage-2.

Quoted 11 lines
Quoted 2 lines

The From: and Signed-off-by: tags do not match. You may want to add a [user] section to your .gitconfig with your full name so that this issue is sorted once and for all.

Quoted 4 lines
---
 arch/arm64/kvm/hyp/nvhe/ffa.c | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 10 lines

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id,

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +0 -0
index e715c157c2c4..e9223cc4f913 100644
@@ -461,6 +461,11 @@ static __always_inline void do_ffa_mem_xfer(const u64 func_id,

/facepalm: why do we have this __always_inline here? Nothing to do with your patch, but definitely worth understanding why it is required.

I don't think it is needed, we can drop it. Maybe as part of this patch ?

Quoted 13 lines

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id, · line 468

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +5 -0
 		goto out_unlock;
 	}
 
+	if (len > ffa_desc_buf.len) {
+		ret = FFA_RET_NO_MEMORY;
+		goto out_unlock;
+	}
+

It took some digging to understand how the various queues are sized, and a comment explaining the relation between ffa_desc_buf and the other queues would be very welcome.

Quoted 3 lines

I also notice that we have other places (apparently dealing with fragments) that do not have such checks. Do they need anything else?

I think we don't need that check in other parts.

Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id, · line 471

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +0 -0
 	buf = hyp_buffers.tx;
 	memcpy(buf, host_buffers.tx, fraglen);
 

Finally, this probably deserves a Fixes: tag and a Cc: stable so that it can be backported.

Thanks,

	M.

Seb

Quoted 2 lines
Signature · 1 line
Without deviation from the norm, progress is not possible.
#3 lore raw

Hi,

Thanks Sebastian for replying to Marc's comments.

Quoted 9 lines
Quoted 7 lines

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id,

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +0 -0
@@ -461,6 +461,11 @@ static __always_inline void do_ffa_mem_xfer(const u64 func_id,

/facepalm: why do we have this __always_inline here? Nothing to do with your patch, but definitely worth understanding why it is required.

I don't think it is needed, we can drop it. Maybe as part of this patch ?

I will send an updated patch removing the inline then.

Quoted 3 lines
Quoted 3 lines

The From: and Signed-off-by: tags do not match. You may want to add a [user] section to your .gitconfig with your full name so that this issue is sorted once and for all.

Thanks, I will update my git config to fix the inconsistency.

Thanks,
Snehal

Quoted 89 lines · Sebastian Ene

On Fri, Sep 06, 2024 at 05:35:39PM +0100, Marc Zyngier wrote:

Hi,

Quoted 7 lines

Hi Snehal,

Quoted 3 lines · Snehal Koukuntla

From: Snehal <[email protected]>

Quoted 8 lines
Quoted 2 lines

Check size during allocation to fix discrepancy in memory reclaim path. Currently only happens during memory reclaim, inconsistent with mem_xfer

Can you please elaborate? It doesn't seem to fail at allocation time here, as everything is pre-allocated. Some context would greatly help, as my FFA-foo is as basic as it gets (I did read the spec once and ran away screaming).

Right, I think what happens is that we use the fragmentation API to transfer memory to Trustzone that normally won't fit on the reclaim path where we use an auxiliary buffer to store the descriptors.

All the descriptors are identified by the same handle and the reclaim will try to store them into the ffa_desc_buf before nuking the FF-A annotation from the stage-2.

Quoted 11 lines
Quoted 2 lines

The From: and Signed-off-by: tags do not match. You may want to add a [user] section to your .gitconfig with your full name so that this issue is sorted once and for all.

Quoted 4 lines
---
 arch/arm64/kvm/hyp/nvhe/ffa.c | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 10 lines

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id,

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +0 -0
index e715c157c2c4..e9223cc4f913 100644
@@ -461,6 +461,11 @@ static __always_inline void do_ffa_mem_xfer(const u64 func_id,

/facepalm: why do we have this __always_inline here? Nothing to do with your patch, but definitely worth understanding why it is required.

I don't think it is needed, we can drop it. Maybe as part of this patch ?

Quoted 9 lines

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id, · line 468

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +5 -0
            goto out_unlock;
    }
 
+   if (len > ffa_desc_buf.len) {
+           ret = FFA_RET_NO_MEMORY;
+           goto out_unlock;
+   }
+
Quoted 7 lines

It took some digging to understand how the various queues are sized, and a comment explaining the relation between ffa_desc_buf and the other queues would be very welcome.

I also notice that we have other places (apparently dealing with fragments) that do not have such checks. Do they need anything else?

I think we don't need that check in other parts.

Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/ffa.c static __always_inline void do_ffa_mem_xfer(const u64 func_id, · line 471

Quoted 3 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +0 -0
    buf = hyp_buffers.tx;
    memcpy(buf, host_buffers.tx, fraglen);
 

Finally, this probably deserves a Fixes: tag and a Cc: stable so that it can be backported.

Thanks,

      M.

Seb

Quoted 2 lines

--
Without deviation from the norm, progress is not possible.