Skip to contentSkip to conversation

hv: kvp: Avoid reading past allocated blocks from KVP file

PATCH In review Long Li 3 participants 5 messages Oct 31, 2017 Hyper-V/Azure core and drivers

lore
Share & tools
lore

Long Li

author
#0 lore raw

PATCH 1/1 hv: kvp: Avoid reading past allocated blocks from KVP file

From: Paul Meyer <[email protected]>

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.

---
 tools/hv/hv_kvp_daemon.c | 66 ++++++++----------------------------------------
 1 file changed, 10 insertions(+), 56 deletions(-)
tools/hv/hv_kvp_daemon.c +10 -56
index eaa3bec..2094036 100644
@@ -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;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 7 lines · Long Li

From: Paul Meyer <[email protected]>

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).

Please wrap changelogs at 72 columns like your editor asked you to...

Quoted 5 lines

Fix this by reading the correct number of KVP records from file each time.

---

Why is your name not also on the signed-off-by chain if you are forwarding on a patch from someone else?

Is this patch also needed on stable kernels?

thanks,

greg k-h

#2 lore raw
Quoted 12 lines

From: Greg KH [mailto:[email protected]]
Sent: Tuesday, October 31, 2017 1:43 AM
To: Long Li <[email protected]>
Cc: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; Paul Meyer <[email protected]>
Subject: Re: [PATCH] hv: kvp: Avoid reading past allocated blocks from KVP file

Quoted 2 lines · Long Li

From: Paul Meyer <[email protected]>

Quoted 8 lines
Quoted 5 lines

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).

Please wrap changelogs at 72 columns like your editor asked you to...

I will fix it.

Quoted 11 lines
Quoted 6 lines

Fix this by reading the correct number of KVP records from file each time.

---

Why is your name not also on the signed-off-by chain if you are forwarding on a patch from someone else?

Is this patch also needed on stable kernels?

I'm sending on behalf of Paul Meyer. I will add a "Reviewed-by:" tag.

Yes it should also go stable. Will send v2 to include that.

Quoted 4 lines

thanks,

greg k-h

#3 lore raw
Quoted 30 lines · Long Li
Quoted 12 lines

From: Greg KH [mailto:[email protected]]
Sent: Tuesday, October 31, 2017 1:43 AM
To: Long Li <[email protected]>
Cc: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; Paul Meyer <[email protected]>
Subject: Re: [PATCH] hv: kvp: Avoid reading past allocated blocks from KVP file

Quoted 2 lines · Long Li

From: Paul Meyer <[email protected]>

Quoted 8 lines
Quoted 5 lines

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).

Please wrap changelogs at 72 columns like your editor asked you to...

I will fix it.

Quoted 7 lines
Quoted 6 lines

Fix this by reading the correct number of KVP records from file each time.

---

Quoted 6 lines
Quoted 4 lines

Why is your name not also on the signed-off-by chain if you are forwarding on a patch from someone else?

Is this patch also needed on stable kernels?

I'm sending on behalf of Paul Meyer. I will add a "Reviewed-by:" tag.

Sending on behalf means you should add your signed-off-by, as it is going through you.

thanks,

greg k-h

#4 lore raw
Quoted 36 lines · Long Li
Quoted 35 lines
Quoted 19 lines

From: Greg KH [mailto:[email protected]]
Sent: Tuesday, October 31, 2017 1:43 AM
To: Long Li <[email protected]>
Cc: KY Srinivasan <[email protected]>; Haiyang Zhang <[email protected]>; Stephen Hemminger <[email protected]>; [email protected]; [email protected]; Paul Meyer <[email protected]>
Subject: Re: [PATCH] hv: kvp: Avoid reading past allocated blocks from KVP file

Quoted 8 lines · Long Li

From: Paul Meyer <[email protected]>

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

Quoted 3 lines

whereas the in-memory database was empty before).

Please wrap changelogs at 72 columns like your editor asked you to...

I will fix it.

Quoted 10 lines
Quoted 6 lines

Fix this by reading the correct number of KVP records from file each time.

---

Why is your name not also on the signed-off-by chain if you are forwarding on a patch from someone else?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Is this patch also needed on stable kernels?

I'm sending on behalf of Paul Meyer. I will add a "Reviewed-by:" tag.

Sending on behalf means you should add your signed-off-by, as it is going through you.

Thanks. I will re-send the patch.

Quoted 4 lines

thanks,

greg k-h