Skip to contentSkip to conversation

fix memory leaks and uninitialized jump

PATCH Superseded [email protected] 3 participants 6 messages Oct 26, 2015 – Oct 27, 2015 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix memory leaks and uninitialized jump

From: William Roberts <[email protected]>

    Some error's were reported by valgrind (below) fix them. The test
    cases on which these leaks were detected:

    1. properly formed file_contexts file.
    2. malformed file_contexts file, unknown type.
    3. malformed file_contexts file, type that fails on validate callback.
    4. malformed file_contexts file, invalid regex.
    5. malformed file_contexts file, invalid mode.

    ==3819== Conditional jump or move depends on uninitialised value(s)
    ==3819==    at 0x12A682: closef (label_file.c:577)
    ==3819==    by 0x12A196: selabel_close (label.c:163)
    ==3819==    by 0x10A2FD: cleanup (checkfc.c:218)
    ==3819==    by 0x5089258: __run_exit_handlers (exit.c:82)
    ==3819==    by 0x50892A4: exit (exit.c:104)
    ==3819==    by 0x10A231: main (checkfc.c:361)
    ==3819==  Uninitialised value was created by a heap allocation
    ==3819==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==3819==    by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==3819==    by 0x12BB31: process_file (label_file.h:273)
    ==3819==    by 0x12A2BA: selabel_file_init (label_file.c:522)
    ==3819==    by 0x12A0BB: selabel_open (label.c:88)
    ==3819==    by 0x10A038: main (checkfc.c:292)
    ==3819==
    ==3819==
    ==3819== HEAP SUMMARY:
    ==3819==     in use at exit: 729 bytes in 19 blocks
    ==3819==   total heap usage: 21,126 allocs, 21,107 frees, 923,854 bytes allocated
    ==3819==
    ==3819== 81 bytes in 1 blocks are definitely lost in loss record 1 of 2
    ==3819==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==3819==    by 0x50D5839: strdup (strdup.c:42)
    ==3819==    by 0x12A2A6: selabel_file_init (label_file.c:517)
    ==3819==    by 0x12A0BB: selabel_open (label.c:88)
    ==3819==    by 0x10A038: main (checkfc.c:292)
    ==3819==

    ==4238== 40 bytes in 1 blocks are definitely lost in loss record 1 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==4238==    by 0x12A1D2: selabel_file_init (label_file.c:886)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
    ==4238==
    ==4238== 81 bytes in 1 blocks are definitely lost in loss record 2 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==4238==    by 0x50D5839: strdup (strdup.c:42)
    ==4238==    by 0x12A2A6: selabel_file_init (label_file.c:517)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
    ==4238==
    ==4238== 386 bytes in 24 blocks are definitely lost in loss record 3 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==4238==    by 0x50D5889: strndup (strndup.c:45)
    ==4238==    by 0x12CDDF: read_spec_entries (label_support.c:37)
    ==4238==    by 0x12B72D: process_file (label_file.h:392)
    ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
    ==4238==
    ==4238== 648 bytes in 18 blocks are definitely lost in loss record 4 of 6
    ==4238==    at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==4238==    by 0x117C9B: avtab_insert_node (avtab.c:105)
    ==4238==    by 0x117C10: avtab_insert (avtab.c:163)
    ==4238==    by 0x11880A: avtab_read_item (avtab.c:566)
    ==4238==    by 0x118BD3: avtab_read (avtab.c:600)
    ==4238==    by 0x125BDD: policydb_read (policydb.c:3854)
    ==4238==    by 0x109F87: main (checkfc.c:273)
    ==4238==
    ==4238== 1,095 bytes in 12 blocks are definitely lost in loss record 5 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==4238==    by 0x12D8D1: pcre_compile2 (pcre_compile.c:9217)
    ==4238==    by 0x12B239: compile_regex (label_file.h:357)
    ==4238==    by 0x12B9C7: process_file (label_file.h:429)
    ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
    ==4238==
    ==4238== 1,296 bytes in 12 blocks are definitely lost in loss record 6 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==4238==    by 0x13EBE5: pcre_study (pcre_study.c:1565)
    ==4238==    by 0x12B25D: compile_regex (label_file.h:366)
    ==4238==    by 0x12B9C7: process_file (label_file.h:429)
    ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
---
 libselinux/src/label.c      |  1 +
 libselinux/src/label_file.c |  5 ++++-
 libselinux/src/label_file.h | 22 ++++++++++++++--------
 3 files changed, 19 insertions(+), 9 deletions(-)
libselinux/src/label.c +1 -0
index c656fda..963bfcb 100644
@@ -338,6 +338,7 @@ struct selabel_handle *selabel_open(unsigned int backend,
 	rec->digest = selabel_is_digest_set(opts, nopts, rec->digest);
 
 	if ((*initfuncs[backend])(rec, opts, nopts)) {
+		free(rec->spec_file);
 		free(rec);
 		rec = NULL;
 	}
libselinux/src/label_file.c +4 -1
index 1a0c15f..071d902 100644
@@ -507,6 +507,8 @@ out:
 	return rc;
 }
 
+static void closef(struct selabel_handle *rec);
+
 static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 		unsigned n)
 {
@@ -580,7 +582,8 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 
 finish:
 	if (status)
-		free(data->spec_arr);
+		closef(rec);
+
 	return status;
 }
 
libselinux/src/label_file.h +14 -8
index 2492826..a85b9bd 100644
@@ -278,6 +278,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->num_stems++;
 
 	return num;
