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