Skip to content

Images with an ENTRYPOINT produce a broken set of command line arguments for the container's entry point #885

Closed

Description

@lopukhov

Describe the bug

When testing different images to create containers with toolbox I have found that with some base images the start command of the container is not set correctly. When trying to enter to one of this containers the toolbox executable inside the container is not called.

I created an image based on parrotsec/core. When entering the toolbox the following error appears.

$ toolbox enter parrot
Error: invalid entry point PID of container parrot

When starting directly from podman, the output is the bash help output.

$ podman start --attach parrot
bash: --log-level: invalid option
Usage:	bash [GNU long option] [option] ...
	bash [GNU long option] [option] script-file ...
GNU long options:
	--debug
	--debugger
	--dump-po-strings
	--dump-strings
	--help
	--init-file
	--login
	--noediting
	--noprofile
	--norc
	--posix
	--pretty-print
	--rcfile
	--restricted
	--verbose
	--version
Shell options:
	-ilrsD or -c command or -O shopt_option		(invocation only)
	-abefhkmnptuvxBCHP or -o option

After inspecting the container, and comparing to another container with a working image, there is a difference in the start path.

$ podman --log-level debug inspect --format json --type container <container>

Not working container looks like:

{
  "Path": "/bin/sh",
  "Args": [
      "-c",
      "bash $@",
      "toolbox",
      "--log-level",
      "debug",
      "init-container",
      "--gid",
      "1000",
      "--home",
      "/home/lopukhv",
      "--shell",
      "/bin/bash",
      "--uid",
      "1000",
      "--user",
      "lopukhv",
      "--monitor-host"
  ]
}

Working container look like:

{
  "Path": "toolbox",
  "Args": [
      "--log-level",
      "debug",
      "init-container",
      "--gid",
      "1000",
      "--home",
      "/home/lopukhv",
      "--shell",
      "/bin/bash",
      "--uid",
      "1000",
      "--user",
      "lopukhv",
      "--monitor-host"
  ],
}

Steps how to reproduce the behaviour

  1. Build local image with provided Containerfile (podman build -t parrot .)
  2. Create a toolbox (toolbox create -i localhost/parrot parrot)
  3. Enter toolbox (toolbox enter parrot)
  4. Start container with podman (podman start --attach parrot)

Expected behaviour
Container should be created with the appropriate start command and should start normally.

Actual behaviour
Container is not created with the appropriate start command, it is not possible to enter the toolbox.

Output of toolbox --version (v0.0.90+)

toolbox version 0.0.99.2

Toolbox package info (rpm -q toolbox)
toolbox-0.0.99.2-7.fc34.x86_64

Output of podman version

Version:      3.3.1
API Version:  3.3.1
Go Version:   go1.16.6
Built:        Mon Aug 30 22:46:36 2021
OS/Arch:      linux/amd64

Podman package info (rpm -q podman)
podman-3.3.1-1.fc34.x86_64

Info about your OS
Fedora Workstation 34

Additional context

Containerfile

FROM docker.io/parrotsec/core

LABEL com.github.containers.toolbox="true" \
      name="parrot-toolbox" \
      version="rolling" \
      usage="This image is meant to be used with the toolbox command" \
      summary="Base image for creating Parrot Sec toolbox containers" \
      maintainer="lopukhov <[email protected]>"

# Install extra packages
COPY extra-packages /
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get -y install $(<extra-packages)
RUN rm /extra-packages

# Allow authentication with empty password, promptless
RUN sed -i '/^auth.*pam_unix.so/s/nullok_secure/try_first_pass nullok/' /etc/pam.d/common-auth

RUN	touch /etc/machine-id

CMD /bin/sh

extra-packages

bash
fish
zsh
git
libcap2-bin
libnss-myhostname 
sudo
unzip
zip

