Skip to content

Commit 149f1e3

Browse files
committed
Fix exec user behavior
Signed-off-by: Daniel Canter <[email protected]>
1 parent 403164d commit 149f1e3

4 files changed

Lines changed: 31 additions & 10 deletions

File tree

internal/guest/runtime/hcsv2/container.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ func (c *Container) ExecProcess(ctx context.Context, process *oci.Process, conSe
6666
return -1, err
6767
}
6868

69+
// If the client provided a user for the container to run as, we want to have the exec run as this user as well
70+
// unless the exec's spec was explicitly set to a different user. If the Username field is filled in on the containers
71+
// spec, at this point that means the work to find a uid:gid pairing for this username has already been done, so simply
72+
// assign the uid:gid from the container.
73+
if process.User.Username != "" {
74+
// The exec provided a user string of it's own. Grab the uid:gid pairing for the string (if one exists).
75+
if err := setUserStr(&oci.Spec{Root: c.spec.Root, Process: process}, process.User.Username); err != nil {
76+
return -1, err
77+
}
78+
} else if c.spec.Process.User.Username != "" {
79+
process.User = c.spec.Process.User
80+
}
81+
6982
p, err := c.container.ExecProcess(process, stdioSet)
7083
if err != nil {
7184
stdioSet.Close()

internal/guest/runtime/hcsv2/sandbox_container.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,12 @@ func setupSandboxContainerSpec(ctx context.Context, id string, spec *oci.Spec) (
102102
return errors.Wrap(err, "failed to write sandbox resolv.conf")
103103
}
104104

105-
if userstr, ok := spec.Annotations["io.microsoft.lcow.userstr"]; ok {
106-
if err := setUserStr(spec, userstr); err != nil {
105+
// User.Username is generally only used on Windows, but as there's no (easy/fast at least) way to grab
106+
// a uid:gid pairing for a username string on the host, we need to defer whatever user string the
107+
// client provided in the guest. The username field is used as a temporary holding place until we
108+
// can perform this work here when we actually have the rootfs to inspect.
109+
if spec.Process.User.Username != "" {
110+
if err := setUserStr(spec, spec.Process.User.Username); err != nil {
107111
return err
108112
}
109113
}

internal/guest/runtime/hcsv2/spec.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ package hcsv2
55
import (
66
"context"
77
"fmt"
8-
"github.com/Microsoft/hcsshim/internal/log"
9-
"github.com/opencontainers/runc/libcontainer/devices"
108
"path/filepath"
119
"strconv"
1210
"strings"
1311

12+
"github.com/Microsoft/hcsshim/internal/log"
13+
"github.com/opencontainers/runc/libcontainer/devices"
1414
"github.com/opencontainers/runc/libcontainer/user"
1515
oci "github.com/opencontainers/runtime-spec/specs-go"
1616
"github.com/pkg/errors"
@@ -225,11 +225,5 @@ func applyAnnotationsToSpec(ctx context.Context, spec *oci.Spec) error {
225225
}
226226
}
227227

228-
// Check if we need to set non-default user
229-
if userstr, ok := spec.Annotations["io.microsoft.lcow.userstr"]; ok {
230-
if err := setUserStr(spec, userstr); err != nil {
231-
return err
232-
}
233-
}
234228
return nil
235229
}

internal/guest/runtime/hcsv2/workload_container.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ func setupWorkloadContainerSpec(ctx context.Context, sbid, id string, spec *oci.
161161
return err
162162
}
163163

164+
// User.Username is generally only used on Windows, but as there's no (easy/fast at least) way to grab
165+
// a uid:gid pairing for a username string on the host, we need to defer whatever user string the
166+
// client provided in the guest. The username field is used as a temporary holding place until we
167+
// can perform this work here when we actually have the rootfs to inspect.
168+
if spec.Process.User.Username != "" {
169+
if err := setUserStr(spec, spec.Process.User.Username); err != nil {
170+
return err
171+
}
172+
}
173+
164174
// Force the parent cgroup into our /containers root
165175
spec.Linux.CgroupsPath = "/containers/" + id
166176

0 commit comments

Comments
 (0)