|
| 1 | +package rhel |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/containernetworking/plugins/plugins/main/tap/distro" |
| 6 | + "github.com/opencontainers/selinux/go-selinux" |
| 7 | + "os/exec" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "syscall" |
| 11 | +) |
| 12 | + |
| 13 | +var Rhel distro.Distro = CreateLink{} |
| 14 | + |
| 15 | +type CreateLink struct{} |
| 16 | + |
| 17 | +func (l CreateLink) CreateLink(tmpName string, mtu int, nsFd int, nsPath string, multique bool, mac string, owner int, group int) error { |
| 18 | + |
| 19 | + err := setContainerSeBool() |
| 20 | + if err != nil { |
| 21 | + return err |
| 22 | + } |
| 23 | + err = createSelinuxTap(tmpName, mtu, nsFd, nsPath, multique, mac, owner, group) |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + return nil |
| 28 | +} |
| 29 | + |
| 30 | +func setContainerSeBool() error { |
| 31 | + output, err := exec.Command("cat", "/sys/fs/selinux/booleans/container_use_devices").CombinedOutput() |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("failed to run getsebool command %s: %v", string(output), err) |
| 34 | + } |
| 35 | + if strings.Contains(string(output), "off") { |
| 36 | + output, err := exec.Command("setsebool", "-P", "container_use_devices", "true").CombinedOutput() |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("failed to run setsebool command %s: %v", string(output), err) |
| 39 | + } |
| 40 | + } |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +// Due to issues with the vishvananda/netlink library (fix pending) this method is using the ip tool to set up |
| 45 | +// the tap device. |
| 46 | +func createSelinuxTap(tmpName string, mtu int, nsFd int, nsPath string, multiqueue bool, mac string, owner int, group int) error { |
| 47 | + if err := selinux.SetExecLabel("system_u:system_r:container_t:s0"); err != nil { |
| 48 | + return fmt.Errorf("failed set socket label: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + minFDToCloseOnExec := 3 |
| 52 | + maxFDToCloseOnExec := 256 |
| 53 | + // we want to share the parent process std{in|out|err} - fds 0 through 2. |
| 54 | + // Since the FDs are inherited on fork / exec, we close on exec all others. |
| 55 | + for fd := minFDToCloseOnExec; fd < maxFDToCloseOnExec; fd++ { |
| 56 | + syscall.CloseOnExec(fd) |
| 57 | + } |
| 58 | + |
| 59 | + tapDeviceArgs := []string{"tuntap", "add", "mode", "tap", "name", tmpName} |
| 60 | + if multiqueue { |
| 61 | + tapDeviceArgs = append(tapDeviceArgs, "multi_queue") |
| 62 | + } |
| 63 | + |
| 64 | + if owner >= 0 { |
| 65 | + tapDeviceArgs = append(tapDeviceArgs, "user", strconv.Itoa(owner)) |
| 66 | + } |
| 67 | + if group >= 0 { |
| 68 | + tapDeviceArgs = append(tapDeviceArgs, "group", strconv.Itoa(group)) |
| 69 | + } |
| 70 | + output, err := exec.Command("ip", tapDeviceArgs...).CombinedOutput() |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("failed to run command %s: %v", output, err) |
| 73 | + } |
| 74 | + |
| 75 | + tapDeviceArgs = []string{"link", "set", tmpName} |
| 76 | + if mtu != 0 { |
| 77 | + tapDeviceArgs = append(tapDeviceArgs, "mtu", strconv.Itoa(mtu)) |
| 78 | + } |
| 79 | + if mac != "" { |
| 80 | + tapDeviceArgs = append(tapDeviceArgs, "address", mac) |
| 81 | + } |
| 82 | + output, err = exec.Command("ip", tapDeviceArgs...).CombinedOutput() |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("failed to run command %s: %v", output, err) |
| 85 | + } |
| 86 | + return nil |
| 87 | +} |
0 commit comments