Skip to contentSkip to conversation

libselinux: do not treat an empty file_contexts(.local) as an error

PATCH New Stephen Smalley 1 participant 1 message Aug 7, 2015 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: do not treat an empty file_contexts(.local) as an error

file_contexts can be legitimately empty, particularly when dealing with a file_contexts.local file. The change to test for file_contexts.bin format by magic number was treating an EOF condition as a fatal error, thereby causing an error on empty file_contexts.local files. Only treat it as an error if there was truly an error on the read, as checked via ferror(). Otherwise, clear the error and EOF indicators so that they do not persist when we rewind the file and try to read it as text.

---
 libselinux/src/label_file.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
libselinux/src/label_file.c +7 -3
index b91e1e6..687d0a7 100644
@@ -447,10 +447,14 @@ static int process_file(const char *path, const char *suffix,
 			return -1;
 		}
 
+		magic = 0;
 		if (fread(&magic, sizeof magic, 1, fp) != 1) {
-			errno = EINVAL;
-			fclose(fp);
-			return -1;
+			if (ferror(fp)) {
+				errno = EINVAL;
+				fclose(fp);
+				return -1;
+			}
+			clearerr(fp);
 		}
 
 		if (magic == SELINUX_MAGIC_COMPILED_FCONTEXT) {
Signature · 1 line
2.1.0