systemd/0003-Always-check-asprintf-return-code.patch

224 lines
9.9 KiB
Diff

Based on 7de80bfe2e61d5818601ccfddbadad3b7703ed70 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Fri, 25 Jul 2014 15:38:31 +0200
Subject: [PATCH] Always check asprintf return code
There is a small number of the places in sources where we don't check
asprintf() return code and assume that after error the function
returns NULL pointer via the first argument. That's wrong, after
error the content of pointer is undefined.
---
src/core/unit-printf.c | 8 ++++----
src/cryptsetup/cryptsetup.c | 11 ++++++++---
src/journal/journalctl.c | 16 +++++++++++-----
src/run/run.c | 20 ++++++++++----------
src/shared/install.c | 15 +++++++++------
src/systemctl/systemctl.c | 18 +++++++++---------
src/tty-ask-password-agent/tty-ask-password-agent.c | 5 +++--
7 files changed, 54 insertions(+), 39 deletions(-)
--- src/core/unit-printf.c
+++ src/core/unit-printf.c 2014-07-28 09:42:20.726235696 +0000
@@ -182,7 +182,7 @@ static int specifier_user_name(char spec
char *printed = NULL;
Unit *u = userdata;
ExecContext *c;
- int r;
+ int r = 0;
assert(u);
@@ -208,7 +208,7 @@ static int specifier_user_name(char spec
if (r < 0)
return -ENODATA;
- asprintf(&printed, "%lu", (unsigned long) uid);
+ r = asprintf(&printed, "%lu", (unsigned long) uid);
}
}
@@ -231,10 +231,10 @@ static int specifier_user_name(char spec
if (specifier == 'u')
printed = strdup(username);
else
- asprintf(&printed, "%lu", (unsigned long) uid);
+ r = asprintf(&printed, "%lu", (unsigned long) uid);
}
- if (!printed)
+ if (r < 0 || !printed)
return -ENOMEM;
*ret = printed;
--- src/cryptsetup/cryptsetup.c
+++ src/cryptsetup/cryptsetup.c 2014-07-28 00:00:00.000000000 +0000
@@ -535,13 +535,18 @@ int main(int argc, char *argv[]) {
description = NULL;
}
+ k = 0;
if (mount_point && description)
- asprintf(&name_buffer, "%s (%s) on %s", description, argv[2], mount_point);
+ k = asprintf(&name_buffer, "%s (%s) on %s", description, argv[2], mount_point);
else if (mount_point)
- asprintf(&name_buffer, "%s on %s", argv[2], mount_point);
+ k = asprintf(&name_buffer, "%s on %s", argv[2], mount_point);
else if (description)
- asprintf(&name_buffer, "%s (%s)", description, argv[2]);
+ k = asprintf(&name_buffer, "%s (%s)", description, argv[2]);
+ if (k < 0) {
+ log_oom();
+ goto finish;
+ }
name = name_buffer ? name_buffer : argv[2];
k = crypt_init(&cd, argv[3]);
--- src/journal/journalctl.c
+++ src/journal/journalctl.c 2014-07-28 00:00:00.000000000 +0000
@@ -746,11 +746,17 @@ static int add_matches(sd_journal *j, ch
}
} else
t = strappend("_EXE=", path);
- } else if (S_ISCHR(st.st_mode))
- asprintf(&t, "_KERNEL_DEVICE=c%u:%u", major(st.st_rdev), minor(st.st_rdev));
- else if (S_ISBLK(st.st_mode))
- asprintf(&t, "_KERNEL_DEVICE=b%u:%u", major(st.st_rdev), minor(st.st_rdev));
- else {
+ } else if (S_ISCHR(st.st_mode)) {
+ if (asprintf(&t, "_KERNEL_DEVICE=c%u:%u",
+ major(st.st_rdev),
+ minor(st.st_rdev)) < 0)
+ return -ENOMEM;
+ } else if (S_ISBLK(st.st_mode)) {
+ if (asprintf(&t, "_KERNEL_DEVICE=b%u:%u",
+ major(st.st_rdev),
+ minor(st.st_rdev)) < 0)
+ return -ENOMEM;
+ } else {
log_error("File is neither a device node, nor regular file, nor executable: %s", *i);
return -EINVAL;
}
--- src/run/run.c
+++ src/run/run.c 2014-07-28 09:46:36.846235596 +0000
@@ -309,12 +309,12 @@ static int start_transient_service(
_cleanup_free_ char *name = NULL;
int r;
- if (arg_unit)
+ if (arg_unit) {
name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
- else
- asprintf(&name, "run-%lu.service", (unsigned long) getpid());
- if (!name)
- return -ENOMEM;
+ if (!name)
+ return log_oom();
+ } else if (asprintf(&name, "run-%lu.service", (unsigned long) getpid()) < 0)
+ return log_oom();
r = message_start_transient_unit_new(bus, name, &m);
if (r < 0)
@@ -436,12 +436,12 @@ static int start_transient_scope(
assert(bus);
- if (arg_unit)
+ if (arg_unit) {
name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
- else
- asprintf(&name, "run-%lu.scope", (unsigned long) getpid());
- if (!name)
- return -ENOMEM;
+ if (!name)
+ return log_oom();
+ } else if (asprintf(&name, "run-%lu.scope", (unsigned long) getpid()) < 0)
+ return log_oom();
r = message_start_transient_unit_new(bus, name, &m);
if (r < 0)
--- src/shared/install.c
+++ src/shared/install.c 2014-07-28 00:00:00.000000000 +0000
@@ -72,13 +72,16 @@ static int get_config_path(UnitFileScope
case UNIT_FILE_SYSTEM:
- if (root_dir && runtime)
- asprintf(&p, "%s/run/systemd/system", root_dir);
- else if (runtime)
+ if (root_dir && runtime) {
+ if (asprintf(&p, "%s/run/systemd/system", root_dir) < 0)
+ return -ENOMEM;
+ } else if (runtime)
p = strdup("/run/systemd/system");
- else if (root_dir)
- asprintf(&p, "%s/%s", root_dir, SYSTEM_CONFIG_UNIT_PATH);
- else
+ else if (root_dir) {
+ if (asprintf(&p, "%s/%s", root_dir,
+ SYSTEM_CONFIG_UNIT_PATH) < 0)
+ return -ENOMEM;
+ } else
p = strdup(SYSTEM_CONFIG_UNIT_PATH);
break;
--- src/systemctl/systemctl.c
+++ src/systemctl/systemctl.c 2014-07-28 09:51:40.894735541 +0000
@@ -4639,11 +4639,11 @@ static int enable_sysv_units(const char
STRV_FOREACH(k, paths.unit_path) {
if (!isempty(arg_root))
- asprintf(&p, "%s/%s/%s", arg_root, *k, name);
+ j = asprintf(&p, "%s/%s/%s", arg_root, *k, name);
else
- asprintf(&p, "%s/%s", *k, name);
+ j = asprintf(&p, "%s/%s", *k, name);
- if (!p) {
+ if (j < 0) {
r = log_oom();
goto finish;
}
@@ -4660,10 +4660,10 @@ static int enable_sysv_units(const char
continue;
if (!isempty(arg_root))
- asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
+ j = asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
else
- asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
- if (!p) {
+ j = asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
+ if (j < 0) {
r = log_oom();
goto finish;
}
@@ -4676,10 +4676,10 @@ static int enable_sysv_units(const char
free(p);
p = NULL;
if (!isempty(arg_root))
- asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/boot.%s", arg_root, name);
+ j = asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/boot.%s", arg_root, name);
else
- asprintf(&p, SYSTEM_SYSVINIT_PATH "/boot.%s", name);
- if (!p) {
+ j = asprintf(&p, SYSTEM_SYSVINIT_PATH "/boot.%s", name);
+ if (j < 0) {
r = log_oom();
goto finish;
}
--- src/tty-ask-password-agent/tty-ask-password-agent.c
+++ src/tty-ask-password-agent/tty-ask-password-agent.c 2014-07-28 00:00:00.000000000 +0000
@@ -102,8 +102,9 @@ static int ask_password_plymouth(
if (accept_cached) {
packet = strdup("c");
n = 1;
- } else
- asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n);
+ } else if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1),
+ message, &n) < 0)
+ packet = NULL;
if (!packet) {
r = -ENOMEM;