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

llbsolver: fix policy rule ordering #4014

Merged
merged 1 commit into from
Jul 12, 2023

Conversation

tonistiigi
Copy link
Member

@tonistiigi tonistiigi commented Jul 12, 2023

The order of rules in policy matters. Eg. in [DENY *, ALLOW ref] mixing the order would deny all sources so map can't be used to deduplicate the rules.

@cpuguy83 @tianon

@tonistiigi
Copy link
Member Author

tonistiigi commented Jul 12, 2023

@cpuguy83 Was there a reason to deduplicate in this function? Other variants could be:

@tianon
Copy link
Member

tianon commented Jul 12, 2023

Nice find! 🎉 ❤️

Since they're order-dependent, can rewrite mid-chain, and have fixed limits on the number of, for example, converts that can happen during a single evaluation, deduplicating feels premature to me, but I definitely might be missing something important. 😅

@cpuguy83
Copy link
Member

I don't remember writing this at all. Wow.

err := b.EachValue(context.TODO(), keySourcePolicy, func(v interface{}) error {
x, ok := v.(spb.Policy)
if !ok {
return errors.Errorf("invalid source policy %T", v)
}
for _, f := range x.Rules {
set[*f] = struct{}{}
r := *f
srcPol.Rules = append(srcPol.Rules, &r)
Copy link
Member

Choose a reason for hiding this comment

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

I guess we should copy Version over as well?

Copy link
Member

Choose a reason for hiding this comment

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

sigh and rules have pointers in them I guess we need to dereference.

Copy link
Member Author

Choose a reason for hiding this comment

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

sigh and rules have pointers in them I guess we need to dereference.

Do we need to make sure these are copied? I just left it like this because that is what old code did?

Copy link
Member

Choose a reason for hiding this comment

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

This is probably fine since nothing is modifying the policies.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added Version just for completeness. Especially for the common case where EachVertex only returns one value. I think as a follow-up, returning the policy array would make more sense here.

The older of rules in policy matters. Eg. in [DENY *, ALLOW ref]
mixing the order would deny all sources so map can't be used
to deduplicate the rules.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
@tonistiigi tonistiigi merged commit 18fc875 into moby:master Jul 12, 2023
56 checks passed
@tianon
Copy link
Member

tianon commented Aug 3, 2023

It seems there might still be a race here somewhere -- I can reproduce it a lot less frequently with this change, but it does still come periodically:

$ printf 'FROM bash:latest' | EXPERIMENTAL_BUILDKIT_SOURCE_POLICY=<(jq -nc '{ rules: [ { action: "DENY", selector: { identifier: "*" } }, { action: "ALLOW", selector: { identifier: "local://dockerfile" } }, { action: "CONVERT", selector: { identifier: "docker-image://docker.io/library/bash:latest@*" }, updates: { identifier: "docker-image://docker.io/library/debian:bullseye-slim" } }, { action: "ALLOW", selector: { identifier: "docker-image://docker.io/library/debian:bullseye-slim" } } ] }') ./buildx --builder foo build --progress=plain -
#0 building with "foo" instance using docker-container driver

#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 53B done
#1 DONE 0.0s

#2 [internal] load metadata for docker.io/library/bash:latest
#2 DONE 0.2s

#3 docker-image://docker.io/library/debian:bullseye-slim
#3 resolve docker.io/library/debian:bullseye-slim
#3 resolve docker.io/library/debian:bullseye-slim 0.2s done
#3 DONE 0.2s
WARNING: No output specified with docker-container driver. Build result will only remain in the build cache. To push result image into registry use --push or to load image into docker use --load
ERROR: failed to solve: missing provenance for h9oahwy6afza4swiriqsd7aor

but every so often (I had to run it 16 times in quick sequence to get this, where before this PR it was way more frequent):

$ printf 'FROM bash:latest' | EXPERIMENTAL_BUILDKIT_SOURCE_POLICY=<(jq -nc '{ rules: [ { action: "DENY", selector: { identifier: "*" } }, { action: "ALLOW", selector: { identifier: "local://dockerfile" } }, { action: "CONVERT", selector: { identifier: "docker-image://docker.io/library/bash:latest@*" }, updates: { identifier: "docker-image://docker.io/library/debian:bullseye-slim" } }, { action: "ALLOW", selector: { identifier: "docker-image://docker.io/library/debian:bullseye-slim" } } ] }') ./buildx --builder foo build --progress=plain -
WARNING: No output specified with docker-container driver. Build result will only remain in the build cache. To push result image into registry use --push or to load image into docker use --load
ERROR: failed to solve: failed to read dockerfile: failed to load LLB: error evaluating the source policy: source "local://dockerfile" denied by policy: source denied by policy
`

@cpuguy83
Copy link
Member

Just to note: The selector docker-image://docker.io/library/bash:latest@* seems incorrect here because it won't match the undigested ref and will always be denied, though this is not the error you are seeing there.

With changing that ref to just docker-image://docker.io/library/bash:latest I'm running your script in a loop against 0.12 and am not seeing any errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants