From: Yu Watanabe Date: Tue, 15 May 2018 13:48:58 +0000 (+0900) Subject: hostname: use free_and_strdup() X-Git-Tag: v239~249^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c650f207218845080d1edd79cd439541fa68ddab;p=thirdparty%2Fsystemd.git hostname: use free_and_strdup() Also, this makes hostnamed check the input strings before bus_verify_polkit_async(). --- diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index f3deece5b0b..88a8938c311 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -399,7 +399,6 @@ static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error * Context *c = userdata; const char *name; int interactive; - char *h; int r; assert(m); @@ -435,12 +434,9 @@ static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error * if (r == 0) return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ - h = strdup(name); - if (!h) - return -ENOMEM; - - free(c->data[PROP_HOSTNAME]); - c->data[PROP_HOSTNAME] = h; + r = free_and_strdup(&c->data[PROP_HOSTNAME], name); + if (r < 0) + return r; r = context_update_kernel_hostname(c); if (r < 0) { @@ -473,6 +469,9 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_ if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME])) return sd_bus_reply_method_return(m, NULL); + if (!isempty(name) && !hostname_is_valid(name, false)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name); + r = bus_verify_polkit_async( m, CAP_SYS_ADMIN, @@ -487,21 +486,9 @@ static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_ if (r == 0) return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ - if (isempty(name)) - c->data[PROP_STATIC_HOSTNAME] = mfree(c->data[PROP_STATIC_HOSTNAME]); - else { - char *h; - - if (!hostname_is_valid(name, false)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name); - - h = strdup(name); - if (!h) - return -ENOMEM; - - free(c->data[PROP_STATIC_HOSTNAME]); - c->data[PROP_STATIC_HOSTNAME] = h; - } + r = free_and_strdup(&c->data[PROP_STATIC_HOSTNAME], name); + if (r < 0) + return r; r = context_update_kernel_hostname(c); if (r < 0) { @@ -539,6 +526,22 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess if (streq_ptr(name, c->data[prop])) return sd_bus_reply_method_return(m, NULL); + if (!isempty(name)) { + /* The icon name might ultimately be used as file + * name, so better be safe than sorry */ + + if (prop == PROP_ICON_NAME && !filename_is_valid(name)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name); + if (prop == PROP_PRETTY_HOSTNAME && string_has_cc(name, NULL)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name); + if (prop == PROP_CHASSIS && !valid_chassis(name)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name); + if (prop == PROP_DEPLOYMENT && !valid_deployment(name)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid deployment '%s'", name); + if (prop == PROP_LOCATION && string_has_cc(name, NULL)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid location '%s'", name); + } + /* Since the pretty hostname should always be changed at the * same time as the static one, use the same policy action for * both... */ @@ -557,32 +560,9 @@ static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_mess if (r == 0) return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ - if (isempty(name)) - c->data[prop] = mfree(c->data[prop]); - else { - char *h; - - /* The icon name might ultimately be used as file - * name, so better be safe than sorry */ - - if (prop == PROP_ICON_NAME && !filename_is_valid(name)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name); - if (prop == PROP_PRETTY_HOSTNAME && string_has_cc(name, NULL)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name); - if (prop == PROP_CHASSIS && !valid_chassis(name)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name); - if (prop == PROP_DEPLOYMENT && !valid_deployment(name)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid deployment '%s'", name); - if (prop == PROP_LOCATION && string_has_cc(name, NULL)) - return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid location '%s'", name); - - h = strdup(name); - if (!h) - return -ENOMEM; - - free(c->data[prop]); - c->data[prop] = h; - } + r = free_and_strdup(&c->data[prop], name); + if (r < 0) + return r; r = context_write_data_machine_info(c); if (r < 0) {