Skip to contentSkip to conversation

ipc/sem: account enlarged sops arrays to memcg

PATCH Superseded Wang Yingjie 3 participants 4 messages Oct 16 – Oct 18

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipc/sem: account enlarged sops arrays to memcg

Commit 50ec499b9a43 ("sysctl: allow change system v ipc sysctls inside ipc namespace") lets an IPC namespace owner change kernel.sem. The SEMOPM field defaults to 500 but has no semantic range check.

do_semtimedop() allocates nsops * sizeof(struct sembuf) with GFP_KERNEL. A blocking operation retains that buffer, so a user can raise SEMOPM and hold large attacker-selected amounts of host kernel memory without charging the calling task memcg.

On the current mainline base, one 10,000,000-operation call retained 60,936,448 bytes while a 32 MiB cgroup peaked at 909,312 bytes and reported no limit event. Sixteen 1,000,000-operation calls retained 100,860,416 bytes while the cgroup peaked at 1,310,720 bytes.

Commit 6a4746ba0619 ("ipc: remove memcg accounting for sops objects in do_semtimedop()") removed accounting on the premise that the buffer was at most two pages. Preserve that default-sized path, but account arrays larger than the default SEMOPM value.

With this change, both test configurations reach the 32 MiB cgroup limit and the caller is terminated by the memcg OOM killer before it can retain the enlarged arrays.

The sysctl also accepts negative SEMOPM values. Since nsops is unsigned, SEMOPM=-1 bypasses the intended operation-count check. This change still accounts any admitted array larger than the default, while leaving SEMOPM range policy for separate review.

Fixes 50ec499b9a43 ("sysctl: allow change system v ipc sysctls inside ipc namespace")
---
 ipc/sem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ipc/sem.c +3 -1
index 5ec41de7e85b2b..8c9edecc160763 100644
@@ -2234,7 +2234,9 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 		return -EINVAL;
 
 	if (nsops > SEMOPM_FAST) {
-		sops = kvmalloc_objs(*sops, nsops);
+		gfp_t gfp = nsops > SEMOPM ? GFP_KERNEL_ACCOUNT : GFP_KERNEL;
+
+		sops = kvmalloc_objs(*sops, nsops, gfp);
 		if (sops == NULL)
 			return -ENOMEM;
 	}

base-commit: 2f2f7eeef8b0a1a9b9ca998231419873ccae8d4e

Signature · 1 line
2.43.0
maintainer
#1 lore raw

(Alexey, Eric, others: please weigh in)

Quoted 14 lines · On Fri, 16 Oct 2026 15:26:11 +0000 Wang Yingjie <[email protected]> wrote:

Commit 50ec499b9a43 ("sysctl: allow change system v ipc sysctls inside ipc namespace") lets an IPC namespace owner change kernel.sem. The SEMOPM field defaults to 500 but has no semantic range check.

do_semtimedop() allocates nsops * sizeof(struct sembuf) with GFP_KERNEL. A blocking operation retains that buffer, so a user can raise SEMOPM and hold large attacker-selected amounts of host kernel memory without charging the calling task memcg.

On the current mainline base, one 10,000,000-operation call retained 60,936,448 bytes while a 32 MiB cgroup peaked at 909,312 bytes and reported no limit event. Sixteen 1,000,000-operation calls retained 100,860,416 bytes while the cgroup peaked at 1,310,720 bytes.

Quoted 4 lines

Commit 6a4746ba0619 ("ipc: remove memcg accounting for sops objects in do_semtimedop()") removed accounting on the premise that the buffer was at most two pages. Preserve that default-sized path, but account arrays larger than the default SEMOPM value.

OK, I think. But...

Quoted 3 lines

With this change, both test configurations reach the 32 MiB cgroup limit and the caller is terminated by the memcg OOM killer before it can retain the enlarged arrays.

Only if memcg is enabled and the task's memory cgroup has an effective limit. Otherwise we can end up handing out idiotic amounts of kernel memory. Memcg accounting isn't a substitute for limiting the allocation size.

Quoted 2 lines

The sysctl also accepts negative SEMOPM values. Since nsops is unsigned, SEMOPM=-1 bypasses the intended operation-count check.

Let's fix this first. nsops is unsigned int, whereas ns->sc_semopm is int. In the comparison nsops > ns->sc_semopm, a negative SEMOPM is converted to unsigned, so SEMOPM=-1 effectively sets the limit to UINT_MAX.

I'm thinking:

- Permit init_ipc_ns.sc_semopm to be set to any value from 1 to INT_MAX. This preserves the existing ability of host root to configure larger limits.

- For all other IPC namespaces, permit values from 1 to 1000. The documentation has long recommended 1000 as an upper limit, although it hasn't been enforced. This would be a new restriction, so the changelog should explain why we're introducing it.

- Validate the new values before installing any of them. The current proc_ipc_sem_dointvec() updates the live array directly, and rejecting an invalid value afterward would leave a window in which other tasks could observe it.

- Update the comment in include/uapi/linux/sem.h. The warning about physical memory fragmentation is outdated now that the allocation uses kvmalloc(). Document the new 1000 limit for non-initial IPC namespaces instead.

Ideally, a new IPC namespace would inherit its parent's configured limit, but currently every new namespace starts at SEMOPM=500. I don't think we need to change that as part of this fix.

Then please redo your memcg-accounting patch as a separate followup, with suitably adjusted changelogging. That would provide additional protection for legitimately enlarged allocations in the initial IPC namespace and for allocations between 501 and 1000 operations elsewhere.

Thanks.

#2 lore raw

Hi,

Quoted 17 lines · Andrew Morton

(Alexey, Eric, others: please weigh in)

Quoted 13 lines · On Fri, 16 Oct 2026 15:26:11 +0000 Wang Yingjie <[email protected]> wrote:

Commit 50ec499b9a43 ("sysctl: allow change system v ipc sysctls inside ipc namespace") lets an IPC namespace owner change kernel.sem. The SEMOPM field defaults to 500 but has no semantic range check.

do_semtimedop() allocates nsops * sizeof(struct sembuf) with GFP_KERNEL. A blocking operation retains that buffer, so a user can raise SEMOPM and hold large attacker-selected amounts of host kernel memory without charging the calling task memcg.

On the current mainline base, one 10,000,000-operation call retained 60,936,448 bytes while a 32 MiB cgroup peaked at 909,312 bytes and reported no limit event. Sixteen 1,000,000-operation calls retained 100,860,416 bytes while the cgroup peaked at 1,310,720 bytes.

Just for understanding:

Each sleeping thread (within do_semtimedop()) can use 60 MB, assuming it triggers a 10 mio semop() call.

It could be a bogus 5 mio times "increase by 1, decrease by 1" semop(), or a valid semop() on a suitably large array.

Quoted 6 lines
Quoted 4 lines

Commit 6a4746ba0619 ("ipc: remove memcg accounting for sops objects in do_semtimedop()") removed accounting on the premise that the buffer was at most two pages. Preserve that default-sized path, but account arrays larger than the default SEMOPM value.

OK, I think. But...

Quoted 7 lines
Quoted 3 lines

With this change, both test configurations reach the 32 MiB cgroup limit and the caller is terminated by the memcg OOM killer before it can retain the enlarged arrays.

Only if memcg is enabled and the task's memory cgroup has an effective limit. Otherwise we can end up handing out idiotic amounts of kernel memory. Memcg accounting isn't a substitute for limiting the allocation size.

The previous assumption was the other way around:

ipc is full of legacy users.

Thus any hard limit risks to break existing setups.

If I remember it right, either SHM or MSG had code that tried to split the permitted allocation, i.e. if someone creates a new namespace, then each name space inherits half of the limit.

This was removed, since memcg would be the better substitute.

Old discussion: https://marc.info/?l=linux-mm&m=139639755705736&w=2

But I can't find right now the code that tried to split the limits.

Quoted 9 lines
Quoted 2 lines

The sysctl also accepts negative SEMOPM values. Since nsops is unsigned, SEMOPM=-1 bypasses the intended operation-count check.

Let's fix this first. nsops is unsigned int, whereas ns->sc_semopm is int. In the comparison nsops > ns->sc_semopm, a negative SEMOPM is converted to unsigned, so SEMOPM=-1 effectively sets the limit to UINT_MAX.

I'm thinking:

Quoted 7 lines

- Permit init_ipc_ns.sc_semopm to be set to any value from 1 to INT_MAX. This preserves the existing ability of host root to configure larger limits.
- For all other IPC namespaces, permit values from 1 to 1000. The documentation has long recommended 1000 as an upper limit, although it hasn't been enforced. This would be a new restriction, so the changelog should explain why we're introducing it.

This would mean workloads with higher SEMOPM values cannot run in containers, correct?

According to commit e4243b8062c1 "ipc/sem: play nicer with large nsops allocations", values up to 1000 are recommended.

And https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Tuning_and_Optimizing_Red_Hat_Enterprise_Linux_for_Oracle_9i_and_10g_Databases/sect-Oracle_9i_and_10g_Tuning_Guide-Setting_Semaphores-An_Example_of_Semaphore_Settings.html recommends to set it to SEMMSL, i.e. 5000 in the given example.

Quoted 20 lines

- Validate the new values before installing any of them. The current proc_ipc_sem_dointvec() updates the live array directly, and rejecting an invalid value afterward would leave a window in which other tasks could observe it.

- Update the comment in include/uapi/linux/sem.h. The warning about physical memory fragmentation is outdated now that the allocation uses kvmalloc(). Document the new 1000 limit for non-initial IPC namespaces instead.

Ideally, a new IPC namespace would inherit its parent's configured limit, but currently every new namespace starts at SEMOPM=500. I don't think we need to change that as part of this fix.

Then please redo your memcg-accounting patch as a separate followup, with suitably adjusted changelogging. That would provide additional protection for legitimately enlarged allocations in the initial IPC namespace and for allocations between 501 and 1000 operations elsewhere.

Thanks.

#3 lore raw

Hi Manfred,

Quoted 2 lines · Manfred Spraul

This would mean workloads with higher SEMOPM values cannot run in containers, correct?

Yes. A fixed limit of 1000 for non-initial IPC namespaces would prevent a workload requiring a larger SEMOPM from running there. Commit e4243b8062c1 ("ipc/sem: play nicer with large nsops allocations") deliberately made larger nsops allocations work, so a new ceiling could be a compatibility regression.

I will not include the fixed 1000 ceiling in v2 without agreement. The parts that appear independent are:

- reject non-positive SEMOPM values while preserving the existing positive range;
- validate the complete kernel.sem vector before installing any value;
- keep memcg accounting for arrays above the default SEMOPM as a separate patch.

This leaves the case where memcg is disabled or unlimited without a hard global ceiling, as Andrew noted. I do not see a compatibility-safe fixed ceiling that can be selected from the existing ABI alone.

Andrew, Manfred, would you prefer a v2 with the validation fix and the separate memcg follow-up, but no new fixed namespace ceiling, or should I hold the reroll until the namespace limit policy is settled?

Thanks,
Yingjie