Make the post import distribution checks less strict (#13036)

* Make the post import distribution checks less strict * Use /etc * Update comment * Explicitely create /etc/wsl.conf

Blue committed Jun 3, 2025 at 22:49 UTC e8813245515789e63948987cda721b2cd5cda2cd
2 files changed +21 -5
src/linux/init/main.cpp
+8 -5
@@ -2618,17 +2618,20 @@ void PostProcessImportedDistribution(wsl::shared::MessageWriter<LX_MINI_INIT_IMP
2618 THROW_LAST_ERROR_IF(chdir(ExtractedPath) < 0);
2619 THROW_LAST_ERROR_IF(chroot(".") < 0);
2620
2621 + Message->ValidDistribution = false;
2622 +
2623 for (auto* path : {"/etc", "/bin/sh"})
2624 {
2623 - if (access(path, F_OK) < 0)
2625 + if (access(path, F_OK) >= 0)
2626 {
2625 - LOG_ERROR("Failed to access {} {}", path, errno);
2626 - Message->ValidDistribution = false;
2627 - return;
2627 + Message->ValidDistribution = true;
2628 }
2629 }
2630
2631 - Message->ValidDistribution = true;
2631 + if (!Message->ValidDistribution)
2632 + {
2633 + return;
2634 + }
2635
2636 auto [flavor, version] = UtilReadFlavorAndVersion("/etc/os-release");
2637
test/windows/UnitTests.cpp
+13
@@ -5854,6 +5854,19 @@ Error code: Wsl/InstallDistro/WSL_E_INVALID_JSON\r\n",
5854 // TODO: Uncomment once SetVersionDebug is removed from the tests .wslconfig.
5855 // VERIFY_ARE_EQUAL(err, L"");
5856 }
5857 +
5858 + // Validate that tars containing /etc, but not /bin/sh are accepted.
5859 + if (LxsstuVmMode())
5860 + {
5861 + auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, []() { LxsstuLaunchWsl(L"--unregister empty-distro"); });
5862 +
5863 + DistroFileChange conf(L"/etc/wsl.conf", false);
5864 + conf.SetContent(L"");
5865 +
5866 + auto [out, err] = LxsstuLaunchWslAndCaptureOutput(
5867 + L"tar cf - /etc/wsl.conf | wsl.exe --install --from-file - --name empty-distro --no-launch "
5868 + L"--version 2");
5869 + }
5870 }
5871
5872 TEST_METHOD(ImportExportStdout)