Skip to contentSkip to conversation

evmctl: fix memory leak in get_password

PATCH Superseded Bruno Meneguele 3 participants 7 messages Aug 10, 2021 – Aug 16, 2021 Keys-trusted

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 evmctl: fix memory leak in get_password

The variable "password" is not freed nor returned in case get_password() succeeds. Instead of using an intermediary variable ("pwd") for returning the value, use the same "password" var. Issue found by Coverity scan tool.

src/evmctl.c:2565: leaked_storage: Variable "password" going out of scope
leaks the storage it points to.

---
 src/evmctl.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
src/evmctl.c +12 -3
index 7a6f2021aa92..b49c7910a4a7 100644
@@ -2601,8 +2601,9 @@ static struct option opts[] = {
 static char *get_password(void)
 {
 	struct termios flags, tmp_flags;
-	char *password, *pwd;
+	char *password;
 	int passlen = 64;
+	bool err = false;
 
 	password = malloc(passlen);
 	if (!password) {
@@ -2622,16 +2623,24 @@ static char *get_password(void)
 	}
 
 	printf("PEM password: ");
-	pwd = fgets(password, passlen, stdin);
+	if (fgets(password, passlen, stdin) == NULL) {
+		perror("fgets");
+		/* we still need to restore the terminal */
+		err = true;
+	}
 
 	/* restore terminal */
 	if (tcsetattr(fileno(stdin), TCSANOW, &flags) != 0) {
 		perror("tcsetattr");
+		err = true;
+	}
+
+	if (err) {
 		free(password);
 		return NULL;
 	}
 
-	return pwd;
+	return password;
 }
 
 int main(int argc, char *argv[])
Signature · 1 line
2.31.1
maintainer
#1 lore raw

Hi Bruno,

Quoted 28 lines · Bruno Meneguele

The variable "password" is not freed nor returned in case get_password() succeeds. Instead of using an intermediary variable ("pwd") for returning the value, use the same "password" var. Issue found by Coverity scan tool.

src/evmctl.c:2565: leaked_storage: Variable "password" going out of scope
leaks the storage it points to.

---
 src/evmctl.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
src/evmctl.c +2 -1
index 7a6f2021aa92..b49c7910a4a7 100644
@@ -2601,8 +2601,9 @@ static struct option opts[] = {
 static char *get_password(void)
 {
 	struct termios flags, tmp_flags;
-	char *password, *pwd;
+	char *password;
 	int passlen = 64;
+	bool err = false;
 
 	password = malloc(passlen);
 	if (!password) {

src/evmctl.c static char *get_password(void) · line 2630

Quoted 9 lines
src/evmctl.c +5 -1
@@ -2622,16 +2623,24 @@ static char *get_password(void)
 	}
 
 	printf("PEM password: ");
-	pwd = fgets(password, passlen, stdin);
+	if (fgets(password, passlen, stdin) == NULL) {
+		perror("fgets");
+		/* we still need to restore the terminal */
+		err = true;
+	}

From the fgets manpage:
fgets() returns s on success, and NULL on error or when end of file
occurs while no characters have been read.

src/evmctl.c static char *get_password(void) · line 2643

Quoted 13 lines
src/evmctl.c +5 -1
 	/* restore terminal */
 	if (tcsetattr(fileno(stdin), TCSANOW, &flags) != 0) {
 		perror("tcsetattr");
+		err = true;
+	}
+
+	if (err) {
 		free(password);
 		return NULL;
 	}
 
-	return pwd;
+	return password;

Wouldn't a simpler fix be to test "pwd" here?

        if (!pwd)
                free(password);
        return pwd;

thanks,

Mimi

Quoted 3 lines
src/evmctl.c +0 -0
 }
 
 int main(int argc, char *argv[])
#2 lore raw
Quoted 33 lines · Mimi Zohar

Hi Bruno,

Quoted 30 lines · Bruno Meneguele

The variable "password" is not freed nor returned in case get_password() succeeds. Instead of using an intermediary variable ("pwd") for returning the value, use the same "password" var. Issue found by Coverity scan tool.

src/evmctl.c:2565: leaked_storage: Variable "password" going out of scope
leaks the storage it points to.

---
 src/evmctl.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
src/evmctl.c +2 -1
index 7a6f2021aa92..b49c7910a4a7 100644
@@ -2601,8 +2601,9 @@ static struct option opts[] = {
 static char *get_password(void)
 {
 	struct termios flags, tmp_flags;
-	char *password, *pwd;
+	char *password;
 	int passlen = 64;
+	bool err = false;
 
 	password = malloc(passlen);
 	if (!password) {
@@ -2622,16 +2623,24 @@ static char *get_password(void)
 	}
 
Quoted 13 lines

src/evmctl.c static char *get_password(void) · line 2630

Quoted 7 lines
src/evmctl.c +5 -1
 	printf("PEM password: ");
-	pwd = fgets(password, passlen, stdin);
+	if (fgets(password, passlen, stdin) == NULL) {
+		perror("fgets");
+		/* we still need to restore the terminal */
+		err = true;
+	}

From the fgets manpage:
fgets() returns s on success, and NULL on error or when end of file
occurs while no characters have been read.

Yes, I was considering "end of file while no characters have been read" as an invalid password. The error message is misleading though, which can be fixed.

Quoted 11 lines
Quoted 11 lines
src/evmctl.c +4 -0
 	/* restore terminal */
 	if (tcsetattr(fileno(stdin), TCSANOW, &flags) != 0) {
 		perror("tcsetattr");
+		err = true;
+	}
+
+	if (err) {
 		free(password);
 		return NULL;
 	}
 
Quoted 8 lines

src/evmctl.c static char *get_password(void) · line 2643

Quoted 2 lines
src/evmctl.c +1 -1
-	return pwd;
+	return password;

Wouldn't a simpler fix be to test "pwd" here?

        if (!pwd)
                free(password);
        return pwd;

The problem is on success, when 'pwd' is actually not NULL. With that, I can't free(password). I would need to asprintf(pwd, ...) or strndup(password). Because of that, I thought it would be cleaner to remove 'password' completely.

Your call ... :)

Quoted 9 lines

thanks,

Mimi

Quoted 3 lines
src/evmctl.c +0 -0
 }
 
 int main(int argc, char *argv[])
Signature · 2 lines
bmeneg 
PGP Key: http://bmeneg.com/pubkey.txt
maintainer
#3 lore raw
Quoted 12 lines · Bruno Meneguele
Quoted 9 lines · Mimi Zohar

src/evmctl.c static char *get_password(void) · line 2643

Quoted 3 lines
src/evmctl.c +1 -1
-	return pwd;
+	return password;

Wouldn't a simpler fix be to test "pwd" here?

        if (!pwd)
                free(password);
        return pwd;
Quoted 4 lines

The problem is on success, when 'pwd' is actually not NULL. With that, I can't free(password). I would need to asprintf(pwd, ...) or strndup(password). Because of that, I thought it would be cleaner to remove 'password' completely.

I see. So instead of "return pwd" as suggested above,

        if (!pwd) {
                free(password);
                password = NULL;  <== set or return NULL
        }

        return password;

thanks,

Mimi

#4 lore raw
Quoted 18 lines · Mimi Zohar
Quoted 12 lines · Bruno Meneguele
Quoted 9 lines · Mimi Zohar

src/evmctl.c static char *get_password(void) · line 2643

Quoted 3 lines
src/evmctl.c +1 -1
-	return pwd;
+	return password;

Wouldn't a simpler fix be to test "pwd" here?

        if (!pwd)
                free(password);
        return pwd;
Quoted 4 lines

The problem is on success, when 'pwd' is actually not NULL. With that, I can't free(password). I would need to asprintf(pwd, ...) or strndup(password). Because of that, I thought it would be cleaner to remove 'password' completely.

Quoted 9 lines

I see. So instead of "return pwd" as suggested above,

        if (!pwd) {
                free(password);
                password = NULL;  <== set or return NULL
        }

        return password;

Ack. Will send a v2 with this change.

Thanks Mimi.

Signature · 2 lines
bmeneg 
PGP Key: http://bmeneg.com/pubkey.txt
#5 lore raw
In reply to Mimi Zohar
Quoted 18 lines · Mimi Zohar
Quoted 12 lines · Bruno Meneguele
Quoted 9 lines · Mimi Zohar

src/evmctl.c static char *get_password(void) · line 2643

Quoted 3 lines
src/evmctl.c +1 -1
-	return pwd;
+	return password;

Wouldn't a simpler fix be to test "pwd" here?

         if (!pwd)
                 free(password);
         return pwd;
Quoted 4 lines

The problem is on success, when 'pwd' is actually not NULL. With that, I can't free(password). I would need to asprintf(pwd, ...) or strndup(password). Because of that, I thought it would be cleaner to remove 'password' completely.

Quoted 8 lines

I see. So instead of "return pwd" as suggested above,

         if (!pwd) {
                 free(password);
                 password = NULL;  <== set or return NULL
         }

         return password;

That looks cleaner to me.

My style would be

	if (pwd == NULL)

which compiles to the same binary, but it less prone to error.

In addition, since this is reading from stdin

1 - Do you want the newline to be part of the password? 2 = Why is an empty password an error?

#6 lore raw
Quoted 32 lines · Ken Goldman
Quoted 18 lines · Mimi Zohar
Quoted 12 lines · Bruno Meneguele
Quoted 9 lines · Mimi Zohar

src/evmctl.c static char *get_password(void) · line 2643

Quoted 3 lines
src/evmctl.c +1 -1
-	return pwd;
+	return password;

Wouldn't a simpler fix be to test "pwd" here?

         if (!pwd)
                 free(password);
         return pwd;
Quoted 4 lines

The problem is on success, when 'pwd' is actually not NULL. With that, I can't free(password). I would need to asprintf(pwd, ...) or strndup(password). Because of that, I thought it would be cleaner to remove 'password' completely.

Quoted 8 lines

I see. So instead of "return pwd" as suggested above,

         if (!pwd) {
                 free(password);
                 password = NULL;  <== set or return NULL
         }

         return password;

That looks cleaner to me.

My style would be

Quoted 7 lines
	if (pwd == NULL)

which compiles to the same binary, but it less prone to error.

In addition, since this is reading from stdin

1 - Do you want the newline to be part of the password?

I would say 'yes'. AFAIK OpenSSL preserves the newline if it's present in the input from <stdin>:

"The returned string is always NUL-terminated and the '\n' is preserved if present in the input data" (BIO_gets() manpage from OpenSSL)

Also, if the user passed the password to the PEM file creation through the arguments list (no newline) it can also do the same with evmctl.

Quoted 2 lines

2 = Why is an empty password an error?

Considering the item 1, I don't think we have an empty string in this case.

Signature · 2 lines
bmeneg 
PGP Key: http://bmeneg.com/pubkey.txt