• W
    KEYS: Avoid false positive ENOMEM error on key read · ce42793c
    Waiman Long 提交于
    stable inclusion
    from linux-4.19.119
    commit e4a281c7daa07814748179ee8b4b483124bb94ea
    
    --------------------------------
    
    [ Upstream commit 4f088249 ]
    
    By allocating a kernel buffer with a user-supplied buffer length, it
    is possible that a false positive ENOMEM error may be returned because
    the user-supplied length is just too large even if the system do have
    enough memory to hold the actual key data.
    
    Moreover, if the buffer length is larger than the maximum amount of
    memory that can be returned by kmalloc() (2^(MAX_ORDER-1) number of
    pages), a warning message will also be printed.
    
    To reduce this possibility, we set a threshold (PAGE_SIZE) over which we
    do check the actual key length first before allocating a buffer of the
    right size to hold it. The threshold is arbitrary, it is just used to
    trigger a buffer length check. It does not limit the actual key length
    as long as there is enough memory to satisfy the memory request.
    
    To further avoid large buffer allocation failure due to page
    fragmentation, kvmalloc() is used to allocate the buffer so that vmapped
    pages can be used when there is not a large enough contiguous set of
    pages available for allocation.
    
    In the extremely unlikely scenario that the key keeps on being changed
    and made longer (still <= buflen) in between 2 __keyctl_read_key()
    calls, the __keyctl_read_key() calling loop in keyctl_read_key() may
    have to be iterated a large number of times, but definitely not infinite.
    Signed-off-by: NWaiman Long <longman@redhat.com>
    Signed-off-by: NDavid Howells <dhowells@redhat.com>
    Signed-off-by: NSasha Levin <sashal@kernel.org>
    Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
    ce42793c
internal.h 10.5 KB