From nobody Thu Dec 18 08:13:16 2025 Received: from forward101a.mail.yandex.net (forward101a.mail.yandex.net [178.154.239.84]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BACE3192D69; Thu, 5 Dec 2024 07:36:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.154.239.84 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733384212; cv=none; b=Tbck4vNqel+IxpSTUzBublCwN1KdF0IKFLofkmrQIndxvNL2zgeipkOv3aOb2DpKVjM71N0pIGidDxQHtI8eNDQJYAG82d1x60Zap93Y6YXvYvZzmBnuPw237YusGG5THPfj4SgZpDrdJtRNeBVnsWr327xXqBv1Axr1spssjzs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733384212; c=relaxed/simple; bh=21Ja/cW+nuYGvUjhuMbMplc1F2C3v6Jt1NUkmdyCWxU=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=p7MnxzMHkHZiXzUYyDICfOnDp64rMLCY37zcGexK8yF0cSXPDYfbMTcMvd9C3OplvH3TWisK2WxiT4E0899pGOhPTfugqunUUuV6zGslXHZhtSqzsiUb8eh5WFkMWAzxDcU49xNQGYDFYA+8qKRlWaEfrFcwRsabyDYVBB6j/wY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=yandex.ru; spf=pass smtp.mailfrom=yandex.ru; dkim=pass (1024-bit key) header.d=yandex.ru header.i=@yandex.ru header.b=ncfby/BP; arc=none smtp.client-ip=178.154.239.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=yandex.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=yandex.ru Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=yandex.ru header.i=@yandex.ru header.b="ncfby/BP" Received: from mail-nwsmtp-smtp-production-main-37.myt.yp-c.yandex.net (mail-nwsmtp-smtp-production-main-37.myt.yp-c.yandex.net [IPv6:2a02:6b8:c12:3e2b:0:640:9b37:0]) by forward101a.mail.yandex.net (Yandex) with ESMTPS id 2B91060C62; Thu, 5 Dec 2024 10:36:40 +0300 (MSK) Received: by mail-nwsmtp-smtp-production-main-37.myt.yp-c.yandex.net (smtp/Yandex) with ESMTPSA id babeRmKOmmI0-tedl8uB3; Thu, 05 Dec 2024 10:36:39 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1733384199; bh=9DY/S36QSTTF6M19/iItAnGmg366WApJ9ys6nHJqW9g=; h=Message-ID:Date:Cc:Subject:To:From; b=ncfby/BPSzUBOAG0mQBfbK2DY4IT8QLm008okAXlXK5b0NZmvn47m6P3+E6jXusj5 WfiMWF6WcHfCYl/OGar2CUnRJoKc/FS/n7Oex7vAz2KdDh9T2KNzD79jMD2CsnqDV5 1tIlhRZWW9cCOSIfClL8egYxfT9Ex3xm9uY6fbwc= Authentication-Results: mail-nwsmtp-smtp-production-main-37.myt.yp-c.yandex.net; dkim=pass header.i=@yandex.ru From: Stas Sergeev To: netdev@vger.kernel.org Cc: Stas Sergeev , Willem de Bruijn , Jason Wang , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-kernel@vger.kernel.org Subject: [PATCH net-next] tun: fix group permission check Date: Thu, 5 Dec 2024 10:36:14 +0300 Message-ID: <20241205073614.294773-1-stsp2@yandex.ru> X-Mailer: git-send-email 2.47.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently tun checks the group permission even if the user have matched. Besides going against the usual permission semantic, this has a very interesting implication: if the tun group is not among the supplementary groups of the tun user, then effectively no one can access the tun device. CAP_SYS_ADMIN still can, but its the same as not setting the tun ownership. This patch relaxes the group checking so that either the user match or the group match is enough. This avoids the situation when no one can access the device even though the ownership is properly set. Also I simplified the logic by removing the redundant inversions: tun_not_capable() --> !tun_capable() Signed-off-by: Stas Sergeev CC: Willem de Bruijn CC: Jason Wang CC: Andrew Lunn CC: "David S. Miller" CC: Eric Dumazet CC: Jakub Kicinski CC: Paolo Abeni CC: netdev@vger.kernel.org CC: linux-kernel@vger.kernel.org Acked-by: Jason Wang Reviewed-by: Willem de Bruijn --- drivers/net/tun.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index 9a0f6eb32016..d35b6a48d138 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -574,14 +574,18 @@ static u16 tun_select_queue(struct net_device *dev, s= truct sk_buff *skb, return ret; } =20 -static inline bool tun_not_capable(struct tun_struct *tun) +static inline bool tun_capable(struct tun_struct *tun) { const struct cred *cred =3D current_cred(); struct net *net =3D dev_net(tun->dev); =20 - return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) || - (gid_valid(tun->group) && !in_egroup_p(tun->group))) && - !ns_capable(net->user_ns, CAP_NET_ADMIN); + if (ns_capable(net->user_ns, CAP_NET_ADMIN)) + return 1; + if (uid_valid(tun->owner) && uid_eq(cred->euid, tun->owner)) + return 1; + if (gid_valid(tun->group) && in_egroup_p(tun->group)) + return 1; + return 0; } =20 static void tun_set_real_num_queues(struct tun_struct *tun) @@ -2778,7 +2782,7 @@ static int tun_set_iff(struct net *net, struct file *= file, struct ifreq *ifr) !!(tun->flags & IFF_MULTI_QUEUE)) return -EINVAL; =20 - if (tun_not_capable(tun)) + if (!tun_capable(tun)) return -EPERM; err =3D security_tun_dev_open(tun->security); if (err < 0) --=20 2.47.0