All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	kernel@savoirfairelinux.com,
	"David S. Miller" <davem@davemloft.net>,
	Florian Fainelli <f.fainelli@gmail.com>,
	Andrew Lunn <andrew@lunn.ch>,
	Egil Hjelmeland <privat@egil-hjelmeland.no>,
	John Crispin <john@phrozen.org>,
	Woojung Huh <Woojung.Huh@microchip.com>,
	Sean Wang <sean.wang@mediatek.com>,
	Nikita Yushchenko <nikita.yoush@cogentembedded.com>,
	Chris Healy <cphealy@gmail.com>
Subject: Re: [PATCH net-next v2 02/10] net: dsa: debugfs: add tree
Date: Fri, 8 Sep 2017 17:29:23 +0200	[thread overview]
Message-ID: <20170908152923.GA19672@kroah.com> (raw)
In-Reply-To: <87k21944rq.fsf@weeman.i-did-not-set--mail-host-address--so-tickle-me>

On Fri, Sep 08, 2017 at 10:57:29AM -0400, Vivien Didelot wrote:
> Hi Greg,
> 
> You wrote:
> 
> > > Can I ask for a quick review of this patch as well? It's the one adding
> > > the boilerplate for a single debugfs file, and I'm pretty sure it can be
> > > reduced somehow.
> > 
> > I don't see a patch here :(
> 
> Oops, you weren't originally in Cc. Please find the patch below.
> 
> > > Also more important, you will notice what seems to be a bug to me:
> > > I can read or write a file even if I didn't mask the corresponding mode
> > > hence the double check in dsa_debugfs_show and dsa_debugfs_write.
> > 
> > The mode can be changed by userspace, you shouldn't ever need to check
> > it in any debugfs calls, right?
> 
> Correct. But this happens even if the file mode isn't changed by
> userspace in the meantime, which seemed weird to me. e.g. echo
> redirected to a -r--r--r-- debugfs entry will call dsa_debugfs_write.
> 
> 
> Thanks,
> 
>         Vivien
> 
> 
> ------ Beginning of the patch ------
> 
> This commit adds the boiler plate to create a DSA related debug
> filesystem entry as well as a "tree" file, containing the tree index.
> 
>     # cat switch1/tree
>     0
> 
> Signed-off-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
> Reviewed-by: Andrew Lunn <andrew@lunn.ch>
> ---
>  net/dsa/debugfs.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 107 insertions(+)
> 
> diff --git a/net/dsa/debugfs.c b/net/dsa/debugfs.c
> index b6b5e5c97389..54e97e05a9d7 100644
> --- a/net/dsa/debugfs.c
> +++ b/net/dsa/debugfs.c
> @@ -10,6 +10,7 @@
>   */
>  
>  #include <linux/debugfs.h>
> +#include <linux/seq_file.h>
>  
>  #include "dsa_priv.h"
>  
> @@ -19,6 +20,107 @@
>  /* DSA module debugfs directory */
>  static struct dentry *dsa_debugfs_dir;
>  
> +struct dsa_debugfs_ops {
> +	int (*read)(struct dsa_switch *ds, int id, struct seq_file *seq);
> +	int (*write)(struct dsa_switch *ds, int id, char *buf);
> +};
> +
> +struct dsa_debugfs_priv {
> +	const struct dsa_debugfs_ops *ops;
> +	struct dsa_switch *ds;
> +	int id;
> +};
> +
> +static int dsa_debugfs_show(struct seq_file *seq, void *p)
> +{
> +	struct dsa_debugfs_priv *priv = seq->private;
> +	struct dsa_switch *ds = priv->ds;
> +
> +	/* Somehow file mode is bypassed... Double check here */

As was said, root can do this, change your comment, just delete it :)

