Blame SOURCES/0059-CVE-2017-15134-crash-in-slapi_filter_sprintf.patch

96373c
From cb008bcace2510f157ccec2df4e5ff254513b7c4 Mon Sep 17 00:00:00 2001
96373c
From: Ludwig Krispenz <lkrispen@redhat.com>
96373c
Date: Mon, 15 Jan 2018 10:24:41 +0100
96373c
Subject: [PATCH] CVE 2017-15134 - crash in slapi_filter_sprintf
8a4a2a
96373c
Signed-off-by: Mark Reynolds <mreynolds@redhat.com>
8a4a2a
---
8a4a2a
 ldap/servers/slapd/util.c | 36 +++++++++++++++++++++++++++++++-----
8a4a2a
 1 file changed, 31 insertions(+), 5 deletions(-)
8a4a2a
8a4a2a
diff --git a/ldap/servers/slapd/util.c b/ldap/servers/slapd/util.c
96373c
index a72de9b07..ddb2cc899 100644
8a4a2a
--- a/ldap/servers/slapd/util.c
8a4a2a
+++ b/ldap/servers/slapd/util.c
96373c
@@ -238,9 +238,10 @@ escape_string_for_filename(const char *str, char buf[BUFSIZ])
96373c
 struct filter_ctx
96373c
 {
96373c
     char *buf;
96373c
-    char attr[ATTRSIZE];
96373c
+    char *attr;
96373c
     int attr_position;
96373c
     int attr_found;
96373c
+    size_t attr_size;
96373c
     int buf_size;
96373c
     int buf_len;
96373c
     int next_arg_needs_esc_norm;
96373c
@@ -279,7 +280,7 @@ filter_stuff_func(void *arg, const char *val, PRUint32 slen)
8a4a2a
      *  Start collecting the attribute name so we can use the correct
8a4a2a
      *  syntax normalization func.
8a4a2a
      */
96373c
-    if (ctx->attr_found == 0 && ctx->attr_position < (ATTRSIZE - 1)) {
96373c
+    if (ctx->attr_found == 0 && ctx->attr_position < (ctx->attr_size - 1)) {
96373c
         if (ctx->attr[0] == '\0') {
96373c
             if (strstr(val, "=")) {
8a4a2a
                 /* we have an attr we need to record */
96373c
@@ -293,6 +294,14 @@ filter_stuff_func(void *arg, const char *val, PRUint32 slen)
8a4a2a
                  *  attr with val.  The next pass should be '=', otherwise we will
8a4a2a
                  *  reset it.
8a4a2a
                  */
8a4a2a
+                if (slen > ctx->attr_size) {
8a4a2a
+                    if (ctx->attr_size == ATTRSIZE) {
8a4a2a
+                        ctx->attr = slapi_ch_calloc(sizeof(char), slen+1);
8a4a2a
+                    } else {
8a4a2a
+                        ctx->attr = slapi_ch_realloc(ctx->attr, sizeof(char) * (slen+1));
8a4a2a
+                    }
8a4a2a
+                    ctx->attr_size = slen+1;
8a4a2a
+                }
8a4a2a
                 memcpy(ctx->attr, val, slen);
8a4a2a
                 ctx->attr_position = slen;
8a4a2a
             }
96373c
@@ -302,9 +311,20 @@ filter_stuff_func(void *arg, const char *val, PRUint32 slen)
8a4a2a
             } else {
96373c
                 if (special_attr_char(val[0])) {
8a4a2a
                     /* this is not an attribute, we should not be collecting this, reset everything */
8a4a2a
-                    memset(ctx->attr, '\0', ATTRSIZE);
8a4a2a
+                    memset(ctx->attr, '\0', ctx->attr_size);
8a4a2a
                     ctx->attr_position = 0;
8a4a2a
                 } else {
8a4a2a
+                    /* we can be adding char by char and overrun allocated size */
8a4a2a
+                    if (ctx->attr_position >= ctx->attr_size) {
8a4a2a
+                        if (ctx->attr_size == ATTRSIZE) {
8a4a2a
+                            char *ctxattr = slapi_ch_calloc(sizeof(char), ctx->attr_size + ATTRSIZE);
8a4a2a
+                            memcpy(ctxattr, ctx->attr, ctx->attr_size);
8a4a2a
+                            ctx->attr = ctxattr;
8a4a2a
+                        } else {
8a4a2a
+                            ctx->attr = slapi_ch_realloc(ctx->attr, sizeof(char) * (ctx->attr_size + ATTRSIZE));
8a4a2a
+                        }
8a4a2a
+                        ctx->attr_size = ctx->attr_size + ATTRSIZE;
8a4a2a
+                    }
8a4a2a
                     memcpy(ctx->attr + ctx->attr_position, val, 1);
8a4a2a
                     ctx->attr_position++;
8a4a2a
                 }
96373c
@@ -377,7 +397,7 @@ filter_stuff_func(void *arg, const char *val, PRUint32 slen)
8a4a2a
         ctx->next_arg_needs_esc_norm = 0;
8a4a2a
         ctx->attr_found = 0;
8a4a2a
         ctx->attr_position = 0;
8a4a2a
-        memset(ctx->attr, '\0', ATTRSIZE);
8a4a2a
+        memset(ctx->attr, '\0', ctx->attr_size);
8a4a2a
         slapi_ch_free_string(&buf);
8a4a2a
 
8a4a2a
         return filter_len;
96373c
@@ -416,12 +436,14 @@ slapi_filter_sprintf(const char *fmt, ...)
8a4a2a
 {
8a4a2a
     struct filter_ctx ctx = {0};
8a4a2a
     va_list args;
8a4a2a
+    char attr_static[ATTRSIZE] = {0};
8a4a2a
     char *buf;
8a4a2a
     int rc;
8a4a2a
 
8a4a2a
     buf = slapi_ch_calloc(sizeof(char), FILTER_BUF + 1);
8a4a2a
     ctx.buf = buf;
96373c
-    memset(ctx.attr, '\0', ATTRSIZE);
8a4a2a
+    ctx.attr = attr_static;
8a4a2a
+    ctx.attr_size = ATTRSIZE;
96373c
     ctx.attr_position = 0;
8a4a2a
     ctx.attr_found = 0;
8a4a2a
     ctx.buf_len = FILTER_BUF;
96373c
@@ -438,6 +460,10 @@ slapi_filter_sprintf(const char *fmt, ...)
8a4a2a
     }
8a4a2a
     va_end(args);
8a4a2a
 
8a4a2a
+    if (ctx.attr_size > ATTRSIZE) {
8a4a2a
+        slapi_ch_free_string(&ctx.attr);
8a4a2a
+    }
8a4a2a
+
8a4a2a
     return ctx.buf;
8a4a2a
 }
8a4a2a
 
8a4a2a
-- 
8a4a2a
2.13.6
8a4a2a