lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <8c17ed4f-fb29-4ff8-35db-afab284c6e71@wanadoo.fr>
Date:   Fri, 17 Apr 2020 15:28:21 +0200
From:   Marion & Christophe JAILLET <christophe.jaillet@...adoo.fr>
To:     Jason Gunthorpe <jgg@...pe.ca>
Cc:     selvin.xavier@...adcom.com, devesh.sharma@...adcom.com,
        dledford@...hat.com, leon@...nel.org, colin.king@...onical.com,
        roland@...estorage.com, linux-rdma@...r.kernel.org,
        linux-kernel@...r.kernel.org, kernel-janitors@...r.kernel.org
Subject: Re: [PATCH] RDMA/ocrdma: Fix an off-by-one issue in 'ocrdma_add_stat'


Le 14/04/2020 à 20:34, Jason Gunthorpe a écrit :
> On Sat, Mar 28, 2020 at 08:30:40AM +0100, Christophe JAILLET wrote:
>> There is an off-by-one issue when checking if there is enough space in the
>> output buffer, because we must keep some place for a final '\0'.
>>
>> While at it:
>>     - Use 'scnprintf' instead of 'snprintf' in order to avoid a superfluous
>>      'strlen'
>>     - avoid some useless initializations
>>     - avoida hard coded buffer size that can be computed at built time.
>>
>> Fixes: a51f06e1679e ("RDMA/ocrdma: Query controller information")
>> Signed-off-by: Christophe JAILLET <christophe.jaillet@...adoo.fr>
>> ---
>> The '\0' comes from memset(..., 0, ...) in all callers.
>> This could be also avoided if needed.
>> ---
>>   drivers/infiniband/hw/ocrdma/ocrdma_stats.c | 9 ++++-----
>>   1 file changed, 4 insertions(+), 5 deletions(-)
>>
>> diff --git a/drivers/infiniband/hw/ocrdma/ocrdma_stats.c b/drivers/infiniband/hw/ocrdma/ocrdma_stats.c
>> index 5f831e3bdbad..614a449e6b87 100644
>> --- a/drivers/infiniband/hw/ocrdma/ocrdma_stats.c
>> +++ b/drivers/infiniband/hw/ocrdma/ocrdma_stats.c
>> @@ -49,13 +49,12 @@ static struct dentry *ocrdma_dbgfs_dir;
>>   static int ocrdma_add_stat(char *start, char *pcur,
>>   				char *name, u64 count)
>>   {
>> -	char buff[128] = {0};
>> -	int cpy_len = 0;
>> +	char buff[128];
>> +	int cpy_len;
>>   
>> -	snprintf(buff, 128, "%s: %llu\n", name, count);
>> -	cpy_len = strlen(buff);
>> +	cpy_len = scnprintf(buff, sizeof(buff), "%s: %llu\n", name, count);
>>   
>> -	if (pcur + cpy_len > start + OCRDMA_MAX_DBGFS_MEM) {
>> +	if (pcur + cpy_len >= start + OCRDMA_MAX_DBGFS_MEM) {
>>   		pr_err("%s: No space in stats buff\n", __func__);
>>   		return 0;
>>   	}
> The memcpy is still kind of silly right? What about this:
>
> static int ocrdma_add_stat(char *start, char *pcur, char *name, u64 count)
> {
> 	size_t len = (start + OCRDMA_MAX_DBGFS_MEM) - pcur;
> 	int cpy_len;
>
> 	cpy_len = snprintf(pcur, len, "%s: %llu\n", name, count);
> 	if (cpy_len >= len || cpy_len < 0) {
> 		pr_err("%s: No space in stats buff\n", __func__);
> 		return 0;
> 	}
> 	return cpy_len;
> }
>
> Jason

It can looks useless, but I think that the goal was to make sure that we 
would not display truncated data. Each line is either complete or absent.

I don't have any strong opinion of what is best, but I can understand 
the current logic.

This function is not a hot spot, so useless memcpy is not a big issue.

CJ


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