netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] net/tls: Remove the context from the list in tls_device_down
@ 2022-07-21  9:11 Maxim Mikityanskiy
  2022-07-22 22:04 ` Jakub Kicinski
  2022-07-24 20:50 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 5+ messages in thread
From: Maxim Mikityanskiy @ 2022-07-21  9:11 UTC (permalink / raw)
  To: Jakub Kicinski, netdev
  Cc: Boris Pismenny, John Fastabend, David S. Miller, Eric Dumazet,
	Paolo Abeni, Tariq Toukan, Maxim Mikityanskiy

tls_device_down takes a reference on all contexts it's going to move to
the degraded state (software fallback). If sk_destruct runs afterwards,
it can reduce the reference counter back to 1 and return early without
destroying the context. Then tls_device_down will release the reference
it took and call tls_device_free_ctx. However, the context will still
stay in tls_device_down_list forever. The list will contain an item,
memory for which is released, making a memory corruption possible.

Fix the above bug by properly removing the context from all lists before
any call to tls_device_free_ctx.

Fixes: 3740651bf7e2 ("tls: Fix context leak on tls_device_down")
Signed-off-by: Maxim Mikityanskiy <maximmi@nvidia.com>
Reviewed-by: Tariq Toukan <tariqt@nvidia.com>
---
 net/tls/tls_device.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c
index 879b9024678e..9975df34d9c2 100644
--- a/net/tls/tls_device.c
+++ b/net/tls/tls_device.c
@@ -1376,8 +1376,13 @@ static int tls_device_down(struct net_device *netdev)
 		 * by tls_device_free_ctx. rx_conf and tx_conf stay in TLS_HW.
 		 * Now release the ref taken above.
 		 */
-		if (refcount_dec_and_test(&ctx->refcount))
+		if (refcount_dec_and_test(&ctx->refcount)) {
+			/* sk_destruct ran after tls_device_down took a ref, and
+			 * it returned early. Complete the destruction here.
+			 */
+			list_del(&ctx->list);
 			tls_device_free_ctx(ctx);
+		}
 	}
 
 	up_write(&device_offload_lock);
-- 
2.30.2


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

* Re: [PATCH net] net/tls: Remove the context from the list in tls_device_down
  2022-07-21  9:11 [PATCH net] net/tls: Remove the context from the list in tls_device_down Maxim Mikityanskiy
@ 2022-07-22 22:04 ` Jakub Kicinski
  2022-07-25 14:35   ` Maxim Mikityanskiy
  2022-07-24 20:50 ` patchwork-bot+netdevbpf
  1 sibling, 1 reply; 5+ messages in thread
From: Jakub Kicinski @ 2022-07-22 22:04 UTC (permalink / raw)
  To: Maxim Mikityanskiy
  Cc: netdev, Boris Pismenny, John Fastabend, David S. Miller,
	Eric Dumazet, Paolo Abeni, Tariq Toukan

On Thu, 21 Jul 2022 12:11:27 +0300 Maxim Mikityanskiy wrote:
> tls_device_down takes a reference on all contexts it's going to move to
> the degraded state (software fallback). If sk_destruct runs afterwards,
> it can reduce the reference counter back to 1 and return early without
> destroying the context. Then tls_device_down will release the reference
> it took and call tls_device_free_ctx. However, the context will still
> stay in tls_device_down_list forever. The list will contain an item,
> memory for which is released, making a memory corruption possible.
> 
> Fix the above bug by properly removing the context from all lists before
> any call to tls_device_free_ctx.

SGTM. The tls_device_down_list has no use, tho, is the plan to remove
it later as a cleanup or your upcoming patches make use of it?

We can delete it now if you don't have a preference, either way the fix
is small.

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

* Re: [PATCH net] net/tls: Remove the context from the list in tls_device_down
  2022-07-21  9:11 [PATCH net] net/tls: Remove the context from the list in tls_device_down Maxim Mikityanskiy
  2022-07-22 22:04 ` Jakub Kicinski
@ 2022-07-24 20:50 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-07-24 20:50 UTC (permalink / raw)
  To: Maxim Mikityanskiy
  Cc: kuba, netdev, borisp, john.fastabend, davem, edumazet, pabeni, tariqt

Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <davem@davemloft.net>:

On Thu, 21 Jul 2022 12:11:27 +0300 you wrote:
> tls_device_down takes a reference on all contexts it's going to move to
> the degraded state (software fallback). If sk_destruct runs afterwards,
> it can reduce the reference counter back to 1 and return early without
> destroying the context. Then tls_device_down will release the reference
> it took and call tls_device_free_ctx. However, the context will still
> stay in tls_device_down_list forever. The list will contain an item,
> memory for which is released, making a memory corruption possible.
> 
> [...]

Here is the summary with links:
  - [net] net/tls: Remove the context from the list in tls_device_down
    https://git.kernel.org/netdev/net/c/f6336724a4d4

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

* Re: [PATCH net] net/tls: Remove the context from the list in tls_device_down
  2022-07-22 22:04 ` Jakub Kicinski
