Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp2264806pxb; Fri, 8 Oct 2021 04:30:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxDZHaiSv5DYIS/0H4RJy9Prt/mPAt3xddmTcFGx7tjL7xTzJd+aQM1BD/rPRb1T9qBCqsn X-Received: by 2002:a17:902:ea05:b0:13f:4b5:cda2 with SMTP id s5-20020a170902ea0500b0013f04b5cda2mr9095677plg.86.1633692645213; Fri, 08 Oct 2021 04:30:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633692645; cv=none; d=google.com; s=arc-20160816; b=SFQ1GMqRKcjDBkgplS/EXTZP6VKjs5XP5BqOJNRYR9rk+7wjcRiOTEonD4AKTatV+4 ya5x2G/tHpbOfsufXKkp5qbtX/DpxRZ+7OQSz96Xj/1PXsL7CDyCXvWY6WzOnna7TGLX Lkez0o10hmxZjVJzTiODz0mRCJYPIVlJ/oozsrzHfC921aYDJOf79yBcT9ZS51eLQa6c afKLSIt0S6odIU9pDFiv/FERmyyI8owjjobnFliQImhuhuaCQT5Xm7Ghlab4p9foxvGy ui74olfr66rmLV7mxQY2OUVl/ezt6i9yKVwDh0ZKep5BQA1kla9MhNF/DNOXyioCwc+8 8Wcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=97v66jcu3YG2oZTKww8KRkuYIH86ZDPuv2DFjQ5eaDs=; b=1BmA/ZYAuingXLSMcM4wm0WYVInObBnFmHlws7hhwQ1TkAYf6IyghCyzzAiZIoCcbt jqdWDGw1hqdooFMJvl/WCWZe/AYKDyXqWI757RKUdoTSd6Gu9q1CJEw5OLwZKSe/UfEs WUf1g7a5nmnp2SympOD+v7q5EIRgYERQxOqOaQCFSpT6rdwlZyA0Riq9De2m7p/6nSZ0 XJOHrJSksP+kylYAT6roWXpGZJI8oc0v40Aq96fU+xgmhO1QSDXb7ZSw2CJDn7JtqlSf EBeOC5LCKw0GtyU4BjN3CKydjd4gBEkGhcnb4jiZH8zmLh62RiDN2vlanpNaGaNFz8bZ OcPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UHSIWg+W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k145si21001320pfd.22.2021.10.08.04.30.32; Fri, 08 Oct 2021 04:30:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UHSIWg+W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240225AbhJHLac (ORCPT + 99 others); Fri, 8 Oct 2021 07:30:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:57706 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240180AbhJHLac (ORCPT ); Fri, 8 Oct 2021 07:30:32 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B31096101A; Fri, 8 Oct 2021 11:28:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1633692517; bh=EAPRvZvvb1JKDJdFP7gSY82qbT448VYa1zeJjPVVnGg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UHSIWg+W2R15cnss13UuyD12FzcfMla54jK6SroWE+cXctRVSXNimp4oTVPVLSCbN odRREZVTZ1WcTcmiFqVwvxK3RWBk26/nS237LeA9PeJHNxhP/eVb+Ff/oTI37XgYXP bQgcLG3xkIYaP6LvNW49Q8F9me7F0BgaYPvsAnbA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , Jann Horn , "Eric W. Biederman" , Luiz Augusto von Dentz , Marcel Holtmann , "David S. Miller" Subject: [PATCH 4.4 1/7] af_unix: fix races in sk_peer_pid and sk_peer_cred accesses Date: Fri, 8 Oct 2021 13:27:33 +0200 Message-Id: <20211008112713.564885585@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211008112713.515980393@linuxfoundation.org> References: <20211008112713.515980393@linuxfoundation.org> User-Agent: quilt/0.66 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Dumazet commit 35306eb23814444bd4021f8a1c3047d3cb0c8b2b upstream. Jann Horn reported that SO_PEERCRED and SO_PEERGROUPS implementations are racy, as af_unix can concurrently change sk_peer_pid and sk_peer_cred. In order to fix this issue, this patch adds a new spinlock that needs to be used whenever these fields are read or written. Jann also pointed out that l2cap_sock_get_peer_pid_cb() is currently reading sk->sk_peer_pid which makes no sense, as this field is only possibly set by AF_UNIX sockets. We will have to clean this in a separate patch. This could be done by reverting b48596d1dc25 "Bluetooth: L2CAP: Add get_peer_pid callback" or implementing what was truly expected. Fixes: 109f6e39fa07 ("af_unix: Allow SO_PEERCRED to work across namespaces.") Signed-off-by: Eric Dumazet Reported-by: Jann Horn Cc: Eric W. Biederman Cc: Luiz Augusto von Dentz Cc: Marcel Holtmann Signed-off-by: David S. Miller [backport note: 4.4 and 4.9 don't have SO_PEERGROUPS, only SO_PEERCRED] [backport note: got rid of sk_get_peer_cred(), no users in 4.4/4.9] Signed-off-by: Jann Horn Signed-off-by: Greg Kroah-Hartman --- include/net/sock.h | 2 ++ net/core/sock.c | 12 +++++++++--- net/unix/af_unix.c | 34 ++++++++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 9 deletions(-) --- a/include/net/sock.h +++ b/include/net/sock.h @@ -429,8 +429,10 @@ struct sock { #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) __u32 sk_cgrp_prioidx; #endif + spinlock_t sk_peer_lock; struct pid *sk_peer_pid; const struct cred *sk_peer_cred; + long sk_rcvtimeo; long sk_sndtimeo; struct timer_list sk_timer; --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1014,7 +1014,6 @@ set_rcvbuf: } EXPORT_SYMBOL(sock_setsockopt); - static void cred_to_ucred(struct pid *pid, const struct cred *cred, struct ucred *ucred) { @@ -1174,7 +1173,11 @@ int sock_getsockopt(struct socket *sock, struct ucred peercred; if (len > sizeof(peercred)) len = sizeof(peercred); + + spin_lock(&sk->sk_peer_lock); cred_to_ucred(sk->sk_peer_pid, sk->sk_peer_cred, &peercred); + spin_unlock(&sk->sk_peer_lock); + if (copy_to_user(optval, &peercred, len)) return -EFAULT; goto lenout; @@ -1467,9 +1470,10 @@ void sk_destruct(struct sock *sk) sk->sk_frag.page = NULL; } - if (sk->sk_peer_cred) - put_cred(sk->sk_peer_cred); + /* We do not need to acquire sk->sk_peer_lock, we are the last user. */ + put_cred(sk->sk_peer_cred); put_pid(sk->sk_peer_pid); + if (likely(sk->sk_net_refcnt)) put_net(sock_net(sk)); sk_prot_free(sk->sk_prot_creator, sk); @@ -2442,6 +2446,8 @@ void sock_init_data(struct socket *sock, sk->sk_peer_pid = NULL; sk->sk_peer_cred = NULL; + spin_lock_init(&sk->sk_peer_lock); + sk->sk_write_pending = 0; sk->sk_rcvlowat = 1; sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT; --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -594,20 +594,42 @@ static void unix_release_sock(struct soc static void init_peercred(struct sock *sk) { - put_pid(sk->sk_peer_pid); - if (sk->sk_peer_cred) - put_cred(sk->sk_peer_cred); + const struct cred *old_cred; + struct pid *old_pid; + + spin_lock(&sk->sk_peer_lock); + old_pid = sk->sk_peer_pid; + old_cred = sk->sk_peer_cred; sk->sk_peer_pid = get_pid(task_tgid(current)); sk->sk_peer_cred = get_current_cred(); + spin_unlock(&sk->sk_peer_lock); + + put_pid(old_pid); + put_cred(old_cred); } static void copy_peercred(struct sock *sk, struct sock *peersk) { - put_pid(sk->sk_peer_pid); - if (sk->sk_peer_cred) - put_cred(sk->sk_peer_cred); + const struct cred *old_cred; + struct pid *old_pid; + + if (sk < peersk) { + spin_lock(&sk->sk_peer_lock); + spin_lock_nested(&peersk->sk_peer_lock, SINGLE_DEPTH_NESTING); + } else { + spin_lock(&peersk->sk_peer_lock); + spin_lock_nested(&sk->sk_peer_lock, SINGLE_DEPTH_NESTING); + } + old_pid = sk->sk_peer_pid; + old_cred = sk->sk_peer_cred; sk->sk_peer_pid = get_pid(peersk->sk_peer_pid); sk->sk_peer_cred = get_cred(peersk->sk_peer_cred); + + spin_unlock(&sk->sk_peer_lock); + spin_unlock(&peersk->sk_peer_lock); + + put_pid(old_pid); + put_cred(old_cred); } static int unix_listen(struct socket *sock, int backlog)