f432f6
From 9934b17365083ce966b44c5ce3c7e052f516e255 Mon Sep 17 00:00:00 2001
f432f6
From: Aditya R <arajan@redhat.com>
f432f6
Date: Wed, 24 Aug 2022 08:42:23 +0530
f432f6
Subject: [PATCH] run: add container gid to additional groups
f432f6
f432f6
When container is created with specific uid and gid also add container
f432f6
gid to supplementary/additional group.
f432f6
f432f6
Signed-off-by: Aditya R <arajan@redhat.com>
f432f6
---
f432f6
 run_common.go                            |  1 +
f432f6
 tests/bud.bats                           | 16 ++++++++++++++++
f432f6
 tests/bud/supplemental-groups/Dockerfile |  3 +++
f432f6
 tests/run.bats                           | 14 ++++++++++++++
f432f6
 4 files changed, 34 insertions(+)
f432f6
 create mode 100644 tests/bud/supplemental-groups/Dockerfile
f432f6
f432f6
diff --git a/run_common.go b/run_common.go
f432f6
index 2054c56527..f5a5ec8505 100644
f432f6
--- a/run_common.go
f432f6
+++ b/run_common.go
f432f6
@@ -262,6 +262,7 @@ func (b *Builder) configureUIDGID(g *generate.Generator, mountPoint string, opti
f432f6
 	}
f432f6
 	g.SetProcessUID(user.UID)
f432f6
 	g.SetProcessGID(user.GID)
f432f6
+	g.AddProcessAdditionalGid(user.GID)
f432f6
 	for _, gid := range user.AdditionalGids {
f432f6
 		g.AddProcessAdditionalGid(gid)
f432f6
 	}
f432f6
diff --git a/tests/bud.bats b/tests/bud.bats
f432f6
index a37e418b41..07bdf45cac 100644
f432f6
--- a/tests/bud.bats
f432f6
+++ b/tests/bud.bats
f432f6
@@ -366,6 +366,22 @@ _EOF
f432f6
   expect_output --substring "invalid response status"
f432f6
 }
f432f6
 
f432f6
+@test "build test has gid in supplemental groups" {
f432f6
+  _prefetch alpine
f432f6
+  run_buildah build $WITH_POLICY_JSON -t source -f $BUDFILES/supplemental-groups/Dockerfile
f432f6
+  # gid 1000 must be in supplemental groups
f432f6
+  expect_output --substring "Groups:	1000"
f432f6
+}
f432f6
+
f432f6
+@test "build test if supplemental groups has gid with --isolation chroot" {
f432f6
+  test -z "${BUILDAH_ISOLATION}" || skip "BUILDAH_ISOLATION=${BUILDAH_ISOLATION} overrides --isolation"
f432f6
+
f432f6
+  _prefetch alpine
f432f6
+  run_buildah build --isolation chroot $WITH_POLICY_JSON -t source -f $BUDFILES/supplemental-groups/Dockerfile
f432f6
+  # gid 1000 must be in supplemental groups
f432f6
+  expect_output --substring "Groups:	1000"
f432f6
+}
f432f6
+
f432f6
 # Test skipping images with FROM
f432f6
 @test "build-test skipping unwanted stages with FROM" {
f432f6
   mkdir -p ${TEST_SCRATCH_DIR}/bud/platform
f432f6
diff --git a/tests/bud/supplemental-groups/Dockerfile b/tests/bud/supplemental-groups/Dockerfile
f432f6
new file mode 100644
f432f6
index 0000000000..462d9ea7a4
f432f6
--- /dev/null
f432f6
+++ b/tests/bud/supplemental-groups/Dockerfile
f432f6
@@ -0,0 +1,3 @@
f432f6
+FROM alpine
f432f6
+USER 1000:1000
f432f6
+RUN cat /proc/$$/status
f432f6
diff --git a/tests/run.bats b/tests/run.bats
f432f6
index 2e8836c470..e34091fcbe 100644
f432f6
--- a/tests/run.bats
f432f6
+++ b/tests/run.bats
f432f6
@@ -349,6 +349,20 @@ function configure_and_check_user() {
f432f6
   expect_output "888:888"
f432f6
 }
f432f6
 
f432f6
+@test "run --user and verify gid in supplemental groups" {
f432f6
+  skip_if_no_runtime
f432f6
+
f432f6
+  # Create the container.
f432f6
+  _prefetch alpine
f432f6
+  run_buildah from $WITH_POLICY_JSON alpine
f432f6
+  ctr="$output"
f432f6
+
f432f6
+  # Run with uid:gid 1000:1000 and verify if gid is present in additional groups
f432f6
+  run_buildah run --user 1000:1000 "$ctr" cat /proc/self/status
f432f6
+  # gid 1000 must be in additional/supplemental groups
f432f6
+  expect_output --substring "Groups:	1000 "
f432f6
+}
f432f6
+
f432f6
 @test "run --workingdir" {
f432f6
 	skip_if_no_runtime
f432f6