diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-04-05 18:01:27 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-05-24 17:01:57 +0200 |
commit | 4756c94e5ee4d5bb868ded9a9666e5ea0c4ba7f8 (patch) | |
tree | c01a4c7d3281b8c19716a6867e488984d9f6fbb5 | |
parent | 1bf36bf9efafea1ee637e77dd9646c981a325aac (diff) | |
download | systemd-4756c94e5ee4d5bb868ded9a9666e5ea0c4ba7f8.tar.gz systemd-4756c94e5ee4d5bb868ded9a9666e5ea0c4ba7f8.tar.bz2 systemd-4756c94e5ee4d5bb868ded9a9666e5ea0c4ba7f8.zip |
machine-image: validate pretty names when auto-generating them
-rw-r--r-- | src/shared/machine-image.c | 62 |
1 files changed, 55 insertions, 7 deletions
diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c index d73719e1dd..853521e6d6 100644 --- a/src/shared/machine-image.c +++ b/src/shared/machine-image.c @@ -157,6 +157,38 @@ static int image_new( return 0; } +static int extract_pretty(const char *path, const char *suffix, char **ret) { + _cleanup_free_ char *name = NULL; + const char *p; + size_t n; + + assert(path); + assert(ret); + + p = last_path_component(path); + n = strcspn(p, "/"); + + name = strndup(p, n); + if (!name) + return -ENOMEM; + + if (suffix) { + char *e; + + e = endswith(name, suffix); + if (!e) + return -EINVAL; + + *e = 0; + } + + if (!image_name_is_valid(name)) + return -EINVAL; + + *ret = TAKE_PTR(name); + return 0; +} + static int image_make( const char *pretty, int dfd, @@ -164,6 +196,7 @@ static int image_make( const char *filename, Image **ret) { + _cleanup_free_ char *pretty_buffer = NULL; struct stat st; bool read_only; int r; @@ -187,8 +220,13 @@ static int image_make( if (!ret) return 1; - if (!pretty) - pretty = filename; + if (!pretty) { + r = extract_pretty(filename, NULL, &pretty_buffer); + if (r < 0) + return r; + + pretty = pretty_buffer; + } fd = openat(dfd, filename, O_CLOEXEC|O_NOCTTY|O_DIRECTORY); if (fd < 0) @@ -264,10 +302,15 @@ static int image_make( if (!ret) return 1; - fd_getcrtime_at(dfd, filename, &crtime, 0); + (void) fd_getcrtime_at(dfd, filename, &crtime, 0); - if (!pretty) - pretty = strndupa(filename, strlen(filename) - 4); + if (!pretty) { + r = extract_pretty(filename, ".raw", &pretty_buffer); + if (r < 0) + return r; + + pretty = pretty_buffer; + } r = image_new(IMAGE_RAW, pretty, @@ -294,8 +337,13 @@ static int image_make( if (!ret) return 1; - if (!pretty) - pretty = filename; + if (!pretty) { + r = extract_pretty(filename, NULL, &pretty_buffer); + if (r < 0) + return r; + + pretty = pretty_buffer; + } block_fd = openat(dfd, filename, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); if (block_fd < 0) |