linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs: Fix memory leak in __add_inode_ref()
@ 2021-12-09  6:56 Jianglei Nie
  2021-12-09  8:39 ` Qu Wenruo
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jianglei Nie @ 2021-12-09  6:56 UTC (permalink / raw)
  To: clm, josef, dsterba; +Cc: linux-btrfs, linux-kernel, Jianglei Nie

Line 1169 (#3) allocates a memory chunk for victim_name by kmalloc(),
but  when the function returns in line 1184 (#4) victim_name allcoated
by line 1169 (#3) is not freed, which will lead to a memory leak.
There is a similar snippet of code in this function as allocating a memory
chunk for victim_name in line 1104 (#1) as well as releasing the memory
in line 1116 (#2).

We should kfree() victim_name when the return value of backref_in_log()
is less than zero and before the function returns in line 1184 (#4).

1057 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
1058 				  struct btrfs_root *root,
1059 				  struct btrfs_path *path,
1060 				  struct btrfs_root *log_root,
1061 				  struct btrfs_inode *dir,
1062 				  struct btrfs_inode *inode,
1063 				  u64 inode_objectid, u64 parent_objectid,
1064 				  u64 ref_index, char *name, int namelen,
1065 				  int *search_done)
1066 {

1104 	victim_name = kmalloc(victim_name_len, GFP_NOFS);
	// #1: kmalloc (victim_name-1)
1105 	if (!victim_name)
1106 		return -ENOMEM;

1112	ret = backref_in_log(log_root, &search_key,
1113			parent_objectid, victim_name,
1114			victim_name_len);
1115	if (ret < 0) {
1116		kfree(victim_name); // #2: kfree (victim_name-1)
1117		return ret;
1118	} else if (!ret) {

1169 	victim_name = kmalloc(victim_name_len, GFP_NOFS);
	// #3: kmalloc (victim_name-2)
1170 	if (!victim_name)
1171 		return -ENOMEM;

1180 	ret = backref_in_log(log_root, &search_key,
1181 			parent_objectid, victim_name,
1182 			victim_name_len);
1183 	if (ret < 0) {
1184 		return ret; // #4: missing kfree (victim_name-2)
1185 	} else if (!ret) {

1241 	return 0;
1242 }

Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
---
 fs/btrfs/tree-log.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 8ab33caf016f..d373fec55521 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -1181,6 +1181,7 @@ static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
 					     parent_objectid, victim_name,
 					     victim_name_len);
 			if (ret < 0) {
+				kfree(victim_name);
 				return ret;
 			} else if (!ret) {
 				ret = -ENOENT;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] btrfs: Fix memory leak in __add_inode_ref()
  2021-12-09  6:56 [PATCH] btrfs: Fix memory leak in __add_inode_ref() Jianglei Nie
@ 2021-12-09  8:39 ` Qu Wenruo
  2021-12-09 12:40   ` Jianglei Nie
  2021-12-09 10:17 ` Filipe Manana
  2021-12-09 14:57 ` David Sterba
  2 siblings, 1 reply; 5+ messages in thread
From: Qu Wenruo @ 2021-12-09  8:39 UTC (permalink / raw)
  To: Jianglei Nie, clm, josef, dsterba; +Cc: linux-btrfs, linux-kernel



On 2021/12/9 14:56, Jianglei Nie wrote:
> Line 1169 (#3) allocates a memory chunk for victim_name by kmalloc(),
> but  when the function returns in line 1184 (#4) victim_name allcoated
> by line 1169 (#3) is not freed, which will lead to a memory leak.
> There is a similar snippet of code in this function as allocating a memory
> chunk for victim_name in line 1104 (#1) as well as releasing the memory
> in line 1116 (#2).
>
> We should kfree() victim_name when the return value of backref_in_log()
> is less than zero and before the function returns in line 1184 (#4).
>
> 1057 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
> 1058 				  struct btrfs_root *root,
> 1059 				  struct btrfs_path *path,
> 1060 				  struct btrfs_root *log_root,
> 1061 				  struct btrfs_inode *dir,
> 1062 				  struct btrfs_inode *inode,
> 1063 				  u64 inode_objectid, u64 parent_objectid,
> 1064 				  u64 ref_index, char *name, int namelen,
> 1065 				  int *search_done)
> 1066 {
>
> 1104 	victim_name = kmalloc(victim_name_len, GFP_NOFS);
> 	// #1: kmalloc (victim_name-1)
> 1105 	if (!victim_name)
> 1106 		return -ENOMEM;
>
> 1112	ret = backref_in_log(log_root, &search_key,
> 1113			parent_objectid, victim_name,
> 1114			victim_name_len);
> 1115	if (ret < 0) {
> 1116		kfree(victim_name); // #2: kfree (victim_name-1)
> 1117		return ret;
> 1118	} else if (!ret) {
>
> 1169 	victim_name = kmalloc(victim_name_len, GFP_NOFS);
> 	// #3: kmalloc (victim_name-2)
> 1170 	if (!victim_name)
> 1171 		return -ENOMEM;
>
> 1180 	ret = backref_in_log(log_root, &search_key,
> 1181 			parent_objectid, victim_name,
> 1182 			victim_name_len);
> 1183 	if (ret < 0) {
> 1184 		return ret; // #4: missing kfree (victim_name-2)
> 1185 	} else if (!ret) {
>
> 1241 	return 0;
> 1242 }
>
> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

BTW, mind to share the way how you exposed the missing kfree()?

Some awesome static analyse tool or just by eyeballing?

Thanks,
Qu
> ---
>   fs/btrfs/tree-log.c | 1 +
>   1 file changed, 1 insertion(+)
>
> diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
> index 8ab33caf016f..d373fec55521 100644
> --- a/fs/btrfs/tree-log.c
> +++ b/fs/btrfs/tree-log.c
> @@ -1181,6 +1181,7 @@ static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
>   					     parent_objectid, victim_name,
>   					     victim_name_len);
>   			if (ret < 0) {
> +				kfree(victim_name);
>   				return ret;
>   			} else if (!ret) {
>   				ret = -ENOENT;
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] btrfs: Fix memory leak in __add_inode_ref()
  2021-12-09  6:56 [PATCH] btrfs: Fix memory leak in __add_inode_ref() Jianglei Nie
  2021-12-09  8:39 ` Qu Wenruo
