Blame libnvme-1.7-stack_smashing_3.patch

Tomas Bzatek cef1f1
From 3bf6e153a1c02b1c684ac3f5949cd32dec5f46c9 Mon Sep 17 00:00:00 2001
Tomas Bzatek cef1f1
From: Tomas Bzatek <tbzatek@redhat.com>
Tomas Bzatek cef1f1
Date: Thu, 12 Oct 2023 18:42:34 +0200
Tomas Bzatek cef1f1
Subject: [PATCH] linux: Allocate aligned payloads for id_ctrl and id_ns calls
Tomas Bzatek cef1f1
Tomas Bzatek cef1f1
Signed-off-by: Tomas Bzatek <tbzatek@redhat.com>
Tomas Bzatek cef1f1
---
Tomas Bzatek cef1f1
 src/nvme/linux.c | 61 ++++++++++++++++++++++++++++++++++--------------
Tomas Bzatek cef1f1
 1 file changed, 43 insertions(+), 18 deletions(-)
Tomas Bzatek cef1f1
Tomas Bzatek cef1f1
diff --git a/src/nvme/linux.c b/src/nvme/linux.c
Tomas Bzatek cef1f1
index adbc4cdb..66be9eb8 100644
Tomas Bzatek cef1f1
--- a/src/nvme/linux.c
Tomas Bzatek cef1f1
+++ b/src/nvme/linux.c
Tomas Bzatek cef1f1
@@ -124,28 +124,37 @@ int nvme_fw_download_seq(int fd, __u32 size, __u32 xfer, __u32 offset,
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
 int nvme_get_telemetry_max(int fd, enum nvme_telemetry_da *da, size_t *data_tx)
Tomas Bzatek cef1f1
 {
Tomas Bzatek cef1f1
-	struct nvme_id_ctrl id_ctrl;
Tomas Bzatek cef1f1
-	int err = nvme_identify_ctrl(fd, &id_ctrl);
Tomas Bzatek cef1f1
+	struct nvme_id_ctrl *id_ctrl;
Tomas Bzatek cef1f1
+	int err;
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
-	if (err)
Tomas Bzatek cef1f1
+	id_ctrl = __nvme_alloc(sizeof(*id_ctrl));
Tomas Bzatek cef1f1
+	if (!id_ctrl) {
Tomas Bzatek cef1f1
+		errno = ENOMEM;
Tomas Bzatek cef1f1
+		return -1;
Tomas Bzatek cef1f1
+	}
Tomas Bzatek cef1f1
+	err = nvme_identify_ctrl(fd, id_ctrl);
Tomas Bzatek cef1f1
+	if (err) {
Tomas Bzatek cef1f1
+		free(id_ctrl);
Tomas Bzatek cef1f1
 		return err;
Tomas Bzatek cef1f1
+	}
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
 	if (data_tx) {
Tomas Bzatek cef1f1
-		*data_tx = id_ctrl.mdts;
Tomas Bzatek cef1f1
-		if (id_ctrl.mdts) {
Tomas Bzatek cef1f1
+		*data_tx = id_ctrl->mdts;
Tomas Bzatek cef1f1
+		if (id_ctrl->mdts) {
Tomas Bzatek cef1f1
 			/* assuming CAP.MPSMIN is zero minimum Memory Page Size is at least
Tomas Bzatek cef1f1
 			 * 4096 bytes
Tomas Bzatek cef1f1
 			 */
Tomas Bzatek cef1f1
-			*data_tx = (1 << id_ctrl.mdts) * 4096;
Tomas Bzatek cef1f1
+			*data_tx = (1 << id_ctrl->mdts) * 4096;
Tomas Bzatek cef1f1
 		}
Tomas Bzatek cef1f1
 	}
Tomas Bzatek cef1f1
 	if (da) {
Tomas Bzatek cef1f1
-		if (id_ctrl.lpa & 0x8)
Tomas Bzatek cef1f1
+		if (id_ctrl->lpa & 0x8)
Tomas Bzatek cef1f1
 			*da = NVME_TELEMETRY_DA_3;
Tomas Bzatek cef1f1
-		if (id_ctrl.lpa & 0x40)
Tomas Bzatek cef1f1
+		if (id_ctrl->lpa & 0x40)
Tomas Bzatek cef1f1
 			*da = NVME_TELEMETRY_DA_4;
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
 	}
Tomas Bzatek cef1f1
+	free(id_ctrl);
Tomas Bzatek cef1f1
 	return err;
Tomas Bzatek cef1f1
 }
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
@@ -376,32 +385,48 @@ int nvme_namespace_detach_ctrls(int fd, __u32 nsid, __u16 num_ctrls,
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
 int nvme_get_ana_log_len(int fd, size_t *analen)
Tomas Bzatek cef1f1
 {
Tomas Bzatek cef1f1
-	struct nvme_id_ctrl ctrl;
Tomas Bzatek cef1f1
+	struct nvme_id_ctrl *ctrl;
Tomas Bzatek cef1f1
 	int ret;
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
-	ret = nvme_identify_ctrl(fd, &ctrl);
Tomas Bzatek cef1f1
-	if (ret)
Tomas Bzatek cef1f1
+	ctrl = __nvme_alloc(sizeof(*ctrl));
Tomas Bzatek cef1f1
+	if (!ctrl) {
Tomas Bzatek cef1f1
+		errno = ENOMEM;
Tomas Bzatek cef1f1
+		return -1;
Tomas Bzatek cef1f1
+	}
Tomas Bzatek cef1f1
+	ret = nvme_identify_ctrl(fd, ctrl);
Tomas Bzatek cef1f1
+	if (ret) {
Tomas Bzatek cef1f1
+		free(ctrl);
Tomas Bzatek cef1f1
 		return ret;
Tomas Bzatek cef1f1
+	}
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
 	*analen = sizeof(struct nvme_ana_log) +
Tomas Bzatek cef1f1
-		le32_to_cpu(ctrl.nanagrpid) * sizeof(struct nvme_ana_group_desc) +
Tomas Bzatek cef1f1
-		le32_to_cpu(ctrl.mnan) * sizeof(__le32);
Tomas Bzatek cef1f1
+		le32_to_cpu(ctrl->nanagrpid) * sizeof(struct nvme_ana_group_desc) +
Tomas Bzatek cef1f1
+		le32_to_cpu(ctrl->mnan) * sizeof(__le32);
Tomas Bzatek cef1f1
+	free(ctrl);
Tomas Bzatek cef1f1
 	return 0;
Tomas Bzatek cef1f1
 }
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
 int nvme_get_logical_block_size(int fd, __u32 nsid, int *blksize)
Tomas Bzatek cef1f1
 {
Tomas Bzatek cef1f1
-	struct nvme_id_ns ns;
Tomas Bzatek cef1f1
+	struct nvme_id_ns *ns;
Tomas Bzatek cef1f1
 	__u8 flbas;
Tomas Bzatek cef1f1
 	int ret;
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
-	ret = nvme_identify_ns(fd, nsid, &ns);
Tomas Bzatek cef1f1
-	if (ret)
Tomas Bzatek cef1f1
+	ns = __nvme_alloc(sizeof(*ns));
Tomas Bzatek cef1f1
+	if (!ns) {
Tomas Bzatek cef1f1
+		errno = ENOMEM;
Tomas Bzatek cef1f1
+		return -1;
Tomas Bzatek cef1f1
+	}
Tomas Bzatek cef1f1
+	ret = nvme_identify_ns(fd, nsid, ns);
Tomas Bzatek cef1f1
+	if (ret) {
Tomas Bzatek cef1f1
+		free(ns);
Tomas Bzatek cef1f1
 		return ret;
Tomas Bzatek cef1f1
+	}
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
-	nvme_id_ns_flbas_to_lbaf_inuse(ns.flbas, &flbas);
Tomas Bzatek cef1f1
-	*blksize = 1 << ns.lbaf[flbas].ds;
Tomas Bzatek cef1f1
+	nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &flbas);
Tomas Bzatek cef1f1
+	*blksize = 1 << ns->lbaf[flbas].ds;
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
+	free(ns);
Tomas Bzatek cef1f1
 	return 0;
Tomas Bzatek cef1f1
 }
Tomas Bzatek cef1f1