libsemanage: handle module writer close and cleanup errors
Stephen Smalley
maintainerPATCH 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
@@ -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
Stephen Smalley
maintainerQuoted 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 onferror(), 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.Signed-off-by Stephen Smalley <[email protected]>
This has been merged.