RDMA/core: Fix storing node_desc
PATCH 1/1 RDMA/core: Fix storing node_desc
When writing to node_desc sysfs using echo a new line symbol will be stored at the end of the string, avoid that by dropping the new line symbol and also make sure to return -EINVAL when the supplied string is bigger then IB_DEVICE_NODE_DESC_MAX.
--- drivers/infiniband/core/sysfs.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/infiniband/core/sysfs.c +10 -1
@@ -1263,12 +1263,21 @@ static ssize_t node_desc_store(struct device *device, {struct ib_device *dev =rdma_device_to_ibdev (device);struct ib_device_modify desc = {}; +size_t len;int ret;if (!dev->ops.modify_device)return -EOPNOTSUPP ; -memcpy (desc.node_desc, buf,min_t (int, count,IB_DEVICE_NODE_DESC_MAX )); +if (count >IB_DEVICE_NODE_DESC_MAX ) +return -EINVAL ; + + len =strlen (buf); +if (buf[len -1 ] =='\n' ) + len--; + +strncpy (desc.node_desc, buf, len); + desc.node_desc[len] =0 ; ret =ib_modify_device (dev,IB_DEVICE_MODIFY_NODE_DESC , &desc);if (ret)return ret;
Signature · 1 line
2.21.0
Quoted 2 lines · Kamal Heib
When writing to
node_descsysfs using echo a new line symbol will be stored at the end of the string, avoid that by dropping the new line
Why do we want to do this? AFAIK technically new line is valid in a node description.
Quoted 2 lines
symbol and also make sure to return
-EINVALwhen the supplied string is bigger thenIB_DEVICE_NODE_DESC_MAX.
This makes sense though
Quoted 9 lines
drivers/infiniband/core/sysfs.c +2
-0+++ b/drivers/infiniband/core/sysfs.c@@ -1263,12 +1263,21 @@ static ssize_t node_desc_store(struct device *device, {struct ib_device *dev =rdma_device_to_ibdev (device);struct ib_device_modify desc = {}; +size_t len;int ret;
drivers/infiniband/core/sysfs.c static ssize_t node_desc_store(struct device *device, · line 1276
Quoted 10 lines
drivers/infiniband/core/sysfs.c +4
-1if (!dev->ops.modify_device)return -EOPNOTSUPP ; -memcpy (desc.node_desc, buf,min_t (int, count,IB_DEVICE_NODE_DESC_MAX )); +if (count >IB_DEVICE_NODE_DESC_MAX ) +return -EINVAL ; + + len =strlen (buf);
Why strlen? The buf is count bytes long.
drivers/infiniband/core/sysfs.c static ssize_t node_desc_store(struct device *device, · line 1277
Quoted 2 lines
drivers/infiniband/core/sysfs.c +2
-0+if (buf[len -1 ] =='\n' ) + len--;
And if it is zero bytes this buffer underflows
drivers/infiniband/core/sysfs.c static ssize_t node_desc_store(struct device *device, · line 1279
Quoted 1 line
drivers/infiniband/core/sysfs.c +1
-0+strncpy (desc.node_desc, buf, len);
What was the point of switching away from memcpy?
drivers/infiniband/core/sysfs.c static ssize_t node_desc_store(struct device *device, · line 1283
Quoted 4 lines
drivers/infiniband/core/sysfs.c +1
-0+ desc.node_desc[len] =0 ; ret =ib_modify_device (dev,IB_DEVICE_MODIFY_NODE_DESC , &desc);if (ret)return ret;
Jason
Quoted 7 lines · Jason Gunthorpe
Quoted 2 lines · Kamal Heib
When writing to
node_descsysfs using echo a new line symbol will be stored at the end of the string, avoid that by dropping the new lineWhy do we want to do this? AFAIK technically new line is valid in a node description.
Self-Nack, please drop this patch, I didn't do a good job here.
Thanks,
Kamal
Quoted 42 lines
Quoted 2 lines
symbol and also make sure to return
-EINVALwhen the supplied string is bigger thenIB_DEVICE_NODE_DESC_MAX.This makes sense though
Quoted 9 lines
drivers/infiniband/core/sysfs.c +2
-0+++ b/drivers/infiniband/core/sysfs.c@@ -1263,12 +1263,21 @@ static ssize_t node_desc_store(struct device *device, {struct ib_device *dev =rdma_device_to_ibdev (device);struct ib_device_modify desc = {}; +size_t len;int ret;drivers/infiniband/core/sysfs.c static ssize_t node_desc_store(struct device *device, · line 1276
Quoted 10 lines
drivers/infiniband/core/sysfs.c +4
-1if (!dev->ops.modify_device)return -EOPNOTSUPP ; -memcpy (desc.node_desc, buf,min_t (int, count,IB_DEVICE_NODE_DESC_MAX )); +if (count >IB_DEVICE_NODE_DESC_MAX ) +return -EINVAL ; + + len =strlen (buf);Why strlen? The buf is count bytes long.
drivers/infiniband/core/sysfs.c static ssize_t node_desc_store(struct device *device, · line 1277
Quoted 2 lines
drivers/infiniband/core/sysfs.c +2
-0+if (buf[len -1 ] =='\n' ) + len--;And if it is zero bytes this buffer underflows
drivers/infiniband/core/sysfs.c static ssize_t node_desc_store(struct device *device, · line 1279
Quoted 1 line
drivers/infiniband/core/sysfs.c +1
-0+strncpy (desc.node_desc, buf, len);What was the point of switching away from memcpy?
drivers/infiniband/core/sysfs.c static ssize_t node_desc_store(struct device *device, · line 1283
Quoted 4 lines
drivers/infiniband/core/sysfs.c +1
-0+ desc.node_desc[len] =0 ; ret =ib_modify_device (dev,IB_DEVICE_MODIFY_NODE_DESC , &desc);if (ret)return ret;Jason