Merge pull request #20609 from DaanDeMeyer/recursive-template

core: Try to prevent infinite recursive template instantiation
This commit is contained in:
Lennart Poettering
2021-10-28 19:23:17 +02:00
committed by GitHub
9 changed files with 231 additions and 7 deletions
+8 -5
View File
@@ -598,9 +598,11 @@ int unit_file_find_fragment(
if (name_type < 0)
return name_type;
r = add_names(unit_ids_map, unit_name_map, unit_name, NULL, name_type, instance, &names, unit_name);
if (r < 0)
return r;
if (ret_names) {
r = add_names(unit_ids_map, unit_name_map, unit_name, NULL, name_type, instance, &names, unit_name);
if (r < 0)
return r;
}
/* First try to load fragment under the original name */
r = unit_ids_map_get(unit_ids_map, unit_name, &fragment);
@@ -619,7 +621,7 @@ int unit_file_find_fragment(
return log_debug_errno(r, "Cannot load template %s: %m", template);
}
if (fragment) {
if (fragment && ret_names) {
const char *fragment_basename = basename(fragment);
if (!streq(fragment_basename, unit_name)) {
@@ -631,7 +633,8 @@ int unit_file_find_fragment(
}
*ret_fragment_path = fragment;
*ret_names = TAKE_PTR(names);
if (ret_names)
*ret_names = TAKE_PTR(names);
return 0;
}
+24
View File
@@ -8,6 +8,7 @@
#include "alloc-util.h"
#include "glob-util.h"
#include "hexdecoct.h"
#include "memory-util.h"
#include "path-util.h"
#include "special.h"
#include "string-util.h"
@@ -797,3 +798,26 @@ bool slice_name_is_valid(const char *name) {
return true;
}
bool unit_name_prefix_equal(const char *a, const char *b) {
const char *p, *q;
assert(a);
assert(b);
if (!unit_name_is_valid(a, UNIT_NAME_ANY) || !unit_name_is_valid(b, UNIT_NAME_ANY))
return false;
p = strchr(a, '@');
if (!p)
p = strrchr(a, '.');
q = strchr(b, '@');
if (!q)
q = strrchr(b, '.');
assert(p);
assert(q);
return memcmp_nn(a, p - a, b, q - b) == 0;
}
+2
View File
@@ -62,3 +62,5 @@ static inline int unit_name_mangle(const char *name, UnitNameMangle flags, char
int slice_build_parent_slice(const char *slice, char **ret);
int slice_build_subslice(const char *slice, const char *name, char **subslice);
bool slice_name_is_valid(const char *name);
bool unit_name_prefix_equal(const char *a, const char *b);