From patchwork Sun May 2 21:11:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xin Long X-Patchwork-Id: 430555 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ACA05C433ED for ; Sun, 2 May 2021 21:12:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 812DA61221 for ; Sun, 2 May 2021 21:12:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232570AbhEBVMw (ORCPT ); Sun, 2 May 2021 17:12:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232338AbhEBVMv (ORCPT ); Sun, 2 May 2021 17:12:51 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AC76C06174A; Sun, 2 May 2021 14:12:00 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id p12so2326383pgj.10; Sun, 02 May 2021 14:12:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=fDNxOY+/09Nl9KfuVzTsfsTgmcyaSu6t9/8XNUyUuFo=; b=RQbGtj4EbLLSBZJ4+DigeYXzy1U++mVcvluqGiVVJ8Kwkefm4pn5/TsPy28CtrzQPL vuTV3hrnjRcGJqc2g8N1cTf9m4LYJgt3nIIFdOlnjrG1m9VU9SHYbjeGHFlfcytI0wr7 76sGsll33jts8/CwxCXTSZ7N8zqzL5S48pwypxMJliYqPXcPFIxKppFOerAaK6PhyjjK 9zdUY6HoN+eSavbTwS3dRUpWDu+ehDI1hpN0vOtg9oqUd4ac1nZtY3+a6O5tV/Utg/9x +31ewdxZwwSN+YwxgKJjv7P9PuDMwb+WLtlyFP/oQ1dPgJwlvgAQVVnaaH/JTFTV9KIB EXmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=fDNxOY+/09Nl9KfuVzTsfsTgmcyaSu6t9/8XNUyUuFo=; b=Aa6vBR3+ASCUnmmQSetqIv6P6sbpTvv+zcgz3Z3s2+qocZfZ94HyetEn/fxYACqgHw YkHgLsj0doItJBaU3kAcG/uYFJOGZv/wbbDPLloQNXGmVuR4zHn5n23s+f+ZgH0WwAzk DfXV0n7WBYsYaCoGZL+46kj2yWMxjPcsx72YtxtGyIzJqwK3i7DxHxa1q1XTMa3Rv/W3 kbTDZsenZ0YEpz/2DRMggSVVo4HgEqDKtyjt9nVD3b2nHnaGs+Lx4t3q2Akk6kgT160z KkI6lTD9lHIymW3/CbLmzbmTzth1Wtgxgv68958RGwN9C+98NLlXZ6VHKUWdVx+60JDA 5mlA== X-Gm-Message-State: AOAM532/lcjPIh7CrPZOEptaXne3qK/6csC7SA7fLd5rcqXb/gI5R64r Xx1jim7m3FlHxWf8btcAgJGbfNeSpYZk7X1K X-Google-Smtp-Source: ABdhPJwLrQQZHA4vVnleLKlDDUuYmZ4uMjJntClNXlBJUA36nN5LStZcPDKg9Zo9R6WWjnKJjE1gfg== X-Received: by 2002:a63:2c14:: with SMTP id s20mr14887638pgs.72.1619989919274; Sun, 02 May 2021 14:11:59 -0700 (PDT) Received: from localhost ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id b13sm7680573pjl.38.2021.05.02.14.11.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 02 May 2021 14:11:58 -0700 (PDT) From: Xin Long To: network dev , linux-sctp@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, Marcelo Ricardo Leitner , Or Cohen Subject: [PATCH net 1/2] Revert "net/sctp: fix race condition in sctp_destroy_sock" Date: Mon, 3 May 2021 05:11:41 +0800 Message-Id: <4dc7d7dd4bcf7122604ccb52a5c747c3fb9101c5.1619989856.git.lucien.xin@gmail.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: References: In-Reply-To: References: Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org This reverts commit b166a20b07382b8bc1dcee2a448715c9c2c81b5b. This one has to be reverted as it introduced a dead lock, as syzbot reported: CPU0 CPU1 ---- ---- lock(&net->sctp.addr_wq_lock); lock(slock-AF_INET6); lock(&net->sctp.addr_wq_lock); lock(slock-AF_INET6); CPU0 is the thread of sctp_addr_wq_timeout_handler(), and CPU1 is that of sctp_close(). The original issue this commit fixed will be fixed in the next patch. Reported-by: syzbot+959223586843e69a2674@syzkaller.appspotmail.com Signed-off-by: Xin Long --- net/sctp/socket.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index b7b9013..76a388b5 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -1520,9 +1520,11 @@ static void sctp_close(struct sock *sk, long timeout) /* Supposedly, no process has access to the socket, but * the net layers still may. + * Also, sctp_destroy_sock() needs to be called with addr_wq_lock + * held and that should be grabbed before socket lock. */ - local_bh_disable(); - bh_lock_sock(sk); + spin_lock_bh(&net->sctp.addr_wq_lock); + bh_lock_sock_nested(sk); /* Hold the sock, since sk_common_release() will put sock_put() * and we have just a little more cleanup. @@ -1531,7 +1533,7 @@ static void sctp_close(struct sock *sk, long timeout) sk_common_release(sk); bh_unlock_sock(sk); - local_bh_enable(); + spin_unlock_bh(&net->sctp.addr_wq_lock); sock_put(sk); @@ -4991,6 +4993,9 @@ static int sctp_init_sock(struct sock *sk) sk_sockets_allocated_inc(sk); sock_prot_inuse_add(net, sk->sk_prot, 1); + /* Nothing can fail after this block, otherwise + * sctp_destroy_sock() will be called without addr_wq_lock held + */ if (net->sctp.default_auto_asconf) { spin_lock(&sock_net(sk)->sctp.addr_wq_lock); list_add_tail(&sp->auto_asconf_list, @@ -5025,9 +5030,7 @@ static void sctp_destroy_sock(struct sock *sk) if (sp->do_auto_asconf) { sp->do_auto_asconf = 0; - spin_lock_bh(&sock_net(sk)->sctp.addr_wq_lock); list_del(&sp->auto_asconf_list); - spin_unlock_bh(&sock_net(sk)->sctp.addr_wq_lock); } sctp_endpoint_free(sp->ep); local_bh_disable(); From patchwork Sun May 2 21:11:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xin Long X-Patchwork-Id: 430770 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D716CC433B4 for ; Sun, 2 May 2021 21:12:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B5CF461221 for ; Sun, 2 May 2021 21:12:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232592AbhEBVNB (ORCPT ); Sun, 2 May 2021 17:13:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232338AbhEBVM7 (ORCPT ); Sun, 2 May 2021 17:12:59 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E312C06174A; Sun, 2 May 2021 14:12:08 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id q9so2319180pgl.13; Sun, 02 May 2021 14:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=6Tew3fIKE8mp1DfEhtGtLfIGo/Xh6uMnhvGW+TPG/Jk=; b=f25eNO1lxxCmddvV0ThPJHHIvRS/nGjlcMg3EpcGD6ItJTLX8wtHkPFF9nBxX9fxhL QBoJAu9rUaxQx/rBG5Wiu3tWpbYKkuYhLr3zNejha97EVrwXLGDtNCOzZ6BsxXqtc09X jYffODzpMyf5xy0fyaH+PlyrJqu80kuSk75x7MDmjRnVDANDXyprSX4oRJaxCbvnnMkU pr47B8KDCWnxuCc77qyKXD1k+QaxNt6XgAVPN7UBhF1qtathH8e9iaZvj6OxDoFTQKe6 6WviJRz2xfyBqDs7hJIwDSmrQgJs8NIITZZ4VSV8gvZqNj2wCgVzi5a8zew1m6WwBVxp 1eiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=6Tew3fIKE8mp1DfEhtGtLfIGo/Xh6uMnhvGW+TPG/Jk=; b=sZRVd80yM91XzIkPEg9mOI6Mfx9OP/lVYnT09s/FI+n833PVuKAuw7l2U38nbpHM3p RPVDNVArOaizIYmMsNhdc7AI41BBUIJRzsbVGckT7p/g4DsdM6FPLqHssi/WDix7LH21 Ux3F7XR7a4RIMjbQcveymhvYlMUwg8G3fSa7Qx46A4169lfxAxNolEdJZjBrSog6IhGs g4oXuxYSgjOEmyjXmjCOYfsnMIXsYCrkuAv2Npu9Hz/wilcQP8i9QNYGsBxJKPgd+YU2 snxZoBCP3Zm5mIhz9QKzZIAHsS7231oSNJcLzgrEXG8/j1zNu4VjdZzmFBJlYSQz34be O1QA== X-Gm-Message-State: AOAM531xZ+pt1wEj5S/zvWgcK86dV2rmvxrWDTyOj2TKosgryC2f+0Mf nTAF8/WVyJYWKoA4Usdau3yUrah5qqsJaOok X-Google-Smtp-Source: ABdhPJxkY9gnYau2UwZDhuNmcIj1pJZmcMVqmamWZfCMDBEV0kIcQ+iWzxoFLpkokXnsOXjIQg5ZXg== X-Received: by 2002:a63:7986:: with SMTP id u128mr15324163pgc.223.1619989927368; Sun, 02 May 2021 14:12:07 -0700 (PDT) Received: from localhost ([209.132.188.80]) by smtp.gmail.com with ESMTPSA id ng6sm15706467pjb.14.2021.05.02.14.12.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 02 May 2021 14:12:06 -0700 (PDT) From: Xin Long To: network dev , linux-sctp@vger.kernel.org Cc: davem@davemloft.net, kuba@kernel.org, Marcelo Ricardo Leitner , Or Cohen Subject: [PATCH net 2/2] sctp: delay auto_asconf init until binding the first addr Date: Mon, 3 May 2021 05:11:42 +0800 Message-Id: <8f397eb9b1e75b4de26fcd76071aa3718ab70a2c.1619989856.git.lucien.xin@gmail.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <4dc7d7dd4bcf7122604ccb52a5c747c3fb9101c5.1619989856.git.lucien.xin@gmail.com> References: <4dc7d7dd4bcf7122604ccb52a5c747c3fb9101c5.1619989856.git.lucien.xin@gmail.com> In-Reply-To: References: Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org As Or Cohen described: If sctp_destroy_sock is called without sock_net(sk)->sctp.addr_wq_lock held and sp->do_auto_asconf is true, then an element is removed from the auto_asconf_splist without any proper locking. This can happen in the following functions: 1. In sctp_accept, if sctp_sock_migrate fails. 2. In inet_create or inet6_create, if there is a bpf program attached to BPF_CGROUP_INET_SOCK_CREATE which denies creation of the sctp socket. This patch is to fix it by moving the auto_asconf init out of sctp_init_sock(), by which inet_create()/inet6_create() won't need to operate it in sctp_destroy_sock() when calling sk_common_release(). It also makes more sense to do auto_asconf init while binding the first addr, as auto_asconf actually requires an ANY addr bind, see it in sctp_addr_wq_timeout_handler(). This addresses CVE-2021-23133. Fixes: 610236587600 ("bpf: Add new cgroup attach type to enable sock modifications") Reported-by: Or Cohen Signed-off-by: Xin Long --- net/sctp/socket.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index 76a388b5..40f9f6c 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -357,6 +357,18 @@ static struct sctp_af *sctp_sockaddr_af(struct sctp_sock *opt, return af; } +static void sctp_auto_asconf_init(struct sctp_sock *sp) +{ + struct net *net = sock_net(&sp->inet.sk); + + if (net->sctp.default_auto_asconf) { + spin_lock(&net->sctp.addr_wq_lock); + list_add_tail(&sp->auto_asconf_list, &net->sctp.auto_asconf_splist); + spin_unlock(&net->sctp.addr_wq_lock); + sp->do_auto_asconf = 1; + } +} + /* Bind a local address either to an endpoint or to an association. */ static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) { @@ -418,8 +430,10 @@ static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) return -EADDRINUSE; /* Refresh ephemeral port. */ - if (!bp->port) + if (!bp->port) { bp->port = inet_sk(sk)->inet_num; + sctp_auto_asconf_init(sp); + } /* Add the address to the bind address list. * Use GFP_ATOMIC since BHs will be disabled. @@ -4993,19 +5007,6 @@ static int sctp_init_sock(struct sock *sk) sk_sockets_allocated_inc(sk); sock_prot_inuse_add(net, sk->sk_prot, 1); - /* Nothing can fail after this block, otherwise - * sctp_destroy_sock() will be called without addr_wq_lock held - */ - if (net->sctp.default_auto_asconf) { - spin_lock(&sock_net(sk)->sctp.addr_wq_lock); - list_add_tail(&sp->auto_asconf_list, - &net->sctp.auto_asconf_splist); - sp->do_auto_asconf = 1; - spin_unlock(&sock_net(sk)->sctp.addr_wq_lock); - } else { - sp->do_auto_asconf = 0; - } - local_bh_enable(); return 0; @@ -9401,6 +9402,8 @@ static int sctp_sock_migrate(struct sock *oldsk, struct sock *newsk, return err; } + sctp_auto_asconf_init(newsp); + /* Move any messages in the old socket's receive queue that are for the * peeled off association to the new socket's receive queue. */