cgroup-network.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "libnetdata/libnetdata.h"
  3. #include "libnetdata/required_dummies.h"
  4. #ifdef HAVE_SETNS
  5. #ifndef _GNU_SOURCE
  6. #define _GNU_SOURCE /* See feature_test_macros(7) */
  7. #endif
  8. #include <sched.h>
  9. #endif
  10. char environment_variable2[FILENAME_MAX + 50] = "";
  11. char *environment[] = {
  12. "PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin",
  13. environment_variable2,
  14. NULL
  15. };
  16. struct iface {
  17. const char *device;
  18. uint32_t hash;
  19. unsigned int ifindex;
  20. unsigned int iflink;
  21. struct iface *next;
  22. };
  23. unsigned int read_iface_iflink(const char *prefix, const char *iface) {
  24. if(!prefix) prefix = "";
  25. char filename[FILENAME_MAX + 1];
  26. snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/iflink", prefix, iface);
  27. unsigned long long iflink = 0;
  28. int ret = read_single_number_file(filename, &iflink);
  29. if(ret) error("Cannot read '%s'.", filename);
  30. return (unsigned int)iflink;
  31. }
  32. unsigned int read_iface_ifindex(const char *prefix, const char *iface) {
  33. if(!prefix) prefix = "";
  34. char filename[FILENAME_MAX + 1];
  35. snprintfz(filename, FILENAME_MAX, "%s/sys/class/net/%s/ifindex", prefix, iface);
  36. unsigned long long ifindex = 0;
  37. int ret = read_single_number_file(filename, &ifindex);
  38. if(ret) error("Cannot read '%s'.", filename);
  39. return (unsigned int)ifindex;
  40. }
  41. struct iface *read_proc_net_dev(const char *scope __maybe_unused, const char *prefix) {
  42. if(!prefix) prefix = "";
  43. procfile *ff = NULL;
  44. char filename[FILENAME_MAX + 1];
  45. snprintfz(filename, FILENAME_MAX, "%s%s", prefix, (*prefix)?"/proc/1/net/dev":"/proc/net/dev");
  46. #ifdef NETDATA_INTERNAL_CHECKS
  47. info("parsing '%s'", filename);
  48. #endif
  49. ff = procfile_open(filename, " \t,:|", PROCFILE_FLAG_DEFAULT);
  50. if(unlikely(!ff)) {
  51. error("Cannot open file '%s'", filename);
  52. return NULL;
  53. }
  54. ff = procfile_readall(ff);
  55. if(unlikely(!ff)) {
  56. error("Cannot read file '%s'", filename);
  57. return NULL;
  58. }
  59. size_t lines = procfile_lines(ff), l;
  60. struct iface *root = NULL;
  61. for(l = 2; l < lines ;l++) {
  62. if (unlikely(procfile_linewords(ff, l) < 1)) continue;
  63. struct iface *t = callocz(1, sizeof(struct iface));
  64. t->device = strdupz(procfile_lineword(ff, l, 0));
  65. t->hash = simple_hash(t->device);
  66. t->ifindex = read_iface_ifindex(prefix, t->device);
  67. t->iflink = read_iface_iflink(prefix, t->device);
  68. t->next = root;
  69. root = t;
  70. #ifdef NETDATA_INTERNAL_CHECKS
  71. info("added %s interface '%s', ifindex %u, iflink %u", scope, t->device, t->ifindex, t->iflink);
  72. #endif
  73. }
  74. procfile_close(ff);
  75. return root;
  76. }
  77. void free_iface(struct iface *iface) {
  78. freez((void *)iface->device);
  79. freez(iface);
  80. }
  81. void free_host_ifaces(struct iface *iface) {
  82. while(iface) {
  83. struct iface *t = iface->next;
  84. free_iface(iface);
  85. iface = t;
  86. }
  87. }
  88. int iface_is_eligible(struct iface *iface) {
  89. if(iface->iflink != iface->ifindex)
  90. return 1;
  91. return 0;
  92. }
  93. int eligible_ifaces(struct iface *root) {
  94. int eligible = 0;
  95. struct iface *t;
  96. for(t = root; t ; t = t->next)
  97. if(iface_is_eligible(t))
  98. eligible++;
  99. return eligible;
  100. }
  101. static void continue_as_child(void) {
  102. pid_t child = fork();
  103. int status;
  104. pid_t ret;
  105. if (child < 0)
  106. error("fork() failed");
  107. /* Only the child returns */
  108. if (child == 0)
  109. return;
  110. for (;;) {
  111. ret = waitpid(child, &status, WUNTRACED);
  112. if ((ret == child) && (WIFSTOPPED(status))) {
  113. /* The child suspended so suspend us as well */
  114. kill(getpid(), SIGSTOP);
  115. kill(child, SIGCONT);
  116. } else {
  117. break;
  118. }
  119. }
  120. /* Return the child's exit code if possible */
  121. if (WIFEXITED(status)) {
  122. exit(WEXITSTATUS(status));
  123. } else if (WIFSIGNALED(status)) {
  124. kill(getpid(), WTERMSIG(status));
  125. }
  126. exit(EXIT_FAILURE);
  127. }
  128. int proc_pid_fd(const char *prefix, const char *ns, pid_t pid) {
  129. if(!prefix) prefix = "";
  130. char filename[FILENAME_MAX + 1];
  131. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/%s", prefix, (int)pid, ns);
  132. int fd = open(filename, O_RDONLY);
  133. if(fd == -1)
  134. error("Cannot open proc_pid_fd() file '%s'", filename);
  135. return fd;
  136. }
  137. static struct ns {
  138. int nstype;
  139. int fd;
  140. int status;
  141. const char *name;
  142. const char *path;
  143. } all_ns[] = {
  144. // { .nstype = CLONE_NEWUSER, .fd = -1, .status = -1, .name = "user", .path = "ns/user" },
  145. // { .nstype = CLONE_NEWCGROUP, .fd = -1, .status = -1, .name = "cgroup", .path = "ns/cgroup" },
  146. // { .nstype = CLONE_NEWIPC, .fd = -1, .status = -1, .name = "ipc", .path = "ns/ipc" },
  147. // { .nstype = CLONE_NEWUTS, .fd = -1, .status = -1, .name = "uts", .path = "ns/uts" },
  148. { .nstype = CLONE_NEWNET, .fd = -1, .status = -1, .name = "network", .path = "ns/net" },
  149. { .nstype = CLONE_NEWPID, .fd = -1, .status = -1, .name = "pid", .path = "ns/pid" },
  150. { .nstype = CLONE_NEWNS, .fd = -1, .status = -1, .name = "mount", .path = "ns/mnt" },
  151. // terminator
  152. { .nstype = 0, .fd = -1, .status = -1, .name = NULL, .path = NULL }
  153. };
  154. int switch_namespace(const char *prefix, pid_t pid) {
  155. #ifdef HAVE_SETNS
  156. int i;
  157. for(i = 0; all_ns[i].name ; i++)
  158. all_ns[i].fd = proc_pid_fd(prefix, all_ns[i].path, pid);
  159. int root_fd = proc_pid_fd(prefix, "root", pid);
  160. int cwd_fd = proc_pid_fd(prefix, "cwd", pid);
  161. setgroups(0, NULL);
  162. // 2 passes - found it at nsenter source code
  163. // this is related CLONE_NEWUSER functionality
  164. // This code cannot switch user namespace (it can all the other namespaces)
  165. // Fortunately, we don't need to switch user namespaces.
  166. int pass;
  167. for(pass = 0; pass < 2 ;pass++) {
  168. for(i = 0; all_ns[i].name ; i++) {
  169. if (all_ns[i].fd != -1 && all_ns[i].status == -1) {
  170. if(setns(all_ns[i].fd, all_ns[i].nstype) == -1) {
  171. if(pass == 1) {
  172. all_ns[i].status = 0;
  173. error("Cannot switch to %s namespace of pid %d", all_ns[i].name, (int) pid);
  174. }
  175. }
  176. else
  177. all_ns[i].status = 1;
  178. }
  179. }
  180. }
  181. setgroups(0, NULL);
  182. if(root_fd != -1) {
  183. if(fchdir(root_fd) < 0)
  184. error("Cannot fchdir() to pid %d root directory", (int)pid);
  185. if(chroot(".") < 0)
  186. error("Cannot chroot() to pid %d root directory", (int)pid);
  187. close(root_fd);
  188. }
  189. if(cwd_fd != -1) {
  190. if(fchdir(cwd_fd) < 0)
  191. error("Cannot fchdir() to pid %d current working directory", (int)pid);
  192. close(cwd_fd);
  193. }
  194. int do_fork = 0;
  195. for(i = 0; all_ns[i].name ; i++)
  196. if(all_ns[i].fd != -1) {
  197. // CLONE_NEWPID requires a fork() to become effective
  198. if(all_ns[i].nstype == CLONE_NEWPID && all_ns[i].status)
  199. do_fork = 1;
  200. close(all_ns[i].fd);
  201. }
  202. if(do_fork)
  203. continue_as_child();
  204. return 0;
  205. #else
  206. errno = ENOSYS;
  207. error("setns() is missing on this system.");
  208. return 1;
  209. #endif
  210. }
  211. pid_t read_pid_from_cgroup_file(const char *filename) {
  212. int fd = open(filename, procfile_open_flags);
  213. if(fd == -1) {
  214. error("Cannot open pid_from_cgroup() file '%s'.", filename);
  215. return 0;
  216. }
  217. FILE *fp = fdopen(fd, "r");
  218. if(!fp) {
  219. error("Cannot upgrade fd to fp for file '%s'.", filename);
  220. return 0;
  221. }
  222. char buffer[100 + 1];
  223. pid_t pid = 0;
  224. char *s;
  225. while((s = fgets(buffer, 100, fp))) {
  226. buffer[100] = '\0';
  227. pid = atoi(s);
  228. if(pid > 0) break;
  229. }
  230. fclose(fp);
  231. #ifdef NETDATA_INTERNAL_CHECKS
  232. if(pid > 0) info("found pid %d on file '%s'", pid, filename);
  233. #endif
  234. return pid;
  235. }
  236. pid_t read_pid_from_cgroup_files(const char *path) {
  237. char filename[FILENAME_MAX + 1];
  238. snprintfz(filename, FILENAME_MAX, "%s/cgroup.procs", path);
  239. pid_t pid = read_pid_from_cgroup_file(filename);
  240. if(pid > 0) return pid;
  241. snprintfz(filename, FILENAME_MAX, "%s/tasks", path);
  242. return read_pid_from_cgroup_file(filename);
  243. }
  244. pid_t read_pid_from_cgroup(const char *path) {
  245. pid_t pid = read_pid_from_cgroup_files(path);
  246. if (pid > 0) return pid;
  247. DIR *dir = opendir(path);
  248. if (!dir) {
  249. error("cannot read directory '%s'", path);
  250. return 0;
  251. }
  252. struct dirent *de = NULL;
  253. while ((de = readdir(dir))) {
  254. if (de->d_type == DT_DIR
  255. && (
  256. (de->d_name[0] == '.' && de->d_name[1] == '\0')
  257. || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
  258. ))
  259. continue;
  260. if (de->d_type == DT_DIR) {
  261. char filename[FILENAME_MAX + 1];
  262. snprintfz(filename, FILENAME_MAX, "%s/%s", path, de->d_name);
  263. pid = read_pid_from_cgroup(filename);
  264. if(pid > 0) break;
  265. }
  266. }
  267. closedir(dir);
  268. return pid;
  269. }
  270. // ----------------------------------------------------------------------------
  271. // send the result to netdata
  272. struct found_device {
  273. const char *host_device;
  274. const char *guest_device;
  275. uint32_t host_device_hash;
  276. struct found_device *next;
  277. } *detected_devices = NULL;
  278. void add_device(const char *host, const char *guest) {
  279. #ifdef NETDATA_INTERNAL_CHECKS
  280. info("adding device with host '%s', guest '%s'", host, guest);
  281. #endif
  282. uint32_t hash = simple_hash(host);
  283. if(guest && (!*guest || strcmp(host, guest) == 0))
  284. guest = NULL;
  285. struct found_device *f;
  286. for(f = detected_devices; f ; f = f->next) {
  287. if(f->host_device_hash == hash && !strcmp(host, f->host_device)) {
  288. if(guest && (!f->guest_device || !strcmp(f->host_device, f->guest_device))) {
  289. if(f->guest_device) freez((void *)f->guest_device);
  290. f->guest_device = strdupz(guest);
  291. }
  292. return;
  293. }
  294. }
  295. f = mallocz(sizeof(struct found_device));
  296. f->host_device = strdupz(host);
  297. f->host_device_hash = hash;
  298. f->guest_device = (guest)?strdupz(guest):NULL;
  299. f->next = detected_devices;
  300. detected_devices = f;
  301. }
  302. int send_devices(void) {
  303. int found = 0;
  304. struct found_device *f;
  305. for(f = detected_devices; f ; f = f->next) {
  306. found++;
  307. printf("%s %s\n", f->host_device, (f->guest_device)?f->guest_device:f->host_device);
  308. }
  309. return found;
  310. }
  311. // ----------------------------------------------------------------------------
  312. // this function should be called only **ONCE**
  313. // also it has to be the **LAST** to be called
  314. // since it switches namespaces, so after this call, everything is different!
  315. void detect_veth_interfaces(pid_t pid) {
  316. struct iface *cgroup = NULL;
  317. struct iface *host, *h, *c;
  318. host = read_proc_net_dev("host", netdata_configured_host_prefix);
  319. if(!host) {
  320. errno = 0;
  321. error("cannot read host interface list.");
  322. goto cleanup;
  323. }
  324. if(!eligible_ifaces(host)) {
  325. errno = 0;
  326. info("there are no double-linked host interfaces available.");
  327. goto cleanup;
  328. }
  329. if(switch_namespace(netdata_configured_host_prefix, pid)) {
  330. errno = 0;
  331. error("cannot switch to the namespace of pid %u", (unsigned int) pid);
  332. goto cleanup;
  333. }
  334. #ifdef NETDATA_INTERNAL_CHECKS
  335. info("switched to namespaces of pid %d", pid);
  336. #endif
  337. cgroup = read_proc_net_dev("cgroup", NULL);
  338. if(!cgroup) {
  339. errno = 0;
  340. error("cannot read cgroup interface list.");
  341. goto cleanup;
  342. }
  343. if(!eligible_ifaces(cgroup)) {
  344. errno = 0;
  345. error("there are not double-linked cgroup interfaces available.");
  346. goto cleanup;
  347. }
  348. for(h = host; h ; h = h->next) {
  349. if(iface_is_eligible(h)) {
  350. for (c = cgroup; c; c = c->next) {
  351. if(iface_is_eligible(c) && h->ifindex == c->iflink && h->iflink == c->ifindex) {
  352. add_device(h->device, c->device);
  353. }
  354. }
  355. }
  356. }
  357. cleanup:
  358. free_host_ifaces(cgroup);
  359. free_host_ifaces(host);
  360. }
  361. // ----------------------------------------------------------------------------
  362. // call the external helper
  363. #define CGROUP_NETWORK_INTERFACE_MAX_LINE 2048
  364. void call_the_helper(pid_t pid, const char *cgroup) {
  365. if(setresuid(0, 0, 0) == -1)
  366. error("setresuid(0, 0, 0) failed.");
  367. char command[CGROUP_NETWORK_INTERFACE_MAX_LINE + 1];
  368. if(cgroup)
  369. snprintfz(command, CGROUP_NETWORK_INTERFACE_MAX_LINE, "exec " PLUGINS_DIR "/cgroup-network-helper.sh --cgroup '%s'", cgroup);
  370. else
  371. snprintfz(command, CGROUP_NETWORK_INTERFACE_MAX_LINE, "exec " PLUGINS_DIR "/cgroup-network-helper.sh --pid %d", pid);
  372. info("running: %s", command);
  373. pid_t cgroup_pid;
  374. FILE *fp = mypopene(command, &cgroup_pid, environment);
  375. if(fp) {
  376. char buffer[CGROUP_NETWORK_INTERFACE_MAX_LINE + 1];
  377. char *s;
  378. while((s = fgets(buffer, CGROUP_NETWORK_INTERFACE_MAX_LINE, fp))) {
  379. trim(s);
  380. if(*s && *s != '\n') {
  381. char *t = s;
  382. while(*t && *t != ' ') t++;
  383. if(*t == ' ') {
  384. *t = '\0';
  385. t++;
  386. }
  387. if(!*s || !*t) continue;
  388. add_device(s, t);
  389. }
  390. }
  391. mypclose(fp, cgroup_pid);
  392. }
  393. else
  394. error("cannot execute cgroup-network helper script: %s", command);
  395. }
  396. int is_valid_path_symbol(char c) {
  397. switch(c) {
  398. case '/': // path separators
  399. case '\\': // needed for virsh domains \x2d1\x2dname
  400. case ' ': // space
  401. case '-': // hyphen
  402. case '_': // underscore
  403. case '.': // dot
  404. case ',': // comma
  405. return 1;
  406. default:
  407. return 0;
  408. }
  409. }
  410. // we will pass this path a shell script running as root
  411. // so, we need to make sure the path will be valid
  412. // and will not include anything that could allow
  413. // the caller use shell expansion for gaining escalated
  414. // privileges.
  415. int verify_path(const char *path) {
  416. struct stat sb;
  417. char c;
  418. const char *s = path;
  419. while((c = *s++)) {
  420. if(!( isalnum(c) || is_valid_path_symbol(c) )) {
  421. error("invalid character in path '%s'", path);
  422. return -1;
  423. }
  424. }
  425. if(strstr(path, "\\") && !strstr(path, "\\x")) {
  426. error("invalid escape sequence in path '%s'", path);
  427. return 1;
  428. }
  429. if(strstr(path, "/../")) {
  430. error("invalid parent path sequence detected in '%s'", path);
  431. return 1;
  432. }
  433. if(path[0] != '/') {
  434. error("only absolute path names are supported - invalid path '%s'", path);
  435. return -1;
  436. }
  437. if (stat(path, &sb) == -1) {
  438. error("cannot stat() path '%s'", path);
  439. return -1;
  440. }
  441. if((sb.st_mode & S_IFMT) != S_IFDIR) {
  442. error("path '%s' is not a directory", path);
  443. return -1;
  444. }
  445. return 0;
  446. }
  447. /*
  448. char *fix_path_variable(void) {
  449. const char *path = getenv("PATH");
  450. if(!path || !*path) return 0;
  451. char *p = strdupz(path);
  452. char *safe_path = callocz(1, strlen(p) + strlen("PATH=") + 1);
  453. strcpy(safe_path, "PATH=");
  454. int added = 0;
  455. char *ptr = p;
  456. while(ptr && *ptr) {
  457. char *s = strsep(&ptr, ":");
  458. if(s && *s) {
  459. if(verify_path(s) == -1) {
  460. error("the PATH variable includes an invalid path '%s' - removed it.", s);
  461. }
  462. else {
  463. info("the PATH variable includes a valid path '%s'.", s);
  464. if(added) strcat(safe_path, ":");
  465. strcat(safe_path, s);
  466. added++;
  467. }
  468. }
  469. }
  470. info("unsafe PATH: '%s'.", path);
  471. info(" safe PATH: '%s'.", safe_path);
  472. freez(p);
  473. return safe_path;
  474. }
  475. */
  476. // ----------------------------------------------------------------------------
  477. // main
  478. void usage(void) {
  479. fprintf(stderr, "%s [ -p PID | --pid PID | --cgroup /path/to/cgroup ]\n", program_name);
  480. exit(1);
  481. }
  482. int main(int argc, char **argv) {
  483. pid_t pid = 0;
  484. program_name = argv[0];
  485. program_version = VERSION;
  486. error_log_syslog = 0;
  487. // since cgroup-network runs as root, prevent it from opening symbolic links
  488. procfile_open_flags = O_RDONLY|O_NOFOLLOW;
  489. // ------------------------------------------------------------------------
  490. // make sure NETDATA_HOST_PREFIX is safe
  491. netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
  492. if(verify_netdata_host_prefix() == -1) exit(1);
  493. if(netdata_configured_host_prefix[0] != '\0' && verify_path(netdata_configured_host_prefix) == -1)
  494. fatal("invalid NETDATA_HOST_PREFIX '%s'", netdata_configured_host_prefix);
  495. // ------------------------------------------------------------------------
  496. // build a safe environment for our script
  497. // the first environment variable is a fixed PATH=
  498. snprintfz(environment_variable2, sizeof(environment_variable2) - 1, "NETDATA_HOST_PREFIX=%s", netdata_configured_host_prefix);
  499. // ------------------------------------------------------------------------
  500. if(argc == 2 && (!strcmp(argv[1], "version") || !strcmp(argv[1], "-version") || !strcmp(argv[1], "--version") || !strcmp(argv[1], "-v") || !strcmp(argv[1], "-V"))) {
  501. fprintf(stderr, "cgroup-network %s\n", VERSION);
  502. exit(0);
  503. }
  504. if(argc != 3)
  505. usage();
  506. if(!strcmp(argv[1], "-p") || !strcmp(argv[1], "--pid")) {
  507. pid = atoi(argv[2]);
  508. if(pid <= 0) {
  509. errno = 0;
  510. error("Invalid pid %d given", (int) pid);
  511. return 2;
  512. }
  513. call_the_helper(pid, NULL);
  514. }
  515. else if(!strcmp(argv[1], "--cgroup")) {
  516. char *cgroup = argv[2];
  517. if(verify_path(cgroup) == -1) {
  518. error("cgroup '%s' does not exist or is not valid.", cgroup);
  519. return 1;
  520. }
  521. pid = read_pid_from_cgroup(cgroup);
  522. call_the_helper(pid, cgroup);
  523. if(pid <= 0 && !detected_devices) {
  524. errno = 0;
  525. error("Cannot find a cgroup PID from cgroup '%s'", cgroup);
  526. }
  527. }
  528. else
  529. usage();
  530. if(pid > 0)
  531. detect_veth_interfaces(pid);
  532. int found = send_devices();
  533. if(found <= 0) return 1;
  534. return 0;
  535. }