All of lore.kernel.org
 help / color / mirror / Atom feed
From: lixiaokeng <lixiaokeng@huawei.com>
To: Martin Wilck <mwilck@suse.com>,
	Benjamin Marzinski <bmarzins@redhat.com>,
	Christophe Varoqui <christophe.varoqui@opensvc.com>,
	dm-devel mailing list <dm-devel@redhat.com>
Cc: linfeilong <linfeilong@huawei.com>,
	"liuzhiqiang (I)" <liuzhiqiang26@huawei.com>
Subject: [PATCH 11/14] mpathpersist: check whether malloc paramp->trnptid_list, fails in handle_args func
Date: Wed, 2 Sep 2020 15:23:42 +0800	[thread overview]
Message-ID: <eeea4f08-bcb1-65b6-2fe8-b078961a07d3@huawei.com> (raw)
In-Reply-To: <37544d4c-950f-4281-3b66-e4d1884c5167@huawei.com>

In handle_args func, we donot check whether malloc paramp and
each paramp->trnptid_list[j] fails before using them, it may
cause access NULL pointer.

Here, we add alloc_prout_param_descriptor to allocate and init
paramp, and we add free_prout_param_descriptor to free paramp
and each paramp->trnptid_list[j].

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Signed-off-by: lixiaokeng <lixiaokeng@huawei.com>
Signed-off-by: Linfeilong <linfeilong@huawei.com>
---
 mpathpersist/main.c | 55 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 46 insertions(+), 9 deletions(-)

diff --git a/mpathpersist/main.c b/mpathpersist/main.c
index 28bfe410..f20c902c 100644
--- a/mpathpersist/main.c
+++ b/mpathpersist/main.c
@@ -153,6 +153,39 @@ static int do_batch_file(const char *batch_fn)
 	return ret;
 }

+static struct prout_param_descriptor *
+alloc_prout_param_descriptor(int num_transportid)
+{
+	struct prout_param_descriptor *paramp;
+
+	if (num_transportid < 0 || num_transportid > MPATH_MX_TIDS)
+		return NULL;
+
+	paramp= malloc(sizeof(struct prout_param_descriptor) +
+				(sizeof(struct transportid *) * num_transportid));
+
+	if (!paramp)
+		return NULL;
+
+	paramp->num_transportid = num_transportid;
+	memset(paramp, 0, sizeof(struct prout_param_descriptor) +
+			(sizeof(struct transportid *) * num_transportid));
+	return paramp;
+}
+
+static void free_prout_param_descriptor(struct prout_param_descriptor *paramp)
+{
+	int i;
+	if (!paramp)
+		return;
+
+	for (i = 0; i < paramp->num_transportid; i++)
+		free(paramp->trnptid_list[i]);
+
+	free(paramp);
+	paramp = NULL;
+}
+
 static int handle_args(int argc, char * argv[], int nline)
 {
 	int c;
@@ -525,9 +558,12 @@ static int handle_args(int argc, char * argv[], int nline)
 		int j;
 		struct prout_param_descriptor *paramp;

-		paramp= malloc(sizeof(struct prout_param_descriptor) + (sizeof(struct transportid *)*(MPATH_MX_TIDS )));
-
-		memset(paramp, 0, sizeof(struct prout_param_descriptor) + (sizeof(struct transportid *)*(MPATH_MX_TIDS)));
+		paramp = alloc_prout_param_descriptor(num_transport);
+		if (!paramp) {
+			fprintf(stderr, "malloc paramp failed\n");
+			ret = MPATH_PR_OTHER;
+			goto out_fd;
+		}

 		for (j = 7; j >= 0; --j) {
 			paramp->key[j] = (param_rk & 0xff);
@@ -551,6 +587,12 @@ static int handle_args(int argc, char * argv[], int nline)
 			for (j = 0 ; j < num_transport; j++)
 			{
 				paramp->trnptid_list[j] = (struct transportid *)malloc(sizeof(struct transportid));
+				if (!paramp->trnptid_list[j]) {
+					fprintf(stderr, "malloc paramp->trnptid_list[%d] failed.\n", j);
+					ret = MPATH_PR_OTHER;
+					free_prout_param_descriptor(paramp);
+					goto out_fd;
+				}
 				memcpy(paramp->trnptid_list[j], &transportids[j],sizeof(struct transportid));
 			}
 		}
@@ -558,12 +600,7 @@ static int handle_args(int argc, char * argv[], int nline)
 		/* PROUT commands other than 'register and move' */
 		ret = __mpath_persistent_reserve_out (fd, prout_sa, 0, prout_type,
 				paramp, noisy);
-		for (j = 0 ; j < num_transport; j++)
-		{
-			tmp = paramp->trnptid_list[j];
-			free(tmp);
-		}
-		free(paramp);
+		free_prout_param_descriptor(paramp);
 	}

 	if (ret != MPATH_PR_SUCCESS)
