summaryrefslogtreecommitdiffstats
path: root/usr.bin/tmux/file.c
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2019-12-16 15:48:50 +0000
committernicm <nicm@openbsd.org>2019-12-16 15:48:50 +0000
commitdab96b187283a4fc23ca5a7ac536e094d6d54d49 (patch)
treebbed2c2f3d2c359c2b4df0141ea1acc7487eff5a /usr.bin/tmux/file.c
parentstrdup may return NULL if memory allocation fails. Use the safer xstrdup (diff)
downloadwireguard-openbsd-dab96b187283a4fc23ca5a7ac536e094d6d54d49.tar.xz
wireguard-openbsd-dab96b187283a4fc23ca5a7ac536e094d6d54d49.zip
Instead of using large buffers in imsgs, add the data or path onto the end.
Diffstat (limited to 'usr.bin/tmux/file.c')
-rw-r--r--usr.bin/tmux/file.c63
1 files changed, 38 insertions, 25 deletions
diff --git a/usr.bin/tmux/file.c b/usr.bin/tmux/file.c
index 7a32b24acdd..c7d68fecf7b 100644
--- a/usr.bin/tmux/file.c
+++ b/usr.bin/tmux/file.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: file.c,v 1.2 2019/12/12 11:51:32 nicm Exp $ */
+/* $OpenBSD: file.c,v 1.3 2019/12/16 15:48:50 nicm Exp $ */
/*
* Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -17,9 +17,11 @@
*/
#include <sys/types.h>
+#include <sys/queue.h>
#include <errno.h>
#include <fcntl.h>
+#include <imsg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -147,7 +149,6 @@ file_vprint(struct client *c, const char *fmt, va_list ap)
msg.stream = 1;
msg.fd = STDOUT_FILENO;
msg.flags = 0;
- strlcpy(msg.path, "-", sizeof msg.path);
proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg);
} else {
evbuffer_add_vprintf(cf->buffer, fmt, ap);
@@ -174,7 +175,6 @@ file_print_buffer(struct client *c, void *data, size_t size)
msg.stream = 1;
msg.fd = STDOUT_FILENO;
msg.flags = 0;
- strlcpy(msg.path, "-", sizeof msg.path);
proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg);
} else {
evbuffer_add(cf->buffer, data, size);
@@ -204,7 +204,6 @@ file_error(struct client *c, const char *fmt, ...)
msg.stream = 2;
msg.fd = STDERR_FILENO;
msg.flags = 0;
- strlcpy(msg.path, "-", sizeof msg.path);
proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg);
} else {
evbuffer_add_vprintf(cf->buffer, fmt, ap);
@@ -220,7 +219,8 @@ file_write(struct client *c, const char *path, int flags, const void *bdata,
{
struct client_file *cf;
FILE *f;
- struct msg_write_open msg;
+ struct msg_write_open *msg;
+ size_t msglen;
int fd = -1;
const char *mode;
@@ -261,17 +261,22 @@ file_write(struct client *c, const char *path, int flags, const void *bdata,
skip:
evbuffer_add(cf->buffer, bdata, bsize);
- msg.stream = cf->stream;
- msg.fd = fd;
- msg.flags = flags;
- if (strlcpy(msg.path, cf->path, sizeof msg.path) >= sizeof msg.path) {
+ msglen = strlen(cf->path) + 1 + sizeof *msg;
+ if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
cf->error = E2BIG;
goto done;
}
- if (proc_send(c->peer, MSG_WRITE_OPEN, -1, &msg, sizeof msg) != 0) {
+ msg = xmalloc(msglen);
+ msg->stream = cf->stream;
+ msg->fd = fd;
+ msg->flags = flags;
+ memcpy(msg + 1, cf->path, msglen - sizeof *msg);
+ if (proc_send(c->peer, MSG_WRITE_OPEN, -1, msg, msglen) != 0) {
+ free(msg);
cf->error = EINVAL;
goto done;
}
+ free(msg);
return;
done:
@@ -283,10 +288,10 @@ file_read(struct client *c, const char *path, client_file_cb cb, void *cbdata)
{
struct client_file *cf;
FILE *f;
- struct msg_read_open msg;
+ struct msg_read_open *msg;
+ size_t msglen, size;
int fd = -1;
char buffer[BUFSIZ];
- size_t size;
if (strcmp(path, "-") == 0) {
cf = file_create(c, file_next_stream++, cb, cbdata);
@@ -327,16 +332,21 @@ file_read(struct client *c, const char *path, client_file_cb cb, void *cbdata)
}
skip:
- msg.stream = cf->stream;
- msg.fd = fd;
- if (strlcpy(msg.path, cf->path, sizeof msg.path) >= sizeof msg.path) {
+ msglen = strlen(cf->path) + 1 + sizeof *msg;
+ if (msglen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) {
cf->error = E2BIG;
goto done;
}
- if (proc_send(c->peer, MSG_READ_OPEN, -1, &msg, sizeof msg) != 0) {
+ msg = xmalloc(msglen);
+ msg->stream = cf->stream;
+ msg->fd = fd;
+ memcpy(msg + 1, cf->path, msglen - sizeof *msg);
+ if (proc_send(c->peer, MSG_READ_OPEN, -1, msg, msglen) != 0) {
+ free(msg);
cf->error = EINVAL;
goto done;
}
+ free(msg);
return;
done:
@@ -358,20 +368,22 @@ void
file_push(struct client_file *cf)
{
struct client *c = cf->c;
- struct msg_write_data msg;
+ struct msg_write_data *msg;
+ size_t msglen, sent, left;
struct msg_write_close close;
- size_t sent, left;
+ msg = xmalloc(sizeof *msg);
left = EVBUFFER_LENGTH(cf->buffer);
while (left != 0) {
sent = left;
- if (sent > sizeof msg.data)
- sent = sizeof msg.data;
- memcpy(msg.data, EVBUFFER_DATA(cf->buffer), sent);
- msg.size = sent;
-
- msg.stream = cf->stream;
- if (proc_send(c->peer, MSG_WRITE, -1, &msg, sizeof msg) != 0)
+ if (sent > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
+ sent = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
+
+ msglen = (sizeof *msg) + sent;
+ msg = xrealloc(msg, msglen);
+ msg->stream = cf->stream;
+ memcpy(msg + 1, EVBUFFER_DATA(cf->buffer), sent);
+ if (proc_send(c->peer, MSG_WRITE, -1, msg, msglen) != 0)
break;
evbuffer_drain(cf->buffer, sent);
@@ -387,4 +399,5 @@ file_push(struct client_file *cf)
proc_send(c->peer, MSG_WRITE_CLOSE, -1, &close, sizeof close);
file_fire_done(cf);
}
+ free(msg);
}