linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [BUG][PATCH][BTRFS-PROGS] Bug overflow fix
@ 2012-10-24  8:24 Rock Lee
  2012-10-25 15:18 ` David Sterba
  0 siblings, 1 reply; 3+ messages in thread
From: Rock Lee @ 2012-10-24  8:24 UTC (permalink / raw)
  To: linux-btrfs

If there's is a long name directory exists in the /dev, then an
overflow will hit in function utils.c btrfs_scan_one_dir:1013!

The minimal fix is to use snprintf instead of strcpy.

The reason why not using strncpy is that, if there is no null byte
among the first n bytes of src, the string  placed  in  dest
will not be null - terminated.

Signed-off-by: Rock Lee <zimilo@code-trick.com>
---
 utils.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/utils.c b/utils.c
index 3c88d2e..7200aef 100644
--- a/utils.c
+++ b/utils.c
@@ -969,7 +969,7 @@ int btrfs_scan_one_dir(char *dirname, int run_ioctl)
 	pending = malloc(sizeof(*pending));
 	if (!pending)
 		return -ENOMEM;
-	strcpy(pending->name, dirname);
+	snprintf(pending->name, sizeof(pending->name), "%s", dirname);

 again:
 	dirname_len = strlen(pending->name);
@@ -1010,7 +1010,8 @@ again:
 				ret = -ENOMEM;
 				goto fail;
 			}
-			strcpy(next->name, fullpath);
+			snprintf(next->name, sizeof(next->name),
+				 "%s", fullpath);
 			list_add_tail(&next->list, &pending_list);
 		}
 		if (!S_ISBLK(st.st_mode)) {
-- 
1.7.7.6

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

* Re: [BUG][PATCH][BTRFS-PROGS] Bug overflow fix
  2012-10-24  8:24 [BUG][PATCH][BTRFS-PROGS] Bug overflow fix Rock Lee
@ 2012-10-25 15:18 ` David Sterba
  2012-10-26  3:22   ` Rock Lee
  0 siblings, 1 reply; 3+ messages in thread
From: David Sterba @ 2012-10-25 15:18 UTC (permalink / raw)
  To: Rock Lee; +Cc: linux-btrfs

On Wed, Oct 24, 2012 at 04:24:02PM +0800, Rock Lee wrote:
> If there's is a long name directory exists in the /dev, then an
> overflow will hit in function utils.c btrfs_scan_one_dir:1013!
> 
> The minimal fix is to use snprintf instead of strcpy.
> 
> The reason why not using strncpy is that, if there is no null byte
> among the first n bytes of src, the string  placed  in  dest
> will not be null - terminated.
> 
> ---
> index 3c88d2e..7200aef 100644
> --- a/utils.c
> +++ b/utils.c
> @@ -969,7 +969,7 @@ int btrfs_scan_one_dir(char *dirname, int run_ioctl)
>  	pending = malloc(sizeof(*pending));
>  	if (!pending)
>  		return -ENOMEM;
> -	strcpy(pending->name, dirname);
> +	snprintf(pending->name, sizeof(pending->name), "%s", dirname);

pending is defined as

919 struct pending_dir {
920         struct list_head list;
921         char name[256];
922 };

and name is supposed to hold a full path, so it should be of PATH_MAX
(4096) size, 256 is a limit for a single filename.

There's another hardcoded value for a path

971         dirname_len = strlen(pending->name);
972         pathlen = 1024;
^^^
973         fullpath = malloc(pathlen);
974         dirname = pending->name;

that shuld be of PATH_MAX size.

thanks,
david

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

* Re: [BUG][PATCH][BTRFS-PROGS] Bug overflow fix
  2012-10-25 15:18 ` David Sterba
@ 2012-10-26  3:22   ` Rock Lee
  0 siblings, 0 replies; 3+ messages in thread
From: Rock Lee @ 2012-10-26  3:22 UTC (permalink / raw)
  To: dave, Rock Lee, linux-btrfs

Yeah, I will improve the patch to get it better.

Thanks,
- Rock

2012/10/25 David Sterba <dave@jikos.cz>:
> On Wed, Oct 24, 2012 at 04:24:02PM +0800, Rock Lee wrote:
>> If there's is a long name directory exists in the /dev, then an
>> overflow will hit in function utils.c btrfs_scan_one_dir:1013!
>>
>> The minimal fix is to use snprintf instead of strcpy.
>>
>> The reason why not using strncpy is that, if there is no null byte
>> among the first n bytes of src, the string  placed  in  dest
>> will not be null - terminated.
>>
>> ---
>> index 3c88d2e..7200aef 100644
>> --- a/utils.c
>> +++ b/utils.c
>> @@ -969,7 +969,7 @@ int btrfs_scan_one_dir(char *dirname, int run_ioctl)
>>       pending = malloc(sizeof(*pending));
>>       if (!pending)
>>               return -ENOMEM;
>> -     strcpy(pending->name, dirname);
>> +     snprintf(pending->name, sizeof(pending->name), "%s", dirname);
>
> pending is defined as
>
> 919 struct pending_dir {
> 920         struct list_head list;
> 921         char name[256];
> 922 };
>
> and name is supposed to hold a full path, so it should be of PATH_MAX
> (4096) size, 256 is a limit for a single filename.
>
> There's another hardcoded value for a path
>
> 971         dirname_len = strlen(pending->name);
> 972         pathlen = 1024;
> ^^^
> 973         fullpath = malloc(pathlen);
> 974         dirname = pending->name;
>
> that shuld be of PATH_MAX size.
>
> thanks,
> david

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

end of thread, other threads:[~2012-10-26  3:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-24  8:24 [BUG][PATCH][BTRFS-PROGS] Bug overflow fix Rock Lee
2012-10-25 15:18 ` David Sterba
2012-10-26  3:22   ` Rock Lee

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).