Skip to content

Commit

Permalink
Merge pull request #7125 from QiWang19/fd-validate
Browse files Browse the repository at this point in the history
validate fds --preserve-fds
  • Loading branch information
openshift-merge-robot authored Aug 5, 2020
2 parents 6260677 + 34e82f8 commit 4797190
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cmd/podman/containers/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/pkg/domain/entities"
envLib "github.com/containers/podman/v2/pkg/env"
"github.com/containers/podman/v2/pkg/rootless"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -110,6 +111,12 @@ func exec(_ *cobra.Command, args []string) error {

execOpts.Envs = envLib.Join(execOpts.Envs, cliEnv)

for fd := 3; fd < int(3+execOpts.PreserveFDs); fd++ {
if !rootless.IsFdInherited(fd) {
return errors.Errorf("file descriptor %d is not available - the preserve-fds option requires that file descriptors must be passed", fd)
}
}

if !execDetach {
streams := define.AttachStreams{}
streams.OutputStream = os.Stdout
Expand Down
5 changes: 5 additions & 0 deletions cmd/podman/containers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ func run(cmd *cobra.Command, args []string) error {
if err := createInit(cmd); err != nil {
return err
}
for fd := 3; fd < int(3+runOpts.PreserveFDs); fd++ {
if !rootless.IsFdInherited(fd) {
return errors.Errorf("file descriptor %d is not available - the preserve-fds option requires that file descriptors must be passed", fd)
}
}

imageName := args[0]
if !cliVals.RootFS {
Expand Down
10 changes: 10 additions & 0 deletions pkg/rootless/rootless_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ can_use_shortcut ()
return ret;
}

int
is_fd_inherited(int fd)
{
if (open_files_set == NULL || fd > open_files_max_fd || fd < 0)
{
return 0;
}
return FD_ISSET(fd % FD_SETSIZE, &(open_files_set[fd / FD_SETSIZE])) ? 1 : 0;
}

static void __attribute__((constructor)) init()
{
const char *xdg_runtime_dir;
Expand Down
6 changes: 6 additions & 0 deletions pkg/rootless/rootless_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern uid_t rootless_gid();
extern int reexec_in_user_namespace(int ready, char *pause_pid_file_path, char *file_to_read, int fd);
extern int reexec_in_user_namespace_wait(int pid, int options);
extern int reexec_userns_join(int pid, char *pause_pid_file_path);
extern int is_fd_inherited(int fd);
*/
import "C"

Expand Down Expand Up @@ -520,3 +521,8 @@ func ConfigurationMatches() (bool, error) {

return matches(GetRootlessGID(), gids, currentGIDs), nil
}

// IsFdInherited checks whether the fd is opened and valid to use
func IsFdInherited(fd int) bool {
return int(C.is_fd_inherited(C.int(fd))) > 0
}
5 changes: 5 additions & 0 deletions pkg/rootless/rootless_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ func GetConfiguredMappings() ([]idtools.IDMap, []idtools.IDMap, error) {
func ReadMappingsProc(path string) ([]idtools.IDMap, error) {
return nil, nil
}

// IsFdInherited checks whether the fd is opened and valid to use
func IsFdInherited(fd int) bool {
return false
}
7 changes: 7 additions & 0 deletions test/e2e/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,13 @@ USER mail`
Expect(session.ExitCode()).To(Equal(0))
})

It("podman run --preserve-fds invalid fd", func() {
session := podmanTest.Podman([]string{"run", "--preserve-fds", "2", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Not(Equal(0)))
Expect(session.ErrorToString()).To(ContainSubstring("file descriptor 3 is not available"))
})

It("podman run --privileged and --group-add", func() {
groupName := "kvm"
session := podmanTest.Podman([]string{"run", "-t", "-i", "--group-add", groupName, "--privileged", fedoraMinimal, "groups"})
Expand Down
1 change: 0 additions & 1 deletion test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ echo $rand | 0 | $rand

# 'run --preserve-fds' passes a number of additional file descriptors into the container
@test "podman run --preserve-fds" {
skip "enable this once #6653 is fixed"
skip_if_remote

content=$(random_string 20)
Expand Down

0 comments on commit 4797190

Please sign in to comment.