Skip to content

Commit

Permalink
libsemanage: check closing written files
Browse files Browse the repository at this point in the history
Check that closing a file that has been written to is successful, to
avoid potential unsuccessful writes/syncs.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
  • Loading branch information
cgzones authored and bachradsusi committed Nov 27, 2024
1 parent ba766fa commit 2cc2d1e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
5 changes: 4 additions & 1 deletion libsemanage/src/database_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ static int dbase_file_flush(semanage_handle_t * handle, dbase_file_t * dbase)
}

dbase_llist_set_modified(&dbase->llist, 0);
fclose(str);
if (fclose(str) != 0 && errno != EINTR) {
str = NULL;
goto err;
}
return STATUS_SUCCESS;

err:
Expand Down
15 changes: 9 additions & 6 deletions libsemanage/src/direct_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,10 @@ static int write_file(semanage_handle_t * sh,
close(out);
return -1;
}
close(out);
if (close(out) == -1 && errno != EINTR) {
ERR(sh, "Error while closing %s.", filename);
return -1;
}
return 0;
}

Expand Down Expand Up @@ -839,7 +842,7 @@ static int semanage_direct_write_langext(semanage_handle_t *sh,
goto cleanup;
}

if (fclose(fp) != 0) {
if (fclose(fp) != 0 && errno != EINTR) {
ERR(sh, "Unable to close %s module ext file.", modinfo->name);
fp = NULL;
ret = -1;
Expand Down Expand Up @@ -1216,7 +1219,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
FILE *touch;
touch = fopen(path, "we");
if (touch != NULL) {
if (fclose(touch) != 0) {
if (fclose(touch) != 0 && errno != EINTR) {
ERR(sh, "Error attempting to create disable_dontaudit flag.");
goto cleanup;
}
Expand Down Expand Up @@ -1248,7 +1251,7 @@ static int semanage_direct_commit(semanage_handle_t * sh)
FILE *touch;
touch = fopen(path, "we");
if (touch != NULL) {
if (fclose(touch) != 0) {
if (fclose(touch) != 0 && errno != EINTR) {
ERR(sh, "Error attempting to create preserve_tunable flag.");
goto cleanup;
}
Expand Down Expand Up @@ -2120,7 +2123,7 @@ static int semanage_direct_set_enabled(semanage_handle_t *sh,

ret = fclose(fp);
fp = NULL;
if (ret != 0) {
if (ret != 0 && errno != EINTR) {
ERR(sh,
"Unable to close disabled file for module %s",
modkey->name);
Expand Down Expand Up @@ -2321,7 +2324,7 @@ static int semanage_direct_get_module_info(semanage_handle_t *sh,
free(tmp);
tmp = NULL;

if (fclose(fp) != 0) {
if (fclose(fp) != 0 && errno != EINTR) {
fp = NULL;
ERR(sh,
"Unable to close %s module lang ext file.",
Expand Down
3 changes: 2 additions & 1 deletion libsemanage/src/genhomedircon.c
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,8 @@ int semanage_genhomedircon(semanage_handle_t * sh,

done:
if (out != NULL)
fclose(out);
if (fclose(out) != 0 && errno != EINTR)
retval = STATUS_ERR;

while (s.fallback)
pop_user_entry(&(s.fallback));
Expand Down
18 changes: 13 additions & 5 deletions libsemanage/src/semanage_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ int semanage_copy_file(semanage_handle_t *sh, const char *src, const char *dst,
errsv = errno;
retval = -1;
}
if (close(out) < 0) {
if (close(out) < 0 && errno != EINTR) {
errsv = errno;
retval = -1;
}
Expand Down Expand Up @@ -1536,9 +1536,11 @@ int semanage_split_fc(semanage_handle_t * sh)
if (file_con)
fclose(file_con);
if (fc >= 0)
close(fc);
if (close(fc) == -1 && errno != EINTR)
retval = -1;
if (hd >= 0)
close(hd);
if (close(hd) == -1 && errno != EINTR)
retval = -1;

return retval;

Expand Down Expand Up @@ -1732,7 +1734,11 @@ static int semanage_commit_sandbox(semanage_handle_t * sh)
close(fd);
return -1;
}
close(fd);
if (close(fd) == -1 && errno != EINTR) {
ERR(sh, "Error while closing commit number file %s.",
commit_filename);
return -1;
}

/* sync changes in sandbox to filesystem */
fd = open(sandbox, O_DIRECTORY | O_CLOEXEC);
Expand Down Expand Up @@ -2157,7 +2163,9 @@ int semanage_write_policydb(semanage_handle_t * sh, sepol_policydb_t * out,

cleanup:
if (outfile != NULL) {
fclose(outfile);
if (fclose(outfile) != 0 && errno != EINTR) {
retval = STATUS_ERR;
}
}
umask(mask);
sepol_policy_file_free(pf);
Expand Down

0 comments on commit 2cc2d1e

Please sign in to comment.