Skip to contentSkip to conversation

ipc: sem: fix used_sems overflow in newary()

PATCH In review Ren Wei 4 participants 4 messages May 11 – Nov 7 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Ren Wei

author
#1 lore raw

PATCH 1/1 ipc: sem: fix used_sems overflow in newary()

From: Ruide Cao <[email protected]>

newary() checks namespace-wide semaphore usage before creating a new array, but the current accounting uses a plain signed addition.

If the accumulated semaphore count overflows, the limit check can fail open and allow allocations past sc_semmns, breaking namespace semaphore resource enforcement and potentially leading to resource exhaustion.

Fix this by using check_add_overflow() before comparing the new total against sc_semmns, and reject overflow the same way as a true limit exceed.

---
 ipc/sem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ipc/sem.c +3 -1
index 6cdf862b1f5c..00c45de33c46 100644
@@ -535,11 +535,13 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int nsems = params->u.nsems;
 	int semflg = params->flg;
+	int total_sems;
 	int i;
 
 	if (!nsems)
 		return -EINVAL;
-	if (ns->used_sems + nsems > ns->sc_semmns)
+	if (check_add_overflow(ns->used_sems, nsems, &total_sems) ||
+	    total_sems > ns->sc_semmns)
 		return -ENOSPC;
 
 	sma = sem_alloc(nsems);
Signature · 1 line
2.34.1
#2 lore raw
Quoted 1 line · On Mon, 11 May 2026 18:42:53 +0800 Ren Wei <[email protected]> wrote:

Slight delay, sorry!

Quoted 5 lines

From: Ruide Cao <[email protected]>

newary() checks namespace-wide semaphore usage before creating a new array, but the current accounting uses a plain signed addition.

Quoted 7 lines

If the accumulated semaphore count overflows, the limit check can fail open and allow allocations past sc_semmns, breaking namespace semaphore resource enforcement and potentially leading to resource exhaustion.

Fix this by using check_add_overflow() before comparing the new total against sc_semmns, and reject overflow the same way as a true limit exceed.

LLM review tells me

: The fix looks correct. The existing writer lock covers the check and : the later used_sems increment, so that increment needs no further : change.

and

: Please qualify the changelog. Default SEMMNS and SEMMSL cannot trigger : this overflow. It requires raised limits and substantial existing : allocations: sem_alloc() separately bounds each array's size, so one : enormous request isn't sufficient to demonstrate a successful bypass. :
: With sufficient memory, semget() can then succeed instead of returning : ENOSPC, exceed the namespace quota and wrap the semaphore usage reported : by SEM_INFO negative. Additional allocations can increase memory : pressure.

So I propose this changelog. Please check:

: newary() checks the namespace-wide semaphore limit using a signed : addition of used_sems and nsems. If that addition overflows, the : comparison can fail to reject a request which exceeds sc_semmns. :
: The default semaphore limits cannot trigger this overflow. It requires : raised limits and substantial existing allocations. sem_alloc() : separately bounds each array's allocation size, so a single enormous : request is insufficient to demonstrate a successful quota bypass. :
: With enough memory, semget() can succeed instead of returning ENOSPC, : allowing allocations beyond the namespace semaphore quota and increasing : memory pressure. The wrapped used_sems count can also cause SEM_INFO to : report a negative semaphore usage count.
:
: Use check_add_overflow() before comparing the new total against : sc_semmns, rejecting overflow with ENOSPC. The existing writer lock : covers both the check and the later accounting update.

Quoted 35 lines
---
 ipc/sem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ipc/sem.c +3 -1
