@@ -144,13 +144,14 @@ uint8_t MyMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secr
144144 return 13 ; // reply length
145145}
146146
147- uint8_t MyMesh::handleAnonRegionsReq (const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t * data) {
147+ uint8_t MyMesh::handleAnonRegionsReq (const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t * data, size_t data_len ) {
148148 if (anon_limiter.allow (rtc_clock.getCurrentTime ())) {
149149 // request data has: {reply-path-len}{reply-path}
150+ if (data_len < 1 ) return 0 ;
150151 reply_path_len = *data & 63 ;
151152 reply_path_hash_size = (*data >> 6 ) + 1 ;
152153 data++;
153-
154+ if ( 1 + ( size_t )reply_path_len * reply_path_hash_size > data_len) return 0 ;
154155 memcpy (reply_path, data, ((uint8_t )reply_path_len) * reply_path_hash_size);
155156 // data += (uint8_t)reply_path_len * reply_path_hash_size;
156157
@@ -163,13 +164,14 @@ uint8_t MyMesh::handleAnonRegionsReq(const mesh::Identity& sender, uint32_t send
163164 return 0 ;
164165}
165166
166- uint8_t MyMesh::handleAnonOwnerReq (const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t * data) {
167+ uint8_t MyMesh::handleAnonOwnerReq (const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t * data, size_t data_len ) {
167168 if (anon_limiter.allow (rtc_clock.getCurrentTime ())) {
168169 // request data has: {reply-path-len}{reply-path}
170+ if (data_len < 1 ) return 0 ;
169171 reply_path_len = *data & 63 ;
170172 reply_path_hash_size = (*data >> 6 ) + 1 ;
171173 data++;
172-
174+ if ( 1 + ( size_t )reply_path_len * reply_path_hash_size > data_len) return 0 ;
173175 memcpy (reply_path, data, ((uint8_t )reply_path_len) * reply_path_hash_size);
174176 // data += (uint8_t)reply_path_len * reply_path_hash_size;
175177
@@ -183,13 +185,14 @@ uint8_t MyMesh::handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender
183185 return 0 ;
184186}
185187
186- uint8_t MyMesh::handleAnonClockReq (const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t * data) {
188+ uint8_t MyMesh::handleAnonClockReq (const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t * data, size_t data_len ) {
187189 if (anon_limiter.allow (rtc_clock.getCurrentTime ())) {
188190 // request data has: {reply-path-len}{reply-path}
191+ if (data_len < 1 ) return 0 ;
189192 reply_path_len = *data & 63 ;
190193 reply_path_hash_size = (*data >> 6 ) + 1 ;
191194 data++;
192-
195+ if ( 1 + ( size_t )reply_path_len * reply_path_hash_size > data_len) return 0 ;
193196 memcpy (reply_path, data, ((uint8_t )reply_path_len) * reply_path_hash_size);
194197 // data += (uint8_t)reply_path_len * reply_path_hash_size;
195198
@@ -531,12 +534,12 @@ void MyMesh::onAnonDataRecv(mesh::Packet *packet, const uint8_t *secret, const m
531534 reply_path_len = -1 ;
532535 if (data[4 ] == 0 || data[4 ] >= ' ' ) { // is password, ie. a login request
533536 reply_len = handleLoginReq (sender, secret, timestamp, &data[4 ], packet->isRouteFlood ());
534- } else if (data[4 ] == ANON_REQ_TYPE_REGIONS && packet->isRouteDirect ()) {
535- reply_len = handleAnonRegionsReq (sender, timestamp, &data[5 ]);
536- } else if (data[4 ] == ANON_REQ_TYPE_OWNER && packet->isRouteDirect ()) {
537- reply_len = handleAnonOwnerReq (sender, timestamp, &data[5 ]);
538- } else if (data[4 ] == ANON_REQ_TYPE_BASIC && packet->isRouteDirect ()) {
539- reply_len = handleAnonClockReq (sender, timestamp, &data[5 ]);
537+ } else if (data[4 ] == ANON_REQ_TYPE_REGIONS && packet->isRouteDirect () && len > 5 ) {
538+ reply_len = handleAnonRegionsReq (sender, timestamp, &data[5 ], len - 5 );
539+ } else if (data[4 ] == ANON_REQ_TYPE_OWNER && packet->isRouteDirect () && len > 5 ) {
540+ reply_len = handleAnonOwnerReq (sender, timestamp, &data[5 ], len - 5 );
541+ } else if (data[4 ] == ANON_REQ_TYPE_BASIC && packet->isRouteDirect () && len > 5 ) {
542+ reply_len = handleAnonClockReq (sender, timestamp, &data[5 ], len - 5 );
540543 } else {
541544 reply_len = 0 ; // unknown/invalid request type
542545 }
0 commit comments