You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Panic in ignore matching on invalid UTF-8 from filesystem watcher #9369
bugA problem with current functionality, as opposed to missing functionality (enhancement)frozen-due-to-ageIssues closed and untouched for a long time, together with being locked for discussionneeds-triageNew issues needed to be validated
Version: Breaks on 1.27.2 and the current git main branch (45beb28f)
Hello devs, thanks for all the hard work. I hadn't updated syncthing in a while and I started seeing a panic in my logs after doing a system update. The panic seems to happen after syncthing finishes scanning all my folders.
I am unfortunately not sure how to reproduce the problem at this time. The earliest error log is of version 1.27.2. Perhaps something to do with the file that syncthing didn't have read permissions on? I attached the logs with personal info removed. panic-20240126-012105.reported.log
I found the source of the problem after some investigating. I had a file in one of my folders with the filename �X! (don't ask me how, I don't know either) which is apparently invalid utf-8 and causes glob to panic for some patterns. I noticed Match() in ignore.go gets called with the same file multiple times and the instance originating from here -> here leads to a panic.
Should Matcher.Match() check for valid utf8 before proceeding? Adding a check there fixes the issue, but I feel like the file should have been filtered out before it gets to that stage, maybe in
Side note, method names in a lot of places are very confusing - many functions imply their parameters are dirs but are called with dir and file paths, like this,
Nice catch. The glob package is known flaky, especially with non-ASCII things, but we're currently a bit tightly tied to it for it to be an easy switch.
It was my impression that we filter quite strictly for valid UTF-8 and don't accept other forms of names, but perhaps not in the watching code.
I think it would make the most sense to do the utf-8 check in the Match function, since that seems to be the only place in the code where it is relevant because of the glob library. Right now it's invoked from multiple places and at least one of them doesn't do all the necessary sanitization.
That being said, it's a bit unexpected that syncthing silently (unless you're looking at the logs) ignores a file that's not in my ignore list. Regardless of the encoding of the file name, it is still a valid file in my file system and I would prefer syncthing to include it by default instead of ignoring it.
All parts of Syncthing depend on being able to understand the filename encoding, as it needs to change that encoding between systems, as not all systems use the same encoding. Treating it as just an opaque string of bytes doesn't work, unfortunately.
I see, I didn't consider cross-compatibility. I assume you have already considered replacing disallowed characters in the file names similar to what lib/fs/util.go does for Folders or keeping some sort of translation/mapping in the db and decided against it?
Anyhow, would it make sense to repurpose WindowsInvalidFilename to filter out all disallowed filenames and use it in the initial walk and monitoring steps to have a more consistent behavior? Correct me if I'm wrong but from what I could tell, it seems other invalid files like CON, etc. are indexed and pushed, and only fail when they are pulled by a Windows client with an error shown on the GUI. The behavior is inconsistent with non utf-8 files, which are just ignored. I'd argue non utf-8 filenames should also be pushed if they can be pulled by the receiving end, or there should at least be an option to bypass the compatibility filters. I'm willing to provide the PR for this if the devs agree to it.
I am a bit confused about the valid UTF-8 requirement - we do unicode normalisation in the protocol layer, but that never fails (doesn't return an error). And that's only in the protocol layer, that e.g. doesn't happen in the scanner. I don't see anything else doing validation from a quick look (very good chance I simply missed it).
So as far as I see there's nothing preventing such an invalid filename to get to the matching library even without filesystem watching. You could test that by disabling watching and seeing if the same panic occurs during scanning. If that's the case, then I think indeed the right thing to do is to add some validation in the matcher library. And probably err on the side of not ignoring it?
... That was a very good check I did. First it validates utf-8 (https://github.com/syncthing/syncthing/blob/main/lib/scanner/walk.go#L279) then it normlizes inside handleItem (after ignoring). So yeah, seems simple and consistent to also do a utf8.ValidString in the fs watcher code to prevent that panic. I'd probably not ignore failures when watching - might be useful to get the error during the scan afterwards to get the users attention. On the other hand the exact same argument could be made to ignore it: Avoid confusing the user, who potentially has no clue what to do with an invalid file anyway xD
I don't quite get the valid utf-8 requirment either, especially when other "invalid" files are handled differently.
From what I could gather in my tests, it's only the monitoring code that's susceptible to a panic - scanning is safe because it skips files with invalid utf-8 before matching, and everything else seems to happen after the scan is complete and shouldn't encounter any weird strings as they are ignored. I think it's best to add a guard to Match() for good measure and open an issue on the glob repo, and change the behavior to include invalid utf-8 files by default instead of ignoring them and handling them similarly to other invalid filenames - at least until glob is updated pattern match them without panicking.
Offending code that doesn't filter invalid utf-8 btw:
@kylosus Don't confuse the requirement for valid UTF-8, which is fundamental, with the bug in the glob matcher, which is just a bug. :) Apparently in this case things could slip from the watcher to the glob library and cause a panic there before passing any of our validation stuff, and that's unfortunate, but wouldn't have been a problem otherwise as all the watcher does is trigger the scanner -- which then does the normalisation and validation.
@imsodin but yes the ignore patterns are UTF-8 and the things passed to it need to be as well. We should do an osutil.NormalizeFilename or something on the way from the watcher to the ignore matcher...
changed the title [-]panic upon start - slice bounds out of range[/-][+]Panic in ignore matching on invalid UTF-8 from filesystem watcher[/+]on Feb 13, 2024
bugA problem with current functionality, as opposed to missing functionality (enhancement)frozen-due-to-ageIssues closed and untouched for a long time, together with being locked for discussionneeds-triageNew issues needed to be validated
Activity
calmh commentedon Jan 25, 2024
Looks like an ignore pattern not understood by the matching library, what are your ignore patterns?
(Relevant part of the trace, for searchability etc:)
kylosus commentedon Jan 26, 2024
It indeed was an ignore pattern that caused the panic:
Works normally if I remove this line.
calmh commentedon Jan 26, 2024
Interesting. That doesn't cause an issue for me. Might there be something in particular with the file names it matches against that cause it?
kylosus commentedon Jan 26, 2024
I found the source of the problem after some investigating. I had a file in one of my folders with the filename
�X!(don't ask me how, I don't know either) which is apparently invalid utf-8 and causes glob to panic for some patterns. I noticedMatch()inignore.gogets called with the same file multiple times and the instance originating from here -> here leads to a panic.Should
Matcher.Match()check for valid utf8 before proceeding? Adding a check there fixes the issue, but I feel like the file should have been filtered out before it gets to that stage, maybe insyncthing/lib/fs/basicfs_watch.go
Line 40 in 45beb28
Side note, method names in a lot of places are very confusing - many functions imply their parameters are dirs but are called with dir and file paths, like this,
calmh commentedon Jan 26, 2024
Nice catch. The glob package is known flaky, especially with non-ASCII things, but we're currently a bit tightly tied to it for it to be an easy switch.
It was my impression that we filter quite strictly for valid UTF-8 and don't accept other forms of names, but perhaps not in the watching code.
imsodin commentedon Jan 26, 2024
We pass a function to check things to be ignored or not to the notify library. And that library probably doesn't have any such checks.
https://github.com/syncthing/notify/blob/main/notify.go#L73
kylosus commentedon Jan 26, 2024
I think it would make the most sense to do the utf-8 check in the Match function, since that seems to be the only place in the code where it is relevant because of the glob library. Right now it's invoked from multiple places and at least one of them doesn't do all the necessary sanitization.
That being said, it's a bit unexpected that syncthing silently (unless you're looking at the logs) ignores a file that's not in my ignore list. Regardless of the encoding of the file name, it is still a valid file in my file system and I would prefer syncthing to include it by default instead of ignoring it.
calmh commentedon Jan 26, 2024
All parts of Syncthing depend on being able to understand the filename encoding, as it needs to change that encoding between systems, as not all systems use the same encoding. Treating it as just an opaque string of bytes doesn't work, unfortunately.
kylosus commentedon Jan 26, 2024
I see, I didn't consider cross-compatibility. I assume you have already considered replacing disallowed characters in the file names similar to what
lib/fs/util.godoes for Folders or keeping some sort of translation/mapping in the db and decided against it?Anyhow, would it make sense to repurpose
WindowsInvalidFilenameto filter out all disallowed filenames and use it in the initial walk and monitoring steps to have a more consistent behavior? Correct me if I'm wrong but from what I could tell, it seems other invalid files likeCON, etc. are indexed and pushed, and only fail when they are pulled by a Windows client with an error shown on the GUI. The behavior is inconsistent with non utf-8 files, which are just ignored. I'd argue non utf-8 filenames should also be pushed if they can be pulled by the receiving end, or there should at least be an option to bypass the compatibility filters. I'm willing to provide the PR for this if the devs agree to it.imsodin commentedon Jan 26, 2024
I am a bit confused about the valid UTF-8 requirement - we do unicode normalisation in the protocol layer, but that never fails (doesn't return an error). And that's only in the protocol layer, that e.g. doesn't happen in the scanner. I don't see anything else doing validation from a quick look (very good chance I simply missed it).
So as far as I see there's nothing preventing such an invalid filename to get to the matching library even without filesystem watching. You could test that by disabling watching and seeing if the same panic occurs during scanning. If that's the case, then I think indeed the right thing to do is to add some validation in the matcher library. And probably err on the side of not ignoring it?
calmh commentedon Jan 26, 2024
The scanner normalises, and there shouldn't be any other ways in for a path.
imsodin commentedon Jan 26, 2024
... That was a very good check I did. First it validates utf-8 (https://github.com/syncthing/syncthing/blob/main/lib/scanner/walk.go#L279) then it normlizes inside
handleItem(after ignoring). So yeah, seems simple and consistent to also do autf8.ValidStringin the fs watcher code to prevent that panic. I'd probably not ignore failures when watching - might be useful to get the error during the scan afterwards to get the users attention. On the other hand the exact same argument could be made to ignore it: Avoid confusing the user, who potentially has no clue what to do with an invalid file anyway xDkylosus commentedon Jan 26, 2024
I don't quite get the valid utf-8 requirment either, especially when other "invalid" files are handled differently.
From what I could gather in my tests, it's only the monitoring code that's susceptible to a panic - scanning is safe because it skips files with invalid utf-8 before matching, and everything else seems to happen after the scan is complete and shouldn't encounter any weird strings as they are ignored. I think it's best to add a guard to
Match()for good measure and open an issue on the glob repo, and change the behavior to include invalid utf-8 files by default instead of ignoring them and handling them similarly to other invalid filenames - at least until glob is updated pattern match them without panicking.Offending code that doesn't filter invalid utf-8 btw:
syncthing/lib/fs/basicfs_watch.go
Line 45 in 45beb28
calmh commentedon Jan 26, 2024
@kylosus Don't confuse the requirement for valid UTF-8, which is fundamental, with the bug in the glob matcher, which is just a bug. :) Apparently in this case things could slip from the watcher to the glob library and cause a panic there before passing any of our validation stuff, and that's unfortunate, but wouldn't have been a problem otherwise as all the watcher does is trigger the scanner -- which then does the normalisation and validation.
@imsodin but yes the ignore patterns are UTF-8 and the things passed to it need to be as well. We should do an
osutil.NormalizeFilenameor something on the way from the watcher to the ignore matcher...Added a guard for invalid UTF-8 strings
lib/fs: Add invalid UTF-8 guards to watcher (fixes #9369) (#9372)
Merge branch 'main' into geoip
[-]panic upon start - slice bounds out of range[/-][+]Panic in ignore matching on invalid UTF-8 from filesystem watcher[/+]Merge tag 'v1.27.4' into ios_try_syncthing_v1.27.4
Merge branch 'main' into integrationtests
Merge branch 'main' into integrationtests