policycoreutils: newrole: do not free pw strings twice

In main(), if "extract_pw_data(&pw)" returns a failed value, it has
already freed pw.pw_name, pw.pw_dir and pw.pw_shell. These fields are
freed a second time in main's err_free label, which is incorrect. Work
around this by setting them to NULL after they are freed.

This issue has been found using clang's static analyzer.

While at it, make extract_pw_data() static.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
This commit is contained in:
Nicolas Iooss 2017-04-11 23:46:02 +02:00 committed by Stephen Smalley
parent bb3f428c08
commit bfe40222e2

View File

@ -412,7 +412,7 @@ static int verify_shell(const char *shell_name)
* This function assigns malloc'd memory into the pw_copy struct.
* Returns zero on success, non-zero otherwise
*/
int extract_pw_data(struct passwd *pw_copy)
static int extract_pw_data(struct passwd *pw_copy)
{
uid_t uid;
struct passwd *pw;
@ -456,6 +456,9 @@ int extract_pw_data(struct passwd *pw_copy)
free(pw->pw_name);
free(pw->pw_dir);
free(pw->pw_shell);
pw->pw_name = NULL;
pw->pw_dir = NULL;
pw->pw_shell = NULL;
return -1;
}