scaramanga::l33t p0st

Resynth v0.7.0 is Released

Resynth v0.7.0 adds whole-program checking, better diagnostics, and fixes a long list of bugs.

resynth v0.7.0 changes how a program is compiled. Earlier versions read a .rsyn file a line at a time and ran each statement as soon as it was parsed. Now the whole program is lexed, parsed and type-checked before the pcap is opened, then run by a new interpreter. Most mistakes, such as a typo in an argument name or a value of the wrong type, are now reported with a precise location before any packets are written. Before, they showed up at run time, after half the pcap had been written.

This release also fixes a long list of packet-encoding bugs, found by a new test suite that reads every header back and checks it byte by byte.

Upgrading from v0.6.0? Running an unchanged program will probably produce a different pcap. Most of the differences are bug fixes, but some are deliberate changes to the defaults. Read Your pcaps will change before regenerating any reference captures.

Your pcaps will change

If you compare generated pcaps against stored copies, expect differences from all of the following:

What v0.6.0 v0.7.0
Capture start time 1970-01-01 00:00:00 UTC 1981-08-15 00:00:00 UTC, configurable with --start-time
Timestamps of packets from one statement (e.g. flow.open()) all stamped with the time after the last packet each packet has its own timestamp
Integer literal used as bytes, e.g. 300 8 bytes: 00 00 00 00 00 00 01 2c the narrowest width that holds it: 01 2c
true / false used as bytes type error 01 / 00
eth::from_ip(10.0.0.1) and every MAC derived from an IP 00:02:0a:00:00:01 (a real vendor's OUI) 02:00:0a:00:00:01 (locally administered, as documented)
Ethernet source of UDP frames 00:00:00:00:00:00 the client's MAC
pcap snaplen 0 262144 (256 KiB)

he bug fixes below also change output for TCP RSTs, UDP and IP checksums, DNS, DHCP, ARP, IP fragments and ERSPAN.

Language changes

Whole-program checking

  • Lexing, parsing and type checking all happen before the output file is created. A syntax or type error no longer leaves a partial pcap behind. Only errors that can happen only at run time, such as io::file() failing to open a file, can still stop a run part way.

  • The checker reports every error in the file, at most one per statement, followed by a count. Uses of a name whose let or import failed are not reported again.

  • Errors say which phase they occurred at, and point at the token responsible:

    late.rsyn:3:44: semantic error: unicast: unknown argument "bogus"
    late.rsyn: semantic error: 1 error
    
  • A parse error lists every token that would have been valid, as in expected one of `(`, `.`, `/` or `;`, found `2` .

  • Passing too many arguments to a function that takes no extra arguments is now an error. Before, io::file() and the IpFrag methods panicked.

Integers

  • Integer literals no longer have a fixed type. Each literal takes its type from where it is used:
    • Passed to an integer parameter, it is narrowed to that type. If the value doesn't fit, it is truncated with a warning.
    • Passed as bytes, it becomes the narrowest of u8, u16, u32 and u64 that holds it.
  • let p = 80; doesn't fix a type either, so p can be used both as a u8 and as a port.
  • Passing a wider typed value to a narrower parameter gives a may be truncated warning. For example, std::be16(text::len(x)) now warns, because text::len returns a u64. A future version may support casts which will silence these warnings.
  • To get a specific width and byte order, use std::u8(), std::be16(), std::le32() and so on, as before.

Syntax

  • . and calls are now postfix operators, so chains like ipv4::tcp::flow(...).open(), f()() and x.a.b parse and execute.
  • Any expression can be a statement. "abc"; warns about a discarded value instead of being a parse error.
  • Whitespace is allowed around the : in a socket address literal since it's now an operator. Previously 1.2.3.4:123 was a single token literal, and / was the operator version. Now they're both just operators and const-propagation gets you a constant sockaddr however you spell it.

Literals are stricter

  • Decimal integers can't have leading zeroes, so 010 is an error, because it would read as octal in other languages. A number with letters stuck to it, like 123abc, is now one bad token instead of a number followed by a name.
  • Negative literals like -1 were never supported, and are now reported as a lex error.
  • IPv4 literals with leading zeroes, or that otherwise don't parse, give invalid IPv4 literal.
  • Ports in a.b.c.d:port above 65535 are an error. Before, they wrapped. Ports on the right of / are still truncated, but now with a warning.
  • String literals may contain only printable ASCII. Anything else, such as é or a control character, is rejected, and the error suggests the hex escape to use instead (|c3 a9|).
  • Hex escapes are checked when the file is lexed. A |...| sequence can no longer be split across two adjacent strings, so "|13" "37|" is an error.

Warnings

Warnings now use the same file:line:col: format as errors. They include multiple imports of "ipv4", discarded value of type u8, integer 70000 truncated to u16, and value of type u64 may be truncated to u16. Existing programs may start printing some of these.

Documentation

docs/grammar.md has been rewritten as a formal description of the language. It covers the lexical and syntactic grammar in ABNF, names and scope, types and typing rules, integer narrowing, call binding, statements and evaluation.

Command line

  • Output files are written next to their inputs by default. foo/a.rsyn now writes foo/a.pcap. Before, every pcap went to the current directory.
    • -o FILE is only allowed with a single input. The old form a.rsyn -o a.pcap b.rsyn -o b.pcap is no longer supported.
    • --out-dir DIR collects every output in one directory.
    • Only the last extension is replaced, so a.b.rsyn becomes a.b.pcap (it used to become a.pcap).
    • An input path with no file name is reported as an error instead of causing a panic.
  • New --start-time sets when the capture starts. It takes Unix seconds or RFC 3339 (2024-01-01T00:00:00Z), and the default is 1981-08-15T00:00:00Z. Pass --start-time 0 to get the old behaviour. The reason for this change is that some programs interpret pcap timestamps of zero as "unset", so the change avoids that ambiguity.
  • Read from stdin: resynth -o out.pcap < prog.rsyn.
  • REPL (experimental): a build with --features repl, run with no input files on a terminal, starts an interactive prompt with syntax highlighting. It lexes, parses and checks what you type, prints errors with carets under the offending source, and shows the checked program. It doesn't generate packets yet.
  • --output-docs and --output-stdlib-json still work, but are hidden from --help.
  • Error messages no longer start with process_file:.
  • I/O failures are reported properly:
    • A failed packet write, such as on a full disk, is reported as an error at the statement that caused it, with exit status 1. Before, resynth panicked.
    • A failure to flush the end of the pcap is now an error. Before, resynth printed ok, exited 0 and left a truncated file.
  • Failed runs no longer delete the output pcap, and --keep is gone. If you want this behaviour (failed runs delete any partial output), then you can use DELETE_ON_ERROR with make, or implement the equivalent behaviour yourself. The number of situations where programs can fail is much reduced (although not impossible), now.

Bug fixes

Packet encoding

  • TCP: client_reset() and server_reset() sent RSTs with a zero TCP checksum. Engines that validate checksums dropped them, and never saw the flow reset.
  • TCP: on server_message, server_segment, server_raw_segment and server_ack, seq: set the ack field and ack: set the seq field.
  • UDP: every UDP frame had an all-zeroes Ethernet source address. This covered ipv4::udp::unicast, broadcast, UdpFlow datagrams, dns::host, and the outer frame of VXLAN.
  • UDP: client_dgram/server_dgram with frag_off: produced a bad IP header checksum.
  • UDP: a checksum that computes to zero is now sent as 0xffff, as RFC 768 requires. About one datagram in 65536 used to look unchecksummed.
  • IP fragments: both Ethernet addresses were set to the source address.
  • ICMP: echo() and echo_reply() with a payload produced a bad IP header checksum.
  • DNS: names with a trailing dot, such as "www.example.com.", produced an extra empty label. That shifted every following field and gave a malformed packet. dns::name("") now produces the root name.
  • DNS: dns::host responses now copy the RD bit from the query (RFC 1035 §4.1.1).
  • DNS: dns::name(complete: false, "www.example") produced a single label containing the dot. One argument is now always split as a dotted name, and several arguments are used as labels as given.
  • DHCP: dhcp::option(dhcp::opt::END) and PAD produced a stray length byte (ff 00). Without data they are now a lone code byte, as RFC 2132 requires. Passing data still adds a length, so you can still build a malformed END.
  • ARP: arp::hrd::ETHER was 8 bytes wide, which shifted every later field. It is now a u16.
  • ERSPAN II/III: frames always carried GRE sequence number 0.

Crashes

  • IpFrag.fragment() with an offset past the end of the payload panicked. It now emits an empty fragment at that offset with MF clear, which is a legitimate evasion test case.
  • Passing extra arguments to io::file() or the IpFrag methods panicked. This is now a semantic error.
  • In debug builds only, TCP, ICMP-echo, GRE and ERSPAN sequence numbers panicked on overflow. So did length fields that overflowed. Sequence numbers now wrap, and length fields are documented as wrapping.

Library-level fixes (pkt / ezpkt)

These didn't affect any packets the standard library generates:

  • GRE: the flags field is now encoded in bits 3–6. Before, the recursion count was written twice.
  • erspan2_hdr::index() wrote to the flags word instead of the index.
  • Unsound pointer casts in Hdr<T> were fixed, and every Hdr<T> is now checked at compile time to be 1-byte aligned.

Standard library

  • std::be16/std::le16 now take a u16, and std::be32/std::le32 take a u32, instead of a u64. A constant that is too big now gets a truncation warning.
  • TcpFlow.client_hdr() and server_hdr() take a new optional csum: argument. These functions build a bare TCP header for a separate IP fragment, so the checksum can't be computed. The default is zero.
  • Every function that writes a length field documents that the length wraps when the content is too long. This covers std::len_*, ipv4::datagram, ipv4::udp::hdr, dhcp::option, dns::answer, netbios::ns::nbstat_rdata and the TLS message builders.
  • Documentation fixes:
    • IpFrag.fragment()'s len is in 8-byte blocks, and MF is cleared on the last fragment.
    • The meaning of the ERSPAN cl/sv addresses is corrected.
    • ipv4::datagram is now described accurately.
    • client_hole/server_hole say which side's sequence space they skip.
    • The descriptions of the "raw" flag and of the TLS cipher-suite and SNI helpers are corrected.
    • The non-existent netbios::name::suffix::DOMAIN_NAME was removed.
  • The TLS module's full handshake example now ships its example-data/server_key_exchange.bin file, so the example runs.

For library users

  • The resynth library API has changed throughout and remains unstable. Errors are now split by phase into LexError, ParseError, SemanticError and RuntimeError, each with a location and a kind. Programs are lowered to a checked HIR before they run.
  • pkt:
    • PcapWriter::write_packet() now takes &Packet. The old mutable headroom variant is available as write_packet_mut().
    • New PcapWriter::flush() reports errors that Drop would ignore.
    • Header structs have getters for every field, returning host-order values.
    • By-value header setters are #[must_use].
  • pkt, ezpkt and resynth are now on the Rust 2024 edition and need Rust 1.88 or newer.
  • Dependencies: regex and lazy-regex were removed, because the lexer is now hand-written. chrono was added for --start-time. The REPL feature adds reedline.

Full changelog: https://github.com/giannitedesco/resynth/compare/v0.6.0...v0.7.0