uverbs_ioctl.c 15.1 KB
Newer Older
M
Matan Barak 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright (c) 2017, Mellanox Technologies inc.  All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <rdma/rdma_user_ioctl.h>
#include <rdma/uverbs_ioctl.h>
#include "rdma_core.h"
#include "uverbs.h"

38 39 40 41 42 43 44 45 46 47 48
static bool uverbs_is_attr_cleared(const struct ib_uverbs_attr *uattr,
				   u16 len)
{
	if (uattr->len > sizeof(((struct ib_uverbs_attr *)0)->data))
		return ib_is_buffer_cleared(u64_to_user_ptr(uattr->data) + len,
					    uattr->len - len);

	return !memchr_inv((const void *)&uattr->data + len,
			   0, uattr->len - len);
}

49
static int uverbs_process_attr(struct ib_uverbs_file *ufile,
M
Matan Barak 已提交
50 51 52 53
			       const struct ib_uverbs_attr *uattr,
			       u16 attr_id,
			       const struct uverbs_attr_spec_hash *attr_spec_bucket,
			       struct uverbs_attr_bundle_hash *attr_bundle_h,
54
			       struct uverbs_obj_attr **destroy_attr,
M
Matan Barak 已提交
55 56 57
			       struct ib_uverbs_attr __user *uattr_ptr)
{
	const struct uverbs_attr_spec *spec;
58
	const struct uverbs_attr_spec *val_spec;
M
Matan Barak 已提交
59 60 61 62 63 64 65 66 67 68 69 70
	struct uverbs_attr *e;
	const struct uverbs_object_spec *object;
	struct uverbs_obj_attr *o_attr;
	struct uverbs_attr *elements = attr_bundle_h->attrs;

	if (attr_id >= attr_spec_bucket->num_attrs) {
		if (uattr->flags & UVERBS_ATTR_F_MANDATORY)
			return -EINVAL;
		else
			return 0;
	}

71 72 73
	if (test_bit(attr_id, attr_bundle_h->valid_bitmap))
		return -EINVAL;

M
Matan Barak 已提交
74
	spec = &attr_spec_bucket->attrs[attr_id];
75
	val_spec = spec;
M
Matan Barak 已提交
76 77 78 79
	e = &elements[attr_id];
	e->uattr = uattr_ptr;

