Skip to content
  • Helge Deller's avatar
    fs/select: Fix memory corruption in compat_get_fd_set() · 79de3cbe
    Helge Deller authored
    Commit 464d6242 ("select: switch compat_{get,put}_fd_set() to
    compat_{get,put}_bitmap()") changed the calculation on how many bytes
    need to be zeroed when userspace handed over a NULL pointer for a fdset
    array in the select syscall.
    
    The calculation was changed in compat_get_fd_set() wrongly from
    	memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
    to
    	memset(fdset, 0, ALIGN(nr, BITS_PER_LONG));
    
    The ALIGN(nr, BITS_PER_LONG) calculates the number of _bits_ which need
    to be zeroed in the target fdset array (rounded up to the next full bits
    for an unsigned long).
    
    But the memset() call expects the number of _bytes_ to be zeroed.
    
    This leads to clearing more memory than wanted (on the stack area or
    even at kmalloc()ed memory areas) and to random kernel crashes as we
    have seen them on the parisc platform.
    
    The correct change should have been
    
    	memset(fdset, 0, (ALIGN(nr, BITS_PER_LONG) / BITS_PER_LONG) * BYTES_PER_LONG);
    
    which is the same as can be archieved with a call to
    
    	zero_fd_set(nr, fdset).
    
    Fixes: 464d6242
    
     ("select: switch compat_{get,put}_fd_set() to compat_{get,put}_bitmap()"
    Acked-by: default avatar: Al Viro <viro@zeniv.linux.org.uk>
    Signed-off-by: default avatarHelge Deller <deller@gmx.de>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    79de3cbe