From mboxrd@z Thu Jan 1 00:00:00 1970 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 7BAE4C7EE23 for ; Fri, 24 Feb 2023 16:14:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229619AbjBXQOI (ORCPT ); Fri, 24 Feb 2023 11:14:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229554AbjBXQOG (ORCPT ); Fri, 24 Feb 2023 11:14:06 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C861DED for ; Fri, 24 Feb 2023 08:14:05 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 84E7C60A56; Fri, 24 Feb 2023 16:14:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1677255244; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Z4y/9dAY3JthMTAKl83y2tl+nDHl7zrev4zhEa+0k1c=; b=Qnoxljf5dfBmT10lLQd0HqOw8X2o2PyE6Wuy65DeYRU0OwcOQqicYjfXV+w3GnRVR+X7lC 4l//BpP93nOMtluK+FJKVpWCi5n9IplsO2Z8ilOatYwoOME2Z5Eh59CVZeMeM4UbrOG6tY iN0yTuAvzQy/Hwy8ML8PDluAuT0/f+c= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1677255244; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Z4y/9dAY3JthMTAKl83y2tl+nDHl7zrev4zhEa+0k1c=; b=6ziVKP0i3UfRMVgZtGKvYCS06C+xI0mti5g9su4Q5+/sDi1q6JjeyXvjSVndyjouBYAJ1h Wn1YhiasAvAg7wBQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 4AC0713246; Fri, 24 Feb 2023 16:14:02 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id v0ZvBUri+GOxKQAAMHmgww (envelope-from ); Fri, 24 Feb 2023 16:14:02 +0000 Date: Sat, 25 Feb 2023 00:13:56 +0800 From: Coly Li To: Li Xiao Keng Cc: Jes Sorensen , Mariusz Tkaczyk , Paul Menzel , linux-raid@vger.kernel.org, linfeilong , "liuzhiqiang (I)" , Wu Guanghao Subject: Re: [PATCH V4] Fix NULL dereference in super_by_fd Message-ID: References: <1dabb70e-ca1a-bd45-182a-ddaa95821f86@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <1dabb70e-ca1a-bd45-182a-ddaa95821f86@huawei.com> Precedence: bulk List-ID: X-Mailing-List: linux-raid@vger.kernel.org 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=, __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=, ss=, dump_directory=, ident=, devlist=) at mdadm.c:1959 > #6 main (argc=, argv=) 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 > Signed-off-by: Wu Guang Hao > --- 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