Skip to content

Commit 075cd7b

Browse files
committed
Merge branch 'ds/trace2-tolerate-failed-timestamp' into seen
Functions like `xstrfmt()` and `xcalloc()` have been banned from use in the trace2 API codebase to prevent calls to `die()` which lead to unwanted process exits and recursion when memory allocation fails. * ds/trace2-tolerate-failed-timestamp: trace2: remove use of xcalloc() trace2: remove use of ALLOC_GROW() trace2: remove use of xstrfmt() trace2: remove use of ALLOC_ARRAY() trace2: remove use of xstrdup() trace2: tolerate failed timestamp formatting banned-die: create header for banning of functions
2 parents ac77734 + a80c36b commit 075cd7b

16 files changed

Lines changed: 250 additions & 31 deletions

‎banned-die.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef BANNED_DIE_H
2+
#define BANNED_DIE_H
3+
4+
#include "banned.h"
5+
6+
/*
7+
* This header lists functions that must not be used by low-level APIs
8+
* because they can cause Git to terminate.
9+
*/
10+
11+
#undef die
12+
#define die BANNED(die)
13+
14+
#undef xsnprintf
15+
#define xsnprintf(...) BANNED(xsnprintf)
16+
17+
#undef xstrdup
18+
#define xstrdup(str) BANNED(xstrdup)
19+
20+
#undef xcalloc
21+
#define xcalloc(nmemb, size) BANNED(xcalloc)
22+
23+
#undef xstrfmt
24+
#define xstrfmt(...) BANNED(xstrfmt)
25+
26+
#undef ALLOC_ARRAY
27+
#define ALLOC_ARRAY(x, alloc) BANNED(ALLOC_ARRAY)
28+
29+
#undef ALLOC_GROW
30+
#define ALLOC_GROW(x, nr, alloc) BANNED(ALLOC_GROW)
31+
32+
#endif /* BANNED_DIE_H */

‎t/t0212-trace2-event.sh‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ test_expect_success 'unsafe URLs are redacted by default in cmd_start events' '
332332
333333
GIT_TRACE2_EVENT="$(pwd)/trace.event" \
334334
test-tool trace2 300redact_start git clone https://user:[email protected]/ clone2 &&
335-
test_grep ! user:pwd trace.event
335+
test_grep ! user:pwd trace.event &&
336+
test_grep "user:<REDACTED>@example.com/" trace.event
336337
'
337338

338339
test_expect_success 'unsafe URLs are redacted by default in child_start events' '
@@ -341,7 +342,8 @@ test_expect_success 'unsafe URLs are redacted by default in child_start events'
341342
342343
GIT_TRACE2_EVENT="$(pwd)/trace.event" \
343344
test-tool trace2 301redact_child_start git clone https://user:[email protected]/ clone2 &&
344-
test_grep ! user:pwd trace.event
345+
test_grep ! user:pwd trace.event &&
346+
test_grep "user:<REDACTED>@example.com/" trace.event
345347
'
346348

347349
test_expect_success 'unsafe URLs are redacted by default in exec events' '
@@ -350,7 +352,8 @@ test_expect_success 'unsafe URLs are redacted by default in exec events' '
350352
351353
GIT_TRACE2_EVENT="$(pwd)/trace.event" \
352354
test-tool trace2 302redact_exec git clone https://user:[email protected]/ clone2 &&
353-
test_grep ! user:pwd trace.event
355+
test_grep ! user:pwd trace.event &&
356+
test_grep "user:<REDACTED>@example.com/" trace.event
354357
'
355358

356359
test_expect_success 'unsafe URLs are redacted by default in def_param events' '
@@ -359,7 +362,8 @@ test_expect_success 'unsafe URLs are redacted by default in def_param events' '
359362
360363
GIT_TRACE2_EVENT="$(pwd)/trace.event" \
361364
test-tool trace2 303redact_def_param url https://user:[email protected]/ &&
362-
test_grep ! user:pwd trace.event
365+
test_grep ! user:pwd trace.event &&
366+
test_grep "user:<REDACTED>@example.com/" trace.event
363367
'
364368

365369
test_done

‎trace2.c‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "trace2/tr2_tgt.h"
1818
#include "trace2/tr2_tls.h"
1919
#include "trace2/tr2_tmr.h"
20+
/* banned-die must be last. */
21+
#include "banned-die.h"
2022

2123
static int trace2_enabled;
2224
static int trace2_redact = 1;
@@ -259,7 +261,10 @@ int trace2_is_enabled(void)
259261
static const char *redact_arg(const char *arg)
260262
{
261263
const char *p, *colon;
264+
const char *redact = ":<REDACTED>";
265+
char *redacted;
262266
size_t at;
267+
size_t prefix_len, suffix_len, redacted_len, redact_len;
263268

264269
if (!trace2_redact ||
265270
(!skip_prefix(arg, "https://", &p) &&
@@ -274,7 +279,25 @@ static const char *redact_arg(const char *arg)
274279
if (!colon)
275280
return arg;
276281

277-
return xstrfmt("%.*s:<REDACTED>%s", (int)(colon - arg), arg, p + at);
282+
redact_len = strlen(redact);
283+
prefix_len = colon - arg;
284+
suffix_len = strlen(p + at);
285+
286+
if (unsigned_add_overflows(prefix_len, suffix_len) ||
287+
unsigned_add_overflows(prefix_len + suffix_len, redact_len) ||
288+
unsigned_add_overflows(prefix_len + suffix_len + redact_len, 1))
289+
return NULL;
290+
291+
redacted_len = prefix_len + suffix_len + redact_len + 1;
292+
293+
redacted = malloc(redacted_len);
294+
if (!redacted)
295+
return NULL;
296+
297+
memcpy(redacted, arg, prefix_len);
298+
memcpy(redacted + prefix_len, redact, redact_len);
299+
memcpy(redacted + prefix_len + redact_len, p + at, suffix_len + 1);
300+
return redacted;
278301
}
279302

280303
/*
@@ -299,19 +322,32 @@ static const char **redact_argv(const char **argv)
299322

300323
if (!argv[i])
301324
return argv;
325+
if (!redacted)
326+
return NULL;
302327

303328
for (j = 0; argv[j]; j++)
304329
; /* keep counting */
305330

306-
ALLOC_ARRAY(ret, j + 1);
331+
ret = calloc(j + 1, sizeof(*ret));
332+
if (!ret) {
333+
free((char *)redacted);
334+
return NULL;
335+
}
307336
ret[j] = NULL;
308337

309338
for (j = 0; j < i; j++)
310339
ret[j] = argv[j];
311340
ret[i] = redacted;
312341
for (++i; argv[i]; i++) {
313342
redacted = redact_arg(argv[i]);
314-
ret[i] = redacted ? redacted : argv[i];
343+
if (!redacted) {
344+
for (j = 0; j < i; j++)
345+
if (ret[j] != argv[j])
346+
free((void *)ret[j]);
347+
free(ret);
348+
return NULL;
349+
}
350+
ret[i] = redacted;
315351
}
316352

317353
return ret;
@@ -344,6 +380,8 @@ void trace2_cmd_start_fl(const char *file, int line, const char **argv)
344380
us_elapsed_absolute = tr2tls_absolute_elapsed(us_now);
345381

346382
redacted = redact_argv(argv);
383+
if (!redacted)
384+
return;
347385

348386
for_each_wanted_builtin (j, tgt_j)
349387
if (tgt_j->pfn_start_fl)
@@ -512,6 +550,7 @@ void trace2_child_start_fl(const char *file, int line,
512550
uint64_t us_now;
513551
uint64_t us_elapsed_absolute;
514552
const char **orig_argv = cmd->args.v;
553+
const char **redacted;
515554

516555
if (!trace2_enabled)
517556
return;
@@ -529,7 +568,10 @@ void trace2_child_start_fl(const char *file, int line,
529568
* temporarily replace the original argv (inside the `strvec`)
530569
* with a possibly redacted version.
531570
*/
532-
cmd->args.v = redact_argv(orig_argv);
571+
redacted = redact_argv(orig_argv);
572+
if (!redacted)
573+
return;
574+
cmd->args.v = redacted;
533575

534576
for_each_wanted_builtin (j, tgt_j)
535577
if (tgt_j->pfn_child_start_fl)
@@ -621,6 +663,8 @@ int trace2_exec_fl(const char *file, int line, const char *exe,
621663
exec_id = tr2tls_locked_increment(&tr2_next_exec_id);
622664

623665
redacted = redact_argv(argv);
666+
if (!redacted)
667+
return exec_id;
624668

625669
for_each_wanted_builtin (j, tgt_j)
626670
if (tgt_j->pfn_exec_fl)

‎trace2/tr2_cfg.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "trace2/tr2_cfg.h"
88
#include "trace2/tr2_sysenv.h"
99
#include "wildmatch.h"
10+
/* banned-die must be last. */
11+
#include "banned-die.h"
1012

1113
static struct string_list tr2_cfg_patterns = STRING_LIST_INIT_DUP;
1214
static int tr2_cfg_loaded;

‎trace2/tr2_cmd_name.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "git-compat-util.h"
22
#include "strbuf.h"
33
#include "trace2/tr2_cmd_name.h"
4+
/* banned-die must be last. */
5+
#include "banned-die.h"
46

57
#define TR2_ENVVAR_PARENT_NAME "GIT_TRACE2_PARENT_NAME"
68

‎trace2/tr2_ctr.c‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "trace2/tr2_tgt.h"
33
#include "trace2/tr2_tls.h"
44
#include "trace2/tr2_ctr.h"
5+
/* banned-die must be last. */
6+
#include "banned-die.h"
57

68
/*
79
* A global counter block to aggregate values from the partial sums
@@ -53,7 +55,11 @@ static struct tr2_counter_metadata tr2_counter_metadata[TRACE2_NUMBER_OF_COUNTER
5355
void tr2_counter_increment(enum trace2_counter_id cid, uint64_t value)
5456
{
5557
struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
56-
struct tr2_counter *c = &ctx->counter_block.counter[cid];
58+
struct tr2_counter *c;
59+
60+
if (tr2tls_is_fallback(ctx))
61+
return;
62+
c = &ctx->counter_block.counter[cid];
5763

5864
c->value += value;
5965

@@ -67,6 +73,8 @@ void tr2_update_final_counters(void)
6773
struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
6874
enum trace2_counter_id cid;
6975

76+
if (tr2tls_is_fallback(ctx))
77+
return;
7078
if (!ctx->used_any_counter)
7179
return;
7280

@@ -88,6 +96,8 @@ void tr2_emit_per_thread_counters(tr2_tgt_evt_counter_t *fn_apply)
8896
struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
8997
enum trace2_counter_id cid;
9098

99+
if (tr2tls_is_fallback(ctx))
100+
return;
91101
if (!ctx->used_any_per_thread_counter)
92102
return;
93103

‎trace2/tr2_dst.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "trace2/tr2_dst.h"
66
#include "trace2/tr2_sid.h"
77
#include "trace2/tr2_sysenv.h"
8+
/* banned-die must be last. */
9+
#include "banned-die.h"
810

911
/*
1012
* How many attempts we will make at creating an automatically-named trace file.

‎trace2/tr2_sid.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "strbuf.h"
44
#include "trace2/tr2_tbuf.h"
55
#include "trace2/tr2_sid.h"
6+
/* banned-die must be last. */
7+
#include "banned-die.h"
68

79
#define TR2_ENVVAR_PARENT_SID "GIT_TRACE2_PARENT_SID"
810

‎trace2/tr2_sysenv.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "config.h"
55
#include "dir.h"
66
#include "tr2_sysenv.h"
7+
/* banned-die must be last. */
8+
#include "banned-die.h"
79

810
/*
911
* Each entry represents a trace2 setting.
@@ -73,7 +75,9 @@ static int tr2_sysenv_cb(const char *key, const char *value,
7375
if (!value)
7476
return config_error_nonbool(key);
7577
free(tr2_sysenv_settings[k].value);
76-
tr2_sysenv_settings[k].value = xstrdup(value);
78+
tr2_sysenv_settings[k].value = strdup(value);
79+
if (!tr2_sysenv_settings[k].value)
80+
return 0;
7781
return 0;
7882
}
7983
}
@@ -109,7 +113,7 @@ const char *tr2_sysenv_get(enum tr2_sysenv_variable var)
109113
const char *v = getenv(tr2_sysenv_settings[var].env_var_name);
110114
if (v && *v) {
111115
free(tr2_sysenv_settings[var].value);
112-
tr2_sysenv_settings[var].value = xstrdup(v);
116+
tr2_sysenv_settings[var].value = strdup(v);
113117
}
114118
tr2_sysenv_settings[var].getenv_called = 1;
115119
}

‎trace2/tr2_tbuf.c‎

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,68 @@
11
#include "git-compat-util.h"
22
#include "tr2_tbuf.h"
3+
/* banned-die must be last. */
4+
#include "banned-die.h"
35

46
void tr2_tbuf_local_time(struct tr2_tbuf *tb)
57
{
6-
struct timeval tv;
7-
struct tm tm;
8+
struct timeval tv = { 0 };
9+
struct tm tm = { 0 };
810
time_t secs;
11+
int len;
912

1013
gettimeofday(&tv, NULL);
1114
secs = tv.tv_sec;
1215
localtime_r(&secs, &tm);
1316

14-
xsnprintf(tb->buf, sizeof(tb->buf), "%02d:%02d:%02d.%06ld", tm.tm_hour,
15-
tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
17+
len = snprintf(tb->buf, sizeof(tb->buf), "%02d:%02d:%02d.%06ld",
18+
tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
19+
20+
if (len < 0 || (size_t)len >= sizeof(tb->buf)) {
21+
const char *blank = "00:00:00.000000";
22+
strlcpy(tb->buf, blank, sizeof(tb->buf));
23+
}
1624
}
1725

1826
void tr2_tbuf_utc_datetime_extended(struct tr2_tbuf *tb)
1927
{
20-
struct timeval tv;
21-
struct tm tm;
28+
struct timeval tv = { 0 };
29+
struct tm tm = { 0 };
2230
time_t secs;
31+
int len;
2332

2433
gettimeofday(&tv, NULL);
2534
secs = tv.tv_sec;
2635
gmtime_r(&secs, &tm);
2736

28-
xsnprintf(tb->buf, sizeof(tb->buf),
29-
"%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ", tm.tm_year + 1900,
30-
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
31-
(long)tv.tv_usec);
37+
len = snprintf(tb->buf, sizeof(tb->buf),
38+
"%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
39+
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
40+
tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
41+
42+
if (len < 0 || (size_t)len >= sizeof(tb->buf)) {
43+
const char *blank = "1900-00-00T00:00:00.000000Z";
44+
strlcpy(tb->buf, blank, sizeof(tb->buf));
45+
}
3246
}
3347

3448
void tr2_tbuf_utc_datetime(struct tr2_tbuf *tb)
3549
{
36-
struct timeval tv;
37-
struct tm tm;
50+
struct timeval tv = { 0 };
51+
struct tm tm = { 0 };
3852
time_t secs;
53+
int len;
3954

4055
gettimeofday(&tv, NULL);
4156
secs = tv.tv_sec;
4257
gmtime_r(&secs, &tm);
4358

44-
xsnprintf(tb->buf, sizeof(tb->buf), "%4d%02d%02dT%02d%02d%02d.%06ldZ",
45-
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
46-
tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
59+
len = snprintf(tb->buf, sizeof(tb->buf),
60+
"%4d%02d%02dT%02d%02d%02d.%06ldZ",
61+
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
62+
tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
63+
64+
if (len < 0 || (size_t)len >= sizeof(tb->buf)) {
65+
const char *blank = "19000000T000000.000000Z";
66+
strlcpy(tb->buf, blank, sizeof(tb->buf));
67+
}
4768
}

0 commit comments

Comments
 (0)