Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve file context and ownership in policy store #66

Open
wants to merge 6 commits into
base: rawhide
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checkpolicy/policy_define.c
Original file line number Diff line number Diff line change
Expand Up @@ -5036,7 +5036,7 @@ int define_ibpkey_context(unsigned int low, unsigned int high)
goto out;
}

if (subnet_prefix.s6_addr[2] || subnet_prefix.s6_addr[3]) {
if (subnet_prefix.s6_addr32[2] || subnet_prefix.s6_addr32[3]) {
yyerror("subnet prefix should be 0's in the low order 64 bits.");
rc = -1;
goto out;
Expand Down
2 changes: 2 additions & 0 deletions libselinux/include/selinux/restorecon.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _RESTORECON_H_
#define _RESTORECON_H_

#include <selinux/label.h>

#include <sys/types.h>
#include <stddef.h>
#include <stdarg.h>
Expand Down
1 change: 1 addition & 0 deletions libselinux/src/label_backends_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ static void closef(struct selabel_handle *rec)
free(data->spec_arr);

free(data);
rec->data = NULL;
}

static struct selabel_lookup_rec *property_lookup(struct selabel_handle *rec,
Expand Down
1 change: 1 addition & 0 deletions libselinux/src/label_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ static void closef(struct selabel_handle *rec)
free(last_area);
}
free(data);
rec->data = NULL;
}

// Finds all the matches of |key| in the given context. Returns the result in
Expand Down
1 change: 1 addition & 0 deletions libselinux/src/label_media.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ static void close(struct selabel_handle *rec)
free(spec_arr);

free(data);
rec->data = NULL;
}

static struct selabel_lookup_rec *lookup(struct selabel_handle *rec,
Expand Down
1 change: 1 addition & 0 deletions libselinux/src/label_x.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ static void close(struct selabel_handle *rec)
free(spec_arr);

free(data);
rec->data = NULL;
}

static struct selabel_lookup_rec *lookup(struct selabel_handle *rec,
Expand Down
8 changes: 4 additions & 4 deletions libselinux/src/selinux_restorecon.c
Original file line number Diff line number Diff line change
Expand Up @@ -1191,8 +1191,8 @@ static int selinux_restorecon_common(const char *pathname_orig,
}

/* Skip digest on in-memory filesystems and /sys */
if (state.sfsb.f_type == RAMFS_MAGIC || state.sfsb.f_type == TMPFS_MAGIC ||
state.sfsb.f_type == SYSFS_MAGIC)
if ((uint32_t)state.sfsb.f_type == (uint32_t)RAMFS_MAGIC ||
state.sfsb.f_type == TMPFS_MAGIC || state.sfsb.f_type == SYSFS_MAGIC)
state.setrestorecondigest = false;

if (state.flags.set_xdev)
Expand Down Expand Up @@ -1490,7 +1490,7 @@ int selinux_restorecon_xattr(const char *pathname, unsigned int xattr_flags,

if (!recurse) {
if (statfs(pathname, &sfsb) == 0) {
if (sfsb.f_type == RAMFS_MAGIC ||
if ((uint32_t)sfsb.f_type == (uint32_t)RAMFS_MAGIC ||
sfsb.f_type == TMPFS_MAGIC)
return 0;
}
Expand Down Expand Up @@ -1525,7 +1525,7 @@ int selinux_restorecon_xattr(const char *pathname, unsigned int xattr_flags,
continue;
case FTS_D:
if (statfs(ftsent->fts_path, &sfsb) == 0) {
if (sfsb.f_type == RAMFS_MAGIC ||
if ((uint32_t)sfsb.f_type == (uint32_t)RAMFS_MAGIC ||
sfsb.f_type == TMPFS_MAGIC)
continue;
}
Expand Down
32 changes: 32 additions & 0 deletions libsemanage/src/semanage_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef struct dbase_policydb dbase_t;
#include "database_policydb.h"
#include "handle.h"

#include <selinux/restorecon.h>
#include <selinux/selinux.h>
#include <sepol/policydb.h>
#include <sepol/module.h>
Expand Down Expand Up @@ -767,6 +768,7 @@ int semanage_copy_file(const char *src, const char *dst, mode_t mode,
if (!retval && rename(tmp, dst) == -1)
return -1;

semanage_setfiles(dst);
out:
errno = errsv;
return retval;
Expand Down Expand Up @@ -819,6 +821,8 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
goto cleanup;
}
umask(mask);

semanage_setfiles(dst);
}

for (i = 0; i < len; i++) {
Expand All @@ -837,6 +841,7 @@ static int semanage_copy_dir_flags(const char *src, const char *dst, int flag)
goto cleanup;
}
umask(mask);
semanage_setfiles(path2);
} else if (S_ISREG(sb.st_mode) && flag == 1) {
mask = umask(0077);
if (semanage_copy_file(path, path2, sb.st_mode,
Expand Down Expand Up @@ -938,6 +943,7 @@ int semanage_mkdir(semanage_handle_t *sh, const char *path)

}
umask(mask);
semanage_setfiles(path);
}
else {
/* check that it really is a directory */
Expand Down Expand Up @@ -1614,16 +1620,19 @@ static int semanage_validate_and_compile_fcontexts(semanage_handle_t * sh)
semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC)) != 0) {
goto cleanup;
}
semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_BIN));

if (sefcontext_compile(sh,
semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL)) != 0) {
goto cleanup;
}
semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_LOCAL_BIN));

