Path: blob/main/components/ws-daemon/pkg/cgroup/plugin_fuse_v2.go
2499 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package cgroup56import (7"context"8"path/filepath"910"github.com/opencontainers/runc/libcontainer/cgroups/ebpf"11"github.com/opencontainers/runc/libcontainer/cgroups/ebpf/devicefilter"12"github.com/opencontainers/runc/libcontainer/devices"13"golang.org/x/sys/unix"14"golang.org/x/xerrors"1516"github.com/gitpod-io/gitpod/common-go/log"17"github.com/gitpod-io/gitpod/ws-daemon/pkg/libcontainer/specconv"18)1920var (21fuseDeviceMajor int64 = 1022fuseDeviceMinor int64 = 22923)2425type FuseDeviceEnablerV2 struct{}2627func (c *FuseDeviceEnablerV2) Name() string { return "fuse-device-enabler-v2" }28func (c *FuseDeviceEnablerV2) Type() Version { return Version2 }2930func (c *FuseDeviceEnablerV2) Apply(ctx context.Context, opts *PluginOptions) error {31if val, ok := opts.Annotations["gitpod.io/fuse-device"]; ok && val == "false" {32return nil33}3435fullCgroupPath := filepath.Join(opts.BasePath, opts.CgroupPath)36log.WithField("cgroupPath", fullCgroupPath).Debug("configuring devices")3738cgroupFD, err := unix.Open(fullCgroupPath, unix.O_DIRECTORY|unix.O_RDONLY|unix.O_CLOEXEC, 0600)39if err != nil {40return xerrors.Errorf("cannot get directory fd for %s: %w", fullCgroupPath, err)41}42defer unix.Close(cgroupFD)4344insts, license, err := devicefilter.DeviceFilter(composeDeviceRules())45if err != nil {46return xerrors.Errorf("failed to generate device filter: %w", err)47}4849_, err = ebpf.LoadAttachCgroupDeviceFilter(insts, license, cgroupFD)50if err != nil {51return xerrors.Errorf("failed to attach cgroup device filter: %w", err)52}5354return nil55}5657func composeDeviceRules() []*devices.Rule {58denyAll := devices.Rule{59Type: 'a',60Permissions: "rwm",61Allow: false,62}6364allowFuse := devices.Rule{65Type: 'c',66Major: fuseDeviceMajor,67Minor: fuseDeviceMinor,68Permissions: "rwm",69Allow: true,70}7172deviceRules := make([]*devices.Rule, 0)73deviceRules = append(deviceRules, &denyAll, &allowFuse)74for _, device := range specconv.AllowedDevices {75deviceRules = append(deviceRules, &device.Rule)76}7778return deviceRules79}808182