Skip to contentSkip to conversation

checker: add input check of state in checker_state_name func

PATCH In review Zhiqiang Liu 2 participants 3 messages Aug 10, 2020 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checker: add input check of state in checker_state_name func

In checker_state_name func, we donot check whether input i is valid. It may cause array access violation problem.

---
 libmultipath/checkers.c | 26 +++++++++++++++-----------
 libmultipath/checkers.h |  3 ++-
 2 files changed, 17 insertions(+), 12 deletions(-)
libmultipath/checkers.c +15 -11
index c3cf53db..c91e1686 100644
@@ -22,23 +22,27 @@ struct checker_class {
 	short msgtable_size;
 };
 
-char *checker_state_names[] = {
-	"wild",
-	"unchecked",
-	"down",
-	"up",
-	"shaky",
-	"ghost",
-	"pending",
-	"timeout",
-	"removed",
-	"delayed",
+static const char *checker_state_names[PATH_MAX_STATE] = {
+	[PATH_WILD] = "wild",
+	[PATH_UNCHECKED] = "unchecked",
+	[PATH_DOWN] = "down",
+	[PATH_UP] = "up",
+	[PATH_SHAKY] = "shaky",
+	[PATH_GHOST] = "ghost",
+	[PATH_PENDING] = "pending",
+	[PATH_TIMEOUT] = "timeout",
+	[PATH_REMOVED] = "removed",
+	[PATH_DELAYED] = "delayed",
 };
 
 static LIST_HEAD(checkers);
 
 const char *checker_state_name(int i)
 {
+	if (i < 0 || i >= PATH_MAX_STATE) {
+		condlog (2, "invalid state index = %d", i);
+		return INVALID;
+	}
 	return checker_state_names[i];
 }
 
libmultipath/checkers.h +2 -1
index 5237e7ec..93052b72 100644
@@ -67,7 +67,7 @@
  *   During this time, it is marked as "delayed"
  */
 enum path_check_state {
-	PATH_WILD,
+	PATH_WILD = 0,
 	PATH_UNCHECKED,
 	PATH_DOWN,
 	PATH_UP,
@@ -88,6 +88,7 @@ enum path_check_state {
 #define READSECTOR0  "readsector0"
 #define CCISS_TUR    "cciss_tur"
 #define NONE         "none"
+#define INVALID      "invalid"
 
 #define ASYNC_TIMEOUT_SEC	30
 
Signature · 1 line
2.24.0.windows.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Zhiqiang Liu

In checker_state_name func, we donot check whether input i is valid. It may cause array access violation problem.

---
 libmultipath/checkers.c | 26 +++++++++++++++-----------
 libmultipath/checkers.h |  3 ++-
 2 files changed, 17 insertions(+), 12 deletions(-)

Thanks for the patch.

Btw, are you currently reviewing multipath-tools with coverity or some similar tool?

Martin

#2 lore raw
Quoted 11 lines · Martin Wilck
Quoted 9 lines · Zhiqiang Liu

In checker_state_name func, we donot check whether input i is valid. It may cause array access violation problem.

---
 libmultipath/checkers.c | 26 +++++++++++++++-----------
 libmultipath/checkers.h |  3 ++-
 2 files changed, 17 insertions(+), 12 deletions(-)
Quoted 8 lines

Thanks for the patch.

Btw, are you currently reviewing multipath-tools with coverity or some similar tool?

Martin

Thanks for your reply.
Actually, I just read and learned multipath-tools code without using any tools. In the future, we may try to use some scanning tools for deep analysis.

Thanks for your review, again.