	switch (spec->type) {
80
	case UVERBS_ATTR_TYPE_ENUM_IN:
81
		if (uattr->attr_data.enum_data.elem_id >= spec->u.enum_def.num_elems)
82 83 84 85 86
			return -EOPNOTSUPP;

		if (uattr->attr_data.enum_data.reserved)
			return -EINVAL;

87
		val_spec = &spec->u2.enum_def.ids[uattr->attr_data.enum_data.elem_id];
88 89 90 91 92 93 94

		/* Currently we only support PTR_IN based enums */
		if (val_spec->type != UVERBS_ATTR_TYPE_PTR_IN)
			return -EOPNOTSUPP;

		e->ptr_attr.enum_id = uattr->attr_data.enum_data.elem_id;
	/* fall through */
M
Matan Barak 已提交
95
	case UVERBS_ATTR_TYPE_PTR_IN:
96 97 98 99 100
		/* Ensure that any data provided by userspace beyond the known
		 * struct is zero. Userspace that knows how to use some future
		 * longer struct will fail here if used with an old kernel and
		 * non-zero content, making ABI compat/discovery simpler.
		 */
101
		if (uattr->len > val_spec->u.ptr.len &&
102
		    val_spec->zero_trailing &&
103
		    !uverbs_is_attr_cleared(uattr, val_spec->u.ptr.len))
104 105 106
			return -EOPNOTSUPP;

	/* fall through */
M
Matan Barak 已提交
107
	case UVERBS_ATTR_TYPE_PTR_OUT:
108
		if (uattr->len < val_spec->u.ptr.min_len ||
109
		    (!val_spec->zero_trailing &&
110
		     uattr->len > val_spec->u.ptr.len))
111 112 113 114
			return -EINVAL;

		if (spec->type != UVERBS_ATTR_TYPE_ENUM_IN &&
		    uattr->attr_data.reserved)
M
Matan Barak 已提交
115 116 117 118
			return -EINVAL;

		e->ptr_attr.len = uattr->len;
		e->ptr_attr.flags = uattr->flags;
119

J
Jason Gunthorpe 已提交
120
		if (val_spec->alloc_and_copy && !uverbs_attr_ptr_is_inline(e)) {
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
			void *p;

			p = kvmalloc(uattr->len, GFP_KERNEL);
			if (!p)
				return -ENOMEM;

			e->ptr_attr.ptr = p;

			if (copy_from_user(p, u64_to_user_ptr(uattr->data),
					   uattr->len)) {
				kvfree(p);
				return -EFAULT;
			}
		} else {
			e->ptr_attr.data = uattr->data;
		}
M
Matan Barak 已提交
137 138 139 140
		break;

	case UVERBS_ATTR_TYPE_IDR:
	case UVERBS_ATTR_TYPE_FD:
141 142 143
		if (uattr->attr_data.reserved)
			return -EINVAL;

144
		if (uattr->len != 0)
M
Matan Barak 已提交
145 146 147
			return -EINVAL;

		o_attr = &e->obj_attr;
148
		object = uverbs_get_object(ufile, spec->u.obj.obj_type);
M
Matan Barak 已提交
149 150 151
		if (!object)
			return -EINVAL;

152 153 154 155 156 157
		/* specs are allowed to have only one destroy attribute */
		WARN_ON(spec->u.obj.access == UVERBS_ACCESS_DESTROY &&
			*destroy_attr);
		if (spec->u.obj.access == UVERBS_ACCESS_DESTROY)
			*destroy_attr = o_attr;

158 159 160 161 162 163
		/*
		 * The type of uattr->data is u64 for UVERBS_ATTR_TYPE_IDR and
		 * s64 for UVERBS_ATTR_TYPE_FD. We can cast the u64 to s64
		 * here without caring about truncation as we know that the
		 * IDR implementation today rejects negative IDs
		 */
164
		o_attr->uobject = uverbs_get_uobject_from_file(
165
					object->type_attrs,
166
					ufile,
167
					spec->u.obj.access,
168
					uattr->data_s64);
M
Matan Barak 已提交
169 170 171 172

		if (IS_ERR(o_attr->uobject))
			return PTR_ERR(o_attr->uobject);

173
		if (spec->u.obj.access == UVERBS_ACCESS_NEW) {
174
			s64 id = o_attr->uobject->id;
M
Matan Barak 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

			/* Copy the allocated id to the user-space */
			if (put_user(id, &e->uattr->data)) {
				uverbs_finalize_object(o_attr->uobject,
						       UVERBS_ACCESS_NEW,
						       false);
				return -EFAULT;
			}
		}

		break;
	default:
		return -EOPNOTSUPP;
	}

	set_bit(attr_id, attr_bundle_h->valid_bitmap);
	return 0;
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207
static int uverbs_finalize_attrs(struct uverbs_attr_bundle *attrs_bundle,
				 struct uverbs_attr_spec_hash *const *spec_hash,
				 size_t num, bool commit)
{
	unsigned int i;
	int ret = 0;

	for (i = 0; i < num; i++) {
		struct uverbs_attr_bundle_hash *curr_bundle =
			&attrs_bundle->hash[i];
		const struct uverbs_attr_spec_hash *curr_spec_bucket =
			spec_hash[i];
		unsigned int j;

208 209 210
		if (!curr_spec_bucket)
			continue;

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
		for (j = 0; j < curr_bundle->num_attrs; j++) {
			struct uverbs_attr *attr;
			const struct uverbs_attr_spec *spec;

			if (!uverbs_attr_is_valid_in_hash(curr_bundle, j))
				continue;

			attr = &curr_bundle->attrs[j];
			spec = &curr_spec_bucket->attrs[j];

			if (spec->type == UVERBS_ATTR_TYPE_IDR ||
			    spec->type == UVERBS_ATTR_TYPE_FD) {
				int current_ret;

				current_ret = uverbs_finalize_object(
					attr->obj_attr.uobject,
227
					spec->u.obj.access, commit);
228 229
				if (!ret)
					ret = current_ret;
230
			} else if (spec->type == UVERBS_ATTR_TYPE_PTR_IN &&
J
Jason Gunthorpe 已提交
231
				   spec->alloc_and_copy &&
232 233
				   !uverbs_attr_ptr_is_inline(attr)) {
				kvfree(attr->ptr_attr.ptr);
234 235 236 237 238 239
			}
		}
	}
	return ret;
}

240
static int uverbs_uattrs_process(struct ib_uverbs_file *ufile,
M
Matan Barak 已提交
241 242 243 244
				 const struct ib_uverbs_attr *uattrs,
				 size_t num_uattrs,
				 const struct uverbs_method_spec *method,
				 struct uverbs_attr_bundle *attr_bundle,
245
				 struct uverbs_obj_attr **destroy_attr,
M
Matan Barak 已提交
246 247 248 249 250 251 252 253 254 255 256 257
				 struct ib_uverbs_attr __user *uattr_ptr)
{
	size_t i;
	int ret = 0;
	int num_given_buckets = 0;

	for (i = 0; i < num_uattrs; i++) {
		const struct ib_uverbs_attr *uattr = &uattrs[i];
		u16 attr_id = uattr->attr_id;
		struct uverbs_attr_spec_hash *attr_spec_bucket;

		ret = uverbs_ns_idx(&attr_id, method->num_buckets);
258
		if (ret < 0 || !method->attr_buckets[ret]) {
M
Matan Barak 已提交
259
			if (uattr->flags & UVERBS_ATTR_F_MANDATORY) {
260 261 262 263
				uverbs_finalize_attrs(attr_bundle,
						      method->attr_buckets,
						      num_given_buckets,
						      false);
M
Matan Barak 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276
				return ret;
			}
			continue;
		}

		/*
		 * ret is the found ns, so increase num_given_buckets if
		 * necessary.
		 */
		if (ret >= num_given_buckets)
			num_given_buckets = ret + 1;

		attr_spec_bucket = method->attr_buckets[ret];
277 278
		ret = uverbs_process_attr(ufile, uattr, attr_id,
					  attr_spec_bucket,
279 280
					  &attr_bundle->hash[ret], destroy_attr,
					  uattr_ptr++);
M
Matan Barak 已提交
281
		if (ret) {
282 283 284 285
			uverbs_finalize_attrs(attr_bundle,
					      method->attr_buckets,
					      num_given_buckets,
					      false);
M
Matan Barak 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
			return ret;
		}
	}

	return num_given_buckets;
}

static int uverbs_validate_kernel_mandatory(const struct uverbs_method_spec *method_spec,
					    struct uverbs_attr_bundle *attr_bundle)
{
	unsigned int i;

	for (i = 0; i < attr_bundle->num_buckets; i++) {
		struct uverbs_attr_spec_hash *attr_spec_bucket =
			method_spec->attr_buckets[i];

302 303 304
		if (!attr_spec_bucket)
			continue;

M
Matan Barak 已提交
305 306 307 308 309 310
		if (!bitmap_subset(attr_spec_bucket->mandatory_attrs_bitmask,
				   attr_bundle->hash[i].valid_bitmap,
				   attr_spec_bucket->num_attrs))
			return -EINVAL;
	}

311 312 313 314 315 316 317 318 319
	for (; i < method_spec->num_buckets; i++) {
		struct uverbs_attr_spec_hash *attr_spec_bucket =
			method_spec->attr_buckets[i];

		if (!bitmap_empty(attr_spec_bucket->mandatory_attrs_bitmask,
				  attr_spec_bucket->num_attrs))
			return -EINVAL;
	}

M
Matan Barak 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333
	return 0;
}

static int uverbs_handle_method(struct ib_uverbs_attr __user *uattr_ptr,
				const struct ib_uverbs_attr *uattrs,
				size_t num_uattrs,
				struct ib_device *ibdev,
				struct ib_uverbs_file *ufile,
				const struct uverbs_method_spec *method_spec,
				struct uverbs_attr_bundle *attr_bundle)
{
	int ret;
	int finalize_ret;
	int num_given_buckets;
334
	struct uverbs_obj_attr *destroy_attr = NULL;
M
Matan Barak 已提交
335

336 337 338
	num_given_buckets =
		uverbs_uattrs_process(ufile, uattrs, num_uattrs, method_spec,
				      attr_bundle, &destroy_attr, uattr_ptr);
M
Matan Barak 已提交
339 340 341 342 343 344 345 346
	if (num_given_buckets <= 0)
		return -EINVAL;

