|
| 1 | +package validate |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/opencontainers/runc/libcontainer/configs" |
| 7 | +) |
| 8 | + |
| 9 | +func init() { |
| 10 | + geteuid = func() int { return 1337 } |
| 11 | + getegid = func() int { return 7331 } |
| 12 | +} |
| 13 | + |
| 14 | +func rootlessConfig() *configs.Config { |
| 15 | + return &configs.Config{ |
| 16 | + Rootfs: "/var", |
| 17 | + Rootless: true, |
| 18 | + Namespaces: configs.Namespaces( |
| 19 | + []configs.Namespace{ |
| 20 | + {Type: configs.NEWUSER}, |
| 21 | + }, |
| 22 | + ), |
| 23 | + UidMappings: []configs.IDMap{ |
| 24 | + { |
| 25 | + HostID: geteuid(), |
| 26 | + ContainerID: 0, |
| 27 | + Size: 1, |
| 28 | + }, |
| 29 | + }, |
| 30 | + GidMappings: []configs.IDMap{ |
| 31 | + { |
| 32 | + HostID: getegid(), |
| 33 | + ContainerID: 0, |
| 34 | + Size: 1, |
| 35 | + }, |
| 36 | + }, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestValidateRootless(t *testing.T) { |
| 41 | + validator := New() |
| 42 | + |
| 43 | + config := rootlessConfig() |
| 44 | + if err := validator.Validate(config); err != nil { |
| 45 | + t.Errorf("Expected error to not occur: %+v", err) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/* rootlessMappings() */ |
| 50 | + |
| 51 | +func TestValidateRootlessUserns(t *testing.T) { |
| 52 | + validator := New() |
| 53 | + |
| 54 | + config := rootlessConfig() |
| 55 | + config.Namespaces = nil |
| 56 | + if err := validator.Validate(config); err == nil { |
| 57 | + t.Errorf("Expected error to occur if user namespaces not set") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestValidateRootlessMappingUid(t *testing.T) { |
| 62 | + validator := New() |
| 63 | + |
| 64 | + config := rootlessConfig() |
| 65 | + config.UidMappings = nil |
| 66 | + if err := validator.Validate(config); err == nil { |
| 67 | + t.Errorf("Expected error to occur if no uid mappings provided") |
| 68 | + } |
| 69 | + |
| 70 | + config = rootlessConfig() |
| 71 | + config.UidMappings[0].HostID = geteuid() + 1 |
| 72 | + if err := validator.Validate(config); err == nil { |
| 73 | + t.Errorf("Expected error to occur if geteuid() != mapped uid") |
| 74 | + } |
| 75 | + |
| 76 | + config = rootlessConfig() |
| 77 | + config.UidMappings[0].Size = 1024 |
| 78 | + if err := validator.Validate(config); err == nil { |
| 79 | + t.Errorf("Expected error to occur if more than one uid mapped") |
| 80 | + } |
| 81 | + |
| 82 | + config = rootlessConfig() |
| 83 | + config.UidMappings = append(config.UidMappings, configs.IDMap{ |
| 84 | + HostID: geteuid() + 1, |
| 85 | + ContainerID: 0, |
| 86 | + Size: 1, |
| 87 | + }) |
| 88 | + if err := validator.Validate(config); err == nil { |
| 89 | + t.Errorf("Expected error to occur if more than one uid extent mapped") |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestValidateRootlessMappingGid(t *testing.T) { |
| 94 | + validator := New() |
| 95 | + |
| 96 | + config := rootlessConfig() |
| 97 | + config.GidMappings = nil |
| 98 | + if err := validator.Validate(config); err == nil { |
| 99 | + t.Errorf("Expected error to occur if no gid mappings provided") |
| 100 | + } |
| 101 | + |
| 102 | + config = rootlessConfig() |
| 103 | + config.GidMappings[0].HostID = getegid() + 1 |
| 104 | + if err := validator.Validate(config); err == nil { |
| 105 | + t.Errorf("Expected error to occur if getegid() != mapped gid") |
| 106 | + } |
| 107 | + |
| 108 | + config = rootlessConfig() |
| 109 | + config.GidMappings[0].Size = 1024 |
| 110 | + if err := validator.Validate(config); err == nil { |
| 111 | + t.Errorf("Expected error to occur if more than one gid mapped") |
| 112 | + } |
| 113 | + |
| 114 | + config = rootlessConfig() |
| 115 | + config.GidMappings = append(config.GidMappings, configs.IDMap{ |
| 116 | + HostID: getegid() + 1, |
| 117 | + ContainerID: 0, |
| 118 | + Size: 1, |
| 119 | + }) |
| 120 | + if err := validator.Validate(config); err == nil { |
| 121 | + t.Errorf("Expected error to occur if more than one gid extent mapped") |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/* rootlessMount() */ |
| 126 | + |
| 127 | +func TestValidateRootlessMountUid(t *testing.T) { |
| 128 | + config := rootlessConfig() |
| 129 | + validator := New() |
| 130 | + |
| 131 | + config.Mounts = []*configs.Mount{ |
| 132 | + { |
| 133 | + Source: "devpts", |
| 134 | + Destination: "/dev/pts", |
| 135 | + Device: "devpts", |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + if err := validator.Validate(config); err != nil { |
| 140 | + t.Errorf("Expected error to not occur when uid= not set in mount options: %+v", err) |
| 141 | + } |
| 142 | + |
| 143 | + config.Mounts[0].Data = "uid=5" |
| 144 | + if err := validator.Validate(config); err == nil { |
| 145 | + t.Errorf("Expected error to occur when setting uid=5 in mount options") |
| 146 | + } |
| 147 | + |
| 148 | + config.Mounts[0].Data = "uid=0" |
| 149 | + if err := validator.Validate(config); err != nil { |
| 150 | + t.Errorf("Expected error to not occur when setting uid=0 in mount options: %+v", err) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +func TestValidateRootlessMountGid(t *testing.T) { |
| 155 | + config := rootlessConfig() |
| 156 | + validator := New() |
| 157 | + |
| 158 | + config.Mounts = []*configs.Mount{ |
| 159 | + { |
| 160 | + Source: "devpts", |
| 161 | + Destination: "/dev/pts", |
| 162 | + Device: "devpts", |
| 163 | + }, |
| 164 | + } |
| 165 | + |
| 166 | + if err := validator.Validate(config); err != nil { |
| 167 | + t.Errorf("Expected error to not occur when gid= not set in mount options: %+v", err) |
| 168 | + } |
| 169 | + |
| 170 | + config.Mounts[0].Data = "gid=5" |
| 171 | + if err := validator.Validate(config); err == nil { |
| 172 | + t.Errorf("Expected error to occur when setting gid=5 in mount options") |
| 173 | + } |
| 174 | + |
| 175 | + config.Mounts[0].Data = "gid=0" |
| 176 | + if err := validator.Validate(config); err != nil { |
| 177 | + t.Errorf("Expected error to not occur when setting gid=0 in mount options: %+v", err) |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +/* rootlessCgroup() */ |
| 182 | + |
| 183 | +func TestValidateRootlessCgroup(t *testing.T) { |
| 184 | + validator := New() |
| 185 | + |
| 186 | + config := rootlessConfig() |
| 187 | + config.Cgroups = &configs.Cgroup{ |
| 188 | + Resources: &configs.Resources{ |
| 189 | + PidsLimit: 1337, |
| 190 | + }, |
| 191 | + } |
| 192 | + if err := validator.Validate(config); err == nil { |
| 193 | + t.Errorf("Expected error to occur if cgroup limits set") |
| 194 | + } |
| 195 | +} |
0 commit comments