| 4132 | } |
| 4133 | |
| 4134 | static bool win_private_directory_tree_secure(const wchar_t *directory_path) { |
| 4135 | if (!directory_path) { |
| 4136 | return false; |
| 4137 | } |
| 4138 | size_t length = wcslen(directory_path); |
| 4139 | bool drive_absolute = length >= 4 && |
| 4140 | ((directory_path[0] >= L'A' && directory_path[0] <= L'Z') || |
| 4141 | (directory_path[0] >= L'a' && directory_path[0] <= L'z')) && |
| 4142 | directory_path[1] == L':' && |
| 4143 | (directory_path[2] == L'\\' || directory_path[2] == L'/'); |
| 4144 | if (!drive_absolute) { |
| 4145 | /* Local current-user ACLs do not provide the intended guarantee for |
| 4146 | * UNC/device namespaces. Daemon cache logs must stay on a local |
| 4147 | * absolute drive path. */ |
| 4148 | return false; |
| 4149 | } |
| 4150 | wchar_t *path = wide_copy(directory_path); |
| 4151 | if (!path) { |
| 4152 | return false; |
| 4153 | } |
| 4154 | for (size_t i = 0; i < length; i++) { |
| 4155 | if (path[i] == L'/') { |
| 4156 | path[i] = L'\\'; |
| 4157 | } |
| 4158 | } |
| 4159 | win_security_t security; |
| 4160 | if (!win_security_init(&security)) { |
| 4161 | free(path); |
| 4162 | return false; |
| 4163 | } |
| 4164 | bool ok = true; |
| 4165 | size_t component_start = 3; |
| 4166 | for (size_t i = component_start; ok && i <= length; i++) { |
| 4167 | if (i < length && path[i] != L'\\') { |
| 4168 | continue; |
| 4169 | } |
| 4170 | if (i == component_start) { |
| 4171 | component_start = i + 1; |
| 4172 | continue; |
| 4173 | } |
| 4174 | wchar_t saved = path[i]; |
| 4175 | path[i] = L'\0'; |
| 4176 | const wchar_t *component = path + component_start; |
| 4177 | if (wcscmp(component, L".") == 0 || wcscmp(component, L"..") == 0) { |
| 4178 | ok = false; |
| 4179 | } else { |
| 4180 | DWORD attributes = GetFileAttributesW(path); |
| 4181 | if (attributes == INVALID_FILE_ATTRIBUTES) { |
| 4182 | DWORD error = GetLastError(); |
| 4183 | ok = (error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) && |
| 4184 | CreateDirectoryW(path, &security.attributes) != 0; |
| 4185 | } |
| 4186 | /* Ancestors are observe-only and must already be secure. The |
| 4187 | * final current-user directory is intentionally handled below by |
| 4188 | * win_runtime_directory_secure(), which may replace its DACL. */ |
| 4189 | if (ok && i < length) { |
| 4190 | ok = win_directory_component_secure(&security, path); |
| 4191 | if (!ok) { |
no test coverage detected