Skip to content

Commit f96e844

Browse files
CathouwuNavtajh04
andauthored
Add cc1120 random number generator and stack smashing protection (#297)
# Purpose Created a number generator function from the c1120 to generate a stack canary for stack overflow protection: https://www.notion.so/uworbital/6375c5c5dcf1485e8a6c47f18d419ae5?v=72baf90d01f643b09d77418bd3f53b14&p=02e40c1185bb439d93fd92288137edcc&pm=s # New Changes - Added a random number generator function in the cc1120 file. - Modified state_mgr to initialize peripherals and cc1120. Changed stack guard after cc1120 is initialized. # Testing - Tested that the cc1120 can generate random numbers - Tested that the stack canary in app_main is updated with the random number. Stack canaries generated (generated after every reboot: ) ![image](https://github.com/user-attachments/assets/0b1fc378-5ae2-4f17-8e1d-c1110319a67f) # Outstanding Changes - n/a --------- Co-authored-by: Navtajhundal <87790825+Navtajh04@users.noreply.github.com>
1 parent 2701d36 commit f96e844

File tree

6 files changed

+84
-15
lines changed

6 files changed

+84
-15
lines changed

obc/app/app_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
#include <het.h>
2020

2121
// This is the stack canary. It should never be overwritten.
22-
// Ideally, it would be a random value, but we don't have a good source of entropy
23-
// that we can use.
22+
2423
void *__stack_chk_guard = (void *)0xDEADBEEF;
2524

25+
uint32_t __stack_chk_guard_init(void);
26+
2627
void __stack_chk_fail(void) { resetSystem(RESET_REASON_STACK_CHECK_FAIL); }
2728

2829
int main(void) {
@@ -33,7 +34,6 @@ int main(void) {
3334
spiInit();
3435
canInit();
3536
hetInit();
36-
3737
_enable_interrupt_();
3838

3939
// Initialize bus mutexes

obc/app/drivers/cc1120/cc1120.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#define CHIP_READY 0
1212
#define CHIP_STATE 0b1110000
1313

14+
#define CC1120_RND_CONFIG 0x80
15+
1416
static const register_setting_t cc1120SettingsStd[] = {
1517
// Set GPIO 0 to RXFIFO_THR_PKT
1618
{CC1120_REGS_IOCFG0, 0x01U},
@@ -52,14 +54,27 @@ static const register_setting_t cc1120SettingsStd[] = {
5254
{CC1120_REGS_PA_CFG0, 0x7DU},
5355
{CC1120_REGS_PKT_LEN, 0x0CU}};
5456

55-
static const register_setting_t cc1120SettingsExt[] = {
56-
{CC1120_REGS_EXT_IF_MIX_CFG, 0x00U}, {CC1120_REGS_EXT_FREQOFF_CFG, 0x34U}, {CC1120_REGS_EXT_FREQ2, 0x6CU},
57-
{CC1120_REGS_EXT_FREQ1, 0x7AU}, {CC1120_REGS_EXT_FREQ0, 0xE1U}, {CC1120_REGS_EXT_FS_DIG1, 0x00U},
58-
{CC1120_REGS_EXT_FS_DIG0, 0x5FU}, {CC1120_REGS_EXT_FS_CAL1, 0x40U}, {CC1120_REGS_EXT_FS_CAL0, 0x0EU},
59-
{CC1120_REGS_EXT_FS_DIVTWO, 0x03U}, {CC1120_REGS_EXT_FS_DSM0, 0x33U}, {CC1120_REGS_EXT_FS_DVC0, 0x17U},
60-
{CC1120_REGS_EXT_FS_PFD, 0x50U}, {CC1120_REGS_EXT_FS_PRE, 0x6EU}, {CC1120_REGS_EXT_FS_REG_DIV_CML, 0x14U},
61-
{CC1120_REGS_EXT_FS_SPARE, 0xACU}, {CC1120_REGS_EXT_FS_VCO0, 0xB4U}, {CC1120_REGS_EXT_XOSC5, 0x0EU},
62-
{CC1120_REGS_EXT_XOSC1, 0x03U}, {CC1120_REGS_EXT_TOC_CFG, 0x89U}};
57+
static const register_setting_t cc1120SettingsExt[] = {{CC1120_REGS_EXT_IF_MIX_CFG, 0x00U},
58+
{CC1120_REGS_EXT_FREQOFF_CFG, 0x34U},
59+
{CC1120_REGS_EXT_FREQ2, 0x6CU},
60+
{CC1120_REGS_EXT_FREQ1, 0x7AU},
61+
{CC1120_REGS_EXT_FREQ0, 0xE1U},
62+
{CC1120_REGS_EXT_FS_DIG1, 0x00U},
63+
{CC1120_REGS_EXT_FS_DIG0, 0x5FU},
64+
{CC1120_REGS_EXT_FS_CAL1, 0x40U},
65+
{CC1120_REGS_EXT_FS_CAL0, 0x0EU},
66+
{CC1120_REGS_EXT_FS_DIVTWO, 0x03U},
67+
{CC1120_REGS_EXT_FS_DSM0, 0x33U},
68+
{CC1120_REGS_EXT_FS_DVC0, 0x17U},
69+
{CC1120_REGS_EXT_FS_PFD, 0x50U},
70+
{CC1120_REGS_EXT_FS_PRE, 0x6EU},
71+
{CC1120_REGS_EXT_FS_REG_DIV_CML, 0x14U},
72+
{CC1120_REGS_EXT_FS_SPARE, 0xACU},
73+
{CC1120_REGS_EXT_FS_VCO0, 0xB4U},
74+
{CC1120_REGS_EXT_XOSC5, 0x0EU},
75+
{CC1120_REGS_EXT_XOSC1, 0x03U},
76+
{CC1120_REGS_EXT_TOC_CFG, 0x89U},
77+
{CC1120_REGS_EXT_RNDGEN, CC1120_RND_CONFIG}};
6378

6479
/**
6580
* @brief - Reads from consecutive registers from the CC1120.
@@ -445,3 +460,17 @@ obc_error_code_t cc1120GetBytesInRxFifo(uint8_t *numBytes) {
445460
RETURN_IF_ERROR_CODE(cc1120ReadExtAddrSpi(CC1120_REGS_EXT_NUM_RXBYTES, numBytes, 1));
446461
return OBC_ERR_CODE_SUCCESS;
447462
}
463+
464+
obc_error_code_t cc1120Rng(uint8_t *randomValue) {
465+
obc_error_code_t errCode;
466+
uint8_t receivedData;
467+
if (randomValue == NULL) {
468+
return OBC_ERR_CODE_INVALID_ARG;
469+
}
470+
RETURN_IF_ERROR_CODE(cc1120StrobeSpi(CC1120_STROBE_SRX));
471+
RETURN_IF_ERROR_CODE(cc1120ReadExtAddrSpi(CC1120_REGS_EXT_RNDGEN, randomValue, 1));
472+
RETURN_IF_ERROR_CODE(cc1120ReadFifo(&receivedData, 1));
473+
(*randomValue) &= ~(1 << 7);
474+
(*randomValue) ^= receivedData;
475+
return OBC_ERR_CODE_SUCCESS;
476+
}

obc/app/drivers/cc1120/cc1120.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,12 @@ obc_error_code_t cc1120GetState(cc1120_state_t *stateNum);
171171
* @return obc_error_code_t - Whether or not the setup was a success
172172
*/
173173
obc_error_code_t cc1120Init(void);
174+
175+
/**
176+
* @brief Returns a random number generated by CC1120
177+
* #Check section 3.2 RNDGEN for more information
178+
*
179+
* @param randomValue- A pointer to an 8-bit integer to store the random value in
180+
* @return obc_error_code_t - Whether or not the register read was successful
181+
*/
182+
obc_error_code_t cc1120Rng(uint8_t *randomValue);

obc/app/modules/comms_link_mgr/comms_manager.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,6 @@ void obcTaskFunctionCommsMgr(void *pvParameters) {
295295

296296
initAllCc1120TxRxSemaphores();
297297

298-
#ifdef CONFIG_CC1120
299-
LOG_IF_ERROR_CODE(cc1120Init());
300-
#endif // CONFIG_CC1120
301298
while (1) {
302299
comms_event_t queueMsg;
303300

obc/app/modules/state_mgr/state_mgr.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "state_mgr.h"
22
#include "comms_manager.h" // for comms_state_t
3-
43
#include "obc_board_config.h"
54
#include "obc_errors.h"
65
#include "obc_logging.h"
@@ -10,6 +9,9 @@
109

1110
#include "fm25v20a.h"
1211
#include "lm75bd.h" // TODO: Handle within thermal manager
12+
#include "cc1120_txrx.h"
13+
#include "cc1120.h"
14+
#include "arducam.h"
1315

1416
#include <FreeRTOS.h>
1517
#include <os_portmacro.h>
@@ -21,6 +23,9 @@
2123
#include <gio.h>
2224
#endif
2325

26+
extern void *__stack_chk_guard;
27+
#define STACK_CANARY_BYTES 4
28+
2429
/* Supervisor queue config */
2530
#define STATE_MGR_QUEUE_LENGTH 10U
2631
#define STATE_MGR_QUEUE_ITEM_SIZE sizeof(state_mgr_event_t)
@@ -59,6 +64,27 @@ obc_error_code_t sendToStateMgrEventQueue(state_mgr_event_t *event) {
5964

6065
static void sendStartupMessages(void) {}
6166

67+
uint32_t stack_chk_guard_change(void) {
68+
obc_error_code_t errCode;
69+
uint32_t newStackGuard = 0;
70+
for (uint8_t i = 0; i < STACK_CANARY_BYTES; i++) {
71+
uint8_t randomByte;
72+
LOG_IF_ERROR_CODE(cc1120Rng(&randomByte));
73+
if (errCode == OBC_ERR_CODE_SUCCESS) {
74+
(newStackGuard) = (newStackGuard << 8) | randomByte;
75+
} else {
76+
return 0xDEADBEEF;
77+
}
78+
}
79+
return newStackGuard;
80+
}
81+
82+
static inline void __attribute__((no_stack_protector)) construct_stk_chk_guard() {
83+
if (__stack_chk_guard == (void *)0xDEADBEEF) {
84+
__stack_chk_guard = (void *)stack_chk_guard_change();
85+
}
86+
}
87+
6288
void obcTaskFunctionStateMgr(void *pvParameters) {
6389
obc_error_code_t errCode;
6490

@@ -75,6 +101,12 @@ void obcTaskFunctionStateMgr(void *pvParameters) {
75101
#ifdef CONFIG_DS3232
76102
LOG_IF_ERROR_CODE(initTime()); // RTC
77103
#endif // CONFIG_DS3232
104+
#ifdef CONFIG_CC1120
105+
LOG_IF_ERROR_CODE(cc1120Init());
106+
construct_stk_chk_guard();
107+
#endif
108+
109+
// TODO add other peripherals
78110

79111
lm75bd_config_t config = {
80112
.devAddr = LM75BD_OBC_I2C_ADDR,

obc/app/sys/obc_errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef enum {
2424
OBC_ERR_CODE_FAILED_PACK = 16,
2525
OBC_ERR_CODE_INVALID_STATE_TRANSITION = 17,
2626
OBC_ERR_CODE_FREERTOS_ASSERT_FAIL = 18,
27+
OBC_ERR_CODE_FAILED_STACK_CANARY = 19,
2728

2829
/* Driver Errors 100 - 199*/
2930
OBC_ERR_CODE_SPI_FAILURE = 100,
@@ -191,4 +192,5 @@ typedef enum {
191192

192193
/** Operation is not supported. */
193194
OBC_ERR_CODE_RED_ENOTSUPP = 1524,
195+
194196
} obc_error_code_t;

0 commit comments

Comments
 (0)