git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Use strncpy to protect from buffer overruns.

From
Steven Michalske <smichalske@gmail.com>
Date
Jun 9, 2010, 20:42 UTC
Message-ID
<1E40D9E3-5459-4D29-9D6D-A6528FF8407F@gmail.com>
In-Reply-To
<AANLkTikQpJa9gzj1riKF79MflGAKv9IqJVXpICgHLXAG@mail.gmail.com>
On Jun 9, 2010, at 12:31 PM, Alex Riesen wrote:
Show 18 quoted lines
> On Wed, Jun 9, 2010 at 20:25, Steven Michalske <smichalske@gmail.com> wrote:
>>> On Wed, Jun 9, 2010 at 12:22, Steven Michalske <smichalske@gmail.com> wrote:
>>>> is_git_directory() uses strcpy with pointer arithmitic, protect it from
>>>> overflowing.  Even though we currently protect higher up when we have the
>>>> environment variable path passed in, we should protect the calls here.
>>> 
>>> Why? The function is static.
>>> 
>> The code might be locally constrained.
>> 
>> I always assume that a bit of code can be overwritten from other portions of code.
>> 
>> A small vulnerability is discovered that lets an attacker remove the length check
>> or edit the pointer in the function call, but could not squeeze in the full shell code
>> snippet.  But the now edited function here lets you put in arbitrarily long code.
> 
> Eh?
> 
Basically the protection is not robust against malicious code.  It's armored with leather, not the modern full body armor.
Show 14 quoted lines
>>>> -       strcpy(path, suspect);
>>>> +       path[sizeof(path) - 1] = '\0';
>>>> +
>>>> +       strncpy(path, suspect, sizeof(path) - 1);
>>> 
>>> And we have strlcpy for such things.
>> 
>> It is not portable.
> 
> Git has its own copy of the function:
> 
>  $ git ls-files *strlcpy.c
> 
>  $
Good to know, I could refactor with this.
Previous: Alex Riesen
Message 5 of 5 in “Use strncpy to protect from buffer overruns.”
  1. Use strncpy to protect from buffer overruns.Steven Michalske, Jun 9, 2010
  2. Alex RiesenJun 9, 2010
  3. Steven MichalskeJun 9, 2010
  4. Alex RiesenJun 9, 2010
  5. Steven MichalskeJun 9, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.