All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock
@ 2022-03-29 16:12 Niels Dossche
  2022-03-30  3:09 ` Hoang Huu Le
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Niels Dossche @ 2022-03-29 16:12 UTC (permalink / raw)
  To: tipc-discussion
  Cc: netdev, Jon Maloy, Ying Xue, David S. Miller, Jakub Kicinski,
	Paolo Abeni, Hoang Le, Niels Dossche

Currently, n->keepalive_intv is written to while n is locked by a read
lock instead of a write lock. This seems to me to break the atomicity
against other readers.
Change this to a write lock instead to solve the issue.

Note:
I am currently working on a static analyser to detect missing locks
using type-based static analysis as my master's thesis
in order to obtain my master's degree.
If you would like to have more details, please let me know.
This was a reported case. I manually verified the report by looking
at the code, so that I do not send wrong information or patches.
After concluding that this seems to be a true positive, I created
this patch. I have both compile-tested this patch and runtime-tested
this patch on x86_64. The effect on a running system could be a
potential race condition in exceptional cases.
This issue was found on Linux v5.17.

Fixes: f5d6c3e5a359 ("tipc: fix node keep alive interval calculation")
Signed-off-by: Niels Dossche <dossche.niels@gmail.com>
---
 net/tipc/node.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/tipc/node.c b/net/tipc/node.c
index 6ef95ce565bd..da867ddb93f5 100644
--- a/net/tipc/node.c
+++ b/net/tipc/node.c
@@ -806,9 +806,9 @@ static void tipc_node_timeout(struct timer_list *t)
 	/* Initial node interval to value larger (10 seconds), then it will be
 	 * recalculated with link lowest tolerance
 	 */
-	tipc_node_read_lock(n);
+	tipc_node_write_lock(n);
 	n->keepalive_intv = 10000;
-	tipc_node_read_unlock(n);
+	tipc_node_write_unlock(n);
 	for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
 		tipc_node_read_lock(n);
 		le = &n->links[bearer_id];
-- 
2.35.1


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

* RE: [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock
  2022-03-29 16:12 [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock Niels Dossche
@ 2022-03-30  3:09 ` Hoang Huu Le
  2022-03-31  5:48 ` Jakub Kicinski
  2022-03-31 14:28 ` Paolo Abeni
  2 siblings, 0 replies; 6+ messages in thread
From: Hoang Huu Le @ 2022-03-30  3:09 UTC (permalink / raw)
  To: Niels Dossche, tipc-discussion
  Cc: netdev, Jon Maloy, Ying Xue, David S. Miller, Jakub Kicinski,
	Paolo Abeni

Hi Niels,

I did consider this function however I guess it is safe to use  tipc_node_read_lock()/unlock() since this value is being apply in this callback function. 

