Skip to content

Commit d966ddc

Browse files
Hoang Huu Lekuba-moo
authored andcommitted
tipc: fix a deadlock when flushing scheduled work
In the commit fdeba99 ("tipc: fix use-after-free in tipc_bcast_get_mode"), we're trying to make sure the tipc_net_finalize_work work item finished if it enqueued. But calling flush_scheduled_work() is not just affecting above work item but either any scheduled work. This has turned out to be overkill and caused to deadlock as syzbot reported: ====================================================== WARNING: possible circular locking dependency detected 5.9.0-rc2-next-20200828-syzkaller #0 Not tainted ------------------------------------------------------ kworker/u4:6/349 is trying to acquire lock: ffff8880aa063d38 ((wq_completion)events){+.+.}-{0:0}, at: flush_workqueue+0xe1/0x13e0 kernel/workqueue.c:2777 but task is already holding lock: ffffffff8a879430 (pernet_ops_rwsem){++++}-{3:3}, at: cleanup_net+0x9b/0xb10 net/core/net_namespace.c:565 [...] Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(pernet_ops_rwsem); lock(&sb->s_type->i_mutex_key#13); lock(pernet_ops_rwsem); lock((wq_completion)events); *** DEADLOCK *** [...] v1: To fix the original issue, we replace above calling by introducing a bit flag. When a namespace cleaned-up, bit flag is set to zero and: - tipc_net_finalize functionial just does return immediately. - tipc_net_finalize_work does not enqueue into the scheduled work queue. v2: Use cancel_work_sync() helper to make sure ONLY the tipc_net_finalize_work() stopped before releasing bcbase object. Reported-by: syzbot+d5aa7e0385f6a5d0f4fd@syzkaller.appspotmail.com Fixes: fdeba99 ("tipc: fix use-after-free in tipc_bcast_get_mode") Acked-by: Jon Maloy <jmaloy@redhat.com> Signed-off-by: Hoang Huu Le <hoang.h.le@dektech.com.au> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 02a20d4 commit d966ddc

4 files changed

Lines changed: 19 additions & 19 deletions

File tree

‎net/tipc/core.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static int __net_init tipc_init_net(struct net *net)
6060
tn->trial_addr = 0;
6161
tn->addr_trial_end = 0;
6262
tn->capabilities = TIPC_NODE_CAPABILITIES;
63+
INIT_WORK(&tn->final_work.work, tipc_net_finalize_work);
6364
memset(tn->node_id, 0, sizeof(tn->node_id));
6465
memset(tn->node_id_string, 0, sizeof(tn->node_id_string));
6566
tn->mon_threshold = TIPC_DEF_MON_THRESHOLD;
@@ -107,13 +108,13 @@ static int __net_init tipc_init_net(struct net *net)
107108

108109
static void __net_exit tipc_exit_net(struct net *net)
109110
{
111+
struct tipc_net *tn = tipc_net(net);
112+
110113
tipc_detach_loopback(net);
114+
/* Make sure the tipc_net_finalize_work() finished */
115+
cancel_work_sync(&tn->final_work.work);
111116
tipc_net_stop(net);
112117

113-
/* Make sure the tipc_net_finalize_work stopped
114-
* before releasing the resources.
115-
*/
116-
flush_scheduled_work();
117118
tipc_bcast_stop(net);
118119
tipc_nametbl_stop(net);
119120
tipc_sk_rht_destroy(net);

‎net/tipc/core.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ extern unsigned int tipc_net_id __read_mostly;
9090
extern int sysctl_tipc_rmem[3] __read_mostly;
9191
extern int sysctl_tipc_named_timeout __read_mostly;
9292

93+
struct tipc_net_work {
94+
struct work_struct work;
95+
struct net *net;
96+
u32 addr;
97+
};
98+
9399
struct tipc_net {
94100
u8 node_id[NODE_ID_LEN];
95101
u32 node_addr;
@@ -143,6 +149,8 @@ struct tipc_net {
143149
/* TX crypto handler */
144150
struct tipc_crypto *crypto_tx;
145151
#endif
152+
/* Work item for net finalize */
153+
struct tipc_net_work final_work;
146154
};
147155

148156
static inline struct tipc_net *tipc_net(struct net *net)

‎net/tipc/net.c‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@
105105
* - A local spin_lock protecting the queue of subscriber events.
106106
*/
107107

108-
struct tipc_net_work {
109-
struct work_struct work;
110-
struct net *net;
111-
u32 addr;
112-
};
113-
114108
static void tipc_net_finalize(struct net *net, u32 addr);
115109

116110
int tipc_net_init(struct net *net, u8 *node_id, u32 addr)
@@ -142,25 +136,21 @@ static void tipc_net_finalize(struct net *net, u32 addr)
142136
TIPC_CLUSTER_SCOPE, 0, addr);
143137
}
144138

145-
static void tipc_net_finalize_work(struct work_struct *work)
139+
void tipc_net_finalize_work(struct work_struct *work)
146140
{
147141
struct tipc_net_work *fwork;
148142

149143
fwork = container_of(work, struct tipc_net_work, work);
150144
tipc_net_finalize(fwork->net, fwork->addr);
151-
kfree(fwork);
152145
}
153146

154147
void tipc_sched_net_finalize(struct net *net, u32 addr)
155148
{
156-
struct tipc_net_work *fwork = kzalloc(sizeof(*fwork), GFP_ATOMIC);
149+
struct tipc_net *tn = tipc_net(net);
157150

158-
if (!fwork)
159-
return;
160-
INIT_WORK(&fwork->work, tipc_net_finalize_work);
161-
fwork->net = net;
162-
fwork->addr = addr;
163-
schedule_work(&fwork->work);
151+
tn->final_work.net = net;
152+
tn->final_work.addr = addr;
153+
schedule_work(&tn->final_work.work);
164154
}
165155

166156
void tipc_net_stop(struct net *net)

‎net/tipc/net.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
extern const struct nla_policy tipc_nl_net_policy[];
4343

4444
int tipc_net_init(struct net *net, u8 *node_id, u32 addr);
45+
void tipc_net_finalize_work(struct work_struct *work);
4546
void tipc_sched_net_finalize(struct net *net, u32 addr);
4647
void tipc_net_stop(struct net *net);
4748
int tipc_nl_net_dump(struct sk_buff *skb, struct netlink_callback *cb);

0 commit comments

Comments
 (0)