Skip to content

LoRaWAN: Memory corruption due to band mishandling #6750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions features/lorawan/lorastack/phy/LoRaPHY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ uint8_t LoRaPHY::request_new_channel(int8_t channel_id, channel_params_t* new_ch
status &= 0xFC;
}
} else {

new_channel->band = lookup_band_for_frequency(new_channel->frequency);
switch (add_channel(new_channel, channel_id)) {
case LORAWAN_STATUS_OK:
{
Expand Down Expand Up @@ -186,13 +186,13 @@ bool LoRaPHY::verify_channel_DR(uint8_t nb_channels, uint16_t* channel_mask,
return false;
}

uint8_t LoRaPHY::val_in_range( int8_t value, int8_t min, int8_t max )
bool LoRaPHY::val_in_range( int8_t value, int8_t min, int8_t max )
{
if ((value >= min) && (value <= max)) {
return 1;
return true;
}

return 0;
return false;
}

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

if (new_channel.frequency != 0) {
//lookup for band
new_channel.band = lookup_band_for_frequency(new_channel.frequency);

// Try to add channel
add_channel(&new_channel, channel_id);
} else {
Expand Down Expand Up @@ -1065,21 +1068,30 @@ bool LoRaPHY::accept_tx_param_setup_req(uint8_t ul_dwell_time, uint8_t dl_dwell_
return phy_params.accept_tx_param_setup_req;
}

bool LoRaPHY::verify_frequency(uint32_t freq)
int LoRaPHY::lookup_band_for_frequency(uint32_t freq) const
{
band_t *bands_table = (band_t *)phy_params.bands.table;

// check all sub bands (if there are sub-bands) to check if the given
// frequency falls into any of the frequency ranges

for (uint8_t i=0; i<phy_params.bands.size; i++) {
if (freq <= bands_table[i].higher_band_freq
&& freq >= bands_table[i].lower_band_freq) {
return true;
for (int band=0; band<phy_params.bands.size; band++) {
if (verify_frequency_for_band(freq, band)) {
return band;
}
}

return false;
return -1;
}

bool LoRaPHY::verify_frequency_for_band(uint32_t freq, uint8_t band) const
{
band_t *bands_table = (band_t *)phy_params.bands.table;

if (freq <= bands_table[band].higher_band_freq
&& freq >= bands_table[band].lower_band_freq) {
return true;
} else {
return false;
}
}

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

// Verify if the frequency is supported
if (verify_frequency(rx1_frequency) == false) {
uint8_t band = lookup_band_for_frequency(rx1_frequency);
if (verify_frequency_for_band(rx1_frequency, band) == false) {
status &= 0xFE;
}

Expand Down Expand Up @@ -1263,7 +1276,7 @@ lorawan_status_t LoRaPHY::set_next_channel(channel_selection_params_t* params,
return LORAWAN_STATUS_NO_CHANNEL_FOUND;
}

lorawan_status_t LoRaPHY::add_channel(channel_params_t* new_channel, uint8_t id)
lorawan_status_t LoRaPHY::add_channel(const channel_params_t* new_channel, uint8_t id)
{
bool dr_invalid = false;
bool freq_invalid = false;
Expand Down Expand Up @@ -1312,7 +1325,9 @@ lorawan_status_t LoRaPHY::add_channel(channel_params_t* new_channel, uint8_t id)

// Check frequency
if (!freq_invalid) {
if (verify_frequency(new_channel->frequency) == false) {
if (new_channel->band >= phy_params.bands.size
|| verify_frequency_for_band(new_channel->frequency,
new_channel->band) == false) {
freq_invalid = true;
}
}
Expand Down
12 changes: 8 additions & 4 deletions features/lorawan/lorastack/phy/LoRaPHY.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ class LoRaPHY : private mbed::NonCopyable<LoRaPHY> {
* @return LORAWAN_STATUS_OK if everything goes fine, negative error code
* otherwise.
*/
virtual lorawan_status_t add_channel(channel_params_t* new_channel, uint8_t id);
virtual lorawan_status_t add_channel(const channel_params_t* new_channel, uint8_t id);

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

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

/**
* Verifies, if a frequency is within a given band.
*/
virtual bool verify_frequency_for_band(uint32_t freq, uint8_t band) const;

/**
* Verifies, if a value is in a given range.
*/
uint8_t val_in_range(int8_t value, int8_t min, int8_t max);
bool val_in_range(int8_t value, int8_t min, int8_t max);

/**
* Verifies, if a datarate is available on an active channel.
Expand Down
2 changes: 1 addition & 1 deletion features/lorawan/lorastack/phy/LoRaPHYKR920.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ int8_t LoRaPHYKR920::get_max_eirp(uint32_t freq)
}


bool LoRaPHYKR920::verify_frequency(uint32_t freq)
bool LoRaPHYKR920::verify_frequency_for_band(uint32_t freq, uint8_t band) const
{
uint32_t tmp_freq = freq;

Expand Down
2 changes: 1 addition & 1 deletion features/lorawan/lorastack/phy/LoRaPHYKR920.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class LoRaPHYKR920 : public LoRaPHY {
LoRaPHYKR920(LoRaWANTimeHandler &lora_time);
virtual ~LoRaPHYKR920();

virtual bool verify_frequency(uint32_t freq);
virtual bool verify_frequency_for_band(uint32_t freq, uint8_t band) const;

virtual bool tx_config(tx_config_params_t* config, int8_t* tx_power,
lorawan_time_t* tx_toa);
Expand Down