linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] IB/mlx5: fix uaccess beyond "count" in debugfs read/write handlers
@ 2018-07-06 20:48 Jann Horn
  2018-07-08  6:56 ` Leon Romanovsky
  2018-07-09 19:18 ` Jason Gunthorpe
  0 siblings, 2 replies; 4+ messages in thread
From: Jann Horn @ 2018-07-06 20:48 UTC (permalink / raw)
  To: Leon Romanovsky, Doug Ledford, Jason Gunthorpe, jannh
  Cc: linux-rdma, linux-kernel

In general, accessing userspace memory beyond the length of the supplied
buffer in VFS read/write handlers can lead to both kernel memory corruption
(via kernel_read()/kernel_write(), which can e.g. be triggered via
sys_splice()) and privilege escalation inside userspace.

In this case, the affected files are in debugfs (and should therefore only
be accessible to root), and the read handlers check that *pos is zero
(meaning that at least sys_splice() can't trigger kernel memory
corruption). Because of the root requirement, this is not a security fix,
but rather a cleanup.

For the read handlers, fix it by using simple_read_from_buffer() instead of
custom logic. Add min() calls to the write handlers.

changed in v2:
 - also fix write handlers

Fixes: 4a2da0b8c078 ("IB/mlx5: Add debug control parameters for congestion control")
Fixes: e126ba97dba9 ("mlx5: Add driver for Mellanox Connect-IB adapters")
Signed-off-by: Jann Horn <jannh@google.com>
---
 drivers/infiniband/hw/mlx5/cong.c |  9 +--------
 drivers/infiniband/hw/mlx5/mr.c   | 32 ++++++++-----------------------
 2 files changed, 9 insertions(+), 32 deletions(-)

