593589
From fb7d2b6bd6a16ffdbe4a69428e3ba5b487719e78 Mon Sep 17 00:00:00 2001
593589
From: Daniel J Walsh <dwalsh@redhat.com>
593589
Date: Tue, 17 Dec 2019 15:24:29 -0500
593589
Subject: [PATCH] Add support for FIPS-Mode backends
593589
593589
If host is running in fips mode, then RHEL8.2 and beyond container images
593589
will come with a directory /usr/share/crypto-policies/back-ends/FIPS.
593589
This directory needs to be bind mounted over /etc/crypto-policies/back-ends in
593589
order to make all tools in the container follow the FIPS Mode rules.
593589
593589
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
593589
---
593589
 pkg/secrets/secrets.go | 48 +++++++++++++++++++++++++++++++++---------
593589
 run_linux.go           |  2 +-
593589
 2 files changed, 39 insertions(+), 11 deletions(-)
593589
593589
diff -up ./libpod-5cc92849f7fc9dd734ca2fd8f3ae8830b9a7eb26/vendor/github.com/containers/buildah/pkg/secrets/secrets.go.1784950 ./libpod-5cc92849f7fc9dd734ca2fd8f3ae8830b9a7eb26/vendor/github.com/containers/buildah/pkg/secrets/secrets.go
593589
--- libpod-5cc92849f7fc9dd734ca2fd8f3ae8830b9a7eb26/vendor/github.com/containers/buildah/pkg/secrets/secrets.go.1784950	2020-02-19 14:58:22.049213896 +0100
593589
+++ libpod-5cc92849f7fc9dd734ca2fd8f3ae8830b9a7eb26/vendor/github.com/containers/buildah/pkg/secrets/secrets.go	2020-02-19 14:58:22.052213937 +0100
593589
@@ -148,12 +148,21 @@ func getMountsMap(path string) (string,
593589
 }
593589
 
593589
 // SecretMounts copies, adds, and mounts the secrets to the container root filesystem
593589
+// Deprecated, Please use SecretMountWithUIDGID
593589
 func SecretMounts(mountLabel, containerWorkingDir, mountFile string, rootless, disableFips bool) []rspec.Mount {
593589
 	return SecretMountsWithUIDGID(mountLabel, containerWorkingDir, mountFile, containerWorkingDir, 0, 0, rootless, disableFips)
593589
 }
593589
 
