]> The Tcpdump Group git mirrors - libpcap/commitdiff
Fix the != comparison for ATM and MTP field values.
authorDenis Ovsienko <[email protected]>
Fri, 10 Jan 2025 19:15:05 +0000 (19:15 +0000)
committerDenis Ovsienko <[email protected]>
Fri, 10 Jan 2025 22:45:21 +0000 (22:45 +0000)
Trying to test "sio", "dpc", "opc", "sls", "hsio", "hdpc", "hopc",
"hsls", "vpi" or "vci" for being not equal to an integer produces
bytecode that does exactly the opposite of what is required:

$ filtertest MTP2 'sls != 8'
(000) ldb      [7]
(001) and      #0xf0
(002) jeq      #0x80            jt 3 jf 4
(003) ret      #262144
(004) ret      #0
$ filtertest SUNATM 'vci != 0x1234'
(000) ldh      [2]
(001) jeq      #0x1234          jt 2 jf 3
(002) ret      #262144
(003) ret      #0

All other comparisons for these fields work as expected.  The problem
occurs when jtype == BPF_JEQ and reverse == 1 in gen_ncmp(), which
reverses the logical meaning only when jtype is one of {BPF_JGT,
BPF_JGE}.  This bug has been in place since the initial
implementations in 2002 for ATM (commit 243b20e) and in 2005 for MTP
(commit a0a4852).

Make the reversal in gen_ncmp() nonspecific to jtype, same as it is in
gen_relation_internal().  This should not cause undesired side effects
because in this context reverse == 1 only if the caller is:
* gen_cmp_lt(), in which case jtype == BPF_JGE; or
* gen_cmp_le(), in which case jtype == BPF_JGT; or
* gen_mtp2type_abbrev(), in which case jtype == BPF_JGT; or
* gen_atmfield_code_internal(), in which case jtype is one of {BPF_JGT,
  BPF_JGE, BPF_JEQ} via "atmvalue: ... irelop NUM" in grammar.y.in; or
* gen_mtp3field_code_internal(), idem via "mtp3value:" ibid.
This way, without jtype the reversal does exactly what it needs to do,
and nothing else.

$ filtertest MTP2 'sls != 8'
(000) ldb      [7]
(001) and      #0xf0
(002) jeq      #0x80            jt 3 jf 4
(003) ret      #0
(004) ret      #262144
$ filtertest SUNATM 'vci != 0x1234'
(000) ldh      [2]
(001) jeq      #0x1234          jt 2 jf 3
(002) ret      #0
(003) ret      #262144

CHANGES
gencode.c

diff --git a/CHANGES b/CHANGES
index 1e5807ed8daa56efadd720c09880f27c968eb5fe..8ea1a7053c67638c0e796e79207ec8ad1237d539 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -46,6 +46,7 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group
       Require "vpi" and "vci" values to be within valid ranges.
       Initialize the scratch memory store to 0.
       Require "[wlan] dir" integer value to be within range.
+      Fix the != comparison for ATM and MTP field values.
     rpcap:
       Support user names and passwords in rpcap:// and rpcaps:// URLs.
       Add a -t flag to rpcapd to specify the data channel port; from
index fe71c9ccfe038119edcbc833e82e1212d0b64e6d..870abff8d77ccf0594789542b9ae2a62230a5d5c 100644 (file)
--- a/gencode.c
+++ b/gencode.c
@@ -1284,7 +1284,7 @@ gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
        b = new_block(cstate, JMP(jtype));
        b->stmts = s;
        b->s.k = v;
-       if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
+       if (reverse)
                gen_not(b);
        return b;
 }