This is the tale of an under-patched bug.
While auditing source code for a customer, Luis noticed that the bspatch sources seemed to not be the latest version. Further comparison with the FreeBSD sources shows that not all patches are applied. Most notably FreeBSD-SA-16:29 is missing.
The bug stems from bspatch not correctly handling large integer values that are read from patch files.
This is easily fixed by checking for more corner cases. The patch resolves more issues, so this is not the only part you should apply.
A good description and demo exploit for the issue can be found in NON-CRYPTANALYTIC ATTACKS AGAINST FREEBSD UPDATE COMPONENTS from an unknown author.
When looking at the FreeBSD patch, you will quickly notice that it is not just a security patch, but that it contains formatting changes as well.
Furthermore, it adds support for capsicum, which is a great way of sandboxing. Due to it being specific to FreeBSD, third parties might have simply overlooked the actual issue patched here and skipped from applying the patch in fear of breakage.
Auditing this patch shows that breakage is unlikely, but finding the part that just patches the security issue takes a minute. We don’t want to blame the FreeBSD folks, they are doing a good job, but we feel like we should highlight this, since we see this for both, open source and proprietary projects. Limiting a bugfix to just the minimum that resolves the security issue, eases people into applying the patch. It’s usually best to split security relevant patches from feature patches, so the user can easily audit them and has a limited risk of breakage.
For some reason, this issue had no CVE assigned in the FreeBSD advisory. The security issue mentioned in FreeBSD-SA-16:25, and fixed 3 month before, was assigned a CVE ID, but this one was not. We can only speculate whether this prevented people from noticing this issue or not. But not having a CVE to properly reference certainly made it harder to talk about this issue during the disclosure process.
The patch above never made it into what some people would consider upstream, so the code of Colin was never patched.
A lot of projects use and depend on bspatch for their update process, to be able to ship incremental updates. For another reason unknown to us, most of these projects ship their own fork or copy of this code and have therefore not received any patches either. We could identify at least the following projects:
A github search finds everal projects using copies of bspatch.
There are various ways to identify whether you are affected by this issue. Best is if you have a proper list of code you forked in the past and can look at this. But we noticed this is not what happens in most companies. If none of the developers happens to track the security reports and knows about the use of bspatch, this will get lost in the noise. We often like to use tools like spatch from the wonderful coccinelle project to identify such issues even when code has been changed to match local coding styles and variable names have been modified.
The following are two spatch files to identify both patches. Please note that the full patch is longer, this is kept short to identify the issue easier.
Another possible way to spot this issue is to enable certain compiler warnings
for your project. The
-Wconversion flag to gcc will result in the following
The journey with this bug shows that there are several areas the disclosure and patching process can be improved upon. Although this is an issue in open source software, we can see these patterns with proprietary code in our daily work as well. Security patches are not minimized, not properly communicated and teams keep private forks of libraries used within the company.
X41 D-Sec GmbH is an expert provider of application security services. With extensive experience and expertise in the information security industry and a strong core security team of world-class experts, X41 can provide premium security services. Their fields of expertise in the area of application security are security-centered code reviews, binary reverse engineering, and vulnerability discovery. Custom research and IT security consulting and support services are the core competencies of X41.