]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm: BUG_ON to avoid NULL deference while __GFP_NOFAIL fails
authorBarry Song <v-songbaohua@oppo.com>
Wed, 31 Jul 2024 00:01:54 +0000 (12:01 +1200)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 17 Aug 2024 00:52:43 +0000 (17:52 -0700)
We have cases we still fail though callers might have __GFP_NOFAIL.  Since
they don't check the return, we are exposed to the security risks for NULL
deference.

Though BUG_ON() is not encouraged by Linus, this is an unrecoverable
situation.

Christoph Hellwig:
The whole freaking point of __GFP_NOFAIL is that callers don't handle
allocation failures.  So in fact a straight BUG is the right thing
here.

Vlastimil Babka:
It's just not a recoverable situation (WARN_ON is for recoverable
situations). The caller cannot handle allocation failure and at the same
time asked for an impossible allocation. BUG_ON() is a guaranteed oops
with stracktrace etc. We don't need to hope for the later NULL pointer
dereference (which might if really unlucky happen from a different
context where it's no longer obvious what lead to the allocation failing).

Michal Hocko:
Linus tends to be against adding new BUG() calls unless the failure is
absolutely unrecoverable (e.g. corrupted data structures etc.). I am
not sure how he would look at simply incorrect memory allocator usage to
blow up the kernel. Now the argument could be made that those failures
could cause subtle memory corruptions or even be exploitable which might
be a sufficient reason to stop them early.

Link: https://lkml.kernel.org/r/20240731000155.109583-4-21cnbao@gmail.com
Signed-off-by: Barry Song <v-songbaohua@oppo.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Uladzislau Rezki (Sony) <urezki@gmail.com>
Cc: Lorenzo Stoakes <lstoakes@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: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Hyeonggon Yoo <42.hyeyoo@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Kees Cook <kees@kernel.org>
Cc: "Eugenio Pérez" <eperezma@redhat.com>
Cc: Hailong.Liu <hailong.liu@oppo.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/linux/slab.h
mm/page_alloc.c
mm/util.c

index c9cb4220318332ffd951b681d7e9a2bb1d7db990..4a4d1fdc2afedce0eb711f3db2e9aed674dfd01b 100644 (file)
@@ -827,8 +827,10 @@ kvmalloc_array_node_noprof(size_t n, size_t size, gfp_t flags, int node)
 {
        size_t bytes;
 
-       if (unlikely(check_mul_overflow(n, size, &bytes)))
+       if (unlikely(check_mul_overflow(n, size, &bytes))) {
+               BUG_ON(flags & __GFP_NOFAIL);
                return NULL;
+       }
 
        return kvmalloc_node_noprof(bytes, flags, node);
 }
index 60742d057b0583a4a5edbd736d1937e8a47a722f..d2c37f8f8d092f558b949a86675ca58289ec1b0d 100644 (file)
@@ -4668,8 +4668,10 @@ struct page *__alloc_pages_noprof(gfp_t gfp, unsigned int order,
         * There are several places where we assume that the order value is sane
         * so bail out early if the request is out of bound.
         */
-       if (WARN_ON_ONCE_GFP(order > MAX_PAGE_ORDER, gfp))
+       if (WARN_ON_ONCE_GFP(order > MAX_PAGE_ORDER, gfp)) {
+               BUG_ON(gfp & __GFP_NOFAIL);
                return NULL;
+       }
 
        gfp &= gfp_allowed_mask;
        /*
index ac01925a41795ffe1342e8d3ab8506d151700a89..678c647b778ff9669f24b6af418043847316ec5b 100644 (file)
--- a/mm/util.c
+++ b/mm/util.c
@@ -667,6 +667,7 @@ void *__kvmalloc_node_noprof(DECL_BUCKET_PARAMS(size, b), gfp_t flags, int node)
 
        /* Don't even allow crazy sizes */
        if (unlikely(size > INT_MAX)) {
+               BUG_ON(flags & __GFP_NOFAIL);
                WARN_ON_ONCE(!(flags & __GFP_NOWARN));
                return NULL;
        }