Skip to content

Commit 5ec7d18

Browse files
lxindavem330
authored andcommitted
sctp: use call_rcu to free endpoint
This patch is to delay the endpoint free by calling call_rcu() to fix another use-after-free issue in sctp_sock_dump(): BUG: KASAN: use-after-free in __lock_acquire+0x36d9/0x4c20 Call Trace: __lock_acquire+0x36d9/0x4c20 kernel/locking/lockdep.c:3218 lock_acquire+0x1ed/0x520 kernel/locking/lockdep.c:3844 __raw_spin_lock_bh include/linux/spinlock_api_smp.h:135 [inline] _raw_spin_lock_bh+0x31/0x40 kernel/locking/spinlock.c:168 spin_lock_bh include/linux/spinlock.h:334 [inline] __lock_sock+0x203/0x350 net/core/sock.c:2253 lock_sock_nested+0xfe/0x120 net/core/sock.c:2774 lock_sock include/net/sock.h:1492 [inline] sctp_sock_dump+0x122/0xb20 net/sctp/diag.c:324 sctp_for_each_transport+0x2b5/0x370 net/sctp/socket.c:5091 sctp_diag_dump+0x3ac/0x660 net/sctp/diag.c:527 __inet_diag_dump+0xa8/0x140 net/ipv4/inet_diag.c:1049 inet_diag_dump+0x9b/0x110 net/ipv4/inet_diag.c:1065 netlink_dump+0x606/0x1080 net/netlink/af_netlink.c:2244 __netlink_dump_start+0x59a/0x7c0 net/netlink/af_netlink.c:2352 netlink_dump_start include/linux/netlink.h:216 [inline] inet_diag_handler_cmd+0x2ce/0x3f0 net/ipv4/inet_diag.c:1170 __sock_diag_cmd net/core/sock_diag.c:232 [inline] sock_diag_rcv_msg+0x31d/0x410 net/core/sock_diag.c:263 netlink_rcv_skb+0x172/0x440 net/netlink/af_netlink.c:2477 sock_diag_rcv+0x2a/0x40 net/core/sock_diag.c:274 This issue occurs when asoc is peeled off and the old sk is freed after getting it by asoc->base.sk and before calling lock_sock(sk). To prevent the sk free, as a holder of the sk, ep should be alive when calling lock_sock(). This patch uses call_rcu() and moves sock_put and ep free into sctp_endpoint_destroy_rcu(), so that it's safe to try to hold the ep under rcu_read_lock in sctp_transport_traverse_process(). If sctp_endpoint_hold() returns true, it means this ep is still alive and we have held it and can continue to dump it; If it returns false, it means this ep is dead and can be freed after rcu_read_unlock, and we should skip it. In sctp_sock_dump(), after locking the sk, if this ep is different from tsp->asoc->ep, it means during this dumping, this asoc was peeled off before calling lock_sock(), and the sk should be skipped; If this ep is the same with tsp->asoc->ep, it means no peeloff happens on this asoc, and due to lock_sock, no peeloff will happen either until release_sock. Note that delaying endpoint free won't delay the port release, as the port release happens in sctp_endpoint_destroy() before calling call_rcu(). Also, freeing endpoint by call_rcu() makes it safe to access the sk by asoc->base.sk in sctp_assocs_seq_show() and sctp_rcv(). Thanks Jones to bring this issue up. v1->v2: - improve the changelog. - add kfree(ep) into sctp_endpoint_destroy_rcu(), as Jakub noticed. Reported-by: syzbot+9276d76e83e3bcde6c99@syzkaller.appspotmail.com Reported-by: Lee Jones <lee.jones@linaro.org> Fixes: d25adbe ("sctp: fix an use-after-free issue in sctp_sock_dump") Signed-off-by: Xin Long <lucien.xin@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b45396a commit 5ec7d18

5 files changed

Lines changed: 41 additions & 26 deletions

File tree

include/net/sctp/sctp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ extern struct percpu_counter sctp_sockets_allocated;
105105
int sctp_asconf_mgmt(struct sctp_sock *, struct sctp_sockaddr_entry *);
106106
struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int, int *);
107107

