Skip to content

Commit 4f52090

Browse files
rgbriggsdavem330
authored andcommitted
netlink: have netlink per-protocol bind function return an error code.
Have the netlink per-protocol optional bind function return an int error code rather than void to signal a failure. This will enable netlink protocols to perform extra checks including capabilities and permissions verifications when updating memberships in multicast groups. In netlink_bind() and netlink_setsockopt() the call to the per-protocol bind function was moved above the multicast group update to prevent any access to the multicast socket groups before checking with the per-protocol bind function. This will enable the per-protocol bind function to be used to check permissions which could be denied before making them available, and to avoid the messy job of undoing the addition should the per-protocol bind function fail. The netfilter subsystem seems to be the only one currently using the per-protocol bind function. Signed-off-by: Richard Guy Briggs <rgb@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent bfe4bc7 commit 4f52090

4 files changed

Lines changed: 56 additions & 24 deletions

File tree

‎include/linux/netlink.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ struct netlink_kernel_cfg {
4545
unsigned int flags;
4646
void (*input)(struct sk_buff *skb);
4747
struct mutex *cb_mutex;
48-
void (*bind)(int group);
48+
int (*bind)(int group);
49+
void (*unbind)(int group);
4950
bool (*compare)(struct net *net, struct sock *sk);
5051
};
5152

‎net/netfilter/nfnetlink.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static void nfnetlink_rcv(struct sk_buff *skb)
400400
}
401401

402402
#ifdef CONFIG_MODULES
403-
static void nfnetlink_bind(int group)
403+
static int nfnetlink_bind(int group)
404404
{
405405
const struct nfnetlink_subsystem *ss;
406406
int type = nfnl_group2type[group];
@@ -410,6 +410,7 @@ static void nfnetlink_bind(int group)
410410
rcu_read_unlock();
411411
if (!ss)
412412
request_module("nfnetlink-subsys-%d", type);
413+
return 0;
413414
}
414415
#endif
415416

‎net/netlink/af_netlink.c‎

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,8 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,
12061206
struct module *module = NULL;
12071207
struct mutex *cb_mutex;
12081208
struct netlink_sock *nlk;
1209-
void (*bind)(int group);
1209+
int (*bind)(int group);
1210+
void (*unbind)(int group);
12101211
int err = 0;
12111212

12121213
sock->state = SS_UNCONNECTED;
@@ -1232,6 +1233,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,
12321233
err = -EPROTONOSUPPORT;
12331234
cb_mutex = nl_table[protocol].cb_mutex;
12341235
bind = nl_table[protocol].bind;
1236+
unbind = nl_table[protocol].unbind;
12351237
netlink_unlock_table();
12361238

12371239
if (err < 0)
@@ -1248,6 +1250,7 @@ static int netlink_create(struct net *net, struct socket *sock, int protocol,
12481250
nlk = nlk_sk(sock->sk);
12491251
nlk->module = module;
12501252
nlk->netlink_bind = bind;
1253+
nlk->netlink_unbind = unbind;
12511254
out:
12521255
return err;
12531256

@@ -1301,6 +1304,7 @@ static int netlink_release(struct socket *sock)
13011304
kfree_rcu(old, rcu);
13021305
nl_table[sk->sk_protocol].module = NULL;
13031306
nl_table[sk->sk_protocol].bind = NULL;
1307+
nl_table[sk->sk_protocol].unbind = NULL;
13041308
nl_table[sk->sk_protocol].flags = 0;
13051309
nl_table[sk->sk_protocol].registered = 0;
13061310
}
@@ -1411,6 +1415,19 @@ static int netlink_realloc_groups(struct sock *sk)
14111415
return err;
14121416
}
14131417

