|
Zbigniew Jędrzejewski-Szmek |
436654 |
From d6e771a1936f54ff1693d84625de57b199bd6c6f Mon Sep 17 00:00:00 2001
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
From: Martin Pitt <martin.pitt@ubuntu.com>
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
Date: Mon, 1 Jun 2015 11:32:39 +0200
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
Subject: [PATCH] sd-device: fix invalid property strv pointers
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
In device_update_properties_bufs(), the strv is built from pointers into the
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
single nul-terminated buf_nulstr string, to avoid allocating the key=value
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
strings twice. However, we must not do that while building and
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
GREEDY_REALLOC0()'ing buf_nulstr, as each time when this actually reallocates
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
memory the pointers we wrote into buf_strv so far become invalid.
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
So change the logic to first completely build the new buf_nulstr, and then
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
iterate over it to pick out the pointers to the individual key=value strings
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
for properties_strv.
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
This fixes invalid environment for udev callouts.
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
(cherry picked from commit d854ba50a82f28b776c670d27156f0e9881fde8a)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
---
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
src/libsystemd/sd-device/device-private.c | 23 +++++++++++++----------
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
1 file changed, 13 insertions(+), 10 deletions(-)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
index 10370af029..deb8efd05d 100644
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
--- a/src/libsystemd/sd-device/device-private.c
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+++ b/src/libsystemd/sd-device/device-private.c
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
@@ -636,10 +636,9 @@ int device_new_from_nulstr(sd_device **ret, uint8_t *nulstr, size_t len) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
static int device_update_properties_bufs(sd_device *device) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
const char *val, *prop;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- char **buf_strv = NULL;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
uint8_t *buf_nulstr = NULL;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- size_t allocated_nulstr = 0, allocated_strv = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- size_t nulstr_len = 0, strv_size = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ size_t allocated_nulstr = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ size_t nulstr_len = 0, num = 0, i;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
assert(device);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
@@ -655,20 +654,24 @@ static int device_update_properties_bufs(sd_device *device) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
if (!buf_nulstr)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
return -ENOMEM;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- buf_strv = GREEDY_REALLOC0(buf_strv, allocated_strv, strv_size + 2);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- if (!buf_strv)
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- return -ENOMEM;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
-
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- buf_strv[strv_size ++] = (char *)&buf_nulstr[nulstr_len];
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
strscpyl((char *)buf_nulstr + nulstr_len, len + 1, prop, "=", val, NULL);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
nulstr_len += len + 1;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ ++num;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
}
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
free(device->properties_nulstr);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- free(device->properties_strv);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
device->properties_nulstr = buf_nulstr;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
device->properties_nulstr_len = nulstr_len;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
- device->properties_strv = buf_strv;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ /* build strv from buf_nulstr */
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ free(device->properties_strv);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ device->properties_strv = new0(char *, num + 1);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ i = 0;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ NULSTR_FOREACH(val, (char*) buf_nulstr) {
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ device->properties_strv[i] = (char *) val;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ assert(i < num);
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ i++;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
+ }
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
device->properties_buf_outdated = false;
|
|
Zbigniew Jędrzejewski-Szmek |
436654 |
|