Skip to contentSkip to conversation

blkmapd: fix coredump in bl_add_disk

PATCH In review lixiaokeng 2 participants 4 messages Dec 1, 2021 – Oct 24, 2022

Subsystems (2)
lore
Share & tools
lore

lixiaokeng

author
#0 lore raw

PATCH 1/1 blkmapd: fix coredump in bl_add_disk

The serial->data is not malloced separately (just part of the serial), so it can't be freed. The bl_serial has its own free function. Use it.

---
 utils/blkmapd/device-discovery.c | 15 +++------------
 utils/blkmapd/device-discovery.h |  2 ++
 utils/blkmapd/device-inq.c       |  4 ++--
 3 files changed, 7 insertions(+), 14 deletions(-)
utils/blkmapd/device-discovery.c +3 -12
index 2736ac89..cea33496 100644
@@ -187,10 +187,7 @@ static void bl_add_disk(char *filepath)
 	}
 
 	if (disk && diskpath) {
-		if (serial) {
-			free(serial->data);
-			free(serial);
-		}
+		bl_free_scsi_string(serial);
 		return;
 	}
 
@@ -228,10 +225,7 @@ static void bl_add_disk(char *filepath)
 			disk->size = size;
 			disk->valid_path = path;
 		}
-		if (serial) {
-			free(serial->data);
-			free(serial);
-		}
+		bl_free_scsi_string(serial);
 	}
 	return;
 
@@ -241,10 +235,7 @@ static void bl_add_disk(char *filepath)
 			free(path->full_path);
 		free(path);
 	}
-	if (serial) {
-		free(serial->data);
-		free(serial);
-	}
+	bl_free_scsi_string(serial);
 	return;
 }
 
utils/blkmapd/device-discovery.h +2 -0
index a86eed99..462aa943 100644
@@ -151,6 +151,8 @@ uint64_t process_deviceinfo(const char *dev_addr_buf,
 
 extern ssize_t atomicio(ssize_t(*f) (int, void *, size_t),
 			int fd, void *_s, size_t n);
+extern struct bl_serial *bl_create_scsi_string(int len, const char *bytes);
+extern void bl_free_scsi_string(struct bl_serial *str);
 extern struct bl_serial *bldev_read_serial(int fd, const char *filename);
 extern enum bl_path_state_e bldev_read_ap_state(int fd);
 extern int bl_discover_devices(void);
utils/blkmapd/device-inq.c +2 -2
index c7952c3e..9e5749ef 100644
@@ -53,7 +53,7 @@
 #define DEF_ALLOC_LEN	255
 #define MX_ALLOC_LEN	(0xc000 + 0x80)
 
-static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
+struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
 {
 	struct bl_serial *s;
 
@@ -66,7 +66,7 @@ static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
 	return s;
 }
 
-static void bl_free_scsi_string(struct bl_serial *str)
+void bl_free_scsi_string(struct bl_serial *str)
 {
 	if (str)
 		free(str);
author
#1 lore raw

ping

Quoted 88 lines · lixiaokeng

The serial->data is not malloced separately (just part of the serial), so it can't be freed. The bl_serial has its own free function. Use it.

---
 utils/blkmapd/device-discovery.c | 15 +++------------
 utils/blkmapd/device-discovery.h |  2 ++
 utils/blkmapd/device-inq.c       |  4 ++--
 3 files changed, 7 insertions(+), 14 deletions(-)
utils/blkmapd/device-discovery.c +3 -12
index 2736ac89..cea33496 100644
@@ -187,10 +187,7 @@ static void bl_add_disk(char *filepath)
 	}
 
 	if (disk && diskpath) {
-		if (serial) {
-			free(serial->data);
-			free(serial);
-		}
+		bl_free_scsi_string(serial);
 		return;
 	}
 
@@ -228,10 +225,7 @@ static void bl_add_disk(char *filepath)
 			disk->size = size;
 			disk->valid_path = path;
 		}
-		if (serial) {
-			free(serial->data);
-			free(serial);
-		}
+		bl_free_scsi_string(serial);
 	}
 	return;
 
@@ -241,10 +235,7 @@ static void bl_add_disk(char *filepath)
 			free(path->full_path);
 		free(path);
 	}
-	if (serial) {
-		free(serial->data);
-		free(serial);
-	}
+	bl_free_scsi_string(serial);
 	return;
 }
 
utils/blkmapd/device-discovery.h +2 -0
index a86eed99..462aa943 100644
@@ -151,6 +151,8 @@ uint64_t process_deviceinfo(const char *dev_addr_buf,
 
 extern ssize_t atomicio(ssize_t(*f) (int, void *, size_t),
 			int fd, void *_s, size_t n);
+extern struct bl_serial *bl_create_scsi_string(int len, const char *bytes);
+extern void bl_free_scsi_string(struct bl_serial *str);
 extern struct bl_serial *bldev_read_serial(int fd, const char *filename);
 extern enum bl_path_state_e bldev_read_ap_state(int fd);
 extern int bl_discover_devices(void);
utils/blkmapd/device-inq.c +2 -2
index c7952c3e..9e5749ef 100644
@@ -53,7 +53,7 @@
 #define DEF_ALLOC_LEN	255
 #define MX_ALLOC_LEN	(0xc000 + 0x80)
 
-static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
+struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
 {
 	struct bl_serial *s;
 
@@ -66,7 +66,7 @@ static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
 	return s;
 }
 
-static void bl_free_scsi_string(struct bl_serial *str)
+void bl_free_scsi_string(struct bl_serial *str)
 {
 	if (str)
 		free(str);
author
#2 lore raw
In reply to lixiaokeng

ping

Quoted 88 lines · lixiaokeng

The serial->data is not malloced separately (just part of the serial), so it can't be freed. The bl_serial has its own free function. Use it.

---
 utils/blkmapd/device-discovery.c | 15 +++------------
 utils/blkmapd/device-discovery.h |  2 ++
 utils/blkmapd/device-inq.c       |  4 ++--
 3 files changed, 7 insertions(+), 14 deletions(-)
utils/blkmapd/device-discovery.c +3 -12
index 2736ac89..cea33496 100644
@@ -187,10 +187,7 @@ static void bl_add_disk(char *filepath)
 	}
 
 	if (disk && diskpath) {
-		if (serial) {
-			free(serial->data);
-			free(serial);
-		}
+		bl_free_scsi_string(serial);
 		return;
 	}
 
@@ -228,10 +225,7 @@ static void bl_add_disk(char *filepath)
 			disk->size = size;
 			disk->valid_path = path;
 		}
-		if (serial) {
-			free(serial->data);
-			free(serial);
-		}
+		bl_free_scsi_string(serial);
 	}
 	return;
 
@@ -241,10 +235,7 @@ static void bl_add_disk(char *filepath)
 			free(path->full_path);
 		free(path);
 	}
-	if (serial) {
-		free(serial->data);
-		free(serial);
-	}
+	bl_free_scsi_string(serial);
 	return;
 }
 
utils/blkmapd/device-discovery.h +2 -0
index a86eed99..462aa943 100644
@@ -151,6 +151,8 @@ uint64_t process_deviceinfo(const char *dev_addr_buf,
 
 extern ssize_t atomicio(ssize_t(*f) (int, void *, size_t),
 			int fd, void *_s, size_t n);
+extern struct bl_serial *bl_create_scsi_string(int len, const char *bytes);
+extern void bl_free_scsi_string(struct bl_serial *str);
 extern struct bl_serial *bldev_read_serial(int fd, const char *filename);
 extern enum bl_path_state_e bldev_read_ap_state(int fd);
 extern int bl_discover_devices(void);
utils/blkmapd/device-inq.c +2 -2
index c7952c3e..9e5749ef 100644
@@ -53,7 +53,7 @@
 #define DEF_ALLOC_LEN	255
 #define MX_ALLOC_LEN	(0xc000 + 0x80)
 
-static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
+struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
 {
 	struct bl_serial *s;
 
@@ -66,7 +66,7 @@ static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
 	return s;
 }
 
-static void bl_free_scsi_string(struct bl_serial *str)
+void bl_free_scsi_string(struct bl_serial *str)
 {
 	if (str)
 		free(str);
#3 lore raw
In reply to lixiaokeng
Quoted 6 lines · lixiaokeng

The serial->data is not malloced separately (just part of the serial), so it can't be freed. The bl_serial has its own free function. Use it.

Committed...

steved.

Quoted 81 lines
---
  utils/blkmapd/device-discovery.c | 15 +++------------
  utils/blkmapd/device-discovery.h |  2 ++
  utils/blkmapd/device-inq.c       |  4 ++--
  3 files changed, 7 insertions(+), 14 deletions(-)
utils/blkmapd/device-discovery.c +3 -12
index 2736ac89..cea33496 100644
@@ -187,10 +187,7 @@ static void bl_add_disk(char *filepath)
  	}
 
  	if (disk && diskpath) {
-		if (serial) {
-			free(serial->data);
-			free(serial);
-		}
+		bl_free_scsi_string(serial);
  		return;
  	}
 
@@ -228,10 +225,7 @@ static void bl_add_disk(char *filepath)
  			disk->size = size;
  			disk->valid_path = path;
  		}
-		if (serial) {
-			free(serial->data);
-			free(serial);
-		}
+		bl_free_scsi_string(serial);
  	}
  	return;
 
@@ -241,10 +235,7 @@ static void bl_add_disk(char *filepath)
  			free(path->full_path);
  		free(path);
  	}
-	if (serial) {
-		free(serial->data);
-		free(serial);
-	}
+	bl_free_scsi_string(serial);
  	return;
  }
 
utils/blkmapd/device-discovery.h +2 -0
index a86eed99..462aa943 100644
@@ -151,6 +151,8 @@ uint64_t process_deviceinfo(const char *dev_addr_buf,
 
  extern ssize_t atomicio(ssize_t(*f) (int, void *, size_t),
  			int fd, void *_s, size_t n);
+extern struct bl_serial *bl_create_scsi_string(int len, const char *bytes);
+extern void bl_free_scsi_string(struct bl_serial *str);
  extern struct bl_serial *bldev_read_serial(int fd, const char *filename);
  extern enum bl_path_state_e bldev_read_ap_state(int fd);
  extern int bl_discover_devices(void);
utils/blkmapd/device-inq.c +2 -2
index c7952c3e..9e5749ef 100644
@@ -53,7 +53,7 @@
  #define DEF_ALLOC_LEN	255
  #define MX_ALLOC_LEN	(0xc000 + 0x80)
 
-static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
+struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
  {
  	struct bl_serial *s;
 
@@ -66,7 +66,7 @@ static struct bl_serial *bl_create_scsi_string(int len, const char *bytes)
  	return s;
  }
 
-static void bl_free_scsi_string(struct bl_serial *str)
+void bl_free_scsi_string(struct bl_serial *str)
  {
  	if (str)
  		free(str);