Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp582106pxb; Thu, 21 Oct 2021 05:40:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzlfv4D0JTUwex60MKSg6QWR9BIsiqXMIUY7i7ZWlRA5S20B9VrwSXYpNZNuOLKuBg8Ch23 X-Received: by 2002:a17:907:6d8b:: with SMTP id sb11mr7180676ejc.332.1634820013959; Thu, 21 Oct 2021 05:40:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634820013; cv=none; d=google.com; s=arc-20160816; b=W2z2qfq/JD8/NZ4OpQD1+xya9l5EB1iyAr6ozOa4wdaOfw4MjJw/pPon00Lq85Ukiq 6zbPcuzK9GWl7OzYLJzJOmA6klO2jx5xEFuE1QA/GRYtJ4QButQ0Nk098+oEAgzl86M3 HgAvyoddhIf8YrgdVMDbZRm6V/+6b3R3cGK+VnMwAok7wzrXTNlzHG+cwIDB7cOK87/7 cJssX4oDmpvVK/jwbbU/XKd9S9kTIVAD6NL71aLEtHcC2cZ+M8B1SGFicBeBsRPIk27/ vx9XdJhuFaFbvOgaHhFaBvPlIKll3jU2Zpdip/XwRvSMhQjmE4am9LQege8z07YutOn5 p+EQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0ZhOVxJom5XEifrrroJYIeqiSKqYh7Pqxg6wfpLtvBA=; b=iMh9wVNkrQftzeksKJ/5LCJr+yBbaGK0RFJdET0UB8NkYCxy81umgs9+qjsgh84pid imtOXijhvQC6PQPiCCqvJ+Oo3K6qGw1EbHVccOFPsc5CoR48nPHS7TXtr1yUGwnQj3z6 CZOHjA+ssB41iirN7ijCj6RFlymLjLxSSabrV0CpOXCrF81gfbSCAvP3k+DHgp1Iv1tU 1j1ZbZIUiY/RCBm6Hx4OUQakLqZquI06rDcShlCirjwJBVq+s99Yt7pzSiNdobOcGS6a V1N2VpPLLbPDFFsyeZaMkUAE+AwV3O3JYCT5PcvrlOrwGArTa5mPbVVotZw9n2t3yG2l GMrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XLtPv3Na; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u1si8536857edp.348.2021.10.21.05.39.50; Thu, 21 Oct 2021 05:40:13 -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=@redhat.com header.s=mimecast20190719 header.b=XLtPv3Na; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231704AbhJUMkB (ORCPT + 99 others); Thu, 21 Oct 2021 08:40:01 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:29463 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230359AbhJUMj4 (ORCPT ); Thu, 21 Oct 2021 08:39:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634819860; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0ZhOVxJom5XEifrrroJYIeqiSKqYh7Pqxg6wfpLtvBA=; b=XLtPv3NalarqSe/e8mmyuBR4+GbqxS5jV6tDEZMCkUREILXXGQCCz49x9wggejFMPpv/Ac pdYAWgtamUKXuVudp7dY1cWbJSq/3/yCYaTnUf1pZBMYiHP1BUNMMfSbuXoG94n+nztogy QAVvpUlNQqKlhrUd3BcS9gmJGsSY+/M= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-386-O2CjlmAOOXu0SSquHHgTJA-1; Thu, 21 Oct 2021 08:37:39 -0400 X-MC-Unique: O2CjlmAOOXu0SSquHHgTJA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4C93B1926DA0; Thu, 21 Oct 2021 12:37:38 +0000 (UTC) Received: from localhost (unknown [10.39.208.31]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4B32C1346F; Thu, 21 Oct 2021 12:37:36 +0000 (UTC) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, sgarzare@redhat.com, davem@davemloft.net, kuba@kernel.org, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Subject: [PATCH 04/10] sock: add sock_swap_peercred Date: Thu, 21 Oct 2021 16:37:08 +0400 Message-Id: <20211021123714.1125384-5-marcandre.lureau@redhat.com> In-Reply-To: <20211021123714.1125384-1-marcandre.lureau@redhat.com> References: <20211021123714.1125384-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Signed-off-by: Marc-André Lureau --- include/net/sock.h | 3 +++ net/core/sock.c | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/net/sock.h b/include/net/sock.h index d6877df26200..635d270c3a65 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1822,6 +1822,9 @@ void sock_init_peercred(struct sock *sk); /* Copy peer credentials from peersk */ void sock_copy_peercred(struct sock *sk, struct sock *peersk); +/* Swap socket credentials */ +void sock_swap_peercred(struct sock *sk, struct sock *peersk); + /* * Socket reference counting postulates. * diff --git a/net/core/sock.c b/net/core/sock.c index f6b2662824df..365b63afa915 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -3239,6 +3239,30 @@ void sock_copy_peercred(struct sock *sk, struct sock *peersk) } EXPORT_SYMBOL(sock_copy_peercred); +void sock_swap_peercred(struct sock *sk, struct sock *peersk) +{ + 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 = peersk->sk_peer_pid; + sk->sk_peer_cred = peersk->sk_peer_cred; + peersk->sk_peer_pid = old_pid; + peersk->sk_peer_cred = old_cred; + + spin_unlock(&sk->sk_peer_lock); + spin_unlock(&peersk->sk_peer_lock); +} +EXPORT_SYMBOL(sock_swap_peercred); + void lock_sock_nested(struct sock *sk, int subclass) { /* The sk_lock has mutex_lock() semantics here. */ -- 2.33.0.721.g106298f7f9