All of lore.kernel.org
 help / color / mirror / Atom feed
* [Revised PATCH v2] hv: kvp: Avoid reading past allocated blocks from KVP file
@ 2017-11-01 18:45 Long Li
  2017-11-08 14:21 ` Haiyang Zhang
  0 siblings, 1 reply; 3+ messages in thread
From: Long Li @ 2017-11-01 18:45 UTC (permalink / raw)
  To: K . Y . Srinivasan, Haiyang Zhang, Stephen Hemminger, devel,
	linux-kernel
  Cc: Paul Meyer, Long Li

From: Paul Meyer <Paul.Meyer@microsoft.com>

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.

Changes since v1:
1. Properly wrapped comment texts.
2. Added the 2nd Signed-off-by.

Signed-off-by: Paul Meyer <Paul.Meyer@microsoft.com>
Signed-off-by: Long Li <longli@microsoft.com>
---
 tools/hv/hv_kvp_daemon.c | 66 ++++++++----------------------------------------
 1 file changed, 10 insertions(+), 56 deletions(-)

diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index eaa3bec..2094036 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -193,11 +193,13 @@ static void kvp_update_mem_state(int pool)
 	for (;;) {
 		readp = &record[records_read];
 		records_read += fread(readp, sizeof(struct kvp_record),
-					ENTRIES_PER_BLOCK * num_blocks,
-					filep);
+				ENTRIES_PER_BLOCK * num_blocks - records_read,
+				filep);
 
 		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));
 			exit(EXIT_FAILURE);
 		}
 
@@ -224,15 +226,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 = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
 
@@ -246,61 +244,17 @@ static int kvp_file_init(void)
 
 	for (i = 0; i < KVP_POOL_COUNT; i++) {
 		fname = kvp_file_info[i].fname;
-		records_read = 0;
-		num_blocks = 1;
 		sprintf(fname, "%s/.kvp_pool_%d", KVP_CONFIG_LOC, i);
 		fd = open(fname, O_RDWR | O_CREAT | O_CLOEXEC, 0644 /* rw-r--r-- */);
 
 		if (fd == -1)
 			return 1;
 
-
-		filep = fopen(fname, "re");
-		if (!filep) {
-			close(fd);
-			return 1;
-		}
-
-		record = malloc(alloc_unit * num_blocks);
-		if (record == NULL) {
-			fclose(filep);
-			close(fd);
-			return 1;
-		}
-		for (;;) {
-			readp = &record[records_read];
-			records_read += 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 = realloc(record, alloc_unit *
-						num_blocks);
-				if (record == NULL) {
-					fclose(filep);
-					close(fd);
-					return 1;
-				}
-				continue;
-			}
-			break;
-		}
 		kvp_file_info[i].fd = fd;
-		kvp_file_info[i].num_blocks = num_blocks;
-		kvp_file_info[i].records = record;
-		kvp_file_info[i].num_records = records_read;
-		fclose(filep);
-
+		kvp_file_info[i].num_blocks = 1;
+		kvp_file_info[i].records = malloc(alloc_unit);
+		kvp_file_info[i].num_records = 0;
+		kvp_update_mem_state(i);
 	}
 
 	return 0;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* RE: [Revised PATCH v2] hv: kvp: Avoid reading past allocated blocks from KVP file
  2017-11-01 18:45 [Revised PATCH v2] hv: kvp: Avoid reading past allocated blocks from KVP file Long Li
@ 2017-11-08 14:21 ` Haiyang Zhang
  2017-11-08 18:34   ` KY Srinivasan
  0 siblings, 1 reply; 3+ messages in thread
From: Haiyang Zhang @ 2017-11-08 14:21 UTC (permalink / raw)
  To: Long Li, KY Srinivasan, Stephen Hemminger, devel, linux-kernel
  Cc: Paul Meyer, Long Li



> -----Original Message-----
> From: Long Li [mailto:longli@exchange.microsoft.com]
> Sent: Wednesday, November 1, 2017 2:45 PM
> To: KY Srinivasan <kys@microsoft.com>; Haiyang Zhang
> <haiyangz@microsoft.com>; Stephen Hemminger
> <sthemmin@microsoft.com>; devel@linuxdriverproject.org; linux-
> kernel@vger.kernel.org
> Cc: Paul Meyer <Paul.Meyer@microsoft.com>; Long Li
> <longli@microsoft.com>
> Subject: [Revised PATCH v2] hv: kvp: Avoid reading past allocated blocks
> from KVP file
> 
> [This sender failed our fraud detection checks and may not be who they
> appear to be. Learn about spoofing at http://aka.ms/LearnAboutSpoofing]
> 
> From: Paul Meyer <Paul.Meyer@microsoft.com>
> 
> 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.
> 
> Changes since v1:
> 1. Properly wrapped comment texts.
> 2. Added the 2nd Signed-off-by.
> 
> Signed-off-by: Paul Meyer <Paul.Meyer@microsoft.com>
> Signed-off-by: Long Li <longli@microsoft.com>

Reviewed-by: Haiyang Zhang <haiyangz@microsoft.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* RE: [Revised PATCH v2] hv: kvp: Avoid reading past allocated blocks from KVP file
  2017-11-08 14:21 ` Haiyang Zhang
