fix memory leaks reported by GCC -fanalyzer
cover fix memory leaks reported by GCC -fanalyzer
From: Edwin Török <[email protected]>
Using GCC 12.2.1 with -fanalyzer it has shown some memory leaks:
This is how I enabled -fanalyzer (adding it to CFLAGS for toplevel configure didn't seem to work):
``` CFLAGS += $(call cc-option,$(CC),-fanalyzer) ```
Note that there are more errors shown than fixed here, but they seem to be false positives (which is why this flag cannot, yet, be enabled by default).
Edwin Török (2):
xc_core_arch_map_p2m_tree_rw: fix memory leak
backup_ptes: fix leak on realloc failure
tools/libs/guest/xg_core_x86.c | 2 ++ tools/libs/guest/xg_offline_page.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.1
PATCH 1/2 xc_core_arch_map_p2m_tree_rw: fix memory leak
From: Edwin Török <[email protected]>
Prior to bd7a29c3d0 'out' would've always been executed and memory freed, but that commit changed it such that it returns early and leaks.
Found using gcc 12.2.1 -fanalyzer:
```
xg_core_x86.c: In function ‘xc_core_arch_map_p2m_tree_rw’:
xg_core_x86.c:300:5: error: leak of ‘p2m_frame_list_list’ [CWE-401] [-Werror=analyzer-malloc-leak]
300 | return p2m_frame_list;
| ^~~~~~
‘xc_core_arch_map_p2m_writable’: events 1-2
|
| 378 | xc_core_arch_map_p2m_writable(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info,
| | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | (1) entry to ‘xc_core_arch_map_p2m_writable’
|......
| 381 | return xc_core_arch_map_p2m_rw(xch, dinfo, info, live_shinfo, live_p2m, 1);
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | (2) calling ‘xc_core_arch_map_p2m_rw’ from ‘xc_core_arch_map_p2m_writable’
|
+--> ‘xc_core_arch_map_p2m_rw’: events 3-10
|
| 319 | xc_core_arch_map_p2m_rw(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info,
| | ^~~~~~~~~~~~~~~~~~~~~~~
| | |
| | (3) entry to ‘xc_core_arch_map_p2m_rw’
|......
| 328 | if ( xc_domain_nr_gpfns(xch, info->domid, &dinfo->p2m_size) < 0 )
| | ~
| | |
| | (4) following ‘false’ branch...
|......
| 334 | if ( dinfo->p2m_size < info->nr_pages )
| | ~~ ~
| | | |
| | | (6) following ‘false’ branch...
| | (5) ...to here
|......
| 340 | p2m_cr3 = GET_FIELD(live_shinfo, arch.p2m_cr3, dinfo->guest_width);
| | ~~~~~~~
| | |
| | (7) ...to here
| 341 |
| 342 | p2m_frame_list = p2m_cr3 ? xc_core_arch_map_p2m_list_rw(xch, dinfo, dom, live_shinfo, p2m_cr3)
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| 343 | : xc_core_arch_map_p2m_tree_rw(xch, dinfo, dom, live_shinfo);
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | | |
| | | (9) ...to here
| | | (10) calling ‘xc_core_arch_map_p2m_tree_rw’ from ‘xc_core_arch_map_p2m_rw’
| | (8) following ‘false’ branch...
|
+--> ‘xc_core_arch_map_p2m_tree_rw’: events 11-24
|
| 228 | xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinfo,
| | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | (11) entry to ‘xc_core_arch_map_p2m_tree_rw’
|......
| 245 | if ( !live_p2m_frame_list_list )
| | ~
| | |
| | (12) following ‘false’ branch (when ‘live_p2m_frame_list_list’ is non-NULL)...
|......
| 252 | if ( !(p2m_frame_list_list = malloc(PAGE_SIZE)) )
| | ~~ ~ ~~~~~~~~~~~~~~~~~
| | | | |
| | | | (14) allocated here
| | | (15) assuming ‘p2m_frame_list_list’ is non-NULL
| | | (16) following ‘false’ branch (when ‘p2m_frame_list_list’ is non-NULL)...
| | (13) ...to here
|......
| 257 | memcpy(p2m_frame_list_list, live_p2m_frame_list_list, PAGE_SIZE);
| | ~~~~~~
| | |
| | (17) ...to here
|......
| 266 | else if ( dinfo->guest_width < sizeof(unsigned long) )
| | ~
| | |
| | (18) following ‘false’ branch...
|......
| 270 | live_p2m_frame_list =
| | ~~~~~~~~~~~~~~~~~~~
| | |
| | (19) ...to here
|......
| 275 | if ( !live_p2m_frame_list )
| | ~
| | |
| | (20) following ‘false’ branch (when ‘live_p2m_frame_list’ is non-NULL)...
|......
| 282 | if ( !(p2m_frame_list = malloc(P2M_TOOLS_FL_SIZE)) )
| | ~~ ~
| | | |
| | | (22) following ‘false’ branch (when ‘p2m_frame_list’ is non-NULL)...
| | (21) ...to here
|......
| 287 | memset(p2m_frame_list, 0, P2M_TOOLS_FL_SIZE);
| | ~~~~~~
| | |
| | (23) ...to here
|......
| 300 | return p2m_frame_list;
| | ~~~~~~
| | |
| | (24) ‘p2m_frame_list_list’ leaks here; was allocated at (14)
|
```--- tools/libs/guest/xg_core_x86.c | 2 ++ 1 file changed, 2 insertions(+)
tools/libs/guest/xg_core_x86.c +2 -0
@@ -297,6 +297,8 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf dinfo->p2m_frames =P2M_FL_ENTRIES ; +free (p2m_frame_list_list); +return p2m_frame_list; out:
Signature · 1 line
2.39.1
Re: [PATCH v1 1/2] xc_core_arch_map_p2m_tree_rw: fix memory leak
Quoted 124 lines · Edwin Török
From: Edwin Török <[email protected]>
Prior to
bd7a29c3d0'out' would've always been executed and memory freed, but that commit changed it such that it returns early and leaks.Found using gcc 12.2.1
-fanalyzer:``` xg_core_x86.c: In function ‘xc_core_arch_map_p2m_tree_rw’: xg_core_x86.c:300:5: error: leak of ‘p2m_frame_list_list’ [CWE-401] [-Werror=analyzer-malloc-leak] 300 | return p2m_frame_list; | ^~~~~~ ‘xc_core_arch_map_p2m_writable’: events 1-2 | | 378 | xc_core_arch_map_p2m_writable(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info, | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (1) entry to ‘xc_core_arch_map_p2m_writable’ |...... | 381 | return xc_core_arch_map_p2m_rw(xch, dinfo, info, live_shinfo, live_p2m, 1); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (2) calling ‘xc_core_arch_map_p2m_rw’ from ‘xc_core_arch_map_p2m_writable’ | +--> ‘xc_core_arch_map_p2m_rw’: events 3-10 | | 319 | xc_core_arch_map_p2m_rw(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info, | | ^~~~~~~~~~~~~~~~~~~~~~~ | | | | | (3) entry to ‘xc_core_arch_map_p2m_rw’ |...... | 328 | if ( xc_domain_nr_gpfns(xch, info->domid, &dinfo->p2m_size) < 0 ) | | ~ | | | | | (4) following ‘false’ branch... |...... | 334 | if ( dinfo->p2m_size < info->nr_pages ) | | ~~ ~ | | | | | | | (6) following ‘false’ branch... | | (5) ...to here |...... | 340 | p2m_cr3 = GET_FIELD(live_shinfo, arch.p2m_cr3, dinfo->guest_width); | | ~~~~~~~ | | | | | (7) ...to here | 341 | | 342 | p2m_frame_list = p2m_cr3 ? xc_core_arch_map_p2m_list_rw(xch, dinfo, dom, live_shinfo, p2m_cr3) | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 343 | : xc_core_arch_map_p2m_tree_rw(xch, dinfo, dom, live_shinfo); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | | | (9) ...to here | | | (10) calling ‘xc_core_arch_map_p2m_tree_rw’ from ‘xc_core_arch_map_p2m_rw’ | | (8) following ‘false’ branch... | +--> ‘xc_core_arch_map_p2m_tree_rw’: events 11-24 | | 228 | xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinfo, | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (11) entry to ‘xc_core_arch_map_p2m_tree_rw’ |...... | 245 | if ( !live_p2m_frame_list_list ) | | ~ | | | | | (12) following ‘false’ branch (when ‘live_p2m_frame_list_list’ is non-NULL)... |...... | 252 | if ( !(p2m_frame_list_list = malloc(PAGE_SIZE)) ) | | ~~ ~ ~~~~~~~~~~~~~~~~~ | | | | | | | | | (14) allocated here | | | (15) assuming ‘p2m_frame_list_list’ is non-NULL | | | (16) following ‘false’ branch (when ‘p2m_frame_list_list’ is non-NULL)... | | (13) ...to here |...... | 257 | memcpy(p2m_frame_list_list, live_p2m_frame_list_list, PAGE_SIZE); | | ~~~~~~ | | | | | (17) ...to here |...... | 266 | else if ( dinfo->guest_width < sizeof(unsigned long) ) | | ~ | | | | | (18) following ‘false’ branch... |...... | 270 | live_p2m_frame_list = | | ~~~~~~~~~~~~~~~~~~~ | | | | | (19) ...to here |...... | 275 | if ( !live_p2m_frame_list ) | | ~ | | | | | (20) following ‘false’ branch (when ‘live_p2m_frame_list’ is non-NULL)... |...... | 282 | if ( !(p2m_frame_list = malloc(P2M_TOOLS_FL_SIZE)) ) | | ~~ ~ | | | | | | | (22) following ‘false’ branch (when ‘p2m_frame_list’ is non-NULL)... | | (21) ...to here |...... | 287 | memset(p2m_frame_list, 0, P2M_TOOLS_FL_SIZE); | | ~~~~~~ | | | | | (23) ...to here |...... | 300 | return p2m_frame_list; | | ~~~~~~ | | | | | (24) ‘p2m_frame_list_list’ leaks here; was allocated at (14) | ```Fixes bd7a29c3d0 ("tools/libs/ctrl: fix xc_core_arch_map_p2m() to support linear p2m table")Signed-off-by Edwin Török <[email protected]>--- tools/libs/guest/xg_core_x86.c | 2 ++ 1 file changed, 2 insertions(+)tools/libs/guest/xg_core_x86.c +0
-0
tools/libs/guest/xg_core_x86.c xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf · line 304
Quoted 8 lines
tools/libs/guest/xg_core_x86.c +2
-0@@ -297,6 +297,8 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf dinfo->p2m_frames =P2M_FL_ENTRIES ; +free (p2m_frame_list_list); +return p2m_frame_list; out:
I agree there are problems here, but I think they're larger still. The live_p2m_frame_list_list and live_p2m_frame_list foreign mappings are leaked too on the success path.
I think this is the necessary fix:
~Andrew
----8<----
tools/libs/guest/xg_core_x86.c +0 -0
@@ -229,11 +229,11 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,
struct domain_info_context *dinf
uint32_t dom, shared_info_any_t *live_shinfo) {
/* Double and single indirect references to the live P2M table */ - xen_pfn_t *live_p2m_frame_list_list;
+ xen_pfn_t *live_p2m_frame_list_list = NULL;
xen_pfn_t *live_p2m_frame_list = NULL;
/* Copies of the above. */
xen_pfn_t *p2m_frame_list_list = NULL;
- xen_pfn_t *p2m_frame_list;
+ xen_pfn_t *p2m_frame_list = NULL;
int err;
int i;
@@ -297,8 +297,6 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf
dinfo->p2m_frames = P2M_FL_ENTRIES;
- return p2m_frame_list;
-
out:
err = errno;
@@ -312,7 +310,7 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf
errno = err;
- return NULL;
+ return p2m_frame_list;
}
static int
Re: [PATCH v1 1/2] xc_core_arch_map_p2m_tree_rw: fix memory leak
Quoted 136 lines · Andrew Cooper
Quoted 131 lines · Edwin Török
From: Edwin Török <[email protected]>
Prior to
bd7a29c3d0'out' would've always been executed and memory freed, but that commit changed it such that it returns early and leaks.Found using gcc 12.2.1
-fanalyzer:``` xg_core_x86.c: In function ‘xc_core_arch_map_p2m_tree_rw’: xg_core_x86.c:300:5: error: leak of ‘p2m_frame_list_list’ [CWE-401] [-Werror=analyzer-malloc-leak] 300 | return p2m_frame_list; | ^~~~~~ ‘xc_core_arch_map_p2m_writable’: events 1-2 | | 378 | xc_core_arch_map_p2m_writable(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info, | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (1) entry to ‘xc_core_arch_map_p2m_writable’ |...... | 381 | return xc_core_arch_map_p2m_rw(xch, dinfo, info, live_shinfo, live_p2m, 1); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (2) calling ‘xc_core_arch_map_p2m_rw’ from ‘xc_core_arch_map_p2m_writable’ | +--> ‘xc_core_arch_map_p2m_rw’: events 3-10 | | 319 | xc_core_arch_map_p2m_rw(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info, | | ^~~~~~~~~~~~~~~~~~~~~~~ | | | | | (3) entry to ‘xc_core_arch_map_p2m_rw’ |...... | 328 | if ( xc_domain_nr_gpfns(xch, info->domid, &dinfo->p2m_size) < 0 ) | | ~ | | | | | (4) following ‘false’ branch... |...... | 334 | if ( dinfo->p2m_size < info->nr_pages ) | | ~~ ~ | | | | | | | (6) following ‘false’ branch... | | (5) ...to here |...... | 340 | p2m_cr3 = GET_FIELD(live_shinfo, arch.p2m_cr3, dinfo->guest_width); | | ~~~~~~~ | | | | | (7) ...to here | 341 | | 342 | p2m_frame_list = p2m_cr3 ? xc_core_arch_map_p2m_list_rw(xch, dinfo, dom, live_shinfo, p2m_cr3) | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 343 | : xc_core_arch_map_p2m_tree_rw(xch, dinfo, dom, live_shinfo); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | | | (9) ...to here | | | (10) calling ‘xc_core_arch_map_p2m_tree_rw’ from ‘xc_core_arch_map_p2m_rw’ | | (8) following ‘false’ branch... | +--> ‘xc_core_arch_map_p2m_tree_rw’: events 11-24 | | 228 | xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinfo, | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (11) entry to ‘xc_core_arch_map_p2m_tree_rw’ |...... | 245 | if ( !live_p2m_frame_list_list ) | | ~ | | | | | (12) following ‘false’ branch (when ‘live_p2m_frame_list_list’ is non-NULL)... |...... | 252 | if ( !(p2m_frame_list_list = malloc(PAGE_SIZE)) ) | | ~~ ~ ~~~~~~~~~~~~~~~~~ | | | | | | | | | (14) allocated here | | | (15) assuming ‘p2m_frame_list_list’ is non-NULL | | | (16) following ‘false’ branch (when ‘p2m_frame_list_list’ is non-NULL)... | | (13) ...to here |...... | 257 | memcpy(p2m_frame_list_list, live_p2m_frame_list_list, PAGE_SIZE); | | ~~~~~~ | | | | | (17) ...to here |...... | 266 | else if ( dinfo->guest_width < sizeof(unsigned long) ) | | ~ | | | | | (18) following ‘false’ branch... |...... | 270 | live_p2m_frame_list = | | ~~~~~~~~~~~~~~~~~~~ | | | | | (19) ...to here |...... | 275 | if ( !live_p2m_frame_list ) | | ~ | | | | | (20) following ‘false’ branch (when ‘live_p2m_frame_list’ is non-NULL)... |...... | 282 | if ( !(p2m_frame_list = malloc(P2M_TOOLS_FL_SIZE)) ) | | ~~ ~ | | | | | | | (22) following ‘false’ branch (when ‘p2m_frame_list’ is non-NULL)... | | (21) ...to here |...... | 287 | memset(p2m_frame_list, 0, P2M_TOOLS_FL_SIZE); | | ~~~~~~ | | | | | (23) ...to here |...... | 300 | return p2m_frame_list; | | ~~~~~~ | | | | | (24) ‘p2m_frame_list_list’ leaks here; was allocated at (14) | ```Fixes bd7a29c3d0 ("tools/libs/ctrl: fix xc_core_arch_map_p2m() to support linear p2m table")Signed-off-by Edwin Török <[email protected]>---
tools/libs/guest/xg_core_x86.c| 2 ++1 file changed, 2 insertions(+)tools/libs/guest/xg_core_x86.c +2
-0@@ -297,6 +297,8 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf dinfo->p2m_frames =P2M_FL_ENTRIES ; +free (p2m_frame_list_list); +return p2m_frame_list;
Quoted 5 lines
tools/libs/guest/xg_core_x86.c xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf · line 304
Quoted 1 line
tools/libs/guest/xg_core_x86.c +0
-0out:I agree there are problems here, but I think they're larger still. The
live_p2m_frame_list_listandlive_p2m_frame_listforeign mappings are leaked too on the success path.
I thought the goal of that function was to create the mapping (judging by its name), but looking at its caller there is another map_foreign_pages there, so there is indeed a leak (-fanalyzer won't be able to spot these unless we figure out a way to put some attributs on these map and unmap to teach it that they are alloc/free pairs).
Pushed updated commits here (top 2): leak-fixes <https://github.com/edwintorok/xen/commits/leak-fixes>
Before posting a V2 I'll try it out on an actual machine though, just to check that we don't have a double-free instead now.
Thanks,
--Edwin
Quoted 47 lines
I think this is the necessary fix:
~Andrew
----8<----
tools/libs/guest/xg_core_x86.c +0
-0@@ -229,11 +229,11 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,struct
domain_info_context*dinf
uint32_tdom,shared_info_any_t*live_shinfo){ /* Double and single indirect references to the live P2M table */tools/libs/guest/xg_core_x86.c +2
-2-xen_pfn_t *live_p2m_frame_list_list; +xen_pfn_t *live_p2m_frame_list_list =NULL ;xen_pfn_t *live_p2m_frame_list =NULL ;/* Copies of the above. */ xen_pfn_t *p2m_frame_list_list =NULL ; -xen_pfn_t *p2m_frame_list; +xen_pfn_t *p2m_frame_list =NULL ;int err;int i;@@ -297,8 +297,6 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,struct
domain_info_context*dinfdinfo->p2m_frames = P2M_FL_ENTRIES;tools/libs/guest/xg_core_x86.c +0
-2-return p2m_frame_list; - out: err = errno;@@ -312,7 +310,7 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,struct
domain_info_context*dinferrno = err;tools/libs/guest/xg_core_x86.c +1
-1-return NULL ; +return p2m_frame_list; } static int
Juergen Gross
Reviewed-byRe: [PATCH v1 1/2] xc_core_arch_map_p2m_tree_rw: fix memory leak
In reply to Andrew CooperQuoted 132 lines · Andrew Cooper
Quoted 131 lines · Edwin Török
From: Edwin Török <[email protected]>
Prior to
bd7a29c3d0'out' would've always been executed and memory freed, but that commit changed it such that it returns early and leaks.Found using gcc 12.2.1
-fanalyzer:``` xg_core_x86.c: In function ‘xc_core_arch_map_p2m_tree_rw’: xg_core_x86.c:300:5: error: leak of ‘p2m_frame_list_list’ [CWE-401] [-Werror=analyzer-malloc-leak] 300 | return p2m_frame_list; | ^~~~~~ ‘xc_core_arch_map_p2m_writable’: events 1-2 | | 378 | xc_core_arch_map_p2m_writable(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info, | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (1) entry to ‘xc_core_arch_map_p2m_writable’ |...... | 381 | return xc_core_arch_map_p2m_rw(xch, dinfo, info, live_shinfo, live_p2m, 1); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (2) calling ‘xc_core_arch_map_p2m_rw’ from ‘xc_core_arch_map_p2m_writable’ | +--> ‘xc_core_arch_map_p2m_rw’: events 3-10 | | 319 | xc_core_arch_map_p2m_rw(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info, | | ^~~~~~~~~~~~~~~~~~~~~~~ | | | | | (3) entry to ‘xc_core_arch_map_p2m_rw’ |...... | 328 | if ( xc_domain_nr_gpfns(xch, info->domid, &dinfo->p2m_size) < 0 ) | | ~ | | | | | (4) following ‘false’ branch... |...... | 334 | if ( dinfo->p2m_size < info->nr_pages ) | | ~~ ~ | | | | | | | (6) following ‘false’ branch... | | (5) ...to here |...... | 340 | p2m_cr3 = GET_FIELD(live_shinfo, arch.p2m_cr3, dinfo->guest_width); | | ~~~~~~~ | | | | | (7) ...to here | 341 | | 342 | p2m_frame_list = p2m_cr3 ? xc_core_arch_map_p2m_list_rw(xch, dinfo, dom, live_shinfo, p2m_cr3) | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 343 | : xc_core_arch_map_p2m_tree_rw(xch, dinfo, dom, live_shinfo); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | | | (9) ...to here | | | (10) calling ‘xc_core_arch_map_p2m_tree_rw’ from ‘xc_core_arch_map_p2m_rw’ | | (8) following ‘false’ branch... | +--> ‘xc_core_arch_map_p2m_tree_rw’: events 11-24 | | 228 | xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinfo, | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (11) entry to ‘xc_core_arch_map_p2m_tree_rw’ |...... | 245 | if ( !live_p2m_frame_list_list ) | | ~ | | | | | (12) following ‘false’ branch (when ‘live_p2m_frame_list_list’ is non-NULL)... |...... | 252 | if ( !(p2m_frame_list_list = malloc(PAGE_SIZE)) ) | | ~~ ~ ~~~~~~~~~~~~~~~~~ | | | | | | | | | (14) allocated here | | | (15) assuming ‘p2m_frame_list_list’ is non-NULL | | | (16) following ‘false’ branch (when ‘p2m_frame_list_list’ is non-NULL)... | | (13) ...to here |...... | 257 | memcpy(p2m_frame_list_list, live_p2m_frame_list_list, PAGE_SIZE); | | ~~~~~~ | | | | | (17) ...to here |...... | 266 | else if ( dinfo->guest_width < sizeof(unsigned long) ) | | ~ | | | | | (18) following ‘false’ branch... |...... | 270 | live_p2m_frame_list = | | ~~~~~~~~~~~~~~~~~~~ | | | | | (19) ...to here |...... | 275 | if ( !live_p2m_frame_list ) | | ~ | | | | | (20) following ‘false’ branch (when ‘live_p2m_frame_list’ is non-NULL)... |...... | 282 | if ( !(p2m_frame_list = malloc(P2M_TOOLS_FL_SIZE)) ) | | ~~ ~ | | | | | | | (22) following ‘false’ branch (when ‘p2m_frame_list’ is non-NULL)... | | (21) ...to here |...... | 287 | memset(p2m_frame_list, 0, P2M_TOOLS_FL_SIZE); | | ~~~~~~ | | | | | (23) ...to here |...... | 300 | return p2m_frame_list; | | ~~~~~~ | | | | | (24) ‘p2m_frame_list_list’ leaks here; was allocated at (14) | ```Fixes bd7a29c3d0 ("tools/libs/ctrl: fix xc_core_arch_map_p2m() to support linear p2m table")Signed-off-by Edwin Török <[email protected]>--- tools/libs/guest/xg_core_x86.c | 2 ++ 1 file changed, 2 insertions(+)tools/libs/guest/xg_core_x86.c +2
-0@@ -297,6 +297,8 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf dinfo->p2m_frames =P2M_FL_ENTRIES ; +free (p2m_frame_list_list); +return p2m_frame_list;
Quoted 7 lines
tools/libs/guest/xg_core_x86.c xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf · line 304
Quoted 1 line
tools/libs/guest/xg_core_x86.c +0
-0out:I agree there are problems here, but I think they're larger still. The
live_p2m_frame_list_listandlive_p2m_frame_listforeign mappings are leaked too on the success path.I think this is the necessary fix:
Yes, I agree.
Quoted 37 lines
~Andrew
----8<----
tools/libs/guest/xg_core_x86.c +0
-0@@ -229,11 +229,11 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,struct
domain_info_context*dinf
uint32_tdom,shared_info_any_t*live_shinfo) {
/* Double and single indirect references to the live P2M table */
-xen_pfn_t*live_p2m_frame_list_list;
+xen_pfn_t*live_p2m_frame_list_list= NULL;
xen_pfn_t*live_p2m_frame_list= NULL;
/* Copies of the above. */
xen_pfn_t*p2m_frame_list_list= NULL;
-xen_pfn_t*p2m_frame_list;
+xen_pfn_t*p2m_frame_list= NULL;int err;
int i;tools/libs/guest/xg_core_x86.c +0
-0@@ -297,8 +297,6 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,struct
domain_info_context*dinf
dinfo->p2m_frames=P2M_FL_ENTRIES;- return
p2m_frame_list;tools/libs/guest/xg_core_x86.c +0
-1- out: err = errno;@@ -312,7 +310,7 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,struct
domain_info_context*dinf
Quoted 8 lines
errno = err;
- return NULL;
+ returnp2m_frame_list;
}static int
In case this fix is taken, my
can be added.
Juergen
Re: [PATCH v1 1/2] xc_core_arch_map_p2m_tree_rw: fix memory leak
Quoted 217 lines · Juergen Gross
Quoted 169 lines · Andrew Cooper
Quoted 157 lines · Edwin Török
From: Edwin Török <[email protected]>
Prior to
bd7a29c3d0'out' would've always been executed and memory freed, but that commit changed it such that it returns early and leaks.Found using gcc 12.2.1
-fanalyzer:``` xg_core_x86.c: In function ‘xc_core_arch_map_p2m_tree_rw’: xg_core_x86.c:300:5: error: leak of ‘p2m_frame_list_list’ [CWE-401] [-Werror=analyzer-malloc-leak] 300 | return p2m_frame_list; | ^~~~~~ ‘xc_core_arch_map_p2m_writable’: events 1-2 | | 378 | xc_core_arch_map_p2m_writable(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info, | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (1) entry to ‘xc_core_arch_map_p2m_writable’ |...... | 381 | return xc_core_arch_map_p2m_rw(xch, dinfo, info, live_shinfo, live_p2m, 1); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (2) calling ‘xc_core_arch_map_p2m_rw’ from ‘xc_core_arch_map_p2m_writable’ | +--> ‘xc_core_arch_map_p2m_rw’: events 3-10 | | 319 | xc_core_arch_map_p2m_rw(xc_interface *xch, struct domain_info_context *dinfo, xc_dominfo_t *info, | | ^~~~~~~~~~~~~~~~~~~~~~~ | | | | | (3) entry to ‘xc_core_arch_map_p2m_rw’ |...... | 328 | if ( xc_domain_nr_gpfns(xch, info->domid, &dinfo->p2m_size) < 0 ) | | ~ | | | | | (4) following ‘false’ branch... |...... | 334 | if ( dinfo->p2m_size < info->nr_pages ) | | ~~ ~ | | | | | | | (6) following ‘false’ branch... | | (5) ...to here |...... | 340 | p2m_cr3 = GET_FIELD(live_shinfo, arch.p2m_cr3, dinfo->guest_width); | | ~~~~~~~ | | | | | (7) ...to here | 341 | | 342 | p2m_frame_list = p2m_cr3 ? xc_core_arch_map_p2m_list_rw(xch, dinfo, dom, live_shinfo, p2m_cr3) | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 343 | : xc_core_arch_map_p2m_tree_rw(xch, dinfo, dom, live_shinfo); | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | | | (9) ...to here | | | (10) calling ‘xc_core_arch_map_p2m_tree_rw’ from ‘xc_core_arch_map_p2m_rw’ | | (8) following ‘false’ branch... | +--> ‘xc_core_arch_map_p2m_tree_rw’: events 11-24 | | 228 | xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinfo, | | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | (11) entry to ‘xc_core_arch_map_p2m_tree_rw’ |...... | 245 | if ( !live_p2m_frame_list_list ) | | ~ | | | | | (12) following ‘false’ branch (when ‘live_p2m_frame_list_list’ is non-NULL)... |...... | 252 | if ( !(p2m_frame_list_list = malloc(PAGE_SIZE)) ) | | ~~ ~ ~~~~~~~~~~~~~~~~~ | | | | | | | | | (14) allocated here | | | (15) assuming ‘p2m_frame_list_list’ is non-NULL | | | (16) following ‘false’ branch (when ‘p2m_frame_list_list’ is non-NULL)... | | (13) ...to here |...... | 257 | memcpy(p2m_frame_list_list, live_p2m_frame_list_list, PAGE_SIZE); | | ~~~~~~ | | | | | (17) ...to here |...... | 266 | else if ( dinfo->guest_width < sizeof(unsigned long) ) | | ~ | | | | | (18) following ‘false’ branch... |...... | 270 | live_p2m_frame_list = | | ~~~~~~~~~~~~~~~~~~~ | | | | | (19) ...to here |...... | 275 | if ( !live_p2m_frame_list ) | | ~ | | | | | (20) following ‘false’ branch (when ‘live_p2m_frame_list’ is non-NULL)... |...... | 282 | if ( !(p2m_frame_list = malloc(P2M_TOOLS_FL_SIZE)) ) | | ~~ ~ | | | | | | | (22) following ‘false’ branch (when ‘p2m_frame_list’ is non-NULL)... | | (21) ...to here |...... | 287 | memset(p2m_frame_list, 0, P2M_TOOLS_FL_SIZE); | | ~~~~~~ | | | | | (23) ...to here |...... | 300 | return p2m_frame_list; | | ~~~~~~ | | | | | (24) ‘p2m_frame_list_list’ leaks here; was allocated at (14) | ```Fixes bd7a29c3d0 ("tools/libs/ctrl: fix xc_core_arch_map_p2m() tosupport linear p2m table")
Signed-off-by Edwin Török <[email protected]>---
tools/libs/guest/xg_core_x86.c| 2 ++1 file changed, 2 insertions(+)tools/libs/guest/xg_core_x86.c +0
-0@@ -297,6 +297,8 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,Quoted 6 lines
struct
domain_info_context*dinf
dinfo->p2m_frames=P2M_FL_ENTRIES;
+free(p2m_frame_list_list);
+
returnp2m_frame_list;
out:I agree there are problems here, but I think they're larger still. The
live_p2m_frame_list_listandlive_p2m_frame_listforeign mappings are leaked too on the success path.Quoted 1 line
I think this is the necessary fix:
Yes, I agree.
Quoted 31 lines
~Andrew
----8<----
tools/libs/guest/xg_core_x86.c +0
-0@@ -229,11 +229,11 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,struct
domain_info_context*dinf
uint32_tdom,shared_info_any_t*live_shinfo)
{
/* Double and single indirect references to the live P2M table */ -xen_pfn_t*live_p2m_frame_list_list;
+xen_pfn_t*live_p2m_frame_list_list= NULL;
xen_pfn_t*live_p2m_frame_list= NULL;
/* Copies of the above. */
xen_pfn_t*p2m_frame_list_list= NULL;
-xen_pfn_t*p2m_frame_list;
+xen_pfn_t*p2m_frame_list= NULL;
int err;
int i;tools/libs/guest/xg_core_x86.c +0
-0@@ -297,8 +297,6 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch,struct
domain_info_context*dinf
dinfo->p2m_frames=P2M_FL_ENTRIES;
- returnp2m_frame_list;
-
out:Quoted 9 lines
err = errno;
@@ -312,7 +310,7 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, structdomain_info_context*dinf
errno = err;
- return NULL;
+ returnp2m_frame_list;
}
static intIn case this fix is taken, my
Quoted 3 lines
Reviewed-by Juergen Gross <[email protected]>can be added.
Thanks. I'll write a full patch and post it, with appropriate tags, and also include it in my commit sweep.
~Andrew
PATCH 1/1 libs/guest: Fix resource leaks in xc_core_arch_map_p2m_tree_rw()
In reply to Edwin TörökEdwin, with the help of GCC's -fanalyzer, identified that p2m_frame_list_list gets leaked. What fanalyzer can't see is that the live_p2m_frame_list_list and live_p2m_frame_list foreign mappings are leaked too.
Rework the logic so the out path is executed unconditionally, which cleans up all the intermediate allocations/mappings appropriately.
--- tools/libs/guest/xg_core_x86.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
tools/libs/guest/xg_core_x86.c +3 -5
@@ -229,11 +229,11 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf uint32_t dom, shared_info_any_t *live_shinfo) {/* Double and single indirect references to the live P2M table */ - xen_pfn_t *live_p2m_frame_list_list; + xen_pfn_t *live_p2m_frame_list_list = NULL;xen_pfn_t *live_p2m_frame_list =NULL ;/* Copies of the above. */ xen_pfn_t *p2m_frame_list_list =NULL ; -xen_pfn_t *p2m_frame_list; +xen_pfn_t *p2m_frame_list =NULL ;int err;int i;
@@ -297,8 +297,6 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf dinfo->p2m_frames =P2M_FL_ENTRIES ; -return p2m_frame_list; - out: err = errno;
@@ -312,7 +310,7 @@ xc_core_arch_map_p2m_tree_rw(xc_interface *xch, struct domain_info_context *dinf errno = err; -return NULL ; +return p2m_frame_list; } static int
Signature · 1 line
2.30.2
PATCH 2/2 backup_ptes: fix leak on realloc failure
In reply to Edwin TörökFrom: Edwin Török <[email protected]>
From man 2 realloc:If realloc() fails, the original block is left untouched; it is not freed or moved.
Found using GCC -fanalyzer:
``` | 184 | backup->entries = realloc(backup->entries, | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | | | | | (91) when ‘realloc’ fails | | | (92) ‘old_ptes.entries’ leaks here; was allocated at (44) | | (90) ...to here ```
--- tools/libs/guest/xg_offline_page.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
tools/libs/guest/xg_offline_page.c +5 -2
@@ -181,10 +181,13 @@ static int backup_ptes(xen_pfn_t table_mfn, int offset,if (backup->max == backup->cur) { - backup->entries =realloc (backup->entries, +void * orig = backup->entries; + backup->entries =realloc (orig, backup->max *2 *sizeof (struct pte_backup_entry )); -if (backup->entries ==NULL ) +if (backup->entries ==NULL ) { +free (orig);return -1 ; + }else backup->max *=2 ; }
Signature · 1 line
2.39.1
Re: [PATCH v1 2/2] backup_ptes: fix leak on realloc failure
Quoted 15 lines · Edwin Török
From: Edwin Török <[email protected]>
From
man 2 realloc:If realloc() fails, the original block is left untouched; it is not freed or moved.Found using GCC -fanalyzer:
``` | 184 | backup->entries = realloc(backup->entries, | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | | | | | | (91) when ‘realloc’ fails | | | (92) ‘old_ptes.entries’ leaks here; was allocated at (44) | | (90) ...to here ```
Quoted 1 line
Signed-off-by Edwin Török <[email protected]>
In terms of the fix, Acked-by: Andrew Cooper
<[email protected]>, but
Quoted 9 lines
--- tools/libs/guest/xg_offline_page.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)tools/libs/guest/xg_offline_page.c +0
-0
tools/libs/guest/xg_offline_page.c static int backup_ptes(xen_pfn_t table_mfn, int offset, · line 184
Quoted 5 lines
tools/libs/guest/xg_offline_page.c +1
-1@@ -181,10 +181,13 @@ static int backup_ptes(xen_pfn_t table_mfn, int offset,if (backup->max == backup->cur) { - backup->entries = realloc(backup->entries, +void * orig = backup->entries;
void *orig, and a newline.
tools/libs/guest/xg_offline_page.c static int backup_ptes(xen_pfn_t table_mfn, int offset, · line 187
Quoted 4 lines
tools/libs/guest/xg_offline_page.c +2
-1+ backup->entries =realloc (orig, backup->max *2 *sizeof (struct pte_backup_entry )); -if (backup->entries ==NULL ) +if (backup->entries ==NULL ) {
Newline.
Can be fixed on commit.
~Andrew
Quoted 6 lines
tools/libs/guest/xg_offline_page.c +2
-0+free (orig);return -1 ; + }else backup->max *=2 ; }