diff --git a/drivers/infiniband/hw/mlx5/cong.c b/drivers/infiniband/hw/mlx5/cong.c
index 985fa2637390..7e4e358a4fd8 100644
--- a/drivers/infiniband/hw/mlx5/cong.c
+++ b/drivers/infiniband/hw/mlx5/cong.c
@@ -359,9 +359,6 @@ static ssize_t get_param(struct file *filp, char __user *buf, size_t count,
 	int ret;
 	char lbuf[11];
 
-	if (*pos)
-		return 0;
-
 	ret = mlx5_ib_get_cc_params(param->dev, param->port_num, offset, &var);
 	if (ret)
 		return ret;
@@ -370,11 +367,7 @@ static ssize_t get_param(struct file *filp, char __user *buf, size_t count,
 	if (ret < 0)
 		return ret;
 
-	if (copy_to_user(buf, lbuf, ret))
-		return -EFAULT;
-
-	*pos += ret;
-	return ret;
+	return simple_read_from_buffer(buf, count, pos, lbuf, ret);
 }
 
 static const struct file_operations dbg_cc_fops = {
diff --git a/drivers/infiniband/hw/mlx5/mr.c b/drivers/infiniband/hw/mlx5/mr.c
index 90a9c461cedc..308456d28afb 100644
--- a/drivers/infiniband/hw/mlx5/mr.c
+++ b/drivers/infiniband/hw/mlx5/mr.c
@@ -271,16 +271,16 @@ static ssize_t size_write(struct file *filp, const char __user *buf,
 {
 	struct mlx5_cache_ent *ent = filp->private_data;
 	struct mlx5_ib_dev *dev = ent->dev;
-	char lbuf[20];
+	char lbuf[20] = {0};
 	u32 var;
 	int err;
 	int c;
 
-	if (copy_from_user(lbuf, buf, sizeof(lbuf)))
+	count = min(count, sizeof(lbuf) - 1);
+	if (copy_from_user(lbuf, buf, count))
 		return -EFAULT;
 
 	c = order2idx(dev, ent->order);
-	lbuf[sizeof(lbuf) - 1] = 0;
 
 	if (sscanf(lbuf, "%u", &var) != 1)
 		return -EINVAL;
@@ -310,19 +310,11 @@ static ssize_t size_read(struct file *filp, char __user *buf, size_t count,
 	char lbuf[20];
 	int err;
 
-	if (*pos)
-		return 0;
-
 	err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->size);
 	if (err < 0)
 		return err;
 
-	if (copy_to_user(buf, lbuf, err))
-		return -EFAULT;
-
-	*pos += err;
-
-	return err;
+	return simple_read_from_buffer(buf, count, pos, lbuf, err);
 }
 
 static const struct file_operations size_fops = {
@@ -337,16 +329,16 @@ static ssize_t limit_write(struct file *filp, const char __user *buf,
 {
 	struct mlx5_cache_ent *ent = filp->private_data;
 	struct mlx5_ib_dev *dev = ent->dev;
-	char lbuf[20];
+	char lbuf[20] = {0};
 	u32 var;
 	int err;
 	int c;
 
-	if (copy_from_user(lbuf, buf, sizeof(lbuf)))
+	count = min(count, sizeof(lbuf) - 1);
+	if (copy_from_user(lbuf, buf, count))
 		return -EFAULT;
 
 	c = order2idx(dev, ent->order);
-	lbuf[sizeof(lbuf) - 1] = 0;
 
 	if (sscanf(lbuf, "%u", &var) != 1)
 		return -EINVAL;
@@ -372,19 +364,11 @@ static ssize_t limit_read(struct file *filp, char __user *buf, size_t count,
 	char lbuf[20];
 	int err;
 
-	if (*pos)
-		return 0;
-
 	err = snprintf(lbuf, sizeof(lbuf), "%d\n", ent->limit);
 	if (err < 0)
 		return err;
 
-	if (copy_to_user(buf, lbuf, err))
-		return -EFAULT;
-
-	*pos += err;
-
-	return err;
+	return simple_read_from_buffer(buf, count, pos, lbuf, err);
 }
 
 static const struct file_operations limit_fops = {
-- 
2.18.0.203.gfac676dfb9-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] IB/mlx5: fix uaccess beyond "count" in debugfs read/write handlers
  2018-07-06 20:48 [PATCH v2] IB/mlx5: fix uaccess beyond "count" in debugfs read/write handlers Jann Horn
@ 2018-07-08  6:56 ` Leon Romanovsky
  2018-07-09 12:22   ` Jann Horn
  2018-07-09 19:18 ` Jason Gunthorpe
  1 sibling, 1 reply; 4+ messages in thread
From: Leon Romanovsky @ 2018-07-08  6:56 UTC (permalink / raw)
  To: Jann Horn; +Cc: Doug Ledford, Jason Gunthorpe, linux-rdma, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1340 bytes --]

On Fri, Jul 06, 2018 at 10:48:03PM +0200, Jann Horn wrote:
> In general, accessing userspace memory beyond the length of the supplied
> buffer in VFS read/write handlers can lead to both kernel memory corruption
> (via kernel_read()/kernel_write(), which can e.g. be triggered via
> sys_splice()) and privilege escalation inside userspace.
>
> In this case, the affected files are in debugfs (and should therefore only
> be accessible to root), and the read handlers check that *pos is zero
> (meaning that at least sys_splice() can't trigger kernel memory
> corruption). Because of the root requirement, this is not a security fix,
> but rather a cleanup.
>
> For the read handlers, fix it by using simple_read_from_buffer() instead of
> custom logic. Add min() calls to the write handlers.
>
> changed in v2:
>  - also fix write handlers

No changelog in commit messages, please

>
> Fixes: 4a2da0b8c078 ("IB/mlx5: Add debug control parameters for congestion control")
> Fixes: e126ba97dba9 ("mlx5: Add driver for Mellanox Connect-IB adapters")
> Signed-off-by: Jann Horn <jannh@google.com>
> ---
>  drivers/infiniband/hw/mlx5/cong.c |  9 +--------
>  drivers/infiniband/hw/mlx5/mr.c   | 32 ++++++++-----------------------
>  2 files changed, 9 insertions(+), 32 deletions(-)
>

Thanks,
Reviewed-by: Leon Romanovsky <leonro@mellanox.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 801 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] IB/mlx5: fix uaccess beyond "count" in debugfs read/write handlers
  2018-07-08  6:56 ` Leon Romanovsky
@ 2018-07-09 12:22   ` Jann Horn
  0 siblings, 0 replies; 4+ messages in thread
From: Jann Horn @ 2018-07-09 12:22 UTC (permalink / raw)
  To: leon; +Cc: dledford, jgg, linux-rdma, kernel list

On Sun, Jul 8, 2018 at 8:56 AM Leon Romanovsky <leon@kernel.org> wrote:
>
> On Fri, Jul 06, 2018 at 10:48:03PM +0200, Jann Horn wrote:
> > In general, accessing userspace memory beyond the length of the supplied
> > buffer in VFS read/write handlers can lead to both kernel memory corruption
> > (via kernel_read()/kernel_write(), which can e.g. be triggered via
> > sys_splice()) and privilege escalation inside userspace.
> >
> > In this case, the affected files are in debugfs (and should therefore only
> > be accessible to root), and the read handlers check that *pos is zero
> > (meaning that at least sys_splice() can't trigger kernel memory
> > corruption). Because of the root requirement, this is not a security fix,
> > but rather a cleanup.
> >
> > For the read handlers, fix it by using simple_read_from_buffer() instead of
> > custom logic. Add min() calls to the write handlers.
> >
> > changed in v2:
> >  - also fix write handlers
>
> No changelog in commit messages, please

I will try to do that better in the future. I never noticed that
changelogs go below the three dashes...

> > Fixes: 4a2da0b8c078 ("IB/mlx5: Add debug control parameters for congestion control")
> > Fixes: e126ba97dba9 ("mlx5: Add driver for Mellanox Connect-IB adapters")
> > Signed-off-by: Jann Horn <jannh@google.com>
> > ---
> >  drivers/infiniband/hw/mlx5/cong.c |  9 +--------
> >  drivers/infiniband/hw/mlx5/mr.c   | 32 ++++++++-----------------------
> >  2 files changed, 9 insertions(+), 32 deletions(-)
> >
>
> Thanks,
> Reviewed-by: Leon Romanovsky <leonro@mellanox.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] IB/mlx5: fix uaccess beyond "count" in debugfs read/write handlers
  2018-07-06 20:48 [PATCH v2] IB/mlx5: fix uaccess beyond "count" in debugfs read/write handlers Jann Horn
  2018-07-08  6:56 ` Leon Romanovsky
@ 2018-07-09 19:18 ` Jason Gunthorpe
  1 sibling, 0 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2018-07-09 19:18 UTC (permalink / raw)
  To: Jann Horn; +Cc: Leon Romanovsky, Doug Ledford, linux-rdma, linux-kernel

On Fri, Jul 06, 2018 at 10:48:03PM +0200, Jann Horn wrote:
> In general, accessing userspace memory beyond the length of the supplied
> buffer in VFS read/write handlers can lead to both kernel memory corruption
> (via kernel_read()/kernel_write(), which can e.g. be triggered via
> sys_splice()) and privilege escalation inside userspace.
> 
> In this case, the affected files are in debugfs (and should therefore only
> be accessible to root), and the read handlers check that *pos is zero
> (meaning that at least sys_splice() can't trigger kernel memory
> corruption). Because of the root requirement, this is not a security fix,
> but rather a cleanup.
> 
> For the read handlers, fix it by using simple_read_from_buffer() instead of
> custom logic. Add min() calls to the write handlers.
> 
> changed in v2:
>  - also fix write handlers
> 
> Fixes: 4a2da0b8c078 ("IB/mlx5: Add debug control parameters for congestion control")
> Fixes: e126ba97dba9 ("mlx5: Add driver for Mellanox Connect-IB adapters")
> Signed-off-by: Jann Horn <jannh@google.com>
> Reviewed-by: Leon Romanovsky <leonro@mellanox.com>
> ---
>  drivers/infiniband/hw/mlx5/cong.c |  9 +--------
>  drivers/infiniband/hw/mlx5/mr.c   | 32 ++++++++-----------------------
>  2 files changed, 9 insertions(+), 32 deletions(-)

Applied to for-next

Thanks,
Jason

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-07-09 19:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-06 20:48 [PATCH v2] IB/mlx5: fix uaccess beyond "count" in debugfs read/write handlers Jann Horn
2018-07-08  6:56 ` Leon Romanovsky
2018-07-09 12:22   ` Jann Horn
2018-07-09 19:18 ` Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).