vfio: Clear the caps->buf to NULL after free
Subsystems (2)
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
@@ -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)
Quoted 2 lines · On Tue, 28 Jun 2022 23:24:29 +0800 Schspa Shi <[email protected]> wrote:
API
vfio_info_cap_addwill freecaps->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
Signed-off-by Schspa Shi <[email protected]>--- drivers/vfio/vfio.c | 1 + 1 file changed, 1 insertion(+)drivers/vfio/vfio.c +1
-0@@ -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 ); }
Cornelia Huck
Reviewed-byQuoted 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_addwill freecaps->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,
AlexQuoted 17 lines
Signed-off-by Schspa Shi <[email protected]>--- drivers/vfio/vfio.c | 1 + 1 file changed, 1 insertion(+)drivers/vfio/vfio.c +1
-0@@ -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 ); }
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_addwill freecaps->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,
Reviewed-by Cornelia Huck <[email protected]>
I have send a V3 patch for this comment change, please review it.
Quoted 22 lines
Quoted 22 lines
Thanks,
AlexQuoted 18 lines
Signed-off-by Schspa Shi <[email protected]>--- drivers/vfio/vfio.c | 1 + 1 file changed, 1 insertion(+)drivers/vfio/vfio.c +1
-0@@ -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