	attr_bundle->num_buckets = num_given_buckets;
	ret = uverbs_validate_kernel_mandatory(method_spec, attr_bundle);
	if (ret)
		goto cleanup;

347 348 349 350 351
	/*
	 * We destroy the HW object before invoking the handler, handlers do
	 * not get to manipulate the HW objects.
	 */
	if (destroy_attr) {
352
		ret = uobj_destroy(destroy_attr->uobject);
353 354 355 356
		if (ret)
			goto cleanup;
	}

357
	ret = method_spec->handler(ufile, attr_bundle);
358

359 360 361 362 363
	if (destroy_attr) {
		uobj_put_destroy(destroy_attr->uobject);
		destroy_attr->uobject = NULL;
	}

M
Matan Barak 已提交
364
cleanup:
365 366 367 368
	finalize_ret = uverbs_finalize_attrs(attr_bundle,
					     method_spec->attr_buckets,
					     attr_bundle->num_buckets,
					     !ret);
M
Matan Barak 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

	return ret ? ret : finalize_ret;
}

#define UVERBS_OPTIMIZE_USING_STACK_SZ  256
static long ib_uverbs_cmd_verbs(struct ib_device *ib_dev,
				struct ib_uverbs_file *file,
				struct ib_uverbs_ioctl_hdr *hdr,
				void __user *buf)
{
	const struct uverbs_object_spec *object_spec;
	const struct uverbs_method_spec *method_spec;
	long err = 0;
	unsigned int i;
	struct {
		struct ib_uverbs_attr		*uattrs;
		struct uverbs_attr_bundle	*uverbs_attr_bundle;
	} *ctx = NULL;
	struct uverbs_attr *curr_attr;
	unsigned long *curr_bitmap;
	size_t ctx_size;
	uintptr_t data[UVERBS_OPTIMIZE_USING_STACK_SZ / sizeof(uintptr_t)];

392 393 394
	if (hdr->driver_id != ib_dev->driver_id)
		return -EINVAL;

395
	object_spec = uverbs_get_object(file, hdr->object_id);
M
Matan Barak 已提交
396
	if (!object_spec)
397
		return -EPROTONOSUPPORT;
M
Matan Barak 已提交
398 399 400

	method_spec = uverbs_get_method(object_spec, hdr->method_id);
	if (!method_spec)
401
		return -EPROTONOSUPPORT;
M
Matan Barak 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415

	ctx_size = sizeof(*ctx) +
		   sizeof(struct uverbs_attr_bundle) +
		   sizeof(struct uverbs_attr_bundle_hash) * method_spec->num_buckets +
		   sizeof(*ctx->uattrs) * hdr->num_attrs +
		   sizeof(*ctx->uverbs_attr_bundle->hash[0].attrs) *
		   method_spec->num_child_attrs +
		   sizeof(*ctx->uverbs_attr_bundle->hash[0].valid_bitmap) *
			(method_spec->num_child_attrs / BITS_PER_LONG +
			 method_spec->num_buckets);

	if (ctx_size <= UVERBS_OPTIMIZE_USING_STACK_SZ)
		ctx = (void *)data;
	if (!ctx)
416
		ctx = kmalloc(ctx_size, GFP_KERNEL);
M
Matan Barak 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	if (!ctx)
		return -ENOMEM;

	ctx->uverbs_attr_bundle = (void *)ctx + sizeof(*ctx);
	ctx->uattrs = (void *)(ctx->uverbs_attr_bundle + 1) +
			      (sizeof(ctx->uverbs_attr_bundle->hash[0]) *
			       method_spec->num_buckets);
	curr_attr = (void *)(ctx->uattrs + hdr->num_attrs);
	curr_bitmap = (void *)(curr_attr + method_spec->num_child_attrs);

	/*
	 * We just fill the pointers and num_attrs here. The data itself will be
	 * filled at a later stage (uverbs_process_attr)
	 */
	for (i = 0; i < method_spec->num_buckets; i++) {
432 433 434 435 436 437
		unsigned int curr_num_attrs;

		if (!method_spec->attr_buckets[i])
			continue;

		curr_num_attrs = method_spec->attr_buckets[i]->num_attrs;
M
Matan Barak 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

		ctx->uverbs_attr_bundle->hash[i].attrs = curr_attr;
		curr_attr += curr_num_attrs;
		ctx->uverbs_attr_bundle->hash[i].num_attrs = curr_num_attrs;
		ctx->uverbs_attr_bundle->hash[i].valid_bitmap = curr_bitmap;
		bitmap_zero(curr_bitmap, curr_num_attrs);
		curr_bitmap += BITS_TO_LONGS(curr_num_attrs);
	}

	err = copy_from_user(ctx->uattrs, buf,
			     sizeof(*ctx->uattrs) * hdr->num_attrs);
	if (err) {
		err = -EFAULT;
		goto out;
	}

	err = uverbs_handle_method(buf, ctx->uattrs, hdr->num_attrs, ib_dev,
				   file, method_spec, ctx->uverbs_attr_bundle);
456 457 458 459 460 461 462 463 464 465

	/*
	 * EPROTONOSUPPORT is ONLY to be returned if the ioctl framework can
	 * not invoke the method because the request is not supported.  No
	 * other cases should return this code.
	*/
	if (unlikely(err == -EPROTONOSUPPORT)) {
		WARN_ON_ONCE(err == -EPROTONOSUPPORT);
		err = -EINVAL;
	}
M
Matan Barak 已提交
466
out:
467 468
	if (ctx != (void *)data)
		kfree(ctx);
M
Matan Barak 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	return err;
}

#define IB_UVERBS_MAX_CMD_SZ 4096

long ib_uverbs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct ib_uverbs_file *file = filp->private_data;
	struct ib_uverbs_ioctl_hdr __user *user_hdr =
		(struct ib_uverbs_ioctl_hdr __user *)arg;
	struct ib_uverbs_ioctl_hdr hdr;
	struct ib_device *ib_dev;
	int srcu_key;
	long err;

	srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
	ib_dev = srcu_dereference(file->device->ib_dev,
				  &file->device->disassociate_srcu);
	if (!ib_dev) {
		err = -EIO;
		goto out;
	}

	if (cmd == RDMA_VERBS_IOCTL) {
		err = copy_from_user(&hdr, user_hdr, sizeof(hdr));

		if (err || hdr.length > IB_UVERBS_MAX_CMD_SZ ||
		    hdr.length != sizeof(hdr) + hdr.num_attrs * sizeof(struct ib_uverbs_attr)) {
			err = -EINVAL;
			goto out;
		}

501
		if (hdr.reserved1 || hdr.reserved2) {
502
			err = -EPROTONOSUPPORT;
M
Matan Barak 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515
			goto out;
		}

		err = ib_uverbs_cmd_verbs(ib_dev, file, &hdr,
					  (__user void *)arg + sizeof(hdr));
	} else {
		err = -ENOIOCTLCMD;
	}
out:
	srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);

	return err;
}
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
		       size_t idx, u64 allowed_bits)
{
	const struct uverbs_attr *attr;
	u64 flags;

	attr = uverbs_attr_get(attrs_bundle, idx);
	/* Missing attribute means 0 flags */
	if (IS_ERR(attr)) {
		*to = 0;
		return 0;
	}

	/*
	 * New userspace code should use 8 bytes to pass flags, but we
	 * transparently support old userspaces that were using 4 bytes as
	 * well.
	 */
	if (attr->ptr_attr.len == 8)
		flags = attr->ptr_attr.data;
	else if (attr->ptr_attr.len == 4)
		memcpy(&flags, &attr->ptr_attr.data, 4);
	else
		return -EINVAL;

	if (flags & ~allowed_bits)
		return -EINVAL;

	*to = flags;
	return 0;
}
EXPORT_SYMBOL(uverbs_get_flags64);

int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
		       size_t idx, u64 allowed_bits)
{
	u64 flags;
	int ret;

	ret = uverbs_get_flags64(&flags, attrs_bundle, idx, allowed_bits);
	if (ret)
		return ret;

	if (flags > U32_MAX)
		return -EINVAL;
	*to = flags;

	return 0;
}
EXPORT_SYMBOL(uverbs_get_flags32);