Skip to contentSkip to conversation

kconfig: use snprintf for formatting pathnames

PATCH In review Jacob Garber 2 participants 2 messages May 10, 2019 – May 12, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kconfig: use snprintf for formatting pathnames

Valid pathnames will never exceed PATH_MAX, but these file names are unsanitized and can cause buffer overflow if set incorrectly. Use snprintf to avoid this. This was flagged during a Coverity scan of the coreboot project, which also uses kconfig for its build system.

---
 scripts/kconfig/confdata.c | 7 ++++---
 scripts/kconfig/lexer.l    | 3 ++-
 2 files changed, 6 insertions(+), 4 deletions(-)
scripts/kconfig/confdata.c +4 -3
index 08ba146a8..847fe428a 100644
@@ -194,7 +194,7 @@ char *conf_get_default_confname(void)
 	name = expand_string(conf_defname);
 	env = getenv(SRCTREE);
 	if (env) {
-		sprintf(fullname, "%s/%s", env, name);
+		snprintf(fullname, sizeof(fullname), "%s/%s", env, name);
 		if (is_present(fullname))
 			return fullname;
 	}
@@ -843,10 +843,11 @@ int conf_write(const char *name)
 	} else
 		basename = conf_get_configname();
 
-	sprintf(newname, "%s%s", dirname, basename);
+	snprintf(newname, sizeof(newname), "%s%s", dirname, basename);
 	env = getenv("KCONFIG_OVERWRITECONFIG");
 	if (!env || !*env) {
-		sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
+		snprintf(tmpname, sizeof(tmpname),
+			 "%s.tmpconfig.%d", dirname, (int)getpid());
 		out = fopen(tmpname, "w");
 	} else {
 		*tmpname = 0;
scripts/kconfig/lexer.l +2 -1
index c9df1c8b9..6354c905b 100644
@@ -378,7 +378,8 @@ FILE *zconf_fopen(const char *name)
 	if (!f && name != NULL && name[0] != '/') {
 		env = getenv(SRCTREE);
 		if (env) {
-			sprintf(fullname, "%s/%s", env, name);
+			snprintf(fullname, sizeof(fullname),
+				 "%s/%s", env, name);
 			f = fopen(fullname, "r");
 		}
 	}
Signature · 1 line
2.21.0
#1 lore raw
Quoted 17 lines · Jacob Garber

Valid pathnames will never exceed PATH_MAX, but these file names are unsanitized and can cause buffer overflow if set incorrectly. Use snprintf to avoid this. This was flagged during a Coverity scan of the coreboot project, which also uses kconfig for its build system.

---
 scripts/kconfig/confdata.c | 7 ++++---
 scripts/kconfig/lexer.l    | 3 ++-
 2 files changed, 6 insertions(+), 4 deletions(-)
scripts/kconfig/confdata.c +0 -0
index 08ba146a8..847fe428a 100644

scripts/kconfig/confdata.c char *conf_get_default_confname(void) · line 200

Quoted 8 lines
scripts/kconfig/confdata.c +1 -1
@@ -194,7 +194,7 @@ char *conf_get_default_confname(void)
        name = expand_string(conf_defname);
        env = getenv(SRCTREE);
        if (env) {
-               sprintf(fullname, "%s/%s", env, name);
+               snprintf(fullname, sizeof(fullname), "%s/%s", env, name);
                if (is_present(fullname))
                        return fullname;
        }

Replacing sprintf() with snprintf() is correct although I think this if-conditional has no sense in the first place.

The returned string from conf_get_default_confname() will be passed to zconf_fopen().
So, SRCTREE/ will be prepended anyway.

I was wondering about applying a small fix like this to already wrong code, but I decided to picked it up.

BTW, this function has one more bug.
expand_string() must be free'd. So it is memory leak.
I will not fix it because I will remove this function sooner or later.

Quoted 4 lines
scripts/kconfig/confdata.c +0 -0
@@ -843,10 +843,11 @@ int conf_write(const char *name)
        } else
                basename = conf_get_configname();
 

scripts/kconfig/confdata.c int conf_write(const char *name) · line 853

Quoted 10 lines
scripts/kconfig/confdata.c +3 -2
-       sprintf(newname, "%s%s", dirname, basename);
+       snprintf(newname, sizeof(newname), "%s%s", dirname, basename);
        env = getenv("KCONFIG_OVERWRITECONFIG");
        if (!env || !*env) {
-               sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
+               snprintf(tmpname, sizeof(tmpname),
+                        "%s.tmpconfig.%d", dirname, (int)getpid());
                out = fopen(tmpname, "w");
        } else {
                *tmpname = 0;

I do not need this hunk.

I had already posted this:
https://patchwork.kernel.org/patch/10938255/

When I touched this hunk, I used snprintf().

Quoted 5 lines
scripts/kconfig/lexer.l +0 -0
index c9df1c8b9..6354c905b 100644

scripts/kconfig/lexer.l FILE *zconf_fopen(const char *name) · line 385

Quoted 9 lines
scripts/kconfig/lexer.l +2 -1
@@ -378,7 +378,8 @@ FILE *zconf_fopen(const char *name)
        if (!f && name != NULL && name[0] != '/') {
                env = getenv(SRCTREE);
                if (env) {
-                       sprintf(fullname, "%s/%s", env, name);
+                       snprintf(fullname, sizeof(fullname),
+                                "%s/%s", env, name);
                        f = fopen(fullname, "r");
                }
        }

This function will stay, so I will apply.

Thanks.

--
Best Regards
Masahiro Yamada