╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1926 /// and return the new Channel. on other types, send CHANNEL_OPEN_FAILURE and 1927 /// return null. 1928 fn handleChannelOpen(conn: *Conn, packet: []const u8) !?Channel { 1929 const allocator = conn.allocator; 1930 1931 // byte SSH_MSG_CHANNEL_OPEN 1932 // string channel_type 1933 // uint32 sender_channel (peer's id) 1934 // uint32 initial_window 1935 // uint32 max_packet 1936 var r = std.Io.Reader.fixed(packet[1..]); 1937 const ch_type = try takeString(&r, 64); 1938 const remote_id = try r.takeInt(u32, .big); 1939 const initial_window = try r.takeInt(u32, .big); 1940 const max_packet = try r.takeInt(u32, .big); 1941 1942 if (!std.mem.eql(u8, ch_type, "session")) { 1943 try sendChannelOpenFailure(conn, packet, SSH_OPEN_UNKNOWN_CHANNEL_TYPE, "only session channels are supported"); 1944 return null; 1945 } 1946 1947 // a zero max_packet would leave writeChannel looping without progress 1948 if (max_packet == 0) { 1949 try sendChannelOpenFailure(conn, packet, SSH_OPEN_RESOURCE_SHORTAGE, "max packet size must be non-zero"); 1950 return null; 1951 } 1952 1953 const local_id: u32 = 0; // single-channel connection — id is always 0 from our side 1954 var reply: std.ArrayList(u8) = .empty; 1955 defer reply.deinit(allocator); 1956 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1957 try writeU32(&reply, allocator, remote_id); 1958 try writeU32(&reply, allocator, local_id); 1959 try writeU32(&reply, allocator, initial_recv_window); 1960 try writeU32(&reply, allocator, max_packet_size); 1961 try conn.writePacket(reply.items); 1962 1963 return .{ 1964 .local_id = local_id, 1965 .remote_id = remote_id, 1966 .local_window = initial_recv_window, 1967 .remote_window = initial_window, 1968 .max_packet = max_packet, 1969 }; 1970 } 1971 1972 fn sendChannelOpenFailure(conn: *Conn, open_packet: []const u8, reason: u32, description: []const u8) !void { 1973 const allocator = conn.allocator; 1974 1975 // recover the sender_channel field from the CHANNEL_OPEN packet so we 1976 // address the failure to the correct id 1977 var r = std.Io.Reader.fixed(open_packet[1..]); 1978 const ch_type_len = try r.takeInt(u32, .big); 1979 try r.discardAll(ch_type_len); 1980 const remote_id = try r.takeInt(u32, .big); 1981 1982 var reply: std.ArrayList(u8) = .empty; 1983 defer reply.deinit(allocator); 1984 try reply.append(allocator, SSH_MSG_CHANNEL_OPEN_FAILURE); 1985 try writeU32(&reply, allocator, remote_id); 1986 try writeU32(&reply, allocator, reason); 1987 try writeStringField(&reply, allocator, description); 1988 try writeStringField(&reply, allocator, ""); // language tag 1989 try conn.writePacket(reply.items); 1990 } 1991 1992 /// returns the session request if this one triggered it, else null. an exec 1993 /// command borrows from `packet`. 1994 fn handleChannelRequest(conn: *Conn, ch: *Channel, packet: []const u8) !?Request { 1995 // byte SSH_MSG_CHANNEL_REQUEST 1996 // uint32 recipient_channel (== our local_id) 1997 // string request_type 1998 // boolean want_reply 1999 // …request-specific data 2000 var r = std.Io.Reader.fixed(packet[1..]); 2001 const recipient_channel = try r.takeInt(u32, .big); 2002 if (recipient_channel != ch.local_id) return error.UnknownChannel; 2003 const req_type = try takeString(&r, 64); 2004 const want_reply = (try r.takeByte()) != 0; 2005 2006 if (std.mem.eql(u8, req_type, "pty-req")) { 2007 // string TERM, uint32 width chars, uint32 height rows, uint32 width px, 2008 // uint32 height px, string modes 2009 _ = try takeString(&r, 64); 2010 ch.pty = try takePtySize(&r); 2011 try replyChannelRequest(conn, ch, want_reply, true); 2012 return null; 2013 } 2014 2015 if (std.mem.eql(u8, req_type, "env")) { 2016 // string name, string value. the rest are ignored silently. 2017 const name = try takeString(&r, max_packet_len); 2018 const value = try takeString(&r, max_packet_len); 2019 if (std.mem.eql(u8, name, "GIT_PROTOCOL") and value.len <= ch.git_protocol_buf.len) { 2020 @memcpy(ch.git_protocol_buf[0..value.len], value); 2021 ch.git_protocol_len = value.len; 2022 } 2023 try replyChannelRequest(conn, ch, want_reply, true); 2024 return null; 2025 } 2026 2027 if (std.mem.eql(u8, req_type, "window-change")) { 2028 // uint32 width cells, uint32 height rows, uint32 width px, uint32 height px 2029 const sz = try takePtySize(&r); 2030 if (ch.pty != null) ch.pty = sz; 2031 // spec: window-change MUST NOT request a reply, but accept either 2032 try replyChannelRequest(conn, ch, want_reply, true); 2033 return null; 2034 } 2035 2036 if (std.mem.eql(u8, req_type, "shell")) { 2037 try replyChannelRequest(conn, ch, want_reply, true); 2038 return .{ .shell = ch.pty }; 2039 } 2040 2041 if (std.mem.eql(u8, req_type, "exec")) { 2042 // capture the command string for the handler to inspect 2043 const cmd = try takeString(&r, 4096); 2044 try replyChannelRequest(conn, ch, want_reply, true); 2045 return .{ .exec = .{ 2046 .command = cmd, 2047 .git_protocol = if (ch.git_protocol_len) |len| ch.git_protocol_buf[0..len] else null, 2048 } }; 2049 } 2050 2051 // unknown request kind — fail it 2052 try replyChannelRequest(conn, ch, want_reply, false); 2053 return null; 2054 } 2055 2056 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 2057 if (!want_reply) return; 2058 try sendChannelMessage(conn, ch, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 2059 } 2060 2061 fn sendChannelMessage(conn: *Conn, ch: *Channel, msg_type: u8) !void { 2062 var packet: [5]u8 = undefined; 2063 packet[0] = msg_type; 2064 std.mem.writeInt(u32, packet[1..5], ch.remote_id, .big); 2065 try conn.writePacket(&packet); 2066 } 2067 2068 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 2069 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 2070 // uint32 recipient_channel 2071 // uint32 bytes_to_add 2072 var r = std.Io.Reader.fixed(packet[1..]); 2073 const recipient_channel = try r.takeInt(u32, .big); 2074 if (recipient_channel != ch.local_id) return error.UnknownChannel; 2075 const add = try r.takeInt(u32, .big); 2076 ch.remote_window +|= add; 2077 } 2078 2079 // charge a payload's bytes against the receive window without keeping them. 2080 // the caller credits the window back, choosing the budget that suits it. 2081 fn discardChannelData(ch: *Channel, packet: []const u8, extended: bool) !void { 2082 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 2083 // uint32 recipient_channel 2084 // [if extended: uint32 type_code] 2085 // string data 2086 const payload = try parseChannelData(packet, extended, ch.local_id); 2087 const data_len: u32 = @intCast(payload.len); 2088 2089 if (data_len > ch.local_window) return error.WindowExceeded; 2090 ch.local_window -= data_len; 2091 } 2092 2093 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 2094 if (packet.len < 1) return error.EmptyPacket; 2095 var r = std.Io.Reader.fixed(packet[1..]); 2096 const recipient_channel = try r.takeInt(u32, .big); 2097 if (recipient_channel != expected_channel) return error.UnknownChannel; 2098 if (extended) { 2099 const data_type = try r.takeInt(u32, .big); 2100 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 2101 } 2102 const data_len = try r.takeInt(u32, .big); 2103 // catches both data_len > remaining (would otherwise be a bounds-check 2104 // panic on the slice) and trailing junk after the declared payload. 2105 if (data_len != r.buffered().len) return error.InvalidChannelData; 2106 return r.buffered(); 2107 } 2108 2109 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 2110 if (packet.len < 1) return error.EmptyPacket; 2111 var r = std.Io.Reader.fixed(packet[1..]); 2112 const recipient_channel = try r.takeInt(u32, .big); 2113 if (recipient_channel != expected_channel) return error.UnknownChannel; 2114 if (r.buffered().len != 0) return error.InvalidChannelMessage; 2115 } 2116 2117 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 2118 /// of `budget`, the credit we're willing to have outstanding. used both for 2119 /// the pre-session discard path and by SessionCtx after it consumes a 2120 /// CHANNEL_DATA payload. 2121 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 2122 if (ch.local_window >= budget / 2) return; 2123 const add = budget - ch.local_window; 2124 ch.local_window += add; 2125 var adj: std.ArrayList(u8) = .empty; 2126 defer adj.deinit(conn.allocator); 2127 try adj.append(conn.allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 2128 try writeU32(&adj, conn.allocator, ch.remote_id); 2129 try writeU32(&adj, conn.allocator, add); 2130 try conn.writePacket(adj.items); 2131 } 2132 2133 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2134 var bytes: [4]u8 = undefined; 2135 std.mem.writeInt(u32, &bytes, value, .big); 2136 try buf.appendSlice(allocator, &bytes); 2137 } 2138 2139 test "pty dimensions have bounded axes, preserving normal and zero sizes" { 2140 for ([_]struct { width: u32, height: u32, expected: PtySize }{ 2141 .{ .width = 80, .height = 24, .expected = .{ .width_cells = 80, .height_cells = 24 } }, 2142 .{ .width = 0, .height = 0, .expected = .{ .width_cells = 0, .height_cells = 0 } }, 2143 .{ .width = 0xffffffff, .height = 0xffffffff, .expected = .{ .width_cells = 512, .height_cells = 128 } }, 2144 }) |case| { 2145 var bytes: [8]u8 = undefined; 2146 std.mem.writeInt(u32, bytes[0..4], case.width, .big); 2147 std.mem.writeInt(u32, bytes[4..8], case.height, .big); 2148 var reader = std.Io.Reader.fixed(&bytes); 2149 try std.testing.expectEqual(case.expected, try takePtySize(&reader)); 2150 } 2151 } 2152 2153 test "channel close before shell or exec is acknowledged" { 2154 const io = std.testing.io; 2155 const allocator = std.testing.allocator; 2156 const key = [_]u8{0x5a} ** 64; 2157 var encoded: [256]u8 = undefined; 2158 var encoded_writer = std.Io.Writer.fixed(&encoded); 2159 var sender = Cipher.init(&key, 0); 2160 var open: std.ArrayList(u8) = .empty; 2161 defer open.deinit(allocator); 2162 try open.append(allocator, SSH_MSG_CHANNEL_OPEN); 2163 try writeStringField(&open, allocator, "session"); 2164 try writeU32(&open, allocator, 7); 2165 try writeU32(&open, allocator, 32768); 2166 try writeU32(&open, allocator, 32768); 2167 try sender.writePacket(io, &encoded_writer, open.items); 2168 try sender.writePacket(io, &encoded_writer, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 0 }); 2169 var reader = std.Io.Reader.fixed(encoded_writer.buffered()); 2170 var output: [256]u8 = undefined; 2171 var writer = std.Io.Writer.fixed(&output); 2172 var conn = Conn{ 2173 .io = io, 2174 .allocator = allocator, 2175 .reader = &reader, 2176 .writer = &writer, 2177 .cs_cipher = Cipher.init(&key, 0), 2178 .sc_cipher = Cipher.init(&key, 0), 2179 .rekey = undefined, 2180 }; 2181 const Handler = struct { 2182 pub fn handleSession(_: *@This(), _: *SessionCtx, _: Request) !void { 2183 return error.UnexpectedSession; 2184 } 2185 }; 2186 var handler = Handler{}; 2187 try runChannelLayer(&conn, &([_]u8{0} ** fingerprint_len), &handler); 2188 var output_reader = std.Io.Reader.fixed(writer.buffered()); 2189 var receiver = Cipher.init(&key, 0); 2190 const confirmation = try receiver.readPacket(allocator, &output_reader); 2191 defer allocator.free(confirmation); 2192 try std.testing.expectEqual(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, confirmation[0]); 2193 const close = try receiver.readPacket(allocator, &output_reader); 2194 defer allocator.free(close); 2195 try std.testing.expectEqualSlices(u8, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 7 }, close); 2196 try std.testing.expectEqual(0, output_reader.buffered().len); 2197 } 2198