Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2160423imm; Sat, 16 Jun 2018 10:50:48 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIRAAGZmPaA82Gfjiwc798cq0TfzQFg4Fz7Zi5ECKFYzafNhODSjFlgKq/OJqnF6vSadQzG X-Received: by 2002:a63:a319:: with SMTP id s25-v6mr5841424pge.62.1529171448451; Sat, 16 Jun 2018 10:50:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529171448; cv=none; d=google.com; s=arc-20160816; b=lp8uVoNVAWngGOz9rANAmyWE04+aO/ARU+25MOkbSOSuio1fA7n5bqgboe3D9Eaqhu RTqLdyNYaZVaptKawFORwN8/5Lhcnhb7yN28bgrE7OiMM//KJwwJ/aULG/H+Q4VjjtPf 9ltMNkCoGNrglMec0aGWSdGUby+v/rjGXxb+DvdCiXidoQsjY85PfU6llGsBcXYb3s+9 WmQ/3/i4/3DcG6XGtw0/GZ31lUibCVtdliVQNhX7BsVSFe5btsgvNGD1C4KyJdBwtW2O w13ORgHAMId4zT0+8WB4pMdKyna6KHPqdT3FpBPLqiwVHJ7hs+FpqYCGv/vW4XIxXc69 d8Bw== 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:references:message-id :in-reply-to:date:mime-version:dkim-signature :arc-authentication-results; bh=G9SwumVn9dGJhv55WcLrC5eA4b3IA8ok87ZlbiK/fYQ=; b=OAoIVuCnlkKKbdWwMa+fL62I0Vh6wy6MZGJfJ6Z8p8Oz09JJX2fNax+o1uTxs7bYj3 JDV3FRCLfamotIBIaHP/Ahb/bLVBU+cKA7uc9ZxM4E9jDvH0OikCo8IZWyeOdGf4u8Fg kFc6K96Ar/LonsemSYZQkf3xtt6+LoL4Iurmkxzf2bXmjZ4oYeD9IlcXnoiM8GcOrSaz pnhpdSVh8W1M5cgRTKQfv/vUMERi9WNPKxJbT1jq9ijlxjItT352UEgICqI13QS9qrLw v2vmcLx4hTbNMjSjakMemofhM8eLoqHqK1fgOiLcvApaGL2TZt4roWxGbOme9fNwMXDV 6Xqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=HjnN9QqP; 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 w3-v6si8767398pgc.645.2018.06.16.10.50.34; Sat, 16 Jun 2018 10:50:48 -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=HjnN9QqP; 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 S1754118AbeFPRtw (ORCPT + 99 others); Sat, 16 Jun 2018 13:49:52 -0400 Received: from mail-qt0-f202.google.com ([209.85.216.202]:52849 "EHLO mail-qt0-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752587AbeFPRtv (ORCPT ); Sat, 16 Jun 2018 13:49:51 -0400 Received: by mail-qt0-f202.google.com with SMTP id m4-v6so10299945qtn.19 for ; Sat, 16 Jun 2018 10:49:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:date:in-reply-to:message-id:references:subject:from:to :cc; bh=G9SwumVn9dGJhv55WcLrC5eA4b3IA8ok87ZlbiK/fYQ=; b=HjnN9QqP7iEwPyT1NgklepQCyJF0Yrgpf4VA/6PfwFAilSnt+jDqjShifWkoZHon8k 4NAzUxuN6veAszPa2ZrXkCwtetX3ymU57tcj1RxexAPQtspp3Ho7BTQ2wc3a6kmSQQSV wyEmeovuWhEpnTAYr97sshbzqsmaOQLrt+JyapAjhX7BDdnuX41OCyRlQj2M/Bm11rS7 Y1ySDmywCdTfNcEGFon382ltlpF43ckv2kPgjJTEPQ+B51Ir4sWOzbIRNL4CcCbDV7D+ nE9oC4uH0qZ40QlJFyHL9p3f7p+C49DBP8nm5kgdYJM45Y/6F3FZKsWBVXs0N+l3+hGi ahYw== 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:in-reply-to:message-id :references:subject:from:to:cc; bh=G9SwumVn9dGJhv55WcLrC5eA4b3IA8ok87ZlbiK/fYQ=; b=kVoXraUcPjzCzOiFvlJJWkYXBkDqfwF3Vp5VGtGr7bx4MCAkRN1/epvRZiv0GkPQbU m3ZJJB91t+d2UmGxP6K2b/rrKerR2bKNOR0C0othHBF05Jsq4E8lYTmrZh2vTywdn253 891Olpu40VOB8qDoP1y3p+H170VmVpZqGjuwzfUnL9XNZX8bMqSpX4W/z/auNcZSmAoQ noiN8TIuguSthz0wTYo45lqQujzdPLX6RdYRIayc+tTQ+knEIWnSMf1T8lGhVSldGA3F 8MfdRmhIWme+LG4VGsu3in1SvGUWk/fdBRyN+SYuuR+IPEiNe++7UtSI0zMj2YjkkKJ9 f7mw== X-Gm-Message-State: APt69E2L+de8pJfdvTsYce8opkQ8H53nvOGvpc8XBv4AXUzBoRVxnfvK kOvMXkbpqMKN3VvEZXnqeYRWYefPPIvN MIME-Version: 1.0 X-Received: by 2002:ac8:3558:: with SMTP id z24-v6mr3365937qtb.21.1529171390859; Sat, 16 Jun 2018 10:49:50 -0700 (PDT) Date: Sat, 16 Jun 2018 10:49:46 -0700 In-Reply-To: <20180616083740.GA23039@kroah.com> Message-Id: <20180616174946.175109-1-rajatja@google.com> References: <20180616083740.GA23039@kroah.com> X-Mailer: git-send-email 2.18.0.rc1.244.gcf134e6275-goog Subject: [PATCH v3] 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 --- v2: Use pr_warn() instead of WARN() v3: drop the extra reference taken by kernfs_find_and_get() fs/sysfs/group.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c index 4802ec0e1e3a..38240410f831 100644 --- a/fs/sysfs/group.c +++ b/fs/sysfs/group.c @@ -119,12 +119,22 @@ 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) { + pr_warn("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; @@ -135,6 +145,10 @@ static int internal_create_group(struct kobject *kobj, int update, kernfs_remove(kn); } kernfs_put(kn); + + if (grp->name && update) + kernfs_put(kn); + return error; } @@ -199,7 +213,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