From patchwork Sat Aug 5 08:41:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Leizhen (ThunderTown)" X-Patchwork-Id: 131465 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp353992vqr; Sat, 5 Aug 2023 02:17:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGDP/zHdpYRP9B9BnBVzYiltcNnFNq+vym60LgEf81LmZsFPd+kAhrxaRi90GaCnbTHs97/ X-Received: by 2002:a05:6808:238f:b0:3a3:eab8:8c40 with SMTP id bp15-20020a056808238f00b003a3eab88c40mr6519350oib.54.1691227040827; Sat, 05 Aug 2023 02:17:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691227040; cv=none; d=google.com; s=arc-20160816; b=ZaEkfev05BnsbuBqoM0P5QHICVyI/4SdEzJ/ARCSVfNzR6J4Sp8Fut5ADWEU//utBp PKheuNapwCHSfIenQqK+1LsJyZ3dFyp7mKv2486PLXQwwhcokZh2/Di7fIx67676eNpM NqX7oqBjsjvbY6SEsRF28+ylPYuXhHAPQ+m21eWG4Yq80FCmi6gUnIt8DaXDLARVkjd1 nDdHZhENF3vQirZSx1HiYM7UyKdMiacCq4zeeh7+DgHnDdqTLfLvacmq5sMnLXHqWIjO xh/dWEhHSm4kuZ2oqyOC2ckep34JaV7Ow29qk7dEDfOgMs66wNINn/QxNepuyRbmT6xF XCMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ffc1QJn4kd0OufeWP8XeSXM7Nh/xMrxZJr5o/aXUFqw=; fh=IVsjub5GkLM/axoPUNNIOqPI7DryKyw7On15pcrOviE=; b=uY9fvtrsugQVfaieVJvOuKR++YVZ25LUAeFGeGjPkoczzMX9Si5cRS1QAXthTLnDvR F75afgApFVspn4C2YrwkUT1i13GT4sGAfElTxRoS8THPL4/EJtOu0ElW/Q94aIgpZg6T RLIywriia6b5lL3DveGLuXmQVkAdCWcZbCYT87J3M11BTpOGzD1/8LsxSKgzSBXxNMg8 +lGS26SB14YVuxTxgqoqhyuI25eSqbGn9EThIFpaaVDtlPnnmh5RnzCXztUh5OL+LYbL d/X81kepUkLAzsrEHL63Ofe50oVQABNE0r5KEG0aB0iwrqxlY872+c5T9G9znkBf+n9d HGWg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bg26-20020a056a02011a00b0056428dcea78si306528pgb.17.2023.08.05.02.17.07; Sat, 05 Aug 2023 02:17:20 -0700 (PDT) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229558AbjHEIl7 (ORCPT + 99 others); Sat, 5 Aug 2023 04:41:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbjHEIlu (ORCPT ); Sat, 5 Aug 2023 04:41:50 -0400 Received: from dggsgout12.his.huawei.com (unknown [45.249.212.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25CA444B8 for ; Sat, 5 Aug 2023 01:41:48 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTP id 4RHwzp1NHpz4f3l7H for ; Sat, 5 Aug 2023 16:41:42 +0800 (CST) Received: from huaweicloud.com (unknown [10.174.178.55]) by APP4 (Coremail) with SMTP id gCh0CgAHuKtGC85kcXBwPg--.1103S5; Sat, 05 Aug 2023 16:41:44 +0800 (CST) From: thunder.leizhen@huaweicloud.com To: Greg Kroah-Hartman , "Rafael J . Wysocki" , linux-kernel@vger.kernel.org Cc: Zhen Lei Subject: [PATCH 1/2] kobject: Add sanity check for kset->kobj.ktype in kset_register() Date: Sat, 5 Aug 2023 16:41:13 +0800 Message-Id: <20230805084114.1298-2-thunder.leizhen@huaweicloud.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230805084114.1298-1-thunder.leizhen@huaweicloud.com> References: <20230805084114.1298-1-thunder.leizhen@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgAHuKtGC85kcXBwPg--.1103S5 X-Coremail-Antispam: 1UD129KBjvJXoW7KrWDKFW3Cr47uw45Zw13Jwb_yoW8Xr1UpF 4akF13trWrCr47Zw4DJF4kCry5XF1kuF4DurWYgw1Fqr4rKFWqgw13tr1kZF43urWxXFW2 qF10yws0v3yDAaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9mb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6r1S6rWUM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUGw A2048vs2IY020Ec7CjxVAFwI0_Jrv_JF4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lw4CEc2x0rVAKj4xxMxAIw28IcxkI7VAKI48JMxC20s 026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_ JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14 v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xva j40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr 0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x07UtsqAUUUUU= X-CM-SenderInfo: hwkx0vthuozvpl2kv046kxt4xhlfz01xgou0bp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773380085854182996 X-GMAIL-MSGID: 1773380085854182996 From: Zhen Lei When I register a kset in the following way: static struct kset my_kset; kobject_set_name(&my_kset.kobj, "my_kset"); ret = kset_register(&my_kset); A null pointer dereference exception is occurred: [ 4453.568337] Unable to handle kernel NULL pointer dereference at \ virtual address 0000000000000028 ... ... [ 4453.810361] Call trace: [ 4453.813062] kobject_get_ownership+0xc/0x34 [ 4453.817493] kobject_add_internal+0x98/0x274 [ 4453.822005] kset_register+0x5c/0xb4 [ 4453.825820] my_kobj_init+0x44/0x1000 [my_kset] ... ... Because I didn't initialize my_kset.kobj.ktype. According to the description in Documentation/core-api/kobject.rst: - A ktype is the type of object that embeds a kobject. Every structure that embeds a kobject needs a corresponding ktype. So add sanity check to make sure kset->kobj.ktype is not NULL. Signed-off-by: Zhen Lei --- lib/kobject.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/kobject.c b/lib/kobject.c index 14e845209226ab8..72fa20f405f1520 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -861,6 +861,11 @@ int kset_register(struct kset *k) if (!k) return -EINVAL; + if (!k->kobj.ktype) { + pr_err("must have a ktype to be initialized properly!\n"); + return -EINVAL; + } + kset_init(k); err = kobject_add_internal(&k->kobj); if (err) { From patchwork Sat Aug 5 08:41:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Leizhen (ThunderTown)" X-Patchwork-Id: 131485 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp469110vqr; Sat, 5 Aug 2023 06:58:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF2Lt7lxwkBjF9saNXGnkPzLiaLLHDnBduD2IR+Exhq7BTmIUHKL906pWPvSyzHD9LaKANd X-Received: by 2002:a05:6a20:ce9d:b0:10f:be0:4dce with SMTP id if29-20020a056a20ce9d00b0010f0be04dcemr5023303pzb.8.1691243908318; Sat, 05 Aug 2023 06:58:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691243908; cv=none; d=google.com; s=arc-20160816; b=OA6p5kcSmSpqwuyX9CBuyPtLfiQrFuuA24nijnYkqLajb/aDKxHGHIDyPHyV/Xg2aM Z1kwJ1HFRGq3Dz8i7EPXQtoImgni44KA9LPnARHLbjSt+v5YyJRKelv0nQgluEqwy2pr jQNHEE+5/Eao//Ke0T9Wq6GaZr7NeJlHp5t9R4tMUGS4RUfo1HsF/wpL3zMM18+8T8Pk HVgFmpw57F6a4jOXMGygIrItbreUt9/Er6EaM4mOZj5H/zGdZiyZhRFAhQoRKobgYYdE YYPl3CAIpcjZFfD6rbURLS7NKV6EWUvs+usH1Uv0tUgTQIb4yNQYdt8u+6HIVsMtlP25 hlzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=eF5Y5WFjtWR60batowSvHLvbH816QnJjLA+IHOLKg3c=; fh=IVsjub5GkLM/axoPUNNIOqPI7DryKyw7On15pcrOviE=; b=09qVOhqCNtAB3O9qkn/pOAgta0SW1GLbSjvGqFXy1mHzXKzWKVWjGL4xLGH/nnMw0L D6UFViKS5czLislzhEnDhes99TqPWQQtWu39vShaaYPt75Y95Atd3XQwgC8fwt8rmpkX sVHFMwQ92atdfDG28fmo4x4x/TGTd1LbyQCUN1sq0UfYtTCaWb3cSjhJ3+PeyrAXVvqq 4q2D8DWJgno8YiCoXuRuGkfN59Y94/y9iHv+2EcGL75oS5X0OWQLJpcY3dpdrWXpuvCi MtYooj2HkD7VpdWIIhPNK7XEnpHceXwa1cLqUDJiCdqIZsTGZO+rOQ/oFpiZ6uX2Od0h zrgg== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s26-20020a63525a000000b0054201a3f08dsi3179773pgl.592.2023.08.05.06.58.04; Sat, 05 Aug 2023 06:58:28 -0700 (PDT) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229479AbjHEIlw (ORCPT + 99 others); Sat, 5 Aug 2023 04:41:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229530AbjHEIlu (ORCPT ); Sat, 5 Aug 2023 04:41:50 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9850E468C for ; Sat, 5 Aug 2023 01:41:48 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4RHwzr63xbz4f3lXJ for ; Sat, 5 Aug 2023 16:41:44 +0800 (CST) Received: from huaweicloud.com (unknown [10.174.178.55]) by APP4 (Coremail) with SMTP id gCh0CgAHuKtGC85kcXBwPg--.1103S6; Sat, 05 Aug 2023 16:41:45 +0800 (CST) From: thunder.leizhen@huaweicloud.com To: Greg Kroah-Hartman , "Rafael J . Wysocki" , linux-kernel@vger.kernel.org Cc: Zhen Lei Subject: [PATCH 2/2] kobject: Remove redundant checks for whether ktype is NULL Date: Sat, 5 Aug 2023 16:41:14 +0800 Message-Id: <20230805084114.1298-3-thunder.leizhen@huaweicloud.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230805084114.1298-1-thunder.leizhen@huaweicloud.com> References: <20230805084114.1298-1-thunder.leizhen@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: gCh0CgAHuKtGC85kcXBwPg--.1103S6 X-Coremail-Antispam: 1UD129KBjvJXoWxur15ZF48GFyUXw18Jw4xZwb_yoW5GF4kpr sxCr9rKFW8Jr4xGwnxAan5Wr10yrn5K398Ca4F9wnavw48Cr90qrWUtFyFvFy5JFWfXrW2 qa1UKas8tw1UKaUanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUU9mb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6r1S6rWUM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUXw A2048vs2IY020Ec7CjxVAFwI0_JFI_Gr1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40Ex7xfMc Ij6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_ Jr0_Gr1lF7xvr2IYc2Ij64vIr41lw4CEc2x0rVAKj4xxMxAIw28IcxkI7VAKI48JMxC20s 026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_ JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14 v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xva j40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8Jr 0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x07jjksgUUUUU= X-CM-SenderInfo: hwkx0vthuozvpl2kv046kxt4xhlfz01xgou0bp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,KHOP_HELO_FCRDNS, SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773397772408597077 X-GMAIL-MSGID: 1773397772408597077 From: Zhen Lei When adding koject or kset, we have made sure that ktype cannot be NULL. Therefore, after adding koject or kset, there is no need to worry about ktype being NULL. Clear all ktype-related redundancy checks. Signed-off-by: Zhen Lei --- lib/kobject.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/kobject.c b/lib/kobject.c index 72fa20f405f1520..59dbcbdb1c916d9 100644 --- a/lib/kobject.c +++ b/lib/kobject.c @@ -74,12 +74,10 @@ static int create_dir(struct kobject *kobj) if (error) return error; - if (ktype) { - error = sysfs_create_groups(kobj, ktype->default_groups); - if (error) { - sysfs_remove_dir(kobj); - return error; - } + error = sysfs_create_groups(kobj, ktype->default_groups); + if (error) { + sysfs_remove_dir(kobj); + return error; } /* @@ -591,8 +589,7 @@ static void __kobject_del(struct kobject *kobj) sd = kobj->sd; ktype = get_ktype(kobj); - if (ktype) - sysfs_remove_groups(kobj, ktype->default_groups); + sysfs_remove_groups(kobj, ktype->default_groups); /* send "remove" if the caller did not do it but sent "add" */ if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) { @@ -669,10 +666,6 @@ static void kobject_cleanup(struct kobject *kobj) pr_debug("'%s' (%p): %s, parent %p\n", kobject_name(kobj), kobj, __func__, kobj->parent); - if (t && !t->release) - pr_debug("'%s' (%p): does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n", - kobject_name(kobj), kobj); - /* remove from sysfs if the caller did not do it */ if (kobj->state_in_sysfs) { pr_debug("'%s' (%p): auto cleanup kobject_del\n", @@ -683,10 +676,13 @@ static void kobject_cleanup(struct kobject *kobj) parent = NULL; } - if (t && t->release) { + if (t->release) { pr_debug("'%s' (%p): calling ktype release\n", kobject_name(kobj), kobj); t->release(kobj); + } else { + pr_debug("'%s' (%p): does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.\n", + kobject_name(kobj), kobj); } /* free name if we allocated it */ @@ -1060,7 +1056,7 @@ const struct kobj_ns_type_operations *kobj_child_ns_ops(const struct kobject *pa { const struct kobj_ns_type_operations *ops = NULL; - if (parent && parent->ktype && parent->ktype->child_ns_type) + if (parent && parent->ktype->child_ns_type) ops = parent->ktype->child_ns_type(parent); return ops;