From cd1ad88f30419f330af22333e1621b92d3a9b695 Mon Sep 17 00:00:00 2001 From: chrislu Date: Wed, 12 Jan 2022 00:16:00 -0800 Subject: [PATCH] POSIX: check name is too long ENAMETOOLONG --- weed/filesys/dir.go | 16 ++++++++++++++++ weed/filesys/dir_link.go | 8 ++++++++ weed/filesys/dir_rename.go | 7 +++++++ weed/filesys/xattr.go | 8 ++++++++ 4 files changed, 39 insertions(+) diff --git a/weed/filesys/dir.go b/weed/filesys/dir.go index 7471e53fa..f322c8e74 100644 --- a/weed/filesys/dir.go +++ b/weed/filesys/dir.go @@ -129,6 +129,10 @@ func (dir *Dir) newDirectory(fullpath util.FullPath) fs.Node { func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) { + if err := checkName(req.Name); err != nil { + return nil, nil, err + } + exclusive := req.Flags&fuse.OpenExclusive != 0 isDirectory := req.Mode&os.ModeDir > 0 @@ -168,6 +172,10 @@ func (dir *Dir) Create(ctx context.Context, req *fuse.CreateRequest, func (dir *Dir) Mknod(ctx context.Context, req *fuse.MknodRequest) (fs.Node, error) { + if err := checkName(req.Name); err != nil { + return nil, err + } + glog.V(3).Infof("dir %s Mknod %+v", dir.FullPath(), req) _, err := dir.doCreateEntry(req.Name, req.Mode, req.Uid, req.Gid, false) @@ -228,6 +236,10 @@ func (dir *Dir) doCreateEntry(name string, mode os.FileMode, uid, gid uint32, ex func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, error) { + if err := checkName(req.Name); err != nil { + return nil, err + } + glog.V(4).Infof("mkdir %s: %s", dir.FullPath(), req.Name) newEntry := &filer_pb.Entry{ @@ -282,6 +294,10 @@ func (dir *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node, err func (dir *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) { + if err := checkName(req.Name); err != nil { + return nil, err + } + dirPath := util.FullPath(dir.FullPath()) // glog.V(4).Infof("dir Lookup %s: %s by %s", dirPath, req.Name, req.Header.String()) diff --git a/weed/filesys/dir_link.go b/weed/filesys/dir_link.go index 68f5a79e2..cc339b2ed 100644 --- a/weed/filesys/dir_link.go +++ b/weed/filesys/dir_link.go @@ -24,6 +24,10 @@ const ( func (dir *Dir) Link(ctx context.Context, req *fuse.LinkRequest, old fs.Node) (fs.Node, error) { + if err := checkName(req.NewName); err != nil { + return nil, err + } + oldFile, ok := old.(*File) if !ok { glog.Errorf("old node is not a file: %+v", old) @@ -102,6 +106,10 @@ func (dir *Dir) Link(ctx context.Context, req *fuse.LinkRequest, old fs.Node) (f func (dir *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) { + if err := checkName(req.NewName); err != nil { + return nil, err + } + glog.V(4).Infof("Symlink: %v/%v to %v", dir.FullPath(), req.NewName, req.Target) request := &filer_pb.CreateEntryRequest{ diff --git a/weed/filesys/dir_rename.go b/weed/filesys/dir_rename.go index 6628b0799..22fe74fef 100644 --- a/weed/filesys/dir_rename.go +++ b/weed/filesys/dir_rename.go @@ -14,6 +14,13 @@ import ( func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error { + if err := checkName(req.NewName); err != nil { + return err + } + if err := checkName(req.OldName); err != nil { + return err + } + newDir := newDirectory.(*Dir) newPath := util.NewFullPath(newDir.FullPath(), req.NewName) diff --git a/weed/filesys/xattr.go b/weed/filesys/xattr.go index 6021fd8ec..818652f64 100644 --- a/weed/filesys/xattr.go +++ b/weed/filesys/xattr.go @@ -3,6 +3,7 @@ package filesys import ( "context" "strings" + "syscall" "github.com/seaweedfs/fuse" @@ -143,3 +144,10 @@ func (wfs *WFS) maybeLoadEntry(dir, name string) (entry *filer_pb.Entry, err err } return cachedEntry.ToProtoEntry(), cacheErr } + +func checkName(name string) error { + if len(name) >= 256 { + return syscall.ENAMETOOLONG + } + return nil +}