A security researcher discovered several vulnerabilities in Alpine Linux, a distribution commonly used with Docker, including one that could allow for arbitrary code execution.
Based on musl and BusyBox, the Alpine Linux distribution has a small size and is heavily used in containers, including Docker, as it provides fast boot times.
APK, the default package manager in Alpine, is impacted by several bugs, security researcher Max Justicz has discovered. The most important of them, the researcher says, could allow a network man-in-the-middle (or a malicious package mirror) to execute arbitrary code on the user’s machine.
“This is especially bad because packages aren’t served over TLS when using the default repositories,” Justicz notes.
An attacker could target a Docker container based on Alpine for code execution, the security researcher reveals. Justicz also published a video detailing such an attack.
The issue, the researcher explains, is that the package manager extracts packages (which are gzipped tar files distributed as apks) before checking their hashes. If the downloaded package’s hash doesn’t match, the APK then attempts to unlink all extracted files and directories.
The APK’s commit hooks feature allows an attacker to turn persistent arbitrary file writes into code execution, as long as the files survive the cleanup process. Thus, the files are executed before apk exits.
For that, the attacker needs to be in control of the downloaded tar file and ensure that the APK won’t be able to unlink the payload and its directory during the cleanup process.
The next step is to make the APK process exit successfully, which requires the return of exit code 0. Normally, it “will return an exit code equal to the number of packages it has failed to install, which is now at least one,” the researcher explains.
However, the value can overflow and, if the number of errors % 256 == 0, the process returns exit code 0, meaning the attack was successful. The researcher was also able to write shellcode to exit(0) directly into memory and have it executed.
The bug likely impacts all those who use Alpine Linux in a production environment. All Alpine-derived container images should be rebuilt to eliminate the issue, the researcher points out.
Alpine Linux, the researcher says, is used by probably hundreds of organizations, all of which could have been affected by this bug.
“Some of those organizations almost certainly have bug bounty programs that would pay generously if a similar bug had been written by one of their own developers. If the goal of a bug bounty program is to help secure an organization, shouldn’t critical bugs in dependencies qualify to some extent?” Justicz concludes.