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