Skip to content

Commit 27ef12d

Browse files
Sean Tranchettigregkh
authored andcommitted
genetlink: remove genl_bind
[ Upstream commit 1e82a62 ] 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 <xiyou.wangcong@gmail.com> Acked-by: Johannes Berg <johannes.berg@intel.com> Reported-by: kernel test robot <lkp@intel.com> Signed-off-by: Sean Tranchetti <stranche@codeaurora.org> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5386ced commit 27ef12d

2 files changed

Lines changed: 0 additions & 57 deletions

File tree

‎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
* @attrbuf: buffer to store parsed attributes (private)
4539
* @mcgrps: multicast groups used by this family
4640
* @n_mcgrps: number of multicast groups
@@ -64,8 +58,6 @@ struct genl_family {
6458
void (*post_doit)(const struct genl_ops *ops,
6559
struct sk_buff *skb,
6660
struct genl_info *info);
67-
int (*mcast_bind)(struct net *net, int group);
68-
void (*mcast_unbind)(struct net *net, int group);
6961
struct nlattr ** attrbuf; /* private */
7062
const struct genl_ops * ops;
7163
const struct genl_multicast_group *mcgrps;

‎net/netlink/genetlink.c‎

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,60 +1088,11 @@ static struct genl_family genl_ctrl __ro_after_init = {
10881088
.netnsok = true,
10891089
};
10901090

1091-
static int genl_bind(struct net *net, int group)
1092-
{
1093-
struct genl_family *f;
1094-
int err = -ENOENT;
1095-
unsigned int id;
1096-
1097-
down_read(&cb_lock);
1098-
1099-
idr_for_each_entry(&genl_fam_idr, f, id) {
1100-
if (group >= f->mcgrp_offset &&
1101-
group < f->mcgrp_offset + f->n_mcgrps) {
1102-
int fam_grp = group - f->mcgrp_offset;
1103-
1104-
if (!f->netnsok && net != &init_net)
1105-
err = -ENOENT;
1106-
else if (f->mcast_bind)
1107-
err = f->mcast_bind(net, fam_grp);
1108-
else
1109-
err = 0;
1110-
break;
1111-
}
1112-
}
1113-
up_read(&cb_lock);
1114-
1115-
return err;
1116-
}
1117-
1118-
static void genl_unbind(struct net *net, int group)
1119-
{
1120-
struct genl_family *f;
1121-
unsigned int id;
1122-
1123-
down_read(&cb_lock);
1124-
1125-
idr_for_each_entry(&genl_fam_idr, f, id) {
1126-
if (group >= f->mcgrp_offset &&
1127-
group < f->mcgrp_offset + f->n_mcgrps) {
1128-
int fam_grp = group - f->mcgrp_offset;
1129-
1130-
if (f->mcast_unbind)
1131-
f->mcast_unbind(net, fam_grp);
1132-
break;
1133-
}
1134-
}
1135-
up_read(&cb_lock);
1136-
}
1137-
11381091
static int __net_init genl_pernet_init(struct net *net)
11391092
{
11401093
struct netlink_kernel_cfg cfg = {
11411094
.input = genl_rcv,
11421095
.flags = NL_CFG_F_NONROOT_RECV,
1143-
.bind = genl_bind,
1144-
.unbind = genl_unbind,
11451096
};
11461097

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

0 commit comments

Comments
 (0)