|
| 1 | +From f9d883c5792fc1b3f9cd29dd8ff255c6c99944a2 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Eduardo Silva <eduardo@chronosphere.io> |
| 3 | +Date: Thu, 9 Apr 2026 12:11:57 -0600 |
| 4 | +Subject: [PATCH] server: parser: harden boundary checks |
| 5 | + |
| 6 | +Tighten parser and helper validation around explicit lengths and |
| 7 | +buffer boundaries. |
| 8 | + |
| 9 | +Require exact header literal matches, validate chunk length tokens, |
| 10 | +and guard helper routines that previously trusted inconsistent |
| 11 | +pointer or length state. |
| 12 | + |
| 13 | +Verified by rebuilding with cmake --build build and replaying the |
| 14 | +reported malformed request fixtures against build/bin/monkey. |
| 15 | + |
| 16 | +Signed-off-by: Eduardo Silva <eduardo@chronosphere.io> |
| 17 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 18 | +Upstream-reference: https://github.com/monkey/monkey/commit/ffe0d0ed1b074ea6f3965c37bb754e9f19130a82.patch |
| 19 | +--- |
| 20 | + lib/monkey/include/monkey/mk_http_parser.h | 6 +++++- |
| 21 | + lib/monkey/mk_server/mk_http_parser.c | 10 ++++++++++ |
| 22 | + lib/monkey/mk_server/mk_mimetype.c | 7 ++++++- |
| 23 | + lib/monkey/mk_server/mk_user.c | 2 +- |
| 24 | + 4 files changed, 22 insertions(+), 3 deletions(-) |
| 25 | + |
| 26 | +diff --git a/lib/monkey/include/monkey/mk_http_parser.h b/lib/monkey/include/monkey/mk_http_parser.h |
| 27 | +index 6d45c39..5ae5c60 100644 |
| 28 | +--- a/lib/monkey/include/monkey/mk_http_parser.h |
| 29 | ++++ b/lib/monkey/include/monkey/mk_http_parser.h |
| 30 | +@@ -335,7 +335,11 @@ static inline void mk_http_parser_init(struct mk_http_parser *p) |
| 31 | + |
| 32 | + static inline int mk_http_parser_more(struct mk_http_parser *p, int len) |
| 33 | + { |
| 34 | +- if (abs(len - p->i) - 1 > 0) { |
| 35 | ++ if (len <= 0 || p->i < 0) { |
| 36 | ++ return MK_FALSE; |
| 37 | ++ } |
| 38 | ++ |
| 39 | ++ if ((p->i + 1) < len) { |
| 40 | + return MK_TRUE; |
| 41 | + } |
| 42 | + |
| 43 | +diff --git a/lib/monkey/mk_server/mk_http_parser.c b/lib/monkey/mk_server/mk_http_parser.c |
| 44 | +index 4e7aa31..898df31 100644 |
| 45 | +--- a/lib/monkey/mk_server/mk_http_parser.c |
| 46 | ++++ b/lib/monkey/mk_server/mk_http_parser.c |
| 47 | +@@ -172,6 +172,16 @@ static inline void request_set(mk_ptr_t *ptr, struct mk_http_parser *p, char *bu |
| 48 | + static inline int header_cmp(const char *expected, char *value, int len) |
| 49 | + { |
| 50 | + int i = 0; |
| 51 | ++ size_t expected_len; |
| 52 | ++ |
| 53 | ++ if (len < 0) { |
| 54 | ++ return -1; |
| 55 | ++ } |
| 56 | ++ |
| 57 | ++ expected_len = strlen(expected); |
| 58 | ++ if ((size_t) len != expected_len) { |
| 59 | ++ return -1; |
| 60 | ++ } |
| 61 | + |
| 62 | + if (len >= 8) { |
| 63 | + if (expected[0] != tolower(value[0])) return -1; |
| 64 | +diff --git a/lib/monkey/mk_server/mk_mimetype.c b/lib/monkey/mk_server/mk_mimetype.c |
| 65 | +index b86b4ef..5462ea5 100644 |
| 66 | +--- a/lib/monkey/mk_server/mk_mimetype.c |
| 67 | ++++ b/lib/monkey/mk_server/mk_mimetype.c |
| 68 | +@@ -197,7 +197,12 @@ struct mk_mimetype *mk_mimetype_find(struct mk_server *server, mk_ptr_t *filenam |
| 69 | + { |
| 70 | + int j, len; |
| 71 | + |
| 72 | +- j = len = filename->len; |
| 73 | ++ if (!filename->data || filename->len <= 0) { |
| 74 | ++ return NULL; |
| 75 | ++ } |
| 76 | ++ |
| 77 | ++ len = filename->len; |
| 78 | ++ j = len - 1; |
| 79 | + |
| 80 | + /* looking for extension */ |
| 81 | + while (j >= 0 && filename->data[j] != '.') { |
| 82 | +diff --git a/lib/monkey/mk_server/mk_user.c b/lib/monkey/mk_server/mk_user.c |
| 83 | +index 7200ff0..716331a 100644 |
| 84 | +--- a/lib/monkey/mk_server/mk_user.c |
| 85 | ++++ b/lib/monkey/mk_server/mk_user.c |
| 86 | +@@ -46,7 +46,7 @@ int mk_user_init(struct mk_http_session *cs, struct mk_http_request *sr, |
| 87 | + } |
| 88 | + |
| 89 | + limit = mk_string_char_search(sr->uri_processed.data + offset, '/', |
| 90 | +- sr->uri_processed.len); |
| 91 | ++ sr->uri_processed.len - offset); |
| 92 | + |
| 93 | + if (limit == -1) { |
| 94 | + limit = (sr->uri_processed.len) - offset; |
| 95 | +-- |
| 96 | +2.45.4 |
| 97 | + |
0 commit comments