-
Notifications
You must be signed in to change notification settings - Fork 424
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
%environment during 'singularity build' executed in host namespace instead of container namespace #1053
Comments
Just to make sure this is clear, my recipe contains:
|
And to add: |
can you give the whole recipe and build command so I can reproduce? |
Hi @vsoch , You need
And also
Then you run:
Then you will get:
My main issue here is that I do not understand why the environment is being sourced at all at this point. I just did a test, created a
and actually, running another:
I get:
I hope we all agree that's a fatal bug... |
Hi @vsoch , |
I can reproduce this (with sudo for build)
and then the source doesn't work
but if I change the line to
but not to get distracted! It is indeed the case that we are in user space during the environment section - I did the same and added a file to my host, and it's executed:
The good news is that we get the file we expected in the container. The bad news is that the build has complete access to the host, so a malicious user could share a build script that might execute something there and act maliciously (if they don't check). |
Yes, I can confirm that the file ends up at the correct place, and Apart from the malicious user, this also hinders a non-malicious user. Say I want to do (and actually I think I even want to do that!):
in my build definition, such that the constructed container will in the end always use the environment from the I don't see a clean workaround for that usecase, apart from fixing Singularity code. Only unclean solutions (like renaming or aliasing |
I'm thinking of things we can do to fix this. If we limit sourcing to during chroot, then it would find the right file under the new root (where the container is being built) but the variables sourced would be lost when we move to the next command no? Most users wouldn't like this, but I think we should not be sourcing the environment at build time - variables that are needed can be defined in |
I would agree the complete idea to run the
Since I am For me as user, this is (from the logical point of view) unexpected, undocumented, unsafe, reducing portability, and generally unwanted. So in my opinion, the best fix would be to just not do that during build. Apart from that...
I don't understand - why would they be lost? I did not read through singularity code too much yet, but I would expect the chroot is done once, and then all sections which run in there execute in the very same environment. |
In the case of having two different chroot it would be lost. I think we would just do the sourcing to go along with the primary session for %post. I am +1 about removing the sourcing, period, because I think it's more secure and trivial to ask users to define that is needed in %post. @gmkurtzer your thoughts on this? |
Of course. Is that how it's done in the code? If so, I would ask why - I don't see any safety or performance gain from that.
👍 |
@olifre This is a really good catch, thanks for bringing it to our attention. I agree with you both, that the default behavior should not be to source the |
also let's be sure to remove the same sourcing of the apps environment at runtime (which I created mimicking the original |
@bauerm97 this was not overlooked. It was consciously added some time ago by @gmkurtzer. The idea was the people could add variables in the I think that is a good idea and it works well for the most part. But it obviously fails if your As far as this being an unsafe bug... meh. I don't really think so. If you find some rando script on the internet and run it as root you could break your computer. Singularity recipe files are no exception. The One workaround would be to copy the contents of the files you are trying to source to |
@GodloveD For very simple variable definition, this might indeed be ok.
will lead to a completely unexpected, always unwanted, and certainly not documented mess. That's why I would propose to at least make this optional. Please let me turn this strange behaviour off. If this was discussed on slack with some user's, I don't care too much - if an open source project has to base decisions on some feedback collected on an invite-only platform thus excluding most of the users, I'd say something is going wrong in selecting the audience. Putting that aside, I would expect singularity follows common behaviour (i.e. "like docker"): Don't allow access to things from the host (or at least outside of the build directory) during build. I don't see how anybody would expect things to work as they work right now. To get some progress here, what I would propose is the following. This still works for the special users needing that strange behaviour, also works for me, and is not too hard to code.
This would run during both
This only runs at runtime, and is not executed during the build phase.
This obviously lets one section run during build, and the other during runtime later. I guess this is very easy and straightforward to implement. If you really already go for backward compatbility (I don't believe that - since you dropped complete commands and fully reworked behaviour with 2.4, and not all have been "deprecation-wrappered"), then the default could be to interpret Does that sound reasonable? |
Another alternative would be to offer a new section: EDIT: The more and more I think about it, the more reasonable this approach would be. |
Until something like this is offered, a viable workaround should be to add to
While this workaround for sure works very well, I don't consider it a "solution".
Please let me know what you think. |
I just hit another compelling example for why sourcing environment is a terrible idea. Take this runscript it fundamentally changed the entire |
I'm perfectly with you on this, there are very many reasons I think this is a terrible idea, and I don't really see how this could lead to expected behaviour. If there's some veto against it by the project leader / majority (reasons could be backwards compatibility, users relying on that strange behavior), my priority for alternative solutions would be:
|
If I read the changes correctly, this fixes neither the problem @vsoch mentioned, |
@olifre Yes you are correct. But it seems to me that the main issue here is that sourcing needs to happen in chroot. I forgot that @vsoch mentioned we need to change sourcing of env vars for the app sections as well (sorry v!). I'm trying to figure out how that code works right now. If it can be done easily with my method I'll just tack into onto my PR. But if it is complicated I see that @cclerget is already addressing those bits so I may just PR him some changes to his PR instead. As for turning off sourcing of the environment at build time, I think the most straightforward method would be to add a flag to the |
hey @GodloveD it's exactly the same, in the deffile-sections file, just go down to %appenv and you will see the same logic as for the main environment. @olifre it definitely still has some issues, but sourcing in chroot is minimally a start, and then we can look into if it works (or if we want further change). For example, for your main issue since the build is isolated from the host you wouldn't have unknowingly copied your host's files. @GodloveD here is the line of interest! https://github.com/singularityware/singularity/blob/development/libexec/bootstrap-scripts/deffile-sections.sh#L343 And I also like the idea of |
@vsoch Agreed, it's a start, but it still means (for me) that code would be executed which I don't want executed during build (even though it will not be harmful anymore, since at least it's the in-container code now). Still, it will also not fix your
I thinks that's a pretty strange idea. If I ship a build recipe e.g. via github in which the |
yes good call, I concur. |
@olifre some users want I'm not crazy about changing the sections in Singularity recipes to try to determine which behavior you want. This is deep enough in the weeds that many users will never even have to think about it. Why make them learn about all of this complexity for a few corner cases? |
Agreed.
This is why I suggested
If I have to pass that option to the singularity binary (and can't put that in the recipe), it means recipes essentially become useless, since they don't guarantee container state anymore - and Singularity Hub needs funny extensions.
I have a problem here. Can somebody show me any example when it's actually useful to share Examples for
Maybe there are good use cases, I just don't see any, please enlighten me. Also, in case
My problem is that I think this strange behaviour ( |
What if we use the same (simple) standard that we have to distinguish between
If a user needs to define an environment variable during build ( I understand that we had users that wanted the |
@vsoch Fully agreed. 👍 And to add, if the users are really against writing it twice, they can use my trick described in the previous comment. |
OK. So what I'm hearing is that:
Does that sum up the request? |
@GodloveD Exactly. And if any users complain here, feel free to ping me, I'm willing to take any blame that may arise, and help explaining why this was a bad idea. Additionally, I'd suggest to add some more explicit hints to the documentation of
This actually already describes the behaviour I am asking for, but maybe it needs to be stressed more that this really is for runtime environment only. And of course, one should mention this (slightly) backwards incompatible case in the release notes. Feel free to blame me whenever somebody complains ;-). |
Docs are updated. That looks perfecto @GodloveD ! http://singularity.lbl.gov/docs-recipes#environment I think if we look at this from the point of security, it's an easy answer. One approach leads to possibly bad behavior (that the user building may not be aware of) and the other is slightly more annoying sometimes, but is consistently likely to be more secure. @GodloveD yes, that's what I'm here for! |
This is all fixed. Thanks everyone! |
Hi, This documentation needs to be updated too : http://singularity.lbl.gov/docs-environment-metadata I was not aware of this modification. IMHO, I think we need compatibility with older recipes. For me, even if From a a syntax and a user point of view, I would prefer using flags This new behaviour is a bit confusing too... Rémy |
So if I need |
I think it would be most clear to just export the variables again in post. If you aren't sure what they are from some previous definition, then source the environment file. Keep in mind this is shell so you might want to use . instead of source explicitly. |
Are the environment file names in I observed some different behavior when building Docker images vs. Singularity images for the "same" HPC Container Maker recipe. The root cause turned out to be that an environment variable from the Docker base image was not set in the Singularity build context. So I'd like to add the following
|
As far as I can see, these days (2.5.2-dist build available from Debian Buster (testing)), it is not even possible to issue source commands in %post . Are you aware that there are compilers (AOCC suite, for example) that generate scripts consisting of "export" commands that have to be sourced prior to using them? If I cannot source the generated script during %post, I will also be unable to use this compiler to build dependencies of the containerised applicatoin during %post. I can think of two workarounds:
Both options seem like an overkill. Any suggestions? |
Version of Singularity:
2.4.
Building a "sandbox" type container using
-vvv
and-d
, I observe the following:According to this output,
%files
is executed first. Only then,%environment
is executed, however, it does not find the files that%files
claimed to have copied into the container.Checking in the container later, I find the files are present as expected.
Is the actual execution order different than the output messages?
Or, and that would be a heavy bug: Is
%environment
during build by any chance executed on the host machine, and only later executed inside the container?The text was updated successfully, but these errors were encountered: