Skip to content

Commit 89dd615

Browse files
mfriedldjmdjm
authored andcommitted
upstream: ttymodes: switch to sshbuf API; ok djm@
OpenBSD-Commit-ID: 5df340c5965e822c9da21e19579d08dea3cbe429
1 parent f4608a7 commit 89dd615

File tree

4 files changed

+81
-64
lines changed

4 files changed

+81
-64
lines changed

clientloop.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: clientloop.c,v 1.315 2018/07/09 21:03:30 markus Exp $ */
1+
/* $OpenBSD: clientloop.c,v 1.316 2018/07/09 21:20:26 markus Exp $ */
22
/*
33
* Author: Tatu Ylonen <[email protected]>
44
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
@@ -2210,7 +2210,7 @@ client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
22102210
packet_put_int((u_int)ws.ws_ypixel);
22112211
if (tiop == NULL)
22122212
tiop = get_saved_tio();
2213-
tty_make_modes(-1, tiop);
2213+
ssh_tty_make_modes(ssh, -1, tiop);
22142214
packet_send();
22152215
/* XXX wait for reply */
22162216
c->client_tty = 1;

packet.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: packet.h,v 1.85 2018/07/06 09:03:02 sf Exp $ */
1+
/* $OpenBSD: packet.h,v 1.86 2018/07/09 21:20:26 markus Exp $ */
22

