Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1714129imm; Sat, 16 Jun 2018 01:19:37 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKrx6lhQQoNVPDptbVtKcK/W9IM5xTfM6wfaEmbrmWLKVb4mBp4Dju7wqWviLNvfVjXByrV X-Received: by 2002:a62:221a:: with SMTP id i26-v6mr5522356pfi.240.1529137177200; Sat, 16 Jun 2018 01:19:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529137177; cv=none; d=google.com; s=arc-20160816; b=gb5zQnq6n8bxje7XQxzNVtGX0yfwn72WiSyVy5pIhkCKAElcKFZNwnqteALusK+sV3 wMPSx0otuV9AjMQlwAT1mlCw87o8HBmQpb3hQQAwf0FU14icYDt+JQjeWUpv8IciLwwB oUN/t/nvcKIEq/qtVFbEg4xAXRpuMCXwVYa38h/50Z+14A1uGlAmkM3J+TL7CO6ZahIp b+OxzuPt6c/olJWpzeHytN59+NclNpaJiXFyKoK34FpX6T/qWQscQKCad/xKWxDb54nd lN40IkrL2ZTHieihqdUUITRAX2Na3Ru3XVsARW7uRed1NF/YfJ45UVWp6+Ddq43R8hYi +cag== 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=T3SBQtHO/b26sBIquivD1pc77kYIMRAxo1vAdKNF47Y=; b=UcULDkhZxwdZPz8KZQDiWU4nvGK+kv9GrdmA4XToplhqteW4A/o9slN2AQOOdHZACk sQJARHQRwgGV3YclG3u1yuV5MW3Csh203x7NJBlhWkCV/BLQpH5M3YDOuMkuDQ86RyVB zUVRBekYCQ5wTCoRgD2UnC9i0+v7moTS1RnFhE6Fzvr+z5J0Q5voFWZPxpNLLCt0RtL1 h2/VNzubBxZ1GO5RvDOZwaTXCUBH4WCQhE0QRfvaOjQiNKuAvJId8tJ5Ltl0SlnXgIUz /2gKlM+K1NDvLKwbKVqcXn5QhzEQM22PxFDz/gY+QrmvXXK2Cx8w6GLRtP6T1v7J1Xib 48zg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=aeQFXalv; 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 f7-v6si11474158plj.122.2018.06.16.01.19.20; Sat, 16 Jun 2018 01:19:37 -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=aeQFXalv; 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 S932459AbeFPISy (ORCPT + 99 others); Sat, 16 Jun 2018 04:18:54 -0400 Received: from mail-ua0-f202.google.com ([209.85.217.202]:46936 "EHLO mail-ua0-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932188AbeFPISw (ORCPT ); Sat, 16 Jun 2018 04:18:52 -0400 Received: by mail-ua0-f202.google.com with SMTP id m1-v6so4071026uao.13 for ; Sat, 16 Jun 2018 01:18:52 -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=T3SBQtHO/b26sBIquivD1pc77kYIMRAxo1vAdKNF47Y=; b=aeQFXalvppdUHFoUFqz4aDpfbzqnY3R10l6rnQWksAtjMbWc6tyeyVSanusBhVy+LG 4U+6Pkp11VSR1CURZE6p6GFlgMsNnQ5B2b1RRkSrcSp7jlXzZ/stLV7rBoU+mDWHsIVd QPLx+35CgcsdPkZsdntdUdILeWVOj8GLN06hQamyuNSakG9AZ42wE84Q3rrnAfQsylBS p6Hs13BH/07l9/6uFNxySGoz4iSRZyRE1csgU/mpJ0FqcdJNXwCG8tVe79u86kURhmep Ge3D7ulSciOPxAi9VnCe6jXIg7NgqdPVXv9O97NLrrVoHGFShFtFqN1tAHGop3n3KZJF fE+w== 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=T3SBQtHO/b26sBIquivD1pc77kYIMRAxo1vAdKNF47Y=; b=Fa1WJQKIavwr4IBWf58ULQ8ZBW+H4UywOl8XbaBoP7rvg9NQMOyxyt9KNib9rTr9UZ 55hKSpvhgyC1rsYB78DLIBxR61ck/iChuIWRet9MuvnxIdShasx+oDz6hmyGZcdCNoZm nhwCPwY3MQrFMaZGvqINrWKNtku0c/IopZipX/OqGD1gDQ2u6vcgIEX7QYfKtkgPIumN dLwJ+GD98Cgsdnq6nEnAvq9lJjmYLguIhgstUFaotFiFak461kmbIGCyRttSplbRmh2T 7HZ3xKVROP2XVKp7GabUSenf2msvXXtQMycC9dUu6qzy0HUSNsAvK1aw697hg3w5pcEv 4shg== X-Gm-Message-State: APt69E3AX8SZVlralqvTKs3cDea2fXkhAS5ZYHX7C3wRIhjc6jEy5UA/ tlSZ1xBD1sp/0mWyT1BRxsCFHtTUY3gZ MIME-Version: 1.0 X-Received: by 2002:ab0:4741:: with SMTP id i1-v6mr2042028uac.116.1529137131853; Sat, 16 Jun 2018 01:18:51 -0700 (PDT) Date: Sat, 16 Jun 2018 01:18:37 -0700 In-Reply-To: Message-Id: <20180616081837.62730-1-rajatja@google.com> References: X-Mailer: git-send-email 2.18.0.rc1.244.gcf134e6275-goog Subject: [PATCH v2] 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() fs/sysfs/group.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c index 4802ec0e1e3a..2b7d3536e6cb 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; @@ -199,7 +209,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