[PATCH v1 2/2] fsi: sbefifo: Validate pending user write

Ninad Palsule posted 2 patches 2 years, 3 months ago
There is a newer version of this series
[PATCH v1 2/2] fsi: sbefifo: Validate pending user write
Posted by Ninad Palsule 2 years, 3 months ago
This commit fails user write operation if previous write operation is
still pending.

As per the driver design write operation only prepares the buffer, the
actual FSI write is performed on next read operation. so if buggy
application sends two back to back writes or two parallel writes then
that could cause memory leak.

Signed-off-by: Ninad Palsule <ninad@linux.ibm.com>
---
 drivers/fsi/fsi-sbefifo.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/fsi/fsi-sbefifo.c b/drivers/fsi/fsi-sbefifo.c
index b771dff27f7f..824e2a921a25 100644
--- a/drivers/fsi/fsi-sbefifo.c
+++ b/drivers/fsi/fsi-sbefifo.c
@@ -874,6 +874,12 @@ static ssize_t sbefifo_user_write(struct file *file, const char __user *buf,
 
 	mutex_lock(&user->file_lock);
 
+	/* Previous write is still in progress */
+	if (user->pending_cmd) {
+		mutex_unlock(&user->file_lock);
+		return -EALREADY;
+	}
+
 	/* Can we use the pre-allocate buffer ? If not, allocate */
 	if (len <= PAGE_SIZE)
 		user->pending_cmd = user->cmd_page;
-- 
2.39.2
Re: [PATCH v1 2/2] fsi: sbefifo: Validate pending user write
Posted by Joel Stanley 2 years, 3 months ago
On Thu, 7 Sept 2023 at 22:10, Ninad Palsule <ninad@linux.ibm.com> wrote:
>
> This commit fails user write operation if previous write operation is
> still pending.
>
> As per the driver design write operation only prepares the buffer, the
> actual FSI write is performed on next read operation. so if buggy
> application sends two back to back writes or two parallel writes then
> that could cause memory leak.

The driver already has this code:


>
> Signed-off-by: Ninad Palsule <ninad@linux.ibm.com>
> ---
>  drivers/fsi/fsi-sbefifo.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/drivers/fsi/fsi-sbefifo.c b/drivers/fsi/fsi-sbefifo.c
> index b771dff27f7f..824e2a921a25 100644
> --- a/drivers/fsi/fsi-sbefifo.c
> +++ b/drivers/fsi/fsi-sbefifo.c
> @@ -874,6 +874,12 @@ static ssize_t sbefifo_user_write(struct file *file, const char __user *buf,
>
>         mutex_lock(&user->file_lock);
>
> +       /* Previous write is still in progress */
> +       if (user->pending_cmd) {
> +               mutex_unlock(&user->file_lock);
> +               return -EALREADY;

That's an unusual return code. I guess it makes sense in this context.

It's good to fix the potential memory leak, and we should add code to
catch that case.

This will change the behaviour of the character device from "overwrite
the previous operation" to "reject operation until a read is
performed". Do you think there's existing code that depends on the old
behaviour?

> +       }
> +
>         /* Can we use the pre-allocate buffer ? If not, allocate */
>         if (len <= PAGE_SIZE)
>                 user->pending_cmd = user->cmd_page;
> --
> 2.39.2
>
Re: [PATCH v1 2/2] fsi: sbefifo: Validate pending user write
Posted by Ninad Palsule 2 years, 3 months ago
Hi Joel,

On 9/11/23 00:52, Joel Stanley wrote:
> On Thu, 7 Sept 2023 at 22:10, Ninad Palsule <ninad@linux.ibm.com> wrote:
>> This commit fails user write operation if previous write operation is
>> still pending.
>>
>> As per the driver design write operation only prepares the buffer, the
>> actual FSI write is performed on next read operation. so if buggy
>> application sends two back to back writes or two parallel writes then
>> that could cause memory leak.
> The driver already has this code:
Yes, I have improved the comment.
>
>
>> Signed-off-by: Ninad Palsule <ninad@linux.ibm.com>
>> ---
>>   drivers/fsi/fsi-sbefifo.c | 6 ++++++
>>   1 file changed, 6 insertions(+)
>>
>> diff --git a/drivers/fsi/fsi-sbefifo.c b/drivers/fsi/fsi-sbefifo.c
>> index b771dff27f7f..824e2a921a25 100644
>> --- a/drivers/fsi/fsi-sbefifo.c
>> +++ b/drivers/fsi/fsi-sbefifo.c
>> @@ -874,6 +874,12 @@ static ssize_t sbefifo_user_write(struct file *file, const char __user *buf,
>>
>>          mutex_lock(&user->file_lock);
>>
>> +       /* Previous write is still in progress */
>> +       if (user->pending_cmd) {
>> +               mutex_unlock(&user->file_lock);
>> +               return -EALREADY;
> That's an unusual return code. I guess it makes sense in this context.
>
> It's good to fix the potential memory leak, and we should add code to
> catch that case.
>
> This will change the behaviour of the character device from "overwrite
> the previous operation" to "reject operation until a read is
> performed". Do you think there's existing code that depends on the old
> behaviour?
I do not see any issue with this rejection. I thought user may wants to 
send reset while command is hung but that case is not valid as pending 
command will hold the lock. User can always close the connection and 
reopen if required. How do I find if this could cause the regression?
>
>> +       }
>> +
>>          /* Can we use the pre-allocate buffer ? If not, allocate */
>>          if (len <= PAGE_SIZE)
>>                  user->pending_cmd = user->cmd_page;
>> --
>> 2.39.2
>>