UPSTREAM: 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 <>
Signed-off-by: Peter Zijlstra (Intel) <>
Signed-off-by: Ingo Molnar <>
Acked-by: Johannes Weiner <>

(cherry picked from commit 6fcca0fa48118e6d63733eb4644c6cd880c15b8f)

Bug: 148159562
Signed-off-by: Suren Baghdasaryan <>
Change-Id: I9ec7acfc6e1083c677a95b0ea1c559ab50152873
diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index e88918e..aa4eeaa 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -1190,6 +1190,9 @@
 	if (static_branch_likely(&psi_disabled))
 		return -EOPNOTSUPP;
+	if (!nbytes)
+		return -EINVAL;
 	buf_size = min(nbytes, (sizeof(buf) - 1));
 	if (copy_from_user(buf, user_buf, buf_size))
 		return -EFAULT;