Skip to contentSkip to conversation

mmap file_contexts and property_contexts:

RFC In review [email protected] 4 participants 13 messages Sep 19, 2016 – Sep 20, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

RFC 1/1 mmap file_contexts and property_contexts:

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140 +++++++++++++++++++-------------
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label.c +0 -2
index 963bfcb..d767b49 100644
@@ -15,8 +15,6 @@
 #include "callbacks.h"
 #include "label_internal.h"
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 typedef int (*selabel_initfunc)(struct selabel_handle *rec,
 				const struct selinux_opt *opts,
 				unsigned nopts);
libselinux/src/label_android_property.c +12 -10
index 290b438..2aac394 100644
@@ -85,13 +85,19 @@ static int process_line(struct selabel_handle *rec,
 			int pass, unsigned lineno)
 {
 	int items;
-	char *prop = NULL, *context = NULL;
+	union {
+		struct {
+			char *prop;
+			char *context;
+		};
+		char *array[2];
+	} found = { .array = { 0 } };
 	struct saved_data *data = (struct saved_data *)rec->data;
 	spec_t *spec_arr = data->spec_arr;
 	unsigned int nspec = data->nspec;
 	const char *errbuf = NULL;
 
-	items = read_spec_entries(line_buf, &errbuf, 2, &prop, &context);
+	items = read_spec_entries(line_buf, &errbuf, ARRAY_SIZE(found.array), found.array);
 	if (items < 0) {
 		items = errno;
 		selinux_log(SELINUX_ERROR,
@@ -108,18 +114,14 @@ static int process_line(struct selabel_handle *rec,
 		selinux_log(SELINUX_ERROR,
 			    "%s:  line %u is missing fields\n", path,
 			    lineno);
-		free(prop);
 		errno = EINVAL;
 		return -1;
 	}
 
-	if (pass == 0) {
-		free(prop);
-		free(context);
-	} else if (pass == 1) {
+	if (pass == 1) {
 		/* On the second pass, process and store the specification in spec. */
-		spec_arr[nspec].property_key = prop;
-		spec_arr[nspec].lr.ctx_raw = context;
+		spec_arr[nspec].property_key = found.prop;
+		spec_arr[nspec].lr.ctx_raw = found.context;
 
 		if (rec->validating) {
 			if (selabel_validate(rec, &spec_arr[nspec].lr) < 0) {
@@ -234,7 +236,7 @@ static void closef(struct selabel_handle *rec)
 	for (i = 0; i < data->nspec; i++) {
 		spec = &data->spec_arr[i];
 		free(spec->property_key);
-		free(spec->lr.ctx_raw);
+		//free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
 	}
 
libselinux/src/label_file.c +84 -56

226 lines not on this page · raw message

libselinux/src/label_file.h +42 -24

149 lines not on this page · raw message

libselinux/src/label_internal.h +2 -1
index aa48fff..e40d68b 100644
@@ -136,10 +136,11 @@ compat_validate(struct selabel_handle *rec,
 		struct selabel_lookup_rec *contexts,
 		const char *path, unsigned lineno) hidden;
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 /*
  * The read_spec_entries function may be used to
  * replace sscanf to read entries from spec files.
  */
-extern int read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...);
+extern int hidden read_spec_entries(char *line_buf, const char **errbuf, unsigned int num, char *found[]);
 
 #endif				/* _SELABEL_INTERNAL_H_ */
libselinux/src/label_support.c +32 -47

112 lines not on this page · raw message

Signature · 1 line
1.9.1
#1 lore raw

FYI I only tested this with checkfc...

Quoted 683 lines

-----Original Message-----
From: Roberts, William C
Sent: Monday, September 19, 2016 2:45 PM
To: [email protected]; [email protected]; [email protected]; [email protected]
Cc: Roberts, William C <[email protected]>
Subject: [RFC] mmap file_contexts and property_contexts:

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140 +++++++++++++++++++-------------
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label.c index 963bfcb..d767b49 renamed from libselinux/src/label.c +0 -0

100644

libselinux/src/label.c +0 -2
@@ -15,8 +15,6 @@
 #include "callbacks.h"
 #include "label_internal.h"
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 typedef int (*selabel_initfunc)(struct selabel_handle *rec,
 				const struct selinux_opt *opts,
 				unsigned nopts);
libselinux/src/label_android_property.c +10 -7
index 290b438..2aac394 100644
@@ -85,13 +85,19 @@ static int process_line(struct selabel_handle *rec,
 			int pass, unsigned lineno)
 {
 	int items;
-	char *prop = NULL, *context = NULL;
+	union {
+		struct {
+			char *prop;
+			char *context;
+		};
+		char *array[2];
+	} found = { .array = { 0 } };
 	struct saved_data *data = (struct saved_data *)rec->data;
 	spec_t *spec_arr = data->spec_arr;
 	unsigned int nspec = data->nspec;
 	const char *errbuf = NULL;
 
-	items = read_spec_entries(line_buf, &errbuf, 2, &prop, &context);
+	items = read_spec_entries(line_buf, &errbuf, ARRAY_SIZE(found.array),
+found.array);
 	if (items < 0) {
 		items = errno;
 		selinux_log(SELINUX_ERROR,
@@ -108,18 +114,14 @@ static int process_line(struct selabel_handle *rec,
 		selinux_log(SELINUX_ERROR,
 			    "%s:  line %u is missing fields\n", path,
 			    lineno);
-		free(prop);
 		errno = EINVAL;
 		return -1;
 	}
 
-	if (pass == 0) {
-		free(prop);
-		free(context);
-	} else if (pass == 1) {
+	if (pass == 1) {
 		/* On the second pass, process and store the specification in

spec. */

libselinux/src/label_android_property.c +2 -2
-		spec_arr[nspec].property_key = prop;
-		spec_arr[nspec].lr.ctx_raw = context;
+		spec_arr[nspec].property_key = found.prop;
+		spec_arr[nspec].lr.ctx_raw = found.context;
 
 		if (rec->validating) {
 			if (selabel_validate(rec, &spec_arr[nspec].lr) < 0) { @@ -

234,7 +236,7 @@ static void closef(struct selabel_handle *rec)

libselinux/src/label_android_property.c +1 -1
 	for (i = 0; i < data->nspec; i++) {
 		spec = &data->spec_arr[i];
 		free(spec->property_key);
-		free(spec->lr.ctx_raw);
+		//free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
 	}
libselinux/src/label_file.c index renamed from libselinux/src/label_file.c +0 -0

7156825..4dc440e 100644

libselinux/src/label_file.c +31 -4
@@ -96,43 +96,64 @@ static int nodups_specs(struct saved_data *data, const
 char *path)
 	return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-			     struct selabel_handle *rec, const char *path)
+static inline struct saved_data *rec_to_data(struct selabel_handle
+*rec) {
+	return (struct saved_data *)rec->data; }
+
+static char *mmap_area_get_line(struct mmap_area *area) {
+	size_t len = area->next_len;
+	if (!len)
+		return NULL;
+
+	char *start = area->next_addr;
+	char *end = memchr(start, '\n', len);
+
+	/* the file may not end with a newline */
+	if (!end)
+		end = (char *)area->next_addr + len - 1;
+
+	*end = '\0';
+	/* len includes null byte */
+	len = end - start;
+
+	area->next_len -= len + 1;
+	area->next_addr = ++end;
+	return start;
+}
+
+static int process_text_file(const char *path, const char *prefix,
+		struct selabel_handle *rec)
 {
 	int rc;
-	size_t line_len;
 	unsigned int lineno = 0;
-	char *line_buf = NULL;
+	char *line_buf;
+	struct mmap_area *areas = rec_to_data(rec)->mmap_areas;
+
+	/* mmap_area_get_line() and process_line() require mutable string

pointers */

libselinux/src/label_file.c +22 -22
+	rc = mprotect(areas->addr, areas->len, PROT_READ | PROT_WRITE);
+	if (rc < 0)
+		goto out;
 
-	while (getline(&line_buf, &line_len, fp) > 0) {
+	while ( (line_buf = mmap_area_get_line(areas)) ) {
 		rc = process_line(rec, path, prefix, line_buf, ++lineno);
 		if (rc)
 			goto out;
 	}
 	rc = 0;
 out:
-	free(line_buf);
 	return rc;
 }
 
-static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
-		     const char *path)
+static int mmap_file(FILE *fp, size_t len, struct saved_data *data)
 {
-	struct saved_data *data = (struct saved_data *)rec->data;
-	int rc;
-	char *addr, *str_buf;
-	int *stem_map;
-	struct mmap_area *mmap_area;
-	uint32_t i, magic, version;
-	uint32_t entry_len, stem_map_len, regex_array_len;
-	const char *reg_version;
-
-	mmap_area = malloc(sizeof(*mmap_area));
+	struct mmap_area *mmap_area = malloc(sizeof(*mmap_area));
 	if (!mmap_area) {
 		return -1;
 	}
 
-	addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
+	void *addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
 	if (addr == MAP_FAILED) {
 		free(mmap_area);
 		perror("mmap");
@@ -145,17 +166,26 @@ static int load_mmap(FILE *fp, size_t len, struct
 selabel_handle *rec,
 	mmap_area->next = data->mmap_areas;
 	data->mmap_areas = mmap_area;
 
-	/* check if this looks like an fcontext file */
-	rc = next_entry(&magic, mmap_area, sizeof(uint32_t));
-	if (rc < 0 || magic != SELINUX_MAGIC_COMPILED_FCONTEXT)
-		return -1;
+	return 0;
+}
+
+static int process_binary_file(const char *path, struct selabel_handle
+*rec) {
+	struct saved_data *data = (struct saved_data *)rec->data;
+	int rc;
+	char *str_buf;
+	int *stem_map;
+	struct mmap_area *mmap_area = data->mmap_areas;
+	uint32_t i, version;
+	uint32_t entry_len, stem_map_len, regex_array_len;
+	size_t len;
 
 	/* check if this version is higher than we understand */
 	rc = next_entry(&version, mmap_area, sizeof(uint32_t));
 	if (rc < 0 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
 		return -1;
 
-	reg_version = regex_version();
+	const char *reg_version = regex_version();
 	if (!reg_version)
 		return -1;
@@ -235,12 +265,11 @@ static int load_mmap(FILE *fp, size_t len, struct
 selabel_handle *rec,
 		/* store the mapping between old and new */
 		newid = find_stem(data, buf, stem_len);
 		if (newid < 0) {
-			newid = store_stem(data, buf, stem_len);
+			newid = store_stem(data, buf, stem_len, true);
 			if (newid < 0) {
 				rc = newid;
 				goto out;
 			}
-			data->stem_arr[newid].from_mmap = 1;
 		}
 		stem_map[i] = newid;
libselinux/src/label_file.c +0 -1
 	}
@@ -262,7 +291,6 @@ static int load_mmap(FILE *fp, size_t len, struct
 selabel_handle *rec,
 			goto out;
 
 		spec = &data->spec_arr[data->nspec];
-		spec->from_mmap = 1;
 
 		/* Process context */

rc = next_entry(&entry_len, mmap_area, sizeof(uint32_t)); @@ -
271,22 +299,10 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

libselinux/src/label_file.c +19 -19
 			goto out;
 		}
 
-		str_buf = malloc(entry_len);
-		if (!str_buf) {
-			rc = -1;
-			goto out;
-		}
-		rc = next_entry(str_buf, mmap_area, entry_len);
+		rc = next_pstr(&spec->lr.ctx_raw, mmap_area, entry_len);
 		if (rc < 0)
 			goto out;
 
-		if (str_buf[entry_len - 1] != '\0') {
-			free(str_buf);
-			rc = -1;
-			goto out;
-		}
-		spec->lr.ctx_raw = str_buf;
-
 		if (strcmp(spec->lr.ctx_raw, "<<none>>") && rec->validating) {
 			if (selabel_validate(rec, &spec->lr) < 0) {
 				selinux_log(SELINUX_ERROR,
@@ -401,17 +417,29 @@ static char *rolling_append(char *current, const char
 *suffix, size_t max)
 	return current;
 }
 
-static bool fcontext_is_binary(FILE *fp)
+static inline int mmap_area_rewind(struct mmap_area *area, size_t
+bytes) {
+	if (area->next_len + bytes > area->len)
+		return -1;
+
+	area->next_addr = (char *) area->next_addr - bytes;
+	area->next_len += bytes;
+	return 0;
+}
+
+static bool fcontext_is_binary(struct mmap_area *area)
 {
 	uint32_t magic;
+	bool is_binary = false;
 
-	size_t len = fread(&magic, sizeof(magic), 1, fp);
-	rewind(fp);
+	int rc = next_entry(&magic, area, sizeof(magic));
 
-	return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
-}
+	is_binary = (!rc && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
+	if (!is_binary)
+		mmap_area_rewind(area, sizeof(magic));
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+	return is_binary;
+}
 
 static FILE *open_file(const char *path, const char *suffix,

char *save_path, size_t len, struct stat *sb, bool open_oldest) @@ -
504,12 +532,17 @@ static int process_file(const char *path, const char *suffix,

libselinux/src/label_file.c +10 -5
 		if (fp == NULL)
 			return -1;
 
-		rc = fcontext_is_binary(fp) ?
-				load_mmap(fp, sb.st_size, rec, found_path) :
-				process_text_file(fp, prefix, rec, found_path);
+		rc = mmap_file(fp, sb.st_size, rec_to_data(rec));
+		if (rc < 0) {
+			fclose(fp);
+			return -1;
+		}
+
+		rc = fcontext_is_binary(rec_to_data(rec)->mmap_areas) ?
+				process_binary_file(found_path, rec) :
+				process_text_file(found_path, prefix, rec);
 		if (!rc)
-			rc = digest_add_specfile(digest, fp, NULL, sb.st_size,
-				found_path);
+			rc = digest_add_specfile(digest, fp, NULL, sb.st_size,

found_path);

libselinux/src/label_file.c +0 -5
 		fclose(fp);
@@ -613,12 +646,7 @@ static void closef(struct selabel_handle *rec)
 	for (i = 0; i < data->nspec; i++) {
 		spec = &data->spec_arr[i];
 		free(spec->lr.ctx_trans);
-		free(spec->lr.ctx_raw);
 		regex_data_free(spec->regex);
-		if (spec->from_mmap)
-			continue;
-		free(spec->regex_str);
-		free(spec->type_str);
 	}
 
 	for (i = 0; i < (unsigned int)data->num_stems; i++) { diff --git

a/libselinux/src/label_file.h b/libselinux/src/label_file.h index 88f4294..2704906 100644

libselinux/src/label_file.h +3 -2
@@ -37,7 +37,6 @@ struct spec {
 	int matches;		/* number of matching pathnames */
 	int stem_id;		/* indicates which stem-compression item */
 	char hasMetaChars;	/* regular expression has meta-chars */
-	char from_mmap;		/* this spec is from an mmap of the data
 */
 	size_t prefix_len;      /* length of fixed path prefix */
 };
@@ -255,7 +254,7 @@ static inline int find_stem(struct saved_data *data, const
 char *buf,  }
 
 /* returns the index of the new stored object */ -static inline int
 store_stem(struct saved_data *data, char *buf, int stem_len)
+static inline int store_stem(struct saved_data *data, char *buf, int
+stem_len, bool from_mmap)
 {
 	int num = data->num_stems;
@@ -271,7 +270,7 @@ static inline int store_stem(struct saved_data *data, char
 *buf, int stem_len)
 	}
 	data->stem_arr[num].len = stem_len;
 	data->stem_arr[num].buf = buf;
-	data->stem_arr[num].from_mmap = 0;
+	data->stem_arr[num].from_mmap = from_mmap;
 	data->num_stems++;
 
libselinux/src/label_file.h +1 -1
 	return num;
@@ -298,7 +297,7 @@ static inline int find_stem_from_spec(struct saved_data
 *data, const char *buf)
 	if (!stem)
 		return -1;
 
-	return store_stem(data, stem, stem_len);
+	return store_stem(data, stem, stem_len, false);
 }
 

/* This will always check for buffer over-runs and either read the next entry @@
-317,6 +316,22 @@ static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

libselinux/src/label_file.h +41 -20
 	return 0;
 }
 
+static inline int next_pstr(char **str, struct mmap_area *fp, size_t
+len) {
+	if (len > fp->next_len)
+		return -1;
+
+	char *tmp = (char *)fp->next_addr;
+	if (tmp[len-1] != '\0')
+		return -1;
+
+	*str = tmp;
+
+	fp->next_addr = (char *)fp->next_addr + len;
+	fp->next_len -= len;
+	return 0;
+}
+
 static inline int compile_regex(struct saved_data *data, struct spec *spec,
 					    const char **errbuf)
 {
@@ -375,13 +390,21 @@ static inline int process_line(struct selabel_handle *rec,
 			char *line_buf, unsigned lineno)
 {
 	int items, len, rc;
-	char *regex = NULL, *type = NULL, *context = NULL;
+	union {
+		struct {
+			char *regex;
+			char *type;
+			char *context;
+		};
+		char *array[3];
+	} found = { .array = { 0 } };
+
 	struct saved_data *data = (struct saved_data *)rec->data;
 	struct spec *spec_arr;
 	unsigned int nspec = data->nspec;
 	const char *errbuf = NULL;
 
-	items = read_spec_entries(line_buf, &errbuf, 3, &regex, &type,
 &context);
+	items = read_spec_entries(line_buf, &errbuf, ARRAY_SIZE(found.array),
+found.array);
 	if (items < 0) {
 		rc = errno;
 		selinux_log(SELINUX_ERROR,
@@ -398,22 +421,17 @@ static inline int process_line(struct selabel_handle *rec,
 		COMPAT_LOG(SELINUX_ERROR,
 			    "%s:  line %u is missing fields\n", path,
 			    lineno);
-		if (items == 1)
-			free(regex);
 		errno = EINVAL;
 		return -1;
 	} else if (items == 2) {
 		/* The type field is optional. */
-		context = type;
-		type = 0;
+		found.context = found.type;
+		found.type = NULL;
 	}
 
-	len = get_stem_from_spec(regex);
-	if (len && prefix && strncmp(prefix, regex, len)) {
+	len = get_stem_from_spec(found.regex);
+	if (len && prefix && strncmp(prefix, found.regex, len)) {
 		/* Stem of regex does not match requested prefix, discard. */
-		free(regex);
-		free(type);
-		free(context);
 		return 0;
 	}
 
@@ -424,13 +442,13 @@ static inline int process_line(struct selabel_handle *rec,
 	spec_arr = data->spec_arr;
 
 	/* process and store the specification in spec. */
-	spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
-	spec_arr[nspec].regex_str = regex;
+	spec_arr[nspec].stem_id = find_stem_from_spec(data, found.regex);
+	spec_arr[nspec].regex_str = found.regex;
 
-	spec_arr[nspec].type_str = type;
+	spec_arr[nspec].type_str = found.type;
 	spec_arr[nspec].mode = 0;
 
-	spec_arr[nspec].lr.ctx_raw = context;
+	spec_arr[nspec].lr.ctx_raw = found.context;
 
 	/*
 	 * bump data->nspecs to cause closef() to cover it in its free @@ -442,18
+460,18 @@ static inline int process_line(struct selabel_handle *rec,
 			&& compile_regex(data, &spec_arr[nspec], &errbuf)) {
 		COMPAT_LOG(SELINUX_ERROR,
 			   "%s:  line %u has invalid regex %s:  %s\n",
-			   path, lineno, regex, errbuf);
+			   path, lineno, found.regex, errbuf);
 		errno = EINVAL;
 		return -1;
 	}
 
-	if (type) {
-		mode_t mode = string_to_mode(type);
+	if (found.type) {
+		mode_t mode = string_to_mode(found.type);
 
 		if (mode == (mode_t)-1) {
 			COMPAT_LOG(SELINUX_ERROR,
 				   "%s:  line %u has invalid file type %s\n",
-				   path, lineno, type);
+				   path, lineno, found.type);
 			errno = EINVAL;
 			return -1;
 		}
@@ -464,7 +482,7 @@ static inline int process_line(struct selabel_handle *rec,
 	 * any meta characters in the RE */
 	spec_hasMetaChars(&spec_arr[nspec]);
 
-	if (strcmp(context, "<<none>>") && rec->validating)
+	if (strcmp(found.context, "<<none>>") && rec->validating)
 		compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
 	return 0;
libselinux/src/label_internal.h index renamed from libselinux/src/label_internal.h +0 -0

aa48fff..e40d68b 100644

libselinux/src/label_internal.h +3 -1
@@ -136,10 +136,11 @@ compat_validate(struct selabel_handle *rec,
 		struct selabel_lookup_rec *contexts,
 		const char *path, unsigned lineno) hidden;
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 /*
  * The read_spec_entries function may be used to
  * replace sscanf to read entries from spec files.
  */
-extern int read_spec_entries(char *line_buf, const char **errbuf, int num_args,
 ...);
+extern int hidden read_spec_entries(char *line_buf, const char
+**errbuf, unsigned int num, char *found[]);
 
 #endif				/* _SELABEL_INTERNAL_H_ */
libselinux/src/label_support.c index renamed from libselinux/src/label_support.c +0 -0

26f9ef1..3f5728d 100644

libselinux/src/label_support.c +24 -22
@@ -17,39 +17,40 @@
  * Read an entry from a spec file (e.g. file_contexts)
  * entry - Buffer to allocate for the entry.
  * ptr - current location of the line to be processed.
- * returns  - 0 on success and *entry is set to be a null
- *            terminated value. On Error it returns -1 and
- *            errno will be set.
+ * returns  - a pointer to the begining of the string.
+ *            that is the
  *
  */
-static inline int read_spec_entry(char **entry, char **ptr, int *len, const char
 **errbuf)
+static inline char *read_spec_entry(char **current, const char
+**errbuf)
 {
-	*entry = NULL;
-	char *tmp_buf = NULL;
-
-	while (isspace(**ptr) && **ptr != '\0')
-		(*ptr)++;
+	if(!**current) {
+		(*current)++;
+		return NULL;
+	}
 
-	tmp_buf = *ptr;
-	*len = 0;
+	while (isspace(**current) && **current != '\0')
+		(*current)++;
 
-	while (!isspace(**ptr) && **ptr != '\0') {
-		if (!isascii(**ptr)) {
+	char *start = *current;
+	while (!isspace(**current) && **current != '\0') {
+		if (!isascii(**current)) {
 			errno = EINVAL;
 			*errbuf = "Non-ASCII characters found";
-			return -1;
+			return NULL;
 		}
-		(*ptr)++;
-		(*len)++;
+		(*current)++;
 	}
 
-	if (*len) {
-		*entry = strndup(tmp_buf, *len);
-		if (!*entry)
-			return -1;
-	}
+	char *end = *current;
 
-	return 0;
+	if (start == end)
+		return NULL;
+
+	if (*end != '\0') {
+		*end = '\0';
+		(*current)++;
+	}
+	return start;
 }
 
 /*
@@ -63,15 +64,13 @@ static inline int read_spec_entry(char **entry, char **ptr,

int *len, const char
* This function calls read_spec_entry() to do the actual string processing.
* As such, can return anything from that function as well.
*/
-int hidden read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...)

libselinux/src/label_support.c +10 -25
+int hidden read_spec_entries(char *line_buf, const char **errbuf,
+unsigned int num, char *found[])
 {
-	char **spec_entry, *buf_p;
-	int len, rc, items, entry_len = 0;
-	va_list ap;
-
+	char*buf_p;
+	unsigned int items = 0;
 	*errbuf = NULL;
 
-	len = strlen(line_buf);
+	size_t len = strlen(line_buf);
 	if (line_buf[len - 1] == '\n')
 		line_buf[len - 1] = '\0';
 	else
@@ -89,26 +88,12 @@ int hidden read_spec_entries(char *line_buf, const char
 **errbuf, int num_args,
 		return 0;
 
 	/* Process the spec file entries */
-	va_start(ap, num_args);
-
-	items = 0;
-	while (items < num_args) {
-		spec_entry = va_arg(ap, char **);
-
-		if (len - 1 == buf_p - line_buf) {
-			va_end(ap);
-			return items;
-		}
-
-		rc = read_spec_entry(spec_entry, &buf_p, &entry_len, errbuf);
-		if (rc < 0) {
-			va_end(ap);
-			return rc;
-		}
-		if (entry_len)
-			items++;
+	while (items < num) {
+		found[items] = read_spec_entry(&buf_p, errbuf);
+		if (!found[items])
+			break;
+		items++;
 	}
-	va_end(ap);
 	return items;
 }
 
--

1.9.1

#2 lore raw
Quoted 1 line · William C

FYI I only tested this with checkfc...

Evidently. matchpathcon and sefcontext_compile both report calls to free() on invalid pointers and abort.

Quoted 686 lines
Quoted 684 lines

-----Original Message-----
From: Roberts, William C
Sent: Monday, September 19, 2016 2:45 PM
To: [email protected]; [email protected]; [email protected]; [email protected]
Cc: Roberts, William C <[email protected]>
Subject: [RFC] mmap file_contexts and property_contexts:

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140 +++++++++++++++++++-------------
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label.c index 963bfcb..d767b49 renamed from libselinux/src/label.c +0 -0

100644

libselinux/src/label.c +0 -2
@@ -15,8 +15,6 @@
 #include "callbacks.h"
 #include "label_internal.h"
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 typedef int (*selabel_initfunc)(struct selabel_handle *rec,
 				const struct selinux_opt *opts,
 				unsigned nopts);
libselinux/src/label_android_property.c +10 -7
index 290b438..2aac394 100644
@@ -85,13 +85,19 @@ static int process_line(struct selabel_handle *rec,
 			int pass, unsigned lineno)
 {
 	int items;
-	char *prop = NULL, *context = NULL;
+	union {
+		struct {
+			char *prop;
+			char *context;
+		};
+		char *array[2];
+	} found = { .array = { 0 } };
 	struct saved_data *data = (struct saved_data *)rec->data;
 	spec_t *spec_arr = data->spec_arr;
 	unsigned int nspec = data->nspec;
 	const char *errbuf = NULL;
 
-	items = read_spec_entries(line_buf, &errbuf, 2, &prop, &context);
+	items = read_spec_entries(line_buf, &errbuf, ARRAY_SIZE(found.array),
+found.array);
 	if (items < 0) {
 		items = errno;
 		selinux_log(SELINUX_ERROR,
@@ -108,18 +114,14 @@ static int process_line(struct selabel_handle *rec,
 		selinux_log(SELINUX_ERROR,
 			    "%s:  line %u is missing fields\n", path,
 			    lineno);
-		free(prop);
 		errno = EINVAL;
 		return -1;
 	}
 
-	if (pass == 0) {
-		free(prop);
-		free(context);
-	} else if (pass == 1) {
+	if (pass == 1) {
 		/* On the second pass, process and store the specification in

spec. */

libselinux/src/label_android_property.c +2 -2
-		spec_arr[nspec].property_key = prop;
-		spec_arr[nspec].lr.ctx_raw = context;
+		spec_arr[nspec].property_key = found.prop;
+		spec_arr[nspec].lr.ctx_raw = found.context;
 
 		if (rec->validating) {
 			if (selabel_validate(rec, &spec_arr[nspec].lr) < 0) { @@ -

234,7 +236,7 @@ static void closef(struct selabel_handle *rec)

libselinux/src/label_android_property.c +1 -1
 	for (i = 0; i < data->nspec; i++) {
 		spec = &data->spec_arr[i];
 		free(spec->property_key);
-		free(spec->lr.ctx_raw);
+		//free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
 	}
libselinux/src/label_file.c index renamed from libselinux/src/label_file.c +0 -0

7156825..4dc440e 100644

libselinux/src/label_file.c +31 -4
@@ -96,43 +96,64 @@ static int nodups_specs(struct saved_data *data, const
 char *path)
 	return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-			     struct selabel_handle *rec, const char *path)
+static inline struct saved_data *rec_to_data(struct selabel_handle
+*rec) {
+	return (struct saved_data *)rec->data; }
+
+static char *mmap_area_get_line(struct mmap_area *area) {
+	size_t len = area->next_len;
+	if (!len)
+		return NULL;
+
+	char *start = area->next_addr;
+	char *end = memchr(start, '\n', len);
+
+	/* the file may not end with a newline */
+	if (!end)
+		end = (char *)area->next_addr + len - 1;
+
+	*end = '\0';
+	/* len includes null byte */
+	len = end - start;
+
+	area->next_len -= len + 1;
+	area->next_addr = ++end;
+	return start;
+}
+
+static int process_text_file(const char *path, const char *prefix,
+		struct selabel_handle *rec)
 {
 	int rc;
-	size_t line_len;
 	unsigned int lineno = 0;
-	char *line_buf = NULL;
+	char *line_buf;
+	struct mmap_area *areas = rec_to_data(rec)->mmap_areas;
+
+	/* mmap_area_get_line() and process_line() require mutable string

pointers */

libselinux/src/label_file.c +22 -22
+	rc = mprotect(areas->addr, areas->len, PROT_READ | PROT_WRITE);
+	if (rc < 0)
+		goto out;
 
-	while (getline(&line_buf, &line_len, fp) > 0) {
+	while ( (line_buf = mmap_area_get_line(areas)) ) {
 		rc = process_line(rec, path, prefix, line_buf, ++lineno);
 		if (rc)
 			goto out;
 	}
 	rc = 0;
 out:
-	free(line_buf);
 	return rc;
 }
 
-static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
-		     const char *path)
+static int mmap_file(FILE *fp, size_t len, struct saved_data *data)
 {
-	struct saved_data *data = (struct saved_data *)rec->data;
-	int rc;
-	char *addr, *str_buf;
-	int *stem_map;
-	struct mmap_area *mmap_area;
-	uint32_t i, magic, version;
-	uint32_t entry_len, stem_map_len, regex_array_len;
-	const char *reg_version;
-
-	mmap_area = malloc(sizeof(*mmap_area));
+	struct mmap_area *mmap_area = malloc(sizeof(*mmap_area));
 	if (!mmap_area) {
 		return -1;
 	}
 
-	addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
+	void *addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
 	if (addr == MAP_FAILED) {
 		free(mmap_area);
 		perror("mmap");
@@ -145,17 +166,26 @@ static int load_mmap(FILE *fp, size_t len, struct
 selabel_handle *rec,
 	mmap_area->next = data->mmap_areas;
 	data->mmap_areas = mmap_area;
 
-	/* check if this looks like an fcontext file */
-	rc = next_entry(&magic, mmap_area, sizeof(uint32_t));
-	if (rc < 0 || magic != SELINUX_MAGIC_COMPILED_FCONTEXT)
-		return -1;
+	return 0;
+}
+
+static int process_binary_file(const char *path, struct selabel_handle
+*rec) {
+	struct saved_data *data = (struct saved_data *)rec->data;
+	int rc;
+	char *str_buf;
+	int *stem_map;
+	struct mmap_area *mmap_area = data->mmap_areas;
+	uint32_t i, version;
+	uint32_t entry_len, stem_map_len, regex_array_len;
+	size_t len;
 
 	/* check if this version is higher than we understand */
 	rc = next_entry(&version, mmap_area, sizeof(uint32_t));
 	if (rc < 0 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
 		return -1;
 
-	reg_version = regex_version();
+	const char *reg_version = regex_version();
 	if (!reg_version)
 		return -1;
@@ -235,12 +265,11 @@ static int load_mmap(FILE *fp, size_t len, struct
 selabel_handle *rec,
 		/* store the mapping between old and new */
 		newid = find_stem(data, buf, stem_len);
 		if (newid < 0) {
-			newid = store_stem(data, buf, stem_len);
+			newid = store_stem(data, buf, stem_len, true);
 			if (newid < 0) {
 				rc = newid;
 				goto out;
 			}
-			data->stem_arr[newid].from_mmap = 1;
 		}
 		stem_map[i] = newid;
libselinux/src/label_file.c +0 -1
 	}
@@ -262,7 +291,6 @@ static int load_mmap(FILE *fp, size_t len, struct
 selabel_handle *rec,
 			goto out;
 
 		spec = &data->spec_arr[data->nspec];
-		spec->from_mmap = 1;
 
 		/* Process context */

rc = next_entry(&entry_len, mmap_area, sizeof(uint32_t)); @@ -
271,22 +299,10 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

libselinux/src/label_file.c +19 -19
 			goto out;
 		}
 
-		str_buf = malloc(entry_len);
-		if (!str_buf) {
-			rc = -1;
-			goto out;
-		}
-		rc = next_entry(str_buf, mmap_area, entry_len);
+		rc = next_pstr(&spec->lr.ctx_raw, mmap_area, entry_len);
 		if (rc < 0)
 			goto out;
 
-		if (str_buf[entry_len - 1] != '\0') {
-			free(str_buf);
-			rc = -1;
-			goto out;
-		}
-		spec->lr.ctx_raw = str_buf;
-
 		if (strcmp(spec->lr.ctx_raw, "<<none>>") && rec->validating) {
 			if (selabel_validate(rec, &spec->lr) < 0) {
 				selinux_log(SELINUX_ERROR,
@@ -401,17 +417,29 @@ static char *rolling_append(char *current, const char
 *suffix, size_t max)
 	return current;
 }
 
-static bool fcontext_is_binary(FILE *fp)
+static inline int mmap_area_rewind(struct mmap_area *area, size_t
+bytes) {
+	if (area->next_len + bytes > area->len)
+		return -1;
+
+	area->next_addr = (char *) area->next_addr - bytes;
+	area->next_len += bytes;
+	return 0;
+}
+
+static bool fcontext_is_binary(struct mmap_area *area)
 {
 	uint32_t magic;
+	bool is_binary = false;
 
-	size_t len = fread(&magic, sizeof(magic), 1, fp);
-	rewind(fp);
+	int rc = next_entry(&magic, area, sizeof(magic));
 
-	return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
-}
+	is_binary = (!rc && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
+	if (!is_binary)
+		mmap_area_rewind(area, sizeof(magic));
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+	return is_binary;
+}
 
 static FILE *open_file(const char *path, const char *suffix,

char *save_path, size_t len, struct stat *sb, bool open_oldest) @@ -
504,12 +532,17 @@ static int process_file(const char *path, const char *suffix,

libselinux/src/label_file.c +10 -5
 		if (fp == NULL)
 			return -1;
 
-		rc = fcontext_is_binary(fp) ?
-				load_mmap(fp, sb.st_size, rec, found_path) :
-				process_text_file(fp, prefix, rec, found_path);
+		rc = mmap_file(fp, sb.st_size, rec_to_data(rec));
+		if (rc < 0) {
+			fclose(fp);
+			return -1;
+		}
+
+		rc = fcontext_is_binary(rec_to_data(rec)->mmap_areas) ?
+				process_binary_file(found_path, rec) :
+				process_text_file(found_path, prefix, rec);
 		if (!rc)
-			rc = digest_add_specfile(digest, fp, NULL, sb.st_size,
-				found_path);
+			rc = digest_add_specfile(digest, fp, NULL, sb.st_size,

found_path);

libselinux/src/label_file.c +0 -5
 		fclose(fp);
@@ -613,12 +646,7 @@ static void closef(struct selabel_handle *rec)
 	for (i = 0; i < data->nspec; i++) {
 		spec = &data->spec_arr[i];
 		free(spec->lr.ctx_trans);
-		free(spec->lr.ctx_raw);
 		regex_data_free(spec->regex);
-		if (spec->from_mmap)
-			continue;
-		free(spec->regex_str);
-		free(spec->type_str);
 	}
 
 	for (i = 0; i < (unsigned int)data->num_stems; i++) { diff --git

a/libselinux/src/label_file.h b/libselinux/src/label_file.h index 88f4294..2704906 100644

libselinux/src/label_file.h +3 -2
@@ -37,7 +37,6 @@ struct spec {
 	int matches;		/* number of matching pathnames */
 	int stem_id;		/* indicates which stem-compression item */
 	char hasMetaChars;	/* regular expression has meta-chars */
-	char from_mmap;		/* this spec is from an mmap of the data
 */
 	size_t prefix_len;      /* length of fixed path prefix */
 };
@@ -255,7 +254,7 @@ static inline int find_stem(struct saved_data *data, const
 char *buf,  }
 
 /* returns the index of the new stored object */ -static inline int
 store_stem(struct saved_data *data, char *buf, int stem_len)
+static inline int store_stem(struct saved_data *data, char *buf, int
+stem_len, bool from_mmap)
 {
 	int num = data->num_stems;
@@ -271,7 +270,7 @@ static inline int store_stem(struct saved_data *data, char
 *buf, int stem_len)
 	}
 	data->stem_arr[num].len = stem_len;
 	data->stem_arr[num].buf = buf;
-	data->stem_arr[num].from_mmap = 0;
+	data->stem_arr[num].from_mmap = from_mmap;
 	data->num_stems++;
 
libselinux/src/label_file.h +1 -1
 	return num;
@@ -298,7 +297,7 @@ static inline int find_stem_from_spec(struct saved_data
 *data, const char *buf)
 	if (!stem)
 		return -1;
 
-	return store_stem(data, stem, stem_len);
+	return store_stem(data, stem, stem_len, false);
 }
 

/* This will always check for buffer over-runs and either read the next entry @@
-317,6 +316,22 @@ static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

libselinux/src/label_file.h +41 -20
 	return 0;
 }
 
+static inline int next_pstr(char **str, struct mmap_area *fp, size_t
+len) {
+	if (len > fp->next_len)
+		return -1;
+
+	char *tmp = (char *)fp->next_addr;
+	if (tmp[len-1] != '\0')
+		return -1;
+
+	*str = tmp;
+
+	fp->next_addr = (char *)fp->next_addr + len;
+	fp->next_len -= len;
+	return 0;
+}
+
 static inline int compile_regex(struct saved_data *data, struct spec *spec,
 					    const char **errbuf)
 {
@@ -375,13 +390,21 @@ static inline int process_line(struct selabel_handle *rec,
 			char *line_buf, unsigned lineno)
 {
 	int items, len, rc;
-	char *regex = NULL, *type = NULL, *context = NULL;
+	union {
+		struct {
+			char *regex;
+			char *type;
+			char *context;
+		};
+		char *array[3];
+	} found = { .array = { 0 } };
+
 	struct saved_data *data = (struct saved_data *)rec->data;
 	struct spec *spec_arr;
 	unsigned int nspec = data->nspec;
 	const char *errbuf = NULL;
 
-	items = read_spec_entries(line_buf, &errbuf, 3, &regex, &type,
 &context);
+	items = read_spec_entries(line_buf, &errbuf, ARRAY_SIZE(found.array),
+found.array);
 	if (items < 0) {
 		rc = errno;
 		selinux_log(SELINUX_ERROR,
@@ -398,22 +421,17 @@ static inline int process_line(struct selabel_handle *rec,
 		COMPAT_LOG(SELINUX_ERROR,
 			    "%s:  line %u is missing fields\n", path,
 			    lineno);
-		if (items == 1)
-			free(regex);
 		errno = EINVAL;
 		return -1;
 	} else if (items == 2) {
 		/* The type field is optional. */
-		context = type;
-		type = 0;
+		found.context = found.type;
+		found.type = NULL;
 	}
 
-	len = get_stem_from_spec(regex);
-	if (len && prefix && strncmp(prefix, regex, len)) {
+	len = get_stem_from_spec(found.regex);
+	if (len && prefix && strncmp(prefix, found.regex, len)) {
 		/* Stem of regex does not match requested prefix, discard. */
-		free(regex);
-		free(type);
-		free(context);
 		return 0;
 	}
 
@@ -424,13 +442,13 @@ static inline int process_line(struct selabel_handle *rec,
 	spec_arr = data->spec_arr;
 
 	/* process and store the specification in spec. */
-	spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
-	spec_arr[nspec].regex_str = regex;
+	spec_arr[nspec].stem_id = find_stem_from_spec(data, found.regex);
+	spec_arr[nspec].regex_str = found.regex;
 
-	spec_arr[nspec].type_str = type;
+	spec_arr[nspec].type_str = found.type;
 	spec_arr[nspec].mode = 0;
 
-	spec_arr[nspec].lr.ctx_raw = context;
+	spec_arr[nspec].lr.ctx_raw = found.context;
 
 	/*
 	 * bump data->nspecs to cause closef() to cover it in its free @@ -442,18
+460,18 @@ static inline int process_line(struct selabel_handle *rec,
 			&& compile_regex(data, &spec_arr[nspec], &errbuf)) {
 		COMPAT_LOG(SELINUX_ERROR,
 			   "%s:  line %u has invalid regex %s:  %s\n",
-			   path, lineno, regex, errbuf);
+			   path, lineno, found.regex, errbuf);
 		errno = EINVAL;
 		return -1;
 	}
 
-	if (type) {
-		mode_t mode = string_to_mode(type);
+	if (found.type) {
+		mode_t mode = string_to_mode(found.type);
 
 		if (mode == (mode_t)-1) {
 			COMPAT_LOG(SELINUX_ERROR,
 				   "%s:  line %u has invalid file type %s\n",
-				   path, lineno, type);
+				   path, lineno, found.type);
 			errno = EINVAL;
 			return -1;
 		}
@@ -464,7 +482,7 @@ static inline int process_line(struct selabel_handle *rec,
 	 * any meta characters in the RE */
 	spec_hasMetaChars(&spec_arr[nspec]);
 
-	if (strcmp(context, "<<none>>") && rec->validating)
+	if (strcmp(found.context, "<<none>>") && rec->validating)
 		compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
 	return 0;
libselinux/src/label_internal.h index renamed from libselinux/src/label_internal.h +0 -0

aa48fff..e40d68b 100644

libselinux/src/label_internal.h +3 -1
@@ -136,10 +136,11 @@ compat_validate(struct selabel_handle *rec,
 		struct selabel_lookup_rec *contexts,
 		const char *path, unsigned lineno) hidden;
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 /*
  * The read_spec_entries function may be used to
  * replace sscanf to read entries from spec files.
  */
-extern int read_spec_entries(char *line_buf, const char **errbuf, int num_args,
 ...);
+extern int hidden read_spec_entries(char *line_buf, const char
+**errbuf, unsigned int num, char *found[]);
 
 #endif				/* _SELABEL_INTERNAL_H_ */
libselinux/src/label_support.c index renamed from libselinux/src/label_support.c +0 -0

26f9ef1..3f5728d 100644

libselinux/src/label_support.c +24 -22
@@ -17,39 +17,40 @@
  * Read an entry from a spec file (e.g. file_contexts)
  * entry - Buffer to allocate for the entry.
  * ptr - current location of the line to be processed.
- * returns  - 0 on success and *entry is set to be a null
- *            terminated value. On Error it returns -1 and
- *            errno will be set.
+ * returns  - a pointer to the begining of the string.
+ *            that is the
  *
  */
-static inline int read_spec_entry(char **entry, char **ptr, int *len, const char
 **errbuf)
+static inline char *read_spec_entry(char **current, const char
+**errbuf)
 {
-	*entry = NULL;
-	char *tmp_buf = NULL;
-
-	while (isspace(**ptr) && **ptr != '\0')
-		(*ptr)++;
+	if(!**current) {
+		(*current)++;
+		return NULL;
+	}
 
-	tmp_buf = *ptr;
-	*len = 0;
+	while (isspace(**current) && **current != '\0')
+		(*current)++;
 
-	while (!isspace(**ptr) && **ptr != '\0') {
-		if (!isascii(**ptr)) {
+	char *start = *current;
+	while (!isspace(**current) && **current != '\0') {
+		if (!isascii(**current)) {
 			errno = EINVAL;
 			*errbuf = "Non-ASCII characters found";
-			return -1;
+			return NULL;
 		}
-		(*ptr)++;
-		(*len)++;
+		(*current)++;
 	}
 
-	if (*len) {
-		*entry = strndup(tmp_buf, *len);
-		if (!*entry)
-			return -1;
-	}
+	char *end = *current;
 
-	return 0;
+	if (start == end)
+		return NULL;
+
+	if (*end != '\0') {
+		*end = '\0';
+		(*current)++;
+	}
+	return start;
 }
 
 /*
@@ -63,15 +64,13 @@ static inline int read_spec_entry(char **entry, char **ptr,

int *len, const char
* This function calls read_spec_entry() to do the actual string processing.
* As such, can return anything from that function as well.
*/
-int hidden read_spec_entries(char *line_buf, const char **errbuf, int num_args, ...)

libselinux/src/label_support.c +10 -25
+int hidden read_spec_entries(char *line_buf, const char **errbuf,
+unsigned int num, char *found[])
 {
-	char **spec_entry, *buf_p;
-	int len, rc, items, entry_len = 0;
-	va_list ap;
-
+	char*buf_p;
+	unsigned int items = 0;
 	*errbuf = NULL;
 
-	len = strlen(line_buf);
+	size_t len = strlen(line_buf);
 	if (line_buf[len - 1] == '\n')
 		line_buf[len - 1] = '\0';
 	else
@@ -89,26 +88,12 @@ int hidden read_spec_entries(char *line_buf, const char
 **errbuf, int num_args,
 		return 0;
 
 	/* Process the spec file entries */
-	va_start(ap, num_args);
-
-	items = 0;
-	while (items < num_args) {
-		spec_entry = va_arg(ap, char **);
-
-		if (len - 1 == buf_p - line_buf) {
-			va_end(ap);
-			return items;
-		}
-
-		rc = read_spec_entry(spec_entry, &buf_p, &entry_len, errbuf);
-		if (rc < 0) {
-			va_end(ap);
-			return rc;
-		}
-		if (entry_len)
-			items++;
+	while (items < num) {
+		found[items] = read_spec_entry(&buf_p, errbuf);
+		if (!found[items])
+			break;
+		items++;
 	}
-	va_end(ap);
 	return items;
 }
 
--

1.9.1

#3 lore raw
Quoted 9 lines

-----Original Message-----
From: Stephen Smalley [mailto:[email protected]]
Sent: Tuesday, September 20, 2016 6:18 AM
To: Roberts, William C <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC] mmap file_contexts and property_contexts:

Quoted 5 lines
Quoted 1 line · William C

FYI I only tested this with checkfc...

Evidently. matchpathcon and sefcontext_compile both report calls to free() on invalid pointers and abort.

That doesn’t surprise me, I only tested the checkfc usages. Hence #4 in todo's

Quoted 701 lines
Quoted 10 lines
Quoted 10 lines

-----Original Message-----
From: Roberts, William C
Sent: Monday, September 19, 2016 2:45 PM
To: [email protected]; [email protected]; [email protected]; [email protected]
Cc: Roberts, William C <[email protected]>
Subject: [RFC] mmap file_contexts and property_contexts:

Quoted 8 lines
Quoted 8 lines

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android

doesn't use monstrous regexs.

Quoted 417 lines
Quoted 417 lines

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140 +++++++++++++++++++-------------
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label.c index renamed from libselinux/src/label.c +0 -0

963bfcb..d767b49
100644

libselinux/src/label.c +0 -2
@@ -15,8 +15,6 @@
 #include "callbacks.h"
 #include "label_internal.h"
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 typedef int (*selabel_initfunc)(struct selabel_handle *rec,
 				const struct selinux_opt *opts,
 				unsigned nopts);
libselinux/src/label_android_property.c +10 -7
index 290b438..2aac394 100644
@@ -85,13 +85,19 @@ static int process_line(struct selabel_handle *rec,
 			int pass, unsigned lineno)
 {
 	int items;
-	char *prop = NULL, *context = NULL;
+	union {
+		struct {
+			char *prop;
+			char *context;
+		};
+		char *array[2];
+	} found = { .array = { 0 } };
 	struct saved_data *data = (struct saved_data *)rec->data;
 	spec_t *spec_arr = data->spec_arr;
 	unsigned int nspec = data->nspec;
 	const char *errbuf = NULL;
 
-	items = read_spec_entries(line_buf, &errbuf, 2, &prop, &context);
+	items = read_spec_entries(line_buf, &errbuf,
+ARRAY_SIZE(found.array), found.array);
 	if (items < 0) {
 		items = errno;
 		selinux_log(SELINUX_ERROR,
@@ -108,18 +114,14 @@ static int process_line(struct selabel_handle *rec,
 		selinux_log(SELINUX_ERROR,
 			    "%s:  line %u is missing fields\n", path,
 			    lineno);
-		free(prop);
 		errno = EINVAL;
 		return -1;
 	}
 
-	if (pass == 0) {
-		free(prop);
-		free(context);
-	} else if (pass == 1) {
+	if (pass == 1) {
 		/* On the second pass, process and store the specification in

spec. */

libselinux/src/label_android_property.c +2 -2
-		spec_arr[nspec].property_key = prop;
-		spec_arr[nspec].lr.ctx_raw = context;
+		spec_arr[nspec].property_key = found.prop;
+		spec_arr[nspec].lr.ctx_raw = found.context;
 
 		if (rec->validating) {
 			if (selabel_validate(rec, &spec_arr[nspec].lr) < 0) { @@ -

234,7 +236,7 @@ static void closef(struct selabel_handle *rec)

libselinux/src/label_android_property.c +1 -1
 	for (i = 0; i < data->nspec; i++) {
 		spec = &data->spec_arr[i];
 		free(spec->property_key);
-		free(spec->lr.ctx_raw);
+		//free(spec->lr.ctx_raw);
 		free(spec->lr.ctx_trans);
 	}
(quoted diff) +0 -0

b/libselinux/src/label_file.c index 7156825..4dc440e 100644

libselinux/src/label_file.c +31 -4
@@ -96,43 +96,64 @@ static int nodups_specs(struct saved_data *data,
 const char *path)
 	return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-			     struct selabel_handle *rec, const char *path)
+static inline struct saved_data *rec_to_data(struct selabel_handle
+*rec) {
+	return (struct saved_data *)rec->data; }
+
+static char *mmap_area_get_line(struct mmap_area *area) {
+	size_t len = area->next_len;
+	if (!len)
+		return NULL;
+
+	char *start = area->next_addr;
+	char *end = memchr(start, '\n', len);
+
+	/* the file may not end with a newline */
+	if (!end)
+		end = (char *)area->next_addr + len - 1;
+
+	*end = '\0';
+	/* len includes null byte */
+	len = end - start;
+
+	area->next_len -= len + 1;
+	area->next_addr = ++end;
+	return start;
+}
+
+static int process_text_file(const char *path, const char *prefix,
+		struct selabel_handle *rec)
 {
 	int rc;
-	size_t line_len;
 	unsigned int lineno = 0;
-	char *line_buf = NULL;
+	char *line_buf;
+	struct mmap_area *areas = rec_to_data(rec)->mmap_areas;
+
+	/* mmap_area_get_line() and process_line() require mutable string

pointers */

libselinux/src/label_file.c +24 -22
+	rc = mprotect(areas->addr, areas->len, PROT_READ | PROT_WRITE);
+	if (rc < 0)
+		goto out;
 
-	while (getline(&line_buf, &line_len, fp) > 0) {
+	while ( (line_buf = mmap_area_get_line(areas)) ) {
 		rc = process_line(rec, path, prefix, line_buf, ++lineno);
 		if (rc)
 			goto out;
 	}
 	rc = 0;
 out:
-	free(line_buf);
 	return rc;
 }
 
-static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
-		     const char *path)
+static int mmap_file(FILE *fp, size_t len, struct saved_data *data)
 {
-	struct saved_data *data = (struct saved_data *)rec->data;
-	int rc;
-	char *addr, *str_buf;
-	int *stem_map;
-	struct mmap_area *mmap_area;
-	uint32_t i, magic, version;
-	uint32_t entry_len, stem_map_len, regex_array_len;
-	const char *reg_version;
-
-	mmap_area = malloc(sizeof(*mmap_area));
+	struct mmap_area *mmap_area = malloc(sizeof(*mmap_area));
 	if (!mmap_area) {
 		return -1;
 	}
 
-	addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
+	void *addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp),
+0);
 	if (addr == MAP_FAILED) {
 		free(mmap_area);
 		perror("mmap");
@@ -145,17 +166,26 @@ static int load_mmap(FILE *fp, size_t len,
 struct selabel_handle *rec,
 	mmap_area->next = data->mmap_areas;
 	data->mmap_areas = mmap_area;
 
-	/* check if this looks like an fcontext file */
-	rc = next_entry(&magic, mmap_area, sizeof(uint32_t));
-	if (rc < 0 || magic != SELINUX_MAGIC_COMPILED_FCONTEXT)
-		return -1;
+	return 0;
+}
+
+static int process_binary_file(const char *path, struct
+selabel_handle
+*rec) {
+	struct saved_data *data = (struct saved_data *)rec->data;
+	int rc;
+	char *str_buf;
+	int *stem_map;
+	struct mmap_area *mmap_area = data->mmap_areas;
+	uint32_t i, version;
+	uint32_t entry_len, stem_map_len, regex_array_len;
+	size_t len;
 
 	/* check if this version is higher than we understand */
 	rc = next_entry(&version, mmap_area, sizeof(uint32_t));
 	if (rc < 0 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
 		return -1;
 
-	reg_version = regex_version();
+	const char *reg_version = regex_version();
 	if (!reg_version)
 		return -1;
@@ -235,12 +265,11 @@ static int load_mmap(FILE *fp, size_t len,
 struct selabel_handle *rec,
 		/* store the mapping between old and new */
 		newid = find_stem(data, buf, stem_len);
 		if (newid < 0) {
-			newid = store_stem(data, buf, stem_len);
+			newid = store_stem(data, buf, stem_len, true);
 			if (newid < 0) {
 				rc = newid;
 				goto out;
 			}
-			data->stem_arr[newid].from_mmap = 1;
 		}
 		stem_map[i] = newid;
libselinux/src/label_file.c +0 -1
 	}
@@ -262,7 +291,6 @@ static int load_mmap(FILE *fp, size_t len, struct
 selabel_handle *rec,
 			goto out;
 
 		spec = &data->spec_arr[data->nspec];
-		spec->from_mmap = 1;
 
 		/* Process context */

rc = next_entry(&entry_len, mmap_area, sizeof(uint32_t)); @@ -
271,22 +299,10 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

libselinux/src/label_file.c +19 -19
 			goto out;
 		}
 
-		str_buf = malloc(entry_len);
-		if (!str_buf) {
-			rc = -1;
-			goto out;
-		}
-		rc = next_entry(str_buf, mmap_area, entry_len);
+		rc = next_pstr(&spec->lr.ctx_raw, mmap_area, entry_len);
 		if (rc < 0)
 			goto out;
 
-		if (str_buf[entry_len - 1] != '\0') {
-			free(str_buf);
-			rc = -1;
-			goto out;
-		}
-		spec->lr.ctx_raw = str_buf;
-
 		if (strcmp(spec->lr.ctx_raw, "<<none>>") && rec->validating) {
 			if (selabel_validate(rec, &spec->lr) < 0) {
 				selinux_log(SELINUX_ERROR,
@@ -401,17 +417,29 @@ static char *rolling_append(char *current,
 const char *suffix, size_t max)
 	return current;
 }
 
-static bool fcontext_is_binary(FILE *fp)
+static inline int mmap_area_rewind(struct mmap_area *area, size_t
+bytes) {
+	if (area->next_len + bytes > area->len)
+		return -1;
+
+	area->next_addr = (char *) area->next_addr - bytes;
+	area->next_len += bytes;
+	return 0;
+}
+
+static bool fcontext_is_binary(struct mmap_area *area)
 {
 	uint32_t magic;
+	bool is_binary = false;
 
-	size_t len = fread(&magic, sizeof(magic), 1, fp);
-	rewind(fp);
+	int rc = next_entry(&magic, area, sizeof(magic));
 
-	return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
-}
+	is_binary = (!rc && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
+	if (!is_binary)
+		mmap_area_rewind(area, sizeof(magic));
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+	return is_binary;
+}
 
 static FILE *open_file(const char *path, const char *suffix,

char *save_path, size_t len, struct stat *sb, bool
open_oldest) @@ -
504,12 +532,17 @@ static int process_file(const char *path, const char *suffix,

libselinux/src/label_file.c +10 -5
 		if (fp == NULL)
 			return -1;
 
-		rc = fcontext_is_binary(fp) ?
-				load_mmap(fp, sb.st_size, rec, found_path) :
-				process_text_file(fp, prefix, rec, found_path);
+		rc = mmap_file(fp, sb.st_size, rec_to_data(rec));
+		if (rc < 0) {
+			fclose(fp);
+			return -1;
+		}
+
+		rc = fcontext_is_binary(rec_to_data(rec)->mmap_areas) ?
+				process_binary_file(found_path, rec) :
+				process_text_file(found_path, prefix, rec);
 		if (!rc)
-			rc = digest_add_specfile(digest, fp, NULL, sb.st_size,
-				found_path);
+			rc = digest_add_specfile(digest, fp, NULL, sb.st_size,

found_path);

libselinux/src/label_file.c +0 -5
 		fclose(fp);
@@ -613,12 +646,7 @@ static void closef(struct selabel_handle *rec)
 	for (i = 0; i < data->nspec; i++) {
 		spec = &data->spec_arr[i];
 		free(spec->lr.ctx_trans);
-		free(spec->lr.ctx_raw);
 		regex_data_free(spec->regex);
-		if (spec->from_mmap)
-			continue;
-		free(spec->regex_str);
-		free(spec->type_str);
 	}
 
 	for (i = 0; i < (unsigned int)data->num_stems; i++) { diff --git

a/libselinux/src/label_file.h b/libselinux/src/label_file.h index 88f4294..2704906
100644

libselinux/src/label_file.h +3 -2
@@ -37,7 +37,6 @@ struct spec {
 	int matches;		/* number of matching pathnames */
 	int stem_id;		/* indicates which stem-compression item */
 	char hasMetaChars;	/* regular expression has meta-chars */
-	char from_mmap;		/* this spec is from an mmap of the data
 */
 	size_t prefix_len;      /* length of fixed path prefix */
 };
@@ -255,7 +254,7 @@ static inline int find_stem(struct saved_data
 *data, const char *buf,  }
 
 /* returns the index of the new stored object */ -static inline int
 store_stem(struct saved_data *data, char *buf, int stem_len)
+static inline int store_stem(struct saved_data *data, char *buf, int
+stem_len, bool from_mmap)
 {
 	int num = data->num_stems;
@@ -271,7 +270,7 @@ static inline int store_stem(struct saved_data
 *data, char *buf, int stem_len)
 	}
 	data->stem_arr[num].len = stem_len;
 	data->stem_arr[num].buf = buf;
-	data->stem_arr[num].from_mmap = 0;
+	data->stem_arr[num].from_mmap = from_mmap;
 	data->num_stems++;
 
libselinux/src/label_file.h +1 -1
 	return num;
@@ -298,7 +297,7 @@ static inline int find_stem_from_spec(struct
 saved_data *data, const char *buf)
 	if (!stem)
 		return -1;
 
-	return store_stem(data, stem, stem_len);
+	return store_stem(data, stem, stem_len, false);
 }
 

/* This will always check for buffer over-runs and either read the
next entry @@
-317,6 +316,22 @@ static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

libselinux/src/label_file.h +12 -0
 	return 0;
 }
 
+static inline int next_pstr(char **str, struct mmap_area *fp, size_t
+len) {
+	if (len > fp->next_len)
+		return -1;
+
+	char *tmp = (char *)fp->next_addr;
+	if (tmp[len-1] != '\0')
+		return -1;
+
+	*str = tmp;
+
+	fp->next_addr = (char *)fp->next_addr + len;
Quoted 8 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle

Quoted 8 lines
libselinux/src/label_file.h +4 -0
+	fp->next_len -= len;
+	return 0;
+}
+
 static inline int compile_regex(struct saved_data *data, struct spec *spec,
 					    const char **errbuf)
 {
@@ -375,13 +390,21 @@ static inline int process_line(struct selabel_handle

*rec,

Quoted 18 lines
Quoted 18 lines
libselinux/src/label_file.h +9 -1
 			char *line_buf, unsigned lineno)
 {
 	int items, len, rc;
-	char *regex = NULL, *type = NULL, *context = NULL;
+	union {
+		struct {
+			char *regex;
+			char *type;
+			char *context;
+		};
+		char *array[3];
+	} found = { .array = { 0 } };
+
 	struct saved_data *data = (struct saved_data *)rec->data;
 	struct spec *spec_arr;
 	unsigned int nspec = data->nspec;
 	const char *errbuf = NULL;
 
Quoted 8 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle

Quoted 8 lines
libselinux/src/label_file.h +2 -1
-	items = read_spec_entries(line_buf, &errbuf, 3, &regex, &type,
 &context);
+	items = read_spec_entries(line_buf, &errbuf,
+ARRAY_SIZE(found.array), found.array);
 	if (items < 0) {
 		rc = errno;
 		selinux_log(SELINUX_ERROR,
@@ -398,22 +421,17 @@ static inline int process_line(struct selabel_handle

*rec,

Quoted 26 lines
Quoted 26 lines
libselinux/src/label_file.h +4 -9
 		COMPAT_LOG(SELINUX_ERROR,
 			    "%s:  line %u is missing fields\n", path,
 			    lineno);
-		if (items == 1)
-			free(regex);
 		errno = EINVAL;
 		return -1;
 	} else if (items == 2) {
 		/* The type field is optional. */
-		context = type;
-		type = 0;
+		found.context = found.type;
+		found.type = NULL;
 	}
 
-	len = get_stem_from_spec(regex);
-	if (len && prefix && strncmp(prefix, regex, len)) {
+	len = get_stem_from_spec(found.regex);
+	if (len && prefix && strncmp(prefix, found.regex, len)) {
 		/* Stem of regex does not match requested prefix, discard. */
-		free(regex);
-		free(type);
-		free(context);
 		return 0;
 	}
 
Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct selabel_handle

Quoted 1 line
libselinux/src/label_file.h +0 -0
@@ -424,13 +442,13 @@ static inline int process_line(struct selabel_handle

*rec,

Quoted 33 lines
Quoted 33 lines
libselinux/src/label_file.h +8 -8
 	spec_arr = data->spec_arr;
 
 	/* process and store the specification in spec. */
-	spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
-	spec_arr[nspec].regex_str = regex;
+	spec_arr[nspec].stem_id = find_stem_from_spec(data, found.regex);
+	spec_arr[nspec].regex_str = found.regex;
 
-	spec_arr[nspec].type_str = type;
+	spec_arr[nspec].type_str = found.type;
 	spec_arr[nspec].mode = 0;
 
-	spec_arr[nspec].lr.ctx_raw = context;
+	spec_arr[nspec].lr.ctx_raw = found.context;
 
 	/*
 	 * bump data->nspecs to cause closef() to cover it in its free @@
-442,18
+460,18 @@ static inline int process_line(struct selabel_handle *rec,
 			&& compile_regex(data, &spec_arr[nspec], &errbuf)) {
 		COMPAT_LOG(SELINUX_ERROR,
 			   "%s:  line %u has invalid regex %s:  %s\n",
-			   path, lineno, regex, errbuf);
+			   path, lineno, found.regex, errbuf);
 		errno = EINVAL;
 		return -1;
 	}
 
-	if (type) {
-		mode_t mode = string_to_mode(type);
+	if (found.type) {
+		mode_t mode = string_to_mode(found.type);
Quoted 9 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle

Quoted 9 lines
libselinux/src/label_file.h +1 -1
 		if (mode == (mode_t)-1) {
 			COMPAT_LOG(SELINUX_ERROR,
 				   "%s:  line %u has invalid file type %s\n",
-				   path, lineno, type);
+				   path, lineno, found.type);
 			errno = EINVAL;
 			return -1;
 		}
@@ -464,7 +482,7 @@ static inline int process_line(struct selabel_handle

*rec,

Quoted 158 lines
Quoted 156 lines
libselinux/src/label_file.h +1 -1
 	 * any meta characters in the RE */
 	spec_hasMetaChars(&spec_arr[nspec]);
 
-	if (strcmp(context, "<<none>>") && rec->validating)
+	if (strcmp(found.context, "<<none>>") && rec->validating)
 		compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
 	return 0;
(quoted diff) +0 -0

b/libselinux/src/label_internal.h index aa48fff..e40d68b 100644

libselinux/src/label_internal.h +3 -1
@@ -136,10 +136,11 @@ compat_validate(struct selabel_handle *rec,
 		struct selabel_lookup_rec *contexts,
 		const char *path, unsigned lineno) hidden;
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 /*
  * The read_spec_entries function may be used to
  * replace sscanf to read entries from spec files.
  */
-extern int read_spec_entries(char *line_buf, const char **errbuf,
 int num_args, ...);
+extern int hidden read_spec_entries(char *line_buf, const char
+**errbuf, unsigned int num, char *found[]);
 
 #endif				/* _SELABEL_INTERNAL_H_ */
(quoted diff) +0 -0

b/libselinux/src/label_support.c index 26f9ef1..3f5728d 100644

libselinux/src/label_support.c +24 -22
@@ -17,39 +17,40 @@
  * Read an entry from a spec file (e.g. file_contexts)
  * entry - Buffer to allocate for the entry.
  * ptr - current location of the line to be processed.
- * returns  - 0 on success and *entry is set to be a null
- *            terminated value. On Error it returns -1 and
- *            errno will be set.
+ * returns  - a pointer to the begining of the string.
+ *            that is the
  *
  */
-static inline int read_spec_entry(char **entry, char **ptr, int
 *len, const char
 **errbuf)
+static inline char *read_spec_entry(char **current, const char
+**errbuf)
 {
-	*entry = NULL;
-	char *tmp_buf = NULL;
-
-	while (isspace(**ptr) && **ptr != '\0')
-		(*ptr)++;
+	if(!**current) {
+		(*current)++;
+		return NULL;
+	}
 
-	tmp_buf = *ptr;
-	*len = 0;
+	while (isspace(**current) && **current != '\0')
+		(*current)++;
 
-	while (!isspace(**ptr) && **ptr != '\0') {
-		if (!isascii(**ptr)) {
+	char *start = *current;
+	while (!isspace(**current) && **current != '\0') {
+		if (!isascii(**current)) {
 			errno = EINVAL;
 			*errbuf = "Non-ASCII characters found";
-			return -1;
+			return NULL;
 		}
-		(*ptr)++;
-		(*len)++;
+		(*current)++;
 	}
 
-	if (*len) {
-		*entry = strndup(tmp_buf, *len);
-		if (!*entry)
-			return -1;
-	}
+	char *end = *current;
 
-	return 0;
+	if (start == end)
+		return NULL;
+
+	if (*end != '\0') {
+		*end = '\0';
+		(*current)++;
+	}
+	return start;
 }
 
 /*
@@ -63,15 +64,13 @@ static inline int read_spec_entry(char **entry,

char **ptr, int *len, const char
* This function calls read_spec_entry() to do the actual string processing.
* As such, can return anything from that function as well.
*/
-int hidden read_spec_entries(char *line_buf, const char **errbuf, int num_args,
...)

libselinux/src/label_support.c +10 -25
+int hidden read_spec_entries(char *line_buf, const char **errbuf,
+unsigned int num, char *found[])
 {
-	char **spec_entry, *buf_p;
-	int len, rc, items, entry_len = 0;
-	va_list ap;
-
+	char*buf_p;
+	unsigned int items = 0;
 	*errbuf = NULL;
 
-	len = strlen(line_buf);
+	size_t len = strlen(line_buf);
 	if (line_buf[len - 1] == '\n')
 		line_buf[len - 1] = '\0';
 	else
@@ -89,26 +88,12 @@ int hidden read_spec_entries(char *line_buf,
 const char **errbuf, int num_args,
 		return 0;
 
 	/* Process the spec file entries */
-	va_start(ap, num_args);
-
-	items = 0;
-	while (items < num_args) {
-		spec_entry = va_arg(ap, char **);
-
-		if (len - 1 == buf_p - line_buf) {
-			va_end(ap);
-			return items;
-		}
-
-		rc = read_spec_entry(spec_entry, &buf_p, &entry_len, errbuf);
-		if (rc < 0) {
-			va_end(ap);
-			return rc;
-		}
-		if (entry_len)
-			items++;
+	while (items < num) {
+		found[items] = read_spec_entry(&buf_p, errbuf);
+		if (!found[items])
+			break;
+		items++;
 	}
-	va_end(ap);
 	return items;
 }
 
--

1.9.1

#4 lore raw
In reply to Stephen Smalley
Quoted 8 lines
Quoted 5 lines · William C
Quoted 1 line

FYI I only tested this with checkfc...

Evidently. matchpathcon and sefcontext_compile both report calls to free() on invalid pointers and abort.

That doesn’t surprise me, I only tested the checkfc usages. Hence #4 in todo's

I looked at the sefcontext_compile bug, and its yet another conglomeration where internal Interfaces into libselinux are presumed and duplicate code exists (for loading a file). It implements its own routine for the freeing of Spec data. Why doesn't it use proper libselinux interfaces, or libselinux expose the proper Interfaces for this type of work? Is this just an example of something that should be fixed or is there some deeper reasoning to its construction?

I could not get matchpatchcon to reproduce (built with ASAN):
./selinux/libselinux/utils/matchpathcon /etc -f file_contexts
/etc u:object_r:rootfs:s0

Bill

#5 lore raw
In reply to Roberts, William C

<snip>

Quoted 10 lines
Quoted 10 lines

-----Original Message-----
From: Roberts, William C
Sent: Monday, September 19, 2016 2:45 PM
To: [email protected]; [email protected]; [email protected]; [email protected]
Cc: Roberts, William C <[email protected]>
Subject: [RFC] mmap file_contexts and property_contexts:

From: William Roberts <[email protected]>

Quoted 21 lines
Quoted 6 lines

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't

use monstrous regexs.

Quoted 14 lines

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

Another potential issue, is that mmaping with textfiles also has the sideffect of mapping in all the spaces or tabs, etc used, between fields, so on files with heavy whitespace, it could be really memory intensive.

I think Janis's patch that has -r option is a more useful way to go. Without precompiled Regex's its essentially a textfile without the whitespace, and it automatically would get pulled into the mmap interface.

I'm abandoning this, since we already get this functionality, without the negatives. Janis, IIRC, you are re-configuring your patch so -r omits pre-compiled regex's (submitted I believe on the list and awaiting other patches) and adding an arch string so we can Detect when we need to recompile the regular expressions if they are supplied but unusable.

I think we may want to investigate to have the "omit pre-compiled regex's" work on PCRE library variants as well, but I doubt it would be used since Android is moving to PCRE2.

<snip>

#6 lore raw
In reply to [email protected]
Quoted 19 lines · On 20 Sep 2016 5:47 am, <[email protected]> wrote:

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

Quoted 5 lines

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

Do you have the scripts that generated these stats so I can play with it too? These stats are from android right? Do you also have a comparison for refpolicy too?

I haven't looked that closely yet but just realised, will this need new perms because of the mmap? If it does, can you send a patch to refpolicy?

-- Jason

Quoted 9 lines

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

Quoted 5 lines
---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140

+++++++++++++++++++-------------

Review

Quoted 19 lines
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label.c +0 -2
index 963bfcb..d767b49 100644
@@ -15,8 +15,6 @@
 #include "callbacks.h"
 #include "label_internal.h"
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 typedef int (*selabel_initfunc)(struct selabel_handle *rec,
                                const struct selinux_opt *opts,
                                unsigned nopts);
(quoted diff) +0 -0

b/libselinux/src/label_android_property.c

Quoted 12 lines

index 290b438..2aac394 100644

libselinux/src/label_android_property.c +4 -1
@@ -85,13 +85,19 @@ static int process_line(struct selabel_handle *rec,
                        int pass, unsigned lineno)
 {
        int items;
-       char *prop = NULL, *context = NULL;
+       union {
+               struct {
+                       char *prop;
+                       char *context;

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec, · line 100

Quoted 10 lines
libselinux/src/label_android_property.c +4 -1
+               };
+               char *array[2];
+       } found = { .array = { 0 } };
        struct saved_data *data = (struct saved_data *)rec->data;
        spec_t *spec_arr = data->spec_arr;
        unsigned int nspec = data->nspec;
        const char *errbuf = NULL;
 
-       items = read_spec_entries(line_buf, &errbuf, 2, &prop, &context);
+       items = read_spec_entries(line_buf, &errbuf,

ARRAY_SIZE(found.array), found.array);

Quoted 8 lines
libselinux/src/label_android_property.c +0 -1
        if (items < 0) {
                items = errno;
                selinux_log(SELINUX_ERROR,
@@ -108,18 +114,14 @@ static int process_line(struct selabel_handle *rec,
                selinux_log(SELINUX_ERROR,
                            "%s:  line %u is missing fields\n", path,
                            lineno);
-               free(prop);

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec, · line 122

Quoted 10 lines
libselinux/src/label_android_property.c +1 -4
                errno = EINVAL;
                return -1;
        }
 
-       if (pass == 0) {
-               free(prop);
-               free(context);
-       } else if (pass == 1) {
+       if (pass == 1) {
                /* On the second pass, process and store the

specification in spec. */

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec,

Quoted 7 lines
libselinux/src/label_android_property.c +2 -2
-               spec_arr[nspec].property_key = prop;
-               spec_arr[nspec].lr.ctx_raw = context;
+               spec_arr[nspec].property_key = found.prop;
+               spec_arr[nspec].lr.ctx_raw = found.context;
 
                if (rec->validating) {
                        if (selabel_validate(rec, &spec_arr[nspec].lr) <

0) {

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data,

Quoted 14 lines
libselinux/src/label_android_property.c +1 -1
@@ -234,7 +236,7 @@ static void closef(struct selabel_handle *rec)
        for (i = 0; i < data->nspec; i++) {
                spec = &data->spec_arr[i];
                free(spec->property_key);
-               free(spec->lr.ctx_raw);
+               //free(spec->lr.ctx_raw);
                free(spec->lr.ctx_trans);
        }
 
libselinux/src/label_file.c +0 -0
index 7156825..4dc440e 100644
@@ -96,43 +96,64 @@ static int nodups_specs(struct saved_data *data,

const char *path)

Quoted 33 lines
libselinux/src/label_file.c +28 -2
        return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-                            struct selabel_handle *rec, const char *path)
+static inline struct saved_data *rec_to_data(struct selabel_handle *rec)
+{
+       return (struct saved_data *)rec->data;
+}
+
+static char *mmap_area_get_line(struct mmap_area *area)
+{
+       size_t len = area->next_len;
+       if (!len)
+               return NULL;
+
+       char *start = area->next_addr;
+       char *end = memchr(start, '\n', len);
+
+       /* the file may not end with a newline */
+       if (!end)
+               end = (char *)area->next_addr + len - 1;
+
+       *end = '\0';
+       /* len includes null byte */
+       len = end - start;
+
+       area->next_len -= len + 1;
+       area->next_addr = ++end;
+       return start;
+}
+
+static int process_text_file(const char *path, const char *prefix,

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 10 lines
libselinux/src/label_file.c +5 -2
+               struct selabel_handle *rec)
 {
        int rc;
-       size_t line_len;
        unsigned int lineno = 0;
-       char *line_buf = NULL;
+       char *line_buf;
+       struct mmap_area *areas = rec_to_data(rec)->mmap_areas;
+
+       /* mmap_area_get_line() and process_line() require mutable string

pointers */

Quoted 27 lines
libselinux/src/label_file.c +5 -11
+       rc = mprotect(areas->addr, areas->len, PROT_READ | PROT_WRITE);
+       if (rc < 0)
+               goto out;
 
-       while (getline(&line_buf, &line_len, fp) > 0) {
+       while ( (line_buf = mmap_area_get_line(areas)) ) {
                rc = process_line(rec, path, prefix, line_buf, ++lineno);
                if (rc)
                        goto out;
        }
        rc = 0;
 out:
-       free(line_buf);
        return rc;
 }
 
-static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
-                    const char *path)
+static int mmap_file(FILE *fp, size_t len, struct saved_data *data)
 {
-       struct saved_data *data = (struct saved_data *)rec->data;
-       int rc;
-       char *addr, *str_buf;
-       int *stem_map;
-       struct mmap_area *mmap_area;
-       uint32_t i, magic, version;
-       uint32_t entry_len, stem_map_len, regex_array_len;

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 10 lines
libselinux/src/label_file.c +2 -4
-       const char *reg_version;
-
-       mmap_area = malloc(sizeof(*mmap_area));
+       struct mmap_area *mmap_area = malloc(sizeof(*mmap_area));
        if (!mmap_area) {
                return -1;
        }
 
-       addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
+       void *addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp),

0);

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 4 lines
libselinux/src/label_file.c +0 -0
        if (addr == MAP_FAILED) {
                free(mmap_area);
                perror("mmap");
@@ -145,17 +166,26 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 11 lines
libselinux/src/label_file.c +4 -4
        mmap_area->next = data->mmap_areas;
        data->mmap_areas = mmap_area;
 
-       /* check if this looks like an fcontext file */
-       rc = next_entry(&magic, mmap_area, sizeof(uint32_t));
-       if (rc < 0 || magic != SELINUX_MAGIC_COMPILED_FCONTEXT)
-               return -1;
+       return 0;
+}
+
+static int process_binary_file(const char *path, struct selabel_handle

*rec)

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 21 lines
libselinux/src/label_file.c +10 -1
+{
+       struct saved_data *data = (struct saved_data *)rec->data;
+       int rc;
+       char *str_buf;
+       int *stem_map;
+       struct mmap_area *mmap_area = data->mmap_areas;
+       uint32_t i, version;
+       uint32_t entry_len, stem_map_len, regex_array_len;
+       size_t len;
 
        /* check if this version is higher than we understand */
        rc = next_entry(&version, mmap_area, sizeof(uint32_t));
        if (rc < 0 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
                return -1;
 
-       reg_version = regex_version();
+       const char *reg_version = regex_version();
        if (!reg_version)
                return -1;
 
@@ -235,12 +265,11 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 14 lines
libselinux/src/label_file.c +1 -2
                /* store the mapping between old and new */
                newid = find_stem(data, buf, stem_len);
                if (newid < 0) {
-                       newid = store_stem(data, buf, stem_len);
+                       newid = store_stem(data, buf, stem_len, true);
                        if (newid < 0) {
                                rc = newid;
                                goto out;
                        }
-                       data->stem_arr[newid].from_mmap = 1;
                }
                stem_map[i] = newid;
        }
@@ -262,7 +291,6 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 8 lines
libselinux/src/label_file.c +0 -1
                        goto out;
 
                spec = &data->spec_arr[data->nspec];
-               spec->from_mmap = 1;
 
                /* Process context */
                rc = next_entry(&entry_len, mmap_area, sizeof(uint32_t));
@@ -271,22 +299,10 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

Quoted 11 lines
libselinux/src/label_file.c +1 -6
                        goto out;
                }
 
-               str_buf = malloc(entry_len);
-               if (!str_buf) {
-                       rc = -1;
-                       goto out;
-               }
-               rc = next_entry(str_buf, mmap_area, entry_len);
+               rc = next_pstr(&spec->lr.ctx_raw, mmap_area, entry_len);
                if (rc < 0)

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 10 lines
libselinux/src/label_file.c +0 -7
                        goto out;
 
-               if (str_buf[entry_len - 1] != '\0') {
-                       free(str_buf);
-                       rc = -1;
-                       goto out;
-               }
-               spec->lr.ctx_raw = str_buf;
-
                if (strcmp(spec->lr.ctx_raw, "<<none>>") &&

rec->validating) {

libselinux/src/label_file.c static char *rolling_append(char *current, const

Quoted 3 lines
libselinux/src/label_file.c +0 -0
                        if (selabel_validate(rec, &spec->lr) < 0) {
                                selinux_log(SELINUX_ERROR,
@@ -401,17 +417,29 @@ static char *rolling_append(char *current, const

char *suffix, size_t max)

Quoted 25 lines
libselinux/src/label_file.c +13 -5
        return current;
 }
 
-static bool fcontext_is_binary(FILE *fp)
+static inline int mmap_area_rewind(struct mmap_area *area, size_t bytes)
+{
+       if (area->next_len + bytes > area->len)
+               return -1;
+
+       area->next_addr = (char *) area->next_addr - bytes;
+       area->next_len += bytes;
+       return 0;
+}
+
+static bool fcontext_is_binary(struct mmap_area *area)
 {
        uint32_t magic;
+       bool is_binary = false;
 
-       size_t len = fread(&magic, sizeof(magic), 1, fp);
-       rewind(fp);
+       int rc = next_entry(&magic, area, sizeof(magic));
 
-       return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
-}

libselinux/src/label_file.c static char *rolling_append(char *current, const char *suffix, size_t max)

Quoted 10 lines
libselinux/src/label_file.c +5 -1
+       is_binary = (!rc && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
+       if (!is_binary)
+               mmap_area_rewind(area, sizeof(magic));
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+       return is_binary;
+}
 
 static FILE *open_file(const char *path, const char *suffix,
               char *save_path, size_t len, struct stat *sb, bool

open_oldest)

libselinux/src/label_file.c static int process_file(const char *path, const

Quoted 1 line
libselinux/src/label_file.c +0 -0
@@ -504,12 +532,17 @@ static int process_file(const char *path, const

char *suffix,

libselinux/src/label_file.c static int process_file(const char *path, const char *suffix,

Quoted 5 lines
libselinux/src/label_file.c +0 -2
                if (fp == NULL)
                        return -1;
 
-               rc = fcontext_is_binary(fp) ?
-                               load_mmap(fp, sb.st_size, rec,

found_path) :

libselinux/src/label_file.c static int process_file(const char *path, const · line 509

Quoted 1 line
libselinux/src/label_file.c +0 -1
-                               process_text_file(fp, prefix, rec,

found_path);

libselinux/src/label_file.c static int process_file(const char *path, const char *suffix,

Quoted 9 lines
libselinux/src/label_file.c +9 -0
+               rc = mmap_file(fp, sb.st_size, rec_to_data(rec));
+               if (rc < 0) {
+                       fclose(fp);
+                       return -1;
+               }
+
+               rc = fcontext_is_binary(rec_to_data(rec)->mmap_areas) ?
+                               process_binary_file(found_path, rec) :
+                               process_text_file(found_path, prefix,

rec);

libselinux/src/label_file.c static int process_file(const char *path, const · line 511

Quoted 2 lines
libselinux/src/label_file.c +0 -1
                if (!rc)
-                       rc = digest_add_specfile(digest, fp, NULL,

sb.st_size,

libselinux/src/label_file.c static int process_file(const char *path, const · line 545

Quoted 2 lines
libselinux/src/label_file.c +1 -1
-                               found_path);
+                       rc = digest_add_specfile(digest, fp, NULL,

sb.st_size, found_path);

Quoted 21 lines
libselinux/src/label_file.c +0 -5
                fclose(fp);
 
@@ -613,12 +646,7 @@ static void closef(struct selabel_handle *rec)
        for (i = 0; i < data->nspec; i++) {
                spec = &data->spec_arr[i];
                free(spec->lr.ctx_trans);
-               free(spec->lr.ctx_raw);
                regex_data_free(spec->regex);
-               if (spec->from_mmap)
-                       continue;
-               free(spec->regex_str);
-               free(spec->type_str);
        }
 
        for (i = 0; i < (unsigned int)data->num_stems; i++) {
libselinux/src/label_file.h +0 -0
index 88f4294..2704906 100644

libselinux/src/label_file.h struct spec { · line 38

Quoted 2 lines
libselinux/src/label_file.h +0 -0
@@ -37,7 +37,6 @@ struct spec {
        int matches;            /* number of matching pathnames */
        int stem_id;            /* indicates which stem-compression item

*/

libselinux/src/label_file.h struct spec { · line 40

Quoted 2 lines
libselinux/src/label_file.h +0 -1
        char hasMetaChars;      /* regular expression has meta-chars */
-       char from_mmap;         /* this spec is from an mmap of the data

*/

libselinux/src/label_file.h static inline int find_stem(struct saved_data *data,

Quoted 4 lines
libselinux/src/label_file.h +0 -0
        size_t prefix_len;      /* length of fixed path prefix */
 };
 
@@ -255,7 +254,7 @@ static inline int find_stem(struct saved_data *data,

const char *buf,

libselinux/src/label_file.h static inline int find_stem(struct saved_data *data, const char *buf,

Quoted 4 lines
libselinux/src/label_file.h +0 -1
 }
 
 /* returns the index of the new stored object */
-static inline int store_stem(struct saved_data *data, char *buf, int

stem_len)

libselinux/src/label_file.h static inline int find_stem(struct saved_data *data, · line 257

Quoted 1 line
libselinux/src/label_file.h +1 -0
+static inline int store_stem(struct saved_data *data, char *buf, int

stem_len, bool from_mmap)

libselinux/src/label_file.h static inline int store_stem(struct saved_data *data,

Quoted 4 lines
libselinux/src/label_file.h +0 -0
 {
        int num = data->num_stems;
 
@@ -271,7 +270,7 @@ static inline int store_stem(struct saved_data *data,

char *buf, int stem_len)

libselinux/src/label_file.h static inline int find_stem_from_spec(struct

Quoted 9 lines
libselinux/src/label_file.h +1 -1
        }
        data->stem_arr[num].len = stem_len;
        data->stem_arr[num].buf = buf;
-       data->stem_arr[num].from_mmap = 0;
+       data->stem_arr[num].from_mmap = from_mmap;
        data->num_stems++;
 
        return num;
@@ -298,7 +297,7 @@ static inline int find_stem_from_spec(struct

saved_data *data, const char *buf)

libselinux/src/label_file.h static inline int find_stem_from_spec(struct saved_data *data, const char *buf)

Quoted 8 lines
libselinux/src/label_file.h +1 -1
        if (!stem)
                return -1;
 
-       return store_stem(data, stem, stem_len);
+       return store_stem(data, stem, stem_len, false);
 }
 
 /* This will always check for buffer over-runs and either read the next

entry

libselinux/src/label_file.h static inline int next_entry(void *buf, struct

Quoted 1 line
libselinux/src/label_file.h +0 -0
@@ -317,6 +316,22 @@ static inline int next_entry(void *buf, struct

mmap_area *fp, size_t bytes)

Quoted 10 lines
libselinux/src/label_file.h +7 -0
        return 0;
 }
 
+static inline int next_pstr(char **str, struct mmap_area *fp, size_t len)
+{
+       if (len > fp->next_len)
+               return -1;
+
+       char *tmp = (char *)fp->next_addr;
+       if (tmp[len-1] != '\0')

libselinux/src/label_file.h static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

Quoted 10 lines
libselinux/src/label_file.h +9 -0
+               return -1;
+
+       *str = tmp;
+
+       fp->next_addr = (char *)fp->next_addr + len;
+       fp->next_len -= len;
+       return 0;
+}
+
 static inline int compile_regex(struct saved_data *data, struct spec

*spec,

libselinux/src/label_file.h static inline int process_line(struct

Quoted 3 lines
libselinux/src/label_file.h +0 -0
                                            const char **errbuf)
 {
@@ -375,13 +390,21 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 9 lines
libselinux/src/label_file.h +5 -1
                        char *line_buf, unsigned lineno)
 {
        int items, len, rc;
-       char *regex = NULL, *type = NULL, *context = NULL;
+       union {
+               struct {
+                       char *regex;
+                       char *type;
+                       char *context;

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 10 lines
libselinux/src/label_file.h +4 -1
+               };
+               char *array[3];
+       } found = { .array = { 0 } };
+
        struct saved_data *data = (struct saved_data *)rec->data;
        struct spec *spec_arr;
        unsigned int nspec = data->nspec;
        const char *errbuf = NULL;
 
-       items = read_spec_entries(line_buf, &errbuf, 3, &regex, &type,

&context);

libselinux/src/label_file.h static inline int process_line(struct · line 407

Quoted 1 line
libselinux/src/label_file.h +1 -0
+       items = read_spec_entries(line_buf, &errbuf,

ARRAY_SIZE(found.array), found.array);

libselinux/src/label_file.h static inline int process_line(struct

Quoted 4 lines
libselinux/src/label_file.h +0 -0
        if (items < 0) {
                rc = errno;
                selinux_log(SELINUX_ERROR,
@@ -398,22 +421,17 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 10 lines
libselinux/src/label_file.h +0 -3
                COMPAT_LOG(SELINUX_ERROR,
                            "%s:  line %u is missing fields\n", path,
                            lineno);
-               if (items == 1)
-                       free(regex);
                errno = EINVAL;
                return -1;
        } else if (items == 2) {
                /* The type field is optional. */
-               context = type;

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 10 lines
libselinux/src/label_file.h +4 -3
-               type = 0;
+               found.context = found.type;
+               found.type = NULL;
        }
 
-       len = get_stem_from_spec(regex);
-       if (len && prefix && strncmp(prefix, regex, len)) {
+       len = get_stem_from_spec(found.regex);
+       if (len && prefix && strncmp(prefix, found.regex, len)) {
                /* Stem of regex does not match requested prefix,

discard. */

libselinux/src/label_file.h static inline int process_line(struct

Quoted 7 lines
libselinux/src/label_file.h +0 -3
-               free(regex);
-               free(type);
-               free(context);
                return 0;
        }
 
@@ -424,13 +442,13 @@ static inline int process_line(struct

selabel_handle *rec,

libselinux/src/label_file.h static inline int process_line(struct

Quoted 18 lines
libselinux/src/label_file.h +4 -4
        spec_arr = data->spec_arr;
 
        /* process and store the specification in spec. */
-       spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
-       spec_arr[nspec].regex_str = regex;
+       spec_arr[nspec].stem_id = find_stem_from_spec(data, found.regex);
+       spec_arr[nspec].regex_str = found.regex;
 
-       spec_arr[nspec].type_str = type;
+       spec_arr[nspec].type_str = found.type;
        spec_arr[nspec].mode = 0;
 
-       spec_arr[nspec].lr.ctx_raw = context;
+       spec_arr[nspec].lr.ctx_raw = found.context;
 
        /*
         * bump data->nspecs to cause closef() to cover it in its free
@@ -442,18 +460,18 @@ static inline int process_line(struct

selabel_handle *rec,

libselinux/src/label_file.h static inline int process_line(struct · line 460

Quoted 1 line
libselinux/src/label_file.h +0 -0
                        && compile_regex(data, &spec_arr[nspec],

&errbuf)) {

Quoted 6 lines
libselinux/src/label_file.h +1 -1
                COMPAT_LOG(SELINUX_ERROR,
                           "%s:  line %u has invalid regex %s:  %s\n",
-                          path, lineno, regex, errbuf);
+                          path, lineno, found.regex, errbuf);
                errno = EINVAL;
                return -1;

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 10 lines
libselinux/src/label_file.h +2 -2
        }
 
-       if (type) {
-               mode_t mode = string_to_mode(type);
+       if (found.type) {
+               mode_t mode = string_to_mode(found.type);
 
                if (mode == (mode_t)-1) {
                        COMPAT_LOG(SELINUX_ERROR,
                                   "%s:  line %u has invalid file type

%s\n",

libselinux/src/label_file.h static inline int process_line(struct selabel_handle

Quoted 6 lines
libselinux/src/label_file.h +1 -1
-                                  path, lineno, type);
+                                  path, lineno, found.type);
                        errno = EINVAL;
                        return -1;
                }
@@ -464,7 +482,7 @@ static inline int process_line(struct selabel_handle

*rec,

Review

Quoted 9 lines
libselinux/src/label_file.h +1 -1
         * any meta characters in the RE */
        spec_hasMetaChars(&spec_arr[nspec]);
 
-       if (strcmp(context, "<<none>>") && rec->validating)
+       if (strcmp(found.context, "<<none>>") && rec->validating)
                compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
        return 0;
(quoted diff) +0 -0

b/libselinux/src/label_internal.h

libselinux/src/label_internal.h compat_validate(struct selabel_handle *rec, · line 143

Quoted 13 lines

index aa48fff..e40d68b 100644

libselinux/src/label_internal.h +1 -1
@@ -136,10 +136,11 @@ compat_validate(struct selabel_handle *rec,
                struct selabel_lookup_rec *contexts,
                const char *path, unsigned lineno) hidden;
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 /*
  * The read_spec_entries function may be used to
  * replace sscanf to read entries from spec files.
  */
-extern int read_spec_entries(char *line_buf, const char **errbuf, int

num_args, ...);

libselinux/src/label_internal.h compat_validate(struct selabel_handle *rec, · line 144

Quoted 1 line
libselinux/src/label_internal.h +1 -0
+extern int hidden read_spec_entries(char *line_buf, const char **errbuf,

unsigned int num, char *found[]);

Review

Quoted 3 lines
libselinux/src/label_internal.h +0 -0
 #endif                         /* _SELABEL_INTERNAL_H_ */
(quoted diff) +0 -0

b/libselinux/src/label_support.c

Quoted 5 lines

index 26f9ef1..3f5728d 100644

libselinux/src/label_support.c +0 -0
@@ -17,39 +17,40 @@
  * Read an entry from a spec file (e.g. file_contexts)

libselinux/src/label_support.c line 25

Quoted 10 lines
libselinux/src/label_support.c +2 -4
  * entry - Buffer to allocate for the entry.
  * ptr - current location of the line to be processed.
- * returns  - 0 on success and *entry is set to be a null
- *            terminated value. On Error it returns -1 and
- *            errno will be set.
+ * returns  - a pointer to the begining of the string.
+ *            that is the
  *
  */
-static inline int read_spec_entry(char **entry, char **ptr, int *len,

const char **errbuf)

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, char

Quoted 52 lines
libselinux/src/label_support.c +21 -18
+static inline char *read_spec_entry(char **current, const char **errbuf)
 {
-       *entry = NULL;
-       char *tmp_buf = NULL;
-
-       while (isspace(**ptr) && **ptr != '\0')
-               (*ptr)++;
+       if(!**current) {
+               (*current)++;
+               return NULL;
+       }
 
-       tmp_buf = *ptr;
-       *len = 0;
+       while (isspace(**current) && **current != '\0')
+               (*current)++;
 
-       while (!isspace(**ptr) && **ptr != '\0') {
-               if (!isascii(**ptr)) {
+       char *start = *current;
+       while (!isspace(**current) && **current != '\0') {
+               if (!isascii(**current)) {
                        errno = EINVAL;
                        *errbuf = "Non-ASCII characters found";
-                       return -1;
+                       return NULL;
                }
-               (*ptr)++;
-               (*len)++;
+               (*current)++;
        }
 
-       if (*len) {
-               *entry = strndup(tmp_buf, *len);
-               if (!*entry)
-                       return -1;
-       }
+       char *end = *current;
 
-       return 0;
+       if (start == end)
+               return NULL;
+
+       if (*end != '\0') {
+               *end = '\0';
+               (*current)++;
+       }
+       return start;
 }
 
 /*
@@ -63,15 +64,13 @@ static inline int read_spec_entry(char **entry, char

**ptr, int *len, const char

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, char · line 64

Quoted 1 line
libselinux/src/label_support.c +0 -0
  * This function calls read_spec_entry() to do the actual string

processing.

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, char **ptr, int *len, const char

Quoted 3 lines
libselinux/src/label_support.c +0 -1
  * As such, can return anything from that function as well.
  */
-int hidden read_spec_entries(char *line_buf, const char **errbuf, int

num_args, ...)

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, char · line 67

Quoted 1 line
libselinux/src/label_support.c +1 -0
+int hidden read_spec_entries(char *line_buf, const char **errbuf,

unsigned int num, char *found[])

libselinux/src/label_support.c int hidden read_spec_entries(char *line_buf, const

Quoted 15 lines
libselinux/src/label_support.c +3 -5
 {
-       char **spec_entry, *buf_p;
-       int len, rc, items, entry_len = 0;
-       va_list ap;
-
+       char*buf_p;
+       unsigned int items = 0;
        *errbuf = NULL;
 
-       len = strlen(line_buf);
+       size_t len = strlen(line_buf);
        if (line_buf[len - 1] == '\n')
                line_buf[len - 1] = '\0';
        else
@@ -89,26 +88,12 @@ int hidden read_spec_entries(char *line_buf, const

char **errbuf, int num_args,

Quoted 5 lines
libselinux/src/label_support.c +0 -2
                return 0;
 
        /* Process the spec file entries */
-       va_start(ap, num_args);
-

libselinux/src/label_support.c int hidden read_spec_entries(char *line_buf, const char **errbuf, int num_args,

Quoted 10 lines
libselinux/src/label_support.c +0 -10
-       items = 0;
-       while (items < num_args) {
-               spec_entry = va_arg(ap, char **);
-
-               if (len - 1 == buf_p - line_buf) {
-                       va_end(ap);
-                       return items;
-               }
-
-               rc = read_spec_entry(spec_entry, &buf_p, &entry_len,

errbuf);

Quoted 19 lines
libselinux/src/label_support.c +5 -8
-               if (rc < 0) {
-                       va_end(ap);
-                       return rc;
-               }
-               if (entry_len)
-                       items++;
+       while (items < num) {
+               found[items] = read_spec_entry(&buf_p, errbuf);
+               if (!found[items])
+                       break;
+               items++;
        }
-       va_end(ap);
        return items;
 }
 
--

1.9.1

Quoted 5 lines

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

#7 lore raw
Quoted 21 lines · Jason Zaman
Quoted 19 lines · On 20 Sep 2016 5:47 am, <[email protected]> wrote:

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

Quoted 7 lines
Quoted 5 lines

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

Do you have the scripts that generated these stats so I can play with it

too? These stats are from android right? Do you also have a comparison for refpolicy too?

For generating these I used checkfc.c from the Android tree. I used valgrind to measure allocations and clock to measure the time in selabel_open().

Quoted 2 lines

I haven't looked that closely yet but just realised, will this need new

perms because of the mmap? If it does, can you send a patch to refpolicy?

I'm confused, mmap is not a permission, even if it was the binary path already was doing an mmap, so the permission would have been there. We're just making it so it always mmaps.

Quoted 17 lines

-- Jason

Quoted 14 lines

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140

+++++++++++++++++++-------------

Quoted 13 lines
Quoted 13 lines
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label.c +0 -0
index 963bfcb..d767b49 100644
@@ -15,8 +15,6 @@
 #include "callbacks.h"
 #include "label_internal.h"
 
Quoted 6 lines

Review

Quoted 6 lines
libselinux/src/label.c +0 -2
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 typedef int (*selabel_initfunc)(struct selabel_handle *rec,
                                const struct selinux_opt *opts,
                                unsigned nopts);
(quoted diff) +0 -0

b/libselinux/src/label_android_property.c

Quoted 20 lines
Quoted 20 lines

index 290b438..2aac394 100644

libselinux/src/label_android_property.c +7 -1
@@ -85,13 +85,19 @@ static int process_line(struct selabel_handle *rec,
                        int pass, unsigned lineno)
 {
        int items;
-       char *prop = NULL, *context = NULL;
+       union {
+               struct {
+                       char *prop;
+                       char *context;
+               };
+               char *array[2];
+       } found = { .array = { 0 } };
        struct saved_data *data = (struct saved_data *)rec->data;
        spec_t *spec_arr = data->spec_arr;
        unsigned int nspec = data->nspec;
        const char *errbuf = NULL;
 
Quoted 1 line

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec, · line 94

Quoted 1 line
libselinux/src/label_android_property.c +0 -1
-       items = read_spec_entries(line_buf, &errbuf, 2, &prop,

&context);

Quoted 1 line

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec, · line 100

Quoted 1 line
libselinux/src/label_android_property.c +1 -0
+       items = read_spec_entries(line_buf, &errbuf,

ARRAY_SIZE(found.array), found.array);

Quoted 4 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle

Quoted 4 lines
libselinux/src/label_android_property.c +0 -0
        if (items < 0) {
                items = errno;
                selinux_log(SELINUX_ERROR,
@@ -108,18 +114,14 @@ static int process_line(struct selabel_handle

*rec,

Quoted 8 lines
Quoted 8 lines
libselinux/src/label_android_property.c +0 -1
                selinux_log(SELINUX_ERROR,
                            "%s:  line %u is missing fields\n", path,
                            lineno);
-               free(prop);
                errno = EINVAL;
                return -1;
        }
 
Quoted 6 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_android_property.c +1 -4
-       if (pass == 0) {
-               free(prop);
-               free(context);
-       } else if (pass == 1) {
+       if (pass == 1) {
                /* On the second pass, process and store the

specification in spec. */

Quoted 7 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec,

Quoted 7 lines
libselinux/src/label_android_property.c +2 -2
-               spec_arr[nspec].property_key = prop;
-               spec_arr[nspec].lr.ctx_raw = context;
+               spec_arr[nspec].property_key = found.prop;
+               spec_arr[nspec].lr.ctx_raw = found.context;
 
                if (rec->validating) {
                        if (selabel_validate(rec, &spec_arr[nspec].lr)

< 0) {

Quoted 9 lines
Quoted 9 lines
libselinux/src/label_android_property.c +1 -1
@@ -234,7 +236,7 @@ static void closef(struct selabel_handle *rec)
        for (i = 0; i < data->nspec; i++) {
                spec = &data->spec_arr[i];
                free(spec->property_key);
-               free(spec->lr.ctx_raw);
+               //free(spec->lr.ctx_raw);
                free(spec->lr.ctx_trans);
        }
 
Quoted 5 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data,

Quoted 5 lines
libselinux/src/label_file.c +0 -0
index 7156825..4dc440e 100644
@@ -96,43 +96,64 @@ static int nodups_specs(struct saved_data *data,

const char *path)

Quoted 5 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 5 lines
libselinux/src/label_file.c +0 -2
        return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-                            struct selabel_handle *rec, const char

*path)

Quoted 1 line

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, · line 99

Quoted 1 line
libselinux/src/label_file.c +1 -0
+static inline struct saved_data *rec_to_data(struct selabel_handle

*rec)

Quoted 29 lines
Quoted 29 lines
libselinux/src/label_file.c +28 -0
+{
+       return (struct saved_data *)rec->data;
+}
+
+static char *mmap_area_get_line(struct mmap_area *area)
+{
+       size_t len = area->next_len;
+       if (!len)
+               return NULL;
+
+       char *start = area->next_addr;
+       char *end = memchr(start, '\n', len);
+
+       /* the file may not end with a newline */
+       if (!end)
+               end = (char *)area->next_addr + len - 1;
+
+       *end = '\0';
+       /* len includes null byte */
+       len = end - start;
+
+       area->next_len -= len + 1;
+       area->next_addr = ++end;
+       return start;
+}
+
+static int process_text_file(const char *path, const char *prefix,
+               struct selabel_handle *rec)
 {
Quoted 8 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 8 lines
libselinux/src/label_file.c +4 -2
        int rc;
-       size_t line_len;
        unsigned int lineno = 0;
-       char *line_buf = NULL;
+       char *line_buf;
+       struct mmap_area *areas = rec_to_data(rec)->mmap_areas;
+
+       /* mmap_area_get_line() and process_line() require mutable

string pointers */

Quoted 7 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 7 lines
libselinux/src/label_file.c +4 -1
+       rc = mprotect(areas->addr, areas->len, PROT_READ | PROT_WRITE);
+       if (rc < 0)
+               goto out;
 
-       while (getline(&line_buf, &line_len, fp) > 0) {
+       while ( (line_buf = mmap_area_get_line(areas)) ) {
                rc = process_line(rec, path, prefix, line_buf,

++lineno);

Quoted 28 lines
Quoted 28 lines
libselinux/src/label_file.c +2 -13
                if (rc)
                        goto out;
        }
        rc = 0;
 out:
-       free(line_buf);
        return rc;
 }
 
-static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
-                    const char *path)
+static int mmap_file(FILE *fp, size_t len, struct saved_data *data)
 {
-       struct saved_data *data = (struct saved_data *)rec->data;
-       int rc;
-       char *addr, *str_buf;
-       int *stem_map;
-       struct mmap_area *mmap_area;
-       uint32_t i, magic, version;
-       uint32_t entry_len, stem_map_len, regex_array_len;
-       const char *reg_version;
-
-       mmap_area = malloc(sizeof(*mmap_area));
+       struct mmap_area *mmap_area = malloc(sizeof(*mmap_area));
        if (!mmap_area) {
                return -1;
        }
 
Quoted 2 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, · line 156

Quoted 2 lines
libselinux/src/label_file.c +1 -1
-       addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
+       void *addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE,

fileno(fp), 0);

Quoted 4 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 4 lines
libselinux/src/label_file.c +0 -0
        if (addr == MAP_FAILED) {
                free(mmap_area);
                perror("mmap");
@@ -145,17 +166,26 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

Quoted 11 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 11 lines
libselinux/src/label_file.c +4 -4
        mmap_area->next = data->mmap_areas;
        data->mmap_areas = mmap_area;
 
-       /* check if this looks like an fcontext file */
-       rc = next_entry(&magic, mmap_area, sizeof(uint32_t));
-       if (rc < 0 || magic != SELINUX_MAGIC_COMPILED_FCONTEXT)
-               return -1;
+       return 0;
+}
+
+static int process_binary_file(const char *path, struct selabel_handle

*rec)

Quoted 15 lines
Quoted 15 lines
libselinux/src/label_file.c +9 -0
+{
+       struct saved_data *data = (struct saved_data *)rec->data;
+       int rc;
+       char *str_buf;
+       int *stem_map;
+       struct mmap_area *mmap_area = data->mmap_areas;
+       uint32_t i, version;
+       uint32_t entry_len, stem_map_len, regex_array_len;
+       size_t len;
 
        /* check if this version is higher than we understand */
        rc = next_entry(&version, mmap_area, sizeof(uint32_t));
        if (rc < 0 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
                return -1;
 
Quoted 6 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 6 lines
libselinux/src/label_file.c +1 -1
-       reg_version = regex_version();
+       const char *reg_version = regex_version();
        if (!reg_version)
                return -1;
 
@@ -235,12 +265,11 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

Quoted 6 lines
Quoted 6 lines
libselinux/src/label_file.c +1 -1
                /* store the mapping between old and new */
                newid = find_stem(data, buf, stem_len);
                if (newid < 0) {
-                       newid = store_stem(data, buf, stem_len);
+                       newid = store_stem(data, buf, stem_len, true);
                        if (newid < 0) {
Quoted 8 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 8 lines
libselinux/src/label_file.c +0 -1
                                rc = newid;
                                goto out;
                        }
-                       data->stem_arr[newid].from_mmap = 1;
                }
                stem_map[i] = newid;
        }
@@ -262,7 +291,6 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

Quoted 7 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 7 lines
libselinux/src/label_file.c +0 -1
                        goto out;
 
                spec = &data->spec_arr[data->nspec];
-               spec->from_mmap = 1;
 
                /* Process context */
                rc = next_entry(&entry_len, mmap_area,

sizeof(uint32_t));

Quoted 1 line

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 1 line
libselinux/src/label_file.c +0 -0
@@ -271,22 +299,10 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

Quoted 13 lines
Quoted 13 lines
libselinux/src/label_file.c +1 -6
                        goto out;
                }
 
-               str_buf = malloc(entry_len);
-               if (!str_buf) {
-                       rc = -1;
-                       goto out;
-               }
-               rc = next_entry(str_buf, mmap_area, entry_len);
+               rc = next_pstr(&spec->lr.ctx_raw, mmap_area, entry_len);
                if (rc < 0)
                        goto out;
 
Quoted 8 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 8 lines
libselinux/src/label_file.c +0 -7
-               if (str_buf[entry_len - 1] != '\0') {
-                       free(str_buf);
-                       rc = -1;
-                       goto out;
-               }
-               spec->lr.ctx_raw = str_buf;
-
                if (strcmp(spec->lr.ctx_raw, "<<none>>") &&

rec->validating) {

Quoted 3 lines

libselinux/src/label_file.c static char *rolling_append(char *current, const

Quoted 3 lines
libselinux/src/label_file.c +0 -0
                        if (selabel_validate(rec, &spec->lr) < 0) {
                                selinux_log(SELINUX_ERROR,
@@ -401,17 +417,29 @@ static char *rolling_append(char *current, const

char *suffix, size_t max)

Quoted 5 lines

libselinux/src/label_file.c static char *rolling_append(char *current, const char *suffix, size_t max)

Quoted 5 lines
libselinux/src/label_file.c +1 -1
        return current;
 }
 
-static bool fcontext_is_binary(FILE *fp)
+static inline int mmap_area_rewind(struct mmap_area *area, size_t

bytes)

Quoted 24 lines
Quoted 24 lines
libselinux/src/label_file.c +15 -4
+{
+       if (area->next_len + bytes > area->len)
+               return -1;
+
+       area->next_addr = (char *) area->next_addr - bytes;
+       area->next_len += bytes;
+       return 0;
+}
+
+static bool fcontext_is_binary(struct mmap_area *area)
 {
        uint32_t magic;
+       bool is_binary = false;
 
-       size_t len = fread(&magic, sizeof(magic), 1, fp);
-       rewind(fp);
+       int rc = next_entry(&magic, area, sizeof(magic));
 
-       return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
-}
+       is_binary = (!rc && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
+       if (!is_binary)
+               mmap_area_rewind(area, sizeof(magic));
 
Quoted 6 lines

libselinux/src/label_file.c static char *rolling_append(char *current, const char *suffix, size_t max)

Quoted 6 lines
libselinux/src/label_file.c +2 -1
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+       return is_binary;
+}
 
 static FILE *open_file(const char *path, const char *suffix,
               char *save_path, size_t len, struct stat *sb, bool

open_oldest)

Quoted 1 line

libselinux/src/label_file.c static int process_file(const char *path, const

Quoted 1 line
libselinux/src/label_file.c +0 -0
@@ -504,12 +532,17 @@ static int process_file(const char *path, const

char *suffix,

Quoted 5 lines

libselinux/src/label_file.c static int process_file(const char *path, const char *suffix,

Quoted 5 lines
libselinux/src/label_file.c +0 -2
                if (fp == NULL)
                        return -1;
 
-               rc = fcontext_is_binary(fp) ?
-                               load_mmap(fp, sb.st_size, rec,

found_path) :

Quoted 1 line

libselinux/src/label_file.c static int process_file(const char *path, const · line 509

Quoted 1 line
libselinux/src/label_file.c +0 -1
-                               process_text_file(fp, prefix, rec,

found_path);

Quoted 9 lines

libselinux/src/label_file.c static int process_file(const char *path, const char *suffix,

Quoted 9 lines
libselinux/src/label_file.c +9 -0
+               rc = mmap_file(fp, sb.st_size, rec_to_data(rec));
+               if (rc < 0) {
+                       fclose(fp);
+                       return -1;
+               }
+
+               rc = fcontext_is_binary(rec_to_data(rec)->mmap_areas) ?
+                               process_binary_file(found_path, rec) :
+                               process_text_file(found_path, prefix,

rec);

Quoted 2 lines

libselinux/src/label_file.c static int process_file(const char *path, const · line 511

Quoted 2 lines
libselinux/src/label_file.c +0 -1
                if (!rc)
-                       rc = digest_add_specfile(digest, fp, NULL,

sb.st_size,

Quoted 2 lines

libselinux/src/label_file.c static int process_file(const char *path, const · line 545

Quoted 2 lines
libselinux/src/label_file.c +1 -1
-                               found_path);
+                       rc = digest_add_specfile(digest, fp, NULL,

sb.st_size, found_path);

Quoted 15 lines
Quoted 15 lines
libselinux/src/label_file.c +0 -5
                fclose(fp);
 
@@ -613,12 +646,7 @@ static void closef(struct selabel_handle *rec)
        for (i = 0; i < data->nspec; i++) {
                spec = &data->spec_arr[i];
                free(spec->lr.ctx_trans);
-               free(spec->lr.ctx_raw);
                regex_data_free(spec->regex);
-               if (spec->from_mmap)
-                       continue;
-               free(spec->regex_str);
-               free(spec->type_str);
        }
 
Quoted 8 lines

libselinux/src/label_file.h struct spec { · line 38

Quoted 8 lines
libselinux/src/label_file.c +0 -0
        for (i = 0; i < (unsigned int)data->num_stems; i++) {
libselinux/src/label_file.h +0 -0
index 88f4294..2704906 100644
@@ -37,7 +37,6 @@ struct spec {
        int matches;            /* number of matching pathnames */
        int stem_id;            /* indicates which stem-compression

item */

Quoted 2 lines

libselinux/src/label_file.h struct spec { · line 40

Quoted 2 lines
libselinux/src/label_file.h +0 -1
        char hasMetaChars;      /* regular expression has meta-chars */
-       char from_mmap;         /* this spec is from an mmap of the

data */

Quoted 4 lines

libselinux/src/label_file.h static inline int find_stem(struct saved_data

Quoted 4 lines
libselinux/src/label_file.h +0 -0
        size_t prefix_len;      /* length of fixed path prefix */
 };
 
@@ -255,7 +254,7 @@ static inline int find_stem(struct saved_data

*data, const char *buf,

Quoted 4 lines

libselinux/src/label_file.h static inline int find_stem(struct saved_data *data, const char *buf,

Quoted 4 lines
libselinux/src/label_file.h +0 -1
 }
 
 /* returns the index of the new stored object */
-static inline int store_stem(struct saved_data *data, char *buf, int

stem_len)

Quoted 1 line

libselinux/src/label_file.h static inline int find_stem(struct saved_data · line 257

Quoted 1 line
libselinux/src/label_file.h +1 -0
+static inline int store_stem(struct saved_data *data, char *buf, int

stem_len, bool from_mmap)

Quoted 4 lines

libselinux/src/label_file.h static inline int store_stem(struct saved_data

Quoted 4 lines
libselinux/src/label_file.h +0 -0
 {
        int num = data->num_stems;
 
@@ -271,7 +270,7 @@ static inline int store_stem(struct saved_data

*data, char *buf, int stem_len)

Quoted 9 lines

libselinux/src/label_file.h static inline int find_stem_from_spec(struct

Quoted 9 lines
libselinux/src/label_file.h +1 -1
        }
        data->stem_arr[num].len = stem_len;
        data->stem_arr[num].buf = buf;
-       data->stem_arr[num].from_mmap = 0;
+       data->stem_arr[num].from_mmap = from_mmap;
        data->num_stems++;
 
        return num;
@@ -298,7 +297,7 @@ static inline int find_stem_from_spec(struct

saved_data *data, const char *buf)

Quoted 8 lines

libselinux/src/label_file.h static inline int find_stem_from_spec(struct saved_data *data, const char *buf)

Quoted 8 lines
libselinux/src/label_file.h +1 -1
        if (!stem)
                return -1;
 
-       return store_stem(data, stem, stem_len);
+       return store_stem(data, stem, stem_len, false);
 }
 
 /* This will always check for buffer over-runs and either read the

next entry

Quoted 1 line

libselinux/src/label_file.h static inline int next_entry(void *buf, struct

Quoted 1 line
libselinux/src/label_file.h +0 -0
@@ -317,6 +316,22 @@ static inline int next_entry(void *buf, struct

mmap_area *fp, size_t bytes)

Quoted 4 lines

libselinux/src/label_file.h static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

Quoted 4 lines
libselinux/src/label_file.h +1 -0
        return 0;
 }
 
+static inline int next_pstr(char **str, struct mmap_area *fp, size_t

len)

Quoted 8 lines
Quoted 8 lines
libselinux/src/label_file.h +8 -0
+{
+       if (len > fp->next_len)
+               return -1;
+
+       char *tmp = (char *)fp->next_addr;
+       if (tmp[len-1] != '\0')
+               return -1;
+
Quoted 8 lines

libselinux/src/label_file.h static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

Quoted 8 lines
libselinux/src/label_file.h +7 -0
+       *str = tmp;
+
+       fp->next_addr = (char *)fp->next_addr + len;
+       fp->next_len -= len;
+       return 0;
+}
+
 static inline int compile_regex(struct saved_data *data, struct spec

*spec,

Quoted 3 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 3 lines
libselinux/src/label_file.h +0 -0
                                            const char **errbuf)
 {
@@ -375,13 +390,21 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 18 lines
Quoted 18 lines
libselinux/src/label_file.h +9 -1
                        char *line_buf, unsigned lineno)
 {
        int items, len, rc;
-       char *regex = NULL, *type = NULL, *context = NULL;
+       union {
+               struct {
+                       char *regex;
+                       char *type;
+                       char *context;
+               };
+               char *array[3];
+       } found = { .array = { 0 } };
+
        struct saved_data *data = (struct saved_data *)rec->data;
        struct spec *spec_arr;
        unsigned int nspec = data->nspec;
        const char *errbuf = NULL;
 
Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct · line 384

Quoted 1 line
libselinux/src/label_file.h +0 -1
-       items = read_spec_entries(line_buf, &errbuf, 3, &regex, &type,

&context);

Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct · line 407

Quoted 1 line
libselinux/src/label_file.h +1 -0
+       items = read_spec_entries(line_buf, &errbuf,

ARRAY_SIZE(found.array), found.array);

Quoted 4 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 4 lines
libselinux/src/label_file.h +0 -0
        if (items < 0) {
                rc = errno;
                selinux_log(SELINUX_ERROR,
@@ -398,22 +421,17 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 15 lines
Quoted 15 lines
libselinux/src/label_file.h +2 -4
                COMPAT_LOG(SELINUX_ERROR,
                            "%s:  line %u is missing fields\n", path,
                            lineno);
-               if (items == 1)
-                       free(regex);
                errno = EINVAL;
                return -1;
        } else if (items == 2) {
                /* The type field is optional. */
-               context = type;
-               type = 0;
+               found.context = found.type;
+               found.type = NULL;
        }
 
Quoted 5 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 5 lines
libselinux/src/label_file.h +2 -2
-       len = get_stem_from_spec(regex);
-       if (len && prefix && strncmp(prefix, regex, len)) {
+       len = get_stem_from_spec(found.regex);
+       if (len && prefix && strncmp(prefix, found.regex, len)) {
                /* Stem of regex does not match requested prefix,

discard. */

Quoted 7 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 7 lines
libselinux/src/label_file.h +0 -3
-               free(regex);
-               free(type);
-               free(context);
                return 0;
        }
 
@@ -424,13 +442,13 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_file.h +1 -2
        spec_arr = data->spec_arr;
 
        /* process and store the specification in spec. */
-       spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
-       spec_arr[nspec].regex_str = regex;
+       spec_arr[nspec].stem_id = find_stem_from_spec(data,

found.regex);

Quoted 6 lines
Quoted 6 lines
libselinux/src/label_file.h +2 -1
+       spec_arr[nspec].regex_str = found.regex;
 
-       spec_arr[nspec].type_str = type;
+       spec_arr[nspec].type_str = found.type;
        spec_arr[nspec].mode = 0;
 
Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 6 lines
libselinux/src/label_file.h +1 -1
-       spec_arr[nspec].lr.ctx_raw = context;
+       spec_arr[nspec].lr.ctx_raw = found.context;
 
        /*
         * bump data->nspecs to cause closef() to cover it in its free
@@ -442,18 +460,18 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct · line 460

Quoted 1 line
libselinux/src/label_file.h +0 -0
                        && compile_regex(data, &spec_arr[nspec],

&errbuf)) {

Quoted 8 lines
Quoted 8 lines
libselinux/src/label_file.h +1 -1
                COMPAT_LOG(SELINUX_ERROR,
                           "%s:  line %u has invalid regex %s:  %s\n",
-                          path, lineno, regex, errbuf);
+                          path, lineno, found.regex, errbuf);
                errno = EINVAL;
                return -1;
        }
 
Quoted 8 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 8 lines
libselinux/src/label_file.h +2 -2
-       if (type) {
-               mode_t mode = string_to_mode(type);
+       if (found.type) {
+               mode_t mode = string_to_mode(found.type);
 
                if (mode == (mode_t)-1) {
                        COMPAT_LOG(SELINUX_ERROR,
                                   "%s:  line %u has invalid file type

%s\n",

Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 6 lines
libselinux/src/label_file.h +1 -1
-                                  path, lineno, type);
+                                  path, lineno, found.type);
                        errno = EINVAL;
                        return -1;
                }
@@ -464,7 +482,7 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 9 lines

Review

Quoted 9 lines
libselinux/src/label_file.h +1 -1
         * any meta characters in the RE */
        spec_hasMetaChars(&spec_arr[nspec]);
 
-       if (strcmp(context, "<<none>>") && rec->validating)
+       if (strcmp(found.context, "<<none>>") && rec->validating)
                compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
        return 0;
(quoted diff) +0 -0

b/libselinux/src/label_internal.h

Quoted 7 lines
Quoted 7 lines

index aa48fff..e40d68b 100644

libselinux/src/label_internal.h +0 -0
@@ -136,10 +136,11 @@ compat_validate(struct selabel_handle *rec,
                struct selabel_lookup_rec *contexts,
                const char *path, unsigned lineno) hidden;
 
Quoted 6 lines

libselinux/src/label_internal.h compat_validate(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_internal.h +1 -1
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 /*
  * The read_spec_entries function may be used to
  * replace sscanf to read entries from spec files.
  */
-extern int read_spec_entries(char *line_buf, const char **errbuf, int

num_args, ...);

Quoted 1 line

libselinux/src/label_internal.h compat_validate(struct selabel_handle *rec, · line 144

Quoted 1 line
libselinux/src/label_internal.h +1 -0
+extern int hidden read_spec_entries(char *line_buf, const char

**errbuf, unsigned int num, char *found[]);

Quoted 3 lines

Review

Quoted 3 lines
libselinux/src/label_internal.h +0 -0
 #endif                         /* _SELABEL_INTERNAL_H_ */
(quoted diff) +0 -0

b/libselinux/src/label_support.c

Quoted 7 lines
Quoted 7 lines

index 26f9ef1..3f5728d 100644

libselinux/src/label_support.c +0 -0
@@ -17,39 +17,40 @@
  * Read an entry from a spec file (e.g. file_contexts)
  * entry - Buffer to allocate for the entry.
  * ptr - current location of the line to be processed.
Quoted 8 lines

libselinux/src/label_support.c

Quoted 8 lines
libselinux/src/label_support.c +2 -4
- * returns  - 0 on success and *entry is set to be a null
- *            terminated value. On Error it returns -1 and
- *            errno will be set.
+ * returns  - a pointer to the begining of the string.
+ *            that is the
  *
  */
-static inline int read_spec_entry(char **entry, char **ptr, int *len,

const char **errbuf)

Quoted 1 line

libselinux/src/label_support.c line 24

Quoted 1 line
libselinux/src/label_support.c +1 -0
+static inline char *read_spec_entry(char **current, const char

**errbuf)

Quoted 49 lines
Quoted 49 lines
libselinux/src/label_support.c +20 -18
 {
-       *entry = NULL;
-       char *tmp_buf = NULL;
-
-       while (isspace(**ptr) && **ptr != '\0')
-               (*ptr)++;
+       if(!**current) {
+               (*current)++;
+               return NULL;
+       }
 
-       tmp_buf = *ptr;
-       *len = 0;
+       while (isspace(**current) && **current != '\0')
+               (*current)++;
 
-       while (!isspace(**ptr) && **ptr != '\0') {
-               if (!isascii(**ptr)) {
+       char *start = *current;
+       while (!isspace(**current) && **current != '\0') {
+               if (!isascii(**current)) {
                        errno = EINVAL;
                        *errbuf = "Non-ASCII characters found";
-                       return -1;
+                       return NULL;
                }
-               (*ptr)++;
-               (*len)++;
+               (*current)++;
        }
 
-       if (*len) {
-               *entry = strndup(tmp_buf, *len);
-               if (!*entry)
-                       return -1;
-       }
+       char *end = *current;
 
-       return 0;
+       if (start == end)
+               return NULL;
+
+       if (*end != '\0') {
+               *end = '\0';
+               (*current)++;
+       }
+       return start;
 }
 
Quoted 2 lines

libselinux/src/label_support.c static inline int read_spec_entry(char **entry,

Quoted 2 lines
libselinux/src/label_support.c +0 -0
 /*
@@ -63,15 +64,13 @@ static inline int read_spec_entry(char **entry,

char **ptr, int *len, const char

Quoted 1 line

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, · line 64

Quoted 1 line
libselinux/src/label_support.c +0 -0
  * This function calls read_spec_entry() to do the actual string

processing.

Quoted 3 lines

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, char **ptr, int *len, const char

Quoted 3 lines
libselinux/src/label_support.c +0 -1
  * As such, can return anything from that function as well.
  */
-int hidden read_spec_entries(char *line_buf, const char **errbuf, int

num_args, ...)

Quoted 1 line

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, · line 67

Quoted 1 line
libselinux/src/label_support.c +1 -0
+int hidden read_spec_entries(char *line_buf, const char **errbuf,

unsigned int num, char *found[])

Quoted 9 lines
Quoted 9 lines
libselinux/src/label_support.c +2 -4
 {
-       char **spec_entry, *buf_p;
-       int len, rc, items, entry_len = 0;
-       va_list ap;
-
+       char*buf_p;
+       unsigned int items = 0;
        *errbuf = NULL;
 
Quoted 6 lines

libselinux/src/label_support.c int hidden read_spec_entries(char *line_buf, const

Quoted 6 lines
libselinux/src/label_support.c +1 -1
-       len = strlen(line_buf);
+       size_t len = strlen(line_buf);
        if (line_buf[len - 1] == '\n')
                line_buf[len - 1] = '\0';
        else
@@ -89,26 +88,12 @@ int hidden read_spec_entries(char *line_buf, const

char **errbuf, int num_args,

Quoted 15 lines
Quoted 5 lines
libselinux/src/label_support.c +0 -2
                return 0;
 
        /* Process the spec file entries */
-       va_start(ap, num_args);
-

libselinux/src/label_support.c int hidden read_spec_entries(char *line_buf, const char **errbuf, int num_args,

Quoted 10 lines
libselinux/src/label_support.c +0 -10
-       items = 0;
-       while (items < num_args) {
-               spec_entry = va_arg(ap, char **);
-
-               if (len - 1 == buf_p - line_buf) {
-                       va_end(ap);
-                       return items;
-               }
-
-               rc = read_spec_entry(spec_entry, &buf_p, &entry_len,

errbuf);

Quoted 16 lines
Quoted 16 lines
libselinux/src/label_support.c +5 -7
-               if (rc < 0) {
-                       va_end(ap);
-                       return rc;
-               }
-               if (entry_len)
-                       items++;
+       while (items < num) {
+               found[items] = read_spec_entry(&buf_p, errbuf);
+               if (!found[items])
+                       break;
+               items++;
        }
-       va_end(ap);
        return items;
 }
 
Quoted 8 lines
Quoted 8 lines

--
1.9.1

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

Quoted 7 lines

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

#8 lore raw
Quoted 23 lines · William Roberts
Quoted 21 lines · Jason Zaman
Quoted 19 lines · On 20 Sep 2016 5:47 am, <[email protected]> wrote:

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

Do you have the scripts that generated these stats so I can play with

it too? These stats are from android right? Do you also have a comparison for refpolicy too?

Quoted 2 lines

For generating these I used checkfc.c from the Android tree. I used

valgrind to measure allocations and clock to measure the time in selabel_open().

Okay cool I'll fetch that and give it a whirl when I get time.

Quoted 2 lines
Quoted 2 lines

I haven't looked that closely yet but just realised, will this need new

perms because of the mmap? If it does, can you send a patch to refpolicy?

Quoted 2 lines

I'm confused, mmap is not a permission, even if it was the binary path

already was doing an mmap, so the permission would have been there. We're just making it so it always mmaps.

Yeah but mmap needs execute perms sometimes (always?). I am out so just wanted to send an email before I forgot. If it was mmaping already then there is nothing to worry about :).

-- Jason

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140

+++++++++++++++++++-------------

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label.c +0 -0
index 963bfcb..d767b49 100644
@@ -15,8 +15,6 @@
 #include "callbacks.h"
 #include "label_internal.h"
 
Quoted 6 lines
Quoted 6 lines

Review

Quoted 6 lines
libselinux/src/label.c +0 -2
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 typedef int (*selabel_initfunc)(struct selabel_handle *rec,
                                const struct selinux_opt *opts,
                                unsigned nopts);
(quoted diff) +0 -0

b/libselinux/src/label_android_property.c

Quoted 4 lines
Quoted 4 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle

Quoted 4 lines

index 290b438..2aac394 100644

libselinux/src/label_android_property.c +0 -0
@@ -85,13 +85,19 @@ static int process_line(struct selabel_handle

*rec,

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
libselinux/src/label_android_property.c +7 -1
                        int pass, unsigned lineno)
 {
        int items;
-       char *prop = NULL, *context = NULL;
+       union {
+               struct {
+                       char *prop;
+                       char *context;
+               };
+               char *array[2];
+       } found = { .array = { 0 } };
        struct saved_data *data = (struct saved_data *)rec->data;
        spec_t *spec_arr = data->spec_arr;
        unsigned int nspec = data->nspec;
        const char *errbuf = NULL;
 
Quoted 1 line
Quoted 1 line

libselinux/src/label_android_property.c static int process_line(struct selabel_handle · line 94

Quoted 1 line
libselinux/src/label_android_property.c +0 -1
-       items = read_spec_entries(line_buf, &errbuf, 2, &prop,

&context);

Quoted 1 line
Quoted 1 line

libselinux/src/label_android_property.c static int process_line(struct selabel_handle · line 100

Quoted 1 line
libselinux/src/label_android_property.c +1 -0
+       items = read_spec_entries(line_buf, &errbuf,

ARRAY_SIZE(found.array), found.array);

Quoted 4 lines
Quoted 4 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle

Quoted 4 lines
libselinux/src/label_android_property.c +0 -0
        if (items < 0) {
                items = errno;
                selinux_log(SELINUX_ERROR,
@@ -108,18 +114,14 @@ static int process_line(struct selabel_handle

*rec,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
libselinux/src/label_android_property.c +0 -1
                selinux_log(SELINUX_ERROR,
                            "%s:  line %u is missing fields\n", path,
                            lineno);
-               free(prop);
                errno = EINVAL;
                return -1;
        }
 
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_android_property.c +1 -4
-       if (pass == 0) {
-               free(prop);
-               free(context);
-       } else if (pass == 1) {
+       if (pass == 1) {
                /* On the second pass, process and store the

specification in spec. */

Quoted 7 lines
Quoted 7 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec,

Quoted 7 lines
libselinux/src/label_android_property.c +2 -2
-               spec_arr[nspec].property_key = prop;
-               spec_arr[nspec].lr.ctx_raw = context;
+               spec_arr[nspec].property_key = found.prop;
+               spec_arr[nspec].lr.ctx_raw = found.context;
 
                if (rec->validating) {
                        if (selabel_validate(rec,

&spec_arr[nspec].lr) < 0) {

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
libselinux/src/label_android_property.c +1 -1
@@ -234,7 +236,7 @@ static void closef(struct selabel_handle *rec)
        for (i = 0; i < data->nspec; i++) {
                spec = &data->spec_arr[i];
                free(spec->property_key);
-               free(spec->lr.ctx_raw);
+               //free(spec->lr.ctx_raw);
                free(spec->lr.ctx_trans);
        }
 
Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data,

Quoted 5 lines
libselinux/src/label_file.c +0 -0
index 7156825..4dc440e 100644
@@ -96,43 +96,64 @@ static int nodups_specs(struct saved_data *data,

const char *path)

Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 5 lines
libselinux/src/label_file.c +0 -2
        return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-                            struct selabel_handle *rec, const char

*path)

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, · line 99

Quoted 1 line
libselinux/src/label_file.c +1 -0
+static inline struct saved_data *rec_to_data(struct selabel_handle

*rec)

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
libselinux/src/label_file.c +28 -0
+{
+       return (struct saved_data *)rec->data;
+}
+
+static char *mmap_area_get_line(struct mmap_area *area)
+{
+       size_t len = area->next_len;
+       if (!len)
+               return NULL;
+
+       char *start = area->next_addr;
+       char *end = memchr(start, '\n', len);
+
+       /* the file may not end with a newline */
+       if (!end)
+               end = (char *)area->next_addr + len - 1;
+
+       *end = '\0';
+       /* len includes null byte */
+       len = end - start;
+
+       area->next_len -= len + 1;
+       area->next_addr = ++end;
+       return start;
+}
+
+static int process_text_file(const char *path, const char *prefix,
+               struct selabel_handle *rec)
 {
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 8 lines
libselinux/src/label_file.c +4 -2
        int rc;
-       size_t line_len;
        unsigned int lineno = 0;
-       char *line_buf = NULL;
+       char *line_buf;
+       struct mmap_area *areas = rec_to_data(rec)->mmap_areas;
+
+       /* mmap_area_get_line() and process_line() require mutable

string pointers */

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, · line 135

Quoted 1 line
libselinux/src/label_file.c +1 -0
+       rc = mprotect(areas->addr, areas->len, PROT_READ |

PROT_WRITE);

Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 6 lines
libselinux/src/label_file.c +3 -1
+       if (rc < 0)
+               goto out;
 
-       while (getline(&line_buf, &line_len, fp) > 0) {
+       while ( (line_buf = mmap_area_get_line(areas)) ) {
                rc = process_line(rec, path, prefix, line_buf,

++lineno);

Quoted 10 lines
Quoted 10 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 10 lines
libselinux/src/label_file.c +0 -2
                if (rc)
                        goto out;
        }
        rc = 0;
 out:
-       free(line_buf);
        return rc;
 }
 
-static int load_mmap(FILE *fp, size_t len, struct selabel_handle

*rec,

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
libselinux/src/label_file.c +2 -11
-                    const char *path)
+static int mmap_file(FILE *fp, size_t len, struct saved_data *data)
 {
-       struct saved_data *data = (struct saved_data *)rec->data;
-       int rc;
-       char *addr, *str_buf;
-       int *stem_map;
-       struct mmap_area *mmap_area;
-       uint32_t i, magic, version;
-       uint32_t entry_len, stem_map_len, regex_array_len;
-       const char *reg_version;
-
-       mmap_area = malloc(sizeof(*mmap_area));
+       struct mmap_area *mmap_area = malloc(sizeof(*mmap_area));
        if (!mmap_area) {
                return -1;
        }
 
Quoted 2 lines
Quoted 2 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, · line 156

Quoted 2 lines
libselinux/src/label_file.c +1 -1
-       addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
+       void *addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE,

fileno(fp), 0);

Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len,

Quoted 4 lines
libselinux/src/label_file.c +0 -0
        if (addr == MAP_FAILED) {
                free(mmap_area);
                perror("mmap");
@@ -145,17 +166,26 @@ static int load_mmap(FILE *fp, size_t len,

struct selabel_handle *rec,

Quoted 11 lines
Quoted 11 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 11 lines
libselinux/src/label_file.c +4 -4
        mmap_area->next = data->mmap_areas;
        data->mmap_areas = mmap_area;
 
-       /* check if this looks like an fcontext file */
-       rc = next_entry(&magic, mmap_area, sizeof(uint32_t));
-       if (rc < 0 || magic != SELINUX_MAGIC_COMPILED_FCONTEXT)
-               return -1;
+       return 0;
+}
+
+static int process_binary_file(const char *path, struct

selabel_handle *rec)

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
libselinux/src/label_file.c +9 -0
+{
+       struct saved_data *data = (struct saved_data *)rec->data;
+       int rc;
+       char *str_buf;
+       int *stem_map;
+       struct mmap_area *mmap_area = data->mmap_areas;
+       uint32_t i, version;
+       uint32_t entry_len, stem_map_len, regex_array_len;
+       size_t len;
 
        /* check if this version is higher than we understand */
        rc = next_entry(&version, mmap_area, sizeof(uint32_t));
        if (rc < 0 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
                return -1;
 
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len,

Quoted 6 lines
libselinux/src/label_file.c +1 -1
-       reg_version = regex_version();
+       const char *reg_version = regex_version();
        if (!reg_version)
                return -1;
 
@@ -235,12 +265,11 @@ static int load_mmap(FILE *fp, size_t len,

struct selabel_handle *rec,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
libselinux/src/label_file.c +1 -1
                /* store the mapping between old and new */
                newid = find_stem(data, buf, stem_len);
                if (newid < 0) {
-                       newid = store_stem(data, buf, stem_len);
+                       newid = store_stem(data, buf, stem_len, true);
                        if (newid < 0) {
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 8 lines
libselinux/src/label_file.c +0 -1
                                rc = newid;
                                goto out;
                        }
-                       data->stem_arr[newid].from_mmap = 1;
                }
                stem_map[i] = newid;
        }
@@ -262,7 +291,6 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

Quoted 7 lines
Quoted 7 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 7 lines
libselinux/src/label_file.c +0 -1
                        goto out;
 
                spec = &data->spec_arr[data->nspec];
-               spec->from_mmap = 1;
 
                /* Process context */
                rc = next_entry(&entry_len, mmap_area,

sizeof(uint32_t));

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len,

Quoted 1 line
libselinux/src/label_file.c +0 -0
@@ -271,22 +299,10 @@ static int load_mmap(FILE *fp, size_t len,

struct selabel_handle *rec,

Quoted 10 lines
Quoted 10 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 10 lines
libselinux/src/label_file.c +1 -6
                        goto out;
                }
 
-               str_buf = malloc(entry_len);
-               if (!str_buf) {
-                       rc = -1;
-                       goto out;
-               }
-               rc = next_entry(str_buf, mmap_area, entry_len);
+               rc = next_pstr(&spec->lr.ctx_raw, mmap_area,

entry_len);

Quoted 11 lines
Quoted 11 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 11 lines
libselinux/src/label_file.c +0 -7
                if (rc < 0)
                        goto out;
 
-               if (str_buf[entry_len - 1] != '\0') {
-                       free(str_buf);
-                       rc = -1;
-                       goto out;
-               }
-               spec->lr.ctx_raw = str_buf;
-
                if (strcmp(spec->lr.ctx_raw, "<<none>>") &&

rec->validating) {

Quoted 3 lines
Quoted 3 lines

libselinux/src/label_file.c static char *rolling_append(char *current,

Quoted 3 lines
libselinux/src/label_file.c +0 -0
                        if (selabel_validate(rec, &spec->lr) < 0) {
                                selinux_log(SELINUX_ERROR,
@@ -401,17 +417,29 @@ static char *rolling_append(char *current,

const char *suffix, size_t max)

Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.c static char *rolling_append(char *current, const char *suffix, size_t max)

Quoted 5 lines
libselinux/src/label_file.c +1 -1
        return current;
 }
 
-static bool fcontext_is_binary(FILE *fp)
+static inline int mmap_area_rewind(struct mmap_area *area, size_t

bytes)

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
libselinux/src/label_file.c +11 -0
+{
+       if (area->next_len + bytes > area->len)
+               return -1;
+
+       area->next_addr = (char *) area->next_addr - bytes;
+       area->next_len += bytes;
+       return 0;
+}
+
+static bool fcontext_is_binary(struct mmap_area *area)
 {
        uint32_t magic;
+       bool is_binary = false;
 
Quoted 7 lines
Quoted 7 lines

libselinux/src/label_file.c static char *rolling_append(char *current, const char *suffix, size_t max)

Quoted 7 lines
libselinux/src/label_file.c +2 -4
-       size_t len = fread(&magic, sizeof(magic), 1, fp);
-       rewind(fp);
+       int rc = next_entry(&magic, area, sizeof(magic));
 
-       return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
-}
+       is_binary = (!rc && (magic ==

SELINUX_MAGIC_COMPILED_FCONTEXT));

Quoted 9 lines
Quoted 9 lines

libselinux/src/label_file.c static char *rolling_append(char *current, const char *suffix, size_t max)

Quoted 9 lines
libselinux/src/label_file.c +4 -1
+       if (!is_binary)
+               mmap_area_rewind(area, sizeof(magic));
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+       return is_binary;
+}
 
 static FILE *open_file(const char *path, const char *suffix,
               char *save_path, size_t len, struct stat *sb, bool

open_oldest)

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int process_file(const char *path, const

Quoted 1 line
libselinux/src/label_file.c +0 -0
@@ -504,12 +532,17 @@ static int process_file(const char *path, const

char *suffix,

Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.c static int process_file(const char *path, const char *suffix,

Quoted 5 lines
libselinux/src/label_file.c +0 -2
                if (fp == NULL)
                        return -1;
 
-               rc = fcontext_is_binary(fp) ?
-                               load_mmap(fp, sb.st_size, rec,

found_path) :

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int process_file(const char *path, const · line 509

Quoted 1 line
libselinux/src/label_file.c +0 -1
-                               process_text_file(fp, prefix, rec,

found_path);

Quoted 7 lines
Quoted 7 lines

libselinux/src/label_file.c static int process_file(const char *path, const char *suffix,

Quoted 7 lines
libselinux/src/label_file.c +7 -0
+               rc = mmap_file(fp, sb.st_size, rec_to_data(rec));
+               if (rc < 0) {
+                       fclose(fp);
+                       return -1;
+               }
+
+               rc = fcontext_is_binary(rec_to_data(rec)->mmap_areas)

?

Quoted 2 lines
Quoted 2 lines

libselinux/src/label_file.c static int process_file(const char *path, const · line 543

Quoted 2 lines
libselinux/src/label_file.c +2 -0
+                               process_binary_file(found_path, rec) :
+                               process_text_file(found_path, prefix,

rec);

Quoted 2 lines
Quoted 2 lines

libselinux/src/label_file.c static int process_file(const char *path, const · line 511

Quoted 2 lines
libselinux/src/label_file.c +0 -1
                if (!rc)
-                       rc = digest_add_specfile(digest, fp, NULL,

sb.st_size,

Quoted 2 lines
Quoted 2 lines

libselinux/src/label_file.c static int process_file(const char *path, const · line 545

Quoted 2 lines
libselinux/src/label_file.c +1 -1
-                               found_path);
+                       rc = digest_add_specfile(digest, fp, NULL,

sb.st_size, found_path);

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
libselinux/src/label_file.c +0 -5
                fclose(fp);
 
@@ -613,12 +646,7 @@ static void closef(struct selabel_handle *rec)
        for (i = 0; i < data->nspec; i++) {
                spec = &data->spec_arr[i];
                free(spec->lr.ctx_trans);
-               free(spec->lr.ctx_raw);
                regex_data_free(spec->regex);
-               if (spec->from_mmap)
-                       continue;
-               free(spec->regex_str);
-               free(spec->type_str);
        }
 
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.h struct spec { · line 38

Quoted 8 lines
libselinux/src/label_file.c +0 -0
        for (i = 0; i < (unsigned int)data->num_stems; i++) {
libselinux/src/label_file.h +0 -0
index 88f4294..2704906 100644
@@ -37,7 +37,6 @@ struct spec {
        int matches;            /* number of matching pathnames */
        int stem_id;            /* indicates which stem-compression

item */

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h struct spec { · line 39

Quoted 1 line
libselinux/src/label_file.h +0 -0
        char hasMetaChars;      /* regular expression has meta-chars

*/

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h struct spec { · line 40

Quoted 1 line
libselinux/src/label_file.h +0 -1
-       char from_mmap;         /* this spec is from an mmap of the

data */

Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int find_stem(struct saved_data

Quoted 4 lines
libselinux/src/label_file.h +0 -0
        size_t prefix_len;      /* length of fixed path prefix */
 };
 
@@ -255,7 +254,7 @@ static inline int find_stem(struct saved_data

*data, const char *buf,

Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int find_stem(struct saved_data *data, const char *buf,

Quoted 4 lines
libselinux/src/label_file.h +0 -1
 }
 
 /* returns the index of the new stored object */
-static inline int store_stem(struct saved_data *data, char *buf, int

stem_len)

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int find_stem(struct saved_data · line 257

Quoted 1 line
libselinux/src/label_file.h +1 -0
+static inline int store_stem(struct saved_data *data, char *buf, int

stem_len, bool from_mmap)

Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int store_stem(struct saved_data

Quoted 4 lines
libselinux/src/label_file.h +0 -0
 {
        int num = data->num_stems;
 
@@ -271,7 +270,7 @@ static inline int store_stem(struct saved_data

*data, char *buf, int stem_len)

Quoted 9 lines
Quoted 9 lines

libselinux/src/label_file.h static inline int find_stem_from_spec(struct

Quoted 9 lines
libselinux/src/label_file.h +1 -1
        }
        data->stem_arr[num].len = stem_len;
        data->stem_arr[num].buf = buf;
-       data->stem_arr[num].from_mmap = 0;
+       data->stem_arr[num].from_mmap = from_mmap;
        data->num_stems++;
 
        return num;
@@ -298,7 +297,7 @@ static inline int find_stem_from_spec(struct

saved_data *data, const char *buf)

Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.h static inline int find_stem_from_spec(struct saved_data *data, const char *buf)

Quoted 8 lines
libselinux/src/label_file.h +1 -1
        if (!stem)
                return -1;
 
-       return store_stem(data, stem, stem_len);
+       return store_stem(data, stem, stem_len, false);
 }
 
 /* This will always check for buffer over-runs and either read the

next entry

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int next_entry(void *buf, struct

Quoted 1 line
libselinux/src/label_file.h +0 -0
@@ -317,6 +316,22 @@ static inline int next_entry(void *buf, struct

mmap_area *fp, size_t bytes)

Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

Quoted 4 lines
libselinux/src/label_file.h +1 -0
        return 0;
 }
 
+static inline int next_pstr(char **str, struct mmap_area *fp, size_t

len)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
libselinux/src/label_file.h +8 -0
+{
+       if (len > fp->next_len)
+               return -1;
+
+       char *tmp = (char *)fp->next_addr;
+       if (tmp[len-1] != '\0')
+               return -1;
+
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.h static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

Quoted 8 lines
libselinux/src/label_file.h +7 -0
+       *str = tmp;
+
+       fp->next_addr = (char *)fp->next_addr + len;
+       fp->next_len -= len;
+       return 0;
+}
+
 static inline int compile_regex(struct saved_data *data, struct spec

*spec,

Quoted 3 lines
Quoted 3 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 3 lines
libselinux/src/label_file.h +0 -0
                                            const char **errbuf)
 {
@@ -375,13 +390,21 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
libselinux/src/label_file.h +9 -1
                        char *line_buf, unsigned lineno)
 {
        int items, len, rc;
-       char *regex = NULL, *type = NULL, *context = NULL;
+       union {
+               struct {
+                       char *regex;
+                       char *type;
+                       char *context;
+               };
+               char *array[3];
+       } found = { .array = { 0 } };
+
        struct saved_data *data = (struct saved_data *)rec->data;
        struct spec *spec_arr;
        unsigned int nspec = data->nspec;
        const char *errbuf = NULL;
 
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct · line 384

Quoted 1 line
libselinux/src/label_file.h +0 -1
-       items = read_spec_entries(line_buf, &errbuf, 3, &regex,

&type, &context);

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct · line 407

Quoted 1 line
libselinux/src/label_file.h +1 -0
+       items = read_spec_entries(line_buf, &errbuf,

ARRAY_SIZE(found.array), found.array);

Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 4 lines
libselinux/src/label_file.h +0 -0
        if (items < 0) {
                rc = errno;
                selinux_log(SELINUX_ERROR,
@@ -398,22 +421,17 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
libselinux/src/label_file.h +2 -4
                COMPAT_LOG(SELINUX_ERROR,
                            "%s:  line %u is missing fields\n", path,
                            lineno);
-               if (items == 1)
-                       free(regex);
                errno = EINVAL;
                return -1;
        } else if (items == 2) {
                /* The type field is optional. */
-               context = type;
-               type = 0;
+               found.context = found.type;
+               found.type = NULL;
        }
 
Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 5 lines
libselinux/src/label_file.h +2 -2
-       len = get_stem_from_spec(regex);
-       if (len && prefix && strncmp(prefix, regex, len)) {
+       len = get_stem_from_spec(found.regex);
+       if (len && prefix && strncmp(prefix, found.regex, len)) {
                /* Stem of regex does not match requested prefix,

discard. */

Quoted 7 lines
Quoted 7 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 7 lines
libselinux/src/label_file.h +0 -3
-               free(regex);
-               free(type);
-               free(context);
                return 0;
        }
 
@@ -424,13 +442,13 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_file.h +1 -2
        spec_arr = data->spec_arr;
 
        /* process and store the specification in spec. */
-       spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
-       spec_arr[nspec].regex_str = regex;
+       spec_arr[nspec].stem_id = find_stem_from_spec(data,

found.regex);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
libselinux/src/label_file.h +2 -1
+       spec_arr[nspec].regex_str = found.regex;
 
-       spec_arr[nspec].type_str = type;
+       spec_arr[nspec].type_str = found.type;
        spec_arr[nspec].mode = 0;
 
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 6 lines
libselinux/src/label_file.h +1 -1
-       spec_arr[nspec].lr.ctx_raw = context;
+       spec_arr[nspec].lr.ctx_raw = found.context;
 
        /*
         * bump data->nspecs to cause closef() to cover it in its free
@@ -442,18 +460,18 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct · line 460

Quoted 1 line
libselinux/src/label_file.h +0 -0
                        && compile_regex(data, &spec_arr[nspec],

&errbuf)) {

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
libselinux/src/label_file.h +1 -1
                COMPAT_LOG(SELINUX_ERROR,
                           "%s:  line %u has invalid regex %s:  %s\n",
-                          path, lineno, regex, errbuf);
+                          path, lineno, found.regex, errbuf);
                errno = EINVAL;
                return -1;
        }
 
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 8 lines
libselinux/src/label_file.h +2 -2
-       if (type) {
-               mode_t mode = string_to_mode(type);
+       if (found.type) {
+               mode_t mode = string_to_mode(found.type);
 
                if (mode == (mode_t)-1) {
                        COMPAT_LOG(SELINUX_ERROR,
                                   "%s:  line %u has invalid file

type %s\n",

Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 6 lines
libselinux/src/label_file.h +1 -1
-                                  path, lineno, type);
+                                  path, lineno, found.type);
                        errno = EINVAL;
                        return -1;
                }
@@ -464,7 +482,7 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_file.h +1 -1
         * any meta characters in the RE */
        spec_hasMetaChars(&spec_arr[nspec]);
 
-       if (strcmp(context, "<<none>>") && rec->validating)
+       if (strcmp(found.context, "<<none>>") && rec->validating)
                compat_validate(rec, &spec_arr[nspec].lr, path,

lineno);

Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
libselinux/src/label_file.h +0 -0
        return 0;
(quoted diff) +0 -0

b/libselinux/src/label_internal.h

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

index aa48fff..e40d68b 100644

libselinux/src/label_internal.h +0 -0
@@ -136,10 +136,11 @@ compat_validate(struct selabel_handle *rec,
                struct selabel_lookup_rec *contexts,
                const char *path, unsigned lineno) hidden;
 
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_internal.h compat_validate(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_internal.h +1 -1
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 /*
  * The read_spec_entries function may be used to
  * replace sscanf to read entries from spec files.
  */
-extern int read_spec_entries(char *line_buf, const char **errbuf,

int num_args, ...);

Quoted 1 line
Quoted 1 line

libselinux/src/label_internal.h compat_validate(struct selabel_handle *rec, · line 144

Quoted 1 line
libselinux/src/label_internal.h +1 -0
+extern int hidden read_spec_entries(char *line_buf, const char

**errbuf, unsigned int num, char *found[]);

Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
libselinux/src/label_internal.h +0 -0
 #endif                         /* _SELABEL_INTERNAL_H_ */
(quoted diff) +0 -0

b/libselinux/src/label_support.c

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

index 26f9ef1..3f5728d 100644

libselinux/src/label_support.c +0 -0
@@ -17,39 +17,40 @@
  * Read an entry from a spec file (e.g. file_contexts)
  * entry - Buffer to allocate for the entry.
  * ptr - current location of the line to be processed.
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_support.c

Quoted 8 lines
libselinux/src/label_support.c +2 -4
- * returns  - 0 on success and *entry is set to be a null
- *            terminated value. On Error it returns -1 and
- *            errno will be set.
+ * returns  - a pointer to the begining of the string.
+ *            that is the
  *
  */
-static inline int read_spec_entry(char **entry, char **ptr, int

*len, const char **errbuf)

Quoted 1 line
Quoted 1 line

libselinux/src/label_support.c line 24

Quoted 1 line
libselinux/src/label_support.c +1 -0
+static inline char *read_spec_entry(char **current, const char

**errbuf)

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
libselinux/src/label_support.c +20 -18
 {
-       *entry = NULL;
-       char *tmp_buf = NULL;
-
-       while (isspace(**ptr) && **ptr != '\0')
-               (*ptr)++;
+       if(!**current) {
+               (*current)++;
+               return NULL;
+       }
 
-       tmp_buf = *ptr;
-       *len = 0;
+       while (isspace(**current) && **current != '\0')
+               (*current)++;
 
-       while (!isspace(**ptr) && **ptr != '\0') {
-               if (!isascii(**ptr)) {
+       char *start = *current;
+       while (!isspace(**current) && **current != '\0') {
+               if (!isascii(**current)) {
                        errno = EINVAL;
                        *errbuf = "Non-ASCII characters found";
-                       return -1;
+                       return NULL;
                }
-               (*ptr)++;
-               (*len)++;
+               (*current)++;
        }
 
-       if (*len) {
-               *entry = strndup(tmp_buf, *len);
-               if (!*entry)
-                       return -1;
-       }
+       char *end = *current;
 
-       return 0;
+       if (start == end)
+               return NULL;
+
+       if (*end != '\0') {
+               *end = '\0';
+               (*current)++;
+       }
+       return start;
 }
 
Quoted 2 lines
Quoted 2 lines

libselinux/src/label_support.c static inline int read_spec_entry(char **entry,

Quoted 2 lines
libselinux/src/label_support.c +0 -0
 /*
@@ -63,15 +64,13 @@ static inline int read_spec_entry(char **entry,

char **ptr, int *len, const char

Quoted 1 line
Quoted 1 line

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, · line 64

Quoted 1 line
libselinux/src/label_support.c +0 -0
  * This function calls read_spec_entry() to do the actual string

processing.

Quoted 3 lines
Quoted 3 lines

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, char **ptr, int *len, const char

Quoted 3 lines
libselinux/src/label_support.c +0 -1
  * As such, can return anything from that function as well.
  */
-int hidden read_spec_entries(char *line_buf, const char **errbuf,

int num_args, ...)

Quoted 1 line
Quoted 1 line

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, · line 67

Quoted 1 line
libselinux/src/label_support.c +1 -0
+int hidden read_spec_entries(char *line_buf, const char **errbuf,

unsigned int num, char *found[])

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
libselinux/src/label_support.c +2 -4
 {
-       char **spec_entry, *buf_p;
-       int len, rc, items, entry_len = 0;
-       va_list ap;
-
+       char*buf_p;
+       unsigned int items = 0;
        *errbuf = NULL;
 
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_support.c int hidden read_spec_entries(char *line_buf,

Quoted 6 lines
libselinux/src/label_support.c +1 -1
-       len = strlen(line_buf);
+       size_t len = strlen(line_buf);
        if (line_buf[len - 1] == '\n')
                line_buf[len - 1] = '\0';
        else
@@ -89,26 +88,12 @@ int hidden read_spec_entries(char *line_buf,

const char **errbuf, int num_args,

Quoted 15 lines
Quoted 15 lines
Quoted 5 lines
libselinux/src/label_support.c +0 -2
                return 0;
 
        /* Process the spec file entries */
-       va_start(ap, num_args);
-

libselinux/src/label_support.c int hidden read_spec_entries(char *line_buf, const char **errbuf, int num_args,

Quoted 10 lines
libselinux/src/label_support.c +0 -10
-       items = 0;
-       while (items < num_args) {
-               spec_entry = va_arg(ap, char **);
-
-               if (len - 1 == buf_p - line_buf) {
-                       va_end(ap);
-                       return items;
-               }
-
-               rc = read_spec_entry(spec_entry, &buf_p, &entry_len,

errbuf);

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
libselinux/src/label_support.c +5 -7
-               if (rc < 0) {
-                       va_end(ap);
-                       return rc;
-               }
-               if (entry_len)
-                       items++;
+       while (items < num) {
+               found[items] = read_spec_entry(&buf_p, errbuf);
+               if (!found[items])
+                       break;
+               items++;
        }
-       va_end(ap);
        return items;
 }
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

--
1.9.1

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

Quoted 7 lines
Quoted 7 lines

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

#9 lore raw
Quoted 2 lines · Jason Zaman

On 20 Sep 2016 12:50 pm, "William Roberts" <[email protected]>

Quoted 23 lines · wrote:
Quoted 23 lines · Jason Zaman
Quoted 21 lines · On 20 Sep 2016 5:47 am, <[email protected]> wrote:
Quoted 19 lines

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

Do you have the scripts that generated these stats so I can play with

it too? These stats are from android right? Do you also have a comparison for refpolicy too?

Quoted 2 lines
Quoted 2 lines

For generating these I used checkfc.c from the Android tree. I used

valgrind to measure allocations and clock to measure the time in selabel_open().

Quoted 5 lines

Okay cool I'll fetch that and give it a whirl when I get time.

Quoted 2 lines
Quoted 2 lines

I haven't looked that closely yet but just realised, will this need

new perms because of the mmap? If it does, can you send a patch to refpolicy?

Quoted 2 lines
Quoted 2 lines

I'm confused, mmap is not a permission, even if it was the binary path

already was doing an mmap, so the permission would have been there. We're just making it so it always mmaps.

Quoted 2 lines

Yeah but mmap needs execute perms sometimes (always?). I am out so just

wanted to send an email before I forgot. If it was mmaping already then there is nothing to worry about :).

Mmap would only need execute if you attempted to set the prot bits to execute it use mprotect to change the mapping. Then things like execmod might come I to play if the mapping was ever writable.

Quoted 17 lines

-- Jason

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140

+++++++++++++++++++-------------

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label.c +0 -0
index 963bfcb..d767b49 100644
@@ -15,8 +15,6 @@
 #include "callbacks.h"
 #include "label_internal.h"
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Review

Quoted 6 lines
libselinux/src/label.c +0 -2
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 typedef int (*selabel_initfunc)(struct selabel_handle *rec,
                                const struct selinux_opt *opts,
                                unsigned nopts);
(quoted diff) +0 -0

b/libselinux/src/label_android_property.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle

Quoted 4 lines

index 290b438..2aac394 100644

libselinux/src/label_android_property.c +0 -0
@@ -85,13 +85,19 @@ static int process_line(struct selabel_handle

*rec,

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
libselinux/src/label_android_property.c +7 -1
                        int pass, unsigned lineno)
 {
        int items;
-       char *prop = NULL, *context = NULL;
+       union {
+               struct {
+                       char *prop;
+                       char *context;
+               };
+               char *array[2];
+       } found = { .array = { 0 } };
        struct saved_data *data = (struct saved_data *)rec->data;
        spec_t *spec_arr = data->spec_arr;
        unsigned int nspec = data->nspec;
        const char *errbuf = NULL;
 
Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_android_property.c static int process_line(struct selabel_handle · line 94

Quoted 1 line
libselinux/src/label_android_property.c +0 -1
-       items = read_spec_entries(line_buf, &errbuf, 2, &prop,

&context);

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_android_property.c static int process_line(struct selabel_handle · line 100

Quoted 1 line
libselinux/src/label_android_property.c +1 -0
+       items = read_spec_entries(line_buf, &errbuf,

ARRAY_SIZE(found.array), found.array);

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle

Quoted 4 lines
libselinux/src/label_android_property.c +0 -0
        if (items < 0) {
                items = errno;
                selinux_log(SELINUX_ERROR,
@@ -108,18 +114,14 @@ static int process_line(struct selabel_handle

*rec,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle · line 115

Quoted 2 lines
libselinux/src/label_android_property.c +0 -0
                selinux_log(SELINUX_ERROR,
                            "%s:  line %u is missing fields\n",

path,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
libselinux/src/label_android_property.c +0 -1
                            lineno);
-               free(prop);
                errno = EINVAL;
                return -1;
        }
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_android_property.c +1 -4
-       if (pass == 0) {
-               free(prop);
-               free(context);
-       } else if (pass == 1) {
+       if (pass == 1) {
                /* On the second pass, process and store the

specification in spec. */

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

libselinux/src/label_android_property.c static int process_line(struct selabel_handle *rec,

Quoted 7 lines
libselinux/src/label_android_property.c +2 -2
-               spec_arr[nspec].property_key = prop;
-               spec_arr[nspec].lr.ctx_raw = context;
+               spec_arr[nspec].property_key = found.prop;
+               spec_arr[nspec].lr.ctx_raw = found.context;
 
                if (rec->validating) {
                        if (selabel_validate(rec,

&spec_arr[nspec].lr) < 0) {

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

Review

Quoted 10 lines
libselinux/src/label_android_property.c +1 -1
@@ -234,7 +236,7 @@ static void closef(struct selabel_handle *rec)
        for (i = 0; i < data->nspec; i++) {
                spec = &data->spec_arr[i];
                free(spec->property_key);
-               free(spec->lr.ctx_raw);
+               //free(spec->lr.ctx_raw);
                free(spec->lr.ctx_trans);
        }
 
(quoted diff) +0 -0

b/libselinux/src/label_file.c

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data

Quoted 4 lines

index 7156825..4dc440e 100644

libselinux/src/label_file.c +0 -0
@@ -96,43 +96,64 @@ static int nodups_specs(struct saved_data

*data, const char *path)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 5 lines
libselinux/src/label_file.c +0 -2
        return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-                            struct selabel_handle *rec, const char

*path)

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int nodups_specs(struct saved_data · line 99

Quoted 1 line
libselinux/src/label_file.c +1 -0
+static inline struct saved_data *rec_to_data(struct selabel_handle

*rec)

Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
libselinux/src/label_file.c +28 -0
+{
+       return (struct saved_data *)rec->data;
+}
+
+static char *mmap_area_get_line(struct mmap_area *area)
+{
+       size_t len = area->next_len;
+       if (!len)
+               return NULL;
+
+       char *start = area->next_addr;
+       char *end = memchr(start, '\n', len);
+
+       /* the file may not end with a newline */
+       if (!end)
+               end = (char *)area->next_addr + len - 1;
+
+       *end = '\0';
+       /* len includes null byte */
+       len = end - start;
+
+       area->next_len -= len + 1;
+       area->next_addr = ++end;
+       return start;
+}
+
+static int process_text_file(const char *path, const char *prefix,
+               struct selabel_handle *rec)
 {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 8 lines
libselinux/src/label_file.c +4 -2
        int rc;
-       size_t line_len;
        unsigned int lineno = 0;
-       char *line_buf = NULL;
+       char *line_buf;
+       struct mmap_area *areas = rec_to_data(rec)->mmap_areas;
+
+       /* mmap_area_get_line() and process_line() require mutable

string pointers */

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int nodups_specs(struct saved_data · line 135

Quoted 1 line
libselinux/src/label_file.c +1 -0
+       rc = mprotect(areas->addr, areas->len, PROT_READ |

PROT_WRITE);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 6 lines
libselinux/src/label_file.c +3 -1
+       if (rc < 0)
+               goto out;
 
-       while (getline(&line_buf, &line_len, fp) > 0) {
+       while ( (line_buf = mmap_area_get_line(areas)) ) {
                rc = process_line(rec, path, prefix, line_buf,

++lineno);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 10 lines
libselinux/src/label_file.c +0 -2
                if (rc)
                        goto out;
        }
        rc = 0;
 out:
-       free(line_buf);
        return rc;
 }
 
-static int load_mmap(FILE *fp, size_t len, struct selabel_handle

*rec,

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
libselinux/src/label_file.c +2 -11
-                    const char *path)
+static int mmap_file(FILE *fp, size_t len, struct saved_data *data)
 {
-       struct saved_data *data = (struct saved_data *)rec->data;
-       int rc;
-       char *addr, *str_buf;
-       int *stem_map;
-       struct mmap_area *mmap_area;
-       uint32_t i, magic, version;
-       uint32_t entry_len, stem_map_len, regex_array_len;
-       const char *reg_version;
-
-       mmap_area = malloc(sizeof(*mmap_area));
+       struct mmap_area *mmap_area = malloc(sizeof(*mmap_area));
        if (!mmap_area) {
                return -1;
        }
 
Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int nodups_specs(struct saved_data · line 135

Quoted 1 line
libselinux/src/label_file.c +0 -1
-       addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fileno(fp),

0);

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int nodups_specs(struct saved_data · line 156

Quoted 1 line
libselinux/src/label_file.c +1 -0
+       void *addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE,

fileno(fp), 0);

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len,

Quoted 4 lines
libselinux/src/label_file.c +0 -0
        if (addr == MAP_FAILED) {
                free(mmap_area);
                perror("mmap");
@@ -145,17 +166,26 @@ static int load_mmap(FILE *fp, size_t len,

struct selabel_handle *rec,

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 11 lines
libselinux/src/label_file.c +4 -4
        mmap_area->next = data->mmap_areas;
        data->mmap_areas = mmap_area;
 
-       /* check if this looks like an fcontext file */
-       rc = next_entry(&magic, mmap_area, sizeof(uint32_t));
-       if (rc < 0 || magic != SELINUX_MAGIC_COMPILED_FCONTEXT)
-               return -1;
+       return 0;
+}
+
+static int process_binary_file(const char *path, struct

selabel_handle *rec)

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
libselinux/src/label_file.c +9 -0
+{
+       struct saved_data *data = (struct saved_data *)rec->data;
+       int rc;
+       char *str_buf;
+       int *stem_map;
+       struct mmap_area *mmap_area = data->mmap_areas;
+       uint32_t i, version;
+       uint32_t entry_len, stem_map_len, regex_array_len;
+       size_t len;
 
        /* check if this version is higher than we understand */
        rc = next_entry(&version, mmap_area, sizeof(uint32_t));
        if (rc < 0 || version > SELINUX_COMPILED_FCONTEXT_MAX_VERS)
                return -1;
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len,

Quoted 6 lines
libselinux/src/label_file.c +1 -1
-       reg_version = regex_version();
+       const char *reg_version = regex_version();
        if (!reg_version)
                return -1;
 
@@ -235,12 +265,11 @@ static int load_mmap(FILE *fp, size_t len,

struct selabel_handle *rec,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 5 lines
libselinux/src/label_file.c +1 -1
                /* store the mapping between old and new */
                newid = find_stem(data, buf, stem_len);
                if (newid < 0) {
-                       newid = store_stem(data, buf, stem_len);
+                       newid = store_stem(data, buf, stem_len,

true);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len,

Quoted 9 lines
libselinux/src/label_file.c +0 -1
                        if (newid < 0) {
                                rc = newid;
                                goto out;
                        }
-                       data->stem_arr[newid].from_mmap = 1;
                }
                stem_map[i] = newid;
        }
@@ -262,7 +291,6 @@ static int load_mmap(FILE *fp, size_t len,

struct selabel_handle *rec,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 7 lines
libselinux/src/label_file.c +0 -1
                        goto out;
 
                spec = &data->spec_arr[data->nspec];
-               spec->from_mmap = 1;
 
                /* Process context */
                rc = next_entry(&entry_len, mmap_area,

sizeof(uint32_t));

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len,

Quoted 1 line
libselinux/src/label_file.c +0 -0
@@ -271,22 +299,10 @@ static int load_mmap(FILE *fp, size_t len,

struct selabel_handle *rec,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 10 lines
libselinux/src/label_file.c +1 -6
                        goto out;
                }
 
-               str_buf = malloc(entry_len);
-               if (!str_buf) {
-                       rc = -1;
-                       goto out;
-               }
-               rc = next_entry(str_buf, mmap_area, entry_len);
+               rc = next_pstr(&spec->lr.ctx_raw, mmap_area,

entry_len);

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 11 lines
libselinux/src/label_file.c +0 -7
                if (rc < 0)
                        goto out;
 
-               if (str_buf[entry_len - 1] != '\0') {
-                       free(str_buf);
-                       rc = -1;
-                       goto out;
-               }
-               spec->lr.ctx_raw = str_buf;
-
                if (strcmp(spec->lr.ctx_raw, "<<none>>") &&

rec->validating) {

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

libselinux/src/label_file.c static char *rolling_append(char *current,

Quoted 3 lines
libselinux/src/label_file.c +0 -0
                        if (selabel_validate(rec, &spec->lr) < 0) {
                                selinux_log(SELINUX_ERROR,
@@ -401,17 +417,29 @@ static char *rolling_append(char *current,

const char *suffix, size_t max)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.c static char *rolling_append(char *current, const char *suffix, size_t max)

Quoted 5 lines
libselinux/src/label_file.c +1 -1
        return current;
 }
 
-static bool fcontext_is_binary(FILE *fp)
+static inline int mmap_area_rewind(struct mmap_area *area, size_t

bytes)

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
libselinux/src/label_file.c +11 -0
+{
+       if (area->next_len + bytes > area->len)
+               return -1;
+
+       area->next_addr = (char *) area->next_addr - bytes;
+       area->next_len += bytes;
+       return 0;
+}
+
+static bool fcontext_is_binary(struct mmap_area *area)
 {
        uint32_t magic;
+       bool is_binary = false;
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

libselinux/src/label_file.c static char *rolling_append(char *current, const char *suffix, size_t max)

Quoted 7 lines
libselinux/src/label_file.c +2 -4
-       size_t len = fread(&magic, sizeof(magic), 1, fp);
-       rewind(fp);
+       int rc = next_entry(&magic, area, sizeof(magic));
 
-       return (len && (magic == SELINUX_MAGIC_COMPILED_FCONTEXT));
-}
+       is_binary = (!rc && (magic ==

SELINUX_MAGIC_COMPILED_FCONTEXT));

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

libselinux/src/label_file.c static char *rolling_append(char *current, const char *suffix, size_t max)

Quoted 9 lines
libselinux/src/label_file.c +4 -1
+       if (!is_binary)
+               mmap_area_rewind(area, sizeof(magic));
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+       return is_binary;
+}
 
 static FILE *open_file(const char *path, const char *suffix,
               char *save_path, size_t len, struct stat *sb, bool

open_oldest)

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int process_file(const char *path,

Quoted 1 line
libselinux/src/label_file.c +0 -0
@@ -504,12 +532,17 @@ static int process_file(const char *path,

const char *suffix,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.c static int process_file(const char *path, const char *suffix,

Quoted 5 lines
libselinux/src/label_file.c +0 -2
                if (fp == NULL)
                        return -1;
 
-               rc = fcontext_is_binary(fp) ?
-                               load_mmap(fp, sb.st_size, rec,

found_path) :

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int process_file(const char *path, · line 509

Quoted 1 line
libselinux/src/label_file.c +0 -1
-                               process_text_file(fp, prefix, rec,

found_path);

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

libselinux/src/label_file.c static int process_file(const char *path, const char *suffix,

Quoted 7 lines
libselinux/src/label_file.c +7 -0
+               rc = mmap_file(fp, sb.st_size, rec_to_data(rec));
+               if (rc < 0) {
+                       fclose(fp);
+                       return -1;
+               }
+
+               rc =

fcontext_is_binary(rec_to_data(rec)->mmap_areas) ?

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int process_file(const char *path, · line 542

Quoted 1 line
libselinux/src/label_file.c +1 -0
+                               process_binary_file(found_path,

rec) :

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.c static int process_file(const char *path, · line 543

Quoted 1 line
libselinux/src/label_file.c +1 -0
+                               process_text_file(found_path,

prefix, rec);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

libselinux/src/label_file.c static int process_file(const char *path, · line 511

Quoted 2 lines
libselinux/src/label_file.c +0 -1
                if (!rc)
-                       rc = digest_add_specfile(digest, fp, NULL,

sb.st_size,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

libselinux/src/label_file.c static int process_file(const char *path, · line 545

Quoted 2 lines
libselinux/src/label_file.c +1 -1
-                               found_path);
+                       rc = digest_add_specfile(digest, fp, NULL,

sb.st_size, found_path);

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
libselinux/src/label_file.c +0 -5
                fclose(fp);
 
@@ -613,12 +646,7 @@ static void closef(struct selabel_handle *rec)
        for (i = 0; i < data->nspec; i++) {
                spec = &data->spec_arr[i];
                free(spec->lr.ctx_trans);
-               free(spec->lr.ctx_raw);
                regex_data_free(spec->regex);
-               if (spec->from_mmap)
-                       continue;
-               free(spec->regex_str);
-               free(spec->type_str);
        }
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Review

Quoted 2 lines
libselinux/src/label_file.c +0 -0
        for (i = 0; i < (unsigned int)data->num_stems; i++) {
(quoted diff) +0 -0

b/libselinux/src/label_file.h

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.h struct spec { · line 38

Quoted 6 lines

index 88f4294..2704906 100644

libselinux/src/label_file.h +0 -0
@@ -37,7 +37,6 @@ struct spec {
        int matches;            /* number of matching pathnames */
        int stem_id;            /* indicates which stem-compression

item */

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h struct spec { · line 39

Quoted 1 line
libselinux/src/label_file.h +0 -0
        char hasMetaChars;      /* regular expression has

meta-chars */

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h struct spec { · line 40

Quoted 1 line
libselinux/src/label_file.h +0 -1
-       char from_mmap;         /* this spec is from an mmap of the

data */

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int find_stem(struct saved_data

Quoted 4 lines
libselinux/src/label_file.h +0 -0
        size_t prefix_len;      /* length of fixed path prefix */
 };
 
@@ -255,7 +254,7 @@ static inline int find_stem(struct saved_data

*data, const char *buf,

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int find_stem(struct saved_data *data, const char *buf,

Quoted 4 lines
libselinux/src/label_file.h +0 -1
 }
 
 /* returns the index of the new stored object */
-static inline int store_stem(struct saved_data *data, char *buf,

int stem_len)

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int find_stem(struct saved_data · line 257

Quoted 1 line
libselinux/src/label_file.h +1 -0
+static inline int store_stem(struct saved_data *data, char *buf,

int stem_len, bool from_mmap)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int store_stem(struct saved_data

Quoted 4 lines
libselinux/src/label_file.h +0 -0
 {
        int num = data->num_stems;
 
@@ -271,7 +270,7 @@ static inline int store_stem(struct saved_data

*data, char *buf, int stem_len)

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

libselinux/src/label_file.h static inline int find_stem_from_spec(struct

Quoted 9 lines
libselinux/src/label_file.h +1 -1
        }
        data->stem_arr[num].len = stem_len;
        data->stem_arr[num].buf = buf;
-       data->stem_arr[num].from_mmap = 0;
+       data->stem_arr[num].from_mmap = from_mmap;
        data->num_stems++;
 
        return num;
@@ -298,7 +297,7 @@ static inline int find_stem_from_spec(struct

saved_data *data, const char *buf)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.h static inline int find_stem_from_spec(struct saved_data *data, const char *buf)

Quoted 8 lines
libselinux/src/label_file.h +1 -1
        if (!stem)
                return -1;
 
-       return store_stem(data, stem, stem_len);
+       return store_stem(data, stem, stem_len, false);
 }
 
 /* This will always check for buffer over-runs and either read the

next entry

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int next_entry(void *buf, struct

Quoted 1 line
libselinux/src/label_file.h +0 -0
@@ -317,6 +316,22 @@ static inline int next_entry(void *buf, struct

mmap_area *fp, size_t bytes)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

Quoted 4 lines
libselinux/src/label_file.h +1 -0
        return 0;
 }
 
+static inline int next_pstr(char **str, struct mmap_area *fp,

size_t len)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
libselinux/src/label_file.h +8 -0
+{
+       if (len > fp->next_len)
+               return -1;
+
+       char *tmp = (char *)fp->next_addr;
+       if (tmp[len-1] != '\0')
+               return -1;
+
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.h static inline int next_entry(void *buf, struct mmap_area *fp, size_t bytes)

Quoted 8 lines
libselinux/src/label_file.h +7 -0
+       *str = tmp;
+
+       fp->next_addr = (char *)fp->next_addr + len;
+       fp->next_len -= len;
+       return 0;
+}
+
 static inline int compile_regex(struct saved_data *data, struct

spec *spec,

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 3 lines
libselinux/src/label_file.h +0 -0
                                            const char **errbuf)
 {
@@ -375,13 +390,21 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
libselinux/src/label_file.h +9 -1
                        char *line_buf, unsigned lineno)
 {
        int items, len, rc;
-       char *regex = NULL, *type = NULL, *context = NULL;
+       union {
+               struct {
+                       char *regex;
+                       char *type;
+                       char *context;
+               };
+               char *array[3];
+       } found = { .array = { 0 } };
+
        struct saved_data *data = (struct saved_data *)rec->data;
        struct spec *spec_arr;
        unsigned int nspec = data->nspec;
        const char *errbuf = NULL;
 
Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct · line 384

Quoted 1 line
libselinux/src/label_file.h +0 -1
-       items = read_spec_entries(line_buf, &errbuf, 3, &regex,

&type, &context);

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct · line 407

Quoted 1 line
libselinux/src/label_file.h +1 -0
+       items = read_spec_entries(line_buf, &errbuf,

ARRAY_SIZE(found.array), found.array);

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 4 lines
libselinux/src/label_file.h +0 -0
        if (items < 0) {
                rc = errno;
                selinux_log(SELINUX_ERROR,
@@ -398,22 +421,17 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

libselinux/src/label_file.h static inline int process_line(struct · line 422

Quoted 2 lines
libselinux/src/label_file.h +0 -0
                COMPAT_LOG(SELINUX_ERROR,
                            "%s:  line %u is missing fields\n",

path,

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
libselinux/src/label_file.h +2 -4
                            lineno);
-               if (items == 1)
-                       free(regex);
                errno = EINVAL;
                return -1;
        } else if (items == 2) {
                /* The type field is optional. */
-               context = type;
-               type = 0;
+               found.context = found.type;
+               found.type = NULL;
        }
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 5 lines
libselinux/src/label_file.h +2 -2
-       len = get_stem_from_spec(regex);
-       if (len && prefix && strncmp(prefix, regex, len)) {
+       len = get_stem_from_spec(found.regex);
+       if (len && prefix && strncmp(prefix, found.regex, len)) {
                /* Stem of regex does not match requested prefix,

discard. */

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 7 lines
libselinux/src/label_file.h +0 -3
-               free(regex);
-               free(type);
-               free(context);
                return 0;
        }
 
@@ -424,13 +442,13 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_file.h +1 -2
        spec_arr = data->spec_arr;
 
        /* process and store the specification in spec. */
-       spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
-       spec_arr[nspec].regex_str = regex;
+       spec_arr[nspec].stem_id = find_stem_from_spec(data,

found.regex);

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 11 lines
libselinux/src/label_file.h +3 -2
+       spec_arr[nspec].regex_str = found.regex;
 
-       spec_arr[nspec].type_str = type;
+       spec_arr[nspec].type_str = found.type;
        spec_arr[nspec].mode = 0;
 
-       spec_arr[nspec].lr.ctx_raw = context;
+       spec_arr[nspec].lr.ctx_raw = found.context;
 
        /*
         * bump data->nspecs to cause closef() to cover it in its

free

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct

Quoted 1 line
libselinux/src/label_file.h +0 -0
@@ -442,18 +460,18 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_file.h static inline int process_line(struct · line 460

Quoted 1 line
libselinux/src/label_file.h +0 -0
                        && compile_regex(data, &spec_arr[nspec],

&errbuf)) {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

libselinux/src/label_file.h static inline int process_line(struct · line 462

Quoted 2 lines
libselinux/src/label_file.h +0 -0
                COMPAT_LOG(SELINUX_ERROR,
                           "%s:  line %u has invalid regex %s:

%s\n",

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
libselinux/src/label_file.h +1 -1
-                          path, lineno, regex, errbuf);
+                          path, lineno, found.regex, errbuf);
                errno = EINVAL;
                return -1;
        }
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 8 lines
libselinux/src/label_file.h +2 -2
-       if (type) {
-               mode_t mode = string_to_mode(type);
+       if (found.type) {
+               mode_t mode = string_to_mode(found.type);
 
                if (mode == (mode_t)-1) {
                        COMPAT_LOG(SELINUX_ERROR,
                                   "%s:  line %u has invalid file

type %s\n",

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 6 lines
libselinux/src/label_file.h +1 -1
-                                  path, lineno, type);
+                                  path, lineno, found.type);
                        errno = EINVAL;
                        return -1;
                }
@@ -464,7 +482,7 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_file.h +1 -1
         * any meta characters in the RE */
        spec_hasMetaChars(&spec_arr[nspec]);
 
-       if (strcmp(context, "<<none>>") && rec->validating)
+       if (strcmp(found.context, "<<none>>") && rec->validating)
                compat_validate(rec, &spec_arr[nspec].lr, path,

lineno);

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
libselinux/src/label_file.h +0 -0
        return 0;
(quoted diff) +0 -0

b/libselinux/src/label_internal.h

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

index aa48fff..e40d68b 100644

libselinux/src/label_internal.h +0 -0
@@ -136,10 +136,11 @@ compat_validate(struct selabel_handle *rec,
                struct selabel_lookup_rec *contexts,
                const char *path, unsigned lineno) hidden;
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_internal.h compat_validate(struct selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_internal.h +1 -1
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 /*
  * The read_spec_entries function may be used to
  * replace sscanf to read entries from spec files.
  */
-extern int read_spec_entries(char *line_buf, const char **errbuf,

int num_args, ...);

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_internal.h compat_validate(struct selabel_handle *rec, · line 144

Quoted 1 line
libselinux/src/label_internal.h +1 -0
+extern int hidden read_spec_entries(char *line_buf, const char

**errbuf, unsigned int num, char *found[]);

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
libselinux/src/label_internal.h +0 -0
 #endif                         /* _SELABEL_INTERNAL_H_ */
(quoted diff) +0 -0

b/libselinux/src/label_support.c

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

index 26f9ef1..3f5728d 100644

libselinux/src/label_support.c +0 -0
@@ -17,39 +17,40 @@
  * Read an entry from a spec file (e.g. file_contexts)
  * entry - Buffer to allocate for the entry.
  * ptr - current location of the line to be processed.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_support.c

Quoted 8 lines
libselinux/src/label_support.c +2 -4
- * returns  - 0 on success and *entry is set to be a null
- *            terminated value. On Error it returns -1 and
- *            errno will be set.
+ * returns  - a pointer to the begining of the string.
+ *            that is the
  *
  */
-static inline int read_spec_entry(char **entry, char **ptr, int

*len, const char **errbuf)

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_support.c line 24

Quoted 1 line
libselinux/src/label_support.c +1 -0
+static inline char *read_spec_entry(char **current, const char

**errbuf)

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
libselinux/src/label_support.c +20 -18
 {
-       *entry = NULL;
-       char *tmp_buf = NULL;
-
-       while (isspace(**ptr) && **ptr != '\0')
-               (*ptr)++;
+       if(!**current) {
+               (*current)++;
+               return NULL;
+       }
 
-       tmp_buf = *ptr;
-       *len = 0;
+       while (isspace(**current) && **current != '\0')
+               (*current)++;
 
-       while (!isspace(**ptr) && **ptr != '\0') {
-               if (!isascii(**ptr)) {
+       char *start = *current;
+       while (!isspace(**current) && **current != '\0') {
+               if (!isascii(**current)) {
                        errno = EINVAL;
                        *errbuf = "Non-ASCII characters found";
-                       return -1;
+                       return NULL;
                }
-               (*ptr)++;
-               (*len)++;
+               (*current)++;
        }
 
-       if (*len) {
-               *entry = strndup(tmp_buf, *len);
-               if (!*entry)
-                       return -1;
-       }
+       char *end = *current;
 
-       return 0;
+       if (start == end)
+               return NULL;
+
+       if (*end != '\0') {
+               *end = '\0';
+               (*current)++;
+       }
+       return start;
 }
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

libselinux/src/label_support.c static inline int read_spec_entry(char **entry,

Quoted 2 lines
libselinux/src/label_support.c +0 -0
 /*
@@ -63,15 +64,13 @@ static inline int read_spec_entry(char **entry,

char **ptr, int *len, const char

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, · line 64

Quoted 1 line
libselinux/src/label_support.c +0 -0
  * This function calls read_spec_entry() to do the actual string

processing.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, char **ptr, int *len, const char

Quoted 3 lines
libselinux/src/label_support.c +0 -1
  * As such, can return anything from that function as well.
  */
-int hidden read_spec_entries(char *line_buf, const char **errbuf,

int num_args, ...)

Quoted 1 line
Quoted 1 line
Quoted 1 line

libselinux/src/label_support.c static inline int read_spec_entry(char **entry, · line 67

Quoted 1 line
libselinux/src/label_support.c +1 -0
+int hidden read_spec_entries(char *line_buf, const char **errbuf,

unsigned int num, char *found[])

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
libselinux/src/label_support.c +2 -4
 {
-       char **spec_entry, *buf_p;
-       int len, rc, items, entry_len = 0;
-       va_list ap;
-
+       char*buf_p;
+       unsigned int items = 0;
        *errbuf = NULL;
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_support.c int hidden read_spec_entries(char *line_buf,

Quoted 6 lines
libselinux/src/label_support.c +1 -1
-       len = strlen(line_buf);
+       size_t len = strlen(line_buf);
        if (line_buf[len - 1] == '\n')
                line_buf[len - 1] = '\0';
        else
@@ -89,26 +88,12 @@ int hidden read_spec_entries(char *line_buf,

const char **errbuf, int num_args,

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 5 lines
libselinux/src/label_support.c +0 -2
                return 0;
 
        /* Process the spec file entries */
-       va_start(ap, num_args);
-

libselinux/src/label_support.c int hidden read_spec_entries(char *line_buf, const char **errbuf, int num_args,

Quoted 10 lines
libselinux/src/label_support.c +0 -10
-       items = 0;
-       while (items < num_args) {
-               spec_entry = va_arg(ap, char **);
-
-               if (len - 1 == buf_p - line_buf) {
-                       va_end(ap);
-                       return items;
-               }
-
-               rc = read_spec_entry(spec_entry, &buf_p,

&entry_len, errbuf);

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
libselinux/src/label_support.c +5 -7
-               if (rc < 0) {
-                       va_end(ap);
-                       return rc;
-               }
-               if (entry_len)
-                       items++;
+       while (items < num) {
+               found[items] = read_spec_entry(&buf_p, errbuf);
+               if (!found[items])
+                       break;
+               items++;
        }
-       va_end(ap);
        return items;
 }
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

--
1.9.1

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

#10 lore raw
Quoted 52 lines · William Roberts
Quoted 2 lines · Jason Zaman

On 20 Sep 2016 12:50 pm, "William Roberts" <[email protected]>

Quoted 23 lines · wrote:
Quoted 23 lines · Jason Zaman
Quoted 21 lines · On 20 Sep 2016 5:47 am, <[email protected]> wrote:
Quoted 19 lines

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

Do you have the scripts that generated these stats so I can play with

it too? These stats are from android right? Do you also have a comparison for refpolicy too?

Quoted 2 lines
Quoted 2 lines

For generating these I used checkfc.c from the Android tree. I used

valgrind to measure allocations and clock to measure the time in selabel_open().

Quoted 5 lines

Okay cool I'll fetch that and give it a whirl when I get time.

Quoted 2 lines
Quoted 2 lines

I haven't looked that closely yet but just realised, will this need

new perms because of the mmap? If it does, can you send a patch to refpolicy?

Quoted 2 lines
Quoted 2 lines

I'm confused, mmap is not a permission, even if it was the binary path

already was doing an mmap, so the permission would have been there. We're just making it so it always mmaps.

Quoted 1 line
Quoted 7 lines
Quoted 1 line

Yeah but mmap needs execute perms sometimes (always?). I am out so just

wanted to send an email before I forgot. If it was mmaping already then there is nothing to worry about :).

Mmap would only need execute if you attempted to set the prot bits to execute it use mprotect to change the mapping. Then things like execmod might come I to play if the mapping was ever writable.

The only case where mmap without PROT_EXEC would require execute would be if the process has READ_IMPLIES_EXEC set in its personality. Typically only for programs with the executable stack flag set.

Anyway, it is already mmap'ing file_contexts.bin so there shouldn't be an issue here.

#11 lore raw
In reply to [email protected]
Quoted 63 lines · On 09/19/2016 05:45 PM, [email protected] wrote:

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140 +++++++++++++++++++-------------
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label_file.c +9 -2
index 7156825..4dc440e 100644
@@ -96,43 +96,64 @@ static int nodups_specs(struct saved_data *data, const char *path)
 	return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-			     struct selabel_handle *rec, const char *path)
+static inline struct saved_data *rec_to_data(struct selabel_handle *rec)
+{
+	return (struct saved_data *)rec->data;
+}
+
+static char *mmap_area_get_line(struct mmap_area *area)
+{
+	size_t len = area->next_len;
+	if (!len)

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path) · line 117

Quoted 10 lines
libselinux/src/label_file.c +10 -0
+		return NULL;
+
+	char *start = area->next_addr;
+	char *end = memchr(start, '\n', len);
+
+	/* the file may not end with a newline */
+	if (!end)
+		end = (char *)area->next_addr + len - 1;
+
+	*end = '\0';

Couldn't this clobber the last character in the file if the file does not end with a newline?

Quoted 10 lines
libselinux/src/label_file.c +10 -0
+	/* len includes null byte */
+	len = end - start;
+
+	area->next_len -= len + 1;
+	area->next_addr = ++end;
+	return start;
+}
+
+static int process_text_file(const char *path, const char *prefix,
+		struct selabel_handle *rec)

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path) · line 135

Quoted 10 lines
libselinux/src/label_file.c +5 -2
 {
 	int rc;
-	size_t line_len;
 	unsigned int lineno = 0;
-	char *line_buf = NULL;
+	char *line_buf;
+	struct mmap_area *areas = rec_to_data(rec)->mmap_areas;
+
+	/* mmap_area_get_line() and process_line() require mutable string pointers */
+	rc = mprotect(areas->addr, areas->len, PROT_READ | PROT_WRITE);

Just map it with PROT_READ|PROT_WRITE in the beginning.

#12 lore raw
Quoted 16 lines

-----Original Message-----
From: Stephen Smalley [mailto:[email protected]]
Sent: Tuesday, September 20, 2016 5:56 AM
To: Roberts, William C <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC] mmap file_contexts and property_contexts:

Quoted 6 lines · On 09/19/2016 05:45 PM, [email protected] wrote:

From: William Roberts <[email protected]>

THIS IS WIP...

Rather than using stdio and making copies, just mmap the files and use the pointers in place. The affect of this change, is that text file

s/affect/effect

Quoted 61 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const

Quoted 43 lines

load time is now faster than binary load time by 4.7% when testing with a file_contexts file from the Android tree. Note that the Android doesn't use monstrous regexs.

Times are the average of 3 runs.

BEFORE:
Text file allocs: 114803
Text file load time: 0.266101
Bin file allocs: 93073
Bin file load time: 0.248757667

AFTER:
Text file allocs: 103933
Text file load time: 0.236192667
Bin file allocs: 87645
Bin file load time: .247607333

THINGS TO DO:
1. What's arm performance like?
2. What interfaces to backends are busted by this (if any)?
3. Test Android Properties
4. Im pretty sure this breaks sefcontext_compile, fix.
5. Test with PCRE2 enabled.
6. Spell check this message!
7. Run checkpatch

---
 libselinux/src/label.c                  |   2 -
 libselinux/src/label_android_property.c |  22 ++---
 libselinux/src/label_file.c             | 140 +++++++++++++++++++-------------
 libselinux/src/label_file.h             |  66 +++++++++------
 libselinux/src/label_internal.h         |   3 +-
 libselinux/src/label_support.c          |  79 ++++++++----------
 6 files changed, 172 insertions(+), 140 deletions(-)
libselinux/src/label_file.c +0 -0
index 7156825..4dc440e 100644
@@ -96,43 +96,64 @@ static int nodups_specs(struct saved_data *data, const

char *path)

Quoted 17 lines
libselinux/src/label_file.c +12 -2
 	return rc;
 }
 
-static int process_text_file(FILE *fp, const char *prefix,
-			     struct selabel_handle *rec, const char *path)
+static inline struct saved_data *rec_to_data(struct selabel_handle
+*rec) {
+	return (struct saved_data *)rec->data; }
+
+static char *mmap_area_get_line(struct mmap_area *area) {
+	size_t len = area->next_len;
+	if (!len)
+		return NULL;
+
+	char *start = area->next_addr;
+	char *end = memchr(start, '\n', len);
+
Quoted 8 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path) · line 117

Quoted 5 lines
libselinux/src/label_file.c +5 -0
+	/* the file may not end with a newline */
+	if (!end)
+		end = (char *)area->next_addr + len - 1;
+
+	*end = '\0';

Couldn't this clobber the last character in the file if the file does not end with a newline?

Yeah I guess I really can't handle this case without increasing the mapping size or allocating A new buffer to copy into. Considering, that the only time a file not ending with a newline was my own stupid mistake, I am not too worried about the not ending newline, especially considering that the Android build process handles it if it's missing.

Perhaps I could always add a terminating \0 to the mapping by growing it by one during mmap And explicitly setting the byte.

Quoted 21 lines
Quoted 10 lines
libselinux/src/label_file.c +9 -0
+	/* len includes null byte */
+	len = end - start;
+
+	area->next_len -= len + 1;
+	area->next_addr = ++end;
+	return start;
+}
+
+static int process_text_file(const char *path, const char *prefix,

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path)

Quoted 10 lines
libselinux/src/label_file.c +5 -2
+		struct selabel_handle *rec)
 {
 	int rc;
-	size_t line_len;
 	unsigned int lineno = 0;
-	char *line_buf = NULL;
+	char *line_buf;
+	struct mmap_area *areas = rec_to_data(rec)->mmap_areas;
+
+	/* mmap_area_get_line() and process_line() require mutable string

pointers */

Quoted 3 lines

libselinux/src/label_file.c static int nodups_specs(struct saved_data *data, const char *path) · line 135

Quoted 1 line
libselinux/src/label_file.c +1 -0
+	rc = mprotect(areas->addr, areas->len, PROT_READ | PROT_WRITE);

Just map it with PROT_READ|PROT_WRITE in the beginning.

Yep.