Skip to content

MultipleTypeField: wrong initialization #1484

@gpotter2

Description

@gpotter2

There is a bug with MultipleTypeField: when a packet containing it is initialized with a value set to MultipleTypeField, the any2i function is executed to set the value to the field before other fields of the same packet are initialized.

In the case where any2i changes the type of the input, it creates a bug. See demo below.

class DebugPacket(Packet):
    fields_desc = [
        ByteEnumField("atyp", 0x1, {0x1: "IPv4", 0x3: "DNS", 0x4: "IPv6"}),
        MultipleTypeField(
            [
                # IPv4
                (IPField("addr", "0.0.0.0"), lambda pkt: pkt.atyp == 0x1),
                # DNS
                (DNSStrField("addr", ""), lambda pkt: pkt.atyp == 0x3),
                # IPv6
                (IP6Field("addr", "::"), lambda pkt: pkt.atyp == 0x4),
            ],
            StrField("addr", "")
        ),
    ]
  • First test (OK)
a = DebugPacket(atyp=0x3, addr="scapy.net")
a = DebugPacket(raw(a))
a.addr

Returns:
b'scapy.net.'

  • Second test (Fails)
a = DebugPacket(addr="scapy.net", atyp=0x3)
a.addr

Returns:
Net('scapy.net')

Ways of fixing this

  • Adding some complicated extra metric (see the PR)
  • Throwing an error on building when we detect a wrong order of the init fields

@p-l-

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions