mm: ksize() should silently accept a NULL pointer
authorWilliam Kucharski <william.kucharski@oracle.com>
Fri, 7 Aug 2020 06:18:17 +0000 (23:18 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 7 Aug 2020 18:33:22 +0000 (11:33 -0700)
Other mm routines such as kfree() and kzfree() silently do the right thing
if passed a NULL pointer, so ksize() should do the same.

Signed-off-by: William Kucharski <william.kucharski@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: David Hildenbrand <david@redhat.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>
Link: http://lkml.kernel.org/r/20200616225409.4670-1-william.kucharski@oracle.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/slab_common.c

index f47a097bb4b8e65b00cd21bbd5e79a9a3823ad8a..e493203b500244d62680eededce77d45f357381f 100644 (file)
@@ -1681,10 +1681,9 @@ static __always_inline void *__do_krealloc(const void *p, size_t new_size,
                                           gfp_t flags)
 {
        void *ret;
-       size_t ks = 0;
+       size_t ks;
 
-       if (p)
-               ks = ksize(p);
+       ks = ksize(p);
 
        if (ks >= new_size) {
                p = kasan_krealloc((void *)p, new_size, flags);
@@ -1744,10 +1743,9 @@ void kfree_sensitive(const void *p)
        size_t ks;
        void *mem = (void *)p;
 
-       if (unlikely(ZERO_OR_NULL_PTR(mem)))
-               return;
        ks = ksize(mem);
-       memzero_explicit(mem, ks);
+       if (ks)
+               memzero_explicit(mem, ks);
        kfree(mem);
 }
 EXPORT_SYMBOL(kfree_sensitive);
@@ -1770,8 +1768,6 @@ size_t ksize(const void *objp)
 {
        size_t size;
 
-       if (WARN_ON_ONCE(!objp))
-               return 0;
        /*
         * We need to check that the pointed to object is valid, and only then
         * unpoison the shadow memory below. We use __kasan_check_read(), to
@@ -1785,7 +1781,7 @@ size_t ksize(const void *objp)
         * We want to perform the check before __ksize(), to avoid potentially
         * crashing in __ksize() due to accessing invalid metadata.
         */
-       if (unlikely(objp == ZERO_SIZE_PTR) || !__kasan_check_read(objp, 1))
+       if (unlikely(ZERO_OR_NULL_PTR(objp)) || !__kasan_check_read(objp, 1))
                return 0;
 
        size = __ksize(objp);