History

options/m_config_frontend: don't allow recursive profile inclusion

It is easy to craft a config file that loads a profile recursively. To prevent this, not only check the depth of the current inclusion but also ignore already-seen profile names.

Kacper Michajłow committed 1 year ago

Commit f59ee851 parent 0fee7c12 files · +16 −9Browse files

Unified · Split

2 files
  1. options/m_config_frontend.c +14 −9
  2. options/m_config_frontend.h +2 −0
options/m_config_frontend.c+14−9
@@ -986,8 +986,14 @@ static struct m_profile *find_check_profile(struct m_config *config, char *name) · 985 unmodified lines
986986 MP_WARN(config, "Unknown profile '%s'.\n", name);
987987 return NULL;
988988 }
989− if (config->profile_depth > MAX_PROFILE_DEPTH) {
990− MP_WARN(config, "WARNING: Profile inclusion too deep.\n");
989+ for (size_t i = 0; i < config->profile_stack_depth; ++i) {
990+ if (strcmp(config->profile_stack[i], name))
991+ continue;
992+ MP_WARN(config, "Profile '%s' has already been applied.\n", name);
993+ return NULL;
994+ }
995+ if (config->profile_stack_depth > MAX_PROFILE_DEPTH) {
996+ MP_WARN(config, "Profile inclusion too deep.\n");
991997 return NULL;
992998 }
993999 return p;
@@ -995,11 +1001,6 @@ static struct m_profile *find_check_profile(struct m_config *config, char *name) · 1 unmodified line
9951001
9961002 int m_config_set_profile(struct m_config *config, char *name, int flags)
9971003 {
998− if ((flags & M_SETOPT_FROM_CONFIG_FILE) && !strcmp(name, "default")) {
999− MP_WARN(config, "Ignoring profile=%s from config file.\n", name);
1000− return 0;
1001− }
1002−
10031004 MP_VERBOSE(config, "Applying profile '%s'...\n", name);
10041005 struct m_profile *p = find_check_profile(config, name);
10051006 if (!p)
@@ -1010,14 +1011,18 @@ int m_config_set_profile(struct m_config *config, char *name, int flags) · 4 unmodified lines
10101011 config->profile_backup_flags = p->restore_mode == 2 ? BACKUP_NVAL : 0;
10111012 }
10121013
1013− config->profile_depth++;
1014+ char *profile_name = talloc_strdup(NULL, name);
1015+ // Note that we don't check if profile applied correctly, it doesn't matter.
1016+ MP_TARRAY_APPEND(config, config->profile_stack, config->profile_stack_depth, profile_name);
1017+ talloc_steal(config->profile_stack, profile_name);
10141018 for (int i = 0; i < p->num_opts; i++) {
10151019 m_config_set_option_cli(config,
10161020 bstr0(p->opts[2 * i]),
10171021 bstr0(p->opts[2 * i + 1]),
10181022 flags | M_SETOPT_FROM_CONFIG_FILE);
10191023 }
1020− config->profile_depth--;
1024+ if (MP_TARRAY_POP(config->profile_stack, config->profile_stack_depth, &profile_name))
1025+ talloc_free(profile_name);
10211026
10221027 if (config->profile_backup_tmp == &p->backups) {
10231028 config->profile_backup_tmp = NULL;
options/m_config_frontend.h+2−0
@@ -69,6 +69,8 @@ typedef struct m_config { · 68 unmodified lines
6969 struct m_profile *profiles;
7070 // Depth when recursively including profiles.
7171 int profile_depth;
72+ char **profile_stack;
73+ size_t profile_stack_depth;
7274 // Temporary during profile application.
7375 struct m_opt_backup **profile_backup_tmp;
7476 int profile_backup_flags;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t