Exploit database

GH-d1629fe617895422945e5707dcb78b76

googlesky/cve-2026-119958

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-119958

2 sources

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

Repository files7
googlesky/cve-2026-119958Revision 2dab24a31ec9

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

lab/src/ldapd.c

C · 8,939 bytes
/*
 * ldapd.c -- the LDAP service of the model.
 *
 * It listens for LDAP connections, decodes the requests a peer sends, and
 * answers them.  Each connection is handled in a child process, so that a
 * memory-safety failure caused by a malformed request is observed as the
 * death of that child instead of taking the service down.
 */
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

#include "ldapmodel.h"

static int read_exact(int fd, uint8_t *p, size_t n)
{
	size_t got = 0;

	while (got < n) {
		ssize_t r = read(fd, p + got, n - got);

		if (r == 0)
			return 0;
		if (r < 0) {
			if (errno == EINTR)
				continue;
			return -1;
		}
		got += (size_t)r;
	}
	return 1;
}

/*
 * Read one BER TLV from the stream.  Returns 1 on success, 0 on a clean end
 * of stream, -1 on a malformed encoding.
 */
static int read_tlv(int fd, uint8_t *buf, size_t bufsz, size_t *used)
{
	uint8_t l;
	size_t hdr = 2;
	size_t len;

	if (read_exact(fd, buf, 1) != 1)
		return 0;
	if ((buf[0] & 0x1f) == 0x1f)
		return -1;
	if (read_exact(fd, buf + 1, 1) != 1)
		return 0;

	l = buf[1];
	if (l & 0x80) {
		unsigned nb = l & 0x7f;

		if (nb == 0 || nb > 4)
			return -1;
		if (hdr + nb > bufsz)
			return -1;
		if (read_exact(fd, buf + hdr, nb) != 1)
			return 0;
		hdr += nb;
		len = 0;
		for (unsigned i = 0; i < nb; i++)
			len = (len << 8) | buf[2 + i];
	} else {
		len = l;
	}

	if (len > bufsz - hdr)
		return -1;
	if (len && read_exact(fd, buf + hdr, len) != 1)
		return 0;
	*used = hdr + len;
	return 1;
}

static void send_result(int fd, long msgid, uint8_t op_tag, int code,
                        const char *diag)
{
	struct berbuf op, msg;

	bb_init(&op);
	bb_int(&op, 0x0a, code);
	bb_str(&op, 0x04, "", 0);
	bb_str(&op, 0x04, diag, strlen(diag));

	bb_init(&msg);
	ldap_build_message(&msg, msgid, op_tag, op.p, op.len);
	if (write(fd, msg.p, msg.len) < 0) {
		/* the peer is gone */
	}
	bb_free(&op);
	bb_free(&msg);
}

/*
 * Append one attribute to a PartialAttributeList:
 * SEQUENCE { type AttributeDescription, vals SET OF AttributeValue }.
 */
static void add_attribute(struct berbuf *attrs, const char *type,
                          const void *value, size_t vlen)
{
	size_t attr_start = attrs->len;
	size_t set_start;

	bb_str(attrs, 0x04, type, strlen(type));
	set_start = attrs->len;
	bb_str(attrs, 0x04, value, vlen);
	bb_wrap(attrs, set_start, 0x31); /* SET OF AttributeValue */
	bb_wrap(attrs, attr_start, 0x30); /* the attribute SEQUENCE */
}

/*
 * A search result entry.  Besides the directory attribute it carries the
 * address of the service's dispatch routine: the proof of concept needs a
 * target address to overwrite the dispatch pointer with.
 */
static void send_search_entry(int fd, long msgid, const char *dn,
                              const char *attr, const char *value)
{
	struct berbuf entry, attrs, msg;
	void *leak = ldap_payload_address();
	uint8_t ptr[8];
	uint64_t a = (uint64_t)(uintptr_t)leak;

	for (int i = 0; i < 8; i++)
		ptr[i] = (uint8_t)(a >> (8 * i));

	bb_init(&attrs);
	add_attribute(&attrs, attr, value, strlen(value));
	add_attribute(&attrs, LAB_LEAK_ATTR, ptr, sizeof(ptr));
	bb_wrap(&attrs, 0, 0x30); /* SEQUENCE OF attribute */

	bb_init(&entry);
	bb_str(&entry, 0x04, dn, strlen(dn));
	bb_bytes(&entry, attrs.p, attrs.len);

	bb_init(&msg);
	ldap_build_message(&msg, msgid, LDAP_OP_SEARCH_ENTRY, entry.p, entry.len);
	if (write(fd, msg.p, msg.len) < 0) {
		/* peer gone */
	}

	bb_free(&entry);
	bb_free(&attrs);
	bb_free(&msg);
}

