Commit graph

123 commits

Author SHA1 Message Date
Tim Wojtulewicz
9f3750d0cc binpac: Apply clang-tidy's modernize-use-nullptr check 2025-08-20 08:52:24 -07:00
Vern Paxson
49a96f5216 binpac: address linter warnings about use of sprintf() 2025-08-20 08:52:23 -07:00
Tomer Lev
d5f2c9c3a8 binpac: Only adding subdir if running standalone 2025-08-20 08:52:23 -07:00
Tomer Lev
9a4e01e634 binpac: Additional Windows fixes. Fixed wrong MSVC macro definition and std::filesystem invocation. Linking to libunistd library 2025-08-20 08:52:23 -07:00
Arne Welzel
60265b8ce7 binpac: pac_scan: Exit with failure for include errors
Elsewhere (zeek/zeek#2482), it was observed that when binpac encounters
include failures, it still exits with 0 indicating success. Subsequent
compilation of the produced .h and .cc files likely fails.

Exit with 1 on include errors to make pin pointing issues easier by
having make/ninja stop earlier.
2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
d21f99ef2b binpac: Wrap native dirname() call in ifdef, call std::filesystem on Windows 2025-08-20 08:52:23 -07:00
Elad Solomon
faa1b7abbf binpac: Adapted binpac to compile with MSVC for Windows environment. 2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
dd3737b5c8 binpac: Apply bits of nadi-bittorrent patch that aren't applied 2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
090325df40 binpac: Add pre-commit hooks and run clang-format on everything 2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
090ac0a6e0 binpac: Remove already-applied binpac-* patches 2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
6034744a0c binpac: Rename files in patches to match current directory structure 2025-08-20 08:52:23 -07:00
Seth Hall
c010152340 binpac: CMAKE_CFG_INTDIR is no longer necessary.
CMake documentation says that CMAKE_CFG_INTDIR is no longer necessary to
find the right binary for the configuration and is in fact deprecated in
recent versions of CMake.
2025-08-20 08:52:23 -07:00
Seth Hall
024b4ff8f3 binpac: Updates for building Zeek as a submodule 2025-08-20 08:52:23 -07:00
Jon Siwek
508e78ebe9 binpac: Prevent use of LeakSanitizer on FreeBSD 2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
f365c253a7 binpac: Fix LGTM findings 2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
5d75f8ed6c binpac: Remove usage of ZEEK_FORWARD_DECLARE_NAMESPACED macro 2025-08-20 08:52:23 -07:00
Jon Siwek
b1101e6e16 binpac: Fix #include of RE.h to use zeek/RE.h 2025-08-20 08:52:23 -07:00
Jon Siwek
c5fe0eaa17 binpac: GH-14: Use larger temporary storage for evaluating case-expressions
For example:

    inum: uint32 = case (ed & 0x0f) of {
        0x00    -> n_8;  # n_8 is a uint8
        0x01    -> n_16; # n_16 is a uint16
        0x02    -> n_32; # n_32 is a uint32
        default -> 0;
    };

Previously, the temporary storage used for evaluating the
case-expression was based on whatever type the first case yields, which
is a uint8 in the above example.  That behavior can lead to a narrowing
conversion whenever the 0x01 or 0x02 cases occur.

The new behavior is to base the temporary storage's type on the largest
numeric type that the case-expression can yield, which is uint32 in the
above example.
2025-08-20 08:52:23 -07:00
Jon Siwek
11b6feb18b binpac: Update minimum required CMake to 3.5 2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
f1d7d0c80d binpac: Move RE_Matcher to zeek namespace 2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
cd7a2e9783 binpac: Squash two rules into one with a wildcard 2025-08-20 08:52:23 -07:00
Tim Wojtulewicz
a7f14ed423 binpac: Allow namespaced names as typenames for function return values 2025-08-20 08:52:23 -07:00
Jon Siwek
9de6212dda binpac: Remove use of Variable-Length-Arrays
Related to https://github.com/zeek/zeek/issues/895
2025-08-20 08:52:23 -07:00
Jon Siwek
db7c3d7c5c binpac: Fix incorrect boundary checks in flowbuffer frame length parsing
Incremental flowbuffer parsing sought to first parse the "minimum header
length" required to get the full frame length, possibly from a record
field, but generating the logic to parse that field could greedily
bundle in additional boundary-checks for all subsequent fields of
known-size.

E.g. for flowunit parsing of this:

    type HDR = record {
        version:    uint8;
        reserved:   uint8;
        len:        uint16;
    } &byteorder=bigendian;

    type FOO_PDU(is_orig: bool) = record {
        hdr:        HDR;
        plen:       uint8;
        ptype:      uint8;
        something:  bytestring &restofdata;
    } &byteorder=bigendian, &length=hdr.len;

The flowbuffer was correctly seeking to buffer 4 bytes and parse the
"hdr.len" field, but the generated parsing logic for "hdr.len" included
a boundary check all the way up to include "plen" and "ptype".

This causes out-of-bounds exceptions to be thrown for inputs that should
actually be possible to incrementally parse via flowbuffer.
2025-08-20 08:52:23 -07:00
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