Skip to content

Commit 4e4b21d

Browse files
Guillaume Naultdavem330
authored andcommitted
l2tp: hold tunnel while handling genl TUNNEL_GET commands
Use l2tp_tunnel_get() instead of l2tp_tunnel_find() so that we get a reference on the tunnel, preventing l2tp_tunnel_destruct() from freeing it from under us. Also move l2tp_tunnel_get() below nlmsg_new() so that we only take the reference when needed. Fixes: 309795f ("l2tp: Add netlink control API for L2TP") Signed-off-by: Guillaume Nault <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8c0e421 commit 4e4b21d

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

net/l2tp/l2tp_netlink.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -444,34 +444,37 @@ static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
444444

445445
if (!info->attrs[L2TP_ATTR_CONN_ID]) {
446446
ret = -EINVAL;
447-
goto out;
447+
goto err;
448448
}
449449

450450
tunnel_id = nla_get_u32(info->attrs[L2TP_ATTR_CONN_ID]);
451451

452-
tunnel = l2tp_tunnel_find(net, tunnel_id);
453-
if (tunnel == NULL) {
454-
ret = -ENODEV;
455-
goto out;
456-
}
457-
458452
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
459453
if (!msg) {
460454
ret = -ENOMEM;
461-
goto out;
455+
goto err;
456+
}
457+
458+
tunnel = l2tp_tunnel_get(net, tunnel_id);
459+
if (!tunnel) {
460+
ret = -ENODEV;
461+
goto err_nlmsg;
462462
}
463463

464464
ret = l2tp_nl_tunnel_send(msg, info->snd_portid, info->snd_seq,
465465
NLM_F_ACK, tunnel, L2TP_CMD_TUNNEL_GET);
466466
if (ret < 0)
467-
goto err_out;
467+
goto err_nlmsg_tunnel;
468+
469+
l2tp_tunnel_dec_refcount(tunnel);
468470

469471
return genlmsg_unicast(net, msg, info->snd_portid);
470472

471-
err_out:
473+
err_nlmsg_tunnel:
474+
l2tp_tunnel_dec_refcount(tunnel);
475+
err_nlmsg:
472476
nlmsg_free(msg);
473-
474-
out:
477+
err:
475478
return ret;
476479
}
477480

0 commit comments

Comments
 (0)