Add __alloc_size()
Subsystems (11)
v3 cover Add __alloc_size()
Hi Andrew,
This is a refresh of the __alloc_size series you have in -mm currently. This addresses the issues[1] Linus had with attribute location and the redundant use of __malloc. These are meant to replace the following patches:
compiler-attributes-add-__alloc_size-for-better-bounds-checking.patch compiler-attributes-add-__alloc_size-for-better-bounds-checking-fix.patch checkpatch-add-__alloc_size-to-known-attribute.patch slab-clean-up-function-declarations.patch slab-add-__alloc_size-attributes-for-better-bounds-checking.patch mm-page_alloc-add-__alloc_size-attributes-for-better-bounds-checking.patch percpu-add-__alloc_size-attributes-for-better-bounds-checking.patch mm-vmalloc-add-__alloc_size-attributes-for-better-bounds-checking.patch rapidio-avoid-bogus-__alloc_size-warning.patch
Thanks!
-Kees
v3:
- move attribute logic around to better handle GCC's weird behavior
- merge __malloc into the __alloc_size macro (Linus)
- refactor attribute positions (Linus)
v2: https://lore.kernel.org/lkml/[email protected]
v1: https://lore.kernel.org/lkml/[email protected]
Original cover letter:
GCC and Clang both use the "alloc_size" attribute to assist with bounds checking around the use of allocation functions. Add the attribute, adjust the Makefile to silence needless warnings, and add the hints to the allocators where possible. These changes have been in use for a while now in GrapheneOS.
Kees Cook (8):
rapidio: Avoid bogus __alloc_size warning
Compiler Attributes: add __alloc_size() for better bounds checking
slab: Clean up function prototypes
slab: Add __alloc_size attributes for better bounds checking mm/kvmalloc: Add __alloc_size attributes for better bounds checking mm/vmalloc: Add __alloc_size attributes for better bounds checking mm/page_alloc: Add __alloc_size attributes for better bounds checking
percpu: Add __alloc_size attributes for better bounds checking
Makefile | 15 ++++ drivers/rapidio/devices/rio_mport_cdev.c | 9 ++- include/linux/compiler-gcc.h | 8 ++ include/linux/compiler_attributes.h | 10 +++ include/linux/compiler_types.h | 12 +++ include/linux/gfp.h | 4 +- include/linux/mm.h | 16 ++-- include/linux/percpu.h | 6 +- include/linux/slab.h | 99 +++++++++++++----------- include/linux/vmalloc.h | 22 +++--- scripts/checkpatch.pl | 3 +- 11 files changed, 128 insertions(+), 76 deletions(-)
Signature · 1 line
2.30.2
PATCH v3 1/8 rapidio: Avoid bogus __alloc_size warning
After adding __alloc_size attributes to the allocators, GCC 9.3 (but not later) may incorrectly evaluate the arguments to check_copy_size(), getting seemingly confused by the size being returned from array_size(). Instead, perform the calculation once, which both makes the code more readable and avoids the bug in GCC.
In file included from arch/x86/include/asm/preempt.h:7,
from include/linux/preempt.h:78,
from include/linux/spinlock.h:55,
from include/linux/mm_types.h:9,
from include/linux/buildid.h:5,
from include/linux/module.h:14,
from drivers/rapidio/devices/rio_mport_cdev.c:13:
In function 'check_copy_size',
inlined from 'copy_from_user' at include/linux/uaccess.h:191:6,
inlined from 'rio_mport_transfer_ioctl' at drivers/rapidio/devices/rio_mport_cdev.c:983:6:
include/linux/thread_info.h:213:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small
213 | __bad_copy_to();
| ^~~~~~~~~~~~~~~But the allocation size and the copy size are identical:
transfer = vmalloc(array_size(sizeof(*transfer), transaction.count));
if (!transfer)
return -ENOMEM;
if (unlikely(copy_from_user(transfer,
(void __user *)(uintptr_t)transaction.block,
array_size(sizeof(*transfer), transaction.count)))) {--- drivers/rapidio/devices/rio_mport_cdev.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/rapidio/devices/rio_mport_cdev.c +5 -4
@@ -965,6 +965,7 @@ static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg)struct rio_transfer_io *transfer;enum dma_data_direction dir;int i, ret =0 ; +size_t size;if (unlikely (copy_from_user (&transaction, arg,sizeof (transaction))))return -EFAULT ;
@@ -976,13 +977,14 @@ static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg) priv->md->properties.transfer_mode) == 0)return -ENODEV ; - transfer =vmalloc (array_size (sizeof (*transfer), transaction.count)); + size =array_size (sizeof (*transfer), transaction.count); + transfer =vmalloc (size);if (!transfer)return -ENOMEM ;if (unlikely (copy_from_user (transfer, (void __user *)(uintptr_t )transaction.block, -array_size (sizeof (*transfer), transaction.count)))) { + size))) { ret = -EFAULT ; goto out_free; }
@@ -994,8 +996,7 @@ static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg) transaction.sync, dir, &transfer[i]);if (unlikely (copy_to_user ((void __user *)(uintptr_t )transaction.block, - transfer, -array_size (sizeof (*transfer), transaction.count)))) + transfer, size))) ret = -EFAULT ; out_free:
Signature · 1 line
2.30.2
Re: [PATCH v3 1/8] rapidio: Avoid bogus __alloc_size warning
Quoted 30 lines · Kees Cook
After adding
__alloc_sizeattributes to the allocators, GCC 9.3 (but not later) may incorrectly evaluate the arguments tocheck_copy_size(), getting seemingly confused by the size being returned fromarray_size(). Instead, perform the calculation once, which both makes the code more readable and avoids the bug in GCC.In file included from
arch/x86/include/asm/preempt.h:7,
frominclude/linux/preempt.h:78,
frominclude/linux/spinlock.h:55,
frominclude/linux/mm_types.h:9,
frominclude/linux/buildid.h:5,
frominclude/linux/module.h:14,
fromdrivers/rapidio/devices/rio_mport_cdev.c:13:
In function 'check_copy_size',
inlined from 'copy_from_user' atinclude/linux/uaccess.h:191:6,
inlined from 'rio_mport_transfer_ioctl' atdrivers/rapidio/devices/rio_mport_cdev.c:983:6:include/linux/thread_info.h:213:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small 213 | __bad_copy_to(); | ^~~~~~~~~~~~~~~But the allocation size and the copy size are identical:
transfer = vmalloc(array_size(sizeof(*transfer), transaction.count)); if (!transfer) return -ENOMEM; if (unlikely(copy_from_user(transfer, (void __user *)(uintptr_t)transaction.block, array_size(sizeof(*transfer), transaction.count)))) {
Quoted 10 lines
Reported-by kernel test robot <[email protected]>Cc Matt Porter <[email protected]>Cc Alexandre Bounine <[email protected]>Cc Jing Xiangfeng <[email protected]>Cc Ira Weiny <[email protected]>Cc Souptick Joarder <[email protected]>Cc Gustavo A. R. Silva <[email protected]>Signed-off-by Kees Cook <[email protected]>Reviewed-by John Hubbard <[email protected]>
Thanks
--
Gustavo
Quoted 46 lines
--- drivers/rapidio/devices/rio_mport_cdev.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)drivers/rapidio/devices/rio_mport_cdev.c +5
-4@@ -965,6 +965,7 @@ static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg)struct rio_transfer_io *transfer;enum dma_data_direction dir;int i, ret =0 ; +size_t size;if (unlikely (copy_from_user (&transaction, arg,sizeof (transaction))))return -EFAULT ;@@ -976,13 +977,14 @@ static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg) priv->md->properties.transfer_mode) == 0)return -ENODEV ; - transfer =vmalloc (array_size (sizeof (*transfer), transaction.count)); + size =array_size (sizeof (*transfer), transaction.count); + transfer =vmalloc (size);if (!transfer)return -ENOMEM ;if (unlikely (copy_from_user (transfer, (void __user *)(uintptr_t )transaction.block, -array_size (sizeof (*transfer), transaction.count)))) { + size))) { ret = -EFAULT ; goto out_free; }@@ -994,8 +996,7 @@ static int rio_mport_transfer_ioctl(struct file *filp, void __user *arg) transaction.sync, dir, &transfer[i]);if (unlikely (copy_to_user ((void __user *)(uintptr_t )transaction.block, - transfer, -array_size (sizeof (*transfer), transaction.count)))) + transfer, size))) ret = -EFAULT ; out_free:Signature · 1 line
2.30.2
PATCH v3 2/8 Compiler Attributes: add __alloc_size() for better bounds checking
In reply to Kees CookGCC and Clang can use the "alloc_size" attribute to better inform the results of __builtin_object_size() (for compile-time constant values). Clang can additionally use alloc_size to inform the results of __builtin_dynamic_object_size() (for run-time values).
Because GCC sees the frequent use of struct_size() as an allocator size argument, and notices it can return SIZE_MAX (the overflow indication), it complains about these call sites overflowing (since SIZE_MAX is greater than the default -Walloc-size-larger-than=PTRDIFF_MAX). This isn't helpful since we already know a SIZE_MAX will be caught at run-time (this was an intentional design). To deal with this, we must disable this check as it is both a false positive and redundant. (Clang does not have this warning option.)
Unfortunately, just checking the -Wno-alloc-size-larger-than is not sufficient to make the __alloc_size attribute behave correctly under older GCC versions. The attribute itself must be disabled in those situations too, as there appears to be no way to reliably silence the SIZE_MAX constant expression cases for GCC versions less than 9.1:
In file included from ./include/linux/resource_ext.h:11,
from ./include/linux/pci.h:40,
from drivers/net/ethernet/intel/ixgbe/ixgbe.h:9,
from drivers/net/ethernet/intel/ixgbe/ixgbe_lib.c:4:
In function 'kmalloc_node',
inlined from 'ixgbe_alloc_q_vector' at ./include/linux/slab.h:743:9:
./include/linux/slab.h:618:9: error: argument 1 value '18446744073709551615' exceeds maximum object size 9223372036854775807 [-Werror=alloc-size-larger-than=]
return __kmalloc_node(size, flags, node);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./include/linux/slab.h: In function 'ixgbe_alloc_q_vector':
./include/linux/slab.h:455:7: note: in a call to allocation function '__kmalloc_node' declared here
void *__kmalloc_node(size_t size, gfp_t flags, int node) __assume_slab_alignment __malloc;
^~~~~~~~~~~~~~Specifically:
-Wno-alloc-size-larger-than is not correctly handled by GCC < 9.1
https://godbolt.org/z/hqsfG7q84 (doesn't disable)
https://godbolt.org/z/P9jdrPTYh (doesn't admit to not knowing about option)
https://godbolt.org/z/465TPMWKb (only warns when other warnings appear)
-Walloc-size-larger-than=18446744073709551615 is not handled by GCC < 8.2
https://godbolt.org/z/73hh1EPxz (ignores numeric value)
Since anything marked with __alloc_size would also qualify for marking with __malloc, just include __malloc along with it to avoid redundant markings. (Suggested by Linus Torvalds.)
Finally, make sure checkpatch.pl doesn't get confused about finding the __alloc_size attribute on functions. (Thanks to Joe Perches.)
--- Makefile | 15 +++++++++++++++ include/linux/compiler-gcc.h | 8 ++++++++ include/linux/compiler_attributes.h | 10 ++++++++++ include/linux/compiler_types.h | 12 ++++++++++++ scripts/checkpatch.pl | 3 ++- 5 files changed, 47 insertions(+), 1 deletion(-)
Makefile +15 -0
@@ -1008,6 +1008,21 @@ ifdef CONFIG_CC_IS_GCC KBUILD_CFLAGS += -Wno-maybe-uninitialized endif +ifdef CONFIG_CC_IS_GCC +# The allocators already balk at large sizes, so silence the compiler +# warnings for bounds checks involving those possible values. While +# -Wno-alloc-size-larger-than would normally be used here, earlier versions +# of gcc (<9.1) weirdly don't handle the option correctly when _other_ +# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX +# doesn't work (as it is documented to), silently resolving to "0" prior to +# version 9.1 (and producing an error more recently). Numeric values larger +# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently +# ignored, continuing to default to PTRDIFF_MAX. So, left with no other +# choice, we must perform a versioned check to disable this warning. +# https://lore.kernel.org/lkml/[email protected] +KBUILD_CFLAGS += $(call cc-ifversion, -ge, 0901, -Wno-alloc-size-larger-than) +endif + # disable invalid "can't wrap" optimizations for signed / pointers KBUILD_CFLAGS += -fno-strict-overflow
include/linux/compiler-gcc.h +8 -0
@@ -144,3 +144,11 @@#else #define __diag_GCC_8 (s)#endif + +/* +* Prior to 9.1, -Wno-alloc-size-larger-than (and therefore the "alloc_size" +* attribute) do not work, and must be disabled. +*/ +#if GCC_VERSION <90100 +#undef __alloc_size__ +#endif
include/linux/compiler_attributes.h +10 -0
@@ -33,6 +33,15 @@#define __aligned (x) __attribute__((__aligned__(x)))#define __aligned_largest __attribute__((__aligned__)) +/* +* Note: do not use this directly. Instead, use __alloc_size() since it is conditionally +* available and includes other attributes. +* +* gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alloc_005fsize-function-attribute +* clang: https://clang.llvm.org/docs/AttributeReference.html#alloc-size +*/ +#define __alloc_size__ (x, ...) __attribute__((__alloc_size__(x, ## __VA_ARGS__))) +/* * Note: users of __always_inline currently do not write "inline" themselves, * which seems to be required by gcc to apply the attribute according
@@ -153,6 +162,7 @@/* * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-malloc-function-attribute +* clang: https://clang.llvm.org/docs/AttributeReference.html#malloc */ #define __malloc __attribute__((__malloc__))
include/linux/compiler_types.h +12 -0
@@ -250,6 +250,18 @@ struct ftrace_likely_data {# define __cficanonical#endif +/* +* Any place that could be marked with the "alloc_size" attribute is also +* a place to be marked with the "malloc" attribute. Do this as part of the +* __alloc_size macro to avoid redundant attributes and to avoid missing a +* __malloc marking. +*/ +#ifdef __alloc_size__ +# define __alloc_size (x, ...) __alloc_size__(x, ## __VA_ARGS__) __malloc +#else +# define __alloc_size (x, ...) __malloc +#endif +#ifndef asm_volatile_goto #define asm_volatile_goto(x...) asm goto(x)#endif
scripts/checkpatch.pl +2 -1
@@ -489,7 +489,8 @@ our $Attribute = qr{ ____cacheline_aligned| ____cacheline_aligned_in_smp| ____cacheline_internodealigned_in_smp| - __weak + __weak| + __alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\) }x; our $Modifier; our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Signature · 1 line
2.30.2
Re: [PATCH v3 2/8] Compiler Attributes: add __alloc_size() for better bounds checking
Quoted 5 lines · Kees Cook
Makefile +4
-0+ifdef CONFIG_CC_IS_GCC +# The allocators already balk at large sizes, so silence the compiler +# warnings for bounds checks involving those possible values. While +# -Wno-alloc-size-larger-than would normally be used here, earlier versions
Makefile ifdef CONFIG_CC_IS_GCC · line 1024
Quoted 10 lines
Makefile +10
-0+# of gcc (<9.1) weirdly don't handle the option correctly when _other_ +# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX +# doesn't work (as it is documented to), silently resolving to "0" prior to +# version 9.1 (and producing an error more recently). Numeric values larger +# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently +# ignored, continuing to default to PTRDIFF_MAX. So, left with no other +# choice, we must perform a versioned check to disable this warning. +# https://lore.kernel.org/lkml/[email protected] +KBUILD_CFLAGS += $(call cc-ifversion, -ge, 0901, -Wno-alloc-size-larger-than) +endif
An amazing journey!
Cheers,
Miguel
PATCH v3 3/8 slab: Clean up function prototypes
In reply to Kees CookBased on feedback from Joe Perches and Linus Torvalds, regularize the slab function prototypes before making attribute changes.
--- include/linux/slab.h | 68 ++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 34 deletions(-)
include/linux/slab.h +34 -34
@@ -152,8 +152,8 @@ struct kmem_cache *kmem_cache_create_usercopy(const char *name,slab_flags_t flags, unsigned int useroffset, unsigned int usersize, void (*ctor)(void *)); -void kmem_cache_destroy (struct kmem_cache *); -int kmem_cache_shrink (struct kmem_cache *); +void kmem_cache_destroy (struct kmem_cache *s); +int kmem_cache_shrink (struct kmem_cache *s);/* * Please use this macro to create slab caches. Simply specify the
@@ -181,11 +181,11 @@ int kmem_cache_shrink(struct kmem_cache *);/* * Common kmalloc functions provided by all allocators */ -void *__must_check krealloc (const void *,size_t ,gfp_t ); -void kfree (const void *); -void kfree_sensitive (const void *); -size_t __ksize (const void *); -size_t ksize (const void *); +void *__must_check krealloc (const void *objp,size_t new_size,gfp_t flags); +void kfree (const void *objp); +void kfree_sensitive (const void *objp); +size_t __ksize (const void *objp); +size_t ksize (const void *objp);#ifdef CONFIG_PRINTK bool kmem_valid_obj (void *object);void kmem_dump_obj (void *object);
@@ -426,8 +426,8 @@ static __always_inline unsigned int __kmalloc_index(size_t size,#endif /* !CONFIG_SLOB */ void *__kmalloc (size_t size,gfp_t flags) __assume_kmalloc_alignment __malloc; -void *kmem_cache_alloc (struct kmem_cache *,gfp_t flags) __assume_slab_alignment __malloc; -void kmem_cache_free (struct kmem_cache *,void *); +void *kmem_cache_alloc (struct kmem_cache *s,gfp_t flags) __assume_slab_alignment __malloc; +void kmem_cache_free (struct kmem_cache *s,void *objp);/* * Bulk allocation and freeing operations. These are accelerated in an
@@ -436,8 +436,8 @@ void kmem_cache_free(struct kmem_cache *, void *);* * Note that interrupts must be enabled when calling these functions. */ -void kmem_cache_free_bulk (struct kmem_cache *,size_t ,void **); -int kmem_cache_alloc_bulk (struct kmem_cache *,gfp_t ,size_t ,void **); +void kmem_cache_free_bulk (struct kmem_cache *s,size_t size,void **p); +int kmem_cache_alloc_bulk (struct kmem_cache *s,gfp_t flags,size_t size,void **p);/* * Caller must not use kfree_bulk() on memory not originally allocated
@@ -450,7 +450,8 @@ static __always_inline void kfree_bulk(size_t size, void **p)#ifdef CONFIG_NUMA void *__kmalloc_node (size_t size,gfp_t flags,int node) __assume_kmalloc_alignment __malloc; -void *kmem_cache_alloc_node (struct kmem_cache *,gfp_t flags,int node) __assume_slab_alignment __malloc; +void *kmem_cache_alloc_node (struct kmem_cache *s,gfp_t flags,int node) __assume_slab_alignment + __malloc;#else static __always_inline void *__kmalloc_node(size_t size, gfp_t flags, int node) {
@@ -464,25 +465,24 @@ static __always_inline void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t f#endif #ifdef CONFIG_TRACING -extern void *kmem_cache_alloc_trace (struct kmem_cache *,gfp_t ,size_t ) __assume_slab_alignment __malloc; +extern void *kmem_cache_alloc_trace (struct kmem_cache *s,gfp_t flags,size_t size) + __assume_slab_alignment __malloc;#ifdef CONFIG_NUMA -extern void *kmem_cache_alloc_node_trace (struct kmem_cache *s, -gfp_t gfpflags, -int node,size_t size) __assume_slab_alignment __malloc; +extern void *kmem_cache_alloc_node_trace (struct kmem_cache *s,gfp_t gfpflags, +int node,size_t size) __assume_slab_alignment __malloc;#else -static __always_inline void * -kmem_cache_alloc_node_trace (struct kmem_cache *s, -gfp_t gfpflags, -int node,size_t size) +static __always_inline void *kmem_cache_alloc_node_trace(struct kmem_cache *s, +gfp_t gfpflags,int node, +size_t size) {return kmem_cache_alloc_trace (s, gfpflags, size); }#endif /* CONFIG_NUMA */ #else /* CONFIG_TRACING */ -static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s, -gfp_t flags,size_t size) +static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t flags, +size_t size) {void *ret =kmem_cache_alloc (s, flags);
@@ -490,10 +490,8 @@ static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s,return ret; } -static __always_inline void * -kmem_cache_alloc_node_trace (struct kmem_cache *s, -gfp_t gfpflags, -int node,size_t size) +static __always_inline void *kmem_cache_alloc_node_trace(struct kmem_cache *s, gfp_t gfpflags, +int node,size_t size) {void *ret =kmem_cache_alloc_node (s, gfpflags, node);
@@ -502,13 +500,14 @@ kmem_cache_alloc_node_trace(struct kmem_cache *s, }#endif /* CONFIG_TRACING */ -extern void *kmalloc_order (size_t size,gfp_t flags,unsigned int order) __assume_page_alignment __malloc; +extern void *kmalloc_order (size_t size,gfp_t flags,unsigned int order) __assume_page_alignment + __malloc;#ifdef CONFIG_TRACING -extern void *kmalloc_order_trace (size_t size,gfp_t flags,unsigned int order) __assume_page_alignment __malloc; +extern void *kmalloc_order_trace (size_t size,gfp_t flags,unsigned int order) + __assume_page_alignment __malloc;#else -static __always_inline void * -kmalloc_order_trace (size_t size,gfp_t flags,unsigned int order) +static __always_inline void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order) {return kmalloc_order (size, flags, order); }
@@ -638,8 +637,8 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)* @new_size: new size of a single member of the array * @flags: the type of memory to allocate (see kmalloc) */ -static __must_check inline void * -krealloc_array (void *p,size_t new_n,size_t new_size,gfp_t flags) +static inline void *__must_check krealloc_array (void *p,size_t new_n,size_t new_size, +gfp_t flags) {size_t bytes;
@@ -668,7 +667,7 @@ static inline void *kcalloc(size_t n, size_t size, gfp_t flags)* allocator where we care about the real place the memory allocation * request comes from. */ -extern void *__kmalloc_track_caller (size_t ,gfp_t ,unsigned long ); +extern void *__kmalloc_track_caller (size_t size,gfp_t flags,unsigned long caller);#define kmalloc_track_caller (size, flags) \ __kmalloc_track_caller(size, flags, _RET_IP_)
@@ -691,7 +690,8 @@ static inline void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node)#ifdef CONFIG_NUMA -extern void *__kmalloc_node_track_caller (size_t ,gfp_t ,int ,unsigned long ); +extern void *__kmalloc_node_track_caller (size_t size,gfp_t flags,int node, +unsigned long caller);#define kmalloc_node_track_caller (size, flags, node) \ __kmalloc_node_track_caller(size, flags, node, \ _RET_IP_)
Signature · 1 line
2.30.2
PATCH v3 4/8 slab: Add __alloc_size attributes for better bounds checking
In reply to Kees CookAs already done in GrapheneOS, add the __alloc_size attribute for regular kmalloc interfaces, to provide additional hinting for better bounds checking, assisting CONFIG_FORTIFY_SOURCE and other compiler optimizations.
--- include/linux/slab.h | 61 ++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 28 deletions(-)
include/linux/slab.h +33 -28
@@ -181,7 +181,7 @@ int kmem_cache_shrink(struct kmem_cache *s);/* * Common kmalloc functions provided by all allocators */ -void *__must_check krealloc (const void *objp,size_t new_size,gfp_t flags); +void *__must_check krealloc (const void *objp,size_t new_size,gfp_t flags)__alloc_size(2) ;void kfree (const void *objp);void kfree_sensitive (const void *objp);size_t __ksize (const void *objp);
@@ -425,7 +425,7 @@ static __always_inline unsigned int __kmalloc_index(size_t size,#define kmalloc_index (s) __kmalloc_index(s, true)#endif /* !CONFIG_SLOB */ -void *__kmalloc (size_t size,gfp_t flags) __assume_kmalloc_alignment __malloc; +void *__kmalloc (size_t size,gfp_t flags) __assume_kmalloc_alignment__alloc_size(1) ;void *kmem_cache_alloc (struct kmem_cache *s,gfp_t flags) __assume_slab_alignment __malloc;void kmem_cache_free (struct kmem_cache *s,void *objp);
@@ -449,11 +449,12 @@ static __always_inline void kfree_bulk(size_t size, void **p) }#ifdef CONFIG_NUMA -void *__kmalloc_node (size_t size,gfp_t flags,int node) __assume_kmalloc_alignment __malloc; +void *__kmalloc_node (size_t size,gfp_t flags,int node) __assume_kmalloc_alignment +__alloc_size(1) ;void *kmem_cache_alloc_node (struct kmem_cache *s,gfp_t flags,int node) __assume_slab_alignment __malloc;#else -static __always_inline void *__kmalloc_node(size_t size, gfp_t flags, int node) +static __always_inline __alloc_size(1) void *__kmalloc_node(size_t size, gfp_t flags, int node) {return __kmalloc (size, flags); }
@@ -466,23 +467,23 @@ static __always_inline void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t f#ifdef CONFIG_TRACING extern void *kmem_cache_alloc_trace (struct kmem_cache *s,gfp_t flags,size_t size) - __assume_slab_alignment __malloc; + __assume_slab_alignment__alloc_size(3) ;#ifdef CONFIG_NUMA extern void *kmem_cache_alloc_node_trace (struct kmem_cache *s,gfp_t gfpflags, -int node,size_t size) __assume_slab_alignment __malloc; +int node,size_t size) __assume_slab_alignment +__alloc_size(4) ;#else -static __always_inline void *kmem_cache_alloc_node_trace(struct kmem_cache *s, -gfp_t gfpflags,int node, -size_t size) +static __always_inline __alloc_size(4) void *kmem_cache_alloc_node_trace(struct kmem_cache *s, +gfp_t gfpflags,int node,size_t size) {return kmem_cache_alloc_trace (s, gfpflags, size); }#endif /* CONFIG_NUMA */ #else /* CONFIG_TRACING */ -static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t flags, -size_t size) +static __always_inline __alloc_size(3) void *kmem_cache_alloc_trace(struct kmem_cache *s, +gfp_t flags,size_t size) {void *ret =kmem_cache_alloc (s, flags);
@@ -501,19 +502,20 @@ static __always_inline void *kmem_cache_alloc_node_trace(struct kmem_cache *s, g#endif /* CONFIG_TRACING */ extern void *kmalloc_order (size_t size,gfp_t flags,unsigned int order) __assume_page_alignment - __malloc; +__alloc_size(1) ;#ifdef CONFIG_TRACING extern void *kmalloc_order_trace (size_t size,gfp_t flags,unsigned int order) - __assume_page_alignment __malloc; + __assume_page_alignment__alloc_size(1) ;#else -static __always_inline void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order) +static __always_inline __alloc_size(1) void *kmalloc_order_trace(size_t size, gfp_t flags, +unsigned int order) {return kmalloc_order (size, flags, order); }#endif -static __always_inline void *kmalloc_large(size_t size, gfp_t flags) +static __always_inline __alloc_size(1) void *kmalloc_large(size_t size, gfp_t flags) {unsigned int order =get_order (size);return kmalloc_order_trace (size, flags, order);
@@ -573,7 +575,7 @@ static __always_inline void *kmalloc_large(size_t size, gfp_t flags)* Try really hard to succeed the allocation but fail * eventually. */ -static __always_inline void *kmalloc(size_t size, gfp_t flags) +static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags) {if (__builtin_constant_p (size)) {#ifndef CONFIG_SLOB
@@ -595,7 +597,7 @@ static __always_inline void *kmalloc(size_t size, gfp_t flags)return __kmalloc (size, flags); } -static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) +static __always_inline __alloc_size(1) void *kmalloc_node(size_t size, gfp_t flags, int node) {#ifndef CONFIG_SLOB if (__builtin_constant_p(size) &&
@@ -619,7 +621,7 @@ static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)* @size: element size. * @flags: the type of memory to allocate (see kmalloc). */ -static inline void *kmalloc_array (size_t n,size_t size,gfp_t flags) +static inline __alloc_size(1, 2) void *kmalloc_array (size_t n,size_t size,gfp_t flags) {size_t bytes;
@@ -637,8 +639,10 @@ static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags)* @new_size: new size of a single member of the array * @flags: the type of memory to allocate (see kmalloc) */ -static inline void *__must_check krealloc_array (void *p,size_t new_n,size_t new_size, -gfp_t flags) +static inline __alloc_size(2, 3) void *__must_check krealloc_array (void *p, +size_t new_n, +size_t new_size, +gfp_t flags) {size_t bytes;
@@ -654,7 +658,7 @@ static inline void * __must_check krealloc_array(void *p, size_t new_n, size_t n* @size: element size. * @flags: the type of memory to allocate (see kmalloc). */ -static inline void *kcalloc (size_t n,size_t size,gfp_t flags) +static inline __alloc_size(1, 2) void *kcalloc (size_t n,size_t size,gfp_t flags) {return kmalloc_array (n, size, flags | __GFP_ZERO); }
@@ -667,12 +671,13 @@ static inline void *kcalloc(size_t n, size_t size, gfp_t flags)* allocator where we care about the real place the memory allocation * request comes from. */ -extern void *__kmalloc_track_caller (size_t size,gfp_t flags,unsigned long caller); +extern void *__kmalloc_track_caller (size_t size,gfp_t flags,unsigned long caller) +__alloc_size(1) ;#define kmalloc_track_caller (size, flags) \ __kmalloc_track_caller(size, flags, _RET_IP_) -static inline void *kmalloc_array_node (size_t n,size_t size,gfp_t flags, -int node) +static inline __alloc_size(1, 2) void *kmalloc_array_node (size_t n,size_t size,gfp_t flags, +int node) {size_t bytes;
@@ -683,7 +688,7 @@ static inline void *kmalloc_array_node(size_t n, size_t size, gfp_t flags,return __kmalloc_node (bytes, flags, node); } -static inline void *kcalloc_node (size_t n,size_t size,gfp_t flags,int node) +static inline __alloc_size(1, 2) void *kcalloc_node (size_t n,size_t size,gfp_t flags,int node) {return kmalloc_array_node (n, size, flags | __GFP_ZERO, node); }
@@ -691,7 +696,7 @@ static inline void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node)#ifdef CONFIG_NUMA extern void *__kmalloc_node_track_caller (size_t size,gfp_t flags,int node, -unsigned long caller); +unsigned long caller)__alloc_size(1) ;#define kmalloc_node_track_caller (size, flags, node) \ __kmalloc_node_track_caller(size, flags, node, \ _RET_IP_)
@@ -716,7 +721,7 @@ static inline void *kmem_cache_zalloc(struct kmem_cache *k, gfp_t flags)* @size: how many bytes of memory are required. * @flags: the type of memory to allocate (see kmalloc). */ -static inline void *kzalloc (size_t size,gfp_t flags) +static inline __alloc_size(1) void *kzalloc (size_t size,gfp_t flags) {return kmalloc (size, flags | __GFP_ZERO); }
@@ -727,7 +732,7 @@ static inline void *kzalloc(size_t size, gfp_t flags)* @flags: the type of memory to allocate (see kmalloc). * @node: memory node from which to allocate */ -static inline void *kzalloc_node (size_t size,gfp_t flags,int node) +static inline __alloc_size(1) void *kzalloc_node (size_t size,gfp_t flags,int node) {return kmalloc_node (size, flags | __GFP_ZERO, node); }
Signature · 1 line
2.30.2
Andrew Morton
maintainerRe: [PATCH v3 4/8] slab: Add __alloc_size attributes for better bounds checking
Quoted 4 lines · On Thu, 30 Sep 2021 15:27:00 -0700 Kees Cook <[email protected]> wrote:
As already done in GrapheneOS, add the
__alloc_sizeattribute for regular kmalloc interfaces, to provide additional hinting for better bounds checking, assistingCONFIG_FORTIFY_SOURCEand other compiler optimizations.
x86_64 allmodconfig:
In file included from ./arch/x86/include/asm/preempt.h:7,
from ./include/linux/preempt.h:78,
from ./include/linux/spinlock.h:55,
from ./include/linux/mmzone.h:8,
from ./include/linux/gfp.h:6,
from ./include/linux/mm.h:10,
from ./include/linux/mman.h:5,
from lib/test_kasan_module.c:10:
In function 'check_copy_size',
inlined from 'copy_user_test' at ./include/linux/uaccess.h:191:6:
./include/linux/thread_info.h:213:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small
213 | __bad_copy_to();
| ^~~~~~~~~~~~~~~
In function 'check_copy_size',
inlined from 'copy_user_test' at ./include/linux/uaccess.h:199:6:
./include/linux/thread_info.h:211:4: error: call to '__bad_copy_from' declared with attribute error: copy source size is too small
211 | __bad_copy_from();
| ^~~~~~~~~~~~~~~~~make[1]: *** [lib/test_kasan_module.o] Error 1
make: *** [lib] Error 2
Re: [PATCH v3 4/8] slab: Add __alloc_size attributes for better bounds checking
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Thu, 30 Sep 2021 15:27:00 -0700 Kees Cook <[email protected]> wrote:
As already done in GrapheneOS, add the
__alloc_sizeattribute for regular kmalloc interfaces, to provide additional hinting for better bounds checking, assistingCONFIG_FORTIFY_SOURCEand other compiler optimizations.
x86_64allmodconfig:
What compiler and version?
Quoted 19 lines
In file included from
./arch/x86/include/asm/preempt.h:7,from ./include/linux/preempt.h:78, from ./include/linux/spinlock.h:55, from ./include/linux/mmzone.h:8, from ./include/linux/gfp.h:6, from ./include/linux/mm.h:10, from ./include/linux/mman.h:5, from lib/test_kasan_module.c:10: In function 'check_copy_size', inlined from 'copy_user_test' at ./include/linux/uaccess.h:191:6: ./include/linux/thread_info.h:213:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small 213 | __bad_copy_to(); | ^~~~~~~~~~~~~~~ In function 'check_copy_size', inlined from 'copy_user_test' at ./include/linux/uaccess.h:199:6: ./include/linux/thread_info.h:211:4: error: call to '__bad_copy_from' declared with attribute error: copy source size is too small 211 | __bad_copy_from(); | ^~~~~~~~~~~~~~~~~
Quoted 2 lines
make[1]: *** [
lib/test_kasan_module.o] Error 1
make: *** [lib] Error 2
Hah, yes, it caught an intentionally bad copy. This may bypass the check, as I've had to do in LKDTM before. I will test...
lib/test_kasan_module.c +6 -1
@@ -19,7 +19,12 @@ static noinline void __init copy_user_test(void) {char *kmem;char __user *usermem; -size_t size =128 -KASAN_GRANULE_SIZE ; +/* +* This is marked volatile to avoid __alloc_size() +* noticing the intentionally out-of-bounds copys +* being done on the allocation. +*/ +volatile size_t size =128 -KASAN_GRANULE_SIZE ;int __maybe_unused unused; kmem =kmalloc (size,GFP_KERNEL );
Signature · 1 line
Kees Cook
Re: [PATCH v3 4/8] slab: Add __alloc_size attributes for better bounds checking
Quoted 42 lines · Kees Cook
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Thu, 30 Sep 2021 15:27:00 -0700 Kees Cook <[email protected]> wrote:
As already done in GrapheneOS, add the
__alloc_sizeattribute for regular kmalloc interfaces, to provide additional hinting for better bounds checking, assistingCONFIG_FORTIFY_SOURCEand other compiler optimizations.
x86_64allmodconfig:What compiler and version?
Quoted 19 lines
In file included from
./arch/x86/include/asm/preempt.h:7,from ./include/linux/preempt.h:78, from ./include/linux/spinlock.h:55, from ./include/linux/mmzone.h:8, from ./include/linux/gfp.h:6, from ./include/linux/mm.h:10, from ./include/linux/mman.h:5, from lib/test_kasan_module.c:10: In function 'check_copy_size', inlined from 'copy_user_test' at ./include/linux/uaccess.h:191:6: ./include/linux/thread_info.h:213:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small 213 | __bad_copy_to(); | ^~~~~~~~~~~~~~~ In function 'check_copy_size', inlined from 'copy_user_test' at ./include/linux/uaccess.h:199:6: ./include/linux/thread_info.h:211:4: error: call to '__bad_copy_from' declared with attribute error: copy source size is too small 211 | __bad_copy_from(); | ^~~~~~~~~~~~~~~~~Quoted 2 lines
make[1]: *** [
lib/test_kasan_module.o] Error 1
make: *** [lib] Error 2Hah, yes, it caught an intentionally bad copy. This may bypass the check, as I've had to do in LKDTM before. I will test...
lib/test_kasan_module.c +0
-0
lib/test_kasan_module.c static noinline void __init copy_user_test(void) · line 27
Quoted 10 lines
lib/test_kasan_module.c +6
-1@@ -19,7 +19,12 @@ static noinline void __init copy_user_test(void) {char *kmem;char __user *usermem; -size_t size =128 -KASAN_GRANULE_SIZE ; +/* +* This is marked volatile to avoid __alloc_size() +* noticing the intentionally out-of-bounds copys +* being done on the allocation. +*/ +volatile size_t size =128 -KASAN_GRANULE_SIZE ;
Maybe OPTIMIZER_HIDE_VAR()? The normal version of that abuses an empty asm statement to hide the value from the compiler.
Re: [PATCH v3 4/8] slab: Add __alloc_size attributes for better bounds checking
Quoted 48 lines · Jann Horn
Quoted 47 lines · Kees Cook
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Thu, 30 Sep 2021 15:27:00 -0700 Kees Cook <[email protected]> wrote:
As already done in GrapheneOS, add the
__alloc_sizeattribute for regular kmalloc interfaces, to provide additional hinting for better bounds checking, assistingCONFIG_FORTIFY_SOURCEand other compiler optimizations.
x86_64allmodconfig:What compiler and version?
Quoted 19 lines
In file included from
./arch/x86/include/asm/preempt.h:7,from ./include/linux/preempt.h:78, from ./include/linux/spinlock.h:55, from ./include/linux/mmzone.h:8, from ./include/linux/gfp.h:6, from ./include/linux/mm.h:10, from ./include/linux/mman.h:5, from lib/test_kasan_module.c:10: In function 'check_copy_size', inlined from 'copy_user_test' at ./include/linux/uaccess.h:191:6: ./include/linux/thread_info.h:213:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small 213 | __bad_copy_to(); | ^~~~~~~~~~~~~~~ In function 'check_copy_size', inlined from 'copy_user_test' at ./include/linux/uaccess.h:199:6: ./include/linux/thread_info.h:211:4: error: call to '__bad_copy_from' declared with attribute error: copy source size is too small 211 | __bad_copy_from(); | ^~~~~~~~~~~~~~~~~Quoted 2 lines
make[1]: *** [
lib/test_kasan_module.o] Error 1
make: *** [lib] Error 2Hah, yes, it caught an intentionally bad copy. This may bypass the check, as I've had to do in LKDTM before. I will test...
lib/test_kasan_module.c +1
-1@@ -19,7 +19,12 @@ static noinline void __init copy_user_test(void) {char *kmem;char __user *usermem; -size_t size =128 -KASAN_GRANULE_SIZE ; +/*
Quoted 8 lines
lib/test_kasan_module.c static noinline void __init copy_user_test(void) · line 27
Quoted 5 lines
lib/test_kasan_module.c +5
-0+* This is marked volatile to avoid __alloc_size() +* noticing the intentionally out-of-bounds copys +* being done on the allocation. +*/ +volatile size_t size =128 -KASAN_GRANULE_SIZE ;Maybe
OPTIMIZER_HIDE_VAR()? The normal version of that abuses an empty asm statement to hide the value from the compiler.
Oh! I hadn't seen that before. Is that better than volatile in this case?
Signature · 1 line
Kees Cook
Re: [PATCH v3 4/8] slab: Add __alloc_size attributes for better bounds checking
Quoted 58 lines · Kees Cook
Quoted 48 lines · Jann Horn
Quoted 47 lines · Kees Cook
Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Thu, 30 Sep 2021 15:27:00 -0700 Kees Cook <[email protected]> wrote:
As already done in GrapheneOS, add the
__alloc_sizeattribute for regular kmalloc interfaces, to provide additional hinting for better bounds checking, assistingCONFIG_FORTIFY_SOURCEand other compiler optimizations.
x86_64allmodconfig:What compiler and version?
Quoted 19 lines
In file included from
./arch/x86/include/asm/preempt.h:7,from ./include/linux/preempt.h:78, from ./include/linux/spinlock.h:55, from ./include/linux/mmzone.h:8, from ./include/linux/gfp.h:6, from ./include/linux/mm.h:10, from ./include/linux/mman.h:5, from lib/test_kasan_module.c:10: In function 'check_copy_size', inlined from 'copy_user_test' at ./include/linux/uaccess.h:191:6: ./include/linux/thread_info.h:213:4: error: call to '__bad_copy_to' declared with attribute error: copy destination size is too small 213 | __bad_copy_to(); | ^~~~~~~~~~~~~~~ In function 'check_copy_size', inlined from 'copy_user_test' at ./include/linux/uaccess.h:199:6: ./include/linux/thread_info.h:211:4: error: call to '__bad_copy_from' declared with attribute error: copy source size is too small 211 | __bad_copy_from(); | ^~~~~~~~~~~~~~~~~Quoted 2 lines
make[1]: *** [
lib/test_kasan_module.o] Error 1
make: *** [lib] Error 2Hah, yes, it caught an intentionally bad copy. This may bypass the check, as I've had to do in LKDTM before. I will test...
lib/test_kasan_module.c +1
-1@@ -19,7 +19,12 @@ static noinline void __init copy_user_test(void) {char *kmem;char __user *usermem; -size_t size =128 -KASAN_GRANULE_SIZE ; +/* Quoted 8 lines
lib/test_kasan_module.c static noinline void __init copy_user_test(void) · line 27
Quoted 5 lines
lib/test_kasan_module.c +5
-0+* This is marked volatile to avoid __alloc_size() +* noticing the intentionally out-of-bounds copys +* being done on the allocation. +*/ +volatile size_t size =128 -KASAN_GRANULE_SIZE ;Maybe
OPTIMIZER_HIDE_VAR()? The normal version of that abuses an empty asm statement to hide the value from the compiler.
Quoted 2 lines
Oh! I hadn't seen that before. Is that better than volatile in this case?
It forces the compiler to assume an arbitrary modification of the value, but doesn't force allocation of a stack slot like "volatile" does, so it probably generates nicer code? Not that it really matters here...
It also has the difference that you can explicitly specify where the compiler's analysis should cut off, instead of just doing it on every access to a variable. But I guess maybe in this case, that's an argument in favor of "volatile"? I don't know... I guess maybe "volatile" does make sense here.
PATCH v3 5/8 mm/kvmalloc: Add __alloc_size attributes for better bounds checking
In reply to Kees CookAs already done in GrapheneOS, add the __alloc_size attribute for regular kvmalloc interfaces, to provide additional hinting for better bounds checking, assisting CONFIG_FORTIFY_SOURCE and other compiler optimizations.
--- include/linux/mm.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
include/linux/mm.h +8 -8
@@ -799,21 +799,21 @@ static inline int is_vmalloc_or_module_addr(const void *x) }#endif -extern void *kvmalloc_node (size_t size,gfp_t flags,int node); -static inline void *kvmalloc (size_t size,gfp_t flags) +extern void *kvmalloc_node (size_t size,gfp_t flags,int node)__alloc_size(1) ; +static inline __alloc_size(1) void *kvmalloc (size_t size,gfp_t flags) {return kvmalloc_node (size, flags,NUMA_NO_NODE ); } -static inline void *kvzalloc_node (size_t size,gfp_t flags,int node) +static inline __alloc_size(1) void *kvzalloc_node (size_t size,gfp_t flags,int node) {return kvmalloc_node (size, flags | __GFP_ZERO, node); } -static inline void *kvzalloc (size_t size,gfp_t flags) +static inline __alloc_size(1) void *kvzalloc (size_t size,gfp_t flags) {return kvmalloc (size, flags | __GFP_ZERO); } -static inline void *kvmalloc_array (size_t n,size_t size,gfp_t flags) +static inline __alloc_size(1, 2) void *kvmalloc_array (size_t n,size_t size,gfp_t flags) {size_t bytes;
@@ -823,13 +823,13 @@ static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags)return kvmalloc (bytes, flags); } -static inline void *kvcalloc (size_t n,size_t size,gfp_t flags) +static inline __alloc_size(1, 2) void *kvcalloc (size_t n,size_t size,gfp_t flags) {return kvmalloc_array (n, size, flags | __GFP_ZERO); } -extern void *kvrealloc (const void *p,size_t oldsize,size_t newsize, -gfp_t flags); +extern void *kvrealloc (const void *p,size_t oldsize,size_t newsize,gfp_t flags) +__alloc_size(3) ;extern void kvfree (const void *addr);extern void kvfree_sensitive (const void *addr,size_t len);
Signature · 1 line
2.30.2
PATCH v3 6/8 mm/vmalloc: Add __alloc_size attributes for better bounds checking
In reply to Kees CookAs already done in GrapheneOS, add the __alloc_size attribute for appropriate vmalloc allocator interfaces, to provide additional hinting for better bounds checking, assisting CONFIG_FORTIFY_SOURCE and other compiler optimizations.
--- include/linux/vmalloc.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
include/linux/vmalloc.h +11 -11
@@ -136,21 +136,21 @@ static inline void vmalloc_init(void)static inline unsigned long vmalloc_nr_pages (void ) {return 0 ; }#endif -extern void *vmalloc (unsigned long size); -extern void *vzalloc (unsigned long size); -extern void *vmalloc_user (unsigned long size); -extern void *vmalloc_node (unsigned long size,int node); -extern void *vzalloc_node (unsigned long size,int node); -extern void *vmalloc_32 (unsigned long size); -extern void *vmalloc_32_user (unsigned long size); -extern void *__vmalloc (unsigned long size,gfp_t gfp_mask); +extern void *vmalloc (unsigned long size)__alloc_size(1) ; +extern void *vzalloc (unsigned long size)__alloc_size(1) ; +extern void *vmalloc_user (unsigned long size)__alloc_size(1) ; +extern void *vmalloc_node (unsigned long size,int node)__alloc_size(1) ; +extern void *vzalloc_node (unsigned long size,int node)__alloc_size(1) ; +extern void *vmalloc_32 (unsigned long size)__alloc_size(1) ; +extern void *vmalloc_32_user (unsigned long size)__alloc_size(1) ; +extern void *__vmalloc (unsigned long size,gfp_t gfp_mask)__alloc_size(1) ;extern void *__vmalloc_node_range (unsigned long size,unsigned long align,unsigned long start,unsigned long end,gfp_t gfp_mask,pgprot_t prot,unsigned long vm_flags,int node, -const void *caller); +const void *caller)__alloc_size(1) ;void *__vmalloc_node (unsigned long size,unsigned long align,gfp_t gfp_mask, -int node,const void *caller); -void *vmalloc_no_huge (unsigned long size); +int node,const void *caller)__alloc_size(1) ; +void *vmalloc_no_huge (unsigned long size)__alloc_size(1) ;extern void vfree (const void *addr);extern void vfree_atomic (const void *addr);
Signature · 1 line
2.30.2
PATCH v3 7/8 mm/page_alloc: Add __alloc_size attributes for better bounds checking
In reply to Kees CookAs already done in GrapheneOS, add the __alloc_size attribute for appropriate page allocator interfaces, to provide additional hinting for better bounds checking, assisting CONFIG_FORTIFY_SOURCE and other compiler optimizations.
--- include/linux/gfp.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/gfp.h +2 -2
@@ -608,9 +608,9 @@ static inline struct page *alloc_pages(gfp_t gfp_mask, unsigned int order)extern unsigned long __get_free_pages (gfp_t gfp_mask,unsigned int order);extern unsigned long get_zeroed_page (gfp_t gfp_mask); -void *alloc_pages_exact (size_t size,gfp_t gfp_mask); +void *alloc_pages_exact (size_t size,gfp_t gfp_mask)__alloc_size(1) ;void free_pages_exact (void *virt,size_t size); -void *__meminit alloc_pages_exact_nid (int nid,size_t size,gfp_t gfp_mask); +__meminit void *alloc_pages_exact_nid (int nid,size_t size,gfp_t gfp_mask)__alloc_size(1) ;#define __get_free_page (gfp_mask) \ __get_free_pages((gfp_mask), 0)
Signature · 1 line
2.30.2
PATCH v3 8/8 percpu: Add __alloc_size attributes for better bounds checking
In reply to Kees CookAs already done in GrapheneOS, add the __alloc_size attribute for appropriate percpu allocator interfaces, to provide additional hinting for better bounds checking, assisting CONFIG_FORTIFY_SOURCE and other compiler optimizations.
Note that due to the implementation of the percpu API, this is unlikely to ever actually provide compile-time checking beyond very simple non-SMP builds. But, since they are technically allocators, mark them as such.
--- include/linux/percpu.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/linux/percpu.h +3 -3
@@ -123,7 +123,7 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size, pcpu_fc_populate_pte_fn_t populate_pte_fn);#endif -extern void __percpu *__alloc_reserved_percpu (size_t size,size_t align); +extern void __percpu *__alloc_reserved_percpu (size_t size,size_t align)__alloc_size(1) ;extern bool __is_kernel_percpu_address (unsigned long addr,unsigned long *can_addr);extern bool is_kernel_percpu_address (unsigned long addr);
@@ -131,8 +131,8 @@ extern bool is_kernel_percpu_address(unsigned long addr);extern void __init setup_per_cpu_areas (void );#endif -extern void __percpu *__alloc_percpu_gfp (size_t size,size_t align,gfp_t gfp); -extern void __percpu *__alloc_percpu (size_t size,size_t align); +extern void __percpu *__alloc_percpu_gfp (size_t size,size_t align,gfp_t gfp)__alloc_size(1) ; +extern void __percpu *__alloc_percpu (size_t size,size_t align)__alloc_size(1) ;extern void free_percpu (void __percpu *__pdata);extern phys_addr_t per_cpu_ptr_to_phys (void *addr);
Signature · 1 line
2.30.2
Re: [PATCH v3 8/8] percpu: Add __alloc_size attributes for better bounds checking
Hello,
Quoted 18 lines · Kees Cook
As already done in GrapheneOS, add the
__alloc_sizeattribute for appropriate percpu allocator interfaces, to provide additional hinting for better bounds checking, assistingCONFIG_FORTIFY_SOURCEand other compiler optimizations.Note that due to the implementation of the percpu API, this is unlikely to ever actually provide compile-time checking beyond very simple non-SMP builds. But, since they are technically allocators, mark them as such.
Cc Dennis Zhou <[email protected]>Cc Tejun Heo <[email protected]>Cc Christoph Lameter <[email protected]>Cc Andy Whitcroft <[email protected]>Cc David Rientjes <[email protected]>Cc Dwaipayan Ray <[email protected]>Cc Joe Perches <[email protected]>Cc Joonsoo Kim <[email protected]>Cc Lukas Bulwahn <[email protected]>
Quoted 8 lines
Cc Miguel Ojeda <[email protected]>Cc Nathan Chancellor <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Pekka Enberg <[email protected]>Cc Vlastimil Babka <[email protected]>Co-developed-by Daniel Micay <[email protected]>Signed-off-by Daniel Micay <[email protected]>Signed-off-by Kees Cook <[email protected]>
Thanks for updating the commit log.
Thanks,
Dennis
Quoted 31 lines
--- include/linux/percpu.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)include/linux/percpu.h +3
-3@@ -123,7 +123,7 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size, pcpu_fc_populate_pte_fn_t populate_pte_fn);#endif -extern void __percpu *__alloc_reserved_percpu (size_t size,size_t align); +extern void __percpu *__alloc_reserved_percpu (size_t size,size_t align)__alloc_size(1) ;extern bool __is_kernel_percpu_address (unsigned long addr,unsigned long *can_addr);extern bool is_kernel_percpu_address (unsigned long addr);@@ -131,8 +131,8 @@ extern bool is_kernel_percpu_address(unsigned long addr);extern void __init setup_per_cpu_areas (void );#endif -extern void __percpu *__alloc_percpu_gfp (size_t size,size_t align,gfp_t gfp); -extern void __percpu *__alloc_percpu (size_t size,size_t align); +extern void __percpu *__alloc_percpu_gfp (size_t size,size_t align,gfp_t gfp)__alloc_size(1) ; +extern void __percpu *__alloc_percpu (size_t size,size_t align)__alloc_size(1) ;extern void free_percpu (void __percpu *__pdata);extern phys_addr_t per_cpu_ptr_to_phys (void *addr);Signature · 1 line
2.30.2