Skip to content

Commit

Permalink
internal: proc: switch to runtime-spec LinuxIDMapping types
Browse files Browse the repository at this point in the history
This will make it easier to use umoci's idtools package.

Signed-off-by: Aleksa Sarai <[email protected]>
  • Loading branch information
cyphar committed Jan 11, 2022
1 parent 14a400b commit 87c257b
Show file tree
Hide file tree
Showing 9 changed files with 984 additions and 41 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/opencontainers/runc v1.0.3
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ github.com/moby/sys/mountinfo v0.4.1/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2J
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
github.com/opencontainers/runc v1.0.3 h1:1hbqejyQWCJBvtKAfdO0b1FmaEf2z/bxnjqbARass5k=
github.com/opencontainers/runc v1.0.3/go.mod h1:aTaHFFwQXuA71CiyxOdFFIorAoemI04suvGRQFzWTD0=
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 h1:3snG66yBm59tKhhSPQrQ/0bCrv1LQbKt40LnUPiUxdc=
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down
16 changes: 6 additions & 10 deletions internal/proc/ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ import (
"fmt"
"io"
"os"
)

type IDMap struct {
ContainerID int
HostID int
Size int
}
rspec "github.com/opencontainers/runtime-spec/specs-go"
)

// ParsePIDNamespace returns the content of /proc/$pid/ns/pid.
func ParsePIDNamespace(pid string) (string, error) {
Expand All @@ -46,14 +42,14 @@ func ParseUserNamespace(pid string) (string, error) {
}

// ReadMappings reads the user namespace mappings at the specified path
func ReadMappings(path string) ([]IDMap, error) {
func ReadMappings(path string) ([]rspec.LinuxIDMapping, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

mappings := []IDMap{}
var mappings []rspec.LinuxIDMapping

buf := bufio.NewReader(file)
for {
Expand All @@ -68,10 +64,10 @@ func ReadMappings(path string) ([]IDMap, error) {
return mappings, nil
}

containerID, hostID, size := 0, 0, 0
var containerID, hostID, size uint32
if _, err := fmt.Sscanf(string(line), "%d %d %d", &containerID, &hostID, &size); err != nil {
return nil, fmt.Errorf("cannot parse %s: %w", string(line), err)
}
mappings = append(mappings, IDMap{ContainerID: containerID, HostID: hostID, Size: size})
mappings = append(mappings, rspec.LinuxIDMapping{ContainerID: containerID, HostID: hostID, Size: size})
}
}
39 changes: 8 additions & 31 deletions psgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,18 @@ import (
"github.com/containers/psgo/internal/dev"
"github.com/containers/psgo/internal/proc"
"github.com/containers/psgo/internal/process"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"golang.org/x/sys/unix"
)

// IDMap specifies a mapping range from the host to the container IDs.
type IDMap struct {
// ContainerID is the first ID in the container.
ContainerID int
// HostID is the first ID in the host.
HostID int
// Size specifies how long is the range. e.g. 1 means a single user
// is mapped.
Size int
}

// JoinNamespaceOpts specifies different options for joining the specified namespaces.
type JoinNamespaceOpts struct {
// UIDMap specifies a mapping for UIDs in the container. If specified
// huser will perform the reverse mapping.
UIDMap []IDMap
UIDMap []rspec.LinuxIDMapping
// GIDMap specifies a mapping for GIDs in the container. If specified
// hgroup will perform the reverse mapping.
GIDMap []IDMap
GIDMap []rspec.LinuxIDMapping

// FillMappings specified whether UIDMap and GIDMap must be initialized
// with the current user namespace.
Expand Down Expand Up @@ -102,7 +92,7 @@ type aixFormatDescriptor struct {
}

// findID converts the specified id to the host mapping
func findID(idStr string, mapping []IDMap, lookupFunc func(uid string) (string, error), overflowFile string) (string, error) {
func findID(idStr string, mapping []rspec.LinuxIDMapping, lookupFunc func(uid string) (string, error), overflowFile string) (string, error) {
if len(mapping) == 0 {
return idStr, nil
}
Expand All @@ -112,8 +102,8 @@ func findID(idStr string, mapping []IDMap, lookupFunc func(uid string) (string,
return "", fmt.Errorf("cannot parse ID: %w", err)
}
for _, m := range mapping {
if int(id) >= m.ContainerID && int(id) < m.ContainerID+m.Size {
user := fmt.Sprintf("%d", m.HostID+(int(id)-m.ContainerID))
if uint32(id) >= m.ContainerID && uint32(id) < m.ContainerID+m.Size {
user := fmt.Sprintf("%d", m.HostID+(uint32(id)-m.ContainerID))

return lookupFunc(user)
}
Expand Down Expand Up @@ -350,29 +340,16 @@ func JoinNamespaceAndProcessInfo(pid string, descriptors []string) ([][]string,
return JoinNamespaceAndProcessInfoWithOptions(pid, descriptors, &JoinNamespaceOpts{})
}

func readMappings(path string) ([]IDMap, error) {
mappings, err := proc.ReadMappings(path)
if err != nil {
return nil, err
}
var res []IDMap
for _, i := range mappings {
m := IDMap{ContainerID: i.ContainerID, HostID: i.HostID, Size: i.Size}
res = append(res, m)
}
return res, nil
}

func contextFromOptions(options *JoinNamespaceOpts) (*psContext, error) {
ctx := new(psContext)
ctx.opts = options
if ctx.opts != nil && ctx.opts.FillMappings {
uidMappings, err := readMappings("/proc/self/uid_map")
uidMappings, err := proc.ReadMappings("/proc/self/uid_map")
if err != nil {
return nil, err
}

gidMappings, err := readMappings("/proc/self/gid_map")
gidMappings, err := proc.ReadMappings("/proc/self/gid_map")
if err != nil {
return nil, err
}
Expand Down
191 changes: 191 additions & 0 deletions vendor/github.com/opencontainers/runtime-spec/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 87c257b

Please sign in to comment.