long get_directory_size(char *dir_path) { long total_size = 0; DIR *dir; struct dirent *de; int ret; struct stat buf; char *file_path = NULL; dir = opendir(dir_path); if (!dir) { telem_perror("Error opening spool dir"); return -1; } while ((de = readdir(dir)) != NULL) { ret = asprintf(&file_path, "%s/%s", dir_path, de->d_name); if (ret == -1) { telem_log(LOG_CRIT, "Cannot allocate memory, exiting\n"); exit(EXIT_FAILURE); } ret = stat(file_path, &buf); if (ret < 0) { telem_log(LOG_ERR, "Could not stat file %s:%s", file_path, strerror(errno)); } else { total_size += buf.st_size; } free(file_path); } closedir(dir); return total_size; }
static void drop_privs(void) { uid_t euid; euid = geteuid(); if (euid != 0) { telem_log(LOG_DEBUG, "Not root; skipping privilege drop\n"); return; } struct passwd *pw; pw = getpwnam("telemetry"); if (!pw) { telem_log(LOG_ERR, "telemetry user not found\n"); exit(EXIT_FAILURE); } if (chdir(LOCALSTATEDIR "/lib/telemetry") != 0) { telem_perror("Not able to change working directory"); exit(EXIT_FAILURE); } // The order is important here: // change supplemental groups, our gid, and then our uid if (initgroups(pw->pw_name, pw->pw_gid) != 0) { telem_perror("Failed to set supplemental group list"); exit(EXIT_FAILURE); } if (setgid(pw->pw_gid) != 0) { telem_perror("Failed to set GID"); exit(EXIT_FAILURE); } if (setuid(pw->pw_uid) != 0) { telem_perror("Failed to set UID"); exit(EXIT_FAILURE); } assert(getuid() == pw->pw_uid); assert(geteuid() == pw->pw_uid); assert(getgid() == pw->pw_gid); assert(getegid() == pw->pw_gid); }
static int temp_core_file(void) { int tmp; ssize_t ret; char core[PATH_MAX] = "/tmp/corefile-XXXXXX"; /* mkstemp() opens the file with O_EXCL and 0600 permissions, so no need * to change umask or manipulate the fd to meet those requirements. */ if ((tmp = mkstemp(core)) < 0) { telem_perror("Failed to create temp core file"); return -1; } #ifndef DEBUG if (unlink(core) < 0) { telem_perror("Failed to unlink temp core file"); return -1; } #endif while (true) { // Use Linux-specific splice(2) here; // simplifies copying data from pipe->file ret = splice(STDIN_FILENO, NULL, tmp, NULL, INT_MAX, SPLICE_F_MORE | SPLICE_F_MOVE); if (ret > 0) { // More data to read continue; } else if (ret == 0) { // End of data break; } else if (ret < 0) { telem_perror("Failed to splice data to core file"); return -1; } } return tmp; }
static int temp_core_file(void) { int tmp; ssize_t ret; char core[PATH_MAX] = "/tmp/corefile-XXXXXX"; if ((tmp = mkstemp(core)) < 0) { telem_perror("Failed to create temp core file"); return -1; } #ifndef DEBUG if (unlink(core) < 0) { telem_perror("Failed to unlink temp core file"); return -1; } #endif while (true) { // Use Linux-specific splice(2) here; // simplifies copying data from pipe->file ret = splice(STDIN_FILENO, NULL, tmp, NULL, INT_MAX, SPLICE_F_MORE | SPLICE_F_MOVE); if (ret > 0) { // More data to read continue; } else if (ret == 0) { // End of data break; } else if (ret < 0) { telem_perror("Failed to splice data to core file"); return -1; } } return tmp; }