diff --git a/include/rayforce.h b/include/rayforce.h index 2ee8716b8..aaf6a28b6 100644 --- a/include/rayforce.h +++ b/include/rayforce.h @@ -870,6 +870,23 @@ void ray_poll_destroy(ray_poll_t* poll); * A NULL poll is ignored. */ void ray_poll_set_restricted(ray_poll_t* poll, bool restricted); +/* Embedders can require this API before starting a password-protected + * listener on an older core. */ +#define RAY_POLL_AUTH_API 1 +/* Configure one optional password for every listener on the poll, native IPC + * and embedder-provided wire protocols alike. A NULL/empty password disables + * authentication. The same password is truncated to 255 bytes as before. + * A NULL password with restricted=true still selects the restricted role. */ +void ray_poll_auth_set(ray_poll_t* poll, const char* password, bool restricted); +/* Check "user:password" (or a bare password) against the poll's secret in + * constant time. With no configured password, any credentials are accepted. + * A single trailing NUL (as on a q or native login) is ignored. No secret is + * returned. */ +bool ray_poll_auth_check(const ray_poll_t* poll, const uint8_t* credentials, size_t len); +/* The evaluation role selected by the poll's password option. Embedders + * should snapshot it on accepted connections, as ray_poll_set_restricted. */ +bool ray_poll_auth_restricted(const ray_poll_t* poll); + /* Run until ray_poll_exit is called. */ int64_t ray_poll_run(ray_poll_t* poll); diff --git a/src/app/main.c b/src/app/main.c index 57be5efb4..6fc471f4d 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -349,14 +349,8 @@ int main(int argc, char** argv) { * the same event loop the REPL/server is driving. */ if (poll) ray_runtime_set_poll(poll); - if (poll && auth_pw) { - size_t pw_len = strlen(auth_pw); - if (pw_len >= sizeof(poll->auth_secret)) - pw_len = sizeof(poll->auth_secret) - 1; - memcpy(poll->auth_secret, auth_pw, pw_len); - poll->auth_secret[pw_len] = '\0'; - ray_poll_set_restricted(poll, auth_restricted); - } + if (poll && auth_pw) + ray_poll_auth_set(poll, auth_pw, auth_restricted); /* Open journal BEFORE the IPC listener accepts any connection. * Replay must complete with a stable global env, and any badtail diff --git a/src/core/ipc.c b/src/core/ipc.c index 884e924f5..72de894f6 100644 --- a/src/core/ipc.c +++ b/src/core/ipc.c @@ -200,39 +200,6 @@ size_t ray_ipc_decompress(const uint8_t* src, size_t clen, static int64_t g_tx_limit_bytes = RAY_IPC_TX_DEFAULT_BYTES; static int64_t g_tx_limit_frames = 0; /* unlimited */ -/* Constant-time comparison — prevents timing side-channel on password. */ -static bool ct_eq(const void* a, const void* b, size_t len) { - const volatile uint8_t* x = a; - const volatile uint8_t* y = b; - volatile uint8_t diff = 0; - for (size_t i = 0; i < len; i++) - diff |= x[i] ^ y[i]; - return diff == 0; -} - -/* Validate credential buffer against secret. Returns true if password matches. - * creds is "user:password\0" with length cred_len. - * secret MUST point to a char[256] buffer (zero-padded beyond the password). - * Compares pw against the full 256-byte secret buffer in constant time. - * No strlen, no secret-length-dependent copies. */ -static bool validate_creds(const uint8_t* buf, uint8_t cred_len, - const char* secret) { - if (cred_len == 0) return false; - const char* creds = (const char*)buf; - const char* colon = memchr(creds, ':', cred_len); - const char* pw = colon ? colon + 1 : creds; - size_t pw_len = colon ? (size_t)(cred_len - (pw - creds)) : cred_len; - if (pw_len > 0 && pw[pw_len - 1] == '\0') pw_len--; - if (pw_len > 255) pw_len = 255; - - /* Zero-pad pw into a 256-byte buffer, then compare all 256 bytes - * against the secret buffer (also 256 bytes, zero-padded at init). - * Matching passwords produce identical 256-byte buffers. */ - uint8_t pw_buf[256] = {0}; - memcpy(pw_buf, pw, pw_len); - return ct_eq(pw_buf, secret, 256); -} - /* ===== Connection hooks (.ipc.on.*) ===== * * Five user-settable lambdas that intercept the connection lifecycle. @@ -1414,8 +1381,7 @@ static ray_t* ipc_read_creds(ray_poll_t* poll, ray_selector_t* sel) ray_ipc_conn_data_t* cd = (ray_ipc_conn_data_t*)sel->data; - bool ok = validate_creds(sel->rx.buf->data + 1, cred_len, - poll->auth_secret); + bool ok = ray_poll_auth_check(poll, sel->rx.buf->data + 1, cred_len); /* Secondary user-defined check via `.ipc.on.auth`. Only consulted * when the constant-time secret compare already passed — this hook diff --git a/src/core/poll.c b/src/core/poll.c index dc945da3a..ca1e0df86 100644 --- a/src/core/poll.c +++ b/src/core/poll.c @@ -25,6 +25,7 @@ #include "core/timer.h" #include "mem/sys.h" #include +#include /* ===== Shared (platform-independent) poll helpers ===== */ @@ -38,6 +39,47 @@ void ray_poll_set_restricted(ray_poll_t* poll, bool restricted) if (poll) poll->restricted = restricted; } +/* A poll has one optional password and an evaluation role. Native IPC and + * embedders with another wire protocol must use this same verifier rather + * than grow subtly different password rules. */ +void ray_poll_auth_set(ray_poll_t* poll, const char* password, bool restricted) +{ + if (!poll) return; + memset(poll->auth_secret, 0, sizeof(poll->auth_secret)); + if (password) { + size_t n = strlen(password); + if (n >= sizeof(poll->auth_secret)) n = sizeof(poll->auth_secret) - 1; + memcpy(poll->auth_secret, password, n); + } + poll->restricted = restricted; +} + +/* Credentials are "user:password" (or a bare password), with no terminating + * NUL included. Compare a fixed-size zero-padded buffer, as the native path + * has always done: no password-length-dependent comparison or secret output. */ +bool ray_poll_auth_check(const ray_poll_t* poll, const uint8_t* credentials, size_t len) +{ + if (!poll || poll->auth_secret[0] == '\0') return true; + if (!credentials || len == 0) return false; + const uint8_t* colon = memchr(credentials, ':', len); + const uint8_t* password = colon ? colon + 1 : credentials; + size_t n = len - (size_t)(password - credentials); + if (n && password[n - 1] == '\0') n--; + if (n >= sizeof(poll->auth_secret)) n = sizeof(poll->auth_secret) - 1; + uint8_t supplied[256] = {0}; + memcpy(supplied, password, n); + const volatile uint8_t* a = supplied; + const volatile uint8_t* b = (const volatile uint8_t*)poll->auth_secret; + uint8_t different = 0; + for (size_t i = 0; i < sizeof supplied; i++) different |= a[i] ^ b[i]; + return different == 0; +} + +bool ray_poll_auth_restricted(const ray_poll_t* poll) +{ + return poll && poll->restricted; +} + ray_selector_t* ray_poll_get(ray_poll_t* poll, int64_t id) { if (!poll || id < 0 || (uint32_t)id >= poll->n_sels) diff --git a/src/core/poll.h b/src/core/poll.h index 7bfff01b3..96ec8889a 100644 --- a/src/core/poll.h +++ b/src/core/poll.h @@ -202,6 +202,9 @@ bool ray_poll_idle(ray_poll_t* poll); void ray_poll_drain_timers(ray_poll_t* poll); void ray_poll_destroy(ray_poll_t* poll); void ray_poll_set_restricted(ray_poll_t* poll, bool restricted); +void ray_poll_auth_set(ray_poll_t* poll, const char* password, bool restricted); +bool ray_poll_auth_check(const ray_poll_t* poll, const uint8_t* credentials, size_t len); +bool ray_poll_auth_restricted(const ray_poll_t* poll); int64_t ray_poll_register(ray_poll_t* poll, ray_poll_reg_t* reg); void ray_poll_deregister(ray_poll_t* poll, int64_t id); int64_t ray_poll_run(ray_poll_t* poll); diff --git a/test/test_ipc.c b/test/test_ipc.c index 578387644..f6d7e610e 100644 --- a/test/test_ipc.c +++ b/test/test_ipc.c @@ -1124,7 +1124,10 @@ static test_result_t test_ipc_poll_auth_creds_path(void) { ray_poll_t* poll = ray_poll_create(); TEST_ASSERT_NOT_NULL(poll); - strcpy(poll->auth_secret, "pollpass"); + ray_poll_auth_set(poll, "pollpass", true); + TEST_ASSERT_TRUE(ray_poll_auth_restricted(poll)); + TEST_ASSERT_TRUE(ray_poll_auth_check(poll, (const uint8_t*)"user:pollpass", 13)); + TEST_ASSERT_FALSE(ray_poll_auth_check(poll, (const uint8_t*)"user:wrong", 10)); int64_t listener_id = ray_ipc_listen(poll, 0); TEST_ASSERT((listener_id) >= (0), "listener_id >= 0"); @@ -1162,7 +1165,11 @@ static test_result_t test_ipc_poll_auth_reject(void) { ray_poll_t* poll = ray_poll_create(); TEST_ASSERT_NOT_NULL(poll); - strcpy(poll->auth_secret, "secret"); + ray_poll_auth_set(poll, "secret", false); + TEST_ASSERT_FALSE(ray_poll_auth_check(poll, (const uint8_t*)"", 0)); + TEST_ASSERT_FALSE(ray_poll_auth_check(poll, (const uint8_t*)"user:", 5)); + TEST_ASSERT_TRUE(ray_poll_auth_check(poll, (const uint8_t*)"secret", 6)); + TEST_ASSERT_TRUE(ray_poll_auth_check(poll, (const uint8_t*)":secret", 7)); int64_t listener_id = ray_ipc_listen(poll, 0); TEST_ASSERT((listener_id) >= (0), "listener_id >= 0");