Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/detect-byte-extract.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2024 Open Information Security Foundation
/* Copyright (C) 2007-2026 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
Expand Down Expand Up @@ -125,10 +125,10 @@ int DetectByteExtractDoMatch(DetectEngineThreadCtx *det_ctx, const SigMatchData
uint64_t val = 0;
int extbytes;
if (data->flags & DETECT_BYTE_EXTRACT_FLAG_STRING) {
extbytes = ByteExtractStringUint64(&val, data->base,
data->nbytes, (const char *)ptr);
extbytes = ByteExtractStringUint64Bounded(
&val, data->base, data->nbytes, (size_t)len, (const char *)ptr);
if (extbytes <= 0) {
/* ByteExtractStringUint64() set val to 0 if there is no numeric value in data string */
/* ByteExtractStringUint64Bounded() sets val to 0 if there is no numeric value */
if (val == 0) {
SCLogDebug("No Numeric value");
return 0;
Expand Down
5 changes: 3 additions & 2 deletions src/detect-bytejump.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2020 Open Information Security Foundation
/* Copyright (C) 2007-2026 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
Expand Down Expand Up @@ -192,7 +192,8 @@ bool DetectBytejumpDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s,

/* Extract the byte data */
if (flags & DETECT_BYTEJUMP_STRING) {
extbytes = ByteExtractStringUint64(&val, data->base, nbytes, (const char *)ptr);
extbytes = ByteExtractStringUint64Bounded(
&val, data->base, nbytes, (size_t)len, (const char *)ptr);
if(extbytes <= 0) {
SCLogDebug("error extracting %d bytes of string data: %d", nbytes, extbytes);
SCReturnBool(false);
Expand Down
7 changes: 4 additions & 3 deletions src/detect-bytetest.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2022 Open Information Security Foundation
/* Copyright (C) 2007-2026 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
Expand Down Expand Up @@ -215,9 +215,10 @@ int DetectBytetestDoMatch(DetectEngineThreadCtx *det_ctx, const Signature *s,

/* Extract the byte data */
if (flags & DETECT_BYTETEST_STRING) {
extbytes = ByteExtractStringUint64(&val, data->base, nbytes, (const char *)ptr);
extbytes = ByteExtractStringUint64Bounded(
&val, data->base, nbytes, (size_t)len, (const char *)ptr);
if (extbytes <= 0) {
/* ByteExtractStringUint64() returns 0 if there is no numeric value in data string */
/* ByteExtractStringUint64Bounded() sets val to 0 if there is no numeric value */
if (val == 0) {
SCLogDebug("No Numeric value");
SCReturnInt(0);
Expand Down
80 changes: 79 additions & 1 deletion src/tests/detect-engine-content-inspection.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2017 Open Information Security Foundation
/* Copyright (C) 2007-2026 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
Expand Down Expand Up @@ -316,6 +316,78 @@ static int DetectEngineContentInspectionTest17(void)
TEST_FOOTER;
}

/** \brief byte_test with 0 bytes and string stops at the end of the buffer
*
* The test passes a length shorter than the digits, so the digits past the
* end of the buffer are in memory but must not be converted. */
static int DetectEngineContentInspectionTest18(void)
{
TEST_HEADER;
TEST_RUN("12345", 2, "byte_test:0,=,12,0,string,dec;", true, 1);
TEST_RUN("12345", 2, "byte_test:0,=,12345,0,string,dec;", false, 1);
TEST_RUN("ff12", 2, "byte_test:0,=,255,0,string,hex;", true, 1);
/* leading zeros do not count toward the 23 byte limit */
TEST_RUN("00000000000000000000000099999", 29, "byte_test:0,>,1000,0,string,dec;", true, 1);
/* nothing left to convert at the end of the buffer */
TEST_RUN("12345", 2, "byte_test:0,=,345,2,string,dec;", false, 1);
/* 0 bytes from a byte_extract variable */
TEST_RUN("X012345", 4,
"content:\"X\"; byte_extract:1,0,n,relative,string,dec; "
"byte_test:n,=,12,0,relative,string,dec;",
true, 3);
TEST_RUN("X012345", 4,
"content:\"X\"; byte_extract:1,0,n,relative,string,dec; "
"byte_test:n,=,12345,0,relative,string,dec;",
false, 3);
TEST_FOOTER;
}

/** \brief byte_jump with 0 bytes and string stops at the end of the buffer */
static int DetectEngineContentInspectionTest19(void)
{
TEST_HEADER;
TEST_RUN("AB1000", 3, "byte_jump:0,2,string,dec,from_beginning; content:\"B\"; within:1;", true,
2);
/* nothing left to convert at the end of the buffer */
TEST_RUN("AB1", 2, "byte_jump:0,2,string,dec,from_beginning;", false, 1);
/* relative: nothing left to convert */
TEST_RUN("120", 2, "content:\"12\"; byte_jump:0,0,relative,string,dec,post_offset -1;", false,
2);
/* relative: the jump steps over the 2 digits inside the buffer */
TEST_RUN("X011", 3,
"content:\"X\"; byte_jump:0,0,relative,string,dec,post_offset -1; "
"byte_test:2,=,1,-2,relative,string,dec;",
true, 3);
/* relative: leading zeros do not count toward the 23 byte limit */
TEST_RUN("X00000000000000000000000001", 27,
"content:\"X\"; byte_jump:0,0,relative,string,dec,post_offset -1; "
"isdataat:!1,relative;",
true, 3);
/* 0 bytes from a byte_extract variable */
TEST_RUN("AX01000", 4,
"content:\"X\"; byte_extract:1,0,n,relative,string,dec; "
"byte_jump:n,0,relative,string,dec,from_beginning; content:\"X\"; within:1;",
true, 4);
TEST_FOOTER;
}

/** \brief byte_extract with 0 bytes and string stops at the end of the buffer */
static int DetectEngineContentInspectionTest20(void)
{
TEST_HEADER;
/* the value is 12 and the read position is the end of the buffer */
TEST_RUN("12345", 2, "byte_extract:0,0,v,string,dec; byte_test:2,=,v,-2,relative,string,dec;",
true, 2);
/* leading zeros do not count toward the 23 byte limit */
TEST_RUN("000000000000000000000000012", 27,
"byte_extract:0,0,v,string,dec; isdataat:!1,relative; "
"byte_test:2,=,v,-2,relative,string,dec;",
true, 3);
/* nothing left to convert at the end of the buffer */
TEST_RUN("12345", 2, "byte_extract:0,2,v,string,dec; isdataat:1;", false, 1);
TEST_FOOTER;
}

void DetectEngineContentInspectionRegisterTests(void)
{
UtRegisterTest("DetectEngineContentInspectionTest01",
Expand Down Expand Up @@ -348,6 +420,12 @@ void DetectEngineContentInspectionRegisterTests(void)
DetectEngineContentInspectionTest14);
UtRegisterTest("DetectEngineContentInspectionTest17 negative distance",
DetectEngineContentInspectionTest17);
UtRegisterTest("DetectEngineContentInspectionTest18 byte_test string 0 bytes",
DetectEngineContentInspectionTest18);
UtRegisterTest("DetectEngineContentInspectionTest19 byte_jump string 0 bytes",
DetectEngineContentInspectionTest19);
UtRegisterTest("DetectEngineContentInspectionTest20 byte_extract string 0 bytes",
DetectEngineContentInspectionTest20);
}

#undef TEST_HEADER
Expand Down
165 changes: 164 additions & 1 deletion src/util-byte.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2010 Open Information Security Foundation
/* Copyright (C) 2007-2026 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
Expand Down Expand Up @@ -191,6 +191,83 @@ int ByteExtractStringUint64(uint64_t *res, int base, size_t len, const char *str
return ByteExtractString(res, base, len, str, false);
}

/* Base 0 is resolved before this function runs. Any base other than 8,
* 10 and 16 is read as base 10 here. A combined base (dec and hex is 26)
* therefore uses base 10 digits when the count is 0. */
static bool ByteIsDigit(const char c, const int base)
{
switch (base) {
case 8:
return c >= '0' && c <= '7';
case 16:
return isxdigit((unsigned char)c);
default:
return isdigit((unsigned char)c);
}
}

int ByteExtractStringUint64Bounded(
uint64_t *res, int base, size_t len, size_t avail, const char *str)
{
*res = 0;

if (len != 0) {
if (len > avail) {
return -1;
}
return ByteExtractStringUint64(res, base, len, str);
}

/* Find the end of the number the way strtoull() does, without reading
* past str + avail. */
size_t i = 0;
while (i < avail && isspace((unsigned char)str[i])) {
i++;
}
char sign = 0;
if (i < avail && (str[i] == '+' || str[i] == '-')) {
sign = str[i++];
}
if ((base == 0 || base == 16) && i + 2 < avail && str[i] == '0' &&
(str[i + 1] == 'x' || str[i + 1] == 'X') && isxdigit((unsigned char)str[i + 2])) {
i += 2;
base = 16;
} else if (base == 0) {
base = (i < avail && str[i] == '0') ? 8 : 10;
}
const size_t start = i;
const size_t max_digits = sign != 0 ? 22 : 23;
size_t first = start;
bool significant = false;
while (i < avail && ByteIsDigit(str[i], base)) {
if (!significant) {
/* Leading zeros do not change the value. Keep the last zero. */
first = i;
significant = str[i] != '0';
} else if (i - first >= max_digits) {
SCLogDebug("numeric value out of range");
*res = UINT64_MAX;
return -1;
}
i++;
}
if (i == start) {
SCLogDebug("no numeric value");
return -1;
}

char num[24];
size_t n = 0;
if (sign != 0) {
num[n++] = sign;
}
memcpy(num + n, str + first, i - first);
n += i - first;

int ret = ByteExtractString(res, base, n, num, false);
return ret <= 0 ? ret : (int)i;
}

int ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str)
{
uint64_t i64;
Expand Down Expand Up @@ -1022,6 +1099,90 @@ static int ByteTest16 (void)
return 0;
}

/** \test a length of 0 converts no more than avail bytes */
static int ByteTest17(void)
{
uint64_t val = 0;

/* a length converts that many bytes */
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 2, 5, "12345") == 2);
FAIL_IF_NOT(val == 12);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 3, 2, "12345") == -1);

/* 0 converts the number inside avail and no further */
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 2, "12345") == 2);
FAIL_IF_NOT(val == 12);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 0, "12345") == -1);
FAIL_IF_NOT(val == 0);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 3, "x12") == -1);
FAIL_IF_NOT(val == 0);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 3, " 1") == -1);
FAIL_IF_NOT(val == 0);

