-
Notifications
You must be signed in to change notification settings - Fork 40
fix(file_sharing): prevent infinite loop on circular symlinks when duplicates are allowed #281
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -219,10 +219,15 @@ bool LocalDirectoryUpdater::sweepSharedDirectories(bool& some_files_not_ready) | |
| { | ||
| RS_DBG4("recursing into \"", stored_dir_it.name()); | ||
|
|
||
| existing_dirs.insert(RsDirUtil::removeSymLinks(stored_dir_it.name())); | ||
| std::string canonical = RsDirUtil::removeSymLinks(stored_dir_it.name()); | ||
| if(!canonical.empty()) | ||
| existing_dirs.insert(canonical); | ||
| std::set<std::string> current_branch_real_paths; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If canonical.empty() is true, it means the path (including the link) is not valid. So there's no point is continuing here. You can also factor the two ifs, which gives: } |
||
| if(!canonical.empty()) | ||
| current_branch_real_paths.insert(canonical); | ||
| recursUpdateSharedDir( | ||
| stored_dir_it.name(), *stored_dir_it, | ||
| existing_dirs, 1, some_files_not_ready ); | ||
| existing_dirs, current_branch_real_paths, 1, some_files_not_ready ); | ||
| /* here we need to use the list that was stored, instead of the shared | ||
| * dir list, because the two are not necessarily in the same order. */ | ||
| } | ||
|
|
@@ -240,7 +245,7 @@ bool LocalDirectoryUpdater::sweepSharedDirectories(bool& some_files_not_ready) | |
|
|
||
| void LocalDirectoryUpdater::recursUpdateSharedDir( | ||
| const std::string& cumulated_path, DirectoryStorage::EntryIndex indx, | ||
| std::set<std::string>& existing_directories, uint32_t current_depth, | ||
| std::set<std::string>& existing_directories, std::set<std::string>& current_branch_real_paths, uint32_t current_depth, | ||
| bool& some_files_not_ready ) | ||
| { | ||
| RS_DBG4("parsing directory \"", cumulated_path, "\" index: ", indx); | ||
|
|
@@ -300,21 +305,37 @@ void LocalDirectoryUpdater::recursUpdateSharedDir( | |
| || (mMaxShareDepth == 0 && current_depth >= 64) ) | ||
| dir_is_accepted = false; | ||
|
|
||
| if(dir_is_accepted && mFollowSymLinks && mIgnoreDuplicates) | ||
| if(dir_is_accepted && mFollowSymLinks) | ||
| { | ||
| std::string real_path = RsDirUtil::removeSymLinks( | ||
| RsDirUtil::makePath(cumulated_path, dirIt.file_name()) ); | ||
|
|
||
| if( existing_directories.end() != | ||
| existing_directories.find(real_path) ) | ||
| if(real_path.empty()) | ||
| { | ||
| RS_WARN( "Directory: \"", cumulated_path, | ||
| "\" has real path: \"", real_path, | ||
| "\" which already belongs to another " | ||
| "shared directory. Ignoring" ); | ||
| RS_WARN( "Broken/circular symlink: \"", cumulated_path, | ||
| "/", dirIt.file_name(), "\". Ignoring." ); | ||
| dir_is_accepted = false; | ||
| } | ||
| else existing_directories.insert(real_path); | ||
| else if(current_branch_real_paths.end() != current_branch_real_paths.find(real_path)) | ||
| { | ||
| RS_WARN( "Circular symlink detected: \"", cumulated_path, | ||
| "\" points to ancestor \"", real_path, | ||
| "\". Ignoring." ); | ||
| dir_is_accepted = false; | ||
| } | ||
| else if(mIgnoreDuplicates) | ||
| { | ||
| if( existing_directories.end() != | ||
| existing_directories.find(real_path) ) | ||
| { | ||
| RS_WARN( "Directory: \"", cumulated_path, | ||
| "\" has real path: \"", real_path, | ||
| "\" which already belongs to another " | ||
| "shared directory. Ignoring" ); | ||
| dir_is_accepted = false; | ||
| } | ||
| else existing_directories.insert(real_path); | ||
| } | ||
| } | ||
|
|
||
| if(dir_is_accepted) subdirs.insert(dirIt.file_name()); | ||
|
|
@@ -364,9 +385,19 @@ void LocalDirectoryUpdater::recursUpdateSharedDir( | |
| // go through the list of sub-dirs and recursively update | ||
| for( DirectoryStorage::DirIterator stored_dir_it(mSharedDirectories, indx); | ||
| stored_dir_it; ++stored_dir_it ) | ||
| recursUpdateSharedDir( RsDirUtil::makePath(cumulated_path, stored_dir_it.name()), | ||
| *stored_dir_it, existing_directories, | ||
| { | ||
| std::string next_path = RsDirUtil::makePath(cumulated_path, stored_dir_it.name()); | ||
| std::string canonical = RsDirUtil::removeSymLinks(next_path); | ||
| if(!canonical.empty()) | ||
| current_branch_real_paths.insert(canonical); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: if the path points to an ancestor, we should ignore it and return. |
||
|
|
||
| recursUpdateSharedDir( next_path, | ||
| *stored_dir_it, existing_directories, current_branch_real_paths, | ||
| current_depth+1, some_files_not_ready ); | ||
|
|
||
| if(!canonical.empty()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...so I suggest you replace all this by and add a comment that canonical cannot be empty. |
||
| current_branch_real_paths.erase(canonical); | ||
| } | ||
| } | ||
|
|
||
| bool LocalDirectoryUpdater::filterFile(const std::string& fname) const | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when there is a loop in the sym links, canonicalize_file_name() will return nullptr and RsDirUtil::removeSymLinks() will return a null std::string (empty()=true). So inserting this empty string in existing_dirs has probably unwanted consequences.