|
|
74177a |
From 7fd15f8e272136955f7ffc37df29fbca9ddceca1 Mon Sep 17 00:00:00 2001
|
|
|
74177a |
From: David Rheinsberg <david.rheinsberg@gmail.com>
|
|
|
74177a |
Date: Tue, 19 Apr 2022 13:11:02 +0200
|
|
|
74177a |
Subject: [PATCH] strnspn: fix buffer overflow
|
|
|
74177a |
|
|
|
74177a |
Fix the strnspn and strncspn functions to use a properly sized buffer.
|
|
|
74177a |
It used to be 1 byte too short. Checking for `0xff` in a string will
|
|
|
74177a |
thus write `0xff` once byte beyond the stack space of the local buffer.
|
|
|
74177a |
|
|
|
74177a |
Note that the public API does not allow to pass `0xff` to those
|
|
|
74177a |
functions. Therefore, this is a read-only buffer overrun, possibly
|
|
|
74177a |
causing bogus reports from the parser, but still well-defined.
|
|
|
74177a |
|
|
|
74177a |
Reported-by: Steffen Robertz
|
|
|
74177a |
Signed-off-by: David Rheinsberg <david.rheinsberg@gmail.com>
|
|
|
74177a |
---
|
|
|
74177a |
/subprojects/c-shquote/src/c-shquote.c | 4 ++--
|
|
|
74177a |
/subprojects/c-shquote/src/test-private.c | 6 ++++++
|
|
|
74177a |
2 files changed, 8 insertions(+), 2 deletions(-)
|
|
|
74177a |
|
|
|
74177a |
diff --git a//subprojects/c-shquote/src/c-shquote.c b//subprojects/c-shquote/src/c-shquote.c
|
|
|
74177a |
index b268906..abb55d6 100644
|
|
|
74177a |
--- a//subprojects/c-shquote/src/c-shquote.c
|
|
|
74177a |
+++ b//subprojects/c-shquote/src/c-shquote.c
|
|
|
74177a |
@@ -85,7 +85,7 @@ int c_shquote_consume_char(char **outp,
|
|
|
74177a |
size_t c_shquote_strnspn(const char *string,
|
|
|
74177a |
size_t n_string,
|
|
|
74177a |
const char *accept) {
|
|
|
74177a |
- bool buffer[UCHAR_MAX] = {};
|
|
|
74177a |
+ bool buffer[UCHAR_MAX + 1] = {};
|
|
|
74177a |
|
|
|
74177a |
for ( ; *accept; ++accept)
|
|
|
74177a |
buffer[(unsigned char)*accept] = true;
|
|
|
74177a |
@@ -100,7 +100,7 @@ size_t c_shquote_strnspn(const char *string,
|
|
|
74177a |
size_t c_shquote_strncspn(const char *string,
|
|
|
74177a |
size_t n_string,
|
|
|
74177a |
const char *reject) {
|
|
|
74177a |
- bool buffer[UCHAR_MAX] = {};
|
|
|
74177a |
+ bool buffer[UCHAR_MAX + 1] = {};
|
|
|
74177a |
|
|
|
74177a |
if (strlen(reject) == 1) {
|
|
|
74177a |
const char *p;
|
|
|
74177a |
diff --git a//subprojects/c-shquote/src/test-private.c b//subprojects/c-shquote/src/test-private.c
|
|
|
74177a |
index 57a7250..c6afe40 100644
|
|
|
74177a |
--- a//subprojects/c-shquote/src/test-private.c
|
|
|
74177a |
+++ b//subprojects/c-shquote/src/test-private.c
|
|
|
74177a |
@@ -148,6 +148,9 @@ static void test_strnspn(void) {
|
|
|
74177a |
|
|
|
74177a |
len = c_shquote_strnspn("ab", 2, "bc");
|
|
|
74177a |
c_assert(len == 0);
|
|
|
74177a |
+
|
|
|
74177a |
+ len = c_shquote_strnspn("ab", 2, "\xff");
|
|
|
74177a |
+ c_assert(len == 0);
|
|
|
74177a |
}
|
|
|
74177a |
|
|
|
74177a |
static void test_strncspn(void) {
|
|
|
74177a |
@@ -167,6 +170,9 @@ static void test_strncspn(void) {
|
|
|
74177a |
|
|
|
74177a |
len = c_shquote_strncspn("ab", 2, "cd");
|
|
|
74177a |
c_assert(len == 2);
|
|
|
74177a |
+
|
|
|
74177a |
+ len = c_shquote_strncspn("ab", 2, "\xff");
|
|
|
74177a |
+ c_assert(len == 2);
|
|
|
74177a |
}
|
|
|
74177a |
|
|
|
74177a |
static void test_discard_comment(void) {
|