uverbs_ioctl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 int uverbs_process_attr(struct ib_device *ibdev,
  37. struct ib_ucontext *ucontext,
  38. const struct ib_uverbs_attr *uattr,
  39. u16 attr_id,
  40. const struct uverbs_attr_spec_hash *attr_spec_bucket,
  41. struct uverbs_attr_bundle_hash *attr_bundle_h,
  42. struct ib_uverbs_attr __user *uattr_ptr)
  43. {
  44. const struct uverbs_attr_spec *spec;
  45. struct uverbs_attr *e;
  46. const struct uverbs_object_spec *object;
  47. struct uverbs_obj_attr *o_attr;
  48. struct uverbs_attr *elements = attr_bundle_h->attrs;
  49. if (uattr->reserved)
  50. return -EINVAL;
  51. if (attr_id >= attr_spec_bucket->num_attrs) {
  52. if (uattr->flags & UVERBS_ATTR_F_MANDATORY)
  53. return -EINVAL;
  54. else
  55. return 0;
  56. }
  57. if (test_bit(attr_id, attr_bundle_h->valid_bitmap))
  58. return -EINVAL;
  59. spec = &attr_spec_bucket->attrs[attr_id];
  60. e = &elements[attr_id];
  61. e->uattr = uattr_ptr;
  62. switch (spec->type) {
  63. case UVERBS_ATTR_TYPE_PTR_IN:
  64. case UVERBS_ATTR_TYPE_PTR_OUT:
  65. if (uattr->len < spec->len ||
  66. (!(spec->flags & UVERBS_ATTR_SPEC_F_MIN_SZ) &&
  67. uattr->len > spec->len))
  68. return -EINVAL;
  69. e->ptr_attr.data = uattr->data;
  70. e->ptr_attr.len = uattr->len;
  71. e->ptr_attr.flags = uattr->flags;
  72. break;
  73. case UVERBS_ATTR_TYPE_IDR:
  74. if (uattr->data >> 32)
  75. return -EINVAL;
  76. /* fall through */
  77. case UVERBS_ATTR_TYPE_FD:
  78. if (uattr->len != 0 || !ucontext || uattr->data > INT_MAX)
  79. return -EINVAL;
  80. o_attr = &e->obj_attr;
  81. object = uverbs_get_object(ibdev, spec->obj.obj_type);
  82. if (!object)
  83. return -EINVAL;
  84. o_attr->type = object->type_attrs;
  85. o_attr->id = (int)uattr->data;
  86. o_attr->uobject = uverbs_get_uobject_from_context(
  87. o_attr->type,
  88. ucontext,
  89. spec->obj.access,
  90. o_attr->id);
  91. if (IS_ERR(o_attr->uobject))
  92. return PTR_ERR(o_attr->uobject);
  93. if (spec->obj.access == UVERBS_ACCESS_NEW) {
  94. u64 id = o_attr->uobject->id;
  95. /* Copy the allocated id to the user-space */
  96. if (put_user(id, &e->uattr->data)) {
  97. uverbs_finalize_object(o_attr->uobject,
  98. UVERBS_ACCESS_NEW,
  99. false);
  100. return -EFAULT;
  101. }
  102. }
  103. break;
  104. default:
  105. return -EOPNOTSUPP;
  106. }
  107. set_bit(attr_id, attr_bundle_h->valid_bitmap);
  108. return 0;
  109. }
  110. static int uverbs_uattrs_process(struct ib_device *ibdev,
  111. struct ib_ucontext *ucontext,
  112. const struct ib_uverbs_attr *uattrs,
  113. size_t num_uattrs,
  114. const struct uverbs_method_spec *method,
  115. struct uverbs_attr_bundle *attr_bundle,
  116. struct ib_uverbs_attr __user *uattr_ptr)
  117. {
  118. size_t i;
  119. int ret = 0;
  120. int num_given_buckets = 0;
  121. for (i = 0; i < num_uattrs; i++) {
  122. const struct ib_uverbs_attr *uattr = &uattrs[i];
  123. u16 attr_id = uattr->attr_id;
  124. struct uverbs_attr_spec_hash *attr_spec_bucket;
  125. ret = uverbs_ns_idx(&attr_id, method->num_buckets);
  126. if (ret < 0) {
  127. if (uattr->flags & UVERBS_ATTR_F_MANDATORY) {
  128. uverbs_finalize_objects(attr_bundle,
  129. method->attr_buckets,
  130. num_given_buckets,
  131. false);
  132. return ret;
  133. }
  134. continue;
  135. }
  136. /*
  137. * ret is the found ns, so increase num_given_buckets if
  138. * necessary.
  139. */
  140. if (ret >= num_given_buckets)
  141. num_given_buckets = ret + 1;
  142. attr_spec_bucket = method->attr_buckets[ret];
  143. ret = uverbs_process_attr(ibdev, ucontext, uattr, attr_id,
  144. attr_spec_bucket, &attr_bundle->hash[ret],
  145. uattr_ptr++);
  146. if (ret) {
  147. uverbs_finalize_objects(attr_bundle,
  148. method->attr_buckets,
  149. num_given_buckets,
  150. false);
  151. return ret;
  152. }
  153. }
  154. return num_given_buckets;
  155. }
  156. static int uverbs_validate_kernel_mandatory(const struct uverbs_method_spec *method_spec,
  157. struct uverbs_attr_bundle *attr_bundle)
  158. {
  159. unsigned int i;
  160. for (i = 0; i < attr_bundle->num_buckets; i++) {
  161. struct uverbs_attr_spec_hash *attr_spec_bucket =
  162. method_spec->attr_buckets[i];
  163. if (!bitmap_subset(attr_spec_bucket->mandatory_attrs_bitmask,
  164. attr_bundle->hash[i].valid_bitmap,
  165. attr_spec_bucket->num_attrs))
  166. return -EINVAL;
  167. }
  168. for (; i < method_spec->num_buckets; i++) {
  169. struct uverbs_attr_spec_hash *attr_spec_bucket =
  170. method_spec->attr_buckets[i];
  171. if (!bitmap_empty(attr_spec_bucket->mandatory_attrs_bitmask,
  172. attr_spec_bucket->num_attrs))
  173. return -EINVAL;
  174. }
  175. return 0;
  176. }
  177. static int uverbs_handle_method(struct ib_uverbs_attr __user *uattr_ptr,
  178. const struct ib_uverbs_attr *uattrs,
  179. size_t num_uattrs,
  180. struct ib_device *ibdev,
  181. struct ib_uverbs_file *ufile,
  182. const struct uverbs_method_spec *method_spec,
  183. struct uverbs_attr_bundle *attr_bundle)
  184. {
  185. int ret;
  186. int finalize_ret;
  187. int num_given_buckets;
  188. num_given_buckets = uverbs_uattrs_process(ibdev, ufile->ucontext, uattrs,
  189. num_uattrs, method_spec,
  190. attr_bundle, uattr_ptr);
  191. if (num_given_buckets <= 0)
  192. return -EINVAL;
  193. attr_bundle->num_buckets = num_given_buckets;
  194. ret = uverbs_validate_kernel_mandatory(method_spec, attr_bundle);
  195. if (ret)
  196. goto cleanup;
  197. ret = method_spec->handler(ibdev, ufile, attr_bundle);
  198. cleanup:
  199. finalize_ret = uverbs_finalize_objects(attr_bundle,
  200. method_spec->attr_buckets,
  201. attr_bundle->num_buckets,
  202. !ret);
  203. return ret ? ret : finalize_ret;
  204. }
  205. #define UVERBS_OPTIMIZE_USING_STACK_SZ 256
  206. static long ib_uverbs_cmd_verbs(struct ib_device *ib_dev,
  207. struct ib_uverbs_file *file,
  208. struct ib_uverbs_ioctl_hdr *hdr,
  209. void __user *buf)
  210. {
  211. const struct uverbs_object_spec *object_spec;
  212. const struct uverbs_method_spec *method_spec;
  213. long err = 0;
  214. unsigned int i;
  215. struct {
  216. struct ib_uverbs_attr *uattrs;
  217. struct uverbs_attr_bundle *uverbs_attr_bundle;
  218. } *ctx = NULL;
  219. struct uverbs_attr *curr_attr;
  220. unsigned long *curr_bitmap;
  221. size_t ctx_size;
  222. #ifdef UVERBS_OPTIMIZE_USING_STACK_SZ
  223. uintptr_t data[UVERBS_OPTIMIZE_USING_STACK_SZ / sizeof(uintptr_t)];
  224. #endif
  225. object_spec = uverbs_get_object(ib_dev, hdr->object_id);
  226. if (!object_spec)
  227. return -EPROTONOSUPPORT;
  228. method_spec = uverbs_get_method(object_spec, hdr->method_id);
  229. if (!method_spec)
  230. return -EPROTONOSUPPORT;
  231. if ((method_spec->flags & UVERBS_ACTION_FLAG_CREATE_ROOT) ^ !file->ucontext)
  232. return -EINVAL;
  233. ctx_size = sizeof(*ctx) +
  234. sizeof(struct uverbs_attr_bundle) +
  235. sizeof(struct uverbs_attr_bundle_hash) * method_spec->num_buckets +
  236. sizeof(*ctx->uattrs) * hdr->num_attrs +
  237. sizeof(*ctx->uverbs_attr_bundle->hash[0].attrs) *
  238. method_spec->num_child_attrs +
  239. sizeof(*ctx->uverbs_attr_bundle->hash[0].valid_bitmap) *
  240. (method_spec->num_child_attrs / BITS_PER_LONG +
  241. method_spec->num_buckets);
  242. #ifdef UVERBS_OPTIMIZE_USING_STACK_SZ
  243. if (ctx_size <= UVERBS_OPTIMIZE_USING_STACK_SZ)
  244. ctx = (void *)data;
  245. if (!ctx)
  246. #endif
  247. ctx = kmalloc(ctx_size, GFP_KERNEL);
  248. if (!ctx)
  249. return -ENOMEM;
  250. ctx->uverbs_attr_bundle = (void *)ctx + sizeof(*ctx);
  251. ctx->uattrs = (void *)(ctx->uverbs_attr_bundle + 1) +
  252. (sizeof(ctx->uverbs_attr_bundle->hash[0]) *
  253. method_spec->num_buckets);
  254. curr_attr = (void *)(ctx->uattrs + hdr->num_attrs);
  255. curr_bitmap = (void *)(curr_attr + method_spec->num_child_attrs);
  256. /*
  257. * We just fill the pointers and num_attrs here. The data itself will be
  258. * filled at a later stage (uverbs_process_attr)
  259. */
  260. for (i = 0; i < method_spec->num_buckets; i++) {
  261. unsigned int curr_num_attrs = method_spec->attr_buckets[i]->num_attrs;
  262. ctx->uverbs_attr_bundle->hash[i].attrs = curr_attr;
  263. curr_attr += curr_num_attrs;
  264. ctx->uverbs_attr_bundle->hash[i].num_attrs = curr_num_attrs;
  265. ctx->uverbs_attr_bundle->hash[i].valid_bitmap = curr_bitmap;
  266. bitmap_zero(curr_bitmap, curr_num_attrs);
  267. curr_bitmap += BITS_TO_LONGS(curr_num_attrs);
  268. }
  269. err = copy_from_user(ctx->uattrs, buf,
  270. sizeof(*ctx->uattrs) * hdr->num_attrs);
  271. if (err) {
  272. err = -EFAULT;
  273. goto out;
  274. }
  275. err = uverbs_handle_method(buf, ctx->uattrs, hdr->num_attrs, ib_dev,
  276. file, method_spec, ctx->uverbs_attr_bundle);
  277. /*
  278. * EPROTONOSUPPORT is ONLY to be returned if the ioctl framework can
  279. * not invoke the method because the request is not supported. No
  280. * other cases should return this code.
  281. */
  282. if (unlikely(err == -EPROTONOSUPPORT)) {
  283. WARN_ON_ONCE(err == -EPROTONOSUPPORT);
  284. err = -EINVAL;
  285. }
  286. out:
  287. #ifdef UVERBS_OPTIMIZE_USING_STACK_SZ
  288. if (ctx_size > UVERBS_OPTIMIZE_USING_STACK_SZ)
  289. #endif
  290. kfree(ctx);
  291. return err;
  292. }
  293. #define IB_UVERBS_MAX_CMD_SZ 4096
  294. long ib_uverbs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  295. {
  296. struct ib_uverbs_file *file = filp->private_data;
  297. struct ib_uverbs_ioctl_hdr __user *user_hdr =
  298. (struct ib_uverbs_ioctl_hdr __user *)arg;
  299. struct ib_uverbs_ioctl_hdr hdr;
  300. struct ib_device *ib_dev;
  301. int srcu_key;
  302. long err;
  303. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  304. ib_dev = srcu_dereference(file->device->ib_dev,
  305. &file->device->disassociate_srcu);
  306. if (!ib_dev) {
  307. err = -EIO;
  308. goto out;
  309. }
  310. if (cmd == RDMA_VERBS_IOCTL) {
  311. err = copy_from_user(&hdr, user_hdr, sizeof(hdr));
  312. if (err || hdr.length > IB_UVERBS_MAX_CMD_SZ ||
  313. hdr.length != sizeof(hdr) + hdr.num_attrs * sizeof(struct ib_uverbs_attr)) {
  314. err = -EINVAL;
  315. goto out;
  316. }
  317. if (hdr.reserved) {
  318. err = -EPROTONOSUPPORT;
  319. goto out;
  320. }
  321. err = ib_uverbs_cmd_verbs(ib_dev, file, &hdr,
  322. (__user void *)arg + sizeof(hdr));
  323. } else {
  324. err = -ENOIOCTLCMD;
  325. }
  326. out:
  327. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  328. return err;
  329. }