All of lore.kernel.org
 help / color / mirror / Atom feed
From: Leon Romanovsky <leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Doug Ledford <dledford-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>,
	Jason Gunthorpe <jgg-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org>
Cc: Leon Romanovsky <leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	RDMA mailing list
	<linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	Matan Barak <matanb-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org>,
	Noa Osherovich <noaos-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org>,
	Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org>
Subject: [PATCH rdma-next 11/14] RDMA/uverbs: Refactor the header validation logic
Date: Wed, 14 Feb 2018 14:38:41 +0200	[thread overview]
Message-ID: <20180214123844.30321-12-leon@kernel.org> (raw)
In-Reply-To: <20180214123844.30321-1-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>

From: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org>

Move all header validation logic to be performed before SRCU read lock.

Signed-off-by: Leon Romanovsky <leonro-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org>
---
 drivers/infiniband/core/uverbs_main.c | 90 ++++++++++++++++++-----------------
 1 file changed, 47 insertions(+), 43 deletions(-)

diff --git a/drivers/infiniband/core/uverbs_main.c b/drivers/infiniband/core/uverbs_main.c
index f8f2182ab86b..e07326139ce9 100644
--- a/drivers/infiniband/core/uverbs_main.c
+++ b/drivers/infiniband/core/uverbs_main.c
@@ -677,6 +677,42 @@ static ssize_t process_hdr(struct ib_uverbs_cmd_hdr *hdr,
 	return 0;
 }
 
+static ssize_t verify_hdr(struct ib_uverbs_cmd_hdr *hdr,
+			  struct ib_uverbs_ex_cmd_hdr *ex_hdr,
+			  size_t count, bool extended)
+{
+	if (extended) {
+		count -= sizeof(*hdr) + sizeof(*ex_hdr);
+
+		if ((hdr->in_words + ex_hdr->provider_in_words) * 8 != count)
+			return -EINVAL;
+
+		if (ex_hdr->cmd_hdr_reserved)
+			return -EINVAL;
+
+		if (ex_hdr->response) {
+			if (!hdr->out_words && !ex_hdr->provider_out_words)
+				return -EINVAL;
+
+			if (!access_ok(VERIFY_WRITE,
+				       u64_to_user_ptr(ex_hdr->response),
+				       (hdr->out_words + ex_hdr->provider_out_words) * 8))
+				return -EFAULT;
+		} else {
+			if (hdr->out_words || ex_hdr->provider_out_words)
+				return -EINVAL;
+		}
+
+		return 0;
+	}
+
+	/* not extended command */
+	if (hdr->in_words * 4 != count)
+		return -EINVAL;
+
+	return 0;
+}
+
 static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
 			     size_t count, loff_t *pos)
 {
@@ -716,6 +752,10 @@ static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
 			return -EFAULT;
 	}
 
+	ret = verify_hdr(&hdr, &ex_hdr, count, extended);
+	if (ret)
+		return ret;
+
 	srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
 	ib_dev = srcu_dereference(file->device->ib_dev,
 				  &file->device->disassociate_srcu);
@@ -729,52 +769,17 @@ static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
 		goto out;
 	}
 
-	if (!extended) {
-		if (hdr.in_words * 4 != count) {
-			ret = -EINVAL;
-			goto out;
-		}
+	buf += sizeof(hdr);
 
-		ret = uverbs_cmd_table[command](file, ib_dev,
-						 buf + sizeof(hdr),
-						 hdr.in_words * 4,
-						 hdr.out_words * 4);
+	if (!extended) {
+		ret = uverbs_cmd_table[command](file, ib_dev, buf,
+						hdr.in_words * 4,
+						hdr.out_words * 4);
 	} else {
 		struct ib_udata ucore;
 		struct ib_udata uhw;
-		size_t written_count = count;
-
-		count -= sizeof(hdr) + sizeof(ex_hdr);
-		buf += sizeof(hdr) + sizeof(ex_hdr);
 
-		if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count) {
-			ret = -EINVAL;
-			goto out;
-		}
-
-		if (ex_hdr.cmd_hdr_reserved) {
-			ret = -EINVAL;
-			goto out;
-		}
-
-		if (ex_hdr.response) {
-			if (!hdr.out_words && !ex_hdr.provider_out_words) {
-				ret = -EINVAL;
-				goto out;
-			}
-
-			if (!access_ok(VERIFY_WRITE,
-				       u64_to_user_ptr(ex_hdr.response),
-				       (hdr.out_words + ex_hdr.provider_out_words) * 8)) {
-				ret = -EFAULT;
-				goto out;
-			}
-		} else {
-			if (hdr.out_words || ex_hdr.provider_out_words) {
-				ret = -EINVAL;
-				goto out;
-			}
-		}
+		buf += sizeof(ex_hdr);
 
 		ib_uverbs_init_udata_buf_or_null(&ucore, buf,
 					u64_to_user_ptr(ex_hdr.response),
@@ -787,8 +792,7 @@ static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
 					ex_hdr.provider_out_words * 8);
 
 		ret = uverbs_ex_cmd_table[command](file, ib_dev, &ucore, &uhw);
-		if (!ret)
-			ret = written_count;
+		ret = (ret) ? : count;
 	}
 
 out:
-- 
2.16.1

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2018-02-14 12:38 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-14 12:38 [PATCH rdma-next 00/14] Refactor ib_uverbs_write path Leon Romanovsky
     [not found] ` <20180214123844.30321-1-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2018-02-14 12:38   ` [PATCH rdma-next 01/14] RDMA/uverbs: Convert command mask validity check function to be bool Leon Romanovsky
     [not found]     ` <20180214123844.30321-2-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2018-02-14 16:26       ` Dennis Dalessandro
2018-02-14 23:41       ` Jason Gunthorpe
     [not found]         ` <20180214234141.GC1718-uk2M96/98Pc@public.gmane.org>
2018-02-15 14:27           ` Leon Romanovsky
2018-02-14 12:38   ` [PATCH rdma-next 02/14] RDMA/uverbs: Update sizeof users Leon Romanovsky
     [not found]     ` <20180214123844.30321-3-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2018-02-14 16:27       ` Dennis Dalessandro
2018-02-14 12:38   ` [PATCH rdma-next 03/14] RDMA/uverbs: Refactor flags checks and update return value Leon Romanovsky
     [not found]     ` <20180214123844.30321-4-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2018-02-14 23:40       ` Jason Gunthorpe
2018-02-14 12:38   ` [PATCH rdma-next 04/14] RDMA/uverbs: Fail as early as possible if not enough header data was provided Leon Romanovsky
2018-02-14 12:38   ` [PATCH rdma-next 05/14] RDMA/uverbs: Return not supported error code for unsupported commands Leon Romanovsky
2018-02-14 12:38   ` [PATCH rdma-next 06/14] RDMA/uverbs: Unify return values of not supported command Leon Romanovsky
2018-02-14 12:38   ` [PATCH rdma-next 07/14] RDMA/uverbs: Refactor command header processing Leon Romanovsky
     [not found]     ` <20180214123844.30321-8-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2018-02-14 23:49       ` Jason Gunthorpe
     [not found]         ` <20180214234951.GF1718-uk2M96/98Pc@public.gmane.org>
2018-02-15 13:51           ` Leon Romanovsky
     [not found]             ` <20180215135120.GG2197-U/DQcQFIOTAAJjI8aNfphQ@public.gmane.org>
2018-02-15 16:23               ` Jason Gunthorpe
2018-02-14 12:38   ` [PATCH rdma-next 08/14] RDMA/uverbs: Properly check command supported mask Leon Romanovsky
     [not found]     ` <20180214123844.30321-9-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2018-02-14 23:47       ` Jason Gunthorpe
     [not found]         ` <20180214234714.GE1718-uk2M96/98Pc@public.gmane.org>
2018-02-15 13:56           ` Leon Romanovsky
     [not found]             ` <20180215135628.GH2197-U/DQcQFIOTAAJjI8aNfphQ@public.gmane.org>
2018-02-15 16:26               ` Jason Gunthorpe
     [not found]                 ` <20180215162603.GB18416-uk2M96/98Pc@public.gmane.org>
2018-02-16  7:31                   ` Leon Romanovsky
     [not found]                     ` <20180216073147.GL2197-U/DQcQFIOTAAJjI8aNfphQ@public.gmane.org>
2018-02-16 15:50                       ` Jason Gunthorpe
2018-02-14 12:38   ` [PATCH rdma-next 09/14] RDMA/uverbs: Move uncontext check before SRCU read lock Leon Romanovsky
2018-02-14 12:38   ` [PATCH rdma-next 10/14] RDMa/uverbs: Copy ex_hdr outside of " Leon Romanovsky
2018-02-14 12:38   ` Leon Romanovsky [this message]
     [not found]     ` <20180214123844.30321-12-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2018-02-14 23:46       ` [PATCH rdma-next 11/14] RDMA/uverbs: Refactor the header validation logic Jason Gunthorpe
     [not found]         ` <20180214234635.GD1718-uk2M96/98Pc@public.gmane.org>
2018-02-15 14:26           ` Leon Romanovsky
2018-02-14 12:38   ` [PATCH rdma-next 12/14] RDMA/verbs: Return proper error code for not supported system call Leon Romanovsky
2018-02-14 12:38   ` [PATCH rdma-next 13/14] RDMA/verbs: Check existence of function prior to accessing it Leon Romanovsky
     [not found]     ` <20180214123844.30321-14-leon-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2018-02-16 16:19       ` Jason Gunthorpe
     [not found]         ` <20180216161956.GA29023-uk2M96/98Pc@public.gmane.org>
2018-02-16 17:09           ` Leon Romanovsky
2018-02-14 12:38   ` [PATCH rdma-next 14/14] RDMA/verbs: Drop uverbs_ex_mask Leon Romanovsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180214123844.30321-12-leon@kernel.org \
    --to=leon-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
    --cc=dledford-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=jgg-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org \
    --cc=leonro-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org \
    --cc=linux-rdma-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=matanb-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org \
    --cc=noaos-VPRAkNaXOzVWk0Htik3J/w@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.