Blame SOURCES/0017-libsepol-cil-Fix-out-of-bound-read-of-file-context-p.patch

71cd55
From e13c8162656665f9ec1c76a033cae5b011b8c658 Mon Sep 17 00:00:00 2001
71cd55
From: James Carter <jwcart2@gmail.com>
71cd55
Date: Thu, 8 Apr 2021 13:24:29 -0400
71cd55
Subject: [PATCH] libsepol/cil: Fix out-of-bound read of file context pattern
71cd55
 ending with "\"
71cd55
71cd55
Based on patch by Nicolas Iooss, who writes:
71cd55
  OSS-Fuzz found a Heap-buffer-overflow in the CIL compiler when trying
71cd55
  to compile the following policy:
71cd55
71cd55
    (sid SID)
71cd55
    (sidorder(SID))
71cd55
    (filecon "\" any ())
71cd55
    (filecon "" any ())
71cd55
71cd55
  When cil_post_fc_fill_data() processes "\", it goes beyond the NUL
71cd55
  terminator of the string. Fix this by returning when '\0' is read
71cd55
  after a backslash.
71cd55
71cd55
To be consistent with the function compute_diffdata() in
71cd55
refpolicy/support/fc_sort.py, also increment str_len in this case.
71cd55
71cd55
Fixes: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=28484
71cd55
Reported-by: Nicolas Iooss <nicolas.iooss@m4x.org>
71cd55
Signed-off-by: James Carter <jwcart2@gmail.com>
71cd55
---
71cd55
 libsepol/cil/src/cil_post.c | 7 +++++++
71cd55
 1 file changed, 7 insertions(+)
71cd55
71cd55
diff --git a/libsepol/cil/src/cil_post.c b/libsepol/cil/src/cil_post.c
71cd55
index d2ecbd430aa3..fd4758dc580e 100644
71cd55
--- a/libsepol/cil/src/cil_post.c
71cd55
+++ b/libsepol/cil/src/cil_post.c
71cd55
@@ -186,6 +186,13 @@ static void cil_post_fc_fill_data(struct fc_data *fc, const char *path)
71cd55
 			break;
71cd55
 		case '\\':
71cd55
 			c++;
71cd55
+			if (path[c] == '\0') {
71cd55
+				if (!fc->meta) {
71cd55
+					fc->stem_len++;
71cd55
+				}
71cd55
+				fc->str_len++;
71cd55
+				return;
71cd55
+			}
71cd55
 			/* FALLTHRU */
71cd55
 		default:
71cd55
 			if (!fc->meta) {
71cd55
-- 
71cd55
2.32.0
71cd55