Skip to content

Commit 60c2530

Browse files
Jon Maloydavem330
authored andcommitted
tipc: fix race between poll() and setsockopt()
Letting tipc_poll() dereference a socket's pointer to struct tipc_group entails a race risk, as the group item may be deleted in a concurrent tipc_sk_join() or tipc_sk_leave() thread. We now move the 'open' flag in struct tipc_group to struct tipc_sock, and let the former retain only a pointer to the moved field. This will eliminate the race risk. Reported-by: [email protected] Signed-off-by: Jon Maloy <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 30c3e9d commit 60c2530

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

net/tipc/group.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,21 @@ struct tipc_group {
9393
u16 max_active;
9494
u16 bc_snd_nxt;
9595
u16 bc_ackers;
96+
bool *open;
9697
bool loopback;
9798
bool events;
98-
bool open;
9999
};
100100

101101
static void tipc_group_proto_xmit(struct tipc_group *grp, struct tipc_member *m,
102102
int mtyp, struct sk_buff_head *xmitq);
103103

104-
bool tipc_group_is_open(struct tipc_group *grp)
105-
{
106-
return grp->open;
107-
}
108-
109104
static void tipc_group_open(struct tipc_member *m, bool *wakeup)
110105
{
111106
*wakeup = false;
112107
if (list_empty(&m->small_win))
113108
return;
114109
list_del_init(&m->small_win);
115-
m->group->open = true;
110+
*m->group->open = true;
116111
*wakeup = true;
117112
}
118113

@@ -170,7 +165,8 @@ int tipc_group_size(struct tipc_group *grp)
170165
}
171166

172167
struct tipc_group *tipc_group_create(struct net *net, u32 portid,
173-
struct tipc_group_req *mreq)
168+
struct tipc_group_req *mreq,
169+
bool *group_is_open)
174170
{
175171
u32 filter = TIPC_SUB_PORTS | TIPC_SUB_NO_STATUS;
176172
bool global = mreq->scope != TIPC_NODE_SCOPE;
@@ -192,6 +188,7 @@ struct tipc_group *tipc_group_create(struct net *net, u32 portid,
192188
grp->scope = mreq->scope;
193189
grp->loopback = mreq->flags & TIPC_GROUP_LOOPBACK;
194190
grp->events = mreq->flags & TIPC_GROUP_MEMBER_EVTS;
191+
grp->open = group_is_open;
195192
filter |= global ? TIPC_SUB_CLUSTER_SCOPE : TIPC_SUB_NODE_SCOPE;
196193
if (tipc_topsrv_kern_subscr(net, portid, type, 0, ~0,
197194
filter, &grp->subid))
@@ -430,7 +427,7 @@ bool tipc_group_cong(struct tipc_group *grp, u32 dnode, u32 dport,
430427
if (m->window >= len)
431428
return false;
432429

433-
grp->open = false;
430+
*grp->open = false;
434431

435432
/* If not fully advertised, do it now to prevent mutual blocking */
436433
adv = m->advertised;
@@ -453,7 +450,7 @@ bool tipc_group_bc_cong(struct tipc_group *grp, int len)
453450

454451
/* If prev bcast was replicast, reject until all receivers have acked */
455452
if (grp->bc_ackers) {
456-
grp->open = false;
453+
*grp->open = false;
457454
return true;
458455
}
459456
if (list_empty(&grp->small_win))
@@ -800,7 +797,7 @@ void tipc_group_proto_rcv(struct tipc_group *grp, bool *usr_wakeup,
800797
if (--grp->bc_ackers)
801798
return;
802799
list_del_init(&m->small_win);
803-
m->group->open = true;
800+
*m->group->open = true;
804801
*usr_wakeup = true;
805802
tipc_group_update_member(m, 0);
806803
return;

net/tipc/group.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ struct tipc_member;
4343
struct tipc_msg;
4444

4545
struct tipc_group *tipc_group_create(struct net *net, u32 portid,
46-
struct tipc_group_req *mreq);
46+
struct tipc_group_req *mreq,
47+
bool *group_is_open);
4748
void tipc_group_join(struct net *net, struct tipc_group *grp, int *sk_rcv_buf);
4849
void tipc_group_delete(struct net *net, struct tipc_group *grp);
4950
void tipc_group_add_member(struct tipc_group *grp, u32 node,
@@ -67,7 +68,6 @@ void tipc_group_update_bc_members(struct tipc_group *grp, int len, bool ack);
6768
bool tipc_group_cong(struct tipc_group *grp, u32 dnode, u32 dport,
6869
int len, struct tipc_member **m);
6970
bool tipc_group_bc_cong(struct tipc_group *grp, int len);
70-
bool tipc_group_is_open(struct tipc_group *grp);
7171
void tipc_group_update_rcv_win(struct tipc_group *grp, int blks, u32 node,
7272
u32 port, struct sk_buff_head *xmitq);
7373
u16 tipc_group_bc_snd_nxt(struct tipc_group *grp);

net/tipc/socket.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ struct tipc_sock {
116116
struct tipc_mc_method mc_method;
117117
struct rcu_head rcu;
118118
struct tipc_group *group;
119+
bool group_is_open;
119120
};
120121

121122
static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
@@ -715,7 +716,6 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
715716
{
716717
struct sock *sk = sock->sk;
717718
struct tipc_sock *tsk = tipc_sk(sk);
718-
struct tipc_group *grp;
719719
u32 revents = 0;
720720

721721
sock_poll_wait(file, sk_sleep(sk), wait);
@@ -736,8 +736,7 @@ static unsigned int tipc_poll(struct file *file, struct socket *sock,
736736
revents |= POLLIN | POLLRDNORM;
737737
break;
738738
case TIPC_OPEN:
739-
grp = tsk->group;
740-
if ((!grp || tipc_group_is_open(grp)) && !tsk->cong_link_cnt)
739+
if (tsk->group_is_open && !tsk->cong_link_cnt)
741740
revents |= POLLOUT;
742741
if (!tipc_sk_type_connectionless(sk))
743742
break;
@@ -2758,7 +2757,7 @@ static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
27582757
return -EINVAL;
27592758
if (grp)
27602759
return -EACCES;
2761-
grp = tipc_group_create(net, tsk->portid, mreq);
2760+
grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
27622761
if (!grp)
27632762
return -ENOMEM;
27642763
tsk->group = grp;

0 commit comments

Comments
 (0)