port upload-archive to Windows
v4 cover port upload-archive to Windows
Here's a new iteration of this series. I delayed it until the improved version of "enter_repo: do not modify input" hit master, which happened recently.
The important change in this iteration (besides the patch that already propagated upstream) is that I've moved compat/win32/sys/poll.[ch] out of the sys-folder (as XSI suggests). This enables us to easily upgrade the poll-emulation without breaking the Windows build in the process.
Erik Faye-Lund (3):
mingw: move poll out of sys-folder compat/win32/poll.c: upgrade from upstream
upload-archive: use start_command instead of fork
Makefile | 6 +- builtin/archive.c | 6 +- builtin/upload-archive.c | 68 ++---- compat/mingw.h | 2 - compat/win32/poll.c | 606 ++++++++++++++++++++++++++++++++++++++++++++++ compat/win32/poll.h | 53 ++++ compat/win32/sys/poll.c | 599 --------------------------------------------- compat/win32/sys/poll.h | 53 ---- t/t5000-tar-tree.sh | 10 +- 9 files changed, 694 insertions(+), 709 deletions(-) create mode 100644 compat/win32/poll.c create mode 100644 compat/win32/poll.h delete mode 100644 compat/win32/sys/poll.c delete mode 100644 compat/win32/sys/poll.h
Signature · 1 line
1.7.7.msysgit.1.1.g7b316
PATCH v4 1/3 mingw: move poll out of sys-folder
Both XSI and upstream Gnulib versions expects to find poll.h at the root of some include path, not inside the sys-folder.
This helps us when upgrading Gnulib.
--- Makefile | 6 +- compat/win32/poll.c | 599 +++++++++++++++++++++++++++++++++++++++++++++++ compat/win32/poll.h | 53 +++++ compat/win32/sys/poll.c | 599 ----------------------------------------------- compat/win32/sys/poll.h | 53 ----- 5 files changed, 656 insertions(+), 654 deletions(-) create mode 100644 compat/win32/poll.c create mode 100644 compat/win32/poll.h delete mode 100644 compat/win32/sys/poll.c delete mode 100644 compat/win32/sys/poll.h
Makefile +4 -2
@@ -1088,6 +1088,7 @@ ifeq ($(uname_S),Windows) NO_PREAD = YesPlease NEEDS_CRYPTO_WITH_SSL = YesPlease NO_LIBGEN_H = YesPlease + NO_SYS_POLL_H = YesPlease NO_SYMLINK_HEAD = YesPlease NO_IPV6 = YesPlease NO_SETENV = YesPlease
@@ -1126,7 +1127,7 @@ ifeq ($(uname_S),Windows) BASIC_CFLAGS = -nologo -I. -I../zlib -Icompat/vcbuild -Icompat/vcbuild/include -DWIN32 -D_CONSOLE -DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE COMPAT_OBJS = compat/msvc.o compat/winansi.o \ compat/win32/pthread.o compat/win32/syslog.o \ - compat/win32/sys/poll.o compat/win32/dirent.o + compat/win32/poll.o compat/win32/dirent.o COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DNOGDI -DHAVE_STRING_H -DHAVE_ALLOCA_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\" BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -SUBSYSTEM:CONSOLE -NODEFAULTLIB:MSVCRT.lib EXTLIBS = user32.lib advapi32.lib shell32.lib wininet.lib ws2_32.lib
@@ -1181,6 +1182,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) NO_PREAD = YesPlease NEEDS_CRYPTO_WITH_SSL = YesPlease NO_LIBGEN_H = YesPlease + NO_SYS_POLL_H = YesPlease NO_SYMLINK_HEAD = YesPlease NO_SETENV = YesPlease NO_UNSETENV = YesPlease
@@ -1214,7 +1216,7 @@ ifneq (,$(findstring MINGW,$(uname_S))) COMPAT_CFLAGS += -DSTRIP_EXTENSION=\".exe\" COMPAT_OBJS += compat/mingw.o compat/winansi.o \ compat/win32/pthread.o compat/win32/syslog.o \ - compat/win32/sys/poll.o compat/win32/dirent.o + compat/win32/poll.o compat/win32/dirent.o EXTLIBS += -lws2_32 PTHREAD_LIBS = X = .exe
compat/win32/poll.c new file +599 -0
599 lines not on this page · raw message
compat/win32/poll.h new file +53 -0
53 lines not on this page · raw message
compat/win32/sys/poll.c deleted +0 -599
599 lines not on this page · raw message
compat/win32/sys/poll.h deleted +0 -53
53 lines not on this page · raw message
Signature · 1 line
1.7.7.msysgit.1.1.g7b316
PATCH v4 2/3 compat/win32/poll.c: upgrade from upstream
In reply to Erik Faye-Lundpoll.c is updated from revision adc3a5b ingit://git.savannah.gnu.org/gnulib.git
The changes are applied with --whitespace=fix to reduce noise.
poll.h is not upgraded, because the most recent version now contains template-stuff that breaks compilation for us.
--- compat/win32/poll.c | 19 +++++++++++++------ 1 files changed, 13 insertions(+), 6 deletions(-)
compat/win32/poll.c +13 -6
@@ -1,7 +1,7 @@/* Emulation for poll(2) Contributed by Paolo Bonzini. -Copyright 2001-2003, 2006-2010 Free Software Foundation, Inc. +Copyright 2001-2003, 2006-2011 Free Software Foundation, Inc. This file is part of gnulib.
@@ -27,7 +27,10 @@#include <malloc.h> #include <sys/types.h> -#include "poll.h" + +/* Specification. */ +#include <poll.h> +#include <errno.h> #include <limits.h> #include <assert.h>
@@ -314,10 +317,7 @@ compute_revents (int fd, int sought, fd_set *rfds, fd_set *wfds, fd_set *efds)#endif /* !MinGW */ int -poll (pfd, nfd, timeout) -struct pollfd *pfd; -nfds_t nfd; -int timeout; +poll (struct pollfd *pfd,nfds_t nfd,int timeout) {#ifndef WIN32_NATIVE fd_set rfds, wfds, efds;
@@ -454,6 +454,7 @@ poll (pfd, nfd, timeout)if (!hEvent) hEvent =CreateEvent (NULL , FALSE, FALSE,NULL ); +restart: handle_array[0 ] = hEvent; nhandles =1 ;FD_ZERO (&rfds);
@@ -594,6 +595,12 @@ poll (pfd, nfd, timeout) rc++; } +if (!rc && timeout ==INFTIM ) + { +SwitchToThread (); + goto restart; + } +return rc;#endif }
Signature · 1 line
1.7.7.msysgit.1.1.g7b316
PATCH v4 3/3 upload-archive: use start_command instead of fork
In reply to Erik Faye-LundThe POSIX-function fork is not supported on Windows. Use our start_command API instead.
As this is the last call-site that depends on the fork-stub in compat/mingw.h, remove that as well.
Add an undocumented flag to git-archive that tells it that the action originated from a remote, so features can be disabled. Thanks to Jeff King for work on this part.
Remove the NOT_MINGW-prereq for t5000, as git-archive --remote now works.
--- builtin/archive.c | 6 +++- builtin/upload-archive.c | 68 ++++++++++++++------------------------------- compat/mingw.h | 2 - t/t5000-tar-tree.sh | 10 +++--- 4 files changed, 31 insertions(+), 55 deletions(-)
builtin/archive.c +5 -1
@@ -87,6 +87,7 @@ int cmd_archive(int argc, const char **argv, const char *prefix)const char *exec ="git-upload-archive" ;const char *output =NULL ;const char *remote =NULL ; +int is_remote =0 ;struct option local_opts[] = {OPT_STRING ('o' ,"output" , &output,"file" ,"write the archive to this file" ),
@@ -94,6 +95,9 @@ int cmd_archive(int argc, const char **argv, const char *prefix) "retrieve the archive from remote repository <repo>"),OPT_STRING (0 ,"exec" , &exec,"cmd" ,"path to the remote git-upload-archive command" ), + { OPTION_BOOLEAN, 0, "remote-request", &is_remote, NULL, +"indicate we are serving a remote request" , + PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },OPT_END () };
@@ -108,5 +112,5 @@ int cmd_archive(int argc, const char **argv, const char *prefix)setvbuf (stderr,NULL , _IOLBF,BUFSIZ ); -return write_archive (argc, argv, prefix,1 , output,0 ); +return write_archive (argc, argv, prefix,1 , output, is_remote); }
builtin/upload-archive.c +21 -47
118 lines not on this page · raw message
compat/mingw.h +0 -2
@@ -85,8 +85,6 @@ static inline int symlink(const char *oldpath, const char *newpath) { errno =ENOSYS ;return -1 ; }static inline int fchmod (int fildes,mode_t mode) { errno =ENOSYS ;return -1 ; } -static inline pid_t fork (void ) -{ errno =ENOSYS ;return -1 ; }static inline unsigned int alarm (unsigned int seconds) {return 0 ; }static inline int fsync (int fd)
t/t5000-tar-tree.sh +5 -5
@@ -96,7 +96,7 @@ test_expect_success 'git archive with --output' \ 'git archive --output=b4.tar HEAD && test_cmp b.tar b4.tar' -test_expect_success NOT_MINGW 'git archive --remote' \ +test_expect_success 'git archive --remote' \ 'git archive --remote=. HEAD >b5.tar && test_cmp b.tar b5.tar'
@@ -266,7 +266,7 @@ test_expect_success 'archive --list mentions user filter' ' grep "^bar\$" output ' -test_expect_success NOT_MINGW 'archive --list shows only enabled remote filters' ' +test_expect_success 'archive --list shows only enabled remote filters' ' git archive --list --remote=. >output && ! grep "^tar\.foo\$" output && grep "^bar\$" output
@@ -298,7 +298,7 @@ test_expect_success 'extension matching requires dot' ' test_cmp b.tar config-implicittar.foo ' -test_expect_success NOT_MINGW 'only enabled filters are available remotely' ' +test_expect_success 'only enabled filters are available remotely' ' test_must_fail git archive --remote=. --format=tar.foo HEAD \ >remote.tar.foo && git archive --remote=. --format=bar >remote.bar HEAD &&
@@ -341,12 +341,12 @@ test_expect_success GZIP,GUNZIP 'extract tgz file' ' test_cmp b.tar j.tar ' -test_expect_success GZIP,NOT_MINGW 'remote tar.gz is allowed by default' ' +test_expect_success GZIP 'remote tar.gz is allowed by default' ' git archive --remote=. --format=tar.gz HEAD >remote.tar.gz && test_cmp j.tgz remote.tar.gz ' -test_expect_success GZIP,NOT_MINGW 'remote tar.gz can be disabled' ' +test_expect_success GZIP 'remote tar.gz can be disabled' ' git config tar.tar.gz.remote false && test_must_fail git archive --remote=. --format=tar.gz HEAD \ >remote.tar.gz
Signature · 1 line
1.7.7.msysgit.1.1.g7b316
Re: [PATCH v4 3/3] upload-archive: use start_command instead of fork
Quoted 10 lines · Erik Faye-Lund
The POSIX-function fork is not supported on Windows. Use our
start_commandAPI instead.As this is the last call-site that depends on the fork-stub in
compat/mingw.h, remove that as well.Add an undocumented flag to git-archive that tells it that the action originated from a remote, so features can be disabled. Thanks to Jeff King for work on this part.
Quoted 6 lines
Remove the NOT_MINGW-prereq for t5000, as git-archive --remote now works.
Signed-off-by Erik Faye-Lund <[email protected]>Helped-by Jeff King <[email protected]>---
As with other versions of this series, I have no comment on the mingw bits. The archive.c parts still look sane to me. :)
-Peff
Re: [PATCH v4 3/3] upload-archive: use start_command instead of fork
In reply to Erik Faye-LundQuoted 6 lines · Erik Faye-Lund
The POSIX-function fork is not supported on Windows. Use our
start_commandAPI instead.As this is the last call-site that depends on the fork-stub in
compat/mingw.h, remove that as well.
Quoted 6 lines
Add an undocumented flag to git-archive that tells it that the action originated from a remote, so features can be disabled. Thanks to Jeff King for work on this part.
Remove the NOT_MINGW-prereq for t5000, as git-archive --remote now works.
I see valgrind failures bisecting to this commit, like so:
==19125== Syscall param execve(argv[i]) points to unaddressable byte(s)
==19125== at 0x5303CB7: execve (in /lib64/libc-2.11.3.so) ==19125== by 0x53045A5: execvpe (in /lib64/libc-2.11.3.so) ==19125== by 0x4B183C: execv_git_cmd (exec_cmd.c:137) ==19125== by 0x4F305E: start_command (run-command.c:277) ==19125== by 0x47F5C9: cmd_upload_archive (upload-archive.c:98) ==19125== by 0x4051F4: run_builtin (git.c:308) ==19125== by 0x40538F: handle_internal_command (git.c:466) ==19125== by 0x405556: main (git.c:553)
==19125== Address 0x7feffe7d0 is not stack'd, malloc'd or (recently) free'd
when running 'make valgrind' in current master. Let me know if you need more information.
Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.chRe: [PATCH v4 3/3] upload-archive: use start_command instead of fork
Quoted 14 lines · Thomas Rast
Quoted 2 lines
Remove the NOT_MINGW-prereq for t5000, as git-archive --remote now works.
I see valgrind failures bisecting to this commit, like so:
==19125== Syscall param
execve(argv[i])points to unaddressable byte(s)==19125== at 0x5303CB7: execve (in /lib64/libc-2.11.3.so) ==19125== by 0x53045A5: execvpe (in /lib64/libc-2.11.3.so) ==19125== by 0x4B183C: execv_git_cmd (exec_cmd.c:137) ==19125== by 0x4F305E: start_command (run-command.c:277) ==19125== by 0x47F5C9: cmd_upload_archive (upload-archive.c:98) ==19125== by 0x4051F4: run_builtin (git.c:308) ==19125== by 0x40538F: handle_internal_command (git.c:466) ==19125== by 0x405556: main (git.c:553)
Quoted 4 lines
==19125== Address
0x7feffe7d0is not stack'd, malloc'd or (recently) free'dwhen running 'make valgrind' in current master. Let me know if you need more information.
With which test, and on what OS? I couldn't replicate running t5000 on Linux.
-Peff
Re: [PATCH v4 3/3] upload-archive: use start_command instead of fork
Quoted 15 lines · Jeff King
Quoted 14 lines · Thomas Rast
I see valgrind failures bisecting to this commit, like so:
==19125== Syscall param
execve(argv[i])points to unaddressable byte(s)==19125== at 0x5303CB7: execve (in /lib64/libc-2.11.3.so) ==19125== by 0x53045A5: execvpe (in /lib64/libc-2.11.3.so) ==19125== by 0x4B183C: execv_git_cmd (exec_cmd.c:137) ==19125== by 0x4F305E: start_command (run-command.c:277) ==19125== by 0x47F5C9: cmd_upload_archive (upload-archive.c:98) ==19125== by 0x4051F4: run_builtin (git.c:308) ==19125== by 0x40538F: handle_internal_command (git.c:466) ==19125== by 0x405556: main (git.c:553)==19125== Address
0x7feffe7d0is not stack'd, malloc'd or (recently) free'd
Quoted 5 lines
Quoted 2 lines
when running 'make valgrind' in current master. Let me know if you need more information.
With which test, and on what OS? I couldn't replicate running t5000 on Linux.
11, and many others after it, on a pretty vanilla opensuse 11.4 VM I use almost exclusively for the valgrind runs. I used a pre-3.7 SVN valgrind build, but 3.6.1 (shipped with opensuse) finds the same.
But after a closer look I think this patch just prodded it enough to unearth long-existing undefined behaviour: prepare_argv() summarizes to something like
static void prepare_argv(const char **sent_argv, const char **argv)
{
char *p, buf[4096];
for (p = buf;;) {
len = packet_read_line(0, p, (buf + sizeof buf) - p);
/* ... p always points into buf ... */
sent_argv[sent_argc++] = p;
p += len;
*p++ = 0;
}
sent_argv[sent_argc] = NULL;
}The code appears to have looked like this ever since the addition of that file back in 39345a2 (Add git-upload-archive, 2006-09-07). So the elements of sent_argv have apparently always pointed into the stack-allocated 'buf'.
(This correlates with the "Address 0x7feffe7d0 is not stack'd", even though it's pretty clearly an address into the stack.)
A quick band-aid would be to heap-allocate it instead:
builtin/upload-archive.c +3 -2
@@ -22,9 +22,10 @@ static const char lostchild[] =static void prepare_argv (const char **sent_argv,const char **argv) {const char *arg_cmd ="argument " ; -char *p, buf[4096 ]; +char *p, *buf;int sent_argc;int len; + buf =xmalloc (4096 );/* put received options in sent_argv[] */ sent_argc =2 ;
@@ -32,7 +33,7 @@ static void prepare_argv(const char **sent_argv, const char **argv) sent_argv[1 ] ="--remote-request" ;for (p = buf;;) {/* This will die if not enough free space in buf */ - len =packet_read_line (0 , p, (buf +sizeof buf) - p); + len =packet_read_line (0 , p, (buf +4096 ) - p);if (len ==0 )break ;/* got a flush */ if (sent_argc >MAX_ARGS -2 )
Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.chRe: [PATCH v4 3/3] upload-archive: use start_command instead of fork
Quoted 18 lines · Thomas Rast
But after a closer look I think this patch just prodded it enough to unearth long-existing undefined behaviour:
prepare_argv()summarizes to something likestatic void
prepare_argv(const char **sent_argv, const char **argv){ char *p, buf[4096]; for (p = buf;;) { len = packet_read_line(0, p, (buf + sizeof buf) - p); /* ... p always points into buf ... */ sent_argv[sent_argc++] = p; p += len; *p++ = 0; } sent_argv[sent_argc] = NULL; }
Quoted 4 lines
The code appears to have looked like this ever since the addition of that file back in 39345a2 (Add git-upload-archive, 2006-09-07). So the elements of
sent_argvhave apparently always pointed into the stack-allocated 'buf'.
Oh, yikes. That is definitely the problem, but it does come from c09cd77e. The prepare_argv function used to be "run_upload_archive", and it would prepare argv on the stack, call into write_archive with it, and then return; nobody else cares about the value afterwards.
Erik's patch converts it into a function that writes the new argv into a parameter and returns, and the now-invalid stack-allocated memory is used by the calling function.
Quoted 1 line
A quick band-aid would be to heap-allocate it instead:
That works. An even shorter band-aid is to mark it as "static".
I think the code would be more readable if it just used the new argv_array.
Junio, this bug is in 1.7.8-rc*. Do you want the one-liner fix for the release, or the nicer fix?
-Peff
Re: [PATCH v4 3/3] upload-archive: use start_command instead of fork
Quoted 2 lines · Jeff King
Junio, this bug is in 1.7.8-rc*. Do you want the one-liner fix for the release, or the nicer fix?
Let's just do "static" for now, if we know the array is large enough.
Re: [PATCH v4 3/3] upload-archive: use start_command instead of fork
Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Jeff King
Junio, this bug is in 1.7.8-rc*. Do you want the one-liner fix for the release, or the nicer fix?
Let's just do "static" for now, if we know the array is large enough.
OK, here it is.
I think it's correct, but I couldn't reproduce the valgrind failure here. Thomas, can you double check that this also solves your problem?
-Peff
-- >8 --
Subject: [PATCH] upload-archive: don't return pointers to stack buffer
The prepare_argv function uses an internal stack-allocated buffer to create the argv array that will be used to run git-archive. Prior to c09cd77e, this was OK, as the function passed the argv array to write_archive itself, and the stack buffer was valid during its use.
Since c09cd77e, however, the function returns an argv array with pointers pointing into the stack buffer. The calling function then passes the result to start_command, which tries to execve using pointers to a now-invalid buffer.
Fix it by making the buffer static, which is a quick and simple fix, and works because we only run this function once per process.
Credit for finding the bug and most of the analysis goes to Thomas Rast.
--- builtin/upload-archive.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
builtin/upload-archive.c +2 -1
@@ -22,7 +22,8 @@static void prepare_argv (const char **sent_argv,const char **argv) {const char *arg_cmd ="argument " ; -char *p, buf[4096 ]; +char *p; +static char buf[4096 ];int sent_argc;int len;
Signature · 1 line
1.7.7.3.8.g38efa
PATCH 1/2 upload-archive: drop extra argument to prepare_argv
We pass a "sent_argv" which is an out-parameter to hold the argv that we were sent over the wire. But we also pass in the "argv" we got on the command line, which is not used at all. Drop this useless and confusing parameter.
The parameter was obsoleted by c09cd77e, which moved the enter_repo function (which looked at argv[1]) out of prepare_argv and into cmd_upload_archive.
---
Quoted 3 lines · Jeff King
Quoted 1 line
Let's just do "static" for now, if we know the array is large enough.
OK, here it is.
And here's the other more invasive cleanup on top (patch 2 is the meaty one).
builtin/upload-archive.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
builtin/upload-archive.c +2 -2
@@ -19,7 +19,7 @@#define MAX_ARGS (64) -static void prepare_argv (const char **sent_argv,const char **argv) +static void prepare_argv (const char **sent_argv) {const char *arg_cmd ="argument " ;char *p;
@@ -95,7 +95,7 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)if (!enter_repo (argv[1 ],0 ))die ("'%s' does not appear to be a git repository" , argv[1 ]); -prepare_argv (sent_argv, argv); +prepare_argv (sent_argv);if (start_command (&cld)) {int err = errno;packet_write (1 ,"NACK fork failed on the remote side\n" );
Signature · 1 line
1.7.7.3.8.g38efa
PATCH 1/1 upload-archive: use argv_array for sent parameters
In reply to Jeff KingThe existing prepare_argv uses a fixed-size static buffer to hold all of the arguments, and then puts pointers into the buffer into a fixed-size array. Using argv_array gets rid of all of the manual bookkeeping and makes the code more readable.
It also lifts the static limits on the size of the array. This is convenient, but is perhaps a security regression, as a malicious client can now ask us to create arbitrary-length argv arrays in memory.
---
I think the code is way more readable and obvious. Do we care about the potential DoS? If so, we can still cap the number of arguments we'll accept. In practice, we never send more than a few.
builtin/upload-archive.c | 45 +++++++++++++++++---------------------------- 1 files changed, 17 insertions(+), 28 deletions(-)
builtin/upload-archive.c +17 -28
@@ -7,6 +7,7 @@#include "pkt-line.h" #include "sideband.h" #include "run-command.h" +#include "argv-array.h" static const char upload_archive_usage[] ="git upload-archive <repo>" ;
@@ -17,42 +18,28 @@static const char lostchild[] ="git upload-archive: archiver process was lost" ; -#define MAX_ARGS (64) - -static void prepare_argv (const char **sent_argv) +static void prepare_argv (struct argv_array *out) {const char *arg_cmd ="argument " ; -char *p; -static char buf[4096 ]; -int sent_argc; -int len; -/* put received options in sent_argv[] */ - sent_argc =2 ; - sent_argv[0 ] ="archive" ; - sent_argv[1 ] ="--remote-request" ; -for (p = buf;;) { +argv_array_push (out,"archive" ); +argv_array_push (out,"--remote-request" ); +while (1 ) { +char buf[4096 ]; +int len;/* This will die if not enough free space in buf */ - len =packet_read_line (0 , p, (buf +sizeof buf) - p); + len =packet_read_line (0 , buf,sizeof (buf));if (len ==0 )break ;/* got a flush */ -if (sent_argc >MAX_ARGS -2 ) -die ("Too many options (>%d)" ,MAX_ARGS -2 ); -if (p[len-1 ] =='\n' ) { - p[--len] =0 ; - } +if (buf[len-1 ] =='\n' ) + buf[--len] =0 ;if (len <strlen (arg_cmd) || -strncmp (arg_cmd, p,strlen (arg_cmd))) +strncmp (arg_cmd, buf,strlen (arg_cmd)))die ("'argument' token or flush expected" ); - len -=strlen (arg_cmd); -memmove (p, p +strlen (arg_cmd), len); - sent_argv[sent_argc++] = p; - p += len; - *p++ =0 ; +argv_array_push (out, buf +strlen (arg_cmd)); } - sent_argv[sent_argc] =NULL ; } __attribute__((format (printf, 1, 2)))
@@ -84,8 +71,8 @@ static ssize_t process_input(int child_fd, int band)int cmd_upload_archive (int argc,const char **argv,const char *prefix) { -const char *sent_argv[MAX_ARGS ]; -struct child_process cld = { sent_argv }; +struct argv_array sent_argv =ARGV_ARRAY_INIT ; +struct child_process cld = {0 }; cld.out = cld.err =-1 ; cld.git_cmd =1 ;
@@ -95,7 +82,8 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)if (!enter_repo (argv[1 ],0 ))die ("'%s' does not appear to be a git repository" , argv[1 ]); -prepare_argv (sent_argv); +prepare_argv (&sent_argv); + cld.argv = sent_argv.argv;if (start_command (&cld)) {int err = errno;packet_write (1 ,"NACK fork failed on the remote side\n" );
@@ -138,5 +126,6 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)packet_flush (1 );break ; } +argv_array_clear (&sent_argv);return 0 ; }
Signature · 1 line
1.7.7.3.8.g38efa
Re: [PATCH] upload-archive: use argv_array for sent parameters
Quoted 6 lines · Jeff King
The existing
prepare_argvuses a fixed-size static buffer to hold all of the arguments, and then puts pointers into the buffer into a fixed-size array. Usingargv_arraygets rid of all of the manual bookkeeping and makes the code more readable.
Quoted 6 lines
It also lifts the static limits on the size of the array. This is convenient, but is perhaps a security regression, as a malicious client can now ask us to create arbitrary-length argv arrays in memory.
Signed-off-by Jeff King <[email protected]>
Sorry, I failed to mark the subject properly. If it was not obvious, this is patch 2/2 of the cleanup.
-Peff
Re: [PATCH v4 3/3] upload-archive: use start_command instead of fork
In reply to Jeff KingQuoted 25 lines · Jeff King
Quoted 18 lines · Thomas Rast
But after a closer look I think this patch just prodded it enough to unearth long-existing undefined behaviour:
prepare_argv()summarizes to something likestatic void
prepare_argv(const char **sent_argv, const char **argv){
char *p, buf[4096];for (p = buf;;) {
len =packet_read_line(0, p, (buf + sizeof buf) - p); /* ... p always points into buf ... */ sent_argv[sent_argc++] = p;
p += len;
*p++ = 0;
}
sent_argv[sent_argc] = NULL;
}Quoted 4 lines
The code appears to have looked like this ever since the addition of that file back in 39345a2 (Add git-upload-archive, 2006-09-07). So the elements of
sent_argvhave apparently always pointed into the stack-allocated 'buf'.
Quoted 9 lines
Oh, yikes. That is definitely the problem, but it does come from
c09cd77e. Theprepare_argvfunction used to be "run_upload_archive", and it would prepare argv on the stack, call intowrite_archivewith it, and then return; nobody else cares about the value afterwards.Erik's patch converts it into a function that writes the new argv into a parameter and returns, and the now-invalid stack-allocated memory is used by the calling function.
Outch. Thanks for spotting.
Quoted 3 lines
Quoted 1 line
A quick band-aid would be to heap-allocate it instead:
That works. An even shorter band-aid is to mark it as "static".
Hmm, I seem to remember spotting it myself at some point and fixing it by marking it as static. I guess I must have forgot to push it...
Quoted 3 lines
I think the code would be more readable if it just used the new
argv_array.
Oooh, nice. The whole argv_array slipped past me, I like it!
Re: [PATCH v4 3/3] upload-archive: use start_command instead of fork
Quoted 4 lines · Erik Faye-Lund
Quoted 3 lines
I think the code would be more readable if it just used the new
argv_array.Oooh, nice. The whole
argv_arrayslipped past me, I like it!
I don't think it existed when you started on this series. :)
-Peff
Re: [PATCH v4 3/3] upload-archive: use start_command instead of fork
In reply to Thomas RastQuoted 23 lines · Thomas Rast
But after a closer look I think this patch just prodded it enough to unearth long-existing undefined behaviour:
prepare_argv()summarizes to something likestatic void
prepare_argv(const char **sent_argv, const char **argv){ char *p, buf[4096]; for (p = buf;;) { len = packet_read_line(0, p, (buf + sizeof buf) - p); /* ... p always points into buf ... */ sent_argv[sent_argc++] = p; p += len; *p++ = 0; } sent_argv[sent_argc] = NULL; }The code appears to have looked like this ever since the addition of that file back in 39345a2 (Add git-upload-archive, 2006-09-07). So the elements of
sent_argvhave apparently always pointed into the stack-allocated 'buf'.
Quoted 4 lines
(This correlates with the "Address
0x7feffe7d0is not stack'd", even though it's pretty clearly an address into the stack.)A quick band-aid would be to heap-allocate it instead:
Or allocate it in the caller:
builtin/upload-archive.c +6 -4
@@ -18,11 +18,12 @@ static const char lostchild[] ="git upload-archive: archiver process was lost" ;#define MAX_ARGS (64) +#define ARGV_BUF_SIZE 4096 -static void prepare_argv (const char **sent_argv,const char **argv) +static void prepare_argv (const char **sent_argv,char *buf,const char **argv) {const char *arg_cmd ="argument " ; -char *p, buf[4096 ]; +char *p;int sent_argc;int len;
@@ -32,7 +33,7 @@ static void prepare_argv(const char **sent_argv, const char **argv) sent_argv[1 ] ="--remote-request" ;for (p = buf;;) {/* This will die if not enough free space in buf */ - len =packet_read_line (0 , p, (buf +sizeof buf) - p); + len =packet_read_line (0 , p, (buf +ARGV_BUF_SIZE ) - p);if (len ==0 )break ;/* got a flush */ if (sent_argc >MAX_ARGS -2 )
@@ -85,6 +86,7 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix) {const char *sent_argv[MAX_ARGS ];struct child_process cld = { sent_argv }; +char argv_buf[ARGV_BUF_SIZE ]; cld.out = cld.err =-1 ; cld.git_cmd =1 ;
@@ -94,7 +96,7 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)if (!enter_repo (argv[1 ],0 ))die ("'%s' does not appear to be a git repository" , argv[1 ]); -prepare_argv (sent_argv, argv); +prepare_argv (sent_argv, argv_buf, argv);if (start_command (&cld)) {int err = errno;packet_write (1 ,"NACK fork failed on the remote side\n" );
Signature · 9 lines
1.7.7.3 Andreas. -- Andreas Schwab, [email protected] GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."