"""Shared ProgramGen execute-only reference client source.""" from __future__ import annotations def programgen_reference_client_c_source() -> str: return r"""#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include extern char **environ; static const char b64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char refusal_message[] = "programgen policy denied: debugging or wrapping " "/reference/executable is not allowed\n"; struct buffer { unsigned char *data; size_t len; size_t cap; }; static int reserve_buffer(struct buffer *buf, size_t extra) { if (extra > SIZE_MAX - buf->len - 1) { return -1; } size_t need = buf->len + extra + 1; if (need <= buf->cap) { return 0; } size_t cap = buf->cap ? buf->cap : 256; while (cap < need) { if (cap > SIZE_MAX / 2) { cap = need; break; } cap *= 2; } unsigned char *next = realloc(buf->data, cap); if (next == NULL) { return -1; } buf->data = next; buf->cap = cap; return 0; } static int append_bytes(struct buffer *buf, const void *data, size_t len) { if (reserve_buffer(buf, len) != 0) { return -1; } memcpy(buf->data + buf->len, data, len); buf->len += len; buf->data[buf->len] = '\0'; return 0; } static int write_all_fd(int fd, const unsigned char *data, size_t len); static int starts_with(const char *value, const char *prefix) { return strncmp(value, prefix, strlen(prefix)) == 0; } static int has_blocked_loader_environment(void) { for (char **entry = environ; entry != NULL && *entry != NULL; entry++) { if (starts_with(*entry, "LD_") || starts_with(*entry, "DYLD_")) { return 1; } } return 0; } static int tracer_pid(void) { FILE *status = fopen("/proc/self/status", "r"); if (status == NULL) { return 0; } char line[256]; int pid = 0; while (fgets(line, sizeof(line), status) != NULL) { if (starts_with(line, "TracerPid:")) { pid = atoi(line + strlen("TracerPid:")); break; } } fclose(status); return pid; } static int harden_process(void) { (void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); (void)prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); if (has_blocked_loader_environment() || tracer_pid() > 0) { (void)write_all_fd( STDERR_FILENO, (const unsigned char *)refusal_message, strlen(refusal_message) ); return -1; } return 0; } static int append_cstr(struct buffer *buf, const char *value) { return append_bytes(buf, value, strlen(value)); } static int append_char(struct buffer *buf, char value) { return append_bytes(buf, &value, 1); } static int append_json_string_content(struct buffer *buf, const char *value) { for (const unsigned char *p = (const unsigned char *)value; *p != '\0'; p++) { unsigned char ch = *p; switch (ch) { case '"': if (append_cstr(buf, "\\\"") != 0) { return -1; } break; case '\\': if (append_cstr(buf, "\\\\") != 0) { return -1; } break; case '\b': if (append_cstr(buf, "\\b") != 0) { return -1; } break; case '\f': if (append_cstr(buf, "\\f") != 0) { return -1; } break; case '\n': if (append_cstr(buf, "\\n") != 0) { return -1; } break; case '\r': if (append_cstr(buf, "\\r") != 0) { return -1; } break; case '\t': if (append_cstr(buf, "\\t") != 0) { return -1; } break; default: if (ch < 0x20) { char escaped[7]; snprintf(escaped, sizeof(escaped), "\\u%04x", ch); if (append_cstr(buf, escaped) != 0) { return -1; } } else if (append_char(buf, (char)ch) != 0) { return -1; } } } return 0; } static char *base64_encode(const unsigned char *data, size_t len) { if (len > (SIZE_MAX / 4 - 1) * 3) { return NULL; } size_t out_len = 4 * ((len + 2) / 3); char *out = malloc(out_len + 1); if (out == NULL) { return NULL; } size_t i = 0; size_t j = 0; while (i < len) { uint32_t octet_a = i < len ? data[i++] : 0; uint32_t octet_b = i < len ? data[i++] : 0; uint32_t octet_c = i < len ? data[i++] : 0; uint32_t triple = (octet_a << 16) | (octet_b << 8) | octet_c; out[j++] = b64_table[(triple >> 18) & 0x3f]; out[j++] = b64_table[(triple >> 12) & 0x3f]; out[j++] = b64_table[(triple >> 6) & 0x3f]; out[j++] = b64_table[triple & 0x3f]; } if (len % 3 == 1) { out[out_len - 2] = '='; out[out_len - 1] = '='; } else if (len % 3 == 2) { out[out_len - 1] = '='; } out[out_len] = '\0'; return out; } static int b64_value(char ch) { if (ch >= 'A' && ch <= 'Z') { return ch - 'A'; } if (ch >= 'a' && ch <= 'z') { return ch - 'a' + 26; } if (ch >= '0' && ch <= '9') { return ch - '0' + 52; } if (ch == '+') { return 62; } if (ch == '/') { return 63; } return -1; } static unsigned char *base64_decode(const char *value, size_t len, size_t *out_len) { *out_len = 0; if (len == 0) { return calloc(1, 1); } if (len % 4 != 0) { return NULL; } size_t padding = 0; if (len >= 1 && value[len - 1] == '=') { padding++; } if (len >= 2 && value[len - 2] == '=') { padding++; } size_t decoded_len = (len / 4) * 3 - padding; unsigned char *out = malloc(decoded_len ? decoded_len : 1); if (out == NULL) { return NULL; } size_t i = 0; size_t j = 0; while (i < len) { int a = b64_value(value[i++]); int b = b64_value(value[i++]); int c = value[i] == '=' ? 0 : b64_value(value[i]); i++; int d = value[i] == '=' ? 0 : b64_value(value[i]); i++; if (a < 0 || b < 0 || c < 0 || d < 0) { free(out); return NULL; } uint32_t triple = ((uint32_t)a << 18) | ((uint32_t)b << 12) | ((uint32_t)c << 6) | (uint32_t)d; if (j < decoded_len) { out[j++] = (triple >> 16) & 0xff; } if (j < decoded_len) { out[j++] = (triple >> 8) & 0xff; } if (j < decoded_len) { out[j++] = triple & 0xff; } } *out_len = decoded_len; return out; } static unsigned char *read_available_stdin(size_t *out_len) { *out_len = 0; fd_set readfds; FD_ZERO(&readfds); FD_SET(STDIN_FILENO, &readfds); struct timeval timeout = {0, 0}; int ready = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &timeout); if (ready <= 0 || !FD_ISSET(STDIN_FILENO, &readfds)) { return calloc(1, 1); } int flags = fcntl(STDIN_FILENO, F_GETFL); if (flags >= 0) { (void)fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); } struct buffer buf = {0}; unsigned char chunk[65536]; for (;;) { ssize_t n = read(STDIN_FILENO, chunk, sizeof(chunk)); if (n > 0) { if (append_bytes(&buf, chunk, (size_t)n) != 0) { free(buf.data); buf.data = NULL; break; } continue; } if (n == 0) { break; } if (errno == EINTR) { continue; } if (errno == EAGAIN || errno == EWOULDBLOCK) { break; } break; } if (flags >= 0) { (void)fcntl(STDIN_FILENO, F_SETFL, flags); } if (buf.data == NULL) { return calloc(1, 1); } *out_len = buf.len; return buf.data; } static char *current_working_directory(void) { size_t size = 256; for (;;) { char *buf = malloc(size); if (buf == NULL) { return NULL; } if (getcwd(buf, size) != NULL) { return buf; } free(buf); if (errno != ERANGE || size > (1 << 20)) { char *fallback = malloc(11); if (fallback != NULL) { memcpy(fallback, "/workspace", 11); } return fallback; } size *= 2; } } static int send_all(int fd, const void *data, size_t len) { const unsigned char *p = data; while (len > 0) { ssize_t n = send(fd, p, len, 0); if (n < 0) { if (errno == EINTR) { continue; } return -1; } if (n == 0) { return -1; } p += n; len -= (size_t)n; } return 0; } static int recv_exact(int fd, unsigned char *data, size_t len) { while (len > 0) { ssize_t n = recv(fd, data, len, 0); if (n < 0) { if (errno == EINTR) { continue; } return -1; } if (n == 0) { return -1; } data += n; len -= (size_t)n; } return 0; } static int send_u64_be(int fd, uint64_t value) { unsigned char encoded[8]; for (int i = 7; i >= 0; i--) { encoded[i] = value & 0xff; value >>= 8; } return send_all(fd, encoded, sizeof(encoded)); } static int recv_u64_be(int fd, uint64_t *value) { unsigned char encoded[8]; if (recv_exact(fd, encoded, sizeof(encoded)) != 0) { return -1; } uint64_t out = 0; for (size_t i = 0; i < sizeof(encoded); i++) { out = (out << 8) | encoded[i]; } *value = out; return 0; } static char *extract_json_string(const char *json, const char *key) { struct buffer pattern = {0}; if (append_char(&pattern, '"') != 0 || append_cstr(&pattern, key) != 0 || append_cstr(&pattern, "\":\"") != 0) { free(pattern.data); return NULL; } char *start = strstr(json, (char *)pattern.data); free(pattern.data); if (start == NULL) { return NULL; } start = strchr(start, ':'); if (start == NULL || start[1] != '"') { return NULL; } start += 2; char *end = start; while (*end != '\0') { if (*end == '"' && (end == start || end[-1] != '\\')) { break; } end++; } if (*end != '"') { return NULL; } size_t len = (size_t)(end - start); char *out = malloc(len + 1); if (out == NULL) { return NULL; } memcpy(out, start, len); out[len] = '\0'; return out; } static int extract_return_code(const char *json) { const char *pattern = "\"return_code\":"; char *start = strstr(json, pattern); if (start == NULL) { return 1; } start += strlen(pattern); return (int)strtol(start, NULL, 10); } static int write_all_fd(int fd, const unsigned char *data, size_t len) { while (len > 0) { ssize_t n = write(fd, data, len); if (n < 0) { if (errno == EINTR) { continue; } return -1; } data += n; len -= (size_t)n; } return 0; } static int env_key_equals(const char *key, size_t key_len, const char *expected) { return strlen(expected) == key_len && memcmp(key, expected, key_len) == 0; } static int is_allowed_env_key(const char *key, size_t key_len) { if (env_key_equals(key, key_len, "LANG") || env_key_equals(key, key_len, "LC_ALL")) { return 1; } return 0; } static int append_allowed_env(struct buffer *payload) { if (append_char(payload, '{') != 0) { return -1; } int first = 1; for (char **entry = environ; entry != NULL && *entry != NULL; entry++) { char *equals = strchr(*entry, '='); if (equals == NULL) { continue; } size_t key_len = (size_t)(equals - *entry); if (!is_allowed_env_key(*entry, key_len)) { continue; } if (!first && append_char(payload, ',') != 0) { return -1; } first = 0; if (append_char(payload, '"') != 0 || append_bytes(payload, *entry, key_len) != 0 || append_cstr(payload, "\":\"") != 0 || append_json_string_content(payload, equals + 1) != 0 || append_char(payload, '"') != 0) { return -1; } } return append_char(payload, '}'); } static int build_request(int argc, char **argv, struct buffer *payload) { if (append_cstr(payload, "{\"argv_b64\":[") != 0) { return -1; } for (int i = 0; i < argc; i++) { char *encoded = base64_encode((const unsigned char *)argv[i], strlen(argv[i])); if (encoded == NULL) { return -1; } int ok = 0; if (i > 0) { ok = append_char(payload, ','); } ok = ok || append_char(payload, '"') || append_cstr(payload, encoded) || append_char(payload, '"'); free(encoded); if (ok != 0) { return -1; } } char *cwd = current_working_directory(); if (cwd == NULL) { return -1; } if (append_cstr(payload, "],\"cwd\":\"") != 0 || append_json_string_content(payload, cwd) != 0 || append_cstr(payload, "\",\"env\":") != 0 || append_allowed_env(payload) != 0 || append_cstr(payload, ",\"stdin_b64\":\"") != 0) { free(cwd); return -1; } free(cwd); size_t stdin_len = 0; unsigned char *stdin_data = read_available_stdin(&stdin_len); if (stdin_data == NULL) { return -1; } char *stdin_b64 = base64_encode(stdin_data, stdin_len); free(stdin_data); if (stdin_b64 == NULL) { return -1; } int ok = append_cstr(payload, stdin_b64) || append_cstr(payload, "\"}"); free(stdin_b64); return ok == 0 ? 0 : -1; } int main(int argc, char **argv) { signal(SIGPIPE, SIG_IGN); if (harden_process() != 0) { return 126; } const char *socket_path = getenv("PROGRAMGEN_REFERENCE_ORACLE_SOCKET"); if (socket_path == NULL || socket_path[0] == '\0') { socket_path = "/run/programgen-reference-oracle/oracle.sock"; } struct buffer payload = {0}; if (build_request(argc, argv, &payload) != 0) { fprintf(stderr, "programgen reference oracle unavailable: failed to build request\n"); free(payload.data); return 127; } int fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd < 0) { fprintf(stderr, "programgen reference oracle unavailable: socket: %s\n", strerror(errno)); free(payload.data); return 127; } struct sockaddr_un addr; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; if (strlen(socket_path) >= sizeof(addr.sun_path)) { fprintf(stderr, "programgen reference oracle unavailable: socket path too long\n"); close(fd); free(payload.data); return 127; } strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) { fprintf(stderr, "programgen reference oracle unavailable: connect: %s\n", strerror(errno)); close(fd); free(payload.data); return 127; } if (send_u64_be(fd, (uint64_t)payload.len) != 0 || send_all(fd, payload.data, payload.len) != 0) { fprintf(stderr, "programgen reference oracle unavailable: send failed\n"); close(fd); free(payload.data); return 127; } free(payload.data); uint64_t response_size = 0; if (recv_u64_be(fd, &response_size) != 0 || response_size > (256ULL << 20)) { fprintf(stderr, "programgen reference oracle unavailable: invalid response\n"); close(fd); return 127; } char *response = malloc((size_t)response_size + 1); if (response == NULL) { close(fd); return 127; } if (recv_exact(fd, (unsigned char *)response, (size_t)response_size) != 0) { fprintf(stderr, "programgen reference oracle unavailable: receive failed\n"); free(response); close(fd); return 127; } close(fd); response[response_size] = '\0'; char *stdout_b64 = extract_json_string(response, "stdout_b64"); char *stderr_b64 = extract_json_string(response, "stderr_b64"); int return_code = extract_return_code(response); free(response); size_t stdout_len = 0; size_t stderr_len = 0; unsigned char *stdout_data = base64_decode(stdout_b64 ? stdout_b64 : "", stdout_b64 ? strlen(stdout_b64) : 0, &stdout_len); unsigned char *stderr_data = base64_decode(stderr_b64 ? stderr_b64 : "", stderr_b64 ? strlen(stderr_b64) : 0, &stderr_len); free(stdout_b64); free(stderr_b64); if (stdout_data == NULL || stderr_data == NULL) { free(stdout_data); free(stderr_data); fprintf(stderr, "programgen reference oracle unavailable: malformed response\n"); return 127; } (void)write_all_fd(STDOUT_FILENO, stdout_data, stdout_len); (void)write_all_fd(STDERR_FILENO, stderr_data, stderr_len); free(stdout_data); free(stderr_data); if (return_code < 0 || return_code > 255) { return 1; } return return_code; } """