AccessControl.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #define LOG_TAG "hwservicemanager"
  17. #include <android-base/logging.h>
  18. #include <hidl-util/FQName.h>
  19. #include <log/log.h>
  20. #include "AccessControl.h"
  21. namespace android {
  22. static const char *kPermissionAdd = "add";
  23. static const char *kPermissionGet = "find";
  24. static const char *kPermissionList = "list";
  25. struct audit_data {
  26. const char* interfaceName;
  27. const char* sid;
  28. pid_t pid;
  29. };
  30. using android::FQName;
  31. AccessControl::AccessControl() {
  32. mSeHandle = selinux_android_hw_service_context_handle();
  33. LOG_ALWAYS_FATAL_IF(mSeHandle == nullptr, "Failed to acquire SELinux handle.");
  34. if (getcon(&mSeContext) != 0) {
  35. LOG_ALWAYS_FATAL("Failed to acquire hwservicemanager context.");
  36. }
  37. selinux_status_open(true);
  38. mSeCallbacks.func_audit = AccessControl::auditCallback;
  39. selinux_set_callback(SELINUX_CB_AUDIT, mSeCallbacks);
  40. mSeCallbacks.func_log = selinux_log_callback; /* defined in libselinux */
  41. selinux_set_callback(SELINUX_CB_LOG, mSeCallbacks);
  42. }
  43. bool AccessControl::canAdd(const std::string& fqName, const CallingContext& callingContext) {
  44. FQName fqIface;
  45. if (!FQName::parse(fqName, &fqIface)) {
  46. return false;
  47. }
  48. const std::string checkName = fqIface.package() + "::" + fqIface.name();
  49. return checkPermission(callingContext, kPermissionAdd, checkName.c_str());
  50. }
  51. bool AccessControl::canGet(const std::string& fqName, const CallingContext& callingContext) {
  52. FQName fqIface;
  53. if (!FQName::parse(fqName, &fqIface)) {
  54. return false;
  55. }
  56. const std::string checkName = fqIface.package() + "::" + fqIface.name();
  57. return checkPermission(callingContext, kPermissionGet, checkName.c_str());
  58. }
  59. bool AccessControl::canList(const CallingContext& callingContext) {
  60. return checkPermission(callingContext, mSeContext, kPermissionList, nullptr);
  61. }
  62. AccessControl::CallingContext AccessControl::getCallingContext(pid_t sourcePid) {
  63. char *sourceContext = nullptr;
  64. if (getpidcon(sourcePid, &sourceContext) < 0) {
  65. ALOGE("SELinux: failed to retrieve process context for pid %d", sourcePid);
  66. return { false, "", sourcePid };
  67. }
  68. std::string context = sourceContext;
  69. freecon(sourceContext);
  70. return { true, context, sourcePid };
  71. }
  72. bool AccessControl::checkPermission(const CallingContext& source, const char *targetContext, const char *perm, const char *interface) {
  73. if (!source.sidPresent) {
  74. return false;
  75. }
  76. bool allowed = false;
  77. struct audit_data ad;
  78. ad.pid = source.pid;
  79. ad.sid = source.sid.c_str();
  80. ad.interfaceName = interface;
  81. allowed = (selinux_check_access(source.sid.c_str(), targetContext, "hwservice_manager",
  82. perm, (void *) &ad) == 0);
  83. return allowed;
  84. }
  85. bool AccessControl::checkPermission(const CallingContext& source, const char *perm, const char *interface) {
  86. char *targetContext = nullptr;
  87. bool allowed = false;
  88. // Lookup service in hwservice_contexts
  89. if (selabel_lookup(mSeHandle, &targetContext, interface, 0) != 0) {
  90. ALOGE("No match for interface %s in hwservice_contexts", interface);
  91. return false;
  92. }
  93. allowed = checkPermission(source, targetContext, perm, interface);
  94. freecon(targetContext);
  95. return allowed;
  96. }
  97. int AccessControl::auditCallback(void *data, security_class_t /*cls*/, char *buf, size_t len) {
  98. struct audit_data *ad = (struct audit_data *)data;
  99. if (!ad || !ad->interfaceName) {
  100. ALOGE("No valid hwservicemanager audit data");
  101. return 0;
  102. }
  103. const char* sid = ad->sid ? ad->sid : "N/A";
  104. snprintf(buf, len, "interface=%s sid=%s pid=%d", ad->interfaceName, sid, ad->pid);
  105. return 0;
  106. }
  107. } // namespace android