Skip to content

Commit 1e82a62

Browse files
Sean Tranchettidavem330
authored andcommitted
genetlink: remove genl_bind
A potential deadlock can occur during registering or unregistering a new generic netlink family between the main nl_table_lock and the cb_lock where each thread wants the lock held by the other, as demonstrated below. 1) Thread 1 is performing a netlink_bind() operation on a socket. As part of this call, it will call netlink_lock_table(), incrementing the nl_table_users count to 1. 2) Thread 2 is registering (or unregistering) a genl_family via the genl_(un)register_family() API. The cb_lock semaphore will be taken for writing. 3) Thread 1 will call genl_bind() as part of the bind operation to handle subscribing to GENL multicast groups at the request of the user. It will attempt to take the cb_lock semaphore for reading, but it will fail and be scheduled away, waiting for Thread 2 to finish the write. 4) Thread 2 will call netlink_table_grab() during the (un)registration call. However, as Thread 1 has incremented nl_table_users, it will not be able to proceed, and both threads will be stuck waiting for the other. genl_bind() is a noop, unless a genl_family implements the mcast_bind() function to handle setting up family-specific multicast operations. Since no one in-tree uses this functionality as Cong pointed out, simply removing the genl_bind() function will remove the possibility for deadlock, as there is no attempt by Thread 1 above to take the cb_lock semaphore. Fixes: c380d9a ("genetlink: pass multicast bind/unbind to families") Suggested-by: Cong Wang <[email protected]> Acked-by: Johannes Berg <[email protected]> Reported-by: kernel test robot <[email protected]> Signed-off-by: Sean Tranchetti <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d3c54f7 commit 1e82a62

File tree

2 files changed

+0
-57
lines changed

2 files changed

+0
-57
lines changed

include/net/genetlink.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ struct genl_info;
3535
* do additional, common, filtering and return an error
3636
* @post_doit: called after an operation's doit callback, it may
3737
* undo operations done by pre_doit, for example release locks
38-
* @mcast_bind: a socket bound to the given multicast group (which
39-
* is given as the offset into the groups array)
40-
* @mcast_unbind: a socket was unbound from the given multicast group.
41-
* Note that unbind() will not be called symmetrically if the
42-
* generic netlink family is removed while there are still open
43-
* sockets.
4438
* @mcgrps: multicast groups used by this family
4539
* @n_mcgrps: number of multicast groups
4640
* @mcgrp_offset: starting number of multicast group IDs in this family
@@ -63,8 +57,6 @@ struct genl_family {
6357
void (*post_doit)(const struct genl_ops *ops,
6458
struct sk_buff *skb,
6559
struct genl_info *info);
66-
int (*mcast_bind)(struct net *net, int group);
67-
void (*mcast_unbind)(struct net *net, int group);
6860
const struct genl_ops * ops;
6961
const struct genl_multicast_group *mcgrps;
7062
unsigned int n_ops;

net/netlink/genetlink.c

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,60 +1144,11 @@ static struct genl_family genl_ctrl __ro_after_init = {
11441144
.netnsok = true,
11451145
};
11461146

1147-
static int genl_bind(struct net *net, int group)
1148-
{
1149-
struct genl_family *f;
1150-
int err = -ENOENT;
1151-
unsigned int id;
1152-
1153-
down_read(&cb_lock);
1154-
1155-
idr_for_each_entry(&genl_fam_idr, f, id) {
1156-
if (group >= f->mcgrp_offset &&
1157-
group < f->mcgrp_offset + f->n_mcgrps) {
1158-
int fam_grp = group - f->mcgrp_offset;
1159-
1160-
if (!f->netnsok && net != &init_net)
1161-
err = -ENOENT;
1162-
else if (f->mcast_bind)
1163-
err = f->mcast_bind(net, fam_grp);
1164-
else
1165-
err = 0;
1166-
break;
1167-
}
1168-
}
1169-
up_read(&cb_lock);
1170-
1171-
return err;
1172-
}
1173-
1174-
static void genl_unbind(struct net *net, int group)
1175-
{
1176-
struct genl_family *f;
1177-
unsigned int id;
1178-
1179-
down_read(&cb_lock);
1180-
1181-
idr_for_each_entry(&genl_fam_idr, f, id) {
1182-
if (group >= f->mcgrp_offset &&
1183-
group < f->mcgrp_offset + f->n_mcgrps) {
1184-
int fam_grp = group - f->mcgrp_offset;
1185-
1186-
if (f->mcast_unbind)
1187-
f->mcast_unbind(net, fam_grp);
1188-
break;
1189-
}
1190-
}
1191-
up_read(&cb_lock);
1192-
}
1193-
11941147
static int __net_init genl_pernet_init(struct net *net)
11951148
{
11961149
struct netlink_kernel_cfg cfg = {
11971150
.input = genl_rcv,
11981151
.flags = NL_CFG_F_NONROOT_RECV,
1199-
.bind = genl_bind,
1200-
.unbind = genl_unbind,
12011152
};
12021153

12031154
/* we'll bump the group number right afterwards */

0 commit comments

Comments
 (0)