diff --git a/atomfs.go b/atomfs.go new file mode 100644 index 0000000..2769f4a --- /dev/null +++ b/atomfs.go @@ -0,0 +1 @@ +package atomfs diff --git a/cmd/atomfs/mount.go b/cmd/atomfs/mount.go index 4eec79d..ec97a10 100644 --- a/cmd/atomfs/mount.go +++ b/cmd/atomfs/mount.go @@ -9,9 +9,8 @@ import ( "github.com/pkg/errors" "github.com/urfave/cli" - - "machinerun.io/atomfs" - "machinerun.io/atomfs/squashfs" + "machinerun.io/atomfs/pkg/common" + "machinerun.io/atomfs/pkg/molecule" ) var mountCmd = cli.Command{ @@ -51,7 +50,7 @@ func findImage(ctx *cli.Context) (string, string, error) { } ocidir := r[0] tag := r[1] - if !atomfs.PathExists(ocidir) { + if !common.PathExists(ocidir) { return "", "", fmt.Errorf("oci directory %s does not exist: %w", ocidir, mountUsage(ctx.App.Name)) } return ocidir, tag, nil @@ -94,7 +93,7 @@ func doMount(ctx *cli.Context) error { return fmt.Errorf("--persist requires an argument") } } - opts := atomfs.MountOCIOpts{ + opts := molecule.MountOCIOpts{ OCIDir: absOCIDir, Tag: tag, Target: absTarget, @@ -104,7 +103,7 @@ func doMount(ctx *cli.Context) error { MetadataDir: ctx.String("metadir"), // nil here means /run/atomfs } - mol, err := atomfs.BuildMoleculeFromOCI(opts) + mol, err := molecule.BuildMoleculeFromOCI(opts) if err != nil { return errors.Wrapf(err, "couldn't build molecule with opts %+v", opts) } @@ -132,7 +131,7 @@ func amPrivileged() bool { func squashUmount(p string) error { if amPrivileged() { - return squashfs.Umount(p) + return common.Umount(p) } return RunCommand("fusermount", "-u", p) } diff --git a/cmd/atomfs/umount.go b/cmd/atomfs/umount.go index fde7340..ffe9675 100644 --- a/cmd/atomfs/umount.go +++ b/cmd/atomfs/umount.go @@ -7,8 +7,8 @@ import ( "syscall" "github.com/urfave/cli" - "machinerun.io/atomfs" - "machinerun.io/atomfs/mount" + "machinerun.io/atomfs/pkg/common" + "machinerun.io/atomfs/pkg/mount" ) var umountCmd = cli.Command{ @@ -62,11 +62,11 @@ func doUmount(ctx *cli.Context) error { // $metadir/meta/config.json // TODO: want to know mountnsname for a target mountpoint... not for our current proc??? - mountNSName, err := atomfs.GetMountNSName() + mountNSName, err := common.GetMountNSName() if err != nil { errs = append(errs, fmt.Errorf("Failed to get mount namespace name")) } - metadir := filepath.Join(atomfs.RuntimeDir(ctx.String("metadir")), "meta", mountNSName, atomfs.ReplacePathSeparators(mountpoint)) + metadir := filepath.Join(common.RuntimeDir(ctx.String("metadir")), "meta", mountNSName, common.ReplacePathSeparators(mountpoint)) mountsdir := filepath.Join(metadir, "mounts") mounts, err := os.ReadDir(mountsdir) diff --git a/cmd/atomfs/verify.go b/cmd/atomfs/verify.go index c587405..d12b6e5 100644 --- a/cmd/atomfs/verify.go +++ b/cmd/atomfs/verify.go @@ -6,10 +6,10 @@ import ( "strings" "github.com/urfave/cli" - "machinerun.io/atomfs" - "machinerun.io/atomfs/log" - "machinerun.io/atomfs/mount" - "machinerun.io/atomfs/squashfs" + "machinerun.io/atomfs/pkg/common" + "machinerun.io/atomfs/pkg/log" + "machinerun.io/atomfs/pkg/mount" + "machinerun.io/atomfs/pkg/verity" ) var verifyCmd = cli.Command{ @@ -49,12 +49,12 @@ func doVerify(ctx *cli.Context) error { return fmt.Errorf("%s is not a mountpoint", mountpoint) } - mountNSName, err := atomfs.GetMountNSName() + mountNSName, err := common.GetMountNSName() if err != nil { return err } - metadir := filepath.Join(atomfs.RuntimeDir(ctx.String("metadir")), "meta", mountNSName, atomfs.ReplacePathSeparators(mountpoint)) + metadir := filepath.Join(common.RuntimeDir(ctx.String("metadir")), "meta", mountNSName, common.ReplacePathSeparators(mountpoint)) mountsdir := filepath.Join(metadir, "mounts") mounts, err := mount.ParseMounts("/proc/self/mountinfo") @@ -83,7 +83,7 @@ func doVerify(ctx *cli.Context) error { continue } checkedCount = checkedCount + 1 - err = squashfs.ConfirmExistingVerityDeviceCurrentValidity(m.Source) + err = verity.ConfirmExistingVerityDeviceCurrentValidity(m.Source) if err != nil { fmt.Printf("%s: CORRUPTION FOUND\n", m.Source) allOK = false diff --git a/pkg/common/common_test.go b/pkg/common/common_test.go new file mode 100644 index 0000000..2d99a2f --- /dev/null +++ b/pkg/common/common_test.go @@ -0,0 +1,49 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type uidmapTestcase struct { + uidmap string + expected bool +} + +var uidmapTests = []uidmapTestcase{ + { + uidmap: ` 0 0 4294967295`, + expected: true, + }, + { + uidmap: ` 0 0 1000 +2000 2000 1`, + expected: false, + }, + { + uidmap: ` 0 0 1000`, + expected: false, + }, + { + uidmap: ` 10 0 4294967295`, + expected: false, + }, + { + uidmap: ` 0 10 4294967295`, + expected: false, + }, + { + uidmap: ` 0 0 1`, + expected: false, + }, +} + +func TestAmHostRoot(t *testing.T) { + t.Parallel() + assert := assert.New(t) + for _, testcase := range uidmapTests { + v := uidmapIsHost(testcase.uidmap) + assert.Equal(v, testcase.expected) + } +} diff --git a/pkg/common/exclude.go b/pkg/common/exclude.go new file mode 100644 index 0000000..e93a9d2 --- /dev/null +++ b/pkg/common/exclude.go @@ -0,0 +1,84 @@ +package common + +import ( + "bytes" + "path" + "path/filepath" + "strings" +) + +// ExcludePaths represents a list of paths to exclude in a squashfs listing. +// Users should do something like filepath.Walk() over the whole filesystem, +// calling AddExclude() or AddInclude() based on whether they want to include +// or exclude a particular file. Note that if e.g. /usr is excluded, then +// everyting underneath is also implicitly excluded. The +// AddExclude()/AddInclude() methods do the math to figure out what is the +// correct set of things to exclude or include based on what paths have been +// previously included or excluded. +type ExcludePaths struct { + exclude map[string]bool + include []string +} + +func NewExcludePaths() *ExcludePaths { + return &ExcludePaths{ + exclude: map[string]bool{}, + include: []string{}, + } +} + +func (eps *ExcludePaths) AddExclude(p string) { + for _, inc := range eps.include { + // If /usr/bin/ls has changed but /usr hasn't, we don't want to list + // /usr in the include paths any more, so let's be sure to only + // add things which aren't prefixes. + if strings.HasPrefix(inc, p) { + return + } + } + eps.exclude[p] = true +} + +func (eps *ExcludePaths) AddInclude(orig string, isDir bool) { + // First, remove this thing and all its parents from exclude. + p := orig + + // normalize to the first dir + if !isDir { + p = path.Dir(p) + } + for { + // our paths are all absolute, so this is a base case + if p == "/" { + break + } + + delete(eps.exclude, p) + p = filepath.Dir(p) + } + + // now add it to the list of includes, so we don't accidentally re-add + // anything above. + eps.include = append(eps.include, orig) +} + +func (eps *ExcludePaths) String() (string, error) { + var buf bytes.Buffer + for p := range eps.exclude { + _, err := buf.WriteString(p) + if err != nil { + return "", err + } + _, err = buf.WriteString("\n") + if err != nil { + return "", err + } + } + + _, err := buf.WriteString("\n") + if err != nil { + return "", err + } + + return buf.String(), nil +} diff --git a/pkg/common/fs.go b/pkg/common/fs.go new file mode 100644 index 0000000..0d98b5f --- /dev/null +++ b/pkg/common/fs.go @@ -0,0 +1,47 @@ +package common + +import ( + "os" + "strings" +) + +func FileChanged(a os.FileInfo, path string) bool { + b, err := os.Lstat(path) + if err != nil { + return true + } + return !os.SameFile(a, b) +} + +// Takes /proc/self/uid_map contents as one string +// Returns true if this is a uidmap representing the whole host +// uid range. +func uidmapIsHost(oneline string) bool { + oneline = strings.TrimSuffix(oneline, "\n") + if len(oneline) == 0 { + return false + } + lines := strings.Split(oneline, "\n") + if len(lines) != 1 { + return false + } + words := strings.Fields(lines[0]) + if len(words) != 3 || words[0] != "0" || words[1] != "0" || words[2] != "4294967295" { + return false + } + + return true +} + +func AmHostRoot() bool { + // if not uid 0, not host root + if os.Geteuid() != 0 { + return false + } + // if uid_map doesn't map 0 to 0, not host root + bytes, err := os.ReadFile("/proc/self/uid_map") + if err != nil { + return false + } + return uidmapIsHost(string(bytes)) +} diff --git a/pkg/common/mount.go b/pkg/common/mount.go new file mode 100644 index 0000000..d3b87e4 --- /dev/null +++ b/pkg/common/mount.go @@ -0,0 +1,99 @@ +package common + +import ( + "os" + "os/exec" + "path/filepath" + + "github.com/pkg/errors" + "golang.org/x/sys/unix" + "machinerun.io/atomfs/pkg/mount" + "machinerun.io/atomfs/pkg/verity" +) + +func HostMount(squashfs string, mountpoint string, rootHash string, veritySize int64, verityOffset uint64) error { + return verity.VerityHostMount(squashfs, mountpoint, rootHash, veritySize, verityOffset) +} + +// Mount a filesystem as container root, without host root +// privileges. We do this using squashfuse. +func GuestMount(squashFile string, mountpoint string, cmd *exec.Cmd) error { + if isMountpoint(mountpoint) { + return errors.Errorf("%s is already mounted", mountpoint) + } + + abs, err := filepath.Abs(squashFile) + if err != nil { + return errors.Errorf("Failed to get absolute path for %s: %v", squashFile, err) + } + squashFile = abs + + abs, err = filepath.Abs(mountpoint) + if err != nil { + return errors.Errorf("Failed to get absolute path for %s: %v", mountpoint, err) + } + mountpoint = abs + + if err := cmd.Process.Release(); err != nil { + return errors.Errorf("Failed to release process after guestmount %s: %v", squashFile, err) + } + return nil +} + +func Umount(mountpoint string) error { + mounts, err := mount.ParseMounts("/proc/self/mountinfo") + if err != nil { + return err + } + + // first, find the verity device that backs the mount + theMount, found := mounts.FindMount(mountpoint) + if !found { + return errors.Errorf("%s is not a mountpoint", mountpoint) + } + + err = unix.Unmount(mountpoint, 0) + if err != nil { + return errors.Wrapf(err, "failed unmounting %v", mountpoint) + } + + if _, err := os.Stat(theMount.Source); err != nil { + if os.IsNotExist(err) { + return nil + } + return errors.WithStack(err) + } + + return verity.VerityUnmount(theMount.Source) +} + +func isMountpoint(dest string) bool { + mounted, err := mount.IsMountpoint(dest) + return err == nil && mounted +} + +func IsMountedAtDir(_, dest string) (bool, error) { + dstat, err := os.Stat(dest) + if os.IsNotExist(err) { + return false, nil + } + if !dstat.IsDir() { + return false, nil + } + mounts, err := mount.ParseMounts("/proc/self/mountinfo") + if err != nil { + return false, err + } + + fdest, err := filepath.Abs(dest) + if err != nil { + return false, err + } + for _, m := range mounts { + if m.Target == fdest { + return true, nil + } + } + + return false, nil +} diff --git a/utils.go b/pkg/common/utils.go similarity index 98% rename from utils.go rename to pkg/common/utils.go index 3788018..324eff9 100644 --- a/utils.go +++ b/pkg/common/utils.go @@ -1,4 +1,4 @@ -package atomfs +package common import ( "fmt" diff --git a/pkg/erofs/erofs.go b/pkg/erofs/erofs.go new file mode 100644 index 0000000..2e186ef --- /dev/null +++ b/pkg/erofs/erofs.go @@ -0,0 +1,643 @@ +// This package is a small go "library" (read: exec wrapper) around the +// mkfs.erofs binary that provides some useful primitives. +package erofs + +import ( + "bytes" + "fmt" + "io" + "os" + "os/exec" + "path" + "path/filepath" + "strings" + "sync" + "syscall" + "time" + + "github.com/Masterminds/semver/v3" + "github.com/pkg/errors" + "golang.org/x/sys/unix" + "machinerun.io/atomfs/pkg/common" + "machinerun.io/atomfs/pkg/log" + vrty "machinerun.io/atomfs/pkg/verity" +) + +var checkZstdSupported sync.Once +var zstdIsSuspported bool + +var exPolInfo struct { + once sync.Once + err error + policy *ExtractPolicy +} + +type erofsFuseInfoStruct struct { + Path string + Version string + SupportsNotfiy bool +} + +var once sync.Once +var erofsFuseInfo = erofsFuseInfoStruct{"", "", false} + +func MakeErofs(tempdir string, rootfs string, eps *common.ExcludePaths, verity vrty.VerityMetadata) (io.ReadCloser, string, string, error) { + var excludesFile string + var err error + var toExclude string + var rootHash string + + if eps != nil { + toExclude, err = eps.String() + if err != nil { + return nil, "", rootHash, errors.Wrapf(err, "couldn't create exclude path list") + } + } + + if len(toExclude) != 0 { + excludes, err := os.CreateTemp(tempdir, "stacker-erofs-exclude-") + if err != nil { + return nil, "", rootHash, err + } + defer os.Remove(excludes.Name()) + + excludesFile = excludes.Name() + _, err = excludes.WriteString(toExclude) + excludes.Close() + if err != nil { + return nil, "", rootHash, err + } + } + + tmpErofs, err := os.CreateTemp(tempdir, "stacker-erofs-img-") + if err != nil { + return nil, "", rootHash, err + } + tmpErofs.Close() + os.Remove(tmpErofs.Name()) + defer os.Remove(tmpErofs.Name()) + args := []string{tmpErofs.Name(), rootfs} + compression := GzipCompression + if mkerofsSupportsZstd() { + args = append(args, "-z", "zstd") + compression = ZstdCompression + } + if len(toExclude) != 0 { + args = append(args, "--exclude-path", excludesFile) + } + cmd := exec.Command("mkfs.erofs", args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err = cmd.Run(); err != nil { + return nil, "", rootHash, errors.Wrap(err, "couldn't build erofs") + } + + if verity { + rootHash, err = vrty.AppendVerityData(tmpErofs.Name()) + if err != nil { + return nil, "", rootHash, err + } + } + + blob, err := os.Open(tmpErofs.Name()) + if err != nil { + return nil, "", rootHash, errors.WithStack(err) + } + + return blob, GenerateErofsMediaType(compression, verity), rootHash, nil +} + +func findErofsFuseInfo() { + var erofsPath string + if p := which("erofsfuse"); p != "" { + erofsPath = p + } else { + erofsPath = which("erofsfuse") + } + if erofsPath == "" { + return + } + version, supportsNotify := erofsfuseSupportsMountNotification(erofsPath) + log.Infof("Found erofsfuse at %s (version=%s notify=%t)", erofsPath, version, supportsNotify) + erofsFuseInfo = erofsFuseInfoStruct{erofsPath, version, supportsNotify} +} + +// erofsfuseSupportsMountNotification - returns true if erofsfuse supports mount +// notification, false otherwise +// erofsfuse is the path to the erofsfuse binary +func erofsfuseSupportsMountNotification(erofsfuse string) (string, bool) { + cmd := exec.Command(erofsfuse) + + // `erofsfuse` always returns an error... so we ignore it. + out, _ := cmd.CombinedOutput() + + firstLine := strings.Split(string(out[:]), "\n")[0] + version := strings.Split(firstLine, " ")[1] + v, err := semver.NewVersion(version) + if err != nil { + return version, false + } + // erofsfuse notify mechanism was merged in 0.5.0 + constraint, err := semver.NewConstraint(">= 0.5.0") + if err != nil { + return version, false + } + if constraint.Check(v) { + return version, true + } + return version, false +} + +var squashNotFound = errors.Errorf("erofsfuse program not found") + +// erofsFuse - mount squashFile to extractDir +// return a pointer to the erofsfuse cmd. +// The caller of the this is responsible for the process created. +func erofsFuse(squashFile, extractDir string) (*exec.Cmd, error) { + var cmd *exec.Cmd + + once.Do(findErofsFuseInfo) + if erofsFuseInfo.Path == "" { + return cmd, squashNotFound + } + + notifyOpts := "" + notifyPath := "" + if erofsFuseInfo.SupportsNotfiy { + sockdir, err := os.MkdirTemp("", "sock") + if err != nil { + return cmd, err + } + defer os.RemoveAll(sockdir) + notifyPath = filepath.Join(sockdir, "notifypipe") + if err := syscall.Mkfifo(notifyPath, 0640); err != nil { + return cmd, err + } + notifyOpts = "notify_pipe=" + notifyPath + } + + // given extractDir of path/to/some/dir[/], log to path/to/some/.dir-squashfs.log + extractDir = strings.TrimSuffix(extractDir, "/") + + var cmdOut io.Writer + var err error + + logf := filepath.Join(path.Dir(extractDir), "."+filepath.Base(extractDir)+"-erofsfuse.log") + if cmdOut, err = os.OpenFile(logf, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0644); err != nil { + log.Infof("Failed to open %s for write: %v", logf, err) + return cmd, err + } + + fiPre, err := os.Lstat(extractDir) + if err != nil { + return cmd, errors.Wrapf(err, "Failed stat'ing %q", extractDir) + } + if fiPre.Mode()&os.ModeSymlink != 0 { + return cmd, errors.Errorf("Refusing to mount onto a symbolic linkd") + } + + // It would be nice to only enable debug (or maybe to only log to file at all) + // if 'stacker --debug', but we do not have access to that info here. + // to debug erofsfuse, use "allow_other,debug" + optionArgs := "allow_other,debug" + if notifyOpts != "" { + optionArgs += "," + notifyOpts + } + cmd = exec.Command(erofsFuseInfo.Path, "-f", "-o", optionArgs, squashFile, extractDir) + cmd.Stdin = nil + cmd.Stdout = cmdOut + cmd.Stderr = cmdOut + cmdOut.Write([]byte(fmt.Sprintf("# %s\n", strings.Join(cmd.Args, " ")))) + if err != nil { + return cmd, errors.Wrapf(err, "Failed writing to %s", logf) + } + log.Debugf("Extracting %s -> %s with %s [%s]", squashFile, extractDir, erofsFuseInfo.Path, logf) + err = cmd.Start() + if err != nil { + return cmd, err + } + + // now poll/wait for one of 3 things to happen + // a. child process exits - if it did, then some error has occurred. + // b. the directory Entry is different than it was before the call + // to erofsfuse. We have to do this because we do not have another + // way to know when the mount has been populated. + // https://github.com/vasi/erofsfuse/issues/49 + // c. a timeout (timeLimit) was hit + startTime := time.Now() + timeLimit := 30 * time.Second + alarmCh := make(chan struct{}) + go func() { + _ = cmd.Wait() + close(alarmCh) + }() + if erofsFuseInfo.SupportsNotfiy { + notifyCh := make(chan byte) + log.Infof("%s supports notify pipe, watching %q", erofsFuseInfo.Path, notifyPath) + go func() { + f, err := os.Open(notifyPath) + if err != nil { + return + } + defer f.Close() + b1 := make([]byte, 1) + for { + n1, err := f.Read(b1) + if err != nil { + return + } + if err == nil && n1 >= 1 { + break + } + } + notifyCh <- b1[0] + }() + + select { + case <-alarmCh: + cmd.Process.Kill() + return cmd, errors.Wrapf(err, "Gave up on erofsfuse mount of %s with %s after %s", squashFile, erofsFuseInfo.Path, timeLimit) + case ret := <-notifyCh: + if ret == 's' { + return cmd, nil + } else { + return cmd, errors.Errorf("erofsfuse returned an error, check %s", logf) + } + } + } + for count := 0; !common.FileChanged(fiPre, extractDir); count++ { + if cmd.ProcessState != nil { + // process exited, the Wait() call in the goroutine above + // caused ProcessState to be populated. + return cmd, errors.Errorf("erofsfuse mount of %s with %s exited unexpectedly with %d", squashFile, erofsFuseInfo.Path, cmd.ProcessState.ExitCode()) + } + if time.Since(startTime) > timeLimit { + cmd.Process.Kill() + return cmd, errors.Wrapf(err, "Gave up on erofsfuse mount of %s with %s after %s", squashFile, erofsFuseInfo.Path, timeLimit) + } + if count%10 == 1 { + log.Debugf("%s is not yet mounted...(%s)", extractDir, time.Since(startTime)) + } + time.Sleep(time.Duration(50 * time.Millisecond)) + } + + return cmd, nil +} + +type ExtractPolicy struct { + Extractors []SquashExtractor + Extractor SquashExtractor + Excuses map[string]error + initialized bool + mutex sync.Mutex +} + +type SquashExtractor interface { + Name() string + IsAvailable() error + // Mount - Mount or extract path to dest. + // Return nil on "already extracted" + // Return error on failure. + Mount(path, dest string) error +} + +func NewExtractPolicy(args ...string) (*ExtractPolicy, error) { + p := &ExtractPolicy{ + Extractors: []SquashExtractor{}, + Excuses: map[string]error{}, + } + + allEx := []SquashExtractor{ + &KernelExtractor{}, + &ErofsFuseExtractor{}, + &UnsquashfsExtractor{}, + } + byName := map[string]SquashExtractor{} + for _, i := range allEx { + byName[i.Name()] = i + } + + for _, i := range args { + extractor, ok := byName[i] + if !ok { + return nil, errors.Errorf("Unknown extractor: '%s'", i) + } + excuse := extractor.IsAvailable() + if excuse != nil { + p.Excuses[i] = excuse + continue + } + p.Extractors = append(p.Extractors, extractor) + } + return p, nil +} + +type UnsquashfsExtractor struct { + mutex sync.Mutex +} + +func (k *UnsquashfsExtractor) Name() string { + return "unsquashfs" +} + +func (k *UnsquashfsExtractor) IsAvailable() error { + if which("unsquashfs") == "" { + return errors.Errorf("no 'unsquashfs' in PATH") + } + return nil +} + +func (k *UnsquashfsExtractor) Mount(squashFile, extractDir string) error { + k.mutex.Lock() + defer k.mutex.Unlock() + + // check if already extracted + empty, err := isEmptyDir(extractDir) + if err != nil { + return errors.Wrapf(err, "Error checking for empty dir") + } + if !empty { + return nil + } + + log.Debugf("unsquashfs %s -> %s", squashFile, extractDir) + cmd := exec.Command("unsquashfs", "-f", "-d", extractDir, squashFile) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = nil + err = cmd.Run() + + // on failure, remove the directory + if err != nil { + if rmErr := os.RemoveAll(extractDir); rmErr != nil { + log.Errorf("Failed to remove %s after failed extraction of %s: %v", extractDir, squashFile, rmErr) + } + return err + } + + // assert that extraction must create files. This way we can assume non-empty dir above + // was populated by unsquashfs. + empty, err = isEmptyDir(extractDir) + if err != nil { + return errors.Errorf("Failed to read %s after successful extraction of %s: %v", + extractDir, squashFile, err) + } + if empty { + return errors.Errorf("%s was an empty fs image", squashFile) + } + + return nil +} + +type KernelExtractor struct { + mutex sync.Mutex +} + +func (k *KernelExtractor) Name() string { + return "kmount" +} + +func (k *KernelExtractor) IsAvailable() error { + if !common.AmHostRoot() { + return errors.Errorf("not host root") + } + return nil +} + +func (k *KernelExtractor) Mount(squashFile, extractDir string) error { + k.mutex.Lock() + defer k.mutex.Unlock() + + if mounted, err := common.IsMountedAtDir(squashFile, extractDir); err != nil { + return err + } else if mounted { + return nil + } + + ecmd := []string{"mount", "-tsquashfs", "-oloop,ro", squashFile, extractDir} + var output bytes.Buffer + cmd := exec.Command(ecmd[0], ecmd[1:]...) + cmd.Stdin = nil + cmd.Stdout = &output + cmd.Stderr = cmd.Stdout + err := cmd.Run() + if err == nil { + return nil + } + + var retErr error + + exitError, ok := err.(*exec.ExitError) + if !ok { + retErr = errors.Errorf("kmount(%s) had unexpected error (no-rc), in exec (%v): %v", + squashFile, ecmd, err) + } else if status, ok := exitError.Sys().(syscall.WaitStatus); !ok { + retErr = errors.Errorf("kmount(%s) had unexpected error (no-status), in exec (%v): %v", + squashFile, ecmd, err) + } else { + retErr = errors.Errorf("kmount(%s) exited %d: %v", squashFile, status.ExitStatus(), output.String()) + } + + return retErr +} + +type ErofsFuseExtractor struct { + mutex sync.Mutex +} + +func (k *ErofsFuseExtractor) Name() string { + return "erofsfuse" +} + +func (k *ErofsFuseExtractor) IsAvailable() error { + once.Do(findErofsFuseInfo) + if erofsFuseInfo.Path == "" { + return errors.Errorf("no 'erofsfuse' in PATH") + } + return nil +} + +func (k *ErofsFuseExtractor) Mount(erofsFile, extractDir string) error { + k.mutex.Lock() + defer k.mutex.Unlock() + + if mounted, err := common.IsMountedAtDir(erofsFile, extractDir); mounted && err == nil { + log.Debugf("[%s] %s already mounted -> %s", k.Name(), erofsFile, extractDir) + return nil + } else if err != nil { + return err + } + + cmd, err := erofsFuse(erofsFile, extractDir) + if err != nil { + return err + } + + log.Debugf("erofsfuse mounted (%d) %s -> %s", cmd.Process.Pid, erofsFile, extractDir) + if err := cmd.Process.Release(); err != nil { + return errors.Errorf("Failed to release process %s: %v", cmd, err) + } + return nil +} + +// ExtractSingleErofsPolicy - extract squashfile to extractDir +func ExtractSingleErofsPolicy(squashFile, extractDir string, policy *ExtractPolicy) error { + const initName = "init" + if policy == nil { + return errors.Errorf("policy cannot be nil") + } + + // avoid taking a lock if already initialized (possibly premature optimization) + if !policy.initialized { + policy.mutex.Lock() + // We may have been waiting on the initializer. If so, then the policy will now be initialized. + // if not, then we are the initializer. + if !policy.initialized { + defer policy.mutex.Unlock() + defer func() { + policy.initialized = true + }() + } else { + policy.mutex.Unlock() + } + } + + err := os.MkdirAll(extractDir, 0755) + if err != nil { + return err + } + + fdest, err := filepath.Abs(extractDir) + if err != nil { + return err + } + + if policy.initialized { + if err, ok := policy.Excuses[initName]; ok { + return err + } + return policy.Extractor.Mount(squashFile, fdest) + } + + // At this point we are the initialzer + if policy.Excuses == nil { + policy.Excuses = map[string]error{} + } + + if len(policy.Extractors) == 0 { + policy.Excuses[initName] = errors.Errorf("policy had no extractors") + return policy.Excuses[initName] + } + + var extractor SquashExtractor + allExcuses := []string{} + for _, extractor = range policy.Extractors { + err = extractor.Mount(squashFile, fdest) + if err == nil { + policy.Extractor = extractor + log.Debugf("Selected squashfs extractor %s", extractor.Name()) + return nil + } + policy.Excuses[extractor.Name()] = err + } + + for n, exc := range policy.Excuses { + allExcuses = append(allExcuses, fmt.Sprintf("%s: %v", n, exc)) + } + + // nothing worked. populate Excuses[initName] + policy.Excuses[initName] = errors.Errorf("No suitable extractor found:\n %s", strings.Join(allExcuses, "\n ")) + return policy.Excuses[initName] +} + +// ExtractSingleErofs - extract the squashFile to extractDir +// Initialize a extractPolicy struct and then call ExtractSingleErofsPolicy +// wik()th that. +func ExtractSingleErofs(squashFile string, extractDir string) error { + exPolInfo.once.Do(func() { + const envName = "STACKER_SQUASHFS_EXTRACT_POLICY" + const defPolicy = "kmount erofsfuse unsquashfs" + val := os.Getenv(envName) + if val == "" { + val = defPolicy + } + exPolInfo.policy, exPolInfo.err = NewExtractPolicy(strings.Fields(val)...) + if exPolInfo.err == nil { + for k, v := range exPolInfo.policy.Excuses { + log.Debugf(" squashfs extractor %s is not available: %v", k, v) + } + } + }) + + if exPolInfo.err != nil { + return exPolInfo.err + } + + return ExtractSingleErofsPolicy(squashFile, extractDir, exPolInfo.policy) +} + +func mkerofsSupportsZstd() bool { + checkZstdSupported.Do(func() { + var stdoutBuffer strings.Builder + var stderrBuffer strings.Builder + + cmd := exec.Command("mkfs.erofs", "--help") + cmd.Stdout = &stdoutBuffer + cmd.Stderr = &stderrBuffer + + // Ignore errs here as `mkerofs --help` exit status code is 1 + _ = cmd.Run() + + if strings.Contains(stdoutBuffer.String(), "zstd") || + strings.Contains(stderrBuffer.String(), "zstd") { + zstdIsSuspported = true + } + }) + + return zstdIsSuspported +} + +func isEmptyDir(path string) (bool, error) { + fh, err := os.Open(path) + if err != nil { + return false, err + } + + _, err = fh.ReadDir(1) + if err == io.EOF { + return true, nil + } + return false, err +} + +// which - like the unix utility, return empty string for not-found. +// this might fit well in lib/, but currently lib's test imports +// squashfs creating a import loop. +func which(name string) string { + return whichSearch(name, strings.Split(os.Getenv("PATH"), ":")) +} + +func whichSearch(name string, paths []string) string { + var search []string + + if strings.ContainsRune(name, os.PathSeparator) { + if filepath.IsAbs(name) { + search = []string{name} + } else { + search = []string{"./" + name} + } + } else { + search = []string{} + for _, p := range paths { + search = append(search, filepath.Join(p, name)) + } + } + + for _, fPath := range search { + if err := unix.Access(fPath, unix.X_OK); err == nil { + return fPath + } + } + + return "" +} diff --git a/pkg/erofs/fs.go b/pkg/erofs/fs.go new file mode 100644 index 0000000..4c9a177 --- /dev/null +++ b/pkg/erofs/fs.go @@ -0,0 +1,77 @@ +package erofs + +import ( + "io" + "os" + + "github.com/pkg/errors" + "machinerun.io/atomfs/pkg/common" + "machinerun.io/atomfs/pkg/verity" +) + +type _erofs struct { +} + +func New() *_erofs { + return &_erofs{} +} + +func (er *_erofs) Make(tempdir string, rootfs string, eps *common.ExcludePaths, verity verity.VerityMetadata) (io.ReadCloser, string, string, error) { + return nil, "", "", nil +} + +func (er *_erofs) ExtractSingle(squashFile string, extractDir string) error { + return ExtractSingleErofs(squashFile, extractDir) +} + +func (er *_erofs) Mount(squashfs, mountpoint, rootHash string) error { + if !common.AmHostRoot() { + return er.guestMount(squashfs, mountpoint) + } + err := er.hostMount(squashfs, mountpoint, rootHash) + if err == nil || rootHash != "" { + return err + } + return er.guestMount(squashfs, mountpoint) +} + +func verityDataLocation1(squashfs string) (int64, uint64, error) { + fi, err := os.Stat(squashfs) + if err != nil { + return -1, 0, errors.WithStack(err) + } + + sblock, err := readSuperblock(squashfs) + if err != nil { + return -1, 0, err + } + + verityOffset, err := verityDataLocation(sblock) + if err != nil { + return -1, 0, err + } + + return fi.Size(), verityOffset, nil +} + +func (er *_erofs) hostMount(squashfs string, mountpoint string, rootHash string) error { + veritySize, verityOffset, err := verityDataLocation1(squashfs) + if err != nil { + return err + } + + return common.HostMount(squashfs, mountpoint, rootHash, veritySize, verityOffset) +} + +func (er *_erofs) guestMount(squashFile string, mountpoint string) error { + cmd, err := erofsFuse(squashFile, mountpoint) + if err != nil { + return err + } + + return common.GuestMount(squashFile, mountpoint, cmd) +} + +func (er *_erofs) Umount(mountpoint string) error { + return common.Umount(mountpoint) +} diff --git a/pkg/erofs/mediatype.go b/pkg/erofs/mediatype.go new file mode 100644 index 0000000..90d29f4 --- /dev/null +++ b/pkg/erofs/mediatype.go @@ -0,0 +1,29 @@ +package erofs + +import ( + "fmt" + "strings" + + vrty "machinerun.io/atomfs/pkg/verity" +) + +type ErofsCompression string + +const ( + BaseMediaTypeLayerErofs = "application/vnd.stacker.image.layer.erofs" + + GzipCompression ErofsCompression = "gzip" + ZstdCompression ErofsCompression = "zstd" +) + +func IsErofsMediaType(mediaType string) bool { + return strings.HasPrefix(mediaType, BaseMediaTypeLayerErofs) +} + +func GenerateErofsMediaType(comp ErofsCompression, verity vrty.VerityMetadata) string { + verityString := "" + if verity { + verityString = fmt.Sprintf("+%s", vrty.VeritySuffix) + } + return fmt.Sprintf("%s+%s%s", BaseMediaTypeLayerErofs, comp, verityString) +} diff --git a/pkg/erofs/superblock.go b/pkg/erofs/superblock.go new file mode 100644 index 0000000..094f68c --- /dev/null +++ b/pkg/erofs/superblock.go @@ -0,0 +1,247 @@ +package erofs + +import ( + "encoding/binary" + "io" + "os" + + "github.com/pkg/errors" +) + +/* + +https://docs.kernel.org/filesystems/erofs.html + +On-disk details + + |-> aligned with the block size + ____________________________________________________________ +| |SB| | ... | Metadata | ... | Data | Metadata | ... | Data | +|_|__|_|_____|__________|_____|______|__________|_____|______| +0 +1K + +*/ + +const ( + // Definitions for superblock. + superblockMagicV1 = 0xe0f5e1e2 + superblockMagic = superblockMagicV1 + superblockOffset = 1024 + + // Inode slot size in bit shift. + InodeSlotBits = 5 + + // Max file name length. + MaxNameLen = 255 +) + +// Bit definitions for Inode*::Format. +const ( + InodeLayoutBit = 0 + InodeLayoutBits = 1 + + InodeDataLayoutBit = 1 + InodeDataLayoutBits = 3 +) + +// Inode layouts. +const ( + InodeLayoutCompact = 0 + InodeLayoutExtended = 1 +) + +// Inode data layouts. +const ( + InodeDataLayoutFlatPlain = iota + InodeDataLayoutFlatCompressionLegacy + InodeDataLayoutFlatInline + InodeDataLayoutFlatCompression + InodeDataLayoutChunkBased + InodeDataLayoutMax +) + +// Features w/ backward compatibility. +// This is not exhaustive, unused features are not listed. +const ( + FeatureCompatSuperBlockChecksum = 0x00000001 +) + +// Features w/o backward compatibility. +// +// Any features that aren't in FeatureIncompatSupported are incompatible +// with this implementation. +// +// This is not exhaustive, unused features are not listed. +const ( + FeatureIncompatSupported = 0x0 +) + +// Sizes of on-disk structures in bytes. +const ( + superblockSize = 128 + InodeCompactSize = 32 + InodeExtendedSize = 64 + DirentSize = 12 +) + +type superblock struct { + Magic uint32 + Checksum uint32 + FeatureCompat uint32 + BlockSizeBits uint8 + ExtSlots uint8 + RootNid uint16 + Inodes uint64 + BuildTime uint64 + BuildTimeNsec uint32 + Blocks uint32 + MetaBlockAddr uint32 + XattrBlockAddr uint32 + UUID [16]uint8 + VolumeName [16]uint8 + FeatureIncompat uint32 + Union1 uint16 + ExtraDevices uint16 + DevTableSlotOff uint16 + Reserved [38]uint8 +} + +/* +// checkRange checks whether the range [off, off+n) is valid. +func (i *Image) checkRange(off, n uint64) bool { + size := uint64(len(i.bytes)) + end := off + n + return off < size && off <= end && end <= size +} + +// BytesAt returns the bytes at [off, off+n) of the image. +func (i *Image) BytesAt(off, n uint64) ([]byte, error) { + if ok := i.checkRange(off, n); !ok { + //log.Warningf("Invalid byte range (off: 0x%x, n: 0x%x) for image (size: 0x%x)", off, n, len(i.bytes)) + return nil, linuxerr.EFAULT + } + return i.bytes[off : off+n], nil +} + +// unmarshalAt deserializes data from the bytes at [off, off+n) of the image. +func (i *Image) unmarshalAt(data marshal.Marshallable, off uint64) error { + bytes, err := i.BytesAt(off, uint64(data.SizeBytes())) + if err != nil { + //log.Warningf("Failed to deserialize %T from 0x%x.", data, off) + return err + } + data.UnmarshalUnsafe(bytes) + return nil +} + +// initSuperBlock initializes the superblock of this image. +func (i *Image) initSuperBlock() error { + // i.sb is used in the hot path. Let's save a copy of the superblock. + if err := i.unmarshalAt(&i.sb, SuperBlockOffset); err != nil { + return fmt.Errorf("image size is too small") + } + + if i.sb.Magic != SuperBlockMagicV1 { + return fmt.Errorf("unknown magic: 0x%x", i.sb.Magic) + } + + if err := i.verifyChecksum(); err != nil { + return err + } + + if featureIncompat := i.sb.FeatureIncompat & ^uint32(FeatureIncompatSupported); featureIncompat != 0 { + return fmt.Errorf("unsupported incompatible features detected: 0x%x", featureIncompat) + } + + if i.BlockSize()%hostarch.PageSize != 0 { + return fmt.Errorf("unsupported block size: 0x%x", i.BlockSize()) + } + + return nil +} + +// verifyChecksum verifies the checksum of the superblock. +func (i *Image) verifyChecksum() error { + if i.sb.FeatureCompat&FeatureCompatSuperBlockChecksum == 0 { + return nil + } + + sb := i.sb + sb.Checksum = 0 + table := crc32.MakeTable(crc32.Castagnoli) + checksum := crc32.Checksum(marshal.Marshal(&sb), table) +// unmarshalAt deserializes data from the bytes at [off, off+n) of the image. +func (i *Image) unmarshalAt(data marshal.Marshallable, off uint64) error { + bytes, err := i.BytesAt(off, uint64(data.SizeBytes())) + if err != nil { + log.Warningf("Failed to deserialize %T from 0x%x.", data, off) + return err + } + data.UnmarshalUnsafe(bytes) + return nil +} + off := SuperBlockOffset + uint64(i.sb.SizeBytes()) + if bytes, err := i.BytesAt(off, uint64(i.BlockSize())-off); err != nil { + return fmt.Errorf("image size is too small") + } else { + checksum = ^crc32.Update(checksum, table, bytes) + } + if checksum != i.sb.Checksum { + return fmt.Errorf("invalid checksum: 0x%x, expected: 0x%x", checksum, i.sb.Checksum) + } + + return nil +} +*/ + +func parseSuperblock(b []byte) (*superblock, error) { + if len(b) != superblockSize { + return nil, errors.Errorf("superblock had %d bytes instead of expected %d", len(b), superblockSize) + } + + magic := binary.LittleEndian.Uint32(b[0:4]) + if magic != superblockMagic { + return nil, errors.Errorf("superblock had magic of %d instead of expected %d", magic, superblockMagic) + } + + // FIXME: also verify checksum + + s := &superblock{ + Magic: magic, // b[0:4] + Checksum: binary.LittleEndian.Uint32(b[4:8]), + FeatureCompat: binary.LittleEndian.Uint32(b[8:12]), + BlockSizeBits: b[12], // b[12:13] + ExtSlots: b[13], // b[13:14] + RootNid: binary.LittleEndian.Uint16(b[14:16]), + Inodes: binary.LittleEndian.Uint64(b[16:24]), + BuildTime: binary.LittleEndian.Uint64(b[24:32]), + BuildTimeNsec: binary.LittleEndian.Uint32(b[32:36]), + Blocks: binary.LittleEndian.Uint32(b[36:40]), + MetaBlockAddr: binary.LittleEndian.Uint32(b[40:44]), + XattrBlockAddr: binary.LittleEndian.Uint32(b[44:48]), + UUID: [16]byte(b[48:64]), + VolumeName: [16]byte(b[64:80]), + FeatureIncompat: binary.LittleEndian.Uint32(b[80:84]), + Union1: binary.LittleEndian.Uint16(b[84:86]), + ExtraDevices: binary.LittleEndian.Uint16(b[86:88]), + DevTableSlotOff: binary.LittleEndian.Uint16(b[88:90]), + Reserved: [38]byte(b[90:128]), + } + + return s, nil +} + +func readSuperblock(path string) (*superblock, error) { + reader, err := os.Open(path) + if err != nil { + return nil, err + } + defer reader.Close() + + buf := make([]byte, superblockOffset+superblockSize) + if _, err := io.ReadFull(reader, buf); err != nil { + return nil, err + } + + return parseSuperblock(buf[superblockOffset:]) +} diff --git a/pkg/erofs/verity.go b/pkg/erofs/verity.go new file mode 100644 index 0000000..d51062c --- /dev/null +++ b/pkg/erofs/verity.go @@ -0,0 +1,5 @@ +package erofs + +func verityDataLocation(sblock *superblock) (uint64, error) { + return uint64((1 << sblock.BlockSizeBits) * sblock.Blocks), nil +} diff --git a/pkg/fs/fs.go b/pkg/fs/fs.go new file mode 100644 index 0000000..e372120 --- /dev/null +++ b/pkg/fs/fs.go @@ -0,0 +1,47 @@ +package fs + +import ( + "io" + + "machinerun.io/atomfs/pkg/common" + "machinerun.io/atomfs/pkg/erofs" + "machinerun.io/atomfs/pkg/squashfs" + "machinerun.io/atomfs/pkg/verity" +) + +type Filesystem interface { + // stacker + Make(tempdir string, rootfs string, eps *common.ExcludePaths, verity verity.VerityMetadata) (io.ReadCloser, string, string, error) + ExtractSingle(fsImgFile string, extractDir string) error + // atomfs + Mount(fsImgFile, mountpoint, rootHash string) error + Umount(mountpoint string) error +} + +type FilesystemType string + +const ( + SquashfsType FilesystemType = "squashfs" + ErofsType FilesystemType = "erofs" +) + +func New(fsType FilesystemType) Filesystem { + switch fsType { + case SquashfsType: + return squashfs.New() + case ErofsType: + return erofs.New() + } + + return nil +} + +func NewFromMediaType(mediaType string) Filesystem { + if squashfs.IsSquashfsMediaType(mediaType) { + return squashfs.New() + } else if erofs.IsErofsMediaType(mediaType) { + return erofs.New() + } + + return nil +} diff --git a/log/log.go b/pkg/log/log.go similarity index 100% rename from log/log.go rename to pkg/log/log.go diff --git a/molecule.go b/pkg/molecule/molecule.go similarity index 89% rename from molecule.go rename to pkg/molecule/molecule.go index f22e0c9..d92a6f2 100644 --- a/molecule.go +++ b/pkg/molecule/molecule.go @@ -1,4 +1,4 @@ -package atomfs +package molecule import ( "fmt" @@ -10,9 +10,11 @@ import ( ispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" "golang.org/x/sys/unix" - "machinerun.io/atomfs/log" - "machinerun.io/atomfs/mount" - "machinerun.io/atomfs/squashfs" + "machinerun.io/atomfs/pkg/common" + "machinerun.io/atomfs/pkg/fs" + "machinerun.io/atomfs/pkg/log" + "machinerun.io/atomfs/pkg/mount" + "machinerun.io/atomfs/pkg/verity" ) type Molecule struct { @@ -25,7 +27,7 @@ type Molecule struct { func (m Molecule) MetadataPath() (string, error) { - mountNSName, err := GetMountNSName() + mountNSName, err := common.GetMountNSName() if err != nil { return "", err } @@ -33,7 +35,7 @@ func (m Molecule) MetadataPath() (string, error) { if err != nil { return "", err } - metadir := filepath.Join(RuntimeDir(m.config.MetadataDir), "meta", mountNSName, ReplacePathSeparators(absTarget)) + metadir := filepath.Join(common.RuntimeDir(m.config.MetadataDir), "meta", mountNSName, common.ReplacePathSeparators(absTarget)) return metadir, nil } @@ -56,7 +58,7 @@ func (m Molecule) mountUnderlyingAtoms() (error, func()) { atomsMounted := []string{} cleanupAtoms := func() { for _, target := range atomsMounted { - if umountErr := squashfs.Umount(target); umountErr != nil { + if umountErr := common.Umount(target); umountErr != nil { log.Warnf("cleanup: failed to unmount atom @ target %q: %s", target, umountErr) } } @@ -69,14 +71,16 @@ func (m Molecule) mountUnderlyingAtoms() (error, func()) { return errors.Wrapf(err, "failed to find mounted atoms path for %+v", a), cleanupAtoms } - rootHash := a.Annotations[squashfs.VerityRootHashAnnotation] + fsi := fs.NewFromMediaType(a.MediaType) + + rootHash := a.Annotations[verity.VerityRootHashAnnotation] if !m.config.AllowMissingVerityData { if rootHash == "" { return errors.Errorf("%v is missing verity data", a.Digest), cleanupAtoms } - if !squashfs.AmHostRoot() { + if !common.AmHostRoot() { return errors.Errorf("won't guestmount an image with verity data without --allow-missing-verity"), cleanupAtoms } } @@ -90,13 +94,13 @@ func (m Molecule) mountUnderlyingAtoms() (error, func()) { if mounted { if rootHash != "" { - err = squashfs.ConfirmExistingVerityDeviceHash(mountpoint.Source, + err = verity.ConfirmExistingVerityDeviceHash(mountpoint.Source, rootHash, m.config.AllowMissingVerityData) if err != nil { return err, cleanupAtoms } - err = squashfs.ConfirmExistingVerityDeviceCurrentValidity(mountpoint.Source) + err = verity.ConfirmExistingVerityDeviceCurrentValidity(mountpoint.Source) if err != nil { return err, cleanupAtoms } @@ -108,7 +112,7 @@ func (m Molecule) mountUnderlyingAtoms() (error, func()) { return err, cleanupAtoms } - err = squashfs.Mount(m.config.AtomsPath(a.Digest.Encoded()), target, rootHash) + err = fsi.Mount(m.config.AtomsPath(a.Digest.Encoded()), target, rootHash) if err != nil { return err, cleanupAtoms } @@ -186,11 +190,11 @@ func (m Molecule) Mount(dest string) error { if err != nil { return errors.Wrapf(err, "can't find metadata path") } - if PathExists(metadir) { + if common.PathExists(metadir) { return fmt.Errorf("%q exists: cowardly refusing to mess with it", metadir) } - if err := EnsureDir(metadir); err != nil { + if err := common.EnsureDir(metadir); err != nil { return err } @@ -238,7 +242,7 @@ func (m Molecule) Mount(dest string) error { overlayArgs := "" if m.config.AddWriteableOverlay { rodest := filepath.Join(metadir, "ro") - if err = EnsureDir(rodest); err != nil { + if err = common.EnsureDir(rodest); err != nil { return err } @@ -249,12 +253,12 @@ func (m Molecule) Mount(dest string) error { } workdir := filepath.Join(persistMetaPath, "work") - if err := EnsureDir(workdir); err != nil { + if err := common.EnsureDir(workdir); err != nil { return errors.Wrapf(err, "failed to ensure workdir %q", workdir) } upperdir := filepath.Join(persistMetaPath, "persist") - if err := EnsureDir(upperdir); err != nil { + if err := common.EnsureDir(upperdir); err != nil { return errors.Wrapf(err, "failed to ensure upperdir %q", upperdir) } @@ -377,7 +381,7 @@ func Umount(dest string) error { continue } - err = squashfs.Umount(a) + err = common.Umount(a) if err != nil { return err } diff --git a/molecule_test.go b/pkg/molecule/molecule_test.go similarity index 97% rename from molecule_test.go rename to pkg/molecule/molecule_test.go index 973aeea..a3345aa 100644 --- a/molecule_test.go +++ b/pkg/molecule/molecule_test.go @@ -1,4 +1,4 @@ -package atomfs +package molecule import ( "fmt" diff --git a/oci.go b/pkg/molecule/oci.go similarity index 96% rename from oci.go rename to pkg/molecule/oci.go index 500247e..a7d5d03 100644 --- a/oci.go +++ b/pkg/molecule/oci.go @@ -1,4 +1,4 @@ -package atomfs +package molecule import ( "encoding/json" @@ -7,7 +7,7 @@ import ( ispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/opencontainers/umoci" - stackeroci "machinerun.io/atomfs/oci" + stackeroci "machinerun.io/atomfs/pkg/oci" ) type MountOCIOpts struct { diff --git a/mount/mountinfo.go b/pkg/mount/mountinfo.go similarity index 100% rename from mount/mountinfo.go rename to pkg/mount/mountinfo.go diff --git a/oci/oci.go b/pkg/oci/oci.go similarity index 100% rename from oci/oci.go rename to pkg/oci/oci.go diff --git a/pkg/squashfs/fs.go b/pkg/squashfs/fs.go new file mode 100644 index 0000000..25c6071 --- /dev/null +++ b/pkg/squashfs/fs.go @@ -0,0 +1,77 @@ +package squashfs + +import ( + "io" + "os" + + "github.com/pkg/errors" + "machinerun.io/atomfs/pkg/common" + "machinerun.io/atomfs/pkg/verity" +) + +type _sqfs struct { +} + +func New() *_sqfs { + return &_sqfs{} +} + +func (sq *_sqfs) Make(tempdir string, rootfs string, eps *common.ExcludePaths, verity verity.VerityMetadata) (io.ReadCloser, string, string, error) { + return nil, "", "", nil +} + +func (sq *_sqfs) ExtractSingle(squashFile string, extractDir string) error { + return ExtractSingleSquash(squashFile, extractDir) +} + +func (sq *_sqfs) Mount(squashfs, mountpoint, rootHash string) error { + if !common.AmHostRoot() { + return sq.guestMount(squashfs, mountpoint) + } + err := sq.hostMount(squashfs, mountpoint, rootHash) + if err == nil || rootHash != "" { + return err + } + return sq.guestMount(squashfs, mountpoint) +} + +func verityDataLocation1(squashfs string) (int64, uint64, error) { + fi, err := os.Stat(squashfs) + if err != nil { + return -1, 0, errors.WithStack(err) + } + + sblock, err := readSuperblock(squashfs) + if err != nil { + return -1, 0, err + } + + verityOffset, err := verityDataLocation(sblock) + if err != nil { + return -1, 0, err + } + + return fi.Size(), verityOffset, nil +} + +func (sq *_sqfs) hostMount(squashfs string, mountpoint string, rootHash string) error { + veritySize, verityOffset, err := verityDataLocation1(squashfs) + if err != nil { + return err + } + + return common.HostMount(squashfs, mountpoint, rootHash, veritySize, verityOffset) +} + +func (sq *_sqfs) guestMount(squashFile string, mountpoint string) error { + cmd, err := squashFuse(squashFile, mountpoint) + if err != nil { + return err + } + + return common.GuestMount(squashFile, mountpoint, cmd) +} + +func (sq *_sqfs) Umount(mountpoint string) error { + return common.Umount(mountpoint) +} diff --git a/squashfs/mediatype.go b/pkg/squashfs/mediatype.go similarity index 61% rename from squashfs/mediatype.go rename to pkg/squashfs/mediatype.go index 051fe9b..e1e007f 100644 --- a/squashfs/mediatype.go +++ b/pkg/squashfs/mediatype.go @@ -3,35 +3,27 @@ package squashfs import ( "fmt" "strings" + + vrty "machinerun.io/atomfs/pkg/verity" ) type SquashfsCompression string -type VerityMetadata bool const ( BaseMediaTypeLayerSquashfs = "application/vnd.stacker.image.layer.squashfs" GzipCompression SquashfsCompression = "gzip" ZstdCompression SquashfsCompression = "zstd" - - veritySuffix = "verity" - - VerityMetadataPresent VerityMetadata = true - VerityMetadataMissing VerityMetadata = false ) func IsSquashfsMediaType(mediaType string) bool { return strings.HasPrefix(mediaType, BaseMediaTypeLayerSquashfs) } -func GenerateSquashfsMediaType(comp SquashfsCompression, verity VerityMetadata) string { +func GenerateSquashfsMediaType(comp SquashfsCompression, verity vrty.VerityMetadata) string { verityString := "" if verity { - verityString = fmt.Sprintf("+%s", veritySuffix) + verityString = fmt.Sprintf("+%s", vrty.VeritySuffix) } return fmt.Sprintf("%s+%s%s", BaseMediaTypeLayerSquashfs, comp, verityString) } - -func HasVerityMetadata(mediaType string) VerityMetadata { - return VerityMetadata(strings.HasSuffix(mediaType, veritySuffix)) -} diff --git a/squashfs/squashfs.go b/pkg/squashfs/squashfs.go similarity index 84% rename from squashfs/squashfs.go rename to pkg/squashfs/squashfs.go index 3b26b5c..13998e8 100644 --- a/squashfs/squashfs.go +++ b/pkg/squashfs/squashfs.go @@ -18,9 +18,9 @@ import ( "github.com/Masterminds/semver/v3" "github.com/pkg/errors" "golang.org/x/sys/unix" - - "machinerun.io/atomfs/log" - "machinerun.io/atomfs/mount" + "machinerun.io/atomfs/pkg/common" + "machinerun.io/atomfs/pkg/log" + vrty "machinerun.io/atomfs/pkg/verity" ) var checkZstdSupported sync.Once @@ -32,19 +32,6 @@ var exPolInfo struct { policy *ExtractPolicy } -// ExcludePaths represents a list of paths to exclude in a squashfs listing. -// Users should do something like filepath.Walk() over the whole filesystem, -// calling AddExclude() or AddInclude() based on whether they want to include -// or exclude a particular file. Note that if e.g. /usr is excluded, then -// everyting underneath is also implicitly excluded. The -// AddExclude()/AddInclude() methods do the math to figure out what is the -// correct set of things to exclude or include based on what paths have been -// previously included or excluded. -type ExcludePaths struct { - exclude map[string]bool - include []string -} - type squashFuseInfoStruct struct { Path string Version string @@ -54,70 +41,7 @@ type squashFuseInfoStruct struct { var once sync.Once var squashFuseInfo = squashFuseInfoStruct{"", "", false} -func NewExcludePaths() *ExcludePaths { - return &ExcludePaths{ - exclude: map[string]bool{}, - include: []string{}, - } -} - -func (eps *ExcludePaths) AddExclude(p string) { - for _, inc := range eps.include { - // If /usr/bin/ls has changed but /usr hasn't, we don't want to list - // /usr in the include paths any more, so let's be sure to only - // add things which aren't prefixes. - if strings.HasPrefix(inc, p) { - return - } - } - eps.exclude[p] = true -} - -func (eps *ExcludePaths) AddInclude(orig string, isDir bool) { - // First, remove this thing and all its parents from exclude. - p := orig - - // normalize to the first dir - if !isDir { - p = path.Dir(p) - } - for { - // our paths are all absolute, so this is a base case - if p == "/" { - break - } - - delete(eps.exclude, p) - p = filepath.Dir(p) - } - - // now add it to the list of includes, so we don't accidentally re-add - // anything above. - eps.include = append(eps.include, orig) -} - -func (eps *ExcludePaths) String() (string, error) { - var buf bytes.Buffer - for p := range eps.exclude { - _, err := buf.WriteString(p) - if err != nil { - return "", err - } - _, err = buf.WriteString("\n") - if err != nil { - return "", err - } - } - - _, err := buf.WriteString("\n") - if err != nil { - return "", err - } - - return buf.String(), nil -} - -func MakeSquashfs(tempdir string, rootfs string, eps *ExcludePaths, verity VerityMetadata) (io.ReadCloser, string, string, error) { +func MakeSquashfs(tempdir string, rootfs string, eps *common.ExcludePaths, verity vrty.VerityMetadata) (io.ReadCloser, string, string, error) { var excludesFile string var err error var toExclude string @@ -169,7 +93,7 @@ func MakeSquashfs(tempdir string, rootfs string, eps *ExcludePaths, verity Verit } if verity { - rootHash, err = appendVerityData(tmpSquashfs.Name()) + rootHash, err = vrty.AppendVerityData(tmpSquashfs.Name()) if err != nil { return nil, "", rootHash, err } @@ -183,32 +107,6 @@ func MakeSquashfs(tempdir string, rootfs string, eps *ExcludePaths, verity Verit return blob, GenerateSquashfsMediaType(compression, verity), rootHash, nil } -func isMountedAtDir(_, dest string) (bool, error) { - dstat, err := os.Stat(dest) - if os.IsNotExist(err) { - return false, nil - } - if !dstat.IsDir() { - return false, nil - } - mounts, err := mount.ParseMounts("/proc/self/mountinfo") - if err != nil { - return false, err - } - - fdest, err := filepath.Abs(dest) - if err != nil { - return false, err - } - for _, m := range mounts { - if m.Target == fdest { - return true, nil - } - } - - return false, nil -} - func findSquashFuseInfo() { var sqfsPath string if p := which("squashfuse_ll"); p != "" { @@ -367,7 +265,7 @@ func squashFuse(squashFile, extractDir string) (*exec.Cmd, error) { } } } - for count := 0; !fileChanged(fiPre, extractDir); count++ { + for count := 0; !common.FileChanged(fiPre, extractDir); count++ { if cmd.ProcessState != nil { // process exited, the Wait() call in the goroutine above // caused ProcessState to be populated. @@ -500,7 +398,7 @@ func (k *KernelExtractor) Name() string { } func (k *KernelExtractor) IsAvailable() error { - if !AmHostRoot() { + if !common.AmHostRoot() { return errors.Errorf("not host root") } return nil @@ -510,7 +408,7 @@ func (k *KernelExtractor) Mount(squashFile, extractDir string) error { k.mutex.Lock() defer k.mutex.Unlock() - if mounted, err := isMountedAtDir(squashFile, extractDir); err != nil { + if mounted, err := common.IsMountedAtDir(squashFile, extractDir); err != nil { return err } else if mounted { return nil @@ -563,7 +461,7 @@ func (k *SquashFuseExtractor) Mount(squashFile, extractDir string) error { k.mutex.Lock() defer k.mutex.Unlock() - if mounted, err := isMountedAtDir(squashFile, extractDir); mounted && err == nil { + if mounted, err := common.IsMountedAtDir(squashFile, extractDir); mounted && err == nil { log.Debugf("[%s] %s already mounted -> %s", k.Name(), squashFile, extractDir) return nil } else if err != nil { diff --git a/squashfs/superblock.go b/pkg/squashfs/superblock.go similarity index 100% rename from squashfs/superblock.go rename to pkg/squashfs/superblock.go diff --git a/pkg/squashfs/verity.go b/pkg/squashfs/verity.go new file mode 100644 index 0000000..92de7b3 --- /dev/null +++ b/pkg/squashfs/verity.go @@ -0,0 +1,12 @@ +package squashfs + +func verityDataLocation(sblock *superblock) (uint64, error) { + squashLen := sblock.size + + // squashfs is padded out to the nearest 4k + if squashLen%4096 != 0 { + squashLen = squashLen + (4096 - squashLen%4096) + } + + return squashLen, nil +} diff --git a/squashfs/verity_test.go b/pkg/squashfs/verity_test.go similarity index 74% rename from squashfs/verity_test.go rename to pkg/squashfs/verity_test.go index 1ee55b7..017c80b 100644 --- a/squashfs/verity_test.go +++ b/pkg/squashfs/verity_test.go @@ -9,50 +9,9 @@ import ( "testing" "github.com/stretchr/testify/assert" + "machinerun.io/atomfs/pkg/verity" ) -type uidmapTestcase struct { - uidmap string - expected bool -} - -var uidmapTests = []uidmapTestcase{ - { - uidmap: ` 0 0 4294967295`, - expected: true, - }, - { - uidmap: ` 0 0 1000 -2000 2000 1`, - expected: false, - }, - { - uidmap: ` 0 0 1000`, - expected: false, - }, - { - uidmap: ` 10 0 4294967295`, - expected: false, - }, - { - uidmap: ` 0 10 4294967295`, - expected: false, - }, - { - uidmap: ` 0 0 1`, - expected: false, - }, -} - -func TestAmHostRoot(t *testing.T) { - t.Parallel() - assert := assert.New(t) - for _, testcase := range uidmapTests { - v := uidmapIsHost(testcase.uidmap) - assert.Equal(v, testcase.expected) - } -} - func TestVerityMetadata(t *testing.T) { assert := assert.New(t) @@ -67,8 +26,8 @@ func TestVerityMetadata(t *testing.T) { err = os.WriteFile(path.Join(rootfs, "foo"), []byte("bar"), 0644) assert.NoError(err) - reader, _, rootHash, err := MakeSquashfs(tempdir, rootfs, nil, VerityMetadataPresent) - if err == cryptsetupTooOld { + reader, _, rootHash, err := MakeSquashfs(tempdir, rootfs, nil, verity.VerityMetadataPresent) + if err == verity.CryptsetupTooOld { t.Skip("libcryptsetup too old") } assert.NoError(err) diff --git a/pkg/verity/metadata.go b/pkg/verity/metadata.go new file mode 100644 index 0000000..6bfabbb --- /dev/null +++ b/pkg/verity/metadata.go @@ -0,0 +1,16 @@ +package verity + +import "strings" + +type VerityMetadata bool + +const ( + VeritySuffix = "verity" + + VerityMetadataPresent VerityMetadata = true + VerityMetadataMissing VerityMetadata = false +) + +func HasVerityMetadata(mediaType string) VerityMetadata { + return VerityMetadata(strings.HasSuffix(mediaType, VeritySuffix)) +} diff --git a/squashfs/verity.go b/pkg/verity/verity.go similarity index 73% rename from squashfs/verity.go rename to pkg/verity/verity.go index ea1bf7b..dfe438d 100644 --- a/squashfs/verity.go +++ b/pkg/verity/verity.go @@ -1,4 +1,4 @@ -package squashfs +package verity // #cgo pkg-config: libcryptsetup devmapper --static // #include @@ -87,8 +87,6 @@ import ( "github.com/martinjungblut/go-cryptsetup" "github.com/pkg/errors" "golang.org/x/sys/unix" - - "machinerun.io/atomfs/mount" ) const VerityRootHashAnnotation = "io.stackeroci.stacker.squashfs_verity_root_hash" @@ -139,9 +137,9 @@ func isCryptsetupEINVAL(err error) bool { return ok && cse.Code() == -22 } -var cryptsetupTooOld = errors.Errorf("libcryptsetup not new enough, need >= 2.3.0") +var CryptsetupTooOld = errors.Errorf("libcryptsetup not new enough, need >= 2.3.0") -func appendVerityData(file string) (string, error) { +func AppendVerityData(file string) (string, error) { fi, err := os.Lstat(file) if err != nil { return "", errors.WithStack(err) @@ -182,7 +180,7 @@ func appendVerityData(file string) (string, error) { // render a special error message. rootHash, _, err := verityDevice.VolumeKeyGet(cryptsetup.CRYPT_ANY_SLOT, "") if isCryptsetupEINVAL(err) { - return "", cryptsetupTooOld + return "", CryptsetupTooOld } else if err != nil { return "", err } @@ -190,128 +188,16 @@ func appendVerityData(file string) (string, error) { return fmt.Sprintf("%x", rootHash), errors.WithStack(err) } -func verityDataLocation(sblock *superblock) (uint64, error) { - squashLen := sblock.size - - // squashfs is padded out to the nearest 4k - if squashLen%4096 != 0 { - squashLen = squashLen + (4096 - squashLen%4096) - } - - return squashLen, nil -} - func verityName(p string) string { - return fmt.Sprintf("%s-%s", p, veritySuffix) -} - -func fileChanged(a os.FileInfo, path string) bool { - b, err := os.Lstat(path) - if err != nil { - return true - } - return !os.SameFile(a, b) -} - -// Mount a filesystem as container root, without host root -// privileges. We do this using squashfuse. -func GuestMount(squashFile string, mountpoint string) error { - if isMountpoint(mountpoint) { - return errors.Errorf("%s is already mounted", mountpoint) - } - - abs, err := filepath.Abs(squashFile) - if err != nil { - return errors.Errorf("Failed to get absolute path for %s: %v", squashFile, err) - } - squashFile = abs - - abs, err = filepath.Abs(mountpoint) - if err != nil { - return errors.Errorf("Failed to get absolute path for %s: %v", mountpoint, err) - } - mountpoint = abs - - cmd, err := squashFuse(squashFile, mountpoint) - if err != nil { - return err - } - if err := cmd.Process.Release(); err != nil { - return errors.Errorf("Failed to release process after guestmount %s: %v", squashFile, err) - } - return nil + return fmt.Sprintf("%s-%s", p, VeritySuffix) } -func isMountpoint(dest string) bool { - mounted, err := mount.IsMountpoint(dest) - return err == nil && mounted -} - -// Takes /proc/self/uid_map contents as one string -// Returns true if this is a uidmap representing the whole host -// uid range. -func uidmapIsHost(oneline string) bool { - oneline = strings.TrimSuffix(oneline, "\n") - if len(oneline) == 0 { - return false - } - lines := strings.Split(oneline, "\n") - if len(lines) != 1 { - return false - } - words := strings.Fields(lines[0]) - if len(words) != 3 || words[0] != "0" || words[1] != "0" || words[2] != "4294967295" { - return false - } - - return true -} - -func AmHostRoot() bool { - // if not uid 0, not host root - if os.Geteuid() != 0 { - return false - } - // if uid_map doesn't map 0 to 0, not host root - bytes, err := os.ReadFile("/proc/self/uid_map") - if err != nil { - return false - } - return uidmapIsHost(string(bytes)) -} - -func Mount(squashfs, mountpoint, rootHash string) error { - if !AmHostRoot() { - return GuestMount(squashfs, mountpoint) - } - err := HostMount(squashfs, mountpoint, rootHash) - if err == nil || rootHash != "" { - return err - } - return GuestMount(squashfs, mountpoint) -} - -func HostMount(squashfs string, mountpoint string, rootHash string) error { - fi, err := os.Stat(squashfs) - if err != nil { - return errors.WithStack(err) - } - - sblock, err := readSuperblock(squashfs) - if err != nil { - return err - } - - verityOffset, err := verityDataLocation(sblock) - if err != nil { - return err - } - - if verityOffset == uint64(fi.Size()) && rootHash != "" { +func VerityHostMount(squashfs string, mountpoint string, rootHash string, veritySize int64, verityOffset uint64) error { + if verityOffset == uint64(veritySize) && rootHash != "" { return errors.Errorf("asked for verity but no data present") } - if rootHash == "" && verityOffset != uint64(fi.Size()) { + if rootHash == "" && verityOffset != uint64(veritySize) { return errors.Errorf("verity data present but no root hash specified") } @@ -322,6 +208,7 @@ func HostMount(squashfs string, mountpoint string, rootHash string) error { loopDevNeedsClosedOnErr := false var loopDev losetup.Device + var err error // set up the verity device if necessary if rootHash != "" { @@ -449,36 +336,13 @@ func findLoopBackingVerity(device string) (int64, error) { return deviceNo, nil } -func Umount(mountpoint string) error { - mounts, err := mount.ParseMounts("/proc/self/mountinfo") - if err != nil { - return err - } - - // first, find the verity device that backs the mount - theMount, found := mounts.FindMount(mountpoint) - if !found { - return errors.Errorf("%s is not a mountpoint", mountpoint) - } - - err = unix.Unmount(mountpoint, 0) - if err != nil { - return errors.Wrapf(err, "failed unmounting %v", mountpoint) - } - - if _, err := os.Stat(theMount.Source); err != nil { - if os.IsNotExist(err) { - return nil - } - return errors.WithStack(err) - } - +func VerityUnmount(mountPath string) error { // was this a verity mount or a regular loopback mount? (if it's a // regular loopback mount, we detached it above, so need to do anything // special here; verity doesn't play as nicely) - if strings.HasSuffix(theMount.Source, veritySuffix) { + if strings.HasSuffix(mountPath, VeritySuffix) { // find the loop device that backs the verity device - deviceNo, err := findLoopBackingVerity(theMount.Source) + deviceNo, err := findLoopBackingVerity(mountPath) if err != nil { return err } @@ -488,7 +352,7 @@ func Umount(mountpoint string) error { // above). the cryptsetup API allows us to pass NULL for the crypt // device, but go-cryptsetup doesn't have a way to initialize a NULL // crypt device short of making the struct by hand like this. - err = (&cryptsetup.Device{}).Deactivate(theMount.Source) + err = (&cryptsetup.Device{}).Deactivate(mountPath) if err != nil { return errors.WithStack(err) } @@ -496,7 +360,7 @@ func Umount(mountpoint string) error { // finally, kill the loop dev err = loopDev.Detach() if err != nil { - return errors.Wrapf(err, "failed to detach loop dev for %v", theMount.Source) + return errors.Wrapf(err, "failed to detach loop dev for %v", mountPath) } } diff --git a/squashfs/verity_static.go b/pkg/verity/verity_static.go similarity index 94% rename from squashfs/verity_static.go rename to pkg/verity/verity_static.go index feac991..d95456c 100644 --- a/squashfs/verity_static.go +++ b/pkg/verity/verity_static.go @@ -1,7 +1,7 @@ //go:build static_build // +build static_build -package squashfs +package verity // cryptsetup's pkgconfig is broken (it does not set Requires.private or // Libs.private at all), so we do the LDLIBS for it by hand.