linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] selftests: sev_migrate_tests: Fix sev_ioctl()
@ 2021-12-07 20:10 Peter Gonda
  2021-12-07 21:24 ` Sean Christopherson
  2021-12-07 22:47 ` Marc Orr
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Gonda @ 2021-12-07 20:10 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Peter Gonda, Paolo Bonzini, Sean Christopherson, Marc Orr

TEST_ASSERT in SEV ioctl was allowing errors because it checked return
value was good OR the FW error code was OK. This TEST_ASSERT should
require both (aka. AND) values are OK. Removes the LAUNCH_START from the
mirror VM because this call correctly fails because mirror VMs cannot
call this command.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Marc Orr <marcorr@google.com>
Signed-off-by: Peter Gonda <pgonda@google.com>
---
 tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
index 29b18d565cf4..8e1b1e737cb1 100644
--- a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
+++ b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
@@ -31,7 +31,7 @@ static void sev_ioctl(int vm_fd, int cmd_id, void *data)
 	int ret;
 
 	ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd);
-	TEST_ASSERT((ret == 0 || cmd.error == SEV_RET_SUCCESS),
+	TEST_ASSERT(ret == 0 && cmd.error == SEV_RET_SUCCESS,
 		    "%d failed: return code: %d, errno: %d, fw error: %d",
 		    cmd_id, ret, errno, cmd.error);
 }