index 6cdf862b1f5c..00c45de33c46 100644
@@ -535,11 +535,13 @@ static int newary(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int nsems = params->u.nsems;
 	int semflg = params->flg;
+	int total_sems;
 	int i;
 
 	if (!nsems)
 		return -EINVAL;
-	if (ns->used_sems + nsems > ns->sc_semmns)
+	if (check_add_overflow(ns->used_sems, nsems, &total_sems) ||
+	    total_sems > ns->sc_semmns)
 		return -ENOSPC;
 
 	sma = sem_alloc(nsems);
Signature · 1 line
2.34.1
#3 lore raw
Quoted 46 lines · Andrew Morton
Quoted 1 line · On Mon, 11 May 2026 18:42:53 +0800 Ren Wei <[email protected]> wrote:

Slight delay, sorry!

Quoted 5 lines

From: Ruide Cao <[email protected]>

newary() checks namespace-wide semaphore usage before creating a new array, but the current accounting uses a plain signed addition.

Quoted 7 lines

If the accumulated semaphore count overflows, the limit check can fail open and allow allocations past sc_semmns, breaking namespace semaphore resource enforcement and potentially leading to resource exhaustion.

Fix this by using check_add_overflow() before comparing the new total against sc_semmns, and reject overflow the same way as a true limit exceed.

LLM review tells me

: The fix looks correct. The existing writer lock covers the check and : the later used_sems increment, so that increment needs no further : change.

and

: Please qualify the changelog. Default SEMMNS and SEMMSL cannot trigger : this overflow. It requires raised limits and substantial existing : allocations: sem_alloc() separately bounds each array's size, so one : enormous request isn't sufficient to demonstrate a successful bypass. :
: With sufficient memory, semget() can then succeed instead of returning : ENOSPC, exceed the namespace quota and wrap the semaphore usage reported : by SEM_INFO negative. Additional allocations can increase memory : pressure.

So I propose this changelog. Please check:

: newary() checks the namespace-wide semaphore limit using a signed : addition of used_sems and nsems. If that addition overflows, the : comparison can fail to reject a request which exceeds sc_semmns. :
: The default semaphore limits cannot trigger this overflow. It requires : raised limits and substantial existing allocations. sem_alloc() : separately bounds each array's allocation size, so a single enormous : request is insufficient to demonstrate a successful quota bypass. :

Quoted 8 lines

: With enough memory, semget() can succeed instead of returning ENOSPC, : allowing allocations beyond the namespace semaphore quota and increasing : memory pressure. The wrapped used_sems count can also cause SEM_INFO to : report a negative semaphore usage count.
:
: Use check_add_overflow() before comparing the new total against : sc_semmns, rejecting overflow with ENOSPC. The existing writer lock : covers both the check and the later accounting update.

lgtm

#4 lore raw
Quoted 38 lines · Davidlohr Bueso
Quoted 35 lines · Andrew Morton
Quoted 1 line · On Mon, 11 May 2026 18:42:53 +0800 Ren Wei <[email protected]> wrote:

Slight delay, sorry!

Quoted 5 lines

From: Ruide Cao <[email protected]>

newary() checks namespace-wide semaphore usage before creating a new array, but the current accounting uses a plain signed addition.

Quoted 7 lines

If the accumulated semaphore count overflows, the limit check can fail open and allow allocations past sc_semmns, breaking namespace semaphore resource enforcement and potentially leading to resource exhaustion.

Fix this by using check_add_overflow() before comparing the new total against sc_semmns, and reject overflow the same way as a true limit exceed.

LLM review tells me

: The fix looks correct. The existing writer lock covers the check and : the later used_sems increment, so that increment needs no further : change.

and

: Please qualify the changelog. Default SEMMNS and SEMMSL cannot trigger : this overflow. It requires raised limits and substantial existing : allocations: sem_alloc() separately bounds each array's size, so one : enormous request isn't sufficient to demonstrate a successful bypass. :
: With sufficient memory, semget() can then succeed instead of returning : ENOSPC, exceed the namespace quota and wrap the semaphore usage reported : by SEM_INFO negative. Additional allocations can increase memory : pressure.

Quoted 1 line
Quoted 1 line

So I propose this changelog. Please check:

This patch is from quite a while ago. Sorry, we forgot to follow up on it.

Should we send a v2 with the updated commit message? Or could you update the commit message when applying the patch?

Quoted 22 lines
Quoted 10 lines

: newary() checks the namespace-wide semaphore limit using a signed : addition of used_sems and nsems. If that addition overflows, the : comparison can fail to reject a request which exceeds sc_semmns. :
: The default semaphore limits cannot trigger this overflow. It requires : raised limits and substantial existing allocations. sem_alloc() : separately bounds each array's allocation size, so a single enormous : request is insufficient to demonstrate a successful quota bypass. :

Quoted 8 lines

: With enough memory, semget() can succeed instead of returning ENOSPC, : allowing allocations beyond the namespace semaphore quota and increasing : memory pressure. The wrapped used_sems count can also cause SEM_INFO to : report a negative semaphore usage count.
:
: Use check_add_overflow() before comparing the new total against : sc_semmns, rejecting overflow with ENOSPC. The existing writer lock : covers both the check and the later accounting update.

lgtm