[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
|
|
Subscribe / Log in / New account

Bpfilter (and user-mode blobs) for 4.18

By Jonathan Corbet
May 30, 2018
In February, the bpfilter mechanism was first posted to the mailing lists. Bpfilter is meant to be a replacement for the current in-kernel firewall/packet-filtering code. It provides little functionality itself; instead, it creates a set of hooks that can run BPF programs to make the packet-filtering decisions. A version of that patch set has been merged into the net-next tree for 4.18. It will not be replacing any existing packet filters in its current form, but it does feature a significant change to one of its more controversial features: the new user-mode helper mechanism.

The core motivation behind bpfilter is performance. An in-kernel, general-purpose packet filter must necessarily offer a wide range of features; any given site will probably only use a small subset of those features. The result is a lot of unused code and time spent checking for whether a given feature is in use, slowing the whole thing down. A packet-filtering configuration expressed as a BPF program, instead, contains only the code needed to implement the desired policy. Once that code is translated to native code by the just-in-time compiler, it should be both compact and fast. The networking developers hope that it will be fast enough to win back some of the users who have moved to proprietary user-space filtering implementations.

If bpfilter is to replace netfilter, though, it must provide ABI compatibility so that existing configurations continue to work. To that end, the bpfilter developers intend to implement the current netfilter configuration protocol; bpfilter will accept iptables rules and compile them to BPF transparently. That compilation is not a trivial task, though, and one that could present some security challenges, so the desire is to do it in user space, but under kernel control.

To make that possible, the initial proposal included a new type of kernel module. Rather than containing kernel code, it contained a normal ELF executable that would be run as a special type of kernel thread. Using the module mechanism allowed this code to be packaged and built with the rest of the kernel; user-mode modules could also be subjected to the same signing rules. There were a number of concerns about how these modules worked, though, which led to some significant changes this time around.

For example, the user-mode helper code is no longer packaged as a module. It is, instead, a blob of code that is built into a normal kernel subsystem (which may be built into the kernel image or packaged as a module). In the sample code, the user-mode component is built as a separate program then, in a process involving "quite a bit of objcopy and Makefile magic", it is turned into an ordinary object file that can be linked into the bpfilter.ko kernel module.

Kernel code that wants to run a blob of code in user space will do so using the new helper code. That is done by calling:

    int fork_usermode_blob(void *data, size_t len, struct umh_info *info);

where data points to the code to be run, and len is the length of that code in bytes. The info structure is defined as:

    struct umh_info {
	struct file *pipe_to_umh;
	struct file *pipe_from_umh;
	pid_t pid;
    };

Assuming the user-space process is successfully created, the kernel will place its process ID into pid. The kernel will also create a pair of pipes for communicating with the new process; they will be stored in pipe_to_umh (for writing) and pipe_from_umh (for reading). The code itself is copied into a tmpfs file and executed from there; that allows it to be paged if needed. If the built-in copy of the code is marked as "initdata" (and thus placed in a different section), the caller can free it once the helper is running.

Kernel code that creates this type of helper process must take care to clean it up when the time comes. The process ID can be used to kill the process, and the pipes need to be closed.

The bpfilter module itself, as found in 4.18, does not actually do much. It creates the helper process and can pass a couple of no-op commands to it, but there is no packet-filtering machinery in place yet. That code exists (and has been posted recently) but has evidently been held back to give the user-mode helper mechanism a cycle to stabilize. Bpfilter is thus starting small, but it may have a big impact in the end; perhaps that's why Dave Miller said "let the madness begin" as he merged the code for 4.18.

The replacement of netfilter, even if it happens as expected, will take years to play out, but we may see a number of interesting uses of the new user-mode helper mechanism before then. The kernel has just gained a way to easily sandbox code that is carrying out complex tasks and which does not need to be running in a privileged mode; it doesn't take much effort to think of other settings where this ability could be used to isolate scary code. Just be careful not to call the result a "microkernel" or people might get upset.

Index entries for this article
KernelBPF
KernelModules/ELF modules
KernelNetworking/Packet filtering


to post comments

Bpfilter (and user-mode blobs) for 4.18

Posted May 30, 2018 18:01 UTC (Wed) by Sesse (subscriber, #53779) [Link] (4 responses)

How fast is JITed BPFed compared to C, anyway? I've never seen any benchmarks on this, and it sort of scares me a bit.

Bpfilter (and user-mode blobs) for 4.18

Posted May 30, 2018 18:20 UTC (Wed) by iabervon (subscriber, #722) [Link] (3 responses)

Are you comparing to C that does what the BPF does, or C that directly interprets a custom rule language? C is not a terribly good language for writing fast interpreters, and few people write their firewall rules in C.

Bpfilter (and user-mode blobs) for 4.18

Posted May 30, 2018 22:15 UTC (Wed) by Sesse (subscriber, #53779) [Link] (2 responses)

C that does what the BPF does. Especially for a complicated firewall rule (e.g. anything that has to parse the IP option header chain), it would be a fair amount of code for each rule. Similarly, people are pushing a lot of what used to be security modules into BPF, and so on, so it'd be interesting to know approximately what the overhead is.

Bpfilter (and user-mode blobs) for 4.18

Posted May 31, 2018 8:34 UTC (Thu) by gdamjan (subscriber, #33634) [Link] (1 responses)

> The core motivation behind bpfilter is performance. An in-kernel, general-purpose packet filter must necessarily offer a wide range of features; any given site will probably only use a small subset of those features. The result is a lot of unused code and time spent checking for whether a given feature is in use, slowing the whole thing down. A packet-filtering configuration expressed as a BPF program, instead, contains only the code needed to implement the desired policy. Once that code is translated to native code by the just-in-time compiler, it should be both compact and fast. The networking developers hope that it will be fast enough to win back some of the users who have moved to proprietary user-space filtering implementations.

Bpfilter (and user-mode blobs) for 4.18

Posted May 31, 2018 10:41 UTC (Thu) by Sesse (subscriber, #53779) [Link]

That's a whole lot of assertion without any benchmarks to back it up.

Bpfilter (and user-mode blobs) for 4.18

Posted May 30, 2018 19:58 UTC (Wed) by bendystraw (guest, #124653) [Link] (8 responses)

It looks like my procrastination when it comes to seriously digging into nftables is going to work out just fine.

Bpfilter (and user-mode blobs) for 4.18

Posted May 30, 2018 21:18 UTC (Wed) by rahvin (guest, #16953) [Link] (5 responses)

I was thinking the same thing as you. ;) Woohoo for sticking to the old stuff until the new stuff is replaced with even newer stuff.

Bpfilter (and user-mode blobs) for 4.18

Posted May 30, 2018 22:06 UTC (Wed) by ibukanov (subscriber, #3942) [Link] (3 responses)

This is a nice demo of Lindy effect, https://en.m.wikipedia.org/wiki/Lindy_effect

Bpfilter (and user-mode blobs) for 4.18

Posted May 31, 2018 8:04 UTC (Thu) by epa (subscriber, #39769) [Link] (2 responses)

Before I read the link I surmised that the Lindy effect was that when a large system adds a general-purpose language (in this case BPF) it will drive out less general, more specialized configuration languages (the firewall rule definitions). The end of this process is when the extension language becomes almost the whole program (Emacs started out as a set of macros for another editor but soon turned into an editor implemented entirely in Lisp). Does that "effect" have a name and a Wikipedia article?

Bpfilter (and user-mode blobs) for 4.18

Posted May 31, 2018 14:30 UTC (Thu) by ehiggs (subscriber, #90713) [Link] (1 responses)

The fact that you reference Lisp makes me think you already know the rule you're referring to:

https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule

> Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified, bug-ridden, slow implementation of half of Common Lisp.

Related is Zawinski's rule of software:

https://en.wikipedia.org/wiki/Jamie_Zawinski#Principles

> Every program attempts to expand until it can read mail. Those programs which cannot so expand are replaced by ones which can.

Bpfilter (and user-mode blobs) for 4.18

Posted May 31, 2018 18:16 UTC (Thu) by epa (subscriber, #39769) [Link]

Yes, I had both of those rules in mind, but that's not quite the effect I was stating. It would be a corollary to Greenspun's rule: that said buggy half-Lisp will then start to take over the rest of the program, usurping first the other configuration languages and then the core functionality. (Javascript in the web browser comes to mind as another example.)

Bpfilter (and user-mode blobs) for 4.18

Posted May 31, 2018 15:34 UTC (Thu) by atai (subscriber, #10977) [Link]

"Not invented now" syndrome, in addition to the "Not invented here" syndrome

Bpfilter (and user-mode blobs) for 4.18

Posted May 31, 2018 19:53 UTC (Thu) by flussence (guest, #85566) [Link] (1 responses)

nftables is a bit nicer to read/maintain than iptables after it's set up, so I don't regret making the effort to switch. I'd hope they learn from history and give this iteration a more reasonable learning curve though.

Bpfilter (and user-mode blobs) for 4.18

Posted Jun 7, 2018 9:15 UTC (Thu) by aktau (subscriber, #99005) [Link]

I read somewhere that they intend to haver nftables configuration be able to use bpfilter as a backend. In that case the learning wasn't for nothing. I also believe the nftables configuration language is far superior to iptables.

Bpfilter (and user-mode blobs) for 4.18

Posted Jun 4, 2018 7:56 UTC (Mon) by robert_s (subscriber, #42402) [Link]

There's an amount of irony in adding a mechanism for the kernel to run full blown user code to support the ability for it to be able to run restricted user code. Granted, there are some differences, but still...

Bpfilter (and user-mode blobs) for 4.18

Posted Sep 5, 2018 15:43 UTC (Wed) by gilbertoferreira43 (guest, #127049) [Link] (1 responses)

Hi there.

How can I disable this damn thing?

Thanks

Bpfilter (and user-mode blobs) for 4.18

Posted Sep 6, 2018 5:05 UTC (Thu) by lkundrak (subscriber, #43452) [Link]

CONFIG_BPFILTER=n

obviously

Bpfilter (and user-mode blobs) for 4.18

Posted Dec 9, 2019 14:03 UTC (Mon) by sofardware (guest, #135967) [Link]

I used Linux5.4.0,bpfilter and bpfilter_umh was installed and started,when iptables rule was created ok, but there is no xdp program for this rule with "bpf-tool p" commond,why ?
[root@localhost linux-5.4]# lsmod |grep bpfilter
bpfilter 24576 0
[root@localhost linux-5.4]# ps -aux | grep umh
root 14830 0.0 0.0 4224 664 ? S 16:18 0:00 bpfilter_umh

[root@localhost linux-5.4]# iptables -t filter -A INPUT -i lo -s 127.0.0.2/32 -d 127.0.0.1/32 -j DROP
[root@localhost linux-5.4]# iptables -nvL
Chain INPUT (policy ACCEPT 29 packets, 2057 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP all -- lo * 127.0.0.2 127.0.0.1

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 16 packets, 1496 bytes)
pkts bytes target prot opt in out source destination
[root@localhost linux-5.4]# bpftool p
[root@localhost linux-5.4]#


Copyright © 2018, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds