uverbs_ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. for (; i < method_spec->num_buckets; i++) {
  204. struct uverbs_attr_spec_hash *attr_spec_bucket =
  205. method_spec->attr_buckets[i];
  206. if (!bitmap_empty(attr_spec_bucket->mandatory_attrs_bitmask,
  207. attr_spec_bucket->num_attrs))
  208. return -EINVAL;
  209. }
  210. return 0;
  211. }
  212. static int uverbs_handle_method(struct ib_uverbs_attr __user *uattr_ptr,
  213. const struct ib_uverbs_attr *uattrs,
  214. size_t num_uattrs,
  215. struct ib_device *ibdev,
  216. struct ib_uverbs_file *ufile,
  217. const struct uverbs_method_spec *method_spec,
  218. struct uverbs_attr_bundle *attr_bundle)
  219. {
  220. int ret;
  221. int finalize_ret;
  222. int num_given_buckets;
  223. num_given_buckets = uverbs_uattrs_process(ibdev, ufile->ucontext, uattrs,
  224. num_uattrs, method_spec,
  225. attr_bundle, uattr_ptr);
  226. if (num_given_buckets <= 0)
  227. return -EINVAL;
  228. attr_bundle->num_buckets = num_given_buckets;
  229. ret = uverbs_validate_kernel_mandatory(method_spec, attr_bundle);
  230. if (ret)
  231. goto cleanup;
  232. ret = method_spec->handler(ibdev, ufile, attr_bundle);
  233. cleanup:
  234. finalize_ret = uverbs_finalize_objects(attr_bundle,
  235. method_spec->attr_buckets,
  236. attr_bundle->num_buckets,
  237. !ret);
  238. return ret ? ret : finalize_ret;
  239. }
  240. #define UVERBS_OPTIMIZE_USING_STACK_SZ 256
  241. static long ib_uverbs_cmd_verbs(struct ib_device *ib_dev,
  242. struct ib_uverbs_file *file,
  243. struct ib_uverbs_ioctl_hdr *hdr,
  244. void __user *buf)
  245. {
  246. const struct uverbs_object_spec *object_spec;
  247. const struct uverbs_method_spec *method_spec;
  248. long err = 0;
  249. unsigned int i;
  250. struct {
  251. struct ib_uverbs_attr *uattrs;
  252. struct uverbs_attr_bundle *uverbs_attr_bundle;
  253. } *ctx = NULL;
  254. struct uverbs_attr *curr_attr;
  255. unsigned long *curr_bitmap;
  256. size_t ctx_size;
  257. uintptr_t data[UVERBS_OPTIMIZE_USING_STACK_SZ / sizeof(uintptr_t)];
  258. if (hdr->driver_id != ib_dev->driver_id)
  259. return -EINVAL;
  260. object_spec = uverbs_get_object(ib_dev, hdr->object_id);
  261. if (!object_spec)
  262. return -EPROTONOSUPPORT;
  263. method_spec = uverbs_get_method(object_spec, hdr->method_id);
  264. if (!method_spec)
  265. return -EPROTONOSUPPORT;
  266. if ((method_spec->flags & UVERBS_ACTION_FLAG_CREATE_ROOT) ^ !file->ucontext)
  267. return -EINVAL;
  268. ctx_size = sizeof(*ctx) +
  269. sizeof(struct uverbs_attr_bundle) +
  270. sizeof(struct uverbs_attr_bundle_hash) * method_spec->num_buckets +
  271. sizeof(*ctx->uattrs) * hdr->num_attrs +
  272. sizeof(*ctx->uverbs_attr_bundle->hash[0].attrs) *
  273. method_spec->num_child_attrs +
  274. sizeof(*ctx->uverbs_attr_bundle->hash[0].valid_bitmap) *
  275. (method_spec->num_child_attrs / BITS_PER_LONG +
  276. method_spec->num_buckets);
  277. if (ctx_size <= UVERBS_OPTIMIZE_USING_STACK_SZ)
  278. ctx = (void *)data;
  279. if (!ctx)
  280. ctx = kmalloc(ctx_size, GFP_KERNEL);
  281. if (!ctx)
  282. return -ENOMEM;
  283. ctx->uverbs_attr_bundle = (void *)ctx + sizeof(*ctx);
  284. ctx->uattrs = (void *)(ctx->uverbs_attr_bundle + 1) +
  285. (sizeof(ctx->uverbs_attr_bundle->hash[0]) *
  286. method_spec->num_buckets);
  287. curr_attr = (void *)(ctx->uattrs + hdr->num_attrs);
  288. curr_bitmap = (void *)(curr_attr + method_spec->num_child_attrs);
  289. /*
  290. * We just fill the pointers and num_attrs here. The data itself will be
  291. * filled at a later stage (uverbs_process_attr)
  292. */
  293. for (i = 0; i < method_spec->num_buckets; i++) {
  294. unsigned int curr_num_attrs = method_spec->attr_buckets[i]->num_attrs;
  295. ctx->uverbs_attr_bundle->hash[i].attrs = curr_attr;
  296. curr_attr += curr_num_attrs;
  297. ctx->uverbs_attr_bundle->hash[i].num_attrs = curr_num_attrs;
  298. ctx->uverbs_attr_bundle->hash[i].valid_bitmap = curr_bitmap;
  299. bitmap_zero(curr_bitmap, curr_num_attrs);
  300. curr_bitmap += BITS_TO_LONGS(curr_num_attrs);
  301. }
  302. err = copy_from_user(ctx->uattrs, buf,
  303. sizeof(*ctx->uattrs) * hdr->num_attrs);
  304. if (err) {
  305. err = -EFAULT;
  306. goto out;
  307. }
  308. err = uverbs_handle_method(buf, ctx->uattrs, hdr->num_attrs, ib_dev,
  309. file, method_spec, ctx->uverbs_attr_bundle);
  310. /*
  311. * EPROTONOSUPPORT is ONLY to be returned if the ioctl framework can
  312. * not invoke the method because the request is not supported. No
  313. * other cases should return this code.
  314. */
  315. if (unlikely(err == -EPROTONOSUPPORT)) {
  316. WARN_ON_ONCE(err == -EPROTONOSUPPORT);
  317. err = -EINVAL;
  318. }
  319. out:
  320. if (ctx != (void *)data)
  321. kfree(ctx);
  322. return err;
  323. }
  324. #define IB_UVERBS_MAX_CMD_SZ 4096
  325. long ib_uverbs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  326. {
  327. struct ib_uverbs_file *file = filp->private_data;
  328. struct ib_uverbs_ioctl_hdr __user *user_hdr =
  329. (struct ib_uverbs_ioctl_hdr __user *)arg;
  330. struct ib_uverbs_ioctl_hdr hdr;
  331. struct ib_device *ib_dev;
  332. int srcu_key;
  333. long err;
  334. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  335. ib_dev = srcu_dereference(file->device->ib_dev,
  336. &file->device->disassociate_srcu);
  337. if (!ib_dev) {
  338. err = -EIO;
  339. goto out;
  340. }
  341. if (cmd == RDMA_VERBS_IOCTL) {
  342. err = copy_from_user(&hdr, user_hdr, sizeof(hdr));
  343. if (err || hdr.length > IB_UVERBS_MAX_CMD_SZ ||
  344. hdr.length != sizeof(hdr) + hdr.num_attrs * sizeof(struct ib_uverbs_attr)) {
  345. err = -EINVAL;
  346. goto out;
  347. }
  348. if (hdr.reserved1 || hdr.reserved2) {
  349. err = -EPROTONOSUPPORT;
  350. goto out;
  351. }
  352. err = ib_uverbs_cmd_verbs(ib_dev, file, &hdr,
  353. (__user void *)arg + sizeof(hdr));
  354. } else {
  355. err = -ENOIOCTLCMD;
  356. }
  357. out:
  358. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  359. return err;
  360. }