if (sefcontext_compile(sh,
semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS)) != 0) {
goto cleanup;
}
semanage_setfiles(semanage_final_path(SEMANAGE_FINAL_TMP, SEMANAGE_FC_HOMEDIRS_BIN));

status = 0;
cleanup:
Expand Down Expand Up @@ -3018,3 +3027,26 @@ int semanage_nc_sort(semanage_handle_t * sh, const char *buf, size_t buf_len,

return 0;
}

/* Make sure the file context and ownership of files in the policy
* store does not change */
void semanage_setfiles(const char *path){
struct stat sb;
int fd;
/* Fix the user and role portions of the context, ignore errors
* since this is not a critical operation */
selinux_restorecon(path, SELINUX_RESTORECON_SET_SPECFILE_CTX | SELINUX_RESTORECON_IGNORE_NOENTRY);

/* Make sure "path" is owned by root */
if ((geteuid() != 0 || getegid() != 0) &&
((fd = open(path, O_RDONLY)) != -1)){
/* Skip files with the SUID or SGID bit set -- abuse protection */
if ((fstat(fd, &sb) != -1) &&
!(S_ISREG(sb.st_mode) &&
(sb.st_mode & (S_ISUID | S_ISGID))) &&
(fchown(fd, 0, 0) == -1))
fprintf(stderr, "Warning! Could not set ownership of %s to root\n", path);

close(fd);
}
}
1 change: 1 addition & 0 deletions libsemanage/src/semanage_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ int semanage_get_cil_paths(semanage_handle_t * sh, semanage_module_info_t *modin
int semanage_get_active_modules(semanage_handle_t *sh,
semanage_module_info_t **modinfo, int *num_modules);

void semanage_setfiles(const char *path);

/* lock file routines */
int semanage_get_trans_lock(semanage_handle_t * sh);
Expand Down
6 changes: 2 additions & 4 deletions libsepol/src/services.c
Original file line number Diff line number Diff line change
Expand Up @@ -1362,14 +1362,12 @@ static int sepol_compute_sid(sepol_security_id_t ssid,
scontext = sepol_sidtab_search(sidtab, ssid);
if (!scontext) {
ERR(NULL, "unrecognized SID %d", ssid);
rc = -EINVAL;
goto out;
return -EINVAL;
}
tcontext = sepol_sidtab_search(sidtab, tsid);
if (!tcontext) {
ERR(NULL, "unrecognized SID %d", tsid);
rc = -EINVAL;
goto out;
return -EINVAL;
}

if (tclass && tclass <= policydb->p_classes.nprim)
Expand Down
Loading