Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp1167206ybi; Fri, 2 Aug 2019 10:24:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqyQ7Dp6Xb+hS+fMNX0is5IPLGld9e6y/klgmPqRtuHczuk8W5R/i+YiW8JPT4gLMktzT5RR X-Received: by 2002:aa7:81d9:: with SMTP id c25mr61850416pfn.255.1564766664350; Fri, 02 Aug 2019 10:24:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564766664; cv=none; d=google.com; s=arc-20160816; b=FyKCP6YPUPEwOVnFVDZmi7k6w3FUJPvG01IgILqQV9Wb6Jei5h+FSgmucsrPZkWWp0 +bWjzjsmV1wV5FO7kWK0dHjG1GafR/MuOw+Fe1C82MoFauowqBcfVpixKT4xJffIb5YY fQFZPn87Fuwp7Zn+plhPFfNl5icwRg/8vetAi9AFegSHM4Z7cKBgwQrGIQZqpJClmNMW 2cJN1j573sHWJiZvs0yGzIooSQLq6bTxL+y9aApK8LDCtciOZZoX8DJ9ML1b1hmP+IVX mT3yJGpc+toPDrscIzdnHgRtyUuKYvQUxrULvLKt7BdgmHssnfqS0K6UhrthjXR5uYjp ULYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=PyGKPYeS5Ck4U8MRjVRp3MOcM4qEo2KsGvbUW7h2OAo=; b=hj7rTcweL0opzdqqjHZyySQRrz2hxq7MgDubsdinOHP4+yYsSSlBrLCK7IWJvZMi0g O1JxUOpbNDKTK59pAIgSlQqQgUue1Z5mxkOy1NekmbC+c1l8X7d0ph6G8vjQ70Z1LELN UDG6pLJ8guvpw+hT8xo56o3K5GwAn2xHFM3SeTzyvR1mTdckMRmtaH9oqzDwrdf4bSb5 LecxLUeAKLLFf5And6u8gqa6PabLlyYgRLX2MkO6BGkuS93pWe5qohTFVB7oiv1ZB9oy X8/LxXqxZuEDt6YH/pV1VEdeqzeyQqzAfOvYvkryf/HPk7KdNvWYnCHLCUEQ+GNWAKaE AXAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zGiVEDY+; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a21si37361588pgv.185.2019.08.02.10.24.09; Fri, 02 Aug 2019 10:24:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zGiVEDY+; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392064AbfHBJqr (ORCPT + 99 others); Fri, 2 Aug 2019 05:46:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:51246 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392055AbfHBJqo (ORCPT ); Fri, 2 Aug 2019 05:46:44 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E2DF2206A2; Fri, 2 Aug 2019 09:46:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564739203; bh=eSwQrJp19Wu7pYTkL5Ic+Y7raAfGp0I4bHJYhCpv9zY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zGiVEDY+jTNv3O67iIVhkYS1GAhDZ5FAtTrFiRX9RVKGNRKx3CsSmZ3ZLaB3M99An iqqNU12hGpk8I+rwdoqUXuhmD3F0VDQDFMbJLLfIVVswa/FZb7pnIZc9m1BFBSdGGV OWDO4emRICxSC2An6MZRt4KbqKzUx82eHpTkLVfs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+7966f2a0b2c7da8939b4@syzkaller.appspotmail.com, David Howells , Marc Dionne , "David S. Miller" Subject: [PATCH 4.9 139/223] rxrpc: Fix send on a connected, but unbound socket Date: Fri, 2 Aug 2019 11:36:04 +0200 Message-Id: <20190802092247.775140491@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190802092238.692035242@linuxfoundation.org> References: <20190802092238.692035242@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Howells [ Upstream commit e835ada07091f40dcfb1bc735082bd0a7c005e59 ] If sendmsg() or sendmmsg() is called on a connected socket that hasn't had bind() called on it, then an oops will occur when the kernel tries to connect the call because no local endpoint has been allocated. Fix this by implicitly binding the socket if it is in the RXRPC_CLIENT_UNBOUND state, just like it does for the RXRPC_UNBOUND state. Further, the state should be transitioned to RXRPC_CLIENT_BOUND after this to prevent further attempts to bind it. This can be tested with: #include #include #include #include #include #include static const unsigned char inet6_addr[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0xac, 0x14, 0x14, 0xaa }; int main(void) { struct sockaddr_rxrpc srx; struct cmsghdr *cm; struct msghdr msg; unsigned char control[16]; int fd; memset(&srx, 0, sizeof(srx)); srx.srx_family = 0x21; srx.srx_service = 0; srx.transport_type = AF_INET; srx.transport_len = 0x1c; srx.transport.sin6.sin6_family = AF_INET6; srx.transport.sin6.sin6_port = htons(0x4e22); srx.transport.sin6.sin6_flowinfo = htons(0x4e22); srx.transport.sin6.sin6_scope_id = htons(0xaa3b); memcpy(&srx.transport.sin6.sin6_addr, inet6_addr, 16); cm = (struct cmsghdr *)control; cm->cmsg_len = CMSG_LEN(sizeof(unsigned long)); cm->cmsg_level = SOL_RXRPC; cm->cmsg_type = RXRPC_USER_CALL_ID; *(unsigned long *)CMSG_DATA(cm) = 0; msg.msg_name = NULL; msg.msg_namelen = 0; msg.msg_iov = NULL; msg.msg_iovlen = 0; msg.msg_control = control; msg.msg_controllen = cm->cmsg_len; msg.msg_flags = 0; fd = socket(AF_RXRPC, SOCK_DGRAM, AF_INET); connect(fd, (struct sockaddr *)&srx, sizeof(srx)); sendmsg(fd, &msg, 0); return 0; } Leading to the following oops: BUG: kernel NULL pointer dereference, address: 0000000000000018 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page ... RIP: 0010:rxrpc_connect_call+0x42/0xa01 ... Call Trace: ? mark_held_locks+0x47/0x59 ? __local_bh_enable_ip+0xb6/0xba rxrpc_new_client_call+0x3b1/0x762 ? rxrpc_do_sendmsg+0x3c0/0x92e rxrpc_do_sendmsg+0x3c0/0x92e rxrpc_sendmsg+0x16b/0x1b5 sock_sendmsg+0x2d/0x39 ___sys_sendmsg+0x1a4/0x22a ? release_sock+0x19/0x9e ? reacquire_held_locks+0x136/0x160 ? release_sock+0x19/0x9e ? find_held_lock+0x2b/0x6e ? __lock_acquire+0x268/0xf73 ? rxrpc_connect+0xdd/0xe4 ? __local_bh_enable_ip+0xb6/0xba __sys_sendmsg+0x5e/0x94 do_syscall_64+0x7d/0x1bf entry_SYSCALL_64_after_hwframe+0x49/0xbe Fixes: 2341e0775747 ("rxrpc: Simplify connect() implementation and simplify sendmsg() op") Reported-by: syzbot+7966f2a0b2c7da8939b4@syzkaller.appspotmail.com Signed-off-by: David Howells Reviewed-by: Marc Dionne Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/rxrpc/af_rxrpc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/net/rxrpc/af_rxrpc.c +++ b/net/rxrpc/af_rxrpc.c @@ -405,6 +405,7 @@ static int rxrpc_sendmsg(struct socket * switch (rx->sk.sk_state) { case RXRPC_UNBOUND: + case RXRPC_CLIENT_UNBOUND: rx->srx.srx_family = AF_RXRPC; rx->srx.srx_service = 0; rx->srx.transport_type = SOCK_DGRAM; @@ -429,10 +430,9 @@ static int rxrpc_sendmsg(struct socket * } rx->local = local; - rx->sk.sk_state = RXRPC_CLIENT_UNBOUND; + rx->sk.sk_state = RXRPC_CLIENT_BOUND; /* Fall through */ - case RXRPC_CLIENT_UNBOUND: case RXRPC_CLIENT_BOUND: if (!m->msg_name && test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {