vhci_sysfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  4. * Copyright (C) 2015-2016 Nobuo Iwata
  5. */
  6. #include <linux/kthread.h>
  7. #include <linux/file.h>
  8. #include <linux/net.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. /* Hardening for Spectre-v1 */
  12. #include <linux/nospec.h>
  13. #include "usbip_common.h"
  14. #include "vhci.h"
  15. /* TODO: refine locking ?*/
  16. /*
  17. * output example:
  18. * hub port sta spd dev sockfd local_busid
  19. * hs 0000 004 000 00000000 000003 1-2.3
  20. * ................................................
  21. * ss 0008 004 000 00000000 000004 2-3.4
  22. * ................................................
  23. *
  24. * Output includes socket fd instead of socket pointer address to avoid
  25. * leaking kernel memory address in:
  26. * /sys/devices/platform/vhci_hcd.0/status and in debug output.
  27. * The socket pointer address is not used at the moment and it was made
  28. * visible as a convenient way to find IP address from socket pointer
  29. * address by looking up /proc/net/{tcp,tcp6}. As this opens a security
  30. * hole, the change is made to use sockfd instead.
  31. *
  32. */
  33. static void port_show_vhci(char **out, int hub, int port, struct vhci_device *vdev)
  34. {
  35. if (hub == HUB_SPEED_HIGH)
  36. *out += sprintf(*out, "hs %04u %03u ",
  37. port, vdev->ud.status);
  38. else /* hub == HUB_SPEED_SUPER */
  39. *out += sprintf(*out, "ss %04u %03u ",
  40. port, vdev->ud.status);
  41. if (vdev->ud.status == VDEV_ST_USED) {
  42. *out += sprintf(*out, "%03u %08x ",
  43. vdev->speed, vdev->devid);
  44. *out += sprintf(*out, "%06u %s",
  45. vdev->ud.sockfd,
  46. dev_name(&vdev->udev->dev));
  47. } else {
  48. *out += sprintf(*out, "000 00000000 ");
  49. *out += sprintf(*out, "000000 0-0");
  50. }
  51. *out += sprintf(*out, "\n");
  52. }
  53. /* Sysfs entry to show port status */
  54. static ssize_t status_show_vhci(int pdev_nr, char *out)
  55. {
  56. struct platform_device *pdev = vhcis[pdev_nr].pdev;
  57. struct vhci *vhci;
  58. struct usb_hcd *hcd;
  59. struct vhci_hcd *vhci_hcd;
  60. char *s = out;
  61. int i;
  62. unsigned long flags;
  63. if (!pdev || !out) {
  64. usbip_dbg_vhci_sysfs("show status error\n");
  65. return 0;
  66. }
  67. hcd = platform_get_drvdata(pdev);
  68. vhci_hcd = hcd_to_vhci_hcd(hcd);
  69. vhci = vhci_hcd->vhci;
  70. spin_lock_irqsave(&vhci->lock, flags);
  71. for (i = 0; i < VHCI_HC_PORTS; i++) {
  72. struct vhci_device *vdev = &vhci->vhci_hcd_hs->vdev[i];
  73. spin_lock(&vdev->ud.lock);
  74. port_show_vhci(&out, HUB_SPEED_HIGH,
  75. pdev_nr * VHCI_PORTS + i, vdev);
  76. spin_unlock(&vdev->ud.lock);
  77. }
  78. for (i = 0; i < VHCI_HC_PORTS; i++) {
  79. struct vhci_device *vdev = &vhci->vhci_hcd_ss->vdev[i];
  80. spin_lock(&vdev->ud.lock);
  81. port_show_vhci(&out, HUB_SPEED_SUPER,
  82. pdev_nr * VHCI_PORTS + VHCI_HC_PORTS + i, vdev);
  83. spin_unlock(&vdev->ud.lock);
  84. }
  85. spin_unlock_irqrestore(&vhci->lock, flags);
  86. return out - s;
  87. }
  88. static ssize_t status_show_not_ready(int pdev_nr, char *out)
  89. {
  90. char *s = out;
  91. int i = 0;
  92. for (i = 0; i < VHCI_HC_PORTS; i++) {
  93. out += sprintf(out, "hs %04u %03u ",
  94. (pdev_nr * VHCI_PORTS) + i,
  95. VDEV_ST_NOTASSIGNED);
  96. out += sprintf(out, "000 00000000 0000000000000000 0-0");
  97. out += sprintf(out, "\n");
  98. }
  99. for (i = 0; i < VHCI_HC_PORTS; i++) {
  100. out += sprintf(out, "ss %04u %03u ",
  101. (pdev_nr * VHCI_PORTS) + VHCI_HC_PORTS + i,
  102. VDEV_ST_NOTASSIGNED);
  103. out += sprintf(out, "000 00000000 0000000000000000 0-0");
  104. out += sprintf(out, "\n");
  105. }
  106. return out - s;
  107. }
  108. static int status_name_to_id(const char *name)
  109. {
  110. char *c;
  111. long val;
  112. int ret;
  113. c = strchr(name, '.');
  114. if (c == NULL)
  115. return 0;
  116. ret = kstrtol(c+1, 10, &val);
  117. if (ret < 0)
  118. return ret;
  119. return val;
  120. }
  121. static ssize_t status_show(struct device *dev,
  122. struct device_attribute *attr, char *out)
  123. {
  124. char *s = out;
  125. int pdev_nr;
  126. out += sprintf(out,
  127. "hub port sta spd dev sockfd local_busid\n");
  128. pdev_nr = status_name_to_id(attr->attr.name);
  129. if (pdev_nr < 0)
  130. out += status_show_not_ready(pdev_nr, out);
  131. else
  132. out += status_show_vhci(pdev_nr, out);
  133. return out - s;
  134. }
  135. static ssize_t nports_show(struct device *dev, struct device_attribute *attr,
  136. char *out)
  137. {
  138. char *s = out;
  139. /*
  140. * Half the ports are for SPEED_HIGH and half for SPEED_SUPER,
  141. * thus the * 2.
  142. */
  143. out += sprintf(out, "%d\n", VHCI_PORTS * vhci_num_controllers);
  144. return out - s;
  145. }
  146. static DEVICE_ATTR_RO(nports);
  147. /* Sysfs entry to shutdown a virtual connection */
  148. static int vhci_port_disconnect(struct vhci_hcd *vhci_hcd, __u32 rhport)
  149. {
  150. struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
  151. struct vhci *vhci = vhci_hcd->vhci;
  152. unsigned long flags;
  153. usbip_dbg_vhci_sysfs("enter\n");
  154. /* lock */
  155. spin_lock_irqsave(&vhci->lock, flags);
  156. spin_lock(&vdev->ud.lock);
  157. if (vdev->ud.status == VDEV_ST_NULL) {
  158. pr_err("not connected %d\n", vdev->ud.status);
  159. /* unlock */
  160. spin_unlock(&vdev->ud.lock);
  161. spin_unlock_irqrestore(&vhci->lock, flags);
  162. return -EINVAL;
  163. }
  164. /* unlock */
  165. spin_unlock(&vdev->ud.lock);
  166. spin_unlock_irqrestore(&vhci->lock, flags);
  167. usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
  168. return 0;
  169. }
  170. static int valid_port(__u32 *pdev_nr, __u32 *rhport)
  171. {
  172. if (*pdev_nr >= vhci_num_controllers) {
  173. pr_err("pdev %u\n", *pdev_nr);
  174. return 0;
  175. }
  176. *pdev_nr = array_index_nospec(*pdev_nr, vhci_num_controllers);
  177. if (*rhport >= VHCI_HC_PORTS) {
  178. pr_err("rhport %u\n", *rhport);
  179. return 0;
  180. }
  181. *rhport = array_index_nospec(*rhport, VHCI_HC_PORTS);
  182. return 1;
  183. }
  184. static ssize_t detach_store(struct device *dev, struct device_attribute *attr,
  185. const char *buf, size_t count)
  186. {
  187. __u32 port = 0, pdev_nr = 0, rhport = 0;
  188. struct usb_hcd *hcd;
  189. struct vhci_hcd *vhci_hcd;
  190. int ret;
  191. if (kstrtoint(buf, 10, &port) < 0)
  192. return -EINVAL;
  193. pdev_nr = port_to_pdev_nr(port);
  194. rhport = port_to_rhport(port);
  195. if (!valid_port(&pdev_nr, &rhport))
  196. return -EINVAL;
  197. hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
  198. if (hcd == NULL) {
  199. dev_err(dev, "port is not ready %u\n", port);
  200. return -EAGAIN;
  201. }
  202. usbip_dbg_vhci_sysfs("rhport %d\n", rhport);
  203. if ((port / VHCI_HC_PORTS) % 2)
  204. vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_ss;
  205. else
  206. vhci_hcd = hcd_to_vhci_hcd(hcd)->vhci->vhci_hcd_hs;
  207. ret = vhci_port_disconnect(vhci_hcd, rhport);
  208. if (ret < 0)
  209. return -EINVAL;
  210. usbip_dbg_vhci_sysfs("Leave\n");
  211. return count;
  212. }
  213. static DEVICE_ATTR_WO(detach);
  214. static int valid_args(__u32 *pdev_nr, __u32 *rhport,
  215. enum usb_device_speed speed)
  216. {
  217. if (!valid_port(pdev_nr, rhport)) {
  218. return 0;
  219. }
  220. switch (speed) {
  221. case USB_SPEED_LOW:
  222. case USB_SPEED_FULL:
  223. case USB_SPEED_HIGH:
  224. case USB_SPEED_WIRELESS:
  225. case USB_SPEED_SUPER:
  226. break;
  227. default:
  228. pr_err("Failed attach request for unsupported USB speed: %s\n",
  229. usb_speed_string(speed));
  230. return 0;
  231. }
  232. return 1;
  233. }
  234. /* Sysfs entry to establish a virtual connection */
  235. /*
  236. * To start a new USB/IP attachment, a userland program needs to setup a TCP
  237. * connection and then write its socket descriptor with remote device
  238. * information into this sysfs file.
  239. *
  240. * A remote device is virtually attached to the root-hub port of @rhport with
  241. * @speed. @devid is embedded into a request to specify the remote device in a
  242. * server host.
  243. *
  244. * write() returns 0 on success, else negative errno.
  245. */
  246. static ssize_t attach_store(struct device *dev, struct device_attribute *attr,
  247. const char *buf, size_t count)
  248. {
  249. struct socket *socket;
  250. int sockfd = 0;
  251. __u32 port = 0, pdev_nr = 0, rhport = 0, devid = 0, speed = 0;
  252. struct usb_hcd *hcd;
  253. struct vhci_hcd *vhci_hcd;
  254. struct vhci_device *vdev;
  255. struct vhci *vhci;
  256. int err;
  257. unsigned long flags;
  258. /*
  259. * @rhport: port number of vhci_hcd
  260. * @sockfd: socket descriptor of an established TCP connection
  261. * @devid: unique device identifier in a remote host
  262. * @speed: usb device speed in a remote host
  263. */
  264. if (sscanf(buf, "%u %u %u %u", &port, &sockfd, &devid, &speed) != 4)
  265. return -EINVAL;
  266. pdev_nr = port_to_pdev_nr(port);
  267. rhport = port_to_rhport(port);
  268. usbip_dbg_vhci_sysfs("port(%u) pdev(%d) rhport(%u)\n",
  269. port, pdev_nr, rhport);
  270. usbip_dbg_vhci_sysfs("sockfd(%u) devid(%u) speed(%u)\n",
  271. sockfd, devid, speed);
  272. /* check received parameters */
  273. if (!valid_args(&pdev_nr, &rhport, speed))
  274. return -EINVAL;
  275. hcd = platform_get_drvdata(vhcis[pdev_nr].pdev);
  276. if (hcd == NULL) {
  277. dev_err(dev, "port %d is not ready\n", port);
  278. return -EAGAIN;
  279. }
  280. vhci_hcd = hcd_to_vhci_hcd(hcd);
  281. vhci = vhci_hcd->vhci;
  282. if (speed == USB_SPEED_SUPER)
  283. vdev = &vhci->vhci_hcd_ss->vdev[rhport];
  284. else
  285. vdev = &vhci->vhci_hcd_hs->vdev[rhport];
  286. /* Extract socket from fd. */
  287. socket = sockfd_lookup(sockfd, &err);
  288. if (!socket)
  289. return -EINVAL;
  290. /* now need lock until setting vdev status as used */
  291. /* begin a lock */
  292. spin_lock_irqsave(&vhci->lock, flags);
  293. spin_lock(&vdev->ud.lock);
  294. if (vdev->ud.status != VDEV_ST_NULL) {
  295. /* end of the lock */
  296. spin_unlock(&vdev->ud.lock);
  297. spin_unlock_irqrestore(&vhci->lock, flags);
  298. sockfd_put(socket);
  299. dev_err(dev, "port %d already used\n", rhport);
  300. /*
  301. * Will be retried from userspace
  302. * if there's another free port.
  303. */
  304. return -EBUSY;
  305. }
  306. dev_info(dev, "pdev(%u) rhport(%u) sockfd(%d)\n",
  307. pdev_nr, rhport, sockfd);
  308. dev_info(dev, "devid(%u) speed(%u) speed_str(%s)\n",
  309. devid, speed, usb_speed_string(speed));
  310. vdev->devid = devid;
  311. vdev->speed = speed;
  312. vdev->ud.sockfd = sockfd;
  313. vdev->ud.tcp_socket = socket;
  314. vdev->ud.status = VDEV_ST_NOTASSIGNED;
  315. spin_unlock(&vdev->ud.lock);
  316. spin_unlock_irqrestore(&vhci->lock, flags);
  317. /* end the lock */
  318. vdev->ud.tcp_rx = kthread_get_run(vhci_rx_loop, &vdev->ud, "vhci_rx");
  319. vdev->ud.tcp_tx = kthread_get_run(vhci_tx_loop, &vdev->ud, "vhci_tx");
  320. rh_port_connect(vdev, speed);
  321. return count;
  322. }
  323. static DEVICE_ATTR_WO(attach);
  324. #define MAX_STATUS_NAME 16
  325. struct status_attr {
  326. struct device_attribute attr;
  327. char name[MAX_STATUS_NAME+1];
  328. };
  329. static struct status_attr *status_attrs;
  330. static void set_status_attr(int id)
  331. {
  332. struct status_attr *status;
  333. status = status_attrs + id;
  334. if (id == 0)
  335. strcpy(status->name, "status");
  336. else
  337. snprintf(status->name, MAX_STATUS_NAME+1, "status.%d", id);
  338. status->attr.attr.name = status->name;
  339. status->attr.attr.mode = S_IRUGO;
  340. status->attr.show = status_show;
  341. sysfs_attr_init(&status->attr.attr);
  342. }
  343. static int init_status_attrs(void)
  344. {
  345. int id;
  346. status_attrs = kcalloc(vhci_num_controllers, sizeof(struct status_attr),
  347. GFP_KERNEL);
  348. if (status_attrs == NULL)
  349. return -ENOMEM;
  350. for (id = 0; id < vhci_num_controllers; id++)
  351. set_status_attr(id);
  352. return 0;
  353. }
  354. static void finish_status_attrs(void)
  355. {
  356. kfree(status_attrs);
  357. }
  358. struct attribute_group vhci_attr_group = {
  359. .attrs = NULL,
  360. };
  361. int vhci_init_attr_group(void)
  362. {
  363. struct attribute **attrs;
  364. int ret, i;
  365. attrs = kcalloc((vhci_num_controllers + 5), sizeof(struct attribute *),
  366. GFP_KERNEL);
  367. if (attrs == NULL)
  368. return -ENOMEM;
  369. ret = init_status_attrs();
  370. if (ret) {
  371. kfree(attrs);
  372. return ret;
  373. }
  374. *attrs = &dev_attr_nports.attr;
  375. *(attrs + 1) = &dev_attr_detach.attr;
  376. *(attrs + 2) = &dev_attr_attach.attr;
  377. *(attrs + 3) = &dev_attr_usbip_debug.attr;
  378. for (i = 0; i < vhci_num_controllers; i++)
  379. *(attrs + i + 4) = &((status_attrs + i)->attr.attr);
  380. vhci_attr_group.attrs = attrs;
  381. return 0;
  382. }
  383. void vhci_finish_attr_group(void)
  384. {
  385. finish_status_attrs();
  386. kfree(vhci_attr_group.attrs);
  387. }