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