Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1456860imm; Fri, 15 Jun 2018 18:29:57 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJKPy15KHu/S3rk797+Jf+gKpFqh51C1ccAggjaKA533pz0hABueo6ZBRazsLSjo9NTgvcl X-Received: by 2002:a65:6148:: with SMTP id o8-v6mr3589587pgv.93.1529112597348; Fri, 15 Jun 2018 18:29:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529112597; cv=none; d=google.com; s=arc-20160816; b=gf3KFAjI3aFQVQiIhNGKjHapqHwNLmo8zKegtLEnVbTafqEPoPmfUJ8nbT0o+XfoEw 6jN776AeL0HxmujrNEG0VQdILNYMwRKHyNMqR0VQuxtwHlIAdpfX1Baxi1a5w42JuwTG MNIPEYrY/C2+QYwZ1NXc7acUlMP1irY8L/ilJA63lBDpyS9qqkewJ8y9H4lKVxvIhg5X cw/c15m+wRRtyX/3CVrIYPRFxqT8vEW5jDwX7HNKegFep+chAwXp/rEytpr1FSVwYZEQ xmbXKZQvAKoOs6F98wmO3YGBqbEPNt2VBjAiCT6RQ93JNqht2EHMTvxHy3VqByoyXy1e v9gA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:message-id:date :mime-version:dkim-signature:arc-authentication-results; bh=1LR+ZVtfUkbb935nF86L0EErFw88+Q1SWc2lo40IwH0=; b=XFCX/C+BpV/o75YC1CHfEAu6ihjfWjqUUhJkfll9rZrVrV0oZpGY+rTwBQh/ze7V6A J8U0xUregqu4C4AB6WzTbAuII+3KEWx+KijPaM3W8+XLyV5MWvZmJr0AtHhG/oVzglWp MZNOL7hB6Ev4ALcah4/MiX56cn+O4zAvz3Ponj5GmLqrGp0TweKaflP7tLiEmooflNon g8r7Pcar8w4lmXHvtZFUibC2mBU/ts7c2N/jl4V6SZd447e+qeMHxiKH++JkvJpXXcMS jLju+VjQnebk8Re5ed/ZwD9vxwnpkG4c8G1X1rL+c4fvRifNlIpfxk+7Ctq1JAxlUeJP PznQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=U6t3tG2Y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k8-v6si9556163plt.410.2018.06.15.18.29.42; Fri, 15 Jun 2018 18:29:57 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=U6t3tG2Y; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756675AbeFPB3R (ORCPT + 99 others); Fri, 15 Jun 2018 21:29:17 -0400 Received: from mail-io0-f201.google.com ([209.85.223.201]:50517 "EHLO mail-io0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753879AbeFPB3Q (ORCPT ); Fri, 15 Jun 2018 21:29:16 -0400 Received: by mail-io0-f201.google.com with SMTP id n15-v6so8569581ioc.17 for ; Fri, 15 Jun 2018 18:29:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:message-id:subject:from:to:cc; bh=1LR+ZVtfUkbb935nF86L0EErFw88+Q1SWc2lo40IwH0=; b=U6t3tG2YhDsj8/VbnEVDemjOGClwPEHN7OeaFNxhhck44Vv+Fv/y3ov8jjr2JFbQZz JSnriWvRjT89b486BkQyUc+mFluxJ5C9wPhJz0mt/UG3CZjVTLgJ1cEXr6TFoNEiHJMy Kx+bOYs/eo28oFJnwFQmdOPlzyXR2JEDQJcwNPIQPmrKYVFBY9qUWyCupMCfvVacEyQR Jl8xayxnHZjHqkXxeYxOaVNkBkFJrjrzg+jLqelDgkc5mb4fyBkzoIzQ+s+pEjJDiAH1 iaaLcXdpa83VueSnLwqitbRmslMNe1tgztFe3N2FRQThT68/c5Q+ROmzC1/WaEINXWRI 00yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:date:message-id:subject:from:to:cc; bh=1LR+ZVtfUkbb935nF86L0EErFw88+Q1SWc2lo40IwH0=; b=KL4NY2nDmpVWgbAbYVDu9+BvnHuDfyl14wCKMKuI/hcmSMKFvJroouNqhiItEmuV+G L9vlVXlLqZxFa18iJ3Y12noRZoahALzukRW1X3XFY81RNT+LtROtW14JAN099BHm7tO2 /PVKlc8cRSLQ+IZnASBecJ+Y+CtKVfQS6pgXje+OU2NKgYjq3KLtKYUEAmJ0HBDdZ+c9 uQgYUdB0Y860q21kuJQG3uImFXNbAGd2U3X82dg7luJxw38ARbMArZ+lp531/eOzFnhj u3fXUd/fIM3SjWpZ2fe/snU2utlBE4zpBKvVGopSvMT4U4lcgMb3Mc0mvIvszGfbRm1G YHIw== X-Gm-Message-State: APt69E0Cr6pWbr9TShvbLwfkQhz03+UIkSZyDrj2U0pT4BgSbvmhxZYV 3pXWY4wqPDTBi+05VmtMdcEK8ao8RnyY MIME-Version: 1.0 X-Received: by 2002:a24:1609:: with SMTP id a9-v6mr1492797ita.0.1529112555499; Fri, 15 Jun 2018 18:29:15 -0700 (PDT) Date: Fri, 15 Jun 2018 18:29:10 -0700 Message-Id: <20180616012910.152694-1-rajatja@google.com> X-Mailer: git-send-email 2.18.0.rc1.244.gcf134e6275-goog Subject: [PATCH] sysfs: Fix internal_create_group() for named group updates From: Rajat Jain To: Greg Kroah-Hartman , linux-kernel@vger.kernel.org Cc: Rajat Jain , rajatxjain@gmail.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are a couple of problems with named group updates in the code today: * sysfs_update_group() will always fail for a named group, because internal_create_group() will try to create a new sysfs directory unconditionally, which will ofcourse fail with -EEXIST. * We can leak the kernfs_node for grp->name if some one tries to: - rename a group (change grp->name), or - update a named group, to an unnamed group It appears that the whole purpose of sysfs_update_group() was to allow changing the permissions or visibility of attributes and not the names. So make it clear in the comments, and allow it to update an existing named group. Signed-off-by: Rajat Jain --- fs/sysfs/group.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c index 4802ec0e1e3a..8bd10dc730ae 100644 --- a/fs/sysfs/group.c +++ b/fs/sysfs/group.c @@ -119,12 +119,23 @@ static int internal_create_group(struct kobject *kobj, int update, return -EINVAL; } if (grp->name) { - kn = kernfs_create_dir(kobj->sd, grp->name, - S_IRWXU | S_IRUGO | S_IXUGO, kobj); - if (IS_ERR(kn)) { - if (PTR_ERR(kn) == -EEXIST) - sysfs_warn_dup(kobj->sd, grp->name); - return PTR_ERR(kn); + if (update) { + kn = kernfs_find_and_get(kobj->sd, grp->name); + if (!kn) { + WARN(1, + "Can't update unknown attr grp name: %s/%s\n", + kobj->name, grp->name); + return -EINVAL; + } + } else { + kn = kernfs_create_dir(kobj->sd, grp->name, + S_IRWXU | S_IRUGO | S_IXUGO, + kobj); + if (IS_ERR(kn)) { + if (PTR_ERR(kn) == -EEXIST) + sysfs_warn_dup(kobj->sd, grp->name); + return PTR_ERR(kn); + } } } else kn = kobj->sd; @@ -199,7 +210,8 @@ EXPORT_SYMBOL_GPL(sysfs_create_groups); * of the attribute files being created already exist. Furthermore, * if the visibility of the files has changed through the is_visible() * callback, it will update the permissions and add or remove the - * relevant files. + * relevant files. Changing a group's name (subdirectory name under + * kobj's directory in sysfs) is not allowed. * * The primary use for this function is to call it after making a change * that affects group visibility. -- 2.18.0.rc1.244.gcf134e6275-goog