A Python hook on every AVP, and the double free underneath it

Four Diameter routing agents run a Python function once per AVP of every message they relay; the interesting part is what that costs in C.

Telecom

The routing agents are freeDiameter with two local extensions. rt_pyform comes from a third-party fork of freeDiameter. It loads a Python module at startup and calls one function per AVP, for every message that passes through:

def transform(appId, flags, cmdCode, HBH_ID, E2E_ID, AVP_Code,
              vendorID, value, imsi, originRealm="", routeRecord=""):
    return value

The function returns the value, rewritten or unchanged. appId distinguishes S6a (16777251) from Cx (16777216); cmdCode names the procedure (316 ULR, 318 AIR, 317 CLR, 304 NOR); AVP_Code says which field is in hand (1 User-Name, 264 Origin-Host, 283 Destination-Realm, 293 Destination-Host, 296 Origin-Realm).

The last three arguments (imsi, originRealm, routeRecord) were added locally, and they are why the hook is usable at all. An AVP cannot be rewritten correctly without knowing which subscriber and which direction it belongs to, and most AVPs are not the User-Name. So the extension lifts the IMSI out of the message once and hands it to every callback — including callbacks for messages that carry no User-Name of their own.

What the hook does

One partner's network sits in a different Diameter realm. Destination-Realm, Origin-Realm and Origin-Host are rewritten in both directions, and a carrier-specific prefix on two roaming realms is stripped on the way out. Two other partners share our realm, but number their subscribers in their own blocks. There, only User-Name is touched, only on S6a, and only on AVP 1 — contiguous range pairs, mapped arithmetically, with a documented gap (one block in the middle was never allocated, and an IMSI landing in it is logged as unknown).

Direction is everything here. The same transform applied twice maps a translated IMSI straight back, and a realm swap on both hops of a relay cancels itself. That constraint decides which agent in a chain runs the transforms and which one relays untouched (the relaying hop rewrites Destination-Host and nothing else). It is written down, not inferred.

Rollout is gated by a TRANSLATION_MODE variable with three values (disable, dev, enable). In dev only one test subscriber is translated, and every other subscriber passes through unchanged. On one flavour the sense is inverted: enable there means stop translating, because removing an old arithmetic translation was the change being rolled out. That is documented precisely because nobody would guess it.

The crash

The symptom was the process aborting. glibc's heap checker raised SIGABRT, with a backtrace that ran free() → libfdproto → fd_msg_free() → the second extension's routing callback (thread 12, under live traffic).

The first fix was wrong, and looked right because the crash rate dropped. TRANSLATION_MODE was read with a default parameter instead of or "dev", so a variable set to the empty string selected a mode that did not exist. Correcting it took traffic off the path that reached the fixups extension as often, and the crashes thinned out. The same commit stopped rt_pyform calling fd_msg_avp_setvalue() when the Python function had returned the value unchanged — which it had been doing for every AVP of every message, a free and a reallocation per field for no change at all.

Neither of those was the bug. The fixups extension called fd_msg_free(*rtmsg) after sending an error response or a steered answer. Both paths build their reply with fd_msg_new_answer_from_req(), which links the answer to the original request, and fd_msg_send() then transfers ownership of both messages to the framework. The explicit free afterwards is a second free of memory the framework already owns. Removing it, and setting *rtmsg = NULL to tell the routing framework the extension consumed the message, is the whole fix (one line out, one comment in).

Two follow-ups came out of review the next day. If AppendAVP or fd_msg_send throws after the answer exists, the answer leaks and stays linked to the request. It is now freed in the catch, which unlinks it and leaves the request valid for continued routing. The callback also gained a catch(...), because it is a C++ function called from libfdcore's C routing dispatcher (a C frame cannot unwind a C++ exception) — undefined behaviour, and not a diagnosable error.

Two smaller things that were also wrong

strcmp() was being used on a Diameter OctetString's os.data. That data is length-prefixed and carries no terminator (os.len holds the length), so the comparison reads past the value until it finds a zero byte somewhere else. It is now memcmp with an explicit length check.

Extension load order in freeDiameter.conf is load-bearing, and getting it wrong fails at startup instead of under load. Dictionaries go first (dict_s6a, dict_cx), then the plain routing extensions, then rt_pyform last among them so its rewrites win, and the access-control list last of all. A lookup of the Cx Public-Identity AVP (601) against a dictionary not yet loaded is how that order was found.

None of this is visible from the protocol side. A relay that rewrites realms correctly, and one that rewrites them correctly and then aborts on a reused heap chunk, look identical in a capture — right up to the point where only one of them is still answering.