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

fix secret mounts for env vars when using chroot isolation #5544

Conversation

jonahbull
Copy link
Contributor

@jonahbull jonahbull commented May 23, 2024

What type of PR is this?

/kind bug

What this PR does / why we need it:

As discussed in #5185, attempting to build an image that uses environmental variables as build secrets fails with the message:

error running subprocess: remounting "/var/tmp/buildah3153589124/mnt/rootfs/run/secrets/MYSECRET" in mount namespace with flags 0x1 instead of 0x0: operation not permitted

In my particular use case, this is using the VFS storage driver and chroot isolation. I haven't been able to reproduce using other isolation types, not sure if other folks have.

As far as I can tell, the cause is that before #5083, when running with chroot isolation ro mounts like secrets from env vars would explicitly have the unix.MS_NOEXEC, unix.MS_NOSUID, unix.MS_NODEV flags set when they were remounted: https://github.com/containers/buildah/pull/5083/files#diff-c287fcc22d32f0775a98f65ec2db880f887c6c392f8e5ed6d7320a24560c1949L468.

Now when running with chroot isolation ro mounts like secrets from env vars are not getting those same flags set and so the remount operation fails. Specifically it looks like in this case we are missing the unix.MS_NOSUID and unix.MS_NODEV flags.

I think the flags aren't being set because when we calculate effectiveUnimportantFlags here, we end up with 0b100000. Forgive the possibly overly-verbose notes here, but I'm not well-versed in these kinds of bitwise operations so it was helpful for me to add as much detail as possible when debugging.

possibleImportantFlags := uintptr(unix.ST_NODEV | unix.ST_NOEXEC | unix.ST_NOSUID | unix.ST_RDONLY)
// fs.Flags: 0b100110 possibleImportantFlags: 0b1111 ^possibleImportantFlags: 0b10000
effectiveUnimportantFlags := uintptr(fs.Flags) & ^possibleImportantFlags
// effectiveUnimportantFlags: 0b100000

When we pass effectiveUnimportantFlags to mountFlagsForFSFlags, none of the possible fsFlags match 0b100000, so it returns 0b0.

This change adds special handling for read-only mounts when we need to do a remount to try to get the desired flags to stick. If we've requested a read-only mount (unix.ST_RDONLY is set in requestFlags), then we add any possibleImportantFlags that are set in fs.Flags to remountFlags so the remount operation doesn't fail because they are missing

How to verify it

I've added a test to bud.bats that reproduces the issue when run locally with a version of buildah that doesn't have the fix applied.

I could definitely have missed something, but this change appears to fix the issue in #5185 without breaking other use cases, at least from my limited local testing (running the bud.bats and chroot.bats suites).

Which issue(s) this PR fixes:

Fixes #5185

Special notes for your reviewer:

Does this PR introduce a user-facing change?

None

@openshift-ci openshift-ci bot added the kind/bug Categorizes issue or PR as related to a bug. label May 23, 2024
@jonahbull
Copy link
Contributor Author

Looks like my initial naive attempt doesn't quite work. I'll poke at getting the unit tests passing (they are failing on TestMounts/dev,exec,suid,rw) but if anyone who happens by has a different solution in mind I'm all ears.

@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 23, 2024
Before containers#5083, when running with chroot isolation ro mounts like secrets
from env vars would explicitly have the unix.MS_NOEXEC, unix.MS_NOSUID
and unix.MS_NODEV flags set when they were remounted. Now when running
with chroot isolation ro mounts like secrets from env vars are not
getting those same flags set and so the remount operation fails.
Specifically it looks like we are missing the unix.MS_NOSUID and
unix.MS_NODEV flags.

This change adds special handling for read-only mounts when we need to do
a remount to try to get the desired flags to stick. If we've requested
a read-only mount (unix.ST_RDONLY is set in requestFlags), then we add any
possibleImportantFlags that are set in fs.Flags to remountFlags so the remount
operation doesn't fail because they are missing. I've also added a test to
bud.bats that covers this case.

Signed-off-by: Jonah Bull <jonah.bull@elastic.co>
@jonahbull jonahbull force-pushed the fix-up-env-var-secret-mounts-for-chroot-isolation branch from 1ec9bc8 to 939a58b Compare May 25, 2024 20:51
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 25, 2024
@rhatdan
Copy link
Member

rhatdan commented May 27, 2024

Thanks @jonahbull
LGTM
/approve
@flouthoc @nalind @giuseppe @mheon PTAL

Copy link
Member

@giuseppe giuseppe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

Copy link
Contributor

openshift-ci bot commented May 27, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: giuseppe, jonahbull, rhatdan

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-bot openshift-merge-bot bot merged commit 6ad7efb into containers:main May 27, 2024
32 checks passed
@stale-locking-app stale-locking-app bot locked as resolved and limited conversation to collaborators Aug 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved kind/bug Categorizes issue or PR as related to a bug. lgtm locked - please file new issue/PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Image build with secret mounts stopped working v1.32
4 participants