@@ -228,9 +228,6 @@ static void sev_mirror_create(int dst_fd, int src_fd)
 static void test_sev_mirror(bool es)
 {
 	struct kvm_vm *src_vm, *dst_vm;
-	struct kvm_sev_launch_start start = {
-		.policy = es ? SEV_POLICY_ES : 0
-	};
 	int i;
 
 	src_vm = sev_vm_create(es);
@@ -241,7 +238,7 @@ static void test_sev_mirror(bool es)
 	/* Check that we can complete creation of the mirror VM.  */
 	for (i = 0; i < NR_MIGRATE_TEST_VCPUS; ++i)
 		vm_vcpu_add(dst_vm, i);
-	sev_ioctl(dst_vm->fd, KVM_SEV_LAUNCH_START, &start);
+
 	if (es)
 		sev_ioctl(dst_vm->fd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL);
 
-- 
2.34.1.400.ga245620fadb-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests: sev_migrate_tests: Fix sev_ioctl()
  2021-12-07 20:10 [PATCH] selftests: sev_migrate_tests: Fix sev_ioctl() Peter Gonda
@ 2021-12-07 21:24 ` Sean Christopherson
  2021-12-07 21:27   ` Peter Gonda
  2021-12-07 22:47 ` Marc Orr
  1 sibling, 1 reply; 4+ messages in thread
From: Sean Christopherson @ 2021-12-07 21:24 UTC (permalink / raw)
  To: Peter Gonda; +Cc: linux-kernel, kvm, Paolo Bonzini, Marc Orr

On Tue, Dec 07, 2021, Peter Gonda wrote:
> TEST_ASSERT in SEV ioctl was allowing errors because it checked return
> value was good OR the FW error code was OK. This TEST_ASSERT should
> require both (aka. AND) values are OK. Removes the LAUNCH_START from the
> mirror VM because this call correctly fails because mirror VMs cannot
> call this command.

This probably should be two separate patches.  First remove the bogus LAUNCH_START
call, then fix the assert.
 
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Marc Orr <marcorr@google.com>
> Signed-off-by: Peter Gonda <pgonda@google.com>
> ---
>  tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> index 29b18d565cf4..8e1b1e737cb1 100644
> --- a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> +++ b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> @@ -31,7 +31,7 @@ static void sev_ioctl(int vm_fd, int cmd_id, void *data)
>  	int ret;
>  
>  	ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd);
> -	TEST_ASSERT((ret == 0 || cmd.error == SEV_RET_SUCCESS),
> +	TEST_ASSERT(ret == 0 && cmd.error == SEV_RET_SUCCESS,
>  		    "%d failed: return code: %d, errno: %d, fw error: %d",
>  		    cmd_id, ret, errno, cmd.error);

Hmm, reading cmd.error could also consume uninitialized data, e.g. if the ioctl()
fails before getting into the PSP command, the error message will dump garbage.

And theoretically this could get a false negative if the test stack happens to have
'0' for cmd.error and KVM neglects to fill cmd.error when the ioctl() succeeds.

So in additional to fixing the assert itself, I vote we also do:

diff --git a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
index 29b18d565cf4..50132e165a8d 100644
--- a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
+++ b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
@@ -26,6 +26,7 @@ static void sev_ioctl(int vm_fd, int cmd_id, void *data)
        struct kvm_sev_cmd cmd = {
                .id = cmd_id,
                .data = (uint64_t)data,
+               .error = -1u,
                .sev_fd = open_sev_dev_path_or_exit(),
        };
        int ret;

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests: sev_migrate_tests: Fix sev_ioctl()
  2021-12-07 21:24 ` Sean Christopherson
@ 2021-12-07 21:27   ` Peter Gonda
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Gonda @ 2021-12-07 21:27 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: linux-kernel, kvm, Paolo Bonzini, Marc Orr

On Tue, Dec 7, 2021 at 2:25 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Tue, Dec 07, 2021, Peter Gonda wrote:
> > TEST_ASSERT in SEV ioctl was allowing errors because it checked return
> > value was good OR the FW error code was OK. This TEST_ASSERT should
> > require both (aka. AND) values are OK. Removes the LAUNCH_START from the
> > mirror VM because this call correctly fails because mirror VMs cannot
> > call this command.
>
> This probably should be two separate patches.  First remove the bogus LAUNCH_START
> call, then fix the assert.

Thanks Sean. I'll split the patch and add your suggestion to the second one.

>
> > Cc: Paolo Bonzini <pbonzini@redhat.com>
> > Cc: Sean Christopherson <seanjc@google.com>
> > Cc: Marc Orr <marcorr@google.com>
> > Signed-off-by: Peter Gonda <pgonda@google.com>
> > ---
> >  tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c | 7 ++-----
> >  1 file changed, 2 insertions(+), 5 deletions(-)
> >
> > diff --git a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> > index 29b18d565cf4..8e1b1e737cb1 100644
> > --- a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> > +++ b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> > @@ -31,7 +31,7 @@ static void sev_ioctl(int vm_fd, int cmd_id, void *data)
> >       int ret;
> >
> >       ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd);
> > -     TEST_ASSERT((ret == 0 || cmd.error == SEV_RET_SUCCESS),
> > +     TEST_ASSERT(ret == 0 && cmd.error == SEV_RET_SUCCESS,
> >                   "%d failed: return code: %d, errno: %d, fw error: %d",
> >                   cmd_id, ret, errno, cmd.error);
>
> Hmm, reading cmd.error could also consume uninitialized data, e.g. if the ioctl()
> fails before getting into the PSP command, the error message will dump garbage.
>
> And theoretically this could get a false negative if the test stack happens to have
> '0' for cmd.error and KVM neglects to fill cmd.error when the ioctl() succeeds.
>
> So in additional to fixing the assert itself, I vote we also do:
>
> diff --git a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> index 29b18d565cf4..50132e165a8d 100644
> --- a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> +++ b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> @@ -26,6 +26,7 @@ static void sev_ioctl(int vm_fd, int cmd_id, void *data)
>         struct kvm_sev_cmd cmd = {
>                 .id = cmd_id,
>                 .data = (uint64_t)data,
> +               .error = -1u,
>                 .sev_fd = open_sev_dev_path_or_exit(),
>         };
>         int ret;

Good idea will do in the 2/2.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] selftests: sev_migrate_tests: Fix sev_ioctl()
  2021-12-07 20:10 [PATCH] selftests: sev_migrate_tests: Fix sev_ioctl() Peter Gonda
  2021-12-07 21:24 ` Sean Christopherson
@ 2021-12-07 22:47 ` Marc Orr
  1 sibling, 0 replies; 4+ messages in thread
From: Marc Orr @ 2021-12-07 22:47 UTC (permalink / raw)
  To: Peter Gonda; +Cc: linux-kernel, kvm, Paolo Bonzini, Sean Christopherson

On Tue, Dec 7, 2021 at 12:10 PM Peter Gonda <pgonda@google.com> wrote:
>
> TEST_ASSERT in SEV ioctl was allowing errors because it checked return
> value was good OR the FW error code was OK. This TEST_ASSERT should
> require both (aka. AND) values are OK. Removes the LAUNCH_START from the
> mirror VM because this call correctly fails because mirror VMs cannot
> call this command.
>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Marc Orr <marcorr@google.com>
> Signed-off-by: Peter Gonda <pgonda@google.com>
> ---
>  tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> index 29b18d565cf4..8e1b1e737cb1 100644
> --- a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> +++ b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c
> @@ -31,7 +31,7 @@ static void sev_ioctl(int vm_fd, int cmd_id, void *data)
>         int ret;
>
>         ret = ioctl(vm_fd, KVM_MEMORY_ENCRYPT_OP, &cmd);
> -       TEST_ASSERT((ret == 0 || cmd.error == SEV_RET_SUCCESS),
> +       TEST_ASSERT(ret == 0 && cmd.error == SEV_RET_SUCCESS,
>                     "%d failed: return code: %d, errno: %d, fw error: %d",
>                     cmd_id, ret, errno, cmd.error);
>  }
> @@ -228,9 +228,6 @@ static void sev_mirror_create(int dst_fd, int src_fd)
>  static void test_sev_mirror(bool es)
>  {
>         struct kvm_vm *src_vm, *dst_vm;
> -       struct kvm_sev_launch_start start = {
> -               .policy = es ? SEV_POLICY_ES : 0
> -       };
>         int i;
>
>         src_vm = sev_vm_create(es);
> @@ -241,7 +238,7 @@ static void test_sev_mirror(bool es)
>         /* Check that we can complete creation of the mirror VM.  */
>         for (i = 0; i < NR_MIGRATE_TEST_VCPUS; ++i)
>                 vm_vcpu_add(dst_vm, i);
> -       sev_ioctl(dst_vm->fd, KVM_SEV_LAUNCH_START, &start);
> +
>         if (es)
>                 sev_ioctl(dst_vm->fd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL);
>
> --
> 2.34.1.400.ga245620fadb-goog
>

+1 to Sean's feedback.

Otherwise:

Reviewed-by: Marc Orr <marcorr@google.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-12-07 22:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-07 20:10 [PATCH] selftests: sev_migrate_tests: Fix sev_ioctl() Peter Gonda
2021-12-07 21:24 ` Sean Christopherson
2021-12-07 21:27   ` Peter Gonda
2021-12-07 22:47 ` Marc Orr

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).