Skip to content

Commit a6a1cee

Browse files
author
Hasnain Virk
committed
LoRaWAN: Memory corruption due to band mishandling
band_t structure in phy parameters was not being filled in properly and we were spilling over the array boundary for bands. In addition to that two utility functions are added to perform safety checking taking in the frequency and filling out band information plus boundry value checks.
1 parent 675528b commit a6a1cee

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

features/lorawan/lorastack/phy/LoRaPHY.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ uint8_t LoRaPHY::request_new_channel(int8_t channel_id, channel_params_t* new_ch
127127
status &= 0xFC;
128128
}
129129
} else {
130-
130+
new_channel->band = lookup_band_for_frequency(new_channel->frequency);
131131
switch (add_channel(new_channel, channel_id)) {
132132
case LORAWAN_STATUS_OK:
133133
{
@@ -186,13 +186,13 @@ bool LoRaPHY::verify_channel_DR(uint8_t nb_channels, uint16_t* channel_mask,
186186
return false;
187187
}
188188

189-
uint8_t LoRaPHY::val_in_range( int8_t value, int8_t min, int8_t max )
189+
bool LoRaPHY::val_in_range( int8_t value, int8_t min, int8_t max )
190190
{
191191
if ((value >= min) && (value <= max)) {
192-
return 1;
192+
return true;
193193
}
194194

195-
return 0;
195+
return false;
196196
}
197197

198198
bool LoRaPHY::disable_channel(uint16_t* channel_mask, uint8_t id,
@@ -733,6 +733,9 @@ void LoRaPHY::apply_cf_list(const uint8_t* payload, uint8_t size)
733733
}
734734

735735
if (new_channel.frequency != 0) {
736+
//lookup for band
737+
new_channel.band = lookup_band_for_frequency(new_channel.frequency);
738+
736739
// Try to add channel
737740
add_channel(&new_channel, channel_id);
738741
} else {
@@ -1065,21 +1068,30 @@ bool LoRaPHY::accept_tx_param_setup_req(uint8_t ul_dwell_time, uint8_t dl_dwell_
10651068
return phy_params.accept_tx_param_setup_req;
10661069
}
10671070

1068-
bool LoRaPHY::verify_frequency(uint32_t freq)
1071+
int LoRaPHY::lookup_band_for_frequency(uint32_t freq) const
10691072
{
1070-
band_t *bands_table = (band_t *)phy_params.bands.table;
1071-
10721073
// check all sub bands (if there are sub-bands) to check if the given
10731074
// frequency falls into any of the frequency ranges
10741075

1075-
for (uint8_t i=0; i<phy_params.bands.size; i++) {
1076-
if (freq <= bands_table[i].higher_band_freq
1077-
&& freq >= bands_table[i].lower_band_freq) {
1078-
return true;
1076+
for (int band=0; band<phy_params.bands.size; band++) {
1077+
if (verify_frequency_for_band(freq, band)) {
1078+
return band;
10791079
}
10801080
}
10811081

1082-
return false;
1082+
return -1;
1083+
}
1084+
1085+
bool LoRaPHY::verify_frequency_for_band(uint32_t freq, uint8_t band) const
1086+
{
1087+
band_t *bands_table = (band_t *)phy_params.bands.table;
1088+
1089+
if (freq <= bands_table[band].higher_band_freq
1090+
&& freq >= bands_table[band].lower_band_freq) {
1091+
return true;
1092+
} else {
1093+
return false;
1094+
}
10831095
}
10841096

10851097
uint8_t LoRaPHY::dl_channel_request(uint8_t channel_id, uint32_t rx1_frequency)
@@ -1091,7 +1103,8 @@ uint8_t LoRaPHY::dl_channel_request(uint8_t channel_id, uint32_t rx1_frequency)
10911103
uint8_t status = 0x03;
10921104

10931105
// Verify if the frequency is supported
1094-
if (verify_frequency(rx1_frequency) == false) {
1106+
uint8_t band = lookup_band_for_frequency(rx1_frequency);
1107+
if (verify_frequency_for_band(rx1_frequency, band) == false) {
10951108
status &= 0xFE;
10961109
}
10971110

@@ -1263,7 +1276,7 @@ lorawan_status_t LoRaPHY::set_next_channel(channel_selection_params_t* params,
12631276
return LORAWAN_STATUS_NO_CHANNEL_FOUND;
12641277
}
12651278

1266-
lorawan_status_t LoRaPHY::add_channel(channel_params_t* new_channel, uint8_t id)
1279+
lorawan_status_t LoRaPHY::add_channel(const channel_params_t* new_channel, uint8_t id)
12671280
{
12681281
bool dr_invalid = false;
12691282
bool freq_invalid = false;
@@ -1312,7 +1325,9 @@ lorawan_status_t LoRaPHY::add_channel(channel_params_t* new_channel, uint8_t id)
13121325

13131326
// Check frequency
13141327
if (!freq_invalid) {
1315-
if (verify_frequency(new_channel->frequency) == false) {
1328+
if (new_channel->band >= phy_params.bands.size
1329+
|| verify_frequency_for_band(new_channel->frequency,
1330+
new_channel->band) == false) {
13161331
freq_invalid = true;
13171332
}
13181333
}

features/lorawan/lorastack/phy/LoRaPHY.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class LoRaPHY : private mbed::NonCopyable<LoRaPHY> {
361361
* @return LORAWAN_STATUS_OK if everything goes fine, negative error code
362362
* otherwise.
363363
*/
364-
virtual lorawan_status_t add_channel(channel_params_t* new_channel, uint8_t id);
364+
virtual lorawan_status_t add_channel(const channel_params_t* new_channel, uint8_t id);
365365

366366
/** Removes a channel from the channel list.
367367
*
@@ -528,15 +528,19 @@ class LoRaPHY : private mbed::NonCopyable<LoRaPHY> {
528528
LoRaPHY(LoRaWANTimeHandler &lora_time);
529529

530530
/**
531-
* Verifies the given frequency.
531+
* Looks up corresponding band for a frequency. Returns -1 if not in any band.
532532
*/
533-
virtual bool verify_frequency(uint32_t freq);
533+
int lookup_band_for_frequency(uint32_t freq) const;
534534

535+
/**
536+
* Verifies, if a frequency is within a given band.
537+
*/
538+
virtual bool verify_frequency_for_band(uint32_t freq, uint8_t band) const;
535539

536540
/**
537541
* Verifies, if a value is in a given range.
538542
*/
539-
uint8_t val_in_range(int8_t value, int8_t min, int8_t max);
543+
bool val_in_range(int8_t value, int8_t min, int8_t max);
540544

541545
/**
542546
* Verifies, if a datarate is available on an active channel.

features/lorawan/lorastack/phy/LoRaPHYKR920.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ int8_t LoRaPHYKR920::get_max_eirp(uint32_t freq)
339339
}
340340

341341

342-
bool LoRaPHYKR920::verify_frequency(uint32_t freq)
342+
bool LoRaPHYKR920::verify_frequency_for_band(uint32_t freq, uint8_t band) const
343343
{
344344
uint32_t tmp_freq = freq;
345345

features/lorawan/lorastack/phy/LoRaPHYKR920.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class LoRaPHYKR920 : public LoRaPHY {
5454
LoRaPHYKR920(LoRaWANTimeHandler &lora_time);
5555
virtual ~LoRaPHYKR920();
5656

57-
virtual bool verify_frequency(uint32_t freq);
57+
virtual bool verify_frequency_for_band(uint32_t freq, uint8_t band) const;
5858

5959
virtual bool tx_config(tx_config_params_t* config, int8_t* tx_power,
6060
lorawan_time_t* tx_toa);

0 commit comments

Comments
 (0)