The best linux distro ever or how Gentoo performs for security research

  Intro

 So, I've been thinking about what I should write in my blog because it doesn't really feel right to leave it empty for long, but I didn't want to write something useless here. And then I came across an article by M. Gorny where he explains why someone would choose Gentoo Linux. 

 And this article is dedicated to my story of how Gentoo became the foundation that powers my homelab and personal infrastructure.

First of all, let me explain the core needs that my distribution of choice has to meet.

  • Flexibility - I have to be able to change (almost) anything at any time, rollback package upgrades and be able to easily add my own packages.
  • Freedom - A very important thing I need to see in a distribution of choice is being able to easily change configuration and recompile software with or without certain features (especially the linux kernel).
  • Security - A distribution should be sufficiently secure in terms of its base. Package metadata, source files, everything should be cryptographically signed and verified.
  • Stability - I really hate to deal with absolutely random things that can happen with a rolling-release distro (even though Gentoo technically is one). I also need to be able to mix unstable and stable packages, sometimes even grab the latest commit without extra hassle. 

Now, of course I understand that all modern distributions meet this criteria to a certain extent, but let's see how Gentoo made me install it on every machine.

  Workstation Experience

 My first ever linux distribution was debian and I was very happy with it until I started descending into the security rabbit hole further and further. I felt like I was essentially entrusting my files' safety into the hands of the debian developer team. And I felt like I had to get closer to zero trust as much as possible. 

I tried Arch Linux and... I didn't like it all that much. AUR is definitely very cool. But you kind of have to install a helper if you want to make good use of it. The installation process also was a little confusing, but maybe I'm just stupid.

Then I tried Ubuntu and Fedora. I felt like they came with absolutely unnecessary bloat and tried to force me into a certain way of using my computer.

And not long after I found myself scrolling the Distrowatch website. And then I found it - Gentoo. I read about it on their website, the premise seemed interesting and I decided to give it a go. After a few days of figuring out USE flags and other things, then on top of that maybe a few months and a few reinstalls I felt like I was fairly happy with my setup. And then I wasn't.

You see, I spent maybe a year on-and-off trying to get my configs the way I wanted. And the more I learned the more I wanted to change something. Nowadays on my work machines I run Sway(FX) on a hardened profile and a fairly minimal kernel config. I have to admit, it is absolutely perfect.

  Server Experience

 Over time I've accumulated some old machines that still run quite well for my tasks. And I decided to install Gentoo on them and make them my servers. I'm not going to talk about service configuration because it's not all that interesting, but one thing I want to mention is musl libc. I decided to go with musl for my servers because its minimalism (and for some reason me feeling like I install faster on the musl profile???) and the fact that it's not the GNU libc proved to be a pretty good idea. Then I installed Graphene's hardened_malloc, compiled with the full variant. 

This was looking very good for a minimalistic environment needed for my also minimalistic needs. But something was missing... YES! SELinux. I found out that Gentoo supports SELinux REALLY well and when I say really well I mean it's amazing. I can easily compile all tools, install the policy, label all of my files and directories. And then actually spend the time configuring and tweaking the policy. Right now all of my servers run SELinux in enforcing mode with the strict policy, plus kernel configuration that does not allow to change the SELinux enforcing mode without editing the config file. So, you don't have unlabeled, you can't just put 'enforcing=0' in the kernel cmdline. It won't work. SELinux was in full swing. 

Well, not really. Because my virtualization server that runs Libvirt+QEMU (and sometimes the xen hypervisor) didn't want to play well with SELinux. After a bit of searching on the internet I came to understand that there was a simple auto-labelling issue where Libvirt was expecting the extended MCS/MLS context instead of the shorter one that you see on strict and targeted policies. A simple XML file edit did the trick. Although, I did have to change the policy to MCS. Not that it matters, but there was some other issue that I can't remember.

  My Gentoo Experience

 So, all this is very nice but how does Gentoo actually play with security? Good question:

  1. You can experiment with software features, easily rebuild said software, easily patch it, easily add new software and so on and so forth. That enables experimentation and gives the user all tools to, for example, disable/enable certain kernel features.
  2. Gentoo is not run by a company. It is built on cryptographic verification and with autonomy in mind.
  3. You can strip out literally everything you don't need. Reduce the attack surface all you want.
  4. Your favorite tools and software are kept up-to-date while not rushing all that much. Although, if you want to - you can build from latest commit or tag a specific ebuild with the unstable keyword and receive updates earlier.
  5.  Concerned about compile times? Distribute the compilation across multiple machines or make a build server and tell portage to retrieve binary packages from the server. By the way, you can do it via ssh too.

I have to say. Gentoo has one hell of a learning curve, that is for sure. But if you have some time on your hands - give it a go, maybe you'll like it.

  The Inner Workings

 Ok, time for more technical details this time. One of the fabulous things I learned about is OpenRC, which is more or less one of the direct alternatives to systemd (more or less because systemd is now something more than a simple init system and service manager). There are not many distributions that support it, but MAN. It is great. I really love how simple writing services is and I also recently learned that they finally made support for user services!

 

Another thing, that I already mentioned, is SELinux support. SELinux is a very silly thing that I love (and hate) with all my heart but unfortunately it didn't play nice when I tried Arch Linux and other distros that DO support it well didn't meed my needs 😭
 When it comes to Gentoo, though... It supports SELinux surprisingly well. I never had any issues with the userspace utilities.
 But to be fair, I do admit that the actual distribution doesn't really mean much. But in my case it does. So, please don't take it the wrong way, I am not saying other distributions are by any means bad.
 
Anyways. Don't mind the cat pictures, there are MANY more to come. I am still working on my projects but I am not sure if there will be anything coming out just yet. 
 
Thank you for reading. 

Popular Posts