swtpm_setup: Handle case when returned profile Name is null

The profile '{"Name": null}' will not lead to a parser error but return
NULL for the 'Name'. Therefore, check for variable name being a NULL
pointer. Since the user may provide this type of profile this could have
lead to crashes when name was accessed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
This commit is contained in:
Stefan Berger 2024-11-13 14:56:26 -05:00 committed by Stefan Berger
parent e02bf61a22
commit fcda38b463
2 changed files with 7 additions and 1 deletions

View File

@ -97,8 +97,13 @@ int check_json_profile(const gchar *swtpm_capabilities_json, const char *json_pr
int ret;
ret = json_get_map_value(json_profile, "Name", &name);
if (ret)
/* { "Name": null } does not lead to parser failure but return name = NULL */
if (ret || name == NULL) {
ret = 1;
logerr(gl_LOGFILE, "Failed to get 'Name' from profile '%s'.\n",
json_profile);
return ret;
}
if (strlen(name) > 32) {
logerr(gl_LOGFILE, "Profile name must not exceed 32 characters.\n");

View File

@ -474,6 +474,7 @@ int json_get_map_value(const char *json_input, const char *field_name,
goto error_unref_jr;
}
*value = g_strdup(json_reader_get_string_value(jr));
/* caller should handle *value == NULL */
ret = 0;