Skip to contentSkip to conversation

vfio: Clear the caps->buf to NULL after free

PATCH Superseded Schspa Shi 3 participants 4 messages Jun 28, 2022 – Jun 29, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Schspa Shi

author
#0 lore raw

PATCH v2 1/1 vfio: Clear the caps->buf to NULL after free

API vfio_info_cap_add will free caps->buf, clear it to NULL after free.

---
 drivers/vfio/vfio.c | 1 +
 1 file changed, 1 insertion(+)
drivers/vfio/vfio.c +1 -0
index 61e71c1154be..a0fb93866f61 100644
@@ -1812,6 +1812,7 @@ struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
 	buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL);
 	if (!buf) {
 		kfree(caps->buf);
+		caps->buf = NULL;
 		caps->size = 0;
 		return ERR_PTR(-ENOMEM);
 	}
Signature · 1 line
2.24.3 (Apple Git-128)
#1 lore raw
Quoted 2 lines · On Tue, 28 Jun 2022 23:24:29 +0800 Schspa Shi <[email protected]> wrote:

API vfio_info_cap_add will free caps->buf, clear it to NULL after free.

Should this be something like:

On buffer resize failure, vfio_info_cap_add() will free the buffer, report zero for the size, and return -ENOMEM. As additional hardening, also clear the buffer pointer to prevent any chance of a double free.

Thanks,
Alex

Quoted 17 lines
---
 drivers/vfio/vfio.c | 1 +
 1 file changed, 1 insertion(+)
drivers/vfio/vfio.c +1 -0
index 61e71c1154be..a0fb93866f61 100644
@@ -1812,6 +1812,7 @@ struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
 	buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL);
 	if (!buf) {
 		kfree(caps->buf);
+		caps->buf = NULL;
 		caps->size = 0;
 		return ERR_PTR(-ENOMEM);
 	}
Reviewed-by
#2 lore raw
Quoted 6 lines · Alex Williamson
Quoted 2 lines · On Tue, 28 Jun 2022 23:24:29 +0800 Schspa Shi <[email protected]> wrote:

API vfio_info_cap_add will free caps->buf, clear it to NULL after free.

Quoted 6 lines

Should this be something like:

On buffer resize failure, vfio_info_cap_add() will free the buffer, report zero for the size, and return -ENOMEM. As additional hardening, also clear the buffer pointer to prevent any chance of a double free.

I like that better. With that,

Quoted 21 lines

Thanks,
Alex

Quoted 17 lines
---
 drivers/vfio/vfio.c | 1 +
 1 file changed, 1 insertion(+)
drivers/vfio/vfio.c +1 -0
index 61e71c1154be..a0fb93866f61 100644
@@ -1812,6 +1812,7 @@ struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
 	buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL);
 	if (!buf) {
 		kfree(caps->buf);
+		caps->buf = NULL;
 		caps->size = 0;
 		return ERR_PTR(-ENOMEM);
 	}
author
#3 lore raw
Quoted 18 lines · Cornelia Huck
Quoted 8 lines · Alex Williamson
Quoted 2 lines · On Tue, 28 Jun 2022 23:24:29 +0800 Schspa Shi <[email protected]> wrote:

API vfio_info_cap_add will free caps->buf, clear it to NULL after free.

Should this be something like:

Quoted 7 lines

On buffer resize failure, vfio_info_cap_add() will free the buffer,
report zero for the size, and return -ENOMEM. As additional
hardening, also clear the buffer pointer to prevent any chance of a
double free.

Quoted 4 lines

I like that better. With that,

I have send a V3 patch for this comment change, please review it.

Quoted 22 lines
Quoted 22 lines

Thanks,
Alex

Quoted 18 lines
---
 drivers/vfio/vfio.c | 1 +
 1 file changed, 1 insertion(+)
drivers/vfio/vfio.c +1 -0
index 61e71c1154be..a0fb93866f61 100644
@@ -1812,6 +1812,7 @@ struct vfio_info_cap_header 
 *vfio_info_cap_add(struct vfio_info_cap *caps,
 	buf = krealloc(caps->buf, caps->size + size, GFP_KERNEL);
 	if (!buf) {
 		kfree(caps->buf);
+		caps->buf = NULL;
 		caps->size = 0;
 		return ERR_PTR(-ENOMEM);

}

Signature · 2 lines
BRs
Schspa Shi