Skip to content

Commit fbea9e0

Browse files
ereshetovadavem330
authored andcommitted
net, l2tp: convert l2tp_tunnel.ref_count from atomic_t to refcount_t
refcount_t type and corresponding API should be used instead of atomic_t when the variable is used as a reference counter. This allows to avoid accidental refcounter overflows that might lead to use-after-free situations. Signed-off-by: Elena Reshetova <elena.reshetova@intel.com> Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com> Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: David Windsor <dwindsor@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 58951dd commit fbea9e0

4 files changed

Lines changed: 12 additions & 11 deletions

File tree

‎net/l2tp/l2tp_core.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,27 +132,27 @@ static inline struct l2tp_net *l2tp_pernet(const struct net *net)
132132
*/
133133
static inline void l2tp_tunnel_inc_refcount_1(struct l2tp_tunnel *tunnel)
134134
{
135-
atomic_inc(&tunnel->ref_count);
135+
refcount_inc(&tunnel->ref_count);
136136
}
137137

138138
static inline void l2tp_tunnel_dec_refcount_1(struct l2tp_tunnel *tunnel)
139139
{
140-
if (atomic_dec_and_test(&tunnel->ref_count))
140+
if (refcount_dec_and_test(&tunnel->ref_count))
141141
l2tp_tunnel_free(tunnel);
142142
}
143143
#ifdef L2TP_REFCNT_DEBUG
144144
#define l2tp_tunnel_inc_refcount(_t) \
145145
do { \
146146
pr_debug("l2tp_tunnel_inc_refcount: %s:%d %s: cnt=%d\n", \
147147
__func__, __LINE__, (_t)->name, \
148-
atomic_read(&_t->ref_count)); \
148+
refcount_read(&_t->ref_count)); \
149149
l2tp_tunnel_inc_refcount_1(_t); \
150150
} while (0)
151151
#define l2tp_tunnel_dec_refcount(_t) \
152152
do { \
153153
pr_debug("l2tp_tunnel_dec_refcount: %s:%d %s: cnt=%d\n", \
154154
__func__, __LINE__, (_t)->name, \
155-
atomic_read(&_t->ref_count)); \
155+
refcount_read(&_t->ref_count)); \
156156
l2tp_tunnel_dec_refcount_1(_t); \
157157
} while (0)
158158
#else
@@ -1353,7 +1353,7 @@ static void l2tp_udp_encap_destroy(struct sock *sk)
13531353
*/
13541354
static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
13551355
{
1356-
BUG_ON(atomic_read(&tunnel->ref_count) != 0);
1356+
BUG_ON(refcount_read(&tunnel->ref_count) != 0);
13571357
BUG_ON(tunnel->sock != NULL);
13581358
l2tp_info(tunnel, L2TP_MSG_CONTROL, "%s: free...\n", tunnel->name);
13591359
kfree_rcu(tunnel, rcu);
@@ -1667,7 +1667,7 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
16671667
/* Bump the reference count. The tunnel context is deleted
16681668
* only when this drops to zero. Must be done before list insertion
16691669
*/
1670-
l2tp_tunnel_inc_refcount(tunnel);
1670+
refcount_set(&tunnel->ref_count, 1);
16711671
spin_lock_bh(&pn->l2tp_tunnel_list_lock);
16721672
list_add_rcu(&tunnel->list, &pn->l2tp_tunnel_list);
16731673
spin_unlock_bh(&pn->l2tp_tunnel_list_lock);
@@ -1706,7 +1706,7 @@ void l2tp_session_free(struct l2tp_session *session)
17061706
{
17071707
struct l2tp_tunnel *tunnel = session->tunnel;
17081708

1709-
BUG_ON(atomic_read(&session->ref_count) != 0);
1709+
BUG_ON(refcount_read(&session->ref_count) != 0);
17101710

17111711
if (tunnel) {
17121712
BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC);

‎net/l2tp/l2tp_core.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* it under the terms of the GNU General Public License version 2 as
88
* published by the Free Software Foundation.
99
*/
10+
#include <linux/refcount.h>
1011

1112
#ifndef _L2TP_CORE_H_
1213
#define _L2TP_CORE_H_
@@ -177,7 +178,7 @@ struct l2tp_tunnel {
177178
struct list_head list; /* Keep a list of all tunnels */
178179
struct net *l2tp_net; /* the net we belong to */
179180

180-
atomic_t ref_count;
181+
refcount_t ref_count;
181182
#ifdef CONFIG_DEBUG_FS
182183
void (*show)(struct seq_file *m, void *arg);
183184
#endif

‎net/l2tp/l2tp_debugfs.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static void l2tp_dfs_seq_tunnel_show(struct seq_file *m, void *v)
145145
"");
146146
seq_printf(m, " %d sessions, refcnt %d/%d\n", session_count,
147147
tunnel->sock ? refcount_read(&tunnel->sock->sk_refcnt) : 0,
148-
atomic_read(&tunnel->ref_count));
148+
refcount_read(&tunnel->ref_count));
149149
seq_printf(m, " %08x rx %ld/%ld/%ld rx %ld/%ld/%ld\n",
150150
tunnel->debug,
151151
atomic_long_read(&tunnel->stats.tx_packets),
@@ -170,7 +170,7 @@ static void l2tp_dfs_seq_session_show(struct seq_file *m, void *v)
170170
"");
171171
if (session->send_seq || session->recv_seq)
172172
seq_printf(m, " nr %hu, ns %hu\n", session->nr, session->ns);
173-
seq_printf(m, " refcnt %d\n", atomic_read(&session->ref_count));
173+
seq_printf(m, " refcnt %d\n", refcount_read(&session->ref_count));
174174
seq_printf(m, " config %d/%d/%c/%c/%s/%s %08x %u\n",
175175
session->mtu, session->mru,
176176
session->recv_seq ? 'R' : '-',

‎net/l2tp/l2tp_ppp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ static void pppol2tp_seq_tunnel_show(struct seq_file *m, void *v)
16161616
seq_printf(m, "\nTUNNEL '%s', %c %d\n",
16171617
tunnel->name,
16181618
(tunnel == tunnel->sock->sk_user_data) ? 'Y' : 'N',
1619-
atomic_read(&tunnel->ref_count) - 1);
1619+
refcount_read(&tunnel->ref_count) - 1);
16201620
seq_printf(m, " %08x %ld/%ld/%ld %ld/%ld/%ld\n",
16211621
tunnel->debug,
16221622
atomic_long_read(&tunnel->stats.tx_packets),

0 commit comments

Comments
 (0)