File Expiration Using BPF
hondu.co
hondu.co
The aim of this proof-of-concept was to showcase how BPF enables the kernel to be programmable. Many behaviours can be retrofitted without having to submit changes upstream. In some cases the overhead may be lower than using other methods (due to avoiding context switches, etc). Even if you decide that the changes can be useful for the broader community, the new feature can be first implemented in BPF, where it’s faster (and safer!) to evolve it.
For example, consider CreateProcess vs fork/exec: at the first glance, the first one is the most obvious and sensible, you want a program launched, you point the OS at the executable and say "execute this". The second API instead makes you to make a perfect copy of the currently executing process, then you tell the OS to completely scrape and discard its state and launch another executable file inside it. Literally no other object in the system works this way (i.e. to create a new X, you must copy an already existing X first, then modify with the copy).
But then consider that you would also like to configure the environment of the newly launched program before it actually launches, and that's when CreateProcess API starts to lose its simplicity: you have to pass all imaginable (and some unimaginable) kinds of configuration, and the API has to be extensible and future-proof, too! Just look at what kind of silliness goes into thread-attribute lists [0]. But with fork/exec, you simply run the normal, user-space code that configures the current process whatever way it pleases, then swaps the executable, done — and you can't do that with CreateProcess, you can't just ask the kernel to run some arbitrary user-provided code in the kernel context... unless it's not entirely arbitrary: enter eBPF.
[0] https://learn.microsoft.com/en-us/windows/win32/api/processt...
That said (you knew there was a "but" coming, right? :->), why ptrace? Was that the only or just the simplest way to do this?
I know this is a PoC, but given disabling ptrace is considered by some (many?) an important security practice (moo), I'm wondering if there is a better alternative?
Thanks!
Maybe this comment will save you a google. But probably not.
> BPF stands for Berkeley Packet Filter, an obscure technology first developed in 1992 that improved the performance of packet capture tools. In 2013, Alexei Starovoitov proposed a major rewrite of BPF, which was further developed by Alexei and Daniel Borkmann and included in the Linux kernel in 2014. This turned BPF into a general-purpose execution engine that can be used for a variety of things, including the creation of advanced performance analysis tools.
> [...]
> Extended BPF is often abbreviated as eBPF, but the official abbreviation is still BPF, without the “e,” so throughout this book I use BPF to refer to extended BPF. The kernel contains only one execution engine, BPF (extended BPF), which runs both extended BPF and “classic” BPF programs.¹
> ¹ Classic BPF programs (which refers to the original BPF) are automatically migrated to the extended BPF engine by the kernel for execution. Classic BPF is also not being developed further.
I get that it's fun to experiment with BPF, but it's also useful to see how many tools already exist. BPF is powerful, yes - maybe too powerful, and it's often not the best tool for the job.
https://man7.org/linux/man-pages/man7/fanotify.7.html
I think fanotify does xattrs, but I haven't tested.
Would be interesting to compare the chances of race conditions with inotify vs BPF for this contrived use-case.
Doesn’t take away from the original point that bpf is cool.
If a directory has a time-to-live of 8 hours, then any files or subdirectories inside the directory will be deleted 8 hours after their last modification.
That way the applications that work on these files don't need to be setting special attributes on every file.
https://www.amazon.com/Oxidize-eBPF-programming-Rust/dp/B0BP...
If one ever happenes to casually find 'several petabytes of old logs that were not deleted', whoever's in charge should consider having several people fired over that.
Serious business these things.