33
/*
44
* Author: Tatu Ylonen <[email protected]>
@@ -147,8 +147,8 @@ int ssh_packet_not_very_much_data_to_write(struct ssh *);
147147
int ssh_packet_connection_is_on_socket(struct ssh *);
148148
int ssh_packet_remaining(struct ssh *);
149149

150-
void tty_make_modes(int, struct termios *);
151-
void tty_parse_modes(int, int *);
150+
void ssh_tty_make_modes(struct ssh *, int, struct termios *);
151+
void ssh_tty_parse_modes(struct ssh *, int);
152152

153153
void ssh_packet_set_alive_timeouts(struct ssh *, int);
154154
int ssh_packet_inc_alive_timeouts(struct ssh *);

session.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: session.c,v 1.301 2018/07/03 10:59:35 djm Exp $ */
1+
/* $OpenBSD: session.c,v 1.302 2018/07/09 21:20:26 markus Exp $ */
22
/*
33
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
44
* All rights reserved
@@ -1912,7 +1912,6 @@ static int
19121912
session_pty_req(struct ssh *ssh, Session *s)
19131913
{
19141914
u_int len;
1915-
int n_bytes;
19161915

19171916
if (!auth_opts->permit_pty_flag || !options.permit_tty) {
19181917
debug("Allocating a pty not permitted for this connection.");
@@ -1947,8 +1946,7 @@ session_pty_req(struct ssh *ssh, Session *s)
19471946
}
19481947
debug("session_pty_req: session %d alloc %s", s->self, s->tty);
19491948

1950-
n_bytes = packet_remaining();
1951-
tty_parse_modes(s->ttyfd, &n_bytes);
1949+
ssh_tty_parse_modes(ssh, s->ttyfd);
19521950

19531951
if (!use_privsep)
19541952
pty_setowner(s->pw, s->tty);

ttymodes.c

+74-55
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: ttymodes.c,v 1.33 2018/02/16 04:43:11 dtucker Exp $ */
1+
/* $OpenBSD: ttymodes.c,v 1.34 2018/07/09 21:20:26 markus Exp $ */
22
/*
33
* Author: Tatu Ylonen <[email protected]>
44
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
@@ -55,8 +55,8 @@
5555
#include "packet.h"
5656
#include "log.h"
5757
#include "compat.h"
58-
#include "buffer.h"
59-
#include "compat.h"
58+
#include "sshbuf.h"
59+
#include "ssherr.h"
6060

6161
#define TTY_OP_END 0
6262
/*
@@ -276,17 +276,18 @@ special_char_decode(u_int c)
276276
* being constructed.
277277
*/
278278
void
279-
tty_make_modes(int fd, struct termios *tiop)
279+
ssh_tty_make_modes(struct ssh *ssh, int fd, struct termios *tiop)
280280
{
281281
struct termios tio;
282-
int baud;
283-
Buffer buf;
282+
struct sshbuf *buf;
283+
int r, ibaud, obaud;
284284

285-
buffer_init(&buf);
285+
if ((buf = sshbuf_new()) == NULL)
286+
fatal("%s: sshbuf_new failed", __func__);
286287

287288
if (tiop == NULL) {
288289
if (fd == -1) {
289-
debug("tty_make_modes: no fd or tio");
290+
debug("%s: no fd or tio", __func__);
290291
goto end;
291292
}
292293
if (tcgetattr(fd, &tio) == -1) {
@@ -297,27 +298,29 @@ tty_make_modes(int fd, struct termios *tiop)
297298
tio = *tiop;
298299

299300
/* Store input and output baud rates. */
300-
baud = speed_to_baud(cfgetospeed(&tio));
301-
buffer_put_char(&buf, TTY_OP_OSPEED);
302-
buffer_put_int(&buf, baud);
303-
baud = speed_to_baud(cfgetispeed(&tio));
304-
buffer_put_char(&buf, TTY_OP_ISPEED);
305-
buffer_put_int(&buf, baud);
301+
obaud = speed_to_baud(cfgetospeed(&tio));
302+
ibaud = speed_to_baud(cfgetispeed(&tio));
303+
if ((r = sshbuf_put_u8(buf, TTY_OP_OSPEED)) != 0 ||
304+
(r = sshbuf_put_u32(buf, obaud)) != 0 ||
305+
(r = sshbuf_put_u8(buf, TTY_OP_ISPEED)) != 0 ||
306+
(r = sshbuf_put_u32(buf, ibaud)) != 0)
307+
fatal("%s: buffer error: %s", __func__, ssh_err(r));
306308

307309
/* Store values of mode flags. */
308310
#define TTYCHAR(NAME, OP) \
309-
buffer_put_char(&buf, OP); \
310-
buffer_put_int(&buf, special_char_encode(tio.c_cc[NAME]));
311+
if ((r = sshbuf_put_u8(buf, OP)) != 0 || \
312+
(r = sshbuf_put_u32(buf, \
313+
special_char_encode(tio.c_cc[NAME]))) != 0) \
314+
fatal("%s: buffer error: %s", __func__, ssh_err(r)); \
311315

312316
#define SSH_TTYMODE_IUTF8 42 /* for SSH_BUG_UTF8TTYMODE */
313317

314318
#define TTYMODE(NAME, FIELD, OP) \
315319
if (OP == SSH_TTYMODE_IUTF8 && (datafellows & SSH_BUG_UTF8TTYMODE)) { \
316320
debug3("%s: SSH_BUG_UTF8TTYMODE", __func__); \
317-
} else { \
318-
buffer_put_char(&buf, OP); \
319-
buffer_put_int(&buf, ((tio.FIELD & NAME) != 0)); \
320-
}
321+
} else if ((r = sshbuf_put_u8(buf, OP)) != 0 || \
322+
(r = sshbuf_put_u32(buf, ((tio.FIELD & NAME) != 0))) != 0) \
323+
fatal("%s: buffer error: %s", __func__, ssh_err(r)); \
321324

322325
#include "ttymodes.h"
323326

@@ -326,26 +329,35 @@ tty_make_modes(int fd, struct termios *tiop)
326329

327330
end:
328331
/* Mark end of mode data. */
329-
buffer_put_char(&buf, TTY_OP_END);
330-
packet_put_string(buffer_ptr(&buf), buffer_len(&buf));
331-
buffer_free(&buf);
332+
if ((r = sshbuf_put_u8(buf, TTY_OP_END)) != 0 ||
333+
(r = sshpkt_put_stringb(ssh, buf)) != 0)
334+
fatal("%s: packet error: %s", __func__, ssh_err(r));
335+
sshbuf_free(buf);
332336
}
333337

334338
/*
335339
* Decodes terminal modes for the terminal referenced by fd in a portable
336340
* manner from a packet being read.
337341
*/
338342
void
339-
tty_parse_modes(int fd, int *n_bytes_ptr)
343+
ssh_tty_parse_modes(struct ssh *ssh, int fd)
340344
{
341345
struct termios tio;
342-
int opcode, baud;
343-
int n_bytes = 0;
344-
int failure = 0;
345-
346-
*n_bytes_ptr = packet_get_int();
347-
if (*n_bytes_ptr == 0)
346+
struct sshbuf *buf;
347+
const u_char *data;
348+
u_char opcode;
349+
u_int baud, u;
350+
int r, failure = 0;
351+
size_t len;
352+
353+
if ((r = sshpkt_get_string_direct(ssh, &data, &len)) != 0)
354+
fatal("%s: packet error: %s", __func__, ssh_err(r));
355+
if (len == 0)
356+
return;
357+
if ((buf = sshbuf_from(data, len)) == NULL) {
358+
error("%s: sshbuf_from failed", __func__);
348359
return;
360+
}
349361

350362
/*
351363
* Get old attributes for the terminal. We will modify these
@@ -357,42 +369,48 @@ tty_parse_modes(int fd, int *n_bytes_ptr)
357369
failure = -1;
358370
}
359371

360-
for (;;) {
361-
n_bytes += 1;
362-
opcode = packet_get_char();
372+
while (sshbuf_len(buf) > 0) {
373+
if ((r = sshbuf_get_u8(buf, &opcode)) != 0)
374+
fatal("%s: packet error: %s", __func__, ssh_err(r));
363375
switch (opcode) {
364376
case TTY_OP_END:
365377
goto set;
366378

367379
case TTY_OP_ISPEED:
368-
n_bytes += 4;
369-
baud = packet_get_int();
380+
if ((r = sshbuf_get_u32(buf, &baud)) != 0)
381+
fatal("%s: packet error: %s",
382+
__func__, ssh_err(r));
370383
if (failure != -1 &&
371384
cfsetispeed(&tio, baud_to_speed(baud)) == -1)
372385
error("cfsetispeed failed for %d", baud);
373386
break;
374387

375388
case TTY_OP_OSPEED:
376-
n_bytes += 4;
377-
baud = packet_get_int();
389+
if ((r = sshbuf_get_u32(buf, &baud)) != 0)
390+
fatal("%s: packet error: %s",
391+
__func__, ssh_err(r));
378392
if (failure != -1 &&
379393
cfsetospeed(&tio, baud_to_speed(baud)) == -1)
380394
error("cfsetospeed failed for %d", baud);
381395
break;
382396

383397
#define TTYCHAR(NAME, OP) \
384-
case OP: \
385-
n_bytes += 4; \
386-
tio.c_cc[NAME] = special_char_decode(packet_get_int()); \
387-
break;
398+
case OP: \
399+
if ((r = sshbuf_get_u32(buf, &u)) != 0) \
400+
fatal("%s: packet error: %s", __func__, \
401+
ssh_err(r)); \
402+
tio.c_cc[NAME] = special_char_decode(u); \
403+
break;
388404
#define TTYMODE(NAME, FIELD, OP) \
389-
case OP: \
390-
n_bytes += 4; \
391-
if (packet_get_int()) \
392-
tio.FIELD |= NAME; \
393-
else \
394-
tio.FIELD &= ~NAME; \
395-
break;
405+
case OP: \
406+
if ((r = sshbuf_get_u32(buf, &u)) != 0) \
407+
fatal("%s: packet error: %s", __func__, \
408+
ssh_err(r)); \
409+
if (u) \
410+
tio.FIELD |= NAME; \
411+
else \
412+
tio.FIELD &= ~NAME; \
413+
break;
396414

397415
#include "ttymodes.h"
398416

@@ -410,22 +428,23 @@ tty_parse_modes(int fd, int *n_bytes_ptr)
410428
* to stop.
411429
*/
412430
if (opcode > 0 && opcode < 160) {
413-
n_bytes += 4;
414-
(void) packet_get_int();
431+
if ((r = sshbuf_get_u32(buf, NULL)) != 0)
432+
fatal("%s: packet error: %s", __func__,
433+
ssh_err(r));
415434
break;
416435
} else {
417-
logit("parse_tty_modes: unknown opcode %d",
436+
logit("%s: unknown opcode %d", __func__,
418437
opcode);
419438
goto set;
420439
}
421440
}
422441
}
423442

424443
set:
425-
if (*n_bytes_ptr != n_bytes) {
426-
*n_bytes_ptr = n_bytes;
427-
logit("parse_tty_modes: n_bytes_ptr != n_bytes: %d %d",
428-
*n_bytes_ptr, n_bytes);
444+
len = sshbuf_len(buf);
445+
sshbuf_free(buf);
446+
if (len > 0) {
447+
logit("%s: %zu bytes left", __func__, len);
429448
return; /* Don't process bytes passed */
430449
}
431450
if (failure == -1)

0 commit comments

Comments
 (0)