[PATCH v1] proc: limit schedstate node write operation

From: Junwen Wu
Date: Fri Apr 22 2022 - 22:34:05 EST


Whatever value is written to /proc/$pid/sched, a task's schedstate data
will reset.In some cases, schedstate will drop by accident. We restrict
writing a certain value to this node before the data is reset.

Signed-off-by: Junwen Wu <wudaemon@xxxxxxx>
---
fs/proc/base.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index d654ce7150fd..6bb2677659ce 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -1459,13 +1459,21 @@ sched_write(struct file *file, const char __user *buf,
{
struct inode *inode = file_inode(file);
struct task_struct *p;
+ char ubuf[5];

- p = get_proc_task(inode);
- if (!p)
- return -ESRCH;
- proc_sched_set_task(p);
+ memset(ubuf, 0, sizeof(ubuf));
+ if (count > 5)
+ count = 0;
+ if (copy_from_user(ubuf, buf, count))
+ return -EFAULT;
+ if (strcmp(ubuf, "reset") == 0) {
+ p = get_proc_task(inode);
+ if (!p)
+ return -ESRCH;
+ proc_sched_set_task(p);

- put_task_struct(p);
+ put_task_struct(p);
+ }

return count;
}
--
2.25.1