Skip to content

Commit f30936d

Browse files
[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 <[email protected]>
1 parent 881ceb7 commit f30936d

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/systemd-sonic-generator/systemd-sonic-generator.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ static int get_install_targets_from_line(char* target_string, char* install_type
141141
char final_target[PATH_MAX];
142142
int num_targets = 0;
143143

144+
assert(target_string);
145+
assert(install_type);
146+
144147
while ((token = strtok_r(target_string, " ", &target_string))) {
145148
if (num_targets + existing_targets >= MAX_NUM_TARGETS) {
146149
fputs("Number of targets found exceeds MAX_NUM_TARGETS\n", stderr);
@@ -269,7 +272,7 @@ int get_install_targets(char* unit_file, char* targets[]) {
269272
char* token;
270273
char* line = NULL;
271274
bool first;
272-
char* target_suffix;
275+
char* target_suffix = NULL;
273276
char *instance_name;
274277
char *dot_ptr;
275278

@@ -306,6 +309,8 @@ int get_install_targets(char* unit_file, char* targets[]) {
306309
}
307310
else if (strstr(token, "WantedBy") != NULL) {
308311
target_suffix = ".wants";
312+
} else {
313+
break;
309314
}
310315
}
311316
else {

src/systemd-sonic-generator/tests/testfiles/test.service

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Type=oneshot
77
RemainAfterExit=yes
88
ExecStart=/usr/bin/test.sh start
99
[Install]
10+
Alias=test.service
1011
WantedBy=multi-user.target

0 commit comments

Comments
 (0)