@@ -425,6 +426,19 @@ static inline int process_line(struct selabel_handle *rec,
 	/* process and store the specification in spec. */
 	spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
 	spec_arr[nspec].regex_str = regex;
+
+	/* Convert the type string to a mode format */
+	spec_arr[nspec].type_str = type;
+	spec_arr[nspec].mode = 0;
+
+	spec_arr[nspec].lr.ctx_raw = context;
+
+	/*
+	 * bump data->nspecs to cause closef() to cover it in its free
+	 * but do not bump nspec since it's used below.
+	 */
+	data->nspec++;
+
 	if (rec->validating &&
 			    compile_regex(data, &spec_arr[nspec], &errbuf)) {
 		COMPAT_LOG(SELINUX_ERROR,
@@ -435,9 +449,6 @@ static inline int process_line(struct selabel_handle *rec,
 		return -1;
 	}
 
-	/* Convert the type string to a mode format */
-	spec_arr[nspec].type_str = type;
-	spec_arr[nspec].mode = 0;
 	if (type) {
 		mode_t mode = string_to_mode(type);
 
@@ -445,14 +456,11 @@ static inline int process_line(struct selabel_handle *rec,
 			COMPAT_LOG(SELINUX_ERROR,
 				   "%s:  line %u has invalid file type %s\n",
 				   path, lineno, type);
-			errno = EINVAL;
 			return -1;
 		}
 		spec_arr[nspec].mode = mode;
 	}
 
-	spec_arr[nspec].lr.ctx_raw = context;
-
 	/* Determine if specification has
 	 * any meta characters in the RE */
 	spec_hasMetaChars(&spec_arr[nspec]);
@@ -460,8 +468,6 @@ static inline int process_line(struct selabel_handle *rec,
 	if (strcmp(context, "<<none>>") && rec->validating)
 		compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
-	data->nspec = ++nspec;
-
 	return 0;
 }
 
Signature · 1 line
1.9.1
#1 lore raw

Shouldn't;
compat_validate(rec, &spec_arr[nspec].lr, path, lineno);

in process_line() cause a failure? Right now the return code is being ignored.

Bill

Quoted 217 lines

-----Original Message-----
From: Roberts, William C
Sent: Monday, October 26, 2015 11:33 AM
To: [email protected]
Cc: [email protected]; Roberts, William C
Subject: [PATCH] fix memory leaks and uninitialized jump

From: William Roberts <[email protected]>

    Some error's were reported by valgrind (below) fix them. The test
    cases on which these leaks were detected:

    1. properly formed file_contexts file.
    2. malformed file_contexts file, unknown type.
    3. malformed file_contexts file, type that fails on validate callback.
    4. malformed file_contexts file, invalid regex.
    5. malformed file_contexts file, invalid mode.

    ==3819== Conditional jump or move depends on uninitialised value(s)
    ==3819==    at 0x12A682: closef (label_file.c:577)
    ==3819==    by 0x12A196: selabel_close (label.c:163)
    ==3819==    by 0x10A2FD: cleanup (checkfc.c:218)
    ==3819==    by 0x5089258: __run_exit_handlers (exit.c:82)
    ==3819==    by 0x50892A4: exit (exit.c:104)
    ==3819==    by 0x10A231: main (checkfc.c:361)
    ==3819==  Uninitialised value was created by a heap allocation
    ==3819==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
    ==3819==    by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
    ==3819==    by 0x12BB31: process_file (label_file.h:273)
    ==3819==    by 0x12A2BA: selabel_file_init (label_file.c:522)
    ==3819==    by 0x12A0BB: selabel_open (label.c:88)
    ==3819==    by 0x10A038: main (checkfc.c:292)
    ==3819==
    ==3819==
    ==3819== HEAP SUMMARY:
    ==3819==     in use at exit: 729 bytes in 19 blocks
    ==3819==   total heap usage: 21,126 allocs, 21,107 frees, 923,854 bytes allocated
    ==3819==
    ==3819== 81 bytes in 1 blocks are definitely lost in loss record 1 of 2
    ==3819==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
    ==3819==    by 0x50D5839: strdup (strdup.c:42)
    ==3819==    by 0x12A2A6: selabel_file_init (label_file.c:517)
    ==3819==    by 0x12A0BB: selabel_open (label.c:88)
    ==3819==    by 0x10A038: main (checkfc.c:292)
    ==3819==

    ==4238== 40 bytes in 1 blocks are definitely lost in loss record 1 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
    ==4238==    by 0x12A1D2: selabel_file_init (label_file.c:886)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
    ==4238==
    ==4238== 81 bytes in 1 blocks are definitely lost in loss record 2 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
    ==4238==    by 0x50D5839: strdup (strdup.c:42)
    ==4238==    by 0x12A2A6: selabel_file_init (label_file.c:517)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
    ==4238==
    ==4238== 386 bytes in 24 blocks are definitely lost in loss record 3 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
    ==4238==    by 0x50D5889: strndup (strndup.c:45)
    ==4238==    by 0x12CDDF: read_spec_entries (label_support.c:37)
    ==4238==    by 0x12B72D: process_file (label_file.h:392)
    ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
    ==4238==
    ==4238== 648 bytes in 18 blocks are definitely lost in loss record 4 of 6
    ==4238==    at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
    ==4238==    by 0x117C9B: avtab_insert_node (avtab.c:105)
    ==4238==    by 0x117C10: avtab_insert (avtab.c:163)
    ==4238==    by 0x11880A: avtab_read_item (avtab.c:566)
    ==4238==    by 0x118BD3: avtab_read (avtab.c:600)
    ==4238==    by 0x125BDD: policydb_read (policydb.c:3854)
    ==4238==    by 0x109F87: main (checkfc.c:273)
    ==4238==
    ==4238== 1,095 bytes in 12 blocks are definitely lost in loss record 5 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
    ==4238==    by 0x12D8D1: pcre_compile2 (pcre_compile.c:9217)
    ==4238==    by 0x12B239: compile_regex (label_file.h:357)
    ==4238==    by 0x12B9C7: process_file (label_file.h:429)
    ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
    ==4238==
    ==4238== 1,296 bytes in 12 blocks are definitely lost in loss record 6 of 6
    ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
    ==4238==    by 0x13EBE5: pcre_study (pcre_study.c:1565)
    ==4238==    by 0x12B25D: compile_regex (label_file.h:366)
    ==4238==    by 0x12B9C7: process_file (label_file.h:429)
    ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
    ==4238==    by 0x12A0BB: selabel_open (label.c:88)
    ==4238==    by 0x10A038: main (checkfc.c:292)
---
 libselinux/src/label.c      |  1 +
 libselinux/src/label_file.c |  5 ++++-
 libselinux/src/label_file.h | 22 ++++++++++++++--------
 3 files changed, 19 insertions(+), 9 deletions(-)
libselinux/src/label.c index c656fda..963bfcb renamed from libselinux/src/label.c +0 -0

100644

libselinux/src/label.c +1 -0
@@ -338,6 +338,7 @@ struct selabel_handle *selabel_open(unsigned int
 backend,
 	rec->digest = selabel_is_digest_set(opts, nopts, rec->digest);
 
 	if ((*initfuncs[backend])(rec, opts, nopts)) {
+		free(rec->spec_file);
 		free(rec);
 		rec = NULL;
libselinux/src/label.c +0 -0
 	}
libselinux/src/label_file.c index renamed from libselinux/src/label_file.c +0 -0

1a0c15f..071d902 100644

libselinux/src/label_file.c +4 -1
@@ -507,6 +507,8 @@ out:
 	return rc;
 }
 
+static void closef(struct selabel_handle *rec);
+
 static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 		unsigned n)
 {
@@ -580,7 +582,8 @@ static int init(struct selabel_handle *rec, const struct
 selinux_opt *opts,
 
 finish:
 	if (status)
-		free(data->spec_arr);
+		closef(rec);
+
 	return status;
 }
libselinux/src/label_file.h index renamed from libselinux/src/label_file.h +0 -0

2492826..a85b9bd 100644

libselinux/src/label_file.h +1 -0
@@ -278,6 +278,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->num_stems++;
 
libselinux/src/label_file.h +13 -9
 	return num;
@@ -425,6 +426,19 @@ static inline int process_line(struct selabel_handle *rec,
 	/* process and store the specification in spec. */
 	spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
 	spec_arr[nspec].regex_str = regex;
+
+	/* Convert the type string to a mode format */
+	spec_arr[nspec].type_str = type;
+	spec_arr[nspec].mode = 0;
+
+	spec_arr[nspec].lr.ctx_raw = context;
+
+	/*
+	 * bump data->nspecs to cause closef() to cover it in its free
+	 * but do not bump nspec since it's used below.
+	 */
+	data->nspec++;
+
 	if (rec->validating &&
 			    compile_regex(data, &spec_arr[nspec], &errbuf)) {
 		COMPAT_LOG(SELINUX_ERROR,
@@ -435,9 +449,6 @@ static inline int process_line(struct selabel_handle *rec,
 		return -1;
 	}
 
-	/* Convert the type string to a mode format */
-	spec_arr[nspec].type_str = type;
-	spec_arr[nspec].mode = 0;
 	if (type) {
 		mode_t mode = string_to_mode(type);
 
@@ -445,14 +456,11 @@ static inline int process_line(struct selabel_handle *rec,
 			COMPAT_LOG(SELINUX_ERROR,
 				   "%s:  line %u has invalid file type %s\n",
 				   path, lineno, type);
-			errno = EINVAL;
 			return -1;
 		}
 		spec_arr[nspec].mode = mode;
 	}
 
-	spec_arr[nspec].lr.ctx_raw = context;
-
 	/* Determine if specification has
 	 * any meta characters in the RE */
 	spec_hasMetaChars(&spec_arr[nspec]);
@@ -460,8 +468,6 @@ static inline int process_line(struct selabel_handle *rec,
 	if (strcmp(context, "<<none>>") && rec->validating)
 		compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
-	data->nspec = ++nspec;
-
 	return 0;
 }
 
--

1.9.1

#2 lore raw
Quoted 4 lines · William C
Shouldn't;
   compat_validate(rec, &spec_arr[nspec].lr, path, lineno);

in process_line() cause a failure? Right now the return code is being ignored.

I think it is historical. Originally we had it bail on error. Red Hat had problems with that because they would ship a file_contexts file that had an invalid context or the user would have one bad entry in file_contexts.local or a local module (maybe due to a policy change that removed a type, so it might have been valid originally and then became invalid), and suddenly restorecon/setfiles/rpm/... couldn't label anything at all, leading to unlabeled files and possibly an unbootable system. So then we took the error handling to the validate callback, where setfiles or sefcontext_compile can make it a fatal error but you can still label most files (the ones that match valid entries) when they get installed via rpm or using restorecon.

Quoted 228 lines

Bill

Quoted 216 lines

-----Original Message-----
From: Roberts, William C
Sent: Monday, October 26, 2015 11:33 AM
To: [email protected]
Cc: [email protected]; Roberts, William C
Subject: [PATCH] fix memory leaks and uninitialized jump

From: William Roberts <[email protected]>

     Some error's were reported by valgrind (below) fix them. The test
     cases on which these leaks were detected:

     1. properly formed file_contexts file.
     2. malformed file_contexts file, unknown type.
     3. malformed file_contexts file, type that fails on validate callback.
     4. malformed file_contexts file, invalid regex.
     5. malformed file_contexts file, invalid mode.

     ==3819== Conditional jump or move depends on uninitialised value(s)
     ==3819==    at 0x12A682: closef (label_file.c:577)
     ==3819==    by 0x12A196: selabel_close (label.c:163)
     ==3819==    by 0x10A2FD: cleanup (checkfc.c:218)
     ==3819==    by 0x5089258: __run_exit_handlers (exit.c:82)
     ==3819==    by 0x50892A4: exit (exit.c:104)
     ==3819==    by 0x10A231: main (checkfc.c:361)
     ==3819==  Uninitialised value was created by a heap allocation
     ==3819==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
     ==3819==    by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
     ==3819==    by 0x12BB31: process_file (label_file.h:273)
     ==3819==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==3819==    by 0x12A0BB: selabel_open (label.c:88)
     ==3819==    by 0x10A038: main (checkfc.c:292)
     ==3819==
     ==3819==
     ==3819== HEAP SUMMARY:
     ==3819==     in use at exit: 729 bytes in 19 blocks
     ==3819==   total heap usage: 21,126 allocs, 21,107 frees, 923,854 bytes allocated
     ==3819==
     ==3819== 81 bytes in 1 blocks are definitely lost in loss record 1 of 2
     ==3819==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
     ==3819==    by 0x50D5839: strdup (strdup.c:42)
     ==3819==    by 0x12A2A6: selabel_file_init (label_file.c:517)
     ==3819==    by 0x12A0BB: selabel_open (label.c:88)
     ==3819==    by 0x10A038: main (checkfc.c:292)
     ==3819==

     ==4238== 40 bytes in 1 blocks are definitely lost in loss record 1 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
     ==4238==    by 0x12A1D2: selabel_file_init (label_file.c:886)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 81 bytes in 1 blocks are definitely lost in loss record 2 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
     ==4238==    by 0x50D5839: strdup (strdup.c:42)
     ==4238==    by 0x12A2A6: selabel_file_init (label_file.c:517)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 386 bytes in 24 blocks are definitely lost in loss record 3 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
     ==4238==    by 0x50D5889: strndup (strndup.c:45)
     ==4238==    by 0x12CDDF: read_spec_entries (label_support.c:37)
     ==4238==    by 0x12B72D: process_file (label_file.h:392)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 648 bytes in 18 blocks are definitely lost in loss record 4 of 6
     ==4238==    at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
     ==4238==    by 0x117C9B: avtab_insert_node (avtab.c:105)
     ==4238==    by 0x117C10: avtab_insert (avtab.c:163)
     ==4238==    by 0x11880A: avtab_read_item (avtab.c:566)
     ==4238==    by 0x118BD3: avtab_read (avtab.c:600)
     ==4238==    by 0x125BDD: policydb_read (policydb.c:3854)
     ==4238==    by 0x109F87: main (checkfc.c:273)
     ==4238==
     ==4238== 1,095 bytes in 12 blocks are definitely lost in loss record 5 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
     ==4238==    by 0x12D8D1: pcre_compile2 (pcre_compile.c:9217)
     ==4238==    by 0x12B239: compile_regex (label_file.h:357)
     ==4238==    by 0x12B9C7: process_file (label_file.h:429)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 1,296 bytes in 12 blocks are definitely lost in loss record 6 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
     ==4238==    by 0x13EBE5: pcre_study (pcre_study.c:1565)
     ==4238==    by 0x12B25D: compile_regex (label_file.h:366)
     ==4238==    by 0x12B9C7: process_file (label_file.h:429)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
---
  libselinux/src/label.c      |  1 +
  libselinux/src/label_file.c |  5 ++++-
  libselinux/src/label_file.h | 22 ++++++++++++++--------
  3 files changed, 19 insertions(+), 9 deletions(-)
libselinux/src/label.c index c656fda..963bfcb renamed from libselinux/src/label.c +0 -0

100644

libselinux/src/label.c +1 -0
@@ -338,6 +338,7 @@ struct selabel_handle *selabel_open(unsigned int
 backend,
  	rec->digest = selabel_is_digest_set(opts, nopts, rec->digest);
 
  	if ((*initfuncs[backend])(rec, opts, nopts)) {
+		free(rec->spec_file);
  		free(rec);
  		rec = NULL;
  	}
libselinux/src/label_file.c index renamed from libselinux/src/label_file.c +0 -0

1a0c15f..071d902 100644

libselinux/src/label_file.c +4 -1
@@ -507,6 +507,8 @@ out:
  	return rc;
  }
 
+static void closef(struct selabel_handle *rec);
+
  static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
  		unsigned n)
  {
@@ -580,7 +582,8 @@ static int init(struct selabel_handle *rec, const struct
 selinux_opt *opts,
 
  finish:
  	if (status)
-		free(data->spec_arr);
+		closef(rec);
+
  	return status;
  }
libselinux/src/label_file.h index renamed from libselinux/src/label_file.h +0 -0

2492826..a85b9bd 100644

libselinux/src/label_file.h +1 -0
@@ -278,6 +278,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->num_stems++;
 

return num;

libselinux/src/label_file.h +13 -9
@@ -425,6 +426,19 @@ static inline int process_line(struct selabel_handle *rec,
  	/* process and store the specification in spec. */
  	spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
  	spec_arr[nspec].regex_str = regex;
+
+	/* Convert the type string to a mode format */
+	spec_arr[nspec].type_str = type;
+	spec_arr[nspec].mode = 0;
+
+	spec_arr[nspec].lr.ctx_raw = context;
+
+	/*
+	 * bump data->nspecs to cause closef() to cover it in its free
+	 * but do not bump nspec since it's used below.
+	 */
+	data->nspec++;
+
  	if (rec->validating &&
  			    compile_regex(data, &spec_arr[nspec], &errbuf)) {
  		COMPAT_LOG(SELINUX_ERROR,
@@ -435,9 +449,6 @@ static inline int process_line(struct selabel_handle *rec,
  		return -1;
  	}
 
-	/* Convert the type string to a mode format */
-	spec_arr[nspec].type_str = type;
-	spec_arr[nspec].mode = 0;
  	if (type) {
  		mode_t mode = string_to_mode(type);
 
@@ -445,14 +456,11 @@ static inline int process_line(struct selabel_handle *rec,
  			COMPAT_LOG(SELINUX_ERROR,
  				   "%s:  line %u has invalid file type %s\n",
  				   path, lineno, type);
-			errno = EINVAL;
  			return -1;
  		}
  		spec_arr[nspec].mode = mode;
  	}
 
-	spec_arr[nspec].lr.ctx_raw = context;
-
  	/* Determine if specification has
  	 * any meta characters in the RE */
  	spec_hasMetaChars(&spec_arr[nspec]);
@@ -460,8 +468,6 @@ static inline int process_line(struct selabel_handle *rec,
  	if (strcmp(context, "<<none>>") && rec->validating)
  		compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
-	data->nspec = ++nspec;
-
  	return 0;
  }
 
--
Quoted 1 line

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].

#3 lore raw
Quoted 7 lines · Stephen Smalley
Quoted 5 lines · William C
Shouldn't;
   compat_validate(rec, &spec_arr[nspec].lr, path, lineno);

in process_line() cause a failure? Right now the return code is being

ignored.

Quoted 3 lines

I think it is historical. Originally we had it bail on error. Red Hat

had problems with that because they would ship a file_contexts file that had an invalid context or the user would have one bad entry in file_contexts.local or a local module (maybe due to a policy change that removed a type, so it might have been valid originally and then became invalid), and suddenly restorecon/setfiles/rpm/... couldn't label anything at all, leading to unlabeled files and possibly an unbootable system. So then we took the error handling to the validate callback, where setfiles or sefcontext_compile can make it a fatal error but you can still label most files (the ones that match valid entries) when they get installed via rpm or using restorecon.

Well it would be nice to reconcile this with Android. What's the plan there? Runtime option, ifdef, etc?

What's the plan on removing backends unused on Android?

Quoted 12 lines
Quoted 12 lines

Bill

Quoted 7 lines

-----Original Message-----
From: Roberts, William C
Sent: Monday, October 26, 2015 11:33 AM
To: [email protected]
Cc: [email protected]; Roberts, William C
Subject: [PATCH] fix memory leaks and uninitialized jump

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

From: William Roberts <[email protected]>

Some error's were reported by valgrind (below) fix them. The test cases on which these leaks were detected:

1. properly formed file_contexts file.
2. malformed file_contexts file, unknown type.
3. malformed file_contexts file, type that fails on validate

callback.

Quoted 12 lines
Quoted 12 lines
Quoted 4 lines
     4. malformed file_contexts file, invalid regex.
     5. malformed file_contexts file, invalid mode.

     ==3819== Conditional jump or move depends on uninitialised value(s)
Quoted 8 lines
     ==3819==    at 0x12A682: closef (label_file.c:577)
     ==3819==    by 0x12A196: selabel_close (label.c:163)
     ==3819==    by 0x10A2FD: cleanup (checkfc.c:218)
     ==3819==    by 0x5089258: __run_exit_handlers (exit.c:82)
     ==3819==    by 0x50892A4: exit (exit.c:104)
     ==3819==    by 0x10A231: main (checkfc.c:361)
     ==3819==  Uninitialised value was created by a heap allocation
     ==3819==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

amd64-linux.so)

     ==3819==    by 0x4C2CF1F: realloc (in

/usr/lib/valgrind/vgpreload_memcheck-

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

amd64-linux.so)

     ==3819==    by 0x12BB31: process_file (label_file.h:273)
     ==3819==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==3819==    by 0x12A0BB: selabel_open (label.c:88)
     ==3819==    by 0x10A038: main (checkfc.c:292)
     ==3819==
     ==3819==
     ==3819== HEAP SUMMARY:
     ==3819==     in use at exit: 729 bytes in 19 blocks
     ==3819==   total heap usage: 21,126 allocs, 21,107 frees, 923,854

bytes allocated

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
     ==3819==
     ==3819== 81 bytes in 1 blocks are definitely lost in loss record 1

of 2

Quoted 1 line
Quoted 1 line
Quoted 1 line
     ==3819==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

amd64-linux.so)

     ==3819==    by 0x50D5839: strdup (strdup.c:42)
     ==3819==    by 0x12A2A6: selabel_file_init (label_file.c:517)
     ==3819==    by 0x12A0BB: selabel_open (label.c:88)
     ==3819==    by 0x10A038: main (checkfc.c:292)
     ==3819==

     ==4238== 40 bytes in 1 blocks are definitely lost in loss record 1

of 6

Quoted 1 line
Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

amd64-linux.so)

     ==4238==    by 0x12A1D2: selabel_file_init (label_file.c:886)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 81 bytes in 1 blocks are definitely lost in loss record 2

of 6

Quoted 1 line
Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

amd64-linux.so)

     ==4238==    by 0x50D5839: strdup (strdup.c:42)
     ==4238==    by 0x12A2A6: selabel_file_init (label_file.c:517)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 386 bytes in 24 blocks are definitely lost in loss record

3 of 6

Quoted 1 line
Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

amd64-linux.so)

     ==4238==    by 0x50D5889: strndup (strndup.c:45)
     ==4238==    by 0x12CDDF: read_spec_entries (label_support.c:37)
     ==4238==    by 0x12B72D: process_file (label_file.h:392)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 648 bytes in 18 blocks are definitely lost in loss record

4 of 6

Quoted 1 line
Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2CC70: calloc (in

/usr/lib/valgrind/vgpreload_memcheck-

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

amd64-linux.so)

     ==4238==    by 0x117C9B: avtab_insert_node (avtab.c:105)
     ==4238==    by 0x117C10: avtab_insert (avtab.c:163)
     ==4238==    by 0x11880A: avtab_read_item (avtab.c:566)
     ==4238==    by 0x118BD3: avtab_read (avtab.c:600)
     ==4238==    by 0x125BDD: policydb_read (policydb.c:3854)
     ==4238==    by 0x109F87: main (checkfc.c:273)
     ==4238==
     ==4238== 1,095 bytes in 12 blocks are definitely lost in loss

record 5 of 6

Quoted 1 line
Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

amd64-linux.so)

     ==4238==    by 0x12D8D1: pcre_compile2 (pcre_compile.c:9217)
     ==4238==    by 0x12B239: compile_regex (label_file.h:357)
     ==4238==    by 0x12B9C7: process_file (label_file.h:429)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 1,296 bytes in 12 blocks are definitely lost in loss

record 6 of 6

Quoted 1 line
Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

amd64-linux.so)

     ==4238==    by 0x13EBE5: pcre_study (pcre_study.c:1565)
     ==4238==    by 0x12B25D: compile_regex (label_file.h:366)
     ==4238==    by 0x12B9C7: process_file (label_file.h:429)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
Quoted 8 lines
Quoted 8 lines

libselinux/src/label.c index

Quoted 8 lines
---
  libselinux/src/label.c      |  1 +
  libselinux/src/label_file.c |  5 ++++-
  libselinux/src/label_file.h | 22 ++++++++++++++--------
  3 files changed, 19 insertions(+), 9 deletions(-)
libselinux/src/label.c index renamed from libselinux/src/label.c +0 -0

c656fda..963bfcb

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

100644

libselinux/src/label.c +0 -0
@@ -338,6 +338,7 @@ struct selabel_handle *selabel_open(unsigned int
 backend,
        rec->digest = selabel_is_digest_set(opts, nopts, rec->digest);
 
Quoted 6 lines
Quoted 6 lines

libselinux/src/label_file.c

Quoted 6 lines
libselinux/src/label.c +1 -0
        if ((*initfuncs[backend])(rec, opts, nopts)) {
+               free(rec->spec_file);
                free(rec);
                rec = NULL;
        }
libselinux/src/label_file.c +0 -0

index

Quoted 10 lines
Quoted 10 lines

libselinux/src/label_file.c out: · line 512

Quoted 10 lines

1a0c15f..071d902 100644

libselinux/src/label_file.c +2 -0
@@ -507,6 +507,8 @@ out:
        return rc;
  }
 
+static void closef(struct selabel_handle *rec);
+
  static int init(struct selabel_handle *rec, const struct selinux_opt

*opts,

Quoted 3 lines
Quoted 3 lines

libselinux/src/label_file.c static int init(struct selabel_handle *rec, const

Quoted 3 lines
libselinux/src/label_file.c +0 -0
                unsigned n)
  {
@@ -580,7 +582,8 @@ static int init(struct selabel_handle *rec, const

struct

Quoted 11 lines
Quoted 11 lines

libselinux/src/label_file.h

Quoted 11 lines
libselinux/src/label_file.c +2 -1
 selinux_opt *opts,
 
  finish:
        if (status)
-               free(data->spec_arr);
+               closef(rec);
+
        return status;
  }
libselinux/src/label_file.h +0 -0

index

Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines

2492826..a85b9bd 100644

libselinux/src/label_file.h +0 -0
@@ -278,6 +278,7 @@ static inline int store_stem(struct saved_data

*data, char

Quoted 9 lines
Quoted 9 lines

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

Quoted 9 lines
libselinux/src/label_file.h +1 -0
 *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->num_stems++;
 
        return num;
libselinux/src/label_file.h +0 -0
@@ -425,6 +426,19 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
libselinux/src/label_file.h +7 -0
        /* process and store the specification in spec. */
        spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
        spec_arr[nspec].regex_str = regex;
+
+       /* Convert the type string to a mode format */
+       spec_arr[nspec].type_str = type;
+       spec_arr[nspec].mode = 0;
+
+       spec_arr[nspec].lr.ctx_raw = context;
+
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 +6 -0
+       /*
+        * bump data->nspecs to cause closef() to cover it in its free
+        * but do not bump nspec since it's used below.
+        */
+       data->nspec++;
+
        if (rec->validating &&
                            compile_regex(data, &spec_arr[nspec],

&errbuf)) {

Quoted 2 lines
Quoted 2 lines

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

Quoted 2 lines
libselinux/src/label_file.h +0 -0
                COMPAT_LOG(SELINUX_ERROR,
@@ -435,9 +449,6 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 10 lines
Quoted 10 lines

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

Quoted 10 lines
libselinux/src/label_file.h +0 -3
                return -1;
        }
 
-       /* Convert the type string to a mode format */
-       spec_arr[nspec].type_str = type;
-       spec_arr[nspec].mode = 0;
        if (type) {
                mode_t mode = string_to_mode(type);
 
@@ -445,14 +456,11 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 2 lines
Quoted 2 lines

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

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

%s\n",

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
libselinux/src/label_file.h +0 -1
                                   path, lineno, type);
-                       errno = EINVAL;
                        return -1;
                }
                spec_arr[nspec].mode = mode;
        }
 
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 +0 -2
-       spec_arr[nspec].lr.ctx_raw = context;
-
        /* Determine if specification has
         * any meta characters in the RE */
        spec_hasMetaChars(&spec_arr[nspec]);
@@ -460,8 +468,6 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
libselinux/src/label_file.h +0 -3
        if (strcmp(context, "<<none>>") && rec->validating)
                compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
-       data->nspec = ++nspec;
-
        return 0;
  }
 
--

1.9.1

Quoted 5 lines
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].

Quoted 7 lines
Quoted 1 line

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

[email protected].

#4 lore raw
In reply to [email protected]

Subject: [PATCH] libselinux: label_file: fix memory leaks and uninitialized jump

Quoted 4 lines · On 10/26/2015 02:32 PM, [email protected] wrote:

From: William Roberts <[email protected]>

Some error's were reported by valgrind (below) fix them. The test cases on which these leaks were detected:

Why do you have leading space in the subject line and every line of the description?

Quoted 139 lines
     1. properly formed file_contexts file.
     2. malformed file_contexts file, unknown type.
     3. malformed file_contexts file, type that fails on validate callback.
     4. malformed file_contexts file, invalid regex.
     5. malformed file_contexts file, invalid mode.

     ==3819== Conditional jump or move depends on uninitialised value(s)
     ==3819==    at 0x12A682: closef (label_file.c:577)
     ==3819==    by 0x12A196: selabel_close (label.c:163)
     ==3819==    by 0x10A2FD: cleanup (checkfc.c:218)
     ==3819==    by 0x5089258: __run_exit_handlers (exit.c:82)
     ==3819==    by 0x50892A4: exit (exit.c:104)
     ==3819==    by 0x10A231: main (checkfc.c:361)
     ==3819==  Uninitialised value was created by a heap allocation
     ==3819==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
     ==3819==    by 0x4C2CF1F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
     ==3819==    by 0x12BB31: process_file (label_file.h:273)
     ==3819==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==3819==    by 0x12A0BB: selabel_open (label.c:88)
     ==3819==    by 0x10A038: main (checkfc.c:292)
     ==3819==
     ==3819==
     ==3819== HEAP SUMMARY:
     ==3819==     in use at exit: 729 bytes in 19 blocks
     ==3819==   total heap usage: 21,126 allocs, 21,107 frees, 923,854 bytes allocated
     ==3819==
     ==3819== 81 bytes in 1 blocks are definitely lost in loss record 1 of 2
     ==3819==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
     ==3819==    by 0x50D5839: strdup (strdup.c:42)
     ==3819==    by 0x12A2A6: selabel_file_init (label_file.c:517)
     ==3819==    by 0x12A0BB: selabel_open (label.c:88)
     ==3819==    by 0x10A038: main (checkfc.c:292)
     ==3819==

     ==4238== 40 bytes in 1 blocks are definitely lost in loss record 1 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
     ==4238==    by 0x12A1D2: selabel_file_init (label_file.c:886)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 81 bytes in 1 blocks are definitely lost in loss record 2 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
     ==4238==    by 0x50D5839: strdup (strdup.c:42)
     ==4238==    by 0x12A2A6: selabel_file_init (label_file.c:517)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 386 bytes in 24 blocks are definitely lost in loss record 3 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
     ==4238==    by 0x50D5889: strndup (strndup.c:45)
     ==4238==    by 0x12CDDF: read_spec_entries (label_support.c:37)
     ==4238==    by 0x12B72D: process_file (label_file.h:392)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 648 bytes in 18 blocks are definitely lost in loss record 4 of 6
     ==4238==    at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
     ==4238==    by 0x117C9B: avtab_insert_node (avtab.c:105)
     ==4238==    by 0x117C10: avtab_insert (avtab.c:163)
     ==4238==    by 0x11880A: avtab_read_item (avtab.c:566)
     ==4238==    by 0x118BD3: avtab_read (avtab.c:600)
     ==4238==    by 0x125BDD: policydb_read (policydb.c:3854)
     ==4238==    by 0x109F87: main (checkfc.c:273)
     ==4238==
     ==4238== 1,095 bytes in 12 blocks are definitely lost in loss record 5 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
     ==4238==    by 0x12D8D1: pcre_compile2 (pcre_compile.c:9217)
     ==4238==    by 0x12B239: compile_regex (label_file.h:357)
     ==4238==    by 0x12B9C7: process_file (label_file.h:429)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 1,296 bytes in 12 blocks are definitely lost in loss record 6 of 6
     ==4238==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
     ==4238==    by 0x13EBE5: pcre_study (pcre_study.c:1565)
     ==4238==    by 0x12B25D: compile_regex (label_file.h:366)
     ==4238==    by 0x12B9C7: process_file (label_file.h:429)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
---
  libselinux/src/label.c      |  1 +
  libselinux/src/label_file.c |  5 ++++-
  libselinux/src/label_file.h | 22 ++++++++++++++--------
  3 files changed, 19 insertions(+), 9 deletions(-)
libselinux/src/label.c +1 -0
index c656fda..963bfcb 100644
@@ -338,6 +338,7 @@ struct selabel_handle *selabel_open(unsigned int backend,
  	rec->digest = selabel_is_digest_set(opts, nopts, rec->digest);
 
  	if ((*initfuncs[backend])(rec, opts, nopts)) {
+		free(rec->spec_file);
  		free(rec);
  		rec = NULL;
  	}
libselinux/src/label_file.c +4 -1
index 1a0c15f..071d902 100644
@@ -507,6 +507,8 @@ out:
  	return rc;
  }
 
+static void closef(struct selabel_handle *rec);
+
  static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
  		unsigned n)
  {
@@ -580,7 +582,8 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 
  finish:
  	if (status)
-		free(data->spec_arr);
+		closef(rec);
+
  	return status;
  }
 
