Commit 660f5faf authored by Stephen Smalley's avatar Stephen Smalley
Browse files

libselinux: test for file_contexts.bin format


Check to see if the file whose path is passed to selabel_open() starts
with the file_contexts.bin magic number, and if so, automatically
treat it as a file_contexts.bin file.  This allows one to open
file_contexts.bin formatted files without necessarily having a .bin
file suffix.  This removes the need for the previously added
.bin file suffix test.

Change-Id: I6a0cb303954cc6fa24c437ccc794104859eac24b
Signed-off-by: default avatarStephen Smalley <sds@tycho.nsa.gov>
parent 8b40b9cc
...@@ -97,7 +97,7 @@ static int nodups_specs(struct saved_data *data, const char *path) ...@@ -97,7 +97,7 @@ static int nodups_specs(struct saved_data *data, const char *path)
} }
static int load_mmap(struct selabel_handle *rec, const char *path, static int load_mmap(struct selabel_handle *rec, const char *path,
struct stat *sb) struct stat *sb, bool isbinary)
{ {
struct saved_data *data = (struct saved_data *)rec->data; struct saved_data *data = (struct saved_data *)rec->data;
char mmap_path[PATH_MAX + 1]; char mmap_path[PATH_MAX + 1];
...@@ -111,8 +111,8 @@ static int load_mmap(struct selabel_handle *rec, const char *path, ...@@ -111,8 +111,8 @@ static int load_mmap(struct selabel_handle *rec, const char *path,
uint32_t i, magic, version; uint32_t i, magic, version;
uint32_t entry_len, stem_map_len, regex_array_len; uint32_t entry_len, stem_map_len, regex_array_len;
len = strlen(path); if (isbinary) {
if (len > 4 && !strcmp(&path[len-4], ".bin")) { len = strlen(path);
if (len >= sizeof(mmap_path)) if (len >= sizeof(mmap_path))
return -1; return -1;
strcpy(mmap_path, path); strcpy(mmap_path, path);
...@@ -412,6 +412,8 @@ static int process_file(const char *path, const char *suffix, ...@@ -412,6 +412,8 @@ static int process_file(const char *path, const char *suffix,
char *line_buf = NULL; char *line_buf = NULL;
int rc; int rc;
char stack_path[PATH_MAX + 1]; char stack_path[PATH_MAX + 1];
bool isbinary = false;
uint32_t magic;
/* append the path suffix if we have one */ /* append the path suffix if we have one */
if (suffix) { if (suffix) {
...@@ -433,6 +435,21 @@ static int process_file(const char *path, const char *suffix, ...@@ -433,6 +435,21 @@ static int process_file(const char *path, const char *suffix,
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
if (fread(&magic, sizeof magic, 1, fp) != 1) {
errno = EINVAL;
fclose(fp);
return -1;
}
if (magic == SELINUX_MAGIC_COMPILED_FCONTEXT) {
/* file_contexts.bin format */
fclose(fp);
fp = NULL;
isbinary = true;
} else {
rewind(fp);
}
} else { } else {
/* /*
* Text file does not exist, so clear the timestamp * Text file does not exist, so clear the timestamp
...@@ -442,7 +459,7 @@ static int process_file(const char *path, const char *suffix, ...@@ -442,7 +459,7 @@ static int process_file(const char *path, const char *suffix,
sb.st_mtime = 0; sb.st_mtime = 0;
} }
rc = load_mmap(rec, path, &sb); rc = load_mmap(rec, path, &sb, isbinary);
if (rc == 0) if (rc == 0)
goto out; goto out;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment