Skip to content
RCE Write-up Advanced

Code execution as root via AT commands on the Quectel EG25-G modem

The modem's AT command daemon used system() with unsanitised user input in the +QFUMOCFG command, allowing an attacker to inject shell commands. An attacker could execute arbitrary commands as root on the modem.

nns Follow

Published on nns.ee

Bounty$2,000 View original

As I mentioned towards the end of my previous blog post, where I detailed running my blog on the PinePhone's GSM/WWAN/GPS modem, I suspected that the daemon responsible for parsing AT commands on the modem's side is susceptible to OS command injection, as it uses a lot of system() calls. My hunch turned out to be true.

nns, on nns.ee

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

In my previous post I described running my blog on the PinePhone's GSM/WWAN/GPS modem. At the end of it I noted a suspicion: the daemon that parses AT commands on the modem side makes heavy use of system(), so it might be open to OS command injection. That hunch turned out to be correct.

Context

The PinePhone talks to its Quectel modem over a serial line. On the PinePhone side the device is /dev/ttyUSB2; on the modem side it is /dev/ttyHSL0. The protocol is the Hayes command set, better known as AT commands.

The modem runs a full Linux install, separate from the PinePhone's main OS. It receives AT commands, parses them, executes them according to its own program logic, and answers OK or ERROR over the same serial line. The daemon primarily responsible for this is atfwd_daemon.

So the feature under review is the AT command front end: a root daemon on a Linux system that takes input from another device over a serial line and acts on it.

Setup

No accounts or special roles are involved. The two things needed are the daemon binary and a way to send it commands.

  1. Get atfwd_daemon. It can be pulled from the device by setting up adb access and extracting it with adb. It can equally be taken from the firmware's update packages, because it is not encrypted in any way.
  2. Load the binary in Ghidra for static analysis.
  3. Send AT commands over the serial line to confirm what the disassembly suggests.

Finding it

Opening atfwd_daemon in Ghidra shows that the executable calls system() in 233 different places across the file. That is a lot.

Using system() with user input is never a good idea, but most of those calls cannot be exploited: many are hardcoded, and in others the user input is converted to an integer with sprintf() before it reaches the shell.

/img/2021-04-03-modem-rce/1BSw4BQZ.png
/img/2021-04-03-modem-rce/1BSw4BQZ.png From the original on nns.ee

A few places are different. There, user input is passed to sprintf() as %s, and no checks or sanitisation are performed on it.

One of these places is a routine called quectel_handle_fumo_cfg_command().

/img/2021-04-03-modem-rce/EnUTEnhj.png
/img/2021-04-03-modem-rce/EnUTEnhj.png From the original on nns.ee

In that routine, param1[1] is formatted into the string ipth_dme -dmacc %s &, which is then handed to system(). One detail is worth noting: ipth_dme does not exist on the system at all, so the intended program would never run.

Walking back up the execution flow, the switch case in the previous screenshot is reached when some part of the user input begins with dmacc. That check lives in a routine called quectel_parse_fumo_cfg_cmd_params().

/img/2021-04-03-modem-rce/DSuRVPK1.png
/img/2021-04-03-modem-rce/DSuRVPK1.png From the original on nns.ee

The rest of the input is left relatively untouched.

Further up still, the AT command that parses this input is +QFUMOCFG.

/img/2021-04-03-modem-rce/wCONynTY.png
/img/2021-04-03-modem-rce/wCONynTY.png From the original on nns.ee

Exploitation

From this chain, arbitrary command execution follows. Backticks can be used to run a command in a subshell inside the argument that reaches system(). To reboot the modem:

Text
AT+QFUMOCFG="dmacc","`reboot`"

Because the daemon runs as root, the injected command also runs as root on the modem.

Reading files and returning the output

In an Asciinema recording I cat /etc/passwd and run id, and return the data to the PinePhone's OS over the serial line.

In the recording, the output of the injected commands was returned to the PinePhone's OS over the serial line.

Root cause

The mistake is a shell string built from unvalidated user input. quectel_handle_fumo_cfg_command() takes param1[1] straight from the AT command and formats it with sprintf() as %s into ipth_dme -dmacc %s &, then passes the result to system(). system() runs its argument through a shell, so any shell metacharacter in the input, backticks here, is interpreted.

The prefix check on dmacc in quectel_parse_fumo_cfg_cmd_params() only selects which branch runs; it does not constrain the rest of the argument, which the report describes as remaining relatively untouched. The other system() calls in the binary are mostly protected by being hardcoded or by integer conversion of the input, which is why this one stands out.

Impact

An attacker who can send AT commands to the modem gets command execution as root on the modem's Linux install. The modem is a separate system from the PinePhone's main OS, but the two are connected by the serial line, and command output can be sent back across it.

The report says it is very possible that other Quectel products are affected, as firmware is commonly reused, but the author had no other hardware to test on.

Fix

The report does not describe a fix or a vendor change. The structural remedy is to stop building shell command lines out of AT command arguments: avoid system() for this path, or pass the argument as data rather than as part of a shell string, and validate it against the expected format before use.

Variants to test

  1. Other %s sprintf() sites. In the same binary, look for further sprintf() calls that place user input into a string later passed to system(). A positive result is an AT command whose argument reaches a shell with metacharacters intact.
  2. Other AT commands handled by the same daemon. Trace each command parser in atfwd_daemon back to its system() call. A positive result is any command where a parameter is not integer-converted or hardcoded before formatting.
  3. Other Quectel firmware. The report says firmware is commonly reused, so other Quectel products may be affected. A positive result is the same +QFUMOCFG handling, or the same ipth_dme -dmacc %s & string, in another model's atfwd_daemon.
  4. Prefix-gated branches generally. Where a parser only checks that input begins with a keyword before passing the remainder on, try appending shell metacharacters after the keyword. A positive result is command output or a side effect such as a reboot.

Timeline

  • 03/04/2021: attempted to contact vendor
  • 13/04/2021: vendor confirmed vulnerability
  • 23/04/2021: vendor issued $2,000 bounty
  • 24/04/2021: assigned ID CVE-2021-31698
  • 08/09/2021: write-up published

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

A security researcher found a way to make a phone's modem run any command he wanted, as the most powerful user on the modem's own small computer. The modem is the chip inside a phone that handles the mobile network connection. In this case it was a Quectel modem inside a PinePhone, a phone designed for people who like to tinker with their hardware and software.

The modem is not just a simple radio. It runs a complete Linux operating system of its own, separate from the phone's main system. The phone and the modem talk to each other over a wire inside the device, using short text instructions called AT commands. One program on the modem, called atfwd_daemon, reads those instructions and carries them out.

The researcher suspected that this program was built in a risky way: it passes pieces of the incoming instructions to the Linux shell, the part of the system that runs commands. If a piece of text is handed to the shell without being checked, an attacker can hide extra commands inside it. That is exactly what turned out to be possible. By sending a specially crafted AT command, the researcher could make the modem run his own commands, and because the program runs with the highest level of permission (the root user), those commands ran with that same power.

The vendor confirmed the problem, paid a $2,000 bounty, and the issue was given the identifier CVE-2021-31698. A CVE is a public catalogue number for a known security flaw, so that everyone can refer to the same issue unambiguously.

Background: the phone, the modem and the wire between them

To follow the rest of this article you only need a few ideas.

The PinePhone and its modem. A PinePhone is a smartphone. Inside it sits a Quectel modem, a component that connects to mobile networks and also handles GPS. In the researcher's earlier blog post he described an unusual experiment: running his personal blog on the modem itself, because the modem is effectively a small Linux computer. At the end of that post he wrote down a suspicion. The program that reads AT commands on the modem side used a function called system() in a great many places, and system() is the standard way for a C program to ask the Linux shell to run a command. Using it with text that comes from outside is a classic recipe for trouble.