BTW, you must be using tipc_node_write_unlock_fast() instead of tipc_node_write_unlock().
Regards,
Hoang
> -----Original Message-----
> From: Niels Dossche <dossche.niels@gmail.com>
> Sent: Tuesday, March 29, 2022 11:12 PM
> To: tipc-discussion@lists.sourceforge.net
> Cc: netdev@vger.kernel.org; Jon Maloy <jmaloy@redhat.com>; Ying Xue <ying.xue@windriver.com>; David S. Miller
> <davem@davemloft.net>; Jakub Kicinski <kuba@kernel.org>; Paolo Abeni <pabeni@redhat.com>; Hoang Huu Le
> <hoang.h.le@dektech.com.au>; Niels Dossche <dossche.niels@gmail.com>
> Subject: [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock
> 
> Currently, n->keepalive_intv is written to while n is locked by a read
> lock instead of a write lock. This seems to me to break the atomicity
> against other readers.
> Change this to a write lock instead to solve the issue.
> 
> Note:
> I am currently working on a static analyser to detect missing locks
> using type-based static analysis as my master's thesis
> in order to obtain my master's degree.
> If you would like to have more details, please let me know.
> This was a reported case. I manually verified the report by looking
> at the code, so that I do not send wrong information or patches.
> After concluding that this seems to be a true positive, I created
> this patch. I have both compile-tested this patch and runtime-tested
> this patch on x86_64. The effect on a running system could be a
> potential race condition in exceptional cases.
> This issue was found on Linux v5.17.
> 
> Fixes: f5d6c3e5a359 ("tipc: fix node keep alive interval calculation")
> Signed-off-by: Niels Dossche <dossche.niels@gmail.com>
> ---
>  net/tipc/node.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/net/tipc/node.c b/net/tipc/node.c
> index 6ef95ce565bd..da867ddb93f5 100644
> --- a/net/tipc/node.c
> +++ b/net/tipc/node.c
> @@ -806,9 +806,9 @@ static void tipc_node_timeout(struct timer_list *t)
>  	/* Initial node interval to value larger (10 seconds), then it will be
>  	 * recalculated with link lowest tolerance
>  	 */
> -	tipc_node_read_lock(n);
> +	tipc_node_write_lock(n);
>  	n->keepalive_intv = 10000;
> -	tipc_node_read_unlock(n);
> +	tipc_node_write_unlock(n);
>  	for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
>  		tipc_node_read_lock(n);
>  		le = &n->links[bearer_id];
> --
> 2.35.1


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

* Re: [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock
  2022-03-29 16:12 [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock Niels Dossche
  2022-03-30  3:09 ` Hoang Huu Le
@ 2022-03-31  5:48 ` Jakub Kicinski
  2022-03-31 14:28 ` Paolo Abeni
  2 siblings, 0 replies; 6+ messages in thread
From: Jakub Kicinski @ 2022-03-31  5:48 UTC (permalink / raw)
  To: Jon Maloy
  Cc: Niels Dossche, tipc-discussion, netdev, Ying Xue,
	David S. Miller, Paolo Abeni, Hoang Le

On Tue, 29 Mar 2022 18:12:14 +0200 Niels Dossche wrote:
> Currently, n->keepalive_intv is written to while n is locked by a read
> lock instead of a write lock. This seems to me to break the atomicity
> against other readers.
> Change this to a write lock instead to solve the issue.
> 
> Note:
> I am currently working on a static analyser to detect missing locks
> using type-based static analysis as my master's thesis
> in order to obtain my master's degree.
> If you would like to have more details, please let me know.
> This was a reported case. I manually verified the report by looking
> at the code, so that I do not send wrong information or patches.
> After concluding that this seems to be a true positive, I created
> this patch. I have both compile-tested this patch and runtime-tested
> this patch on x86_64. The effect on a running system could be a
> potential race condition in exceptional cases.
> This issue was found on Linux v5.17.
> 
> Fixes: f5d6c3e5a359 ("tipc: fix node keep alive interval calculation")
> Signed-off-by: Niels Dossche <dossche.niels@gmail.com>

Looks good, Jon?

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

* Re: [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock
  2022-03-29 16:12 [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock Niels Dossche
  2022-03-30  3:09 ` Hoang Huu Le
  2022-03-31  5:48 ` Jakub Kicinski
@ 2022-03-31 14:28 ` Paolo Abeni
  2022-03-31 16:54   ` Jon Maloy
  2 siblings, 1 reply; 6+ messages in thread
From: Paolo Abeni @ 2022-03-31 14:28 UTC (permalink / raw)
  To: Niels Dossche, tipc-discussion
  Cc: netdev, Jon Maloy, Ying Xue, David S. Miller, Jakub Kicinski, Hoang Le

On Tue, 2022-03-29 at 18:12 +0200, Niels Dossche wrote:
> Currently, n->keepalive_intv is written to while n is locked by a read
> lock instead of a write lock. This seems to me to break the atomicity
> against other readers.
> Change this to a write lock instead to solve the issue.
> 
> Note:
> I am currently working on a static analyser to detect missing locks
> using type-based static analysis as my master's thesis
> in order to obtain my master's degree.
> If you would like to have more details, please let me know.
> This was a reported case. I manually verified the report by looking
> at the code, so that I do not send wrong information or patches.
> After concluding that this seems to be a true positive, I created
> this patch. I have both compile-tested this patch and runtime-tested
> this patch on x86_64. The effect on a running system could be a
> potential race condition in exceptional cases.
> This issue was found on Linux v5.17.
> 
> Fixes: f5d6c3e5a359 ("tipc: fix node keep alive interval calculation")
> Signed-off-by: Niels Dossche <dossche.niels@gmail.com>
> ---
>  net/tipc/node.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/net/tipc/node.c b/net/tipc/node.c
> index 6ef95ce565bd..da867ddb93f5 100644
> --- a/net/tipc/node.c
> +++ b/net/tipc/node.c
> @@ -806,9 +806,9 @@ static void tipc_node_timeout(struct timer_list *t)
>  	/* Initial node interval to value larger (10 seconds), then it will be
>  	 * recalculated with link lowest tolerance
>  	 */
> -	tipc_node_read_lock(n);
> +	tipc_node_write_lock(n);

I agree with Hoang, this should be safe even without write lock, as
tipc_node_timeout() is the only function modifying keepalive_intv, and
such function is invoked only by a timer, so we are guaranteeded there
are no possible concurrent updates...

>  	n->keepalive_intv = 10000;
> -	tipc_node_read_unlock(n);
> +	tipc_node_write_unlock(n);
>  	for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
>  		tipc_node_read_lock(n);

...otherwise we have a similar issue here: a few line below
keepalive_intv is updated via tipc_node_calculate_timer(), still under
the read lock

Thanks!

Paolo


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

* Re: [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock
  2022-03-31 14:28 ` Paolo Abeni
@ 2022-03-31 16:54   ` Jon Maloy
  2022-03-31 17:59     ` Niels Dossche
  0 siblings, 1 reply; 6+ messages in thread
From: Jon Maloy @ 2022-03-31 16:54 UTC (permalink / raw)
  To: Paolo Abeni, Niels Dossche, tipc-discussion
  Cc: netdev, Ying Xue, David S. Miller, Jakub Kicinski, Hoang Le



On 3/31/22 10:28, Paolo Abeni wrote:
> On Tue, 2022-03-29 at 18:12 +0200, Niels Dossche wrote:
>> Currently, n->keepalive_intv is written to while n is locked by a read
>> lock instead of a write lock. This seems to me to break the atomicity
>> against other readers.
>> Change this to a write lock instead to solve the issue.
>>
>> Note:
>> I am currently working on a static analyser to detect missing locks
>> using type-based static analysis as my master's thesis
>> in order to obtain my master's degree.
>> If you would like to have more details, please let me know.
>> This was a reported case. I manually verified the report by looking
>> at the code, so that I do not send wrong information or patches.
>> After concluding that this seems to be a true positive, I created
>> this patch. I have both compile-tested this patch and runtime-tested
>> this patch on x86_64. The effect on a running system could be a
>> potential race condition in exceptional cases.
>> This issue was found on Linux v5.17.
>>
>> Fixes: f5d6c3e5a359 ("tipc: fix node keep alive interval calculation")
>> Signed-off-by: Niels Dossche <dossche.niels@gmail.com>
>> ---
>>   net/tipc/node.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/net/tipc/node.c b/net/tipc/node.c
>> index 6ef95ce565bd..da867ddb93f5 100644
>> --- a/net/tipc/node.c
>> +++ b/net/tipc/node.c
>> @@ -806,9 +806,9 @@ static void tipc_node_timeout(struct timer_list *t)
>>   	/* Initial node interval to value larger (10 seconds), then it will be
>>   	 * recalculated with link lowest tolerance
>>   	 */
>> -	tipc_node_read_lock(n);
>> +	tipc_node_write_lock(n);
> I agree with Hoang, this should be safe even without write lock, as
> tipc_node_timeout() is the only function modifying keepalive_intv, and
> such function is invoked only by a timer, so we are guaranteeded there
> are no possible concurrent updates...
>
>>   	n->keepalive_intv = 10000;
>> -	tipc_node_read_unlock(n);
>> +	tipc_node_write_unlock(n);
>>   	for (bearer_id = 0; remains && (bearer_id < MAX_BEARERS); bearer_id++) {
>>   		tipc_node_read_lock(n);
> ...otherwise we have a similar issue here: a few line below
> keepalive_intv is updated via tipc_node_calculate_timer(), still under
> the read lock
>
> Thanks!
>
> Paolo
>
Hoang's and Paolo's conclusion is correct.
The patch is not needed.
///jon


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

* Re: [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock
  2022-03-31 16:54   ` Jon Maloy
@ 2022-03-31 17:59     ` Niels Dossche
  0 siblings, 0 replies; 6+ messages in thread
From: Niels Dossche @ 2022-03-31 17:59 UTC (permalink / raw)
  To: Jon Maloy, Paolo Abeni, tipc-discussion
  Cc: netdev, Ying Xue, David S. Miller, Jakub Kicinski, Hoang Le

On 31/03/2022 18:54, Jon Maloy wrote:
> Hoang's and Paolo's conclusion is correct.
> The patch is not needed.
> ///jon
> 

Thank you everyone for commenting on this.

Kind regards
Niels

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

end of thread, other threads:[~2022-03-31 17:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-29 16:12 [PATCH net] tipc: use a write lock for keepalive_intv instead of a read lock Niels Dossche
2022-03-30  3:09 ` Hoang Huu Le
2022-03-31  5:48 ` Jakub Kicinski
2022-03-31 14:28 ` Paolo Abeni
2022-03-31 16:54   ` Jon Maloy
2022-03-31 17:59     ` Niels Dossche

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.