[sheepdog] [PATCH Update] collie: more strick check against 'node kill'

Liu Yuan namei.unix at gmail.com
Tue Dec 18 06:37:49 CET 2012


From: Liu Yuan <tailai.ly at taobao.com>

We should only allow numberic parameter, otherwise, error return.

Reported-by: Valerio Pachera <sirio81 at gmail.com>
Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 update:
 - remove the extra printf

 collie/node.c  |    9 ++++++++-
 include/util.h |    1 +
 lib/util.c     |   16 ++++++++++++++++
 3 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/collie/node.c b/collie/node.c
index b5324f0..f79927f 100644
--- a/collie/node.c
+++ b/collie/node.c
@@ -185,8 +185,15 @@ static int node_kill(int argc, char **argv)
 	char host[128];
 	int node_id, ret;
 	struct sd_node_req req;
+	const char *p = argv[optind++];
 
-	node_id = strtol(argv[optind++], NULL, 10);
+	if (!is_numeric(p)) {
+		fprintf(stderr, "Invalid node id '%s', "
+			"please specify a numeric value\n", p);
+		exit(EXIT_USAGE);
+	}
+
+	node_id = strtol(p, NULL, 10);
 	if (node_id < 0 || node_id >= sd_nodes_nr) {
 		fprintf(stderr, "Invalid node id '%d'\n", node_id);
 		exit(EXIT_USAGE);
diff --git a/include/util.h b/include/util.h
index 7422dbf..44c2a2a 100644
--- a/include/util.h
+++ b/include/util.h
@@ -82,6 +82,7 @@ extern ssize_t xpread(int fd, void *buf, size_t count, off_t offset);
 extern ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset);
 extern void pstrcpy(char *buf, int buf_size, const char *str);
 extern int rmdir_r(char *dir_path);
+extern int is_numeric(const char *p);
 
 void trim_zero_sectors(void *buf, uint64_t *offset, uint32_t *len);
 void set_trimmed_sectors(void *buf, uint64_t offset, uint32_t len,
diff --git a/lib/util.c b/lib/util.c
index ea00afd..84aad0a 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -18,6 +18,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <assert.h>
+#include <ctype.h>
 
 #include "util.h"
 #include "logger.h"
@@ -329,3 +330,18 @@ void set_trimmed_sectors(void *buf, uint64_t offset, uint32_t len,
 	if (offset + len < requested_len)
 		memset(p + offset + len, 0, requested_len - offset - len);
 }
+
+int is_numeric(const char *s)
+{
+	const char *p = s;
+
+	if (*p) {
+		char c;
+
+		while ((c=*p++))
+			if (!isdigit(c))
+				return 0;
+		return 1;
+	}
+	return 0;
+}
-- 
1.7.9.5




More information about the sheepdog mailing list