packages feed

encapsule-0.6.1: src/Volume.hs

-- SPDX-License-Identifier: Apache-2.0

module Volume (
  addSelinuxLabel,
  disablesSelinux,
  expandVolumeHost,
  isVolumePathStart
  ) where

import Control.Monad (unless, when)
import Data.List (isPrefixOf)
import Data.List.Extra (splitOn)
import SimpleCmd (error', (+-+))
import System.Directory (canonicalizePath, doesPathExist)
import System.Posix.Files (fileOwner, getFileStatus, isSocket)
import System.Posix.User (getEffectiveUserID)

import Expand (expandPath, expandContainerPath)

-- Only suppress automatic relabeling; explicitly requested :z/:Z stays intact.
disablesSelinux :: [String] -> Bool
disablesSelinux [] = False
disablesSelinux ("--security-opt":"label=disable":_) = True
disablesSelinux ("--security-opt=label=disable":_) = True
disablesSelinux (_:rest) = disablesSelinux rest

-- Canonical paths prevent aliases such as /dev/ and /tmp/../dev bypassing this.
isSystemPath :: FilePath -> Bool
isSystemPath path =
  path `elem` ["/", "/home", "/tmp", "/mnt", "/media"] ||
  any (\base -> path == base || (base ++ "/") `isPrefixOf` path)
    ["/dev", "/proc", "/sys", "/etc", "/usr", "/run", "/var",
     "/boot", "/root", "/opt", "/srv"]

isVolumePathStart :: String -> Bool
isVolumePathStart ('/':_) = True
isVolumePathStart ('~':_) = True
isVolumePathStart ('$':_) = True
isVolumePathStart _       = False

-- SELinux labeling

-- Rootless podman cannot lsetxattr on files owned by another uid (e.g. /etc/*)
-- FIXME rather return Mount type or triple?
addSelinuxLabel :: Bool -> FilePath -> FilePath -> String -> IO String
addSelinuxLabel labelDisabled hostHome containerHome spec =
  case break (== ':') spec of
    (hostPart, []) -> do
      hostExp <- expandVolumeHost hostHome hostPart
      containerExp <- expandContainerPath containerHome hostPart
      skipLabel <- shouldSkipLabel hostExp
      return $ hostExp ++ ":" ++ containerExp ++ if skipLabel then "" else ":z"
    (hostPart, _:rest') -> do
      hostExp <- expandVolumeHost hostHome hostPart
      let (containerPart, optsPart)
            | isVolumePathStart rest' =
                case break (== ':') rest' of
                  (c, [])  -> (c, Nothing)
                  (c, _:o) -> (c, Just o)
            | otherwise = (hostPart, if null rest' then Nothing else Just rest')
      containerExp <- expandContainerPath containerHome containerPart
      skipLabel <- shouldSkipLabel hostExp
      let labeled = case optsPart of
            Nothing ->
              if skipLabel
              then hostExp ++ ":" ++ containerExp
              else hostExp ++ ":" ++ containerExp ++ ":z"
            Just o ->
              let flags = splitOn "," o
              in if skipLabel || "z" `elem` flags || "Z" `elem` flags
                    || "O" `elem` flags
                 then hostExp ++ ":" ++ containerExp ++ ":" ++ o
                 else hostExp ++ ":" ++ containerExp ++ ":" ++ o ++ ",z"
      return labeled
  where
    -- Skip auto :z for sockets, real $HOME (uses label=disable), and paths we
    -- cannot relabel (rootless lsetxattr fails on files owned by another user)
    shouldSkipLabel hostExp = do
      sockFile <- isSocketFile hostExp
      selfOwned <- ownedBySelf hostExp
      canonical <- canonicalizePath hostExp
      home <- canonicalizePath hostHome
      return $ labelDisabled || isSystemPath canonical || sockFile
               || canonical == home || not selfOwned

expandVolumeHost :: FilePath -> String -> IO FilePath
expandVolumeHost home source = do
  path <- expandPath home source
  when (null path) $ error' "volume host path must not be empty"
  exists <- doesPathExist path
  unless exists $
    error' $ "volume host path not found:" +-+ path
  return path

isSocketFile :: FilePath -> IO Bool
isSocketFile path = isSocket <$> getFileStatus path

ownedBySelf :: FilePath -> IO Bool
ownedBySelf path = do
  uid <- getEffectiveUserID
  st <- getFileStatus path
  return $ fileOwner st == uid