PermissionCache.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2009 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 "PermissionCache"
  17. #include <stdint.h>
  18. #include <utils/Log.h>
  19. #include <binder/IPCThreadState.h>
  20. #include <binder/IServiceManager.h>
  21. #include <binder/PermissionCache.h>
  22. #include <utils/String8.h>
  23. namespace android {
  24. // ----------------------------------------------------------------------------
  25. ANDROID_SINGLETON_STATIC_INSTANCE(PermissionCache) ;
  26. // ----------------------------------------------------------------------------
  27. PermissionCache::PermissionCache() {
  28. }
  29. status_t PermissionCache::check(bool* granted,
  30. const String16& permission, uid_t uid) const {
  31. Mutex::Autolock _l(mLock);
  32. Entry e;
  33. e.name = permission;
  34. e.uid = uid;
  35. ssize_t index = mCache.indexOf(e);
  36. if (index >= 0) {
  37. *granted = mCache.itemAt(index).granted;
  38. return NO_ERROR;
  39. }
  40. return NAME_NOT_FOUND;
  41. }
  42. void PermissionCache::cache(const String16& permission,
  43. uid_t uid, bool granted) {
  44. Mutex::Autolock _l(mLock);
  45. Entry e;
  46. ssize_t index = mPermissionNamesPool.indexOf(permission);
  47. if (index > 0) {
  48. e.name = mPermissionNamesPool.itemAt(index);
  49. } else {
  50. mPermissionNamesPool.add(permission);
  51. e.name = permission;
  52. }
  53. // note, we don't need to store the pid, which is not actually used in
  54. // permission checks
  55. e.uid = uid;
  56. e.granted = granted;
  57. index = mCache.indexOf(e);
  58. if (index < 0) {
  59. mCache.add(e);
  60. }
  61. }
  62. void PermissionCache::purge() {
  63. Mutex::Autolock _l(mLock);
  64. mCache.clear();
  65. }
  66. bool PermissionCache::checkCallingPermission(const String16& permission) {
  67. return PermissionCache::checkCallingPermission(permission, NULL, NULL);
  68. }
  69. bool PermissionCache::checkCallingPermission(
  70. const String16& permission, int32_t* outPid, int32_t* outUid) {
  71. IPCThreadState* ipcState = IPCThreadState::self();
  72. pid_t pid = ipcState->getCallingPid();
  73. uid_t uid = ipcState->getCallingUid();
  74. if (outPid) *outPid = pid;
  75. if (outUid) *outUid = uid;
  76. return PermissionCache::checkPermission(permission, pid, uid);
  77. }
  78. bool PermissionCache::checkPermission(
  79. const String16& permission, pid_t pid, uid_t uid) {
  80. if ((uid == 0) || (pid == getpid())) {
  81. // root and ourselves is always okay
  82. return true;
  83. }
  84. PermissionCache& pc(PermissionCache::getInstance());
  85. bool granted = false;
  86. if (pc.check(&granted, permission, uid) != NO_ERROR) {
  87. nsecs_t t = -systemTime();
  88. granted = android::checkPermission(permission, pid, uid);
  89. t += systemTime();
  90. ALOGD("checking %s for uid=%d => %s (%d us)",
  91. String8(permission).string(), uid,
  92. granted?"granted":"denied", (int)ns2us(t));
  93. pc.cache(permission, uid, granted);
  94. }
  95. return granted;
  96. }
  97. // ---------------------------------------------------------------------------
  98. }; // namespace android