Skip to content

Commit e08293a

Browse files
Guillaume Naultdavem330
authored andcommitted
l2tp: take reference on sessions being dumped
Take a reference on the sessions returned by l2tp_session_find_nth() (and rename it l2tp_session_get_nth() to reflect this change), so that caller is assured that the session isn't going to disappear while processing it. For procfs and debugfs handlers, the session is held in the .start() callback and dropped in .show(). Given that pppol2tp_seq_session_show() dereferences the associated PPPoL2TP socket and that l2tp_dfs_seq_session_show() might call pppol2tp_show(), we also need to call the session's .ref() callback to prevent the socket from going away from under us. Fixes: fd558d1 ("l2tp: Split pppol2tp patch into separate l2tp and ppp parts") Fixes: 0ad6614 ("l2tp: Add debugfs files for dumping l2tp debug info") Fixes: 309795f ("l2tp: Add netlink control API for L2TP") Signed-off-by: Guillaume Nault <g.nault@alphalink.fr> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 0b9aefe commit e08293a

5 files changed

Lines changed: 27 additions & 11 deletions

File tree

‎net/l2tp/l2tp_core.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ struct l2tp_session *l2tp_session_get(struct net *net,
327327
}
328328
EXPORT_SYMBOL_GPL(l2tp_session_get);
329329

330-
struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
330+
struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
331+
bool do_ref)
331332
{
332333
int hash;
333334
struct l2tp_session *session;
@@ -337,6 +338,9 @@ struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
337338
for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
338339
hlist_for_each_entry(session, &tunnel->session_hlist[hash], hlist) {
339340
if (++count > nth) {
341+
l2tp_session_inc_refcount(session);
342+
if (do_ref && session->ref)
343+
session->ref(session);
340344
read_unlock_bh(&tunnel->hlist_lock);
341345
return session;
342346
}
@@ -347,7 +351,7 @@ struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth)
347351

348352
return NULL;
349353
}
350-
EXPORT_SYMBOL_GPL(l2tp_session_find_nth);
354+
EXPORT_SYMBOL_GPL(l2tp_session_get_nth);
351355

352356
/* Lookup a session by interface name.
353357
* This is very inefficient but is only used by management interfaces.

‎net/l2tp/l2tp_core.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ struct l2tp_session *l2tp_session_get(struct net *net,
236236
struct l2tp_session *l2tp_session_find(struct net *net,
237237
struct l2tp_tunnel *tunnel,
238238
u32 session_id);
239-
struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth);
239+
struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth,
240+
bool do_ref);
240241
struct l2tp_session *l2tp_session_get_by_ifname(struct net *net, char *ifname,
241242
bool do_ref);
242243
struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id);

‎net/l2tp/l2tp_debugfs.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void l2tp_dfs_next_tunnel(struct l2tp_dfs_seq_data *pd)
5353

5454
static void l2tp_dfs_next_session(struct l2tp_dfs_seq_data *pd)
5555
{
56-
pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
56+
pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx, true);
5757
pd->session_idx++;
5858

5959
if (pd->session == NULL) {
@@ -238,10 +238,14 @@ static int l2tp_dfs_seq_show(struct seq_file *m, void *v)
238238
}
239239

240240
/* Show the tunnel or session context */
241-
if (pd->session == NULL)
241+
if (!pd->session) {
242242
l2tp_dfs_seq_tunnel_show(m, pd->tunnel);
243-
else
243+
} else {
244244
l2tp_dfs_seq_session_show(m, pd->session);
245+
if (pd->session->deref)
246+
pd->session->deref(pd->session);
247+
l2tp_session_dec_refcount(pd->session);
248+
}
245249

246250
out:
247251
return 0;

‎net/l2tp/l2tp_netlink.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback
867867
goto out;
868868
}
869869

870-
session = l2tp_session_find_nth(tunnel, si);
870+
session = l2tp_session_get_nth(tunnel, si, false);
871871
if (session == NULL) {
872872
ti++;
873873
tunnel = NULL;
@@ -877,8 +877,11 @@ static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback
877877

878878
if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
879879
cb->nlh->nlmsg_seq, NLM_F_MULTI,
880-
session, L2TP_CMD_SESSION_GET) < 0)
880+
session, L2TP_CMD_SESSION_GET) < 0) {
881+
l2tp_session_dec_refcount(session);
881882
break;
883+
}
884+
l2tp_session_dec_refcount(session);
882885

883886
si++;
884887
}

‎net/l2tp/l2tp_ppp.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
15601560

15611561
static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
15621562
{
1563-
pd->session = l2tp_session_find_nth(pd->tunnel, pd->session_idx);
1563+
pd->session = l2tp_session_get_nth(pd->tunnel, pd->session_idx, true);
15641564
pd->session_idx++;
15651565

15661566
if (pd->session == NULL) {
@@ -1687,10 +1687,14 @@ static int pppol2tp_seq_show(struct seq_file *m, void *v)
16871687

16881688
/* Show the tunnel or session context.
16891689
*/
1690-
if (pd->session == NULL)
1690+
if (!pd->session) {
16911691
pppol2tp_seq_tunnel_show(m, pd->tunnel);
1692-
else
1692+
} else {
16931693
pppol2tp_seq_session_show(m, pd->session);
1694+
if (pd->session->deref)
1695+
pd->session->deref(pd->session);
1696+
l2tp_session_dec_refcount(pd->session);
1697+
}
16941698

16951699
out:
16961700
return 0;

0 commit comments

Comments
 (0)