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