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

Re: [PATCH v2] builtin/mv.c: fix possible segfault in add_slash()

From
Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>
Date
Sep 9, 2022, 22:40 UTC
Message-ID
<150fabf7-0b5b-9029-0d60-f83885c0cc41@gmail.com>
In-Reply-To
<xmqqo7voiaaf.fsf@gitster.g>
On 9/9/2022 1:04 PM, Junio C Hamano wrote:
Show 22 quoted lines
> Shaoxuan Yuan <shaoxuan.yuan02@gmail.com> writes:
> 
>> A possible segfault was introduced in c08830de41 (mv: check if
>> <destination> is a SKIP_WORKTREE_DIR, 2022-08-09).
>>
>> When running t7001 with SANITIZE=address, problem appears when running:
>>
>> 	git mv path1/path2/ .
>> or
>> 	git mv directory ../
>> or
>> 	any <destination> that makes dest_path[0] an empty string.
>>
>> The add_slash() call could segfault when dest_path[0] is an empty string,
>> because it was accessing a null value in such case.
> 
> Terminology.  The relevant preimage is
> 
>>  	size_t len = strlen(path);
>> -	if (path[len - 1] != '/') {
> 
> An access to path[-1] is an out-of-bounds access.
Thanks for the term, new thing learned :-)
Show 13 quoted lines
>> Change add_slash() to check the path argument is a non-empty string
>> before accessing its value. If the path is empty, return it as-is.
> 
> That is not wrong per-se, but...
> 
>> Explanation:
> 
> ... you'd need this funny label here.  If this is where your
> explanation begins, what was the reader reading before it? ;-)
> 
> The logic would flow more naturally if you added your "explanation"
> material between "what is wrong in the current code" and "what to do
> to fix it", perhaps like so:
Indeed, explanation before action sounds more reasonable.
Show 15 quoted lines
> 	... could segfault when path argument to it is an empty
> 	string, because it makes an out-of-bounds read to decide if
> 	an extra slash '/' needs to be appended to it.
> 
> 	As add_slash() is used to make sure that a valid pathname to
> 	a file in the given directory can be made by appending a
> 	filename after the value returned from it, if path is an
> 	empty string, we want to return it as-is.  The path to a
> 	file "F" in the top-level of the working tree (i.e.
> 	path=="") is formed by appending "F" after "" (i.e. path)
> 	without any slash in between.
> 
> 	So, just like the case where a non-empty path already ends
> 	with a slash, return an empty path as-is.
> 
Thanks for the paraphrase, I put it in the v3 just sent.
Show 16 quoted lines
>> diff --git a/builtin/mv.c b/builtin/mv.c
>> index 2d64c1e80f..3413ad1c9b 100644
>> --- a/builtin/mv.c
>> +++ b/builtin/mv.c
>> @@ -71,7 +71,7 @@ static const char **internal_prefix_pathspec(const char *prefix,
>>  static const char *add_slash(const char *path)
>>  {
>>  	size_t len = strlen(path);
>> -	if (path[len - 1] != '/') {
>> +	if (len && path[len - 1] != '/') {
>>  		char *with_slash = xmalloc(st_add(len, 2));
>>  		memcpy(with_slash, path, len);
>>  		with_slash[len++] = '/';
> 
> Yup.  It cannot be seen in the patch but the post-context of this
> hunk just returns path as-is, which is what we want to happen.
Yes.

Thanks, Shaoxuan

Previous: Junio C HamanoNext: Shaoxuan Yuan
Message 7 of 9 in “builtin/mv.c: fix possible segfault in add_slash()”
  1. builtin/mv.c: fix possible segfault in add_slash()Shaoxuan Yuan, Sep 8, 2022
  2. Jeff KingSep 9, 2022
  3. Derrick StoleeSep 9, 2022
  4. Junio C HamanoSep 9, 2022
  5. builtin/mv.c: fix possible segfault in add_slash()Shaoxuan Yuan, Sep 9, 2022
  6. Junio C HamanoSep 9, 2022
  7. Shaoxuan YuanSep 9, 2022
  8. builtin/mv.c: fix possible segfault in add_slash()Shaoxuan Yuan, Sep 9, 2022
  9. Junio C HamanoSep 9, 2022

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.