[PATCH 4/5] libselinux: label_file: fstat after open in open_file()

Stephen Smalley <[email protected]>
Newsgroups org.kernel.vger.selinux
Message-ID <[email protected]>
Obtain the file stats via fstat() after opening it to ensure that we
use the correct size for the file that was actually opened. The
earlier stat() by path is still required to obtain and compare mtimes
when picking the newest file.

Signed-off-by: Stephen Smalley <[email protected]>
---
 libselinux/src/label_file.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/libselinux/src/label_file.c b/libselinux/src/label_file.c
index 427e4071..1d80dd68 100644
--- a/libselinux/src/label_file.c
+++ b/libselinux/src/label_file.c
@@ -1240,8 +1240,20 @@ static FILE *open_file(const char *path, const char *suffix, char *save_path,
 		return NULL;
 	}
 
-	memcpy(sb, &found->sb, sizeof(*sb));
-	return fopen(save_path, "re");
+	FILE *fp = fopen(save_path, "re");
+	if (!fp)
+		return NULL;
+
+	/*
+	 * Re-stat to ensure we use the same file size
+	 * as the file we just opened.
+	 */
+	if (fstat(fileno(fp), sb) < 0) {
+		fclose_errno_safe(fp);
+		return NULL;
+	}
+
+	return fp;
 }
 
 static int process_file(const char *path, const char *suffix,
-- 
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.