Skip to contentSkip to conversation

xfsdump: prevent use-after-free in fstab_commit()

PATCH In review Bill O'Donnell 2 participants 8 messages Aug 29, 2024 – Aug 30, 2024 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfsdump: prevent use-after-free in fstab_commit()

Fix potential use-after-free of list pointer in fstab_commit(). Use a copy of the pointer when calling invidx_commit().

Coverity CID 1498039.

---
 invutil/fstab.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
invutil/fstab.c +7 -2
index 88d849e..fe2b1f9 100644
@@ -66,6 +66,7 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
     data_t *d;
     invt_fstab_t *fstabentry;
     int fstabentry_idx;
+    node_t *list_cpy = list;
 
     n = current;
     if(n == NULL || n->data == NULL)
@@ -77,8 +78,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 
     if(d->deleted == BOOL_TRUE && d->imported == BOOL_FALSE) {
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;
+		invidx_commit(win, d->children[i], list_cpy);
 	}
+	free(list_cpy);
 	mark_all_children_commited(current);
 
 	fstabentry_idx = (int)(((long)fstabentry - (long)fstab_file[fidx].mapaddr - sizeof(invt_counter_t)) / sizeof(invt_fstab_t));
@@ -101,8 +104,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 	invt_fstab_t *dest;
 
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;
+		invidx_commit(win, d->children[i], list_cpy);
 	}
+	free(list_cpy);
 	mark_all_children_commited(current);
 
 	if(find_matching_fstab(0, fstabentry) >= 0) {
Signature · 1 line
2.46.0
#1 lore raw
Quoted 2 lines · Bill O'Donnell

Fix potential use-after-free of list pointer in fstab_commit(). Use a copy of the pointer when calling invidx_commit().

I'm not sure how (or even if) the use after free happens -xfsdump is so hard to read - but ...

Quoted 20 lines

Coverity CID 1498039.

---
 invutil/fstab.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
invutil/fstab.c +1 -0
index 88d849e..fe2b1f9 100644
@@ -66,6 +66,7 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
     data_t *d;
     invt_fstab_t *fstabentry;
     int fstabentry_idx;
+    node_t *list_cpy = list;
 
     n = current;
     if(n == NULL || n->data == NULL)

invutil/fstab.c fstab_commit(WINDOW *win, node_t *current, node_t *list) · line 81

Quoted 5 lines
invutil/fstab.c +1 -1
@@ -77,8 +78,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 
     if(d->deleted == BOOL_TRUE && d->imported == BOOL_FALSE) {
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;

this copies the memory address stored in "list" into your new pointer var "list_cpy"

invutil/fstab.c fstab_commit(WINDOW *win, node_t *current, node_t *list) · line 82

Quoted 1 line
invutil/fstab.c +1 -0
+		invidx_commit(win, d->children[i], list_cpy);

If invidx_commit() frees the 2nd argument, it frees the memory address pointed to by both list and list_cpy.

Storing the same memory address in 2 pointer variables does not prevent that memory from being freed.

invutil/fstab.c fstab_commit(WINDOW *win, node_t *current, node_t *list) · line 84

Quoted 2 lines
invutil/fstab.c +1 -0
 	}
+	free(list_cpy);

and then this would double-free that same memory address.

Quoted 15 lines
invutil/fstab.c +3 -1
 	mark_all_children_commited(current);
 
 	fstabentry_idx = (int)(((long)fstabentry - (long)fstab_file[fidx].mapaddr - sizeof(invt_counter_t)) / sizeof(invt_fstab_t));
@@ -101,8 +104,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 	invt_fstab_t *dest;
 
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;	
+		invidx_commit(win, d->children[i], list_cpy);
 	}
+	free(list_cpy);
 	mark_all_children_commited(current);
 
 	if(find_matching_fstab(0, fstabentry) >= 0) {
#2 lore raw
Quoted 40 lines · Eric Sandeen
Quoted 2 lines · Bill O'Donnell

Fix potential use-after-free of list pointer in fstab_commit(). Use a copy of the pointer when calling invidx_commit().

I'm not sure how (or even if) the use after free happens -xfsdump is so hard to read - but ...

Quoted 20 lines

Coverity CID 1498039.

---
 invutil/fstab.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
invutil/fstab.c +1 -0
index 88d849e..fe2b1f9 100644
@@ -66,6 +66,7 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
     data_t *d;
     invt_fstab_t *fstabentry;
     int fstabentry_idx;
+    node_t *list_cpy = list;
 
     n = current;
     if(n == NULL || n->data == NULL)

invutil/fstab.c fstab_commit(WINDOW *win, node_t *current, node_t *list) · line 81

Quoted 5 lines
invutil/fstab.c +1 -1
@@ -77,8 +78,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 
     if(d->deleted == BOOL_TRUE && d->imported == BOOL_FALSE) {
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;

this copies the memory address stored in "list" into your new pointer var "list_cpy"

invutil/fstab.c fstab_commit(WINDOW *win, node_t *current, node_t *list) · line 82

Quoted 1 line
invutil/fstab.c +1 -0
+		invidx_commit(win, d->children[i], list_cpy);

If invidx_commit() frees the 2nd argument, it frees the memory address pointed to by both list and list_cpy.

Quoted 7 lines

Storing the same memory address in 2 pointer variables does not prevent that memory from being freed.

invutil/fstab.c fstab_commit(WINDOW *win, node_t *current, node_t *list) · line 84

Quoted 2 lines
invutil/fstab.c +1 -0
 	}
+	free(list_cpy);

and then this would double-free that same memory address.

I see that now. This code is indeed difficult to grok.

Perhaps (if this a legitimate finding of use after free), instead of having the memory freed in invidx_commit(), it should instead be freed once in fstab_commit() after the iterations of the for-loops in that function. I'll have a look at that possibility.

Thanks-
Bill

Quoted 18 lines
Quoted 17 lines
invutil/fstab.c +3 -1
 	mark_all_children_commited(current);
 
 	fstabentry_idx = (int)(((long)fstabentry - (long)fstab_file[fidx].mapaddr - sizeof(invt_counter_t)) / sizeof(invt_fstab_t));
@@ -101,8 +104,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 	invt_fstab_t *dest;
 
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;	
+		invidx_commit(win, d->children[i], list_cpy);
 	}