593589
-// SecretMountsWithUIDGID specifies the uid/gid of the owner
593589
-func SecretMountsWithUIDGID(mountLabel, containerWorkingDir, mountFile, mountPrefix string, uid, gid int, rootless, disableFips bool) []rspec.Mount {
593589
+// SecretMountsWithUIDGID copies, adds, and mounts the secrets to the container root filesystem
593589
+// mountLabel: MAC/SELinux label for container content
593589
+// containerWorkingDir: Private data for storing secrets on the host mounted in container.
593589
+// mountFile: Additional mount points required for the container.
593589
+// mountPoint: Container image mountpoint
593589
+// uid: to assign to content created for secrets
593589
+// gid: to assign to content created for secrets
593589
+// rootless: indicates whether container is running in rootless mode
593589
+// disableFips: indicates whether system should ignore fips mode
593589
+func SecretMountsWithUIDGID(mountLabel, containerWorkingDir, mountFile, mountPoint string, uid, gid int, rootless, disableFips bool) []rspec.Mount {
593589
 	var (
593589
 		secretMounts []rspec.Mount
593589
 		mountFiles   []string
593589
@@ -171,7 +180,7 @@ func SecretMountsWithUIDGID(mountLabel,
593589
 	}
593589
 	for _, file := range mountFiles {
593589
 		if _, err := os.Stat(file); err == nil {
593589
-			mounts, err := addSecretsFromMountsFile(file, mountLabel, containerWorkingDir, mountPrefix, uid, gid)
593589
+			mounts, err := addSecretsFromMountsFile(file, mountLabel, containerWorkingDir, uid, gid)
593589
 			if err != nil {
593589
 				logrus.Warnf("error mounting secrets, skipping entry in %s: %v", file, err)
593589
 			}
593589
@@ -187,7 +196,7 @@ func SecretMountsWithUIDGID(mountLabel,
593589
 	// Add FIPS mode secret if /etc/system-fips exists on the host
593589
 	_, err := os.Stat("/etc/system-fips")
593589
 	if err == nil {
593589
-		if err := addFIPSModeSecret(&secretMounts, containerWorkingDir, mountPrefix, mountLabel, uid, gid); err != nil {
593589
+		if err := addFIPSModeSecret(&secretMounts, containerWorkingDir, mountPoint, mountLabel, uid, gid); err != nil {
593589
 			logrus.Errorf("error adding FIPS mode secret to container: %v", err)
593589
 		}
593589
 	} else if os.IsNotExist(err) {
593589
@@ -206,7 +215,7 @@ func rchown(chowndir string, uid, gid in
593589
 
593589
 // addSecretsFromMountsFile copies the contents of host directory to container directory
593589
 // and returns a list of mounts
593589
-func addSecretsFromMountsFile(filePath, mountLabel, containerWorkingDir, mountPrefix string, uid, gid int) ([]rspec.Mount, error) {
593589
+func addSecretsFromMountsFile(filePath, mountLabel, containerWorkingDir string, uid, gid int) ([]rspec.Mount, error) {
593589
 	var mounts []rspec.Mount
593589
 	defaultMountsPaths := getMounts(filePath)
593589
 	for _, path := range defaultMountsPaths {
593589
@@ -285,7 +294,7 @@ func addSecretsFromMountsFile(filePath,
593589
 		}
593589
 
593589
 		m := rspec.Mount{
593589
-			Source:      filepath.Join(mountPrefix, ctrDirOrFile),
593589
+			Source:      ctrDirOrFileOnHost,
593589
 			Destination: ctrDirOrFile,
593589
 			Type:        "bind",
593589
 			Options:     []string{"bind", "rprivate"},
593589
@@ -300,15 +309,15 @@ func addSecretsFromMountsFile(filePath,
593589
 // root filesystem if /etc/system-fips exists on hosts.
593589
 // This enables the container to be FIPS compliant and run openssl in
593589
 // FIPS mode as the host is also in FIPS mode.
593589
-func addFIPSModeSecret(mounts *[]rspec.Mount, containerWorkingDir, mountPrefix, mountLabel string, uid, gid int) error {
593589
+func addFIPSModeSecret(mounts *[]rspec.Mount, containerWorkingDir, mountPoint, mountLabel string, uid, gid int) error {
593589
 	secretsDir := "/run/secrets"
593589
 	ctrDirOnHost := filepath.Join(containerWorkingDir, secretsDir)
593589
 	if _, err := os.Stat(ctrDirOnHost); os.IsNotExist(err) {
593589
 		if err = idtools.MkdirAllAs(ctrDirOnHost, 0755, uid, gid); err != nil {
593589
-			return errors.Wrapf(err, "making container directory on host failed")
593589
+			return errors.Wrapf(err, "making container directory %q on host failed", ctrDirOnHost)
593589
 		}
593589
 		if err = label.Relabel(ctrDirOnHost, mountLabel, false); err != nil {
593589
-			return errors.Wrap(err, "error applying correct labels")
593589
+			return errors.Wrapf(err, "error applying correct labels on %q", ctrDirOnHost)
593589
 		}
593589
 	}
593589
 	fipsFile := filepath.Join(ctrDirOnHost, "system-fips")
593589
@@ -323,7 +332,7 @@ func addFIPSModeSecret(mounts *[]rspec.M
593589
 
593589
 	if !mountExists(*mounts, secretsDir) {
593589
 		m := rspec.Mount{
593589
-			Source:      filepath.Join(mountPrefix, secretsDir),
593589
+			Source:      ctrDirOnHost,
593589
 			Destination: secretsDir,
593589
 			Type:        "bind",
593589
 			Options:     []string{"bind", "rprivate"},
593589
@@ -331,6 +340,25 @@ func addFIPSModeSecret(mounts *[]rspec.M
593589
 		*mounts = append(*mounts, m)
593589
 	}
593589
 
593589
+	srcBackendDir := "/usr/share/crypto-policies/back-ends/FIPS"
593589
+	destDir := "/etc/crypto-policies/back-ends"
593589
+	srcOnHost := filepath.Join(mountPoint, srcBackendDir)
593589
+	if _, err := os.Stat(srcOnHost); err != nil {
593589
+		if os.IsNotExist(err) {
593589
+			return nil
593589
+		}
593589
+		return errors.Wrapf(err, "failed to stat FIPS Backend directory %q", ctrDirOnHost)
593589
+	}
593589
+
593589
+	if !mountExists(*mounts, destDir) {
593589
+		m := rspec.Mount{
593589
+			Source:      srcOnHost,
593589
+			Destination: destDir,
593589
+			Type:        "bind",
593589
+			Options:     []string{"bind", "rprivate"},
593589
+		}
593589
+		*mounts = append(*mounts, m)
593589
+	}
593589
 	return nil
593589
 }
593589
 
593589
diff -up ./libpod-5cc92849f7fc9dd734ca2fd8f3ae8830b9a7eb26/vendor/github.com/containers/buildah/run_linux.go.1784950 ./libpod-5cc92849f7fc9dd734ca2fd8f3ae8830b9a7eb26/vendor/github.com/containers/buildah/run_linux.go
593589
--- libpod-5cc92849f7fc9dd734ca2fd8f3ae8830b9a7eb26/vendor/github.com/containers/buildah/run_linux.go.1784950	2020-02-19 14:58:22.021213507 +0100
593589
+++ libpod-5cc92849f7fc9dd734ca2fd8f3ae8830b9a7eb26/vendor/github.com/containers/buildah/run_linux.go	2020-02-19 14:58:22.024213549 +0100
593589
@@ -460,7 +460,7 @@ func (b *Builder) setupMounts(mountPoint
593589
 	}
593589
 
593589
 	// Get the list of secrets mounts.
593589
-	secretMounts := secrets.SecretMountsWithUIDGID(b.MountLabel, cdir, b.DefaultMountsFilePath, cdir, int(rootUID), int(rootGID), unshare.IsRootless(), false)
593589
+	secretMounts := secrets.SecretMountsWithUIDGID(b.MountLabel, cdir, b.DefaultMountsFilePath, mountPoint, int(rootUID), int(rootGID), unshare.IsRootless(), false)
593589
 
593589
 	// Add temporary copies of the contents of volume locations at the
593589
 	// volume locations, unless we already have something there.