Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Critical] x86/uaccess, vhost: missing access_ok() check in
  set_bit_user()
- [High] x86/uaccess: set_bit_user() uses write-only =r constraint on
  __err
--

commit 508047a5899f1ab9b68b7a3a4c36fffabf6ab858
Author: Michael S. Tsirkin <[email protected]>

vhost: set dirty log bits without pinning pages on x86

This commit introduces a set_bit_user() helper on x86 to atomically set a
bit in user memory using an exception table entry, and uses it in vhost's
set_bit_to_user() to avoid pinning user pages on the dirty-logging path.

Link: https://lore.kernel.org/all/[email protected]/

> diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
> index 3a0dd3c2b233e..ba1e7c3f83c8e 100644
> --- a/arch/x86/include/asm/uaccess.h
> +++ b/arch/x86/include/asm/uaccess.h
> @@ -594,6 +594,24 @@ _label:                                                  
>                 \
>       __ret;                                                          \
>                                                       })
>  
> +/* set_bit() on a byte of user memory; returns -EFAULT on fault. */
> +#define set_bit_user(nr, addr)                                       \
> +({                                                                   \
> +     int __err = 0;                                                  \
> +     __uaccess_begin();                                              \
> +     asm volatile("\n"                                               \
> +                  "1: " LOCK_PREFIX "orb %[mask], %[ptr]\n"          \
> +                  "2:\n"                                             \
> +                  _ASM_EXTABLE_TYPE_REG(1b, 2b, EX_TYPE_EFAULT_REG,  \
> +                                        %[errout])                   \
> +                  : [errout] "=r" (__err),                           \
> +                    [ptr] "+m" (*(u8 __user *)(addr))                \
> +                  : [mask] "q" ((unsigned char)BIT(nr))              \
> +                  : "memory", "cc");                         \
> +     __uaccess_end();                                                \
> +     __err ? -EFAULT : 0;                                            \
> +})

[Severity: Critical]
Does set_bit_user() need an access_ok() check (or user_access_begin() /
scoped_user_rw_access()) before calling __uaccess_begin()?

Previously, set_bit_to_user() called pin_user_pages_fast(), which rejected
kernel addresses.  Not every vhost path validates vq->log_base with
access_ok() before reaching set_bit_to_user(): when vq->iotlb is non-NULL
and vq->log_used is set (with VHOST_F_LOG_ALL unset), vq_log_access_ok()
skips log_access_ok() because vq_log_used_access_ok() returns true early:

drivers/vhost/vhost.c:vq_log_used_access_ok() {
        if (vq->iotlb)
                return true;

        return !log_used || log_access_ok(log_base, log_addr,
                                          vhost_get_used_size(vq, vq->num));
}

When log_used() then runs, it calls log_write_hva() ->
log_write(vq->log_base, ...) -> set_bit_to_user(), allowing userspace to
set bits in arbitrary kernel memory with SMAP disabled.

[Severity: High]
Should the [errout] constraint on __err be "+r" instead of "=r"?

%[errout] is only written by the exception handler when instruction 1b
faults.  With a write-only "=r" constraint, the compiler treats the
initial "__err = 0" assignment as dead and can reuse a register holding a
non-zero input (such as [mask]), causing set_bit_user() to return -EFAULT
even when the atomic orb succeeds and aborting vhost dirty-page logging.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/af7f05bf637337d35fdd10f3e0dbb5e39d7780e7.1794480795.git....@redhat.com?part=1

Reply via email to