+	free(list_cpy);
 	mark_all_children_commited(current);
 
 	if(find_matching_fstab(0, fstabentry) >= 0) {
#3 lore raw
Quoted 49 lines · Bill O'Donnell
Quoted 40 lines · Eric Sandeen
Quoted 2 lines · Bill O'Donnell

Fix potential use-after-free of list pointer in fstab_commit(). Use a copy of the pointer when calling invidx_commit().

I'm not sure how (or even if) the use after free happens -xfsdump is so hard to read - but ...

Quoted 20 lines

Coverity CID 1498039.

---
 invutil/fstab.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
invutil/fstab.c +1 -0
index 88d849e..fe2b1f9 100644
@@ -66,6 +66,7 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
     data_t *d;
     invt_fstab_t *fstabentry;
     int fstabentry_idx;
+    node_t *list_cpy = list;
 
     n = current;
     if(n == NULL || n->data == NULL)

invutil/fstab.c fstab_commit(WINDOW *win, node_t *current, node_t *list) · line 81

Quoted 5 lines
invutil/fstab.c +1 -1
@@ -77,8 +78,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 
     if(d->deleted == BOOL_TRUE && d->imported == BOOL_FALSE) {
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;

this copies the memory address stored in "list" into your new pointer var "list_cpy"

invutil/fstab.c fstab_commit(WINDOW *win, node_t *current, node_t *list) · line 82

Quoted 1 line
invutil/fstab.c +1 -0
+		invidx_commit(win, d->children[i], list_cpy);

If invidx_commit() frees the 2nd argument, it frees the memory address pointed to by both list and list_cpy.

Quoted 7 lines

Storing the same memory address in 2 pointer variables does not prevent that memory from being freed.

invutil/fstab.c fstab_commit(WINDOW *win, node_t *current, node_t *list) · line 84

Quoted 2 lines
invutil/fstab.c +1 -0
 	}
+	free(list_cpy);

and then this would double-free that same memory address.

Quoted 5 lines

I see that now. This code is indeed difficult to grok.

Perhaps (if this a legitimate finding of use after free), instead of having the memory freed in invidx_commit(), it should instead be freed once in fstab_commit() after the iterations of the for-loops in that function. I'll have a look at that possibility.

i.e., Removing what Coverity tags as the culprit (node_free(list_del(dst_n)) from invidx_commit(), and adding free(list) following the for-loop iteration in fstab_commit() may be a better solution.

Quoted 21 lines
Quoted 20 lines
Quoted 19 lines
invutil/fstab.c +3 -1
 	mark_all_children_commited(current);
 
 	fstabentry_idx = (int)(((long)fstabentry - (long)fstab_file[fidx].mapaddr - sizeof(invt_counter_t)) / sizeof(invt_fstab_t));
@@ -101,8 +104,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 	invt_fstab_t *dest;
 
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;	
+		invidx_commit(win, d->children[i], list_cpy);
 	}
+	free(list_cpy);
 	mark_all_children_commited(current);
 
 	if(find_matching_fstab(0, fstabentry) >= 0) {
#4 lore raw
Quoted 1 line · Bill O'Donnell

On Thu, Aug 29, 2024 at 02:47:24PM -0500, Bill O'Donnell wrote:

...

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	free(list_cpy);

and then this would double-free that same memory address.

I see that now. This code is indeed difficult to grok.

Quoted 7 lines
Quoted 3 lines

Perhaps (if this a legitimate finding of use after free), instead of having the memory freed in invidx_commit(), it should instead be freed once in fstab_commit() after the iterations of the for-loops in that function. I'll have a look at that possibility.

i.e., Removing what Coverity tags as the culprit (node_free(list_del(dst_n)) from invidx_commit(), and adding free(list) following the for-loop iteration in fstab_commit() may be a better solution.

I don't think that's the right approach.

invidx_commit() has this while loop, which is where coverity thinks the passed-in "list" might get freed, before the caller uses it again:

                /* Clean up the mess we just created */
                /* find node for dst_fileidx */
                dst_n = find_invidx_node(list, dst_fileidx);
                tmp_parent = ((data_t *)(dst_n->data))->parent;
                while(dst_n != NULL) {
                    node_t *tmp_n1;

                    dst_d = dst_n->data;

                    /* close affected invidx file and stobj files */
                    for(i = 0; i < dst_d->nbr_children; i++) {
                        close_stobj_file(((data_t *)(dst_d->children[i]->data))->file_idx, BOOL_FALSE);
                    }

                    /* free_all_children on that node */
                    free_all_children(dst_n);
                    tmp_n1 = find_invidx_node(dst_n->next, dst_fileidx);
                    node_free(list_del(dst_n));
                    dst_n = tmp_n1;
                }

"list" is presumably the head of a list of items, and this is cleaning up / freeing items within that list. Coverity seems to think that the while loop can end up getting back to the head and freeing it, which the caller then uses again in a loop.

My guess is that coverity is wrong, but I don't think you're going to be able to prove that (or fix this) without at least getting a sense of what this code is actually doing, and how this list is shaped and managed...

-Eric

#5 lore raw
Quoted 45 lines · Eric Sandeen
Quoted 1 line · Bill O'Donnell

On Thu, Aug 29, 2024 at 02:47:24PM -0500, Bill O'Donnell wrote:

...

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	free(list_cpy);

and then this would double-free that same memory address.

I see that now. This code is indeed difficult to grok.

Quoted 7 lines
Quoted 3 lines

Perhaps (if this a legitimate finding of use after free), instead of having the memory freed in invidx_commit(), it should instead be freed once in fstab_commit() after the iterations of the for-loops in that function. I'll have a look at that possibility.

i.e., Removing what Coverity tags as the culprit (node_free(list_del(dst_n)) from invidx_commit(), and adding free(list) following the for-loop iteration in fstab_commit() may be a better solution.

I don't think that's the right approach.

invidx_commit() has this while loop, which is where coverity thinks the passed-in "list" might get freed, before the caller uses it again:

                /* Clean up the mess we just created */
                /* find node for dst_fileidx */
                dst_n = find_invidx_node(list, dst_fileidx);
                tmp_parent = ((data_t *)(dst_n->data))->parent;
                while(dst_n != NULL) {
                    node_t *tmp_n1;

                    dst_d = dst_n->data;

                    /* close affected invidx file and stobj files */
                    for(i = 0; i < dst_d->nbr_children; i++) {
                        close_stobj_file(((data_t *)(dst_d->children[i]->data))->file_idx, BOOL_FALSE);
                    }

                    /* free_all_children on that node */
                    free_all_children(dst_n);
                    tmp_n1 = find_invidx_node(dst_n->next, dst_fileidx);
                    node_free(list_del(dst_n));
                    dst_n = tmp_n1;
                }
Quoted 7 lines

"list" is presumably the head of a list of items, and this is cleaning up / freeing items within that list. Coverity seems to think that the while loop can end up getting back to the head and freeing it, which the caller then uses again in a loop.

My guess is that coverity is wrong, but I don't think you're going to be able to prove that (or fix this) without at least getting a sense of what this code is actually doing, and how this list is shaped and managed...

That's my take on it as well. I'm leaning towards a false positive. I'll have another look. Thanks for reviewing.
-Bill

A message that is not in the archive (sent off-list or not yet received): SA1PR10MB758637A0D3EE774C9DF5B94489972@SA1PR10MB7586.namprd10.prod.outlook.com

#7 lore raw

Re: [External] : Re: [PATCH] xfsdump: prevent use-after-free in fstab_commit()

Quoted 6 lines · Mark Tinguely

I spent some time in xfsdump/xfsrestore but am not an inventory expert…I don’t see a use after free problem but I am scratching my head with the node->data->children memory.

The callers all do a free_all_children() which marks the node->data->children pointer to be NULL and then calls node_free() which does a free to node->data->children.

IMO, the inventory management code is not worth a ton of time.

Thanks Mark. Agree on that, but certain security orgs think that every "finding" of a certain severity needs triage, so we're a little bit between a rock and a hard place here. :)

-Eric

Quoted 69 lines

Mark

*From: *Bill O'Donnell <[email protected]>
*Date: *Thursday, August 29, 2024 at 5:49 PM
*To: *Eric Sandeen <[email protected]>
*Cc: *[email protected] <[email protected]>, [email protected] <[email protected]>, [email protected] <[email protected]> *Subject: *[External] : Re: [PATCH] xfsdump: prevent use-after-free in fstab_commit()

Quoted 45 lines · Eric Sandeen
Quoted 1 line · Bill O'Donnell

On Thu, Aug 29, 2024 at 02:47:24PM -0500, Bill O'Donnell wrote:

...

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+        free(list_cpy);

and then this would double-free that same memory address.

I see that now. This code is indeed difficult to grok.

Quoted 7 lines
Quoted 3 lines

Perhaps (if this a legitimate finding of use after free), instead of having the memory freed in invidx_commit(), it should instead be freed once in fstab_commit() after the iterations of the for-loops in that function. I'll have a look at that possibility.

i.e., Removing what Coverity tags as the culprit (node_free(list_del(dst_n)) from invidx_commit(), and adding free(list) following the for-loop iteration in fstab_commit() may be a better solution.

I don't think that's the right approach.

invidx_commit() has this while loop, which is where coverity thinks the passed-in "list" might get freed, before the caller uses it again:

                 /* Clean up the mess we just created */ /* find node for dst_fileidx */
                 dst_n = find_invidx_node(list, dst_fileidx); tmp_parent = ((data_t *)(dst_n->data))->parent; while(dst_n != NULL) {
                     node_t *tmp_n1;

                     dst_d = dst_n->data;

                     /* close affected invidx file and stobj files */
                     for(i = 0; i < dst_d->nbr_children; i++) { close_stobj_file(((data_t *)(dst_d->children[i]->data))->file_idx, BOOL_FALSE); }

                     /* free_all_children on that node */ free_all_children(dst_n);
                     tmp_n1 = find_invidx_node(dst_n->next, dst_fileidx); node_free(list_del(dst_n));
                     dst_n = tmp_n1;
                 }

Quoted 7 lines

"list" is presumably the head of a list of items, and this is cleaning up / freeing items within that list. Coverity seems to think that the while loop can end up getting back to the head and freeing it, which the caller then uses again in a loop.

My guess is that coverity is wrong, but I don't think you're going to be able to prove that (or fix this) without at least getting a sense of what this code is actually doing, and how this list is shaped and managed...

That's my take on it as well. I'm leaning towards a false positive. I'll have another look. Thanks for reviewing.
-Bill

#8 lore raw
In reply to Bill O'Donnell
Quoted 6 lines · Bill O'Donnell

Fix potential use-after-free of list pointer in fstab_commit(). Use a copy of the pointer when calling invidx_commit().

Coverity CID 1498039.

NACK. Deeming this finding as a false positive.

Quoted 43 lines
---
 invutil/fstab.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
invutil/fstab.c +7 -2
index 88d849e..fe2b1f9 100644
@@ -66,6 +66,7 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
     data_t *d;
     invt_fstab_t *fstabentry;
     int fstabentry_idx;
+    node_t *list_cpy = list;
 
     n = current;
     if(n == NULL || n->data == NULL)
@@ -77,8 +78,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 
     if(d->deleted == BOOL_TRUE && d->imported == BOOL_FALSE) {
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;
+		invidx_commit(win, d->children[i], list_cpy);
 	}
+	free(list_cpy);
 	mark_all_children_commited(current);
 
 	fstabentry_idx = (int)(((long)fstabentry - (long)fstab_file[fidx].mapaddr - sizeof(invt_counter_t)) / sizeof(invt_fstab_t));
@@ -101,8 +104,10 @@ fstab_commit(WINDOW *win, node_t *current, node_t *list)
 	invt_fstab_t *dest;
 
 	for(i = 0; i < d->nbr_children; i++) {
-	    invidx_commit(win, d->children[i], list);
+		list_cpy = list;
+		invidx_commit(win, d->children[i], list_cpy);
 	}
+	free(list_cpy);
 	mark_all_children_commited(current);
 
 	if(find_matching_fstab(0, fstabentry) >= 0) {
Signature · 1 line
2.46.0