Skip to content

Commit

Permalink
genetlink: add CAP_NET_ADMIN test for multicast bind
Browse files Browse the repository at this point in the history
genetlink sets NL_CFG_F_NONROOT_RECV for its netlink socket so anyone can
subscribe to multicast messages.

rtnetlink doesn't allow this unconditionally,  rtnetlink_bind() restricts
bind requests to CAP_NET_ADMIN for a few groups.

This allows to set GENL_UNS_ADMIN_PERM flag on genl mcast groups to
mandate CAP_NET_ADMIN.

This will be used by the upcoming mptcp netlink event facility which
exposes the token (mptcp connection identifier) to userspace.

Acked-by: Mat Martineau <[email protected]>
Signed-off-by: Florian Westphal <[email protected]>
  • Loading branch information
Florian Westphal authored and jenkins-tessares committed Feb 2, 2021
1 parent 875cda9 commit cf6cac4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/net/genetlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
struct genl_multicast_group {
char name[GENL_NAMSIZ];
u8 flags;
};

struct genl_ops;
Expand Down
32 changes: 32 additions & 0 deletions net/netlink/genetlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -1360,11 +1360,43 @@ static struct genl_family genl_ctrl __ro_after_init = {
.netnsok = true,
};

static int genl_bind(struct net *net, int group)
{
const struct genl_family *family;
unsigned int id;
int ret = 0;

genl_lock_all();

idr_for_each_entry(&genl_fam_idr, family, id) {
const struct genl_multicast_group *grp;
int i;

if (family->n_mcgrps == 0)
continue;

i = group - family->mcgrp_offset;
if (i < 0 || i >= family->n_mcgrps)
continue;

grp = &family->mcgrps[i];
if ((grp->flags & GENL_UNS_ADMIN_PERM) &&
!ns_capable(net->user_ns, CAP_NET_ADMIN))
ret = -EPERM;

break;
}

genl_unlock_all();
return ret;
}

static int __net_init genl_pernet_init(struct net *net)
{
struct netlink_kernel_cfg cfg = {
.input = genl_rcv,
.flags = NL_CFG_F_NONROOT_RECV,
.bind = genl_bind,
};

/* we'll bump the group number right afterwards */
Expand Down

0 comments on commit cf6cac4

Please sign in to comment.