Skip to content

Commit f057012

Browse files
cgzonesjwcart2
authored andcommitted
libselinux: limit node depth while parsing compiled fcontexts
Limit the node depth while parsing a pre-compiled fcontext definition to avoid unlimited recursions causing stack overflows. Use a sufficiently high value of 32, instead of the node depth of currently 3 for generating a database, to not unnecessarily limit custom changes. Fixes: 92306da ("libselinux: rework selabel_file(5) database") Reported-by: oss-fuzz (issues 388615595 and 388592303) Signed-off-by: Christian Göttsche <[email protected]> Acked-by: James Carter <[email protected]>
1 parent b325348 commit f057012

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

libselinux/src/label_file.c

+13-3
Original file line numberDiff line numberDiff line change
@@ -674,12 +674,22 @@ static int load_mmap_regex_spec(struct mmap_area *mmap_area, bool validating, bo
674674
}
675675

676676
static int load_mmap_spec_node(struct mmap_area *mmap_area, const char *path, bool validating, bool do_load_precompregex,
677-
struct spec_node *node, bool is_root, uint8_t inputno, const struct context_array *ctx_array)
677+
struct spec_node *node, const unsigned depth, uint8_t inputno, const struct context_array *ctx_array)
678678
{
679679
uint32_t data_u32, lspec_num, rspec_num, children_num;
680680
uint16_t data_u16, stem_len;
681+
const bool is_root = (depth == 0);
681682
int rc;
682683

684+
/*
685+
* Guard against deep recursion by malicious pre-compiled fcontext
686+
* definitions. The limit of 32 is chosen intuitively and should
687+
* suffice for any real world scenario. See the macro
688+
* SPEC_NODE_MAX_DEPTH for the current value used for tree building.
689+
*/
690+
if (depth >= 32)
691+
return -1;
692+
683693
node->from_mmap = true;
684694

685695

@@ -794,7 +804,7 @@ static int load_mmap_spec_node(struct mmap_area *mmap_area, const char *path, bo
794804
node->children_alloc = children_num;
795805

796806
for (uint32_t i = 0; i < children_num; i++) {
797-
rc = load_mmap_spec_node(mmap_area, path, validating, do_load_precompregex, &node->children[i], false, inputno, ctx_array);
807+
rc = load_mmap_spec_node(mmap_area, path, validating, do_load_precompregex, &node->children[i], depth + 1, inputno, ctx_array);
798808
if (rc)
799809
return -1;
800810

@@ -969,7 +979,7 @@ FUZZ_EXTERN int load_mmap(FILE *fp, const size_t len, struct selabel_handle *rec
969979

970980
rc = load_mmap_spec_node(mmap_area, path, rec->validating,
971981
reg_version_matches && reg_arch_matches,
972-
root, true,
982+
root, 0,
973983
inputno,
974984
&ctx_array);
975985
if (rc)

0 commit comments

Comments
 (0)