Skip to contentSkip to conversation

srcu: The value may overflow

PATCH Superseded Denis Arefev 2 participants 3 messages Sep 1, 2023 – Sep 2, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 srcu: The value may overflow

The value of an arithmetic expression 1 << (cpu - sdp->mynode->grplo) is subject to overflow due to a failure to cast operands to a larger data type before performing arithmetic

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
 kernel/rcu/srcutree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/srcutree.c +1 -1
index 20d7a238d675..e14b74fb1ba0 100644
@@ -223,7 +223,7 @@ static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags)
 				snp->grplo = cpu;
 			snp->grphi = cpu;
 		}
-		sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
+		sdp->grpmask = 1UL << (cpu - sdp->mynode->grplo);
 	}
 	smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_WAIT_BARRIER);
 	return true;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 16 lines · Denis Arefev

The value of an arithmetic expression 1 << (cpu - sdp->mynode->grplo) is subject to overflow due to a failure to cast operands to a larger data type before performing arithmetic

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
  kernel/rcu/srcutree.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/rcu/srcutree.c +0 -0
index 20d7a238d675..e14b74fb1ba0 100644

kernel/rcu/srcutree.c static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags) · line 226

Quoted 5 lines
kernel/rcu/srcutree.c +1 -1
@@ -223,7 +223,7 @@ static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags)
  				snp->grplo = cpu;
  			snp->grphi = cpu;
  		}
-		sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
+		sdp->grpmask = 1UL << (cpu - sdp->mynode->grplo);

What possible values of cpus supported by the Linux kernel and grplo can cause this to overflow on 64-bit architectures ? I suspect the maximum result of this subtraction is defined by the RCU_FANOUT or other srcu level-spread values assigned by rcu_init_levelspread(), which can indeed cause the signed 32-bit integer literal ("1") to overflow when shifted by any value greater than 31. This analysis should be added to the commit message so the impact of the issue can be understood.

I also notice this in the same file:

srcu_schedule_cbs_snp():

        for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
                if (!(mask & (1 << (cpu - snp->grplo))))
                        continue;

Which should be fixed at the same time.

Thanks,

Mathieu

Quoted 3 lines
kernel/rcu/srcutree.c +0 -0
  	}
  	smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_WAIT_BARRIER);
  	return true;
Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
#2 lore raw
Quoted 12 lines · Mathieu Desnoyers
Quoted 11 lines · Denis Arefev

The value of an arithmetic expression 1 << (cpu - sdp->mynode->grplo) is subject to overflow due to a failure to cast operands to a larger data type before performing arithmetic

Found by Linux Verification Center (linuxtesting.org) with SVACE.

---
  kernel/rcu/srcutree.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

kernel/rcu/srcutree.c static bool init_srcu_struct_nodes(struct · line 227

Quoted 11 lines
kernel/rcu/srcutree.c +1 -1
index 20d7a238d675..e14b74fb1ba0 100644
@@ -223,7 +223,7 @@ static bool init_srcu_struct_nodes(struct 
 srcu_struct *ssp, gfp_t gfp_flags)
                   snp->grplo = cpu;
               snp->grphi = cpu;
           }
-        sdp->grpmask = 1 << (cpu - sdp->mynode->grplo);
+        sdp->grpmask = 1UL << (cpu - sdp->mynode->grplo);

What possible values of cpus supported by the Linux kernel and grplo can cause this to overflow on 64-bit architectures ? I suspect the maximum result of this subtraction is defined by the RCU_FANOUT or other srcu level-spread values assigned by rcu_init_levelspread(), which can indeed cause the signed 32-bit integer literal ("1") to overflow when shifted by any value greater than 31. This analysis should be added to the commit message so the impact of the issue can be understood.

Another effect that might be worth mentioning in the commit message is what happens with the sign-carry for "1 << 31" when it is promoted from signed 32-bit integer to unsigned long on a 64-bit kernel: it translates to 0xffffffff80000000, which is certainly not what is expected here.

Thanks,

Mathieu

Quoted 19 lines

I also notice this in the same file:

srcu_schedule_cbs_snp():

        for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) { if (!(mask & (1 << (cpu - snp->grplo)))) continue;

Which should be fixed at the same time.

Thanks,

Mathieu

Quoted 3 lines
kernel/rcu/srcutree.c +0 -0
       }
       smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_WAIT_BARRIER);

      return true;

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com