[systemd-sonic-generator] Fix handling service files with additional fields under [Install] section (#17764)

If encountered a line without RequiredBy or WantedBy the code passes uninitialized pointer to get_install_targets_from_line(). Where it can fail with segfault or silently pass randomly.

- Why I did it
Uninitialized target_suffix is passed to get_install_targets_from_line() when other fields are present in [Install] section, like this:

root@sonic:/home/admin# systemctl cat ntpsec
...
[Install]
Alias=ntp.service
Alias=ntpd.service
WantedBy=multi-user.target

- How I did it
Initialize target_suffix with NULL, put an assert in get_install_targets_from_line(). Edited test to cover this scenario.

- How to verify it
UT and on the switch.

Signed-off-by: Stepan Blyschak <stepanb@nvidia.com>
This commit is contained in:
Stepan Blyshchak 2024-02-29 15:59:36 +02:00 committed by GitHub
parent 881ceb7034
commit f30936d1e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View File

@ -141,6 +141,9 @@ static int get_install_targets_from_line(char* target_string, char* install_type
char final_target[PATH_MAX];
int num_targets = 0;
assert(target_string);
assert(install_type);
while ((token = strtok_r(target_string, " ", &target_string))) {
if (num_targets + existing_targets >= MAX_NUM_TARGETS) {
fputs("Number of targets found exceeds MAX_NUM_TARGETS\n", stderr);
@ -269,7 +272,7 @@ int get_install_targets(char* unit_file, char* targets[]) {
char* token;
char* line = NULL;
bool first;
char* target_suffix;
char* target_suffix = NULL;
char *instance_name;
char *dot_ptr;
@ -306,6 +309,8 @@ int get_install_targets(char* unit_file, char* targets[]) {
}
else if (strstr(token, "WantedBy") != NULL) {
target_suffix = ".wants";
} else {
break;
}
}
else {

View File

@ -7,4 +7,5 @@ Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/test.sh start
[Install]
Alias=test.service
WantedBy=multi-user.target