tool_parsecfg: detect and error on recursive --config use

The config file parser now has a maximum level of inclusions allowed (5)
to detect and prevent recursive inclusions of itself leading to badness.

Bonus: clean up return code handling from the config parser.

Test 774 verifies
Closes #19168
This commit is contained in:
Daniel Stenberg 2025-10-20 22:46:56 +02:00
parent b4f57c8045
commit 9e198618de
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
10 changed files with 72 additions and 32 deletions

View file

@ -81,11 +81,11 @@ static int unslashquote(const char *line, struct dynbuf *param)
#define MAX_CONFIG_LINE_LENGTH (10*1024*1024)
/* return 0 on everything-is-fine, and non-zero otherwise */
int parseconfig(const char *filename)
ParameterError parseconfig(const char *filename, int max_recursive)
{
FILE *file = NULL;
bool usedarg = FALSE;
int rc = 0;
ParameterError err = PARAM_OK;
struct OperationConfig *config = global->last;
char *pathalloc = NULL;
@ -96,7 +96,7 @@ int parseconfig(const char *filename)
file = curlx_fopen(curlrc, FOPEN_READTEXT);
if(!file) {
free(curlrc);
return 1;
return PARAM_READ_ERROR;
}
filename = pathalloc = curlrc;
}
@ -133,12 +133,12 @@ int parseconfig(const char *filename)
curlx_dyn_init(&pbuf, MAX_CONFIG_LINE_LENGTH);
DEBUGASSERT(filename);
while(!rc && my_get_line(file, &buf, &fileerror)) {
while(!err && my_get_line(file, &buf, &fileerror)) {
ParameterError res;
lineno++;
line = curlx_dyn_ptr(&buf);
if(!line) {
rc = 1; /* out of memory */
err = PARAM_NO_MEM; /* out of memory */
break;
}
@ -166,9 +166,11 @@ int parseconfig(const char *filename)
/* the parameter starts here (unless quoted) */
if(*line == '\"') {
/* quoted parameter, do the quote dance */
rc = unslashquote(++line, &pbuf);
if(rc)
int rc = unslashquote(++line, &pbuf);
if(rc) {
err = PARAM_BAD_USE;
break;
}
param = curlx_dyn_len(&pbuf) ? curlx_dyn_ptr(&pbuf) : CURL_UNCONST("");
}
else {
@ -206,7 +208,7 @@ int parseconfig(const char *filename)
#ifdef DEBUG_CONFIG
curl_mfprintf(tool_stderr, "PARAM: \"%s\"\n",(param ? param : "(null)"));
#endif
res = getparameter(option, param, &usedarg, config);
res = getparameter(option, param, &usedarg, config, max_recursive);
config = global->last;
if(!res && param && *param && !usedarg)
@ -240,10 +242,12 @@ int parseconfig(const char *filename)
res != PARAM_VERSION_INFO_REQUESTED &&
res != PARAM_ENGINES_REQUESTED &&
res != PARAM_CA_EMBED_REQUESTED) {
const char *reason = param2text(res);
errorf("%s:%d: '%s' %s",
filename, lineno, option, reason);
rc = (int)res;
/* only show error in the first level config call */
if(max_recursive == CONFIG_MAX_LEVELS) {
const char *reason = param2text(res);
errorf("%s:%d: '%s' %s", filename, lineno, option, reason);
}
err = res;
}
}
}
@ -252,13 +256,16 @@ int parseconfig(const char *filename)
if(file != stdin)
curlx_fclose(file);
if(fileerror)
rc = 1;
err = PARAM_READ_ERROR;
}
else
rc = 1; /* could not open the file */
err = PARAM_READ_ERROR; /* could not open the file */
if((err == PARAM_READ_ERROR) && filename)
errorf("cannot read config from '%s'", filename);
free(pathalloc);
return rc;
return err;
}