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