1418+
static void netlink_unbind(int group, long unsigned int groups,
1419+
struct netlink_sock *nlk)
1420+
{
1421+
int undo;
1422+
1423+
if (!nlk->netlink_unbind)
1424+
return;
1425+
1426+
for (undo = 0; undo < group; undo++)
1427+
if (test_bit(group, &groups))
1428+
nlk->netlink_unbind(undo);
1429+
}
1430+
14141431
static int netlink_bind(struct socket *sock, struct sockaddr *addr,
14151432
int addr_len)
14161433
{
@@ -1419,6 +1436,7 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr,
14191436
struct netlink_sock *nlk = nlk_sk(sk);
14201437
struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
14211438
int err;
1439+
long unsigned int groups = nladdr->nl_groups;
14221440

14231441
if (addr_len < sizeof(struct sockaddr_nl))
14241442
return -EINVAL;
@@ -1427,45 +1445,53 @@ static int netlink_bind(struct socket *sock, struct sockaddr *addr,
14271445
return -EINVAL;
14281446

14291447
/* Only superuser is allowed to listen multicasts */
1430-
if (nladdr->nl_groups) {
1448+
if (groups) {
14311449
if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
14321450
return -EPERM;
14331451
err = netlink_realloc_groups(sk);
14341452
if (err)
14351453
return err;
14361454
}
14371455

1438-
if (nlk->portid) {
1456+
if (nlk->portid)
14391457
if (nladdr->nl_pid != nlk->portid)
14401458
return -EINVAL;
1441-
} else {
1459+
1460+
if (nlk->netlink_bind && groups) {
1461+
int group;
1462+
1463+
for (group = 0; group < nlk->ngroups; group++) {
1464+
if (!test_bit(group, &groups))
1465+
continue;
1466+
err = nlk->netlink_bind(group);
1467+
if (!err)
1468+
continue;
1469+
netlink_unbind(group, groups, nlk);
1470+
return err;
1471+
}
1472+
}
1473+
1474+
if (!nlk->portid) {
14421475
err = nladdr->nl_pid ?
14431476
netlink_insert(sk, net, nladdr->nl_pid) :
14441477
netlink_autobind(sock);
1445-
if (err)
1478+
if (err) {
1479+
netlink_unbind(nlk->ngroups - 1, groups, nlk);
14461480
return err;
1481+
}
14471482
}
14481483

1449-
if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
1484+
if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
14501485
return 0;
14511486

14521487
netlink_table_grab();
14531488
netlink_update_subscriptions(sk, nlk->subscriptions +
1454-
hweight32(nladdr->nl_groups) -
1489+
hweight32(groups) -
14551490
hweight32(nlk->groups[0]));
1456-
nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
1491+
nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
14571492
netlink_update_listeners(sk);
14581493
netlink_table_ungrab();
14591494

1460-
if (nlk->netlink_bind && nlk->groups[0]) {
1461-
int i;
1462-
1463-
for (i = 0; i < nlk->ngroups; i++) {
1464-
if (test_bit(i, nlk->groups))
1465-
nlk->netlink_bind(i);
1466-
}
1467-
}
1468-
14691495
return 0;
14701496
}
14711497

@@ -2103,14 +2129,16 @@ static int netlink_setsockopt(struct socket *sock, int level, int optname,
21032129
return err;
21042130
if (!val || val - 1 >= nlk->ngroups)
21052131
return -EINVAL;
2132+
if (nlk->netlink_bind) {
2133+
err = nlk->netlink_bind(val);
2134+
if (err)
2135+
return err;
2136+
}
21062137
netlink_table_grab();
21072138
netlink_update_socket_mc(nlk, val,
21082139
optname == NETLINK_ADD_MEMBERSHIP);
21092140
netlink_table_ungrab();
21102141

2111-
if (nlk->netlink_bind)
2112-
nlk->netlink_bind(val);
2113-
21142142
err = 0;
21152143
break;
21162144
}

‎net/netlink/af_netlink.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ struct netlink_sock {
3838
struct mutex *cb_mutex;
3939
struct mutex cb_def_mutex;
4040
void (*netlink_rcv)(struct sk_buff *skb);
41-
void (*netlink_bind)(int group);
41+
int (*netlink_bind)(int group);
42+
void (*netlink_unbind)(int group);
4243
struct module *module;
4344
#ifdef CONFIG_NETLINK_MMAP
4445
struct mutex pg_vec_lock;
@@ -74,7 +75,8 @@ struct netlink_table {
7475
unsigned int groups;
7576
struct mutex *cb_mutex;
7677
struct module *module;
77-
void (*bind)(int group);
78+
int (*bind)(int group);
79+
void (*unbind)(int group);
7880
bool (*compare)(struct net *net, struct sock *sock);
7981
int registered;
8082
};

0 commit comments

Comments
 (0)