@@ -83,7 +83,7 @@ class PluginTests
8383 return true ;
8484 }
8585
86- void ConfigurePlugin (PluginTestType testCase) const
86+ void ConfigurePlugin (PluginTestType testCase, LPCWSTR mountFolder = L" " ) const
8787 {
8888 StopWslService ();
8989 if (!DeleteFile (logFile.c_str ()))
@@ -94,6 +94,7 @@ class PluginTests
9494 const auto testKey = OpenTestRegistryKey (KEY_SET_VALUE );
9595 WriteDword (testKey.get (), nullptr , c_testType, static_cast <DWORD >(testCase));
9696 WriteString (testKey.get (), nullptr , c_logFile, logFile.c_str ());
97+ WriteString (testKey.get (), nullptr , c_mountFolder, mountFolder);
9798
9899 const auto lxssKey =
99100 CreateKey (HKEY_LOCAL_MACHINE , L" SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Lxss\\ Plugins" , KEY_SET_VALUE , nullptr , 0 );
@@ -166,6 +167,62 @@ class PluginTests
166167 ValidateLogFile (ExpectedOutput);
167168 }
168169
170+ WSL2_TEST_METHOD (MountFolderAccess)
171+ {
172+ const auto testFolder = std::filesystem::current_path () / " deny-write" ;
173+ VERIFY_IS_TRUE (std::filesystem::create_directory (testFolder));
174+
175+ auto cleanup = wil::scope_exit_log (WI_DIAGNOSTICS_INFO , [&]() { std::filesystem::remove_all (testFolder); });
176+
177+ const auto user = wil::get_token_information<TOKEN_USER >();
178+ EXPLICIT_ACCESSW access{};
179+ access.grfAccessPermissions = FILE_ADD_FILE ;
180+ access.grfAccessMode = DENY_ACCESS ;
181+ access.grfInheritance = NO_INHERITANCE ;
182+ access.Trustee .TrusteeForm = TRUSTEE_IS_SID ;
183+ access.Trustee .ptstrName = static_cast <LPWSTR >(user->User .Sid );
184+
185+ PACL acl = nullptr ;
186+ wil::unique_hlocal descriptor;
187+ THROW_IF_WIN32_ERROR (GetNamedSecurityInfoW (
188+ testFolder.c_str (), SE_FILE_OBJECT , DACL_SECURITY_INFORMATION , nullptr , nullptr , &acl, nullptr , &descriptor));
189+
190+ wsl::windows::common::security::unique_acl newAcl;
191+ THROW_IF_WIN32_ERROR (SetEntriesInAclW (1 , &access, acl, &newAcl));
192+ THROW_IF_WIN32_ERROR (SetNamedSecurityInfoW (
193+ const_cast <LPWSTR >(testFolder.c_str ()), SE_FILE_OBJECT , DACL_SECURITY_INFORMATION , nullptr , nullptr , newAcl.get (), nullptr ));
194+
195+ const auto testFile = testFolder / L" plugin-test.txt" ;
196+ wil::unique_hfile deniedFile{CreateFileW (testFile.c_str (), GENERIC_WRITE , 0 , nullptr , CREATE_NEW , FILE_ATTRIBUTE_NORMAL , nullptr )};
197+ VERIFY_IS_TRUE (!deniedFile);
198+ VERIFY_ARE_EQUAL (GetLastError (), ERROR_ACCESS_DENIED );
199+
200+ auto resetAcl = wil::scope_exit_log (WI_DIAGNOSTICS_INFO , [&]() {
201+ wsl::windows::common::security::unique_acl restoredAcl;
202+ access.grfAccessPermissions = 0 ;
203+ access.grfAccessMode = REVOKE_ACCESS ;
204+
205+ THROW_IF_WIN32_ERROR (SetEntriesInAclW (1 , &access, acl, &restoredAcl));
206+
207+ THROW_IF_WIN32_ERROR (SetNamedSecurityInfoW (
208+ const_cast <LPWSTR >(testFolder.c_str ()), SE_FILE_OBJECT , DACL_SECURITY_INFORMATION , nullptr , nullptr , restoredAcl.get (), nullptr ));
209+ });
210+
211+ ConfigurePlugin (PluginTestType::MountFolderAccess, testFolder.c_str ());
212+
213+ constexpr auto ExpectedOutput =
214+ LR"( Plugin loaded. TestMode=18
215+ VM created (settings->CustomConfigurationFlags=0)
216+ /bin/sh: line 1: /test-plugin-access/plugin-test.txt: Permission denied
217+ Distribution started, name=test_distro, package=, PidNs=*, InitPid=*, Flavor=debian, Version=13
218+ Distribution Stopping, name=test_distro, package=, PidNs=*, Flavor=debian, Version=13
219+ VM Stopping)" ;
220+
221+ StartWsl (0 );
222+ VERIFY_IS_FALSE (std::filesystem::exists (testFile));
223+ ValidateLogFile (ExpectedOutput);
224+ }
225+
169226 WSL2_TEST_METHOD (CustomKernelOverriddenByPolicy)
170227 {
171228 RegistryKeyChange policy (
0 commit comments