Message ID | 20240221113204.147478-1-aleksei.kodanev@bell-sw.com (mailing list archive) |
---|---|
State | Accepted |
Headers | show |
Series | RDMA/rtrs-clt: check strnlen return len in sysfs mpath_policy_store() | expand |
On Wed, Feb 21, 2024 at 12:33 PM Alexey Kodanev <aleksei.kodanev@bell-sw.com> wrote: > > strnlen() may return 0 (e.g. for "\0\n" string), it's better to > check the result of strnlen() before using 'len - 1' expression > for the 'buf' array index. > > Detected using the static analysis tool - Svace. > > Fixes: dc3b66a0ce70 ("RDMA/rtrs-clt: Add a minimum latency multipath policy") > Signed-off-by: Alexey Kodanev <aleksei.kodanev@bell-sw.com> lgtm, thx Acked-by: Jack Wang <jinpu.wang@ionos.com> > --- > drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c > index d3c436ead694..4aa80c9388f0 100644 > --- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c > +++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c > @@ -133,7 +133,7 @@ static ssize_t mpath_policy_store(struct device *dev, > > /* distinguish "mi" and "min-latency" with length */ > len = strnlen(buf, NAME_MAX); > - if (buf[len - 1] == '\n') > + if (len && buf[len - 1] == '\n') > len--; > > if (!strncasecmp(buf, "round-robin", 11) || > -- > 2.25.1 >
On Wed, 21 Feb 2024 11:32:04 +0000, Alexey Kodanev wrote: > strnlen() may return 0 (e.g. for "\0\n" string), it's better to > check the result of strnlen() before using 'len - 1' expression > for the 'buf' array index. > > Detected using the static analysis tool - Svace. > > > [...] Applied, thanks! [1/1] RDMA/rtrs-clt: check strnlen return len in sysfs mpath_policy_store() https://git.kernel.org/rdma/rdma/c/7a7b7f575a25aa Best regards,
diff --git a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c index d3c436ead694..4aa80c9388f0 100644 --- a/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c +++ b/drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c @@ -133,7 +133,7 @@ static ssize_t mpath_policy_store(struct device *dev, /* distinguish "mi" and "min-latency" with length */ len = strnlen(buf, NAME_MAX); - if (buf[len - 1] == '\n') + if (len && buf[len - 1] == '\n') len--; if (!strncasecmp(buf, "round-robin", 11) ||
strnlen() may return 0 (e.g. for "\0\n" string), it's better to check the result of strnlen() before using 'len - 1' expression for the 'buf' array index. Detected using the static analysis tool - Svace. Fixes: dc3b66a0ce70 ("RDMA/rtrs-clt: Add a minimum latency multipath policy") Signed-off-by: Alexey Kodanev <aleksei.kodanev@bell-sw.com> --- drivers/infiniband/ulp/rtrs/rtrs-clt-sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)