AT commands. AT commands are a very old, very simple language for talking to modems. They are plain text lines, usually starting with AT, followed by a command name and sometimes parameters. You can think of them as the modem's remote control buttons: each line is one button press, and the modem answers OK if it worked or ERROR if it did not. The name comes from the Hayes command set, and the idea dates back to dial-up modems.

The serial line. The phone and the modem are connected by a serial line, a simple wire-based connection over which text flows in both directions. On the PinePhone side this connection appears as a device file called /dev/ttyUSB2. On the modem side the same connection appears as /dev/ttyHSL0. A device file is how Linux represents hardware: reading from and writing to that file is the same as reading from and writing to the wire.

The daemon. A daemon is a program that runs in the background all the time, without a window or a user watching it. The daemon responsible for AT commands here is called atfwd_daemon. It receives each AT command, works out what it means, does the corresponding action, and replies OK or ERROR over the same serial line.

So the feature under review is the modem's AT command front end: a background program running with full privileges on a Linux system, taking text input from another computer over a wire and acting on it.

Setting up: getting the program and looking inside it

No accounts, logins or special roles are involved in this research. Two things are needed: a copy of the program, and a way to send it commands.

  1. Obtain atfwd_daemon. There are two routes. The first is to enable adb access on the modem and pull the file off the device with adb. adb is the Android Debug Bridge, a standard tool for talking to Android-based devices from a computer; here it is used simply as a file transfer mechanism. The second route is to take the file out of the firmware update packages, because the program is not encrypted in any way. Firmware is the software image that gets flashed onto a device; update packages are the files distributed to install new firmware.
  2. Load the program into Ghidra. Ghidra is a free tool for reverse engineering: it takes a compiled program, which is just machine instructions, and reconstructs something a human can read, showing functions, strings and how they call each other. This is called static analysis, meaning the program is examined without running it.
  3. Send AT commands over the serial line. Static analysis gives hints; sending real commands confirms whether the hints are true. This is called dynamic testing, because the program is observed while it runs.

Finding the flaw

Opening atfwd_daemon in Ghidra shows that the program calls system() in 233 different places across the file. That is a lot.

Why does that matter? system() hands a text string to the Linux shell and asks it to run that string as a command. The shell is powerful: it understands special characters such as backticks, semicolons and pipes, which let one command line contain several commands. If any part of the string comes from an attacker, the attacker can use those special characters to add commands of their own. This class of mistake is called OS command injection: injecting operating system commands into a command line that the program builds.

Using system() with user input is never a good idea, but most of those 233 calls cannot be exploited. In many of them the command is hardcoded, meaning the text is fixed inside the program and no outside input reaches it. In others, the user input is converted to an integer with sprintf() before it reaches the shell. sprintf() is a C function that builds a formatted string, a bit like filling in a template. Its format specifiers decide how each value is turned into text: %d means a whole number, %s means a piece of text. If input is forced through %d, it becomes a number, and numbers cannot carry shell special characters, so the injection route is closed.

/img/2021-04-03-modem-rce/1BSw4BQZ.png
/img/2021-04-03-modem-rce/1BSw4BQZ.png From the original on nns.ee

A few places are different. There, user input is passed to sprintf() as %s, meaning it stays as free text, and no checks or sanitisation are performed on it. Sanitisation means cleaning input, for example by removing or escaping special characters. Without it, whatever the attacker sends arrives at the shell unchanged.

One of these places is a routine called quectel_handle_fumo_cfg_command(). A routine is simply a named block of code that does one job; the name suggests it handles configuration for a Quectel feature whose command is +QFUMOCFG.

/img/2021-04-03-modem-rce/EnUTEnhj.png
/img/2021-04-03-modem-rce/EnUTEnhj.png From the original on nns.ee

In that routine, a value called param1[1] is formatted into the string ipth_dme -dmacc %s &, and that string is then handed to system(). param1[1] means the second parameter of the AT command, counting from zero, so it is a value the sender controls. The %s is where that value lands.

One detail is worth noting: ipth_dme does not exist on the system at all. In other words, the program the line is apparently meant to run is not present, so the intended program would never run.

Walking back up the execution flow, the switch case in the previous screenshot is reached when some part of the user input begins with dmacc. A switch case is a branch in the code: the program looks at a value and jumps to the matching section. That check lives in a routine called quectel_parse_fumo_cfg_cmd_params(), whose job is to split the incoming AT command into its parameters.

/img/2021-04-03-modem-rce/DSuRVPK1.png
/img/2021-04-03-modem-rce/DSuRVPK1.png From the original on nns.ee

The rest of the input is left relatively untouched. This is the heart of the problem: the check only looks at the beginning of the text and does not restrict what comes after it.

Further up still, the AT command that parses this input is +QFUMOCFG. That is the command an attacker has to send to reach all of the code described above.

/img/2021-04-03-modem-rce/wCONynTY.png
/img/2021-04-03-modem-rce/wCONynTY.png From the original on nns.ee

Exploitation: making the modem run a command

From this chain, arbitrary command execution follows. Arbitrary means the attacker is not limited to one fixed action: any command the shell can run can be injected. Backticks can be used to run a command in a subshell inside the argument that reaches system(). A subshell is a second shell started from inside the first one; the text between backticks is executed first, and its output is substituted into the surrounding command line. To reboot the modem:

Text
AT+QFUMOCFG="dmacc","`reboot`"

Line by line, in plain words:

  • AT+QFUMOCFG= is the AT command being sent. AT marks it as an AT command, +QFUMOCFG is the specific Quectel command, and = introduces the parameters.
  • "reboot" is the second parameter, the one that ends up as %s in ipth_dme -dmacc %s &. The backticks tell the shell to run reboot first and put its output in place.

Because the daemon runs as root, the injected command also runs as root on the modem. Root is the administrator account on Linux, the one allowed to do anything: read any file, change any setting, restart the machine.

Reading files and returning the output

Running a command is one thing; seeing its result is another. In an Asciinema recording (Asciinema is a service for recording and sharing terminal sessions, so viewers can watch a command line play back exactly as it happened) the researcher ran cat /etc/passwd and id, and returned the data to the PinePhone's OS over the serial line.

  • cat /etc/passwd prints the contents of /etc/passwd, a standard Linux file that lists the user accounts on the system.
  • id prints which user the current process is running as, including whether it is root.

That last point is the useful part for an attacker: the modem is a separate Linux machine, but its serial link to the PinePhone gives a channel for the output of the injected command to come back. In the recording, the output of the injected commands was returned to the PinePhone's OS over the serial line.

Root cause

The mistake is a shell string built from unvalidated user input. quectel_handle_fumo_cfg_command() takes param1[1] straight from the AT command and formats it with sprintf() as %s into ipth_dme -dmacc %s &, then passes the result to system(). system() runs its argument through a shell, so any shell metacharacter in the input, backticks here, is interpreted. A metacharacter is a character with special meaning to the shell rather than literal text.

The prefix check on dmacc in quectel_parse_fumo_cfg_cmd_params() only selects which branch runs; it does not constrain the rest of the argument, which the report describes as remaining relatively untouched. The other system() calls in the binary are mostly protected by being hardcoded or by integer conversion of the input, which is why this one stands out.

A useful everyday comparison: a bouncer who only checks the first word of your name and then waves you and everyone behind you through. The check exists, but it guards nothing that matters.

Impact

An attacker who can send AT commands to the modem gets command execution as root on the modem's Linux install. The modem is a separate system from the PinePhone's main OS, but the two are connected by the serial line, and command output can be sent back across it.

The report says it is very possible that other Quectel products are affected, as firmware is commonly reused, but the author had no other hardware to test on. Firmware reuse means manufacturers often build many models from the same software base, so a flaw in one model's code can quietly exist in others.

Fix

The report does not describe a fix or a vendor change. The structural remedy is to stop building shell command lines out of AT command arguments: avoid system() for this path, or pass the argument as data rather than as part of a shell string, and validate it against the expected format before use. Validating against an expected format means, for example, accepting only letters and digits of a known length, so that no shell special character can ever get through.

Variants to test

These are the follow-up checks a researcher would run to see how widespread the same mistake is.

  1. Other %s sprintf() sites. In the same binary, look for further sprintf() calls that place user input into a string later passed to system(). A positive result is an AT command whose argument reaches a shell with metacharacters intact.
  2. Other AT commands handled by the same daemon. Trace each command parser in atfwd_daemon back to its system() call. A positive result is any command where a parameter is not integer-converted or hardcoded before formatting.
  3. Other Quectel firmware. The report says firmware is commonly reused, so other Quectel products may be affected. A positive result is the same +QFUMOCFG handling, or the same ipth_dme -dmacc %s & string, in another model's atfwd_daemon.
  4. Prefix-gated branches generally. Where a parser only checks that input begins with a keyword before passing the remainder on, try appending shell metacharacters after the keyword. A positive result is command output or a side effect such as a reboot.

Timeline

  • 03/04/2021: attempted to contact vendor
  • 13/04/2021: vendor confirmed vulnerability
  • 23/04/2021: vendor issued $2,000 bounty
  • 24/04/2021: assigned ID CVE-2021-31698
  • 08/09/2021: write-up published

Glossary

  • AT commands: a simple text-based language for giving instructions to a modem, one line per command, answered with OK or ERROR.
  • Hayes command set: the original standard that AT commands come from, named after the modem manufacturer Hayes.
  • Modem: the component in a device that connects to mobile networks, and in this case also runs its own Linux system.
  • PinePhone: a smartphone designed for hardware and software experimentation.
  • Quectel: the manufacturer of the modem in question.
  • Serial line: a simple wire connection over which text travels in both directions between two devices.
  • Device file: how Linux represents hardware, for example /dev/ttyUSB2, so that reading and writing the file means reading and writing the hardware.
  • Daemon: a program that runs in the background continuously, without a visible window.
  • atfwd_daemon: the background program on the modem that receives and executes AT commands.
  • adb: Android Debug Bridge, a tool for communicating with and extracting files from Android-based devices.
  • Firmware: the software image installed on a device such as a modem.
  • Ghidra: a free reverse-engineering tool that turns a compiled program back into a readable form.
  • Static analysis: examining a program without running it.
  • Dynamic testing: observing a program while it runs, for example by sending it real input.
  • system(): a C function that hands a text string to the Linux shell to be executed as a command.
  • Shell: the part of Linux that reads command lines and runs the programs they name, interpreting special characters along the way.
  • sprintf(): a C function that builds a formatted string from a template and values.
  • Format specifier: the part of a template that says how a value is turned into text, such as %d for a whole number or %s for text.
  • Hardcoded: fixed inside the program, with no outside input reaching it.
  • Sanitisation: cleaning input, for example by removing or escaping characters that have special meaning.
  • Routine: a named block of code that performs one job.
  • Switch case: a branch in a program that jumps to a matching section based on a value.
  • Parameter: one of the values supplied with a command, separated by commas in an AT command.
  • Subshell: a second shell started from inside the first, used here through backticks.
  • Backticks: characters that tell the shell to run the text between them first and substitute its output into the surrounding command.
  • Metacharacter: a character with special meaning to the shell rather than literal text.
  • Root: the administrator account on Linux, allowed to do anything on the system.
  • /etc/passwd: a standard Linux file listing the user accounts on the system.
  • id: a command that prints which user the current process is running as.
  • Asciinema: a service for recording and sharing terminal sessions so they can be played back.
  • CVE: a public catalogue identifier for a known security vulnerability, here CVE-2021-31698.
  • Bounty: a payment a vendor makes to a researcher who reports a valid security flaw.
  • Firmware reuse: building several products from the same software base, so a flaw in one can exist in others.
  • Validation: checking input against an expected format before using it.

More by nns

View all

1 more disclosed report, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.