╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2168 const add = try r.takeInt(u32, .big); 2169 ch.remote_window +|= add; 2170 } 2171 2172 // charge a payload's bytes against the receive window without keeping them. 2173 // the caller credits the window back, choosing the budget that suits it. 2174 fn discardChannelData(ch: *Channel, packet: []const u8, extended: bool) !void { 2175 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 2176 // uint32 recipient_channel 2177 // [if extended: uint32 type_code] 2178 // string data 2179 const payload = try parseChannelData(packet, extended, ch.local_id); 2180 const data_len: u32 = @intCast(payload.len); 2181 2182 if (data_len > ch.local_window) return error.WindowExceeded; 2183 ch.local_window -= data_len; 2184 } 2185 2186 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 2187 if (packet.len < 1) return error.EmptyPacket; 2188 var r = std.Io.Reader.fixed(packet[1..]); 2189 const recipient_channel = try r.takeInt(u32, .big); 2190 if (recipient_channel != expected_channel) return error.UnknownChannel; 2191 if (extended) { 2192 const data_type = try r.takeInt(u32, .big); 2193 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 2194 } 2195 const data_len = try r.takeInt(u32, .big); 2196 // catches both data_len > remaining (would otherwise be a bounds-check 2197 // panic on the slice) and trailing junk after the declared payload. 2198 if (data_len != r.buffered().len) return error.InvalidChannelData; 2199 return r.buffered(); 2200 } 2201 2202 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 2203 var r = std.Io.Reader.fixed(packet[1..]); 2204 const recipient_channel = try r.takeInt(u32, .big); 2205 if (recipient_channel != expected_channel) return error.UnknownChannel; 2206 if (r.buffered().len != 0) return error.InvalidChannelMessage; 2207 } 2208 2209 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 2210 /// of `budget`, the credit we're willing to have outstanding. used both for 2211 /// the pre-session discard path and by SessionCtx after it consumes a 2212 /// CHANNEL_DATA payload. 2213 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 2214 if (ch.local_window >= budget / 2) return; 2215 const add = budget - ch.local_window; 2216 ch.local_window += add; 2217 var packet: [9]u8 = undefined; 2218 packet[0] = SSH_MSG_CHANNEL_WINDOW_ADJUST; 2219 std.mem.writeInt(u32, packet[1..5], ch.remote_id, .big); 2220 std.mem.writeInt(u32, packet[5..9], add, .big); 2221 try conn.writePacket(&.{&packet}); 2222 } 2223 2224 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2225 var bytes: [4]u8 = undefined; 2226 std.mem.writeInt(u32, &bytes, value, .big); 2227 try buf.appendSlice(allocator, &bytes); 2228 } 2229 2230 test "pty dimensions have bounded axes, preserving normal and zero sizes" { 2231 for ([_]struct { width: u32, height: u32, expected: PtySize }{ 2232 .{ .width = 80, .height = 24, .expected = .{ .width_cells = 80, .height_cells = 24 } }, 2233 .{ .width = 0, .height = 0, .expected = .{ .width_cells = 0, .height_cells = 0 } }, 2234 .{ .width = 0xffffffff, .height = 0xffffffff, .expected = .{ .width_cells = 512, .height_cells = 128 } }, 2235 }) |case| { 2236 var bytes: [8]u8 = undefined; 2237 std.mem.writeInt(u32, bytes[0..4], case.width, .big); 2238 std.mem.writeInt(u32, bytes[4..8], case.height, .big); 2239 var reader = std.Io.Reader.fixed(&bytes); 2240 try std.testing.expectEqual(case.expected, try takePtySize(&reader)); 2241 } 2242 } 2243 2244 test "channel close before shell or exec is acknowledged" { 2245 const io = std.testing.io; 2246 const allocator = std.testing.allocator; 2247 const key: [64]u8 = @splat(0x5a); 2248 var encoded: [256]u8 = undefined; 2249 var encoded_writer = std.Io.Writer.fixed(&encoded); 2250 var sender = Cipher.init(&key, 0); 2251 var open: std.ArrayList(u8) = .empty; 2252 defer open.deinit(allocator); 2253 try open.append(allocator, SSH_MSG_CHANNEL_OPEN); 2254 try writeStringField(&open, allocator, "session"); 2255 try writeU32(&open, allocator, 7); 2256 try writeU32(&open, allocator, 32768); 2257 try writeU32(&open, allocator, 32768); 2258 try sender.writePacket(io, &encoded_writer, &.{open.items}); 2259 try sender.writePacket(io, &encoded_writer, &.{&.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 0 }}); 2260 var reader = std.Io.Reader.fixed(encoded_writer.buffered()); 2261 var output: [256]u8 = undefined; 2262 var writer = std.Io.Writer.fixed(&output); 2263 var idle = IdleState{}; 2264 var conn = Conn{ 2265 .io = io, 2266 .allocator = allocator, 2267 .reader = &reader, 2268 .writer = &writer, 2269 .cs_cipher = Cipher.init(&key, 0), 2270 .sc_cipher = Cipher.init(&key, 0), 2271 .rekey = undefined, 2272 .idle = &idle, 2273 }; 2274 const Handler = struct { 2275 pub fn handleSession(_: *@This(), _: *SessionCtx, _: Request) !void { 2276 return error.UnexpectedSession; 2277 } 2278 }; 2279 var handler = Handler{}; 2280 try runChannelLayer(&conn, &@as([fingerprint_len]u8, @splat(0)), &handler); 2281 var output_reader = std.Io.Reader.fixed(writer.buffered()); 2282 var receiver = Cipher.init(&key, 0); 2283 const confirmation = try receiver.readPacket(allocator, &output_reader); 2284 defer allocator.free(confirmation); 2285 try std.testing.expectEqual(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, confirmation[0]); 2286 const close = try receiver.readPacket(allocator, &output_reader); 2287 defer allocator.free(close); 2288 try std.testing.expectEqualSlices(u8, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 7 }, close); 2289 try std.testing.expectEqual(0, output_reader.buffered().len); 2290 } 2291