Blame libnvme-1.7-stack_smashing_2.patch

Tomas Bzatek cef1f1
From 68c6ffb11d40a427fc1fd70ac2ac97fd01952913 Mon Sep 17 00:00:00 2001
Tomas Bzatek cef1f1
From: Tomas Bzatek <tbzatek@redhat.com>
Tomas Bzatek cef1f1
Date: Tue, 10 Oct 2023 18:18:38 +0200
Tomas Bzatek cef1f1
Subject: [PATCH] tree: Allocate aligned payloads for ns scan
Tomas Bzatek cef1f1
Tomas Bzatek cef1f1
libnvme is actually doing some namespace identification
Tomas Bzatek cef1f1
during tree scan, leading to stack smash on some systems.
Tomas Bzatek cef1f1
Tomas Bzatek cef1f1
Signed-off-by: Tomas Bzatek <tbzatek@redhat.com>
Tomas Bzatek cef1f1
---
Tomas Bzatek cef1f1
 src/nvme/tree.c | 29 ++++++++++++++++++-----------
Tomas Bzatek cef1f1
 1 file changed, 18 insertions(+), 11 deletions(-)
Tomas Bzatek cef1f1
Tomas Bzatek cef1f1
diff --git a/src/nvme/tree.c b/src/nvme/tree.c
Tomas Bzatek cef1f1
index 00cf96f7..5636aa18 100644
Tomas Bzatek cef1f1
--- a/src/nvme/tree.c
Tomas Bzatek cef1f1
+++ b/src/nvme/tree.c
Tomas Bzatek cef1f1
@@ -2404,26 +2404,33 @@ static void nvme_ns_parse_descriptors(struct nvme_ns *n,
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
 static int nvme_ns_init(struct nvme_ns *n)
Tomas Bzatek cef1f1
 {
Tomas Bzatek cef1f1
-	struct nvme_id_ns ns = { };
Tomas Bzatek cef1f1
-	uint8_t buffer[NVME_IDENTIFY_DATA_SIZE] = { };
Tomas Bzatek cef1f1
-	struct nvme_ns_id_desc *descs = (void *)buffer;
Tomas Bzatek cef1f1
+	struct nvme_id_ns *ns;
Tomas Bzatek cef1f1
+	struct nvme_ns_id_desc *descs;
Tomas Bzatek cef1f1
 	uint8_t flbas;
Tomas Bzatek cef1f1
 	int ret;
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
-	ret = nvme_ns_identify(n, &ns);
Tomas Bzatek cef1f1
-	if (ret)
Tomas Bzatek cef1f1
+	ns = __nvme_alloc(sizeof(*ns));
Tomas Bzatek cef1f1
+	if (!ns)
Tomas Bzatek cef1f1
+		return 0;
Tomas Bzatek cef1f1
+	ret = nvme_ns_identify(n, 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
-	n->lba_shift = ns.lbaf[flbas].ds;
Tomas Bzatek cef1f1
+	nvme_id_ns_flbas_to_lbaf_inuse(ns->flbas, &flbas);
Tomas Bzatek cef1f1
+	n->lba_shift = ns->lbaf[flbas].ds;
Tomas Bzatek cef1f1
 	n->lba_size = 1 << n->lba_shift;
Tomas Bzatek cef1f1
-	n->lba_count = le64_to_cpu(ns.nsze);
Tomas Bzatek cef1f1
-	n->lba_util = le64_to_cpu(ns.nuse);
Tomas Bzatek cef1f1
-	n->meta_size = le16_to_cpu(ns.lbaf[flbas].ms);
Tomas Bzatek cef1f1
+	n->lba_count = le64_to_cpu(ns->nsze);
Tomas Bzatek cef1f1
+	n->lba_util = le64_to_cpu(ns->nuse);
Tomas Bzatek cef1f1
+	n->meta_size = le16_to_cpu(ns->lbaf[flbas].ms);
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
-	if (!nvme_ns_identify_descs(n, descs))
Tomas Bzatek cef1f1
+	descs = __nvme_alloc(NVME_IDENTIFY_DATA_SIZE);
Tomas Bzatek cef1f1
+	if (descs && !nvme_ns_identify_descs(n, descs))
Tomas Bzatek cef1f1
 		nvme_ns_parse_descriptors(n, descs);
Tomas Bzatek cef1f1
 
Tomas Bzatek cef1f1
+	free(ns);
Tomas Bzatek cef1f1
+	free(descs);
Tomas Bzatek cef1f1
 	return 0;
Tomas Bzatek cef1f1
 }
Tomas Bzatek cef1f1