@cryptotaxi247 / netdata-1 / commits / 3cd61bcb1

fix buffer overflow incgroups_detect_systemd() (#17799)

Ilya Mashchenko committed Jun 3, 2024 at 20:57 UTC 3cd61bcb10ec55e8522d1d745eb5ee9bff7269e9
1 file changed +12 -14
src/collectors/cgroups.plugin/sys_fs_cgroup.c
+12 -14
@@ -84,24 +84,25 @@ static enum cgroups_systemd_setting cgroups_detect_systemd(const char *exec)
84 if (!fp_child_output)
85 return retval;
86
87 - fd_set rfds;
88 - struct timeval timeout;
87 int fd = fileno(fp_child_output);
90 - int ret = -1;
88 + if (fd == -1 ) {
89 + collector_error("Cannot get the output of \"%s\": failed to get file descriptor", exec);
90 + netdata_pclose(fp_child_input, fp_child_output, command_pid);
91 + return retval;
92 + }
93
92 - FD_ZERO(&rfds);
93 - FD_SET(fd, &rfds);
94 - timeout.tv_sec = 3;
95 - timeout.tv_usec = 0;
94 + struct pollfd pfd;
95 + pfd.fd = fd;
96 + pfd.events = POLLIN;
97
97 - if (fd != -1) {
98 - ret = select(fd + 1, &rfds, NULL, NULL, &timeout);
99 - }
98 + int timeout = 3000; // milliseconds
99 +
100 + int ret = poll(&pfd, 1, timeout);
101
102 if (ret == -1) {
103 collector_error("Failed to get the output of \"%s\"", exec);
104 } else if (ret == 0) {
104 - collector_info("Cannot get the output of \"%s\" within %"PRId64" seconds", exec, (int64_t)timeout.tv_sec);
105 + collector_info("Cannot get the output of \"%s\" within timeout (%d ms)", exec, timeout);
106 } else {
107 while (fgets(buf, MAXSIZE_PROC_CMDLINE, fp_child_output) != NULL) {
108 if ((begin = strstr(buf, SYSTEMD_HIERARCHY_STRING))) {
@@ -169,9 +170,6 @@ static enum cgroups_type cgroups_try_detect_version()
170 if ((systemd_setting = cgroups_detect_systemd("systemd --version")) == SYSTEMD_CGROUP_ERR)
171 systemd_setting = cgroups_detect_systemd(SYSTEMD_CMD_RHEL);
172
172 - if(systemd_setting == SYSTEMD_CGROUP_ERR)
173 - return CGROUPS_AUTODETECT_FAIL;
174 -
173 if(systemd_setting == SYSTEMD_CGROUP_LEGACY || systemd_setting == SYSTEMD_CGROUP_HYBRID) {
174 // currently we prefer V1 if HYBRID is set as it seems to be more feature complete
175 // in the future we might want to continue here if SYSTEMD_CGROUP_HYBRID