@ 2021-12-09 10:17 ` Filipe Manana
  2021-12-09 14:57 ` David Sterba
  2 siblings, 0 replies; 5+ messages in thread
From: Filipe Manana @ 2021-12-09 10:17 UTC (permalink / raw)
  To: Jianglei Nie; +Cc: clm, josef, dsterba, linux-btrfs, linux-kernel

On Thu, Dec 09, 2021 at 02:56:31PM +0800, Jianglei Nie wrote:
> Line 1169 (#3) allocates a memory chunk for victim_name by kmalloc(),
> but  when the function returns in line 1184 (#4) victim_name allcoated
> by line 1169 (#3) is not freed, which will lead to a memory leak.
> There is a similar snippet of code in this function as allocating a memory
> chunk for victim_name in line 1104 (#1) as well as releasing the memory
> in line 1116 (#2).
> 
> We should kfree() victim_name when the return value of backref_in_log()
> is less than zero and before the function returns in line 1184 (#4).
> 
> 1057 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
> 1058 				  struct btrfs_root *root,
> 1059 				  struct btrfs_path *path,
> 1060 				  struct btrfs_root *log_root,
> 1061 				  struct btrfs_inode *dir,
> 1062 				  struct btrfs_inode *inode,
> 1063 				  u64 inode_objectid, u64 parent_objectid,
> 1064 				  u64 ref_index, char *name, int namelen,
> 1065 				  int *search_done)
> 1066 {
> 
> 1104 	victim_name = kmalloc(victim_name_len, GFP_NOFS);
> 	// #1: kmalloc (victim_name-1)
> 1105 	if (!victim_name)
> 1106 		return -ENOMEM;
> 
> 1112	ret = backref_in_log(log_root, &search_key,
> 1113			parent_objectid, victim_name,
> 1114			victim_name_len);
> 1115	if (ret < 0) {
> 1116		kfree(victim_name); // #2: kfree (victim_name-1)
> 1117		return ret;
> 1118	} else if (!ret) {
> 
> 1169 	victim_name = kmalloc(victim_name_len, GFP_NOFS);
> 	// #3: kmalloc (victim_name-2)
> 1170 	if (!victim_name)
> 1171 		return -ENOMEM;
> 
> 1180 	ret = backref_in_log(log_root, &search_key,
> 1181 			parent_objectid, victim_name,
> 1182 			victim_name_len);
> 1183 	if (ret < 0) {
> 1184 		return ret; // #4: missing kfree (victim_name-2)
> 1185 	} else if (!ret) {
> 
> 1241 	return 0;
> 1242 }
> 
> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>

Fixes: d3316c8233bb05 ("btrfs: Properly handle backref_in_log retval")
Reviewed-by: Filipe Manana <fdmanana@suse.com>

Looks good, thanks.

> ---
>  fs/btrfs/tree-log.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
> index 8ab33caf016f..d373fec55521 100644
> --- a/fs/btrfs/tree-log.c
> +++ b/fs/btrfs/tree-log.c
> @@ -1181,6 +1181,7 @@ static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
>  					     parent_objectid, victim_name,
>  					     victim_name_len);
>  			if (ret < 0) {
> +				kfree(victim_name);
>  				return ret;
>  			} else if (!ret) {
>  				ret = -ENOENT;
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] btrfs: Fix memory leak in __add_inode_ref()
  2021-12-09  8:39 ` Qu Wenruo
@ 2021-12-09 12:40   ` Jianglei Nie
  0 siblings, 0 replies; 5+ messages in thread
From: Jianglei Nie @ 2021-12-09 12:40 UTC (permalink / raw)
  To: quwenruo.btrfs
  Cc: clm, dsterba, josef, linux-btrfs, linux-kernel, niejianglei2021

I did a static analysis for the Linux kernel using the clang checker


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] btrfs: Fix memory leak in __add_inode_ref()
  2021-12-09  6:56 [PATCH] btrfs: Fix memory leak in __add_inode_ref() Jianglei Nie
  2021-12-09  8:39 ` Qu Wenruo
  2021-12-09 10:17 ` Filipe Manana
@ 2021-12-09 14:57 ` David Sterba
  2 siblings, 0 replies; 5+ messages in thread
From: David Sterba @ 2021-12-09 14:57 UTC (permalink / raw)
  To: Jianglei Nie; +Cc: clm, josef, dsterba, linux-btrfs, linux-kernel

On Thu, Dec 09, 2021 at 02:56:31PM +0800, Jianglei Nie wrote:
> Line 1169 (#3) allocates a memory chunk for victim_name by kmalloc(),
> but  when the function returns in line 1184 (#4) victim_name allcoated
> by line 1169 (#3) is not freed, which will lead to a memory leak.
> There is a similar snippet of code in this function as allocating a memory
> chunk for victim_name in line 1104 (#1) as well as releasing the memory
> in line 1116 (#2).
> 
> We should kfree() victim_name when the return value of backref_in_log()
> is less than zero and before the function returns in line 1184 (#4).
> 
> 1057 static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
> 1058 				  struct btrfs_root *root,
> 1059 				  struct btrfs_path *path,
> 1060 				  struct btrfs_root *log_root,
> 1061 				  struct btrfs_inode *dir,
> 1062 				  struct btrfs_inode *inode,
> 1063 				  u64 inode_objectid, u64 parent_objectid,
> 1064 				  u64 ref_index, char *name, int namelen,
> 1065 				  int *search_done)
> 1066 {
> 
> 1104 	victim_name = kmalloc(victim_name_len, GFP_NOFS);
> 	// #1: kmalloc (victim_name-1)
> 1105 	if (!victim_name)
> 1106 		return -ENOMEM;
> 
> 1112	ret = backref_in_log(log_root, &search_key,
> 1113			parent_objectid, victim_name,
> 1114			victim_name_len);
> 1115	if (ret < 0) {
> 1116		kfree(victim_name); // #2: kfree (victim_name-1)
> 1117		return ret;
> 1118	} else if (!ret) {
> 
> 1169 	victim_name = kmalloc(victim_name_len, GFP_NOFS);
> 	// #3: kmalloc (victim_name-2)
> 1170 	if (!victim_name)
> 1171 		return -ENOMEM;
> 
> 1180 	ret = backref_in_log(log_root, &search_key,
> 1181 			parent_objectid, victim_name,
> 1182 			victim_name_len);
> 1183 	if (ret < 0) {
> 1184 		return ret; // #4: missing kfree (victim_name-2)
> 1185 	} else if (!ret) {
> 
> 1241 	return 0;
> 1242 }
> 
> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>

Added to misc-next, thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-12-09 14:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-09  6:56 [PATCH] btrfs: Fix memory leak in __add_inode_ref() Jianglei Nie
2021-12-09  8:39 ` Qu Wenruo
2021-12-09 12:40   ` Jianglei Nie
2021-12-09 10:17 ` Filipe Manana
2021-12-09 14:57 ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).