From 93c474725c0fb2530f093c106de0bce956544d29 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 5 May 2015 13:39:14 -0700 Subject: core: be more strict when manipulating slices names and unescaping paths from unit names Let's better be safe then sorry. --- src/core/slice.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'src/core/slice.c') diff --git a/src/core/slice.c b/src/core/slice.c index 0bebdbcbc6..b965850dab 100644 --- a/src/core/slice.c +++ b/src/core/slice.c @@ -93,27 +93,28 @@ static int slice_add_default_dependencies(Slice *s) { return 0; } + static int slice_verify(Slice *s) { + _cleanup_free_ char *parent = NULL; + int r; + assert(s); if (UNIT(s)->load_state != UNIT_LOADED) return 0; - if (UNIT_DEREF(UNIT(s)->slice)) { - char *a, *dash; + if (!slice_name_is_valid(UNIT(s)->id)) { + log_unit_error(UNIT(s)->id, "Slice name %s is not valid. Refusing.", UNIT(s)->id); + return -EINVAL; + } - a = strdupa(UNIT(s)->id); - dash = strrchr(a, '-'); - if (dash) - strcpy(dash, ".slice"); - else - a = (char*) SPECIAL_ROOT_SLICE; + r = slice_build_parent_slice(UNIT(s)->id, &parent); + if (r < 0) + return log_unit_error_errno(UNIT(s)->id, r, "Failed to determine parent slice: %m"); - if (!unit_has_name(UNIT_DEREF(UNIT(s)->slice), a)) { - log_unit_error(UNIT(s)->id, - "%s located outside its parent slice. Refusing.", UNIT(s)->id); - return -EINVAL; - } + if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) { + log_unit_error(UNIT(s)->id, "%s located outside its parent slice. Refusing.", UNIT(s)->id); + return -EINVAL; } return 0; -- cgit v1.2.3-54-g00ecf