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