Skip to content

Commit 4a9042a

Browse files
authored
fix(secret-provider-agent): improve robustness of provider agent (#43)
* fix: add NULL check to adppId * fix: add control on ip address and port * fix: enforce const on immutable values * fix: enforce const on immutable values * fix: replace strtok with strchr to avoid modifying sbsEndpoint variable
1 parent 3c08df4 commit 4a9042a

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

cvmassistants/secretprovider/secret-provider-agent/src/secret_provider_agent.c

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
do { \
2121
if (log_level <= rats_level) { \
2222
time_t now = time(NULL); \
23-
struct tm *t = gmtime(&now); \
23+
const struct tm *t = gmtime(&now); \
2424
char ts[24]; \
2525
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S UTC", t); \
2626
printf("%-29s [%-5s] [%s:%d] " fmt "\n", ts, level, __FILE__, __LINE__, ##__VA_ARGS__); \
@@ -44,14 +44,14 @@ rats_tls_log_level_t log_level = RATS_TLS_LOG_LEVEL_INFO;
4444
const char* command_get_secret = "getSecret";
4545

4646
char* get_secret_from_sbs_through_rats_tls(rats_tls_log_level_t log_level,
47-
char* attester_type,
48-
char* verifier_type,
49-
char* tls_type,
50-
char* crypto_type,
47+
const char* attester_type,
48+
const char* verifier_type,
49+
const char* tls_type,
50+
const char* crypto_type,
5151
bool mutual,
52-
char* ip,
52+
const char* ip,
5353
int port,
54-
char* app_id) {
54+
const char* app_id) {
5555

5656
bool validation_error = false;
5757
if (attester_type == NULL || strlen(attester_type) >= ENCLAVE_ATTESTER_TYPE_NAME_SIZE) {
@@ -223,10 +223,10 @@ int main(int argc, char** argv) {
223223
char* secret = "";
224224
LOG_INFO("Try to get key from SBS");
225225

226-
char* secret_save_path = NULL;
227-
char* sbs_endpoint = NULL;
228-
char* srv_ip = NULL;
229-
char* str_port = NULL;
226+
const char* secret_save_path = NULL;
227+
const char* sbs_endpoint = NULL;
228+
char ip_buf[INET_ADDRSTRLEN];
229+
const char* str_port = NULL;
230230
int port;
231231

232232
char* const short_options = "a:v:t:c:ml:s:i:e:h";
@@ -243,12 +243,12 @@ int main(int argc, char** argv) {
243243
{"help", no_argument, NULL, 'h'},
244244
{0, 0, 0, 0}};
245245

246-
char* attester_type = "";
247-
char* verifier_type = "";
248-
char* tls_type = "";
249-
char* crypto_type = "";
246+
const char* attester_type = "";
247+
const char* verifier_type = "";
248+
const char* tls_type = "";
249+
const char* crypto_type = "";
250250
bool mutual = true;
251-
char* app_id = NULL;
251+
const char* app_id = NULL;
252252
int opt;
253253
do {
254254
opt = getopt_long(argc, argv, short_options, long_options, NULL);
@@ -317,22 +317,51 @@ int main(int argc, char** argv) {
317317

318318
LOG_INFO("Selected log level %d", log_level);
319319

320+
if (app_id == NULL) {
321+
LOG_ERROR("App ID is missing");
322+
return -1;
323+
}
324+
320325
if (sbs_endpoint == NULL) {
321326
LOG_ERROR("SBS mode must provide sbsEndpoint argument (--sbsEndpoint/-e)");
322327
return -1;
323328
}
324329

325330
LOG_DEBUG("Config of SBS endpoint is %s", sbs_endpoint);
326331

327-
srv_ip = strtok(sbs_endpoint, ":");
328-
str_port = strtok(NULL, ":");
329-
if (NULL == str_port) {
330-
LOG_ERROR("sbsEndpoint format error, eg: 127.0.0.1:5443");
332+
const char* colon = strchr(sbs_endpoint, ':');
333+
if (colon == NULL) {
334+
LOG_ERROR("sbsEndpoint format error: missing ':', eg: 127.0.0.1:5443");
335+
return -1;
336+
}
337+
338+
size_t ip_len = colon - sbs_endpoint;
339+
if (ip_len == 0) {
340+
LOG_ERROR("sbsEndpoint format error: missing IP address");
341+
return -1;
342+
}
343+
if (ip_len >= INET_ADDRSTRLEN) {
344+
LOG_ERROR("sbsEndpoint format error: IP address too long");
345+
return -1;
346+
}
347+
348+
memcpy(ip_buf, sbs_endpoint, ip_len);
349+
ip_buf[ip_len] = '\0';
350+
351+
struct in_addr test_addr;
352+
if (inet_pton(AF_INET, ip_buf, &test_addr) != 1) {
353+
LOG_ERROR("Invalid IP address format: %s", ip_buf);
354+
return -1;
355+
}
356+
357+
str_port = colon + 1;
358+
if (*str_port == '\0') {
359+
LOG_ERROR("sbsEndpoint format error: missing port, eg: 5443");
331360
return -1;
332361
}
333362
port = atoi(str_port);
334-
if (port == 0) {
335-
LOG_ERROR("Port is invalid, got %s", str_port);
363+
if (port <= 0 || port > 65535) {
364+
LOG_ERROR("Port is invalid or out of valid range (1-65535), got %d", port);
336365
return -1;
337366
}
338367

@@ -347,7 +376,7 @@ int main(int argc, char** argv) {
347376
}
348377

349378
secret = get_secret_from_sbs_through_rats_tls(log_level, attester_type, verifier_type,
350-
tls_type, crypto_type, mutual, srv_ip,
379+
tls_type, crypto_type, mutual, ip_buf,
351380
port, app_id);
352381
if (secret == NULL) {
353382
LOG_ERROR("Get secret from SBS failed");

0 commit comments

Comments
 (0)