> +	if (!priv->ops->read)
> +		return -EOPNOTSUPP;
> +
> +	return priv->ops->read(ds, priv->id, seq);
> +}
> +
> +static ssize_t dsa_debugfs_write(struct file *file, const char __user *user_buf,
> +				 size_t count, loff_t *ppos)
> +{
> +	struct seq_file *seq = file->private_data;
> +	struct dsa_debugfs_priv *priv = seq->private;
> +	struct dsa_switch *ds = priv->ds;
> +	char buf[count + 1];

Nice, userspace asks to write 100Gb, and boom, you just smashed the
stack!

Repeat after me:
	All input is evil.

Say it again.

Always remember it.

> +	int err;
> +
> +	/* Somehow file mode is bypassed... Double check here */
> +	if (!priv->ops->write)
> +		return -EOPNOTSUPP;
> +
> +	if (copy_from_user(buf, user_buf, count))
> +		return -EFAULT;
> +
> +	buf[count] = '\0';

Be careful here.

Use the kernel library functions instead of a "raw" copy_from/to_user()
calls, that is what they are there for (simple_read_to_buffer,
simple_write_to_buffer).

> +
> +	err = priv->ops->write(ds, priv->id, buf);
> +
> +	return err ? err : count;
> +}
> +
> +static int dsa_debugfs_open(struct inode *inode, struct file *file)
> +{
> +	return single_open(file, dsa_debugfs_show, inode->i_private);
> +}
> +
> +static const struct file_operations dsa_debugfs_fops = {
> +	.open = dsa_debugfs_open,
> +	.read = seq_read,
> +	.write = dsa_debugfs_write,
> +	.llseek = no_llseek,
> +	.release = single_release,
> +	.owner = THIS_MODULE,
> +};
> +
> +static int dsa_debugfs_create_file(struct dsa_switch *ds, struct dentry *dir,
> +				   char *name, int id,
> +				   const struct dsa_debugfs_ops *ops)
> +{
> +	struct dsa_debugfs_priv *priv;
> +	struct dentry *entry;
> +	umode_t mode;
> +
> +	priv = devm_kzalloc(ds->dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
> +	priv->ops = ops;
> +	priv->ds = ds;
> +	priv->id = id;
> +
> +	mode = 0;
> +	if (ops->read)
> +		mode |= 0444;
> +	if (ops->write)
> +		mode |= 0200;
> +
> +	entry = debugfs_create_file(name, mode, dir, priv, &dsa_debugfs_fops);
> +	if (IS_ERR_OR_NULL(entry))
> +		return -EFAULT;

Again, you don't care, don't check!

thanks,

greg k-h

  parent reply	other threads:[~2017-09-08 15:29 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-28 19:17 [PATCH net-next v2 00/10] net: dsa: add generic debugfs interface Vivien Didelot
2017-08-28 19:17 ` [PATCH net-next v2 01/10] net: dsa: add " Vivien Didelot
2017-08-28 19:50   ` Jiri Pirko
2017-08-28 19:58     ` Florian Fainelli
2017-08-28 20:05       ` Jiri Pirko
2017-08-28 20:19   ` Andrew Lunn
2017-09-07 19:34   ` Greg KH
2017-09-08 13:58     ` Vivien Didelot
2017-09-14 19:59       ` Maxim Uvarov
2017-09-14 20:12         ` Alexander Duyck
2017-09-14 21:01           ` Andrew Lunn
2017-09-15  5:51             ` Jiri Pirko
2017-09-15  7:35               ` Egil Hjelmeland
2017-09-15 14:08               ` Andrew Lunn
2017-09-15 14:26                 ` Jiri Pirko
2017-09-15 15:19                   ` Andrew Lunn
2017-08-28 19:17 ` [PATCH net-next v2 02/10] net: dsa: debugfs: add tree Vivien Didelot
2017-09-08 14:18   ` Vivien Didelot
2017-09-08 14:40     ` Greg Kroah-Hartman
2017-09-08 14:57   ` Vivien Didelot
2017-09-08 15:03     ` David Laight
2017-09-08 15:29     ` Greg Kroah-Hartman [this message]
2017-08-28 19:17 ` [PATCH net-next v2 03/10] net: dsa: debugfs: add tag_protocol Vivien Didelot
2017-08-28 20:16   ` Andrew Lunn
2017-08-28 19:17 ` [PATCH net-next v2 04/10] net: dsa: debugfs: add port stats Vivien Didelot
2017-08-28 19:17 ` [PATCH net-next v2 05/10] net: dsa: debugfs: add port regs Vivien Didelot
2017-08-28 19:17 ` [PATCH net-next v2 06/10] net: dsa: debugfs: add port fdb Vivien Didelot
2017-08-28 19:17 ` [PATCH net-next v2 07/10] net: dsa: restore mdb dump Vivien Didelot
2017-08-28 19:17 ` [PATCH net-next v2 08/10] net: dsa: debugfs: add port mdb Vivien Didelot
2017-08-28 19:17 ` [PATCH net-next v2 09/10] net: dsa: restore VLAN dump Vivien Didelot
2017-08-28 19:17 ` [PATCH net-next v2 10/10] net: dsa: debugfs: add port vlan Vivien Didelot
2017-08-28 19:53 ` [PATCH net-next v2 00/10] net: dsa: add generic debugfs interface Jiri Pirko
2017-08-28 20:08   ` Andrew Lunn
2017-08-29  6:25     ` Jiri Pirko
2017-08-29 12:50       ` Andrew Lunn
2017-08-29 19:05         ` Arkadi Sharshevsky
2017-08-29 19:19           ` Florian Fainelli
2017-08-29 20:27             ` Andrew Lunn
2017-08-30  7:43         ` Jiri Pirko
2017-08-29  4:38 ` David Miller
2017-08-29  6:29   ` Jiri Pirko
2017-08-29 15:57     ` Vivien Didelot
2017-08-30  7:40       ` Jiri Pirko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170908152923.GA19672@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=Woojung.Huh@microchip.com \
    --cc=andrew@lunn.ch \
    --cc=cphealy@gmail.com \
    --cc=davem@davemloft.net \
    --cc=f.fainelli@gmail.com \
    --cc=john@phrozen.org \
    --cc=kernel@savoirfairelinux.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=nikita.yoush@cogentembedded.com \
    --cc=privat@egil-hjelmeland.no \
    --cc=sean.wang@mediatek.com \
    --cc=vivien.didelot@savoirfairelinux.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.