Skip to content

Commit

Permalink
libsemanage: Allow tmp files to be kept if a compile fails
Browse files Browse the repository at this point in the history
Allow the tmp build files to be kept for debugging when a policy
build fails.

Signed-off-by: Richard Haines <[email protected]>
  • Loading branch information
Richard Haines authored and William Roberts committed Jan 25, 2018
1 parent f47c291 commit f281fc5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
56 changes: 38 additions & 18 deletions libsemanage/src/direct_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,25 +323,43 @@ static void semanage_direct_destroy(semanage_handle_t * sh
/* do nothing */
}

static int semanage_direct_disconnect(semanage_handle_t * sh)
static int semanage_remove_tmps(semanage_handle_t *sh)
{
/* destroy transaction */
if (sh->is_in_transaction) {
/* destroy sandbox */
if (semanage_remove_directory
(semanage_path(SEMANAGE_TMP, SEMANAGE_TOPLEVEL)) < 0) {
if (sh->commit_err)
return 0;

/* destroy sandbox if it exists */
if (semanage_remove_directory
(semanage_path(SEMANAGE_TMP, SEMANAGE_TOPLEVEL)) < 0) {
if (errno != ENOENT) {
ERR(sh, "Could not cleanly remove sandbox %s.",
semanage_path(SEMANAGE_TMP, SEMANAGE_TOPLEVEL));
return -1;
}
if (semanage_remove_directory
(semanage_final_path(SEMANAGE_FINAL_TMP,
SEMANAGE_FINAL_TOPLEVEL)) < 0) {
}

/* destroy tmp policy if it exists */
if (semanage_remove_directory
(semanage_final_path(SEMANAGE_FINAL_TMP,
SEMANAGE_FINAL_TOPLEVEL)) < 0) {
if (errno != ENOENT) {
ERR(sh, "Could not cleanly remove tmp %s.",
semanage_final_path(SEMANAGE_FINAL_TMP,
SEMANAGE_FINAL_TOPLEVEL));
return -1;
}
}

return 0;
}

static int semanage_direct_disconnect(semanage_handle_t *sh)
{
int retval = 0;

/* destroy transaction and remove tmp files if no commit error */
if (sh->is_in_transaction) {
retval = semanage_remove_tmps(sh);
semanage_release_trans_lock(sh);
}

Expand Down Expand Up @@ -375,7 +393,7 @@ static int semanage_direct_disconnect(semanage_handle_t * sh)
/* Release object databases: active kernel policy */
bool_activedb_dbase_release(semanage_bool_dbase_active(sh));

return 0;
return retval;
}

static int semanage_direct_begintrans(semanage_handle_t * sh)
Expand Down Expand Up @@ -1635,17 +1653,19 @@ static int semanage_direct_commit(semanage_handle_t * sh)
free(mod_filenames);
sepol_policydb_free(out);
cil_db_destroy(&cildb);
semanage_release_trans_lock(sh);

free(fc_buffer);

/* regardless if the commit was successful or not, remove the
sandbox if it is still there */
semanage_remove_directory(semanage_path
(SEMANAGE_TMP, SEMANAGE_TOPLEVEL));
semanage_remove_directory(semanage_final_path
(SEMANAGE_FINAL_TMP,
SEMANAGE_FINAL_TOPLEVEL));
/* Set commit_err so other functions can detect any errors. Note that
* retval > 0 will be the commit number.
*/
if (retval < 0)
sh->commit_err = retval;

if (semanage_remove_tmps(sh) != 0)
retval = -1;

semanage_release_trans_lock(sh);
umask(mask);

return retval;
Expand Down
2 changes: 2 additions & 0 deletions libsemanage/src/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ semanage_handle_t *semanage_handle_create(void)
* If any changes are made, this flag is ignored */
sh->do_rebuild = 0;

sh->commit_err = 0;

/* By default always reload policy after commit if SELinux is enabled. */
sh->do_reload = (is_selinux_enabled() > 0);

Expand Down
4 changes: 4 additions & 0 deletions libsemanage/src/handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ struct semanage_handle {
int is_in_transaction;
int do_reload; /* whether to reload policy after commit */
int do_rebuild; /* whether to rebuild policy if there were no changes */
int commit_err; /* set by semanage_direct_commit() if there are
* any errors when building or committing the
* sandbox to kernel policy at /etc/selinux
*/
int modules_modified;
int create_store; /* whether to create the store if it does not exist
* this will only have an effect on direct connections */
Expand Down

0 comments on commit f281fc5

Please sign in to comment.