1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
From 84681d9728ceb7f6ea2b6751b4d87200d8a62f14 Mon Sep 17 00:00:00 2001
From: "Roger A. Light" <roger@atchoo.org>
Date: Tue, 17 Sep 2019 14:56:08 +0100
Subject: Fix for CVE-xxxx-xxxx
diff --git a/lib/util_mosq.c b/lib/util_mosq.c
index 25bd61d..cfc3ce8 100644
--- a/lib/util_mosq.c
+++ b/lib/util_mosq.c
@@ -143,14 +143,25 @@ uint16_t mosquitto__mid_generate(struct mosquitto *mosq)
int mosquitto_pub_topic_check(const char *str)
{
int len = 0;
+#ifdef WITH_BROKER
+ int hier_count = 0;
+#endif
while(str && str[0]){
if(str[0] == '+' || str[0] == '#'){
return MOSQ_ERR_INVAL;
}
+#ifdef WITH_BROKER
+ else if(str[0] == '/'){
+ hier_count++;
+ }
+#endif
len++;
str = &str[1];
}
if(len > 65535) return MOSQ_ERR_INVAL;
+#ifdef WITH_BROKER
+ if(hier_count > TOPIC_HIERARCHY_LIMIT) return MOSQ_ERR_INVAL;
+#endif
return MOSQ_ERR_SUCCESS;
}
@@ -158,6 +169,9 @@ int mosquitto_pub_topic_check(const char *str)
int mosquitto_pub_topic_check2(const char *str, size_t len)
{
int i;
+#ifdef WITH_BROKER
+ int hier_count = 0;
+#endif
if(len > 65535) return MOSQ_ERR_INVAL;
@@ -165,7 +179,15 @@ int mosquitto_pub_topic_check2(const char *str, size_t len)
if(str[i] == '+' || str[i] == '#'){
return MOSQ_ERR_INVAL;
}
+#ifdef WITH_BROKER
+ else if(str[i] == '/'){
+ hier_count++;
+ }
+#endif
}
+#ifdef WITH_BROKER
+ if(hier_count > TOPIC_HIERARCHY_LIMIT) return MOSQ_ERR_INVAL;
+#endif
return MOSQ_ERR_SUCCESS;
}
@@ -181,6 +203,10 @@ int mosquitto_sub_topic_check(const char *str)
{
char c = '\0';
int len = 0;
+#ifdef WITH_BROKER
+ int hier_count = 0;
+#endif
+
while(str && str[0]){
if(str[0] == '+'){
if((c != '\0' && c != '/') || (str[1] != '\0' && str[1] != '/')){
@@ -191,11 +217,19 @@ int mosquitto_sub_topic_check(const char *str)
return MOSQ_ERR_INVAL;
}
}
+#ifdef WITH_BROKER
+ else if(str[0] == '/'){
+ hier_count++;
+ }
+#endif
len++;
c = str[0];
str = &str[1];
}
if(len > 65535) return MOSQ_ERR_INVAL;
+#ifdef WITH_BROKER
+ if(hier_count > TOPIC_HIERARCHY_LIMIT) return MOSQ_ERR_INVAL;
+#endif
return MOSQ_ERR_SUCCESS;
}
@@ -204,6 +238,9 @@ int mosquitto_sub_topic_check2(const char *str, size_t len)
{
char c = '\0';
int i;
+#ifdef WITH_BROKER
+ int hier_count = 0;
+#endif
if(len > 65535) return MOSQ_ERR_INVAL;
@@ -217,8 +254,16 @@ int mosquitto_sub_topic_check2(const char *str, size_t len)
return MOSQ_ERR_INVAL;
}
}
+#ifdef WITH_BROKER
+ else if(str[i] == '/'){
+ hier_count++;
+ }
+#endif
c = str[i];
}
+#ifdef WITH_BROKER
+ if(hier_count > TOPIC_HIERARCHY_LIMIT) return MOSQ_ERR_INVAL;
+#endif
return MOSQ_ERR_SUCCESS;
}
diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h
index 512937a..5c077cb 100644
--- a/src/mosquitto_broker_internal.h
+++ b/src/mosquitto_broker_internal.h
@@ -70,6 +70,9 @@ Contributors:
#define WEBSOCKET_CLIENT -2
+
+#define TOPIC_HIERARCHY_LIMIT 200
+
/* ========================================
* UHPA data types
* ======================================== */
diff --git a/src/subs.c b/src/subs.c
index 6b53aa6..a03c7bd 100644
--- a/src/subs.c
+++ b/src/subs.c
@@ -178,6 +178,7 @@ static int sub__topic_tokenise(const char *subtopic, struct sub__token **topics)
int start, stop, tlen;
int i;
mosquitto__topic_element_uhpa topic;
+ int count = 0;
assert(subtopic);
assert(topics);
@@ -200,6 +201,7 @@ static int sub__topic_tokenise(const char *subtopic, struct sub__token **topics)
stop = 0;
for(i=start; i<len+1; i++){
+ count++;
if(subtopic[i] == '/' || subtopic[i] == '\0'){
stop = i;
@@ -219,6 +221,11 @@ static int sub__topic_tokenise(const char *subtopic, struct sub__token **topics)
}
}
+ if(count > TOPIC_HIERARCHY_LIMIT){
+ /* Set limit on hierarchy levels, to restrict stack usage. */
+ goto cleanup;
+ }
+
return MOSQ_ERR_SUCCESS;
cleanup:
--
2.20.1
|