Skip to content

Commit 05a9406

Browse files
keestehcaster
authored andcommitted
slab: Introduce kmalloc_size_roundup()
In the effort to help the compiler reason about buffer sizes, the __alloc_size attribute was added to allocators. This improves the scope of the compiler's ability to apply CONFIG_UBSAN_BOUNDS and (in the near future) CONFIG_FORTIFY_SOURCE. For most allocations, this works well, as the vast majority of callers are not expecting to use more memory than what they asked for. There is, however, one common exception to this: anticipatory resizing of kmalloc allocations. These cases all use ksize() to determine the actual bucket size of a given allocation (e.g. 128 when 126 was asked for). This comes in two styles in the kernel: 1) An allocation has been determined to be too small, and needs to be resized. Instead of the caller choosing its own next best size, it wants to minimize the number of calls to krealloc(), so it just uses ksize() plus some additional bytes, forcing the realloc into the next bucket size, from which it can learn how large it is now. For example: data = krealloc(data, ksize(data) + 1, gfp); data_len = ksize(data); 2) The minimum size of an allocation is calculated, but since it may grow in the future, just use all the space available in the chosen bucket immediately, to avoid needing to reallocate later. A good example of this is skbuff's allocators: data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); ... /* kmalloc(size) might give us more room than requested. * Put skb_shared_info exactly at the end of allocated zone, * to allow max possible filling before reallocation. */ osize = ksize(data); size = SKB_WITH_OVERHEAD(osize); In both cases, the "how much was actually allocated?" question is answered _after_ the allocation, where the compiler hinting is not in an easy place to make the association any more. This mismatch between the compiler's view of the buffer length and the code's intention about how much it is going to actually use has already caused problems[1]. It is possible to fix this by reordering the use of the "actual size" information. We can serve the needs of users of ksize() and still have accurate buffer length hinting for the compiler by doing the bucket size calculation _before_ the allocation. Code can instead ask "how large an allocation would I get for a given size?". Introduce kmalloc_size_roundup(), to serve this function so we can start replacing the "anticipatory resizing" uses of ksize(). [1] ClangBuiltLinux#1599 KSPP#183 [ vbabka@suse.cz: add SLOB version ] Cc: Vlastimil Babka <vbabka@suse.cz> 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: linux-mm@kvack.org Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
1 parent 9ed9cac commit 05a9406

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

include/linux/slab.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,21 @@ void * __must_check krealloc(const void *objp, size_t new_size, gfp_t flags) __r
188188
void kfree(const void *objp);
189189
void kfree_sensitive(const void *objp);
190190
size_t __ksize(const void *objp);
191+
192+
/**
193+
* ksize - Report actual allocation size of associated object
194+
*
195+
* @objp: Pointer returned from a prior kmalloc()-family allocation.
196+
*
197+
* This should not be used for writing beyond the originally requested
198+
* allocation size. Either use krealloc() or round up the allocation size
199+
* with kmalloc_size_roundup() prior to allocation. If this is used to
200+
* access beyond the originally requested allocation size, UBSAN_BOUNDS
201+
* and/or FORTIFY_SOURCE may trip, since they only know about the
202+
* originally allocated size via the __alloc_size attribute.
203+
*/
191204
size_t ksize(const void *objp);
205+
192206
#ifdef CONFIG_PRINTK
193207
bool kmem_valid_obj(void *object);
194208
void kmem_dump_obj(void *object);
@@ -779,6 +793,23 @@ extern void kvfree(const void *addr);
779793
extern void kvfree_sensitive(const void *addr, size_t len);
780794

781795
unsigned int kmem_cache_size(struct kmem_cache *s);
796+
797+
/**
798+
* kmalloc_size_roundup - Report allocation bucket size for the given size
799+
*
800+
* @size: Number of bytes to round up from.
801+
*
802+
* This returns the number of bytes that would be available in a kmalloc()
803+
* allocation of @size bytes. For example, a 126 byte request would be
804+
* rounded up to the next sized kmalloc bucket, 128 bytes. (This is strictly
805+
* for the general-purpose kmalloc()-based allocations, and is not for the
806+
* pre-sized kmem_cache_alloc()-based allocations.)
807+
*
808+
* Use this to kmalloc() the full bucket size ahead of time instead of using
809+
* ksize() to query the size after an allocation.
810+
*/
811+
size_t kmalloc_size_roundup(size_t size);
812+
782813
void __init kmem_cache_init_late(void);
783814

784815
#if defined(CONFIG_SMP) && defined(CONFIG_SLAB)

mm/slab.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4192,11 +4192,14 @@ void __check_heap_object(const void *ptr, unsigned long n,
41924192
#endif /* CONFIG_HARDENED_USERCOPY */
41934193

41944194
/**
4195-
* __ksize -- Uninstrumented ksize.
4195+
* __ksize -- Report full size of underlying allocation
41964196
* @objp: pointer to the object
41974197
*
4198-
* Unlike ksize(), __ksize() is uninstrumented, and does not provide the same
4199-
* safety checks as ksize() with KASAN instrumentation enabled.
4198+
* This should only be used internally to query the true size of allocations.
4199+
* It is not meant to be a way to discover the usable size of an allocation
4200+
* after the fact. Instead, use kmalloc_size_roundup(). Using memory beyond
4201+
* the originally requested allocation size may trigger KASAN, UBSAN_BOUNDS,
4202+
* and/or FORTIFY_SOURCE.
42004203
*
42014204
* Return: size of the actual memory used by @objp in bytes
42024205
*/

mm/slab_common.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,26 @@ struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
737737
return kmalloc_caches[kmalloc_type(flags)][index];
738738
}
739739

740+
size_t kmalloc_size_roundup(size_t size)
741+
{
742+
struct kmem_cache *c;
743+
744+
/* Short-circuit the 0 size case. */
745+
if (unlikely(size == 0))
746+
return 0;
747+
/* Short-circuit saturated "too-large" case. */
748+
if (unlikely(size == SIZE_MAX))
749+
return SIZE_MAX;
750+
/* Above the smaller buckets, size is a multiple of page size. */
751+
if (size > KMALLOC_MAX_CACHE_SIZE)
752+
return PAGE_SIZE << get_order(size);
753+
754+
/* The flags don't matter since size_index is common to all. */
755+
c = kmalloc_slab(size, GFP_KERNEL);
756+
return c ? c->object_size : 0;
757+
}
758+
EXPORT_SYMBOL(kmalloc_size_roundup);
759+
740760
#ifdef CONFIG_ZONE_DMA
741761
#define KMALLOC_DMA_NAME(sz) .name[KMALLOC_DMA] = "dma-kmalloc-" #sz,
742762
#else

mm/slob.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,20 @@ void kfree(const void *block)
574574
}
575575
EXPORT_SYMBOL(kfree);
576576

577+
size_t kmalloc_size_roundup(size_t size)
578+
{
579+
/* Short-circuit the 0 size case. */
580+
if (unlikely(size == 0))
581+
return 0;
582+
/* Short-circuit saturated "too-large" case. */
583+
if (unlikely(size == SIZE_MAX))
584+
return SIZE_MAX;
585+
586+
return ALIGN(size, ARCH_KMALLOC_MINALIGN);
587+
}
588+
589+
EXPORT_SYMBOL(kmalloc_size_roundup);
590+
577591
/* can't use ksize for kmem_cache_alloc memory, only kmalloc */
578592
size_t __ksize(const void *block)
579593
{

0 commit comments

Comments
 (0)