| #include <linux/stat.h> |
| 6 |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <unistd.h> |
| 10 | #include <fcntl.h> |
| 11 |
| 12 | #include "read_cert.h" |
| 13 |
| 14 | std::string read_certificate(int fd) { |
| 15 |
| 16 | uint32_t size4; |
| 17 | uint64_t size8; |
| 18 |
| 19 | // Find EOCD |
| 20 | for (int i = 0;; i++) { |
| 21 | // i is the absolute offset to end of file |
| 22 | uint16_t comment_sz = 0; |
| 23 | lseek(fd, -((off_t) sizeof(comment_sz)) - i, SEEK_END); |
| 24 | read(fd, &comment_sz, sizeof(comment_sz)); |
| 25 | if (comment_sz == i) { |
| 26 | // Double check if we actually found the structure |
| 27 | lseek(fd, -((off_t) sizeof(EOCD)), SEEK_CUR); |
| 28 | uint32_t magic = 0; |
| 29 | read(fd, &magic, sizeof(magic)); |
| 30 | if (magic == EOCD_MAGIC) { |
| 31 | break; |
| 32 | } |
| 33 | } |
| 34 |
| 35 | if (i == 0xffff) { |
| 36 | // Comments cannot be longer than 0xffff (overflow), abort |
| 37 | return {}; |
| 38 | } |
| 39 | } |
| 40 |
| 41 | // We are now at EOCD + sizeof(magic) |
| 42 | // Seek and read central_dir_off to find start of central directory |
| 43 |
| 44 | uint32_t central_dir_off = 0; |
| 45 | { |
| 46 | constexpr off_t off = offsetof(EOCD, central_dir_off) - sizeof(EOCD::magic); |
| 47 | lseek(fd, off, SEEK_CUR); |
| 48 | } |
| 49 |
| 50 | read(fd, ¢ral_dir_off, sizeof(central_dir_off)); |
| 51 |
| 52 | // Next, find the start of the APK signing block |
| 53 | { |
| 54 | constexpr int off = sizeof(signing_block::block_sz_) + sizeof(signing_block::magic); |
| 55 | lseek(fd, (off_t) (central_dir_off - off), SEEK_SET); |
| 56 | } |
| 57 |
| 58 | read(fd, &size8, sizeof(size8)); // size8 = block_sz_ |
| 59 | char magic[sizeof(signing_block::magic)] = {0}; |
| 60 | read(fd, magic, sizeof(magic)); |
| 61 |
| 62 | if (memcmp(magic, APK_SIGNING_BLOCK_MAGIC, sizeof(magic)) != 0) { |
| 63 | // Invalid signing block magic, abort |
| 64 | return {}; |
| 65 | } |
| 66 |
| 67 | uint64_t signing_blk_sz = 0; |
| 68 | lseek(fd, (off_t) (central_dir_off - size8 - sizeof(signing_blk_sz)), SEEK_SET); |
| 69 | read(fd, &signing_blk_sz, sizeof(signing_blk_sz)); |
| 70 |
| 71 | if (signing_blk_sz != size8) { |
| 72 | // block_sz != block_sz_, invalid signing block format, abort |
| 73 | return {}; |
| 74 | } |
| 75 | // Finally, we are now at the beginning of the id-value pair sequence |
| 76 | for (;;) { |
| 77 | read(fd, &size8, sizeof(size8)); // id-value pair length |
| 78 | if (size8 == signing_blk_sz) { |
| 79 | // Outside of the id-value pair sequence; actually reading block_sz_ |
| 80 | break; |
| 81 | } |
| 82 | uint32_t id; |
| 83 | read(fd, &id, sizeof(id)); |
| 84 | if (id == SIGNATURE_SCHEME_V2_MAGIC) { |
| 85 | read(fd, &size4, sizeof(size4)); // signer sequence length |
| 86 | read(fd, &size4, sizeof(size4)); // signer length |
| 87 | read(fd, &size4, sizeof(size4)); // signed data length |
| 88 | read(fd, &size4, sizeof(size4)); // digest sequence length |
| 89 | lseek(fd, (off_t) (size4), SEEK_CUR); // skip all digests |
| 90 | read(fd, &size4, sizeof(size4)); // cert sequence length |
| 91 | read(fd, &size4, sizeof(size4)); // cert length |
| 92 | std::string cert; |
| 93 | cert.resize(size4); |
| 94 | read(fd, (void *) cert.data(), size4); |
| 95 | return cert; |
| 96 | } else { |
| 97 | // Skip this id-value pair |
| 98 | lseek(fd, (off_t) (size8 - sizeof(id)), SEEK_CUR); |
| 99 | } |
| 100 | } |
| 101 | return {}; |
| 102 | } |
| 103 |
| 104 | bool stat_c_check(int* uid, int* gid, const char* path) { |
| 105 | LOGI("use c stat check"); |
| 106 | struct stat st = {0}; |
| 107 | int res1 = stat(path, &st); |
| 108 | if (res1 < 0) { |
| 109 | LOGE("Failed to stat file %s", path); |
| 110 | return false; |
| 111 | } |
| 112 | uid_t _uid = st.st_uid; |
| 113 | gid_t _gid = st.st_gid; |
| 114 |
| 115 | if (_uid <= INT_MAX && _gid <= INT_MAX) { |
| 116 | *uid = (int) _uid; |
| 117 | *gid = (int) _gid; |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 |
| 122 | bool stat_system_check(int* uid, int* gid, const char* path) { |
| 123 | LOGI("use system statx check"); |
| 124 | struct statx st = {0}; |
| 125 | long res = syscall(__NR_statx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT, STATX_BTIME, &st); |
| 126 | if (res < 0) { |
| 127 | LOGE("Failed to statx file %s", path); |
| 128 | return false; |
| 129 | } |
| 130 | uid_t _uid = st.stx_uid; |
| 131 | gid_t _gid = st.stx_gid; |
| 132 |
| 133 | if (_uid <= INT_MAX && _gid <= INT_MAX) { |
| 134 | *uid = (int) _uid; |
| 135 | *gid = (int) _gid; |
| 136 | } |
| 137 | return true; |
| 138 | } |
| 139 |
| 140 | bool more_check(int fd, const char* path) { |
| 141 | // check apk path |
| 142 | char buff[PATH_MAX] = {0}; |
| 143 | std::string fdPath("/proc/"); |
| 144 | fdPath.append(std::to_string(getpid())).append("/fd/").append(std::to_string(fd)); |
| 145 | long len = syscall(__NR_readlinkat, AT_FDCWD, fdPath.c_str(), buff, PATH_MAX); |
| 146 | if (len < 0) { |
| 147 | LOGE("Failed to read link for fd %d", fd); |
| 148 | return false; |
| 149 | } |
| 150 | buff[len] = '\0'; |
| 151 | if (strcmp(path, buff) != 0) { |
| 152 | LOGE("Path mismatch: expected '%s', got '%s'", path, buff); |
| 153 | return false; |
| 154 | } |
| 155 | int uid; |
| 156 | int gid; |
| 157 |
| 158 | char sdk[128] = "0"; |
| 159 | __system_property_get("ro.build.version.sdk", sdk); |
| 160 | int sdk_version = atoi(sdk); |
| 161 |
| 162 | if (sdk_version < 30) { |
| 163 | stat_c_check(&uid, &gid, path); |
| 164 | } else { |
| 165 | if (!stat_system_check(&uid, &gid, path)) { |
| 166 | stat_c_check(&uid, &gid, path); |
| 167 | } |
| 168 | } |
| 169 | // check uid&gid |
| 170 | if (uid != 1000 && gid != 1000) { |
| 171 | LOGE("UID/GID check failed for fd %d: UID=%d, GID=%d", fd, uid, gid); |
| 172 | return false; |
| 173 | } |
| 174 | return true; |
| 175 | } |
| 176 |