[PATCH 1/2] nvme: Ensure char members of struct nvme_id_ctrl are null-terminated

From: Erwan Velu
Date: Tue Jun 14 2022 - 17:09:15 EST


When issueing an identify command, the resulting data structure stored
as a nvme_id_ctrl struct, contains a couple of char members.

These char members features:
- the serial number (sn)
- the model number (mn)
- the firmware revision (fr)

As per the current code, nothing enforce these members to be
null-terminated which causes issues when manipulating them with regular
string manupulation functions.

This commit ensure the three of them are always null-terminated.

Signed-off-by: Erwan Velu <e.velu@xxxxxxxxxx>
---
drivers/nvme/host/core.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 24165daee3c8..0f7e625e8bd0 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -1274,6 +1274,13 @@ static int nvme_identify_ctrl(struct nvme_ctrl *dev, struct nvme_id_ctrl **id)
sizeof(struct nvme_id_ctrl));
if (error)
kfree(*id);
+ else {
+ /* Ensure that model, serial and firmware fields are always null-terminated */
+ (*id)->mn[sizeof((*id)->mn)-1] = 0;
+ (*id)->sn[sizeof((*id)->sn)-1] = 0;
+ (*id)->fr[sizeof((*id)->fr)-1] = 0;
+ }
+
return error;
}

--
2.35.3