Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp581872pxb; Thu, 21 Oct 2021 05:39:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxWk/GB9yDbaVntlw7Wtp74hzoqplFgP4TpA2XO3DS9IyJfzgFg6RWhT7a3fkleehrDB2FB X-Received: by 2002:a05:6402:4410:: with SMTP id y16mr7333368eda.366.1634819996163; Thu, 21 Oct 2021 05:39:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634819996; cv=none; d=google.com; s=arc-20160816; b=WjMe3NfS06YoJ1izDiJBmL7OKFTh9+ilHBEsOheRqxIkAIUO6MLlI4OWI/axHOyA49 7XjeM8RP9tuxRKav1NCx5FFcJ7R5zYMlBUAFps+cMNOaD0x6BhsI2yaYBTrtb5PSJCob du7Mxd+h9bSzzADg23AgyV8znlyrrvbNUQ9AZ4sr7yBsZu3fbbzIIJB6l55DKW+xfRRN YlUc1567fKbmYsOIL1Fq+yl7bZpKdpcwZANokrfWvsd59EjaHbjOza/81mlhr069stfe Ty0CvL4TT7iAnhIR3uMoevSKRVgmxJ1l/k+qL7ZGWOptslqOPOey46GyxuowfZ5QWlrk JLNQ== 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=Pz9Q5MsaEAhd9vpFjFj0Ubly2C9dI+VhasxldWaXTYg=; b=D9B9ONV3tk6H2qp9RN+BOt2IpctUpjJdjvWs/3eukv0oEIFNIR8qZ2LNF/ICLgpDy7 TW6UhaBsXwCHC5mgYREx8dnQ62MgQzXr2mzgNa2jNEQtjByoCUVqNvql+dYtgz5+uR/a oI2etvm2mrpwmZ5RNr+gu6qZcHc5aHbXp5CVFoT1bKBcAC35x0i/OjdryI/Ho3jDzjFd QSEsGeJwhL8U9tWo78I+V89tWr0zBchMfGX27j3oNrgfuYuw0XO6RebwT+q4jNn40cpE gqfhjXs6ku8Zhu2nohGpTl92JEHGMdhMtukbxF8hkBUNPQ8VzUd6jThZdngl+hFi1zDr cOyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=GvW5U4K8; 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 y2si6722637ejw.689.2021.10.21.05.39.32; Thu, 21 Oct 2021 05:39:56 -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=GvW5U4K8; 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 S231690AbhJUMjx (ORCPT + 99 others); Thu, 21 Oct 2021 08:39:53 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:45174 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231683AbhJUMjs (ORCPT ); Thu, 21 Oct 2021 08:39:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634819852; 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=Pz9Q5MsaEAhd9vpFjFj0Ubly2C9dI+VhasxldWaXTYg=; b=GvW5U4K84UV3t2j/G01jv3yHGqfSYUFjrJGY9LL22ridBo4zONh/60D0HRwLlwsRv7ojZ5 Sa/XmRbhdzG5n+2CYl15AIzN2qh+pp3DaKLd8wHq2S/0Ojivr9/40PvhplqN3GQGZWNCau iUhz4b+dCIo+IkIhqjYfMdnB+pr2UWg= 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-427-dYH93tcNNgOgCE39S4m5GA-1; Thu, 21 Oct 2021 08:37:29 -0400 X-MC-Unique: dYH93tcNNgOgCE39S4m5GA-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 E9E411926DA1; Thu, 21 Oct 2021 12:37:27 +0000 (UTC) Received: from localhost (unknown [10.39.208.31]) by smtp.corp.redhat.com (Postfix) with ESMTP id B5D1B1346F; Thu, 21 Oct 2021 12:37:26 +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 02/10] sock: move sock_copy_peercred() from af_unix Date: Thu, 21 Oct 2021 16:37:06 +0400 Message-Id: <20211021123714.1125384-3-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 SO_PEERCRED can be made to work with other kind of sockets. Signed-off-by: Marc-André Lureau --- include/net/sock.h | 3 +++ net/core/sock.c | 25 +++++++++++++++++++++++++ net/unix/af_unix.c | 26 +------------------------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index 8b12953752e6..d6877df26200 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1819,6 +1819,9 @@ void sock_init_data(struct socket *sock, struct sock *sk); /* Set socket peer PID and credentials with current process. */ void sock_init_peercred(struct sock *sk); +/* Copy peer credentials from peersk */ +void sock_copy_peercred(struct sock *sk, struct sock *peersk); + /* * Socket reference counting postulates. * diff --git a/net/core/sock.c b/net/core/sock.c index 997e8d256e2f..f6b2662824df 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -3214,6 +3214,31 @@ void sock_init_peercred(struct sock *sk) } EXPORT_SYMBOL(sock_init_peercred); +void sock_copy_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 = 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); +} +EXPORT_SYMBOL(sock_copy_peercred); + void lock_sock_nested(struct sock *sk, int subclass) { /* The sk_lock has mutex_lock() semantics here. */ diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index e56f320dff20..9109df1efdb6 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -606,30 +606,6 @@ static void unix_release_sock(struct sock *sk, int embrion) unix_gc(); /* Garbage collect fds */ } -static void copy_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 = 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) { int err; @@ -1460,7 +1436,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, smp_store_release(&newu->addr, otheru->addr); /* Set credentials */ - copy_peercred(sk, other); + sock_copy_peercred(sk, other); sock->state = SS_CONNECTED; sk->sk_state = TCP_ESTABLISHED; -- 2.33.0.721.g106298f7f9