linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] copy_{to,from}_user(): fix compile-time sanity checks with gcc -Og
@ 2018-11-02 15:56 Arnd Bergmann
  2018-11-03  0:03 ` Changbin Du
  0 siblings, 1 reply; 3+ messages in thread
From: Arnd Bergmann @ 2018-11-02 15:56 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Masahiro Yamada, Al Viro, Arnd Bergmann, Changbin Du,
	Marc Zyngier, Alex Bennée, Dave Martin, Kees Cook, Levin,
	Alexander (Sasha Levin),
	linux-kernel

When CONFIG_CC_OPTIMIZE_FOR_DEBUGGING is set, we get countless warnings
like

In function 'check_copy_size',
    inlined from 'copy_from_user' at include/linux/uaccess.h:146:6,
    inlined from '__copy_siginfo_from_user' at kernel/signal.c:3032:6:
include/linux/thread_info.h:147:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small

It seems that constant propagation doesn't work well enough to make
this code reliable, so turn it off for that configuration.

Cc: Changbin Du<changbin.du@gmail.com>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 include/linux/thread_info.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
index 8d8821b3689a..762f379bdf5d 100644
--- a/include/linux/thread_info.h
+++ b/include/linux/thread_info.h
@@ -138,6 +138,11 @@ static __always_inline bool
 check_copy_size(const void *addr, size_t bytes, bool is_source)
 {
 	int sz = __compiletime_object_size(addr);
+
+	/* constant propagation doesn't work well with -Og */
+	if (IS_ENABLED(CONFIG_CC_OPTIMIZE_FOR_DEBUGGING))
+		return true;
+
 	if (unlikely(sz >= 0 && sz < bytes)) {
 		if (!__builtin_constant_p(bytes))
 			copy_overflow(sz, bytes);
-- 
2.18.0


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

* Re: [PATCH] copy_{to,from}_user(): fix compile-time sanity checks with gcc -Og
  2018-11-02 15:56 [PATCH] copy_{to,from}_user(): fix compile-time sanity checks with gcc -Og Arnd Bergmann
@ 2018-11-03  0:03 ` Changbin Du
  2018-11-03 15:46   ` Arnd Bergmann
  0 siblings, 1 reply; 3+ messages in thread
From: Changbin Du @ 2018-11-03  0:03 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Andrew Morton, Masahiro Yamada, Al Viro, Changbin Du,
	Marc Zyngier, Alex Bennée, Dave Martin, Kees Cook, Levin,
	Alexander (Sasha Levin),
	linux-kernel

On Fri, Nov 02, 2018 at 04:56:58PM +0100, Arnd Bergmann wrote:
> When CONFIG_CC_OPTIMIZE_FOR_DEBUGGING is set, we get countless warnings
> like
> 
> In function 'check_copy_size',
>     inlined from 'copy_from_user' at include/linux/uaccess.h:146:6,
>     inlined from '__copy_siginfo_from_user' at kernel/signal.c:3032:6:
> include/linux/thread_info.h:147:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small
> 
> It seems that constant propagation doesn't work well enough to make
> this code reliable, so turn it off for that configuration.
> 
This is caused by __compiletime_warning() and fixed by below change
already. Could you try the latest kbuild tree?

--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -86,8 +86,10 @@
 #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)

 #ifndef __CHECKER__
+#ifndef CONFIG_CC_OPTIMIZE_FOR_DEBUGGING
 #define __compiletime_warning(message) __attribute__((warning(message)))
 #define __compiletime_error(message) __attribute__((error(message)))
+#endif

> Cc: Changbin Du<changbin.du@gmail.com>
> Cc: Al Viro <viro@zeniv.linux.org.uk>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
> ---
>  include/linux/thread_info.h | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h
> index 8d8821b3689a..762f379bdf5d 100644
> --- a/include/linux/thread_info.h
> +++ b/include/linux/thread_info.h
> @@ -138,6 +138,11 @@ static __always_inline bool
>  check_copy_size(const void *addr, size_t bytes, bool is_source)
>  {
>  	int sz = __compiletime_object_size(addr);
> +
> +	/* constant propagation doesn't work well with -Og */
> +	if (IS_ENABLED(CONFIG_CC_OPTIMIZE_FOR_DEBUGGING))
> +		return true;
> +
>  	if (unlikely(sz >= 0 && sz < bytes)) {
>  		if (!__builtin_constant_p(bytes))
>  			copy_overflow(sz, bytes);
> -- 
> 2.18.0
> 

-- 
Thanks,
Changbin Du

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

* Re: [PATCH] copy_{to,from}_user(): fix compile-time sanity checks with gcc -Og
  2018-11-03  0:03 ` Changbin Du
@ 2018-11-03 15:46   ` Arnd Bergmann
  0 siblings, 0 replies; 3+ messages in thread
From: Arnd Bergmann @ 2018-11-03 15:46 UTC (permalink / raw)
  To: Changbin Du
  Cc: Andrew Morton, Masahiro Yamada, Al Viro, Marc Zyngier,
	Alex Bennée, Dave Martin, Kees Cook, Levin,
	Alexander (Sasha Levin),
	linux-kernel

On 11/3/18, Changbin Du <changbin.du@gmail.com> wrote:
> On Fri, Nov 02, 2018 at 04:56:58PM +0100, Arnd Bergmann wrote:
>> When CONFIG_CC_OPTIMIZE_FOR_DEBUGGING is set, we get countless warnings
>> like
>>
>> In function 'check_copy_size',
>>     inlined from 'copy_from_user' at include/linux/uaccess.h:146:6,
>>     inlined from '__copy_siginfo_from_user' at kernel/signal.c:3032:6:
>> include/linux/thread_info.h:147:4: error: call to '__bad_copy_to' declared
>> with attribute error: copy destination size is too small
>>
>> It seems that constant propagation doesn't work well enough to make
>> this code reliable, so turn it off for that configuration.
>>
> This is caused by __compiletime_warning() and fixed by below change
> already. Could you try the latest kbuild tree?
>
> --- a/include/linux/compiler-gcc.h
> +++ b/include/linux/compiler-gcc.h
> @@ -86,8 +86,10 @@
>  #define __compiletime_object_size(obj) __builtin_object_size(obj, 0)
>
>  #ifndef __CHECKER__
> +#ifndef CONFIG_CC_OPTIMIZE_FOR_DEBUGGING
>  #define __compiletime_warning(message) __attribute__((warning(message)))
>  #define __compiletime_error(message) __attribute__((error(message)))
> +#endif

Right, that works as well, but my version is a bit less invasive as it
only disables the __compiletime_error in the one file that introduced
the regression, and not all of them. I have built hundreds of randconfig
builds and found no other problem with __compiletime_error(), though
I did find a bunch of (correct) section mismatch warnings and
false-positive -Wformat-overflow= warnings with
CONFIG_CC_OPTIMIZE_FOR_DEBUGGING (patches pending).

I also got some link errors for which I already posted patches.

       Arnd

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

end of thread, other threads:[~2018-11-03 15:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-02 15:56 [PATCH] copy_{to,from}_user(): fix compile-time sanity checks with gcc -Og Arnd Bergmann
2018-11-03  0:03 ` Changbin Du
2018-11-03 15:46   ` Arnd Bergmann

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