| 4208 | } |
| 4209 | |
| 4210 | static bool win_private_directory_tree_secure(const wchar_t *directory_path) { |
| 4211 | if (!directory_path) { |
| 4212 | return false; |
| 4213 | } |
| 4214 | size_t length = wcslen(directory_path); |
| 4215 | bool drive_absolute = length >= 4 && |
| 4216 | ((directory_path[0] >= L'A' && directory_path[0] <= L'Z') || |
| 4217 | (directory_path[0] >= L'a' && directory_path[0] <= L'z')) && |
| 4218 | directory_path[1] == L':' && |
| 4219 | (directory_path[2] == L'\\' || directory_path[2] == L'/'); |
| 4220 | if (!drive_absolute) { |
| 4221 | /* Local current-user ACLs do not provide the intended guarantee for |
| 4222 | * UNC/device namespaces. Daemon cache logs must stay on a local |
| 4223 | * absolute drive path. */ |
| 4224 | return false; |
| 4225 | } |
| 4226 | wchar_t *path = wide_copy(directory_path); |
| 4227 | if (!path) { |
| 4228 | return false; |
| 4229 | } |
| 4230 | for (size_t i = 0; i < length; i++) { |
| 4231 | if (path[i] == L'/') { |
| 4232 | path[i] = L'\\'; |
| 4233 | } |
| 4234 | } |
| 4235 | win_security_t security; |
| 4236 | if (!win_security_init(&security)) { |
| 4237 | free(path); |
| 4238 | return false; |
| 4239 | } |
| 4240 | bool ok = true; |
| 4241 | size_t component_start = 3; |
| 4242 | for (size_t i = component_start; ok && i <= length; i++) { |
| 4243 | if (i < length && path[i] != L'\\') { |
| 4244 | continue; |
| 4245 | } |
| 4246 | if (i == component_start) { |
| 4247 | component_start = i + 1; |
| 4248 | continue; |
| 4249 | } |
| 4250 | wchar_t saved = path[i]; |
| 4251 | path[i] = L'\0'; |
| 4252 | const wchar_t *component = path + component_start; |
| 4253 | if (wcscmp(component, L".") == 0 || wcscmp(component, L"..") == 0) { |
| 4254 | ok = false; |
| 4255 | } else { |
| 4256 | DWORD attributes = GetFileAttributesW(path); |
| 4257 | if (attributes == INVALID_FILE_ATTRIBUTES) { |
| 4258 | DWORD error = GetLastError(); |
| 4259 | ok = (error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) && |
| 4260 | CreateDirectoryW(path, &security.directory_attributes) != 0; |
| 4261 | } |
| 4262 | /* Ancestors are observe-only and must already be secure. The |
| 4263 | * final current-user directory is intentionally handled below by |
| 4264 | * win_runtime_directory_secure(), which may replace its DACL. */ |
| 4265 | if (ok && i < length) { |
| 4266 | ok = win_directory_component_secure(&security, path); |
| 4267 | if (!ok) { |
no test coverage detected