Skip to content

Commit

Permalink
tun: fix group permission check
Browse files Browse the repository at this point in the history
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 <stsp2@yandex.ru>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Link: https://patch.msgid.link/20241205073614.294773-1-stsp2@yandex.ru
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Stas Sergeev authored and Jakub Kicinski committed Dec 8, 2024
1 parent 81d89e6 commit 3ca459e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions drivers/net/tun.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,14 +574,18 @@ static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
return ret;
}

static inline bool tun_not_capable(struct tun_struct *tun)
static inline bool tun_capable(struct tun_struct *tun)
{
const struct cred *cred = current_cred();
struct net *net = dev_net(tun->dev);

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;
}

static void tun_set_real_num_queues(struct tun_struct *tun)
Expand Down Expand Up @@ -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;

if (tun_not_capable(tun))
if (!tun_capable(tun))
return -EPERM;
err = security_tun_dev_open(tun->security);
if (err < 0)
Expand Down

0 comments on commit 3ca459e

Please sign in to comment.