lkml.org 
[lkml]   [2022]   [Mar]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    From
    Subject[RFC PATCH v1 15/15] mm/sl[au]b: check if large object is valid in __ksize()
    Date
    __ksize() returns size of objects allocated from slab allocator.
    When invalid object is passed to __ksize(), returning zero
    prevents further memory corruption and makes caller be able to
    check if there is an error.

    If address of large object is not beginning of folio or size of
    the folio is too small, it must be invalid. Return zero in such cases.

    Suggested-by: Vlastimil Babka <vbabka@suse.cz>
    Signed-off-by: Hyeonggon Yoo <42.hyeyoo@gmail.com>
    ---
    mm/slab_common.c | 6 +++++-
    1 file changed, 5 insertions(+), 1 deletion(-)

    diff --git a/mm/slab_common.c b/mm/slab_common.c
    index 07ed382ed5a9..acb1d27fc9e3 100644
    --- a/mm/slab_common.c
    +++ b/mm/slab_common.c
    @@ -986,8 +986,12 @@ size_t __ksize(const void *object)

    folio = virt_to_folio(object);

    - if (unlikely(!folio_test_slab(folio)))
    + if (unlikely(!folio_test_slab(folio))) {
    + if (object != folio_address(folio) ||
    + folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE)
    + return 0;
    return folio_size(folio);
    + }

    return slab_ksize(folio_slab(folio)->slab_cache);
    }
    --
    2.33.1
    \
     
     \ /
      Last update: 2022-03-08 12:45    [W:7.896 / U:0.008 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site