-- 

  parent reply	other threads:[~2020-09-02  7:23 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-02  6:40 [PATCH 00/14] multipath-tools series: some cleanups and fixes checked by codedex tool lixiaokeng
2020-09-02  7:15 ` [PATCH 01/14] multipathd: return if dm_get_major_minor failed in, cli_add_map lixiaokeng
2020-09-03 17:26   ` Martin Wilck
2020-09-04  2:48     ` lixiaokeng
2020-09-04 13:13       ` Martin Wilck
2020-09-02  7:16 ` [PATCH 02/14] libmultipath: check malloc return value in, print_foreign_topology lixiaokeng
2020-09-03 17:29   ` Martin Wilck
2020-09-02  7:17 ` [PATCH 03/14] libmultipath: use map instead of dm_task_get_name lixiaokeng
2020-09-03 17:35   ` Martin Wilck
2020-09-02  7:17 ` [PATCH 04/14] multipathd: check MALLOC return value in, mpath_pr_event_handler_fn lixiaokeng
2020-09-03 18:57   ` Martin Wilck
2020-09-02  7:18 ` [PATCH 05/14] multipathd: use MALLOC and check return value in, cli_getprkey func lixiaokeng
2020-09-03 19:13   ` Martin Wilck
2020-09-04 18:25     ` Benjamin Marzinski
2020-09-04 21:24       ` Martin Wilck
2020-09-02  7:19 ` [PATCH 06/14] kpartx: check return value of malloc in main func lixiaokeng
2020-09-03 19:23   ` Martin Wilck
2020-09-02  7:19 ` [PATCH 07/14] libmultipath: check return value of dm_mapname in, sysfs_check_holders lixiaokeng
2020-09-04 20:28   ` Benjamin Marzinski
2020-09-02  7:20 ` [PATCH 08/14] libmultipath: donot free *dst if REALLOC fails in, merge_words lixiaokeng
2020-09-04 21:11   ` Benjamin Marzinski
2020-09-07 11:58     ` lixiaokeng
2020-09-02  7:21 ` [PATCH 09/14] libmultipath: check whether mp->features is NULl in, assemble_map lixiaokeng
2020-09-04 21:30   ` Benjamin Marzinski
2020-09-07 12:21     ` lixiaokeng
2020-09-08 15:45       ` Benjamin Marzinski
2020-09-08 16:35         ` Martin Wilck
2020-09-08 16:44           ` Benjamin Marzinski
2020-09-09  3:18           ` lixiaokeng
2020-09-09 10:11             ` Martin Wilck
2020-09-02  7:22 ` [PATCH 10/14] util/tests: use assert_non_null to ensure malloc, returns non-null pointer lixiaokeng
2020-09-04 21:31   ` Benjamin Marzinski
2020-09-02  7:23 ` lixiaokeng [this message]
2020-09-04 23:52   ` [PATCH 11/14] mpathpersist: check whether malloc paramp->trnptid_list, fails in handle_args func Benjamin Marzinski
2020-09-07 12:26     ` lixiaokeng
2020-09-02  7:24 ` [PATCH 12/14] libmultipathpersist: use update_multipath_table/status, in get_mpvec lixiaokeng
2020-09-05  0:05   ` Benjamin Marzinski
2020-09-07 13:26     ` lixiaokeng
2020-09-07 14:33     ` Martin Wilck
2020-09-02  7:25 ` [PATCH 13/14] multipath: use update_multipath_table/status in, check_useable_paths lixiaokeng
2020-09-05  0:10   ` Benjamin Marzinski
2020-09-02  7:26 ` [PATCH 14/14] multipathpersist: delete unused variable in handle_args lixiaokeng
2020-09-05  0:14   ` Benjamin Marzinski
2020-09-03 20:08 ` [PATCH 00/14] multipath-tools series: some cleanups and fixes checked by codedex tool Martin Wilck
2020-09-04  2:24   ` lixiaokeng
2020-09-04 20:00   ` Benjamin Marzinski
2020-09-04 21:28     ` Martin Wilck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eeea4f08-bcb1-65b6-2fe8-b078961a07d3@huawei.com \
    --to=lixiaokeng@huawei.com \
    --cc=bmarzins@redhat.com \
    --cc=christophe.varoqui@opensvc.com \
    --cc=dm-devel@redhat.com \
    --cc=linfeilong@huawei.com \
    --cc=liuzhiqiang26@huawei.com \
    --cc=mwilck@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.