Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp12011451rwb; Sat, 26 Nov 2022 03:05:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf58f9zLDYhYV9W/ezp0nO24Zxrc4FPJGaD/qzWzheHw8Ce9wxlVqfjtxaAXkEL8Ztkw6TD4 X-Received: by 2002:a63:1247:0:b0:476:ed2a:6216 with SMTP id 7-20020a631247000000b00476ed2a6216mr33591662pgs.556.1669460724576; Sat, 26 Nov 2022 03:05:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669460723; cv=none; d=google.com; s=arc-20160816; b=RIEOuCI4u07jAp7+1fKE2fv/A9HZ5whFMMIRaOD2qaLJg+yb2K37s709LL3FcnQyZD 2ssLqiYJLjejY/5J1kbzJwQCTiZGihco2tKZo4WjPb7Wx+MB3OIhjuy4gqQFgwZHHgL4 fgq1F7dz6nDGQbV7Jix7DzLID2oR9/PqLQteQBefzCkacrWYNFyh4Gtjz1LL4edGnvGi ma40tsX2OdVX8B+BVT56K8B8Gd6UmWD9j0FEVXZLyn9/gsWVpJax3pbp2/KvaKLFyadA Evuz2ZD42kRSE/QwAa6zMVewhUj8x/gvfoB4HPAOiLrz3eEQf0qtZkSjRE+vbvtsRRnZ 6EvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:from:subject; bh=0MaUzE6lyxbGFeS6BVwpSiZBh9qMGyafdVhp0fUr+bA=; b=XhUS51rsMPoeB6fejIQI+oOZCCTxppO16fd5TEglB3bi8Ue92kvQYiVWgj8mL22JNs 16BDW9i9ffxeHueQO0rQbAmiKW+ItkNU47nj5EEUcpq7g2Mz9Ee0gxU38NODXo0AllY3 oRo3FWY7qpFQsSinOl5/LLSEd9r7IhiQ/9UUbdN9eAN1yZWJmUItEjA4AjSYXjvoXYXA YND/5KySFBUHVMKRWTYn9j7Qf5blirVLPhILcv5qzqk0KbclQQy+6q8LlU1Sgc2VOMBN +9W+EbjrCdIvxVQtXL80Jeq19n0nPkoCFQnbCZqSj6AW4yIXyjfeJlbr+zFKWvSEVwUL j8QA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t9-20020a17090340c900b00178431e09bdsi6272566pld.230.2022.11.26.03.04.49; Sat, 26 Nov 2022 03:05:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229673AbiKZKqd (ORCPT + 84 others); Sat, 26 Nov 2022 05:46:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229670AbiKZKqP (ORCPT ); Sat, 26 Nov 2022 05:46:15 -0500 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8F2E14D38 for ; Sat, 26 Nov 2022 02:46:12 -0800 (PST) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4NK7fz2yCFzHw6Z; Sat, 26 Nov 2022 18:45:31 +0800 (CST) Received: from dggpemm500006.china.huawei.com (7.185.36.236) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 26 Nov 2022 18:46:10 +0800 Received: from [10.174.178.55] (10.174.178.55) by dggpemm500006.china.huawei.com (7.185.36.236) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Sat, 26 Nov 2022 18:46:10 +0800 Subject: Re: [PATCH] kernfs: fix potential null-ptr-deref in kernfs_path_from_node_locked() From: "Leizhen (ThunderTown)" To: Tejun Heo CC: Greg Kroah-Hartman , References: <20221123020419.1867-1-thunder.leizhen@huawei.com> <55041efe-7443-d576-287b-49d1221fced2@huawei.com> <164f759d-23b8-0a68-e68e-2f0a46318e94@huawei.com> Message-ID: <06b68b24-63f3-79d2-2d1e-d61516a84bfb@huawei.com> Date: Sat, 26 Nov 2022 18:46:09 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: <164f759d-23b8-0a68-e68e-2f0a46318e94@huawei.com> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.178.55] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpemm500006.china.huawei.com (7.185.36.236) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2022/11/26 17:49, Leizhen (ThunderTown) wrote: > > > On 2022/11/24 10:52, Leizhen (ThunderTown) wrote: >> >> >> On 2022/11/24 10:28, Leizhen (ThunderTown) wrote: >>> >>> >>> On 2022/11/24 10:24, Leizhen (ThunderTown) wrote: >>>> >>>> >>>> On 2022/11/24 0:55, Tejun Heo wrote: >>>>> On Wed, Nov 23, 2022 at 10:04:19AM +0800, Zhen Lei wrote: >>>>>> Ensure that the 'buf' is not empty before strlcpy() uses it. >>>>>> >>>>>> Commit bbe70e4e4211 ("fs: kernfs: Fix possible null-pointer dereferences >>>>>> in kernfs_path_from_node_locked()") first noticed this, but it didn't >>>>>> fix it completely. >>>>>> >>>>>> Fixes: 9f6df573a404 ("kernfs: Add API to generate relative kernfs path") >>>>>> Signed-off-by: Zhen Lei >>>>> >>>>> I think the right thing to do is removing that if. It makes no sense to call >>>>> that function with NULL buf and the fact that nobody reported crashes on >>>>> NULL buf indicates that we in fact never do. > > kernfs_path_from_node > -->kernfs_path_from_node_locked > > EXPORT_SYMBOL_GPL(kernfs_path_from_node) > > I've rethought it. The export APIs need to do null pointer check, right? Sorry,I looked at some of the other functions and they didn't check either. > >>>> >>>> OK. >>>> >>>> How about I remove "buf[0] = '\0';" too? It seems to be a useless operation. >>>> When 'kn_from' and 'kn_to' have a common ancestor, there must be a path from >>>> 'kn_from' to 'kn_to', and strlcpy() always fills in the terminator correctly, >>>> even if the buf is too small to save the first path node. >>> >>> Sorry, I misanalyzed. The length used by "len < buflen ? buflen - len : 0" may >>> be zero. >> >> Ah, my brain is unstable today. The initial value of len is 0. So "buf[0] = '\0';" >> can still be safely removed. >> >>> >>>> >>>> static void test(void) >>>> { >>>> char buf[4]; >>>> int i, n, buflen; >>>> >>>> buflen = 1; >>>> n = strlcpy(buf, "string", buflen); >>>> for (i = 0; i < buflen; i++) >>>> printk("%d: %02x\n", i, buf[i]); >>>> printk("n=%d\n\n", n); >>>> >>>> buflen = sizeof(buf); >>>> n = strlcpy(buf, "string", buflen); >>>> for (i = 0; i < buflen; i++) >>>> printk("%d: %02x\n", i, buf[i]); >>>> printk("n=%d\n", n); >>>> } >>>> >>>> Output: >>>> [ 33.691497] 0: 00 >>>> [ 33.691569] n=6 >>>> >>>> [ 33.691595] 0: 73 >>>> [ 33.691622] 1: 74 >>>> [ 33.691630] 2: 72 >>>> [ 33.691637] 3: 00 >>>> [ 33.691650] n=6 >>>> >>>>> >>>>> Thanks. >>>>> >>>> >>> >> > -- Regards, Zhen Lei