* [PATCH] [v2] scsi: ipr: work around fortify-string warning
@ 2023-02-14 13:28 Arnd Bergmann
2023-02-14 21:49 ` Kees Cook
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Arnd Bergmann @ 2023-02-14 13:28 UTC (permalink / raw)
To: Brian King, James E.J. Bottomley, Martin K. Petersen,
Nathan Chancellor, Nick Desaulniers, James Bottomley
Cc: Damien Le Moal, Arnd Bergmann, Kees Cook, Niklas Cassel,
John Garry, linux-scsi, linux-kernel, llvm
From: Arnd Bergmann <arnd@arndb.de>
The ipr_log_vpd_compact() function triggers a fortified memcpy() warning
about a potential string overflow with all versions of clang:
In file included from drivers/scsi/ipr.c:43:
In file included from include/linux/string.h:254:
include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
__write_overflow_field(p_size_field, size);
^
include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
2 errors generated.
I don't see anything actually wrong with the function, but this is the
only instance I can reproduce of the fortification going wrong in the
kernel at the moment, so the easiest solution may be to rewrite the
function into something that does not trigger the warning.
Instead of having a combined buffer for vendor/device/serial strings,
use three separate local variables and just truncate the whitespace
individually.
Fixes: 8cf093e275d0 ("[SCSI] ipr: Improved dual adapter errors")
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
I did not try to bisect which commit introduced this behavior into
the fortified memcpy(), the Fixes: commit is the one that introduced
the ipr_log_vpd_compact() function but this predates the fortified
string helpers.
v2 changes:
- fix off-by-one error
---
drivers/scsi/ipr.c | 41 +++++++++++++++++++++--------------------
1 file changed, 21 insertions(+), 20 deletions(-)
diff --git a/drivers/scsi/ipr.c b/drivers/scsi/ipr.c
index 198d3f20d682..f1f61705ccd5 100644
--- a/drivers/scsi/ipr.c
+++ b/drivers/scsi/ipr.c
@@ -1516,23 +1516,22 @@ static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
}
/**
- * strip_and_pad_whitespace - Strip and pad trailing whitespace.
- * @i: index into buffer
- * @buf: string to modify
+ * strip_whitespace - Strip and pad trailing whitespace.
+ * @i: size of buffer
+ * @buf: string to modify
*
- * This function will strip all trailing whitespace, pad the end
- * of the string with a single space, and NULL terminate the string.
+ * This function will strip all trailing whitespace and
+ * NUL terminate the string.
*
- * Return value:
- * new length of string
**/
-static int strip_and_pad_whitespace(int i, char *buf)
+static void strip_whitespace(int i, char *buf)
{
+ if (i < 1)
+ return;
+ i--;
while (i && buf[i] == ' ')
i--;
- buf[i+1] = ' ';
- buf[i+2] = '\0';
- return i + 2;
+ buf[i+1] = '\0';
}
/**
@@ -1547,19 +1546,21 @@ static int strip_and_pad_whitespace(int i, char *buf)
static void ipr_log_vpd_compact(char *prefix, struct ipr_hostrcb *hostrcb,
struct ipr_vpd *vpd)
{
- char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN + IPR_SERIAL_NUM_LEN + 3];
- int i = 0;
+ char vendor_id[IPR_VENDOR_ID_LEN + 1];
+ char product_id[IPR_PROD_ID_LEN + 1];
+ char sn[IPR_SERIAL_NUM_LEN + 1];
- memcpy(buffer, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
- i = strip_and_pad_whitespace(IPR_VENDOR_ID_LEN - 1, buffer);
+ memcpy(vendor_id, vpd->vpids.vendor_id, IPR_VENDOR_ID_LEN);
+ strip_whitespace(IPR_VENDOR_ID_LEN, vendor_id);
- memcpy(&buffer[i], vpd->vpids.product_id, IPR_PROD_ID_LEN);
- i = strip_and_pad_whitespace(i + IPR_PROD_ID_LEN - 1, buffer);
+ memcpy(product_id, vpd->vpids.product_id, IPR_PROD_ID_LEN);
+ strip_whitespace(IPR_PROD_ID_LEN, product_id);
- memcpy(&buffer[i], vpd->sn, IPR_SERIAL_NUM_LEN);
- buffer[IPR_SERIAL_NUM_LEN + i] = '\0';
+ memcpy(sn, vpd->sn, IPR_SERIAL_NUM_LEN);
+ strip_whitespace(IPR_SERIAL_NUM_LEN, sn);
- ipr_hcam_err(hostrcb, "%s VPID/SN: %s\n", prefix, buffer);
+ ipr_hcam_err(hostrcb, "%s VPID/SN: %s %s %s\n", prefix,
+ vendor_id, product_id, sn);
}
/**
--
2.39.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH] [v2] scsi: ipr: work around fortify-string warning
2023-02-14 13:28 [PATCH] [v2] scsi: ipr: work around fortify-string warning Arnd Bergmann
@ 2023-02-14 21:49 ` Kees Cook
2023-02-14 21:53 ` Arnd Bergmann
2023-02-15 4:42 ` Damien Le Moal
` (3 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Kees Cook @ 2023-02-14 21:49 UTC (permalink / raw)
To: Arnd Bergmann
Cc: Brian King, James E.J. Bottomley, Martin K. Petersen,
Nathan Chancellor, Nick Desaulniers, James Bottomley,
Damien Le Moal, Arnd Bergmann, Niklas Cassel, John Garry,
linux-scsi, linux-kernel, llvm
On Tue, Feb 14, 2023 at 02:28:08PM +0100, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> The ipr_log_vpd_compact() function triggers a fortified memcpy() warning
> about a potential string overflow with all versions of clang:
Perhaps this is arch or config specific? I haven't been able to reproduce
this for some reason.
--
Kees Cook
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] [v2] scsi: ipr: work around fortify-string warning
2023-02-14 21:49 ` Kees Cook
@ 2023-02-14 21:53 ` Arnd Bergmann
0 siblings, 0 replies; 7+ messages in thread
From: Arnd Bergmann @ 2023-02-14 21:53 UTC (permalink / raw)
To: Kees Cook
Cc: Brian King, James E.J. Bottomley, Martin K. Petersen,
Nathan Chancellor, Nick Desaulniers, James Bottomley,
Damien Le Moal, Arnd Bergmann, Niklas Cassel, John Garry,
linux-scsi, linux-kernel, llvm
On Tue, Feb 14, 2023, at 22:49, Kees Cook wrote:
> On Tue, Feb 14, 2023 at 02:28:08PM +0100, Arnd Bergmann wrote:
>> From: Arnd Bergmann <arnd@arndb.de>
>>
>> The ipr_log_vpd_compact() function triggers a fortified memcpy() warning
>> about a potential string overflow with all versions of clang:
>
> Perhaps this is arch or config specific? I haven't been able to reproduce
> this for some reason.
I saw it on three arm32 randconfig builds out
of a few thousand, with clang-15, clang-16 and
clang-17. See [1] for one config that triggered
it with recent linux-next based tree.
Arnd
[1] https://pastebin.com/c9FEjzKe
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] [v2] scsi: ipr: work around fortify-string warning
2023-02-14 13:28 [PATCH] [v2] scsi: ipr: work around fortify-string warning Arnd Bergmann
2023-02-14 21:49 ` Kees Cook
@ 2023-02-15 4:42 ` Damien Le Moal
2023-02-15 21:32 ` Kees Cook
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Damien Le Moal @ 2023-02-15 4:42 UTC (permalink / raw)
To: Arnd Bergmann, Brian King, James E.J. Bottomley,
Martin K. Petersen, Nathan Chancellor, Nick Desaulniers,
James Bottomley
Cc: Arnd Bergmann, Kees Cook, Niklas Cassel, John Garry, linux-scsi,
linux-kernel, llvm
On 2/14/23 22:28, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> The ipr_log_vpd_compact() function triggers a fortified memcpy() warning
> about a potential string overflow with all versions of clang:
>
> In file included from drivers/scsi/ipr.c:43:
> In file included from include/linux/string.h:254:
> include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
> __write_overflow_field(p_size_field, size);
> ^
> include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
> 2 errors generated.
>
> I don't see anything actually wrong with the function, but this is the
> only instance I can reproduce of the fortification going wrong in the
> kernel at the moment, so the easiest solution may be to rewrite the
> function into something that does not trigger the warning.
>
> Instead of having a combined buffer for vendor/device/serial strings,
> use three separate local variables and just truncate the whitespace
> individually.
>
> Fixes: 8cf093e275d0 ("[SCSI] ipr: Improved dual adapter errors")
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Looks good to me.
Reviewed-by: Damien Le Moal <damien.lemoal@opensource.wdc.com>
--
Damien Le Moal
Western Digital Research
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] [v2] scsi: ipr: work around fortify-string warning
2023-02-14 13:28 [PATCH] [v2] scsi: ipr: work around fortify-string warning Arnd Bergmann
2023-02-14 21:49 ` Kees Cook
2023-02-15 4:42 ` Damien Le Moal
@ 2023-02-15 21:32 ` Kees Cook
2023-02-21 19:37 ` Brian King
2023-02-21 23:30 ` Martin K. Petersen
4 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2023-02-15 21:32 UTC (permalink / raw)
To: Arnd Bergmann
Cc: Brian King, James E.J. Bottomley, Martin K. Petersen,
Nathan Chancellor, Nick Desaulniers, James Bottomley,
Damien Le Moal, Arnd Bergmann, Niklas Cassel, John Garry,
linux-scsi, linux-kernel, llvm
On Tue, Feb 14, 2023 at 02:28:08PM +0100, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> The ipr_log_vpd_compact() function triggers a fortified memcpy() warning
> about a potential string overflow with all versions of clang:
>
> In file included from drivers/scsi/ipr.c:43:
> In file included from include/linux/string.h:254:
> include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
> __write_overflow_field(p_size_field, size);
> ^
> include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
> 2 errors generated.
>
> I don't see anything actually wrong with the function, but this is the
> only instance I can reproduce of the fortification going wrong in the
> kernel at the moment, so the easiest solution may be to rewrite the
> function into something that does not trigger the warning.
>
> Instead of having a combined buffer for vendor/device/serial strings,
> use three separate local variables and just truncate the whitespace
> individually.
>
> Fixes: 8cf093e275d0 ("[SCSI] ipr: Improved dual adapter errors")
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reproduced this locally -- I agree your fix looks like the best
approach. I think Clang was seeing the old "i + 2" return as potentially
overflowing in the case where there was no space-padding on any strings.
--
Kees Cook
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] [v2] scsi: ipr: work around fortify-string warning
2023-02-14 13:28 [PATCH] [v2] scsi: ipr: work around fortify-string warning Arnd Bergmann
` (2 preceding siblings ...)
2023-02-15 21:32 ` Kees Cook
@ 2023-02-21 19:37 ` Brian King
2023-02-21 23:30 ` Martin K. Petersen
4 siblings, 0 replies; 7+ messages in thread
From: Brian King @ 2023-02-21 19:37 UTC (permalink / raw)
To: Arnd Bergmann, Brian King, James E.J. Bottomley,
Martin K. Petersen, Nathan Chancellor, Nick Desaulniers,
James Bottomley
Cc: Damien Le Moal, Arnd Bergmann, Kees Cook, Niklas Cassel,
John Garry, linux-scsi, linux-kernel, llvm
On 2/14/23 7:28 AM, Arnd Bergmann wrote:
> From: Arnd Bergmann <arnd@arndb.de>
>
> The ipr_log_vpd_compact() function triggers a fortified memcpy() warning
> about a potential string overflow with all versions of clang:
>
> In file included from drivers/scsi/ipr.c:43:
> In file included from include/linux/string.h:254:
> include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
> __write_overflow_field(p_size_field, size);
> ^
> include/linux/fortify-string.h:520:4: error: call to '__write_overflow_field' declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
> 2 errors generated.
>
> I don't see anything actually wrong with the function, but this is the
> only instance I can reproduce of the fortification going wrong in the
> kernel at the moment, so the easiest solution may be to rewrite the
> function into something that does not trigger the warning.
>
> Instead of having a combined buffer for vendor/device/serial strings,
> use three separate local variables and just truncate the whitespace
> individually.
>
> Fixes: 8cf093e275d0 ("[SCSI] ipr: Improved dual adapter errors")
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Acked-by: Brian King <brking@linux.vnet.ibm.com>
--
Brian King
Power Linux I/O
IBM Linux Technology Center
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] [v2] scsi: ipr: work around fortify-string warning
2023-02-14 13:28 [PATCH] [v2] scsi: ipr: work around fortify-string warning Arnd Bergmann
` (3 preceding siblings ...)
2023-02-21 19:37 ` Brian King
@ 2023-02-21 23:30 ` Martin K. Petersen
4 siblings, 0 replies; 7+ messages in thread
From: Martin K. Petersen @ 2023-02-21 23:30 UTC (permalink / raw)
To: Arnd Bergmann
Cc: Brian King, James E.J. Bottomley, Martin K. Petersen,
Nathan Chancellor, Nick Desaulniers, James Bottomley,
Damien Le Moal, Arnd Bergmann, Kees Cook, Niklas Cassel,
John Garry, linux-scsi, linux-kernel, llvm
Arnd,
> The ipr_log_vpd_compact() function triggers a fortified memcpy()
> warning about a potential string overflow with all versions of clang:
Applied to 6.3/scsi-staging, thanks!
--
Martin K. Petersen Oracle Linux Engineering
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-02-21 23:30 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-14 13:28 [PATCH] [v2] scsi: ipr: work around fortify-string warning Arnd Bergmann
2023-02-14 21:49 ` Kees Cook
2023-02-14 21:53 ` Arnd Bergmann
2023-02-15 4:42 ` Damien Le Moal
2023-02-15 21:32 ` Kees Cook
2023-02-21 19:37 ` Brian King
2023-02-21 23:30 ` Martin K. Petersen
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).