|
1 | 1 | package parent |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "os" |
5 | 6 | "strconv" |
6 | 7 | "strings" |
7 | 8 |
|
8 | 9 | "github.com/moby/sys/mountinfo" |
9 | 10 | "github.com/sirupsen/logrus" |
| 11 | + "golang.org/x/sys/unix" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | func warnPropagation(propagation string) { |
@@ -57,3 +59,46 @@ func warnSysctl() { |
57 | 59 | } |
58 | 60 | } |
59 | 61 | } |
| 62 | + |
| 63 | +func warnOnChildStartFailure(childStartErr error) { |
| 64 | + if errors.Is(childStartErr, unix.EACCES) { |
| 65 | + // apparmor_restrict_unprivileged_userns is available since Ubuntu 23.10. |
| 66 | + // Enabled by default since Ubuntu 24.04. |
| 67 | + // https://github.com/containerd/nerdctl/issues/2847 |
| 68 | + b, err := os.ReadFile("/proc/sys/kernel/apparmor_restrict_unprivileged_userns") |
| 69 | + if err == nil { |
| 70 | + s := strings.TrimSpace(string(b)) |
| 71 | + i, err := strconv.ParseInt(s, 10, 64) |
| 72 | + if err != nil { |
| 73 | + logrus.WithError(err).Warnf("Failed to parse /proc/sys/kernel/apparmor_restrict_unprivileged_userns (%q)", s) |
| 74 | + } else if i == 1 { |
| 75 | + logrus.WithError(childStartErr).Warnf("This error might have happened because /proc/sys/kernel/apparmor_restrict_unprivileged_userns is set to 1") |
| 76 | + selfExe, err := os.Executable() |
| 77 | + if err != nil { |
| 78 | + selfExe = "/usr/local/bin/rootlesskit" |
| 79 | + logrus.WithError(err).Warnf("Failed to detect the path of the rootlesskit binary, assuming it to be %q", selfExe) |
| 80 | + } |
| 81 | + profileName := strings.ReplaceAll(strings.TrimPrefix(selfExe, "/"), "/", ".") |
| 82 | + const tmpl = ` |
| 83 | +
|
| 84 | +########## BEGIN ########## |
| 85 | +cat <<EOT | sudo tee "/etc/apparmor.d/%s" |
| 86 | +# ref: https://ubuntu.com/blog/ubuntu-23-10-restricted-unprivileged-user-namespaces |
| 87 | +abi <abi/4.0>, |
| 88 | +include <tunables/global> |
| 89 | +
|
| 90 | +%s flags=(unconfined) { |
| 91 | + userns, |
| 92 | +
|
| 93 | + # Site-specific additions and overrides. See local/README for details. |
| 94 | + include if exists <local/%s> |
| 95 | +} |
| 96 | +EOT |
| 97 | +sudo systemctl restart apparmor.service |
| 98 | +########## END ########## |
| 99 | +` |
| 100 | + logrus.Warnf("Hint: try running the following commands:\n"+tmpl+"\n", profileName, selfExe, profileName) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments