From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CEEF6C4332F for ; Thu, 21 Oct 2021 12:37:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B2C4D610A2 for ; Thu, 21 Oct 2021 12:37:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230285AbhJUMjy (ORCPT ); Thu, 21 Oct 2021 08:39:54 -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