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

cmd/mount: Support all-squash mount option #5395

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cmd/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,27 @@ func TestMountVersionMatch(t *testing.T) {
err = tryMountTemp(t, nil, []string{"--enable-acl=true"}, nil)
assert.Contains(t, err.Error(), "check version")
}

func TestParseUIDGID(t *testing.T) {
tests := []struct {
input string
defaultUid uint32
defaultGid uint32
expectedUid uint32
expectedGid uint32
}{
{"1000:1000", 65534, 65534, 1000, 1000},
{"1000:", 65534, 65534, 1000, 65534},
{":1000", 65534, 65534, 65534, 1000},
{"", 65534, 65534, 65534, 65534},
{"0:1000", 65534, 65534, 65534, 1000},
{"1000:0", 65534, 65534, 1000, 65534},
}

for _, tt := range tests {
uid, gid := parseUIDGID(tt.input, tt.defaultUid, tt.defaultGid)
if uid != tt.expectedUid || gid != tt.expectedGid {
t.Errorf("parseUIDGID(%q) = (%d, %d), want (%d, %d)", tt.input, uid, gid, tt.expectedUid, tt.expectedGid)
}
}
}
96 changes: 64 additions & 32 deletions cmd/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ func fuseFlags() []cli.Flag {
Name: "root-squash",
Usage: "mapping local root user (uid = 0) to another one specified as <uid>:<gid>",
},
&cli.StringFlag{
Name: "all-squash",
Usage: "mapping all users to another one specified as <uid>:<gid>",
},
&cli.BoolFlag{
Name: "prefix-internal",
Usage: "add '.jfs' prefix to all internal files",
Expand Down Expand Up @@ -828,6 +832,53 @@ func launchMount(mp string, conf *vfs.Config) error {
}
}

func getNobodyUIDGID() (uint32, uint32) {
var uid, gid uint32 = 65534, 65534
if u, err := user.Lookup("nobody"); err == nil {
nobody, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
logger.Fatalf("invalid uid: %s", u.Uid)
}
uid = uint32(nobody)
}
if g, err := user.LookupGroup("nogroup"); err == nil {
nogroup, err := strconv.ParseUint(g.Gid, 10, 32)
if err != nil {
logger.Fatalf("invalid gid: %s", g.Gid)
}
gid = uint32(nogroup)
}
return uid, gid
}

func parseUIDGID(input string, defaultUid uint32, defaultGid uint32) (uint32, uint32) {
ss := strings.SplitN(strings.TrimSpace(input), ":", 2)
uid, gid := defaultUid, defaultGid
if ss[0] != "" {
u, err := strconv.ParseUint(ss[0], 10, 32)
if err != nil {
logger.Fatalf("invalid uid: %s", ss[0])
}
uid = uint32(u)
if uid == 0 {
logger.Warnf("Can't map uid as 0, use %d instead", defaultUid)
uid = defaultUid
}
}
if len(ss) == 2 && ss[1] != "" {
g, err := strconv.ParseUint(ss[1], 10, 32)
if err != nil {
logger.Fatalf("invalid gid: %s", ss[1])
}
gid = uint32(g)
if gid == 0 {
logger.Warnf("Can't map gid as 0, use %d instead", defaultGid)
gid = defaultGid
}
}
return uid, gid
}

func mountMain(v *vfs.VFS, c *cli.Context) {
if os.Getuid() == 0 {
disableUpdatedb()
Expand All @@ -838,39 +889,20 @@ func mountMain(v *vfs.VFS, c *cli.Context) {
conf.DirEntryTimeout = utils.Duration(c.String("dir-entry-cache"))
conf.NonDefaultPermission = c.Bool("non-default-permission")
rootSquash := c.String("root-squash")
if rootSquash != "" {
var uid, gid uint32 = 65534, 65534
if u, err := user.Lookup("nobody"); err == nil {
nobody, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
logger.Fatalf("invalid uid: %s", u.Uid)
}
uid = uint32(nobody)
}
if g, err := user.LookupGroup("nogroup"); err == nil {
nogroup, err := strconv.ParseUint(g.Gid, 10, 32)
if err != nil {
logger.Fatalf("invalid gid: %s", g.Gid)
}
gid = uint32(nogroup)
}

ss := strings.SplitN(strings.TrimSpace(rootSquash), ":", 2)
if ss[0] != "" {
u, err := strconv.ParseUint(ss[0], 10, 32)
if err != nil {
logger.Fatalf("invalid uid: %s", ss[0])
}
uid = uint32(u)
}
if len(ss) == 2 && ss[1] != "" {
g, err := strconv.ParseUint(ss[1], 10, 32)
if err != nil {
logger.Fatalf("invalid gid: %s", ss[1])
}
gid = uint32(g)
allSquash := c.String("all-squash")
SandyXSD marked this conversation as resolved.
Show resolved Hide resolved
if allSquash != "" || rootSquash != "" {
nobodyUid, nobodyGid := getNobodyUIDGID()
// all-squash takes precedence over root-squash
if allSquash != "" {
conf.NonDefaultPermission = true // disable kernel permission check
uid, gid := parseUIDGID(allSquash, nobodyUid, nobodyGid)
conf.AllSquash = &vfs.AnonymousAccount{Uid: uid, Gid: gid}
logger.Infof("Map all uid/gid to %d/%d by setting all-squash", uid, gid)
} else { // rootSquash != ""
uid, gid := parseUIDGID(rootSquash, nobodyUid, nobodyGid)
conf.RootSquash = &vfs.AnonymousAccount{Uid: uid, Gid: gid}
logger.Infof("Map root uid/gid 0 to %d/%d by setting root-squash", uid, gid)
}
conf.RootSquash = &vfs.RootSquash{Uid: uid, Gid: gid}
}
logger.Infof("Mounting volume %s at %s ...", conf.Format.Name, conf.Meta.MountPoint)
err := fuse.Serve(v, c.String("o"), c.Bool("enable-xattr"), c.Bool("enable-ioctl"))
Expand Down
5 changes: 5 additions & 0 deletions pkg/fuse/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (fs *fileSystem) newContext(cancel <-chan struct{}, header *fuse.InHeader)
ctx.header.Uid = fs.conf.RootSquash.Uid
ctx.header.Gid = fs.conf.RootSquash.Gid
}
if fs.conf.AllSquash != nil {
ctx.checkPermission = true
ctx.header.Uid = fs.conf.AllSquash.Uid
ctx.header.Gid = fs.conf.AllSquash.Gid
}
return ctx
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/vfs/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ type Config struct {
AccessLog string `json:",omitempty"`
PrefixInternal bool
HideInternal bool
RootSquash *RootSquash `json:",omitempty"`
NonDefaultPermission bool `json:",omitempty"`
RootSquash *AnonymousAccount `json:",omitempty"`
AllSquash *AnonymousAccount `json:",omitempty"`
NonDefaultPermission bool `json:",omitempty"`

Pid int
PPid int
Expand All @@ -138,7 +139,7 @@ type Config struct {
FuseOpts *FuseOptions `json:",omitempty"`
}

type RootSquash struct {
type AnonymousAccount struct {
Uid uint32
Gid uint32
}
Expand Down
Loading