Message ID | 20210818214021.2476230-5-keescook@chromium.org (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Add __alloc_size() for better bounds checking | expand |
On 18/08/2021 23.40, Kees Cook wrote: > As 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. > > #ifdef CONFIG_NUMA > +__alloc_size(1) > void *__kmalloc_node(size_t size, gfp_t flags, int node) __assume_slab_alignment __malloc; Eh, can we keep all the attributes together instead of having some before, some after? I don't necessarily think this is a good idea, but just throwing it out there: __alloc_size almost always goes along with __malloc, so one could define __alloc_size in such a way that it implies __malloc, then just have a "raw" ____alloc_size version to use for krealloc() and similar. But I guess it's cleaner to keep it this way. While declared in string.h, kmemdup() is also eligible for alloc_size(2). Which brings me to an old wishlist item of mine [it's almost christmas]: that alloc_size could understand more general expressions for the size of the returned memory, not just the primitive one based on malloc()/calloc() prototypes. So e.g. kmemdup_nul() returns something of size $2+1, while it is also very common to have a alloc_foo(void) helper which returns something of size sizeof(struct foo). Unfortunately I don't think gcc's attribute parsing machinery can easily be tweaked into accepting struct bar *alloc_bars(unsigned count) __new_a_s(count * sizeof(struct bar)) but maybe clang could. If a compiler could understand that kind of attribute, it would also pave the way for implementing __attribute__((__buffer_size__(param, size, access))) e.g. memchr(src, c, size) __buffer_size(src, size, "r") clk_bulk_get(struct device *dev, int num_clks, struct clk_bulk_data *clks) __buffer_size(clks, num_clks * sizeof(*clks), "rw") which could be used for both static analysis and optional run-time instrumentation. Rasmus
On Wed, Aug 18, 2021 at 2:40 PM Kees Cook <keescook@chromium.org> wrote: > > As 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. > > Co-developed-by: Daniel Micay <danielmicay@gmail.com> > Signed-off-by: Daniel Micay <danielmicay@gmail.com> > Cc: Christoph Lameter <cl@linux.com> > Cc: Pekka Enberg <penberg@kernel.org> > Cc: David Rientjes <rientjes@google.com> > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > Cc: Andrew Morton <akpm@linux-foundation.org> > Cc: Vlastimil Babka <vbabka@suse.cz> > Cc: linux-mm@kvack.org > Signed-off-by: Kees Cook <keescook@chromium.org> This is a good start, so Reviewed-by: Nick Desaulniers <ndesaulniers@google.com> Do we also want to attribute: * __kmalloc_index * kmem_cache_free_bulk * kmem_cache_alloc_bulk * kmem_cache_alloc_trace * kmalloc_order * kmalloc_order_trace * kmalloc_large * kmalloc_node * __kmalloc_track_caller * kmalloc_array_node * __kmalloc_node_track_caller > --- > include/linux/slab.h | 20 ++++++++++++++++++-- > 1 file changed, 18 insertions(+), 2 deletions(-) > > diff --git a/include/linux/slab.h b/include/linux/slab.h > index 10fd0a8c816a..6ce826d8194d 100644 > --- a/include/linux/slab.h > +++ b/include/linux/slab.h > @@ -181,7 +181,7 @@ int kmem_cache_shrink(struct kmem_cache *s); > /* > * Common kmalloc functions provided by all allocators > */ > -__must_check > +__must_check __alloc_size(2) > void *krealloc(const void *objp, size_t new_size, gfp_t flags); > void kfree(const void *objp); > void kfree_sensitive(const void *objp); > @@ -426,6 +426,7 @@ static __always_inline unsigned int __kmalloc_index(size_t size, > #define kmalloc_index(s) __kmalloc_index(s, true) > #endif /* !CONFIG_SLOB */ > > +__alloc_size(1) > void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __malloc; > void *kmem_cache_alloc(struct kmem_cache *s, gfp_t flags) __assume_kmalloc_alignment __malloc; > void kmem_cache_free(struct kmem_cache *s, void *objp); > @@ -450,6 +451,7 @@ static __always_inline void kfree_bulk(size_t size, void **p) > } > > #ifdef CONFIG_NUMA > +__alloc_size(1) > void *__kmalloc_node(size_t size, 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; > @@ -574,6 +576,7 @@ static __always_inline void *kmalloc_large(size_t size, gfp_t flags) > * Try really hard to succeed the allocation but fail > * eventually. > */ > +__alloc_size(1) > static __always_inline void *kmalloc(size_t size, gfp_t flags) > { > if (__builtin_constant_p(size)) { > @@ -596,6 +599,7 @@ static __always_inline void *kmalloc(size_t size, gfp_t flags) > return __kmalloc(size, flags); > } > > +__alloc_size(1) > static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) > { > #ifndef CONFIG_SLOB > @@ -620,6 +624,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). > */ > +__alloc_size(1, 2) > static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) > { > size_t bytes; > @@ -638,7 +643,7 @@ 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) > */ > -__must_check > +__must_check __alloc_size(2, 3) > static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, > gfp_t flags) > { > @@ -656,6 +661,7 @@ static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, > * @size: element size. > * @flags: the type of memory to allocate (see kmalloc). > */ > +__alloc_size(1, 2) > static inline void *kcalloc(size_t n, size_t size, gfp_t flags) > { > return kmalloc_array(n, size, flags | __GFP_ZERO); > @@ -685,6 +691,7 @@ static inline void *kmalloc_array_node(size_t n, size_t size, gfp_t flags, > return __kmalloc_node(bytes, flags, node); > } > > +__alloc_size(1, 2) > static inline void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node) > { > return kmalloc_array_node(n, size, flags | __GFP_ZERO, node); > @@ -718,6 +725,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). > */ > +__alloc_size(1) > static inline void *kzalloc(size_t size, gfp_t flags) > { > return kmalloc(size, flags | __GFP_ZERO); > @@ -729,25 +737,31 @@ 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 > */ > +__alloc_size(1) > static inline void *kzalloc_node(size_t size, gfp_t flags, int node) > { > return kmalloc_node(size, flags | __GFP_ZERO, node); > } > > +__alloc_size(1) > extern void *kvmalloc_node(size_t size, gfp_t flags, int node); > +__alloc_size(1) > static inline void *kvmalloc(size_t size, gfp_t flags) > { > return kvmalloc_node(size, flags, NUMA_NO_NODE); > } > +__alloc_size(1) > static inline void *kvzalloc_node(size_t size, gfp_t flags, int node) > { > return kvmalloc_node(size, flags | __GFP_ZERO, node); > } > +__alloc_size(1) > static inline void *kvzalloc(size_t size, gfp_t flags) > { > return kvmalloc(size, flags | __GFP_ZERO); > } > > +__alloc_size(1, 2) > static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) > { > size_t bytes; > @@ -758,11 +772,13 @@ static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) > return kvmalloc(bytes, flags); > } > > +__alloc_size(1, 2) > static inline void *kvcalloc(size_t n, size_t size, gfp_t flags) > { > return kvmalloc_array(n, size, flags | __GFP_ZERO); > } > > +__alloc_size(3) > extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, > gfp_t flags); > extern void kvfree(const void *addr); > --
On Wed, Aug 25, 2021 at 02:31:34PM -0700, Nick Desaulniers wrote: > On Wed, Aug 18, 2021 at 2:40 PM Kees Cook <keescook@chromium.org> wrote: > > > > As 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. > > > > Co-developed-by: Daniel Micay <danielmicay@gmail.com> > > Signed-off-by: Daniel Micay <danielmicay@gmail.com> > > Cc: Christoph Lameter <cl@linux.com> > > Cc: Pekka Enberg <penberg@kernel.org> > > Cc: David Rientjes <rientjes@google.com> > > Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> > > Cc: Andrew Morton <akpm@linux-foundation.org> > > Cc: Vlastimil Babka <vbabka@suse.cz> > > Cc: linux-mm@kvack.org > > Signed-off-by: Kees Cook <keescook@chromium.org> > > This is a good start, so > Reviewed-by: Nick Desaulniers <ndesaulniers@google.com> Thanks! > Do we also want to attribute: > * __kmalloc_index This is just the bucketizer (it returns "int" for the kmalloc bucket). > * kmem_cache_free_bulk Not an allocator. > * kmem_cache_alloc_bulk This allocates a list of pointers, where "size" is the length of the list. > * kmalloc_order > * kmalloc_order_trace > * kmalloc_large Yes, these should be marked, good point. > * kmalloc_node This was already marked. > * kmem_cache_alloc_trace > * __kmalloc_track_caller > * __kmalloc_node_track_caller Yeah, these might get passed through in LTO situations. I'll add them. > * kmalloc_array_node I'll add this -- I thought it was already here but it got missed. Thanks! -Kees > > > --- > > include/linux/slab.h | 20 ++++++++++++++++++-- > > 1 file changed, 18 insertions(+), 2 deletions(-) > > > > diff --git a/include/linux/slab.h b/include/linux/slab.h > > index 10fd0a8c816a..6ce826d8194d 100644 > > --- a/include/linux/slab.h > > +++ b/include/linux/slab.h > > @@ -181,7 +181,7 @@ int kmem_cache_shrink(struct kmem_cache *s); > > /* > > * Common kmalloc functions provided by all allocators > > */ > > -__must_check > > +__must_check __alloc_size(2) > > void *krealloc(const void *objp, size_t new_size, gfp_t flags); > > void kfree(const void *objp); > > void kfree_sensitive(const void *objp); > > @@ -426,6 +426,7 @@ static __always_inline unsigned int __kmalloc_index(size_t size, > > #define kmalloc_index(s) __kmalloc_index(s, true) > > #endif /* !CONFIG_SLOB */ > > > > +__alloc_size(1) > > void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __malloc; > > void *kmem_cache_alloc(struct kmem_cache *s, gfp_t flags) __assume_kmalloc_alignment __malloc; > > void kmem_cache_free(struct kmem_cache *s, void *objp); > > @@ -450,6 +451,7 @@ static __always_inline void kfree_bulk(size_t size, void **p) > > } > > > > #ifdef CONFIG_NUMA > > +__alloc_size(1) > > void *__kmalloc_node(size_t size, 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; > > @@ -574,6 +576,7 @@ static __always_inline void *kmalloc_large(size_t size, gfp_t flags) > > * Try really hard to succeed the allocation but fail > > * eventually. > > */ > > +__alloc_size(1) > > static __always_inline void *kmalloc(size_t size, gfp_t flags) > > { > > if (__builtin_constant_p(size)) { > > @@ -596,6 +599,7 @@ static __always_inline void *kmalloc(size_t size, gfp_t flags) > > return __kmalloc(size, flags); > > } > > > > +__alloc_size(1) > > static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) > > { > > #ifndef CONFIG_SLOB > > @@ -620,6 +624,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). > > */ > > +__alloc_size(1, 2) > > static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) > > { > > size_t bytes; > > @@ -638,7 +643,7 @@ 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) > > */ > > -__must_check > > +__must_check __alloc_size(2, 3) > > static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, > > gfp_t flags) > > { > > @@ -656,6 +661,7 @@ static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, > > * @size: element size. > > * @flags: the type of memory to allocate (see kmalloc). > > */ > > +__alloc_size(1, 2) > > static inline void *kcalloc(size_t n, size_t size, gfp_t flags) > > { > > return kmalloc_array(n, size, flags | __GFP_ZERO); > > @@ -685,6 +691,7 @@ static inline void *kmalloc_array_node(size_t n, size_t size, gfp_t flags, > > return __kmalloc_node(bytes, flags, node); > > } > > > > +__alloc_size(1, 2) > > static inline void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node) > > { > > return kmalloc_array_node(n, size, flags | __GFP_ZERO, node); > > @@ -718,6 +725,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). > > */ > > +__alloc_size(1) > > static inline void *kzalloc(size_t size, gfp_t flags) > > { > > return kmalloc(size, flags | __GFP_ZERO); > > @@ -729,25 +737,31 @@ 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 > > */ > > +__alloc_size(1) > > static inline void *kzalloc_node(size_t size, gfp_t flags, int node) > > { > > return kmalloc_node(size, flags | __GFP_ZERO, node); > > } > > > > +__alloc_size(1) > > extern void *kvmalloc_node(size_t size, gfp_t flags, int node); > > +__alloc_size(1) > > static inline void *kvmalloc(size_t size, gfp_t flags) > > { > > return kvmalloc_node(size, flags, NUMA_NO_NODE); > > } > > +__alloc_size(1) > > static inline void *kvzalloc_node(size_t size, gfp_t flags, int node) > > { > > return kvmalloc_node(size, flags | __GFP_ZERO, node); > > } > > +__alloc_size(1) > > static inline void *kvzalloc(size_t size, gfp_t flags) > > { > > return kvmalloc(size, flags | __GFP_ZERO); > > } > > > > +__alloc_size(1, 2) > > static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) > > { > > size_t bytes; > > @@ -758,11 +772,13 @@ static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) > > return kvmalloc(bytes, flags); > > } > > > > +__alloc_size(1, 2) > > static inline void *kvcalloc(size_t n, size_t size, gfp_t flags) > > { > > return kvmalloc_array(n, size, flags | __GFP_ZERO); > > } > > > > +__alloc_size(3) > > extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, > > gfp_t flags); > > extern void kvfree(const void *addr); > > -- > > -- > Thanks, > ~Nick Desaulniers
diff --git a/include/linux/slab.h b/include/linux/slab.h index 10fd0a8c816a..6ce826d8194d 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -181,7 +181,7 @@ int kmem_cache_shrink(struct kmem_cache *s); /* * Common kmalloc functions provided by all allocators */ -__must_check +__must_check __alloc_size(2) void *krealloc(const void *objp, size_t new_size, gfp_t flags); void kfree(const void *objp); void kfree_sensitive(const void *objp); @@ -426,6 +426,7 @@ static __always_inline unsigned int __kmalloc_index(size_t size, #define kmalloc_index(s) __kmalloc_index(s, true) #endif /* !CONFIG_SLOB */ +__alloc_size(1) void *__kmalloc(size_t size, gfp_t flags) __assume_kmalloc_alignment __malloc; void *kmem_cache_alloc(struct kmem_cache *s, gfp_t flags) __assume_kmalloc_alignment __malloc; void kmem_cache_free(struct kmem_cache *s, void *objp); @@ -450,6 +451,7 @@ static __always_inline void kfree_bulk(size_t size, void **p) } #ifdef CONFIG_NUMA +__alloc_size(1) void *__kmalloc_node(size_t size, 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; @@ -574,6 +576,7 @@ static __always_inline void *kmalloc_large(size_t size, gfp_t flags) * Try really hard to succeed the allocation but fail * eventually. */ +__alloc_size(1) static __always_inline void *kmalloc(size_t size, gfp_t flags) { if (__builtin_constant_p(size)) { @@ -596,6 +599,7 @@ static __always_inline void *kmalloc(size_t size, gfp_t flags) return __kmalloc(size, flags); } +__alloc_size(1) static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) { #ifndef CONFIG_SLOB @@ -620,6 +624,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). */ +__alloc_size(1, 2) static inline void *kmalloc_array(size_t n, size_t size, gfp_t flags) { size_t bytes; @@ -638,7 +643,7 @@ 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) */ -__must_check +__must_check __alloc_size(2, 3) static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, gfp_t flags) { @@ -656,6 +661,7 @@ static inline void *krealloc_array(void *p, size_t new_n, size_t new_size, * @size: element size. * @flags: the type of memory to allocate (see kmalloc). */ +__alloc_size(1, 2) static inline void *kcalloc(size_t n, size_t size, gfp_t flags) { return kmalloc_array(n, size, flags | __GFP_ZERO); @@ -685,6 +691,7 @@ static inline void *kmalloc_array_node(size_t n, size_t size, gfp_t flags, return __kmalloc_node(bytes, flags, node); } +__alloc_size(1, 2) static inline void *kcalloc_node(size_t n, size_t size, gfp_t flags, int node) { return kmalloc_array_node(n, size, flags | __GFP_ZERO, node); @@ -718,6 +725,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). */ +__alloc_size(1) static inline void *kzalloc(size_t size, gfp_t flags) { return kmalloc(size, flags | __GFP_ZERO); @@ -729,25 +737,31 @@ 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 */ +__alloc_size(1) static inline void *kzalloc_node(size_t size, gfp_t flags, int node) { return kmalloc_node(size, flags | __GFP_ZERO, node); } +__alloc_size(1) extern void *kvmalloc_node(size_t size, gfp_t flags, int node); +__alloc_size(1) static inline void *kvmalloc(size_t size, gfp_t flags) { return kvmalloc_node(size, flags, NUMA_NO_NODE); } +__alloc_size(1) static inline void *kvzalloc_node(size_t size, gfp_t flags, int node) { return kvmalloc_node(size, flags | __GFP_ZERO, node); } +__alloc_size(1) static inline void *kvzalloc(size_t size, gfp_t flags) { return kvmalloc(size, flags | __GFP_ZERO); } +__alloc_size(1, 2) static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) { size_t bytes; @@ -758,11 +772,13 @@ static inline void *kvmalloc_array(size_t n, size_t size, gfp_t flags) return kvmalloc(bytes, flags); } +__alloc_size(1, 2) static inline void *kvcalloc(size_t n, size_t size, gfp_t flags) { return kvmalloc_array(n, size, flags | __GFP_ZERO); } +__alloc_size(3) extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags); extern void kvfree(const void *addr);