|
6 | 6 | */ |
7 | 7 | #include "foundation/compat.h" |
8 | 8 | #include "foundation/constants.h" |
| 9 | +#include "foundation/secure_random.h" |
9 | 10 |
|
10 | 11 | #include <errno.h> |
11 | 12 | #include <stdlib.h> |
@@ -114,52 +115,80 @@ static bool win_mkdtemp_private_create(const char *path) { |
114 | 115 | } |
115 | 116 |
|
116 | 117 | char *cbm_mkdtemp(char *tmpl) { |
117 | | - /* Build path in static buffer, then copy back to caller. |
118 | | - * Callers must provide buffers >= CBM_SZ_256 bytes (all test code does). */ |
119 | | - static char buf[CBM_SZ_512]; |
| 118 | + /* Per-call storage is required: daemon sessions invoke mkdtemp concurrently. |
| 119 | + * A process-global buffer lets one request overwrite another request's path |
| 120 | + * between expansion, creation, and the copy back to its caller. */ |
| 121 | + char buf[CBM_SZ_512]; |
| 122 | + int written; |
120 | 123 | if (strncmp(tmpl, "/tmp/", 5) == 0) { |
121 | 124 | const char *tmp = getenv("TEMP"); |
122 | 125 | if (!tmp) |
123 | 126 | tmp = getenv("TMP"); |
124 | 127 | if (!tmp) |
125 | 128 | tmp = "."; |
126 | | - snprintf(buf, sizeof(buf), "%s\\%s", tmp, tmpl + 5); |
| 129 | + written = snprintf(buf, sizeof(buf), "%s\\%s", tmp, tmpl + 5); |
127 | 130 | } else { |
128 | | - snprintf(buf, sizeof(buf), "%s", tmpl); |
| 131 | + written = snprintf(buf, sizeof(buf), "%s", tmpl); |
129 | 132 | } |
130 | | - /* Wide-API template expansion: the ANSI CRT interprets the UTF-8 bytes of |
131 | | - * non-ASCII cache/temp components in the local codepage and fails. */ |
132 | | - wchar_t *wide_template = cbm_utf8_to_wide(buf); |
133 | | - if (!wide_template || !_wmktemp(wide_template)) { |
134 | | - free(wide_template); |
| 133 | + if (written < 0 || (size_t)written >= sizeof(buf)) { |
| 134 | + errno = ENAMETOOLONG; |
135 | 135 | return NULL; |
136 | 136 | } |
137 | | - char *expanded = cbm_wide_to_utf8(wide_template); |
138 | | - free(wide_template); |
139 | | - if (!expanded || strlen(expanded) >= sizeof(buf)) { |
140 | | - free(expanded); |
| 137 | + |
| 138 | + size_t length = strlen(buf); |
| 139 | + if (length < 6 || strcmp(buf + length - 6, "XXXXXX") != 0) { |
| 140 | + errno = EINVAL; |
141 | 141 | return NULL; |
142 | 142 | } |
143 | | - strcpy(buf, expanded); |
144 | | - free(expanded); |
145 | | - if (!win_mkdtemp_private_create(buf)) { |
146 | | - /* One-time note: every private-namespace validation downstream |
147 | | - * depends on the explicit descriptor, so a silent fallback turns |
148 | | - * into unexplained owner/DACL refusals far from this call site. */ |
149 | | - static bool fallback_reported; |
| 143 | + |
| 144 | + static const char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 145 | + bool created = false; |
| 146 | + for (int attempt = 0; attempt < 128; attempt++) { |
| 147 | + unsigned char random_suffix[6]; |
| 148 | + if (!cbm_secure_random(random_suffix, sizeof(random_suffix))) { |
| 149 | + errno = EIO; |
| 150 | + return NULL; |
| 151 | + } |
| 152 | + for (size_t index = 0; index < sizeof(random_suffix); index++) { |
| 153 | + buf[length - sizeof(random_suffix) + index] = |
| 154 | + alphabet[random_suffix[index] % (sizeof(alphabet) - 1)]; |
| 155 | + } |
| 156 | + |
| 157 | + if (win_mkdtemp_private_create(buf)) { |
| 158 | + created = true; |
| 159 | + break; |
| 160 | + } |
| 161 | + |
| 162 | + /* Keep the existing compatibility fallback when an explicit private |
| 163 | + * descriptor is unavailable. A name collision is retried; any other |
| 164 | + * filesystem refusal is returned to the caller immediately. */ |
150 | 165 | DWORD create_error = GetLastError(); |
151 | 166 | wchar_t *wide_directory = cbm_utf8_to_wide(buf); |
| 167 | + errno = 0; |
152 | 168 | int mkdir_result = wide_directory ? _wmkdir(wide_directory) : -1; |
| 169 | + int mkdir_error = errno; |
153 | 170 | free(wide_directory); |
154 | | - if (mkdir_result != 0) |
155 | | - return NULL; |
156 | | - if (!fallback_reported) { |
157 | | - fallback_reported = true; |
158 | | - (void)fprintf(stderr, |
159 | | - "warning: private temp-directory descriptor unavailable " |
160 | | - "(os %lu); using default directory security\n", |
161 | | - (unsigned long)create_error); |
| 171 | + if (mkdir_result == 0) { |
| 172 | + static volatile LONG fallback_reported; |
| 173 | + if (InterlockedCompareExchange(&fallback_reported, 1, 0) == 0) { |
| 174 | + (void)fprintf(stderr, |
| 175 | + "warning: private temp-directory descriptor unavailable " |
| 176 | + "(os %lu); using default directory security\n", |
| 177 | + (unsigned long)create_error); |
| 178 | + } |
| 179 | + created = true; |
| 180 | + break; |
| 181 | + } |
| 182 | + if (create_error == ERROR_ALREADY_EXISTS || create_error == ERROR_FILE_EXISTS || |
| 183 | + mkdir_error == EEXIST) { |
| 184 | + continue; |
162 | 185 | } |
| 186 | + errno = mkdir_error != 0 ? mkdir_error : EACCES; |
| 187 | + return NULL; |
| 188 | + } |
| 189 | + if (!created) { |
| 190 | + errno = EEXIST; |
| 191 | + return NULL; |
163 | 192 | } |
164 | 193 | /* Normalize to forward slashes. Callers embed this path in JSON repo_path |
165 | 194 | * (where "\t"/"\a" are invalid escapes → index fails) and pass it to git -C. |
|
0 commit comments