From 16a11d879ff62b8dd6e698afd77748ea2edbf242 Mon Sep 17 00:00:00 2001 From: Stepan Blyshchak <38952541+stepanblyschak@users.noreply.github.com> Date: Sat, 18 Feb 2023 03:31:14 +0200 Subject: [PATCH] [systemd-sonic-generator] Fix overlapping strings being passed to strcpy/strcat (#13647) #### Why I did it Fix an issue that services do not start automatically on first boot and start only after hostcfgd enables them. This is due to a bug in systemd-sonic-generator: ``` admin@arc-switch1004:~$ /usr/lib/systemd/system-generators/systemd-sonic-generator dir Failed to open file /usr/lib/systemd/system/database.servcee Error parsing targets for database.servcee Error parsing database.servcee Failed to open file /usr/lib/systemd/system/bgp.servcee Error parsing targets for bgp.servcee Error parsing bgp.servcee Failed to open file /usr/lib/systemd/system/lldp.servcee Error parsing targets for lldp.servcee Error parsing lldp.servcee Failed to open file /usr/lib/systemd/system/swss.servcee Error parsing targets for swss.servcee Error parsing swss.servcee Failed to open file /usr/lib/systemd/system/teamd.servcee Error parsing targets for teamd.servcee Error parsing teamd.servcee Failed to open file /usr/lib/systemd/system/syncd.servcee Error parsing targets for syncd.servcee Error parsing syncd.servcee ``` A wrong file name is generated (e.g database.**servcee**). #### How I did it Fixed overlapping strings being passed to strcpy/strcat that receive restirct* pointers (strings should not overlap). #### How to verify it Perform first boot and observe services start immidiatelly after boot. --- src/systemd-sonic-generator/systemd-sonic-generator.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/systemd-sonic-generator/systemd-sonic-generator.c b/src/systemd-sonic-generator/systemd-sonic-generator.c index 9267a48aa4a6..fb82c41b7af8 100644 --- a/src/systemd-sonic-generator/systemd-sonic-generator.c +++ b/src/systemd-sonic-generator/systemd-sonic-generator.c @@ -592,11 +592,14 @@ int ssg_main(int argc, char **argv) { for (int i = 0; i < num_unit_files; i++) { unit_instance = strdup(unit_files[i]); if ((num_asics == 1) && strstr(unit_instance, "@") != NULL) { - prefix = strtok_r(unit_instance, "@", &saveptr); - suffix = strtok_r(NULL, "@", &saveptr); + prefix = strdup(strtok_r(unit_instance, "@", &saveptr)); + suffix = strdup(strtok_r(NULL, "@", &saveptr)); strcpy(unit_instance, prefix); strcat(unit_instance, suffix); + + free(prefix); + free(suffix); } num_targets = get_install_targets(unit_instance, targets);