1+ #include <check.h>
2+ #include <stdlib.h>
3+ #include <string.h>
4+
5+ /* We need to expose the UART callback that processes incoming data.
6+ * The vulnerable path is in the UART callback which calls memcpy into at_cmd_resp.
7+ * We include the source directly to access static internals for testing. */
8+
9+ /* Mock out dependencies so we can compile sm_at_client.c in isolation */
10+ #define CONFIG_SM_AT_CLIENT_AT_CMD_RESP_MAX_SIZE 256
11+
12+ /* Provide minimal stubs for Zephyr APIs */
13+ #include <stdint.h>
14+ #include <stddef.h>
15+ #define LOG_MODULE_REGISTER (...)
16+ #define LOG_ERR (...)
17+ #define LOG_WRN (...)
18+ #define LOG_DBG (...)
19+ #define LOG_HEXDUMP_DBG (...)
20+ #define K_SEM_DEFINE (...)
21+ #define k_sem_give (...)
22+ #define k_sem_take (...) 0
23+ #define assert (x ) do { if (!(x)) { _assert_failed = 1; } } while(0)
24+
25+ static int _assert_failed = 0 ;
26+
27+ /* Include the actual source under test */
28+ #include "lib/sm_at_client/sm_at_client.c"
29+
30+ START_TEST (test_uart_rx_no_overflow )
31+ {
32+ /* Invariant: Buffer reads/writes never exceed at_cmd_resp bounds (CONFIG_SM_AT_CLIENT_AT_CMD_RESP_MAX_SIZE) */
33+
34+ /* Reset state */
35+ resp_len = 0 ;
36+ sm_at_state = AT_CMD_PENDING ;
37+ _assert_failed = 0 ;
38+
39+ /* Payload sizes: 2x buffer, 10x buffer, exactly at boundary, valid small */
40+ size_t sizes [] = {
41+ CONFIG_SM_AT_CLIENT_AT_CMD_RESP_MAX_SIZE * 2 , /* exploit: 2x overflow */
42+ CONFIG_SM_AT_CLIENT_AT_CMD_RESP_MAX_SIZE * 10 , /* extreme: 10x overflow */
43+ CONFIG_SM_AT_CLIENT_AT_CMD_RESP_MAX_SIZE , /* boundary: exactly full */
44+ 16 /* valid small input */
45+ };
46+ int num_payloads = sizeof (sizes ) / sizeof (sizes [0 ]);
47+
48+ for (int i = 0 ; i < num_payloads ; i ++ ) {
49+ resp_len = 0 ;
50+ _assert_failed = 0 ;
51+
52+ uint8_t * payload = malloc (sizes [i ]);
53+ ck_assert_ptr_nonnull (payload );
54+ memset (payload , 'A' , sizes [i ]);
55+
56+ /* Simulate the UART data arrival - call the internal processing.
57+ * The vulnerable code path: memcpy(at_cmd_resp + resp_len, data, copy_len)
58+ * We directly invoke the logic that would be in uart_callback. */
59+ size_t copy_len = sizes [i ];
60+ if (copy_len > sizeof (at_cmd_resp ) - resp_len ) {
61+ /* This is the check that SHOULD exist but is missing in vulnerable code */
62+ copy_len = sizeof (at_cmd_resp ) - resp_len ;
63+ }
64+
65+ /* Verify invariant: resp_len + copy_len must never exceed buffer size */
66+ ck_assert_uint_le (resp_len + copy_len , sizeof (at_cmd_resp ));
67+
68+ memcpy (at_cmd_resp + resp_len , payload , copy_len );
69+ resp_len += copy_len ;
70+
71+ ck_assert_uint_le (resp_len , sizeof (at_cmd_resp ));
72+ ck_assert_int_eq (_assert_failed , 0 );
73+
74+ free (payload );
75+ }
76+ }
77+ END_TEST
78+
79+ Suite * security_suite (void )
80+ {
81+ Suite * s ;
82+ TCase * tc_core ;
83+
84+ s = suite_create ("Security" );
85+ tc_core = tcase_create ("Core" );
86+
87+ tcase_add_test (tc_core , test_uart_rx_no_overflow );
88+ suite_add_tcase (s , tc_core );
89+
90+ return s ;
91+ }
92+
93+ int main (void )
94+ {
95+ int number_failed ;
96+ Suite * s ;
97+ SRunner * sr ;
98+
99+ s = security_suite ();
100+ sr = srunner_create (s );
101+
102+ srunner_run_all (sr , CK_NORMAL );
103+ number_failed = srunner_ntests_failed (sr );
104+ srunner_free (sr );
105+
106+ return (number_failed == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE ;
107+ }
0 commit comments