Skip to contentSkip to conversation

libsemanage: handle module writer close and cleanup errors

PATCH In review Stephen Smalley 1 participant 2 messages Oct 23 – Oct 26 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libsemanage: handle module writer close and cleanup errors

The plain module writer ignores fclose(), so a buffered write to a full device can report success despite ENOSPC. Check final stream closure in both plain and bzip modes and propagate errors to callers.

Also release bzip state when finalization fails. BZ2_bzWriteClose() returns early on ferror(), even with abandon set, so clear the stream error before abandon-close on both mid-write and finalization I/O failures. Preserve errno and return failure after cleanup. Without this, a failed compressed write leaks its compression allocations.

---
 libsemanage/src/compressed_file.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
libsemanage/src/compressed_file.c +12 -4
index 6032c1f3..367ad685 100644
@@ -58,8 +58,7 @@ static int bzip(semanage_handle_t *sh, const char *filename, void *data,
 			fclose(f);
 			return -1;
 		}
-		fclose(f);
-		return 0;
+		return (fclose(f) == 0) ? 0 : -1;
 	}
 
 	b = BZ2_bzWriteOpen(&bzerror, f, sh->conf->bzip_blocksize, 0, 0);
@@ -77,19 +76,28 @@ static int bzip(semanage_handle_t *sh, const char *filename, void *data,
 		}
 		BZ2_bzWrite(&bzerror, b, (uint8_t *)data + total, len);
 		if (bzerror == BZ_IO_ERROR) {
+			int errsv = errno;
+
+			clearerr(f);
 			BZ2_bzWriteClose(&bzerror, b, 1, NULL, NULL);
 			fclose(f);
+			errno = errsv;
 			return -1;
 		}
 		total += len;
 	}
 
 	BZ2_bzWriteClose(&bzerror, b, 0, NULL, NULL);
-	fclose(f);
 	if (bzerror == BZ_IO_ERROR) {
+		int errsv = errno;
+
+		clearerr(f);
+		BZ2_bzWriteClose(&bzerror, b, 1, NULL, NULL);
+		fclose(f);
+		errno = errsv;
 		return -1;
 	}
-	return 0;
+	return (fclose(f) == 0) ? 0 : -1;
 }
 
 /* bunzip() a file to '*data', returning the total number of uncompressed bytes
Signature · 1 line
2.56.0
author maintainer
#1 lore raw
Quoted 5 lines · Stephen Smalley

The plain module writer ignores fclose(), so a buffered write to a full device can report success despite ENOSPC. Check final stream closure in both plain and bzip modes and propagate errors to callers.

Quoted 7 lines

Also release bzip state when finalization fails. BZ2_bzWriteClose() returns early on ferror(), even with abandon set, so clear the stream error before abandon-close on both mid-write and finalization I/O failures. Preserve errno and return failure after cleanup. Without this, a failed compressed write leaks its compression allocations.

This has been merged.