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