123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package apparmor
- import (
- "bufio"
- "errors"
- "fmt"
- "io/ioutil"
- "os"
- "path"
- "strings"
- "k8s.io/api/core/v1"
- utilfeature "k8s.io/apiserver/pkg/util/feature"
- podutil "k8s.io/kubernetes/pkg/api/v1/pod"
- "k8s.io/kubernetes/pkg/features"
- kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
- utilpath "k8s.io/utils/path"
- )
- var isDisabledBuild bool
- type Validator interface {
- Validate(pod *v1.Pod) error
- ValidateHost() error
- }
- func NewValidator(runtime string) Validator {
- if err := validateHost(runtime); err != nil {
- return &validator{validateHostErr: err}
- }
- appArmorFS, err := getAppArmorFS()
- if err != nil {
- return &validator{
- validateHostErr: fmt.Errorf("error finding AppArmor FS: %v", err),
- }
- }
- return &validator{
- appArmorFS: appArmorFS,
- }
- }
- type validator struct {
- validateHostErr error
- appArmorFS string
- }
- func (v *validator) Validate(pod *v1.Pod) error {
- if !isRequired(pod) {
- return nil
- }
- if v.ValidateHost() != nil {
- return v.validateHostErr
- }
- loadedProfiles, err := v.getLoadedProfiles()
- if err != nil {
- return fmt.Errorf("could not read loaded profiles: %v", err)
- }
- var retErr error
- podutil.VisitContainers(&pod.Spec, func(container *v1.Container) bool {
- retErr = validateProfile(GetProfileName(pod, container.Name), loadedProfiles)
- if retErr != nil {
- return false
- }
- return true
- })
- return retErr
- }
- func (v *validator) ValidateHost() error {
- return v.validateHostErr
- }
- func validateHost(runtime string) error {
-
- if !utilfeature.DefaultFeatureGate.Enabled(features.AppArmor) {
- return errors.New("AppArmor disabled by feature-gate")
- }
-
- if isDisabledBuild {
- return errors.New("binary not compiled for linux")
- }
-
- if !IsAppArmorEnabled() {
- return errors.New("AppArmor is not enabled on the host")
- }
-
- if runtime != kubetypes.DockerContainerRuntime && runtime != kubetypes.RemoteContainerRuntime {
- return fmt.Errorf("AppArmor is only enabled for 'docker' and 'remote' runtimes. Found: %q", runtime)
- }
- return nil
- }
- func validateProfile(profile string, loadedProfiles map[string]bool) error {
- if err := ValidateProfileFormat(profile); err != nil {
- return err
- }
- if strings.HasPrefix(profile, ProfileNamePrefix) {
- profileName := strings.TrimPrefix(profile, ProfileNamePrefix)
- if !loadedProfiles[profileName] {
- return fmt.Errorf("profile %q is not loaded", profileName)
- }
- }
- return nil
- }
- func ValidateProfileFormat(profile string) error {
- if profile == "" || profile == ProfileRuntimeDefault || profile == ProfileNameUnconfined {
- return nil
- }
- if !strings.HasPrefix(profile, ProfileNamePrefix) {
- return fmt.Errorf("invalid AppArmor profile name: %q", profile)
- }
- return nil
- }
- func (v *validator) getLoadedProfiles() (map[string]bool, error) {
- profilesPath := path.Join(v.appArmorFS, "profiles")
- profilesFile, err := os.Open(profilesPath)
- if err != nil {
- return nil, fmt.Errorf("failed to open %s: %v", profilesPath, err)
- }
- defer profilesFile.Close()
- profiles := map[string]bool{}
- scanner := bufio.NewScanner(profilesFile)
- for scanner.Scan() {
- profileName := parseProfileName(scanner.Text())
- if profileName == "" {
-
- continue
- }
- profiles[profileName] = true
- }
- return profiles, nil
- }
- func parseProfileName(profileLine string) string {
- modeIndex := strings.IndexRune(profileLine, '(')
- if modeIndex < 0 {
- return ""
- }
- return strings.TrimSpace(profileLine[:modeIndex])
- }
- func getAppArmorFS() (string, error) {
- mountsFile, err := os.Open("/proc/mounts")
- if err != nil {
- return "", fmt.Errorf("could not open /proc/mounts: %v", err)
- }
- defer mountsFile.Close()
- scanner := bufio.NewScanner(mountsFile)
- for scanner.Scan() {
- fields := strings.Fields(scanner.Text())
- if len(fields) < 3 {
-
- continue
- }
- if fields[2] == "securityfs" {
- appArmorFS := path.Join(fields[1], "apparmor")
- if ok, err := utilpath.Exists(utilpath.CheckFollowSymlink, appArmorFS); !ok {
- msg := fmt.Sprintf("path %s does not exist", appArmorFS)
- if err != nil {
- return "", fmt.Errorf("%s: %v", msg, err)
- }
- return "", errors.New(msg)
- }
- return appArmorFS, nil
- }
- }
- if err := scanner.Err(); err != nil {
- return "", fmt.Errorf("error scanning mounts: %v", err)
- }
- return "", errors.New("securityfs not found")
- }
- func IsAppArmorEnabled() bool {
- if _, err := os.Stat("/sys/kernel/security/apparmor"); err == nil && os.Getenv("container") == "" {
- if _, err = os.Stat("/sbin/apparmor_parser"); err == nil {
- buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
- return err == nil && len(buf) > 1 && buf[0] == 'Y'
- }
- }
- return false
- }
|