Return-Path: Received: from mail-by2nam03on0120.outbound.protection.outlook.com ([104.47.42.120]:64240 "EHLO NAM03-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753476AbeARVDF (ORCPT ); Thu, 18 Jan 2018 16:03:05 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Paul Meyer , Long Li , "KY Srinivasan" , Greg Kroah-Hartman , Sasha Levin Subject: [added to the 4.1 stable tree] hv: kvp: Avoid reading past allocated blocks from KVP file Date: Thu, 18 Jan 2018 21:00:44 +0000 Message-ID: <20180118205908.3220-150-alexander.levin@microsoft.com> References: <20180118205908.3220-1-alexander.levin@microsoft.com> In-Reply-To: <20180118205908.3220-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Paul Meyer This patch has been added to the stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit 297d6b6e56c2977fc504c61bbeeaa21296923f89 ] While reading in more than one block (50) of KVP records, the allocation goes per block, but the reads used the total number of allocated records (without resetting the pointer/stream). This causes the records buffer to overrun when the refresh reads more than one block over the previous capacity (e.g. reading more than 100 KVP records whereas the in-memory database was empty before). Fix this by reading the correct number of KVP records from file each time. Signed-off-by: Paul Meyer Signed-off-by: Long Li Cc: stable@vger.kernel.org Signed-off-by: K. Y. Srinivasan Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- tools/hv/hv_kvp_daemon.c | 70 ++++++++++----------------------------------= ---- 1 file changed, 14 insertions(+), 56 deletions(-) diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c index 408bb076a234..db4a1eb5af7d 100644 --- a/tools/hv/hv_kvp_daemon.c +++ b/tools/hv/hv_kvp_daemon.c @@ -195,11 +195,14 @@ static void kvp_update_mem_state(int pool) for (;;) { readp =3D &record[records_read]; records_read +=3D fread(readp, sizeof(struct kvp_record), - ENTRIES_PER_BLOCK * num_blocks, - filep); + ENTRIES_PER_BLOCK * num_blocks - records_read, + filep); =20 if (ferror(filep)) { - syslog(LOG_ERR, "Failed to read file, pool: %d", pool); + syslog(LOG_ERR, + "Failed to read file, pool: %d; error: %d %s", + pool, errno, strerror(errno)); + kvp_release_lock(pool); exit(EXIT_FAILURE); } =20 @@ -212,6 +215,7 @@ static void kvp_update_mem_state(int pool) =20 if (record =3D=3D NULL) { syslog(LOG_ERR, "malloc failed"); + kvp_release_lock(pool); exit(EXIT_FAILURE); } continue; @@ -226,15 +230,11 @@ static void kvp_update_mem_state(int pool) fclose(filep); kvp_release_lock(pool); } + static int kvp_file_init(void) { int fd; - FILE *filep; - size_t records_read; char *fname; - struct kvp_record *record; - struct kvp_record *readp; - int num_blocks; int i; int alloc_unit =3D sizeof(struct kvp_record) * ENTRIES_PER_BLOCK; =20 @@ -248,61 +248,19 @@ static int kvp_file_init(void) =20 for (i =3D 0; i < KVP_POOL_COUNT; i++) { fname =3D kvp_file_info[i].fname; - records_read =3D 0; - num_blocks =3D 1; sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i); fd =3D open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */); =20 if (fd =3D=3D -1) return 1; =20 - - filep =3D fopen(fname, "re"); - if (!filep) { - close(fd); - return 1; - } - - record =3D malloc(alloc_unit * num_blocks); - if (record =3D=3D NULL) { - fclose(filep); - close(fd); - return 1; - } - for (;;) { - readp =3D &record[records_read]; - records_read +=3D fread(readp, sizeof(struct kvp_record), - ENTRIES_PER_BLOCK, - filep); - - if (ferror(filep)) { - syslog(LOG_ERR, "Failed to read file, pool: %d", - i); - exit(EXIT_FAILURE); - } - - if (!feof(filep)) { - /* - * We have more data to read. - */ - num_blocks++; - record =3D realloc(record, alloc_unit * - num_blocks); - if (record =3D=3D NULL) { - fclose(filep); - close(fd); - return 1; - } - continue; - } - break; - } kvp_file_info[i].fd =3D fd; - kvp_file_info[i].num_blocks =3D num_blocks; - kvp_file_info[i].records =3D record; - kvp_file_info[i].num_records =3D records_read; - fclose(filep); - + kvp_file_info[i].num_blocks =3D 1; + kvp_file_info[i].records =3D malloc(alloc_unit); + if (kvp_file_info[i].records =3D=3D NULL) + return 1; + kvp_file_info[i].num_records =3D 0; + kvp_update_mem_state(i); } =20 return 0; --=20 2.11.0