@ 2017-11-08 18:34   ` KY Srinivasan
  0 siblings, 0 replies; 3+ messages in thread
From: KY Srinivasan @ 2017-11-08 18:34 UTC (permalink / raw)
  To: Haiyang Zhang, Long Li, Stephen Hemminger, devel, linux-kernel
  Cc: Paul Meyer, Long Li



> -----Original Message-----
> From: Haiyang Zhang
> Sent: Wednesday, November 8, 2017 6:22 AM
> To: Long Li <longli@microsoft.com>; KY Srinivasan <kys@microsoft.com>;
> Stephen Hemminger <sthemmin@microsoft.com>;
> devel@linuxdriverproject.org; linux-kernel@vger.kernel.org
> Cc: Paul Meyer <Paul.Meyer@microsoft.com>; Long Li
> <longli@microsoft.com>
> Subject: RE: [Revised PATCH v2] hv: kvp: Avoid reading past allocated blocks
> from KVP file
> 
> 
> 
> > -----Original Message-----
> > From: Long Li [mailto:longli@exchange.microsoft.com]
> > Sent: Wednesday, November 1, 2017 2:45 PM
> > To: KY Srinivasan <kys@microsoft.com>; Haiyang Zhang
> > <haiyangz@microsoft.com>; Stephen Hemminger
> > <sthemmin@microsoft.com>; devel@linuxdriverproject.org; linux-
> > kernel@vger.kernel.org
> > Cc: Paul Meyer <Paul.Meyer@microsoft.com>; Long Li
> > <longli@microsoft.com>
> > Subject: [Revised PATCH v2] hv: kvp: Avoid reading past allocated blocks
> > from KVP file
> >
> > [This sender failed our fraud detection checks and may not be who they
> > appear to be. Learn about spoofing at http://aka.ms/LearnAboutSpoofing]
> >
> > From: Paul Meyer <Paul.Meyer@microsoft.com>
> >
> > 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.
> >
> > Changes since v1:
> > 1. Properly wrapped comment texts.
> > 2. Added the 2nd Signed-off-by.
> >
> > Signed-off-by: Paul Meyer <Paul.Meyer@microsoft.com>
> > Signed-off-by: Long Li <longli@microsoft.com>
> 
> Reviewed-by: Haiyang Zhang <haiyangz@microsoft.com>

I will take this patch.

K. Y

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-11-08 18:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-01 18:45 [Revised PATCH v2] hv: kvp: Avoid reading past allocated blocks from KVP file Long Li
2017-11-08 14:21 ` Haiyang Zhang
2017-11-08 18:34   ` KY Srinivasan

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.