Files
Ryan El Kochta 435549c32d tmpfs: implement POSIX ACLs
Implements POSIX ACLs for gVisor's tmpfs. As a summary:
- Adds `acl` and `defaultACL` fields to tmpfs's inode
- Allows `system.posix_acl_access` and `system.posix_acl_default`
  xattrs to set and retrieve POSIX ACLs
- Updates tmpfs's stat implementation to account for the ACL in
  setting/retrieving a file's mode
- Updates `GenericCheckPermission` to take an optional ACL. All
  non-tmpfs filesystems pass the `nil` ACL.
- Updates tmpfs's tar save-and-restore logic to store the base64-encoded
  POSIX ACL xattrs in the tar header
- Adds unit tests for POSIX ACL parsing/serialization
- Adds integration tests for POSIX ACLs
- Adds a container test to verify the tmpfs tar save/restore logic with
  ACLs. This required a new subcommand for `test_app` to set and fetch
  xattrs in the container

The motivation for POSIX ACLs is for journald (which uses POSIX ACLs to
give non-root users access to their own `systemctl --user` services'
logs). This commit is not actually sufficient to get this working -- we
will need to plumb ACLs to overlayfs, too, which will be done in a
follow-up commit.

The implementation is very similar to the implementation in Linux,
although it is filesystem-specific rather than at the VFS layer (much
like we store `mode`, `uid`, `gid`, etc at the FS layer currently). An
optional access and default ACL is stored in each inode. If present, it
is used for permission checks and is kept in sync with the `mode`. The
`mode` is computed from the ACL when permissions are updated. If the ACL
is simple enough to be stored equivalently as a `mode`, an ACL is not
stored.

One limitation at the moment is that upon file creation, the process's
umask is masked against the creation mode regardless of the presence of
a default ACL, whereas Linux only masks against umask when *not*
inheriting from a default ACL. Fixing this would require a fair bit of
refactoring since umask isn't passed through to filesystems at all, and
is instead masked with the creation mode at the syscall layer. This is
not a security issue since the resulting permissions are always of
equal-or-greater restrictiveness to those of Linux, but is tracked in
\#13688.
2026-07-15 08:34:54 -07:00

116 lines
3.6 KiB
Go

// Copyright 2026 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"encoding/base64"
"fmt"
"log"
"github.com/google/subcommands"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/runsc/flag"
)
const xattrSizeMax = 65536
// setXattr sets an extended attribute on a file. The value is passed as a
// base64-encoded string so that arbitrary binary blobs can be specified on
// the command line.
type setXattr struct {
path string
name string
value string
}
// Name implements subcommands.Command.Name.
func (*setXattr) Name() string { return "setxattr" }
// Synopsis implements subcommands.Command.Synopsis.
func (*setXattr) Synopsis() string { return "sets an extended attribute (value is base64-encoded)" }
// Usage implements subcommands.Command.Usage.
func (*setXattr) Usage() string {
return "setxattr --path=<path> --name=<name> --value=<base64>\n"
}
// SetFlags implements subcommands.Command.SetFlags.
func (c *setXattr) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.path, "path", "", "path of the file to set the xattr on")
f.StringVar(&c.name, "name", "", "name of the xattr")
f.StringVar(&c.value, "value", "", "base64-encoded value of the xattr")
}
// Execute implements subcommands.Command.Execute.
func (c *setXattr) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
if c.path == "" || c.name == "" {
log.Print("--path and --name must be set")
return subcommands.ExitUsageError
}
value, err := base64.StdEncoding.DecodeString(c.value)
if err != nil {
log.Printf("failed to decode --value %q: %v", c.value, err)
return subcommands.ExitUsageError
}
if err := unix.Setxattr(c.path, c.name, value, 0); err != nil {
log.Printf("setxattr(%q, %q) failed: %v", c.path, c.name, err)
return subcommands.ExitFailure
}
return subcommands.ExitSuccess
}
// getXattr prints the base64-encoded value of an extended attribute to stdout.
type getXattr struct {
path string
name string
}
// Name implements subcommands.Command.Name.
func (*getXattr) Name() string { return "getxattr" }
// Synopsis implements subcommands.Command.Synopsis.
func (*getXattr) Synopsis() string {
return "prints the base64-encoded value of an extended attribute to stdout"
}
// Usage implements subcommands.Command.Usage.
func (*getXattr) Usage() string {
return "getxattr --path=<path> --name=<name>\n"
}
// SetFlags implements subcommands.Command.SetFlags.
func (c *getXattr) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.path, "path", "", "path of the file to get the xattr from")
f.StringVar(&c.name, "name", "", "name of the xattr")
}
// Execute implements subcommands.Command.Execute.
func (c *getXattr) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
if c.path == "" || c.name == "" {
log.Print("--path and --name must be set")
return subcommands.ExitUsageError
}
buf := make([]byte, xattrSizeMax)
n, err := unix.Getxattr(c.path, c.name, buf)
if err != nil {
log.Printf("getxattr(%q, %q) failed: %v", c.path, c.name, err)
return subcommands.ExitFailure
}
fmt.Println(base64.StdEncoding.EncodeToString(buf[:n]))
return subcommands.ExitSuccess
}