Skip to content

Namespace cache volumes by module #7211

Closed

Description

@shykes

Problem

Cache volumes exist in the engine's global namespace: if modules A and B each create a cache volume with id foo, they will share read and write access to the same volume, as long as they are run on the same engine. This allows one module to corrupt the data of another module, either accidentally or maliciously.

Solution

Namespace the name of cache volumes, so that each module can only read and write to its own cache volumes.

See also

This issue is an up-to-date reboot of #3345

Activity

  1. sagikazarmark commented on Apr 26, 2024

    @sagikazarmark
    Contributor

    What does module mean in this context? Each instance/call of a module gets separate cache volumes?

    What if I have multiple calls to a Go module within the same project and I want to share the module/build cache between them?

  2. shykes commented on Apr 26, 2024

    @shykes
    ContributorAuthor

    What does module mean in this context? Each instance/call of a module gets separate cache volumes?

    What if I have multiple calls to a Go module within the same project and I want to share the module/build cache between them?

    I mean the full canonical address of the module, for example github.com/shykes/daggerverse/hello.

    All instances of the same module would share the same volume, as long as they share the same persisted cache volume storage.

  3. sipsma commented on Apr 29, 2024

    @sipsma
    Contributor

    I agree we need to do something like this, but do want to note that the fact that cache volumes can be shared across modules is highly beneficial to performance for many common use cases. E.g. anything that uses Go benefits from sharing a cache volume for downloading deps (and possibly build cache, etc.).

    Obviously in the choice between security-by-default and performance, security-by-default should win.

    But in past discussions around all this the idea of cache volumes being tied to modules but still allowing modules to pass their own cache volumes around came up and is still worth considering imo. So say you are writing a module that calls to a bunch of other modules that do "go things"; you should be able to define a cache volume and pass those cache volumes to be used by modules you call.

    • This of course requires that the modules you are calling accept an optional cache volume to use as an override for their default (private) one, which probably just needs to become a best practice in this scenario.

    That seems like one reasonable way of maintaining security by default while still allowing opt-in performance benefits. I'm sure there's other approaches possible too.

  4. shykes commented on Apr 29, 2024

    @shykes
    ContributorAuthor

    Sure, since there's a cache volume type, it makes sense that you can pass it as argument. I've never seen that done so hadn't even thought about it, but it seems reasonable to me that we don't break it. As long as it doesn't break namespacing of modules (which I don't think it would), then I don't see any problem with that.

    So these things should all be true:

    1. When a cache volume is loaded by key (cacheVolume(key: String!)), the key is always namespaced by module address. There is no way to bypass this.
    2. A cache volume ID can be used by any module. But a module cannot guess the ID of another module's cache volume: it needs to be passed explicitly.
    3. Ideally, cache volume IDs do NOT become sensitive values to be scrubbed from screenshots and logs (because ie. they are not reusable across sessions)
  5. sipsma commented on Apr 29, 2024

    @sipsma
    Contributor

    Yeah SGTM, coincidentally everything required to implement enforcement of only using cache volumes you create or are explicitly passed is also what's required to safely pass sockets around (#6747), which I'm working on right now, so should be feasible to implement all this in the very near future.

  6. shykes commented on Aug 8, 2024

    @shykes
    ContributorAuthor

    @sipsma putting this back on your radar since you're working on improved persistence of cache volumes. Seemed like a good time to keep this one in mind.

  7. sipsma commented on Aug 8, 2024

    @sipsma
    Contributor

    @shykes Yep I've been thinking about it. As of right now I've found it to be orthogonal to the efforts in #8004. The namespacing is something we'd implement on a higher level that's agnostic to the underlying storage.

    The only connection point is that storage of cache volumes are keyed by their definition (just a hash of its definition essentially), so namespacing is mainly a matter of incorporating another string into that definition.

  8. shykes commented on Aug 9, 2024

    @shykes
    ContributorAuthor

    @sipsma but wouldn't it be operationally convenient if the path of each volume on the host filesystem were human-readable and keyed by volume name instead of hash? Eg /var/lib/dagger/cachevolumes/node_modules.

    With namespacing that might become: /var/lib/dagger/cachevolumes/github.com%2Fshykes%2Fdaggerverse%2Fnodejs/node_modules

    wdyt?

  9. sipsma commented on Aug 9, 2024

    @sipsma
    Contributor

    @shykes whether to store the cache volume with human-readable names of a hash is already independent of namespacing by volume.

    A cache volume is currently a struct like:

    type CacheVolume struct {
      Key string
      Uid int
      Gid int
      Source *Directory
    }

    Where right now the key in the storage is a hash of those fields (/var/lib/dagger/volumes/deadbeef123).

    • The uid/gid/source fields are all necessary since they already are part of our existing cache volume API and require separate cache volumes instances.

    We could already change it to be human readable by just making the path instead be like /var/lib/dagger/volumes/<key>/<uid>-<gid>/<source dir hash>.

    • The reason I went with a hash for now is that you previously gave feedback that it's better for the structure to be opaque, which was fine by me since users can interact with the volumes via the API and that's better in the sense that we can change the layout more freely without being backwards incompatible.

    Point being that if we do go with human readable paths for cache volume storage paths, then namespacing is just a matter of appending another field to that struct and the underlying path. The actual implementation of namespacing by module is still totally independent.

  10. shykes commented on Aug 9, 2024

    @shykes
    ContributorAuthor

    OK makes sense! Sorry I totally forgot I said that previously 😁 Keeping the format opaque to avoid a one-way door, does make sense.

  11. rajatjindal commented on Oct 1, 2024

    @rajatjindal
    Contributor

    Hi @sipsma,

    I am starting to look into this issue. is that ok or you are looking into this issue already?

  12. rajatjindal commented on Oct 13, 2024

    @rajatjindal
    Contributor

    Hi @shykes @sipsma

    I am looking into this issue and running into a scenario which is working in an unexpected way:

    The setup looks like follows:

    (also in repo here: https://github.com/rajatjindal/dagger-same-cache-volume-id)

    • mod B has following functions:
      • access cache volume by ID and Name and list a specific file in it
    • mod A has following functions:
      • mod A has dependency on mod B
      • to populate cache and return cache volume id.
      • to call mod B function using cache volume id

    Scenario step:

    - call mod B function to access cache volume by name and list the specific file. it fails as expected
    - call mod A function to populate cache volume and a specific file. it works and print the cache volume id
    - call mod B function to access cache volume by name and list the specific file. it fails, as the ID is different
    - call mod A function to accces cache volume via calling the mod B function to access cache volume by its id. it works
    - call mod B function to access cache volume by name and list the specific file. it works this time. I was expecting it not to work
    

    It seems like the "access cache volume" step is getting cached and because "volume name" is not a cache buster in buildkit (from discussion here: https://discord.com/channels/707636530424053791/1294252239702982717), its returning the cached result.

    One other scenario that I am trying to validate further is if two different modules have dependency on module B, does the cached results work across modules. Because IMO, if that is the case, then this may be problematic.

  13. rajatjindal commented on Oct 16, 2024

    @rajatjindal
    Contributor

    @marcosnils helped narrow down the reproducible case to below code:

    with module code:

    package main
    
    import (
            "context"
    )
    
    type Test struct{}
    
    func (m *Test) Read(ctx context.Context, name string) (string, error) {
            return dag.Container().
                    From("alpine:latest").
                    WithMountedCache("/bar", dag.CacheVolume(name)).
                    WithExec([]string{"sh", "-c", "ls /bar/bar.txt"}).
                    Stdout(ctx)
    }
    
    func (m *Test) Write(ctx context.Context, name string) (string, error) {
            return dag.Container().
                    From("alpine:latest").
                    WithMountedCache("/bar", dag.CacheVolume(name)).
                    WithExec([]string{"sh", "-c", "touch /bar/bar.txt"}).
                    Stdout(ctx)
    }
    
    

    to reproduce the issue:

    dagger call read --name foo > fails
    dagger call write --name foo
    dagger call read --name foo > succeds
    dagger call read --name bar > also succeds when it should have failed
    

    more details in the discord thread here.

  14. rajatjindal commented on Oct 17, 2024

    @rajatjindal
    Contributor

    Hi @marcosnils,

    As discussed, I've opened a draft PR for this.

    Could you please help get clarification to requirement #.3 from #7211 (comment).

    thanks

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions