Commit Graph

1126 Commits

Author SHA1 Message Date
Guy Harris f15b7b0ccc proto: fix proto_tree_add_bitmask_list_ret_uint64 to always return a value.
A "proto_tree_add..._ret_..." routine *must* return the value through
the pointer, even if no protocol tree is being built, as there's no
guarantee that a protocol tree will be built under all circumstances
(for example, if the dissection is only being done to generate the
column values, no column is a custom column, there are no coloring
rules, etc., so that none of the named field values are of interest, and
the protocol tree isn't going to be displayed, no protocol tree will be
built).

Fixes #18203.
2022-07-15 00:24:58 -07:00
Joakim Karlsson bf8577b88c pfcp: change to utilize proto_tree_add_bitmask_list 2022-07-14 12:46:09 +00:00
Chuck Craft e12954a637 epan: ws_debug log for heuristic that claims frame (len != 0)
It's possible for a dissector to claim a frame without adding to
the tree or being added to frame.protocols (see !6669)
Log a debug message showing the pinfo layers and the dissector that
claimed the tvb (frame/packet).
2022-07-12 14:15:33 +00:00
John Thacker 02b00a8ee5 epan: Copy multifield custom column undecoded values correctly
When writing a custom column, some field types can't have a resolved
value, and just copy the label from the expression to the value.
Only copy information from the most recent field when doing so,
so that with multifield custom columns the entire unresolved value
doesn't get overwritten with the resolved value (if some fields
have resolved values and some don't.) This also reduces copying
from O(N^2) to O(N).

Fixes the display "unresolved" value for multifield custom columns
that are a mix of field types.
2022-07-08 09:54:54 -04:00
John Thacker dd5e2f3b3f epan: Fix return value of prooto_strlcpy when not enough room
proto_strlcpy in normal situations returns the number of bytes
copied (because the return value of g_strlcpy is strlen of the
source buffer). It can copy no more than dest_size - 1, because
dest_size is the size of the buffer, including the null terminator.
(https://docs.gtk.org/glib/func.strlcpy.html)

Returning dest_size can cause offsets to get off by one and reach
the end of the buffer, and can cause subsequent calls to have
buffer overflows. (See #16905 for an example in the comments.)
2022-07-05 22:12:41 +00:00
David Perry 88a7bf9db2 Properly free range strings, ext strings, custom base 2022-07-05 20:43:31 +00:00
João Valverde b10db887ce dfilter: Remove unparsed syntax type and RHS literal bias
This removes unparsed name resolution during the semantic
check because it feels like a hack to work around limitations
in the language syntax, that should be solved at the lexical
level instead.

We were interpreting unparsed differently on the LHS and RHS.
Now an unparsed value is always a field if it matches a
registered field name (this matches the implementation in 3.6
and before).

This requires tightening a bit the allowed filter names for
protocols to avoid some common and potentially weird conflicting
cases.

Incidentally this extends set grammar to accept all entities.
That is experimental and may be reverted in the future.
2022-07-02 11:18:20 +01:00
João Valverde 0615ba6317 ftypes: Make accessor functions type safe 2022-06-20 17:29:57 +00:00
João Valverde 51de43cfd2 dfilter: Fix protocol slices with negative indexes
Field infos have a length property that was not stored with the
field value so when using a negative index the end was computed
from the captured length of the frame tvbuff, leading to incorrect
results. The documentation in wireshark-filter(5) describes how
this was supposed to work but as far as I can tell it never worked
properly.

We now store the length and use that (when it is different from -1)
to locate the end of the protocol data in the tvbuff. An extra wrinkle
is that sometimes the length is set after the field value is created.
This is the most common case as the majority of protocols have a
variable length and dissection generally proceeds with a TVB subset from
the current layer (with offset zero) through all remaining layers to the
end of the captured length. For that reason we must use an expedient to allow
changing the protocol length of an existing protocol fvalue, whenever
proto_item_set_len() is called.

Fixes #17772.
2022-05-23 23:04:07 +01:00
John Thacker f2fb1662b2 proto: Handle BASE_SPECIAL_VALS in add_bitmask_ title
Respect BASE_SPECIAL_VALS when adding to the title item of an
item added with the proto_tree_add_bitmask* functions.

Note that the documentation for the BMT_NO_INT flag has always
said that "only boolean flags are added to the title" and that
no integer based items are added, but the actual behavior has been
to add integer items with custom format functions and value strings.
2022-05-15 09:59:52 -04:00
John Thacker 1e7a600680 proto: Fix display of BASE_UNIT_STRING for 64 bit fields in bitmask
When integer fields are displayed in the bitmask header item in
proto_tree_add_bitmask_tree and hf->strings is set, only the string
from the value_string is used, not the integer value, to save space.

However, that means that BASE_UNIT_STRING fields have to be treated
differently from all the other fields with hf->strings set. If not,
then only the units are displayed instead of the number with the units.

Fields based on 32 bit integers were already being handled correctly.
Use that same logic for fields based on 64 bit integers.
(See commit 24d991dab4 for something similar.)
2022-05-14 15:14:22 -04:00
John Thacker a98391e316 proto: Fix reversed test for signed ints with unit strings
In proto_item_add_bitmask_tree, on the signed integer path, the
test for if the display uses a unit string is clearly reversed,
calling it only if BASE_UNIT_STRING is unset. Use the correct
test from the unsigned integer path.
2022-05-14 09:26:20 -04:00
John Thacker 8a872d6142 proto: Add support for BASE_SPECIAL_VALS to fields with bitmasks
Add support for BASE_SPECIAL_VALS to fill_label_bitfield[64], for
fields with a nonzero bitmask, using the same logic as
fill_label_number[64].

There's at least one dissector (packet-ipmi-se.c) that was trying
to use this already, but silently had no effect.
2022-05-13 21:02:54 -04:00
João Valverde b602911b31 dfilter: Add support for universal quantifiers
Adds the keywords "any" and "all" to implement the quantification
to any existing relational operator.

Filter: all tcp.port in {100, 2000..3000}

Syntax tree:
 0 ALL TEST_IN:
   1 FIELD(tcp.port)
   1 SET(#2):
     2 FVALUE(100 <FT_UINT16>)
     2 FVALUE(2000 <FT_UINT16>) .. FVALUE(3000 <FT_UINT16>)

Instructions:
00000 READ_TREE		tcp.port -> reg#0
00001 IF_FALSE_GOTO	5
00002 ALL_EQ		reg#0 === 100 <FT_UINT16>
00003 IF_TRUE_GOTO	5
00004 ALL_IN_RANGE	reg#0 in { 2000 <FT_UINT16> .. 3000 <FT_UINT16> }
00005 RETURN
2022-05-12 14:26:54 +01:00
João Valverde d517feee74 epan: Add more bookkeeping for layers
Packet info already contains the notion of layer depth for the
current protocol, among all the protocols in the frame. This
adds an extra layer number for the protocols that are the same
as the current one. Obviously this will only go above one if
the protocol is repeated in the stack, such as with IP tunneling.

Adds extra logic to track numbers for each protocol in the frame
and update them when calling a dissector.

The total layer number and protocol layer number are store in
the field info structure so they can be used after dissection,
namely by display filters.
2022-04-26 16:50:59 +00:00
John Thacker 7a97a1dc22 epan: Add comments about _get_parent, _set_len and faked items
If we're faking items, then proto_[item|tree]_get_parent[_nth] return
the parent of the faked item, which may not be what we want. We have
no way of knowing if the logical item meant was the faked item itself
or one of its children that share the same proto_item when faked.

Thus we don't know if we should return the proto_item itself or its
parent when called on a possibly faked item. Most of the time we will
be adding new items to what we return here, which means not faking items
that could be faked (since we might be returning the root node, which
doesn't have a field_info), hurting performance (see #8069).

It can also have some unusual effects on the protocol hierarchy stats,
particularly if we change things so that non-visible items can change
their length, which has a similar issue. (#17877)
2022-04-20 21:30:34 +00:00
Chuck Craft 4e0cd3dbd2 epan: add ENC_TIME_USECS timestamp encoding
Needed to format timestamp in #18038 - packet-cql.c
Mirrors changes made in !1924 - Add ENC_TIME_NSECS timestamp encoding
Documentation in README.dissector, proto.c, proto.h - could use
refresh in a different merge request.
2022-04-14 15:18:30 +00:00
João Valverde 260942e170 dfilter: Refactor macro tree references
This replaces the current macro reference system with
a completely different implementation. Instead of a macro a reference
is a syntax element. A reference is a constant that can be filled
in the dfilter code after compilation from an existing protocol tree.
It is best understood as a field value that can be read from a fixed
tree that is not the frame being filtered. Usually this fixed tree
is the currently selected frame when the filter is applied. This
allows comparing fields in the filtered frame with fields in the
selected frame.

Because the field reference syntax uses the same sigil notation
as a macro we have to use a heuristic to distinguish them:
if the name has a dot it is a field reference, otherwise
it is a macro name.

The reference is synctatically validated at compile time.

There are two main advantages to this implementation (and a couple of
minor ones):

The protocol tree for each selected frame is only walked if we have a
display filter and if the display filter uses references. Also only the
actual reference values are copied, intead of loading the entire tree
into a hash table (in textual form even).

The other advantage is that the reference is tested like a protocol
field against all the values in the selected frame (if there is more
than one).

Currently the reference fields are not "primed" during dissection, so
the entire tree is walked to find a particular reference (this is
similar to the previous implementation).

If the display filter contains a valid reference and the reference is
not loaded at the time the filter is run the result is the same as a
non existing field for a regular READ_TREE instruction.

Fixes #17599.
2022-03-29 12:36:31 +00:00
João Valverde b9b45a4a8f dfilter: Add ftypes pseudofields
This adds a _ws.ftypes namespace with protocol fields with all
the existing field types.

Currently this is only useful to debug the display filter compiler,
without having to find a real protocol field with the desired type.

Later it may find other uses.
2022-03-28 15:42:32 +01:00
John Thacker d7f3612613 proto: Fix comment on NTP Era 1 Epoch
NTP Era 1 begins on 7 February 2036, 06:28:16 UTC, exactly when
the 64 bit fixed point timestamp rolls over. See RFC 4330/5905 (and
the correct comments later in get_time_value). Fix the comment where
the constant is defined (the value is already correct, however.)
2022-03-25 17:16:54 -04:00
Dario Lombardo 9012722f9b elastic: fix mapping with recent es versions. 2022-03-14 08:34:48 +00:00
Gerald Combs 8575914213 epan: Make sure we always set our return values.
Make sure we always set a return value in our various
proto_tree_add_item_ret_* routines. Fixes #17994.
2022-03-12 01:52:56 +00:00
Uli Heilmeier 33f5b9e145 Proto: Return NULL for 0 or lower -1 length items
Return NULL when an item with length zero or lower -1 is added to
the tree.

With this the calling dissector doesn't have to check the length and
there is no Dissector bug reported.

Related to #17890
2022-02-25 09:53:14 +00:00
Gerald Combs fbf403de00 epan: Always set our proto_item_fill_label label.
Make sure label_str is valid, and print a warning if it's NULL. Try to
fix

```
/builds/wireshark/wireshark/epan/dissectors/packet-diameter.c: 1174 in integer32_avp()
1168     	gint length = tvb_reported_length(tvb);
1169     	if (length == 4) {
1170     		if (c->tree) {
1171     			pi= proto_tree_add_item(c->tree, a->hf_value, tvb, 0, length, ENC_BIG_ENDIAN);
1172     			label = (char *)wmem_alloc(wmem_packet_scope(), ITEM_LABEL_LENGTH+1);
1173     			proto_item_fill_label(PITEM_FINFO(pi), label);
>>>     CID 1499506:  Memory - illegal accesses  (STRING_NULL)
>>>     Passing unterminated string "label" to "strstr", which expects a null-terminated string.
1174     			label = strstr(label,": ")+2;
1175     		}
1176     	}
1177     	else {
1178     		pi = proto_tree_add_bytes_format(c->tree, hf_diameter_avp_data_wrong_length,
1179     						 tvb, 0, length, NULL,

```
2022-02-18 17:26:40 +00:00
João Valverde 8608a432ee Epan: Add some missing reserved keywords 2022-02-17 20:50:30 +00:00
Guy Harris 1e27fe59fd Clean up checking for invalid field definitions.
Add comments to indicate what types of display information various field
types are allowed.

Make the error messages for fields that only allow some particular
display information types specific to those types, rather than saying
"no field information allowed".  This also gets rid of some
fallthroughs, one of which allows BASE_PROTOCOL_INFO for floating-point
types, which makes no sense.
2022-02-07 22:01:45 -08:00
Gerald Combs a8c8ee6908 epan: Fix our FT_UINT_BYTES and FT_UINT_STRING checks.
FT_UINT_BYTES and FT_UINT_STRING lengths are always at least the width
of the count bytes. Fix for 1136ce9610 / #17891.
2022-02-08 02:29:50 +00:00
John Thacker 25d0c88251 epan: Add BASE_SHOW_UTF_8_PRINTABLE
Add BASE_SHOW_UTF_8_PRINTABLE and related function tvb_utf_8_isprint
for supporting fields of bytes that are "maybe UTF-8" (default or
SHOULD be UTF-8 but could be something else, with no encoding indicator),
such as SSID fields in IEEE 802.11 (See #16208), certain OctetString
fields in Diameter or PFCP, and other places where
BASE_SHOW_ASCII_PRINTABLE is currently used. Fix #5307
2022-02-06 00:32:13 +00:00
Gerald Combs 1136ce9610 epan: Check our FT_UINT_BYTES and FT_UINT_STRING lengths.
Add length checks to get_full_length for FT_UINT_BYTES and
FT_UINT_STRING items. Fixes #17891.
2022-02-03 23:05:00 +00:00
Guy Harris 18748abb5e Fix handling of dissector registration errors.
Report all registration errors with REPORT_DISSECTOR_BUG().

In the workers for register_all_protocols() and
register_all_protocol_handlers(), use TRY/CATCH/ENDTRY to catch
DissectorError exceptions thrown by REPORT_DISSECTOR_BUG() when
registering dissectors.  Return the error message from the main thread
routine and, when joining the worker thread, if there's an error message
returned, throw it in the current thread, so that it gets caught by the
main libwireshark initialization code.

Fixes the crash in #17856.
2022-01-11 19:56:14 +00:00
Benedikt Heumüller 893290c603 Extended range_string to 64 bit 2022-01-05 09:14:46 +00:00
João Valverde a566076839 epan: Move time display types to field_display_e
This makes it easier to understand the code, avoids conflicts
and ugly and unnecessary casts.

The field display enum has evolved over time from integer types
to a type generic parameter.
2021-12-27 22:31:31 +00:00
Jaap Keuter f151e1b0c2 Refactor VARINT handling 2021-12-23 14:27:30 +00:00
Jaap Keuter 1b5acc8d57 Replace ENC_VARIANT_MASK by ENC_VARINT_MASK 2021-12-22 20:14:31 +00:00
Moshe Kaplan 69d54d6f8e Corrects repeated words throughout the code.
Repeated words were found with:
egrep "(\b[a-zA-Z]+) +\1\b" . -Ir
and then manually reviewed.
Non-displayed strings (e.g., in comments)
were also corrected, to ease future review.
2021-12-22 11:01:11 +00:00
Jaap Keuter 6fa56bca0c Remove remnants of bygone GTK era 2021-12-22 06:02:10 +00:00
João Valverde c5a19582e4 epan: Convert to use stdio.h from GLib
Replace:
    g_snprintf() -> snprintf()
    g_vsnprintf() -> vsnprintf()
    g_strdup_printf() -> ws_strdup_printf()
    g_strdup_vprintf() -> ws_strdup_vprintf()

This is more portable, user-friendly and faster on platforms
where GLib does not like the native I/O.

Adjust the format string to use macros from intypes.h.
2021-12-19 19:29:53 +00:00
John Thacker a68be23b7e proto: Don't crash on adding 64 bit BASE_CUSTOM column
Similar to elsewhere, in proto_custom_set don't call hf_try_val[64]_to_str
if BASE_CUSTOM is set, even if hfinfo->strings is non-null. Fix #17762.
2021-12-12 21:22:57 +00:00
João Valverde 6c5d00a746 epan: Remove obsolete function proto_register_fields_manual()
Related with #17774.
2021-12-11 17:02:16 +00:00
João Valverde d2a9cb940a epan: Remove new proto tree API
Remove experimental new API.

Fix Netlink dissector to compile with normal proto tree API.

Closes #17774.
2021-12-10 14:37:01 +00:00
João Valverde 19dcb725b6 epan: Remove STR_ASCII and STR_UNICODE
These display bases work to replace unprintable characters so the
name is a misnomer. In addition they are the same option and this
display behaviour is not something that is configurable.

This does not affect encodings because all our internal text strings
need to be valid UTF-8 and the source encoding is specified using
ENC_*.

Remove the assertion for valid UTF-8 in proto.c because
tvb_get_*_string() must return a valid UTF-8 string, always, and we
don't need to assert that, it is expensive.
2021-12-03 04:35:56 +00:00
John Thacker aadf4efcbe epan: Add ENC_ISO_8601_DATE_TIME_BASIC
Add the ISO 8601 Basic date time format as another string time
option. This could be used for e.g. ASN.1 GeneralizedTime.
Add tests for it.
2021-12-02 14:19:49 +00:00
AndersBroman c6e6f21685 proto.c: Increase PROTO_PRE_ALLOC_HF_FIELDS_MEM 2021-11-30 18:36:30 +00:00
João Valverde 1a32a75a62 ftypes: Internal headers need to be internal
The header ftypes-int.h should not be used outside of epan/ftypes
because it is a private header.

The functions fvalue_free() and fvalue_cleanup() need not and should
not be macros either.
2021-11-11 03:15:31 +00:00
João Valverde b49abcb215 epan: Remove fvalue_string_repr_len() from the public API
This function is unnecessary. Clients are receiving a wmem-allocated
buffer and have no need to know the length apriori.
2021-11-10 16:01:21 +00:00
João Valverde 1fc621e38d epan: Fix crash with upper-case protocol filter names
Registering a preference module for a protocol filter name with
upper case letters aborts the program. Relax this restriction to
conform with the rules for protocols. The recommendation is still
to use all lower-case letters.

Fixes 070aeddf76.
2021-11-04 16:29:34 +00:00
João Valverde 01f234571f epan: Optimize heuristic name validity check
Do the name check in one pass only, instead of two passes, one
for all letters and a second one to exclude upper case letters.
2021-11-04 14:03:37 +00:00
João Valverde 070aeddf76 Lift restriction on upper case protocol display filter names
Unlike other header fields in filter expressions protocol names
cannot contain upper-case letters. Remove that restriction. This
should make start-up slightly faster as it remove an extra loop
for each protocol filter name.

This was added in 9ead15a6eb but
I don't see a reason to have different rules for protocols and
fields, it seems the README.developer was just being vague and
conflating PROTOABBREV with PROTOFILTERNAME.

The recommendation for lower case is a style recommendation,
and it's a good one, but it should be applied uniformly. As
long as we are not enforcing this for all field filter values
there is no point in enforcing it just for protocol names and
actually it is detrimental, e.g:

hi2operations
HI2Operations.IRIsContent
HI2Operations.UUS1_Content_element
HI2Operations.iRIContent
HI2Operations.iRISequence
HI2Operations.IRIContent
HI2Operations.iRI_Begin_record_element
HI2Operations.iRI_End_record_element
HI2Operations.iRI_Continue_record_element
HI2Operations.iRI_Report_record_element
(...)

It's weird and unexpected to have this difference and there is
no technical reason to require it. What we should probably do
is not include the protocol name in the FIELDFILTERNAME and
have the registration mechanism append it to the PROTOFILTERNAME.

Also disallow leading '-' everywhere in filter names, not just
protocol filter names. It's a universal requirement.
2021-11-02 08:35:24 +00:00
João Valverde efdf8f7d01 proto: Fix reserved filter name check
Hash table keys cannot be compared directly.

Fixes 6d4a463620.
2021-10-25 09:29:40 +01:00
João Valverde 0abe10e040 dfilter: Fix "!=" relation to be free of contradictions
Wireshark defines the relation of equality A == B as
A any_eq B <=> An == Bn for at least one An, Bn.
More accurately I think this is (formally) an equivalence
relation, not true equality.

Whichever definition for "==" we choose we must keep the
definition of "!=" as !(A == B), otherwise it will
lead to logical contradictions like (A == B) AND (A != B)
being true.

Fix the '!=' relation to match the definition of equality:
  A != B <=> !(A == B) <=> A all_ne B <=> An != Bn, for
every n.

This has been the recomended way to write "not equal" for a
long time in the documentation, even to the point where != was
deprecated, but it just wasn't implemented consistently in the
language, which has understandably been a persistent source
of confusion. Even a field that is normally well-behaved
with "!=" like "ip.src" or "ip.dst" will produce unexpected
results with encapsulations like IP-over-IP.

The opcode ALL_NE could have been implemented in the compiler
instead using NOT and ANY_EQ but I chose to implement it in
bytecode. It just seemed more elegant and efficient
but the difference was not very significant.

Keep around "~=" for any_ne relation, in case someone depends
on that, and because we don't have an operator for true equality:
  A strict_equal B <=> A all_eq B <=> !(A any_ne B).
If there is only one value then any_ne and all_ne are the same
comparison operation.

Implementing this change did not require fixing any tests so it
is unlikely the relation "~=" (any_ne) will be very useful.

Note that the behaviour of the '<' (less than) comparison relation
is a separate, more subtle issue. In the general case the definition
of '<' that is used is only a partial order.
2021-10-24 06:55:54 +00:00