]> git.ipfire.org Git - thirdparty/git.git/blame - fsmonitor.c
fsmonitor: avoid global-buffer-overflow READ when checking trivial response
[thirdparty/git.git] / fsmonitor.c
CommitLineData
883e248b
BP
1#include "cache.h"
2#include "config.h"
3#include "dir.h"
4#include "ewah/ewok.h"
5#include "fsmonitor.h"
6#include "run-command.h"
7#include "strbuf.h"
8
56c69100
KW
9#define INDEX_EXTENSION_VERSION1 (1)
10#define INDEX_EXTENSION_VERSION2 (2)
8da2c576
KW
11#define HOOK_INTERFACE_VERSION1 (1)
12#define HOOK_INTERFACE_VERSION2 (2)
883e248b
BP
13
14struct trace_key trace_fsmonitor = TRACE_KEY_INIT(FSMONITOR);
15
16static void fsmonitor_ewah_callback(size_t pos, void *is)
17{
18 struct index_state *istate = (struct index_state *)is;
3444ec2e 19 struct cache_entry *ce;
883e248b 20
3444ec2e
WB
21 if (pos >= istate->cache_nr)
22 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" >= %u)",
23 (uintmax_t)pos, istate->cache_nr);
24
25 ce = istate->cache[pos];
883e248b
BP
26 ce->ce_flags &= ~CE_FSMONITOR_VALID;
27}
28
8da2c576
KW
29static int fsmonitor_hook_version(void)
30{
31 int hook_version;
32
33 if (git_config_get_int("core.fsmonitorhookversion", &hook_version))
34 return -1;
35
36 if (hook_version == HOOK_INTERFACE_VERSION1 ||
37 hook_version == HOOK_INTERFACE_VERSION2)
38 return hook_version;
39
40 warning("Invalid hook version '%i' in core.fsmonitorhookversion. "
41 "Must be 1 or 2.", hook_version);
42 return -1;
43}
44
883e248b
BP
45int read_fsmonitor_extension(struct index_state *istate, const void *data,
46 unsigned long sz)
47{
48 const char *index = data;
49 uint32_t hdr_version;
50 uint32_t ewah_size;
51 struct ewah_bitmap *fsmonitor_dirty;
883e248b 52 int ret;
56c69100
KW
53 uint64_t timestamp;
54 struct strbuf last_update = STRBUF_INIT;
883e248b 55
56c69100 56 if (sz < sizeof(uint32_t) + 1 + sizeof(uint32_t))
883e248b
BP
57 return error("corrupt fsmonitor extension (too short)");
58
59 hdr_version = get_be32(index);
60 index += sizeof(uint32_t);
56c69100
KW
61 if (hdr_version == INDEX_EXTENSION_VERSION1) {
62 timestamp = get_be64(index);
63 strbuf_addf(&last_update, "%"PRIu64"", timestamp);
64 index += sizeof(uint64_t);
65 } else if (hdr_version == INDEX_EXTENSION_VERSION2) {
66 strbuf_addstr(&last_update, index);
67 index += last_update.len + 1;
68 } else {
883e248b 69 return error("bad fsmonitor version %d", hdr_version);
56c69100 70 }
883e248b 71
56c69100 72 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
883e248b
BP
73
74 ewah_size = get_be32(index);
75 index += sizeof(uint32_t);
76
77 fsmonitor_dirty = ewah_new();
78 ret = ewah_read_mmap(fsmonitor_dirty, index, ewah_size);
79 if (ret != ewah_size) {
80 ewah_free(fsmonitor_dirty);
81 return error("failed to parse ewah bitmap reading fsmonitor index extension");
82 }
ba1b9cac 83 istate->fsmonitor_dirty = fsmonitor_dirty;
883e248b 84
61eea521
JH
85 if (!istate->split_index &&
86 istate->fsmonitor_dirty->bit_size > istate->cache_nr)
3444ec2e
WB
87 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
88 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
89
29fbbf43
JH
90 trace2_data_string("index", NULL, "extension/fsmn/read/token",
91 istate->fsmonitor_last_update);
92 trace_printf_key(&trace_fsmonitor,
93 "read fsmonitor extension successful '%s'",
94 istate->fsmonitor_last_update);
883e248b
BP
95 return 0;
96}
97
3bd28eb2
AV
98void fill_fsmonitor_bitmap(struct index_state *istate)
99{
3444ec2e 100 unsigned int i, skipped = 0;
3bd28eb2 101 istate->fsmonitor_dirty = ewah_new();
3444ec2e
WB
102 for (i = 0; i < istate->cache_nr; i++) {
103 if (istate->cache[i]->ce_flags & CE_REMOVE)
104 skipped++;
105 else if (!(istate->cache[i]->ce_flags & CE_FSMONITOR_VALID))
106 ewah_set(istate->fsmonitor_dirty, i - skipped);
107 }
3bd28eb2
AV
108}
109
883e248b
BP
110void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
111{
112 uint32_t hdr_version;
883e248b
BP
113 uint32_t ewah_start;
114 uint32_t ewah_size = 0;
115 int fixup = 0;
116
61eea521
JH
117 if (!istate->split_index &&
118 istate->fsmonitor_dirty->bit_size > istate->cache_nr)
3444ec2e
WB
119 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
120 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
121
56c69100 122 put_be32(&hdr_version, INDEX_EXTENSION_VERSION2);
883e248b
BP
123 strbuf_add(sb, &hdr_version, sizeof(uint32_t));
124
56c69100
KW
125 strbuf_addstr(sb, istate->fsmonitor_last_update);
126 strbuf_addch(sb, 0); /* Want to keep a NUL */
127
883e248b
BP
128 fixup = sb->len;
129 strbuf_add(sb, &ewah_size, sizeof(uint32_t)); /* we'll fix this up later */
130
131 ewah_start = sb->len;
3bd28eb2
AV
132 ewah_serialize_strbuf(istate->fsmonitor_dirty, sb);
133 ewah_free(istate->fsmonitor_dirty);
134 istate->fsmonitor_dirty = NULL;
883e248b
BP
135
136 /* fix up size field */
137 put_be32(&ewah_size, sb->len - ewah_start);
138 memcpy(sb->buf + fixup, &ewah_size, sizeof(uint32_t));
139
29fbbf43
JH
140 trace2_data_string("index", NULL, "extension/fsmn/write/token",
141 istate->fsmonitor_last_update);
142 trace_printf_key(&trace_fsmonitor,
143 "write fsmonitor extension successful '%s'",
144 istate->fsmonitor_last_update);
883e248b
BP
145}
146
147/*
56c69100 148 * Call the query-fsmonitor hook passing the last update token of the saved results.
883e248b 149 */
56c69100 150static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result)
883e248b
BP
151{
152 struct child_process cp = CHILD_PROCESS_INIT;
940b94f3 153 int result;
883e248b 154
735e4173 155 if (!core_fsmonitor)
883e248b
BP
156 return -1;
157
ef8d7ac4
JK
158 strvec_push(&cp.args, core_fsmonitor);
159 strvec_pushf(&cp.args, "%d", version);
160 strvec_pushf(&cp.args, "%s", last_update);
883e248b 161 cp.use_shell = 1;
11cf33be 162 cp.dir = get_git_work_tree();
883e248b 163
940b94f3
JH
164 trace2_region_enter("fsm_hook", "query", NULL);
165
166 result = capture_command(&cp, query_result, 1024);
167
168 if (result)
169 trace2_data_intmax("fsm_hook", NULL, "query/failed", result);
170 else {
171 trace2_data_intmax("fsm_hook", NULL, "query/response-length",
172 query_result->len);
173
174 if (fsmonitor_is_trivial_response(query_result))
175 trace2_data_intmax("fsm_hook", NULL,
176 "query/trivial-response", 1);
177 }
178
179 trace2_region_leave("fsm_hook", "query", NULL);
180
181 return result;
182}
183
184int fsmonitor_is_trivial_response(const struct strbuf *query_result)
185{
186 static char trivial_response[3] = { '\0', '/', '\0' };
940b94f3 187
097ea2c8
AH
188 return query_result->len >= 3 &&
189 !memcmp(trivial_response,
190 &query_result->buf[query_result->len - 3], 3);
883e248b
BP
191}
192
ff03836b 193static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
883e248b 194{
ff03836b
KW
195 int i, len = strlen(name);
196 if (name[len - 1] == '/') {
197
198 /*
199 * TODO We should binary search to find the first path with
200 * TODO this directory prefix. Then linearly update entries
201 * TODO while the prefix matches. Taking care to search without
202 * TODO the trailing slash -- because '/' sorts after a few
203 * TODO interesting special chars, like '.' and ' '.
204 */
205
206 /* Mark all entries for the folder invalid */
207 for (i = 0; i < istate->cache_nr; i++) {
208 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID &&
209 starts_with(istate->cache[i]->name, name))
210 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
211 }
212 /* Need to remove the / from the path for the untracked cache */
213 name[len - 1] = '\0';
214 } else {
215 int pos = index_name_pos(istate, name, strlen(name));
883e248b 216
ff03836b
KW
217 if (pos >= 0) {
218 struct cache_entry *ce = istate->cache[pos];
219 ce->ce_flags &= ~CE_FSMONITOR_VALID;
220 }
883e248b
BP
221 }
222
223 /*
224 * Mark the untracked cache dirty even if it wasn't found in the index
225 * as it could be a new untracked file.
226 */
227 trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
0cacebf0 228 untracked_cache_invalidate_path(istate, name, 0);
883e248b
BP
229}
230
231void refresh_fsmonitor(struct index_state *istate)
232{
883e248b 233 struct strbuf query_result = STRBUF_INIT;
8da2c576
KW
234 int query_success = 0, hook_version = -1;
235 size_t bol = 0; /* beginning of line */
883e248b 236 uint64_t last_update;
56c69100 237 struct strbuf last_update_token = STRBUF_INIT;
883e248b 238 char *buf;
5d137fc2 239 unsigned int i;
883e248b 240
398a3b08 241 if (!core_fsmonitor || istate->fsmonitor_has_run_once)
883e248b 242 return;
8da2c576
KW
243
244 hook_version = fsmonitor_hook_version();
245
398a3b08 246 istate->fsmonitor_has_run_once = 1;
883e248b
BP
247
248 trace_printf_key(&trace_fsmonitor, "refresh fsmonitor");
249 /*
250 * This could be racy so save the date/time now and query_fsmonitor
251 * should be inclusive to ensure we don't miss potential changes.
252 */
253 last_update = getnanotime();
8da2c576
KW
254 if (hook_version == HOOK_INTERFACE_VERSION1)
255 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
883e248b
BP
256
257 /*
8da2c576
KW
258 * If we have a last update token, call query_fsmonitor for the set of
259 * changes since that token, else assume everything is possibly dirty
883e248b
BP
260 * and check it all.
261 */
262 if (istate->fsmonitor_last_update) {
8da2c576
KW
263 if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) {
264 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION2,
265 istate->fsmonitor_last_update, &query_result);
266
267 if (query_success) {
268 if (hook_version < 0)
269 hook_version = HOOK_INTERFACE_VERSION2;
270
271 /*
272 * First entry will be the last update token
273 * Need to use a char * variable because static
274 * analysis was suggesting to use strbuf_addbuf
275 * but we don't want to copy the entire strbuf
6d12b533 276 * only the chars up to the first NUL
8da2c576
KW
277 */
278 buf = query_result.buf;
279 strbuf_addstr(&last_update_token, buf);
280 if (!last_update_token.len) {
281 warning("Empty last update token.");
282 query_success = 0;
283 } else {
284 bol = last_update_token.len + 1;
285 }
286 } else if (hook_version < 0) {
287 hook_version = HOOK_INTERFACE_VERSION1;
288 if (!last_update_token.len)
289 strbuf_addf(&last_update_token, "%"PRIu64"", last_update);
290 }
291 }
292
293 if (hook_version == HOOK_INTERFACE_VERSION1) {
294 query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION1,
295 istate->fsmonitor_last_update, &query_result);
296 }
297
883e248b
BP
298 trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
299 trace_printf_key(&trace_fsmonitor, "fsmonitor process '%s' returned %s",
300 core_fsmonitor, query_success ? "success" : "failure");
301 }
302
303 /* a fsmonitor process can return '/' to indicate all entries are invalid */
8da2c576 304 if (query_success && query_result.buf[bol] != '/') {
883e248b
BP
305 /* Mark all entries returned by the monitor as dirty */
306 buf = query_result.buf;
8da2c576 307 for (i = bol; i < query_result.len; i++) {
883e248b
BP
308 if (buf[i] != '\0')
309 continue;
310 fsmonitor_refresh_callback(istate, buf + bol);
311 bol = i + 1;
312 }
313 if (bol < query_result.len)
314 fsmonitor_refresh_callback(istate, buf + bol);
679f2f9f
US
315
316 /* Now mark the untracked cache for fsmonitor usage */
317 if (istate->untracked)
318 istate->untracked->use_fsmonitor = 1;
883e248b 319 } else {
679f2f9f
US
320
321 /* We only want to run the post index changed hook if we've actually changed entries, so keep track
322 * if we actually changed entries or not */
323 int is_cache_changed = 0;
883e248b 324 /* Mark all entries invalid */
679f2f9f
US
325 for (i = 0; i < istate->cache_nr; i++) {
326 if (istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) {
327 is_cache_changed = 1;
328 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
329 }
330 }
883e248b 331
ca598d5f 332 /* If we're going to check every file, ensure we save the results */
679f2f9f
US
333 if (is_cache_changed)
334 istate->cache_changed |= FSMONITOR_CHANGED;
ca598d5f 335
883e248b
BP
336 if (istate->untracked)
337 istate->untracked->use_fsmonitor = 0;
338 }
339 strbuf_release(&query_result);
340
56c69100
KW
341 /* Now that we've updated istate, save the last_update_token */
342 FREE_AND_NULL(istate->fsmonitor_last_update);
343 istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
883e248b
BP
344}
345
fcd19b09
JH
346/*
347 * The caller wants to turn on FSMonitor. And when the caller writes
348 * the index to disk, a FSMonitor extension should be included. This
349 * requires that `istate->fsmonitor_last_update` not be NULL. But we
350 * have not actually talked to a FSMonitor process yet, so we don't
351 * have an initial value for this field.
352 *
353 * For a protocol V1 FSMonitor process, this field is a formatted
354 * "nanoseconds since epoch" field. However, for a protocol V2
355 * FSMonitor process, this field is an opaque token.
356 *
357 * Historically, `add_fsmonitor()` has initialized this field to the
358 * current time for protocol V1 processes. There are lots of race
359 * conditions here, but that code has shipped...
360 *
361 * The only true solution is to use a V2 FSMonitor and get a current
362 * or default token value (that it understands), but we cannot do that
363 * until we have actually talked to an instance of the FSMonitor process
364 * (but the protocol requires that we send a token first...).
365 *
366 * For simplicity, just initialize like we have a V1 process and require
367 * that V2 processes adapt.
368 */
369static void initialize_fsmonitor_last_update(struct index_state *istate)
370{
371 struct strbuf last_update = STRBUF_INIT;
372
373 strbuf_addf(&last_update, "%"PRIu64"", getnanotime());
374 istate->fsmonitor_last_update = strbuf_detach(&last_update, NULL);
375}
376
883e248b
BP
377void add_fsmonitor(struct index_state *istate)
378{
5d137fc2 379 unsigned int i;
883e248b
BP
380
381 if (!istate->fsmonitor_last_update) {
382 trace_printf_key(&trace_fsmonitor, "add fsmonitor");
383 istate->cache_changed |= FSMONITOR_CHANGED;
fcd19b09 384 initialize_fsmonitor_last_update(istate);
883e248b
BP
385
386 /* reset the fsmonitor state */
387 for (i = 0; i < istate->cache_nr; i++)
388 istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
389
390 /* reset the untracked cache */
391 if (istate->untracked) {
392 add_untracked_cache(istate);
393 istate->untracked->use_fsmonitor = 1;
394 }
395
396 /* Update the fsmonitor state */
397 refresh_fsmonitor(istate);
398 }
399}
400
401void remove_fsmonitor(struct index_state *istate)
402{
403 if (istate->fsmonitor_last_update) {
404 trace_printf_key(&trace_fsmonitor, "remove fsmonitor");
405 istate->cache_changed |= FSMONITOR_CHANGED;
56c69100 406 FREE_AND_NULL(istate->fsmonitor_last_update);
883e248b
BP
407 }
408}
409
410void tweak_fsmonitor(struct index_state *istate)
411{
5d137fc2 412 unsigned int i;
ba1b9cac
AV
413 int fsmonitor_enabled = git_config_get_fsmonitor();
414
415 if (istate->fsmonitor_dirty) {
416 if (fsmonitor_enabled) {
417 /* Mark all entries valid */
418 for (i = 0; i < istate->cache_nr; i++) {
419 istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
420 }
421
422 /* Mark all previously saved entries as dirty */
3444ec2e
WB
423 if (istate->fsmonitor_dirty->bit_size > istate->cache_nr)
424 BUG("fsmonitor_dirty has more entries than the index (%"PRIuMAX" > %u)",
425 (uintmax_t)istate->fsmonitor_dirty->bit_size, istate->cache_nr);
ba1b9cac
AV
426 ewah_each_bit(istate->fsmonitor_dirty, fsmonitor_ewah_callback, istate);
427
679f2f9f 428 refresh_fsmonitor(istate);
ba1b9cac
AV
429 }
430
431 ewah_free(istate->fsmonitor_dirty);
432 istate->fsmonitor_dirty = NULL;
433 }
434
435 switch (fsmonitor_enabled) {
883e248b
BP
436 case -1: /* keep: do nothing */
437 break;
438 case 0: /* false */
439 remove_fsmonitor(istate);
440 break;
441 case 1: /* true */
442 add_fsmonitor(istate);
443 break;
444 default: /* unknown value: do nothing */
445 break;
446 }
447}