]> www.infradead.org Git - users/hch/misc.git/commitdiff
net: pktgen: fix ctrl interface command parsing
authorPeter Seiderer <ps.report@gmx.net>
Wed, 19 Feb 2025 08:45:26 +0000 (09:45 +0100)
committerJakub Kicinski <kuba@kernel.org>
Fri, 21 Feb 2025 01:24:56 +0000 (17:24 -0800)
Enable command writing without trailing '\n':

- the good case

$ echo "reset" > /proc/net/pktgen/pgctrl

- the bad case (before the patch)

$ echo -n "reset" > /proc/net/pktgen/pgctrl
-bash: echo: write error: Invalid argument

- with patch applied

$ echo -n "reset" > /proc/net/pktgen/pgctrl

Signed-off-by: Peter Seiderer <ps.report@gmx.net>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://patch.msgid.link/20250219084527.20488-7-ps.report@gmx.net
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/core/pktgen.c

index c8a5b4d174073f54c1dc0c1cee7ff89746960916..f6e35ba035c7ce941b485f4864f6e2e6f5337538 100644 (file)
@@ -517,21 +517,23 @@ static ssize_t pgctrl_write(struct file *file, const char __user *buf,
                            size_t count, loff_t *ppos)
 {
        char data[128];
+       size_t max;
        struct pktgen_net *pn = net_generic(current->nsproxy->net_ns, pg_net_id);
 
        if (!capable(CAP_NET_ADMIN))
                return -EPERM;
 
-       if (count == 0)
+       if (count < 1)
                return -EINVAL;
 
-       if (count > sizeof(data))
-               count = sizeof(data);
-
-       if (copy_from_user(data, buf, count))
+       max = min(count, sizeof(data) - 1);
+       if (copy_from_user(data, buf, max))
                return -EFAULT;
 
-       data[count - 1] = 0;    /* Strip trailing '\n' and terminate string */
+       if (data[max - 1] == '\n')
+               data[max - 1] = 0; /* strip trailing '\n', terminate string */
+       else
+               data[max] = 0; /* terminate string */
 
        if (!strcmp(data, "stop"))
                pktgen_stop_all_threads(pn);