Skip to content

Commit 3bb3d6b

Browse files
ttaylorrdscho
authored andcommitted
config.c: disallow overly-long lines in copy_or_rename_section_in_file()
As a defense-in-depth measure to guard against any potentially-unknown buffer overflows in `copy_or_rename_section_in_file()`, refuse to work with overly-long lines in a gitconfig. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent e91cfe6 commit 3bb3d6b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

config.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,6 +3083,8 @@ static int section_name_is_ok(const char *name)
30833083
return 1;
30843084
}
30853085

3086+
#define GIT_CONFIG_MAX_LINE_LEN (512 * 1024)
3087+
30863088
/* if new_name == NULL, the section is removed instead */
30873089
static int git_config_copy_or_rename_section_in_file(const char *config_filename,
30883090
const char *old_name,
@@ -3097,6 +3099,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
30973099
struct stat st;
30983100
struct strbuf copystr = STRBUF_INIT;
30993101
struct config_store_data store;
3102+
uint32_t line_nr = 0;
31003103

31013104
memset(&store, 0, sizeof(store));
31023105

@@ -3137,6 +3140,16 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
31373140
size_t i, length;
31383141
int is_section = 0;
31393142
char *output = buf.buf;
3143+
3144+
line_nr++;
3145+
3146+
if (buf.len >= GIT_CONFIG_MAX_LINE_LEN) {
3147+
ret = error(_("refusing to work with overly long line "
3148+
"in '%s' on line %"PRIuMAX),
3149+
config_filename, (uintmax_t)line_nr);
3150+
goto out;
3151+
}
3152+
31403153
for (i = 0; buf.buf[i] && isspace(buf.buf[i]); i++)
31413154
; /* do nothing */
31423155
if (buf.buf[i] == '[') {

t/t1300-config.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,16 @@ test_expect_success 'renaming an embedded section with a long line' '
633633
test_must_fail git config -f y foo.e
634634
'
635635

636+
test_expect_success 'renaming a section with an overly-long line' '
637+
{
638+
printf "[b]\\n" &&
639+
printf " c = d %525000s e" " " &&
640+
printf "[a] g = h\\n"
641+
} >y &&
642+
test_must_fail git config -f y --rename-section a xyz 2>err &&
643+
test_i18ngrep "refusing to work with overly long line in .y. on line 2" err
644+
'
645+
636646
cat >> .git/config << EOF
637647
[branch "zwei"] a = 1 [branch "vier"]
638648
EOF

0 commit comments

Comments
 (0)