linux-raid.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V4] Fix NULL dereference in super_by_fd
@ 2022-12-21  9:37 Li Xiao Keng
  2022-12-21 12:05 ` Mariusz Tkaczyk
  2023-02-24 16:13 ` Coly Li
  0 siblings, 2 replies; 6+ messages in thread
From: Li Xiao Keng @ 2022-12-21  9:37 UTC (permalink / raw)
  To: Jes Sorensen, Mariusz Tkaczyk, Paul Menzel, linux-raid
  Cc: linfeilong, liuzhiqiang (I), Wu Guanghao

When we create 100 partitions (major is 259 not 254) in a raid device,
mdadm may coredump:

Core was generated by `/usr/sbin/mdadm --detail --export /dev/md1p7'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
74		VPCMPEQ	(%rdi), %ymm0, %ymm1
(gdb) bt
#0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
#1  0x00007fbb9a7e4139 in __strcpy_chk (dest=dest@entry=0x55d55d6a13ac "", src=0x0, destlen=destlen@entry=32) at strcpy_chk.c:28
#2  0x000055d55ba1766d in strcpy (__src=<optimized out>, __dest=0x55d55d6a13ac "") at /usr/include/bits/string_fortified.h:79
#3  super_by_fd (fd=fd@entry=3, subarrayp=subarrayp@entry=0x7fff44dfcc48) at util.c:1289
#4  0x000055d55ba273a6 in Detail (dev=0x7fff44dfef0b "/dev/md1p7", c=0x7fff44dfe440) at Detail.c:101
#5  0x000055d55ba0de61 in misc_list (c=<optimized out>, ss=<optimized out>, dump_directory=<optimized out>, ident=<optimized out>, devlist=<optimized out>) at mdadm.c:1959
#6  main (argc=<optimized out>, argv=<optimized out>) at mdadm.c:1629

The direct cause is fd2devnm returning NULL, so add a check.

V1->V2: When fd2devnm return NULL, super_by_fd return NULL but not an
incomplete 'st' entry. At the same time, add a check in map_by_devnm
to avoid coredump.

V2->V3: Fix style issues.
V3->V4: Change strcpy() to strncpy().

Signed-off-by: Li Xiao Keng <lixiaokeng@huawei.com>
Signed-off-by: Wu Guang Hao <wuguanghao3@huawei.com>
---
 mapfile.c | 4 ++++
 util.c    | 7 ++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/mapfile.c b/mapfile.c
index 8d7acb3..f72fe0d 100644
--- a/mapfile.c
+++ b/mapfile.c
@@ -292,6 +292,10 @@ struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
 struct map_ent *map_by_devnm(struct map_ent **map, char *devnm)
 {
 	struct map_ent *mp;
+
+	if (!devnm)
+		return NULL;
+
 	if (!*map)
 		map_read(map);

diff --git a/util.c b/util.c
index 64dd409..3a84ee3 100644
--- a/util.c
+++ b/util.c
@@ -1241,6 +1241,11 @@ struct supertype *super_by_fd(int fd, char **subarrayp)
 	int i;
 	char *subarray = NULL;
 	char container[32] = "";
+	char *devnm = NULL;
+
+	devnm = fd2devnm(fd);
+	if (!devnm)
+		return NULL;

 	sra = sysfs_read(fd, NULL, GET_VERSION);

@@ -1286,7 +1291,7 @@ struct supertype *super_by_fd(int fd, char **subarrayp)
 		if (subarrayp)
 			*subarrayp = subarray;
 		strcpy(st->container_devnm, container);
-		strcpy(st->devnm, fd2devnm(fd));
+		strncpy(st->devnm, devnm, MD_NAME_MAX - 1);
 	} else
 		free(subarray);

-- 
1.8.3.1

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

* Re: [PATCH V4] Fix NULL dereference in super_by_fd
  2022-12-21  9:37 [PATCH V4] Fix NULL dereference in super_by_fd Li Xiao Keng
@ 2022-12-21 12:05 ` Mariusz Tkaczyk
  2023-01-06  7:07   ` Li Xiao Keng
  2023-02-24 16:13 ` Coly Li
  1 sibling, 1 reply; 6+ messages in thread
From: Mariusz Tkaczyk @ 2022-12-21 12:05 UTC (permalink / raw)
  To: Li Xiao Keng, Coly Li
  Cc: Jes Sorensen, Paul Menzel, linux-raid, linfeilong,
	liuzhiqiang (I), Wu Guanghao

On Wed, 21 Dec 2022 17:37:52 +0800
Li Xiao Keng <lixiaokeng@huawei.com> wrote:

