|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <bpfcore/vmlinux.h> |
| 4 | +#include <bpfcore/bpf_endian.h> |
| 5 | +#include <bpfcore/bpf_helpers.h> |
| 6 | +#include <bpfcore/utils.h> |
| 7 | + |
| 8 | +#include <common/common.h> |
| 9 | +#include <common/connection_info.h> |
| 10 | +#include <common/http_types.h> |
| 11 | +#include <common/pin_internal.h> |
| 12 | +#include <common/ringbuf.h> |
| 13 | +#include <common/runtime.h> |
| 14 | +#include <common/scratch_mem.h> |
| 15 | +#include <common/sql.h> |
| 16 | +#include <common/tp_info.h> |
| 17 | +#include <common/trace_common.h> |
| 18 | + |
| 19 | +#include <generictracer/protocol_common.h> |
| 20 | +#include <generictracer/k_tracer_tailcall.h> |
| 21 | + |
| 22 | +#include <generictracer/maps/protocol_cache.h> |
| 23 | + |
| 24 | +#include <maps/active_ssl_connections.h> |
| 25 | + |
| 26 | +struct postgres_hdr { |
| 27 | + u32 message_len; |
| 28 | + u8 message_type; |
| 29 | + u8 _pad[3]; |
| 30 | +}; |
| 31 | + |
| 32 | +enum { |
| 33 | + // Postgres header |
| 34 | + k_pg_hdr_size = 5, |
| 35 | + k_pg_messages_in_packet_max = 10, |
| 36 | + |
| 37 | + // Postgres frontend message types |
| 38 | + k_pg_msg_bind = 'B', // Bind a named portal to a prepared statement |
| 39 | + k_pg_msg_execute = 'E', // Execute a portal |
| 40 | + k_pg_msg_parse = 'P', // Parses a query and creates a prepared statement |
| 41 | + k_pg_msg_query = 'Q', // Executes a simple SQL query |
| 42 | + |
| 43 | + // Large buffer |
| 44 | + k_pg_large_buf_max_size = 1 << 14, // 16K |
| 45 | + k_pg_large_buf_max_size_mask = k_pg_large_buf_max_size - 1, |
| 46 | +}; |
| 47 | + |
| 48 | +SCRATCH_MEM_SIZED(postgres_large_buffers, k_pg_large_buf_max_size); |
| 49 | + |
| 50 | +// Emit a large buffer event for Postgres protocol. |
| 51 | +// The return value is used to control the flow for this specific protocol. |
| 52 | +// -1: wait additional data; 0: continue, regardless of errors. |
| 53 | +static __always_inline int postgres_send_large_buffer(tcp_req_t *req, |
| 54 | + pid_connection_info_t *pid_conn, |
| 55 | + const void *u_buf, |
| 56 | + u32 bytes_len, |
| 57 | + u8 direction, |
| 58 | + enum large_buf_action action) { |
| 59 | + if (!is_pow2(postgres_buffer_size)) { |
| 60 | + bpf_dbg_printk("postgres_send_large_buffer: bug: postgres_buffer_size is not a power of 2"); |
| 61 | + return -1; |
| 62 | + } |
| 63 | + const u8 buf_len_mask = postgres_buffer_size - 1; |
| 64 | + |
| 65 | + tcp_large_buffer_t *large_buf = (tcp_large_buffer_t *)postgres_large_buffers_mem(); |
| 66 | + if (!large_buf) { |
| 67 | + bpf_dbg_printk( |
| 68 | + "postgres_send_large_buffer: failed to reserve space for Postgres large buffer"); |
| 69 | + return 0; |
| 70 | + } |
| 71 | + |
| 72 | + large_buf->type = EVENT_TCP_LARGE_BUFFER; |
| 73 | + large_buf->direction = direction; |
| 74 | + large_buf->action = action; |
| 75 | + __builtin_memcpy((void *)&large_buf->tp, (void *)&req->tp, sizeof(tp_info_t)); |
| 76 | + |
| 77 | + large_buf->len = bytes_len; |
| 78 | + if (large_buf->len >= postgres_buffer_size) { |
| 79 | + large_buf->len = postgres_buffer_size; |
| 80 | + bpf_dbg_printk("WARN: postgres_send_large_buffer: buffer is full, truncating data"); |
| 81 | + } |
| 82 | + bpf_probe_read(large_buf->buf, large_buf->len & buf_len_mask, u_buf); |
| 83 | + |
| 84 | + req->has_large_buffers = true; |
| 85 | + bpf_ringbuf_output(&events, |
| 86 | + large_buf, |
| 87 | + (sizeof(tcp_large_buffer_t) + large_buf->len) & k_pg_large_buf_max_size_mask, |
| 88 | + get_flags()); |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +static __always_inline struct postgres_hdr postgres_parse_hdr(const unsigned char *data) { |
| 93 | + struct postgres_hdr hdr = {}; |
| 94 | + |
| 95 | + u8 header[k_pg_hdr_size] = {}; |
| 96 | + bpf_probe_read(header, k_pg_hdr_size, data); |
| 97 | + |
| 98 | + u32 message_len_le; |
| 99 | + __builtin_memcpy(&message_len_le, header + 1, sizeof(message_len_le)); |
| 100 | + |
| 101 | + hdr.message_type = header[0]; |
| 102 | + hdr.message_len = bpf_ntohl(message_len_le); |
| 103 | + |
| 104 | + return hdr; |
| 105 | +} |
| 106 | + |
| 107 | +static __always_inline u8 is_postgres(connection_info_t *conn_info, |
| 108 | + const unsigned char *data, |
| 109 | + u32 data_len, |
| 110 | + enum protocol_type *protocol_type) { |
| 111 | + if (*protocol_type != k_protocol_type_postgres && *protocol_type != k_protocol_type_unknown) { |
| 112 | + // Already classified, not postgres. |
| 113 | + return 0; |
| 114 | + } |
| 115 | + |
| 116 | + if (data_len < k_pg_hdr_size) { |
| 117 | + bpf_dbg_printk("is_postgres: data_len is too short: %d", data_len); |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + size_t message_size = 0; |
| 122 | + struct postgres_hdr hdr; |
| 123 | + bool includes_known_command = false; |
| 124 | + |
| 125 | + for (u8 i = 0; i < k_pg_messages_in_packet_max; i++) { |
| 126 | + if (message_size + k_pg_hdr_size > data_len) { |
| 127 | + break; |
| 128 | + } |
| 129 | + |
| 130 | + hdr = postgres_parse_hdr(data + message_size); |
| 131 | + |
| 132 | + message_size += hdr.message_len + 1; |
| 133 | + if (hdr.message_len == 0) { |
| 134 | + break; |
| 135 | + } |
| 136 | + |
| 137 | + switch (hdr.message_type) { |
| 138 | + case k_pg_msg_query: |
| 139 | + case k_pg_msg_parse: |
| 140 | + case k_pg_msg_bind: |
| 141 | + case k_pg_msg_execute: |
| 142 | + includes_known_command = true; |
| 143 | + break; |
| 144 | + default: |
| 145 | + bpf_dbg_printk("postgres_detect: unhandled message type 0x%x", hdr.message_type); |
| 146 | + return 0; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if (message_size != data_len) { |
| 151 | + bpf_dbg_printk("is_postgres: message length mismatch: message_size=%d data_len=%u", |
| 152 | + message_size, |
| 153 | + data_len); |
| 154 | + return 0; |
| 155 | + } |
| 156 | + |
| 157 | + if (!includes_known_command) { |
| 158 | + bpf_dbg_printk("is_postgres: no known command found"); |
| 159 | + return 0; |
| 160 | + } |
| 161 | + |
| 162 | + *protocol_type = k_protocol_type_postgres; |
| 163 | + bpf_map_update_elem(&protocol_cache, conn_info, protocol_type, BPF_ANY); |
| 164 | + |
| 165 | + bpf_dbg_printk("is_postgres: postgres! message_type=%u", hdr.message_type); |
| 166 | + return 1; |
| 167 | +} |
0 commit comments