Do not use JSON_ARRAY with sudo_json_add_value()

This commit is contained in:
Todd C. Miller
2020-03-29 05:05:08 -06:00
parent f24dacdee2
commit cffda82e20
7 changed files with 48 additions and 59 deletions

View File

@@ -37,7 +37,6 @@ struct json_value {
enum json_value_type type;
union {
const char *string;
char * const * array;
long long number;
id_t id;
bool boolean;

View File

@@ -932,6 +932,8 @@ iolog_write_info_file_json(int dfd, const char *parent, struct iolog_info *info)
bool ret = false;
FILE *fp = NULL;
int fd = -1;
size_t i;
char *cp;
debug_decl(iolog_write_info_file_json, SUDO_DEBUG_UTIL);
if (info->cmd == NULL || info->user == NULL || info->runas_user == NULL)
@@ -974,16 +976,28 @@ iolog_write_info_file_json(int dfd, const char *parent, struct iolog_info *info)
goto oom;
if (info->argv != NULL) {
json_value.type = JSON_ARRAY;
json_value.u.array = info->argv;
if (!sudo_json_add_value(&json, "runargv", &json_value))
if (!sudo_json_open_array(&json, "runargv"))
goto oom;
for (i = 0; (cp = info->argv[i]) != NULL; i++) {
json_value.type = JSON_STRING;
json_value.u.string = cp;
if (!sudo_json_add_value(&json, NULL, &json_value))
goto oom;
}
if (!sudo_json_close_array(&json))
goto oom;
}
if (info->envp != NULL) {
json_value.type = JSON_ARRAY;
json_value.u.array = info->envp;
if (!sudo_json_add_value(&json, "runenv", &json_value))
if (!sudo_json_open_array(&json, "runenv"))
goto oom;
for (i = 0; (cp = info->envp[i]) != NULL; i++) {
json_value.type = JSON_STRING;
json_value.u.string = cp;
if (!sudo_json_add_value(&json, NULL, &json_value))
goto oom;
}
if (!sudo_json_close_array(&json))
goto oom;
}

View File

@@ -305,7 +305,6 @@ sudo_json_add_value_int(struct json_container *json, const char *name,
struct json_value *value, bool as_object)
{
char numbuf[(((sizeof(long long) * 8) + 2) / 3) + 2];
unsigned int i;
debug_decl(sudo_json_add_value, SUDO_DEBUG_UTIL);
/* Add comma if we are continuing an object/array. */
@@ -356,41 +355,7 @@ sudo_json_add_value_int(struct json_container *json, const char *name,
debug_return_bool(false);
break;
case JSON_ARRAY:
if (value->u.array[0] == NULL || value->u.array[1] == NULL) {
if (!json_append_buf(json, "[ "))
debug_return_bool(false);
if (value->u.array[0] != NULL) {
if (!json_append_string(json, value->u.array[0]))
debug_return_bool(false);
if (!json_append_buf(json, " "))
debug_return_bool(false);
}
if (!json_append_buf(json, "]"))
debug_return_bool(false);
} else {
if (!json_append_buf(json, "["))
debug_return_bool(false);
if (!json_append_buf(json, json->compact ? " " : "\n"))
debug_return_bool(false);
json->indent_level += json->indent_increment;
for (i = 0; value->u.array[i] != NULL; i++) {
if (!json_append_indent(json, json->indent_level))
debug_return_bool(false);
if (!json_append_string(json, value->u.array[i]))
debug_return_bool(false);
if (value->u.array[i + 1] != NULL) {
if (!json_append_buf(json, ","))
debug_return_bool(false);
}
if (!json_append_buf(json, json->compact ? " " : "\n"))
debug_return_bool(false);
}
json->indent_level -= json->indent_increment;
if (!json_append_indent(json, json->indent_level))
debug_return_bool(false);
if (!json_append_buf(json, "]"))
debug_return_bool(false);
}
sudo_fatalx("internal error: can't print JSON_ARRAY");
break;
case JSON_OBJECT:
sudo_fatalx("internal error: can't print JSON_OBJECT");

View File

@@ -288,7 +288,6 @@ format_json(ClientMessage__TypeCase event_type,
struct json_container json = { 0 };
struct json_value json_value;
struct timespec ts;
char **strvec;
size_t idx;
debug_decl(format_json, SUDO_DEBUG_UTIL);
@@ -365,20 +364,22 @@ format_json(ClientMessage__TypeCase event_type,
if (!sudo_json_add_value(&json, info->key, &json_value))
goto bad;
break;
case INFO_MESSAGE__VALUE_STRLISTVAL:
/* Must convert to NULL-terminated string vector. */
strvec = strlist_copy(info->strlistval);
if (strvec == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO|SUDO_DEBUG_LINENO,
"%s: %s", __func__, "unable to allocate memory");
case INFO_MESSAGE__VALUE_STRLISTVAL: {
InfoMessage__StringList *strlist = info->strlistval;
size_t n;
if (!sudo_json_open_array(&json, info->key))
goto bad;
for (n = 0; n < strlist->n_strings; n++) {
json_value.type = JSON_STRING;
json_value.u.string = strlist->strings[n];
if (!sudo_json_add_value(&json, NULL, &json_value))
goto bad;
}
json_value.type = JSON_ARRAY;
json_value.u.array = strvec;
if (!sudo_json_add_value(&json, info->key, &json_value))
if (!sudo_json_close_array(&json))
goto bad;
free(strvec);
break;
}
default:
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unexpected value case %d", info->value_case);

View File

@@ -70,7 +70,7 @@ has_strlistval(InfoMessage *info)
* The input string list need not be NULL-terminated.
* Returns a NULL-terminated string vector.
*/
char **
static char **
strlist_copy(InfoMessage__StringList *strlist)
{
char **dst, **src = strlist->strings;

View File

@@ -188,7 +188,6 @@ int store_suspend(CommandSuspend *msg, struct connection_closure *closure);
int store_winsize(ChangeWindowSize *msg, struct connection_closure *closure);
void iolog_close_all(struct connection_closure *closure);
void iolog_details_free(struct iolog_details *details);
char ** strlist_copy(InfoMessage__StringList *strlist);
/* logsrvd_conf.c */
bool logsrvd_conf_read(const char *path);

View File

@@ -258,12 +258,23 @@ add_key_value(struct json_container *json, const char *str)
static bool
add_array(struct json_container *json, const char *name, char * const * array)
{
const char *cp;
struct json_value json_value;
debug_decl(add_array, SUDO_DEBUG_PLUGIN);
json_value.type = JSON_ARRAY;
json_value.u.array = array;
debug_return_bool(sudo_json_add_value(json, name, &json_value));
if (!sudo_json_open_array(json, name))
debug_return_bool(false);
while ((cp = *array) != NULL) {
json_value.type = JSON_STRING;
json_value.u.string = cp;
if (!sudo_json_add_value(json, name, &json_value))
debug_return_bool(false);
array++;
}
if (!sudo_json_close_array(json))
debug_return_bool(false);
debug_return_bool(true);
}
static bool