Skip to content

Commit d2b3452

Browse files
committed
fix(tpm2_attr_util): tighten token parsing in handle_dispatch
Prevents false matches like (name = "foo" vs. token = "foobar...") and spurious dispatch_err during table iteration. - Inline token_match() into handle_dispatch() - no-arg tokens require exact strcmp(token, name) match - arg tokens must be in "name=arg" form, which avoid prefix matches - Split token on '=' only after a confirmed match; keep non-matching entries from failing Signed-off-by: Takuma IMAMURA <209989118+hyperfinitism@users.noreply.github.com>
1 parent 29e23a6 commit d2b3452

1 file changed

Lines changed: 25 additions & 32 deletions

File tree

lib/tpm2_attr_util.c

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -381,21 +381,6 @@ static dispatch_table obj_attr_table[] = { // Bit Index
381381
dispatch_reserved(31), // 31
382382
};
383383

384-
static bool token_match(const char *name, const char *token, bool has_arg,
385-
char **sep) {
386-
387-
/* if it has an argument, we expect a separator */
388-
size_t match_len = strlen(token);
389-
if (has_arg) {
390-
*sep = strchr(token, '=');
391-
if (*sep) {
392-
match_len = *sep - token;
393-
}
394-
}
395-
396-
return !strncmp(name, token, match_len);
397-
}
398-
399384
static dispatch_error handle_dispatch(dispatch_table *d, char *token,
400385
TPMA_NV *nvattrs) {
401386

@@ -408,32 +393,40 @@ static dispatch_error handle_dispatch(dispatch_table *d, char *token,
408393
return dispatch_no_match;
409394
}
410395

411-
char *sep = NULL;
412-
bool match = token_match(name, token, has_arg, &sep);
413-
if (!match) {
414-
return dispatch_no_match;
415-
}
416-
417-
/*
418-
* If it has an argument, match should have found the separator.
419-
*/
420396
char *arg = NULL;
421-
if (has_arg) {
422-
if (!sep) {
397+
398+
if (!has_arg) {
399+
if (strcmp(token, name) != 0) {
400+
return dispatch_no_match;
401+
}
402+
} else {
403+
size_t name_len = strlen(name);
404+
405+
/* check if name is a prefix of token */
406+
if (strncmp(token, name, name_len) != 0) {
407+
return dispatch_no_match;
408+
}
409+
410+
/* character = is not found despite has_arg is true */
411+
if (!token[name_len]) {
423412
LOG_ERR("Expected argument for \"%s\", got none.", token);
424413
return dispatch_err;
425414
}
415+
/* name does not match exactly; it is just a prefix */
416+
if (token[name_len] != '=') {
417+
return dispatch_no_match;
418+
}
419+
/* otherwise name exactly matches */
426420

427-
/* split token on = */
428-
*sep = '\0';
429-
sep++;
430-
if (!*sep) {
421+
/* check if the argument is non-null */
422+
arg = token + name_len + 1;
423+
if (!*arg) {
431424
LOG_ERR("Expected argument for \"%s\", got none.", token);
432425
return dispatch_err;
433426
}
434427

435-
/* valid argument string, assign */
436-
arg = sep;
428+
/* split token on = */
429+
token[name_len] = '\0';
437430
}
438431

439432
bool result = cb(nvattrs, arg);

0 commit comments

Comments
 (0)