Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #define LOG_TAG "libvintf" |
| 19 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 20 | #include "RuntimeInfo.h" |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 21 | |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 22 | #include "CompatibilityMatrix.h" |
| 23 | #include "parse_string.h" |
| 24 | |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <sys/utsname.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <android-base/logging.h> |
| 30 | #include <selinux/selinux.h> |
| 31 | #include <zlib.h> |
| 32 | |
| 33 | #define PROC_CONFIG "/proc/config.gz" |
| 34 | #define BUFFER_SIZE sysconf(_SC_PAGESIZE) |
| 35 | |
| 36 | namespace android { |
| 37 | namespace vintf { |
| 38 | |
| 39 | static void removeTrailingComments(std::string *s) { |
| 40 | size_t sharpPos = s->find('#'); |
| 41 | if (sharpPos != std::string::npos) { |
| 42 | s->erase(sharpPos); |
| 43 | } |
| 44 | } |
| 45 | static void trim(std::string *s) { |
| 46 | auto l = s->begin(); |
| 47 | for (; l != s->end() && std::isspace(*l); ++l); |
| 48 | s->erase(s->begin(), l); |
| 49 | auto r = s->rbegin(); |
| 50 | for (; r != s->rend() && std::isspace(*r); ++r); |
| 51 | s->erase(r.base(), s->end()); |
| 52 | } |
| 53 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 54 | struct RuntimeInfoFetcher { |
| 55 | RuntimeInfoFetcher(RuntimeInfo *ki) : mRuntimeInfo(ki) { } |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 56 | status_t fetchAllInformation(); |
| 57 | private: |
| 58 | void streamConfig(const char *buf, size_t len); |
| 59 | void parseConfig(std::string *s); |
| 60 | status_t fetchVersion(); |
| 61 | status_t fetchKernelConfigs(); |
| 62 | status_t fetchCpuInfo(); |
| 63 | status_t fetchKernelSepolicyVers(); |
| 64 | status_t fetchSepolicyFiles(); |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 65 | status_t parseKernelVersion(); |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 66 | RuntimeInfo *mRuntimeInfo; |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 67 | std::string mRemaining; |
| 68 | }; |
| 69 | |
| 70 | // decompress /proc/config.gz and read its contents. |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 71 | status_t RuntimeInfoFetcher::fetchKernelConfigs() { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 72 | gzFile f = gzopen(PROC_CONFIG, "rb"); |
| 73 | if (f == NULL) { |
| 74 | LOG(ERROR) << "Could not open /proc/config.gz: " << errno; |
| 75 | return -errno; |
| 76 | } |
| 77 | |
| 78 | char buf[BUFFER_SIZE]; |
| 79 | int len; |
| 80 | while ((len = gzread(f, buf, sizeof buf)) > 0) { |
| 81 | streamConfig(buf, len); |
| 82 | } |
| 83 | status_t err = OK; |
| 84 | if (len < 0) { |
| 85 | int errnum; |
| 86 | const char *errmsg = gzerror(f, &errnum); |
| 87 | LOG(ERROR) << "Could not read /proc/config.gz: " << errmsg; |
| 88 | err = (errnum == Z_ERRNO ? -errno : errnum); |
| 89 | } |
| 90 | |
| 91 | // stream a "\n" to end the stream to finish the last line. |
| 92 | streamConfig("\n", 1 /* sizeof "\n" */); |
| 93 | |
| 94 | gzclose(f); |
| 95 | return err; |
| 96 | } |
| 97 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 98 | void RuntimeInfoFetcher::parseConfig(std::string *s) { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 99 | removeTrailingComments(s); |
| 100 | trim(s); |
| 101 | if (s->empty()) { |
| 102 | return; |
| 103 | } |
| 104 | size_t equalPos = s->find('='); |
| 105 | if (equalPos == std::string::npos) { |
| 106 | LOG(WARNING) << "Unrecognized line in /proc/config.gz: " << *s; |
| 107 | return; |
| 108 | } |
| 109 | std::string key = s->substr(0, equalPos); |
| 110 | std::string value = s->substr(equalPos + 1); |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 111 | if (!mRuntimeInfo->mKernelConfigs.emplace(std::move(key), std::move(value)).second) { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 112 | LOG(WARNING) << "Duplicated key in /proc/config.gz: " << s->substr(0, equalPos); |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 117 | void RuntimeInfoFetcher::streamConfig(const char *buf, size_t len) { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 118 | const char *begin = buf; |
| 119 | const char *end = buf; |
| 120 | const char *stop = buf + len; |
| 121 | while (end < stop) { |
| 122 | if (*end == '\n') { |
| 123 | mRemaining.insert(mRemaining.size(), begin, end - begin); |
| 124 | parseConfig(&mRemaining); |
| 125 | mRemaining.clear(); |
| 126 | begin = end + 1; |
| 127 | } |
| 128 | end++; |
| 129 | } |
| 130 | mRemaining.insert(mRemaining.size(), begin, end - begin); |
| 131 | } |
| 132 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 133 | status_t RuntimeInfoFetcher::fetchCpuInfo() { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 134 | // TODO implement this; 32-bit and 64-bit has different format. |
| 135 | return OK; |
| 136 | } |
| 137 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 138 | status_t RuntimeInfoFetcher::fetchKernelSepolicyVers() { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 139 | int pv = security_policyvers(); |
| 140 | if (pv < 0) { |
| 141 | return pv; |
| 142 | } |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 143 | mRuntimeInfo->mKernelSepolicyVersion = pv; |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 144 | return OK; |
| 145 | } |
| 146 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 147 | status_t RuntimeInfoFetcher::fetchVersion() { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 148 | struct utsname buf; |
| 149 | if (uname(&buf)) { |
| 150 | return -errno; |
| 151 | } |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 152 | mRuntimeInfo->mOsName = buf.sysname; |
| 153 | mRuntimeInfo->mNodeName = buf.nodename; |
| 154 | mRuntimeInfo->mOsRelease = buf.release; |
| 155 | mRuntimeInfo->mOsVersion = buf.version; |
| 156 | mRuntimeInfo->mHardwareId = buf.machine; |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 157 | |
| 158 | status_t err = parseKernelVersion(); |
| 159 | if (err != OK) { |
| 160 | LOG(ERROR) << "Could not parse kernel version from \"" |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 161 | << mRuntimeInfo->mOsRelease << "\""; |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 162 | } |
| 163 | return err; |
| 164 | } |
| 165 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 166 | status_t RuntimeInfoFetcher::parseKernelVersion() { |
| 167 | auto pos = mRuntimeInfo->mOsRelease.find('.'); |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 168 | if (pos == std::string::npos) { |
| 169 | return UNKNOWN_ERROR; |
| 170 | } |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 171 | pos = mRuntimeInfo->mOsRelease.find('.', pos + 1); |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 172 | if (pos == std::string::npos) { |
| 173 | return UNKNOWN_ERROR; |
| 174 | } |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 175 | pos = mRuntimeInfo->mOsRelease.find_first_not_of("0123456789", pos + 1); |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 176 | // no need to check pos == std::string::npos, because substr will handle this |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 177 | if (!parse(mRuntimeInfo->mOsRelease.substr(0, pos), &mRuntimeInfo->mKernelVersion)) { |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 178 | return UNKNOWN_ERROR; |
| 179 | } |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 180 | return OK; |
| 181 | } |
| 182 | |
| 183 | // Grab sepolicy files. |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 184 | status_t RuntimeInfoFetcher::fetchSepolicyFiles() { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 185 | // TODO implement this |
| 186 | return OK; |
| 187 | } |
| 188 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 189 | status_t RuntimeInfoFetcher::fetchAllInformation() { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 190 | status_t err; |
| 191 | if ((err = fetchVersion()) != OK) { |
| 192 | return err; |
| 193 | } |
| 194 | if ((err = fetchKernelConfigs()) != OK) { |
| 195 | return err; |
| 196 | } |
| 197 | if ((err = fetchCpuInfo()) != OK) { |
| 198 | return err; |
| 199 | } |
| 200 | if ((err = fetchKernelSepolicyVers()) != OK) { |
| 201 | return err; |
| 202 | } |
| 203 | if ((err = fetchSepolicyFiles()) != OK) { |
| 204 | return err; |
| 205 | } |
| 206 | return OK; |
| 207 | } |
| 208 | |
| 209 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 210 | const std::string &RuntimeInfo::osName() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 211 | return mOsName; |
| 212 | } |
| 213 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 214 | const std::string &RuntimeInfo::nodeName() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 215 | return mNodeName; |
| 216 | } |
| 217 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 218 | const std::string &RuntimeInfo::osRelease() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 219 | return mOsRelease; |
| 220 | } |
| 221 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 222 | const std::string &RuntimeInfo::osVersion() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 223 | return mOsVersion; |
| 224 | } |
| 225 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 226 | const std::string &RuntimeInfo::hardwareId() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 227 | return mHardwareId; |
| 228 | } |
| 229 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 230 | size_t RuntimeInfo::kernelSepolicyVersion() const { |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 231 | return mKernelSepolicyVersion; |
| 232 | } |
| 233 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 234 | void RuntimeInfo::clear() { |
Yifan Hong | f1af752 | 2017-02-16 18:00:55 -0800 | [diff] [blame] | 235 | mKernelConfigs.clear(); |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 236 | mOsName.clear(); |
| 237 | mNodeName.clear(); |
| 238 | mOsRelease.clear(); |
| 239 | mOsVersion.clear(); |
| 240 | mHardwareId.clear(); |
| 241 | } |
| 242 | |
Yifan Hong | a7201e7 | 2017-02-17 10:09:59 -0800 | [diff] [blame] | 243 | bool RuntimeInfo::checkCompatibility(const CompatibilityMatrix &mat, |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 244 | std::string *error) const { |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame^] | 245 | if (mat.mType != SchemaType::FRAMEWORK) { |
| 246 | if (error != nullptr) { |
| 247 | *error = "Should not check runtime info against " + to_string(mat.mType) |
| 248 | + " compatibility matrix."; |
| 249 | } |
| 250 | return false; |
| 251 | } |
| 252 | if (kernelSepolicyVersion() != mat.framework.mSepolicy.kernelSepolicyVersion()) { |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 253 | if (error != nullptr) { |
| 254 | *error = "kernelSepolicyVersion = " + to_string(kernelSepolicyVersion()) |
Yifan Hong | 7c7d706 | 2017-04-04 16:26:51 -0700 | [diff] [blame^] | 255 | + " but required " + to_string(mat.framework.mSepolicy.kernelSepolicyVersion()); |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 256 | } |
| 257 | return false; |
| 258 | } |
| 259 | |
Yifan Hong | e8b7d95 | 2017-04-04 15:44:26 -0700 | [diff] [blame] | 260 | // TODO(b/35217573): check sepolicy version against mat.mSepolicy.sepolicyVersion() here. |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 261 | |
| 262 | const MatrixKernel *matrixKernel = mat.findKernel(this->mKernelVersion); |
| 263 | if (matrixKernel == nullptr) { |
| 264 | if (error != nullptr) { |
| 265 | *error = "Cannot find suitable kernel entry for " + to_string(mKernelVersion); |
| 266 | } |
| 267 | return false; |
| 268 | } |
| 269 | for (const KernelConfig &matrixConfig : matrixKernel->configs()) { |
| 270 | const std::string &key = matrixConfig.first; |
Yifan Hong | f1af752 | 2017-02-16 18:00:55 -0800 | [diff] [blame] | 271 | auto it = this->mKernelConfigs.find(key); |
| 272 | if (it == this->mKernelConfigs.end()) { |
Yifan Hong | c66ad1e | 2017-02-08 20:19:45 -0800 | [diff] [blame] | 273 | // special case: <value type="tristate">n</value> matches if the config doesn't exist. |
| 274 | if (matrixConfig.second == KernelConfigTypedValue::gMissingConfig) { |
| 275 | continue; |
| 276 | } |
| 277 | if (error != nullptr) { |
| 278 | *error = "Missing config " + key; |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | const std::string &kernelValue = it->second; |
| 283 | if (!matrixConfig.second.matchValue(kernelValue)) { |
| 284 | if (error != nullptr) { |
| 285 | *error = "For config " + key + ", value = " + kernelValue |
| 286 | + " but required " + to_string(matrixConfig.second); |
| 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | } |
| 291 | return true; |
| 292 | } |
| 293 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 294 | status_t RuntimeInfo::fetchAllInformation() { |
| 295 | return RuntimeInfoFetcher(this).fetchAllInformation(); |
Yifan Hong | ccf967b | 2017-01-18 11:04:19 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | } // namespace vintf |
| 299 | } // namespace android |