1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
//go:build !windows
// +build !windows
package preflight
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
"github.com/crc-org/crc/v2/pkg/crc/cache"
"github.com/crc-org/crc/v2/pkg/crc/constants"
"github.com/crc-org/crc/v2/pkg/crc/logging"
crcpreset "github.com/crc-org/crc/v2/pkg/crc/preset"
"github.com/crc-org/crc/v2/pkg/crc/version"
crcos "github.com/crc-org/crc/v2/pkg/os"
"github.com/pkg/errors"
)
var nonWinPreflightChecks = []Check{
{
configKeySuffix: "check-root-user",
checkDescription: "Checking if running as non-root",
check: checkIfRunningAsNormalUser,
fixDescription: "crc should not be run as root",
flags: NoFix,
// no need for an "os" label as this is only built on relevant OSes through the use of golang build tags
labels: None,
},
}
func genericPreflightChecks(_ crcpreset.Preset) []Check {
return []Check{
{
configKeySuffix: "check-admin-helper-cached",
checkDescription: "Checking if crc-admin-helper executable is cached",
check: checkAdminHelperExecutableCached,
fixDescription: "Caching crc-admin-helper executable",
fix: fixAdminHelperExecutableCached,
labels: None,
},
{
configKeySuffix: "check-supported-cpu-arch",
checkDescription: "Checking if running on a supported CPU architecture",
check: checkSupportedCPUArch,
fixDescription: "CRC is only supported on AMD64/Intel64 hardware",
flags: NoFix,
labels: None,
},
{
configKeySuffix: "check-crc-symlink",
checkDescription: "Checking if crc executable symlink exists",
check: checkCrcSymlink,
fixDescription: "Creating symlink for crc executable",
fix: fixCrcSymlink,
cleanupDescription: "Removing crc executable symlink",
cleanup: removeCrcSymlink,
labels: None,
},
}
}
func checkIfRunningAsNormalUser() error {
if os.Geteuid() != 0 {
return nil
}
logging.Debug("Ran as root")
return errors.New("crc should not be run as root")
}
func setSuid(path string) error {
logging.Debugf("Making %s suid", path)
stdOut, stdErr, err := crcos.RunPrivileged(fmt.Sprintf("Changing ownership of %s", path), "chown", "root", path)
if err != nil {
return fmt.Errorf("Unable to set ownership of %s to root: %s %v: %s",
path, stdOut, err, stdErr)
}
/* Can't do this before the chown as the chown will reset the suid bit */
stdOut, stdErr, err = crcos.RunPrivileged(fmt.Sprintf("Setting suid for %s", path), "chmod", "u+s,g+x", path)
if err != nil {
return fmt.Errorf("Unable to set suid bit on %s: %s %v: %s", path, stdOut, err, stdErr)
}
return nil
}
func checkSuid(path string) error {
fi, err := os.Stat(path)
if err != nil {
return err
}
if fi.Mode()&os.ModeSetuid == 0 {
return fmt.Errorf("%s does not have the SUID bit set (%s)", path, fi.Mode().String())
}
if fi.Sys().(*syscall.Stat_t).Uid != 0 {
return fmt.Errorf("%s is not owned by root", path)
}
return nil
}
// Check if helper executable is cached or not
func checkAdminHelperExecutableCached() error {
if version.IsInstaller() {
return nil
}
helper := cache.NewAdminHelperCache()
if !helper.IsCached() {
return errors.New("crc-admin-helper executable is not cached")
}
if err := helper.CheckVersion(); err != nil {
return errors.Wrap(err, "unexpected version of the crc-admin-helper executable")
}
logging.Debug("crc-admin-helper executable already cached")
return checkSuid(helper.GetExecutablePath())
}
func fixAdminHelperExecutableCached() error {
if version.IsInstaller() {
return nil
}
helper := cache.NewAdminHelperCache()
if err := helper.EnsureIsCached(); err != nil {
return errors.Wrap(err, "Unable to download crc-admin-helper executable")
}
logging.Debug("crc-admin-helper executable cached")
return setSuid(helper.GetExecutablePath())
}
func checkSupportedCPUArch() error {
logging.Debugf("GOARCH is %s GOOS is %s", runtime.GOARCH, runtime.GOOS)
// Only supported arches are amd64, and arm64 on macOS
if runtime.GOARCH == "amd64" || (runtime.GOARCH == "arm64" && runtime.GOOS == "darwin") {
return nil
}
return fmt.Errorf("CRC can only run on AMD64/Intel64 CPUs and Apple silicon")
}
func runtimeExecutablePath() (string, error) {
path, err := exec.LookPath(os.Args[0])
if err != nil {
// os.Args[0] is not in $PATH, crc must have been started by specifying the path to its binary
path = os.Args[0]
}
path, err = filepath.Abs(path)
if err != nil {
return "", err
}
return filepath.EvalSymlinks(path)
}
func checkCrcSymlink() error {
runtimePath, err := runtimeExecutablePath()
if err != nil {
return err
}
symlinkPath, err := filepath.EvalSymlinks(constants.CrcSymlinkPath)
if err != nil {
return err
}
if symlinkPath != runtimePath {
return fmt.Errorf("%s points to %s, not to %s", constants.CrcSymlinkPath, symlinkPath, runtimePath)
}
return nil
}
func fixCrcSymlink() error {
_ = os.Remove(constants.CrcSymlinkPath)
runtimePath, err := runtimeExecutablePath()
if err != nil {
return err
}
logging.Debugf("symlinking %s to %s", runtimePath, constants.CrcSymlinkPath)
return os.Symlink(runtimePath, constants.CrcSymlinkPath)
}
func removeCrcSymlink() error {
if crcos.FileExists(constants.CrcSymlinkPath) {
return os.Remove(constants.CrcSymlinkPath)
}
return nil
}
|