Public: anyone can read this; the crew can change it.
An SSH peer cannot make the forge allocate without bound #14979
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14956-an-ssh-peer-cannot-make-the-forge-alloca → main 2 commits fork 1 thread · 1 unresolved
2 files
changed +157
−12
src/ssh/channels.ts +49 −11
| @@ -33,6 +33,27 @@ const MAX_CHANNELS = 8; | ||
| 33 | 33 | const MAX_ENV_VARS = 32; |
| 34 | 34 | const MAX_ENV_VALUE = 1024; |
| 35 | 35 | const UINT32_MAX = 0xffffffff; |
| 36 | /** | |
| 37 | * The largest terminal a peer may claim in `pty-req` or `window-change`. The | |
| 38 | * sizes arrive as u32s and the TUI allocates a cell grid from them, so an | |
| 39 | * unclamped 2^32 x 2^32 is an allocation the peer chooses. 2,000 x 1,000 is | |
| 40 | * far past any real screen (a 500 x 200 terminal is ordinary). | |
| 41 | */ | |
| 42 | export const MAX_TERM_COLS = 2000; | |
| 43 | export const MAX_TERM_ROWS = 1000; | |
| 44 | /** Pixel geometry is only a ratio for terminal images; 16 bits is plenty. */ | |
| 45 | export const MAX_TERM_PX = 0xffff; | |
| 46 | /** | |
| 47 | * Bytes `write` may hold for a peer whose window is shut. A producer that | |
| 48 | * awaits `drain()` never comes close; one that does not, behind a peer that | |
| 49 | * never adjusts its window, would grow the queue without bound. Past this the | |
| 50 | * channel is closed: a peer that reads nothing gets nothing more. | |
| 51 | */ | |
| 52 | export const MAX_QUEUED_BYTES = 8 * 1024 * 1024; | |
| 53 | ||
| 54 | function clamp(n: number, max: number): number { | |
| 55 | return n > max ? max : n; | |
| 56 | } | |
| 36 | 57 | |
| 37 | 58 | export interface SshSession { |
| 38 | 59 | readonly key: SshPublicKey; |
| @@ -112,6 +133,8 @@ class Channel implements SshSession { | ||
| 112 | 133 | |
| 113 | 134 | private started = false; |
| 114 | 135 | private readonly queue: Outgoing[] = []; |
| 136 | /** Bytes in `queue` not yet sent: `pending`, kept as a running count. */ | |
| 137 | private queued = 0; | |
| 115 | 138 | private exitCode: number | undefined; |
| 116 | 139 | private sentClose = false; |
| 117 | 140 | private closeFired = false; |
| @@ -171,9 +194,7 @@ class Channel implements SshSession { | ||
| 171 | 194 | } |
| 172 | 195 | |
| 173 | 196 | get pending(): number { |
| 174 | let total = 0; | |
| 175 | for (const item of this.queue) total += item.data.length - item.offset; | |
| 176 | return total; | |
| 197 | return this.queued; | |
| 177 | 198 | } |
| 178 | 199 | |
| 179 | 200 | drain(): Promise<void> { |
| @@ -191,7 +212,23 @@ class Channel implements SshSession { | ||
| 191 | 212 | const bytes = typeof data === "string" ? utf8(data) : data; |
| 192 | 213 | if (bytes.length === 0) return; |
| 193 | 214 | this.queue.push({ data: bytes, offset: 0, stderr }); |
| 215 | this.queued += bytes.length; | |
| 194 | 216 | this.flush(); |
| 217 | // The cap bounds what is held for a peer that is not reading, not the | |
| 218 | // size of one write: what the window let out does not count. | |
| 219 | if (this.queued > MAX_QUEUED_BYTES) this.overflow(); | |
| 220 | } | |
| 221 | ||
| 222 | /** The peer stopped reading and the queue hit its cap: drop what is queued, | |
| 223 | * close the channel, and tell the application so its producer stops. */ | |
| 224 | private overflow(): void { | |
| 225 | this.host.log.warn("ssh channel closed: peer is not reading", { | |
| 226 | queued: this.queued, | |
| 227 | }); | |
| 228 | this.queue.length = 0; | |
| 229 | this.queued = 0; | |
| 230 | this.closeNow(); | |
| 231 | this.fireClose(); | |
| 195 | 232 | } |
| 196 | 233 | |
| 197 | 234 | exit(code: number): void { |
| @@ -223,6 +260,7 @@ class Channel implements SshSession { | ||
| 223 | 260 | } |
| 224 | 261 | this.host.send(w.string(slice).build()); |
| 225 | 262 | head.offset += n; |
| 263 | this.queued -= n; | |
| 226 | 264 | this.remoteWindow -= n; |
| 227 | 265 | if (head.offset >= head.data.length) this.queue.shift(); |
| 228 | 266 | } |
| @@ -336,10 +374,10 @@ class Channel implements SshSession { | ||
| 336 | 374 | switch (type) { |
| 337 | 375 | case "pty-req": { |
| 338 | 376 | this.term = r.text(); |
| 339 | this.cols = r.u32(); | |
| 340 | this.rows = r.u32(); | |
| 341 | this.widthPx = r.u32(); | |
| 342 | this.heightPx = r.u32(); | |
| 377 | this.cols = clamp(r.u32(), MAX_TERM_COLS); | |
| 378 | this.rows = clamp(r.u32(), MAX_TERM_ROWS); | |
| 379 | this.widthPx = clamp(r.u32(), MAX_TERM_PX); | |
| 380 | this.heightPx = clamp(r.u32(), MAX_TERM_PX); | |
| 343 | 381 | // Terminal modes are parsed only to prove the packet is well formed; |
| 344 | 382 | // we allocate no pty, so there is no termios to apply. |
| 345 | 383 | r.string(); |
| @@ -367,10 +405,10 @@ class Channel implements SshSession { | ||
| 367 | 405 | return true; |
| 368 | 406 | } |
| 369 | 407 | case "window-change": { |
| 370 | this.cols = r.u32(); | |
| 371 | this.rows = r.u32(); | |
| 372 | this.widthPx = r.u32(); | |
| 373 | this.heightPx = r.u32(); | |
| 408 | this.cols = clamp(r.u32(), MAX_TERM_COLS); | |
| 409 | this.rows = clamp(r.u32(), MAX_TERM_ROWS); | |
| 410 | this.widthPx = clamp(r.u32(), MAX_TERM_PX); | |
| 411 | this.heightPx = clamp(r.u32(), MAX_TERM_PX); | |
| 374 | 412 | const cb = this.resizeCb; |
| 375 | 413 | if (cb) { |
| 376 | 414 | try { |
test/ssh-channel-limits.test.ts +108 −1
| @@ -8,7 +8,14 @@ | ||
| 8 | 8 | |
| 9 | 9 | import { describe, expect, test } from "bun:test"; |
| 10 | 10 | import { createLog } from "../src/kernel/log.ts"; |
| 11 | import { Channels, type SshSession } from "../src/ssh/channels.ts"; | |
| 11 | import { | |
| 12 | Channels, | |
| 13 | MAX_QUEUED_BYTES, | |
| 14 | MAX_TERM_COLS, | |
| 15 | MAX_TERM_PX, | |
| 16 | MAX_TERM_ROWS, | |
| 17 | type SshSession, | |
| 18 | } from "../src/ssh/channels.ts"; | |
| 12 | 19 | import { |
| 13 | 20 | DISCONNECT, |
| 14 | 21 | MSG, |
| @@ -206,4 +213,104 @@ describe("ssh channel limits", () => { | ||
| 206 | 213 | }, |
| 207 | 214 | ]); |
| 208 | 215 | }); |
| 216 | ||
| 217 | function size(cols: number, rows: number, w: number, h: number) { | |
| 218 | return new Writer(32).u32(cols).u32(rows).u32(w).u32(h).build(); | |
| 219 | } | |
| 220 | ||
| 221 | function ptyReq(channel: number, cols: number, rows: number, w = 0, h = 0) { | |
| 222 | return request( | |
| 223 | channel, | |
| 224 | "pty-req", | |
| 225 | new Writer(64) | |
| 226 | .string("xterm-256color") | |
| 227 | .raw(size(cols, rows, w, h)) | |
| 228 | .string(new Uint8Array([0])) | |
| 229 | .build(), | |
| 230 | ); | |
| 231 | } | |
| 232 | ||
| 233 | test("pty-req and window-change sizes are clamped to a named maximum", () => { | |
| 234 | const h = harness(); | |
| 235 | h.handle(open(1)); | |
| 236 | h.handle(ptyReq(1, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX)); | |
| 237 | h.handle(request(1, "shell")); | |
| 238 | const session = h.sessions[0]!; | |
| 239 | expect([session.cols, session.rows, session.widthPx, session.heightPx]) | |
| 240 | .toEqual([MAX_TERM_COLS, MAX_TERM_ROWS, MAX_TERM_PX, MAX_TERM_PX]); | |
| 241 | ||
| 242 | const resized: [number, number][] = []; | |
| 243 | session.onResize((c, r) => resized.push([c, r])); | |
| 244 | h.handle(request(1, "window-change", size(UINT32_MAX, 1 << 30, 1 << 20, 7))); | |
| 245 | expect(resized).toEqual([[MAX_TERM_COLS, MAX_TERM_ROWS]]); | |
| 246 | expect([session.widthPx, session.heightPx]).toEqual([MAX_TERM_PX, 7]); | |
| 247 | }); | |
| 248 | ||
| 249 | test("an ordinary large terminal, 500 x 200, keeps its size", () => { | |
| 250 | const h = harness(); | |
| 251 | h.handle(open(1)); | |
| 252 | h.handle(ptyReq(1, 500, 200, 4000, 3200)); | |
| 253 | h.handle(request(1, "shell")); | |
| 254 | const session = h.sessions[0]!; | |
| 255 | expect([session.cols, session.rows, session.widthPx, session.heightPx]) | |
| 256 | .toEqual([500, 200, 4000, 3200]); | |
| 257 | h.handle(request(1, "window-change", size(480, 190, 0, 0))); | |
| 258 | expect([session.cols, session.rows]).toEqual([480, 190]); | |
| 259 | }); | |
| 260 | ||
| 261 | test("output queued past the byte cap closes the channel", () => { | |
| 262 | const h = harness(); | |
| 263 | // A window of zero: the peer never lets a byte out. | |
| 264 | h.handle(open(1, 32768, 0)); | |
| 265 | h.handle(request(1, "shell")); | |
| 266 | const session = h.sessions[0]!; | |
| 267 | let closed = 0; | |
| 268 | session.onClose(() => closed++); | |
| 269 | ||
| 270 | const chunk = new Uint8Array(1024 * 1024); | |
| 271 | for (let i = 0; i < MAX_QUEUED_BYTES / chunk.length; i++) { | |
| 272 | session.write(chunk); | |
| 273 | } | |
| 274 | expect(session.pending).toBe(MAX_QUEUED_BYTES); | |
| 275 | expect(closed).toBe(0); | |
| 276 | ||
| 277 | session.write(new Uint8Array(1)); | |
| 278 | expect(closed).toBe(1); | |
| 279 | expect(session.ended).toBe(true); | |
| 280 | expect(session.pending).toBe(0); | |
| 281 | expect(h.last()).toEqual( | |
| 282 | new Writer(8).u8(MSG.CHANNEL_CLOSE).u32(1).build(), | |
| 283 | ); | |
| 284 | ||
| 285 | // Nothing more queues, and the peer's CLOSE reply is quiet. | |
| 286 | session.write(chunk); | |
| 287 | expect(session.pending).toBe(0); | |
| 288 | h.handle(new Writer(8).u8(MSG.CHANNEL_CLOSE).u32(1).build()); | |
| 289 | expect(closed).toBe(1); | |
| 290 | expect(h.disconnects).toEqual([]); | |
| 291 | }); | |
| 292 | ||
| 293 | test("one write past the cap goes out whole to a peer whose window is open", () => { | |
| 294 | const h = harness(); | |
| 295 | const big = 9 * 1024 * 1024; | |
| 296 | h.handle(open(1, 32768, 16 * 1024 * 1024)); | |
| 297 | h.handle(request(1, "shell")); | |
| 298 | const session = h.sessions[0]!; | |
| 299 | let closed = 0; | |
| 300 | session.onClose(() => closed++); | |
| 301 | const before = h.sent.length; | |
| 302 | ||
| 303 | session.write(new Uint8Array(big)); | |
| 304 | expect(closed).toBe(0); | |
| 305 | expect(session.ended).toBe(false); | |
| 306 | expect(session.pending).toBe(0); | |
| 307 | let sent = 0; | |
| 308 | for (const payload of h.sent.slice(before)) { | |
| 309 | const r = new Reader(payload); | |
| 310 | expect(r.u8()).toBe(MSG.CHANNEL_DATA); | |
| 311 | r.u32(); | |
| 312 | sent += r.string().length; | |
| 313 | } | |
| 314 | expect(sent).toBe(big); | |
| 315 | }); | |
| 209 | 316 | }); |
new above
offline
This check runs before flush(). A single write over 8 MiB to a peer with an open window is closed although nothing would stay queued. Queue the data and flush first, then call overflow() only if
this.queuedstill exceeds MAX_QUEUED_BYTES. Add a test with a large write and an open window.