Don't fail if /etc/passwd or /etc/group does not exists

Container images can be created without passwd or group file, currently
if one of these containers gets run with a --user flag the container blows
up complaining about t a missing /etc/passwd file.

We just need to check if the error on read is ENOEXIST then allow the
read to return, not fail.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
Daniel J Walsh
2018-11-06 06:26:35 -05:00
parent 48914d67ae
commit ae68bec75c
2 changed files with 7 additions and 5 deletions

View File

@ -5,7 +5,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/opencontainers/runc/libcontainer/user"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
@ -25,6 +24,7 @@ import (
"github.com/containers/storage/pkg/archive" "github.com/containers/storage/pkg/archive"
"github.com/containers/storage/pkg/chrootarchive" "github.com/containers/storage/pkg/chrootarchive"
"github.com/containers/storage/pkg/mount" "github.com/containers/storage/pkg/mount"
"github.com/opencontainers/runc/libcontainer/user"
spec "github.com/opencontainers/runtime-spec/specs-go" spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate" "github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/selinux/go-selinux/label" "github.com/opencontainers/selinux/go-selinux/label"
@ -1069,7 +1069,7 @@ func (c *Container) generatePasswd() (string, error) {
} }
originPasswdFile := filepath.Join(c.state.Mountpoint, "/etc/passwd") originPasswdFile := filepath.Join(c.state.Mountpoint, "/etc/passwd")
orig, err := ioutil.ReadFile(originPasswdFile) orig, err := ioutil.ReadFile(originPasswdFile)
if err != nil { if err != nil && !os.IsNotExist(err) {
return "", errors.Wrapf(err, "unable to read passwd file %s", originPasswdFile) return "", errors.Wrapf(err, "unable to read passwd file %s", originPasswdFile)
} }

View File

@ -1,10 +1,12 @@
package lookup package lookup
import ( import (
"os"
"strconv"
"github.com/cyphar/filepath-securejoin" "github.com/cyphar/filepath-securejoin"
"github.com/opencontainers/runc/libcontainer/user" "github.com/opencontainers/runc/libcontainer/user"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"strconv"
) )
const ( const (
@ -116,7 +118,7 @@ func GetUser(containerMount, userIDorName string) (*user.User, error) {
} }
return u.Uid == uid return u.Uid == uid
}) })
if err != nil { if err != nil && !os.IsNotExist(err) {
return nil, err return nil, err
} }
if len(users) > 0 { if len(users) > 0 {
@ -146,7 +148,7 @@ func GetGroup(containerMount, groupIDorName string) (*user.Group, error) {
} }
return g.Gid == gid return g.Gid == gid
}) })
if err != nil { if err != nil && !os.IsNotExist(err) {
return nil, err return nil, err
} }
if len(groups) > 0 { if len(groups) > 0 {