Skip to contentSkip to conversation

libmultipath, kpartx: fix callers of dm_get_next_target()

PATCH In review [email protected] 2 participants 2 messages May 31, 2022 – Jun 9, 2022 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libmultipath, kpartx: fix callers of dm_get_next_target()

From: Martin Wilck <[email protected]>

dm_get_next_target() may return NULL for target_type and params in some situations. Follows the same idea as the previous patch "dm_get_map: fix segfault when can't found target" by Wu Guanghao.

---
 kpartx/devmapper.c       |  6 ++++--
 libmultipath/devmapper.c | 10 ++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)
kpartx/devmapper.c +4 -2
index 49ffd31..bf14c78 100644
@@ -412,8 +412,10 @@ dm_get_map(const char *mapname, char * outparams)
 		goto out;
 
 	/* Fetch 1st target */
-	dm_get_next_target(dmt, NULL, &start, &length,
-			   &target_type, &params);
+	if (dm_get_next_target(dmt, NULL, &start, &length,
+			       &target_type, &params) != NULL || !params)
+		/* more than one target or not found target */
+		goto out;
 
 	if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
 		r = 0;
libmultipath/devmapper.c +10 -0
index 9819e29..1ada3fb 100644
@@ -1715,6 +1715,16 @@ int dm_reassign_table(const char *name, char *old, char *new)
 	do {
 		next = dm_get_next_target(dmt, next, &start, &length,
 					  &target, &params);
+		if (!target || !params) {
+			/*
+			 * We can't call dm_task_add_target() with
+			 * invalid parameters. But simply dropping this
+			 * target feels wrong, too. Abort and warn.
+			 */
+			condlog(1, "%s: invalid target found in map %s",
+				__func__, name);
+			goto out_reload;
+		}
 		buff = strdup(params);
 		if (!buff) {
 			condlog(3, "%s: failed to replace target %s, "
Signature · 6 lines
2.36.1

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · On Tue, May 31, 2022 at 12:24:45PM +0200, [email protected] wrote:

From: Martin Wilck <[email protected]>

dm_get_next_target() may return NULL for target_type and params in some situations. Follows the same idea as the previous patch "dm_get_map: fix segfault when can't found target" by Wu Guanghao.

Quoted 45 lines
---
 kpartx/devmapper.c       |  6 ++++--
 libmultipath/devmapper.c | 10 ++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)
kpartx/devmapper.c +4 -2
index 49ffd31..bf14c78 100644
@@ -412,8 +412,10 @@ dm_get_map(const char *mapname, char * outparams)
 		goto out;
 
 	/* Fetch 1st target */
-	dm_get_next_target(dmt, NULL, &start, &length,
-			   &target_type, &params);
+	if (dm_get_next_target(dmt, NULL, &start, &length,
+			       &target_type, &params) != NULL || !params)
+		/* more than one target or not found target */
+		goto out;
 
 	if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
 		r = 0;
libmultipath/devmapper.c +10 -0
index 9819e29..1ada3fb 100644
@@ -1715,6 +1715,16 @@ int dm_reassign_table(const char *name, char *old, char *new)
 	do {
 		next = dm_get_next_target(dmt, next, &start, &length,
 					  &target, &params);
+		if (!target || !params) {
+			/*
+			 * We can't call dm_task_add_target() with
+			 * invalid parameters. But simply dropping this
+			 * target feels wrong, too. Abort and warn.
+			 */
+			condlog(1, "%s: invalid target found in map %s",
+				__func__, name);
+			goto out_reload;
+		}
 		buff = strdup(params);
 		if (!buff) {
 			condlog(3, "%s: failed to replace target %s, "
Signature · 1 line
2.36.1

--
dm-devel mailing list
[email protected]
https://listman.redhat.com/mailman/listinfo/dm-devel