2020-02-03 21:23:51

by Suren Baghdasaryan

[permalink] [raw]
Subject: [PATCH] psi: Fix OOB write when writing 0 bytes to psi files

Issuing write() with count parameter set to 0 on any file under
/proc/pressure/ will cause an OOB write because of the access to
buf[buf_size-1] when NUL-termination is performed. Fix this by checking
for buf_size to be non-zero.

Signed-off-by: Suren Baghdasaryan <[email protected]>
---
kernel/sched/psi.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index db7b50bba3f1..38ccd49b9bf6 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -1199,6 +1199,9 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,
if (static_branch_likely(&psi_disabled))
return -EOPNOTSUPP;

+ if (!nbytes)
+ return -EINVAL;
+
buf_size = min(nbytes, sizeof(buf));
if (copy_from_user(buf, user_buf, buf_size))
return -EFAULT;
--
2.25.0.341.g760bfbb309-goog


2020-02-04 18:56:37

by Johannes Weiner

[permalink] [raw]
Subject: Re: [PATCH] psi: Fix OOB write when writing 0 bytes to psi files

On Mon, Feb 03, 2020 at 01:22:16PM -0800, Suren Baghdasaryan wrote:
> Issuing write() with count parameter set to 0 on any file under
> /proc/pressure/ will cause an OOB write because of the access to
> buf[buf_size-1] when NUL-termination is performed. Fix this by checking
> for buf_size to be non-zero.
>
> Signed-off-by: Suren Baghdasaryan <[email protected]>

Acked-by: Johannes Weiner <[email protected]>

Thanks Suren.

2020-02-06 14:06:22

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH] psi: Fix OOB write when writing 0 bytes to psi files

On Tue, Feb 04, 2020 at 01:55:23PM -0500, Johannes Weiner wrote:
> On Mon, Feb 03, 2020 at 01:22:16PM -0800, Suren Baghdasaryan wrote:
> > Issuing write() with count parameter set to 0 on any file under
> > /proc/pressure/ will cause an OOB write because of the access to
> > buf[buf_size-1] when NUL-termination is performed. Fix this by checking
> > for buf_size to be non-zero.
> >
> > Signed-off-by: Suren Baghdasaryan <[email protected]>
>
> Acked-by: Johannes Weiner <[email protected]>

Thanks!

Subject: [tip: sched/urgent] sched/psi: Fix OOB write when writing 0 bytes to PSI files

The following commit has been merged into the sched/urgent branch of tip:

Commit-ID: 6fcca0fa48118e6d63733eb4644c6cd880c15b8f
Gitweb: https://git.kernel.org/tip/6fcca0fa48118e6d63733eb4644c6cd880c15b8f
Author: Suren Baghdasaryan <[email protected]>
AuthorDate: Mon, 03 Feb 2020 13:22:16 -08:00
Committer: Ingo Molnar <[email protected]>
CommitterDate: Tue, 11 Feb 2020 13:00:02 +01:00

sched/psi: Fix OOB write when writing 0 bytes to PSI files

Issuing write() with count parameter set to 0 on any file under
/proc/pressure/ will cause an OOB write because of the access to
buf[buf_size-1] when NUL-termination is performed. Fix this by checking
for buf_size to be non-zero.

Signed-off-by: Suren Baghdasaryan <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Signed-off-by: Ingo Molnar <[email protected]>
Acked-by: Johannes Weiner <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
---
kernel/sched/psi.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index db7b50b..38ccd49 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -1199,6 +1199,9 @@ static ssize_t psi_write(struct file *file, const char __user *user_buf,
if (static_branch_likely(&psi_disabled))
return -EOPNOTSUPP;

+ if (!nbytes)
+ return -EINVAL;
+
buf_size = min(nbytes, sizeof(buf));
if (copy_from_user(buf, user_buf, buf_size))
return -EFAULT;