From patchwork Wed Dec 14 03:17:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Xiao Keng X-Patchwork-Id: 13072747 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C94AC4332F for ; Wed, 14 Dec 2022 03:17:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237084AbiLNDRv (ORCPT ); Tue, 13 Dec 2022 22:17:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237203AbiLNDRt (ORCPT ); Tue, 13 Dec 2022 22:17:49 -0500 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FF7811A1A for ; Tue, 13 Dec 2022 19:17:44 -0800 (PST) Received: from kwepemm600010.china.huawei.com (unknown [172.30.72.56]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4NX0nm4ZYBzJpLH; Wed, 14 Dec 2022 11:14:04 +0800 (CST) Received: from [10.174.177.197] (10.174.177.197) by kwepemm600010.china.huawei.com (7.193.23.86) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Wed, 14 Dec 2022 11:17:41 +0800 To: Jes Sorensen , CC: linfeilong , "liuzhiqiang (I)" , Wu Guanghao From: lixiaokeng Subject: [PATCH V2] Fix NULL dereference in super_by_fd Message-ID: Date: Wed, 14 Dec 2022 11:17:41 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Language: en-GB X-Originating-IP: [10.174.177.197] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm600010.china.huawei.com (7.193.23.86) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-raid@vger.kernel.org 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/md1p3'. Program terminated with signal SIGSEGV, Segmentation fault. #0 __strlen_sse2 () at ../sysdeps/x86_64/multiarch/strlen-vec.S:126 126 movdqu (%rax), %xmm4 (gdb) bt #0 __strlen_sse2 () at ../sysdeps/x86_64/multiarch/strlen-vec.S:126 #1 0x00007f1944659139 in __strcpy_chk ( dest=dest@entry=0x55ea8d7c23ac "", src=0x0, destlen=destlen@entry=32) at strcpy_chk.c:28 #2 0x000055ea8d10b66d in strcpy (__src=, __dest=0x55ea8d7c23ac "") at /usr/include/bits/string_fortified.h:79 #3 super_by_fd (fd=fd@entry=3, subarrayp=subarrayp@entry=0x7ffe6a1dff08) at util.c:1289 #4 0x000055ea8d11b3a6 in Detail ( dev=0x7ffe6a1e2f22 "/dev/md1p3", c=0x7ffe6a1e1700) at Detail.c:101 #5 0x000055ea8d101e61 in misc_list (c=, ss=, dump_directory=, ident=, devlist=) at mdadm.c:1959 #6 main (argc=, argv=) at mdadm.c:1629 The direct cause is fd2devnm return NULL. Here add a check. Signed-off-by:Lixiaokeng Signed-off-by:Wuguanghao --- 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)); + strcpy(st->devnm, devnm); } else free(subarray);