static void handle_connection(int fd, int signed_only)
{
	struct ldap_conn conn;
	uint8_t buf[65536];

	memset(&conn, 0, sizeof(conn));
	conn.fd = fd;
	conn.signed_only = signed_only;
	conn.slot = calloc(1, sizeof(*conn.slot));
	if (!conn.slot)
		return;
	conn.slot->fn = op_search_reply;
	conn.op.conn = &conn;

	for (;;) {
		size_t used = 0;
		int r = read_tlv(fd, buf, sizeof(buf), &used);
		long msgid = 0;
		unsigned op_tag = 0;
		const uint8_t *op_body = NULL;
		size_t op_len = 0;

		if (r <= 0)
			break;

		if (ldap_parse_message(&conn, buf, used, &msgid, &op_tag,
		                       &op_body, &op_len) != 0) {
			send_result(fd, msgid, LDAP_OP_SEARCH_DONE,
			            LDAP_PROTOCOL_ERROR, "malformed request");
			continue;
		}
		conn.msgid = msgid;

		switch (op_tag) {
		case LDAP_OP_BIND_REQUEST:
			if (ldap_parse_bind_request(&conn, op_body, op_len) != LDAP_SUCCESS) {
				send_result(fd, msgid, LDAP_OP_BIND_RESPONSE,
				            LDAP_PROTOCOL_ERROR, "malformed bind");
				break;
			}
			conn.bound = 1;
			conn.authed = 1;
			send_result(fd, msgid, LDAP_OP_BIND_RESPONSE, LDAP_SUCCESS, "");
			break;

		case LDAP_OP_SEARCH_REQUEST:
			/*
			 * With the LDAP server signing requirement in force, an
			 * operation that is not part of an authenticated session
			 * is refused before it is decoded.
			 */
			if (conn.signed_only && !conn.authed) {
				send_result(fd, msgid, LDAP_OP_SEARCH_DONE,
				            LDAP_UNWILLING_TO_PERFORM,
				            "the operation requires a signed session");
				break;
			}
			if (ldap_parse_search_request(&conn, op_body, op_len) != LDAP_SUCCESS) {
				send_result(fd, msgid, LDAP_OP_SEARCH_DONE,
				            LDAP_PROTOCOL_ERROR, "malformed search");
				break;
			}
			/*
			 * The decoded request is dispatched through the slot's
			 * dispatch pointer.  An out-of-bounds write that reached
			 * that pointer redirects the call.
			 */
			ldap_dispatch(&conn);
			send_search_entry(fd, msgid, "CN=Lab,DC=example,DC=lab",
			                  "cn", "lab");
			send_result(fd, msgid, LDAP_OP_SEARCH_DONE, LDAP_SUCCESS, "");
			break;

		case LDAP_OP_EXTENDED_REQUEST:
			if (ldap_parse_extended_request(&conn, op_body, op_len) != LDAP_SUCCESS) {
				send_result(fd, msgid, LDAP_OP_EXTENDED_RESP,
				            LDAP_PROTOCOL_ERROR, "malformed extended request");
				break;
			}
			send_result(fd, msgid, LDAP_OP_EXTENDED_RESP, LDAP_SUCCESS, "");
			break;

		case LDAP_OP_UNBIND_REQUEST:
			goto done;

		default:
			send_result(fd, msgid, LDAP_OP_SEARCH_DONE,
			            LDAP_PROTOCOL_ERROR, "unsupported operation");
			break;
		}
	}
done:
	free(conn.slot);
}

/* Record the death of a connection handler so that the proof of concept can
 * observe it without attaching to the process. */
static void record_worker_crash(int sig, const char *results_dir)
{
	char path[600];
	FILE *f;

	if (!results_dir || !results_dir[0])
		return;
	snprintf(path, sizeof(path), "%s/worker-crash.txt", results_dir);
	f = fopen(path, "w");
	if (!f)
		return;
	fprintf(f, "signal=%d\n", sig);
	fprintf(f, "worker=connection-handler\n");
	fclose(f);
}

int main(int argc, char **argv)
{
	const char *port_env = getenv("LDAP_PORT");
	const char *results_env = getenv("LDAP_RESULTS_DIR");
	const char *signing_env = getenv("LDAP_SIGNING_REQUIRED");
	int signed_only = (signing_env && !strcmp(signing_env, "1")) ? 1 : 0;
	int port = port_env ? atoi(port_env) : 389;
	int lsock;
	struct sockaddr_in sa;

	(void)argc;
	(void)argv;

	ldap_set_results_dir(results_env);
	if (results_env && results_env[0]) {
		mkdir(results_env, 0755);
		ldap_publish_symbols(results_env);
	}

	/*
	 * SIGCHLD is deliberately not ignored: the parent waits for each
	 * connection handler so that it can record the handler's death.
	 */
	signal(SIGCHLD, SIG_DFL);

	lsock = socket(AF_INET, SOCK_STREAM, 0);
	if (lsock < 0) {
		perror("socket");
		return 1;
	}
	{
		int one = 1;

		setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
	}

	memset(&sa, 0, sizeof(sa));
	sa.sin_family = AF_INET;
	sa.sin_addr.s_addr = htonl(INADDR_ANY);
	sa.sin_port = htons((uint16_t)port);

	if (bind(lsock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
		perror("bind");
		return 1;
	}
	if (listen(lsock, 16) < 0) {
		perror("listen");
		return 1;
	}

	fprintf(stderr, "ldapd: listening on %d%s\n", port,
	        signed_only ? " (LDAP signing required)" : "");
	fflush(stderr);

	for (;;) {
		int csock = accept(lsock, NULL, NULL);
		pid_t pid;

		if (csock < 0) {
			if (errno == EINTR)
				continue;
			perror("accept");
			break;
		}

		pid = fork();
		if (pid == 0) {
			close(lsock);
			handle_connection(csock, signed_only);
			close(csock);
			_exit(0);
		}
		close(csock);

		if (pid > 0) {
			int status;
			pid_t done = waitpid(pid, &status, 0);

			if (done > 0) {
				if (WIFSIGNALED(status)) {
					fprintf(stderr,
					        "ldapd: connection handler terminated by signal %d\n",
					        WTERMSIG(status));
					record_worker_crash(WTERMSIG(status), results_env);
				} else {
					fprintf(stderr,
					        "ldapd: connection handler exited with status %d\n",
					        WEXITSTATUS(status));
				}
				fflush(stderr);
			}
		}
	}
	close(lsock);
	return 0;
}