|
|
Subscribe / Log in / New account

TLS in the kernel

By Jake Edge
December 2, 2015

An RFC patch from Dave Watson at Facebook proposes moving the bulk of Transport Layer Security (TLS) processing into the kernel. There are a number of advantages he sees for doing so, but most of the commenters on the patch set seem a bit skeptical about the idea. TLS is, of course, the encryption layer that protects HTTPS and other internet protocols.

The patch set implements RFC 5288 encryption for TLS, which is based on the 128-bit advanced encryption standard (AES) using Galois counter mode (GCM)—also known as "gcm(aes)". That accounts for roughly 80% of the TLS connections that Facebook sees, Watson said. The idea is for the kernel to handle the symmetric encryption and decryption, while leaving the handshake processing to user space. The feature uses the user-space API to the kernel's crypto subsystem, which is accessed via sockets created using the AF_ALG address family.

The basic idea is that an AF_ALG socket and a regular TCP socket are both created. The TCP socket is used to do the handshake with the remote endpoint, which establishes keys and such. The keys (one each for sending and receiving) are passed to the crypto socket using setsockopt(). An operational socket is also created by making an accept() call on the crypto socket. That socket is used in further processing, including setting the initialization vectors (IVs) using sendmsg() and control messages created using CMSG. There are also two IVs, one for each direction. In addition, the file descriptor for the TCP socket is passed to the operational socket in a control message; the application will then read and write data from the operational socket. Watson pointed to an example C program that uses the new facility.

That approach has a number of benefits, according to Watson. Using some additional code that was not part of his submission, he said the in-kernel TLS showed 2-7% better performance than the equivalent done in user space. The idea was inspired by some work [PDF] that Netflix did on FreeBSD to improve the performance of TLS. In addition, two other features could benefit from having TLS in the kernel, he said. The kernel connection multiplexer (KCM) needs access to unencrypted data in the kernel, which this would provide; offloading TLS encryption and decryption to NICs would also require TLS framing support in the kernel.

But Hannes Frederic Sowa questioned two of those advantages. He believes that the existing facilities provided by Linux already do less copying than those that FreeBSD provides, so he suggested comparing the in-kernel approach with a user-space implementation using mmap() and vmsplice() on the TCP socket. Beyond that, he noted that kernel developers have been strong opponents of TCP-offloading efforts. In order to provide TLS offloading, a NIC would also need to handle the TCP layer, so it would effectively be doing TCP offloading as well.

Crypto maintainer Herbert Xu was a bit surprised at the approach. While he can see that using AF_ALG makes sense as a way to export TLS functionality to user space, it's not the way he might have approached it:

However, I must say that it wouldn't have been my first pick. I'd imagine a TLS socket to look more like a TCP socket, or perhaps a KCM socket as proposed by Tom.

But Watson noted that handling out-of-band (OOB) data is one reason to not just layer TLS on top of a TCP socket. TLS transfers data beyond just the data being sent by the application, for things like alerts or to change the cipher being used, but a TCP socket lacks an easy way to signal the reception of that kind of data. In Watson's patches, the crypto socket returns an error in that situation and user space can then read the OOB data from the TCP socket if it wishes.

But others also questioned the value of having TLS in the kernel at all. Modern processors provide user-space programs with access to accelerated crypto instructions directly, without a need for kernel intervention. There is some crypto-acceleration hardware out there, where there might be some benefit to having TLS in the kernel, but it has mostly fallen by the wayside because of better processor support for crypto. As Sowa put it:

There are some crypto [accelerators] out there so that putting tls into the kernel would give a net benefit, because otherwise user space has to copy data into the kernel for device access and back to user space until it can finally be send out on the wire.

Since processors provide aesni and other crypto extensions as part of their instruction set architecture, this, of course, does not make sense any more.

Overall, it looks like it will take some more convincing arguments before putting TLS in the kernel will be seriously considered. For some specialized situations, it might make sense to do so, but even the limited version Watson posted adds more than 1200 lines of code to the kernel—for dubious gains. Over time, more and more crypto has been added to the kernel, though, so maybe TLS will eventually find its way in too.


Index entries for this article
KernelNetworking/Protocols
SecurityLinux kernel
SecurityTransport Layer Security (TLS)


to post comments

TLS in the kernel

