How a botched kernel patch broke Ubuntu – and why it may happen again
- Reference: 1657265225
- News link: https://www.theregister.co.uk/2022/07/08/kernel_patch_borked_ubuntu_systems/
- Source link:
A problem with a Ubuntu-specific Linux kernel patch early last month rendered many systems, running Docker on that flavor of the operating system unusable, and it probably won't be the last time.
The whole debacle can be traced back to a bad distro-specific kernel update for Ubuntu 20.04 — Canonical's long-term support (LTS) release — that started rolling out on or about June 8. Within hours of the patch hitting systems, bug reports began filing in.
[1]
The source of the trouble was quickly isolated to Ubuntu systems running Docker with the hardware-enablement (HWE) stack enabled. As the name suggests, HWE adds support for newer hardware by shipping updated kernels – and Ubuntu routinely pushes out new kernels via these HWE updates. While switching this on is usually a manual process for server systems, it's a standard feature on many Ubuntu images available in the cloud. To this end, several users reported VM images on AWS, GCP, Azure, and Oracle were affected. HWE is also usually enabled by default for new desktop installs.
[2]
[3]
The bug itself triggered a kernel panic any time a Docker container was started. Some users even reported the update resulted in a bootloop, and the only cure was to roll back to a previous working kernel during startup. This is presumably because their Docker containers were set to start with the rest of the system, causing a vicious cycle in which Ubuntu boots, the Docker containers start, the system kernel panics … rinse, repeat.
To make matters worse, Ubuntu's unattended-upgrades service, which is responsible for keeping systems patched and usually free of issues, made this particular kernel update more difficult to avoid.
[4]
The crash stemmed from an [5]issue with /proc/self/map_files and container environment file systems overlayfs and shiftfs that the kernel patch intended to fix. A revised kernel was released a few days later by Ubuntu addressing the issue. The impact of this botched patch is hard to gauge, but Ubuntu 20.04 remains a popular choice for production environments thanks to its relatively long support life.
Ironically, the five years of support that makes LTS releases so popular was also partially to blame, according to an [6]analysis this week by Jordan Webb, shared via LWN.
[7]Tuxedo Pulse G2: Linux in your lap
[8]Gtk 5 might drop X11 support, says GNOME dev
[9]China rallies support for Kylin Linux in war on Windows
[10]Ubuntu Unity desktop back from the dead after several years' hiatus
A crucial point in this saga is that Ubuntu has up until 21.04 included another container-related file system, aufs , in its distro-specific Linux kernels; this file system code was never merged into the mainline kernel, and was maintained out-of-tree. When Ubuntu's developers came to backport the shiftfs -related patch to Ubuntu 20.04, due to a chain of events, part of the patch code was dropped because it depended on aufs that wasn't present during the backporting process – but aufs was in fact in the 5.13 kernel used by Ubuntu 20.04 HWE.
Owing to that, and changes to how overlayfs worked internally, a reference to already free()'d memory in a kernel data structure would be released, triggering a panic. This would happen any time a Docker container spun up. According to Webb, this clash was caught almost immediately and fixed in Ubuntu's 5.15 kernel source. But for reasons that aren't clear, the 5.13 kernel in Ubuntu 20.04 HWE was overlooked and would continue to crash.
As Webb put it:
When Ubuntu's developers ported the shiftfs-related patches from their 5.8 kernel branch to their 5.13 and 5.15 kernels, the patch that corrected the problem with map_files and shiftfs was left out, because it depended on AUFS, which had been dropped from Ubuntu's kernel. When those kernels were backported to Ubuntu 20.04, where AUFS continues to be supported, the missing patch was noticed, and it was applied to Ubuntu's 5.13 and 5.15 trees as well.
Unfortunately, the internals of overlayfs changed over time in a way that eventually caused the patch to be incorrect. As a result, when a file on an overlayfs is mapped into memory, the function added by the patch attempts to release a reference to a struct file using fput(), but the structure had already been freed due to an earlier fput() call. That causes the kernel to panic.
On Ubuntu 21.10, where 5.13 is the default kernel, this didn't cause any problems. Since AUFS is not enabled, the #ifdef block around the code introduced by the patch prevented it from being compiled into the kernel. The problem occurred when 5.13 and 5.15 were rebuilt for Ubuntu 20.04. Since an HWE kernel needs to support all of the features that are supported by the kernel it is replacing, AUFS was enabled in these builds, and the code containing the extraneous fput() was compiled in.
This particular issue has since been resolved, and anyone who's only now returned to find their VMs or server deployments bootlooping should roll back to an earlier kernel and update their systems.
Unfortunately, gremlins like these may be hard to avoid given the lifespan of Canonical's LTS releases, which has led to developers juggling multiple branches of the kernel simultaneously.
[11]
"Maintaining an out-of-tree kernel patch for any length of time is an arduous task," Webb wrote, adding that the situation is unlikely to get any easier for the Ubuntu kernel devs and may actually become more difficult before long. ®
Get our [12]Tech Resources
[1] https://pubads.g.doubleclick.net/gampad/jump?co=1&iu=/6978/reg_software/oses&sz=300x50%7C300x100%7C300x250%7C300x251%7C300x252%7C300x600%7C300x601&tile=2&c=2YsgAQhsc0KXf92COGVSqIgAAAMo&t=ct%3Dns%26unitnum%3D2%26raptor%3Dcondor%26pos%3Dtop%26test%3D0
[2] https://pubads.g.doubleclick.net/gampad/jump?co=1&iu=/6978/reg_software/oses&sz=300x50%7C300x100%7C300x250%7C300x251%7C300x252%7C300x600%7C300x601&tile=4&c=44YsgAQhsc0KXf92COGVSqIgAAAMo&t=ct%3Dns%26unitnum%3D4%26raptor%3Dfalcon%26pos%3Dmid%26test%3D0
[3] https://pubads.g.doubleclick.net/gampad/jump?co=1&iu=/6978/reg_software/oses&sz=300x50%7C300x100%7C300x250%7C300x251%7C300x252%7C300x600%7C300x601&tile=3&c=33YsgAQhsc0KXf92COGVSqIgAAAMo&t=ct%3Dns%26unitnum%3D3%26raptor%3Deagle%26pos%3Dmid%26test%3D0
[4] https://pubads.g.doubleclick.net/gampad/jump?co=1&iu=/6978/reg_software/oses&sz=300x50%7C300x100%7C300x250%7C300x251%7C300x252%7C300x600%7C300x601&tile=4&c=44YsgAQhsc0KXf92COGVSqIgAAAMo&t=ct%3Dns%26unitnum%3D4%26raptor%3Dfalcon%26pos%3Dmid%26test%3D0
[5] https://kernel.ubuntu.com/git/ubuntu/ubuntu-impish.git/commit/?id=9680f5f6d8f93a82abfcc5e2c66faada3d4f2ef5
[6] https://lwn.net/Articles/899420/
[7] https://www.theregister.com/2022/07/05/tuxedo_pulse_g3_laptop/
[8] https://www.theregister.com/2022/07/05/gtk_5_might_drop_x11/
[9] https://www.theregister.com/2022/07/03/china_openkylin/
[10] https://www.theregister.com/2022/07/01/ubuntu_unity_desktop_updated_after/
[11] https://pubads.g.doubleclick.net/gampad/jump?co=1&iu=/6978/reg_software/oses&sz=300x50%7C300x100%7C300x250%7C300x251%7C300x252%7C300x600%7C300x601&tile=3&c=33YsgAQhsc0KXf92COGVSqIgAAAMo&t=ct%3Dns%26unitnum%3D3%26raptor%3Deagle%26pos%3Dmid%26test%3D0
[12] https://whitepapers.theregister.com/
Only a risk if you are patching your live system without having tested it on your test system first, but who has one of those these days.
Panic! at the distro? Have your pint your magnificent bastard.
Seconded, that brought me tears of joy
..if you could only get Jack White to sing it, as in the original, that would make my day.
"Maintaining an out-of-tree kernel patch for any length of time is an arduous task," Webb wrote, adding that the situation is unlikely to get any easier for the Ubuntu kernel devs and may actually become more difficult before long.
It's almost as if Linux would benefit from a well defined, documented, versioned and stable driver interface rather than a collection of source code patches and hope.
(I am actually making a serious point, but I know it doesn't chime well with the open source ideals, or something? Never quite understood why good engineering should be forgoed for the GPL, but hey ho.
It would also be virtually impossible to do in Linux as it would require a lot of co-operation and work by people who would ultimatly decry the outcome as it would allow people to more easly ship binary drivers.
But imagine if an Android kernel upgrade didn't need to be patched with the latest drivers as they just worked to a stable, well defined and well versioned interface.
It's like how Winodows XP could support SATA drives even though it was released first.... it has a well defined and documented driver interfaces the manufacturers could write storage drivers for.)
Never understood...
" Never quite understood why good engineering should be forgoed for the GPL, but hey ho. "
Good engineering has not featured in software development in general for absolutely ages - probably since the days of mainframes and batch jobs. In those days, your bad code could bring everyone's jobs to a halt, so you'd get pretty unpopular. As computing became 'personalised' the exposure to censure reduced. However it's potentially back again as 'everyone' migrates to the 'cloud' (your mainframe in the sky), so a foul-up by the service provider can take all of you down.
Unfortunately, sloppy habits are by now so engrained that the situation is unlikely to improve. It would be magnificent if 'software enginering' became worthy of the name, but I don't think it ever will now.
Alas, that is always the risk of patching a system: don't and you might get hacked, do and you might get borked.