Secure file ops, TLS hardening, input validation, security docs. (0.40.7)
- Added secure_state_open(): hardened file open with O_CLOEXEC, O_NOFOLLOW, mode 0600, regular-file and ownership checks. Prevents symlink attacks, TOCTOU races, and world-writable temp files. - Replaced all fopen() calls for temp state files with secure_state_open() + fdopen() across hot reload, compile-restart, and rebuild paths. - Hardened TLS initialization: enforces minimum TLS 1.2, disables compression, checks SSL_CTX_set_default_verify_paths() return value. - CMake compiler/linker hardening: -Wformat=2, -Wstrict-prototypes, -fstack-protector-strong, _FORTIFY_SOURCE=3, RELRO, noexecstack. - Added !calc input validation: max 256 chars, numeric-only characters. - Added !stock symbol validation: alphanumerics plus .-^= only. - Fixed !quit, !reload, !restart to require channel context (DMs rejected). - Fixed log timestamp format: %y (2-digit) to %Y (4-digit year). - Fixed asprintf return value check in !forecast error paths. - Fixed !reload fork-in-place mode: only saves TLS state when exec_new=1. - Cleaned up reload state file on failure and in fork child. - Fixed git SSH-to-HTTPS URL conversion in !gitlog and !changelog. - AI plan mode no longer passes --dangerously-skip-permissions. - Added IRC buffer overflow protection: oversized lines are dropped. - Added doc comments to ZYNK_RELOAD_FILE, secure_state_open(), RateLimitEntry, and rate_limits.
This commit is contained in:
parent
3b863d581d
commit
72f7812630
16 changed files with 187 additions and 38 deletions
84
zynk.c
84
zynk.c
|
|
@ -28,14 +28,52 @@ void log_stamp(void) {
|
|||
time_t t = time(NULL);
|
||||
tm = localtime(&t);
|
||||
char buf[64];
|
||||
strftime(buf, sizeof buf, "%y-%m-%d %H:%M:%S", tm);
|
||||
strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S", tm);
|
||||
fprintf(stderr, CLR_DIM "[%s]" CLR_RESET " ", buf);
|
||||
}
|
||||
|
||||
/* ---- hot reload ---- */
|
||||
|
||||
/*
|
||||
* ZYNK_RELOAD_FILE - Temporary file used to persist TLS session state
|
||||
* across hot reloads. Written by reload_save() with the full SSL session
|
||||
* (hex-encoded) and all connection parameters (host, port, nick, etc.),
|
||||
* then read back by reload_try_restore() so the new process can resume
|
||||
* the IRC connection without a visible disconnect. The file is created
|
||||
* with mode 0600 via secure_state_open() and unlinked after use.
|
||||
*/
|
||||
#define ZYNK_RELOAD_FILE "/tmp/zynk_reload"
|
||||
|
||||
/*
|
||||
* secure_state_open - Open a file descriptor with security hardening.
|
||||
*
|
||||
* Opens the given path with O_CLOEXEC and O_NOFOLLOW appended to the
|
||||
* caller-supplied flags, and mode 0600 (owner read/write only). After
|
||||
* opening, verifies the file is a regular file (S_ISREG) owned by the
|
||||
* effective user (geteuid). If O_CREAT is among the flags, fchmod is
|
||||
* called to enforce S_IRUSR | S_IWUSR. This prevents TOCTOU races,
|
||||
* symlink attacks, and world-writable state files.
|
||||
*
|
||||
* Parameters:
|
||||
* path - The filesystem path to open.
|
||||
* flags - open() flags (O_CLOEXEC and O_NOFOLLOW are added implicitly).
|
||||
*
|
||||
* Returns: A valid file descriptor on success, or -1 on failure (with
|
||||
* errno set to EPERM for ownership/type checks).
|
||||
*/
|
||||
int secure_state_open(const char *path, int flags) {
|
||||
int fd = open(path, flags | O_CLOEXEC | O_NOFOLLOW, 0600);
|
||||
if (fd < 0) return -1;
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode) || st.st_uid != geteuid()) {
|
||||
close(fd);
|
||||
errno = EPERM;
|
||||
return -1;
|
||||
}
|
||||
if (flags & O_CREAT) fchmod(fd, S_IRUSR | S_IWUSR);
|
||||
return fd;
|
||||
}
|
||||
|
||||
/*
|
||||
* reload_read_line - Read a single line from a file, stripping newlines.
|
||||
*
|
||||
|
|
@ -81,7 +119,8 @@ static int reload_save(Session *s) {
|
|||
unsigned char *p = sbuf;
|
||||
i2d_SSL_SESSION(sess, &p);
|
||||
SSL_SESSION_free(sess);
|
||||
FILE *f = fopen(ZYNK_RELOAD_FILE, "w");
|
||||
int fd = secure_state_open(ZYNK_RELOAD_FILE, O_WRONLY | O_CREAT | O_TRUNC);
|
||||
FILE *f = fd >= 0 ? fdopen(fd, "w") : NULL;
|
||||
if (!f) { free(sbuf); return -1; }
|
||||
fprintf(f, "%d\n%s\n%d\n%s\n%s\n%s\n%s\n%s\n%d\n%d\n",
|
||||
slen, s->host, s->port, s->nick, s->user, s->real,
|
||||
|
|
@ -106,7 +145,8 @@ static int reload_save(Session *s) {
|
|||
* Returns: 1 when settings were restored, -1 if no reload file or on error.
|
||||
*/
|
||||
static int reload_try_restore(Session *s) {
|
||||
FILE *f = fopen(ZYNK_RELOAD_FILE, "r");
|
||||
int fd = secure_state_open(ZYNK_RELOAD_FILE, O_RDONLY);
|
||||
FILE *f = fd >= 0 ? fdopen(fd, "r") : NULL;
|
||||
if (!f) return -1;
|
||||
char line[1024];
|
||||
int slen = 0, port = 0, use_tls_f = 0, tls_verify_f = 0;
|
||||
|
|
@ -144,7 +184,8 @@ static int reload_try_restore(Session *s) {
|
|||
s->use_tls = use_tls_f;
|
||||
tls_verify = tls_verify_f;
|
||||
free(sbuf);
|
||||
FILE *cf = fopen("/tmp/zynk_code_restart", "r");
|
||||
int cfd = secure_state_open("/tmp/zynk_code_restart", O_RDONLY);
|
||||
FILE *cf = cfd >= 0 ? fdopen(cfd, "r") : NULL;
|
||||
if (cf) {
|
||||
fclose(cf);
|
||||
unlink("/tmp/zynk_code_restart");
|
||||
|
|
@ -154,6 +195,7 @@ static int reload_try_restore(Session *s) {
|
|||
return 1;
|
||||
fail:
|
||||
fclose(f);
|
||||
unlink(ZYNK_RELOAD_FILE);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -176,7 +218,7 @@ void reload_do(Session *s, const char *reply_target, int exec_new) {
|
|||
irc_msg(s, reply_target, "Cannot reload: not connected with TLS");
|
||||
return;
|
||||
}
|
||||
if (reload_save(s) < 0) {
|
||||
if (exec_new && reload_save(s) < 0) {
|
||||
irc_msg(s, reply_target, "Failed to save session state");
|
||||
return;
|
||||
}
|
||||
|
|
@ -209,6 +251,7 @@ void reload_do(Session *s, const char *reply_target, int exec_new) {
|
|||
}
|
||||
_exit(1);
|
||||
}
|
||||
unlink(ZYNK_RELOAD_FILE);
|
||||
return;
|
||||
}
|
||||
_exit(0);
|
||||
|
|
@ -234,7 +277,26 @@ static void handle_sigterm(int sig) { (void)sig; running = 0; }
|
|||
|
||||
/* ---- rate limiting ---- */
|
||||
|
||||
/*
|
||||
* RateLimitEntry - A single rate-limit tracking slot.
|
||||
*
|
||||
* Records the IRC nick and the timestamp of its most recent command.
|
||||
* Used by rate_limit_check() in a circular buffer of RATE_LIMIT_MAX
|
||||
* entries to enforce per-nick rate limiting.
|
||||
*
|
||||
* Fields:
|
||||
* nick - The IRC nick (NUL-terminated, max 31 chars).
|
||||
* t - The time() timestamp of the last command from this nick.
|
||||
*/
|
||||
typedef struct { char nick[32]; time_t t; } RateLimitEntry;
|
||||
|
||||
/*
|
||||
* rate_limits - Circular buffer of rate-limit entries.
|
||||
*
|
||||
* Holds RATE_LIMIT_MAX slots. When the buffer is full and a new entry
|
||||
* is needed, the oldest slot is evicted (LRU). A nick whose most
|
||||
* recent timestamp is within RATE_LIMIT_WINDOW seconds is rejected.
|
||||
*/
|
||||
static RateLimitEntry rate_limits[RATE_LIMIT_MAX];
|
||||
|
||||
/*
|
||||
|
|
@ -546,17 +608,25 @@ char *fetch_url(const char *url) { return run_curl(url); }
|
|||
*/
|
||||
int try_compile_and_restart(Session *s, const char *target) {
|
||||
log_stamp(); fprintf(stderr, CLR_GREEN "Code change detected, compiling..." CLR_RESET "\n");
|
||||
int efd = secure_state_open("/tmp/zynk_make_err", O_WRONLY | O_CREAT | O_TRUNC);
|
||||
if (efd < 0) {
|
||||
irc_msg(s, target, "Cannot create secure build log");
|
||||
return 0;
|
||||
}
|
||||
close(efd);
|
||||
int status = system("make 2>/tmp/zynk_make_err");
|
||||
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
||||
irc_msg(s, target, "Code compiled successfully, restarting...");
|
||||
log_stamp(); fprintf(stderr, CLR_GREEN "Compilation successful, restarting..." CLR_RESET "\n");
|
||||
FILE *cf = fopen("/tmp/zynk_code_restart", "w");
|
||||
int cfd = secure_state_open("/tmp/zynk_code_restart", O_WRONLY | O_CREAT | O_TRUNC);
|
||||
FILE *cf = cfd >= 0 ? fdopen(cfd, "w") : NULL;
|
||||
if (cf) { fprintf(cf, "%s\n", target); fclose(cf); }
|
||||
reload_do(s, target, 1);
|
||||
return 1;
|
||||
} else {
|
||||
char errbuf[512];
|
||||
FILE *f = fopen("/tmp/zynk_make_err", "r");
|
||||
efd = secure_state_open("/tmp/zynk_make_err", O_RDONLY);
|
||||
FILE *f = efd >= 0 ? fdopen(efd, "r") : NULL;
|
||||
if (f) {
|
||||
size_t n = fread(errbuf, 1, sizeof errbuf - 1, f);
|
||||
errbuf[n] = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue