[PATCH] fpga: fpga-sec-mgr: handle trailing newline in filename_store

From: Martin Hundebøll
Date: Thu Nov 26 2020 - 08:19:51 EST


The copying of the filename written to the sysfs unconditionally remove
the last character, as (I assume) if it were a newline char. For the
most cases this is true (e.g. when using `echo` in the shell), but some
software writes the filename without a trailing newline, in which the
firmware load obviously fails.

Fix this by checking for a newline char, and replacing it with '\0' if
found.

Fixes: 1815cc58d473 ("fpga: sec-mgr: enable secure updates")
Signed-off-by: Martin Hundebøll <mhu@xxxxxxxxxx>
---
drivers/fpga/fpga-sec-mgr.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/fpga/fpga-sec-mgr.c b/drivers/fpga/fpga-sec-mgr.c
index 72b61dc173db..d2ce4c682bd9 100644
--- a/drivers/fpga/fpga-sec-mgr.c
+++ b/drivers/fpga/fpga-sec-mgr.c
@@ -423,12 +423,16 @@ static ssize_t filename_store(struct device *dev, struct device_attribute *attr,
goto unlock_exit;
}

- smgr->filename = kstrndup(buf, count - 1, GFP_KERNEL);
+ smgr->filename = kstrndup(buf, count, GFP_KERNEL);
if (!smgr->filename) {
ret = -ENOMEM;
goto unlock_exit;
}

+ /* remove trailing newline */
+ if (smgr->filename[count - 1] == '\n')
+ smgr->filename[count - 1] = '\0';
+
smgr->err_code = FPGA_SEC_ERR_NONE;
smgr->hw_errinfo = 0;
smgr->request_cancel = false;
--
2.29.2


--------------C1B711DDA171BC4A3054BAAD--