|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package firewall |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + cgroups "github.com/containerd/cgroups/v3" |
| 11 | +) |
| 12 | + |
| 13 | +// DetectCgroupVersion detects the cgroup version at runtime by checking |
| 14 | +// whether the system is using unified (v2) or legacy (v1) cgroup hierarchy. |
| 15 | +// This correctly handles: |
| 16 | +// - Jammy VM on Jammy host: Detects cgroup v1 |
| 17 | +// - Jammy container on Noble host: Detects cgroup v2 (inherits from host!) |
| 18 | +// - Noble anywhere: Detects cgroup v2 |
| 19 | +func DetectCgroupVersion() (CgroupVersion, error) { |
| 20 | + if cgroups.Mode() == cgroups.Unified { |
| 21 | + return CgroupV2, nil |
| 22 | + } |
| 23 | + return CgroupV1, nil |
| 24 | +} |
| 25 | + |
| 26 | +// GetProcessCgroup gets the cgroup identity for a process by reading /proc/<pid>/cgroup |
| 27 | +func GetProcessCgroup(pid int, version CgroupVersion) (ProcessCgroup, error) { |
| 28 | + cgroupFile := fmt.Sprintf("/proc/%d/cgroup", pid) |
| 29 | + data, err := os.ReadFile(cgroupFile) |
| 30 | + if err != nil { |
| 31 | + return ProcessCgroup{}, fmt.Errorf("reading %s: %w", cgroupFile, err) |
| 32 | + } |
| 33 | + |
| 34 | + if version == CgroupV2 { |
| 35 | + return parseCgroupV2(string(data)) |
| 36 | + } |
| 37 | + return parseCgroupV1(string(data)) |
| 38 | +} |
| 39 | + |
| 40 | +// parseCgroupV2 extracts the cgroup path from /proc/<pid>/cgroup for cgroup v2 |
| 41 | +// Format: "0::/system.slice/bosh-agent.service" |
| 42 | +func parseCgroupV2(data string) (ProcessCgroup, error) { |
| 43 | + for _, line := range strings.Split(data, "\n") { |
| 44 | + line = strings.TrimSpace(line) |
| 45 | + if strings.HasPrefix(line, "0::") { |
| 46 | + path := strings.TrimPrefix(line, "0::") |
| 47 | + return ProcessCgroup{ |
| 48 | + Version: CgroupV2, |
| 49 | + Path: path, |
| 50 | + }, nil |
| 51 | + } |
| 52 | + } |
| 53 | + return ProcessCgroup{}, fmt.Errorf("cgroup v2 path not found in /proc/self/cgroup") |
| 54 | +} |
| 55 | + |
| 56 | +// parseCgroupV1 extracts the cgroup info from /proc/<pid>/cgroup for cgroup v1 |
| 57 | +// Format: "12:net_cls,net_prio:/system.slice/bosh-agent.service" |
| 58 | +func parseCgroupV1(data string) (ProcessCgroup, error) { |
| 59 | + // Look for net_cls controller which is used for firewall matching |
| 60 | + for _, line := range strings.Split(data, "\n") { |
| 61 | + line = strings.TrimSpace(line) |
| 62 | + if strings.Contains(line, "net_cls") { |
| 63 | + parts := strings.SplitN(line, ":", 3) |
| 64 | + if len(parts) >= 3 { |
| 65 | + return ProcessCgroup{ |
| 66 | + Version: CgroupV1, |
| 67 | + Path: parts[2], |
| 68 | + // ClassID will be set when the process is added to the cgroup |
| 69 | + }, nil |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Fallback: return empty path, will use classid-based matching |
| 75 | + return ProcessCgroup{ |
| 76 | + Version: CgroupV1, |
| 77 | + }, nil |
| 78 | +} |
| 79 | + |
| 80 | +// ReadOperatingSystem reads the operating system name from the BOSH-managed file |
| 81 | +func ReadOperatingSystem() (string, error) { |
| 82 | + data, err := os.ReadFile("/var/vcap/bosh/etc/operating_system") |
| 83 | + if err != nil { |
| 84 | + return "", err |
| 85 | + } |
| 86 | + return strings.TrimSpace(string(data)), nil |
| 87 | +} |
0 commit comments