Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * Copyright (C) 2019 The LineageOS Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <ctype.h> |
| 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <poll.h> |
| 23 | #include <pwd.h> |
| 24 | #include <signal.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #define LOG_TAG "ProcessKiller" |
| 32 | |
| 33 | #include <android-base/file.h> |
| 34 | #include <android-base/logging.h> |
| 35 | #include <android-base/stringprintf.h> |
| 36 | #include <cutils/log.h> |
| 37 | |
| 38 | #include "Process.h" |
| 39 | |
| 40 | using android::base::ReadFileToString; |
| 41 | using android::base::StringPrintf; |
| 42 | |
| 43 | int Process::readSymLink(const char* path, char* link, size_t max) { |
| 44 | struct stat s; |
| 45 | int length; |
| 46 | |
| 47 | if (lstat(path, &s) < 0) return 0; |
| 48 | if ((s.st_mode & S_IFMT) != S_IFLNK) return 0; |
| 49 | |
| 50 | // we have a symlink |
| 51 | length = readlink(path, link, max - 1); |
| 52 | if (length <= 0) return 0; |
| 53 | link[length] = 0; |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | int Process::pathMatchesMountPoint(const char* path, const char* mountPoint) { |
| 58 | int length = strlen(mountPoint); |
| 59 | if (length > 1 && strncmp(path, mountPoint, length) == 0) { |
| 60 | // we need to do extra checking if mountPoint does not end in a '/' |
| 61 | if (mountPoint[length - 1] == '/') return 1; |
| 62 | // if mountPoint does not have a trailing slash, we need to make sure |
| 63 | // there is one in the path to avoid partial matches. |
| 64 | return (path[length] == 0 || path[length] == '/'); |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | void Process::getProcessName(int pid, std::string& out_name) { |
| 71 | if (!ReadFileToString(StringPrintf("/proc/%d/cmdline", pid), &out_name)) { |
| 72 | out_name = "???"; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | int Process::checkFileDescriptorSymLinks(int pid, const char* mountPoint) { |
| 77 | return checkFileDescriptorSymLinks(pid, mountPoint, NULL, 0); |
| 78 | } |
| 79 | |
| 80 | int Process::checkFileDescriptorSymLinks(int pid, const char* mountPoint, char* openFilename, |
| 81 | size_t max) { |
| 82 | // compute path to process's directory of open files |
| 83 | char path[PATH_MAX]; |
| 84 | snprintf(path, sizeof(path), "/proc/%d/fd", pid); |
| 85 | DIR* dir = opendir(path); |
| 86 | if (!dir) return 0; |
| 87 | |
| 88 | // remember length of the path |
| 89 | int parent_length = strlen(path); |
| 90 | // append a trailing '/' |
| 91 | path[parent_length++] = '/'; |
| 92 | |
| 93 | struct dirent* de; |
| 94 | while ((de = readdir(dir))) { |
| 95 | if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") || |
| 96 | strlen(de->d_name) + parent_length + 1 >= PATH_MAX) |
| 97 | continue; |
| 98 | |
| 99 | // append the file name, after truncating to parent directory |
| 100 | path[parent_length] = 0; |
| 101 | strlcat(path, de->d_name, PATH_MAX); |
| 102 | |
| 103 | char link[PATH_MAX]; |
| 104 | |
| 105 | if (readSymLink(path, link, sizeof(link)) && pathMatchesMountPoint(link, mountPoint)) { |
| 106 | if (openFilename) { |
| 107 | memset(openFilename, 0, max); |
| 108 | strlcpy(openFilename, link, max); |
| 109 | } |
| 110 | closedir(dir); |
| 111 | return 1; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | closedir(dir); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int Process::checkFileMaps(int pid, const char* mountPoint) { |
| 120 | return checkFileMaps(pid, mountPoint, NULL, 0); |
| 121 | } |
| 122 | |
| 123 | int Process::checkFileMaps(int pid, const char* mountPoint, char* openFilename, size_t max) { |
| 124 | FILE* file; |
| 125 | char buffer[PATH_MAX + 100]; |
| 126 | |
| 127 | snprintf(buffer, sizeof(buffer), "/proc/%d/maps", pid); |
| 128 | file = fopen(buffer, "re"); |
| 129 | if (!file) return 0; |
| 130 | |
| 131 | while (fgets(buffer, sizeof(buffer), file)) { |
| 132 | // skip to the path |
| 133 | const char* path = strchr(buffer, '/'); |
| 134 | if (path && pathMatchesMountPoint(path, mountPoint)) { |
| 135 | if (openFilename) { |
| 136 | memset(openFilename, 0, max); |
| 137 | strlcpy(openFilename, path, max); |
| 138 | } |
| 139 | fclose(file); |
| 140 | return 1; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | fclose(file); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | int Process::checkSymLink(int pid, const char* mountPoint, const char* name) { |
| 149 | char path[PATH_MAX]; |
| 150 | char link[PATH_MAX]; |
| 151 | |
| 152 | snprintf(path, sizeof(path), "/proc/%d/%s", pid, name); |
| 153 | if (readSymLink(path, link, sizeof(link)) && pathMatchesMountPoint(link, mountPoint)) return 1; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | int Process::getPid(const char* s) { |
| 158 | int result = 0; |
| 159 | while (*s) { |
| 160 | if (!isdigit(*s)) return -1; |
| 161 | result = 10 * result + (*s++ - '0'); |
| 162 | } |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Hunt down processes that have files open at the given mount point. |
| 168 | */ |
| 169 | int Process::killProcessesWithOpenFiles(const char* path, int signal) { |
| 170 | int count = 0; |
| 171 | DIR* dir; |
| 172 | struct dirent* de; |
| 173 | |
| 174 | if (!(dir = opendir("/proc"))) { |
| 175 | SLOGE("opendir failed (%s)", strerror(errno)); |
| 176 | return count; |
| 177 | } |
| 178 | |
| 179 | while ((de = readdir(dir))) { |
| 180 | int pid = getPid(de->d_name); |
| 181 | if (pid == -1) continue; |
| 182 | |
| 183 | std::string name; |
| 184 | getProcessName(pid, name); |
| 185 | |
| 186 | char openfile[PATH_MAX]; |
| 187 | |
| 188 | if (checkFileDescriptorSymLinks(pid, path, openfile, sizeof(openfile))) { |
| 189 | SLOGE("Process %s (%d) has open file %s", name.c_str(), pid, openfile); |
| 190 | } else if (checkFileMaps(pid, path, openfile, sizeof(openfile))) { |
| 191 | SLOGE("Process %s (%d) has open filemap for %s", name.c_str(), pid, openfile); |
| 192 | } else if (checkSymLink(pid, path, "cwd")) { |
| 193 | SLOGE("Process %s (%d) has cwd within %s", name.c_str(), pid, path); |
| 194 | } else if (checkSymLink(pid, path, "root")) { |
| 195 | SLOGE("Process %s (%d) has chroot within %s", name.c_str(), pid, path); |
| 196 | } else if (checkSymLink(pid, path, "exe")) { |
| 197 | SLOGE("Process %s (%d) has executable path within %s", name.c_str(), pid, path); |
| 198 | } else { |
| 199 | continue; |
| 200 | } |
| 201 | |
| 202 | if (signal != 0) { |
| 203 | SLOGW("Sending %s to process %d", strsignal(signal), pid); |
| 204 | kill(pid, signal); |
| 205 | count++; |
| 206 | } |
| 207 | } |
| 208 | closedir(dir); |
| 209 | return count; |
| 210 | } |