Harden size arithmetic and input parsing
Use limits.h/stdint.h limits for allocation sizes and int casts, replace atoi with strtol, guard CAP buffer arithmetic, and clamp scrollback wrapping.
This commit is contained in:
parent
643c6df5ef
commit
557a909a9f
1 changed files with 86 additions and 30 deletions
116
irc9.c
116
irc9.c
|
|
@ -12,12 +12,14 @@
|
|||
#endif
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <locale.h>
|
||||
#include <netdb.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
|
@ -38,6 +40,7 @@
|
|||
#define MAXINPUT 512
|
||||
#define MAXHIST 64
|
||||
#define MAXSCROLL 500
|
||||
#define MAXSCROLLROWS (MAXSCROLL * 128) /* push_wrapped emits at most 128 rows per line */
|
||||
#define TREEW 24
|
||||
#define RETRY 5
|
||||
|
||||
|
|
@ -154,16 +157,40 @@ static int comp_count, comp_start, comp_end;
|
|||
/* ------------------------------------------------------------------ */
|
||||
/* small helpers */
|
||||
|
||||
static void fatal(const char *msg)
|
||||
{
|
||||
endwin();
|
||||
fprintf(stderr, "irc9: %s\n", msg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void *xmalloc(size_t n)
|
||||
{
|
||||
void *p = malloc(n);
|
||||
if (!p) { endwin(); fprintf(stderr, "irc9: out of memory\n"); exit(1); }
|
||||
if (!p) fatal("out of memory");
|
||||
return p;
|
||||
}
|
||||
|
||||
static size_t size_add(size_t a, size_t b)
|
||||
{
|
||||
if (a > SIZE_MAX - b) fatal("integer overflow");
|
||||
return a + b;
|
||||
}
|
||||
|
||||
static size_t size_mul(size_t a, size_t b)
|
||||
{
|
||||
if (a && b > SIZE_MAX / a) fatal("integer overflow");
|
||||
return a * b;
|
||||
}
|
||||
|
||||
static void *xalloc_array(size_t n, size_t size)
|
||||
{
|
||||
return xmalloc(size_mul(n, size));
|
||||
}
|
||||
|
||||
static char *dupstr(const char *s)
|
||||
{
|
||||
size_t n = strlen(s) + 1;
|
||||
size_t n = size_add(strlen(s), 1);
|
||||
char *p = xmalloc(n);
|
||||
memcpy(p, s, n);
|
||||
return p;
|
||||
|
|
@ -171,7 +198,7 @@ static char *dupstr(const char *s)
|
|||
|
||||
static char *dupnstr(const char *s, size_t n)
|
||||
{
|
||||
char *p = xmalloc(n + 1);
|
||||
char *p = xmalloc(size_add(n, 1));
|
||||
memcpy(p, s, n);
|
||||
p[n] = 0;
|
||||
return p;
|
||||
|
|
@ -216,7 +243,7 @@ static Window *window_new(Server *s, const char *name, Wintype type)
|
|||
snprintf(w->name, sizeof w->name, "%s", name);
|
||||
w->type = type;
|
||||
w->srv = s;
|
||||
w->lines = xmalloc(MAXSCROLL * sizeof *w->lines);
|
||||
w->lines = xalloc_array(MAXSCROLL, sizeof *w->lines);
|
||||
while (*tail) tail = &(*tail)->next;
|
||||
*tail = w;
|
||||
return w;
|
||||
|
|
@ -277,7 +304,7 @@ static void window_close(Server *s, Window *w)
|
|||
static char *strip_ircfmt(const char *s)
|
||||
{
|
||||
size_t n = strlen(s);
|
||||
char *out = xmalloc(n + 1);
|
||||
char *out = xmalloc(size_add(n, 1));
|
||||
size_t j = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
unsigned char c = (unsigned char)s[i];
|
||||
|
|
@ -311,8 +338,9 @@ static void win_append_raw(Window *w, const char *text)
|
|||
char stamp[16], *line;
|
||||
char *clean = strip_formatting ? strip_ircfmt(text) : dupstr(text);
|
||||
fmttime(stamp, sizeof stamp, "[%H:%M] ");
|
||||
line = xmalloc(strlen(stamp) + strlen(clean) + 1);
|
||||
snprintf(line, strlen(stamp) + strlen(clean) + 1, "%s%s", stamp, clean);
|
||||
size_t sz = size_add(size_add(strlen(stamp), strlen(clean)), 1);
|
||||
line = xmalloc(sz);
|
||||
snprintf(line, sz, "%s%s", stamp, clean);
|
||||
free(clean);
|
||||
w->lines[w->nlines++] = line;
|
||||
if (w->scroll > 0) w->scroll++;
|
||||
|
|
@ -415,11 +443,11 @@ static void show_names(Window *w)
|
|||
return;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
size_t count = 0;
|
||||
for (Member *m = w->members; m; m = m->next) count++;
|
||||
|
||||
char line[MAXLINE * 2];
|
||||
int len = snprintf(line, sizeof line, "* %d on %s:", count, w->name);
|
||||
int len = snprintf(line, sizeof line, "* %zu on %s:", count, w->name);
|
||||
if (len < 0) len = 0;
|
||||
if ((size_t)len >= sizeof line) len = (int)sizeof line - 1;
|
||||
|
||||
|
|
@ -436,6 +464,16 @@ static void show_names(Window *w)
|
|||
/* ------------------------------------------------------------------ */
|
||||
/* config */
|
||||
|
||||
static int parse_port(const char *str)
|
||||
{
|
||||
char *end = NULL;
|
||||
errno = 0;
|
||||
long v = strtol(str, &end, 10);
|
||||
if (errno == ERANGE || end == str || *end != '\0' || v < 1 || v > 65535)
|
||||
return 6667;
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
static void load_config(const char *path)
|
||||
{
|
||||
FILE *f = fopen(path, "r");
|
||||
|
|
@ -472,8 +510,7 @@ static void load_config(const char *path)
|
|||
Server *s = xmalloc(sizeof *s);
|
||||
memset(s, 0, sizeof *s);
|
||||
snprintf(s->host, sizeof s->host, "%s", a1);
|
||||
s->port = a2 ? atoi(a2) : 6667;
|
||||
if (s->port < 1 || s->port > 65535) s->port = 6667;
|
||||
s->port = a2 ? parse_port(a2) : 6667;
|
||||
if (a3 && (strcasecmp(a3, "ssl") == 0 || strcasecmp(a3, "tls") == 0))
|
||||
s->tls = 1;
|
||||
snprintf(s->nick, sizeof s->nick, "%s", g_nick);
|
||||
|
|
@ -511,6 +548,7 @@ static void load_config(const char *path)
|
|||
|
||||
static ssize_t net_read(Server *s, void *buf, size_t len)
|
||||
{
|
||||
if (len > INT_MAX) len = INT_MAX; /* SSL_read/write take int */
|
||||
#ifdef USE_OPENSSL
|
||||
if (s->ssl) return SSL_read(s->ssl, buf, (int)len);
|
||||
#endif
|
||||
|
|
@ -519,6 +557,7 @@ static ssize_t net_read(Server *s, void *buf, size_t len)
|
|||
|
||||
static ssize_t net_write(Server *s, const void *buf, size_t len)
|
||||
{
|
||||
if (len > INT_MAX) len = INT_MAX; /* SSL_read/write take int */
|
||||
#ifdef USE_OPENSSL
|
||||
if (s->ssl) return SSL_write(s->ssl, buf, (int)len);
|
||||
#endif
|
||||
|
|
@ -558,7 +597,7 @@ static void sendf(Server *s, const char *fmt, ...)
|
|||
va_start(ap, fmt);
|
||||
int n = vsnprintf(buf, sizeof buf - 2, fmt, ap);
|
||||
va_end(ap);
|
||||
if (n < 0) return;
|
||||
if (n < 0 || (size_t)n >= sizeof buf - 2) return; /* encoding error or truncation */
|
||||
size_t len = strlen(buf);
|
||||
buf[len] = '\r';
|
||||
buf[len + 1] = '\n';
|
||||
|
|
@ -787,8 +826,9 @@ static char *base64_encode(const unsigned char *in, size_t len)
|
|||
{
|
||||
static const char tbl[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
size_t cap = 4 * ((len + 2) / 3);
|
||||
char *out = xmalloc(cap + 1);
|
||||
size_t groups = size_add(len, 2) / 3;
|
||||
size_t cap = size_mul(groups, 4);
|
||||
char *out = xmalloc(size_add(cap, 1));
|
||||
size_t i = 0, j = 0;
|
||||
while (i + 2 < len) {
|
||||
unsigned v = (unsigned)in[i] << 16 | (unsigned)in[i + 1] << 8 | in[i + 2];
|
||||
|
|
@ -813,8 +853,8 @@ static char *base64_encode(const unsigned char *in, size_t len)
|
|||
static void sasl_authenticate(Server *s)
|
||||
{
|
||||
size_t ulen = strlen(s->sasl_user), plen = strlen(s->sasl_pass);
|
||||
size_t rawlen = 1 + ulen + 1 + plen;
|
||||
unsigned char *raw = xmalloc(rawlen + 1);
|
||||
size_t rawlen = size_add(size_add(ulen, plen), 2); /* NUL user NUL pass */
|
||||
unsigned char *raw = xmalloc(size_add(rawlen, 1));
|
||||
raw[0] = 0;
|
||||
memcpy(raw + 1, s->sasl_user, ulen);
|
||||
raw[1 + ulen] = 0;
|
||||
|
|
@ -827,6 +867,7 @@ static void sasl_authenticate(Server *s)
|
|||
|
||||
static void prefix_nick(const char *prefix, char *out, size_t outsz)
|
||||
{
|
||||
if (!outsz) return;
|
||||
if (!prefix) { out[0] = 0; return; }
|
||||
const char *p = prefix;
|
||||
while (*p && *p != '!' && *p != '@') p++;
|
||||
|
|
@ -930,7 +971,7 @@ static void handle_line(Server *s, char *line)
|
|||
int more = narg >= 4 && strcmp(arg[2], "*") == 0;
|
||||
const char *part = more ? arg[3] : arg[2];
|
||||
size_t used = strlen(s->caps);
|
||||
size_t room = sizeof s->caps - used - 1;
|
||||
size_t room = used < sizeof s->caps ? sizeof s->caps - used - 1 : 0;
|
||||
if (part && room) strncat(s->caps, part, room - 1);
|
||||
if (more) return;
|
||||
|
||||
|
|
@ -1769,7 +1810,15 @@ static void handle_key(int c)
|
|||
if (c == KEY_UP) { history_browse(-1); dirty = 1; return; }
|
||||
if (c == KEY_DOWN) { history_browse(1); dirty = 1; return; }
|
||||
if (c == KEY_PPAGE) {
|
||||
if (curwin) { curwin->scroll += mainh > 1 ? mainh - 1 : 1; dirty = 1; }
|
||||
if (curwin) {
|
||||
int step = mainh > 1 ? mainh - 1 : 1;
|
||||
if (curwin->scroll < 0) curwin->scroll = 0;
|
||||
if (curwin->scroll > MAXSCROLLROWS - step)
|
||||
curwin->scroll = MAXSCROLLROWS;
|
||||
else
|
||||
curwin->scroll += step;
|
||||
dirty = 1;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (c == KEY_NPAGE) {
|
||||
|
|
@ -1863,13 +1912,15 @@ static size_t ircfmt_bytes_for_vis(const char *s, int vis)
|
|||
static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int width)
|
||||
{
|
||||
if (width < 1) width = 1;
|
||||
if (width > MAXLINE * 2 - 1) width = MAXLINE * 2 - 1;
|
||||
if (*nr >= maxrows) return;
|
||||
|
||||
char *copy = dupstr(s);
|
||||
char *segs[128];
|
||||
/* Tokens plus the single spaces we reinsert never exceed the source line. */
|
||||
char *cur = xmalloc(size_add(strlen(s), 2));
|
||||
size_t clen = 0;
|
||||
int nseg = 0;
|
||||
char cur[MAXLINE * 2];
|
||||
int clen = 0;
|
||||
int curvis = 0;
|
||||
cur[0] = 0;
|
||||
|
||||
|
|
@ -1882,11 +1933,11 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w
|
|||
if (nseg < 128) segs[nseg++] = dupnstr(tok, b);
|
||||
tok += b; tlvis -= width; tlbytes -= b;
|
||||
}
|
||||
memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = (int)tlbytes; curvis = tlvis;
|
||||
memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = tlbytes; curvis = tlvis;
|
||||
} else if (curvis + 1 + tlvis <= width) {
|
||||
cur[clen++] = ' ';
|
||||
memcpy(cur + clen, tok, tlbytes);
|
||||
clen += (int)tlbytes;
|
||||
clen += tlbytes;
|
||||
cur[clen] = 0;
|
||||
curvis += 1 + tlvis;
|
||||
} else {
|
||||
|
|
@ -1896,7 +1947,7 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w
|
|||
if (nseg < 128) segs[nseg++] = dupnstr(tok, b);
|
||||
tok += b; tlvis -= width; tlbytes -= b;
|
||||
}
|
||||
memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = (int)tlbytes; curvis = tlvis;
|
||||
memcpy(cur, tok, tlbytes); cur[tlbytes] = 0; clen = tlbytes; curvis = tlvis;
|
||||
}
|
||||
}
|
||||
if (clen || nseg == 0) {
|
||||
|
|
@ -1906,6 +1957,7 @@ static void push_wrapped(char **rows, int *nr, int maxrows, const char *s, int w
|
|||
if (*nr < maxrows) rows[(*nr)++] = segs[i];
|
||||
else free(segs[i]);
|
||||
}
|
||||
free(cur);
|
||||
free(copy);
|
||||
}
|
||||
|
||||
|
|
@ -2041,15 +2093,19 @@ static void draw_main(void)
|
|||
|
||||
if (curwin && mainh > 0) {
|
||||
int ww = width - 2;
|
||||
if (ww < 1) ww = 1;
|
||||
int h = mainh;
|
||||
int need = h + curwin->scroll;
|
||||
if (need < 0) need = 0;
|
||||
int cap = need + curwin->nlines * 8 + 64;
|
||||
char **rows = xmalloc((size_t)cap * sizeof *rows);
|
||||
long long need_ll = (long long)h + curwin->scroll;
|
||||
if (need_ll < 0) need_ll = 0;
|
||||
size_t need = (size_t)need_ll;
|
||||
size_t cap = size_add(need,
|
||||
size_add(size_mul((size_t)curwin->nlines, 8), 64));
|
||||
char **rows = xalloc_array(cap, sizeof *rows);
|
||||
int nr = 0;
|
||||
for (int i = curwin->nlines - 1; i >= 0 && nr < need; i--)
|
||||
push_wrapped(rows, &nr, need, curwin->lines[i], ww);
|
||||
if (nr < need) {
|
||||
int maxrows = need > INT_MAX ? INT_MAX : (int)need;
|
||||
for (int i = curwin->nlines - 1; i >= 0 && nr < maxrows; i--)
|
||||
push_wrapped(rows, &nr, maxrows, curwin->lines[i], ww);
|
||||
if ((size_t)nr < need) {
|
||||
int maxscroll = nr - h;
|
||||
if (maxscroll < 0) maxscroll = 0;
|
||||
if (curwin->scroll > maxscroll) curwin->scroll = maxscroll;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue