test_utils.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #pragma once
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/capability.h>
  20. #include <android-base/logging.h>
  21. #include <android-base/stringprintf.h>
  22. #include <selinux/android.h>
  23. uint8_t kBase64Map[256] = {
  24. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  25. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  26. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  27. 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
  28. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
  29. 255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
  30. 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  31. 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
  32. 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
  33. 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  34. 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  35. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  36. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  37. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  38. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  39. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  40. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  41. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  42. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  43. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  44. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  45. 255, 255, 255, 255
  46. };
  47. uint8_t* DecodeBase64(const char* src, size_t* dst_size) {
  48. CHECK(dst_size != nullptr);
  49. std::vector<uint8_t> tmp;
  50. uint32_t t = 0, y = 0;
  51. int g = 3;
  52. for (size_t i = 0; src[i] != '\0'; ++i) {
  53. uint8_t c = kBase64Map[src[i] & 0xFF];
  54. if (c == 255) continue;
  55. // the final = symbols are read and used to trim the remaining bytes
  56. if (c == 254) {
  57. c = 0;
  58. // prevent g < 0 which would potentially allow an overflow later
  59. if (--g < 0) {
  60. *dst_size = 0;
  61. return nullptr;
  62. }
  63. } else if (g != 3) {
  64. // we only allow = to be at the end
  65. *dst_size = 0;
  66. return nullptr;
  67. }
  68. t = (t << 6) | c;
  69. if (++y == 4) {
  70. tmp.push_back((t >> 16) & 255);
  71. if (g > 1) {
  72. tmp.push_back((t >> 8) & 255);
  73. }
  74. if (g > 2) {
  75. tmp.push_back(t & 255);
  76. }
  77. y = t = 0;
  78. }
  79. }
  80. if (y != 0) {
  81. *dst_size = 0;
  82. return nullptr;
  83. }
  84. std::unique_ptr<uint8_t[]> dst(new uint8_t[tmp.size()]);
  85. *dst_size = tmp.size();
  86. std::copy(tmp.begin(), tmp.end(), dst.get());
  87. return dst.release();
  88. }
  89. bool WriteBase64ToFile(const char* base64, const std::string& file,
  90. uid_t uid, gid_t gid, int mode, std::string* error_msg) {
  91. CHECK(base64 != nullptr);
  92. size_t length;
  93. std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
  94. CHECK(bytes != nullptr);
  95. int fd = open(file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  96. using android::base::StringPrintf;
  97. if (fd < 0) {
  98. *error_msg = StringPrintf("Could not open file %s: %s", file.c_str(), strerror(errno));
  99. return false;
  100. }
  101. size_t wrote = 0;
  102. while (wrote < length) {
  103. ssize_t cur = write(fd, bytes.get() + wrote, length - wrote);
  104. if (cur == -1) {
  105. *error_msg = StringPrintf("Could not write file %s: %s", file.c_str(), strerror(errno));
  106. return false;
  107. }
  108. wrote += cur;
  109. }
  110. if (::chown(file.c_str(), uid, gid) != 0) {
  111. *error_msg = StringPrintf("Could not chown file %s: %s", file.c_str(), strerror(errno));
  112. return false;
  113. }
  114. if (::chmod(file.c_str(), mode) != 0) {
  115. *error_msg = StringPrintf("Could not chmod file %s: %s", file.c_str(), strerror(errno));
  116. return false;
  117. }
  118. return true;
  119. }
  120. // TODO(calin): fix dexopt drop_capabilities and move to general utils (b/69678790).
  121. bool DropCapabilities(uid_t uid, gid_t gid) {
  122. if (setgid(gid) != 0) {
  123. PLOG(ERROR) << "setgid failed: " << gid;
  124. return false;
  125. }
  126. if (setuid(uid) != 0) {
  127. PLOG(ERROR) << "setuid failed: " << uid;
  128. return false;
  129. }
  130. // drop capabilities
  131. struct __user_cap_header_struct capheader;
  132. struct __user_cap_data_struct capdata[2];
  133. memset(&capheader, 0, sizeof(capheader));
  134. memset(&capdata, 0, sizeof(capdata));
  135. capheader.version = _LINUX_CAPABILITY_VERSION_3;
  136. if (capset(&capheader, &capdata[0]) < 0) {
  137. PLOG(ERROR) << "capset failed";
  138. return false;
  139. }
  140. return true;
  141. }