> When we create 100 partitions (major is 259 not 254) in a raid device,
> mdadm may coredump:
> 
> Core was generated by `/usr/sbin/mdadm --detail --export /dev/md1p7'.
> Program terminated with signal SIGSEGV, Segmentation fault.
> #0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
> 74		VPCMPEQ	(%rdi), %ymm0, %ymm1
> (gdb) bt
> #0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
> #1  0x00007fbb9a7e4139 in __strcpy_chk (dest=dest@entry=0x55d55d6a13ac "",
> src=0x0, destlen=destlen@entry=32) at strcpy_chk.c:28 #2  0x000055d55ba1766d
> in strcpy (__src=<optimized out>, __dest=0x55d55d6a13ac "") at
> /usr/include/bits/string_fortified.h:79 #3  super_by_fd (fd=fd@entry=3,
> subarrayp=subarrayp@entry=0x7fff44dfcc48) at util.c:1289 #4
> 0x000055d55ba273a6 in Detail (dev=0x7fff44dfef0b "/dev/md1p7",
> c=0x7fff44dfe440) at Detail.c:101 #5  0x000055d55ba0de61 in misc_list
> (c=<optimized out>, ss=<optimized out>, dump_directory=<optimized out>,
> ident=<optimized out>, devlist=<optimized out>) at mdadm.c:1959 #6  main
> (argc=<optimized out>, argv=<optimized out>) at mdadm.c:1629
> 
> The direct cause is fd2devnm returning NULL, so add a check.
> 
> V1->V2: When fd2devnm return NULL, super_by_fd return NULL but not an
> incomplete 'st' entry. At the same time, add a check in map_by_devnm
> to avoid coredump.
> 
> V2->V3: Fix style issues.
> V3->V4: Change strcpy() to strncpy().
> 
> Signed-off-by: Li Xiao Keng <lixiaokeng@huawei.com>
> Signed-off-by: Wu Guang Hao <wuguanghao3@huawei.com>


Acked-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>

Coly could you please take a look?

Thanks,
Mariusz

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

* Re: [PATCH V4] Fix NULL dereference in super_by_fd
  2022-12-21 12:05 ` Mariusz Tkaczyk
@ 2023-01-06  7:07   ` Li Xiao Keng
  2023-02-24  1:02     ` Li Xiao Keng
  0 siblings, 1 reply; 6+ messages in thread
From: Li Xiao Keng @ 2023-01-06  7:07 UTC (permalink / raw)
  To: Mariusz Tkaczyk, Coly Li
  Cc: Jes Sorensen, Paul Menzel, linux-raid, linfeilong,
	liuzhiqiang (I), Wu Guanghao

ping

On 2022/12/21 20:05, Mariusz Tkaczyk wrote:
> On Wed, 21 Dec 2022 17:37:52 +0800
> Li Xiao Keng <lixiaokeng@huawei.com> wrote:
> 
>> When we create 100 partitions (major is 259 not 254) in a raid device,
>> mdadm may coredump:
>>
>> Core was generated by `/usr/sbin/mdadm --detail --export /dev/md1p7'.
>> Program terminated with signal SIGSEGV, Segmentation fault.
>> #0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
>> 74		VPCMPEQ	(%rdi), %ymm0, %ymm1
>> (gdb) bt
>> #0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
>> #1  0x00007fbb9a7e4139 in __strcpy_chk (dest=dest@entry=0x55d55d6a13ac "",
>> src=0x0, destlen=destlen@entry=32) at strcpy_chk.c:28 #2  0x000055d55ba1766d
>> in strcpy (__src=<optimized out>, __dest=0x55d55d6a13ac "") at
>> /usr/include/bits/string_fortified.h:79 #3  super_by_fd (fd=fd@entry=3,
>> subarrayp=subarrayp@entry=0x7fff44dfcc48) at util.c:1289 #4
>> 0x000055d55ba273a6 in Detail (dev=0x7fff44dfef0b "/dev/md1p7",
>> c=0x7fff44dfe440) at Detail.c:101 #5  0x000055d55ba0de61 in misc_list
>> (c=<optimized out>, ss=<optimized out>, dump_directory=<optimized out>,
>> ident=<optimized out>, devlist=<optimized out>) at mdadm.c:1959 #6  main
>> (argc=<optimized out>, argv=<optimized out>) at mdadm.c:1629
>>
>> The direct cause is fd2devnm returning NULL, so add a check.
>>
>> V1->V2: When fd2devnm return NULL, super_by_fd return NULL but not an
>> incomplete 'st' entry. At the same time, add a check in map_by_devnm
>> to avoid coredump.
>>
>> V2->V3: Fix style issues.
>> V3->V4: Change strcpy() to strncpy().
>>
>> Signed-off-by: Li Xiao Keng <lixiaokeng@huawei.com>
>> Signed-off-by: Wu Guang Hao <wuguanghao3@huawei.com>
> 
> 
> Acked-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
> 
> Coly could you please take a look?
> 
> Thanks,
> Mariusz
> .
> 

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

* Re: [PATCH V4] Fix NULL dereference in super_by_fd
  2023-01-06  7:07   ` Li Xiao Keng
@ 2023-02-24  1:02     ` Li Xiao Keng
  2023-02-24  2:11       ` Coly Li
  0 siblings, 1 reply; 6+ messages in thread
From: Li Xiao Keng @ 2023-02-24  1:02 UTC (permalink / raw)
  To: Mariusz Tkaczyk, Coly Li
  Cc: Jes Sorensen, Paul Menzel, linux-raid, linfeilong,
	liuzhiqiang (I), Wu Guanghao

ping

On 2023/1/6 15:07, Li Xiao Keng wrote:
> ping
> 
> On 2022/12/21 20:05, Mariusz Tkaczyk wrote:
>> On Wed, 21 Dec 2022 17:37:52 +0800
>> Li Xiao Keng <lixiaokeng@huawei.com> wrote:
>>
>>> When we create 100 partitions (major is 259 not 254) in a raid device,
>>> mdadm may coredump:
>>>
>>> Core was generated by `/usr/sbin/mdadm --detail --export /dev/md1p7'.
>>> Program terminated with signal SIGSEGV, Segmentation fault.
>>> #0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
>>> 74		VPCMPEQ	(%rdi), %ymm0, %ymm1
>>> (gdb) bt
>>> #0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
>>> #1  0x00007fbb9a7e4139 in __strcpy_chk (dest=dest@entry=0x55d55d6a13ac "",
>>> src=0x0, destlen=destlen@entry=32) at strcpy_chk.c:28 #2  0x000055d55ba1766d
>>> in strcpy (__src=<optimized out>, __dest=0x55d55d6a13ac "") at
>>> /usr/include/bits/string_fortified.h:79 #3  super_by_fd (fd=fd@entry=3,
>>> subarrayp=subarrayp@entry=0x7fff44dfcc48) at util.c:1289 #4
>>> 0x000055d55ba273a6 in Detail (dev=0x7fff44dfef0b "/dev/md1p7",
>>> c=0x7fff44dfe440) at Detail.c:101 #5  0x000055d55ba0de61 in misc_list
>>> (c=<optimized out>, ss=<optimized out>, dump_directory=<optimized out>,
>>> ident=<optimized out>, devlist=<optimized out>) at mdadm.c:1959 #6  main
>>> (argc=<optimized out>, argv=<optimized out>) at mdadm.c:1629
>>>
>>> The direct cause is fd2devnm returning NULL, so add a check.
>>>
>>> V1->V2: When fd2devnm return NULL, super_by_fd return NULL but not an
>>> incomplete 'st' entry. At the same time, add a check in map_by_devnm
>>> to avoid coredump.
>>>
>>> V2->V3: Fix style issues.
>>> V3->V4: Change strcpy() to strncpy().
>>>
>>> Signed-off-by: Li Xiao Keng <lixiaokeng@huawei.com>
>>> Signed-off-by: Wu Guang Hao <wuguanghao3@huawei.com>
>>
>>
>> Acked-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
>>
>> Coly could you please take a look?
>>
>> Thanks,
>> Mariusz
>> .
>>

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

* Re: [PATCH V4] Fix NULL dereference in super_by_fd
  2023-02-24  1:02     ` Li Xiao Keng
@ 2023-02-24  2:11       ` Coly Li
  0 siblings, 0 replies; 6+ messages in thread
From: Coly Li @ 2023-02-24  2:11 UTC (permalink / raw)
  To: Li Xiao Keng
  Cc: Mariusz Tkaczyk, Jes Sorensen, Paul Menzel, linux-raid,
	linfeilong, liuzhiqiang (I), Wu Guanghao



> 2023年2月24日 09:02,Li Xiao Keng <lixiaokeng@huawei.com> 写道:
> 
> ping

Copied. I will take a look soon.

Thanks for reminding.

Coly Li


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

* Re: [PATCH V4] Fix NULL dereference in super_by_fd
  2022-12-21  9:37 [PATCH V4] Fix NULL dereference in super_by_fd Li Xiao Keng
  2022-12-21 12:05 ` Mariusz Tkaczyk
