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