Fix NULL dereference in super_by_fd
PATCH v5 1/1 Fix NULL dereference in super_by_fd
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(). V4->V5: Move changelog to the location after '---'
mapfile.c | 4 ++++ util.c | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-)
mapfile.c +4 -0
@@ -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);
util.c +6 -1
@@ -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);
Signature · 1 line
1.8.3.1
Quoted 20 lines
2023年2月27日 11:12,Li Xiao Keng <[email protected]> 写道:
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:7474 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
Quoted 4 lines
The direct cause is fd2devnm returning NULL, so add a check.
Signed-off-by Li Xiao Keng <[email protected]>Signed-off-by Wu Guang Hao <[email protected]>
Thanks.
Coly Li
Quoted 55 lines
---
V1->V2: When fd2devnm return NULL,super_by_fdreturn NULL but not an incomplete 'st' entry. At the same time, add a check inmap_by_devnmto avoid coredump.
V2->V3: Fix style issues.V3->V4: Changestrcpy()tostrncpy().V4->V5: Move changelog to the location after '---'
mapfile.c| 4 ++++util.c| 7 ++++++-2 files changed, 10 insertions(+), 1 deletion(-)mapfile.c +4
-0@@ -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);util.c +6
-1@@ -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);Signature · 1 line
1.8.3.1
Quoted 16 lines · Li Xiao Keng
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
Quoted 4 lines
The direct cause is fd2devnm returning NULL, so add a check.
Signed-off-by Li Xiao Keng <[email protected]>Signed-off-by Wu Guang Hao <[email protected]>
Please forget the previous email, MacOS Mail client "intelligently" extends my email address which is not called for.
Thanks.
Coly Li
Quoted 55 lines
---
V1->V2: When fd2devnm return NULL,super_by_fdreturn NULL but not an incomplete 'st' entry. At the same time, add a check inmap_by_devnmto avoid coredump.
V2->V3: Fix style issues.V3->V4: Changestrcpy()tostrncpy().V4->V5: Move changelog to the location after '---'mapfile.c | 4 ++++ util.c | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-)mapfile.c +4
-0@@ -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);util.c +6
-1@@ -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);Signature · 1 line
1.8.3.1
Signature · 1 line
Coly Li
Jes Sorensen
AppliedQuoted 25 lines · Li Xiao Keng
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:1629The direct cause is fd2devnm returning NULL, so add a check.
Signed-off-by Li Xiao Keng <[email protected]>Signed-off-by Wu Guang Hao <[email protected]>---
V1->V2: When fd2devnm return NULL,super_by_fdreturn NULL but not an incomplete 'st' entry. At the same time, add a check inmap_by_devnmto avoid coredump.
Quoted 8 lines
V2->V3: Fix style issues.V3->V4: Changestrcpy()tostrncpy().V4->V5: Move changelog to the location after '---'mapfile.c | 4 ++++ util.c | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-)
Applied!
Thanks,
Jes