All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] l2tp: fix possible use-after-free
@ 2022-06-10  5:16 Xiaohui Zhang
  2022-06-14  8:06 ` Paolo Abeni
  0 siblings, 1 reply; 3+ messages in thread
From: Xiaohui Zhang @ 2022-06-10  5:16 UTC (permalink / raw)
  To: Xiaohui Zhang, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Xiyu Yang, Xin Tan, Xin Xiong, Tom Parkin, netdev,
	linux-kernel

Similar to the handling of l2tp_tunnel_get in commit a622b40035d1
("l2ip: fix possible use-after-free"), we thought a patch might
be needed here as well.

Before taking a refcount on a rcu protected structure,
we need to make sure the refcount is not zero.

Signed-off-by: Xiaohui Zhang <xiaohuizhang@ruc.edu.cn>
---
 net/l2tp/l2tp_core.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c
index 7499c51b1850..96e8966f900d 100644
--- a/net/l2tp/l2tp_core.c
+++ b/net/l2tp/l2tp_core.c
@@ -252,8 +252,8 @@ struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
 
 	rcu_read_lock_bh();
 	hlist_for_each_entry_rcu(session, session_list, hlist)
-		if (session->session_id == session_id) {
-			l2tp_session_inc_refcount(session);
+		if (session->session_id == session_id &&
+		    refcount_inc_not_zero(&session->ref_count)) {
 			rcu_read_unlock_bh();
 
 			return session;
@@ -273,8 +273,8 @@ struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id)
 
 	rcu_read_lock_bh();
 	hlist_for_each_entry_rcu(session, session_list, global_hlist)
-		if (session->session_id == session_id) {
-			l2tp_session_inc_refcount(session);
+		if (session->session_id == session_id &&
+		    refcount_inc_not_zero(&session->ref_count)) {
 			rcu_read_unlock_bh();
 
 			return session;
@@ -294,8 +294,8 @@ struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth)
 	rcu_read_lock_bh();
 	for (hash = 0; hash < L2TP_HASH_SIZE; hash++) {
 		hlist_for_each_entry_rcu(session, &tunnel->session_hlist[hash], hlist) {
-			if (++count > nth) {
-				l2tp_session_inc_refcount(session);
+			if (++count > nth &&
+			    refcount_inc_not_zero(&session->ref_count)) {
 				rcu_read_unlock_bh();
 				return session;
 			}
@@ -321,8 +321,8 @@ struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
 	rcu_read_lock_bh();
 	for (hash = 0; hash < L2TP_HASH_SIZE_2; hash++) {
 		hlist_for_each_entry_rcu(session, &pn->l2tp_session_hlist[hash], global_hlist) {
-			if (!strcmp(session->ifname, ifname)) {
-				l2tp_session_inc_refcount(session);
+			if (!strcmp(session->ifname, ifname) &&
+			    refcount_inc_not_zero(&session->ref_count)) {
 				rcu_read_unlock_bh();
 
 				return session;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/1] l2tp: fix possible use-after-free
  2022-06-10  5:16 [PATCH 1/1] l2tp: fix possible use-after-free Xiaohui Zhang
@ 2022-06-14  8:06 ` Paolo Abeni
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Abeni @ 2022-06-14  8:06 UTC (permalink / raw)
  To: Xiaohui Zhang, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Xiyu Yang, Xin Tan, Xin Xiong, Tom Parkin, netdev, linux-kernel

On Fri, 2022-06-10 at 13:16 +0800, Xiaohui Zhang wrote:
> Similar to the handling of l2tp_tunnel_get in commit a622b40035d1
> ("l2ip: fix possible use-after-free"), we thought a patch might
> be needed here as well.
> 
> Before taking a refcount on a rcu protected structure,
> we need to make sure the refcount is not zero.
> 
> Signed-off-by: Xiaohui Zhang <xiaohuizhang@ruc.edu.cn>

I'm sorry for the late, trivial feedback, but we need a Fixes: tag
here. Since you need to repost, please insert the target tree
explicitly into the email subj.

Thanks!

Paolo


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/1] l2tp: fix possible use-after-free
@ 2022-06-05 11:35 Xiaohui Zhang
  0 siblings, 0 replies; 3+ messages in thread
From: Xiaohui Zhang @ 2022-06-05 11:35 UTC (permalink / raw)
  To: Xiaohui Zhang, David S . Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, netdev, linux-kernel

From: xiaohuizhang98 <ruc_zhangxiaohui@163.com>

We detected a suspected bug with our code clone detection tool.

Similar to the handling of l2tp_tunnel_get in commit a622b40035d1
("l2ip: fix possible use-after-free"), we thought a patch might
be needed here as well.

Before taking a refcount on a rcu protected structure,
we need to make sure the refcount is not zero.

Signed-off-by: xiaohuizhang98 <ruc_zhangxiaohui@163.com>
---
 net/l2tp/l2tp_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c
index 7499c51b1850..b759fbd09b65 100644
--- a/net/l2tp/l2tp_core.c
+++ b/net/l2tp/l2tp_core.c
@@ -252,8 +252,8 @@ struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
 
 	rcu_read_lock_bh();
 	hlist_for_each_entry_rcu(session, session_list, hlist)
-		if (session->session_id == session_id) {
-			l2tp_session_inc_refcount(session);
+		if (session->session_id == session_id &&
+		    refcount_inc_not_zero(&session->ref_count)) {
 			rcu_read_unlock_bh();
 
 			return session;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-06-14  8:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-10  5:16 [PATCH 1/1] l2tp: fix possible use-after-free Xiaohui Zhang
2022-06-14  8:06 ` Paolo Abeni
  -- strict thread matches above, loose matches on Subject: below --
2022-06-05 11:35 Xiaohui Zhang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.