grant_store.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "grant_store.h"
  17. #include "blob.h"
  18. #include <algorithm>
  19. #include <sstream>
  20. namespace keystore {
  21. static constexpr uint64_t kInvalidGrantNo = std::numeric_limits<uint64_t>::max();
  22. static const char* kKeystoreGrantInfix = "_KEYSTOREGRANT_";
  23. static constexpr size_t kKeystoreGrantInfixLength = 15;
  24. Grant::Grant(const KeyBlobEntry& entry, const uint64_t grant_no)
  25. : entry_(entry), grant_no_(grant_no) {}
  26. static std::pair<uint64_t, std::string> parseGrantAlias(const std::string& grantAlias) {
  27. auto pos = grantAlias.rfind(kKeystoreGrantInfix);
  28. if (pos == std::string::npos) return {kInvalidGrantNo, ""};
  29. std::stringstream s(grantAlias.substr(pos + kKeystoreGrantInfixLength));
  30. std::string wrapped_alias = grantAlias.substr(0, pos);
  31. uint64_t grant_no = kInvalidGrantNo;
  32. s >> grant_no;
  33. if (s.fail() || grant_no == kInvalidGrantNo) return {kInvalidGrantNo, ""};
  34. return {grant_no, wrapped_alias};
  35. }
  36. std::string GrantStore::put(const uid_t uid, const LockedKeyBlobEntry& lockedEntry) {
  37. std::unique_lock<std::shared_mutex> lock(mutex_);
  38. std::stringstream s;
  39. KeyBlobEntry blobEntry = *lockedEntry;
  40. s << blobEntry.alias() << kKeystoreGrantInfix;
  41. std::set<Grant, std::less<>>& uid_grant_list = grants_[uid];
  42. bool success = false;
  43. auto iterator =
  44. std::find_if(uid_grant_list.begin(), uid_grant_list.end(),
  45. [&](const Grant& entry) { return success = entry.entry_ == blobEntry; });
  46. while (!success) {
  47. std::tie(iterator, success) = uid_grant_list.emplace(blobEntry, std::rand());
  48. }
  49. s << iterator->grant_no_;
  50. return s.str();
  51. }
  52. ReadLockedGrant GrantStore::get(const uid_t uid, const std::string& alias) const {
  53. std::shared_lock<std::shared_mutex> lock(mutex_);
  54. uint64_t grant_no;
  55. std::string wrappedAlias;
  56. std::tie(grant_no, wrappedAlias) = parseGrantAlias(alias);
  57. if (grant_no == kInvalidGrantNo) return {};
  58. auto uid_set_iter = grants_.find(uid);
  59. if (uid_set_iter == grants_.end()) return {};
  60. auto& uid_grant_list = uid_set_iter->second;
  61. auto grant = uid_grant_list.find(grant_no);
  62. if (grant == uid_grant_list.end()) return {};
  63. if (grant->entry_.alias() != wrappedAlias) return {};
  64. return {&(*grant), std::move(lock)};
  65. }
  66. bool GrantStore::removeByFileAlias(const uid_t granteeUid, const LockedKeyBlobEntry& lockedEntry) {
  67. std::unique_lock<std::shared_mutex> lock(mutex_);
  68. auto& uid_grant_list = grants_[granteeUid];
  69. for (auto i = uid_grant_list.begin(); i != uid_grant_list.end(); ++i) {
  70. if (i->entry_ == *lockedEntry) {
  71. uid_grant_list.erase(i);
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. void GrantStore::removeAllGrantsToKey(const uid_t granterUid, const std::string& alias) {
  78. std::unique_lock<std::shared_mutex> lock(mutex_);
  79. for (auto& uid_grant_list : grants_) {
  80. for (auto i = uid_grant_list.second.begin(); i != uid_grant_list.second.end(); ++i) {
  81. if (i->entry_.alias() == alias && i->entry_.uid() == granterUid) {
  82. uid_grant_list.second.erase(i);
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. void GrantStore::removeAllGrantsToUid(const uid_t granteeUid) {
  89. std::unique_lock<std::shared_mutex> lock(mutex_);
  90. grants_.erase(granteeUid);
  91. }
  92. } // namespace keystore