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

Limit the maximum resolve depth to prevent infinite loops #3896

Merged
merged 13 commits into from
Jul 13, 2023
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