A directory whose owner changes gives its contents the new owner

A module moved to an account of its own (runs-as) has files below its
directories that it wrote as the account it ran as before (gitea's webhook
secret, 0600, as the operator's account); the node-engine changed only the
directory's owner, so the new account could not read them. Now applyDirectory
and applyMeshDirectory give the new owner to what the previous owner held below
the directory, but never a path the declaration or another module's composition
places (and what lies below it), never an entry another account owns, never
through a link (Lchown) and never on another file system.

Builds: design 47 H2 H-A-545 (2), H-A-550
This commit is contained in:
2026-11-23 12:07:40 +01:00
parent 1a19492541
commit d16c4cd003
8 changed files with 228 additions and 11 deletions
+19 -3
View File
@@ -794,6 +794,10 @@ func applyScoped(
in.agentHomes = rootNeverHomes(d)
in.dirsBefore = map[string]bool{}
in.files = declaredFiles(d.Resources)
in.placed = placedPaths(d.Resources)
if scope != nil {
in.placed = placedPaths(d.Resources, scope.Others)
}
for _, resource := range d.Resources {
in.declares[resource.Identity()] = declaredDigest(resource)
if dir, ok := resource.(*declaration.Directory); ok {
@@ -1603,7 +1607,7 @@ func applyOne(ctx context.Context, sys system.System, r declaration.Resource, ru
}
switch res := r.(type) {
case *declaration.Directory:
return applyDirectory(res, previous, in.dirsBefore[filepath.Clean(res.Path)])
return applyDirectory(res, previous, in.dirsBefore[filepath.Clean(res.Path)], in.placed)
case *declaration.File:
return applyFile(res, previous, unseal, keepFound)
case *declaration.Service:
@@ -1705,7 +1709,11 @@ func modeOf(spec string, fallback os.FileMode) (os.FileMode, error) {
// directory is the mesh's when its record says the mesh applied it at this path before (a record from before
// this rule counts, which is every directory on a running machine), when this apply made it, or when it
// already has the declared owner and mode, so a person who sets them by hand at the machine hands it over.
func applyDirectory(r *declaration.Directory, previous store.Applied, wasBefore bool) (Outcome, error) {
//
// **A directory whose owner changes gives its contents the new owner too** (H-A-545 (2), H-A-550): what its previous
// owner held below it, but for the paths placed names (every path the declaration places) and what lies below them
// (giveContents).
func applyDirectory(r *declaration.Directory, previous store.Applied, wasBefore bool, placed map[string]bool) (Outcome, error) {
out := begin(r)
mode, err := dirModeOf(r.Mode, 0o755)
if err != nil {
@@ -1776,7 +1784,7 @@ func applyDirectory(r *declaration.Directory, previous store.Applied, wasBefore
// Outside every home, made and given its mode and owner only through a descriptor opened from /, following
// no link another account could have made (novox/hq issue 358, mesh_dirs.go).
if meshDirsByDescriptor && homeAbove(r.Path) == "" {
return applyMeshDirectory(r, out, mode, existed)
return applyMeshDirectory(r, out, mode, existed, placed)
}
if !existed {
if err := makeDirs(r.Path, mode, r.Owner); err != nil {
@@ -1810,6 +1818,11 @@ func applyDirectory(r *declaration.Directory, previous store.Applied, wasBefore
if err := own(r.Path, r.Owner); err != nil {
return out, err
}
if existed {
if err := giveContentsTo(r, before, placed); err != nil {
return out, err
}
}
}
out.Action = "unchanged"
@@ -2802,6 +2815,9 @@ type inputs struct {
// dirsBefore is every declared directory that was on the machine when this apply began (novox/hq issue
// 339): one the apply itself made — a file's parent — is the mesh's, one that was there may not be.
dirsBefore map[string]bool
// placed is every path the declaration and the other modules' compositions place (Scope.Others): a directory whose
// owner changes leaves them to their own resource when it gives its contents the new owner (H-A-550).
placed map[string]bool
// held is the root of every library whose disk or mount is not there, with its name: no container that mounts
// it is created (novox/hq ADR 0328 §9).
held map[string]string