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