Fix #2678 don't add loadmodule when from config

only protect loadmodule from include files

Signed-off-by: Remi Collet <remi@remirepo.net>
This commit is contained in:
Remi Collet 2025-10-04 07:23:52 +02:00 committed by Remi Collet
parent 064674945d
commit b40ab88996
3 changed files with 20 additions and 9 deletions

View File

@ -448,6 +448,8 @@ static int updateClientOutputBufferLimit(sds *args, int arg_len, const char **er
* within conf file parsing. This is only needed to support the deprecated * within conf file parsing. This is only needed to support the deprecated
* abnormal aggregate `save T C` functionality. Remove in the future. */ * abnormal aggregate `save T C` functionality. Remove in the future. */
static int reading_config_file; static int reading_config_file;
/* support detecting include vs main config file */
static int reading_include_file = 0;
void loadServerConfigFromString(sds config) { void loadServerConfigFromString(sds config) {
deprecatedConfig deprecated_configs[] = { deprecatedConfig deprecated_configs[] = {
@ -539,7 +541,9 @@ void loadServerConfigFromString(sds config) {
/* Execute config directives */ /* Execute config directives */
if (!strcasecmp(argv[0], "include") && argc == 2) { if (!strcasecmp(argv[0], "include") && argc == 2) {
reading_include_file = 1;
loadServerConfig(argv[1], 0, NULL); loadServerConfig(argv[1], 0, NULL);
reading_include_file = 0;
} else if (!strcasecmp(argv[0], "rename-command") && argc == 3) { } else if (!strcasecmp(argv[0], "rename-command") && argc == 3) {
struct serverCommand *cmd = lookupCommandBySds(argv[1]); struct serverCommand *cmd = lookupCommandBySds(argv[1]);
@ -572,7 +576,7 @@ void loadServerConfigFromString(sds config) {
goto loaderr; goto loaderr;
} }
} else if (!strcasecmp(argv[0], "loadmodule") && argc >= 2) { } else if (!strcasecmp(argv[0], "loadmodule") && argc >= 2) {
moduleEnqueueLoadModule(argv[1], &argv[2], argc - 2); moduleEnqueueLoadModule(argv[1], &argv[2], argc - 2, reading_include_file);
} else if (strchr(argv[0], '.')) { } else if (strchr(argv[0], '.')) {
if (argc < 2) { if (argc < 2) {
err = "Module config specified without value"; err = "Module config specified without value";
@ -1605,7 +1609,7 @@ void rewriteConfigLoadmoduleOption(struct rewriteConfigState *state) {
while ((de = dictNext(di)) != NULL) { while ((de = dictNext(di)) != NULL) {
struct ValkeyModule *module = dictGetVal(de); struct ValkeyModule *module = dictGetVal(de);
line = moduleLoadQueueEntryToLoadmoduleOptionStr(module, "loadmodule"); line = moduleLoadQueueEntryToLoadmoduleOptionStr(module, "loadmodule");
rewriteConfigRewriteLine(state, "loadmodule", line, 1); if (line) rewriteConfigRewriteLine(state, "loadmodule", line, 1);
} }
dictReleaseIterator(di); dictReleaseIterator(di);
/* Mark "loadmodule" as processed in case modules is empty. */ /* Mark "loadmodule" as processed in case modules is empty. */

View File

@ -84,6 +84,7 @@
struct moduleLoadQueueEntry { struct moduleLoadQueueEntry {
sds path; sds path;
int from_include;
int argc; int argc;
robj **argv; robj **argv;
}; };
@ -670,7 +671,7 @@ void freeClientModuleData(client *c) {
c->module_data = NULL; c->module_data = NULL;
} }
void moduleEnqueueLoadModule(sds path, sds *argv, int argc) { void moduleEnqueueLoadModule(sds path, sds *argv, int argc, int from_include) {
int i; int i;
struct moduleLoadQueueEntry *loadmod; struct moduleLoadQueueEntry *loadmod;
@ -678,6 +679,7 @@ void moduleEnqueueLoadModule(sds path, sds *argv, int argc) {
loadmod->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL; loadmod->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL;
loadmod->path = sdsnew(path); loadmod->path = sdsnew(path);
loadmod->argc = argc; loadmod->argc = argc;
loadmod->from_include = from_include;
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
loadmod->argv[i] = createRawStringObject(argv[i], sdslen(argv[i])); loadmod->argv[i] = createRawStringObject(argv[i], sdslen(argv[i]));
} }
@ -688,6 +690,10 @@ sds moduleLoadQueueEntryToLoadmoduleOptionStr(ValkeyModule *module,
const char *config_option_str) { const char *config_option_str) {
sds line; sds line;
if (module->loadmod->from_include) {
/* no need to add as already from config */
return NULL;
}
line = sdsnew(config_option_str); line = sdsnew(config_option_str);
line = sdscatlen(line, " ", 1); line = sdscatlen(line, " ", 1);
line = sdscatsds(line, module->loadmod->path); line = sdscatsds(line, module->loadmod->path);
@ -12350,7 +12356,7 @@ void moduleLoadFromQueue(void) {
listRewind(server.loadmodule_queue, &li); listRewind(server.loadmodule_queue, &li);
while ((ln = listNext(&li))) { while ((ln = listNext(&li))) {
struct moduleLoadQueueEntry *loadmod = ln->value; struct moduleLoadQueueEntry *loadmod = ln->value;
if (moduleLoad(loadmod->path, (void **)loadmod->argv, loadmod->argc, 0) == C_ERR) { if (moduleLoad(loadmod->path, (void **)loadmod->argv, loadmod->argc, 0, loadmod->from_include) == C_ERR) {
serverLog(LL_WARNING, "Can't load module from %s: server aborting", loadmod->path); serverLog(LL_WARNING, "Can't load module from %s: server aborting", loadmod->path);
exit(1); exit(1);
} }
@ -12531,7 +12537,7 @@ void moduleUnregisterCleanup(ValkeyModule *module) {
/* Load a module and initialize it. On success C_OK is returned, otherwise /* Load a module and initialize it. On success C_OK is returned, otherwise
* C_ERR is returned. */ * C_ERR is returned. */
int moduleLoad(const char *path, void **module_argv, int module_argc, int is_loadex) { int moduleLoad(const char *path, void **module_argv, int module_argc, int is_loadex, int from_include) {
int (*onload)(void *, void **, int); int (*onload)(void *, void **, int);
void *handle; void *handle;
@ -12606,6 +12612,7 @@ int moduleLoad(const char *path, void **module_argv, int module_argc, int is_loa
ctx.module->loadmod->path = sdsnew(path); ctx.module->loadmod->path = sdsnew(path);
ctx.module->loadmod->argv = module_argc ? zmalloc(sizeof(robj *) * module_argc) : NULL; ctx.module->loadmod->argv = module_argc ? zmalloc(sizeof(robj *) * module_argc) : NULL;
ctx.module->loadmod->argc = module_argc; ctx.module->loadmod->argc = module_argc;
ctx.module->loadmod->from_include = from_include;
for (int i = 0; i < module_argc; i++) { for (int i = 0; i < module_argc; i++) {
ctx.module->loadmod->argv[i] = module_argv[i]; ctx.module->loadmod->argv[i] = module_argv[i];
incrRefCount(ctx.module->loadmod->argv[i]); incrRefCount(ctx.module->loadmod->argv[i]);
@ -13529,7 +13536,7 @@ void moduleCommand(client *c) {
argv = &c->argv[3]; argv = &c->argv[3];
} }
if (moduleLoad(c->argv[2]->ptr, (void **)argv, argc, 0) == C_OK) if (moduleLoad(c->argv[2]->ptr, (void **)argv, argc, 0, 0) == C_OK)
addReply(c, shared.ok); addReply(c, shared.ok);
else else
addReplyError(c, "Error loading the extension. Please check the server logs."); addReplyError(c, "Error loading the extension. Please check the server logs.");
@ -13544,7 +13551,7 @@ void moduleCommand(client *c) {
/* If this is a loadex command we want to populate server.module_configs_queue with /* If this is a loadex command we want to populate server.module_configs_queue with
* sds NAME VALUE pairs. We also want to increment argv to just after ARGS, if supplied. */ * sds NAME VALUE pairs. We also want to increment argv to just after ARGS, if supplied. */
if (parseLoadexArguments((ValkeyModuleString ***)&argv, &argc) == VALKEYMODULE_OK && if (parseLoadexArguments((ValkeyModuleString ***)&argv, &argc) == VALKEYMODULE_OK &&
moduleLoad(c->argv[2]->ptr, (void **)argv, argc, 1) == C_OK) moduleLoad(c->argv[2]->ptr, (void **)argv, argc, 1, 0) == C_OK)
addReply(c, shared.ok); addReply(c, shared.ok);
else { else {
dictEmpty(server.module_configs_queue, NULL); dictEmpty(server.module_configs_queue, NULL);

View File

@ -169,7 +169,7 @@ static inline void moduleInitDigestContext(ValkeyModuleDigest *mdvar) {
memset(mdvar->x, 0, sizeof(mdvar->x)); memset(mdvar->x, 0, sizeof(mdvar->x));
} }
void moduleEnqueueLoadModule(sds path, sds *argv, int argc); void moduleEnqueueLoadModule(sds path, sds *argv, int argc, int from_include);
sds moduleLoadQueueEntryToLoadmoduleOptionStr(ValkeyModule *module, sds moduleLoadQueueEntryToLoadmoduleOptionStr(ValkeyModule *module,
const char *config_option_str); const char *config_option_str);
ValkeyModuleCtx *moduleAllocateContext(void); ValkeyModuleCtx *moduleAllocateContext(void);
@ -180,7 +180,7 @@ void moduleFreeContext(ValkeyModuleCtx *ctx);
void moduleInitModulesSystem(void); void moduleInitModulesSystem(void);
void moduleInitModulesSystemLast(void); void moduleInitModulesSystemLast(void);
void modulesCron(void); void modulesCron(void);
int moduleLoad(const char *path, void **argv, int argc, int is_loadex); int moduleLoad(const char *path, void **argv, int argc, int is_loadex, int from_include);
int moduleUnload(sds name, const char **errmsg); int moduleUnload(sds name, const char **errmsg);
void moduleLoadFromQueue(void); void moduleLoadFromQueue(void);
int moduleGetCommandKeysViaAPI(struct serverCommand *cmd, robj **argv, int argc, getKeysResult *result); int moduleGetCommandKeysViaAPI(struct serverCommand *cmd, robj **argv, int argc, getKeysResult *result);