@ 2022-07-25 14:35   ` Maxim Mikityanskiy
  2022-07-25 18:37     ` Jakub Kicinski
  0 siblings, 1 reply; 5+ messages in thread
From: Maxim Mikityanskiy @ 2022-07-25 14:35 UTC (permalink / raw)
  To: kuba
  Cc: Tariq Toukan, pabeni, edumazet, netdev, Boris Pismenny,
	john.fastabend, davem

On Fri, 2022-07-22 at 15:04 -0700, Jakub Kicinski wrote:
> On Thu, 21 Jul 2022 12:11:27 +0300 Maxim Mikityanskiy wrote:
> > tls_device_down takes a reference on all contexts it's going to move to
> > the degraded state (software fallback). If sk_destruct runs afterwards,
> > it can reduce the reference counter back to 1 and return early without
> > destroying the context. Then tls_device_down will release the reference
> > it took and call tls_device_free_ctx. However, the context will still
> > stay in tls_device_down_list forever. The list will contain an item,
> > memory for which is released, making a memory corruption possible.
> > 
> > Fix the above bug by properly removing the context from all lists before
> > any call to tls_device_free_ctx.
> 
> SGTM. The tls_device_down_list has no use, tho, is the plan to remove
> it later as a cleanup or your upcoming patches make use of it?

I don't plan to remove it. Right, we never iterate over it, so instead
of moving the context to tls_device_down_list, we can remove it from
list, as long as we check to not remove it second time on destruction.

However, this way we don't gain anything, but lose a debugging
opportunity: for example, when list debugging is enabled, double
list_del will be detected.

So, it doesn't make sense to me to remove this list, but if you still
want to do it, Tariq has a patch for this.

> 
> We can delete it now if you don't have a preference, either way the fix
> is small.


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

* Re: [PATCH net] net/tls: Remove the context from the list in tls_device_down
  2022-07-25 14:35   ` Maxim Mikityanskiy
@ 2022-07-25 18:37     ` Jakub Kicinski
  0 siblings, 0 replies; 5+ messages in thread
From: Jakub Kicinski @ 2022-07-25 18:37 UTC (permalink / raw)
  To: Maxim Mikityanskiy
  Cc: Tariq Toukan, pabeni, edumazet, netdev, Boris Pismenny,
	john.fastabend, davem

On Mon, 25 Jul 2022 14:35:08 +0000 Maxim Mikityanskiy wrote:
> On Fri, 2022-07-22 at 15:04 -0700, Jakub Kicinski wrote:
> > On Thu, 21 Jul 2022 12:11:27 +0300 Maxim Mikityanskiy wrote:  
> > > tls_device_down takes a reference on all contexts it's going to move to
> > > the degraded state (software fallback). If sk_destruct runs afterwards,
> > > it can reduce the reference counter back to 1 and return early without
> > > destroying the context. Then tls_device_down will release the reference
> > > it took and call tls_device_free_ctx. However, the context will still
> > > stay in tls_device_down_list forever. The list will contain an item,
> > > memory for which is released, making a memory corruption possible.
> > > 
> > > Fix the above bug by properly removing the context from all lists before
> > > any call to tls_device_free_ctx.  
> > 
> > SGTM. The tls_device_down_list has no use, tho, is the plan to remove
> > it later as a cleanup or your upcoming patches make use of it?  
> 
> I don't plan to remove it. Right, we never iterate over it, so instead
> of moving the context to tls_device_down_list, we can remove it from
> list, as long as we check to not remove it second time on destruction.
> 
> However, this way we don't gain anything, but lose a debugging
> opportunity: for example, when list debugging is enabled, double
> list_del will be detected.

I see. I haven't actually checked if list_del_init() would do as well
here.

> So, it doesn't make sense to me to remove this list, but if you still
> want to do it, Tariq has a patch for this.

Fine either way, thanks for the explanation.

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

end of thread, other threads:[~2022-07-25 18:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-21  9:11 [PATCH net] net/tls: Remove the context from the list in tls_device_down Maxim Mikityanskiy
2022-07-22 22:04 ` Jakub Kicinski
2022-07-25 14:35   ` Maxim Mikityanskiy
2022-07-25 18:37     ` Jakub Kicinski
2022-07-24 20:50 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).