Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp581687pxb; Thu, 21 Oct 2021 05:39:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZb8LBhXS3y5ikfu76szD1foLEO7h2JhphwclqufHnotTCyJ2t24gRivTn1EqXtg17Qo9Z X-Received: by 2002:a17:906:254b:: with SMTP id j11mr6939672ejb.513.1634819981158; Thu, 21 Oct 2021 05:39:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634819981; cv=none; d=google.com; s=arc-20160816; b=gaXEihhYxwz9F/Ow7QAS56/80yr5wtR3CGQupsRuknrqCo3Bc+mQaI2P1HwEb3PIIl Y9RsxTUFFQKlx2SfD8QOFA7KleC3T5jJ2iK/vpf/oZpnl7f+HjlQGPnoLTO6Gy3RIpg3 nOqBeppvnRd4sBzKaKIDgjWwPeGPYmwl2XPDCEYVP/IgY/omBhmAduIv+Rfo8TZFvH17 H0PTATHe3A4ZBdOHzzBBPK2Ptqf+H8oPW8r2PMpUjKl8qyI/sVjjsEiIdCUFIW8CkbOJ r7pM2wJNMWgwya8IeaX2Oi65awEt7XmALrIipZPGMetvbQRQlBtwOJNbTDOgY4CXzp2o oTZA== 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=RxvTAezPJGB8dfIBumv6gkdy8ZhbMjjJ/oV0H/w85HE=; b=vst0FScDRqkgdnNtOu4RmUrXuPGdNMK//nAiMQh8ncW07FHMn/tGoOwd7ZzM7LfYhz M7IgbeyiUUxctokLQ6sybbNrs9Il5ecpGzePNamrPSGiKVgkclxK1P4aHTL9GI0rnFgG 2OGjFUnm0rYfuoCxiVjniZwlS42+c4c/SRAOBv+/h31X4UDq9eQeyoH2GqCIoAz4BrNn 8M4solaTh6R1oMePSDzJxZBc/tzgl+fZYyqQmgRa/JR2jy5/0ubrYara3UR4pU64sSs/ LGa/Ax9s+mxbjOnWmoLiTf0zDMt8BToHuimm5/JRqTvkgOamAJjQmC/0+PLCgDmNsVFB 4/0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FqpUbmEo; 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 o22si9439640edc.7.2021.10.21.05.39.17; Thu, 21 Oct 2021 05:39:41 -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=FqpUbmEo; 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 S231727AbhJUMjp (ORCPT + 99 others); Thu, 21 Oct 2021 08:39:45 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:42313 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231697AbhJUMjn (ORCPT ); Thu, 21 Oct 2021 08:39:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634819847; 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=RxvTAezPJGB8dfIBumv6gkdy8ZhbMjjJ/oV0H/w85HE=; b=FqpUbmEougJey9rE+IAS7B+QFKfvVfnjM628eqybu3HYmB3Pg/BHAbO82CReAnFN/nPYrg LcSTPsnt/yRR+qsNLQfIjuPerAyc5xdqSA5+zRBVa+/YTdLgjYXv8IqxjNwGNdiW1dnfSJ 3bRH3KbCCjwdRwrHLyTLxD+F/b72+wU= 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-257-Lc6LZAh4OcqUcZI2r6AneQ-1; Thu, 21 Oct 2021 08:37:23 -0400 X-MC-Unique: Lc6LZAh4OcqUcZI2r6AneQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C0E7E8797E5; Thu, 21 Oct 2021 12:37:22 +0000 (UTC) Received: from localhost (unknown [10.39.208.31]) by smtp.corp.redhat.com (Postfix) with ESMTP id CA4CF5C3E0; Thu, 21 Oct 2021 12:37:21 +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 01/10] sock: move sock_init_peercred() from af_unix Date: Thu, 21 Oct 2021 16:37:05 +0400 Message-Id: <20211021123714.1125384-2-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.16 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 | 17 +++++++++++++++++ net/unix/af_unix.c | 24 ++++-------------------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index ea6fbc88c8f9..8b12953752e6 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1816,6 +1816,9 @@ void sk_common_release(struct sock *sk); /* Initialise core socket variables */ 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); + /* * Socket reference counting postulates. * diff --git a/net/core/sock.c b/net/core/sock.c index c1601f75ec4b..997e8d256e2f 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -3197,6 +3197,23 @@ void sock_init_data(struct socket *sock, struct sock *sk) } EXPORT_SYMBOL(sock_init_data); +void sock_init_peercred(struct sock *sk) +{ + 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); +} +EXPORT_SYMBOL(sock_init_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 89f9e85ae970..e56f320dff20 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -606,22 +606,6 @@ static void unix_release_sock(struct sock *sk, int embrion) unix_gc(); /* Garbage collect fds */ } -static void init_peercred(struct sock *sk) -{ - 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) { const struct cred *old_cred; @@ -666,7 +650,7 @@ static int unix_listen(struct socket *sock, int backlog) sk->sk_max_ack_backlog = backlog; sk->sk_state = TCP_LISTEN; /* set credentials so connect can copy them */ - init_peercred(sk); + sock_init_peercred(sk); err = 0; out_unlock: @@ -1446,7 +1430,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, unix_peer(newsk) = sk; newsk->sk_state = TCP_ESTABLISHED; newsk->sk_type = sk->sk_type; - init_peercred(newsk); + sock_init_peercred(newsk); newu = unix_sk(newsk); RCU_INIT_POINTER(newsk->sk_wq, &newu->peer_wq); otheru = unix_sk(other); @@ -1518,8 +1502,8 @@ static int unix_socketpair(struct socket *socka, struct socket *sockb) sock_hold(skb); unix_peer(ska) = skb; unix_peer(skb) = ska; - init_peercred(ska); - init_peercred(skb); + sock_init_peercred(ska); + sock_init_peercred(skb); ska->sk_state = TCP_ESTABLISHED; skb->sk_state = TCP_ESTABLISHED; -- 2.33.0.721.g106298f7f9