Skip to content

Commit e4572e6

Browse files
committed
wip
1 parent 3b7392e commit e4572e6

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

plugins/in_forward/fw_conn.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ struct fw_conn *fw_conn_add(struct flb_connection *connection, struct flb_in_fw_
159159
return NULL;
160160
}
161161

162+
conn->being_deleted = 0;
162163
conn->handshake_status = FW_HANDSHAKE_ESTABLISHED;
163164
/*
164165
* Always force the secure-forward handshake when:
@@ -243,6 +244,12 @@ struct fw_conn *fw_conn_add(struct flb_connection *connection, struct flb_in_fw_
243244

244245
int fw_conn_del(struct fw_conn *conn)
245246
{
247+
/*
248+
* Set being_deleted flag to prevent any in-flight processing
249+
* from accessing this connection's resources
250+
*/
251+
conn->being_deleted = 1;
252+
246253
/* The downstream unregisters the file descriptor from the event-loop
247254
* so there's nothing to be done by the plugin
248255
*/
@@ -254,6 +261,7 @@ int fw_conn_del(struct fw_conn *conn)
254261
/* Release decompression context if it exists */
255262
if (conn->d_ctx) {
256263
flb_decompression_context_destroy(conn->d_ctx);
264+
conn->d_ctx = NULL;
257265
}
258266

259267
if (conn->helo != NULL) {
@@ -264,8 +272,15 @@ int fw_conn_del(struct fw_conn *conn)
264272
flb_sds_destroy(conn->helo->salt);
265273
}
266274
flb_free(conn->helo);
275+
conn->helo = NULL;
267276
}
268-
flb_free(conn->buf);
277+
278+
/* Free buffer and set to NULL to prevent use-after-free */
279+
if (conn->buf) {
280+
flb_free(conn->buf);
281+
conn->buf = NULL;
282+
}
283+
269284
flb_free(conn);
270285

271286
return 0;

plugins/in_forward/fw_conn.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct flb_in_fw_helo;
4343
struct fw_conn {
4444
int status; /* Connection status */
4545
int handshake_status; /* handshake status */
46+
int being_deleted; /* Flag: connection is being deleted */
4647

4748
/* Buffer */
4849
char *buf; /* Buffer data */

plugins/in_forward/fw_prot.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,11 @@ static size_t receiver_recv(struct fw_conn *conn, char *buf, size_t try_size) {
10761076
size_t off;
10771077
size_t actual_size;
10781078

1079+
/* Safety check: ensure connection is not being deleted and buffer exists */
1080+
if (conn->being_deleted || !conn->buf) {
1081+
return 0;
1082+
}
1083+
10791084
off = conn->buf_len - conn->rest;
10801085
actual_size = try_size;
10811086

@@ -1288,6 +1293,24 @@ int fw_prot_process(struct flb_input_instance *ins, struct fw_conn *conn)
12881293
conn->rest = conn->buf_len;
12891294

12901295
while (1) {
1296+
/* Check if connection is being deleted or plugin is paused */
1297+
if (conn->being_deleted) {
1298+
msgpack_unpacker_free(unp);
1299+
msgpack_unpacked_destroy(&result);
1300+
flb_sds_destroy(out_tag);
1301+
return 0;
1302+
}
1303+
1304+
pthread_mutex_lock(&ctx->conn_mutex);
1305+
if (ctx->is_paused) {
1306+
pthread_mutex_unlock(&ctx->conn_mutex);
1307+
msgpack_unpacker_free(unp);
1308+
msgpack_unpacked_destroy(&result);
1309+
flb_sds_destroy(out_tag);
1310+
return 0;
1311+
}
1312+
pthread_mutex_unlock(&ctx->conn_mutex);
1313+
12911314
recv_len = receiver_to_unpacker(conn, EACH_RECV_SIZE, unp);
12921315
if (recv_len == 0) {
12931316
/* No more data */

0 commit comments

Comments
 (0)