Move the parsing into ddebug_proc_write_buf() and implement
->proc_write_buf() with it so procfs copies in what is written before
it pins the entry. The debugfs file keeps copying in itself. Writes
larger than USER_BUF_PAGE - 1 bytes to the procfs file now fail with
-EINVAL from procfs instead of -E2BIG.

Signed-off-by: Christian Brauner (Amutable) <[email protected]>
---
 lib/dynamic_debug.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 64a211290872..1d6012c9eb41 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -1002,11 +1002,28 @@ __setup("dyndbg=", dyndbg_setup);
  * command text from userspace, parses and executes it.
  */
 #define USER_BUF_PAGE 4096
+
+static ssize_t ddebug_proc_write_buf(struct file *file, char *buf, size_t len,
+                                    loff_t *offp)
+{
+       int ret;
+
+       if (len == 0)
+               return 0;
+       v2pr_info("read %zu bytes from userspace\n", len);
+
+       ret = ddebug_exec_queries(buf, NULL);
+       if (ret < 0)
+               return ret;
+
+       *offp += len;
+       return len;
+}
+
 static ssize_t ddebug_proc_write(struct file *file, const char __user *ubuf,
                                  size_t len, loff_t *offp)
 {
-       char *tmpbuf;
-       int ret;
+       char *tmpbuf __free(kfree) = NULL;
 
        if (len == 0)
                return 0;
@@ -1017,15 +1034,7 @@ static ssize_t ddebug_proc_write(struct file *file, 
const char __user *ubuf,
        tmpbuf = memdup_user_nul(ubuf, len);
        if (IS_ERR(tmpbuf))
                return PTR_ERR(tmpbuf);
-       v2pr_info("read %zu bytes from userspace\n", len);
-
-       ret = ddebug_exec_queries(tmpbuf, NULL);
-       kfree(tmpbuf);
-       if (ret < 0)
-               return ret;
-
-       *offp += len;
-       return len;
+       return ddebug_proc_write_buf(file, tmpbuf, len, offp);
 }
 
 /*
@@ -1196,7 +1205,8 @@ static const struct file_operations ddebug_proc_fops = {
 static const struct proc_ops proc_fops = {
        .proc_seq_ops = &ddebug_proc_seqops,
        .proc_state_size = sizeof(struct ddebug_iter),
-       .proc_write = ddebug_proc_write
+       .proc_write_buf = ddebug_proc_write_buf,
+       .proc_write_size = USER_BUF_PAGE - 1,
 };
 
 static void ddebug_attach_module_classes(struct ddebug_table *dt,

-- 
2.53.0


Reply via email to