482bfd
From b41c864d569357a102ee2335a4947e59e5e2b08a Mon Sep 17 00:00:00 2001
482bfd
From: Matthew Heon <matthew.heon@pm.me>
482bfd
Date: Thu, 27 Feb 2020 16:08:29 -0500
482bfd
Subject: [PATCH] Ensure that exec sessions inherit supplemental groups
482bfd
482bfd
This corrects a regression from Podman 1.4.x where container exec
482bfd
sessions inherited supplemental groups from the container, iff
482bfd
the exec session did not specify a user.
482bfd
482bfd
Signed-off-by: Matthew Heon <matthew.heon@pm.me>
482bfd
---
482bfd
 libpod/container_api.go            |  5 -----
482bfd
 libpod/container_internal_linux.go |  5 ++++-
482bfd
 libpod/oci_conmon_linux.go         | 25 +++++++++++++++++++++----
482bfd
 test/e2e/exec_test.go              | 24 ++++++++++++++++++++++++
482bfd
 4 files changed, 49 insertions(+), 10 deletions(-)
482bfd
482bfd
diff --git a/libpod/container_api.go b/libpod/container_api.go
482bfd
index d612341bce..dabbe27dcd 100644
482bfd
--- a/libpod/container_api.go
482bfd
+++ b/libpod/container_api.go
482bfd
@@ -270,11 +270,6 @@ func (c *Container) Exec(tty, privileged bool, env map[string]string, cmd []stri
482bfd
 		}
482bfd
 	}()
482bfd
 
482bfd
-	// if the user is empty, we should inherit the user that the container is currently running with
482bfd
-	if user == "" {
482bfd
-		user = c.config.User
482bfd
-	}
482bfd
-
482bfd
 	opts := new(ExecOptions)
482bfd
 	opts.Cmd = cmd
482bfd
 	opts.CapAdd = capList
482bfd
diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go
482bfd
index 7390262647..63968918cb 100644
482bfd
--- a/libpod/container_internal_linux.go
482bfd
+++ b/libpod/container_internal_linux.go
482bfd
@@ -330,7 +330,10 @@ func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
482bfd
 
482bfd
 	// Add addition groups if c.config.GroupAdd is not empty
482bfd
 	if len(c.config.Groups) > 0 {
482bfd
-		gids, _ := lookup.GetContainerGroups(c.config.Groups, c.state.Mountpoint, nil)
482bfd
+		gids, err := lookup.GetContainerGroups(c.config.Groups, c.state.Mountpoint, overrides)
482bfd
+		if err != nil {
482bfd
+			return nil, errors.Wrapf(err, "error looking up supplemental groups for container %s", c.ID())
482bfd
+		}
482bfd
 		for _, gid := range gids {
482bfd
 			g.AddProcessAdditionalGid(gid)
482bfd
 		}
482bfd
diff --git a/libpod/oci_conmon_linux.go b/libpod/oci_conmon_linux.go
482bfd
index 07d38693f0..800f896036 100644
482bfd
--- a/libpod/oci_conmon_linux.go
482bfd
+++ b/libpod/oci_conmon_linux.go
482bfd
@@ -1252,18 +1252,35 @@ func prepareProcessExec(c *Container, cmd, env []string, tty bool, cwd, user, se
482bfd
 
482bfd
 	}
482bfd
 
482bfd
+	var addGroups []string
482bfd
+	var sgids []uint32
482bfd
+
482bfd
+	// if the user is empty, we should inherit the user that the container is currently running with
482bfd
+	if user == "" {
482bfd
+		user = c.config.User
482bfd
+		addGroups = c.config.Groups
482bfd
+	}
482bfd
+
482bfd
 	overrides := c.getUserOverrides()
482bfd
 	execUser, err := lookup.GetUserGroupInfo(c.state.Mountpoint, user, overrides)
482bfd
 	if err != nil {
482bfd
 		return nil, err
482bfd
 	}
482bfd
 
482bfd
+	if len(addGroups) > 0 {
482bfd
+		sgids, err = lookup.GetContainerGroups(addGroups, c.state.Mountpoint, overrides)
482bfd
+		if err != nil {
482bfd
+			return nil, errors.Wrapf(err, "error looking up supplemental groups for container %s exec session %s", c.ID(), sessionID)
482bfd
+		}
482bfd
+	}
482bfd
+
482bfd
 	// If user was set, look it up in the container to get a UID to use on
482bfd
 	// the host
482bfd
-	if user != "" {
482bfd
-		sgids := make([]uint32, 0, len(execUser.Sgids))
482bfd
-		for _, sgid := range execUser.Sgids {
482bfd
-			sgids = append(sgids, uint32(sgid))
482bfd
+	if user != "" || len(sgids) > 0 {
482bfd
+		if user != "" {
482bfd
+			for _, sgid := range execUser.Sgids {
482bfd
+				sgids = append(sgids, uint32(sgid))
482bfd
+			}
482bfd
 		}
482bfd
 		processUser := spec.User{
482bfd
 			UID:            uint32(execUser.Uid),
482bfd
diff --git a/test/e2e/exec_test.go b/test/e2e/exec_test.go
482bfd
index ed4eb3335f..ab806f6831 100644
482bfd
--- a/test/e2e/exec_test.go
482bfd
+++ b/test/e2e/exec_test.go
482bfd
@@ -1,6 +1,7 @@
482bfd
 package integration
482bfd
 
482bfd
 import (
482bfd
+	"fmt"
482bfd
 	"os"
482bfd
 	"strings"
482bfd
 
482bfd
@@ -244,4 +245,27 @@ var _ = Describe("Podman exec", func() {
482bfd
 		Expect(session.ExitCode()).To(Equal(0))
482bfd
 	})
482bfd
 
482bfd
+	It("podman exec preserves --group-add groups", func() {
482bfd
+		groupName := "group1"
482bfd
+		gid := "4444"
482bfd
+		ctrName1 := "ctr1"
482bfd
+		ctr1 := podmanTest.Podman([]string{"run", "-ti", "--name", ctrName1, fedoraMinimal, "groupadd", "-g", gid, groupName})
482bfd
+		ctr1.WaitWithDefaultTimeout()
482bfd
+		Expect(ctr1.ExitCode()).To(Equal(0))
482bfd
+
482bfd
+		imgName := "img1"
482bfd
+		commit := podmanTest.Podman([]string{"commit", ctrName1, imgName})
482bfd
+		commit.WaitWithDefaultTimeout()
482bfd
+		Expect(commit.ExitCode()).To(Equal(0))
482bfd
+
482bfd
+		ctrName2 := "ctr2"
482bfd
+		ctr2 := podmanTest.Podman([]string{"run", "-d", "--name", ctrName2, "--group-add", groupName, imgName, "sleep", "300"})
482bfd
+		ctr2.WaitWithDefaultTimeout()
482bfd
+		Expect(ctr2.ExitCode()).To(Equal(0))
482bfd
+
482bfd
+		exec := podmanTest.Podman([]string{"exec", "-ti", ctrName2, "id"})
482bfd
+		exec.WaitWithDefaultTimeout()
482bfd
+		Expect(exec.ExitCode()).To(Equal(0))
482bfd
+		Expect(strings.Contains(exec.OutputToString(), fmt.Sprintf("%s(%s)", gid, groupName))).To(BeTrue())
482bfd
+	})
482bfd
 })