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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
package apparmor
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/lxc/incus/v6/internal/server/cgroup"
"github.com/lxc/incus/v6/internal/server/instance/drivers/edk2"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/project"
storageDrivers "github.com/lxc/incus/v6/internal/server/storage/drivers"
"github.com/lxc/incus/v6/internal/server/sys"
localUtil "github.com/lxc/incus/v6/internal/server/util"
internalUtil "github.com/lxc/incus/v6/internal/util"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/osarch"
"github.com/lxc/incus/v6/shared/util"
)
// Internal copy of the instance interface.
type instance interface {
Project() api.Project
Name() string
ExpandedConfig() map[string]string
Type() instancetype.Type
LogPath() string
RunPath() string
Path() string
DevicesPath() string
IsPrivileged() bool
}
// InstanceProfileName returns the instance's AppArmor profile name.
func InstanceProfileName(inst instance) string {
path := internalUtil.VarPath("")
name := fmt.Sprintf("%s_<%s>", project.Instance(inst.Project().Name, inst.Name()), path)
return profileName("", name)
}
// InstanceNamespaceName returns the instance's AppArmor namespace.
func InstanceNamespaceName(inst instance) string {
// Unlike in profile names, / isn't an allowed character so replace with a -.
path := strings.ReplaceAll(strings.Trim(internalUtil.VarPath(""), "/"), "/", "-")
name := fmt.Sprintf("%s_<%s>", project.Instance(inst.Project().Name, inst.Name()), path)
return profileName("", name)
}
// instanceProfileFilename returns the name of the on-disk profile name.
func instanceProfileFilename(inst instance) string {
name := project.Instance(inst.Project().Name, inst.Name())
return profileName("", name)
}
// InstanceLoad ensures that the instances's policy is loaded into the kernel so the it can boot.
func InstanceLoad(sysOS *sys.OS, inst instance, extraBinaries []string) error {
if inst.Type() == instancetype.Container {
err := createNamespace(sysOS, InstanceNamespaceName(inst))
if err != nil {
return err
}
}
err := instanceProfileGenerate(sysOS, inst, extraBinaries)
if err != nil {
return err
}
err = loadProfile(sysOS, instanceProfileFilename(inst))
if err != nil {
return err
}
return nil
}
// InstanceUnload ensures that the instances's policy namespace is unloaded to free kernel memory.
// This does not delete the policy from disk or cache.
func InstanceUnload(sysOS *sys.OS, inst instance) error {
if inst.Type() == instancetype.Container {
err := deleteNamespace(sysOS, InstanceNamespaceName(inst))
if err != nil {
return err
}
}
err := unloadProfile(sysOS, InstanceProfileName(inst), instanceProfileFilename(inst))
if err != nil {
return err
}
return nil
}
// InstanceValidate generates the instance profile file and validates it.
func InstanceValidate(sysOS *sys.OS, inst instance, extraBinaries []string) error {
err := instanceProfileGenerate(sysOS, inst, extraBinaries)
if err != nil {
return err
}
return parseProfile(sysOS, instanceProfileFilename(inst))
}
// InstanceDelete removes the policy from cache/disk.
func InstanceDelete(sysOS *sys.OS, inst instance) error {
return deleteProfile(sysOS, InstanceProfileName(inst), instanceProfileFilename(inst))
}
// instanceProfileGenerate generates instance apparmor profile policy file.
func instanceProfileGenerate(sysOS *sys.OS, inst instance, extraBinaries []string) error {
/* In order to avoid forcing a profile parse (potentially slow) on
* every container start, let's use AppArmor's binary policy cache,
* which checks mtime of the files to figure out if the policy needs to
* be regenerated.
*
* Since it uses mtimes, we shouldn't just always write out our local
* AppArmor template; instead we should check to see whether the
* template is the same as ours. If it isn't we should write our
* version out so that the new changes are reflected and we definitely
* force a recompile.
*/
profile := filepath.Join(aaPath, "profiles", instanceProfileFilename(inst))
content, err := os.ReadFile(profile)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
updated, err := instanceProfile(sysOS, inst, extraBinaries)
if err != nil {
return err
}
if string(content) != string(updated) {
err = os.WriteFile(profile, []byte(updated), 0o600)
if err != nil {
return err
}
}
return nil
}
// instanceProfile generates the AppArmor profile template from the given instance.
func instanceProfile(sysOS *sys.OS, inst instance, extraBinaries []string) (string, error) {
// Prepare raw.apparmor.
rawContent := ""
rawApparmor, ok := inst.ExpandedConfig()["raw.apparmor"]
if ok {
for _, line := range strings.Split(strings.Trim(rawApparmor, "\n"), "\n") {
rawContent += fmt.Sprintf(" %s\n", line)
}
}
// Check for features.
unixSupported, err := parserSupports(sysOS, "unix")
if err != nil {
return "", err
}
usernsSupported, err := parserSupports(sysOS, "userns")
if err != nil {
return "", err
}
// Deref the extra binaries.
for i, entry := range extraBinaries {
fullPath, err := filepath.EvalSymlinks(entry)
if err != nil {
continue
}
extraBinaries[i] = fullPath
}
// Render the profile.
var sb *strings.Builder = &strings.Builder{}
if inst.Type() == instancetype.Container {
err = lxcProfileTpl.Execute(sb, map[string]any{
"extra_binaries": extraBinaries,
"feature_cgns": sysOS.CGInfo.Namespacing,
"feature_cgroup2": sysOS.CGInfo.Layout == cgroup.CgroupsUnified || sysOS.CGInfo.Layout == cgroup.CgroupsHybrid,
"feature_stacking": sysOS.AppArmorStacking && !sysOS.AppArmorStacked,
"feature_unix": unixSupported,
"feature_userns": usernsSupported,
"kernel_binfmt": util.IsFalseOrEmpty(inst.ExpandedConfig()["security.privileged"]) && sysOS.UnprivBinfmt,
"name": InstanceProfileName(inst),
"namespace": InstanceNamespaceName(inst),
"nesting": util.IsTrue(inst.ExpandedConfig()["security.nesting"]),
"raw": rawContent,
"unprivileged": util.IsFalseOrEmpty(inst.ExpandedConfig()["security.privileged"]) || sysOS.RunningInUserNS,
"zfs_delegation": !inst.IsPrivileged() && storageDrivers.ZFSSupportsDelegation() && util.PathExists("/dev/zfs"),
})
if err != nil {
return "", err
}
} else {
// AppArmor requires deref of all paths.
path, err := filepath.EvalSymlinks(inst.Path())
if err != nil {
return "", err
}
var edk2Paths []string
edk2Path, err := edk2.GetenvEdk2Path()
if err != nil {
return "", err
}
if edk2Path != "" {
edk2Path, err := filepath.EvalSymlinks(edk2Path)
if err != nil {
return "", err
}
edk2Paths = append(edk2Paths, edk2Path)
} else {
arch, err := osarch.ArchitectureGetLocalID()
if err == nil {
for _, installation := range edk2.GetArchitectureInstallations(arch) {
if util.PathExists(installation.Path) {
edk2Path, err := filepath.EvalSymlinks(installation.Path)
if err != nil {
return "", err
}
edk2Paths = append(edk2Paths, edk2Path)
}
}
}
}
agentPath := ""
if os.Getenv("INCUS_AGENT_PATH") != "" {
agentPath, err = filepath.EvalSymlinks(os.Getenv("INCUS_AGENT_PATH"))
if err != nil {
return "", err
}
}
execPath := localUtil.GetExecPath()
execPathFull, err := filepath.EvalSymlinks(execPath)
if err == nil {
execPath = execPathFull
}
// Extra (read-only) config paths.
extraConfig := []string{}
if util.PathExists("/etc/ceph") {
extraConfig = append(extraConfig, "/etc/ceph")
// See if default config points to another path.
if util.PathExists("/etc/ceph/ceph.conf") {
target, err := filepath.EvalSymlinks("/etc/ceph/ceph.conf")
if err == nil && target != "/etc/ceph/ceph.conf" {
extraConfig = append(extraConfig, filepath.Dir(target))
}
}
}
err = qemuProfileTpl.Execute(sb, map[string]any{
"devicesPath": inst.DevicesPath(),
"exePath": execPath,
"extra_config": extraConfig,
"extra_binaries": extraBinaries,
"libraryPath": strings.Split(os.Getenv("LD_LIBRARY_PATH"), ":"),
"logPath": inst.LogPath(),
"runPath": inst.RunPath(),
"name": InstanceProfileName(inst),
"path": path,
"raw": rawContent,
"edk2Paths": edk2Paths,
"agentPath": agentPath,
})
if err != nil {
return "", err
}
}
return sb.String(), nil
}
|