Skip to contentSkip to conversation

ratio of const vs dynamic usercopy

INFO Kees Cook 4 participants 9 messages Sep 20, 2016 – Sep 21, 2016

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

Hi,

Al had asked me a couple weeks back what the ratio of const vs dynamic usercopying was. With Josh's cleanup and my fix-up to only call the hardened usercopy when non-const, I can actually gather these statistics on a build. It's a bit of a hack (see attached patch that should not go into the tree), but with my not-very-defconfig, it's rougly 2 to 1 const vs dynamic. However, this doesn't take into account the frequency at _runtime_, which maybe could be discovered via perf comparing copy*user() calls to __check_object_size() calls, but I didn't try that. Does someone have perf setup to check this?

$ grep 'warning: call to' build.stderr | wc -l
1505
$ grep 'warning: call to' build.stderr | grep 'const usercopy' | wc -l
998
$ grep 'warning: call to' build.stderr | grep 'dynamic usercopy' | wc -l
507
---
 include/linux/thread_info.h | 8 +++++++-
 mm/usercopy.c               | 4 ++++
 2 files changed, 11 insertions(+), 1 deletion(-)
include/linux/thread_info.h +7 -1
index 2b5b10eed74f..4cae922797e5 100644
@@ -115,14 +115,20 @@ static inline int arch_within_stack_frames(const void * const stack,
 #endif
 
 #ifdef CONFIG_HARDENED_USERCOPY
-extern void __check_object_size(const void *ptr, unsigned long n,
+extern void __compiletime_warning("dynamic usercopy")
+__check_object_size(const void *ptr, unsigned long n,
 					bool to_user);
 
+extern void __compiletime_warning("builtin-const usercopy")
+__skip_check_object_size(void);
+
 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);
+	else
+		__skip_check_object_size();
 }
 #else
 static inline void check_object_size(const void *ptr, unsigned long n,
mm/usercopy.c +4 -0
index 089328f2b920..9969a06f5e25 100644
@@ -275,3 +275,7 @@ report:
 	report_usercopy(ptr, n, to_user, err);
 }
 EXPORT_SYMBOL(__check_object_size);
+
+void __skip_check_object_size(void)
+{ }
+EXPORT_SYMBOL(__skip_check_object_size);
Signature · 6 lines
2.7.4


-- 
Kees Cook
Nexus Security
#1 lore raw
url: https://github.com/0day-ci/linux/commits/Kees-Cook/ratio-of-const-vs-dynamic-usercopy/20160920-130133 config: i386-randconfig-s0-09191616 (attached as .config) compiler: gcc-6 (Debian 6.2.0-3)…

Hi Kees,

[auto build test WARNING on linus/master]
[also build test WARNING on v4.8-rc7 next-20160919]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
[Suggest to use git(>=2.9.0) format-patch --base=<commit> (or --base=auto for convenience) to record what (public, well-known) commit your patch series was built on] [Check https://git-scm.com/docs/git-format-patch for more information]

url: https://github.com/0day-ci/linux/commits/Kees-Cook/ratio-of-const-vs-dynamic-usercopy/20160920-130133
config: i386-randconfig-s0-09191616 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:

        # save the attached .config to linux build tree
        make ARCH=i386 

All warnings (new ones prefixed by >>):

In file included from arch/x86/include/asm/preempt.h:6:0,

                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/mm_types.h:8,
                    from include/linux/kmemcheck.h:4,
                    from include/linux/skbuff.h:18,
                    from include/linux/if_arp.h:26,
                    from net/ipx/af_ipx.c:33:
   In function 'check_object_size',
       inlined from 'copy_to_user' at arch/x86/include/asm/uaccess.h:742:3,
       inlined from 'ipx_getsockopt' at net/ipx/af_ipx.c:1316:2:
>> include/linux/thread_info.h:129:3: warning: call to '__check_object_size' declared with attribute warning: dynamic usercopy
      __check_object_size(ptr, n, to_user);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'check_object_size',
       inlined from 'copy_from_user' at arch/x86/include/asm/uaccess.h:722:3,
       inlined from 'ipxitf_ioctl' at net/ipx/af_ipx.c:1129:3,
       inlined from 'ipx_ioctl' at net/ipx/af_ipx.c:1872:6:
>> include/linux/thread_info.h:131:3: warning: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
      __skip_check_object_size();
      ^~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'check_object_size',
       inlined from 'copy_from_user' at arch/x86/include/asm/uaccess.h:722:3,
       inlined from 'ipxitf_ioctl' at net/ipx/af_ipx.c:1154:3,
       inlined from 'ipx_ioctl' at net/ipx/af_ipx.c:1872:6:
>> include/linux/thread_info.h:131:3: warning: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
      __skip_check_object_size();
      ^~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'check_object_size',
       inlined from 'copy_to_user' at arch/x86/include/asm/uaccess.h:742:3,
       inlined from 'ipxitf_ioctl' at net/ipx/af_ipx.c:1172:3,
       inlined from 'ipx_ioctl' at net/ipx/af_ipx.c:1872:6:
>> include/linux/thread_info.h:131:3: warning: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
      __skip_check_object_size();
      ^~~~~~~~~~~~~~~~~~~~~~~~~~
   In function 'check_object_size',
       inlined from 'copy_to_user' at arch/x86/include/asm/uaccess.h:742:3,
       inlined from 'ipxcfg_get_config_data' at net/ipx/af_ipx.c:105:9,
       inlined from 'ipx_ioctl' at net/ipx/af_ipx.c:1875:6:
>> include/linux/thread_info.h:131:3: warning: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
      __skip_check_object_size();
      ^~~~~~~~~~~~~~~~~~~~~~~~~~
--
   In file included from arch/x86/include/asm/preempt.h:6:0,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/mmzone.h:7,
                    from include/linux/gfp.h:5,
                    from include/linux/slab.h:14,
                    from net/ipx/ipx_route.c:12:
   In function 'check_object_size',
       inlined from 'copy_from_user' at arch/x86/include/asm/uaccess.h:722:3,
       inlined from 'ipxrtr_ioctl' at net/ipx/ipx_route.c:264:2:
>> include/linux/thread_info.h:131:3: warning: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
      __skip_check_object_size();
      ^~~~~~~~~~~~~~~~~~~~~~~~~~

--
In file included from arch/x86/include/asm/preempt.h:6:0,
from include/linux/preempt.h:59,
from include/linux/spinlock.h:50,
from include/linux/seqlock.h:35,
from include/linux/time.h:5,
from include/uapi/linux/timex.h:56,
from include/linux/timex.h:56,
from include/linux/sched.h:19,
from include/linux/uaccess.h:4,
from security/keys/encrypted-keys/encrypted.c:17:
In function 'check_object_size',
inlined from 'copy_to_user' at arch/x86/include/asm/uaccess.h:742:3, inlined from 'encrypted_read' at security/keys/encrypted-keys/encrypted.c:961:2:

>> include/linux/thread_info.h:129:3: warning: call to '__check_object_size' declared with attribute warning: dynamic usercopy
      __check_object_size(ptr, n, to_user);
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/__check_object_size +129 include/linux/thread_info.h

2c4beea2 Kees Cook 2016-09-19  123  __skip_check_object_size(void);
2c4beea2 Kees Cook 2016-09-19  124  
a85d6b82 Kees Cook 2016-09-07  125  static __always_inline void check_object_size(const void *ptr, unsigned long n,
f5509cc1 Kees Cook 2016-06-07  126  					      bool to_user)
f5509cc1 Kees Cook 2016-06-07  127  {
81409e9e Kees Cook 2016-08-31  128  	if (!__builtin_constant_p(n))
f5509cc1 Kees Cook 2016-06-07 @129  		__check_object_size(ptr, n, to_user);
2c4beea2 Kees Cook 2016-09-19  130  	else
2c4beea2 Kees Cook 2016-09-19 @131  		__skip_check_object_size();
f5509cc1 Kees Cook 2016-06-07  132  }
f5509cc1 Kees Cook 2016-06-07  133  #else
f5509cc1 Kees Cook 2016-06-07  134  static inline void check_object_size(const void *ptr, unsigned long n,

:::::: The code at line 129 was first introduced by commit :::::: f5509cc18daa7f82bcc553be70df2117c8eedc16 mm: Hardened usercopy

:::::: TO: Kees Cook <[email protected]>
:::::: CC: Kees Cook <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

author
#2 lore raw
Quoted 4 lines · kbuild test robot

Hi Kees,

[auto build test WARNING on linus/master] [also build test WARNING on v4.8-rc7 next-20160919]

Hah, and here I thought if I didn't mark it with "[PATCH]" 0-day would skip it! ;)

Is there a way to mark "just FYI" patches that go to the list to get skipped by 0-day?

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
build bot
#3 lore raw
The 0-day bot will auto skip emailed patches without "Signed-off-by: ". Will that be enough? I can also add the "[PATCH...]" detect.

Hi Kees,

On Tue, Sep 20, 2016 at 04:17:36PM -0700, Kees Cook wrote:

>On Mon, Sep 19, 2016 at 10:48 PM, kbuild test robot <[email protected]> wrote:
>> Hi Kees,
>>
>> [auto build test WARNING on linus/master]
>> [also build test WARNING on v4.8-rc7 next-20160919]
>
>Hah, and here I thought if I didn't mark it with "[PATCH]" 0-day would
>skip it! ;)
>
>Is there a way to mark "just FYI" patches that go to the list to get
>skipped by 0-day?

The 0-day bot will auto skip emailed patches without "Signed-off-by: ". Will that be enough? I can also add the "[PATCH...]" detect.

Thanks,
Fengguang

author
#4 lore raw
Quoted 15 lines · Fengguang Wu

Hi Kees,

Quoted 12 lines · Kees Cook
Quoted 5 lines · kbuild test robot

Hi Kees,

[auto build test WARNING on linus/master] [also build test WARNING on v4.8-rc7 next-20160919]

Hah, and here I thought if I didn't mark it with "[PATCH]" 0-day would skip it! ;)

Quoted 6 lines
Quoted 2 lines

Is there a way to mark "just FYI" patches that go to the list to get skipped by 0-day?

The 0-day bot will auto skip emailed patches without "Signed-off-by: ". Will that be enough? I can also add the "[PATCH...]" detect.

Ah, perfect, I'll just leave out my S-o-B next time. Thanks!

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#5 lore raw
In reply to Kees Cook
Quoted 11 lines · Kees Cook

Hi,

Al had asked me a couple weeks back what the ratio of const vs dynamic usercopying was. With Josh's cleanup and my fix-up to only call the hardened usercopy when non-const, I can actually gather these statistics on a build. It's a bit of a hack (see attached patch that should not go into the tree), but with my not-very-defconfig, it's rougly 2 to 1 const vs dynamic. However, this doesn't take into account the frequency at _runtime_, which maybe could be discovered via perf comparing copy*user() calls to __check_object_size() calls, but I didn't try that. Does someone have perf setup to check this?

Maybe do something like this with your patch?

trace-cmd record -p function -l __check_object_size -l __skip_check_object_size [command you want to benchmark] trace-cmd report

Signature · 1 line
Josh
author
#6 lore raw
Quoted 13 lines · Josh Poimboeuf
Quoted 11 lines · Kees Cook

Hi,

Al had asked me a couple weeks back what the ratio of const vs dynamic usercopying was. With Josh's cleanup and my fix-up to only call the hardened usercopy when non-const, I can actually gather these statistics on a build. It's a bit of a hack (see attached patch that should not go into the tree), but with my not-very-defconfig, it's rougly 2 to 1 const vs dynamic. However, this doesn't take into account the frequency at _runtime_, which maybe could be discovered via perf comparing copy*user() calls to __check_object_size() calls, but I didn't try that. Does someone have perf setup to check this?

Quoted 4 lines

Maybe do something like this with your patch?

trace-cmd record -p function -l __check_object_size -l __skip_check_object_size [command you want to benchmark] trace-cmd report

trace-cmd seemed to break for me (lost one of the CPU buffers?), so I just did this manually:

# echo __skip_check_object_size > set_ftrace_filter
# echo __check_object_size >> set_ftrace_filter
# echo 1 > function_profile_enabled
... build the kernel 5 times ...

Out of trace_stat/function* I get:

Function Hit Time Avg

         s^2
  --------                               ---    ----            ---
         ---

__check_object_size 7692563 593308.5 us 0.077
us 1104414 us
__skip_check_object_size 3832439 254356.3 us 0.066
us 205958.2 us
Function Hit Time Avg

         s^2
  --------                               ---    ----            ---
         ---

__check_object_size 7474288 564353.9 us 0.075
us 1282606 us
__skip_check_object_size 3809103 261658.7 us 0.068
us 192132.0 us
Function Hit Time Avg

         s^2
  --------                               ---    ----            ---
         ---

__check_object_size 7472437 573517.2 us 0.076
us 4566908 us
__skip_check_object_size 3883037 251919.3 us 0.064
us 3780134 us
Function Hit Time Avg

         s^2
  --------                               ---    ----            ---
         ---

__check_object_size 7882525 581605.9 us 0.073
us 51814941 us
__skip_check_object_size 4061014 277332.0 us 0.068
us 5237258 us

So, together:

30521813 dynamic calls and 15585593 const calls, or almost exactly 2 to 1 dynamic to const. (And the dynamic calls seem to add 0.00875us average overhead vs const.)

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
#7 lore raw
Quoted 8 lines · Kees Cook

trace-cmd seemed to break for me (lost one of the CPU buffers?), so I just did this manually:

# echo __skip_check_object_size > set_ftrace_filter
# echo __check_object_size >> set_ftrace_filter
# echo 1 > function_profile_enabled
... build the kernel 5 times ...

I suspect other loads will give possibly radically different numbers.

I like the kernel build as a benchmark, but at the same time I have to admit that it's fairly specific. It tends to do mostly some fairly simple filesystem stuff.

It would be interesting to see what the most common direct callchains for the object size check is, though. Maybe there's only one or two really common cases (the page cache copies for read/write? I dunno) Maybe if we special-case those, that cuts down on the dynamic cases a lot.

And obviously the reason it would be good to make that size check as uncommon as possible is that obviously once it's not a very common case, that makes it much more valid to enable this all by default and/or make more expensive checks.

Linus

#8 lore raw
In reply to Kees Cook
url: https://github.com/0day-ci/linux/commits/Kees-Cook/ratio-of-const-vs-dynamic-usercopy/20160920-130133 config: x86_64-randconfig-s2-09211158 (attached as .config) compiler: gcc-4.4 (Debian…

Hi Kees,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.8-rc7 next-20160920]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
[Suggest to use git(>=2.9.0) format-patch --base=<commit> (or --base=auto for convenience) to record what (public, well-known) commit your patch series was built on] [Check https://git-scm.com/docs/git-format-patch for more information]

url: https://github.com/0day-ci/linux/commits/Kees-Cook/ratio-of-const-vs-dynamic-usercopy/20160920-130133
config: x86_64-randconfig-s2-09211158 (attached as .config)
compiler: gcc-4.4 (Debian 4.4.7-8) 4.4.7 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

cc1: warnings being treated as errors
In file included from arch/x86/include/asm/preempt.h:6,

                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/seqlock.h:35,
                    from include/linux/time.h:5,
                    from include/linux/stat.h:18,
                    from include/linux/compat.h:12,
                    from drivers/gpu/drm/i915/i915_ioc32.c:32:
   In function 'copy_from_user',
       inlined from 'compat_i915_getparam' at drivers/gpu/drm/i915/i915_ioc32.c:54:
>> include/linux/thread_info.h:131: error: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
--
   cc1: warnings being treated as errors
   In file included from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/wait.h:8,
                    from include/linux/fs.h:5,
                    from include/linux/seq_file.h:10,
                    from drivers/gpu/drm/i915/i915_debugfs.c:29:
   include/linux/thread_info.h: In function 'wm_latency_write':
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   In function 'copy_to_user',
       inlined from 'i915_pipe_crc_read' at drivers/gpu/drm/i915/i915_debugfs.c:3641:
>> include/linux/thread_info.h:131: error: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
   include/linux/thread_info.h: In function 'display_crc_ctl_write':
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   include/linux/thread_info.h: In function 'i915_displayport_test_active_write':
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
--
   cc1: warnings being treated as errors
   In file included from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/rcupdate.h:38,
                    from include/linux/idr.h:18,
                    from include/linux/kernfs.h:14,
                    from include/linux/sysfs.h:15,
                    from include/linux/kobject.h:21,
                    from include/linux/cdev.h:4,
                    from include/drm/drmP.h:36,
                    from drivers/gpu/drm/i915/i915_gem_execbuffer.c:29:
   include/linux/thread_info.h: In function 'i915_gem_execbuffer_relocate_vma':
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   In function 'copy_from_user',
       inlined from 'i915_gem_execbuffer_relocate_slow' at drivers/gpu/drm/i915/i915_gem_execbuffer.c:872:
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   include/linux/thread_info.h: In function 'i915_gem_execbuffer_relocate_slow':
>> include/linux/thread_info.h:131: error: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
   In function 'copy_from_user',
       inlined from 'i915_gem_execbuffer2' at drivers/gpu/drm/i915/i915_gem_execbuffer.c:1776:
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   include/linux/thread_info.h: In function 'i915_gem_execbuffer2':
>> include/linux/thread_info.h:131: error: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
   In function 'copy_from_user',
       inlined from 'i915_gem_execbuffer' at drivers/gpu/drm/i915/i915_gem_execbuffer.c:1688:
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   include/linux/thread_info.h: In function 'i915_gem_execbuffer':
>> include/linux/thread_info.h:131: error: call to '__skip_check_object_size' declared with attribute warning: builtin-const usercopy
--
   cc1: warnings being treated as errors
   In file included from arch/x86/include/asm/preempt.h:6,
                    from include/linux/preempt.h:59,
                    from include/linux/spinlock.h:50,
                    from include/linux/rcupdate.h:38,
                    from include/linux/idr.h:18,
                    from include/linux/kernfs.h:14,
                    from include/linux/sysfs.h:15,
                    from include/linux/kobject.h:21,
                    from include/linux/cdev.h:4,
                    from include/drm/drmP.h:36,
                    from drivers/gpu/drm/i915/i915_gem.c:28:
   include/linux/thread_info.h: In function 'shmem_pread_fast':
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   include/linux/thread_info.h: In function 'shmem_pwrite_slow':
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   include/linux/thread_info.h: In function 'shmem_pread_slow':
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   include/linux/thread_info.h: In function 'shmem_pwrite_fast':
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy
   include/linux/thread_info.h: In function 'i915_gem_phys_pwrite':
>> include/linux/thread_info.h:129: error: call to '__check_object_size' declared with attribute warning: dynamic usercopy

vim +/__skip_check_object_size +131 include/linux/thread_info.h

123 __skip_check_object_size(void);
124
125 static __always_inline void check_object_size(const void *ptr, unsigned long n,
126 bool to_user)
127 {
128 if (!__builtin_constant_p(n))

 > 129			__check_object_size(ptr, n, to_user);
   130		else
 > 131			__skip_check_object_size();

132 }
133 #else
134 static inline void check_object_size(const void *ptr, unsigned long n,

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation