Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp879737ybl; Fri, 24 Jan 2020 11:12:44 -0800 (PST) X-Google-Smtp-Source: APXvYqx6dGMZTGkYJNGegKltTrmBN9uN4fJ4Z+zMerkj+VMxomndOMgqNGzFxTdNpTdZicOMALwy X-Received: by 2002:aca:ec93:: with SMTP id k141mr191413oih.145.1579893164664; Fri, 24 Jan 2020 11:12:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579893164; cv=none; d=google.com; s=arc-20160816; b=yMTX6LTaVN9Vs8rvftT+0jUkgCzTJQaH8RjFSjNqmo0blBhJ4ZNVC7drV2bLyQmAtL 1WaSLpUhevunSsh98POeJVLyvyvY2YUSczVtPTlDDGyZxlJKrNxHl9N4J9JPYmNRYcdn 7K1EKgzWvlVIdW+TPkfkJLZX7FzMFVHxo3rIyeJrnht4BsVVkR97ccCYGcl3+Xd9Mhd4 /h87R48B+1ViNGEBHNe5l7856/yiajALoC2ODf51a41LlurFA2P4yk8GUKGVVqcEg0In EAOQzCt5tYSx6AqDBdAQbNWFhqejLjUesABsvuTBjUF1c0/hSLrbRmz4BwS8+u3XXBID H3JQ== 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=VmoSOvAeTI80kgrnJfgIzitKAMvbW5H8u2dxZxu9KV8=; b=KTq0uRjGufbUOH230o1tVjt+4wm3EQQ4H6fBSbvK14SH/oOHU9CsMWw2E6C3Fe890B fIkJNxIY9nHjwW4AUe41Mo58hMKVfB8LJ+7y5yHtGDLSn18r9PJrn4H9pc/9C+nppue6 ypyPQtq6uq30eQqyFDP55xC9Nz7v5hOxCdgAScKAbJmw3jK8styv+GeHJaeLvRi2w87M zTa9vNqjz+/0NoK+IjYsRJ0ySHnEY4ispHlQZUrDVtp1CVI0n32T4biTcZxQR25EbUHf lsTHI0BverZnXmDwFATZsN021848nQq4Fb0FDbDYGvTkdrMRbj1UPK5fAnYo7fiVMuQy 4Kxg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=I5TfdPVx; 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 w2si3286039otk.126.2020.01.24.11.12.32; Fri, 24 Jan 2020 11:12:44 -0800 (PST) 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=I5TfdPVx; 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 S2404630AbgAXLdm (ORCPT + 99 others); Fri, 24 Jan 2020 06:33:42 -0500 Received: from mail.kernel.org ([198.145.29.99]:53316 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404620AbgAXLdl (ORCPT ); Fri, 24 Jan 2020 06:33:41 -0500 Received: from localhost (ip-213-127-102-57.ip.prioritytelecom.net [213.127.102.57]) (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 F35822075D; Fri, 24 Jan 2020 11:33:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579865620; bh=MK+0H2m0NwaFC2Z2/WNt1A4Nh/a09D9UD1TDwoJi0r8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I5TfdPVxbsB+FMNVNBclKXNMHZoVIW3oO2/zmbFOSmfKo/bv+Lxa+n7sh0JxdXVjk y8BL8oMrMdTWdDpzY2OnEfQ8mKDHWhqTLNVd1jm1+rqmm1WIXjQ2iMd2FF7vccnxzT FEdHQ6zAVz13oQEofjtY2laMzfTbfk5x8AcHytto= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ying Xu , Marcelo Ricardo Leitner , Xin Long , Neil Horman , Jakub Kicinski , Sasha Levin Subject: [PATCH 4.19 595/639] sctp: add chunks to sk_backlog when the newsk sk_socket is not set Date: Fri, 24 Jan 2020 10:32:45 +0100 Message-Id: <20200124093203.915043986@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200124093047.008739095@linuxfoundation.org> References: <20200124093047.008739095@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: Xin Long [ Upstream commit 819be8108fded0b9e710bbbf81193e52f7bab2f7 ] This patch is to fix a NULL-ptr deref in selinux_socket_connect_helper: [...] kasan: GPF could be caused by NULL-ptr deref or user memory access [...] RIP: 0010:selinux_socket_connect_helper+0x94/0x460 [...] Call Trace: [...] selinux_sctp_bind_connect+0x16a/0x1d0 [...] security_sctp_bind_connect+0x58/0x90 [...] sctp_process_asconf+0xa52/0xfd0 [sctp] [...] sctp_sf_do_asconf+0x785/0x980 [sctp] [...] sctp_do_sm+0x175/0x5a0 [sctp] [...] sctp_assoc_bh_rcv+0x285/0x5b0 [sctp] [...] sctp_backlog_rcv+0x482/0x910 [sctp] [...] __release_sock+0x11e/0x310 [...] release_sock+0x4f/0x180 [...] sctp_accept+0x3f9/0x5a0 [sctp] [...] inet_accept+0xe7/0x720 It was caused by that the 'newsk' sk_socket was not set before going to security sctp hook when processing asconf chunk with SCTP_PARAM_ADD_IP or SCTP_PARAM_SET_PRIMARY: inet_accept()-> sctp_accept(): lock_sock(): lock listening 'sk' do_softirq(): sctp_rcv(): <-- [1] asconf chunk arrives and enqueued in 'sk' backlog sctp_sock_migrate(): set asoc's sk to 'newsk' release_sock(): sctp_backlog_rcv(): lock 'newsk' sctp_process_asconf() <-- [2] unlock 'newsk' sock_graft(): set sk_socket <-- [3] As it shows, at [1] the asconf chunk would be put into the listening 'sk' backlog, as accept() was holding its sock lock. Then at [2] asconf would get processed with 'newsk' as asoc's sk had been set to 'newsk'. However, 'newsk' sk_socket is not set until [3], while selinux_sctp_bind_connect() would deref it, then kernel crashed. Here to fix it by adding the chunk to sk_backlog until newsk sk_socket is set when .accept() is done. Note that sk->sk_socket can be NULL when the sock is closed, so SOCK_DEAD flag is also needed to check in sctp_newsk_ready(). Thanks to Ondrej for reviewing the code. Fixes: d452930fd3b9 ("selinux: Add SCTP support") Reported-by: Ying Xu Suggested-by: Marcelo Ricardo Leitner Signed-off-by: Xin Long Acked-by: Marcelo Ricardo Leitner Acked-by: Neil Horman Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- include/net/sctp/sctp.h | 5 +++++ net/sctp/input.c | 12 +++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/net/sctp/sctp.h b/include/net/sctp/sctp.h index 2abbc15824af9..2c6570e6fcfec 100644 --- a/include/net/sctp/sctp.h +++ b/include/net/sctp/sctp.h @@ -625,4 +625,9 @@ static inline __u32 sctp_min_frag_point(struct sctp_sock *sp, __u16 datasize) return sctp_mtu_payload(sp, SCTP_DEFAULT_MINSEGMENT, datasize); } +static inline bool sctp_newsk_ready(const struct sock *sk) +{ + return sock_flag(sk, SOCK_DEAD) || sk->sk_socket; +} + #endif /* __net_sctp_h__ */ diff --git a/net/sctp/input.c b/net/sctp/input.c index bfe29158afccb..f64d882c86985 100644 --- a/net/sctp/input.c +++ b/net/sctp/input.c @@ -255,7 +255,7 @@ int sctp_rcv(struct sk_buff *skb) bh_lock_sock(sk); } - if (sock_owned_by_user(sk)) { + if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) { if (sctp_add_backlog(sk, skb)) { bh_unlock_sock(sk); sctp_chunk_free(chunk); @@ -333,7 +333,7 @@ int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb) local_bh_disable(); bh_lock_sock(sk); - if (sock_owned_by_user(sk)) { + if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) { if (sk_add_backlog(sk, skb, sk->sk_rcvbuf)) sctp_chunk_free(chunk); else @@ -348,7 +348,13 @@ int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb) if (backloged) return 0; } else { - sctp_inq_push(inqueue, chunk); + if (!sctp_newsk_ready(sk)) { + if (!sk_add_backlog(sk, skb, sk->sk_rcvbuf)) + return 0; + sctp_chunk_free(chunk); + } else { + sctp_inq_push(inqueue, chunk); + } } done: -- 2.20.1