Skip to content

Commit

Permalink
Limit the maximum resolve depth to prevent infinite loops (#3896)
Browse files Browse the repository at this point in the history
* Limit the maximum resolve depth to prevent infinite loops  Co-authored-by: Cheng Zhou <zhoucheng@juicedata.io>
  • Loading branch information
zhijian-pro authored Jul 13, 2023
1 parent 4e28868 commit 9077586
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
25 changes: 22 additions & 3 deletions .github/scripts/command/sync_minio.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,32 @@ test_sync_external_link(){
[ -z $(./mc cat myminio/myjfs/hello) ]
}

skip_test_sync_broken_link(){
test_sync_loop_symlink(){
prepare_test
touch hello
ln -s hello /jfs/hello
./juicefs sync minio://minioadmin:minioadmin@localhost:9005/myjfs/ myjfs/ && exit 1 || true
./juicefs sync minio://minioadmin:minioadmin@localhost:9005/myjfs/ minio://minioadmin:minioadmin@localhost:9000/myjfs/ && exit 1 || true
rm -rf /jfs/hello
./juicefs sync minio://minioadmin:minioadmin@localhost:9005/myjfs/ myjfs/
./juicefs sync minio://minioadmin:minioadmin@localhost:9005/myjfs/ minio://minioadmin:minioadmin@localhost:9000/myjfs/
}

test_sync_deep_symlink(){
prepare_test
cd /jfs
echo hello > hello
ln -s hello symlink_1
for i in {1..40}; do
ln -s symlink_$i symlink_$((i+1))
done
cat symlink_40 | grep hello
cat symlink_41 && echo "cat symlink_41 fail" && exit 1 || true
cd -
./juicefs sync minio://minioadmin:minioadmin@localhost:9005/myjfs/ minio://minioadmin:minioadmin@localhost:9000/myjfs/ && echo "sync should fail" && exit 1 || true
rm -rf /jfs/symlink_41
./juicefs sync minio://minioadmin:minioadmin@localhost:9005/myjfs/ minio://minioadmin:minioadmin@localhost:9000/myjfs/
for i in {1..40}; do
./mc cat myminio/myjfs/symlink_$i | grep "^hello$"
done
}

prepare_test(){
Expand Down
9 changes: 8 additions & 1 deletion pkg/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,10 @@ func (fs *FileSystem) lookup(ctx meta.Context, parent Ino, name string, inode *I
}

func (fs *FileSystem) resolve(ctx meta.Context, p string, followLastSymlink bool) (fi *FileStat, err syscall.Errno) {
return fs.doResolve(ctx, p, followLastSymlink, 0)
}

func (fs *FileSystem) doResolve(ctx meta.Context, p string, followLastSymlink bool, depth int) (fi *FileStat, err syscall.Errno) {
var inode Ino
var attr = &Attr{}

Expand Down Expand Up @@ -736,6 +740,9 @@ func (fs *FileSystem) resolve(ctx meta.Context, p string, followLastSymlink bool
}
fi = AttrToFileInfo(inode, attr)
if (!resolved || followLastSymlink) && fi.IsSymlink() {
if depth > 39 {
return nil, syscall.ELOOP
}
var buf []byte
err = fs.m.ReadLink(ctx, inode, &buf)
if err != 0 {
Expand All @@ -746,7 +753,7 @@ func (fs *FileSystem) resolve(ctx meta.Context, p string, followLastSymlink bool
return &FileStat{name: target}, syscall.ENOTSUP
}
target = path.Join(strings.Join(ss[:i], "/"), target)
fi, err = fs.resolve(ctx, target, followLastSymlink)
fi, err = fs.doResolve(ctx, target, followLastSymlink, depth+1)
if err != 0 {
return
}
Expand Down

0 comments on commit 9077586

Please sign in to comment.