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

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