Re: [PATCH] usb: typec: tcpm: replace strcpy with strscpy
From: Amit Sunil Dhamne
Date: Mon May 04 2026 - 14:09:52 EST
Hi David,
On 4/29/26 1:54 AM, David Laight wrote:
> On Thu, 23 Apr 2026 12:23:09 -0700
> Amit Sunil Dhamne <amitsd@xxxxxxxxxx> wrote:
>
>> Hi Maxwell,
>>
>> On 4/19/26 2:36 PM, Maxwell Doose wrote:
>>> The function strcpy() is deprecated as it can be used in buffer overflow
>>> attacks. This patch replaces strcpy() with strscpy() to improve
>>> security and stability.
>>>
>>> Signed-off-by: Maxwell Doose <m32285159@xxxxxxxxx>
>>> ---
>>> drivers/usb/typec/tcpm/tcpm.c | 6 +++---
>>> 1 file changed, 3 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
>>> index 8e0e14a2704e..69574c5e79e1 100644
>>> --- a/drivers/usb/typec/tcpm/tcpm.c
>>> +++ b/drivers/usb/typec/tcpm/tcpm.c
>>> @@ -725,7 +725,7 @@ static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
>>>
>>> if (tcpm_log_full(port)) {
>>> port->logbuffer_head = max(port->logbuffer_head - 1, 0);
>>> - strcpy(tmpbuffer, "overflow");
>>> + strscpy(tmpbuffer, "overflow", sizeof(tmpbuffer))
>>> }
>>>
>>> if (port->logbuffer_head < 0 ||
>>> @@ -841,10 +841,10 @@ static void tcpm_log_source_caps(struct tcpm_port *port)
>>> pdo_spr_avs_apdo_15v_to_20v_max_current_ma(pdo),
>>> pdo_spr_avs_apdo_src_peak_current(pdo));
>>> else
>>> - strcpy(msg, "undefined APDO");
>>> + strscpy(msg, "undefined APDO", sizeof(msg));
>>> break;
>>> default:
>>> - strcpy(msg, "undefined");
>>> + strscpy(msg, "undefined", sizeof(msg));
>>> break;
>>> }
>>> tcpm_log(port, " PDO %d: type %d, %s",
>> This has already been fixed as part of [1].
> It is also 'not a fix'.
> strcpy() is fine for copying literal strings into arrays.
> With the kernel headers you get a compile error from strcpy() if the string
> is too long.
> OTOH strscpy() will truncate overlong strings.
Thanks for the explanation :) .
> David
>
>> [1] https://patch.msgid.link/20260310094434.3639602-5-aichao@xxxxxxxxxx
>>
>>
>> BR,
>>
>> Amit
>>
>>