Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher surpassing file system permissions

While experimenting with FileSystemWatcher, I've found out that it somehow surpasses my user account's permissions to files and folders, and will raise change events with information about what has changed in files and folders that you don't even have access to.

I have two questions about that:

1) Why does this happen ?
2) Is this a problem in the AD configuration ? how do I fix it ?
3) Is there any way to gather these files, or even create a FileSystemInfo of them to get more info about the files (not only the changes made on them) ?

As far as I've tried, only the FileSystemWatcher immune to the restrictions, I can't run any other thing over it, here's a list of what I've tried:

  • File.Exists
  • Directory.Exists
  • FileInfo instance on found files
  • DirectoryInfo instance on found files
  • File.Copy
  • File.Delete

Update: Tried helge's solution, with somethin similar to what he's sugested, not through windows' api, but through the command prompt:

robocopy /B \myserver\folder c:\somefolder

Best command name ever.

You can check through robocopy that /B stands for "backup mode", which is what helge's suggested that would be the cause to this security surpassing.

I'll try anything, I want to find out what exactly causes FileSystemWatcher to be able to watch folders I do not have permission to open. Knowing why, I want to learn both how to block FileSystemWatcher, and how to gather found files.

I'd make a survey if I was with my personal account. Please, can someone help me ? I'll write a blog post about the solution, among other things that might help anyone with the same doubt in the future.

like image 239
DevexPP Avatar asked Jan 07 '11 12:01

DevexPP


1 Answers

According to this answer on SO the FileSystemWatcher is based on the API function ReadDirectoryChangesW. If that is true it explains the behavior witnessed by you - and why that is not a security hole.

As documented on MSDN ReadDirectoryChangesW needs the privilege SeBackupPrivilege (which is requested by the parameter FILE_FLAG_BACKUP_SEMANTICS to CreateFile). If a file is opened in that mode, the returned handle grants full access to the file, circumventing access checks. This feature is designed for backup programs that need to be able to read everything on disk regardless of permissions.

This is not a security hole because the privilege SeBackupPrivilege which is required for this to work is by default granted to administrators only. Administrators, and in fact anyone with physical access to a machine, are always capable of taking control of and reading every file - unless it is encrypted.

As to which functions can be used to access files in backup mode: There is at least BackupRead for reading. Enumeration is easily possible with FindFirstFile/FindNextFile. Of course this requires the real Windows API, not the crippled .NET file system functions.

like image 189
Helge Klein Avatar answered Sep 21 '22 10:09

Helge Klein