Skip to content

Commit b8aa21c

Browse files
committed
Fix group custom packet size limits
Lossy custom packets cannot be split, therefore they need to be limited to the maximum safe UDP packet size.
1 parent de97532 commit b8aa21c

File tree

5 files changed

+68
-23
lines changed

5 files changed

+68
-23
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
769233afaac07be03c094411e6ec8f031bde41beae475c74e154e51e51e9168b /usr/local/bin/tox-bootstrapd
1+
cbd8bea9d23a961f27aacd35c4509fc1c22d72356f61d1ec74cc469b6b14490d /usr/local/bin/tox-bootstrapd

toxcore/group_chats.c

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4893,10 +4893,26 @@ static int handle_gc_private_message(const GC_Session *c, const GC_Chat *chat, c
48934893
return 0;
48944894
}
48954895

4896+
/** @brief Returns false if a custom packet is too large. */
4897+
static bool custom_gc_packet_length_is_valid(uint16_t length, bool lossless)
4898+
{
4899+
if (lossless) {
4900+
if (length > MAX_GC_CUSTOM_LOSSLESS_PACKET_SIZE) {
4901+
return false;
4902+
}
4903+
} else {
4904+
if (length > MAX_GC_CUSTOM_LOSSY_PACKET_SIZE) {
4905+
return false;
4906+
}
4907+
}
4908+
4909+
return true;
4910+
}
4911+
48964912
int gc_send_custom_private_packet(const GC_Chat *chat, bool lossless, uint32_t peer_id, const uint8_t *message,
48974913
uint16_t length)
48984914
{
4899-
if (length > MAX_GC_CUSTOM_PACKET_SIZE) {
4915+
if (!custom_gc_packet_length_is_valid(length, lossless)) {
49004916
return -1;
49014917
}
49024918

@@ -4926,16 +4942,23 @@ int gc_send_custom_private_packet(const GC_Chat *chat, bool lossless, uint32_t p
49264942

49274943
return ret ? 0 : -5;
49284944
}
4945+
4946+
4947+
49294948
/** @brief Handles a custom private packet.
49304949
*
49314950
* @retval 0 if packet is handled correctly.
49324951
* @retval -1 if packet has invalid size.
49334952
*/
4934-
non_null(1, 2, 3, 4) nullable(6)
4953+
non_null(1, 2, 3, 4) nullable(7)
49354954
static int handle_gc_custom_private_packet(const GC_Session *c, const GC_Chat *chat, const GC_Peer *peer,
4936-
const uint8_t *data, uint16_t length, void *userdata)
4955+
const uint8_t *data, uint16_t length, bool lossless, void *userdata)
49374956
{
4938-
if (data == nullptr || length == 0 || length > MAX_GC_CUSTOM_PACKET_SIZE) {
4957+
if (!custom_gc_packet_length_is_valid(length, lossless)) {
4958+
return -1;
4959+
}
4960+
4961+
if (data == nullptr || length == 0) {
49394962
return -1;
49404963
}
49414964

@@ -4952,7 +4975,7 @@ static int handle_gc_custom_private_packet(const GC_Session *c, const GC_Chat *c
49524975

49534976
int gc_send_custom_packet(const GC_Chat *chat, bool lossless, const uint8_t *data, uint16_t length)
49544977
{
4955-
if (length > MAX_GC_CUSTOM_PACKET_SIZE) {
4978+
if (!custom_gc_packet_length_is_valid(length, lossless)) {
49564979
return -1;
49574980
}
49584981

@@ -4978,11 +5001,15 @@ int gc_send_custom_packet(const GC_Chat *chat, bool lossless, const uint8_t *dat
49785001
* Return 0 if packet is handled correctly.
49795002
* Return -1 if packet has invalid size.
49805003
*/
4981-
non_null(1, 2, 3, 4) nullable(6)
5004+
non_null(1, 2, 3, 4) nullable(7)
49825005
static int handle_gc_custom_packet(const GC_Session *c, const GC_Chat *chat, const GC_Peer *peer, const uint8_t *data,
4983-
uint16_t length, void *userdata)
5006+
uint16_t length, bool lossless, void *userdata)
49845007
{
4985-
if (data == nullptr || length == 0 || length > MAX_GC_CUSTOM_PACKET_SIZE) {
5008+
if (!custom_gc_packet_length_is_valid(length, lossless)) {
5009+
return -1;
5010+
}
5011+
5012+
if (data == nullptr || length == 0) {
49865013
return -1;
49875014
}
49885015

@@ -5913,12 +5940,12 @@ bool handle_gc_lossless_helper(const GC_Session *c, GC_Chat *chat, uint32_t peer
59135940
}
59145941

59155942
case GP_CUSTOM_PACKET: {
5916-
ret = handle_gc_custom_packet(c, chat, peer, data, length, userdata);
5943+
ret = handle_gc_custom_packet(c, chat, peer, data, length, true, userdata);
59175944
break;
59185945
}
59195946

59205947
case GP_CUSTOM_PRIVATE_PACKET: {
5921-
ret = handle_gc_custom_private_packet(c, chat, peer, data, length, userdata);
5948+
ret = handle_gc_custom_private_packet(c, chat, peer, data, length, true, userdata);
59225949
break;
59235950
}
59245951

@@ -6168,12 +6195,12 @@ static bool handle_gc_lossy_packet(const GC_Session *c, GC_Chat *chat, const uin
61686195
}
61696196

61706197
case GP_CUSTOM_PACKET: {
6171-
ret = handle_gc_custom_packet(c, chat, peer, data, payload_len, userdata);
6198+
ret = handle_gc_custom_packet(c, chat, peer, data, payload_len, false, userdata);
61726199
break;
61736200
}
61746201

61756202
case GP_CUSTOM_PRIVATE_PACKET: {
6176-
ret = handle_gc_custom_private_packet(c, chat, peer, data, payload_len, userdata);
6203+
ret = handle_gc_custom_private_packet(c, chat, peer, data, payload_len, false, userdata);
61776204
break;
61786205
}
61796206

toxcore/group_common.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@
2323
#define GC_MESSAGE_PSEUDO_ID_SIZE 4
2424
#define GROUP_MAX_MESSAGE_LENGTH 1368
2525

26-
#define MAX_GC_MESSAGE_SIZE GROUP_MAX_MESSAGE_LENGTH
27-
#define MAX_GC_MESSAGE_RAW_SIZE (MAX_GC_MESSAGE_SIZE + GC_MESSAGE_PSEUDO_ID_SIZE)
28-
#define MAX_GC_CUSTOM_PACKET_SIZE 1373
26+
/* Max size of a packet chunk. Packets larger than this must be split up.
27+
*
28+
* For an explanation on why this value was chosen, see the following link: https://archive.ph/vsCOG
29+
*/
30+
#define MAX_GC_PACKET_CHUNK_SIZE 500
31+
32+
#define MAX_GC_MESSAGE_SIZE GROUP_MAX_MESSAGE_LENGTH
33+
#define MAX_GC_MESSAGE_RAW_SIZE (MAX_GC_MESSAGE_SIZE + GC_MESSAGE_PSEUDO_ID_SIZE)
34+
#define MAX_GC_CUSTOM_LOSSLESS_PACKET_SIZE 1373
35+
#define MAX_GC_CUSTOM_LOSSY_PACKET_SIZE MAX_GC_PACKET_CHUNK_SIZE
2936
#define MAX_GC_PASSWORD_SIZE 32
3037
#define MAX_GC_SAVED_INVITES 10
3138
#define MAX_GC_PEERS_DEFAULT 100
3239
#define MAX_GC_SAVED_TIMEOUTS 12
3340
#define GC_MAX_SAVED_PEERS 100
3441
#define GC_SAVED_PEER_SIZE (ENC_PUBLIC_KEY_SIZE + sizeof(Node_format) + sizeof(IP_Port))
3542

36-
/* Max size of a packet chunk. Packets larger than this must be split up. */
37-
#define MAX_GC_PACKET_CHUNK_SIZE 500
38-
3943
/* Max size of a complete encrypted packet including headers. */
4044
#define MAX_GC_PACKET_SIZE (MAX_GC_PACKET_CHUNK_SIZE * 100)
4145

toxcore/tox.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ static_assert(TOX_MAX_STATUS_MESSAGE_LENGTH == MAX_STATUSMESSAGE_LENGTH,
5858
"TOX_MAX_STATUS_MESSAGE_LENGTH is assumed to be equal to MAX_STATUSMESSAGE_LENGTH");
5959
static_assert(TOX_GROUP_MAX_MESSAGE_LENGTH == GROUP_MAX_MESSAGE_LENGTH,
6060
"TOX_GROUP_MAX_MESSAGE_LENGTH is assumed to be equal to GROUP_MAX_MESSAGE_LENGTH");
61-
static_assert(TOX_MAX_CUSTOM_PACKET_SIZE == MAX_GC_CUSTOM_PACKET_SIZE,
62-
"TOX_MAX_CUSTOM_PACKET_SIZE is assumed to be equal to MAX_GC_CUSTOM_PACKET_SIZE");
61+
static_assert(TOX_MAX_CUSTOM_PACKET_SIZE == MAX_GC_CUSTOM_LOSSLESS_PACKET_SIZE,
62+
"TOX_MAX_CUSTOM_PACKET_SIZE is assumed to be equal to MAX_GC_CUSTOM_LOSSLESS_PACKET_SIZE");
6363

6464
struct Tox_Userdata {
6565
Tox *tox;

toxcore/tox.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,16 @@ uint32_t tox_group_max_part_length(void);
33033303
*/
33043304
#define TOX_GROUP_MAX_MESSAGE_LENGTH 1368
33053305

3306+
/**
3307+
* Maximum length of a group custom lossy packet.
3308+
*/
3309+
#define TOX_GROUP_MAX_CUSTOM_LOSSY_PACKET_LENGTH 500
3310+
3311+
/**
3312+
* Maximum length of a group custom lossless packet.
3313+
*/
3314+
#define TOX_GROUP_MAX_CUSTOM_LOSSLESS_PACKET_LENGTH 1373
3315+
33063316
/**
33073317
* Maximum length of a group name.
33083318
*/
@@ -4477,7 +4487,9 @@ typedef enum Tox_Err_Group_Send_Custom_Packet {
44774487
TOX_ERR_GROUP_SEND_CUSTOM_PACKET_GROUP_NOT_FOUND,
44784488

44794489
/**
4480-
* Message length exceeded TOX_GROUP_MAX_MESSAGE_LENGTH.
4490+
* Message length exceeded TOX_GROUP_MAX_CUSTOM_LOSSY_PACKET_LENGTH if the
4491+
* packet was lossy, or TOX_GROUP_MAX_CUSTOM_LOSSLESS_PACKET_LENGTH if the
4492+
* packet was lossless.
44814493
*/
44824494
TOX_ERR_GROUP_SEND_CUSTOM_PACKET_TOO_LONG,
44834495

@@ -4541,7 +4553,9 @@ typedef enum Tox_Err_Group_Send_Custom_Private_Packet {
45414553
TOX_ERR_GROUP_SEND_CUSTOM_PRIVATE_PACKET_GROUP_NOT_FOUND,
45424554

45434555
/**
4544-
* Message length exceeded TOX_MAX_CUSTOM_PACKET_SIZE.
4556+
* Message length exceeded TOX_GROUP_MAX_CUSTOM_LOSSY_PACKET_LENGTH if the
4557+
* packet was lossy, or TOX_GROUP_MAX_CUSTOM_LOSSLESS_PACKET_LENGTH if the
4558+
* packet was lossless.
45454559
*/
45464560
TOX_ERR_GROUP_SEND_CUSTOM_PRIVATE_PACKET_TOO_LONG,
45474561

0 commit comments

Comments
 (0)