a19bc6
From 7ac4fc60181cfc7ff06e696da78e1b2819580745 Mon Sep 17 00:00:00 2001
a19bc6
From: Evgeny Vereshchagin <evvers@ya.ru>
a19bc6
Date: Mon, 9 Jan 2017 04:46:11 +0000
a19bc6
Subject: [PATCH] shared: fix double free in link
a19bc6
a19bc6
Fixes:
a19bc6
```
a19bc6
touch hola.service
a19bc6
systemctl link $(pwd)/hola.service $(pwd)/hola.service
a19bc6
```
a19bc6
a19bc6
```
a19bc6
==1==ERROR: AddressSanitizer: attempting double-free on 0x60300002c560 in thread T0 (systemd):
a19bc6
    #0 0x7fc8c961cb00 in free (/lib64/libasan.so.3+0xc6b00)
a19bc6
    #1 0x7fc8c90ebd3b in strv_clear src/basic/strv.c:83
a19bc6
    #2 0x7fc8c90ebdb6 in strv_free src/basic/strv.c:89
a19bc6
    #3 0x55637c758c77 in strv_freep src/basic/strv.h:37
a19bc6
    #4 0x55637c763ba9 in method_enable_unit_files_generic src/core/dbus-manager.c:1960
a19bc6
    #5 0x55637c763d16 in method_link_unit_files src/core/dbus-manager.c:2001
a19bc6
    #6 0x7fc8c92537ec in method_callbacks_run src/libsystemd/sd-bus/bus-objects.c:418
a19bc6
    #7 0x7fc8c9258830 in object_find_and_run src/libsystemd/sd-bus/bus-objects.c:1255
a19bc6
    #8 0x7fc8c92594d7 in bus_process_object src/libsystemd/sd-bus/bus-objects.c:1371
a19bc6
    #9 0x7fc8c91e7553 in process_message src/libsystemd/sd-bus/sd-bus.c:2563
a19bc6
    #10 0x7fc8c91e78ce in process_running src/libsystemd/sd-bus/sd-bus.c:2605
a19bc6
    #11 0x7fc8c91e8f61 in bus_process_internal src/libsystemd/sd-bus/sd-bus.c:2837
a19bc6
    #12 0x7fc8c91e90d2 in sd_bus_process src/libsystemd/sd-bus/sd-bus.c:2856
a19bc6
    #13 0x7fc8c91ea8f9 in io_callback src/libsystemd/sd-bus/sd-bus.c:3126
a19bc6
    #14 0x7fc8c928333b in source_dispatch src/libsystemd/sd-event/sd-event.c:2268
a19bc6
    #15 0x7fc8c9285cf7 in sd_event_dispatch src/libsystemd/sd-event/sd-event.c:2627
a19bc6
    #16 0x7fc8c92865fa in sd_event_run src/libsystemd/sd-event/sd-event.c:2686
a19bc6
    #17 0x55637c6b5257 in manager_loop src/core/manager.c:2274
a19bc6
    #18 0x55637c6a2194 in main src/core/main.c:1920
a19bc6
    #19 0x7fc8c7ac7400 in __libc_start_main (/lib64/libc.so.6+0x20400)
a19bc6
    #20 0x55637c697339 in _start (/usr/lib/systemd/systemd+0xcd339)
a19bc6
a19bc6
0x60300002c560 is located 0 bytes inside of 19-byte region [0x60300002c560,0x60300002c573)
a19bc6
freed by thread T0 (systemd) here:
a19bc6
    #0 0x7fc8c961cb00 in free (/lib64/libasan.so.3+0xc6b00)
a19bc6
    #1 0x7fc8c90ee320 in strv_remove src/basic/strv.c:630
a19bc6
    #2 0x7fc8c90ee190 in strv_uniq src/basic/strv.c:602
a19bc6
    #3 0x7fc8c9180533 in unit_file_link src/shared/install.c:1996
a19bc6
    #4 0x55637c763b25 in method_enable_unit_files_generic src/core/dbus-manager.c:1985
a19bc6
    #5 0x55637c763d16 in method_link_unit_files src/core/dbus-manager.c:2001
a19bc6
    #6 0x7fc8c92537ec in method_callbacks_run src/libsystemd/sd-bus/bus-objects.c:418
a19bc6
    #7 0x7fc8c9258830 in object_find_and_run src/libsystemd/sd-bus/bus-objects.c:1255
a19bc6
    #8 0x7fc8c92594d7 in bus_process_object src/libsystemd/sd-bus/bus-objects.c:1371
a19bc6
    #9 0x7fc8c91e7553 in process_message src/libsystemd/sd-bus/sd-bus.c:2563
a19bc6
    #10 0x7fc8c91e78ce in process_running src/libsystemd/sd-bus/sd-bus.c:2605
a19bc6
    #11 0x7fc8c91e8f61 in bus_process_internal src/libsystemd/sd-bus/sd-bus.c:2837
a19bc6
    #12 0x7fc8c91e90d2 in sd_bus_process src/libsystemd/sd-bus/sd-bus.c:2856
a19bc6
    #13 0x7fc8c91ea8f9 in io_callback src/libsystemd/sd-bus/sd-bus.c:3126
a19bc6
    #14 0x7fc8c928333b in source_dispatch src/libsystemd/sd-event/sd-event.c:2268
a19bc6
    #15 0x7fc8c9285cf7 in sd_event_dispatch src/libsystemd/sd-event/sd-event.c:2627
a19bc6
    #16 0x7fc8c92865fa in sd_event_run src/libsystemd/sd-event/sd-event.c:2686
a19bc6
    #17 0x55637c6b5257 in manager_loop src/core/manager.c:2274
a19bc6
    #18 0x55637c6a2194 in main src/core/main.c:1920
a19bc6
        #19 0x7fc8c7ac7400 in __libc_start_main (/lib64/libc.so.6+0x20400)
a19bc6
a19bc6
previously allocated by thread T0 (systemd) here:
a19bc6
    #0 0x7fc8c95b0160 in strdup (/lib64/libasan.so.3+0x5a160)
a19bc6
    #1 0x7fc8c90edf32 in strv_extend src/basic/strv.c:552
a19bc6
    #2 0x7fc8c923ae41 in bus_message_read_strv_extend src/libsystemd/sd-bus/bus-message.c:5578
a19bc6
    #3 0x7fc8c923b0de in sd_bus_message_read_strv src/libsystemd/sd-bus/bus-message.c:5600
a19bc6
    #4 0x55637c7639d1 in method_enable_unit_files_generic src/core/dbus-manager.c:1969
a19bc6
    #5 0x55637c763d16 in method_link_unit_files src/core/dbus-manager.c:2001
a19bc6
    #6 0x7fc8c92537ec in method_callbacks_run src/libsystemd/sd-bus/bus-objects.c:418
a19bc6
    #7 0x7fc8c9258830 in object_find_and_run src/libsystemd/sd-bus/bus-objects.c:1255
a19bc6
    #8 0x7fc8c92594d7 in bus_process_object src/libsystemd/sd-bus/bus-objects.c:1371
a19bc6
    #9 0x7fc8c91e7553 in process_message src/libsystemd/sd-bus/sd-bus.c:2563
a19bc6
    #10 0x7fc8c91e78ce in process_running src/libsystemd/sd-bus/sd-bus.c:2605
a19bc6
    #11 0x7fc8c91e8f61 in bus_process_internal src/libsystemd/sd-bus/sd-bus.c:2837
a19bc6
    #12 0x7fc8c91e90d2 in sd_bus_process src/libsystemd/sd-bus/sd-bus.c:2856
a19bc6
    #13 0x7fc8c91ea8f9 in io_callback src/libsystemd/sd-bus/sd-bus.c:3126
a19bc6
    #14 0x7fc8c928333b in source_dispatch src/libsystemd/sd-event/sd-event.c:2268
a19bc6
    #15 0x7fc8c9285cf7 in sd_event_dispatch src/libsystemd/sd-event/sd-event.c:2627
a19bc6
    #16 0x7fc8c92865fa in sd_event_run src/libsystemd/sd-event/sd-event.c:2686
a19bc6
    #17 0x55637c6b5257 in manager_loop src/core/manager.c:2274
a19bc6
    #18 0x55637c6a2194 in main src/core/main.c:1920
a19bc6
    #19 0x7fc8c7ac7400 in __libc_start_main (/lib64/libc.so.6+0x20400)
a19bc6
a19bc6
SUMMARY: AddressSanitizer: double-free (/lib64/libasan.so.3+0xc6b00) in free
a19bc6
==1==ABORTING
a19bc6
```
a19bc6
a19bc6
Closes #5015
a19bc6
a19bc6
(cherry picked from commit 8af35ba681116eb79a46e3dbd65b166c1efd6164)
a19bc6
Related: #1409997
a19bc6
---
a19bc6
 src/shared/install.c | 8 ++++++--
