2d9f68
From 1e84cb90b63bce841376140a7a80107e5ec1e1a8 Mon Sep 17 00:00:00 2001
2d9f68
From: Adrian Reber <areber@redhat.com>
2d9f68
Date: Fri, 3 May 2019 06:27:51 +0000
2d9f68
Subject: [PATCH] lsm: fix compiler error 'unused-result'
2d9f68
2d9f68
Reading out the xattr 'security.selinux' of checkpointed sockets with
2d9f68
fscanf() works (at least in theory) without checking the result of
2d9f68
fscanf(). There are, however, multiple CI failures when ignoring the
2d9f68
return value of fscanf().
2d9f68
2d9f68
This adds ferror() to check if the stream has an actual error or if '-1'
2d9f68
just mean EOF.
2d9f68
2d9f68
Handle all errors of fscanf() // Andrei
2d9f68
2d9f68
Signed-off-by: Adrian Reber <areber@redhat.com>
2d9f68
Signed-off-by: Andrei Vagin <avagin@gmail.com>
2d9f68
---
2d9f68
 criu/lsm.c | 22 +++++++++++++---------
2d9f68
 1 file changed, 13 insertions(+), 9 deletions(-)
2d9f68
2d9f68
diff --git a/criu/lsm.c b/criu/lsm.c
2d9f68
index ef6ba112b3..9c9ac7f80e 100644
2d9f68
--- a/criu/lsm.c
2d9f68
+++ b/criu/lsm.c
2d9f68
@@ -33,8 +33,8 @@ static int apparmor_get_label(pid_t pid, char **profile_name)
2d9f68
 		return -1;
2d9f68
 
2d9f68
 	if (fscanf(f, "%ms", profile_name) != 1) {
2d9f68
-		fclose(f);
2d9f68
 		pr_perror("err scanfing");
2d9f68
+		fclose(f);
2d9f68
 		return -1;
2d9f68
 	}
2d9f68
 
2d9f68
@@ -111,19 +111,23 @@ static int selinux_get_label(pid_t pid, char **output)
2d9f68
 static int selinux_get_sockcreate_label(pid_t pid, char **output)
2d9f68
 {
2d9f68
 	FILE *f;
2d9f68
+	int ret;
2d9f68
 
2d9f68
 	f = fopen_proc(pid, "attr/sockcreate");
2d9f68
 	if (!f)
2d9f68
 		return -1;
2d9f68
 
2d9f68
-	fscanf(f, "%ms", output);
2d9f68
-	/*
2d9f68
-	 * No need to check the result of fscanf(). If there is something
2d9f68
-	 * in /proc/PID/attr/sockcreate it will be copied to *output. If
2d9f68
-	 * there is nothing it will stay NULL. So whatever fscanf() does
2d9f68
-	 * it should be correct.
2d9f68
-	 */
2d9f68
-
2d9f68
+	ret = fscanf(f, "%ms", output);
2d9f68
+	if (ret == -1 && errno != 0) {
2d9f68
+		pr_perror("Unable to parse /proc/%d/attr/sockcreate", pid);
2d9f68
+		/*
2d9f68
+		 * Only if the error indicator is set it is a real error.
2d9f68
+		 * -1 could also be EOF, which would mean that sockcreate
2d9f68
+		 * was just empty, which is the most common case.
2d9f68
+		 */
2d9f68
+		fclose(f);
2d9f68
+		return -1;
2d9f68
+	}
2d9f68
 	fclose(f);
2d9f68
 	return 0;
2d9f68
 }