KVM: arm64: Return -EINVAL for an empty SMCCC filter range at base 0
cover KVM: arm64: Return -EINVAL for an empty SMCCC filter range at base 0
KVM_ARM_VM_SMCCC_FILTER rejects an empty range (nr_functions == 0) with -EINVAL for every base except 0. kvm_smccc_set_filter() only rejects a range if its inclusive end, base + nr_functions - 1, is below base; for base 0 that end wraps to U32_MAX, and KVM then tries to insert [0, U32_MAX], which overlaps the reserved Arm Architecture Calls ranges, so the attribute returns -EEXIST instead of -EINVAL.
The request is rejected either way; only the errno is wrong, so this is a minor UAPI validation fix.
Patch 1 rejects a zero function count explicitly. Patch 2 adds the base 0 case to test_invalid_nr_functions(), which only covered a nonzero base and therefore never caught this.
Tested on an arm64 KVM host in VHE mode running under QEMU TCG: a small userspace reproducer gets EEXIST before patch 1 and EINVAL after it, and the smccc_filter selftest including the new assertion passes with patch 1 applied and fails at the new assertion with EEXIST without it.
Karl Mehltretter (2):
KVM: arm64: Return -EINVAL for an empty SMCCC filter range at base 0
KVM: arm64: selftests: Test empty SMCCC filter range at base 0
arch/arm64/kvm/hypercalls.c | 3 ++- tools/testing/selftests/kvm/arm64/smccc_filter.c | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-)
base-commit: 548e7bcd0c5460ddcbca9600cea603ebeebf4da7
Signature · 1 line
2.39.5 (Apple Git-154)
PATCH 1/2 KVM: arm64: Return -EINVAL for an empty SMCCC filter range at base 0
kvm_smccc_set_filter() only rejects a range if its inclusive end, base + nr_functions - 1, is below base. That catches an empty range (nr_functions == 0) at every nonzero base, but at base 0 the end wraps to U32_MAX and KVM tries to insert [0, U32_MAX], which overlaps the reserved Arm Architecture Calls ranges. KVM_ARM_VM_SMCCC_FILTER then returns -EEXIST instead of the -EINVAL that the smccc_filter selftest expects for an empty range.
Reject a zero function count explicitly.
Tested with a userspace reproducer on an arm64 VHE host under QEMU TCG:
EEXIST before, EINVAL after.
--- arch/arm64/kvm/hypercalls.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/hypercalls.c +2 -1
@@ -185,7 +185,8 @@ static int kvm_smccc_set_filter(struct kvm *kvm, struct kvm_smccc_filter __user start = filter.base; end = start + filter.nr_functions -1 ; -if (end < start || filter.action >=NR_SMCCC_FILTER_ACTIONS ) +if (!filter.nr_functions || end < start || + filter.action >=NR_SMCCC_FILTER_ACTIONS )return -EINVAL ;mutex_lock (&kvm->arch.config_lock);
Signature · 1 line
2.39.5 (Apple Git-154)
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Unprivileged userspace-reachable WARN_ON_ONCE can be triggered by memory pressure, potentially causing a host panic. --
commit 7eae467c4f77ea62fe7302656a7c43a21cc35eb0
Author: Karl Mehltretter <[email protected]>
KVM: arm64: Return -EINVAL for an empty SMCCC filter range at base 0
This commit fixes an issue where an empty SMCCC filter range at base 0 wraps around to U32_MAX, overlapping reserved ranges and incorrectly returning -EEXIST. It explicitly rejects a zero function count instead.
Quoted 5 lines
arch/arm64/kvm/hypercalls.c +0
-0
arch/arm64/kvm/hypercalls.c static int kvm_smccc_set_filter(struct kvm *kvm, struct kvm_smccc_filter __user · line 192
Quoted 9 lines
arch/arm64/kvm/hypercalls.c +2
-1@@ -185,7 +185,8 @@ static int kvm_smccc_set_filter(struct kvm *kvm, struct kvm_smccc_filter __user start = filter.base; end = start + filter.nr_functions -1 ; -if (end < start || filter.action >=NR_SMCCC_FILTER_ACTIONS ) +if (!filter.nr_functions || end < start || + filter.action >=NR_SMCCC_FILTER_ACTIONS )return -EINVAL ;mutex_lock (&kvm->arch.config_lock);
[Severity: High]
This isn't a bug introduced by this patch, but while looking at kvm_smccc_set_filter(), is it possible for an unprivileged user to trigger a kernel panic via the WARN_ON_ONCE lower down in this function?
A userspace process can call the KVM_ARM_VM_SMCCC_FILTER ioctl, which requires /dev/kvm access. The kernel calls kvm_smccc_filter_insert_reserved(), which in turn invokes mtree_insert_range() with GFP_KERNEL_ACCOUNT.
If this maple tree node allocation fails due to memory pressure or cgroup limits, it returns -ENOMEM.
The caller kvm_smccc_set_filter() then unconditionally executes the warning check:
if (!kvm_smccc_filter_configured(kvm)) {
r = kvm_smccc_filter_insert_reserved(kvm);
if (WARN_ON_ONCE(r))
goto out_unlock;
}Because KVM subsystem guidelines forbid host-userspace-reachable warnings, could this lead to a host denial-of-service if panic_on_warn is enabled?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 9 lines · Karl Mehltretter
kvm_smccc_set_filter()only rejects a range if its inclusive end, base +nr_functions- 1, is below base. That catches an empty range (nr_functions== 0) at every nonzero base, but at base 0 the end wraps toU32_MAXand KVM tries to insert [0,U32_MAX], which overlaps the reserved Arm Architecture Calls ranges.KVM_ARM_VM_SMCCC_FILTERthen returns-EEXISTinstead of the-EINVALthat thesmccc_filterselftest expects for an empty range.
Quoted 7 lines
Reject a zero function count explicitly.
Tested with a userspace reproducer on an arm64 VHE host under QEMU TCG:
EEXIST before, EINVAL after.Fixes 821d935c87bc ("KVM: arm64: Introduce support for userspace SMCCC filtering")Assisted-by LLM
nit: this should be Assisted-by: AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2] rather than LLM
Quoted 1 line
Signed-off-by Karl Mehltretter <[email protected]>
I think Sashiko is onto something, but that's for another day.
Cheers,
/fuad
Quoted 21 lines
--- arch/arm64/kvm/hypercalls.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/arm64/kvm/hypercalls.c +2
-2@@ -185,7 +185,8 @@ static int kvm_smccc_set_filter(struct kvm *kvm, struct kvm_smccc_filter __user start = filter.base; end = start + filter.nr_functions -1 ; -if (end < start || filter.action >=NR_SMCCC_FILTER_ACTIONS ) +if (!filter.nr_functions || end < start || + filter.action >=NR_SMCCC_FILTER_ACTIONS )return -EINVAL ;mutex_lock (&kvm->arch.config_lock); --2.39.5 (Apple Git-154)
Quoted 6 lines · Fuad Tabba
Quoted 2 lines
Fixes 821d935c87bc ("KVM: arm64: Introduce support for userspace SMCCC filtering")Assisted-by LLMnit: this should be
Assisted-by: AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]rather than LLM
When I first saw 'LLM' in a patch I thought so too.
But it is now allowed, see recent discussion and merge:
https://lore.kernel.org/r/[email protected]/
816d9992d9ed ("coding-assistants: simplify attribution")
Quoted 2 lines
Reviewed-by Fuad Tabba <[email protected]>Tested-by Fuad Tabba < [email protected]>
Thanks for the review and also testing!
Karl
Fuad Tabba
reviewerQuoted 9 lines · Karl Mehltretter
Quoted 6 lines · Fuad Tabba
Quoted 2 lines
Fixes 821d935c87bc ("KVM: arm64: Introduce support for userspace SMCCC filtering")Assisted-by LLMnit: this should be
Assisted-by: AGENT_NAME:MODEL_VERSION [TOOL1] [TOOL2]rather than LLM
Quoted 5 lines
When I first saw 'LLM' in a patch I thought so too.
But it is now allowed, see recent discussion and merge:
I missed that. Makes sense to be honest. Either way, that was just a nit :)
Thanks!
/fuad
Quoted 9 lines
816d9992d9ed("coding-assistants: simplify attribution")Quoted 2 lines
Reviewed-by Fuad Tabba <[email protected]>Tested-by Fuad Tabba < [email protected]>Thanks for the review and also testing!
Karl
Quoted 8 lines · Karl Mehltretter
kvm_smccc_set_filter()only rejects a range if its inclusive end, base +nr_functions- 1, is below base. That catches an empty range (nr_functions== 0) at every nonzero base, but at base 0 the end wraps toU32_MAXand KVM tries to insert [0,U32_MAX], which overlaps the reserved Arm Architecture Calls ranges.KVM_ARM_VM_SMCCC_FILTERthen returns-EEXISTinstead of the-EINVALthat thesmccc_filterselftest expects for an empty range.
Quoted 8 lines
Reject a zero function count explicitly.
Tested with a userspace reproducer on an arm64 VHE host under QEMU TCG:
EEXIST before, EINVAL after.Fixes 821d935c87bc ("KVM: arm64: Introduce support for userspace SMCCC filtering")Assisted-by LLMSigned-off-by Karl Mehltretter <[email protected]>
Good catch.
FWIW: Tested it on my downstream version of arm-on-s390 with hypercalls already implemented.
PATCH 2/2 KVM: arm64: selftests: Test empty SMCCC filter range at base 0
In reply to Karl Mehltrettertest_invalid_nr_functions() only checks an empty range at PSCI_0_2_FN64_CPU_ON, which KVM's end < start check happens to catch. It never exercised base 0, where the inclusive end wraps to U32_MAX instead.
Add the base 0 case. Without the preceding fix it fails with EEXIST.
--- tools/testing/selftests/kvm/arm64/smccc_filter.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/testing/selftests/kvm/arm64/smccc_filter.c +4 -0
@@ -140,6 +140,10 @@ static void test_invalid_nr_functions(void)TEST_ASSERT (r <0 && errno ==EINVAL ,"Attempt to filter 0 functions should return EINVAL" ); + r =__set_smccc_filter (vm,0 ,0 ,KVM_SMCCC_FILTER_DENY ); +TEST_ASSERT (r <0 && errno ==EINVAL , +"Attempt to filter 0 functions at base 0 should return EINVAL" ); +kvm_vm_free (vm); }
Signature · 1 line
2.39.5 (Apple Git-154)
Re: [PATCH 2/2] KVM: arm64: selftests: Test empty SMCCC filter range at base 0
Quoted 10 lines · Karl Mehltretter
test_invalid_nr_functions()only checks an empty range atPSCI_0_2_FN64_CPU_ON, which KVM's end < start check happens to catch. It never exercised base 0, where the inclusive end wraps toU32_MAXinstead.Add the base 0 case. Without the preceding fix it fails with EEXIST.
Assisted-by LLMSigned-off-by Karl Mehltretter <[email protected]>
Same nit as before.
Cheers,
/fuad
Quoted 22 lines
--- tools/testing/selftests/kvm/arm64/smccc_filter.c | 4 ++++ 1 file changed, 4 insertions(+)tools/testing/selftests/kvm/arm64/smccc_filter.c +4
-1@@ -140,6 +140,10 @@ static void test_invalid_nr_functions(void)TEST_ASSERT (r <0 && errno ==EINVAL ,"Attempt to filter 0 functions should return EINVAL" ); + r =__set_smccc_filter (vm,0 ,0 ,KVM_SMCCC_FILTER_DENY ); +TEST_ASSERT (r <0 && errno ==EINVAL , +"Attempt to filter 0 functions at base 0 should return EINVAL" ); +kvm_vm_free (vm); } --2.39.5 (Apple Git-154)
Re: [PATCH 2/2] KVM: arm64: selftests: Test empty SMCCC filter range at base 0
In reply to Karl MehltretterQuoted 9 lines · Karl Mehltretter
test_invalid_nr_functions()only checks an empty range atPSCI_0_2_FN64_CPU_ON, which KVM's end < start check happens to catch. It never exercised base 0, where the inclusive end wraps toU32_MAXinstead.Add the base 0 case. Without the preceding fix it fails with EEXIST.
Assisted-by LLMSigned-off-by Karl Mehltretter <[email protected]>
Quoted 8 lines · Karl Mehltretter
KVM_ARM_VM_SMCCC_FILTERrejects an empty range (nr_functions== 0) with-EINVALfor every base except 0.kvm_smccc_set_filter()only rejects a range if its inclusive end, base +nr_functions- 1, is below base; for base 0 that end wraps toU32_MAX, and KVM then tries to insert [0,U32_MAX], which overlaps the reserved Arm Architecture Calls ranges, so the attribute returns-EEXISTinstead of-EINVAL.[...]
Applied to fixes, thanks!
[1/2] KVM: arm64: Return -EINVAL for an empty SMCCC filter range at base 0
https://git.kernel.org/kvmarm/kvmarm/c/64dc6f1db7e6 [2/2] KVM: arm64: selftests: Test empty SMCCC filter range at base 0 https://git.kernel.org/kvmarm/kvmarm/c/0a46eb5719fa
--
Best,
Oliver