@ 2023-02-24 16:13 ` Coly Li
  1 sibling, 0 replies; 6+ messages in thread
From: Coly Li @ 2023-02-24 16:13 UTC (permalink / raw)
  To: Li Xiao Keng
  Cc: Jes Sorensen, Mariusz Tkaczyk, Paul Menzel, linux-raid,
	linfeilong, liuzhiqiang (I), Wu Guanghao

On Wed, Dec 21, 2022 at 05:37:52PM +0800, Li Xiao Keng wrote:
> When we create 100 partitions (major is 259 not 254) in a raid device,
> mdadm may coredump:
> 
> Core was generated by `/usr/sbin/mdadm --detail --export /dev/md1p7'.
> Program terminated with signal SIGSEGV, Segmentation fault.
> #0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
> 74		VPCMPEQ	(%rdi), %ymm0, %ymm1
> (gdb) bt
> #0  __strlen_avx2_rtm () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
> #1  0x00007fbb9a7e4139 in __strcpy_chk (dest=dest@entry=0x55d55d6a13ac "", src=0x0, destlen=destlen@entry=32) at strcpy_chk.c:28
> #2  0x000055d55ba1766d in strcpy (__src=<optimized out>, __dest=0x55d55d6a13ac "") at /usr/include/bits/string_fortified.h:79
> #3  super_by_fd (fd=fd@entry=3, subarrayp=subarrayp@entry=0x7fff44dfcc48) at util.c:1289
> #4  0x000055d55ba273a6 in Detail (dev=0x7fff44dfef0b "/dev/md1p7", c=0x7fff44dfe440) at Detail.c:101
> #5  0x000055d55ba0de61 in misc_list (c=<optimized out>, ss=<optimized out>, dump_directory=<optimized out>, ident=<optimized out>, devlist=<optimized out>) at mdadm.c:1959
> #6  main (argc=<optimized out>, argv=<optimized out>) at mdadm.c:1629
> 
> The direct cause is fd2devnm returning NULL, so add a check.
> 

The patch is fine to me. But the following changelog should be moved to the
location after '---'.



> V1->V2: When fd2devnm return NULL, super_by_fd return NULL but not an
> incomplete 'st' entry. At the same time, add a check in map_by_devnm
> to avoid coredump.
> 
> V2->V3: Fix style issues.
> V3->V4: Change strcpy() to strncpy().
> 
> Signed-off-by: Li Xiao Keng <lixiaokeng@huawei.com>
> Signed-off-by: Wu Guang Hao <wuguanghao3@huawei.com>
> ---

The changelog should be moved here, then they won't show up in commit
log when Jes takes this patch into mdadm git tree.

V1->V2: When fd2devnm return NULL, super_by_fd return NULL but not an
incomplete 'st' entry. At the same time, add a check in map_by_devnm
to avoid coredump.
 
V2->V3: Fix style issues.
V3->V4: Change strcpy() to strncpy().


Thanks.

>  mapfile.c | 4 ++++
>  util.c    | 7 ++++++-
>  2 files changed, 10 insertions(+), 1 deletion(-)
> 
> diff --git a/mapfile.c b/mapfile.c
> index 8d7acb3..f72fe0d 100644
> --- a/mapfile.c
> +++ b/mapfile.c
> @@ -292,6 +292,10 @@ struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
>  struct map_ent *map_by_devnm(struct map_ent **map, char *devnm)
>  {
>  	struct map_ent *mp;
> +
> +	if (!devnm)
> +		return NULL;
> +
>  	if (!*map)
>  		map_read(map);
> 
> diff --git a/util.c b/util.c
> index 64dd409..3a84ee3 100644
> --- a/util.c
> +++ b/util.c
> @@ -1241,6 +1241,11 @@ struct supertype *super_by_fd(int fd, char **subarrayp)
>  	int i;
>  	char *subarray = NULL;
>  	char container[32] = "";
> +	char *devnm = NULL;
> +
> +	devnm = fd2devnm(fd);
> +	if (!devnm)
> +		return NULL;
> 
>  	sra = sysfs_read(fd, NULL, GET_VERSION);
> 
> @@ -1286,7 +1291,7 @@ struct supertype *super_by_fd(int fd, char **subarrayp)
>  		if (subarrayp)
>  			*subarrayp = subarray;
>  		strcpy(st->container_devnm, container);
> -		strcpy(st->devnm, fd2devnm(fd));
> +		strncpy(st->devnm, devnm, MD_NAME_MAX - 1);
>  	} else
>  		free(subarray);
> 
> -- 
> 1.8.3.1

-- 
Coly Li

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

end of thread, other threads:[~2023-02-24 16:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-21  9:37 [PATCH V4] Fix NULL dereference in super_by_fd Li Xiao Keng
2022-12-21 12:05 ` Mariusz Tkaczyk
2023-01-06  7:07   ` Li Xiao Keng
2023-02-24  1:02     ` Li Xiao Keng
2023-02-24  2:11       ` Coly Li
2023-02-24 16:13 ` Coly Li

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