diff mbox series

[RFC,v3,2/3] fs: debugfs: Add write functionality to debugfs blobs

Message ID 20230612215139.5132-3-Avadhut.Naik@amd.com (mailing list archive)
State RFC, archived
Headers show
Series Add support for Vendor Defined Error Types in Einj Module | expand

Commit Message

Avadhut Naik June 12, 2023, 9:51 p.m. UTC
Currently, debugfs_create_blob() creates read-only debugfs binary blob
files.

In some cases, however, userspace tools need to write variable length
data structures into predetermined memory addresses. An example is when
injecting Vendor-defined error types through the einj module. In such
cases, the functionality to write to these blob files in debugfs would
be desired since the mapping aspect can be handled within the modules
with userspace tools only needing to write into the blob files.

Implement a write callback to enable writing to these blob files in
debugfs.

Signed-off-by: Avadhut Naik <Avadhut.Naik@amd.com>
Reviewed-by: Alexey Kardashevskiy <aik@amd.com>
---
 fs/debugfs/file.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

Comments

Greg Kroah-Hartman June 13, 2023, 7:59 a.m. UTC | #1
On Mon, Jun 12, 2023 at 09:51:38PM +0000, Avadhut Naik wrote:
>  /**
> - * debugfs_create_blob - create a debugfs file that is used to read a binary blob
> + * debugfs_create_blob - create a debugfs file that is used to read and write
> + * a binary blob
>   * @name: a pointer to a string containing the name of the file to create.
> - * @mode: the read permission that the file should have (other permissions are
> - *	  masked out)
> + * @mode: the permission that the file should have
>   * @parent: a pointer to the parent dentry for this file.  This should be a
>   *          directory dentry if set.  If this parameter is %NULL, then the
>   *          file will be created in the root of the debugfs filesystem.
> @@ -992,7 +1010,7 @@ static const struct file_operations fops_blob = {
>   *
>   * This function creates a file in debugfs with the given name that exports
>   * @blob->data as a binary blob. If the @mode variable is so set it can be
> - * read from. Writing is not supported.
> + * read from and written to.
>   *
>   * This function will return a pointer to a dentry if it succeeds.  This
>   * pointer must be passed to the debugfs_remove() function when the file is
> @@ -1007,7 +1025,7 @@ struct dentry *debugfs_create_blob(const char *name, umode_t mode,
>  				   struct dentry *parent,
>  				   struct debugfs_blob_wrapper *blob)
>  {
> -	return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
> +	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);

Have you audited all calls to this function to verify that you haven't
just turned on write access to some debugfs files?

Why not rename this to debugfs_create_blob_wo() and then make a new
debugfs_create_blob_rw() call to ensure that it all is ok?

thanks,

greg k-h
Alexey Kardashevskiy June 13, 2023, 10:05 a.m. UTC | #2
On 13/6/23 17:59, Greg KH wrote:
> On Mon, Jun 12, 2023 at 09:51:38PM +0000, Avadhut Naik wrote:
>>   /**
>> - * debugfs_create_blob - create a debugfs file that is used to read a binary blob
>> + * debugfs_create_blob - create a debugfs file that is used to read and write
>> + * a binary blob
>>    * @name: a pointer to a string containing the name of the file to create.
>> - * @mode: the read permission that the file should have (other permissions are
>> - *	  masked out)
>> + * @mode: the permission that the file should have
>>    * @parent: a pointer to the parent dentry for this file.  This should be a
>>    *          directory dentry if set.  If this parameter is %NULL, then the
>>    *          file will be created in the root of the debugfs filesystem.
>> @@ -992,7 +1010,7 @@ static const struct file_operations fops_blob = {
>>    *
>>    * This function creates a file in debugfs with the given name that exports
>>    * @blob->data as a binary blob. If the @mode variable is so set it can be
>> - * read from. Writing is not supported.
>> + * read from and written to.
>>    *
>>    * This function will return a pointer to a dentry if it succeeds.  This
>>    * pointer must be passed to the debugfs_remove() function when the file is
>> @@ -1007,7 +1025,7 @@ struct dentry *debugfs_create_blob(const char *name, umode_t mode,
>>   				   struct dentry *parent,
>>   				   struct debugfs_blob_wrapper *blob)
>>   {
>> -	return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
>> +	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
> 
> Have you audited all calls to this function to verify that you haven't
> just turned on write access to some debugfs files?

I just did, it is one of S_IRUGO/S_IRUSR/0444/0400/(S_IFREG | 0444). So 
we are quite safe here. Except (S_IFREG | 0444) in 
drivers/platform/chrome/cros_ec_debugfs.c which seems wrong as debugfs 
files are not regular files.

> Why not rename this to debugfs_create_blob_wo() and then make a new
> debugfs_create_blob_rw() call to ensure that it all is ok?

It is already taking the mode for this purpose. imho just 
cros_ec_create_panicinfo()'s debugfs_create_blob("panicinfo", S_IFREG | 
0444,...) needs fixing.

> 
> thanks,
> 
> greg k-h
Greg Kroah-Hartman June 13, 2023, 10:22 a.m. UTC | #3
On Tue, Jun 13, 2023 at 08:05:41PM +1000, Alexey Kardashevskiy wrote:
> 
> 
> On 13/6/23 17:59, Greg KH wrote:
> > On Mon, Jun 12, 2023 at 09:51:38PM +0000, Avadhut Naik wrote:
> > >   /**
> > > - * debugfs_create_blob - create a debugfs file that is used to read a binary blob
> > > + * debugfs_create_blob - create a debugfs file that is used to read and write
> > > + * a binary blob
> > >    * @name: a pointer to a string containing the name of the file to create.
> > > - * @mode: the read permission that the file should have (other permissions are
> > > - *	  masked out)
> > > + * @mode: the permission that the file should have
> > >    * @parent: a pointer to the parent dentry for this file.  This should be a
> > >    *          directory dentry if set.  If this parameter is %NULL, then the
> > >    *          file will be created in the root of the debugfs filesystem.
> > > @@ -992,7 +1010,7 @@ static const struct file_operations fops_blob = {
> > >    *
> > >    * This function creates a file in debugfs with the given name that exports
> > >    * @blob->data as a binary blob. If the @mode variable is so set it can be
> > > - * read from. Writing is not supported.
> > > + * read from and written to.
> > >    *
> > >    * This function will return a pointer to a dentry if it succeeds.  This
> > >    * pointer must be passed to the debugfs_remove() function when the file is
> > > @@ -1007,7 +1025,7 @@ struct dentry *debugfs_create_blob(const char *name, umode_t mode,
> > >   				   struct dentry *parent,
> > >   				   struct debugfs_blob_wrapper *blob)
> > >   {
> > > -	return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
> > > +	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
> > 
> > Have you audited all calls to this function to verify that you haven't
> > just turned on write access to some debugfs files?
> 
> I just did, it is one of S_IRUGO/S_IRUSR/0444/0400/(S_IFREG | 0444). So we
> are quite safe here. Except (S_IFREG | 0444) in
> drivers/platform/chrome/cros_ec_debugfs.c which seems wrong as debugfs files
> are not regular files.
> 
> > Why not rename this to debugfs_create_blob_wo() and then make a new
> > debugfs_create_blob_rw() call to ensure that it all is ok?
> 
> It is already taking the mode for this purpose. imho just
> cros_ec_create_panicinfo()'s debugfs_create_blob("panicinfo", S_IFREG |
> 0444,...) needs fixing.

Yes, well it's taking the mode, but silently modifying it :)

Ok, thanks for the audit, respin this with that fix and then I don't
have a problem with it (other than binary debugfs files fill me with
dread, what could go wrong...)

thanks,

greg k-h
Naik, Avadhut June 13, 2023, 10:35 p.m. UTC | #4
Hi,

	Thanks for reviewing!	

On 6/13/2023 05:22, Greg KH wrote:
> On Tue, Jun 13, 2023 at 08:05:41PM +1000, Alexey Kardashevskiy wrote:
>>
>>
>> On 13/6/23 17:59, Greg KH wrote:
>>> On Mon, Jun 12, 2023 at 09:51:38PM +0000, Avadhut Naik wrote:
>>>>   /**
>>>> - * debugfs_create_blob - create a debugfs file that is used to read a binary blob
>>>> + * debugfs_create_blob - create a debugfs file that is used to read and write
>>>> + * a binary blob
>>>>    * @name: a pointer to a string containing the name of the file to create.
>>>> - * @mode: the read permission that the file should have (other permissions are
>>>> - *	  masked out)
>>>> + * @mode: the permission that the file should have
>>>>    * @parent: a pointer to the parent dentry for this file.  This should be a
>>>>    *          directory dentry if set.  If this parameter is %NULL, then the
>>>>    *          file will be created in the root of the debugfs filesystem.
>>>> @@ -992,7 +1010,7 @@ static const struct file_operations fops_blob = {
>>>>    *
>>>>    * This function creates a file in debugfs with the given name that exports
>>>>    * @blob->data as a binary blob. If the @mode variable is so set it can be
>>>> - * read from. Writing is not supported.
>>>> + * read from and written to.
>>>>    *
>>>>    * This function will return a pointer to a dentry if it succeeds.  This
>>>>    * pointer must be passed to the debugfs_remove() function when the file is
>>>> @@ -1007,7 +1025,7 @@ struct dentry *debugfs_create_blob(const char *name, umode_t mode,
>>>>   				   struct dentry *parent,
>>>>   				   struct debugfs_blob_wrapper *blob)
>>>>   {
>>>> -	return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
>>>> +	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
>>>
>>> Have you audited all calls to this function to verify that you haven't
>>> just turned on write access to some debugfs files?
>>
>> I just did, it is one of S_IRUGO/S_IRUSR/0444/0400/(S_IFREG | 0444). So we
>> are quite safe here. Except (S_IFREG | 0444) in
>> drivers/platform/chrome/cros_ec_debugfs.c which seems wrong as debugfs files
>> are not regular files.
>>
>>> Why not rename this to debugfs_create_blob_wo() and then make a new
>>> debugfs_create_blob_rw() call to ensure that it all is ok?
>>
>> It is already taking the mode for this purpose. imho just
>> cros_ec_create_panicinfo()'s debugfs_create_blob("panicinfo", S_IFREG |
>> 0444,...) needs fixing.
> 
> Yes, well it's taking the mode, but silently modifying it :)
> 
> Ok, thanks for the audit, respin this with that fix and then I don't
> have a problem with it (other than binary debugfs files fill me with
> dread, what could go wrong...)
> 
	Will add the fix for cros_ec_create_panicinfo()'s debugfs_create_blob()
usage.

Thanks,
Avadhut Naik

> thanks,
> 
> greg k-h

--
diff mbox series

Patch

diff --git a/fs/debugfs/file.c b/fs/debugfs/file.c
index 1f971c880dde..fab5a562b57c 100644
--- a/fs/debugfs/file.c
+++ b/fs/debugfs/file.c
@@ -973,17 +973,35 @@  static ssize_t read_file_blob(struct file *file, char __user *user_buf,
 	return r;
 }
 
+static ssize_t write_file_blob(struct file *file, const char __user *user_buf,
+			       size_t count, loff_t *ppos)
+{
+	struct debugfs_blob_wrapper *blob = file->private_data;
+	struct dentry *dentry = F_DENTRY(file);
+	ssize_t r;
+
+	r = debugfs_file_get(dentry);
+	if (unlikely(r))
+		return r;
+	r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf,
+				   count);
+
+	debugfs_file_put(dentry);
+	return r;
+}
+
 static const struct file_operations fops_blob = {
 	.read =		read_file_blob,
+	.write =	write_file_blob,
 	.open =		simple_open,
 	.llseek =	default_llseek,
 };
 
 /**
- * debugfs_create_blob - create a debugfs file that is used to read a binary blob
+ * debugfs_create_blob - create a debugfs file that is used to read and write
+ * a binary blob
  * @name: a pointer to a string containing the name of the file to create.
- * @mode: the read permission that the file should have (other permissions are
- *	  masked out)
+ * @mode: the permission that the file should have
  * @parent: a pointer to the parent dentry for this file.  This should be a
  *          directory dentry if set.  If this parameter is %NULL, then the
  *          file will be created in the root of the debugfs filesystem.
@@ -992,7 +1010,7 @@  static const struct file_operations fops_blob = {
  *
  * This function creates a file in debugfs with the given name that exports
  * @blob->data as a binary blob. If the @mode variable is so set it can be
- * read from. Writing is not supported.
+ * read from and written to.
  *
  * This function will return a pointer to a dentry if it succeeds.  This
  * pointer must be passed to the debugfs_remove() function when the file is
@@ -1007,7 +1025,7 @@  struct dentry *debugfs_create_blob(const char *name, umode_t mode,
 				   struct dentry *parent,
 				   struct debugfs_blob_wrapper *blob)
 {
-	return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
+	return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
 }
 EXPORT_SYMBOL_GPL(debugfs_create_blob);