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