[PATCH 2/4] restorecond: copy homedir to avoid overwrite by later getpw*() calls

Stephen Smalley <[email protected]>
Newsgroups org.kernel.vger.selinux
Message-ID <[email protected]>
homedir was set to pwd->pw_dir returned by getpwuid() and the
referenced string could be overwritten by subsequent getpw*() calls,
e.g. from utmpwatcher.c:watch_file(). Create a copy to be safe.  NB
Intentionally not bothering with free'ing the copy on all the
potential exit paths; it's small and will be taken care of at exit
regardless.

Signed-off-by: Stephen Smalley <[email protected]>
---
 restorecond/restorecond.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/restorecond/restorecond.c b/restorecond/restorecond.c
index 0e973d83..6548634d 100644
--- a/restorecond/restorecond.c
+++ b/restorecond/restorecond.c
@@ -206,7 +206,10 @@ int main(int argc, char **argv)
 	if (!pwd)
 		exitApp("getpwuid");
 
-	homedir = pwd->pw_dir;
+	homedir = strdup(pwd->pw_dir);
+	if (!homedir)
+		exitApp("strdup");
+
 	if (uid != 0) {
 		if (run_as_user)
 			return server(master_fd, user_watch_file);
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.