108+
typedef int (*sctp_callback_t)(struct sctp_endpoint *, struct sctp_transport *, void *);
108109
void sctp_transport_walk_start(struct rhashtable_iter *iter);
109110
void sctp_transport_walk_stop(struct rhashtable_iter *iter);
110111
struct sctp_transport *sctp_transport_get_next(struct net *net,
@@ -115,9 +116,8 @@ int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
115116
struct net *net,
116117
const union sctp_addr *laddr,
117118
const union sctp_addr *paddr, void *p);
118-
int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
119-
int (*cb_done)(struct sctp_transport *, void *),
120-
struct net *net, int *pos, void *p);
119+
int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done,
120+
struct net *net, int *pos, void *p);
121121
int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *), void *p);
122122
int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
123123
struct sctp_info *info);

include/net/sctp/structs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,7 @@ struct sctp_endpoint {
13551355
reconf_enable:1;
13561356

13571357
__u8 strreset_enable;
1358+
struct rcu_head rcu;
13581359
};
13591360

13601361
/* Recover the outter endpoint structure. */
@@ -1370,7 +1371,7 @@ static inline struct sctp_endpoint *sctp_ep(struct sctp_ep_common *base)
13701371
struct sctp_endpoint *sctp_endpoint_new(struct sock *, gfp_t);
13711372
void sctp_endpoint_free(struct sctp_endpoint *);
13721373
void sctp_endpoint_put(struct sctp_endpoint *);
1373-
void sctp_endpoint_hold(struct sctp_endpoint *);
1374+
int sctp_endpoint_hold(struct sctp_endpoint *ep);
13741375
void sctp_endpoint_add_asoc(struct sctp_endpoint *, struct sctp_association *);
13751376
struct sctp_association *sctp_endpoint_lookup_assoc(
13761377
const struct sctp_endpoint *ep,

net/sctp/diag.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,8 @@ static int sctp_tsp_dump_one(struct sctp_transport *tsp, void *p)
290290
return err;
291291
}
292292