Activity

  1. added
    1. Bug
    on Sep 25, 2021
  2. debarshiray commented on Nov 17, 2021

    @debarshiray
    Member

    $ podman start --attach parrot
    bash: --log-level: invalid option

    This indicates that somehow you are using the POSIX shell implementation of Toolbox and mixing it up with the Go implementation.

    Only use the Go implementation. The other one is long gone. :)

  3. debarshiray commented on Nov 17, 2021

    @debarshiray
    Member

    Closing.

    Feel free to leave a comment or re-open if you have reason to believe otherwise.

  4. lopukhov commented on Nov 24, 2021

    @lopukhov
    Author

    Thank you for the response.

    I made sure that I was using the Go implementation of the tool.

    After some more debugging I think I have found out the root cause:

    When podman creates containers with a command like

    podman create <image> <command> <arg1> <arg2> ...

    With some images the command that gets assigned the following command

    <command> <arg1> <arg2> ...

    And with some other images the command that gets assigned is something like

    bash -c <command> <arg1> <arg2> ...

    I am not sure why it happens but with the Containerfile in my first post it generates the second one. This produces that <arg1> and <arg2> get passed to bash instead of <command>.

    But when podman creates containers with a command like

    podman create <image> "<command> <arg1> <arg2> ..."

    the command that gets assigned is something like

    bash -c "<command> <arg1> <arg2> ..."

    So the arguments get passed to <command> as intended. I believe that it should be a simple fix, passing entryPoint in stc/cmd/create.go as a string instead of as a slice of strings. I can create a PR this weekend if you think thats an appropiate solution.

    (Apologies if I was too verbose, I wanted to make sure my explanation was clear)

  5. lopukhov commented on Nov 24, 2021

    @lopukhov
    Author

    @debarshiray I am not sure how would I re-open the issue or if i am able to

  6. lopukhov commented on Nov 25, 2021

    @lopukhov
    Author

    Actually, a better solution would be to unset the entrypoint when creating the container. The other solution would include regressions with the Fedora images.

  7. debarshiray commented on Nov 26, 2021

    @debarshiray
    Member

    Ok, many thanks for the detailed explanation. I can reproduce it now.

    The problem originates from the docker.io/parrotsec/core:latest image that overrides the entry point of the image to be /bin/sh -c bash "$@". The idea is that the command gets appended to the entry point, and that's why we end up with such a strange command line.

    You can try to reset the entry point of the image to the default with something like:

    ENTRYPOINT ["/bin/sh", "-c"]
    

    We should document this in the README.md.

  8. lopukhov commented on Nov 28, 2021

    @lopukhov
    Author

    When changing the entry point in the Containerfile as you suggested the problem still persists. The command when starting becomes

    /bin/sh -c toolbox --log-level debug init-container ...

    Which passes the arguments (like init-container) to /bin/sh. So when starting the container with podman start -a <name>, it prints the help message from toolbox.

    toolbox - Tool for containerized command line environments on Linux
    
    Common commands are:
    create    Create a new toolbox container
    enter     Enter an existing toolbox container
    list      List all existing toolbox containers and images
    
    Go to https://github.com/containers/toolbox for further information.
    

    From what I have tested I see two solutions:

    1. Add the --entrypoint "" flag to the podman create ... command. This would help with any other image that also sets the entrypoint also. I have not noticed any impact to other images, but right now my testing has been a little bit limited.
    2. Add to the README.md in the image requirements the information that the entrypoint should not be set. And for this image in particular I can take it to upstream to see if it is possible to not set the entrypoint on base images.

    Do you see any other options? What do you see as the best option between 1 or 2?

  9. debarshiray commented on Nov 30, 2021

    @debarshiray
    Member

    You can try to reset the entry point of the image to the default with
    something like:

    Oops! I got that wrong. It should be:

    ENTRYPOINT []
    

    Note that there shouldn't be any space between the opening and closing brackets.

    podman inspect --type image ... doesn't show any entry point, and the created container works as expected.

    I had gotten a bit confused because I read somewhere that ENTRYPOINT defaults to sh -c, which doesn't look true, and confused myself over how the command line arguments get passed around. Sorry about that.

  10. debarshiray commented on Dec 1, 2021

    @debarshiray
    Member
    Add the --entrypoint "" flag to the podman create ... command. This
    

    would help with any other image that also sets the entrypoint also. I have
    not noticed any impact to other images, but right now my testing has
    been a little bit limited.

    Since Toolbox only works with OCI images that fulfill certain requirements, I feel tempted to put this burden on the image. ie., the image should ensure that there's no entry point.

    We can revisit this if it proves to be a problem in the long run.

    Add to the README.md in the image requirements the information that
    the entrypoint should not be set.

    I have done this now in #940

  11. changed the title [-]Incorrectly created containers with some custom images[/-] [+]Images with an ENTRYPOINT produce a broken set of command line arguments for the container's entry point[/+] on Dec 1, 2021
  12. lopukhov commented on Dec 3, 2021

    @lopukhov
    Author

    That works! I thought that ENTRYPOINT [] was not valid. I agree with you in putting the burden on the image. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    1. Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions