Skip to content

Commit 1ffe1bd

Browse files
committed
update
1 parent f66d497 commit 1ffe1bd

File tree

5 files changed

+56
-30
lines changed

5 files changed

+56
-30
lines changed

include/cdk/cdk-types.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ struct cdk_unpacker_s {
262262
} lengthfield;
263263

264264
struct {
265-
void (*unpack)(cdk_channel_t* channel);
265+
bool (*unpack)(cdk_channel_t* channel);
266266
} userdefined;
267267
};
268268
};
@@ -363,6 +363,7 @@ struct cdk_channel_error_s {
363363
CHANNEL_ERROR_POLLER_SHUTDOWN,
364364
CHANNEL_ERROR_SYSCALL_FAIL,
365365
CHANNEL_ERROR_TLS_FAIL,
366+
CHANNEL_ERROR_BUFFER_OVERFLOW,
366367
CHANNEL_ERROR_END,
367368
} code;
368369
char* codestr;

src/net/channel.c

+25-4
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,15 @@ static void _tls_recv(cdk_channel_t* channel) {
284284
}
285285
channel->latest_rd_time = cdk_time_now();
286286
channel->rxbuf.off += n;
287-
if (channel->rxbuf.off > MAX_TCP_RECVBUF_SIZE) {
288-
abort();
287+
288+
if (!unpacker_unpack(channel)) {
289+
cdk_channel_error_t error = {
290+
.code = CHANNEL_ERROR_BUFFER_OVERFLOW,
291+
.codestr = CHANNEL_ERROR_BUFFER_OVERFLOW_STR};
292+
channel_error_update(channel, error);
293+
channel_destroy(channel);
294+
return;
289295
}
290-
unpacker_unpack(channel);
291296
}
292297

293298
static void _tcp_recv(cdk_channel_t* channel) {
@@ -320,7 +325,23 @@ static void _tcp_recv(cdk_channel_t* channel) {
320325
}
321326
channel->latest_rd_time = cdk_time_now();
322327
channel->rxbuf.off += n;
323-
unpacker_unpack(channel);
328+
329+
if (channel->rxbuf.off > MAX_TCP_RECVBUF_SIZE) {
330+
cdk_channel_error_t error = {
331+
.code = CHANNEL_ERROR_BUFFER_OVERFLOW,
332+
.codestr = CHANNEL_ERROR_BUFFER_OVERFLOW_STR};
333+
channel_error_update(channel, error);
334+
channel_destroy(channel);
335+
return;
336+
}
337+
if (!unpacker_unpack(channel)) {
338+
cdk_channel_error_t error = {
339+
.code = CHANNEL_ERROR_BUFFER_OVERFLOW,
340+
.codestr = CHANNEL_ERROR_BUFFER_OVERFLOW_STR};
341+
channel_error_update(channel, error);
342+
channel_destroy(channel);
343+
return;
344+
}
324345
}
325346

326347
static void _udp_recv(cdk_channel_t* channel) {

src/net/channel.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ _Pragma("once")
3636
"Channel destroyed due to connection establishment timeout"
3737
#define CHANNEL_ERROR_POLLER_SHUTDOWN_STR \
3838
"Channel destroyed due to poller shutdown"
39+
#define CHANNEL_ERROR_BUFFER_OVERFLOW_STR \
40+
"Channel destroyed due to buffer overflow"
3941

40-
extern cdk_channel_t* channel_create(cdk_poller_t* poller, cdk_sock_t sock, cdk_channel_mode_t mode, cdk_side_t side, cdk_handler_t* handler, cdk_tls_ctx_t* tls_ctx);
42+
extern cdk_channel_t* channel_create(cdk_poller_t* poller, cdk_sock_t sock, cdk_channel_mode_t mode, cdk_side_t side, cdk_handler_t* handler, cdk_tls_ctx_t* tls_ctx);
4143
extern void channel_destroy(cdk_channel_t* channel);
4244
extern void channel_recv(cdk_channel_t* channel);
4345
extern void channel_send(cdk_channel_t* channel);

src/net/unpacker.c

+25-23
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "cdk/cdk-utils.h"
2424
#include "cdk/encoding/cdk-varint.h"
2525

26-
static inline void _fixedlen_unpack(cdk_channel_t* channel) {
26+
static inline bool _fixedlen_unpack(cdk_channel_t* channel) {
2727
char* head = channel->rxbuf.buf;
2828
char* tail = (char*)channel->rxbuf.buf + channel->rxbuf.off;
2929
char* tmp = head;
@@ -33,22 +33,26 @@ static inline void _fixedlen_unpack(cdk_channel_t* channel) {
3333
if (accumulated < channel->handler->unpacker->fixedlen.len) {
3434
break;
3535
}
36+
if (channel->handler->unpacker->fixedlen.len > channel->rxbuf.len) {
37+
return false;
38+
}
3639
if (channel->handler->on_read) {
3740
channel->handler->on_read(channel, tmp, channel->handler->unpacker->fixedlen.len);
3841
}
3942
tmp += channel->handler->unpacker->fixedlen.len;
4043
accumulated -= channel->handler->unpacker->fixedlen.len;
4144
}
4245
if (tmp == head) {
43-
return;
46+
return true;
4447
}
4548
channel->rxbuf.off = accumulated;
4649
if (accumulated) {
4750
memmove(channel->rxbuf.buf, tmp, accumulated);
4851
}
52+
return true;
4953
}
5054

51-
static inline void _delimiter_unpack(cdk_channel_t* channel) {
55+
static inline bool _delimiter_unpack(cdk_channel_t* channel) {
5256
char* head = channel->rxbuf.buf;
5357
char* tail = (char*)channel->rxbuf.buf + channel->rxbuf.off;
5458
char* tmp = head;
@@ -57,7 +61,7 @@ static inline void _delimiter_unpack(cdk_channel_t* channel) {
5761

5862
uint32_t accumulated = (uint32_t)(tail - head);
5963
if (accumulated < dlen) {
60-
return;
64+
return true;
6165
}
6266
/**
6367
* for performance, thus split buffer by KMP.
@@ -84,6 +88,9 @@ static inline void _delimiter_unpack(cdk_channel_t* channel) {
8488
j++;
8589
}
8690
if (j == dlen) {
91+
if (((i - dlen + 1) + dlen) > channel->rxbuf.len) {
92+
return false;
93+
}
8794
if (channel->handler->on_read) {
8895
channel->handler->on_read(channel, tmp, ((i - dlen + 1) + dlen));
8996
}
@@ -97,16 +104,16 @@ static inline void _delimiter_unpack(cdk_channel_t* channel) {
97104
free(next);
98105
next = NULL;
99106
if (tmp == head) {
100-
return;
107+
return true;
101108
}
102109
channel->rxbuf.off = accumulated;
103110
if (accumulated) {
104111
memmove(channel->rxbuf.buf, tmp, accumulated);
105112
}
106-
return;
113+
return true;
107114
}
108115

109-
static inline void _lengthfield_unpack(cdk_channel_t* channel) {
116+
static inline bool _lengthfield_unpack(cdk_channel_t* channel) {
110117
uint32_t fs; /* frame size */
111118
uint32_t hs; /* header size */
112119
uint32_t ps; /* payload size */
@@ -144,7 +151,7 @@ static inline void _lengthfield_unpack(cdk_channel_t* channel) {
144151
fs = hs + ps + channel->handler->unpacker->lengthfield.adj;
145152

146153
if (fs > channel->rxbuf.len) {
147-
abort();
154+
return false;
148155
}
149156
if (accumulated < fs) {
150157
break;
@@ -156,40 +163,35 @@ static inline void _lengthfield_unpack(cdk_channel_t* channel) {
156163
accumulated -= fs;
157164
}
158165
if (tmp == head) {
159-
return;
166+
return true;
160167
}
161168
channel->rxbuf.off = accumulated;
162169
if (accumulated) {
163170
memmove(channel->rxbuf.buf, tmp, accumulated);
164171
}
165-
return;
172+
return true;
166173
}
167174

168-
static void _userdefined_unpack(cdk_channel_t* channel) {
169-
channel->handler->unpacker->userdefined.unpack(channel);
175+
static bool _userdefined_unpack(cdk_channel_t* channel) {
176+
return channel->handler->unpacker->userdefined.unpack(channel);
170177
}
171178

172-
void unpacker_unpack(cdk_channel_t* channel) {
179+
bool unpacker_unpack(cdk_channel_t* channel) {
173180
switch (channel->handler->unpacker->type)
174181
{
175182
case UNPACKER_TYPE_FIXEDLEN: {
176-
_fixedlen_unpack(channel);
177-
break;
183+
return _fixedlen_unpack(channel);
178184
}
179185
case UNPACKER_TYPE_DELIMITER: {
180-
_delimiter_unpack(channel);
181-
break;
186+
return _delimiter_unpack(channel);
182187
}
183188
case UNPACKER_TYPE_LENGTHFIELD: {
184-
_lengthfield_unpack(channel);
185-
break;
189+
return _lengthfield_unpack(channel);
186190
}
187191
case UNPACKER_TYPE_USERDEFINED: {
188-
_userdefined_unpack(channel);
189-
break;
192+
return _userdefined_unpack(channel);
190193
}
191194
default:
192-
abort();
195+
return false;
193196
}
194-
return;
195197
}

src/net/unpacker.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ _Pragma("once")
2323

2424
#include "cdk/cdk-types.h"
2525

26-
extern void unpacker_unpack(cdk_channel_t* channel);
26+
extern bool unpacker_unpack(cdk_channel_t* channel);

0 commit comments

Comments
 (0)