uverbs_ioctl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <rdma/rdma_user_ioctl.h>
  33. #include <rdma/uverbs_ioctl.h>
  34. #include "rdma_core.h"
  35. #include "uverbs.h"
  36. static bool uverbs_is_attr_cleared(const struct ib_uverbs_attr *uattr,
  37. u16 len)
  38. {
  39. if (uattr->len > sizeof(((struct ib_uverbs_attr *)0)->data))
  40. return ib_is_buffer_cleared(u64_to_user_ptr(uattr->data) + len,
  41. uattr->len - len);
  42. return !memchr_inv((const void *)&uattr->data + len,
  43. 0, uattr->len - len);
  44. }
  45. static int uverbs_process_attr(struct ib_uverbs_file *ufile,
  46. const struct ib_uverbs_attr *uattr,
  47. u16 attr_id,
  48. const struct uverbs_attr_spec_hash *attr_spec_bucket,
  49. struct uverbs_attr_bundle_hash *attr_bundle_h,
  50. struct ib_uverbs_attr __user *uattr_ptr)
  51. {
  52. const struct uverbs_attr_spec *spec;
  53. const struct uverbs_attr_spec *val_spec;
  54. struct uverbs_attr *e;
  55. const struct uverbs_object_spec *object;
  56. struct uverbs_obj_attr *o_attr;
  57. struct uverbs_attr *elements = attr_bundle_h->attrs;
  58. if (attr_id >= attr_spec_bucket->num_attrs) {
  59. if (uattr->flags & UVERBS_ATTR_F_MANDATORY)
  60. return -EINVAL;
  61. else
  62. return 0;
  63. }
  64. if (test_bit(attr_id, attr_bundle_h->valid_bitmap))
  65. return -EINVAL;
  66. spec = &attr_spec_bucket->attrs[attr_id];
  67. val_spec = spec;
  68. e = &elements[attr_id];
  69. e->uattr = uattr_ptr;
  70. switch (spec->type) {
  71. case UVERBS_ATTR_TYPE_ENUM_IN:
  72. if (uattr->attr_data.enum_data.elem_id >= spec->u.enum_def.num_elems)
  73. return -EOPNOTSUPP;
  74. if (uattr->attr_data.enum_data.reserved)
  75. return -EINVAL;
  76. val_spec = &spec->u2.enum_def.ids[uattr->attr_data.enum_data.elem_id];
  77. /* Currently we only support PTR_IN based enums */
  78. if (val_spec->type != UVERBS_ATTR_TYPE_PTR_IN)
  79. return -EOPNOTSUPP;
  80. e->ptr_attr.enum_id = uattr->attr_data.enum_data.elem_id;
  81. /* fall through */
  82. case UVERBS_ATTR_TYPE_PTR_IN:
  83. /* Ensure that any data provided by userspace beyond the known
  84. * struct is zero. Userspace that knows how to use some future
  85. * longer struct will fail here if used with an old kernel and
  86. * non-zero content, making ABI compat/discovery simpler.
  87. */
  88. if (uattr->len > val_spec->u.ptr.len &&
  89. val_spec->zero_trailing &&
  90. !uverbs_is_attr_cleared(uattr, val_spec->u.ptr.len))
  91. return -EOPNOTSUPP;
  92. /* fall through */
  93. case UVERBS_ATTR_TYPE_PTR_OUT:
  94. if (uattr->len < val_spec->u.ptr.min_len ||
  95. (!val_spec->zero_trailing &&
  96. uattr->len > val_spec->u.ptr.len))
  97. return -EINVAL;
  98. if (spec->type != UVERBS_ATTR_TYPE_ENUM_IN &&
  99. uattr->attr_data.reserved)
  100. return -EINVAL;
  101. e->ptr_attr.len = uattr->len;
  102. e->ptr_attr.flags = uattr->flags;
  103. if (val_spec->alloc_and_copy && !uverbs_attr_ptr_is_inline(e)) {
  104. void *p;
  105. p = kvmalloc(uattr->len, GFP_KERNEL);
  106. if (!p)
  107. return -ENOMEM;
  108. e->ptr_attr.ptr = p;
  109. if (copy_from_user(p, u64_to_user_ptr(uattr->data),
  110. uattr->len)) {
  111. kvfree(p);
  112. return -EFAULT;
  113. }
  114. } else {
  115. e->ptr_attr.data = uattr->data;
  116. }
  117. break;
  118. case UVERBS_ATTR_TYPE_IDR:
  119. case UVERBS_ATTR_TYPE_FD:
  120. if (uattr->attr_data.reserved)
  121. return -EINVAL;
  122. if (uattr->len != 0 || !ufile->ucontext)
  123. return -EINVAL;
  124. o_attr = &e->obj_attr;
  125. object = uverbs_get_object(ufile, spec->u.obj.obj_type);
  126. if (!object)
  127. return -EINVAL;
  128. /*
  129. * The type of uattr->data is u64 for UVERBS_ATTR_TYPE_IDR and
  130. * s64 for UVERBS_ATTR_TYPE_FD. We can cast the u64 to s64
  131. * here without caring about truncation as we know that the
  132. * IDR implementation today rejects negative IDs
  133. */
  134. o_attr->uobject = uverbs_get_uobject_from_file(
  135. object->type_attrs,
  136. ufile,
  137. spec->u.obj.access,
  138. uattr->data_s64);
  139. if (IS_ERR(o_attr->uobject))
  140. return PTR_ERR(o_attr->uobject);
  141. if (spec->u.obj.access == UVERBS_ACCESS_NEW) {
  142. s64 id = o_attr->uobject->id;
  143. /* Copy the allocated id to the user-space */
  144. if (put_user(id, &e->uattr->data)) {
  145. uverbs_finalize_object(o_attr->uobject,
  146. UVERBS_ACCESS_NEW,
  147. false);
  148. return -EFAULT;
  149. }
  150. }
  151. break;
  152. default:
  153. return -EOPNOTSUPP;
  154. }
  155. set_bit(attr_id, attr_bundle_h->valid_bitmap);
  156. return 0;
  157. }
  158. static int uverbs_finalize_attrs(struct uverbs_attr_bundle *attrs_bundle,
  159. struct uverbs_attr_spec_hash *const *spec_hash,
  160. size_t num, bool commit)
  161. {
  162. unsigned int i;
  163. int ret = 0;
  164. for (i = 0; i < num; i++) {
  165. struct uverbs_attr_bundle_hash *curr_bundle =
  166. &attrs_bundle->hash[i];
  167. const struct uverbs_attr_spec_hash *curr_spec_bucket =
  168. spec_hash[i];
  169. unsigned int j;
  170. if (!curr_spec_bucket)
  171. continue;
  172. for (j = 0; j < curr_bundle->num_attrs; j++) {
  173. struct uverbs_attr *attr;
  174. const struct uverbs_attr_spec *spec;
  175. if (!uverbs_attr_is_valid_in_hash(curr_bundle, j))
  176. continue;
  177. attr = &curr_bundle->attrs[j];
  178. spec = &curr_spec_bucket->attrs[j];
  179. if (spec->type == UVERBS_ATTR_TYPE_IDR ||
  180. spec->type == UVERBS_ATTR_TYPE_FD) {
  181. int current_ret;
  182. current_ret = uverbs_finalize_object(
  183. attr->obj_attr.uobject,
  184. spec->u.obj.access, commit);
  185. if (!ret)
  186. ret = current_ret;
  187. } else if (spec->type == UVERBS_ATTR_TYPE_PTR_IN &&
  188. spec->alloc_and_copy &&
  189. !uverbs_attr_ptr_is_inline(attr)) {
  190. kvfree(attr->ptr_attr.ptr);
  191. }
  192. }
  193. }
  194. return ret;
  195. }
  196. static int uverbs_uattrs_process(struct ib_uverbs_file *ufile,
  197. const struct ib_uverbs_attr *uattrs,
  198. size_t num_uattrs,
  199. const struct uverbs_method_spec *method,
  200. struct uverbs_attr_bundle *attr_bundle,
  201. struct ib_uverbs_attr __user *uattr_ptr)
  202. {
  203. size_t i;
  204. int ret = 0;
  205. int num_given_buckets = 0;
  206. for (i = 0; i < num_uattrs; i++) {
  207. const struct ib_uverbs_attr *uattr = &uattrs[i];
  208. u16 attr_id = uattr->attr_id;
  209. struct uverbs_attr_spec_hash *attr_spec_bucket;
  210. ret = uverbs_ns_idx(&attr_id, method->num_buckets);
  211. if (ret < 0 || !method->attr_buckets[ret]) {
  212. if (uattr->flags & UVERBS_ATTR_F_MANDATORY) {
  213. uverbs_finalize_attrs(attr_bundle,
  214. method->attr_buckets,
  215. num_given_buckets,
  216. false);
  217. return ret;
  218. }
  219. continue;
  220. }
  221. /*
  222. * ret is the found ns, so increase num_given_buckets if
  223. * necessary.
  224. */
  225. if (ret >= num_given_buckets)
  226. num_given_buckets = ret + 1;
  227. attr_spec_bucket = method->attr_buckets[ret];
  228. ret = uverbs_process_attr(ufile, uattr, attr_id,
  229. attr_spec_bucket,
  230. &attr_bundle->hash[ret], uattr_ptr++);
  231. if (ret) {
  232. uverbs_finalize_attrs(attr_bundle,
  233. method->attr_buckets,
  234. num_given_buckets,
  235. false);
  236. return ret;
  237. }
  238. }
  239. return num_given_buckets;
  240. }
  241. static int uverbs_validate_kernel_mandatory(const struct uverbs_method_spec *method_spec,
  242. struct uverbs_attr_bundle *attr_bundle)
  243. {
  244. unsigned int i;
  245. for (i = 0; i < attr_bundle->num_buckets; i++) {
  246. struct uverbs_attr_spec_hash *attr_spec_bucket =
  247. method_spec->attr_buckets[i];
  248. if (!attr_spec_bucket)
  249. continue;
  250. if (!bitmap_subset(attr_spec_bucket->mandatory_attrs_bitmask,
  251. attr_bundle->hash[i].valid_bitmap,
  252. attr_spec_bucket->num_attrs))
  253. return -EINVAL;
  254. }
  255. for (; i < method_spec->num_buckets; i++) {
  256. struct uverbs_attr_spec_hash *attr_spec_bucket =
  257. method_spec->attr_buckets[i];
  258. if (!bitmap_empty(attr_spec_bucket->mandatory_attrs_bitmask,
  259. attr_spec_bucket->num_attrs))
  260. return -EINVAL;
  261. }
  262. return 0;
  263. }
  264. static int uverbs_handle_method(struct ib_uverbs_attr __user *uattr_ptr,
  265. const struct ib_uverbs_attr *uattrs,
  266. size_t num_uattrs,
  267. struct ib_device *ibdev,
  268. struct ib_uverbs_file *ufile,
  269. const struct uverbs_method_spec *method_spec,
  270. struct uverbs_attr_bundle *attr_bundle)
  271. {
  272. int ret;
  273. int finalize_ret;
  274. int num_given_buckets;
  275. num_given_buckets = uverbs_uattrs_process(
  276. ufile, uattrs, num_uattrs, method_spec, attr_bundle, uattr_ptr);
  277. if (num_given_buckets <= 0)
  278. return -EINVAL;
  279. attr_bundle->num_buckets = num_given_buckets;
  280. ret = uverbs_validate_kernel_mandatory(method_spec, attr_bundle);
  281. if (ret)
  282. goto cleanup;
  283. ret = method_spec->handler(ibdev, ufile, attr_bundle);
  284. cleanup:
  285. finalize_ret = uverbs_finalize_attrs(attr_bundle,
  286. method_spec->attr_buckets,
  287. attr_bundle->num_buckets,
  288. !ret);
  289. return ret ? ret : finalize_ret;
  290. }
  291. #define UVERBS_OPTIMIZE_USING_STACK_SZ 256
  292. static long ib_uverbs_cmd_verbs(struct ib_device *ib_dev,
  293. struct ib_uverbs_file *file,
  294. struct ib_uverbs_ioctl_hdr *hdr,
  295. void __user *buf)
  296. {
  297. const struct uverbs_object_spec *object_spec;
  298. const struct uverbs_method_spec *method_spec;
  299. long err = 0;
  300. unsigned int i;
  301. struct {
  302. struct ib_uverbs_attr *uattrs;
  303. struct uverbs_attr_bundle *uverbs_attr_bundle;
  304. } *ctx = NULL;
  305. struct uverbs_attr *curr_attr;
  306. unsigned long *curr_bitmap;
  307. size_t ctx_size;
  308. uintptr_t data[UVERBS_OPTIMIZE_USING_STACK_SZ / sizeof(uintptr_t)];
  309. if (hdr->driver_id != ib_dev->driver_id)
  310. return -EINVAL;
  311. object_spec = uverbs_get_object(file, hdr->object_id);
  312. if (!object_spec)
  313. return -EPROTONOSUPPORT;
  314. method_spec = uverbs_get_method(object_spec, hdr->method_id);
  315. if (!method_spec)
  316. return -EPROTONOSUPPORT;
  317. if ((method_spec->flags & UVERBS_ACTION_FLAG_CREATE_ROOT) ^ !file->ucontext)
  318. return -EINVAL;
  319. ctx_size = sizeof(*ctx) +
  320. sizeof(struct uverbs_attr_bundle) +
  321. sizeof(struct uverbs_attr_bundle_hash) * method_spec->num_buckets +
  322. sizeof(*ctx->uattrs) * hdr->num_attrs +
  323. sizeof(*ctx->uverbs_attr_bundle->hash[0].attrs) *
  324. method_spec->num_child_attrs +
  325. sizeof(*ctx->uverbs_attr_bundle->hash[0].valid_bitmap) *
  326. (method_spec->num_child_attrs / BITS_PER_LONG +
  327. method_spec->num_buckets);
  328. if (ctx_size <= UVERBS_OPTIMIZE_USING_STACK_SZ)
  329. ctx = (void *)data;
  330. if (!ctx)
  331. ctx = kmalloc(ctx_size, GFP_KERNEL);
  332. if (!ctx)
  333. return -ENOMEM;
  334. ctx->uverbs_attr_bundle = (void *)ctx + sizeof(*ctx);
  335. ctx->uattrs = (void *)(ctx->uverbs_attr_bundle + 1) +
  336. (sizeof(ctx->uverbs_attr_bundle->hash[0]) *
  337. method_spec->num_buckets);
  338. curr_attr = (void *)(ctx->uattrs + hdr->num_attrs);
  339. curr_bitmap = (void *)(curr_attr + method_spec->num_child_attrs);
  340. /*
  341. * We just fill the pointers and num_attrs here. The data itself will be
  342. * filled at a later stage (uverbs_process_attr)
  343. */
  344. for (i = 0; i < method_spec->num_buckets; i++) {
  345. unsigned int curr_num_attrs;
  346. if (!method_spec->attr_buckets[i])
  347. continue;
  348. curr_num_attrs = method_spec->attr_buckets[i]->num_attrs;
  349. ctx->uverbs_attr_bundle->hash[i].attrs = curr_attr;
  350. curr_attr += curr_num_attrs;
  351. ctx->uverbs_attr_bundle->hash[i].num_attrs = curr_num_attrs;
  352. ctx->uverbs_attr_bundle->hash[i].valid_bitmap = curr_bitmap;
  353. bitmap_zero(curr_bitmap, curr_num_attrs);
  354. curr_bitmap += BITS_TO_LONGS(curr_num_attrs);
  355. }
  356. err = copy_from_user(ctx->uattrs, buf,
  357. sizeof(*ctx->uattrs) * hdr->num_attrs);
  358. if (err) {
  359. err = -EFAULT;
  360. goto out;
  361. }
  362. err = uverbs_handle_method(buf, ctx->uattrs, hdr->num_attrs, ib_dev,
  363. file, method_spec, ctx->uverbs_attr_bundle);
  364. /*
  365. * EPROTONOSUPPORT is ONLY to be returned if the ioctl framework can
  366. * not invoke the method because the request is not supported. No
  367. * other cases should return this code.
  368. */
  369. if (unlikely(err == -EPROTONOSUPPORT)) {
  370. WARN_ON_ONCE(err == -EPROTONOSUPPORT);
  371. err = -EINVAL;
  372. }
  373. out:
  374. if (ctx != (void *)data)
  375. kfree(ctx);
  376. return err;
  377. }
  378. #define IB_UVERBS_MAX_CMD_SZ 4096
  379. long ib_uverbs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  380. {
  381. struct ib_uverbs_file *file = filp->private_data;
  382. struct ib_uverbs_ioctl_hdr __user *user_hdr =
  383. (struct ib_uverbs_ioctl_hdr __user *)arg;
  384. struct ib_uverbs_ioctl_hdr hdr;
  385. struct ib_device *ib_dev;
  386. int srcu_key;
  387. long err;
  388. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  389. ib_dev = srcu_dereference(file->device->ib_dev,
  390. &file->device->disassociate_srcu);
  391. if (!ib_dev) {
  392. err = -EIO;
  393. goto out;
  394. }
  395. if (cmd == RDMA_VERBS_IOCTL) {
  396. err = copy_from_user(&hdr, user_hdr, sizeof(hdr));
  397. if (err || hdr.length > IB_UVERBS_MAX_CMD_SZ ||
  398. hdr.length != sizeof(hdr) + hdr.num_attrs * sizeof(struct ib_uverbs_attr)) {
  399. err = -EINVAL;
  400. goto out;
  401. }
  402. if (hdr.reserved1 || hdr.reserved2) {
  403. err = -EPROTONOSUPPORT;
  404. goto out;
  405. }
  406. err = ib_uverbs_cmd_verbs(ib_dev, file, &hdr,
  407. (__user void *)arg + sizeof(hdr));
  408. } else {
  409. err = -ENOIOCTLCMD;
  410. }
  411. out:
  412. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  413. return err;
  414. }