293-
static int sctp_sock_dump(struct sctp_transport *tsp, void *p)
293+
static int sctp_sock_dump(struct sctp_endpoint *ep, struct sctp_transport *tsp, void *p)
294294
{
295-
struct sctp_endpoint *ep = tsp->asoc->ep;
296295
struct sctp_comm_param *commp = p;
297296
struct sock *sk = ep->base.sk;
298297
struct sk_buff *skb = commp->skb;
@@ -302,6 +301,8 @@ static int sctp_sock_dump(struct sctp_transport *tsp, void *p)
302301
int err = 0;
303302

304303
lock_sock(sk);
304+
if (ep != tsp->asoc->ep)
305+
goto release;
305306
list_for_each_entry(assoc, &ep->asocs, asocs) {
306307
if (cb->args[4] < cb->args[1])
307308
goto next;
@@ -344,9 +345,8 @@ static int sctp_sock_dump(struct sctp_transport *tsp, void *p)
344345
return err;
345346
}
346347

347-
static int sctp_sock_filter(struct sctp_transport *tsp, void *p)
348+
static int sctp_sock_filter(struct sctp_endpoint *ep, struct sctp_transport *tsp, void *p)
348349
{
349-
struct sctp_endpoint *ep = tsp->asoc->ep;
350350
struct sctp_comm_param *commp = p;
351351
struct sock *sk = ep->base.sk;
352352
const struct inet_diag_req_v2 *r = commp->r;
@@ -505,8 +505,8 @@ static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
505505
if (!(idiag_states & ~(TCPF_LISTEN | TCPF_CLOSE)))
506506
goto done;
507507

508-
sctp_for_each_transport(sctp_sock_filter, sctp_sock_dump,
509-
net, &pos, &commp);
508+
sctp_transport_traverse_process(sctp_sock_filter, sctp_sock_dump,
509+
net, &pos, &commp);
510510
cb->args[2] = pos;
511511

512512
done:

net/sctp/endpointola.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,18 @@ void sctp_endpoint_free(struct sctp_endpoint *ep)
184184
}
185185

186186
/* Final destructor for endpoint. */
187+
static void sctp_endpoint_destroy_rcu(struct rcu_head *head)
188+
{
189+
struct sctp_endpoint *ep = container_of(head, struct sctp_endpoint, rcu);
190+
struct sock *sk = ep->base.sk;
191+
192+
sctp_sk(sk)->ep = NULL;
193+
sock_put(sk);
194+
195+
kfree(ep);
196+
SCTP_DBG_OBJCNT_DEC(ep);
197+
}
198+
187199
static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
188200
{
189201
struct sock *sk;
@@ -213,18 +225,13 @@ static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
213225
if (sctp_sk(sk)->bind_hash)
214226
sctp_put_port(sk);
215227

216-
sctp_sk(sk)->ep = NULL;
217-
/* Give up our hold on the sock */
218-
sock_put(sk);
219-
220-
kfree(ep);
221-
SCTP_DBG_OBJCNT_DEC(ep);
228+
call_rcu(&ep->rcu, sctp_endpoint_destroy_rcu);
222229
}
223230

224231
/* Hold a reference to an endpoint. */
225-
void sctp_endpoint_hold(struct sctp_endpoint *ep)
232+
int sctp_endpoint_hold(struct sctp_endpoint *ep)
226233
{
227-
refcount_inc(&ep->base.refcnt);
234+
return refcount_inc_not_zero(&ep->base.refcnt);
228235
}
229236

230237
/* Release a reference to an endpoint and clean up if there are

net/sctp/socket.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5338,11 +5338,12 @@ int sctp_transport_lookup_process(int (*cb)(struct sctp_transport *, void *),
53385338
}
53395339
EXPORT_SYMBOL_GPL(sctp_transport_lookup_process);
53405340

5341-
int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
5342-
int (*cb_done)(struct sctp_transport *, void *),
5343-
struct net *net, int *pos, void *p) {
5341+
int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done,
5342+
struct net *net, int *pos, void *p)
5343+
{
53445344
struct rhashtable_iter hti;
53455345
struct sctp_transport *tsp;
5346+
struct sctp_endpoint *ep;
53465347
int ret;
53475348

53485349
again:
@@ -5351,26 +5352,32 @@ int sctp_for_each_transport(int (*cb)(struct sctp_transport *, void *),
53515352

53525353
tsp = sctp_transport_get_idx(net, &hti, *pos + 1);
53535354
for (; !IS_ERR_OR_NULL(tsp); tsp = sctp_transport_get_next(net, &hti)) {
5354-
ret = cb(tsp, p);
5355-
if (ret)
5356-
break;
5355+
ep = tsp->asoc->ep;
5356+
if (sctp_endpoint_hold(ep)) { /* asoc can be peeled off */
5357+
ret = cb(ep, tsp, p);
5358+
if (ret)
5359+
break;
5360+
sctp_endpoint_put(ep);
5361+
}
53575362
(*pos)++;
53585363
sctp_transport_put(tsp);
53595364
}
53605365
sctp_transport_walk_stop(&hti);
53615366

53625367
if (ret) {
5363-
if (cb_done && !cb_done(tsp, p)) {
5368+
if (cb_done && !cb_done(ep, tsp, p)) {
53645369
(*pos)++;
5370+
sctp_endpoint_put(ep);
53655371
sctp_transport_put(tsp);
53665372
goto again;
53675373
}
5374+
sctp_endpoint_put(ep);
53685375
sctp_transport_put(tsp);
53695376
}
53705377

53715378
return ret;
53725379
}
5373-
EXPORT_SYMBOL_GPL(sctp_for_each_transport);
5380+
EXPORT_SYMBOL_GPL(sctp_transport_traverse_process);
53745381

53755382
/* 7.2.1 Association Status (SCTP_STATUS)
53765383

0 commit comments

Comments
 (0)