Commit graph

102 commits

Author SHA1 Message Date
Jon Siwek
3aad9c74c3 binpac: Disable LeakSanitizer
https://github.com/zeek/zeek/issues/699
2025-08-20 08:52:23 -07:00
Jon Siwek
b2ef28c2e7 binpac: Fix Zeek build for multi-config CMake generators (e.g. Xcode) 2025-08-20 08:52:23 -07:00
Jon Siwek
3cba237e84 binpac: Fix shared library versioning to agree with OpenBSD expectations
OpenBSD shared library names are like "libfoo.so.major.minor" and
binpac was previously letting the post-release number into the name
like "libbinpac.so.0.54-7", which isn't compatible with that scheme.

Related to https://github.com/zeek/zeek/issues/649
2025-08-20 08:52:23 -07:00
Jon Siwek
8b4b74e54a binpac: Move CMake project() after cmake_minimum_required() 2025-08-20 08:52:23 -07:00
Jon Siwek
ef933c9e76 binpac: Fix signed integer overflow in array bounds checks
Array lengths use signed integer storage, so multiplication of that
by the element size for purpose of bounds checking against available
data may produce a signed integer overlow, which is undefined behavior.
2025-08-20 08:52:23 -07:00
Jon Siwek
7632c69566 binpac: Fix a printf format specifier 2025-08-20 08:52:23 -07:00
Daniel Thayer
219dbd0fa3 binpac: More Bro to Zeek renaming 2025-08-20 08:52:23 -07:00
Daniel Thayer
078722fc8d binpac: Rename Bro to Zeek 2025-08-20 08:52:23 -07:00
Jon Siwek
533d823514 binpac: Fix C++11 compatibility issue for older compilers 2025-08-20 08:52:23 -07:00
Jon Siwek
21cf20fc6f binpac: Improve storage type used for case-type index
The type used to store the index for a case-type now tracks the
type of the index expression rather than always using an "int".

The case fields also now have some checking done at code-gen-time to
ensure the constants used for cases does not exceed the numeric limit
of the type used in the case's index expression.  Then, assuming, it
looks safe, the C++ case labels are generated with casts to the type
of the Binpac case's index expression to ensure compilers accept it
(since all Binpac numbers use "int" for storage/printing internally).
2025-08-20 08:52:23 -07:00
Jon Siwek
b4b229acf7 binpac: Add FlowBuffer policy mechanisms
This allows for tunability of the following behaviors:

* Minimum flowbuffer capacity to use when parsing a new unit

* Threshold at which flowbuffer capacity is contracted back to the
  minimum after parsing a complete unit and before parsing the next

* Maximum flowbuffer capacity to allow when parsing a given unit

Failed flowbuffer allocations due to reaching maximum capacity or any
other reason now throw ExceptionFlowBufferAlloc.
2025-08-20 08:52:23 -07:00
Jon Siwek
7e6e24a4d8 binpac: Build binpac shared lib instead of static by default
Related to https://github.com/zeek/zeek/issues/307
2025-08-20 08:52:23 -07:00
Jon Siwek
7bb1a148af binpac: Bump minimum CMake version to 2.8.12 2025-08-20 08:52:23 -07:00
Derek Ditch
2da5fecc16 binpac: Set installdirs using GNUInstallDirs module in CMake 2025-08-20 08:52:23 -07:00
Jon Siwek
1d750aa164 binpac: Replace u_char usages with unsigned char
Improve Alpine (musl) support by not relying on the (technically)
non-standard u_char typedef.
2025-08-20 08:52:23 -07:00
Jon Siwek
46e2490cb0 binpac: Fix array bounds checking
For arrays that are fields within a record, the bounds check was based
on a pointer to the start of the record rather than the start of the
array field.
2025-08-20 08:52:23 -07:00
Jon Siwek
de87adf398 binpac: Update BinPAC_EXE CMake variable to be a full path 2025-08-20 08:52:23 -07:00
Jon Siwek
721ea63a33 binpac: Install headers to alternate path as part of Bro sub-project 2025-08-20 08:52:23 -07:00
Jon Siwek
eea32ada6d binpac: Remove "installation skip" code paths 2025-08-20 08:52:23 -07:00
Jon Siwek
9bb3a94595 binpac: Don't install binpac.h.in 2025-08-20 08:52:23 -07:00
Robin Sommer
c149922d64 binpac: Fix compiler warning. 2025-08-20 08:52:23 -07:00
Jon Siwek
465fd6bd56 binpac: BIT-1829: throw exceptions for negative array length expressions 2025-08-20 08:52:23 -07:00
Jon Siwek
455e2fbac5 binpac: BIT-1829: throw exceptions for excessive array sizes
For arrays with unknown element size, it used to cap the array length to
be the maximum it could be for the given input buffer, assuming 1-byte
elements.  An exception is instead now raised for cases where the
maximum array size (number of elements) exceeds what's possibly in the
buffer.  Using an exception versus capping the length may help prevent
protocol analyzers from unintentionally accessing array indices that
were not actually parsed even if the evauluated-length-expression for
that given array implies it may have been.
2025-08-20 08:52:23 -07:00
Jon Siwek
5a688c2730 binpac: Migrate fmt() usage to strfmt()
The former is easy to misuse by accidentally storing the contents of
the temporary string return value and accessing it later.  There's also
potential pitfalls in changing it to return a pointer into a static
buffer, so instead start using strfmt() uniformly across the codebase
and change some methods to use strings instead of char*.
2025-08-20 08:52:23 -07:00
Jon Siwek
0a05aa92fc binpac: Improve parsing of known-length, static-size arrays
In this case, the bounds checking for individual elements can be
optimized out of the parsing-loop in favor of a single, array-wide
bounds check beforehand.
2025-08-20 08:52:23 -07:00
Jon Siwek
d6fc439c21 binpac: GH-4: fix premature loop termination when parsing known-length arrays
For arrays with a length expression (e.g. uint16[size] instead of
uint16[]), the parsing loop would consider reaching the end of the
data buffer as a successful loop termination condition even if it's
not yet parsed the required number of elements.

Now, for such arrays, the loop will only terminate based on the loop
counter (derived from the length expression) or else it will throw an
OOB exception when trying to parse an element and finding not enough
data in the buffer.

Credit to Tomas Bortoli for reporting the problem and proposing
patches.
2025-08-20 08:52:23 -07:00
Jon Siwek
9c61eefe0d binpac: BIT-1829: fix &length suppressing boundary checks for array elements
It should only suppress the parsing-loop boundary check in the case
where array elaments are a single byte in length and thus covered by
the boundary check (generated as a result of &length) that is placed
before the parsing-loop.
2025-08-20 08:52:23 -07:00
Jon Siwek
39547dccec binpac: BIT-1914: emit deprecation warning for &check usages 2025-08-20 08:52:23 -07:00
Jon Siwek
8a1c8db02e binpac: BIT-1914: move &check implementation to new &enforce attribute
&check returns to being a no-op to avoid unintentionally
breaking existing code.
2025-08-20 08:52:23 -07:00
Jon Siwek
0ecf7755ea binpac: BIT-1914: Implement &check
Patch submitted by Antoine
2025-08-20 08:52:23 -07:00
Jon Siwek
0b84838465 binpac: Fix an uninitialized member warning 2025-08-20 08:52:23 -07:00
giralt
5cfbefca7c binpac: Extends BinPAC to support arbitrary line breakers via &linebreaker attribute
This feature is needed to run the FIX ASCII analyzer: https://github.com/reservoirlabs/fix-ascii
2025-08-20 08:52:23 -07:00
Johanna Amann
827d1ff11e binpac: Fix integer overflow in binpac generated code.
The issue is that t_begin_of_data + %s can sometimes overflow.

Bug reported and patch proposed by
Philippe Antoine <p.antoine@catenacyber.fr> from Catena cyber.
2025-08-20 08:52:23 -07:00
Johanna Amann
e4c168836e binpac: Allow more than one &require attribute on a field.
Patch by François Pennaneach <francois.pennaneach@free.fr>.

BIT-1753 #close
2025-08-20 08:52:23 -07:00
Bryon Gloden, CISSP®
ffe92f487f binpac: Update pac_parse.yy
(error) Memory leak: msgbug

Found by https://github.com/bryongloden/cppcheck
2025-08-20 08:52:23 -07:00
Bartolo Otrit
f1239143cb binpac: Bug fix of pac_swap function with int32 type of argument.
How to reproduce:

>>>code
int32 n = 0xF71B0000;
int32 n1 = pac_swap(n);
code<<<

n1 becomes 0xFFFFFFF7 instead of 0x00001BF7

Reason: Undefined behaviour after bit shift operation because of
negative value of the argument. See C++ standard (2011) 5.8.2 (Shift
operators).
2025-08-20 08:52:23 -07:00
Robin Sommer
d73e3485c1 binpac: Fixing Coverity warning. 2025-08-20 08:52:23 -07:00
Vlad Grigorescu
dc49b0343f binpac: Add a comment in the generated C++ code for fall through in switch
A common BinPAC construct for parsing records is a switch statement,
with no breaks between the cases, as control is expected to fall
through.

Coverity raises an error about this; this commit should fix that.
2025-08-20 08:52:23 -07:00
Seth Hall
a9d294528d binpac: Fixed compiler complaining about recursive function. 2025-08-20 08:52:23 -07:00
Robin Sommer
e3e4453dac binpac: Adding missing include. 2025-08-20 08:52:23 -07:00
Jon Siwek
ae20042943 binpac: BIT-1343: fix %include to work with relative paths 2025-08-20 08:52:23 -07:00
Jon Siwek
ded5abb01e binpac: BIT-1361: Improve boundary checks of records that use &length
Specifying &length on a record no longer skips generating boundary
checks for individual fields.  E.g. a record field that specifies a
&length that extends beyond the &length of the record containing it
should throw binpac::ExceptionOutOfBound, the usual way of handling
out-of-bounds conditions.
2025-08-20 08:52:23 -07:00
Jon Siwek
8648820497 binpac: Fix potential out-of-bounds memory reads in generated code.
Field lengths derived from other data in the input could potentially
lead to reading from outside the bounds of the input buffer.

Reported by John Villamil and Chris Rohlf - Yahoo Paranoids
2025-08-20 08:52:23 -07:00
Jon Siwek
db1c70b32e binpac: Separate declaration of binpac::init from definition.
When friend'ing a global function via qualified-id, Clang complains if
it's not been previously declared.
2025-08-20 08:52:23 -07:00
Robin Sommer
434f147932 binpac: Adding a new binpac::init() function that must be called by the host
before anything else.

Internally, this function compiles all regular expressions, avoiding
to that inside the regexp constructore. The code is a bit hackish due
to the way the regexp code depends on the Bro header.
2025-08-20 08:52:23 -07:00
Jon Siwek
498a5314ed binpac: Request format macros from inttypes.h explicitly.
This helps ensure the availability of PRI* macros from .pac files,
which cannot create this definition themselves since the inclusion
of binpac.h is hardcoded to be placed very early in the generated
code and already includes inttypes.h itself.
2025-08-20 08:52:23 -07:00
Jon Siwek
a5fb8e3787 binpac: Fix uninitialized (or unused) fields. 2025-08-20 08:52:23 -07:00
Jon Siwek
81bf65e148 binpac: Generate initialization code for external types.
Numeric/pointer types can be initialized to 0.
2025-08-20 08:52:23 -07:00
Jon Siwek
201b43f3be binpac: Optimize negative string length check.
Strings with a constant &length expression can be checked for negative
length values while generating the parser instead of in the parser
itself (which likely just ends up being dead code).
2025-08-20 08:52:23 -07:00
Jon Siwek
13e14768da binpac: Add virtual dtor to RefCount base class.
The code generated for types w/ &refcount will subclass RefCount and
Unref definitely deletes via a pointer to that base class so it needs a
virtual dtor.
2025-08-20 08:52:23 -07:00