kcm: fix a socket double free
Subsystems (2)
PATCH 1/1 kcm: fix a socket double free
Dmitry reported a double free on kcm socket, which could be easily reproduced by:
#include <unistd.h>
#include <sys/syscall.h>
int main()
{
int fd = syscall(SYS_socket, 0x29ul, 0x5ul, 0x0ul, 0, 0, 0);
syscall(SYS_ioctl, fd, 0x89e2ul, 0x20a98000ul, 0, 0, 0);
return 0;
}This is because on the error path, after we install the new socket file, we call sock_release() to clean up the socket, which leaves the fd pointing to a freed socket. Fix this by calling sys_close() on that fd directly.
--- net/kcm/kcmsock.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/kcm/kcmsock.c +2 -1
@@ -13,6 +13,7 @@#include <linux/socket.h> #include <linux/uaccess.h> #include <linux/workqueue.h> +#include <linux/syscalls.h> #include <net/kcm.h> #include <net/netns/generic.h> #include <net/sock.h>
@@ -2029,7 +2030,7 @@ static int kcm_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)if (copy_to_user ((void __user *)arg, &info,sizeof (info))) { err = -EFAULT ; -sock_release (newsock); +sys_close (info.fd); } }
Signature · 1 line
1.8.4.5
David Miller
maintainerFrom: Cong Wang <[email protected]>
Date: Sun, 28 Aug 2016 21:28:26 -0700
Quoted 19 lines
Dmitry reported a double free on kcm socket, which could be easily reproduced by:
#include <unistd.h> #include <sys/syscall.h> int main() { int fd = syscall(SYS_socket, 0x29ul, 0x5ul, 0x0ul, 0, 0, 0); syscall(SYS_ioctl, fd, 0x89e2ul, 0x20a98000ul, 0, 0, 0); return 0; }This is because on the error path, after we install the new socket file, we call
sock_release()to clean up the socket, which leaves the fd pointing to a freed socket. Fix this by callingsys_close()on that fd directly.
Quoted 4 lines
Fixes ab7ac4eb9832 ("kcm: Kernel Connection Multiplexor module")Reported-by Dmitry Vyukov <[email protected]>Cc Tom Herbert <[email protected]>Signed-off-by Cong Wang <[email protected]>
Applied and queued up for -stable, thanks.