linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Jason Gunthorpe <jgg@ziepe.ca>
Cc: linux-rdma <linux-rdma@vger.kernel.org>,
	linux-kernel <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH -next] mm: Use sysfs_emit functions not sprintf
Date: Wed, 07 Oct 2020 11:04:42 -0700	[thread overview]
Message-ID: <a334b30e7b617eb6b0ea22f2bf00e0f188c4ae42.camel@perches.com> (raw)
In-Reply-To: <20201007125330.GO5177@ziepe.ca>

[-- Attachment #1: Type: text/plain, Size: 3648 bytes --]

On Wed, 2020-10-07 at 09:53 -0300, Jason Gunthorpe wrote:
> On Wed, Oct 07, 2020 at 12:16:01AM -0700, Kees Cook wrote:
> > On Tue, Oct 06, 2020 at 09:28:17AM -0700, Joe Perches wrote:
> > > Convert the various uses of sprintf/snprintf/scnprintf to
> > > format sysfs output to sysfs_emit and sysfs_emit_at to make
> > > clear the output is sysfs related and to avoid any possible
> > > buffer overrun of the PAGE_SIZE buffer.
> > > 
> > > Done with cocci scripts and some typing.
> > 
> > Can you include the cocci script in the commit log? It might be nicer to
> > split the "manual" changes from the cocci changes, as that makes review
> > much easier too.
> > 
> > Regardless, yes, I'm a fan of switching these all around to
> > sysfs_emit*(). :)
> 
> Yah, +1, I'd welcome patches for drivers/infiniband as well next cycle

The script to change <foo>_show(struct device *, ...)
function uses of
sprintf to sysfs_emit is attached.

The cocci script is coarse and doesn't find nor change all
the possible variants of the sprintf uses in these functions.

It could be run using:

$ spatch --in-place -sp-file sysfs_emit.cocci drivers/infiniband/

Against next-20201007 it produces:

$ git diff --shortstat drivers/infiniband
 25 files changed, 322 insertions(+), 303 deletions(-)

Because it touches a lot of drivers, the 'cc' list is
pretty large for the diff.

Given the size of the cc list, unless there's a single
acceptable patch, I will not submit individual patches as
I really dislike the back and forth of this sub-maintainer
will but this sub-maintainer will not apply a patch.

Doug Ledford <dledford@redhat.com> (supporter:INFINIBAND SUBSYSTEM)
Jason Gunthorpe <jgg@ziepe.ca> (supporter:INFINIBAND SUBSYSTEM)
Selvin Xavier <selvin.xavier@broadcom.com> (supporter:BROADCOM NETXTREME-E ROCE DRIVER)
Devesh Sharma <devesh.sharma@broadcom.com> (supporter:BROADCOM NETXTREME-E ROCE DRIVER)
Somnath Kotur <somnath.kotur@broadcom.com> (supporter:BROADCOM NETXTREME-E ROCE DRIVER)
Sriharsha Basavapatna <sriharsha.basavapatna@broadcom.com> (supporter:BROADCOM NETXTREME-E ROCE DRIVER)
Naresh Kumar PBS <nareshkumar.pbs@broadcom.com> (supporter:BROADCOM NETXTREME-E ROCE DRIVER)
Potnuri Bharat Teja <bharat@chelsio.com> (supporter:CXGB4 IWARP RNIC DRIVER (IW_CXGB4))
Mike Marciniszyn <mike.marciniszyn@intel.com> (supporter:HFI1 DRIVER)
Dennis Dalessandro <dennis.dalessandro@intel.com> (supporter:HFI1 DRIVER)
Faisal Latif <faisal.latif@intel.com> (supporter:INTEL RDMA RNIC DRIVER)
Shiraz Saleem <shiraz.saleem@intel.com> (supporter:INTEL RDMA RNIC DRIVER)
Yishai Hadas <yishaih@nvidia.com> (supporter:MELLANOX MLX4 IB driver)
Leon Romanovsky <leon@kernel.org> (supporter:MELLANOX MLX5 IB driver)
Michal Kalderon <mkalderon@marvell.com> (supporter:QLOGIC QL4xxx RDMA DRIVER)
Ariel Elior <aelior@marvell.com> (supporter:QLOGIC QL4xxx RDMA DRIVER)
Christian Benvenuti <benve@cisco.com> (supporter:CISCO VIC LOW LATENCY NIC DRIVER)
Nelson Escobar <neescoba@cisco.com> (supporter:CISCO VIC LOW LATENCY NIC DRIVER)
Parvi Kaustubhi <pkaustub@cisco.com> (supporter:CISCO VIC LOW LATENCY NIC DRIVER)
Adit Ranadive <aditr@vmware.com> (maintainer:VMWARE PVRDMA DRIVER)
VMware PV-Drivers <pv-drivers@vmware.com> (maintainer:VMWARE PVRDMA DRIVER)
Zhu Yanjun <yanjunz@nvidia.com> (supporter:SOFT-ROCE DRIVER (rxe))
Danil Kipnis <danil.kipnis@cloud.ionos.com> (maintainer:RTRS TRANSPORT DRIVERS)
Jack Wang <jinpu.wang@cloud.ionos.com> (maintainer:RTRS TRANSPORT DRIVERS)
Bart Van Assche <bvanassche@acm.org> (supporter:SCSI RDMA PROTOCOL (SRP) INITIATOR)
linux-rdma@vger.kernel.org (open list:INFINIBAND SUBSYSTEM)
linux-kernel@vger.kernel.org (open list)


[-- Attachment #2: sysfs_emit.cocci --]
[-- Type: text/plain, Size: 6206 bytes --]

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier arg1, arg2, arg3;
@@
ssize_t d_show(struct device *
-	arg1
+	dev
	, struct device_attribute *
-	arg2
+	attr
	, char *
-	arg3
+	buf
	)
{
	<...
(
-	arg1
+	dev
|
-	arg2
+	attr
|
-	arg3
+	buf
)
	...>
}

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier dev, attr, buf;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	return
-	sprintf(buf,
+	sysfs_emit(buf,
	...);
	...>
}

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier dev, attr, buf;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	return
-	snprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
}

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier dev, attr, buf;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	return
-	scnprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
}

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier dev, attr, buf;
expression chr;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	return
-	strcpy(buf, chr);
+	sysfs_emit(buf, chr);
	...>
}

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	len =
-	sprintf(buf,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	len =
-	snprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
	len =
-	scnprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier dev, attr, buf;
identifier len;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	<...
-	len += scnprintf(buf + len, PAGE_SIZE - len,
+	len += sysfs_emit_at(buf, len,
	...);
	...>
	return len;
}

@@
//identifier d_show =~ "^.*show.*$";
identifier d_show;
identifier dev, attr, buf;
expression chr;
@@

ssize_t d_show(struct device *dev, struct device_attribute *attr, char *buf)
{
-	strcpy(buf, chr);
-	return strlen(buf);
+	return sysfs_emit(buf, chr);
}

@@
identifier k_show =~ "^.*show.*$";
identifier arg1, arg2, arg3;
@@
ssize_t k_show(struct kobject *
-	arg1
+	kobj
	, struct kobj_attribute *
-	arg2
+	attr
	, char *
-	arg3
+	buf
	)
{
	...
(
-	arg1
+	kobj
|
-	arg2
+	attr
|
-	arg3
+	buf
)
	...
}

@@
identifier k_show =~ "^.*show.*$";
identifier kobj, attr, buf;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	return
-	sprintf(buf,
+	sysfs_emit(buf,
	...);
	...>
}

@@
identifier k_show =~ "^.*show.*$";
identifier kobj, attr, buf;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	return
-	snprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
}

@@
identifier k_show =~ "^.*show.*$";
identifier kobj, attr, buf;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	return
-	scnprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
}

@@
identifier k_show =~ "^.*show.*$";
identifier kobj, attr, buf;
expression chr;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	return
-	strcpy(buf, chr);
+	sysfs_emit(buf, chr);
	...>
}

@@
identifier k_show =~ "^.*show.*$";
identifier kobj, attr, buf;
identifier len;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	len =
-	sprintf(buf,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
identifier k_show =~ "^.*show.*$";
identifier kobj, attr, buf;
identifier len;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	len =
-	snprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
identifier k_show =~ "^.*show.*$";
identifier kobj, attr, buf;
identifier len;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
	len =
-	scnprintf(buf, PAGE_SIZE,
+	sysfs_emit(buf,
	...);
	...>
	return len;
}

@@
identifier k_show =~ "^.*show.*$";
identifier kobj, attr, buf;
identifier len;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	<...
-	len += scnprintf(buf + len, PAGE_SIZE - len,
+	len += sysfs_emit_at(buf, len,
	...);
	...>
	return len;
}

@@
identifier k_show =~ "^.*show.*$";
identifier kobj, attr, buf;
expression chr;
@@

ssize_t k_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
-	strcpy(buf, chr);
-	return strlen(buf);
+	return sysfs_emit(buf, chr);
}

// Rename the sysfs_emit assigned variables not named len and not already int
// and set the name to len and type to int

@not_int_not_len exists@
type T != int;
identifier x != len;
position p;
identifier sysfs =~ "^sysfs_emit.*$";
assignment operator aop;
@@

T x@p;
...
x aop sysfs(...)
...

@@
type not_int_not_len.T;
identifier not_int_not_len.x;
position not_int_not_len.p;
@@

- T x@p;
+ int len;
  <...
- x
+ len
  ...>

// Rename the already int sysfs_emit assigned variables not named len
// and set the name to len

@int_not_len exists@
type T = int;
identifier x != len;
position p;
identifier sysfs =~ "^sysfs_emit.*$";
assignment operator aop;
@@

T x@p;
...
x aop sysfs(...)
...

@@
type int_not_len.T;
identifier int_not_len.x;
position int_not_len.p;
@@

- T x@p;
+ int len;
  <...
- x
+ len
  ...>

// Rename the non-int int sysfs_emit assigned variables named len
// and set the type to int

@not_int_has_len exists@
type T != int;
identifier x = len;
position p;
identifier sysfs =~ "^sysfs_emit.*$";
assignment operator aop;
@@

T x@p;
...
x aop sysfs(...)
...

@@
type not_int_has_len.T;
identifier not_int_has_len.x;
position not_int_has_len.p;
@@

- T x@p;
+ int len;

  reply	other threads:[~2020-10-07 18:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-06 16:28 [PATCH -next] mm: Use sysfs_emit functions not sprintf Joe Perches
2020-10-07  7:16 ` Kees Cook
2020-10-07 12:53   ` Jason Gunthorpe
2020-10-07 18:04     ` Joe Perches [this message]
2020-10-07 18:56       ` Jason Gunthorpe

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=a334b30e7b617eb6b0ea22f2bf00e0f188c4ae42.camel@perches.com \
    --to=joe@perches.com \
    --cc=jgg@ziepe.ca \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).