a19bc6
 1 file changed, 6 insertions(+), 2 deletions(-)
a19bc6
a19bc6
diff --git a/src/shared/install.c b/src/shared/install.c
a19bc6
index 1b59a96..87d805c 100644
a19bc6
--- a/src/shared/install.c
a19bc6
+++ b/src/shared/install.c
a19bc6
@@ -1682,7 +1682,7 @@ int unit_file_link(
a19bc6
 
a19bc6
         _cleanup_lookup_paths_free_ LookupPaths paths = {};
a19bc6
         _cleanup_free_ char *config_path = NULL;
a19bc6
-        _cleanup_free_ char **todo = NULL;
a19bc6
+        _cleanup_strv_free_ char **todo = NULL;
a19bc6
         size_t n_todo = 0, n_allocated = 0;
a19bc6
         char **i;
a19bc6
         int r,q;
a19bc6
@@ -1736,7 +1736,11 @@ int unit_file_link(
a19bc6
                 if (!GREEDY_REALLOC0(todo, n_allocated, n_todo + 2))
a19bc6
                         return -ENOMEM;
a19bc6
 
a19bc6
-                todo[n_todo++] = *i;
a19bc6
+                todo[n_todo] = strdup(*i);
a19bc6
+                if (!todo[n_todo])
a19bc6
+                        return -ENOMEM;
a19bc6
+
a19bc6
+                n_todo++;
a19bc6
         }
a19bc6
 
a19bc6
         strv_uniq(todo);