Skip to content

Commit 85c5f24

Browse files
fdmananagregkh
authored andcommitted
Btrfs: fix null pointer dereference on compressed write path error
commit 3527a01 upstream. At inode.c:compress_file_range(), under the "free_pages_out" label, we can end up dereferencing the "pages" pointer when it has a NULL value. This case happens when "start" has a value of 0 and we fail to allocate memory for the "pages" pointer. When that happens we jump to the "cont" label and then enter the "if (start == 0)" branch where we immediately call the cow_file_range_inline() function. If that function returns 0 (success creating an inline extent) or an error (like -ENOMEM for example) we jump to the "free_pages_out" label and then access "pages[i]" leading to a NULL pointer dereference, since "nr_pages" has a value greater than zero at that point. Fix this by setting "nr_pages" to 0 when we fail to allocate memory for the "pages" pointer. Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=201119 Fixes: 771ed68 ("Btrfs: Optimize compressed writeback and reads") CC: [email protected] # 4.4+ Reviewed-by: Liu Bo <[email protected]> Signed-off-by: Filipe Manana <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 8181a8f commit 85c5f24

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

fs/btrfs/inode.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ static noinline void compress_file_range(struct inode *inode,
503503
pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
504504
if (!pages) {
505505
/* just bail out to the uncompressed code */
506+
nr_pages = 0;
506507
goto cont;
507508
}
508509

0 commit comments

Comments
 (0)