binder_security_policy.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2021 gRPC authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef GRPCPP_SECURITY_BINDER_SECURITY_POLICY_H
  15. #define GRPCPP_SECURITY_BINDER_SECURITY_POLICY_H
  16. #include <memory>
  17. #ifdef GPR_ANDROID
  18. #include <jni.h>
  19. #endif
  20. namespace grpc {
  21. namespace experimental {
  22. namespace binder {
  23. // EXPERIMENTAL Determinines if a connection is allowed to be
  24. // established on Android. See https://source.android.com/security/app-sandbox
  25. // for more info about UID.
  26. class SecurityPolicy {
  27. public:
  28. virtual ~SecurityPolicy() = default;
  29. // Returns true if the UID is authorized to connect.
  30. // Must return the same value for the same inputs so callers can safely cache
  31. // the result.
  32. virtual bool IsAuthorized(int uid) = 0;
  33. };
  34. // EXPERIMENTAL Allows all connection. Anything on the Android device will be
  35. // able to connect, use with caution!
  36. class UntrustedSecurityPolicy : public SecurityPolicy {
  37. public:
  38. UntrustedSecurityPolicy();
  39. ~UntrustedSecurityPolicy() override;
  40. bool IsAuthorized(int uid) override;
  41. };
  42. // EXPERIMENTAL Only allows the connections from processes with the same UID. In
  43. // most cases this means "from the same APK".
  44. class InternalOnlySecurityPolicy : public SecurityPolicy {
  45. public:
  46. InternalOnlySecurityPolicy();
  47. ~InternalOnlySecurityPolicy() override;
  48. bool IsAuthorized(int uid) override;
  49. };
  50. #ifdef GPR_ANDROID
  51. // EXPERIMENTAL Only allows the connections from the APK that have the same
  52. // signature.
  53. class SameSignatureSecurityPolicy : public SecurityPolicy {
  54. public:
  55. // `context` is required for getting PackageManager Java class
  56. SameSignatureSecurityPolicy(JavaVM* jvm, jobject context);
  57. ~SameSignatureSecurityPolicy() override;
  58. bool IsAuthorized(int uid) override;
  59. private:
  60. JavaVM* jvm_;
  61. jobject context_;
  62. };
  63. #endif
  64. } // namespace binder
  65. } // namespace experimental
  66. } // namespace grpc
  67. #endif // GRPCPP_SECURITY_BINDER_SECURITY_POLICY_H