| 1 | #include <jni.h> |
| 2 | #include <android/log.h> |
| 3 | #include <sys/syscall.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include "read_cert.h" |
| 7 | |
| 8 | std::string read_certificate(int fd) { |
| 9 | |
| 10 | uint32_t size4; |
| 11 | uint64_t size8; |
| 12 | |
| 13 | // Find EOCD |
| 14 | for (int i = 0;; i++) { |
| 15 | // i is the absolute offset to end of file |
| 16 | uint16_t comment_sz = 0; |
| 17 | lseek(fd, -((off_t) sizeof(comment_sz)) - i, SEEK_END); |
| 18 | read(fd, &comment_sz, sizeof(comment_sz)); |
| 19 | if (comment_sz == i) { |
| 20 | // Double check if we actually found the structure |
| 21 | lseek(fd, -((off_t) sizeof(EOCD)), SEEK_CUR); |
| 22 | uint32_t magic = 0; |
| 23 | read(fd, &magic, sizeof(magic)); |
| 24 | if (magic == EOCD_MAGIC) { |
| 25 | break; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | if (i == 0xffff) { |
| 30 | // Comments cannot be longer than 0xffff (overflow), abort |
| 31 | return {}; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // We are now at EOCD + sizeof(magic) |
| 36 | // Seek and read central_dir_off to find start of central directory |
| 37 | |
| 38 | uint32_t central_dir_off = 0; |
| 39 | { |
| 40 | constexpr off_t off = offsetof(EOCD, central_dir_off) - sizeof(EOCD::magic); |
| 41 | lseek(fd, off, SEEK_CUR); |
| 42 | } |
| 43 | |
| 44 | read(fd, ¢ral_dir_off, sizeof(central_dir_off)); |
| 45 | |
| 46 | // Next, find the start of the APK signing block |
| 47 | { |
| 48 | constexpr int off = sizeof(signing_block::block_sz_) + sizeof(signing_block::magic); |
| 49 | lseek(fd, (off_t) (central_dir_off - off), SEEK_SET); |
| 50 | } |
| 51 | |
| 52 | read(fd, &size8, sizeof(size8)); // size8 = block_sz_ |
| 53 | char magic[sizeof(signing_block::magic)] = {0}; |
| 54 | read(fd, magic, sizeof(magic)); |
| 55 | |
| 56 | if (memcmp(magic, APK_SIGNING_BLOCK_MAGIC, sizeof(magic)) != 0) { |
| 57 | // Invalid signing block magic, abort |
| 58 | return {}; |
| 59 | } |
| 60 | |
| 61 | uint64_t signing_blk_sz = 0; |
| 62 | lseek(fd, (off_t) (central_dir_off - size8 - sizeof(signing_blk_sz)), SEEK_SET); |
| 63 | read(fd, &signing_blk_sz, sizeof(signing_blk_sz)); |
| 64 | |
| 65 | if (signing_blk_sz != size8) { |
| 66 | // block_sz != block_sz_, invalid signing block format, abort |
| 67 | return {}; |
| 68 | } |
| 69 | // Finally, we are now at the beginning of the id-value pair sequence |
| 70 | for (;;) { |
| 71 | read(fd, &size8, sizeof(size8)); // id-value pair length |
| 72 | if (size8 == signing_blk_sz) { |
| 73 | // Outside of the id-value pair sequence; actually reading block_sz_ |
| 74 | break; |
| 75 | } |
| 76 | uint32_t id; |
| 77 | read(fd, &id, sizeof(id)); |
| 78 | if (id == SIGNATURE_SCHEME_V2_MAGIC) { |
| 79 | read(fd, &size4, sizeof(size4)); // signer sequence length |
| 80 | read(fd, &size4, sizeof(size4)); // signer length |
| 81 | read(fd, &size4, sizeof(size4)); // signed data length |
| 82 | read(fd, &size4, sizeof(size4)); // digest sequence length |
| 83 | lseek(fd, (off_t) (size4), SEEK_CUR); // skip all digests |
| 84 | read(fd, &size4, sizeof(size4)); // cert sequence length |
| 85 | read(fd, &size4, sizeof(size4)); // cert length |
| 86 | std::string cert; |
| 87 | cert.resize(size4); |
| 88 | read(fd, (void *) cert.data(), size4); |
| 89 | return cert; |
| 90 | } else { |
| 91 | // Skip this id-value pair |
| 92 | lseek(fd, (off_t) (size8 - sizeof(id)), SEEK_CUR); |
| 93 | } |
| 94 | } |
| 95 | return {}; |
| 96 | } |
| 97 | |
| 98 | bool more_check(int fd, const char* path) { |
| 99 | // check apk path |
| 100 | char buff[PATH_MAX] = {0}; |
| 101 | std::string fdPath("/proc/"); |
| 102 | fdPath.append(std::to_string(getpid())).append("/fd/").append(std::to_string(fd)); |
| 103 | long len = syscall(__NR_readlinkat, AT_FDCWD, fdPath.c_str(), buff, PATH_MAX); |
| 104 | if (len < 0) { |
| 105 | LOGE("Failed to read link for fd %d", fd); |
| 106 | return false; |
| 107 | } |
| 108 | buff[len] = '\0'; |
| 109 | if (strcmp(path, buff) != 0) { |
| 110 | LOGE("Path mismatch: expected '%s', got '%s'", path, buff); |
| 111 | return false; |
| 112 | } |
| 113 | // fstat |
| 114 | struct stat statBuff = {0}; |
| 115 | long stat = syscall(__NR_fstat, fd, &statBuff); |
| 116 | if (stat < 0) { |
| 117 | LOGE("Failed to fstat fd %d", fd); |
| 118 | return false; |
| 119 | } |
| 120 | // check uid&gid |
| 121 | if (statBuff.st_uid != 1000 && statBuff.st_gid != 1000) { |
| 122 | LOGE("UID/GID check failed for fd %d: UID=%d, GID=%d", fd, statBuff.st_uid, statBuff.st_gid); |
| 123 | return false; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |