Re: [PATCH v4] mempool: Do not use ksize() for poisoning

From: Kees Cook
Date: Mon Oct 31 2022 - 11:22:58 EST


On Mon, Oct 31, 2022 at 03:12:33PM +0000, Matthew Wilcox wrote:
> On Mon, Oct 31, 2022 at 04:00:25PM +0100, Vlastimil Babka wrote:
> > +++ b/mm/mempool.c
> > @@ -57,8 +57,10 @@ static void __check_element(mempool_t *pool, void *element, size_t size)
> > static void check_element(mempool_t *pool, void *element)
> > {
> > /* Mempools backed by slab allocator */
> > - if (pool->free == mempool_free_slab || pool->free == mempool_kfree) {
> > + if (pool->free == mempool_kfree) {
> > __check_element(pool, element, (size_t)pool->pool_data);
> > + } else if (pool->free == mempool_free_slab) {
> > + __check_element(pool, element, kmem_cache_size(pool->pool_data));
> > } else if (pool->free == mempool_free_pages) {
> > /* Mempools backed by page allocator */
> > int order = (int)(long)pool->pool_data;
>
> I had a quick look at this to be sure I understood what was going on,
> and I found a grotesque bug that has been with us since the introduction
> of check_element() in 2015.
>
> + if (pool->free == mempool_free_pages) {
> + int order = (int)(long)pool->pool_data;
> + void *addr = kmap_atomic((struct page *)element);
> +
> + __check_element(pool, addr, 1UL << (PAGE_SHIFT + order));
> + kunmap_atomic(addr);
>
> kmap_atomic() and friends only map a single page. So this is all
> nonsense for HIGHMEM kernels, GFP_HIGHMEM allocations and order > 0.
> The consequence of doing that will be calling memset(POISON_INUSE)
> on random pages that we don't own.

Ah-ha! Thank you both! Seems like the first fix should be squashed and
the latter one is separate? Or just put it all together?

--
Kees Cook