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