Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1964436pxb; Thu, 11 Feb 2021 00:10:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJwIQ+kJAqPSPmqWXehVvysuFZlo4jcOBf1cwv/biPaBsIETdWtIzVTqiNBAha2aD4MUtxks X-Received: by 2002:a17:906:fc5:: with SMTP id c5mr6989683ejk.538.1613031052074; Thu, 11 Feb 2021 00:10:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613031052; cv=none; d=google.com; s=arc-20160816; b=TgernVObZ8bCwMlNLxXibGeO9BxFbSY6k6T9FCZbynzJWAntCZQBpcruPpvmg+e169 MXz/eG/RNq0b4A7jcewB669iyBHPoWnkNs66u2rE9yioeoY5X4z3EV0BB/RJEXymP4za p4QoW6LkrvvCUGngJSAT5X7KOkGfd5PmfZyxisO7EFaCICmuxQ3hF/S4ixyPYoVN+kGT fogU8OYB0x7OqVrGEETaOZINUr5gjzn+nNNhFmeL3A7R6/LD2r5SzO28+kqFRpQ/hoXL jjFVPfH2Uho6/jKYgBRRXb+y0p1XDGhw0HE1Re9Cx5WnThsHDkLw9GAywzUVtAWIcDs3 wTSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=SNAwIXjIQ9eB3Qc3bo7k/zXUOGRML/0pvKDnfyOi4gM=; b=0gve/eF17VSnkFFgrW/ZbuxzPmK4tSYzp/d7FrP1Pw31tS7FhvZokqsUJeQPksCtQ/ Z5HOWwoO+0QSVwkESeZUG0qFEjoGvCKKtY+dO7wDTp2r3wBmucp0Gud/JRv/PPsuktb6 wl3CJ1kxlXXlB0ZiCV7NA6YCm8K14kzb/AIq5awq3DFqwSj33+GBSyv68sDElRJZ1l9w azOoFI7YddYjltnbZwHQX2fV7WOKUdskh9P+dd+/IzHSdRa8eJLrNy1b3VoZEohzova1 kSYZnNVV7JUo2zcRG/KVfKNquiVNZo1oBpK/XEL1MwTy/PdoLTqTggH2EE1EGohofUWp 7M9Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z23si3892403edr.483.2021.02.11.00.10.28; Thu, 11 Feb 2021 00:10:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229517AbhBKIJ7 (ORCPT + 99 others); Thu, 11 Feb 2021 03:09:59 -0500 Received: from mail-oi1-f179.google.com ([209.85.167.179]:44236 "EHLO mail-oi1-f179.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229469AbhBKIJz (ORCPT ); Thu, 11 Feb 2021 03:09:55 -0500 Received: by mail-oi1-f179.google.com with SMTP id r75so5151679oie.11; Thu, 11 Feb 2021 00:09:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SNAwIXjIQ9eB3Qc3bo7k/zXUOGRML/0pvKDnfyOi4gM=; b=Pf9SLwNO8ILxdDr54amsau/Tl199tPukwlsdTa2jfatioTKUNjF/oQimNUt6vD/Tbk HGCo67wtlle6vgbcOKxu3VB4To77DlASidu+P73x+o9pL1+v3ZBzdpwyRWlKC/yBjVjE 0uSeWRdjPrWw/dohccq5cf90PfxXwwJqjeiMBxJuRA1E9GaQTX3URF0eQzIzuVJW34Cn 6ZEvufQzpZ0mOMqCkt3BH41YotwjDdGsd+qG3wEFsQnvhTqHJde/e1BOrAxfyUmxoHSC HHbbmBhKDyWaUJSL8tPBM3LrCSqq2RHCofygFdCPDhZzDAZCShBN19OddGotrPG8lp8t FXvA== X-Gm-Message-State: AOAM533kHUoDEcWci/J2/yYRIFFzb5vy8Jp4uT7VWcqHQSscXsvSXQAC hDbjP4wfjRpXjDoPSzhgDmhRvF8XQV3vqKPCSek= X-Received: by 2002:aca:d8c6:: with SMTP id p189mr1985706oig.54.1613030954688; Thu, 11 Feb 2021 00:09:14 -0800 (PST) MIME-Version: 1.0 References: <20210210222851.232374-1-drew@beagleboard.org> <20210210222851.232374-3-drew@beagleboard.org> In-Reply-To: <20210210222851.232374-3-drew@beagleboard.org> From: Geert Uytterhoeven Date: Thu, 11 Feb 2021 09:09:03 +0100 Message-ID: Subject: Re: [PATCH v4 2/2] pinctrl: pinmux: Add pinmux-select debugfs file To: Drew Fustini Cc: Linus Walleij , "open list:GPIO SUBSYSTEM" , Linux Kernel Mailing List , Tony Lindgren , Andy Shevchenko , Alexandre Belloni , Pantelis Antoniou , Jason Kridner , Robert Nelson , Joe Perches , Dan Carpenter Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Drew, On Wed, Feb 10, 2021 at 11:33 PM Drew Fustini wrote: > Add "pinmux-select" to debugfs which will activate a function and group > when "" are written to the file. The write > operation pinmux_select() handles this by checking that the names map to > valid selectors and then calling ops->set_mux(). > > The existing "pinmux-functions" debugfs file lists the pin functions > registered for the pin controller. For example: > > function: pinmux-uart0, groups = [ pinmux-uart0-pins ] > function: pinmux-mmc0, groups = [ pinmux-mmc0-pins ] > function: pinmux-mmc1, groups = [ pinmux-mmc1-pins ] > function: pinmux-i2c0, groups = [ pinmux-i2c0-pins ] > function: pinmux-i2c1, groups = [ pinmux-i2c1-pins ] > function: pinmux-spi1, groups = [ pinmux-spi1-pins ] > > To activate function pinmux-i2c1 and group pinmux-i2c1-pins: > > echo "pinmux-i2c1 pinmux-i2c1-pins" > pinmux-select > > Signed-off-by: Drew Fustini Thanks for your patch! > --- a/drivers/pinctrl/pinmux.c > +++ b/drivers/pinctrl/pinmux.c > @@ -673,6 +673,111 @@ void pinmux_show_setting(struct seq_file *s, > DEFINE_SHOW_ATTRIBUTE(pinmux_functions); > DEFINE_SHOW_ATTRIBUTE(pinmux_pins); > > +#define PINMUX_MAX_NAME 64 > +static ssize_t pinmux_select(struct file *file, const char __user *user_buf, > + size_t len, loff_t *ppos) > +{ > + struct seq_file *sfile = file->private_data; > + struct pinctrl_dev *pctldev = sfile->private; > + const struct pinmux_ops *pmxops = pctldev->desc->pmxops; > + const char *const *groups; > + char *buf, *fname, *gname; > + unsigned int num_groups; > + int fsel, gsel, ret; > + > + if (len > (PINMUX_MAX_NAME * 2)) { > + dev_err(pctldev->dev, "write too big for buffer"); > + return -EINVAL; > + } > + > + buf = kzalloc(PINMUX_MAX_NAME * 2, GFP_KERNEL); > + if (!buf) > + return -ENOMEM; > + > + fname = kzalloc(PINMUX_MAX_NAME, GFP_KERNEL); > + if (!fname) { > + ret = -ENOMEM; > + goto free_buf; > + } > + > + gname = kzalloc(PINMUX_MAX_NAME, GFP_KERNEL); > + if (!buf) { > + ret = -ENOMEM; > + goto free_fname; > + } > + > + ret = strncpy_from_user(buf, user_buf, PINMUX_MAX_NAME * 2); While this guarantees buf is not overflowed... > + if (ret < 0) { > + dev_err(pctldev->dev, "failed to copy buffer from userspace"); > + goto free_gname; > + } > + buf[len-1] = '\0'; > + > + ret = sscanf(buf, "%s %s", fname, gname); ... one of the two strings can still be longer than PINMUX_MAX_NAME, thus overflowing fname or gname. As buf is already a copy, it may be easier to just find the strings in buf, write the NUL terminators into buf, and set up fname and gname to point to the strings inside buf. > + if (ret != 2) { > + dev_err(pctldev->dev, "expected format: "); > + goto free_gname; > + } > +static const struct file_operations pinmux_select_ops = { > + .owner = THIS_MODULE, > + .open = pinmux_select_open, > + .read = seq_read, I don't think you need to fill in .read for a write-only file. > + .write = pinmux_select, > + .llseek = no_llseek, > + .release = single_release, > +}; > + > void pinmux_init_device_debugfs(struct dentry *devroot, > struct pinctrl_dev *pctldev) > { Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds