linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* usercopy_warn in __copy_to_user
@ 2018-12-19 19:51 rishabhb
  2018-12-19 21:51 ` Kees Cook
  0 siblings, 1 reply; 2+ messages in thread
From: rishabhb @ 2018-12-19 19:51 UTC (permalink / raw)
  To: keescook, linux-kernel, linux-arm-msm, Linux-arm Kernel; +Cc: tsoni, ckadabi


In the 4.19 kernel, we are seeing a USERCOPY_WARN in __copy_to_user 
during bootup.
The code-flow is something like this:

(arch/arm64/kernel/signal.c)
struct sigset_t *set;
__copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set))

(include/linux/uaccess.h)
__copy_to_user(void __user *to, const void *from, unsigned long n)
{
                 might_fault();
                 kasan_check_read(from, n);
                 check_object_size(from, n, true);
                 return raw_copy_to_user(to, from, n);
}

(include/linux/thread_info.h)
static __always_inline void check_object_size(const void *ptr,
                                  unsigned long n, bool to_user)
{
                 if (!__builtin_constant_p(n))
                                 __check_object_size(ptr, n, to_user);
}

Since sizeof(*set) is constant, __builtin_constant_p(n) should return 
true.
But we are seeing that its returning the value as false. Because of 
which
the code goes on to __check_object_size and generates a USERCOPY_WARN
("usercopy: WARN() on slab cache usercopy
region violations").

We are using LLVM clang version 6.0 to compile the kernel and not gcc.
In clang, __builtin_constant_p is evaluated immediately, before inlining
or other optimizations run, gcc evaluates it later.
We believe that maybe causing __builtin_constant_p(n) to return false.
There’s upstream work to change LLVM, so __builtin_constant_p works more
like gcc when optimization is enabled, but its still in progress.


For this scenario is there a way to avoid the warning? Should the code 
be
written in a different to avoid dependency on compiler?

Thanks,
Rishabh

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

* Re: usercopy_warn in __copy_to_user
  2018-12-19 19:51 usercopy_warn in __copy_to_user rishabhb
@ 2018-12-19 21:51 ` Kees Cook
  0 siblings, 0 replies; 2+ messages in thread
From: Kees Cook @ 2018-12-19 21:51 UTC (permalink / raw)
  To: rishabhb; +Cc: LKML, linux-arm-msm, Linux-arm Kernel, tsoni, ckadabi

On Wed, Dec 19, 2018 at 11:51 AM <rishabhb@codeaurora.org> wrote:
>
>
> In the 4.19 kernel, we are seeing a USERCOPY_WARN in __copy_to_user
> during bootup.
> The code-flow is something like this:
>
> (arch/arm64/kernel/signal.c)
> struct sigset_t *set;
> __copy_to_user(&sf->uc.uc_sigmask, set, sizeof(*set))
>
> (include/linux/uaccess.h)
> __copy_to_user(void __user *to, const void *from, unsigned long n)
> {
>                  might_fault();
>                  kasan_check_read(from, n);
>                  check_object_size(from, n, true);
>                  return raw_copy_to_user(to, from, n);
> }
>
> (include/linux/thread_info.h)
> static __always_inline void check_object_size(const void *ptr,
>                                   unsigned long n, bool to_user)
> {
>                  if (!__builtin_constant_p(n))
>                                  __check_object_size(ptr, n, to_user);
> }
>
> Since sizeof(*set) is constant, __builtin_constant_p(n) should return
> true.
> But we are seeing that its returning the value as false. Because of
> which
> the code goes on to __check_object_size and generates a USERCOPY_WARN
> ("usercopy: WARN() on slab cache usercopy
> region violations").
>
> We are using LLVM clang version 6.0 to compile the kernel and not gcc.

This is fixed in the latest LLVM and Clang:

https://github.com/ClangBuiltLinux/linux/issues/7

-Kees

> In clang, __builtin_constant_p is evaluated immediately, before inlining
> or other optimizations run, gcc evaluates it later.
> We believe that maybe causing __builtin_constant_p(n) to return false.
> There’s upstream work to change LLVM, so __builtin_constant_p works more
> like gcc when optimization is enabled, but its still in progress.
>
>
> For this scenario is there a way to avoid the warning? Should the code
> be
> written in a different to avoid dependency on compiler?
>
> Thanks,
> Rishabh



-- 
Kees Cook

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

end of thread, other threads:[~2018-12-19 21:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-19 19:51 usercopy_warn in __copy_to_user rishabhb
2018-12-19 21:51 ` Kees Cook

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).