IPermissionController.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2005 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 "PermissionController"
  17. #include <binder/IPermissionController.h>
  18. #include <utils/Log.h>
  19. #include <binder/Parcel.h>
  20. #include <utils/String8.h>
  21. #include <private/binder/Static.h>
  22. namespace android {
  23. // ----------------------------------------------------------------------
  24. class BpPermissionController : public BpInterface<IPermissionController>
  25. {
  26. public:
  27. explicit BpPermissionController(const sp<IBinder>& impl)
  28. : BpInterface<IPermissionController>(impl)
  29. {
  30. }
  31. virtual bool checkPermission(const String16& permission, int32_t pid, int32_t uid)
  32. {
  33. Parcel data, reply;
  34. data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
  35. data.writeString16(permission);
  36. data.writeInt32(pid);
  37. data.writeInt32(uid);
  38. remote()->transact(CHECK_PERMISSION_TRANSACTION, data, &reply);
  39. // fail on exception
  40. if (reply.readExceptionCode() != 0) return 0;
  41. return reply.readInt32() != 0;
  42. }
  43. virtual int32_t noteOp(const String16& op, int32_t uid, const String16& packageName)
  44. {
  45. Parcel data, reply;
  46. data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
  47. data.writeString16(op);
  48. data.writeInt32(uid);
  49. data.writeString16(packageName);
  50. remote()->transact(NOTE_OP_TRANSACTION, data, &reply);
  51. // fail on exception
  52. if (reply.readExceptionCode() != 0) return 2; // MODE_ERRORED
  53. return reply.readInt32();
  54. }
  55. virtual void getPackagesForUid(const uid_t uid, Vector<String16>& packages)
  56. {
  57. Parcel data, reply;
  58. data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
  59. data.writeInt32(uid);
  60. remote()->transact(GET_PACKAGES_FOR_UID_TRANSACTION, data, &reply);
  61. // fail on exception
  62. if (reply.readExceptionCode() != 0) {
  63. return;
  64. }
  65. const int32_t size = reply.readInt32();
  66. if (size <= 0) {
  67. return;
  68. }
  69. for (int i = 0; i < size; i++) {
  70. packages.push(reply.readString16());
  71. }
  72. }
  73. virtual bool isRuntimePermission(const String16& permission)
  74. {
  75. Parcel data, reply;
  76. data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
  77. data.writeString16(permission);
  78. remote()->transact(IS_RUNTIME_PERMISSION_TRANSACTION, data, &reply);
  79. // fail on exception
  80. if (reply.readExceptionCode() != 0) return false;
  81. return reply.readInt32() != 0;
  82. }
  83. virtual int getPackageUid(const String16& package, int flags)
  84. {
  85. Parcel data, reply;
  86. data.writeInterfaceToken(IPermissionController::getInterfaceDescriptor());
  87. data.writeString16(package);
  88. data.writeInt32(flags);
  89. remote()->transact(GET_PACKAGE_UID_TRANSACTION, data, &reply);
  90. // fail on exception
  91. if (reply.readExceptionCode() != 0) return false;
  92. return reply.readInt32();
  93. }
  94. };
  95. IMPLEMENT_META_INTERFACE(PermissionController, "android.os.IPermissionController");
  96. // ----------------------------------------------------------------------
  97. // NOLINTNEXTLINE(google-default-arguments)
  98. status_t BnPermissionController::onTransact(
  99. uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
  100. {
  101. switch(code) {
  102. case CHECK_PERMISSION_TRANSACTION: {
  103. CHECK_INTERFACE(IPermissionController, data, reply);
  104. String16 permission = data.readString16();
  105. int32_t pid = data.readInt32();
  106. int32_t uid = data.readInt32();
  107. bool res = checkPermission(permission, pid, uid);
  108. reply->writeNoException();
  109. reply->writeInt32(res ? 1 : 0);
  110. return NO_ERROR;
  111. } break;
  112. case NOTE_OP_TRANSACTION: {
  113. CHECK_INTERFACE(IPermissionController, data, reply);
  114. String16 op = data.readString16();
  115. int32_t uid = data.readInt32();
  116. String16 packageName = data.readString16();
  117. int32_t res = noteOp(op, uid, packageName);
  118. reply->writeNoException();
  119. reply->writeInt32(res);
  120. return NO_ERROR;
  121. } break;
  122. case GET_PACKAGES_FOR_UID_TRANSACTION: {
  123. CHECK_INTERFACE(IPermissionController, data, reply);
  124. int32_t uid = data.readInt32();
  125. Vector<String16> packages;
  126. getPackagesForUid(uid, packages);
  127. reply->writeNoException();
  128. size_t size = packages.size();
  129. reply->writeInt32(size);
  130. for (size_t i = 0; i < size; i++) {
  131. reply->writeString16(packages[i]);
  132. }
  133. return NO_ERROR;
  134. } break;
  135. case IS_RUNTIME_PERMISSION_TRANSACTION: {
  136. CHECK_INTERFACE(IPermissionController, data, reply);
  137. String16 permission = data.readString16();
  138. const bool res = isRuntimePermission(permission);
  139. reply->writeNoException();
  140. reply->writeInt32(res ? 1 : 0);
  141. return NO_ERROR;
  142. } break;
  143. case GET_PACKAGE_UID_TRANSACTION: {
  144. CHECK_INTERFACE(IPermissionController, data, reply);
  145. String16 package = data.readString16();
  146. int flags = data.readInt32();
  147. const int uid = getPackageUid(package, flags);
  148. reply->writeNoException();
  149. reply->writeInt32(uid);
  150. return NO_ERROR;
  151. } break;
  152. default:
  153. return BBinder::onTransact(code, data, reply, flags);
  154. }
  155. }
  156. }; // namespace android