| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | LxssInstance.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains lxss instance declarations. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | |
| 17 | #include "LxssIpTables.h" |
| 18 | #include "LxssConsoleManager.h" |
| 19 | #include "helpers.hpp" |
| 20 | #include "Lifetime.h" |
| 21 | #include "LxssCreateProcess.h" |
| 22 | #include "LxssServerPort.h" |
| 23 | #include "filesystem.hpp" |
| 24 | #include "WslCoreHostDnsInfo.h" |
| 25 | |
| 26 | class LxssInstance; |
| 27 | |
| 28 | /// <summary> |
| 29 | /// Represents an instance. Instances may or may not be running. This object is |
| 30 | /// created via LxssUserSession::CreateInstance. |
| 31 | /// </summary> |
| 32 | class LxssInstance : public LxssRunningInstance |
| 33 | { |
| 34 | public: |
| 35 | /// <summary> |
| 36 | /// Sets up a new LxssInstance. |
| 37 | /// </summary> |
| 38 | LxssInstance( |
| 39 | _In_ const GUID& InstanceId, |
| 40 | _In_ const LXSS_DISTRO_CONFIGURATION& Configuration, |
| 41 | _In_ ULONG DefaultUid, |
| 42 | _In_ ULONG64 ClientLifetimeId, |
| 43 | _In_ const std::function<void()>& TerminationCallback, |
| 44 | _In_ const std::function<void()>& UpdateInitCallback, |
| 45 | _In_ ULONG Flags, |
| 46 | _In_ int IdleTimeout); |
| 47 | |
| 48 | virtual ~LxssInstance(); |
| 49 | |
| 50 | void CreateLxProcess( |
| 51 | _In_ const CreateLxProcessData& CreateProcessData, |
| 52 | _In_ const CreateLxProcessContext& CreateProcessContext, |
| 53 | _In_ const CreateLxProcessConsoleData& ConsoleData, |
| 54 | _In_ SHORT Columns, |
| 55 | _In_ SHORT Rows, |
| 56 | _In_ PLXSS_STD_HANDLES StdHandles, |
| 57 | _Out_ GUID* InstanceId, |
| 58 | _Out_ HANDLE* ProcessHandle, |
| 59 | _Out_ HANDLE* ServerHandle, |
| 60 | _Out_ HANDLE* StandardIn, |
| 61 | _Out_ HANDLE* StandardOut, |
| 62 | _Out_ HANDLE* StandardErr, |
| 63 | _Out_ HANDLE* CommunicationChannel, |
| 64 | _Out_ HANDLE* InteropSocket) override; |
| 65 | |
| 66 | /// <returns> |
| 67 | /// The instance's client identifier. |
| 68 | /// </returns> |
| 69 | ULONG GetClientId() const override; |
| 70 | |
| 71 | /// <returns> |
| 72 | /// The distribution id. |
| 73 | /// </returns> |
| 74 | GUID GetDistributionId() const override; |
| 75 | |
| 76 | /// <returns> |
| 77 | /// The message port to the init daemon. |
| 78 | /// </returns> |
| 79 | std::shared_ptr<LxssPort> GetInitPort() override; |
| 80 | |
| 81 | /// <returns> |
| 82 | /// Informs the instance that the timezone has changed. |
| 83 | /// </returns> |
| 84 | void UpdateTimezone() override; |
| 85 | |
| 86 | /// <returns> |
| 87 | /// The unique lifetime manager identifier for the instance. |
| 88 | /// </returns> |
| 89 | ULONG64 GetLifetimeManagerId() const override; |
| 90 | |
| 91 | /// <summary> |
| 92 | /// This routine initializes an instance. |
| 93 | /// </summary> |
| 94 | void Initialize() override; |
| 95 | |
| 96 | /// <returns> |
| 97 | /// Calls the termination callback that was registered when the instance starts. |
| 98 | /// </returns> |
| 99 | void OnTerminated() const; |
| 100 | |
| 101 | /// <summary> |
| 102 | /// Requests for the instance to stop. |
| 103 | /// </summary> |
| 104 | bool RequestStop(_In_ bool Force) override; |
| 105 | |
| 106 | /// <summary> |
| 107 | /// Stops the instance. Terminates the LXSS instance, and potentially |
| 108 | /// deletes the directories created for runtime of the instance. |
| 109 | /// This method can be called at any time (started or stopped) and will do the right cleanup. |
| 110 | /// </summary> |
| 111 | void Stop() override; |
| 112 | |
| 113 | /// <summary> |
| 114 | /// Registers connection targets with the Plan 9 Redirector for the calling user, if they're |
| 115 | /// not already registered. |
| 116 | /// </summary> |
| 117 | void RegisterPlan9ConnectionTarget(_In_ HANDLE userToken) override; |
| 118 | |
| 119 | /// <summary> |
| 120 | /// Returns information about the distribution. |
| 121 | /// </summary> |
| 122 | const WSLDistributionInformation* DistributionInformation() const noexcept override; |
| 123 | |
| 124 | protected: |
| 125 | /// <summary> |
| 126 | /// Configures the filesystem for this instance. Creates both "RootFS" |
| 127 | /// and a "temp" directory that LXSS requires to function. |
| 128 | /// </summary> |
| 129 | void _ConfigureFilesystem(_In_ ULONG Flags); |
| 130 | |
| 131 | /// <summary> |
| 132 | /// Creates the backing LXSS instance and starts it. |
| 133 | /// </summary> |
| 134 | void _StartInstance(_In_ ULONG DistributionFlags); |
| 135 | |
| 136 | /// <summary> |
| 137 | /// Initializes mount points to be passed during instance creation. |
| 138 | /// </summary> |
| 139 | std::vector<wsl::windows::common::filesystem::unique_lxss_addmount> _InitializeMounts() const; |
| 140 | |
| 141 | /// <summary> |
| 142 | /// Server port for listening and accepting of new connections. |
| 143 | /// </summary> |
| 144 | std::shared_ptr<LxssServerPort> m_ServerPort; |
| 145 | |
| 146 | /// <summary> |
| 147 | /// Message port to the init process. |
| 148 | /// </summary> |
| 149 | std::shared_ptr<LxssMessagePort> m_InitMessagePort; |
| 150 | |
| 151 | /// <summary> |
| 152 | /// Creates a process in the instance. |
| 153 | /// </summary> |
| 154 | wil::unique_handle _CreateLxProcess( |
| 155 | _In_ const std::shared_ptr<LxssMessagePort>& MessagePort, |
| 156 | _In_ const CreateLxProcessData& CreateProcessData, |
| 157 | _In_ const std::vector<wil::unique_handle>& StdHandles, |
| 158 | _In_ const wil::unique_handle& Token, |
| 159 | _In_ ULONG DefaultUid, |
| 160 | _Out_opt_ PHANDLE ServerPortHandle); |
| 161 | |
| 162 | /// <summary> |
| 163 | /// Creates a create process message. |
| 164 | /// </summary> |
| 165 | std::vector<gsl::byte> _CreateLxProcessMarshalMessage( |
| 166 | _In_ const std::shared_ptr<LxssMessagePort>& MessagePort, |
| 167 | _In_ const CreateLxProcessData& CreateProcessData, |
| 168 | _In_ const std::vector<wil::unique_handle>& StdHandles, |
| 169 | _In_ const wil::unique_handle& Token, |
| 170 | _In_ ULONG DefaultUid) const; |
| 171 | |
| 172 | /// <summary> |
| 173 | /// Cleans up orphaned handles from a create process message. |
| 174 | /// </summary> |
| 175 | static void _ReleaseHandlesFromLxProcessMarshalMessage(_In_ const std::shared_ptr<LxssMessagePort>& MessagePort, _In_ PLX_INIT_CREATE_PROCESS MessageLocal); |
| 176 | |
| 177 | /// <summary> |
| 178 | /// Initializes networking information and registers for WNF network state change notifications. |
| 179 | /// </summary> |
| 180 | void _InitializeNetworking(); |
| 181 | |
| 182 | /// <summary> |
| 183 | /// Updates networking information for the instance. |
| 184 | /// </summary> |
| 185 | void _UpdateNetworkInformation(); |
| 186 | |
| 187 | /// <summary> |
| 188 | /// Initializes communication channel to the init process. |
| 189 | /// </summary> |
| 190 | void _InitiateConnectionToInitProcess(); |
| 191 | |
| 192 | /// <summary> |
| 193 | /// Initializes configuration information for the instance. Updates host name |
| 194 | /// information for the instance and supplies the list of DrvFs volumes to mount. |
| 195 | /// </summary> |
| 196 | void _InitializeConfiguration(_In_ const std::filesystem::path& Plan9SocketPath); |
| 197 | |
| 198 | /// <summary> |
| 199 | /// Writes out configuration files used by a running Lx instance. |
| 200 | /// </summary> |
| 201 | void _UpdateNetworkConfigurationFiles(_In_ bool UpdateAlways); |
| 202 | |
| 203 | private: |
| 204 | /// <summary> |
| 205 | /// Basic state of this object - instance identifier, instance handle, |
| 206 | /// instance terminated event, and termination callback state. |
| 207 | /// </summary> |
| 208 | GUID m_instanceId; |
| 209 | wil::unique_handle m_instanceHandle; |
| 210 | wil::unique_event m_instanceTerminatedEvent; |
| 211 | std::function<void()> m_terminationCallback; |
| 212 | wil::unique_threadpool_wait m_terminationWait; |
| 213 | wil::unique_handle m_userToken; |
| 214 | |
| 215 | /// <summary> |
| 216 | /// Lock to protect instance state. |
| 217 | /// </summary> |
| 218 | std::mutex m_stateLock; |
| 219 | _Guarded_by_(m_stateLock) bool m_initialized; |
| 220 | _Guarded_by_(m_stateLock) bool m_running; |
| 221 | |
| 222 | /// <summary> |
| 223 | /// Provides iptables emulation support. |
| 224 | /// </summary> |
| 225 | LxssIpTables m_ipTables; |
| 226 | |
| 227 | /// <summary> |
| 228 | /// Settings for updating /etc/resolv.conf. |
| 229 | /// </summary> |
| 230 | bool m_enableVpnDetection; |
| 231 | std::mutex m_resolvConfLock; |
| 232 | _Guarded_by_(m_resolvConfLock) std::string m_lastResolvConfContents; |
| 233 | |
| 234 | /// <summary> |
| 235 | /// Path and directory handles for this instance. The base path is always available, the |
| 236 | /// handles are only valid while the instance is running. |
| 237 | /// </summary> |
| 238 | std::filesystem::path m_tempPath; |
| 239 | wil::unique_hfile m_tempDirectory; |
| 240 | wil::unique_hfile m_rootDirectory; |
| 241 | |
| 242 | /// <summary> |
| 243 | /// Specifies the immutable settings for the instance. These are read from |
| 244 | /// the registry when the instance is created. |
| 245 | /// </summary> |
| 246 | ULONG m_defaultUid; |
| 247 | LXSS_DISTRO_CONFIGURATION m_configuration; |
| 248 | |
| 249 | /// <summary> |
| 250 | /// Specifies information about the distro. |
| 251 | /// </summary> |
| 252 | WSLDistributionInformation m_distributionInfo{}; |
| 253 | |
| 254 | /// <summary> |
| 255 | /// This job object contains all pico processes within the instance. |
| 256 | /// </summary> |
| 257 | wil::unique_handle m_instanceJob; |
| 258 | |
| 259 | /// <summary> |
| 260 | /// Handle for network state change notifications. |
| 261 | /// </summary> |
| 262 | wsl::core::networking::unique_notify_handle m_networkNotificationHandle; |
| 263 | |
| 264 | /// <summary> |
| 265 | /// Lifetime manager ID for registering NT client termination callbacks. |
| 266 | /// </summary> |
| 267 | ULONG64 m_ntClientLifetimeId; |
| 268 | |
| 269 | /// <summary> |
| 270 | /// Class to keep track of client process and consoles. |
| 271 | /// </summary> |
| 272 | std::shared_ptr<ConsoleManager> m_consoleManager; |
| 273 | |
| 274 | /// <summary> |
| 275 | /// Stores if the basic integrity level check is enabled. |
| 276 | /// </summary> |
| 277 | bool m_instanceBasicIntegrityLevelCheckEnabled; |
| 278 | |
| 279 | /// <summary> |
| 280 | /// The basic integrity level of the caller that created the instance. |
| 281 | /// </summary> |
| 282 | DWORD m_instanceBasicIntegrityLevel; |
| 283 | |
| 284 | /// <summary> |
| 285 | /// The authentication IDs used for the Plan 9 redirector connection target. |
| 286 | /// </summary> |
| 287 | wsl::windows::common::redirector::ConnectionTargetManager m_redirectorConnectionTargets; |
| 288 | |
| 289 | std::thread m_oobeThread; |
| 290 | wil::unique_event m_destroyingEvent{wil::EventOptions::ManualReset}; |
| 291 | wil::unique_event m_oobeCompleteEvent; |
| 292 | }; |