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

Invoking scuba with pwd at or above /home/$USER (on the host) will chmod it to 0700 #219

Open
JonathonReinhart opened this issue Sep 10, 2023 · 3 comments
Labels
bug scubainit Relating to the scubainit binary which runs at container startup

Comments

@JonathonReinhart
Copy link
Owner

JonathonReinhart commented Sep 10, 2023

scubainit has code to create the user's home dir (in the container), originally the scubauser:
https://github.com/JonathonReinhart/scuba/blob/v2.11.0/scubainit/scubainit.c#L390-L409

It will unconditionally chmod(path, 0700) and chown(path, uid, gid).

Usually, this is not a problem because either:

  • scuba is invoked from outside /home/$USER (on the host)
    • The created home directory is in the ephemeral container overlay, so it doesn't matter what scuba does to it.
  • scuba is invoked from a subdirectory of /home/$USER (on the host)
    • The home directory was created in the container overlay by Docker as part of setting up the pwd bind mount, so it doesn't matter what scuba does to it.

But there is an unlikely corner-case:

  • scuba is invoked directory in the /home/$USER
    • In this case, /home/$USER is the pwd, so it is bind mounted exactly in the container (at the same path).
    • Thus, when the chmod(path, 0700) runs, it actually affects the directory on the host.
    • I guess this would also happen if scuba were invoked in /home...
$ chmod 755 /home/jreinhart
$ ls -ld /home/jreinhart/
drwxr-xr-x 61 jreinhart jreinhart 4096 Sep  9 22:59 /home/jreinhart/

$ cd /home/jreinhart/
$ scuba --image debian:11 exit
$ ls -ld /home/jreinhart/
drwx------ 61 jreinhart jreinhart 4096 Sep  9 22:59 /home/jreinhart/

Note: We say /home/$USER rather than "the home directory" because $HOME could point somewhere else entirely, and this problem only applies to /home/$USER because that's what scubainit is currently hard-coded to use. (#216 is not yet implemented.)

@JonathonReinhart
Copy link
Owner Author

There is some history to this code:

@JonathonReinhart
Copy link
Owner Author

JonathonReinhart commented Sep 10, 2023

My first thought was to simply remove the chown() call, since mkdir_p accepts a mode argument, which is used only if the directory is created. However:


When trying to identify a solution, I realized this problem also exists if scuba were invoked in /home (don't ask why anyone would do that):

  • mkdir_p() would create nothing, as /home/$USER (presumably) already exists.
  • chown() would modify the real (host) /home/$USER.

Going another step, if a user invoked scuba in /, this would also happen.

So I guess, before chowning the homedir, we need to answer the question:

  • Is this the real home directory (from the host)? STOP, don't chown
  • Or did docker create it as a parent for the pwd bind mount? PROCEED
  • Or did scubainit create it? PROCEED

How do we determine if the path comes from the host? It's not as simple as asking "is this a bind mount", as noted above. I think we need to ask "is this directory, or any directory above it a bind mount?"

@JonathonReinhart JonathonReinhart changed the title Invoking scuba in /home/$USER (on the host) will chmod it to 0700 Invoking scuba with pwd at or above /home/$USER (on the host) will chmod it to 0700 Sep 10, 2023
@JonathonReinhart
Copy link
Owner Author

Another weird corner case:

  • scuba executed in /home or /
  • real $HOMEDIR somewhere other than /home/$USER

When scubainit creates /home/$USER it will be created in the host /home/. Ugh.

Maybe we should just disallow running scuba in / or /home...

@JonathonReinhart JonathonReinhart added the scubainit Relating to the scubainit binary which runs at container startup label Jan 2, 2024
JonathonReinhart added a commit that referenced this issue Apr 8, 2024
This works if scubaroot = /home/$USER: is_bind_mount() detects the bind mount
because we're trying to mount the /jreinhart subdirectory of the /home/
mount:

  scubainit: ERROR - Error creating home dir: "/home/jreinhart" is on a bind-mount: /dev/mapper/magnum--vg-home[/jreinhart]

But it fails for scubaroot = /home: is_bind_mount() sees root="/" and
returns false.

I think there is simply no way to reliably detect a bind mount!

So now we might be to the point of either passing the list of
bind-mount volumes from scuba to scubainit, or by checking for this
situation in scuba instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug scubainit Relating to the scubainit binary which runs at container startup
Projects
None yet
Development

No branches or pull requests

1 participant