Skip to contentSkip to conversation

policy,tests: add tests for cgroup charge_to

PATCH New Albert Esteve 1 participant 1 message Nov 25 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 policy,tests: add tests for cgroup charge_to

Test the cgroup:charge_to permission added by the dma-buf memcg accounting series [1], for both charge_fd flavours (pidfd and cgroupfd), negative and positive tests, including a default-labeled (genfscon) and an xattr-relabeled cgroup.

The class is new, so the base policy has to be taught about it first:

  semodule -c -E base
  echo '(class cgroup (charge_to))' >> base.cil
  perl -0pi -e \
    's/\(classorder\s*\((.*?)\)\)/(classorder ($1 cgroup))/s' base.cil
  semodule -X 456 -i base.cil

AI agent helped with policy file and reviews.

[1] https://lore.kernel.org/all/20261124-v2_20230123_tjmercier_google_com-v6-0-dce12fa03ad6@redhat.com

Assisted-by Claude Code:deepseek-v4.1-flash
---
 defconfig                     |   6 +++
 policy/Makefile               |   6 +++
 policy/test_memcg_charge.te   |  50 ++++++++++++++++++++
 tests/Makefile                |   5 ++
 tests/memcg_charge/.gitignore |   1 +
 tests/memcg_charge/Makefile   |   5 ++
 tests/memcg_charge/charge.c   | 106 ++++++++++++++++++++++++++++++++++++++++++
 tests/memcg_charge/test       |  76 ++++++++++++++++++++++++++++++
 8 files changed, 255 insertions(+)
defconfig +6 -0
index 3dcf710..67f3515 100644
@@ -94,6 +94,12 @@ CONFIG_NET_KEY=m
 # This is not required for SELinux operation itself.
 CONFIG_TUN=m
 
+# Test the cgroup charge_to checks
+# They are not required for SELinux operation itself.
+CONFIG_MEMCG=y
+CONFIG_DMABUF_HEAPS=y
+CONFIG_DMABUF_HEAPS_SYSTEM=y
+
 # Test perf events.
 # This is not required for SELinux operation itself.
 CONFIG_HAVE_PERF_EVENTS=y
policy/Makefile +6 -0
index 012199a..262fb7c 100644
@@ -212,6 +212,12 @@ export M4PARAM += -Dio_uring_allowed_defined
 endif
 endif
 
+# memcg_charge test dependencies: charge_to on the cgroup class, in the
+# loaded policy (or the policy headers when building without selinuxfs).
+ifeq ($(shell if [ -e $(SELINUXFS)/policyvers ]; then [ -f $(SELINUXFS)/class/cgroup/perms/charge_to ]; else grep -q all_cgroup_perms.*charge_to $(POLDEV)/include/support/all_perms.spt; fi && grep -q dev_rw_dma_dev $(POLDEV)/include/kernel/devices.if && echo true),true)
+TARGETS += test_memcg_charge.te
+endif
+
 .PHONY: all build clean expand_check load unload
 
 all: build
policy/test_memcg_charge.te new file +50 -0

50 lines not on this page · raw message

tests/Makefile +5 -0
index 4c8e82b..e9da52b 100644
@@ -186,6 +186,11 @@ SUBDIRS += io_uring
 endif
 endif
 
+# memcg_charge test dependencies: charge_to on the cgroup class in the loaded policy
+ifeq ($(shell [ -f $(SELINUXFS)/class/cgroup/perms/charge_to ] && grep -qs charge_fd $(INCLUDEDIR)/linux/dma-heap.h && echo true),true)
+SUBDIRS += memcg_charge
+endif
+
 ifeq ($(DISTRO),RHEL4)
     SUBDIRS:=$(filter-out bounds dyntrace dyntrans inet_socket mmap nnp_nosuid overlay unix_socket, $(SUBDIRS))
 endif
tests/memcg_charge/.gitignore new file +1 -0
new file mode 100644
index 0000000..e78f0d8
@@ -0,0 +1 @@
+charge
tests/memcg_charge/Makefile new file +5 -0
new file mode 100644
index 0000000..0b48692
@@ -0,0 +1,5 @@
+TARGETS=charge
+
+all: $(TARGETS)
+clean:
+	rm -f $(TARGETS)
tests/memcg_charge/charge.c new file +106 -0

106 lines not on this page · raw message

tests/memcg_charge/test new file +76 -0

76 lines not on this page · raw message

---
base-commit: 459fc1c7f460a712d7aa4ee19ff31d4d3ae193c9
change-id: 20261125-cgroup-transfer-test-6b5683bdd064

Best regards,

Signature · 1 line
Albert Esteve <[email protected]>