fake_permission_broker_client.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "chromeos/dbus/permission_broker/fake_permission_broker_client.h"
  5. #include <fcntl.h>
  6. #include <linux/usbdevice_fs.h>
  7. #include <stdint.h>
  8. #include <sys/ioctl.h>
  9. #include "base/bind.h"
  10. #include "base/callback.h"
  11. #include "base/check_op.h"
  12. #include "base/containers/contains.h"
  13. #include "base/location.h"
  14. #include "base/logging.h"
  15. #include "base/posix/eintr_wrapper.h"
  16. #include "base/strings/stringprintf.h"
  17. #include "base/task/thread_pool.h"
  18. #include "base/threading/thread_task_runner_handle.h"
  19. #include "base/unguessable_token.h"
  20. namespace chromeos {
  21. namespace {
  22. constexpr char kOpenFailedError[] = "open_failed";
  23. constexpr char kDupFailedError[] = "dup_failed";
  24. constexpr char kWatchLifelineFdFailedError[] = "watch_lifeline_fd_failed";
  25. FakePermissionBrokerClient* g_instance = nullptr;
  26. // So that real devices can be accessed by tests and "Chromium OS on Linux" this
  27. // function implements a simplified version of the method implemented by the
  28. // permission broker by opening the path specified and returning the resulting
  29. // file descriptor.
  30. void OpenPath(const std::string& path,
  31. PermissionBrokerClient::OpenPathCallback callback,
  32. PermissionBrokerClient::ErrorCallback error_callback,
  33. scoped_refptr<base::TaskRunner> task_runner) {
  34. base::ScopedFD fd(HANDLE_EINTR(open(path.c_str(), O_RDWR)));
  35. if (!fd.is_valid()) {
  36. int error_code = logging::GetLastSystemErrorCode();
  37. task_runner->PostTask(
  38. FROM_HERE,
  39. base::BindOnce(
  40. std::move(error_callback), kOpenFailedError,
  41. base::StringPrintf(
  42. "Failed to open '%s': %s", path.c_str(),
  43. logging::SystemErrorCodeToString(error_code).c_str())));
  44. return;
  45. }
  46. task_runner->PostTask(FROM_HERE,
  47. base::BindOnce(std::move(callback), std::move(fd)));
  48. }
  49. bool DisconnectInterface(const std::string& path, uint8_t iface_num) {
  50. base::ScopedFD fd(HANDLE_EINTR(open(path.c_str(), O_RDWR)));
  51. if (!fd.is_valid()) {
  52. PLOG(ERROR) << "Failed to open path " << path;
  53. return false;
  54. }
  55. struct usbdevfs_ioctl dio = {};
  56. dio.ifno = iface_num;
  57. dio.ioctl_code = USBDEVFS_DISCONNECT;
  58. dio.data = nullptr;
  59. int rc = HANDLE_EINTR(ioctl(fd.get(), USBDEVFS_IOCTL, &dio));
  60. if (rc < 0) {
  61. PLOG(ERROR) << "Failed to disconnect interface "
  62. << static_cast<int>(iface_num) << " on path " << path;
  63. return false;
  64. }
  65. return true;
  66. }
  67. bool ConnectInterface(const std::string& path, uint8_t iface_num) {
  68. base::ScopedFD fd(HANDLE_EINTR(open(path.c_str(), O_RDWR)));
  69. if (!fd.is_valid()) {
  70. PLOG(ERROR) << "Failed to open path " << path;
  71. return false;
  72. }
  73. struct usbdevfs_ioctl dio = {};
  74. dio.ifno = iface_num;
  75. dio.ioctl_code = USBDEVFS_CONNECT;
  76. dio.data = nullptr;
  77. int rc = HANDLE_EINTR(ioctl(fd.get(), USBDEVFS_IOCTL, &dio));
  78. if (rc < 0) {
  79. PLOG(ERROR) << "Failed to connect interface " << static_cast<int>(iface_num)
  80. << " on path " << path;
  81. return false;
  82. }
  83. return true;
  84. }
  85. } // namespace
  86. FakePermissionBrokerClient::FakePermissionBrokerClient() {
  87. DCHECK(!g_instance);
  88. g_instance = this;
  89. }
  90. FakePermissionBrokerClient::~FakePermissionBrokerClient() {
  91. DCHECK_EQ(this, g_instance);
  92. g_instance = nullptr;
  93. }
  94. // static
  95. FakePermissionBrokerClient* FakePermissionBrokerClient::Get() {
  96. DCHECK(g_instance);
  97. return g_instance;
  98. }
  99. void FakePermissionBrokerClient::CheckPathAccess(const std::string& path,
  100. ResultCallback callback) {
  101. std::move(callback).Run(true);
  102. }
  103. void FakePermissionBrokerClient::OpenPath(const std::string& path,
  104. OpenPathCallback callback,
  105. ErrorCallback error_callback) {
  106. base::ThreadPool::PostTask(
  107. FROM_HERE,
  108. {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
  109. base::BindOnce(&chromeos::OpenPath, path, std::move(callback),
  110. std::move(error_callback),
  111. base::ThreadTaskRunnerHandle::Get()));
  112. }
  113. void FakePermissionBrokerClient::ClaimDevicePath(
  114. const std::string& path,
  115. uint32_t allowed_interfaces_mask,
  116. int lifeline_fd,
  117. OpenPathCallback callback,
  118. ErrorCallback error_callback) {
  119. OpenPath(path, std::move(callback), std::move(error_callback));
  120. }
  121. void FakePermissionBrokerClient::OpenPathAndRegisterClient(
  122. const std::string& path,
  123. uint32_t allowed_interfaces_mask,
  124. int lifeline_fd,
  125. OpenPathAndRegisterClientCallback callback,
  126. ErrorCallback error_callback) {
  127. std::string client_id;
  128. do {
  129. client_id = base::UnguessableToken::Create().ToString();
  130. } while (base::Contains(clients_, client_id));
  131. base::ScopedFD dup_lifeline_fd(HANDLE_EINTR(dup(lifeline_fd)));
  132. if (!dup_lifeline_fd.is_valid()) {
  133. int error_code = logging::GetLastSystemErrorCode();
  134. std::move(error_callback)
  135. .Run(kDupFailedError,
  136. base::StringPrintf(
  137. "Failed to dup lifeline fd %d: %s", lifeline_fd,
  138. logging::SystemErrorCodeToString(error_code).c_str()));
  139. return;
  140. }
  141. auto controller = base::FileDescriptorWatcher::WatchReadable(
  142. dup_lifeline_fd.get(),
  143. base::BindRepeating(&FakePermissionBrokerClient::HandleClosedClient,
  144. weak_factory_.GetWeakPtr(), client_id));
  145. if (!controller) {
  146. std::move(error_callback)
  147. .Run(kWatchLifelineFdFailedError,
  148. base::StringPrintf("Failed to watch dup lifeline fd %d",
  149. dup_lifeline_fd.get()));
  150. return;
  151. }
  152. clients_.emplace(client_id, UsbInterfaces(path, std::move(dup_lifeline_fd),
  153. std::move(controller)));
  154. // No concern of OpenPath failure causing orphan client record here, as the
  155. // inserted client's record will still be removed when requester does error
  156. // handling and closes the lifeline_fd.
  157. OpenPath(path, base::BindOnce(std::move(callback), client_id),
  158. std::move(error_callback));
  159. }
  160. void FakePermissionBrokerClient::DetachInterface(const std::string& client_id,
  161. uint8_t iface_num,
  162. ResultCallback callback) {
  163. auto client_it = clients_.find(client_id);
  164. if (client_it == clients_.end()) {
  165. LOG(ERROR) << "Unknown client_id: " << client_id;
  166. std::move(callback).Run(false);
  167. return;
  168. }
  169. base::ThreadPool::PostTaskAndReplyWithResult(
  170. FROM_HERE,
  171. {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
  172. base::BindOnce(&chromeos::DisconnectInterface, client_it->second.path,
  173. iface_num),
  174. std::move(callback));
  175. }
  176. void FakePermissionBrokerClient::ReattachInterface(const std::string& client_id,
  177. uint8_t iface_num,
  178. ResultCallback callback) {
  179. auto client_it = clients_.find(client_id);
  180. if (client_it == clients_.end()) {
  181. LOG(ERROR) << "Unknown client_id: " << client_id;
  182. std::move(callback).Run(false);
  183. return;
  184. }
  185. base::ThreadPool::PostTaskAndReplyWithResult(
  186. FROM_HERE,
  187. {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
  188. base::BindOnce(&chromeos::ConnectInterface, client_it->second.path,
  189. iface_num),
  190. std::move(callback));
  191. }
  192. void FakePermissionBrokerClient::RequestTcpPortAccess(
  193. uint16_t port,
  194. const std::string& interface,
  195. int lifeline_fd,
  196. ResultCallback callback) {
  197. std::move(callback).Run(
  198. RequestPortImpl(port, interface, tcp_deny_rule_set_, &tcp_hole_set_));
  199. }
  200. void FakePermissionBrokerClient::RequestUdpPortAccess(
  201. uint16_t port,
  202. const std::string& interface,
  203. int lifeline_fd,
  204. ResultCallback callback) {
  205. std::move(callback).Run(
  206. RequestPortImpl(port, interface, udp_deny_rule_set_, &udp_hole_set_));
  207. }
  208. void FakePermissionBrokerClient::ReleaseTcpPort(uint16_t port,
  209. const std::string& interface,
  210. ResultCallback callback) {
  211. std::move(callback).Run(tcp_hole_set_.erase(std::make_pair(port, interface)));
  212. }
  213. void FakePermissionBrokerClient::ReleaseUdpPort(uint16_t port,
  214. const std::string& interface,
  215. ResultCallback callback) {
  216. std::move(callback).Run(udp_hole_set_.erase(std::make_pair(port, interface)));
  217. }
  218. void FakePermissionBrokerClient::AddTcpDenyRule(uint16_t port,
  219. const std::string& interface) {
  220. tcp_deny_rule_set_.insert(std::make_pair(port, interface));
  221. }
  222. void FakePermissionBrokerClient::AddUdpDenyRule(uint16_t port,
  223. const std::string& interface) {
  224. udp_deny_rule_set_.insert(std::make_pair(port, interface));
  225. }
  226. bool FakePermissionBrokerClient::HasTcpHole(uint16_t port,
  227. const std::string& interface) {
  228. auto rule = std::make_pair(port, interface);
  229. return tcp_hole_set_.find(rule) != tcp_hole_set_.end();
  230. }
  231. bool FakePermissionBrokerClient::HasUdpHole(uint16_t port,
  232. const std::string& interface) {
  233. auto rule = std::make_pair(port, interface);
  234. return udp_hole_set_.find(rule) != udp_hole_set_.end();
  235. }
  236. bool FakePermissionBrokerClient::HasTcpPortForward(
  237. uint16_t port,
  238. const std::string& interface) {
  239. auto rule = std::make_pair(port, interface);
  240. return tcp_forwarding_set_.find(rule) != tcp_forwarding_set_.end();
  241. }
  242. bool FakePermissionBrokerClient::HasUdpPortForward(
  243. uint16_t port,
  244. const std::string& interface) {
  245. auto rule = std::make_pair(port, interface);
  246. return udp_forwarding_set_.find(rule) != udp_forwarding_set_.end();
  247. }
  248. void FakePermissionBrokerClient::RequestTcpPortForward(
  249. uint16_t in_port,
  250. const std::string& in_interface,
  251. const std::string& dst_ip,
  252. uint16_t dst_port,
  253. int lifeline_fd,
  254. ResultCallback callback) {
  255. // TODO(matterchen): Increase logic for adding duplicate ports.
  256. auto rule = std::make_pair(in_port, in_interface);
  257. tcp_forwarding_set_.insert(rule);
  258. std::move(callback).Run(true);
  259. }
  260. void FakePermissionBrokerClient::RequestUdpPortForward(
  261. uint16_t in_port,
  262. const std::string& in_interface,
  263. const std::string& dst_ip,
  264. uint16_t dst_port,
  265. int lifeline_fd,
  266. ResultCallback callback) {
  267. auto rule = std::make_pair(in_port, in_interface);
  268. udp_forwarding_set_.insert(rule);
  269. std::move(callback).Run(true);
  270. }
  271. void FakePermissionBrokerClient::ReleaseTcpPortForward(
  272. uint16_t in_port,
  273. const std::string& in_interface,
  274. ResultCallback callback) {
  275. auto rule = std::make_pair(in_port, in_interface);
  276. tcp_forwarding_set_.erase(rule);
  277. std::move(callback).Run(true);
  278. }
  279. void FakePermissionBrokerClient::ReleaseUdpPortForward(
  280. uint16_t in_port,
  281. const std::string& in_interface,
  282. ResultCallback callback) {
  283. auto rule = std::make_pair(in_port, in_interface);
  284. udp_forwarding_set_.erase(rule);
  285. std::move(callback).Run(true);
  286. }
  287. bool FakePermissionBrokerClient::RequestPortImpl(uint16_t port,
  288. const std::string& interface,
  289. const RuleSet& deny_rule_set,
  290. RuleSet* hole_set) {
  291. auto rule = std::make_pair(port, interface);
  292. // If there is already a hole, returns true.
  293. if (hole_set->find(rule) != hole_set->end())
  294. return true;
  295. // If it is denied to make a hole, returns false.
  296. if (deny_rule_set.find(rule) != deny_rule_set.end())
  297. return false;
  298. hole_set->insert(rule);
  299. return true;
  300. }
  301. FakePermissionBrokerClient::UsbInterfaces::UsbInterfaces(
  302. const std::string& path,
  303. base::ScopedFD lifeline_fd,
  304. std::unique_ptr<base::FileDescriptorWatcher::Controller> controller)
  305. : path(std::move(path)),
  306. lifeline_fd(std::move(lifeline_fd)),
  307. controller(std::move(controller)) {}
  308. FakePermissionBrokerClient::UsbInterfaces::~UsbInterfaces() = default;
  309. FakePermissionBrokerClient::UsbInterfaces::UsbInterfaces(UsbInterfaces&&) =
  310. default;
  311. FakePermissionBrokerClient::UsbInterfaces&
  312. FakePermissionBrokerClient::UsbInterfaces::operator=(UsbInterfaces&&) = default;
  313. } // namespace chromeos