<feed xmlns='http://www.w3.org/2005/Atom'>
<title>git/run-command.c, branch v2.13.2</title>
<subtitle>Mirror of https://git.kernel.org/pub/scm/git/git.git/
</subtitle>
<id>https://git.shady.money/git/atom?h=v2.13.2</id>
<link rel='self' href='https://git.shady.money/git/atom?h=v2.13.2'/>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/'/>
<updated>2017-04-26T06:17:36Z</updated>
<entry>
<title>run-command: restrict PATH search to executable files</title>
<updated>2017-04-26T06:17:36Z</updated>
<author>
<name>Brandon Williams</name>
<email>bmwill@google.com</email>
</author>
<published>2017-04-25T23:47:00Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=940283101ce87250cf31a592730386f5061e1286'/>
<id>urn:sha1:940283101ce87250cf31a592730386f5061e1286</id>
<content type='text'>
In some situations run-command will incorrectly try (and fail) to
execute a directory instead of an executable file.  This was observed by
having a directory called "ssh" in $PATH before the real ssh and trying
to use ssh protoccol, reslting in the following:

	$ git ls-remote ssh://url
	fatal: cannot exec 'ssh': Permission denied

It ends up being worse and run-command will even try to execute a
non-executable file if it preceeds the executable version of a file on
the PATH.  For example, if PATH=~/bin1:~/bin2:~/bin3 and there exists a
directory 'git-hello' in 'bin1', a non-executable file 'git-hello' in
bin2 and an executable file 'git-hello' (which prints "Hello World!") in
bin3 the following will occur:

	$ git hello
	fatal: cannot exec 'git-hello': Permission denied

This is due to only checking 'access()' when locating an executable in
PATH, which doesn't distinguish between files and directories.  Instead
use 'is_executable()' which check that the path is to a regular,
executable file.  Now run-command won't try to execute the directory or
non-executable file 'git-hello':

	$ git hello
	Hello World!

which matches what execvp(3) would have done when asked to execute
git-hello with such a $PATH.

Reported-by: Brian Hatfield &lt;bhatfield@google.com&gt;
Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Reviewed-by: Jonathan Nieder &lt;jrnieder@gmail.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>run-command: expose is_executable function</title>
<updated>2017-04-26T01:45:29Z</updated>
<author>
<name>Brandon Williams</name>
<email>bmwill@google.com</email>
</author>
<published>2017-04-25T23:46:59Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=38124a40e480c1717326b7bc27bcbca758de908e'/>
<id>urn:sha1:38124a40e480c1717326b7bc27bcbca758de908e</id>
<content type='text'>
Move the logic for 'is_executable()' from help.c to run_command.c and
expose it so that callers from outside help.c can access the function.
This is to enable run-command to be able to query if a file is
executable in a future patch.

Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Reviewed-by: Jonathan Nieder &lt;jrnieder@gmail.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>run-command: block signals between fork and execve</title>
<updated>2017-04-21T00:55:32Z</updated>
<author>
<name>Eric Wong</name>
<email>e@80x24.org</email>
</author>
<published>2017-04-19T23:13:27Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=45afb1ca9c28855096c94926e5b16dfbcde7381f'/>
<id>urn:sha1:45afb1ca9c28855096c94926e5b16dfbcde7381f</id>
<content type='text'>
Signal handlers of the parent firing in the forked child may
have unintended side effects.  Rather than auditing every signal
handler we have and will ever have, block signals while forking
and restore default signal handlers in the child before execve.

Restoring default signal handlers is required because
execve does not unblock signals, it only restores default
signal handlers.  So we must restore them with sigprocmask
before execve, leaving a window when signal handlers
we control can fire in the child.  Continue ignoring
ignored signals, but reset the rest to defaults.

Similarly, disable pthread cancellation to future-proof our code
in case we start using cancellation; as cancellation is
implemented with signals in glibc.

Signed-off-by: Eric Wong &lt;e@80x24.org&gt;
Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>run-command: add note about forking and threading</title>
<updated>2017-04-21T00:55:32Z</updated>
<author>
<name>Brandon Williams</name>
<email>bmwill@google.com</email>
</author>
<published>2017-04-19T23:13:26Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=e503cd6ed336d70d716e194ef6c5469330bea9da'/>
<id>urn:sha1:e503cd6ed336d70d716e194ef6c5469330bea9da</id>
<content type='text'>
All non-Async-Signal-Safe functions (e.g. malloc and die) were removed
between 'fork' and 'exec' in start_command in order to avoid potential
deadlocking when forking while multiple threads are running.  This
deadlocking is possible when a thread (other than the one forking) has
acquired a lock and didn't get around to releasing it before the fork.
This leaves the lock in a locked state in the resulting process with no
hope of it ever being released.

Add a note describing this potential pitfall before the call to 'fork()'
so people working in this section of the code know to only use
Async-Signal-Safe functions in the child process.

Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>run-command: handle dup2 and close errors in child</title>
<updated>2017-04-21T00:55:32Z</updated>
<author>
<name>Brandon Williams</name>
<email>bmwill@google.com</email>
</author>
<published>2017-04-19T23:13:25Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=53fa6753b30c4fb4ea768d16d41d723ea19a3b00'/>
<id>urn:sha1:53fa6753b30c4fb4ea768d16d41d723ea19a3b00</id>
<content type='text'>
Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>run-command: eliminate calls to error handling functions in child</title>
<updated>2017-04-21T00:55:32Z</updated>
<author>
<name>Brandon Williams</name>
<email>bmwill@google.com</email>
</author>
<published>2017-04-19T23:13:24Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=79319b1949f0055bd42bac7fa398fca8c2f26116'/>
<id>urn:sha1:79319b1949f0055bd42bac7fa398fca8c2f26116</id>
<content type='text'>
All of our standard error handling paths have the potential to
call malloc or take stdio locks; so we must avoid them inside
the forked child.

Instead, the child only writes an 8 byte struct atomically to
the parent through the notification pipe to propagate an error.
All user-visible error reporting happens from the parent;
even avoiding functions like atexit(3) and exit(3).

Helped-by: Eric Wong &lt;e@80x24.org&gt;
Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>run-command: don't die in child when duping /dev/null</title>
<updated>2017-04-21T00:55:32Z</updated>
<author>
<name>Brandon Williams</name>
<email>bmwill@google.com</email>
</author>
<published>2017-04-19T23:13:23Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=db015a284e74b93db9184d39eb0be749e631242d'/>
<id>urn:sha1:db015a284e74b93db9184d39eb0be749e631242d</id>
<content type='text'>
Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>run-command: prepare child environment before forking</title>
<updated>2017-04-21T00:55:32Z</updated>
<author>
<name>Brandon Williams</name>
<email>bmwill@google.com</email>
</author>
<published>2017-04-19T23:13:22Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=ae25394b4c21f072f176ccbaf7047abe7fa5f04d'/>
<id>urn:sha1:ae25394b4c21f072f176ccbaf7047abe7fa5f04d</id>
<content type='text'>
In order to avoid allocation between 'fork()' and 'exec()' prepare the
environment to be used in the child process prior to forking.

Switch to using 'execve()' so that the construct child environment can
used in the exec'd process.

Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>run-command: use the async-signal-safe execv instead of execvp</title>
<updated>2017-04-21T00:55:32Z</updated>
<author>
<name>Brandon Williams</name>
<email>bmwill@google.com</email>
</author>
<published>2017-04-19T23:13:20Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=e3a434468fecca7c14a6bef32050dfa60534fde6'/>
<id>urn:sha1:e3a434468fecca7c14a6bef32050dfa60534fde6</id>
<content type='text'>
Convert the function used to exec from 'execvp()' to 'execv()' as the (p)
variant of exec isn't async-signal-safe and has the potential to call malloc
during the path resolution it performs.  Instead we simply do the path
resolution ourselves during the preparation stage prior to forking.  There also
don't exist any portable (p) variants which also take in an environment to use
in the exec'd process.  This allows easy migration to using 'execve()' in a
future patch.

Also, as noted in [1], in the event of an ENOEXEC the (p) variants of
exec will attempt to execute the command by interpreting it with the
'sh' utility.  To maintain this functionality, if 'execv()' fails with
ENOEXEC, start_command will atempt to execute the command by
interpreting it with 'sh'.

[1] http://pubs.opengroup.org/onlinepubs/009695399/functions/exec.html

Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>run-command: prepare command before forking</title>
<updated>2017-04-21T00:55:32Z</updated>
<author>
<name>Brandon Williams</name>
<email>bmwill@google.com</email>
</author>
<published>2017-04-19T23:13:19Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=3967e25be11ab96ced71f16b9f082de270a518db'/>
<id>urn:sha1:3967e25be11ab96ced71f16b9f082de270a518db</id>
<content type='text'>
According to [1] we need to only call async-signal-safe operations between fork
and exec.  Using malloc to build the argv array isn't async-signal-safe.

In order to avoid allocation between 'fork()' and 'exec()' prepare the
argv array used in the exec call prior to forking the process.

[1] http://pubs.opengroup.org/onlinepubs/009695399/functions/fork.html

Signed-off-by: Brandon Williams &lt;bmwill@google.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
</feed>
