Skip to content

Commit c7397dc

Browse files
intermamy-ship-it
authored andcommitted
Add magic number field in the ICProxyPkt (#14926)
icproxy processes communicate with each other with the public addr:port (see gp_interconnect_proxy_addresses), but no auth control here. So, any connection may connect in and send any byte, it may cause some unexpected exceptions (e.g. OOM). In the PR, introduces a magic number field in the ICProxyPkt and does a sanity check: drop the corresponding package if magic number doesn't match. This method does not attempt to solve all problems, but only as a simple solution to avoid internal misaccess, looks it's good enough.
1 parent 7158e52 commit c7397dc

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

contrib/interconnect/proxy/ic_proxy_backend.c

+10
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ ic_proxy_backend_on_read_hello_ack(uv_stream_t *stream, ssize_t nread, const uv_
182182

183183
/* we have received a complete HELLO ACK message */
184184
pkt = (ICProxyPkt *)backend->buffer;
185+
186+
/* sanity check: drop the packet with incorrect magic number */
187+
if (!ic_proxy_pkt_is_valid(pkt))
188+
{
189+
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG, DEBUG1,
190+
"ic-proxy: backend %s: received %s, dropping the invalid package (magic number mismatch)",
191+
ic_proxy_key_to_str(&backend->key), ic_proxy_pkt_to_str(pkt));
192+
return;
193+
}
194+
185195
Assert(ic_proxy_pkt_is(pkt, IC_PROXY_MESSAGE_HELLO_ACK));
186196
uv_read_stop(stream);
187197

contrib/interconnect/proxy/ic_proxy_client.c

+20
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,15 @@ ic_proxy_client_on_hello_pkt(void *opaque, const void *data, uint16 size)
659659
ICProxyKey key;
660660
ICProxyPkt *ackpkt;
661661

662+
/* sanity check: drop the packet with incorrect magic number */
663+
if (!ic_proxy_pkt_is_valid(pkt))
664+
{
665+
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG, DEBUG1,
666+
"ic-proxy: %s: received %s, dropping the invalid package (magic number mismatch)",
667+
ic_proxy_client_get_name(client), ic_proxy_pkt_to_str(pkt));
668+
return;
669+
}
670+
662671
/* we only expect one HELLO message */
663672
uv_read_stop((uv_stream_t *) &client->pipe);
664673

@@ -1176,6 +1185,15 @@ void
11761185
ic_proxy_client_on_p2c_data(ICProxyClient *client, ICProxyPkt *pkt,
11771186
ic_proxy_sent_cb callback, void *opaque)
11781187
{
1188+
/* sanity check: drop the packet with incorrect magic number */
1189+
if (!ic_proxy_pkt_is_valid(pkt))
1190+
{
1191+
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG, DEBUG1,
1192+
"ic-proxy: %s: received %s, dropping the invalid package (magic number mismatch)",
1193+
ic_proxy_client_get_name(client), ic_proxy_pkt_to_str(pkt));
1194+
return;
1195+
}
1196+
11791197
/* A placeholder does not send any packets, it always cache them */
11801198
if (client->state & IC_PROXY_CLIENT_STATE_PLACEHOLDER)
11811199
{
@@ -1256,6 +1274,8 @@ ic_proxy_client_cache_p2c_pkt(ICProxyClient *client, ICProxyPkt *pkt)
12561274
/* TODO: drop out-of-date pkt directly */
12571275
/* TODO: verify the pkt is to client */
12581276

1277+
Assert(ic_proxy_pkt_is_valid(pkt));
1278+
12591279
client->pkts = lappend(client->pkts, pkt);
12601280

12611281
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG, DEBUG3,

contrib/interconnect/proxy/ic_proxy_packet.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ void
8282
ic_proxy_message_init(ICProxyPkt *pkt, ICProxyMessageType type,
8383
const ICProxyKey *key)
8484
{
85+
pkt->magicNumber = IC_PROXY_PKT_MAGIC_NUMBER;
8586
pkt->type = type;
8687
pkt->len = sizeof(*pkt);
8788

@@ -156,13 +157,14 @@ ic_proxy_pkt_to_str(const ICProxyPkt *pkt)
156157
static char buf[256];
157158

158159
snprintf(buf, sizeof(buf),
159-
"%s [con%d,cmd%d,slice[%hd->%hd] %hu bytes seg%hd:dbid%hu:p%d->seg%hd:dbid%hu:p%d]",
160+
"%s [con%d,cmd%d,slice[%hd->%hd] %hu bytes seg%hd:dbid%hu:p%d->seg%hd:dbid%hu:p%d magic%u]",
160161
ic_proxy_message_type_to_str(pkt->type),
161162
pkt->sessionId, pkt->commandId,
162163
pkt->sendSliceIndex, pkt->recvSliceIndex,
163164
pkt->len,
164165
pkt->srcContentId, pkt->srcDbid, pkt->srcPid,
165-
pkt->dstContentId, pkt->dstDbid, pkt->dstPid);
166+
pkt->dstContentId, pkt->dstDbid, pkt->dstPid,
167+
pkt->magicNumber);
166168

167169
return buf;
168170
}

contrib/interconnect/proxy/ic_proxy_packet.h

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ typedef struct ICProxyPkt ICProxyPkt;
2222
#include "ic_proxy_key.h"
2323

2424
#define IC_PROXY_MAX_PKT_SIZE (Gp_max_packet_size + sizeof(ICProxyPkt))
25+
#define IC_PROXY_PKT_MAGIC_NUMBER (0xBADDCAFE)
2526

2627
typedef enum
2728
{
@@ -41,6 +42,9 @@ typedef enum
4142

4243
struct ICProxyPkt
4344
{
45+
/* for the sanity check of package */
46+
uint32 magicNumber;
47+
4448
uint16 len;
4549
uint16 type;
4650

@@ -97,4 +101,12 @@ ic_proxy_pkt_is(const ICProxyPkt *pkt, ICProxyMessageType type)
97101
return type == pkt->type;
98102
}
99103

104+
/* check the validity of a package by magicNumber */
105+
static inline bool
106+
ic_proxy_pkt_is_valid(const ICProxyPkt *pkt)
107+
{
108+
Assert(pkt);
109+
return pkt->magicNumber == IC_PROXY_PKT_MAGIC_NUMBER;
110+
}
111+
100112
#endif /* IC_PROXY_PACKET_H */

contrib/interconnect/proxy/ic_proxy_peer.c

+27
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,15 @@ ic_proxy_peer_on_data_pkt(void *opaque, const void *data, uint16 size)
290290
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG, DEBUG5,
291291
"ic-proxy: %s: received %s", peer->name, ic_proxy_pkt_to_str(pkt));
292292

293+
/* sanity check: drop the packet with incorrect magic number */
294+
if (!ic_proxy_pkt_is_valid(pkt))
295+
{
296+
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG, DEBUG1,
297+
"ic-proxy: %s: received %s, dropping the invalid package (magic number mismatch)",
298+
peer->name, ic_proxy_pkt_to_str(pkt));
299+
return;
300+
}
301+
293302
if (!(peer->state & IC_PROXY_PEER_STATE_READY_FOR_DATA))
294303
{
295304
elog(WARNING, "ic-proxy: %s: not ready to receive DATA yet: %s",
@@ -529,6 +538,15 @@ ic_proxy_peer_on_hello_pkt(void *opaque, const void *data, uint16 size)
529538
ICProxyPeer *peer = opaque;
530539
ICProxyKey key;
531540

541+
/* sanity check: drop the packet with incorrect magic number */
542+
if (!ic_proxy_pkt_is_valid(pkt))
543+
{
544+
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG, DEBUG1,
545+
"ic-proxy: %s: received %s, dropping the invalid package (magic number mismatch)",
546+
peer->name, ic_proxy_pkt_to_str(pkt));
547+
return;
548+
}
549+
532550
/* we only expect one hello message */
533551
uv_read_stop((uv_stream_t *) &peer->tcp);
534552

@@ -636,6 +654,15 @@ ic_proxy_peer_on_hello_ack_pkt(void *opaque, const void *data, uint16 size)
636654
elog(ERROR, "ic-proxy: %s: received incomplete HELLO ACK: size = %d",
637655
peer->name, size);
638656

657+
/* sanity check: drop the packet with incorrect magic number */
658+
if (!ic_proxy_pkt_is_valid(pkt))
659+
{
660+
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG, DEBUG1,
661+
"ic-proxy: %s: received %s, dropping the invalid package (magic number mismatch)",
662+
peer->name, ic_proxy_pkt_to_str(pkt));
663+
return;
664+
}
665+
639666
if (peer->state & IC_PROXY_PEER_STATE_RECEIVED_HELLO_ACK)
640667
{
641668
/*

contrib/interconnect/proxy/ic_proxy_router.c

+3
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ void
202202
ic_proxy_router_route(uv_loop_t *loop, ICProxyPkt *pkt,
203203
ic_proxy_sent_cb callback, void *opaque)
204204
{
205+
Assert(ic_proxy_pkt_is_valid(pkt));
205206
if (pkt->dstDbid == pkt->srcDbid)
206207
{
207208
/*
@@ -254,6 +255,8 @@ ic_proxy_router_on_write(uv_write_t *req, int status)
254255
ICProxyWriteReq *wreq = (ICProxyWriteReq *) req;
255256
ICProxyPkt *pkt = req->data;
256257

258+
Assert(ic_proxy_pkt_is_valid(pkt));
259+
257260
if (status < 0)
258261
{
259262
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG, DEBUG5,

0 commit comments

Comments
 (0)