|
|
Log in / Subscribe / Register

Leading items

Welcome to the LWN.net Weekly Edition for December 9, 2021

This edition contains the following feature content:

This week's edition also includes these inner pages:

  • Brief items: Brief news items from throughout the community.
  • Announcements: Newsletters, conferences, security updates, patches, and more.

Please enjoy this week's edition, and, as always, thank you for supporting LWN.net.

Comments (none posted)

Python discusses deprecations

By Jake Edge
December 8, 2021

Feature deprecations are often controversial, but many projects find it necessary, or desirable, to lose some of the baggage that has accreted over time. A mid-November request to get rid of three Python standard library modules provides a case in point. It was initially greeted as a good idea since the modules had been officially deprecated starting with Python 3.6; there are better ways to accomplish their tasks now. But, of course, removing a module breaks any project that uses it, at least without the project making some, perhaps even trivial, changes. The cost of that is not insignificant, and the value in doing so is not always clear, which led to higher-level conversation about deprecations.

Module removal

Victor Stinner posted a notice to the python-dev mailing list saying that he wanted to remove asyncore, asynchat, and smtpd from the standard library for Python 3.11 (due in October 2022). His justification noted that there are better alternatives (asyncio for the first two and aiosmtpd, available in PyPI, for the last). As can be seen in the links, all of the modules suggested for removal are documented to be deprecated and have been since Python 3.6 was released in 2016. But Stinner also noted that the DeprecationWarning for those modules was only emitted at run time in Python 3.10, which drew an objection from Petr Viktorin:

According to the policy, the warning should be there for *at least* two releases. (That's a minimum, for removing entire modules it might make sense to give people even more time.)

Viktorin is referring to PEP 387 ("Backwards Compatibility Policy"), which says that features cannot be removed "without notice between any two consecutive releases". Stinner interpreted that to mean the documentation notice (for 4 releases) was sufficient, but Viktorin and others did not agree. As can be seen in the bug tracking the removal, which goes back to 2016, Stinner added a note about the removal plan and, after getting several approvals from other core developers, merged that change on November 15.

But Brett Cannon thought that the removal was not in keeping with the PEP, thus premature. He noted that the steering council (which he is a member of) can grant exceptions, so Stinner opened an issue with the council to resolve the question. On December 6, the council issued its ruling that the removals should not be done for Python 3.11, so Stinner duly reverted the change.

Wider context

During that discussion, Viktorin started another thread about the more general question of deprecations for the language. It was motivated, in part, by rebuilds the Fedora project was doing using the alpha versions of Python 3.11. He pointed to a few examples of problems that arise from names that have been removed from standard library modules under the deprecation policy. He wondered:

Are these changes necessary? Does it really cost us that much in maintainer effort to keep a well-tested backwards compatibility alias name, or a function that has a better alternative?

I think that rather than helping our users, changes like these are making Python projects painful to maintain. If we remove them to make Python easier for us to develop, is it now actually that much easier to [maintain]?

He also said that he was complaining that the PEP guideline was meant as a minimum of two releases, but it is often treated as "wait two releases and remove". He was not talking about "code that's buggy, insecure, or genuinely hard to maintain", but that routinely removing names of various sorts makes it harder for Python-based projects to keep up. He asked if there might be a need for an alternative formulation:

If deprecation now means "we've come up with a new way to do things, and you have two years to switch", can we have something else that means "there's now a better way to do things; the old way is a bit worse but continues to work as before"?

Stinner pointed out that all of the examples he cited had been deprecated (and emitted a warning) for far longer than two cycles (the most recent was deprecated in Python 3.5). Viktorin acknowledged that but: "as far as I know, they haven't really caused problems in all that time"

Stinner described some of the problems that arise from leaving deprecated functions in the code base. They cause users and developers to do some extra thinking when they are encountered: "Why is it still there? What is its purpose? Is there a better alternative?". The answers to those questions are sometimes only found in the bug tracker or Git repository. Beyond that, some deprecations are being done to prod developers into using better alternatives; as Stinner mentioned for the removals he wanted to do, deprecated code is effectively unmaintained:

Open issues in asyncore, asynchat and smtpd have been closed as "wont fix" because these modules are deprecated. These modules are basically no longer maintained.

But Viktorin said that he is not arguing against the removal "if something's an attractive-looking trap", but there are other types of changes that basically boil down to the core Python developers enforcing their opinions on names and functionality:

Python users want to write commits that either bring value, or that are fun. Mass-replacing "failUnless" with "assertTrue" just because someone decided it's a better name is neither. Same with a forced move to the latest version of a function, if you don't use the bells and whistles it added.

Stinner had said that those who are using deprecated functions are building up technical debt: "An application using multiple deprecated functions will break with a future Python version." But Viktorin said in some cases that is the fault of the core developers:

But "will break with a future Python version" just means that people's code breaks because *we break it*. If we stopped doing that (in the simple cases of name aliases or functions that are older but not dangerous), then their code wouldn't break.

Christopher Barker said that keeping old names around "forever" was not a good solution, but that name changes like for failUnless should not be made if it is only done because someone liked the new name better. Removing the older versions of the names does have beneficial effects, Serhiy Storchaka said, since typos that land on the older valid names can persist in a code base without being noticed:

It is so easy to make a typo and write assertEquals instead of assertEqual or assertRaisesRegexp instead of assertRaisesRegex. Tests are passed, and warnings are ignored. Then I run tests with -Werror to test new warnings and get a lot of unrelated failures because of PRs merged at last half-year. I am very glad that these long time ago deprecated aliases are finally removed.

Increasing the window

Some kind of tool that helped maintainers make these simple substitution changes would be a nice addition, Stephen J. Turnbull said. Barker agreed but noted that a big problem is trying to support applications across multiple versions of the language, some of which have the new feature and some of which have to use the older name.

But do we need to support running the same code on 3.5 to 3.10? I don't think so. If you can't upgrade Python to a supported version, you probably shouldn't upgrade your code or libraries.

Which is a thought — maybe the policy should be that we remove things when the new way is usable in all supported versions of Python. So as of today (if I'm correct) anything needed in 3.5 can be dropped.

That idea, in a somewhat different guise, was suggested by Eric V. Smith, who was also in favor of tools to assist maintainers:

[...] I think a useful stance is "we won't remove anything that would make it hard to support a single code base across all supported python versions". We'd need to define "hard", maybe "no hasattr calls" would be part of it.

Reliable tools to make the migration between versions would help, too.

Cannon restated Smith and Barker's ideas in a more concrete form:

I think Eric was suggesting more along the lines of PEP 387 saying that deprecations should last as long as there is a supported version of Python that lacks the deprecation. So for something that's deprecated in 3.10, we wouldn't remove it until 3.10 is the oldest Python version we support. That would be October 2025 when Python 3.9 reaches EOL and Python 3.13 comes out as at that point you could safely rely on the non-deprecated solution across all supported Python versions (or if you want a full year of overlap, October 2026 and Python 3.14).

I think the key point with that approach is if you wanted to maximize your support across supported versions, this would mean there wouldn't be transition code except when the SC [steering council] approves of a shorter deprecation. So a project would simply rely on the deprecated approach until they started work towards Python 3.13, at which point they drop support for the deprecated approach and cleanly switch over to the new approach as all versions of Python at that point will support the new approach as well.

That is a direction Viktorin would like to see, but if the PEP were to change along those lines (as Cannon suggested might be a path forward), he would rather see some recognition that there is a cost to these kinds of changes, but also to retain some flexibility:

I'm not looking for a contract, rather a best practice. I think we should see Python's benign warts as nice gestures to the users: signs that we're letting them focus on issues that matter to them, rather than forcing them to join a quest for perfection. If a wart turns out to be a tumor, we should be able to remove it after the 2 years of warnings (or less with an exception). That's fine as a contract. But I don't like "spring cleaning" -- removing everything the contract allows us to remove.

Ensuring more perfect code should be a job for linters, not the interpreter/stdlib.

Terry Reedy noted that the change to yearly releases also shortened the minimum deprecation period from three years to two, so the idea of waiting until a replacement is available in all supported Python versions makes sense. Looking to the future:

Python is nearly 30 years old. I am really glad it is not burdened with 30 years of old names. I expect someone reading this may write some version of Python 50 years from now. I would not want [them] to have to read about names deprecated 60 years before such a time.

Jeremiah Paige mentioned a tool that might be used to help maintainers going forward: pyupgrade. It is a tool to upgrade code to newer versions of Python, but does not cover some of the cases that Viktorin mentioned. That could presumably be fixed with code contributions to the project. Stinner suggested a collaboration between the Python core developers and the pyupgrade developers to ensure that deprecations are added to the tool as things are being removed from the language.

The deprecation problem is something that crops up for Python and other projects with some frequency. There is a desire to remove old cruft (or "dead batteries") from the language, but there is obviously a balance to be struck. Other projects, the Linux kernel being a famous example, generally never deprecate anything unless it can be shown that it is no longer in use—or actively harmful. While there was some agreement with Viktorin in the thread, there were others who are intent on removing things as soon as reasonably possible. Whether that turns out to be longer than the minimum of two releases remains to be seen.

Comments (63 posted)

Detecting missing memory barriers with KCSAN

By Jonathan Corbet
December 2, 2021
Writing (correct) concurrent code that uses locking to avoid race conditions is difficult enough. When the objective is to use lockless algorithms, relying on memory barriers instead of locks to eliminate locking overhead, the problem becomes harder still. Bugs are easy to create and hard to find in this type of code. There may be some help on the way, though, in the form of this patch set from Marco Elver that enhances the Kernel Concurrency Sanitizer (KCSAN) with the ability to detect some types of missing memory barriers.

KCSAN works in a statistical manner by watching accesses to specific memory addresses and trying to detect racy patterns; the algorithm used is described in this article. In its current form, though, KCSAN can only catch certain types of race conditions, specifically those that arise from locking errors. Other types of races remain invisible to this tool, including a number that can arise in incorrect lockless code. KCSAN is, by design, blind to the kinds of problems that occur when CPUs and memory controllers reorder the visibility of memory writes.

Consider this code example, taken from this documentation patch in Elver's series (and lightly edited):

    int x, flag;
    void T1(void)
    {
        x = 1;                  // data race!
        WRITE_ONCE(flag, 1);    // should be: smp_store_release(&flag, 1)
    }

    void T2(void)
    {
        while (!READ_ONCE(flag))    // should be: smp_load_acquire(&flag)
	    ;
        ... = x;                    // data race!
    }

At a first glance, this code appears correct. T1() stores a value into the variable x then sets flag to indicate that x is valid. The other thread, running in T2(), does not attempt to read x until flag is set, so it should always proceed with a valid value. There is only one little problem: the lack of memory barriers gives the CPU the permission to reorder those operations, which appear to be independent; the write to x in T1() could, in fact, be visible to the rest of the system after the write to flag. That could cause T2() to proceed thinking it has a valid value of x when the real value is not yet visible to that thread.

Correct code would have used smp_store_release() to write flag; that would ensure that all writes done prior to that store would be visible globally before the store to flag becomes visible. Similarly, smp_load_acquire() is needed to read flag in a way that doesn't allow later reads to be reordered to happen earlier. Barriers almost always need to be paired in this way to work properly; omitting either half of the pair creates erroneous code.

This kind of bug can be difficult for developers implementing lockless algorithms to avoid; the need for a memory barrier for a specific access is not always obvious. Code containing such bugs can work just fine with all of the developer's tests, only to fail on a handful of production systems in obscure settings. That is why some developers, once they understand the challenges of lockless programming, conclude that dedicating their career to implementing annoying popups in JavaScript isn't such a bad thing after all.

KCSAN, in current kernels, cannot detect this race either. A system running under KCSAN may decide that the store to x in T1() is interesting and worthy of watching, but the way that KCSAN watches work will prevent the race from occurring. KCSAN will start monitoring accesses to x and, while that watch exists, it will delay the further execution of T1() to see if any racy accesses happen. T1() will only proceed (and set flag) once the watch concludes. KCSAN will thus delay the write to flag, causing T2() to wait, until the watch concludes. So the racy access cannot happen as long as KCSAN is watching, and will go undetected.

The new code makes one seemingly simple change to try to detect this kind of problem — though that apparent simplicity is belied by the fact that a 25-part patch series is required to implement it. Rather than just forgetting about x after the watch period ends, KCSAN will repeat the watch after every subsequent memory access until either a memory barrier is encountered or the function returns. In the case described above, KCSAN will watch x again after the assignment to flag, essentially simulating the reordering of the writes to those two variables. In essence, this repeated watch is seeing what will happen if the write to x becomes visible later than the developer expects. Any access to x seen on repeated watches is still racy, since no memory barrier has been executed to ensure correct ordering. So KCSAN will now detect the racy read of x in T2() and raise the alarm.

This algorithm can detect a set of race conditions caused by missing barriers, but not all of them. Most notably, it can test the effects of delaying an access to shared data — such as delaying the visibility of the write to x in the example above — but not the effects of executing that access earlier than the developer expects. But, even if its coverage is incomplete, the improved KCSAN may well be able to prevent a number of barrier-related bugs from getting to the point where they impact users. That may make lockless algorithms a bit more accessible to non-superhuman developers and might even free a few of them from the specter of a future devoted to JavaScript.

Comments (13 posted)

A filesystem for namespaces

By Jonathan Corbet
December 3, 2021
It is natural, when looking at the kernel development process, to focus on patches that find their way to acceptance and become a part of future kernels. But there can be value in looking at work that doesn't clear the bar; in failing, these patches often reveal things about the kernel and the community that creates it. Such is the case with the proof-of-concept namespacefs patch series recently posted by Yordan Karadzhov. One should not expect to see namespacefs in a future kernel but, in failing, this work showed a real use case and why it is hard to satisfy that use case in the kernel.

Namespacefs is, as one might expect, a virtual filesystem implemented by the kernel. Its job is to display the hierarchy of namespaces running on the system; this information reflects the hierarchy of containers that are running. By using namespacefs, administrators can more readily see what is happening on their systems; it is also meant to facilitate complicated use cases like tracing multiple containers and watching how they interact.

The initial implementation was limited to the PID and time namespaces. One can use it to traverse the hierarchy of PID namespaces (time namespaces are not hierarchical) and obtain the list of processes running in each. Other types of namespaces are not supported in this posting, but the intent was seemingly to add that support in a future version if namespacefs looked like the right solution to the problem.

As Karadzhov wrote:

Being able to see the structure of the namespaces can be very useful in the context of the containerized workloads. This will provide universal methods for detecting, examining and monitoring all sorts of containers running on the system, without relaying on any specific user-space software.

Much of this information is available in user space now in the form of directories under /proc, but there are some missing pieces and that information is not organized in a way that shows the actual namespace hierarchy. Container-orchestration systems can also provide a view of the containers they manage, of course, but they don't provide a solution for the general case. Namespacefs was meant to make this information readily available regardless of which orchestration systems are in use.

There were a few objections raised to this work, starting with the fact that a namespace's entry in namespacefs needs to have a name. There are currently no names associated with namespaces, so namespacefs uses the number of the inode that is attached to each namespace inside the kernel. Eric Biederman was quick to criticize that approach, saying: "It is not correct to use inode numbers as the actual names for namespaces". He went on to say that there was nothing else that could be used as names for namespaces either, and that the entire idea was unworkable.

There are, it seems, a couple of problems with using inode numbers as names for namespaces. One of those, which Biederman spelled out later, is that there is no way to recreate the namespace hierarchy at a later time with the same names. That, he said, would break any system that uses, for example, CRIU to checkpoint and restart containers, perhaps as part of a live-migration scheme. The only way to handle this properly, he said, is to create a namespace for namespace names, and that has proved to be a hard problem in the past.

The CRIU issue is only relevant if containers that may be checkpointed will use namespacefs. As both Karadzhov and Steve Rostedt pointed out, that is unlikely; the whole point of namespacefs is to show the situation on a specific machine. There is no reason for anybody to want to move namespacefs — or any container making use of namespacefs — across machines or even to checkpoint it. It is, of course, hazardous to assume that nobody will want to use a feature in a certain way in the future but, in the absence of a surprising use case, the naming problem may not be an issue in actual use.

An arguably deeper problem, though, is that namespacefs can be seen as an attempt to recognize containers in the kernel, but the kernel has (by design) no concept of a "container". The kernel, instead, provides a set of pieces that user-space systems can assemble into varying types of containers. The namespacefs patches use PID namespaces as the objects around which the hierarchy is built and ignore user namespaces entirely. Biederman, in his initial response, criticized that decision, saying that "there is definitely no meaningful hierarchy without the user namespace". Not all containers use user namespaces, though, and those namespaces lack the process-ID information that Karadzhov's patch was meant to expose in the first place.

But, as James Bottomley pointed out, not all containers use PID namespaces either. Trying to identify containers without PID namespaces in namespacefs is not going to lead to much joy.

The end result is that it appears difficult to implement something like namespacefs in the kernel without introducing some sort of concept of what a container is. There is no more appetite for doing that now than there has been in past years; the lack of a container abstraction in the kernel is seen as having enabled a great deal of innovation on the user-space side. For this reason alone, namespacefs would be a hard sell in the kernel community.

It also appears, though, that it should be possible to get the required information entirely in user space by digging through a lot of /proc files. If there is information that is missing, it can be added to /proc rather than introducing an entirely new filesystem. So that is the approach that Karadzhov will take to solve this problem. Another proof of concept will be put together to show how it would work.

If that implementation turns out to be difficult or impossible to do efficiently, then there might be an argument for reconsidering namespacefs. Otherwise, though, a mechanism like namespacefs seems unlikely to make it into the kernel. That particular effort may not have led directly to the desired result, but it did create a discussion that coalesced on a seemingly better solution and, in the process, highlighted some of the constraints brought by the kernel's lack of a container concept. A reluctance to implement policy is generally a good thing, but it can end up making certain kinds of problems harder for users to solve.

Comments (15 posted)

A reference-count tracking infrastructure

By Jonathan Corbet
December 6, 2021
Reference counts are a commonly used mechanism for tracking the life cycle of objects in a computing system. As long as every user of an object correctly maintains its references by incrementing and decrementing the reference count, that object will persist for as long as it is needed and will be properly destroyed once the last user is done. The "correctly" in that sentence is important, though; things do not work as well in the presence of reference-counting errors. Networking developer Eric Dumazet is working on a reference-count tracking system that could prove useful for finding these errors in the networking subsystem and, someday, throughout the kernel.

Bugs in reference-count manipulation can be hard to find because the references themselves are anonymous. It may become clear, for example, that some user of an object has failed to release a reference before forgetting about that object, but there is generally no way to know which user has done this. So the kernel ends up with an unused object that cannot be released, but there is no way to know where the reference-counting mechanism failed, or even which reference was lost. If there were a way to determine which of (say) several dozen references to an object was leaked, the task of finding the erroneous release path would be made considerably easier.

Dumazet's patch set addresses this problem by creating a tracking mechanism that essentially surrounds the use of reference counts. The first step in using reference-count tracking is to add the tracker itself, in the form of a ref_tracker_dir structure:

    struct ref_tracker_dir {
    #ifdef CONFIG_REF_TRACKER
	spinlock_t		lock;
	unsigned int		quarantine_avail;
	refcount_t		untracked;
	struct list_head	list; /* List of active trackers */
	struct list_head	quarantine; /* List of dead trackers */
    #endif
    };

This structure does not contain the reference count itself; instead, it is added to the object that contains the reference count of interest. The first use is in the massive net_device structure that, as one might expect, is used to manage network devices. These structures are referenced in many places in the networking subsystem and are prone to reference-counting bugs; references are taken and released so often that a bespoke, per-CPU reference-counting mechanism is used. One leaked reference is enough to prevent the removal of a network device from the system; that, in turn, can block the clean-up of containers or virtual machines. To reduce the pain of finding these bugs, Dumazet's patch set starts by adding a ref_tracker_dir structure to struct net_device. This structure is initialized (via a required call to ref_tracker_dir_init()) with the lists empty and quarantine_avail (to be discussed below) set to a caller-supplied value.

For the reference-count tracking mechanism to work properly, every code path that takes or releases a reference must also tell the tracker about that action. That is relatively easily done in settings where reference-count manipulation is encapsulated within some other function; otherwise a fair number of code changes may be required. Whenever the code takes a tracked reference to an object, it should make a call to:

    int ref_tracker_alloc(struct ref_tracker_dir *dir, struct ref_tracker **trackerp,
			  gfp_t gfp_flags);

The dir argument points to the ref_tracker_dir structure added to the reference-counted object. This function will allocate a ref_tracker structure to track this specific reference, using the given gfp_flags, and storing its address in *trackerp. The return value is zero or an error code (mostly likely -ENOMEM if it is not possible to allocate the ref_tracker structure).

ref_tracker_alloc() will add the new structure to list in the ref_tracker_dir structure, thus noting that a reference has been taken. To be useful, though, this tracker must somehow make a note of where this specific reference was taken. The name of the function taking the reference would be useful, but often the real bug is several layers up the call stack, so a bit more information is needed to identify the real source of a problem. The tracking mechanism uses a kernel debugging feature called "stackdepot", which is able to generate and save full stack traces; stackdepot is rigorously undocumented, but the source is in lib/stackdepot.c. By storing the full stack trace, the tracking mechanism gains the ability to shed light on the real source of reference-counting problems.

When a reference is released, a call must be made to:

    int ref_tracker_free(struct ref_tracker_dir *dir, struct ref_tracker **trackerp);

This function will do a number of things, starting by removing the tracker pointed to (indirectly) by trackerp from list. The tracker is marked internally as having been released, but that structure is not immediately freed; instead, it is added to the quarantine list and the current stack trace is stored within it. This is done to catch double-free bugs; if ref_tracker_free() is called on a ref_tracker structure that is marked as already having been released, an immediate report is generated showing the stack traces associated with the allocation event and both free events.

With busy objects, the quarantine list could easily grow to great length, so the above-mentioned quarantine_avail count is used to limit its length. Whenever a tracker is added to quarantine, the count is checked; if quarantine_avail is zero, the oldest tracker on the quarantine list will be freed, otherwise the count will be decremented. The initial value of quarantine_avail is specified at ref_tracker_dir initialization time; for network devices, it can grow to 128 entries.

Double-free bugs can be caught immediately, but leaked references can only be detected when the time comes to free the reference-counted object. When the kernel is instructed to remove a network device, it will wait until the reference count on that device reaches zero; at this point, a leaked reference makes itself known by blocking the operation until the system is restarted. By this time, of course, the information about which reference has been leaked is long gone — at least, until the reference-count tracking mechanism comes into play. Any references taken to the object that were never released will still have active ref_tracker structures sitting around. A simple call to ref_tracker_dir_print() will cause the stack traces associated with any leaked references to be printed to the system log.

The advantage of this mechanism is clear: it should be able to shine a light on a reference-counting bugs, easing the process of debugging those errors. On the other hand, it is a fairly heavyweight mechanism that is not suitable for production use. The implementation also requires that reference-count tracking be separately plumbed into every subsystem that wants to use it, and the changes may not be small; 21 patches out of Dumazet's 23-part series are dedicated to instrumenting struct net_device. A debugging mechanism transparently built directly into the refcount_t type would potentially be more applicable and less intrusive, but it wouldn't work with the one-off mechanism used with network devices.

So this work is not, yet, a general-purpose reference-count debugging tool, but it is a significant step in that direction. The pieces are there waiting for a sufficiently motivated developer to turn them into something more general. Meanwhile, what's there should help to reduce the number of reference-counting bugs in the networking code, which is a good start.

Comments (none posted)

FIPS-compliant random numbers for the kernel

By Jake Edge
December 7, 2021

The Linux random-number generator (RNG) seems to attract an outsized amount of attention (and work) for what is, or seemingly should be, a fairly small component of the kernel. In part that is because random numbers, and their quality, are extremely important to a number of security protections, from unpredictable IP-packet sequence numbers to cryptographic keys. A recent post of version 43 of the Linux Random Number Generator (LRNG) by Stephan Müller is not likely to go any further than its predecessors, but the discussion around it may lead to support for a feature that some distributions need.

The cover letter for the LRNG patch set is titled "/dev/random - a new approach", which is true, but also sure to elicit highly skeptical responses or cause the patches to be ignored entirely. As was reiterated in the discussion, kernel development generally does not proceed along the "wholesale replacement" path; features are added slowly, in bite-sized chunks, instead. But LRNG is meant to be a drop-in replacement for the existing kernel RNG, while adding a long list of additional features—some of which would likely be welcome if they were separated out.

Müller pointed to a set of presentation slides for a good overview of LRNG. One area where the kernel RNG has had difficulties over the years is in gathering enough entropy to provide cryptographic-strength random numbers at boot time, especially for virtual machines and systems without much entropy from disk interrupts (e.g. using SSDs). LRNG collects entropy faster at boot time using CPU execution-time jitter and other techniques. As described in his LRNG paper, this entropy collection complies with the SP 800-90B standard from the US National Institute of Standards and Technology (NIST). In addition, LRNG uses techniques to combine entropy sources in a fully documented, mathematical approach, rather than the informal mechanism in the current kernel RNG.

The first patch covers the gathering and handling of entropy by the LRNG framework. It provides a deterministic RNG (DRNG) that is compliant with NIST SP 800-90A, but it allows for other DRNG implementations to be used within the framework. But the fact that it is meant as a drop-in replacement for the existing RNG means that it replaces all of that code. The existing code works, with some known limitations, perhaps, but "starting over" with a new implementation has its own set of dangers.

Beyond that, several in the discussion were skeptical about the value of the NIST standards (also known as FIPS standards); Jason A. Donenfeld said:

You've posted it again, and yet I still believe this is not the correct design or direction. I do not think the explicit goal of extended configurability ("flexibility") or the explicit goal of being FIPS compatible represent good directions, and I think this introduces new problems rather than solving any existing ones. While there are ways the current RNG could or even should be improved -- or rewritten -- this approach is still not that, no matter how many times you post it.

But Müller noted that some distributions are carrying patches to comply with FIPS, which has led to fragmentation of the cryptographic RNG in the kernel. The idea behind LRNG is to try to ensure that whichever DRNG is chosen, the resulting random numbers are generated in a secure way. In addition, he said that he had not received complaints about the LRNG design, while he had incorporated lots of changes along the way as suggested by various Linux developers. The changelog for the patch set, which goes back to 2016, shows quite a few changes of that sort.

Distributions and FIPS

Greg Kroah-Hartman wanted to know about distributions carrying patches for FIPS compliance and wondered: "Why have the distros not submitted their changes upstream?" Simo Sorce, who is on the RHEL crypto team, answered: "We have not proposed them because they are hacks, we know they are hacks, and we know they are not the long term solution." Red Hat does need some way to have FIPS compliance in its products, he said.

But Kroah-Hartman said: "Hacks that work today are the step toward a real solution." He reiterated that evolution is what is needed to get FIPS compliance into the kernel, rather than completely replacing the random-number subsystem. "Work off of those known-working-and-certified hacks. Submit them and let's go from there please."

Similarly, John Haxby reported that Oracle carries a patch to enable a FIPS-compliant RNG at boot time or by writing to a sysfs file; in FIPS mode, it always reseeds the DRNG from the jitter entropy. He said that it is "not healthy" for Oracle to carry out-of-tree patches like this, but it was expedient. He would rather have something upstream that is shared by everyone, but sees the patch as a temporary workaround:

We're carrying this patch simply because the certification requirements changed and this was the quickest and easiest way to workaround today's problem. It won't fix tomorrow's problem and next time we, and others, attempt FIPS certification then we, and others, will need a different /dev/random because neither the old one nor our quick and dirty workaround will actually be acceptable.

Kroah-Hartman suggested that the patch was also a good starting point: "Now that's a much smaller and simpler and easier to understand change, compared to 'rewrite the whole random number generator'." He said that if those who need FIPS compliance worked together to get something working into the mainline, that would likely be an easier path.

But adding a stand-alone separate random subsystem just for this is not a good idea and is one huge reason why this patch set keeps being ignored by the kernel developers.

Sorce also replied to Donenfeld, noting that "FIPS is essential for us and any design must include an option to be FIPS certifiable"; Müller has been working with distributions and standards organizations to gather and implement the requirements. In Sorce's other message, he described some of that work:

These patches have not been maturing in a void, but Stephan basically distilled discussions between multiple vendors as well as regulatory bodies (as you can see he has reviews from BSI and NIST requirements are also fully represented here).

He addressed a few aspects I can mention but are not the only ones: performance (esp on NUMA systems), not blocking at boot due to lack of entropy, NIST/BSI conformance, flexibility so that future regulatory requirements can be easily integrated and upstreamed.

More FIPS

Kroah-Hartman would rather see the normal kernel development path followed here: "Remember, evolution is the correct way of kernel development, not intelligent design :)". But, as Müller pointed out, there was a patch set posted for discussion in September 2020 to evolve the current RNG into one that was compliant, which never really went anywhere. Kroah-Hartman asked: "That's a load of patches, some of them seem sane, what ever happened to them?" The answer, Müller said, is: "Nothing was discussed, nothing was picked up."

Müller also said that LRNG does not actually replace the existing kernel RNG, it just provides a way for alternatives to be used:

One side note: the LRNG patch set does not replace random.c, but provides an additional implementation that can be selected at compile time. I am under the impression that is an equal approach considering other areas of the kernel like file systems, memory allocators, and similar.

While it may make sense to have multiple upstream implementations in some areas, the kernel RNG is not one of those areas, Kroah-Hartman said. Beyond that, the kernel RNG is used in multiple places in the kernel; "Odds are, you REALLY do not want the in-kernel calls to be pulling from the 'random-government-crippled-specification' implementation, right?" Sorce did not agree:

When our customers are mandated to use FIPS certified cryptography, they want to use it for kernel cryptography as well, and in general they want to use a certified randomness source as well.

He understands the hesitancy to trust government agencies in light of problems like the Dual EC DRBG mess, but the NIST specifications are not mandating a particular algorithm; the requirements are meant to allow multiple different implementations. Furthermore:

The specification is quite thorough and provides well reasoned requirements as well as self-test that insure coding mistakes won't end up returning non-random values.

Maintainer questions

But there is another problem in following the evolution path to change the kernel RNG, Sorce said:

And the main question here is, how can we get there, in any case, if the maintainer of the random device doesn't even participate in discussions, does not pick obvious bug fixes and is simply not engaging at all?

Your plan requires an active maintainer that guides these changes and interacts with the people proposing them to negotiate the best outcome. But that is not happening so that road seems blocked at the moment.

Ted Ts'o is the maintainer of the kernel RNG, but has been notably absent in this and other discussions of changes and fixes for that subsystem. Kroah-Hartman seemed skeptical that bug fixes were not being picked up, but Eric Biggers listed several fixes and cleanups that had languished before eventually being picked up by other maintainers (one of them by Kroah-Hartman, in fact). Biggers concluded: "So unfortunately, as far as I can tell, Ted is not maintaining random.c anymore."

Donenfeld said that he was willing to review fixes and improvements for the kernel RNG, but cautioned that he is concerned that the FIPS requirements may be overbroad:

And so it would seem that the goal of implementing the RNG as best as we can might potentially be at odds with the goal of getting that green compliance checkbox, because that checkbox oversteps its bounds a bit.

[...] I would like the kernel to have an excellent CSPRNG [cryptographically-secure pseudorandom number generator], from a cryptographic point of view, from a performance point of view, from an API point of view. I think these motivations are consistent with how the kernel is generally developed. And I think front loading the motivations with an external compliance goal greatly deviates and even detracts from the way the kernel is generally developed.

[...] Specifically, I think that if you change your perspective from, "how can we change the algorithms of the RNG to be FIPS" to "how can we bend FIPS within its limits so that having what customers want would minimally impact the quality of the RNG implementation or introduce undue maintenance burdens." This means: not refactoring the RNG into some large abstraction layer that's pluggable and supports multiple different implementations, not rewriting the world in a massive patchset, not adding clutter. Instead, perhaps there's a very, very minimal set of things that can be done that would be considerably less controversial.

Sorce was amenable to that approach, and Haxby said that he would submit the Oracle patch as a possible path forward. What we are seeing, at least in part, is a new maintainer volunteering to help out with the kernel RNG, which Ts'o supports. If a more evolutionary approach, with reasoning beyond just "because FIPS", is proposed, it would seem that the kernel RNG may be able to check the compliance box without the upheaval that a full-on replacement could bring. Another possibility was raised by Sandy Harris; the FIPS requirements might be met with the existing RNG, but there are hurdles there as well:

[...] in fact their DRNG design requires an external source of random bits. However, it requires that the source be certified & that would be a problem for us. Intel & others might be able to get their random number instructions certified and vendors of crypto or SOC chips might get theirs certified, but the kernel community could not do that.

I think the kernel's entropy collection routines are good enough that they could, in principle, be certified, but that would involve some work & considerable money.

It seems clear that some solution is needed, at least for the enterprise distributions. Müller's patches provide a mechanism that is FIPS-compliant and apparently has minimal impact in terms of performance—perhaps even better performance than the existing implementation—while solving a number of other problems. The techniques used could form a basis for a relatively small number of changes that might benefit all users of the kernel RNG. The patch set as it stands now is not going to fly, but, with luck and some perseverance, the FIPS requirements could be met by following the usual kernel-development strategy. Only time will tell.

Comments (13 posted)

Page editor: Jonathan Corbet
Next page: Brief items>>


Copyright © 2021, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds