File size: 18,103 Bytes
d086025 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | """Shared ProgramGen execute-only reference client source."""
from __future__ import annotations
def programgen_reference_client_c_source() -> str:
return r"""#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
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;
}
"""
|