Posted Dec 3, 2015 6:01 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link] (13 responses)

I wish the TLS layer was added as a completely transparent wrapper on top of TCP (and maybe UDP) sockets.

Then it could be easily plugged into ANY daemon that can accept socket FDs. Want your wonderful fingerd talk over a secure connection? No problem, just run it under a TLS wrapper.

Also, my another dream is credential separation. I want all private keys to be confined to a super secure process that communicates that simply sends sockets to other system processes once TLS handshake completes.

I have something like this for my toy TLS implementation, but getting to full TLS is complicated.

TLS in the kernel

Posted Dec 3, 2015 10:04 UTC (Thu) by pbonzini (subscriber, #60935) [Link]

For fingerd and anything that can be run through inetd that's easy, just pass the operational AF_ALG socket as stdin/stdout. But these simple daemons don't need a full-blown TCP socket.

TLS in the kernel

Posted Dec 3, 2015 11:13 UTC (Thu) by zdzichu (subscriber, #17118) [Link] (1 responses)

For super secure key storage, wouldn't kernel keyring be the best?

TLS in the kernel

Posted Dec 3, 2015 18:17 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link]

I'm not aware of any TLS libraries that can utilize it. Also I remember problems when I tried to use it for tens of thousands of keys (for ipsec).

TLS in the kernel

Posted Dec 3, 2015 12:50 UTC (Thu) by richmoore (guest, #53133) [Link] (3 responses)

Unfortunately there are a few problems with this - sometimes reading from TLS requires writing more data this would seriously complicate things like select(). There are also the issues of error reporting, cipher suite selection etc. I think using something like stunnel is probably a better solution for just wrapping existing services in TLS.

TLS in the kernel

Posted Dec 3, 2015 18:25 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link] (2 responses)

> Unfortunately there are a few problems with this - sometimes reading from TLS requires writing more data this would seriously complicate things like select(). There are also the issues of error reporting, cipher suite selection etc.
So let the kernel deal with select() complications and everything. It's what it's designed to do, after all.

Error reporting and ciphersuit selection can very well be done by the "handshaker" process. In fact, it should be done there to make sure cipher preferences are not hard-coded in the application.

And I actually want in-kernel TLS for applications like webservers first.

TLS in the kernel

Posted Sep 5, 2017 10:31 UTC (Tue) by federico3 (guest, #101963) [Link] (1 responses)

> So let the kernel deal with select() complications and everything.

No, you are oversimplifying the problem. Moving all that complexity into the kernel would also greatly increase the attack surface and make security fixes more difficult to apply.

TLS in the kernel

Posted Sep 5, 2017 18:50 UTC (Tue) by Cyberax (✭ supporter ✭, #52523) [Link]

TLS can be implemented as a module, so it'll be at least runtime-replaceable. And having one place for fixes will greatly simplify a lot of stuff.

Also, don't exaggerate the complexity of code. TLS is nicely segmented, so the code to read and parse TLS datagrams is not more complicated than a typical in-kernel tunnel protocol.

TLS in the kernel

Posted Dec 3, 2015 16:54 UTC (Thu) by zokier (guest, #103686) [Link] (4 responses)

Something like tcpcrypt maybe? https://lwn.net/Articles/401943/

TLS in the kernel

Posted Dec 3, 2015 18:15 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link] (3 responses)

Tcpcrypt is pretty much an alternative name for TLS-in-the-kernel. Except that it's incompatible with it.

TLS in the kernel

Posted Dec 4, 2015 22:45 UTC (Fri) by eternaleye (guest, #67051) [Link] (2 responses)

This is incorrect - for one, Tcpcrypt has a fundamentally different security model.

To explain, while TLS is based on authenticated key exchange design principles, in which you perform a key exchange under some proof of identity, Tcpcrypt is based on the idea of anonymous key exchange with channel binding. In that model, you default to a channel which is secure against passive attackers, and by using channel binding can voluntarily authenticate the peer in order to secure it against active attackers.

As a side benefit, authentication becomes an application-level concern, fixing one of the longest-standing mismatches between what TLS provides and what applications need. If you don't believe me, just look at how SRP and client certificates have obsoleted javascript-based password pr- oh wait.

TLS in the kernel

Posted Dec 5, 2015 0:02 UTC (Sat) by Cyberax (✭ supporter ✭, #52523) [Link] (1 responses)

There's no fundamental difference between TLS and tcpcrypt. They both do an asymmetric key exchange to establish a session key.

They both are secure from passive attacks and insecure from MITM. Securing both protocols requires some sort of a shared info: a shared CA infrastructure or a pre-shared key (yes, TLS actually can use a pre-shared key using TLS-PSK or J-PAKE). Key pinning is one way to establish a pre-shared key (by trusting the first interaction).

TLS in the kernel

Posted Dec 8, 2015 3:03 UTC (Tue) by eternaleye (guest, #67051) [Link]

No, there is a fundamental difference, specifically in that tcpcrypt provides a primitive that can be used for authentication (the channel binding) while TLS encapsulates the authentication within the protocol. One can't use, say, SASL for TLS authentication; one can not use any TLS auth method that has not been allocated a ciphersuite.

TLS still lacks support for strong channel bindings; it has TLS-unique, but that's broken by Triple Handshake. Until proper token binding (https://tools.ietf.org/html/draft-ietf-tokbind-negotiatio...) is added, TLS cannot support the same authentication flows as tcpcrypt can, where upper-layer authentication (of either or both endpoints) is securely linked to the underlying channel.

TLS in the kernel

Posted Dec 4, 2015 13:11 UTC (Fri) by paulj (subscriber, #341) [Link]

For the likes of fingerd and other stdin/stdio type daemons designed to let a super-server handle the actual networking, could running them via a wrapper like stunnel not work?

TLS in the kernel

Posted Dec 3, 2015 16:44 UTC (Thu) by Trou.fr (subscriber, #26289) [Link] (1 responses)

Because adding a notoriously complex protocol parser in the kernel, reachable from the network, can only go well ... remember heartbleed ?

TLS in the kernel

Posted Dec 10, 2015 7:52 UTC (Thu) by farnz (subscriber, #17727) [Link]

The patch doesn't put a full TLS parser in the kernel, though. It puts in just enough to handle Application Data records, and leaves the rest of the protocol to userspace.

As it happens, Application Data records are the most common in any high-usage TLS connection, because they're the records that do useful work from an application perspective. Thus, leaving everything else (including Heartbeat records, which were the ones that Heartbleed got wrong) to userspace isn't going to cost much in performance, but is a huge boost in kernel security.

TLS in the kernel

Posted Dec 11, 2015 22:42 UTC (Fri) by ksandstr (guest, #60862) [Link]

>Using some additional code that was not part of his submission, he said the in-kernel TLS showed 2-7% better performance than the equivalent done in user space.

Like any other proposal where performance gain is central to its claimed benefits, this one doesn't describe how much TLS was reimplemented nor estimates of which portion of the gain comes from the reimplementation's simply being the better horsecart.

For example, with kernel code often comes a restriction of applicability which limits features, therefore making common code paths simpler in terms of the length of the shortest sequence of data-dependent branches. Kernels also tend not to link to huge middleware libraries, giving more opportunities for intramodule call optimization (regparms, inlining, etc.). These two alone could account for 2%; measurement jitter could be another 2%, and after that it's marginal.

Example C Program

Posted Oct 5, 2017 15:15 UTC (Thu) by arkguy (guest, #118914) [Link] (2 responses)

The mentioned example C program from https://github.com/djwatson/ktls is utterly out of date and fails to work.
Unfortunately I have not found any better, so the Documentation/networking/tls.txt seems to be the best start.

Example C Program

Posted Oct 5, 2017 16:24 UTC (Thu) by arkguy (guest, #118914) [Link]

Correct myself: https://github.com/Mellanox/tls-af_ktls_tool , which is mentioned in Documentation/networking/tls.txt, seems to be a working example.

Example C Program

Posted Jun 14, 2019 12:57 UTC (Fri) by mallesh537@gmail.com (guest, #132640) [Link]

Yes I think it will work with applied patches from watson

TLS in the kernel

Posted Jul 4, 2022 3:11 UTC (Mon) by scientes (guest, #83068) [Link]

I wish that

1. TLS implementations implemented RFC 7250, raw public keys.

2. TLS didn't require ASN.1 and a bunch of other overly complicated junk.

Like look at WireGuard. It isn't so friggen ugly.


Copyright © 2015, 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