Skip to content

Commit 14d7583

Browse files
phillipwoodgitster
authored andcommitted
config: remove unneeded struct field
As well as receiving the config key and value, config callbacks also receive a "struct key_value_info" containing information about the source of the key-value pair. Accessing the "path" field of this struct from a callback passed to repo_config() results in a use-after-free. This happens because repo_config() first populates a configset by calling config_with_options() and then iterates over the configset with the callback passed by the caller. When the configset is constructed it takes a shallow copy of the "struct key_value_info" for each config setting. This leads to the use-after-free as the "path" member is freed before config_with_options() returns. We could fix this by interning the "path" field as we do for the "filename" field but the "path" field is not actually needed. It is populated with a copy of the "path" field from "struct config_source". That field was added in d14d424 (config: disallow relative include paths from blobs, 2014-02-19) to distinguish between relative include directives in files and those in blobs. However, since 1b8132d (i18n: config: unfold error messages marked for translation, 2016-07-28) we can differentiate these by looking at the "origin_type" field in "struct key_value_info". So let's remove the "path" members from "struct config_source" and "struct key_value_info" and instead use a combination of the "filename" and "origin_type" fields to determine the absolute path of relative includes. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16bd9f2 commit 14d7583

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

config.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ struct config_source {
5656
} u;
5757
enum config_origin_type origin_type;
5858
const char *name;
59-
const char *path;
6059
enum config_error_action default_error_action;
6160
int linenr;
6261
int eof;
@@ -173,14 +172,14 @@ static int handle_path_include(const struct key_value_info *kvi,
173172
if (!is_absolute_path(path)) {
174173
char *slash;
175174

176-
if (!kvi || !kvi->path) {
175+
if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE) {
177176
ret = error(_("relative config includes must come from files"));
178177
goto cleanup;
179178
}
180179

181-
slash = find_last_dir_sep(kvi->path);
180+
slash = find_last_dir_sep(kvi->filename);
182181
if (slash)
183-
strbuf_add(&buf, kvi->path, slash - kvi->path + 1);
182+
strbuf_add(&buf, kvi->filename, slash - kvi->filename + 1);
184183
strbuf_addstr(&buf, path);
185184
path = buf.buf;
186185
}
@@ -224,11 +223,11 @@ static int prepare_include_condition_pattern(const struct key_value_info *kvi,
224223
if (pat->buf[0] == '.' && is_dir_sep(pat->buf[1])) {
225224
const char *slash;
226225

227-
if (!kvi || !kvi->path)
226+
if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE)
228227
return error(_("relative config include "
229228
"conditionals must come from files"));
230229

231-
strbuf_realpath(&path, kvi->path, 1);
230+
strbuf_realpath(&path, kvi->filename, 1);
232231
slash = find_last_dir_sep(path.buf);
233232
if (!slash)
234233
BUG("how is this possible?");
@@ -633,7 +632,6 @@ void kvi_from_param(struct key_value_info *out)
633632
out->linenr = -1;
634633
out->origin_type = CONFIG_ORIGIN_CMDLINE;
635634
out->scope = CONFIG_SCOPE_COMMAND;
636-
out->path = NULL;
637635
}
638636

639637
int git_config_parse_parameter(const char *text,
@@ -1036,7 +1034,6 @@ static void kvi_from_source(struct config_source *cs,
10361034
out->origin_type = cs->origin_type;
10371035
out->linenr = cs->linenr;
10381036
out->scope = scope;
1039-
out->path = cs->path;
10401037
}
10411038

10421039
static int git_parse_source(struct config_source *cs, config_fn_t fn,
@@ -1855,17 +1852,19 @@ static int do_config_from(struct config_source *top, config_fn_t fn,
18551852

18561853
static int do_config_from_file(config_fn_t fn,
18571854
const enum config_origin_type origin_type,
1858-
const char *name, const char *path, FILE *f,
1859-
void *data, enum config_scope scope,
1855+
const char *name, FILE *f, void *data,
1856+
enum config_scope scope,
18601857
const struct config_options *opts)
18611858
{
18621859
struct config_source top = CONFIG_SOURCE_INIT;
18631860
int ret;
18641861

1862+
if (origin_type == CONFIG_ORIGIN_FILE && (!name || !*name))
1863+
BUG("missing filename for CONFIG_ORIGIN_FILE");
1864+
18651865
top.u.file = f;
18661866
top.origin_type = origin_type;
18671867
top.name = name;
1868-
top.path = path;
18691868
top.default_error_action = CONFIG_ERROR_DIE;
18701869
top.do_fgetc = config_file_fgetc;
18711870
top.do_ungetc = config_file_ungetc;
@@ -1880,8 +1879,8 @@ static int do_config_from_file(config_fn_t fn,
18801879
static int git_config_from_stdin(config_fn_t fn, void *data,
18811880
enum config_scope scope)
18821881
{
1883-
return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", NULL, stdin,
1884-
data, scope, NULL);
1882+
return do_config_from_file(fn, CONFIG_ORIGIN_STDIN, "", stdin, data,
1883+
scope, NULL);
18851884
}
18861885

18871886
int git_config_from_file_with_options(config_fn_t fn, const char *filename,
@@ -1896,7 +1895,7 @@ int git_config_from_file_with_options(config_fn_t fn, const char *filename,
18961895
f = fopen_or_warn(filename, "r");
18971896
if (f) {
18981897
ret = do_config_from_file(fn, CONFIG_ORIGIN_FILE, filename,
1899-
filename, f, data, scope, opts);
1898+
f, data, scope, opts);
19001899
fclose(f);
19011900
}
19021901
return ret;
@@ -1921,7 +1920,6 @@ int git_config_from_mem(config_fn_t fn,
19211920
top.u.buf.pos = 0;
19221921
top.origin_type = origin_type;
19231922
top.name = name;
1924-
top.path = NULL;
19251923
top.default_error_action = CONFIG_ERROR_ERROR;
19261924
top.do_fgetc = config_buf_fgetc;
19271925
top.do_ungetc = config_buf_ungetc;

config.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,12 @@ struct key_value_info {
122122
int linenr;
123123
enum config_origin_type origin_type;
124124
enum config_scope scope;
125-
const char *path;
126125
};
127126
#define KVI_INIT { \
128127
.filename = NULL, \
129128
.linenr = -1, \
130129
.origin_type = CONFIG_ORIGIN_UNKNOWN, \
131130
.scope = CONFIG_SCOPE_UNKNOWN, \
132-
.path = NULL, \
133131
}
134132

135133
/* Captures additional information that a config callback can use. */

0 commit comments

Comments
 (0)