All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Amit Machhiwal" <amachhiw@linux.ibm.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v6 4/4] KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl
Date: Tue, 04 Aug 2026 18:27:18 +0000	[thread overview]
Message-ID: <20260804182718.DB3B01F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260804180705.59160-5-amachhiw@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Unconditional rejection of larger structs breaks the forward compatibility mechanism of copy_struct_from_user().
- [High] Overwriting the userspace size field with the kernel's struct size creates dangerous ambiguities on success.
--

--- Patch [4]: [PATCH v6 4/4] KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl ---
commit 5d4e2edcf1688edc6d21e5f2463ef8b58bd3d0a7
Author: Amit Machhiwal <amachhiw@linux.ibm.com>

KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl
    
Add documentation for the KVM_PPC_GET_COMPAT_CAPS ioctl to the KVM API
documentation. The documentation covers error code descriptions including
E2BIG for forward compatibility and the extensible size-based versioning
contract.

> diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst
> index e3003a241d5b0..22fedb0aa34b4 100644
> --- a/Documentation/virt/kvm/api.rst
> +++ b/Documentation/virt/kvm/api.rst
> @@ -6566,6 +6566,85 @@ KVM_S390_KEYOP_SSKE
>    Sets the storage key for the guest address ``guest_addr`` to the key
>    specified in ``key``, returning the previous value in ``key``.
>  
> +4.145 KVM_PPC_GET_COMPAT_CAPS
> +-----------------------------
> +:Capability: KVM_CAP_PPC_COMPAT_CAPS
> +:Architectures: powerpc
> +:Type: vm ioctl
> +:Parameters: struct kvm_ppc_compat_caps (in/out)
> +:Returns: 0 on success, negative value on failure
> +
> +Errors include:
> +
> +  ======== ============================================================
> +  EFAULT   if ``struct kvm_ppc_compat_caps`` cannot be read from or
> +           written to userspace
> +  EINVAL   if the ``size`` field is smaller than
> +           ``KVM_PPC_COMPAT_CAPS_SIZE_VER0``, if the ``flags`` field
> +           is non-zero, or if the backend fails to retrieve or map
> +           CPU compatibility capabilities
> +  E2BIG    if ``size`` is larger than the kernel's struct size
> +           (new userspace on old kernel); the kernel writes back its
> +           own struct size into the ``size`` field so userspace can
> +           retry with the correct size

[Severity: High]
Does explicitly rejecting a larger struct size with -E2BIG break the standard
forward compatibility mechanism of copy_struct_from_user()?

Extensible struct UAPIs are designed to allow newer userspace to run seamlessly
on older kernels as long as new fields are zeroed. 

Looking at arch/powerpc/kvm/powerpc.c in kvm_arch_vm_ioctl(), the code
short-circuits the native trailing-zero check in copy_struct_from_user():

		if (usize > sizeof(host_caps)) {
			host_caps.size = sizeof(host_caps);
			r = -EFAULT;
			if (put_user(host_caps.size, (__u64 __user *)argp))
				goto out;
			r = -E2BIG;
			goto out;
		}

This forces userspace to implement fallback retry loops instead of relying on
the kernel's native UAPI extensibility.

[ ... ]

> +Before calling this ioctl, userspace must set the ``size`` field to
> +``sizeof(struct kvm_ppc_compat_caps)`` and zero the ``flags`` field.
> +The kernel rejects non-zero ``flags`` with ``-EINVAL`` to prevent
> +uninitialized stack values from being silently accepted, keeping the
> +field available for future use without ABI ambiguity.
> +
> +The ioctl uses ``copy_struct_from_user()`` and ``copy_struct_to_user()``
> +to support extensible versioning: if userspace passes a struct smaller
> +than the current kernel version (``size >= KVM_PPC_COMPAT_CAPS_SIZE_VER0``),
> +the kernel zero-pads unknown trailing fields. If userspace passes a larger
> +struct (``size > sizeof(struct kvm_ppc_compat_caps)``), the kernel writes
> +back its own struct size into the ``size`` field and returns ``-E2BIG``,
> +allowing userspace to discover the kernel's struct size and retry.

[Severity: High]
Can overwriting the userspace size field with the kernel's struct size create
dangerous ambiguities on success?

If userspace passes a smaller struct (e.g., 24 bytes) and the kernel uses a
larger struct (e.g., 32 bytes), the kernel updates host_caps.size to 32 and
copies 24 bytes back to userspace.

Looking at arch/powerpc/kvm/powerpc.c in kvm_arch_vm_ioctl():

		host_caps.size = sizeof(host_caps);
		r = copy_struct_to_user(argp, usize, &host_caps,
					sizeof(host_caps), NULL);

Userspace would receive a success code (0) but its size field is now 32. This
falsely indicates to userspace that the kernel provided 32 bytes of valid data,
which can lead to out-of-bounds reads if userspace trusts the returned size
field to determine valid data bounds within its buffer.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260804180705.59160-1-amachhiw@linux.ibm.com?part=4

  reply	other threads:[~2026-08-04 18:27 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-04 18:07 [PATCH v6 0/4] KVM: PPC: Expose CPU compatibility modes for nested guests Amit Machhiwal
2026-08-04 18:07 ` [PATCH v6 1/4] KVM: PPC: Introduce KVM_CAP_PPC_COMPAT_CAPS and wire up ioctl Amit Machhiwal
2026-08-04 18:19   ` sashiko-bot
2026-08-06 16:07     ` Amit Machhiwal
2026-08-04 18:07 ` [PATCH v6 2/4] KVM: PPC: Book3S HV: Implement compat CPU capability retrieval for KVM on PowerVM Amit Machhiwal
2026-08-04 18:21   ` sashiko-bot
2026-08-06 16:09     ` Amit Machhiwal
2026-08-04 18:07 ` [PATCH v6 3/4] KVM: PPC: Book3S HV: Add support for compat CPU capabilities for KVM on PowerNV Amit Machhiwal
2026-08-04 18:27   ` sashiko-bot
2026-08-06 16:13     ` Amit Machhiwal
2026-08-04 18:07 ` [PATCH v6 4/4] KVM: PPC: Document KVM_PPC_GET_COMPAT_CAPS ioctl Amit Machhiwal
2026-08-04 18:27   ` sashiko-bot [this message]
2026-08-06 16:15     ` Amit Machhiwal
2026-08-05  4:32 ` [PATCH v6 0/4] KVM: PPC: Expose CPU compatibility modes for nested guests Anushree Mathur
2026-08-05 18:39 ` Ritesh Harjani
2026-08-06  5:33   ` Amit Machhiwal
2026-08-06 13:05     ` Ritesh Harjani
2026-08-06 14:58       ` Amit Machhiwal

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=20260804182718.DB3B01F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=amachhiw@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.