libselinux/src/label_file.h +1 -0
index 2492826..a85b9bd 100644
@@ -278,6 +278,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->num_stems++;
 
  	return num;

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

Quoted 5 lines
libselinux/src/label_file.h +2 -0
@@ -425,6 +426,19 @@ static inline int process_line(struct selabel_handle *rec,
  	/* process and store the specification in spec. */
  	spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
  	spec_arr[nspec].regex_str = regex;
+
+	/* Convert the type string to a mode format */

Technically we should leave this comment line down where it was, at the point that we actually convert the type string to a mode.

Quoted 25 lines
libselinux/src/label_file.h +11 -3
+	spec_arr[nspec].type_str = type;
+	spec_arr[nspec].mode = 0;
+
+	spec_arr[nspec].lr.ctx_raw = context;
+
+	/*
+	 * bump data->nspecs to cause closef() to cover it in its free
+	 * but do not bump nspec since it's used below.
+	 */
+	data->nspec++;
+
  	if (rec->validating &&
  			    compile_regex(data, &spec_arr[nspec], &errbuf)) {
  		COMPAT_LOG(SELINUX_ERROR,
@@ -435,9 +449,6 @@ static inline int process_line(struct selabel_handle *rec,
  		return -1;
  	}
 
-	/* Convert the type string to a mode format */
-	spec_arr[nspec].type_str = type;
-	spec_arr[nspec].mode = 0;
  	if (type) {
  		mode_t mode = string_to_mode(type);
 

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

Quoted 4 lines
libselinux/src/label_file.h +0 -1
@@ -445,14 +456,11 @@ static inline int process_line(struct selabel_handle *rec,
  			COMPAT_LOG(SELINUX_ERROR,
  				   "%s:  line %u has invalid file type %s\n",
  				   path, lineno, type);
-			errno = EINVAL;

Don't want to remove this.

Quoted 20 lines
libselinux/src/label_file.h +0 -4
  			return -1;
  		}
  		spec_arr[nspec].mode = mode;
  	}
 
-	spec_arr[nspec].lr.ctx_raw = context;
-
  	/* Determine if specification has
  	 * any meta characters in the RE */
  	spec_hasMetaChars(&spec_arr[nspec]);
@@ -460,8 +468,6 @@ static inline int process_line(struct selabel_handle *rec,
  	if (strcmp(context, "<<none>>") && rec->validating)
  		compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
-	data->nspec = ++nspec;
-
  	return 0;
  }
 
#5 lore raw
Quoted 2 lines · Stephen Smalley

Subject: [PATCH] libselinux: label_file: fix memory leaks and

uninitialized jump

Quoted 11 lines · On 10/26/2015 02:32 PM, [email protected] wrote:
Quoted 5 lines

From: William Roberts <[email protected]>

Some error's were reported by valgrind (below) fix them. The test cases on which these leaks were detected:

Why do you have leading space in the subject line and every line of the

description?

Quoted 6 lines
Quoted 6 lines

1. properly formed file_contexts file.
2. malformed file_contexts file, unknown type.
3. malformed file_contexts file, type that fails on validate

callback.

Quoted 12 lines
Quoted 4 lines
     4. malformed file_contexts file, invalid regex.
     5. malformed file_contexts file, invalid mode.

     ==3819== Conditional jump or move depends on uninitialised value(s)
Quoted 8 lines
     ==3819==    at 0x12A682: closef (label_file.c:577)
     ==3819==    by 0x12A196: selabel_close (label.c:163)
     ==3819==    by 0x10A2FD: cleanup (checkfc.c:218)
     ==3819==    by 0x5089258: __run_exit_handlers (exit.c:82)
     ==3819==    by 0x50892A4: exit (exit.c:104)
     ==3819==    by 0x10A231: main (checkfc.c:361)
     ==3819==  Uninitialised value was created by a heap allocation
     ==3819==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

Quoted 1 line
Quoted 1 line
     ==3819==    by 0x4C2CF1F: realloc (in

/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

Quoted 9 lines
Quoted 9 lines
     ==3819==    by 0x12BB31: process_file (label_file.h:273)
     ==3819==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==3819==    by 0x12A0BB: selabel_open (label.c:88)
     ==3819==    by 0x10A038: main (checkfc.c:292)
     ==3819==
     ==3819==
     ==3819== HEAP SUMMARY:
     ==3819==     in use at exit: 729 bytes in 19 blocks
     ==3819==   total heap usage: 21,126 allocs, 21,107 frees, 923,854

bytes allocated

Quoted 2 lines
Quoted 2 lines
     ==3819==
     ==3819== 81 bytes in 1 blocks are definitely lost in loss record 1

of 2

Quoted 1 line
Quoted 1 line
     ==3819==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

Quoted 7 lines
Quoted 7 lines
     ==3819==    by 0x50D5839: strdup (strdup.c:42)
     ==3819==    by 0x12A2A6: selabel_file_init (label_file.c:517)
     ==3819==    by 0x12A0BB: selabel_open (label.c:88)
     ==3819==    by 0x10A038: main (checkfc.c:292)
     ==3819==

     ==4238== 40 bytes in 1 blocks are definitely lost in loss record 1

of 6

Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

Quoted 5 lines
Quoted 5 lines
     ==4238==    by 0x12A1D2: selabel_file_init (label_file.c:886)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 81 bytes in 1 blocks are definitely lost in loss record 2

of 6

Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

Quoted 6 lines
Quoted 6 lines
     ==4238==    by 0x50D5839: strdup (strdup.c:42)
     ==4238==    by 0x12A2A6: selabel_file_init (label_file.c:517)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 386 bytes in 24 blocks are definitely lost in loss record

3 of 6

Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

Quoted 8 lines
Quoted 8 lines
     ==4238==    by 0x50D5889: strndup (strndup.c:45)
     ==4238==    by 0x12CDDF: read_spec_entries (label_support.c:37)
     ==4238==    by 0x12B72D: process_file (label_file.h:392)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 648 bytes in 18 blocks are definitely lost in loss record

4 of 6

Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2CC70: calloc (in

/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

Quoted 8 lines
Quoted 8 lines
     ==4238==    by 0x117C9B: avtab_insert_node (avtab.c:105)
     ==4238==    by 0x117C10: avtab_insert (avtab.c:163)
     ==4238==    by 0x11880A: avtab_read_item (avtab.c:566)
     ==4238==    by 0x118BD3: avtab_read (avtab.c:600)
     ==4238==    by 0x125BDD: policydb_read (policydb.c:3854)
     ==4238==    by 0x109F87: main (checkfc.c:273)
     ==4238==
     ==4238== 1,095 bytes in 12 blocks are definitely lost in loss

record 5 of 6

Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

Quoted 8 lines
Quoted 8 lines
     ==4238==    by 0x12D8D1: pcre_compile2 (pcre_compile.c:9217)
     ==4238==    by 0x12B239: compile_regex (label_file.h:357)
     ==4238==    by 0x12B9C7: process_file (label_file.h:429)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
     ==4238==
     ==4238== 1,296 bytes in 12 blocks are definitely lost in loss

record 6 of 6

Quoted 1 line
Quoted 1 line
     ==4238==    at 0x4C2AB80: malloc (in

/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

Quoted 14 lines
Quoted 14 lines
     ==4238==    by 0x13EBE5: pcre_study (pcre_study.c:1565)
     ==4238==    by 0x12B25D: compile_regex (label_file.h:366)
     ==4238==    by 0x12B9C7: process_file (label_file.h:429)
     ==4238==    by 0x12A2BA: selabel_file_init (label_file.c:522)
     ==4238==    by 0x12A0BB: selabel_open (label.c:88)
     ==4238==    by 0x10A038: main (checkfc.c:292)
---
  libselinux/src/label.c      |  1 +
  libselinux/src/label_file.c |  5 ++++-
  libselinux/src/label_file.h | 22 ++++++++++++++--------
  3 files changed, 19 insertions(+), 9 deletions(-)
Quoted 5 lines

libselinux/src/label.c struct selabel_handle *selabel_open(unsigned int

Quoted 5 lines
libselinux/src/label.c +0 -0
index c656fda..963bfcb 100644
@@ -338,6 +338,7 @@ struct selabel_handle *selabel_open(unsigned int

backend,

Quoted 15 lines
Quoted 15 lines
libselinux/src/label.c +1 -0
        rec->digest = selabel_is_digest_set(opts, nopts, rec->digest);
 
        if ((*initfuncs[backend])(rec, opts, nopts)) {
+               free(rec->spec_file);
                free(rec);
                rec = NULL;
        }
libselinux/src/label_file.c +0 -0
index 1a0c15f..071d902 100644
@@ -507,6 +507,8 @@ out:
        return rc;
  }
 
Quoted 3 lines

libselinux/src/label_file.c out:

Quoted 3 lines
libselinux/src/label_file.c +2 -0
+static void closef(struct selabel_handle *rec);
+
  static int init(struct selabel_handle *rec, const struct selinux_opt

*opts,

Quoted 3 lines

libselinux/src/label_file.c static int init(struct selabel_handle *rec, const

Quoted 3 lines
libselinux/src/label_file.c +0 -0
                unsigned n)
  {
@@ -580,7 +582,8 @@ static int init(struct selabel_handle *rec, const

struct selinux_opt *opts,

Quoted 9 lines
Quoted 9 lines
libselinux/src/label_file.c +2 -1
  finish:
        if (status)
-               free(data->spec_arr);
+               closef(rec);
+
        return status;
  }
 
Quoted 5 lines

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

Quoted 5 lines
libselinux/src/label_file.h +0 -0
index 2492826..a85b9bd 100644
@@ -278,6 +278,7 @@ static inline int store_stem(struct saved_data

*data, char *buf, int stem_len)

Quoted 8 lines

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

Quoted 8 lines
libselinux/src/label_file.h +1 -0
        }
        data->stem_arr[num].len = stem_len;
        data->stem_arr[num].buf = buf;
+       data->stem_arr[num].from_mmap = 0;
        data->num_stems++;
 
        return num;
@@ -425,6 +426,19 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 8 lines

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

Quoted 5 lines
libselinux/src/label_file.h +2 -0
        /* process and store the specification in spec. */
        spec_arr[nspec].stem_id = find_stem_from_spec(data, regex);
        spec_arr[nspec].regex_str = regex;
+
+       /* Convert the type string to a mode format */

Technically we should leave this comment line down where it was, at the

point that we actually convert the type string to a mode.

Quoted 15 lines
Quoted 5 lines
libselinux/src/label_file.h +3 -0
+       spec_arr[nspec].type_str = type;
+       spec_arr[nspec].mode = 0;
+

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

Quoted 10 lines
libselinux/src/label_file.h +8 -0
+       spec_arr[nspec].lr.ctx_raw = context;
+
+       /*
+        * bump data->nspecs to cause closef() to cover it in its free
+        * but do not bump nspec since it's used below.
+        */
+       data->nspec++;
+
        if (rec->validating &&
                            compile_regex(data, &spec_arr[nspec],

&errbuf)) {

Quoted 2 lines

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

Quoted 2 lines
libselinux/src/label_file.h +0 -0
                COMPAT_LOG(SELINUX_ERROR,
@@ -435,9 +449,6 @@ static inline int process_line(struct selabel_handle

*rec,

Quoted 10 lines

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

Quoted 10 lines
libselinux/src/label_file.h +0 -3
                return -1;
        }
 
-       /* Convert the type string to a mode format */
-       spec_arr[nspec].type_str = type;
-       spec_arr[nspec].mode = 0;
        if (type) {
                mode_t mode = string_to_mode(type);
 
@@ -445,14 +456,11 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 2 lines

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

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

%s\n",

Quoted 5 lines

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

Quoted 2 lines
libselinux/src/label_file.h +0 -1
                                   path, lineno, type);
-                       errno = EINVAL;

Don't want to remove this.

Yes definitely not. That crept in on version 8 of the patch on gerrit you +1'd. Cannot believe we missed that.

Quoted 7 lines
Quoted 7 lines
libselinux/src/label_file.h +0 -0
                        return -1;
                }
                spec_arr[nspec].mode = mode;
        }
 
Quoted 6 lines

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

Quoted 6 lines
libselinux/src/label_file.h +0 -2
-       spec_arr[nspec].lr.ctx_raw = context;
-
        /* Determine if specification has
         * any meta characters in the RE */
        spec_hasMetaChars(&spec_arr[nspec]);
@@ -460,8 +468,6 @@ static inline int process_line(struct selabel_handle

*rec,

Quoted 10 lines

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

Quoted 9 lines
libselinux/src/label_file.h +0 -2
        if (strcmp(context, "<<none>>") && rec->validating)
                compat_validate(rec, &spec_arr[nspec].lr, path, lineno);
 
-       data->nspec = ++nspec;
-
        return 0;
  }
 
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].