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