Skip to content

Commit

Permalink
Merge pull request #47 from Legion2/dev
Browse files Browse the repository at this point in the history
fixed integer overflow
added validation of internal data
validate the data loaded from EEPROM #44
  • Loading branch information
Legion2 authored Oct 19, 2019
2 parents 283c118 + ad10e06 commit 83d3259
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Corsair Lighting Protocol
version=0.6.0
version=0.6.1
author=Leon Kiefer
maintainer=Leon Kiefer
sentence=Allows iCUE to control RGB LEDs.
Expand Down
16 changes: 14 additions & 2 deletions src/FastLEDController.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class FastLEDController : public LEDController {
virtual void addLeds(uint8_t channel, CRGB * led_buffer);
virtual bool updateLEDs();
virtual size_t getEEPROMSize();
virtual bool isValidLEDGroup(const LEDGroup& ledGroup) override;
protected:
TemperatureController* const temperatureController;

Expand Down Expand Up @@ -144,7 +145,7 @@ bool FastLEDController<CHANNEL_LED_COUNT>::updateLEDs()
}
case CHANNEL_MODE_ON:
{
for (int i = 0; i < channel.groupsSet; i++) {
for (uint8_t i = 0; i < channel.groupsSet; i++) {
LEDGroup& group = channel.groups[i];
switch (group.mode)
{
Expand Down Expand Up @@ -449,10 +450,21 @@ inline size_t FastLEDController<CHANNEL_LED_COUNT>::getEEPROMSize()
return sizeof(channels);
}

template<size_t CHANNEL_LED_COUNT>
inline bool FastLEDController<CHANNEL_LED_COUNT>::isValidLEDGroup(const LEDGroup& ledGroup)
{
return LEDController::isValidLEDGroup(ledGroup) && CHANNEL_LED_COUNT >= (int)ledGroup.ledIndex + ledGroup.ledCount;
}

template<size_t CHANNEL_LED_COUNT>
bool FastLEDController<CHANNEL_LED_COUNT>::load() {
if (useEEPROM) {
EEPROM.get(EEPROM_ADDRESS, channels);
for (LEDChannel& channel: channels) {
if (!isValidLEDChannel(channel)) {
channel = LEDChannel();
}
}
return true;
}
return false;
Expand Down Expand Up @@ -485,7 +497,7 @@ inline void FastLEDController<CHANNEL_LED_COUNT>::setLEDExternalTemperature(uint
template<size_t CHANNEL_LED_COUNT>
inline void FastLEDController<CHANNEL_LED_COUNT>::setLEDColorValues(uint8_t channel, uint8_t color, uint8_t offset, const uint8_t* values, size_t len)
{
size_t copyLength = min(CHANNEL_LED_COUNT - offset, len);
int copyLength = min((int)CHANNEL_LED_COUNT - offset, (int)len);
if (copyLength >= 0) {
memcpy(volatileData[channel].values_buffer[color] + offset, values, copyLength);
}
Expand Down
30 changes: 30 additions & 0 deletions src/LEDController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
limitations under the License.
*/
#include "LEDController.h"
#include "TemperatureController.h"

void LEDController::handleLEDControl(const Command& command, const CorsairLightingProtocolResponse* response)
{
Expand Down Expand Up @@ -101,6 +102,11 @@ void LEDController::handleLEDControl(const Command& command, const CorsairLighti
group.temp2 = fromBigEndian(data[19], data[20]);
group.temp3 = fromBigEndian(data[21], data[22]);

if (!isValidLEDGroup(group)) {
response->sendError();
return;
}

trigger_save |= setLEDGroup(channel, channels[channel].groupsSet++, group);
break;
}
Expand Down Expand Up @@ -156,6 +162,30 @@ void LEDController::handleLEDControl(const Command& command, const CorsairLighti
response->send(nullptr, 0);
}

bool LEDController::isValidLEDChannel(const LEDChannel& ledChannel)
{
if (ledChannel.ledMode <= CHANNEL_MODE_SOFTWARE_PLAYBACK
&& ledChannel.ledPortType <= PORT_TYPE_UCS1903
&& ledChannel.groupsSet < GROUPS_NUM) {
for (uint8_t i = 0; i < ledChannel.groupsSet; i++) {
if (!isValidLEDGroup(ledChannel.groups[i])) {
return false;
}
}
return true;
}
return false;
}

bool LEDController::isValidLEDGroup(const LEDGroup& ledGroup)
{
return ledGroup.mode <= GROUP_MODE_Rainbow
&& ledGroup.speed <= GROUP_SPEED_LOW
&& ledGroup.direction <= GROUP_DIRECTION_FORWARD
&& (ledGroup.tempGroup == GROUP_TEMP_GROUP_EXTERNAL
|| ledGroup.tempGroup < TEMPERATURE_NUM);
}

uint8_t LEDController::getLEDStripMask(uint8_t channel, uint8_t set)
{
return channels[channel].groups[set].ledCount;
Expand Down
2 changes: 2 additions & 0 deletions src/LEDController.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct LEDChannel {
class LEDController : public ILEDController {
public:
virtual void handleLEDControl(const Command & command, const CorsairLightingProtocolResponse* response) override;
virtual bool isValidLEDChannel(const LEDChannel& ledChannel);
virtual bool isValidLEDGroup(const LEDGroup& ledGroup);
protected:
LEDChannel channels[CHANNEL_NUM];
// Indicates that the configuration of the channels has been changed and should be saved
Expand Down

0 comments on commit 83d3259

Please sign in to comment.