uverbs_ioctl.c 12 KB

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