mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Randy Dunlap <rdunlap@infradead.org>
To: Bagas Sanjaya <bagasdotme@gmail.com>,
	James Dutton <james.dutton@gmail.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Cc: Justin Stitt <justinstitt@google.com>,
	Calvince Otieno <calvncce@gmail.com>,
	Azeem Shaikh <azeemshaikh38@gmail.com>,
	Kees Cook <keescook@chromium.org>,
	Andy Shevchenko <andy@kernel.org>
Subject: Re: Is strncpy really less secure than strscpy ?
Date: Wed, 18 Oct 2023 19:27:20 -0700	[thread overview]
Message-ID: <cf6950bc-32c8-459c-a4b1-ca0a291fc2f8@infradead.org> (raw)
In-Reply-To: <ZTCLLinnaqIILXsJ@debian.me>



On 10/18/23 18:49, Bagas Sanjaya wrote:
> [Disclaimer: I have little to no knowledge of C, so things may be wrong.
>  Please correct me if it is the case. Also Cc: recent people who work on
>  strscpy() conversion.]
> 

Also Cc: the STRING maintainers.

> On Thu, Oct 19, 2023 at 12:22:33AM +0100, James Dutton wrote:
>> Is strncpy really less secure than strscpy ?
>>
>> If one uses strncpy and thus put a limit on the buffer size during the
>> copy, it is safe. There are no writes outside of the buffer.
>> If one uses strscpy and thus put a limit on the buffer size during the
>> copy, it is safe. There are no writes outside of the buffer.
> 
> Well, assuming that the string is NUL-terminated, the end result should
> be the same.
> 
>> But, one can fit more characters in strncpy than strscpy because
>> strscpy enforces the final \0 on the end.
>> One could argue that strncpy is better because it might save the space
>> of one char at the end of a string array.
>> There are cases where strncpy might be unsafe. For example copying
>> between arrays of different sizes, and that is a case where strscpy
>> might be safer, but strncpy can be made safe if one ensures that the
>> size used in strncpy is the smallest of the two different array sizes.
> 
> Code example on both cases?
> 
>>
>> If one blindly replaces strncpy with strscpy across all uses, one
>> could unintentionally be truncating the results and introduce new
>> bugs.
>>
>> The real insecurity surely comes when one tries to use the string.
>> For example:
>>
>> #include <stdio.h>
>> #include <string.h>
>>
>> int main() {
>>         char a[10] = "HelloThere";
>>         char b[10];
>>         char c[10] = "Overflow";
>>         strncpy(b, a, 10);
>>         /* This overflows and so in unsafe */
>>         printf("a is  %s\n", a);
>>         /* This overflows and so in unsafe */
>>         printf("b is  %s\n", b);
>>         /* This is safe */
>>         printf("b is  %.*s\n", 10, a);
>>         /* This is safe */
>>         printf("b is  %.*s\n", 4, a);
>>         return 0;
>> }
> 
> What if printf("a is  %.*s\n", a);?
> 

>>
>>
>> So, why isn't the printk format specifier "%.*s" used more instead of
>> "%s" in the kernel?
> 
> Since basically strings are pointers.
> 
> Thanks.
> 

-- 
~Randy

  reply	other threads:[~2023-10-19  2:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-18 23:22 James Dutton
2023-10-19  1:49 ` Bagas Sanjaya
2023-10-19  2:27   ` Randy Dunlap [this message]
2023-10-19  2:56     ` Kees Cook
2023-10-19  3:40       ` Bagas Sanjaya
2023-10-19 17:09       ` Justin Stitt
2023-10-19 18:13     ` James Dutton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cf6950bc-32c8-459c-a4b1-ca0a291fc2f8@infradead.org \
    --to=rdunlap@infradead.org \
    --cc=andy@kernel.org \
    --cc=azeemshaikh38@gmail.com \
    --cc=bagasdotme@gmail.com \
    --cc=calvncce@gmail.com \
    --cc=james.dutton@gmail.com \
    --cc=justinstitt@google.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®