Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: can't use sudo command #2120

Merged
merged 2 commits into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/containerd/containerd/mount"
"github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"
)

var (
Expand Down Expand Up @@ -386,15 +387,17 @@ func (c *Container) SetSnapshotterMeta(mounts []mount.Mount) {
// GetSpecificBasePath accepts a given path, look for whether the path is exist
// within container, if has, returns container base path like BaseFS, if not, return empty string
func (c *Container) GetSpecificBasePath(path string) string {
// first try container BaseFS, it is a general view,
if utils.IsFileExist(filepath.Join(c.BaseFS, path)) {
return c.BaseFS
}

// then try lower and upper directory, since overlay filesystem support only.
for _, prefixPath := range c.Snapshotter.Data {
if utils.IsFileExist(filepath.Join(prefixPath, path)) {
return prefixPath
logrus.Debugf("GetSpecificBasePath, snapshotter data: (%v)", c.Snapshotter.Data)

// try lower and upper directory, since overlay filesystem support only.
for _, key := range []string{"MergedDir", "UpperDir", "LowerDir"} {
if prefixPath, ok := c.Snapshotter.Data[key]; ok && prefixPath != "" {
for _, p := range strings.Split(prefixPath, ":") {
absPath := filepath.Join(p, path)
if utils.IsFileExist(absPath) {
return absPath
}
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions daemon/mgr/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func createSpec(ctx context.Context, c *Container, specWrapper *SpecWrapper) err
if err != nil {
return errors.Wrapf(err, "failed to generate spec: %s", c.ID)
}

// fix https://github.com/opencontainers/runc/issues/705
// use ssh connect to container, can't use sudo command.
s.Process.NoNewPrivileges = false

specWrapper.s = s

s.Hostname = c.Config.Hostname.String()
Expand Down
42 changes: 39 additions & 3 deletions pkg/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/opencontainers/runc/libcontainer/user"
"github.com/sirupsen/logrus"
)

var (
Expand Down Expand Up @@ -45,13 +49,45 @@ type gidParser struct {
// Get accepts user and group slice, return valid uid, gid and additional gids.
// Through Get is a interface returns all user informations runtime-spec need,
// GetUser, GetIntegerID, GetAdditionalGids still can be used independently.
func Get(passwdPath, groupPath, user string, groups []string) (uint32, uint32, []uint32, error) {
uid, gid, err := GetUser(passwdPath, groupPath, user)
func Get(passwdPath, groupPath, username string, groups []string) (uint32, uint32, []uint32, error) {
logrus.Debugf("get users, passwd path: (%s), group path: (%s), username: (%s), groups: (%v)",
passwdPath, groupPath, username, groups)

if passwdPath == "" || groupPath == "" {
logrus.Warn("get passwd file or group file is nil")
}

passwdFile, err := os.Open(passwdPath)
if err == nil {
defer passwdFile.Close()
}

groupFile, err := os.Open(groupPath)
if err == nil {
defer groupFile.Close()
}

execUser, err := user.GetExecUser(username, nil, passwdFile, groupFile)
if err != nil {
return 0, 0, nil, err
}

return uid, gid, GetAdditionalGids(groups), nil
var addGroups []int
if len(groups) > 0 {
addGroups, err = user.GetAdditionalGroups(groups, groupFile)
if err != nil {
return 0, 0, nil, err
}
}
uid := uint32(execUser.Uid)
gid := uint32(execUser.Gid)
sgids := append(execUser.Sgids, addGroups...)
var additionalGids []uint32
for _, g := range sgids {
additionalGids = append(additionalGids, uint32(g))
}

return uid, gid, additionalGids, nil
}

// GetUser accepts user string like <uid|username>:<gid|groupname>, and transfers them to format valid uid:gid.
Expand Down