Blob Blame History Raw
From 9f6a3c484ee780b0330a24941419c0a99a10ecf8 Mon Sep 17 00:00:00 2001
From: "Dmitry V. Levin" <ldv@altlinux.org>
Date: Thu, 18 May 2017 12:28:10 +0300
Subject: [PATCH] quotacheck: change to the directory before opening it, not
 after
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This avoids the race between opening the directory being scanned
and changing into that directory for processing its contants.

This is not the only race of that kind, but chdir return code
has to be checked anyway and the fix costs nothing, so let it be fixed.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Signed-off-by: Jan Kara <jack@suse.cz>
Signed-off-by: Petr Písař <ppisar@redhat.com>
---
 quotacheck.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/quotacheck.c b/quotacheck.c
index a6fe432..1675de8 100644
--- a/quotacheck.c
+++ b/quotacheck.c
@@ -508,10 +508,15 @@ static int scan_dir(const char *pathname)
 		add_to_quota(GRPQUOTA, st.st_ino, st.st_uid, st.st_gid, st.st_mode,
 			     st.st_nlink, qspace, 0);
 
-	if ((dp = opendir(pathname)) == (DIR *) NULL)
-		die(2, _("\nCan open directory %s: %s\n"), pathname, strerror(errno));
+	if (chdir(pathname) == -1) {
+		errstr(_("Cannot chdir to %s: %s\n"), pathname, strerror(errno));
+		goto out;
+	}
+
+	if ((dp = opendir(".")) == (DIR *) NULL)
+		die(2, _("\nCannot open directory %s: %s\n"),
+		    pathname, strerror(errno));
 
-	chdir(pathname);
 	if (flags & FL_VERYVERBOSE)
 		blit(pathname);
 	while ((de = readdir(dp)) != (struct dirent *)NULL) {
-- 
2.9.4