/* leading whitespace and zeros do not count toward 23 bytes */
FAIL_IF_NOT(
ByteExtractStringUint64Bounded(&val, 10, 0, 28, "0000000000000000000000999991") == 28);
FAIL_IF_NOT(val == 999991);
FAIL_IF_NOT(
ByteExtractStringUint64Bounded(&val, 10, 0, 26, " 79") == 26);
FAIL_IF_NOT(val == 7);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 4, "0000") == 4);
FAIL_IF_NOT(val == 0);

/* a value past uint64_t is an error */
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 21, "184467440737095516160") == -1);
FAIL_IF_NOT(val == UINT64_MAX);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 20, "18446744073709551615") == 20);
FAIL_IF_NOT(val == UINT64_MAX);

/* bases, prefixes and signs follow strtoull() */
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 16, 0, 4, "0x1fz") == 4);
FAIL_IF_NOT(val == 0x1f);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 16, 0, 2, "0x1f") == 1);
FAIL_IF_NOT(val == 0);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 16, 0, 3, "0xz") == 1);
FAIL_IF_NOT(val == 0);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 8, 0, 3, "0179") == 3);
FAIL_IF_NOT(val == 017);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 0, 0, 4, "0x1f") == 4);
FAIL_IF_NOT(val == 0x1f);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 0, 0, 4, "0017") == 4);
FAIL_IF_NOT(val == 017);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 0, 0, 2, "17") == 2);
FAIL_IF_NOT(val == 17);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 3, "-05") == 3);
FAIL_IF_NOT(val == (uint64_t)-5);

