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