l2tp: fix tunnel and session refcount leak on seq_file release
Subsystems (3)
PATCH 1/1 l2tp: fix tunnel and session refcount leak on seq_file release
In pppol2tp_proc_open() and l2tp_dfs_seq_open(), iteration state (pd->tunnel and pd->session) is kept in seq_file private data to allow iteration across multiple read() system calls.
However, if userspace closes /proc/net/pppol2tp or /sys/kernel/debug/l2tp/tunnels before reading to end-of-file (EOF), any tunnel or session reference stored in pd->tunnel / pd->session is left un-dropped when seq_file private data is freed.
Fix this by dropping any remaining pd->tunnel and pd->session references in pppol2tp_proc_release() and l2tp_dfs_seq_release() when closing the file.
--- net/l2tp/l2tp_debugfs.c | 4 ++++ net/l2tp/l2tp_ppp.c | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-)
net/l2tp/l2tp_debugfs.c +4 -0
@@ -306,6 +306,10 @@ static int l2tp_dfs_seq_release(struct inode *inode, struct file *file) seq = file->private_data; pd = seq->private; +if (pd->session) +l2tp_session_put (pd->session); +if (pd->tunnel) +l2tp_tunnel_put (pd->tunnel);if (pd->net)put_net_track (pd->net, &pd->ns_tracker);kfree (pd);
net/l2tp/l2tp_ppp.c +36 -2
@@ -1597,6 +1597,40 @@ static const struct seq_operations pppol2tp_seq_ops = { .stop = pppol2tp_seq_stop, .show = pppol2tp_seq_show, }; + +static int pppol2tp_proc_open (struct inode *inode,struct file *file) +{ +struct net *net =pde_data (inode); +struct pppol2tp_seq_data *pd; + + pd =__seq_open_private (file, &pppol2tp_seq_ops,sizeof (*pd)); +if (!pd) +return -ENOMEM ; + + pd->p.net =get_net_track (net, &pd->p.ns_tracker,GFP_KERNEL ); +return 0 ; +} + +static int pppol2tp_proc_release (struct inode *inode,struct file *file) +{ +struct seq_file *seq = file->private_data; +struct pppol2tp_seq_data *pd = seq->private; + +if (pd->session) +l2tp_session_put (pd->session); +if (pd->tunnel) +l2tp_tunnel_put (pd->tunnel); + +put_net_track (pd->p.net, &pd->p.ns_tracker); +return seq_release_private (inode, file); +} + +static const struct proc_ops pppol2tp_proc_ops = { + .proc_open = pppol2tp_proc_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = pppol2tp_proc_release, +};#endif /* CONFIG_PROC_FS */ /*****************************************************************************
@@ -1608,8 +1642,8 @@ static __net_init int pppol2tp_init_net(struct net *net)struct proc_dir_entry *pde;int err =0 ; - pde =proc_create_net ("pppol2tp" ,0444 , net->proc_net, - &pppol2tp_seq_ops,sizeof (struct pppol2tp_seq_data )); + pde =proc_create_data ("pppol2tp" ,0444 , net->proc_net, + &pppol2tp_proc_ops, net);if (!pde) { err = -ENOMEM ; goto out;
Signature · 1 line
2.55.0.654.g21b8a5bc05-goog
Quoted 49 lines · Eric Dumazet
In
pppol2tp_proc_open()andl2tp_dfs_seq_open(), iteration state (pd->tunnelandpd->session) is kept inseq_fileprivate data to allow iteration across multipleread()system calls.However, if userspace closes
/proc/net/pppol2tpor/sys/kernel/debug/l2tp/tunnelsbefore reading to end-of-file (EOF), any tunnel or session reference stored inpd->tunnel/pd->sessionis left un-dropped whenseq_fileprivate data is freed.Fix this by dropping any remaining
pd->tunnelandpd->sessionreferences inpppol2tp_proc_release()andl2tp_dfs_seq_release()when closing the file.Fixes 0e0c3fee3a59 ("l2tp: hold reference on tunnels printed in pppol2tp proc file")Fixes f726214d9b23 ("l2tp: hold reference on tunnels printed in l2tp/tunnels debugfs file")Reported-by [email protected]Assisted-by Jetski:Gemini-3.1-ProCc James Chapman <[email protected]>Cc Guillaume Nault <[email protected]>Signed-off-by Eric Dumazet <[email protected]>--- net/l2tp/l2tp_debugfs.c | 4 ++++ net/l2tp/l2tp_ppp.c | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-)net/l2tp/l2tp_debugfs.c +4
-0@@ -306,6 +306,10 @@ static int l2tp_dfs_seq_release(struct inode *inode, struct file *file) seq = file->private_data; pd = seq->private; +if (pd->session) +l2tp_session_put (pd->session); +if (pd->tunnel) +l2tp_tunnel_put (pd->tunnel);if (pd->net)put_net_track (pd->net, &pd->ns_tracker);kfree (pd);net/l2tp/l2tp_ppp.c +1
-0@@ -1597,6 +1597,40 @@ static const struct seq_operations pppol2tp_seq_ops = { .stop = pppol2tp_seq_stop, .show = pppol2tp_seq_show, }; +
net/l2tp/l2tp_ppp.c static const struct seq_operations pppol2tp_seq_ops = { · line 1610
Quoted 10 lines
net/l2tp/l2tp_ppp.c +10
-0+static int pppol2tp_proc_open (struct inode *inode,struct file *file) +{ +struct net *net =pde_data (inode); +struct pppol2tp_seq_data *pd; + + pd =__seq_open_private (file, &pppol2tp_seq_ops,sizeof (*pd)); +if (!pd) +return -ENOMEM ; + + pd->p.net =get_net_track (net, &pd->p.ns_tracker,GFP_KERNEL );
Oh well, we have a dependency against CONFIG_NET_NS for this stuff.
pw-bot: cr