Skip to content

Commit 4fe78de

Browse files
authored
fix: reject invalid runtime variable names (#27)
fix: validate runtime variable names
1 parent 5a5bf8c commit 4fe78de

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

core/runtime_services.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,22 @@ int eos_rtsvc_get_variable(const char *name, void *data, uint32_t *size)
5454
return EOS_OK;
5555
}
5656

57-
int eos_rtsvc_set_variable(const char *name, const void *data, uint32_t size)
58-
{
59-
if (!name || !data || size == 0) return EOS_ERR_INVALID;
60-
if (!rtsvc_initialized) return EOS_ERR_GENERIC;
61-
if (size > EOS_RTSVC_MAX_VAR_SIZE) return EOS_ERR_FULL;
62-
63-
eos_runtime_var_t *var = find_var(name);
57+
int eos_rtsvc_set_variable(const char *name, const void *data, uint32_t size)
58+
{
59+
if (!name || !data || size == 0) return EOS_ERR_INVALID;
60+
if (!rtsvc_initialized) return EOS_ERR_GENERIC;
61+
if (size > EOS_RTSVC_MAX_VAR_SIZE) return EOS_ERR_FULL;
62+
63+
size_t name_len = strlen(name);
64+
if (name_len == 0 || name_len >= EOS_RTSVC_MAX_VAR_NAME)
65+
return EOS_ERR_INVALID;
66+
67+
eos_runtime_var_t *var = find_var(name);
6468
if (!var) {
6569
if (var_count >= EOS_RTSVC_MAX_VARS) return EOS_ERR_FULL;
6670
var = &var_store[var_count++];
67-
size_t nlen = strlen(name);
68-
if (nlen >= EOS_RTSVC_MAX_VAR_NAME) nlen = EOS_RTSVC_MAX_VAR_NAME - 1;
69-
memcpy(var->name, name, nlen);
70-
var->name[nlen] = '\0';
71+
memcpy(var->name, name, name_len);
72+
var->name[name_len] = '\0';
7173
}
7274

7375
memcpy(var->data, data, size);

include/eos_runtime_svc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int eos_rtsvc_get_variable(const char *name, void *data, uint32_t *size);
4545

4646
/**
4747
* Set a runtime variable.
48+
* Names must contain 1 to EOS_RTSVC_MAX_VAR_NAME - 1 characters.
4849
*/
4950
int eos_rtsvc_set_variable(const char *name, const void *data, uint32_t size);
5051

tests/unit/test_runtime_svc.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ TEST(test_string_variable)
9494
ASSERT(strcmp(out, "hello boot") == 0);
9595
}
9696

97+
TEST(test_reject_invalid_variable_names)
98+
{
99+
uint32_t value = 42;
100+
char long_name[EOS_RTSVC_MAX_VAR_NAME + 1];
101+
memset(long_name, 'a', sizeof(long_name));
102+
long_name[sizeof(long_name) - 1] = '\0';
103+
104+
ASSERT(eos_rtsvc_set_variable("", &value, sizeof(value)) == EOS_ERR_INVALID);
105+
ASSERT(eos_rtsvc_set_variable(long_name, &value, sizeof(value)) == EOS_ERR_INVALID);
106+
107+
/* Invalid names must not consume slots in the fixed-size variable store. */
108+
for (int i = 0; i < EOS_RTSVC_MAX_VARS; i++) {
109+
char name[EOS_RTSVC_MAX_VAR_NAME];
110+
snprintf(name, sizeof(name), "valid-%d", i);
111+
ASSERT(eos_rtsvc_set_variable(name, &value, sizeof(value)) == EOS_OK);
112+
}
113+
}
114+
97115
TEST(test_next_boot_slot)
98116
{
99117
ASSERT(eos_rtsvc_get_next_boot() == EOS_SLOT_NONE);
@@ -118,10 +136,11 @@ int main(void)
118136
run_test_get_nonexistent();
119137
run_test_null_args();
120138
run_test_string_variable();
139+
run_test_reject_invalid_variable_names();
121140
run_test_next_boot_slot();
122141
run_test_time();
123142

124-
tests_run = 8;
143+
tests_run = 9;
125144
printf("\n%d/%d tests passed\n", tests_passed, tests_run);
126145
return (tests_passed == tests_run) ? 0 : 1;
127146
}

0 commit comments

Comments
 (0)