Skip to content

Commit f6bd5d2

Browse files
OneBlueJohnMcPMS
andauthored
Backport various fixes to the 2.7 release branch (#41351)
* Mount plugin folders on behalf of the user owning the wsl session (#41331) * Ignore GSL warning (#40560) * Use an azure key vault to store Github tokens for automated pull requests & release creation (#41158) * Experiment with keyvault * Iterate * Iterate * Prepare for PR * Fix bad merge --------- Co-authored-by: JohnMcPMS <[email protected]>
1 parent acbcb81 commit f6bd5d2

8 files changed

Lines changed: 103 additions & 6 deletions

File tree

‎.pipelines/flight-stage.yml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,18 @@ stages:
109109
inputs:
110110
artifactFeeds: 'wsl'
111111

112+
- task: AzureKeyVault@2
113+
displayName: Fetch GitHub release token from Key Vault
114+
inputs:
115+
azureSubscription: 'Azure-Connection'
116+
KeyVaultName: 'wsl-ci-key-vault'
117+
SecretsFilter: 'GithubReleaseToken'
118+
112119
# Create a draft github release
113120
- powershell: |
114121
pip install --user -r tools/devops/requirements.txt
115122
python tools/devops/create-release.py '${{ parameters.packageVersion }}' drop\bundle\Microsoft.WSL_${{ parameters.packageVersion }}.0_x64_ARM64.msixbundle drop\bundle\wsl.${{ parameters.packageVersion }}.0.arm64.msi drop\bundle\wsl.${{ parameters.packageVersion }}.0.x64.msi --no-fetch --github-token "$env:token" ${{ iif(parameters.publishPackage, '--publish --auto-release-notes', '--use-current-ref') }}
116123
117124
displayName: Create GitHub release
118125
env:
119-
token: $(GITHUB_RELEASE_TOKEN)
126+
token: $(GithubReleaseToken)

‎.pipelines/wsl-build-nightly-localization.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ steps:
4646
inputs:
4747
artifactFeeds: 'wsl'
4848

49+
- task: AzureKeyVault@2
50+
displayName: Fetch GitHub PR token from Key Vault
51+
condition: and(succeeded(), in(variables['Build.Reason'], 'Schedule', 'Manual'))
52+
inputs:
53+
azureSubscription: 'Azure-Connection'
54+
KeyVaultName: 'wsl-ci-key-vault'
55+
SecretsFilter: 'GithubPRToken'
56+
4957
- powershell: |
5058
pip install --user -r tools/devops/requirements.txt
5159
python tools/devops/create-change.py . "$env:token" "WSL localization" "Localization change from build: $env:buildId" "user/localization/$env:buildId" "$env:targetBranch"

‎.pipelines/wsl-build-notice.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ steps:
2626
inputs:
2727
artifactFeeds: 'wsl'
2828

29+
- task: AzureKeyVault@2
30+
displayName: Fetch GitHub PR token from Key Vault
31+
inputs:
32+
azureSubscription: 'Azure-Connection'
33+
KeyVaultName: 'wsl-ci-key-vault'
34+
SecretsFilter: 'GithubPRToken'
35+
2936
- powershell: |
3037
pip install --user -r tools/devops/requirements.txt
3138
python tools/devops/create-change.py . "$env:token" "WSL notice" "Notice change from build: $env:buildId" "user/notice/$env:buildId" "$env:targetBranch"

‎src/windows/common/precomp.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Module Name:
1212
1313
--*/
1414

15-
#pragma warning(disable : 4200 4214)
15+
#pragma warning(disable : 4200 4214 4875)
1616

1717
#ifdef __cplusplus
1818
#define _WINSOCKAPI_

‎src/windows/service/exe/WslCoreVm.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,8 @@ void WslCoreVm::MountRootNamespaceFolder(_In_ LPCWSTR HostPath, _In_ LPCWSTR Gue
20552055
auto lock = m_lock.lock_exclusive();
20562056

20572057
const auto flags = (ReadOnly ? hcs::Plan9ShareFlags::ReadOnly : hcs::Plan9ShareFlags::None) | hcs::Plan9ShareFlags::AllowOptions;
2058-
wsl::windows::common::hcs::AddPlan9Share(m_system.get(), Name, Name, HostPath, LX_INIT_UTILITY_VM_PLAN9_PORT, flags);
2058+
wsl::windows::common::hcs::AddPlan9Share(
2059+
m_system.get(), Name, Name, HostPath, LX_INIT_UTILITY_VM_PLAN9_PORT, flags, m_userToken.get());
20592060

20602061
wsl::shared::MessageWriter<LX_MINI_INIT_MOUNT_FOLDER_MESSAGE> message(LxMiniInitMountFolder);
20612062
message.WriteString(message->PathIndex, GuestPath);

‎test/windows/PluginTests.cpp‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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(

‎test/windows/PluginTests.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ enum class PluginTestType
3737
InitPidIsDifferent,
3838
FailToRegisterUnregisterDistro,
3939
RunDistroCommand,
40-
GetUsername
40+
GetUsername,
41+
MountFolderAccess
4142
};
4243

4344
constexpr auto c_testType = L"TestType";
4445
constexpr auto c_logFile = L"LogFile";
46+
constexpr auto c_mountFolder = L"MountFolder";
4547

4648
inline wil::unique_hkey OpenTestRegistryKey(REGSAM AccessMask)
4749
{

‎test/windows/testplugin/Plugin.cpp‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ HRESULT OnVmStarted(const WSLSessionInformation* Session, const WSLVmCreationSet
100100
return E_ABORT;
101101
}
102102
}
103+
else if (g_testType == PluginTestType::MountFolderAccess)
104+
{
105+
const auto key = OpenTestRegistryKey(KEY_READ);
106+
const auto mountSource = ReadString(key.get(), nullptr, c_mountFolder);
107+
108+
RETURN_IF_FAILED(
109+
g_api->MountFolder(Session->SessionId, mountSource.c_str(), L"/test-plugin-access", false, L"test-plugin-access"));
110+
111+
std::vector<const char*> arguments = {"/bin/sh", "-c", "{ echo test > /test-plugin-access/plugin-test.txt; } 2>&1", nullptr};
112+
wil::unique_socket socket;
113+
RETURN_IF_FAILED(g_api->ExecuteBinary(Session->SessionId, arguments[0], arguments.data(), &socket));
114+
115+
const auto output = ReadFromSocket(socket.get());
116+
g_logfile.write(output.data(), output.size());
117+
}
103118
else if (g_testType == PluginTestType::ApiErrors)
104119
{
105120
auto result = g_api->MountFolder(Session->SessionId, L"C:\\DoesNotExit", L"/dummy", true, L"test-plugin-mount");
@@ -349,7 +364,7 @@ EXTERN_C __declspec(dllexport) HRESULT WSLPLUGINAPI_ENTRYPOINTV1(const WSLPlugin
349364
THROW_HR_IF(E_UNEXPECTED, !g_logfile);
350365

351366
g_testType = static_cast<PluginTestType>(ReadDword(key.get(), nullptr, c_testType, static_cast<DWORD>(PluginTestType::Invalid)));
352-
THROW_HR_IF(E_INVALIDARG, static_cast<DWORD>(g_testType) <= 0 || static_cast<DWORD>(g_testType) > static_cast<DWORD>(PluginTestType::GetUsername));
367+
THROW_HR_IF(E_INVALIDARG, static_cast<DWORD>(g_testType) <= 0 || static_cast<DWORD>(g_testType) > static_cast<DWORD>(PluginTestType::MountFolderAccess));
353368

354369
g_logfile << "Plugin loaded. TestMode=" << static_cast<DWORD>(g_testType) << std::endl;
355370
g_api = Api;

0 commit comments

Comments
 (0)