mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] IB/hfi1: fix buffer underflow in fault injection code
@ 2024-12-27 23:09 Vitaliy Shevtsov
  2025-01-15 17:55 ` Dennis Dalessandro
  0 siblings, 1 reply; 4+ messages in thread
From: Vitaliy Shevtsov @ 2024-12-27 23:09 UTC (permalink / raw)
  To: Dennis Dalessandro
  Cc: Vitaliy Shevtsov, Jason Gunthorpe, Leon Romanovsky,
	Mitko Haralanov, Mike Marciniszyn, Doug Ledford, Don Hiatt,
	linux-rdma, linux-kernel, lvc-project

[Why]
The fault injection code may have a buffer underflow, which may cause
memory corruption by writing a newline character before the base address of
the array. This can happen if the fault->opcodes bitmap is empty.

Since a file in debugfs is created with an empty bitmap, it is possible to
read the file before any set bits are written to it.

[How]
Fix this by checking that the size variable is greater than zero, otherwise
return zero as the number of bytes read.

Found by Linux Verification Center (linuxtesting.org) with Svace.

Fixes: a74d5307caba ("IB/hfi1: Rework fault injection machinery")
Signed-off-by: Vitaliy Shevtsov <v.shevtsov@maxima.ru>
---
 drivers/infiniband/hw/hfi1/fault.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/infiniband/hw/hfi1/fault.c b/drivers/infiniband/hw/hfi1/fault.c
index ec9ee59fcf0c..2d87f9c8b89d 100644
--- a/drivers/infiniband/hw/hfi1/fault.c
+++ b/drivers/infiniband/hw/hfi1/fault.c
@@ -190,7 +190,8 @@ static ssize_t fault_opcodes_read(struct file *file, char __user *buf,
 		bit = find_next_bit(fault->opcodes, bitsize, zero);
 	}
 	debugfs_file_put(file->f_path.dentry);
-	data[size - 1] = '\n';
+	if (size)
+		data[size - 1] = '\n';
 	data[size] = '\0';
 	ret = simple_read_from_buffer(buf, len, pos, data, size);
 free_data:
-- 
2.47.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] IB/hfi1: fix buffer underflow in fault injection code
  2024-12-27 23:09 [PATCH] IB/hfi1: fix buffer underflow in fault injection code Vitaliy Shevtsov
@ 2025-01-15 17:55 ` Dennis Dalessandro
  2025-01-15 19:14   ` [lvc-project] " Fedor Pchelkin
  0 siblings, 1 reply; 4+ messages in thread
From: Dennis Dalessandro @ 2025-01-15 17:55 UTC (permalink / raw)
  To: Vitaliy Shevtsov
  Cc: Jason Gunthorpe, Leon Romanovsky, Doug Ledford, linux-rdma,
	linux-kernel, lvc-project

On 12/27/24 6:09 PM, Vitaliy Shevtsov wrote:
> [Why]
> The fault injection code may have a buffer underflow, which may cause
> memory corruption by writing a newline character before the base address of
> the array. This can happen if the fault->opcodes bitmap is empty.
> 
> Since a file in debugfs is created with an empty bitmap, it is possible to
> read the file before any set bits are written to it.
> 
> [How]
> Fix this by checking that the size variable is greater than zero, otherwise
> return zero as the number of bytes read.
> 
> Found by Linux Verification Center (linuxtesting.org) with Svace.
> 
> Fixes: a74d5307caba ("IB/hfi1: Rework fault injection machinery")
> Signed-off-by: Vitaliy Shevtsov <v.shevtsov@maxima.ru>
> ---
>  drivers/infiniband/hw/hfi1/fault.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/infiniband/hw/hfi1/fault.c b/drivers/infiniband/hw/hfi1/fault.c
> index ec9ee59fcf0c..2d87f9c8b89d 100644
> --- a/drivers/infiniband/hw/hfi1/fault.c
> +++ b/drivers/infiniband/hw/hfi1/fault.c
> @@ -190,7 +190,8 @@ static ssize_t fault_opcodes_read(struct file *file, char __user *buf,
>  		bit = find_next_bit(fault->opcodes, bitsize, zero);
>  	}
>  	debugfs_file_put(file->f_path.dentry);
> -	data[size - 1] = '\n';
> +	if (size)
> +		data[size - 1] = '\n';
>  	data[size] = '\0';
>  	ret = simple_read_from_buffer(buf, len, pos, data, size);
>  free_data:

I don't think size can ever be 0. No reason to change this I don't think.

-Denny

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [lvc-project] [PATCH] IB/hfi1: fix buffer underflow in fault injection code
  2025-01-15 17:55 ` Dennis Dalessandro
@ 2025-01-15 19:14   ` Fedor Pchelkin
  2025-01-16  4:16     ` Dennis Dalessandro
  0 siblings, 1 reply; 4+ messages in thread
From: Fedor Pchelkin @ 2025-01-15 19:14 UTC (permalink / raw)
  To: Dennis Dalessandro
  Cc: Vitaliy Shevtsov, lvc-project, Leon Romanovsky, linux-rdma,
	linux-kernel, Jason Gunthorpe, Doug Ledford

On Wed, 15. Jan 12:55, Dennis Dalessandro wrote:
> On 12/27/24 6:09 PM, Vitaliy Shevtsov wrote:
> > [Why]
> > The fault injection code may have a buffer underflow, which may cause
> > memory corruption by writing a newline character before the base address of
> > the array. This can happen if the fault->opcodes bitmap is empty.
> > 
> > Since a file in debugfs is created with an empty bitmap, it is possible to
> > read the file before any set bits are written to it.
> > 
> > [How]
> > Fix this by checking that the size variable is greater than zero, otherwise
> > return zero as the number of bytes read.
> > 
> > Found by Linux Verification Center (linuxtesting.org) with Svace.
> > 
> > Fixes: a74d5307caba ("IB/hfi1: Rework fault injection machinery")
> > Signed-off-by: Vitaliy Shevtsov <v.shevtsov@maxima.ru>
> > ---
> >  drivers/infiniband/hw/hfi1/fault.c | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/infiniband/hw/hfi1/fault.c b/drivers/infiniband/hw/hfi1/fault.c
> > index ec9ee59fcf0c..2d87f9c8b89d 100644
> > --- a/drivers/infiniband/hw/hfi1/fault.c
> > +++ b/drivers/infiniband/hw/hfi1/fault.c
> > @@ -190,7 +190,8 @@ static ssize_t fault_opcodes_read(struct file *file, char __user *buf,
> >  		bit = find_next_bit(fault->opcodes, bitsize, zero);
> >  	}
> >  	debugfs_file_put(file->f_path.dentry);
> > -	data[size - 1] = '\n';
> > +	if (size)
> > +		data[size - 1] = '\n';
> >  	data[size] = '\0';
> >  	ret = simple_read_from_buffer(buf, len, pos, data, size);
> >  free_data:
> 
> I don't think size can ever be 0. No reason to change this I don't think.
> 
> -Denny
> 

Seems the patch description rather clearly shows the size can be zero in
case the corresponding opcodes bitmap is empty. Which is the case when
user reads the file before writing anything to it.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [lvc-project] [PATCH] IB/hfi1: fix buffer underflow in fault injection code
  2025-01-15 19:14   ` [lvc-project] " Fedor Pchelkin
@ 2025-01-16  4:16     ` Dennis Dalessandro
  0 siblings, 0 replies; 4+ messages in thread
From: Dennis Dalessandro @ 2025-01-16  4:16 UTC (permalink / raw)
  To: Fedor Pchelkin
  Cc: Vitaliy Shevtsov, lvc-project, Leon Romanovsky, linux-rdma,
	linux-kernel, Jason Gunthorpe

On 1/15/25 2:14 PM, Fedor Pchelkin wrote:
> On Wed, 15. Jan 12:55, Dennis Dalessandro wrote:
>> On 12/27/24 6:09 PM, Vitaliy Shevtsov wrote:
>>> [Why]
>>> The fault injection code may have a buffer underflow, which may cause
>>> memory corruption by writing a newline character before the base address of
>>> the array. This can happen if the fault->opcodes bitmap is empty.
>>>
>>> Since a file in debugfs is created with an empty bitmap, it is possible to
>>> read the file before any set bits are written to it.
>>>
>>> [How]
>>> Fix this by checking that the size variable is greater than zero, otherwise
>>> return zero as the number of bytes read.
>>>
>>> Found by Linux Verification Center (linuxtesting.org) with Svace.
>>>
>>> Fixes: a74d5307caba ("IB/hfi1: Rework fault injection machinery")
>>> Signed-off-by: Vitaliy Shevtsov <v.shevtsov@maxima.ru>
>>> ---
>>>  drivers/infiniband/hw/hfi1/fault.c | 3 ++-
>>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/infiniband/hw/hfi1/fault.c b/drivers/infiniband/hw/hfi1/fault.c
>>> index ec9ee59fcf0c..2d87f9c8b89d 100644
>>> --- a/drivers/infiniband/hw/hfi1/fault.c
>>> +++ b/drivers/infiniband/hw/hfi1/fault.c
>>> @@ -190,7 +190,8 @@ static ssize_t fault_opcodes_read(struct file *file, char __user *buf,
>>>  		bit = find_next_bit(fault->opcodes, bitsize, zero);
>>>  	}
>>>  	debugfs_file_put(file->f_path.dentry);
>>> -	data[size - 1] = '\n';
>>> +	if (size)
>>> +		data[size - 1] = '\n';
>>>  	data[size] = '\0';
>>>  	ret = simple_read_from_buffer(buf, len, pos, data, size);
>>>  free_data:
>>
>> I don't think size can ever be 0. No reason to change this I don't think.
>>
>> -Denny
>>
> 
> Seems the patch description rather clearly shows the size can be zero in
> case the corresponding opcodes bitmap is empty. Which is the case when
> user reads the file before writing anything to it.

Guess it's OK then.

Acked-by: Dennis Dalessandro <dennis.dalessandro@cornelisnetworks.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-01-16  4:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-27 23:09 [PATCH] IB/hfi1: fix buffer underflow in fault injection code Vitaliy Shevtsov
2025-01-15 17:55 ` Dennis Dalessandro
2025-01-15 19:14   ` [lvc-project] " Fedor Pchelkin
2025-01-16  4:16     ` Dennis Dalessandro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®