Skip to content

Commit fa9ba3a

Browse files
William Kucharskitorvalds
authored andcommitted
mm: ksize() should silently accept a NULL pointer
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>
1 parent 453431a commit fa9ba3a

1 file changed

Lines changed: 5 additions & 9 deletions

File tree

‎mm/slab_common.c‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,10 +1681,9 @@ static __always_inline void *__do_krealloc(const void *p, size_t new_size,
16811681
gfp_t flags)
16821682
{
16831683
void *ret;
1684-
size_t ks = 0;
1684+
size_t ks;
16851685

1686-
if (p)
1687-
ks = ksize(p);
1686+
ks = ksize(p);
16881687

16891688
if (ks >= new_size) {
16901689
p = kasan_krealloc((void *)p, new_size, flags);
@@ -1744,10 +1743,9 @@ void kfree_sensitive(const void *p)
17441743
size_t ks;
17451744
void *mem = (void *)p;
17461745

1747-
if (unlikely(ZERO_OR_NULL_PTR(mem)))
1748-
return;
17491746
ks = ksize(mem);
1750-
memzero_explicit(mem, ks);
1747+
if (ks)
1748+
memzero_explicit(mem, ks);
17511749
kfree(mem);
17521750
}
17531751
EXPORT_SYMBOL(kfree_sensitive);
@@ -1770,8 +1768,6 @@ size_t ksize(const void *objp)
17701768
{
17711769
size_t size;
17721770

1773-
if (WARN_ON_ONCE(!objp))
1774-
return 0;
17751771
/*
17761772
* We need to check that the pointed to object is valid, and only then
17771773
* unpoison the shadow memory below. We use __kasan_check_read(), to
@@ -1785,7 +1781,7 @@ size_t ksize(const void *objp)
17851781
* We want to perform the check before __ksize(), to avoid potentially
17861782
* crashing in __ksize() due to accessing invalid metadata.
17871783
*/
1788-
if (unlikely(objp == ZERO_SIZE_PTR) || !__kasan_check_read(objp, 1))
1784+
if (unlikely(ZERO_OR_NULL_PTR(objp)) || !__kasan_check_read(objp, 1))
17891785
return 0;
17901786

17911787
size = __ksize(objp);

0 commit comments

Comments
 (0)