Capitalize [UG]idMappings as [UG]IDMappings

Signed-off-by: Francis Laniel <flaniel@linux.microsoft.com>
This commit is contained in:
Francis Laniel
2023-07-18 10:38:47 +02:00
parent b338accc78
commit c47f58c4e9
12 changed files with 61 additions and 61 deletions
+2 -2
View File
@@ -184,14 +184,14 @@ config := &configs.Config{
Flags: defaultMountFlags | unix.MS_RDONLY, Flags: defaultMountFlags | unix.MS_RDONLY,
}, },
}, },
UidMappings: []configs.IDMap{ UIDMappings: []configs.IDMap{
{ {
ContainerID: 0, ContainerID: 0,
HostID: 1000, HostID: 1000,
Size: 65536, Size: 65536,
}, },
}, },
GidMappings: []configs.IDMap{ GIDMappings: []configs.IDMap{
{ {
ContainerID: 0, ContainerID: 0,
HostID: 1000, HostID: 1000,
+4 -4
View File
@@ -159,11 +159,11 @@ type Config struct {
// More information about kernel oom score calculation here: https://lwn.net/Articles/317814/ // More information about kernel oom score calculation here: https://lwn.net/Articles/317814/
OomScoreAdj *int `json:"oom_score_adj,omitempty"` OomScoreAdj *int `json:"oom_score_adj,omitempty"`
// UidMappings is an array of User ID mappings for User Namespaces // UIDMappings is an array of User ID mappings for User Namespaces
UidMappings []IDMap `json:"uid_mappings"` UIDMappings []IDMap `json:"uid_mappings"`
// GidMappings is an array of Group ID mappings for User Namespaces // GIDMappings is an array of Group ID mappings for User Namespaces
GidMappings []IDMap `json:"gid_mappings"` GIDMappings []IDMap `json:"gid_mappings"`
// MaskPaths specifies paths within the container's rootfs to mask over with a bind // MaskPaths specifies paths within the container's rootfs to mask over with a bind
// mount pointing to /dev/null as to prevent reads of the file. // mount pointing to /dev/null as to prevent reads of the file.
+4 -4
View File
@@ -13,10 +13,10 @@ var (
// different when user namespaces are enabled. // different when user namespaces are enabled.
func (c Config) HostUID(containerId int) (int, error) { func (c Config) HostUID(containerId int) (int, error) {
if c.Namespaces.Contains(NEWUSER) { if c.Namespaces.Contains(NEWUSER) {
if c.UidMappings == nil { if c.UIDMappings == nil {
return -1, errNoUIDMap return -1, errNoUIDMap
} }
id, found := c.hostIDFromMapping(containerId, c.UidMappings) id, found := c.hostIDFromMapping(containerId, c.UIDMappings)
if !found { if !found {
return -1, errNoUserMap return -1, errNoUserMap
} }
@@ -36,10 +36,10 @@ func (c Config) HostRootUID() (int, error) {
// different when user namespaces are enabled. // different when user namespaces are enabled.
func (c Config) HostGID(containerId int) (int, error) { func (c Config) HostGID(containerId int) (int, error) {
if c.Namespaces.Contains(NEWUSER) { if c.Namespaces.Contains(NEWUSER) {
if c.GidMappings == nil { if c.GIDMappings == nil {
return -1, errNoGIDMap return -1, errNoGIDMap
} }
id, found := c.hostIDFromMapping(containerId, c.GidMappings) id, found := c.hostIDFromMapping(containerId, c.GIDMappings)
if !found { if !found {
return -1, errNoGroupMap return -1, errNoGroupMap
} }
+2 -2
View File
@@ -34,7 +34,7 @@ func TestHostRootUIDNoUSERNS(t *testing.T) {
func TestHostRootUIDWithUSERNS(t *testing.T) { func TestHostRootUIDWithUSERNS(t *testing.T) {
config := &Config{ config := &Config{
Namespaces: Namespaces{{Type: NEWUSER}}, Namespaces: Namespaces{{Type: NEWUSER}},
UidMappings: []IDMap{ UIDMappings: []IDMap{
{ {
ContainerID: 0, ContainerID: 0,
HostID: 1000, HostID: 1000,
@@ -67,7 +67,7 @@ func TestHostRootGIDNoUSERNS(t *testing.T) {
func TestHostRootGIDWithUSERNS(t *testing.T) { func TestHostRootGIDWithUSERNS(t *testing.T) {
config := &Config{ config := &Config{
Namespaces: Namespaces{{Type: NEWUSER}}, Namespaces: Namespaces{{Type: NEWUSER}},
GidMappings: []IDMap{ GIDMappings: []IDMap{
{ {
ContainerID: 0, ContainerID: 0,
HostID: 1000, HostID: 1000,
+4 -4
View File
@@ -42,10 +42,10 @@ func rootlessEUIDMappings(config *configs.Config) error {
return errors.New("rootless container requires user namespaces") return errors.New("rootless container requires user namespaces")
} }
if len(config.UidMappings) == 0 { if len(config.UIDMappings) == 0 {
return errors.New("rootless containers requires at least one UID mapping") return errors.New("rootless containers requires at least one UID mapping")
} }
if len(config.GidMappings) == 0 { if len(config.GIDMappings) == 0 {
return errors.New("rootless containers requires at least one GID mapping") return errors.New("rootless containers requires at least one GID mapping")
} }
return nil return nil
@@ -68,7 +68,7 @@ func rootlessEUIDMount(config *configs.Config) error {
// Ignore unknown mount options. // Ignore unknown mount options.
continue continue
} }
if !hasIDMapping(uid, config.UidMappings) { if !hasIDMapping(uid, config.UIDMappings) {
return errors.New("cannot specify uid= mount options for unmapped uid in rootless containers") return errors.New("cannot specify uid= mount options for unmapped uid in rootless containers")
} }
} }
@@ -79,7 +79,7 @@ func rootlessEUIDMount(config *configs.Config) error {
// Ignore unknown mount options. // Ignore unknown mount options.
continue continue
} }
if !hasIDMapping(gid, config.GidMappings) { if !hasIDMapping(gid, config.GIDMappings) {
return errors.New("cannot specify gid= mount options for unmapped gid in rootless containers") return errors.New("cannot specify gid= mount options for unmapped gid in rootless containers")
} }
} }
+13 -13
View File
@@ -16,14 +16,14 @@ func rootlessEUIDConfig() *configs.Config {
{Type: configs.NEWUSER}, {Type: configs.NEWUSER},
}, },
), ),
UidMappings: []configs.IDMap{ UIDMappings: []configs.IDMap{
{ {
HostID: 1337, HostID: 1337,
ContainerID: 0, ContainerID: 0,
Size: 1, Size: 1,
}, },
}, },
GidMappings: []configs.IDMap{ GIDMappings: []configs.IDMap{
{ {
HostID: 7331, HostID: 7331,
ContainerID: 0, ContainerID: 0,
@@ -52,7 +52,7 @@ func TestValidateRootlessEUIDUserns(t *testing.T) {
func TestValidateRootlessEUIDMappingUid(t *testing.T) { func TestValidateRootlessEUIDMappingUid(t *testing.T) {
config := rootlessEUIDConfig() config := rootlessEUIDConfig()
config.UidMappings = nil config.UIDMappings = nil
if err := Validate(config); err == nil { if err := Validate(config); err == nil {
t.Errorf("Expected error to occur if no uid mappings provided") t.Errorf("Expected error to occur if no uid mappings provided")
} }
@@ -60,7 +60,7 @@ func TestValidateRootlessEUIDMappingUid(t *testing.T) {
func TestValidateNonZeroEUIDMappingGid(t *testing.T) { func TestValidateNonZeroEUIDMappingGid(t *testing.T) {
config := rootlessEUIDConfig() config := rootlessEUIDConfig()
config.GidMappings = nil config.GIDMappings = nil
if err := Validate(config); err == nil { if err := Validate(config); err == nil {
t.Errorf("Expected error to occur if no gid mappings provided") t.Errorf("Expected error to occur if no gid mappings provided")
} }
@@ -93,15 +93,15 @@ func TestValidateRootlessEUIDMountUid(t *testing.T) {
} }
config.Mounts[0].Data = "uid=2" config.Mounts[0].Data = "uid=2"
config.UidMappings[0].Size = 10 config.UIDMappings[0].Size = 10
if err := Validate(config); err != nil { if err := Validate(config); err != nil {
t.Errorf("Expected error to not occur when setting uid=2 in mount options and UidMapping[0].size is 10") t.Errorf("Expected error to not occur when setting uid=2 in mount options and UIDMappings[0].size is 10")
} }
config.Mounts[0].Data = "uid=20" config.Mounts[0].Data = "uid=20"
config.UidMappings[0].Size = 10 config.UIDMappings[0].Size = 10
if err := Validate(config); err == nil { if err := Validate(config); err == nil {
t.Errorf("Expected error to occur when setting uid=20 in mount options and UidMapping[0].size is 10") t.Errorf("Expected error to occur when setting uid=20 in mount options and UIDMappings[0].size is 10")
} }
} }
@@ -130,21 +130,21 @@ func TestValidateRootlessEUIDMountGid(t *testing.T) {
} }
config.Mounts[0].Data = "gid=5" config.Mounts[0].Data = "gid=5"
config.GidMappings[0].Size = 10 config.GIDMappings[0].Size = 10
if err := Validate(config); err != nil { if err := Validate(config); err != nil {
t.Errorf("Expected error to not occur when setting gid=5 in mount options and GidMapping[0].size is 10") t.Errorf("Expected error to not occur when setting gid=5 in mount options and GIDMappings[0].size is 10")
} }
config.Mounts[0].Data = "gid=11" config.Mounts[0].Data = "gid=11"
config.GidMappings[0].Size = 10 config.GIDMappings[0].Size = 10
if err := Validate(config); err == nil { if err := Validate(config); err == nil {
t.Errorf("Expected error to occur when setting gid=11 in mount options and GidMapping[0].size is 10") t.Errorf("Expected error to occur when setting gid=11 in mount options and GIDMappings[0].size is 10")
} }
} }
func BenchmarkRootlessEUIDMount(b *testing.B) { func BenchmarkRootlessEUIDMount(b *testing.B) {
config := rootlessEUIDConfig() config := rootlessEUIDConfig()
config.GidMappings[0].Size = 10 config.GIDMappings[0].Size = 10
config.Mounts = []*configs.Mount{ config.Mounts = []*configs.Mount{
{ {
Source: "devpts", Source: "devpts",
+4 -4
View File
@@ -95,7 +95,7 @@ func namespaces(config *configs.Config) error {
return errors.New("USER namespaces aren't enabled in the kernel") return errors.New("USER namespaces aren't enabled in the kernel")
} }
} else { } else {
if config.UidMappings != nil || config.GidMappings != nil { if config.UIDMappings != nil || config.GIDMappings != nil {
return errors.New("User namespace mappings specified, but USER namespace isn't enabled in the config") return errors.New("User namespace mappings specified, but USER namespace isn't enabled in the config")
} }
} }
@@ -264,13 +264,13 @@ func checkIDMapMounts(config *configs.Config, m *configs.Mount) error {
if config.RootlessEUID { if config.RootlessEUID {
return fmt.Errorf("gidMappings/uidMappings is not supported when runc is being launched with EUID != 0, needs CAP_SYS_ADMIN on the runc parent's user namespace") return fmt.Errorf("gidMappings/uidMappings is not supported when runc is being launched with EUID != 0, needs CAP_SYS_ADMIN on the runc parent's user namespace")
} }
if len(config.UidMappings) == 0 || len(config.GidMappings) == 0 { if len(config.UIDMappings) == 0 || len(config.GIDMappings) == 0 {
return fmt.Errorf("not yet supported to use gidMappings/uidMappings in a mount without also using a user namespace") return fmt.Errorf("not yet supported to use gidMappings/uidMappings in a mount without also using a user namespace")
} }
if !sameMapping(config.UidMappings, m.UIDMappings) { if !sameMapping(config.UIDMappings, m.UIDMappings) {
return fmt.Errorf("not yet supported for the mount uidMappings to be different than user namespace uidMapping") return fmt.Errorf("not yet supported for the mount uidMappings to be different than user namespace uidMapping")
} }
if !sameMapping(config.GidMappings, m.GIDMappings) { if !sameMapping(config.GIDMappings, m.GIDMappings) {
return fmt.Errorf("not yet supported for the mount gidMappings to be different than user namespace gidMapping") return fmt.Errorf("not yet supported for the mount gidMappings to be different than user namespace gidMapping")
} }
if !filepath.IsAbs(m.Source) { if !filepath.IsAbs(m.Source) {
+17 -17
View File
@@ -192,7 +192,7 @@ func TestValidateUsernamespaceWithoutUserNS(t *testing.T) {
uidMap := configs.IDMap{ContainerID: 123} uidMap := configs.IDMap{ContainerID: 123}
config := &configs.Config{ config := &configs.Config{
Rootfs: "/var", Rootfs: "/var",
UidMappings: []configs.IDMap{uidMap}, UIDMappings: []configs.IDMap{uidMap},
} }
err := Validate(config) err := Validate(config)
@@ -405,8 +405,8 @@ func TestValidateIDMapMounts(t *testing.T) {
name: "idmap mount without bind opt specified", name: "idmap mount without bind opt specified",
isErr: true, isErr: true,
config: &configs.Config{ config: &configs.Config{
UidMappings: mapping, UIDMappings: mapping,
GidMappings: mapping, GIDMappings: mapping,
Mounts: []*configs.Mount{ Mounts: []*configs.Mount{
{ {
Source: "/abs/path/", Source: "/abs/path/",
@@ -422,8 +422,8 @@ func TestValidateIDMapMounts(t *testing.T) {
isErr: true, isErr: true,
config: &configs.Config{ config: &configs.Config{
RootlessEUID: true, RootlessEUID: true,
UidMappings: mapping, UIDMappings: mapping,
GidMappings: mapping, GIDMappings: mapping,
Mounts: []*configs.Mount{ Mounts: []*configs.Mount{
{ {
Source: "/abs/path/", Source: "/abs/path/",
@@ -454,8 +454,8 @@ func TestValidateIDMapMounts(t *testing.T) {
name: "idmap mounts with different userns and mount mappings", name: "idmap mounts with different userns and mount mappings",
isErr: true, isErr: true,
config: &configs.Config{ config: &configs.Config{
UidMappings: mapping, UIDMappings: mapping,
GidMappings: mapping, GIDMappings: mapping,
Mounts: []*configs.Mount{ Mounts: []*configs.Mount{
{ {
Source: "/abs/path/", Source: "/abs/path/",
@@ -477,8 +477,8 @@ func TestValidateIDMapMounts(t *testing.T) {
name: "idmap mounts with different userns and mount mappings", name: "idmap mounts with different userns and mount mappings",
isErr: true, isErr: true,
config: &configs.Config{ config: &configs.Config{
UidMappings: mapping, UIDMappings: mapping,
GidMappings: mapping, GIDMappings: mapping,
Mounts: []*configs.Mount{ Mounts: []*configs.Mount{
{ {
Source: "/abs/path/", Source: "/abs/path/",
@@ -500,8 +500,8 @@ func TestValidateIDMapMounts(t *testing.T) {
name: "idmap mounts without abs source path", name: "idmap mounts without abs source path",
isErr: true, isErr: true,
config: &configs.Config{ config: &configs.Config{
UidMappings: mapping, UIDMappings: mapping,
GidMappings: mapping, GIDMappings: mapping,
Mounts: []*configs.Mount{ Mounts: []*configs.Mount{
{ {
Source: "./rel/path/", Source: "./rel/path/",
@@ -517,8 +517,8 @@ func TestValidateIDMapMounts(t *testing.T) {
name: "idmap mounts without abs dest path", name: "idmap mounts without abs dest path",
isErr: true, isErr: true,
config: &configs.Config{ config: &configs.Config{
UidMappings: mapping, UIDMappings: mapping,
GidMappings: mapping, GIDMappings: mapping,
Mounts: []*configs.Mount{ Mounts: []*configs.Mount{
{ {
Source: "/abs/path/", Source: "/abs/path/",
@@ -534,8 +534,8 @@ func TestValidateIDMapMounts(t *testing.T) {
{ {
name: "simple idmap mount", name: "simple idmap mount",
config: &configs.Config{ config: &configs.Config{
UidMappings: mapping, UIDMappings: mapping,
GidMappings: mapping, GIDMappings: mapping,
Mounts: []*configs.Mount{ Mounts: []*configs.Mount{
{ {
Source: "/another-abs/path/", Source: "/another-abs/path/",
@@ -550,8 +550,8 @@ func TestValidateIDMapMounts(t *testing.T) {
{ {
name: "idmap mount with more flags", name: "idmap mount with more flags",
config: &configs.Config{ config: &configs.Config{
UidMappings: mapping, UIDMappings: mapping,
GidMappings: mapping, GIDMappings: mapping,
Mounts: []*configs.Mount{ Mounts: []*configs.Mount{
{ {
Source: "/another-abs/path/", Source: "/another-abs/path/",
+5 -5
View File
@@ -2219,7 +2219,7 @@ func (c *Container) bootstrapData(cloneFlags uintptr, nsMaps map[configs.Namespa
_, joinExistingUser := nsMaps[configs.NEWUSER] _, joinExistingUser := nsMaps[configs.NEWUSER]
if !joinExistingUser { if !joinExistingUser {
// write uid mappings // write uid mappings
if len(c.config.UidMappings) > 0 { if len(c.config.UIDMappings) > 0 {
if c.config.RootlessEUID { if c.config.RootlessEUID {
// We resolve the paths for new{u,g}idmap from // We resolve the paths for new{u,g}idmap from
// the context of runc to avoid doing a path // the context of runc to avoid doing a path
@@ -2231,7 +2231,7 @@ func (c *Container) bootstrapData(cloneFlags uintptr, nsMaps map[configs.Namespa
}) })
} }
} }
b, err := encodeIDMapping(c.config.UidMappings) b, err := encodeIDMapping(c.config.UIDMappings)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -2242,8 +2242,8 @@ func (c *Container) bootstrapData(cloneFlags uintptr, nsMaps map[configs.Namespa
} }
// write gid mappings // write gid mappings
if len(c.config.GidMappings) > 0 { if len(c.config.GIDMappings) > 0 {
b, err := encodeIDMapping(c.config.GidMappings) b, err := encodeIDMapping(c.config.GIDMappings)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -2356,5 +2356,5 @@ func requiresRootOrMappingTool(c *configs.Config) bool {
gidMap := []configs.IDMap{ gidMap := []configs.IDMap{
{ContainerID: 0, HostID: os.Getegid(), Size: 1}, {ContainerID: 0, HostID: os.Getegid(), Size: 1},
} }
return !reflect.DeepEqual(c.GidMappings, gidMap) return !reflect.DeepEqual(c.GIDMappings, gidMap)
} }
+2 -2
View File
@@ -1774,8 +1774,8 @@ func TestBindMountAndUser(t *testing.T) {
}) })
// Set HostID to 1000 to avoid DAC_OVERRIDE bypassing the purpose of this test. // Set HostID to 1000 to avoid DAC_OVERRIDE bypassing the purpose of this test.
config.UidMappings[0].HostID = 1000 config.UIDMappings[0].HostID = 1000
config.GidMappings[0].HostID = 1000 config.GIDMappings[0].HostID = 1000
// Set the owner of rootfs to the effective IDs in the host to avoid errors // Set the owner of rootfs to the effective IDs in the host to avoid errors
// while creating the folders to perform the mounts. // while creating the folders to perform the mounts.
+2 -2
View File
@@ -193,8 +193,8 @@ func newTemplateConfig(t *testing.T, p *tParam) *configs.Config {
} }
if p.userns { if p.userns {
config.UidMappings = []configs.IDMap{{HostID: 0, ContainerID: 0, Size: 1000}} config.UIDMappings = []configs.IDMap{{HostID: 0, ContainerID: 0, Size: 1000}}
config.GidMappings = []configs.IDMap{{HostID: 0, ContainerID: 0, Size: 1000}} config.GIDMappings = []configs.IDMap{{HostID: 0, ContainerID: 0, Size: 1000}}
config.Namespaces = append(config.Namespaces, configs.Namespace{Type: configs.NEWUSER}) config.Namespaces = append(config.Namespaces, configs.Namespace{Type: configs.NEWUSER})
} else { } else {
config.Mounts = append(config.Mounts, &configs.Mount{ config.Mounts = append(config.Mounts, &configs.Mount{
+2 -2
View File
@@ -945,8 +945,8 @@ next:
func setupUserNamespace(spec *specs.Spec, config *configs.Config) error { func setupUserNamespace(spec *specs.Spec, config *configs.Config) error {
if spec.Linux != nil { if spec.Linux != nil {
config.UidMappings = toConfigIDMap(spec.Linux.UIDMappings) config.UIDMappings = toConfigIDMap(spec.Linux.UIDMappings)
config.GidMappings = toConfigIDMap(spec.Linux.GIDMappings) config.GIDMappings = toConfigIDMap(spec.Linux.GIDMappings)
} }
rootUID, err := config.HostRootUID() rootUID, err := config.HostRootUID()
if err != nil { if err != nil {