/* more than 23 significant digits is an error, with or without a sign */
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 24, "123456789012345678901234") == -1);
FAIL_IF_NOT(val == UINT64_MAX);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 24, "-12345678901234567890123") == -1);
FAIL_IF_NOT(val == UINT64_MAX);

/* leading zeros do not count toward the 23 digit limit */
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 23, "00009999999999999999999") == 23);
FAIL_IF_NOT(val == 9999999999999999999ULL);

PASS;
}

static int ByteTest18(void)
{
uint64_t val = 0;

/* one-byte value (Content-Length: 0): a digit after the value is
* outside avail; an unbounded scan would read it and shift the
* value ("0" -> "04") */
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 1, "04") == 1);
FAIL_IF_NOT(val == 0);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 1, "14") == 1);
FAIL_IF_NOT(val == 1);
FAIL_IF_NOT(ByteExtractStringUint64Bounded(&val, 10, 0, 1, "0x") == 1);
FAIL_IF_NOT(val == 0);

PASS;
}

void ByteRegisterTests(void)
{
UtRegisterTest("ByteTest01", ByteTest01);
Expand All @@ -1040,6 +1201,8 @@ void ByteRegisterTests(void)
UtRegisterTest("ByteTest14", ByteTest14);
UtRegisterTest("ByteTest15", ByteTest15);
UtRegisterTest("ByteTest16", ByteTest16);
UtRegisterTest("ByteTest17", ByteTest17);
UtRegisterTest("ByteTest18", ByteTest18);
}
#endif /* UNITTESTS */

26 changes: 25 additions & 1 deletion src/util-byte.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2007-2022 Open Information Security Foundation
/* Copyright (C) 2007-2026 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
Expand Down Expand Up @@ -154,6 +154,30 @@ int WARN_UNUSED ByteExtractString(
*/
int WARN_UNUSED ByteExtractStringUint64(uint64_t *res, int base, size_t len, const char *str);

/**
* Extract unsigned integer value as uint64_t from a buffer that is not NUL
* terminated.
*
* With len set, this converts len bytes as ByteExtractStringUint64() does.
* With len 0, it converts the number at the start of str the way strtoull()
* does for bases 8, 10 and 16, but never reads past str + avail. Leading
* whitespace and zeros do not count toward the 23 byte limit, but a sign does.
* Every number that fits in a uint64_t converts in full.
*
* \param res Stores result
* \param base Base of the number to extract, or 0 to detect it as strtoull()
* does
* \param len Number of bytes to extract (23 max), or 0 for the number at str
* \param avail Number of bytes that can be read at str
* \param str Buffer to extract from
*
* \return n Number of bytes consumed on success
* \return -1 On error; *res is 0 when there is no number at str, and
* UINT64_MAX when the number does not fit in a uint64_t
*/
int WARN_UNUSED ByteExtractStringUint64Bounded(
uint64_t *res, int base, size_t len, size_t avail, const char *str);

/**
* Extract unsigned integer value from a string as uint32_t.
*
Expand Down
Loading