setenv/putenv errors
v2 cover setenv/putenv errors
Here's v2 of this series. It grew a bit since the last round, as I figured fixing the error-paths in compat/setenv.c made sense to do while I was at it, considering how Windows (which is one of the few likely platforms to be able to actually trigger this issue) use it.
Erik Faye-Lund (4):
compat/setenv.c: update errno when erroring out
compat/setenv.c: error if name contains '='
wrapper: supply xsetenv and xputenv
use wrapper for unchecked setenv/putenv calls
builtin/clone.c | 2 +- builtin/commit.c | 6 +++--- builtin/help.c | 4 ++-- builtin/init-db.c | 2 +- builtin/merge.c | 4 ++-- builtin/notes.c | 2 +- builtin/remote-ext.c | 4 ++-- builtin/revert.c | 2 +- compat/setenv.c | 10 ++++++++-- config.c | 2 +- exec_cmd.c | 4 ++-- git-compat-util.h | 2 ++ git.c | 18 +++++++++--------- pager.c | 2 +- run-command.c | 2 +- setup.c | 6 +++--- wrapper.c | 14 ++++++++++++++ 17 files changed, 54 insertions(+), 32 deletions(-)
Signature · 1 line
1.7.7.1.msysgit.0.272.g9e47e
PATCH v2 1/4 compat/setenv.c: update errno when erroring out
Previously, gitsetenv didn't update errno as it should when erroring out. Fix this.
--- compat/setenv.c | 10 ++++++++-- 1 files changed, 8 insertions(+), 2 deletions(-)
compat/setenv.c +8 -2
@@ -6,7 +6,10 @@ int gitsetenv(const char *name, const char *value, int replace)size_t namelen, valuelen;char *envstr; -if (!name || !value)return -1 ; +if (!name || !value) { + errno =EINVAL ; +return -1 ; + }if (!replace) {char *oldval =NULL ; oldval =getenv (name);
@@ -16,7 +19,10 @@ int gitsetenv(const char *name, const char *value, int replace) namelen =strlen (name); valuelen =strlen (value); envstr =malloc ((namelen + valuelen +2 )); -if (!envstr)return -1 ; +if (!envstr) { + errno =ENOMEM ; +return -1 ; + }memcpy (envstr, name, namelen); envstr[namelen] ='=' ;
Signature · 1 line
1.7.7.1.msysgit.0.272.g9e47e
PATCH v2 2/4 compat/setenv.c: error if name contains '='
In reply to Erik Faye-LundAccording to POSIX, setenv should error out with EINVAL if it's asked to set an environment variable whose name contains an equals sign. Implement this detail in our compatibility-fallback.
--- compat/setenv.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
compat/setenv.c +1 -1
@@ -6,7 +6,7 @@ int gitsetenv(const char *name, const char *value, int replace)size_t namelen, valuelen;char *envstr; -if (!name || !value) { +if (!name ||strchr (name,'=' ) || !value) { errno =EINVAL ;return -1 ; }
Signature · 1 line
1.7.7.1.msysgit.0.272.g9e47e
PATCH v2 3/4 wrapper: supply xsetenv and xputenv
In reply to Erik Faye-Lund--- git-compat-util.h | 2 ++ wrapper.c | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 0 deletions(-)
git-compat-util.h +2 -0
@@ -439,6 +439,8 @@ extern int xdup(int fd);extern FILE *xfdopen (int fd,const char *mode);extern int xmkstemp (char *template);extern int xmkstemp_mode (char *template,int mode); +extern int xsetenv (const char *name,const char *val,int override); +extern int xputenv (const char *string);extern int odb_mkstemp (char *template,size_t limit,const char *pattern);extern int odb_pack_keep (char *name,size_t namesz,unsigned char *sha1);
wrapper.c +14 -0
@@ -381,3 +381,17 @@ int remove_or_warn(unsigned int mode, const char *file) {return S_ISGITLINK (mode) ?rmdir_or_warn (file) :unlink_or_warn (file); } + +int xsetenv (const char *name,const char *val,int overwrite) +{ +if (setenv (name, val, overwrite)) +die_errno ("setenv failed to set '%s' to '%s'" , name, val); +return 0 ; +} + +int xputenv (const char *string) +{ +if (putenv (string)) +die_errno ("putenv failed to set '%s' " , string); +return 0 ; +}
Signature · 1 line
1.7.7.1.msysgit.0.272.g9e47e
Re: [PATCH v2 3/4] wrapper: supply xsetenv and xputenv
git-compat-util.h extern int xdup(int fd); · line 443
Quoted 2 lines · Erik Faye-Lund
git-compat-util.h +2
-0+extern int xsetenv (const char *name,const char *val,int override); +extern int xputenv (const char *string);
Actually, putenv(3) takes "char *string".
I adjusted 3 & 4 locally before queuing, so there is no need for resend, and judging from the later part of the discussion, it seems that it may be better to use only the first two patches from this series after all.
Re: [PATCH v2 3/4] wrapper: supply xsetenv and xputenv
Quoted 6 lines · Junio C Hamano
git-compat-util.h extern int xdup(int fd); · line 443
Quoted 2 lines · Erik Faye-Lund
git-compat-util.h +2
-0+extern int xsetenv (const char *name,const char *val,int override); +extern int xputenv (const char *string);Actually, putenv(3) takes "char *string".
Ah, thanks for spotting.
Quoted 3 lines
I adjusted 3 & 4 locally before queuing, so there is no need for resend, and judging from the later part of the discussion, it seems that it may be better to use only the first two patches from this series after all.
Yeah, I agree; patch 1 and 2 are the only ones that makes sense.
PATCH v2 4/4 use wrapper for unchecked setenv/putenv calls
In reply to Erik Faye-LundThis avoids us from accidentally dropping state, possibly leading to unexpected behaviour.
This is especially important on Windows, where the maximum size of the environment is 32 kB.
--- builtin/clone.c | 2 +- builtin/commit.c | 6 +++--- builtin/help.c | 4 ++-- builtin/init-db.c | 2 +- builtin/merge.c | 4 ++-- builtin/notes.c | 2 +- builtin/remote-ext.c | 4 ++-- builtin/revert.c | 2 +- config.c | 2 +- exec_cmd.c | 4 ++-- git.c | 18 +++++++++--------- pager.c | 2 +- run-command.c | 2 +- setup.c | 6 +++--- 14 files changed, 30 insertions(+), 30 deletions(-)
builtin/clone.c +1 -1
@@ -566,7 +566,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)atexit (remove_junk);sigchain_push_common (remove_junk_on_signal); -setenv (CONFIG_ENVIRONMENT ,mkpath ("%s/config" , git_dir),1 ); +xsetenv (CONFIG_ENVIRONMENT ,mkpath ("%s/config" , git_dir),1 );if (safe_create_leading_directories_const (git_dir) <0 )die (_ ("could not create leading directories of '%s'" ), git_dir);
builtin/commit.c +3 -3
@@ -361,13 +361,13 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,die (_ ("unable to create temporary index" )); old_index_env =getenv (INDEX_ENVIRONMENT ); -setenv (INDEX_ENVIRONMENT , index_lock.filename,1 ); +xsetenv (INDEX_ENVIRONMENT , index_lock.filename,1 );if (interactive_add (argc, argv, prefix, patch_interactive) !=0 )die (_ ("interactive add failed" ));if (old_index_env && *old_index_env) -setenv (INDEX_ENVIRONMENT , old_index_env,1 ); +xsetenv (INDEX_ENVIRONMENT , old_index_env,1 );else unsetenv (INDEX_ENVIRONMENT );
@@ -1023,7 +1023,7 @@ static int parse_and_validate_options(int argc, const char *argv[],if (edit_flag) use_editor =1 ;if (!use_editor) -setenv ("GIT_EDITOR" ,":" ,1 ); +xsetenv ("GIT_EDITOR" ,":" ,1 );/* Sanity check options */ if (amend && !current_head)
builtin/help.c +2 -2
@@ -330,7 +330,7 @@ static void setup_man_path(void)if (old_path)strbuf_addstr (&new_path, old_path); -setenv ("MANPATH" , new_path.buf,1 ); +xsetenv ("MANPATH" , new_path.buf,1 );strbuf_release (&new_path); }
@@ -371,7 +371,7 @@ static void show_man_page(const char *git_cmd)static void show_info_page (const char *git_cmd) {const char *page =cmd_to_page (git_cmd); -setenv ("INFOPATH" ,system_path (GIT_INFO_PATH ),1 ); +xsetenv ("INFOPATH" ,system_path (GIT_INFO_PATH ),1 );execlp ("info" ,"info" ,"gitman" , page, (char *)NULL );die ("no info viewer handled the request" ); }
builtin/init-db.c +1 -1
@@ -537,7 +537,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)if (is_bare_repository_cfg ==1 ) {static char git_dir[PATH_MAX +1 ]; -setenv (GIT_DIR_ENVIRONMENT , +xsetenv (GIT_DIR_ENVIRONMENT ,getcwd (git_dir,sizeof (git_dir)), argc >0 ); }
builtin/merge.c +2 -2
@@ -1257,7 +1257,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)strbuf_addstr (&buf,"merge" );for (i =0 ; i < argc; i++)strbuf_addf (&buf," %s" , argv[i]); -setenv ("GIT_REFLOG_ACTION" , buf.buf,0 ); +xsetenv ("GIT_REFLOG_ACTION" , buf.buf,0 );strbuf_reset (&buf);for (i =0 ; i < argc; i++) {
@@ -1267,7 +1267,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) remotes = &commit_list_insert (commit, remotes)->next;strbuf_addf (&buf,"GITHEAD_%s" ,sha1_to_hex (commit->object.sha1)); -setenv (buf.buf, argv[i],1 ); +xsetenv (buf.buf, argv[i],1 );strbuf_reset (&buf); if (merge_remote_util(commit) && merge_remote_util(commit)->obj &&
builtin/notes.c +1 -1
@@ -1076,7 +1076,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)struct strbuf sb =STRBUF_INIT ;strbuf_addstr (&sb, override_notes_ref);expand_notes_ref (&sb); -setenv ("GIT_NOTES_REF" , sb.buf,1 ); +xsetenv ("GIT_NOTES_REF" , sb.buf,1 );strbuf_release (&sb); }
builtin/remote-ext.c +2 -2
@@ -38,8 +38,8 @@ static char *strip_escapes(const char *str, const char *service, psoff =4 ;/* Pass the service to command. */ -setenv ("GIT_EXT_SERVICE" , service,1 ); -setenv ("GIT_EXT_SERVICE_NOPREFIX" , service + psoff,1 ); +xsetenv ("GIT_EXT_SERVICE" , service,1 ); +xsetenv ("GIT_EXT_SERVICE_NOPREFIX" , service + psoff,1 );/* Scan the length of argument. */ while (str[rpos] && (escape || str[rpos] !=' ' )) {
builtin/revert.c +1 -1
@@ -1007,7 +1007,7 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)struct commit_list *cur;int res; -setenv (GIT_REFLOG_ACTION ,action_name (opts),0 ); +xsetenv (GIT_REFLOG_ACTION ,action_name (opts),0 );if (opts->allow_ff)assert (!(opts->signoff || opts->no_commit || opts->record_origin || opts->edit));
config.c +1 -1
@@ -43,7 +43,7 @@ void git_config_push_parameter(const char *text)strbuf_addch (&env,' ' ); }sq_quote_buf (&env, text); -setenv (CONFIG_DATA_ENVIRONMENT , env.buf,1 ); +xsetenv (CONFIG_DATA_ENVIRONMENT , env.buf,1 );strbuf_release (&env); }
exec_cmd.c +2 -2
@@ -63,7 +63,7 @@ void git_set_argv_exec_path(const char *exec_path)/* * Propagate this setting to external programs. */ -setenv (EXEC_PATH_ENVIRONMENT , exec_path,1 ); +xsetenv (EXEC_PATH_ENVIRONMENT , exec_path,1 ); }
@@ -108,7 +108,7 @@ void setup_path(void)else strbuf_addstr (&new_path, _PATH_DEFPATH); -setenv ("PATH" , new_path.buf,1 ); +xsetenv ("PATH" , new_path.buf,1 );strbuf_release (&new_path); }
git.c +9 -9
@@ -54,7 +54,7 @@ int check_pager_config(const char *cmd)static void commit_pager_choice (void ) {switch (use_pager) {case 0 : -setenv ("GIT_PAGER" ,"cat" ,1 ); +xsetenv ("GIT_PAGER" ,"cat" ,1 );break ;case 1 :setup_pager ();
@@ -109,7 +109,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--no-replace-objects" )) { read_replace_refs =0 ; -setenv (NO_REPLACE_OBJECTS_ENVIRONMENT ,"1" ,1 ); +xsetenv (NO_REPLACE_OBJECTS_ENVIRONMENT ,"1" ,1 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--git-dir" )) {
@@ -117,13 +117,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)fprintf (stderr,"No directory given for --git-dir.\n" );usage (git_usage_string); } -setenv (GIT_DIR_ENVIRONMENT , (*argv)[1 ],1 ); +xsetenv (GIT_DIR_ENVIRONMENT , (*argv)[1 ],1 );if (envchanged) *envchanged =1 ; (*argv)++; (*argc)--; }else if (!prefixcmp (cmd,"--git-dir=" )) { -setenv (GIT_DIR_ENVIRONMENT , cmd +10 ,1 ); +xsetenv (GIT_DIR_ENVIRONMENT , cmd +10 ,1 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--namespace" )) {
@@ -131,13 +131,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)fprintf (stderr,"No namespace given for --namespace.\n" );usage (git_usage_string); } -setenv (GIT_NAMESPACE_ENVIRONMENT , (*argv)[1 ],1 ); +xsetenv (GIT_NAMESPACE_ENVIRONMENT , (*argv)[1 ],1 );if (envchanged) *envchanged =1 ; (*argv)++; (*argc)--; }else if (!prefixcmp (cmd,"--namespace=" )) { -setenv (GIT_NAMESPACE_ENVIRONMENT , cmd +12 ,1 ); +xsetenv (GIT_NAMESPACE_ENVIRONMENT , cmd +12 ,1 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--work-tree" )) {
@@ -145,19 +145,19 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)fprintf (stderr,"No directory given for --work-tree.\n" );usage (git_usage_string); } -setenv (GIT_WORK_TREE_ENVIRONMENT , (*argv)[1 ],1 ); +xsetenv (GIT_WORK_TREE_ENVIRONMENT , (*argv)[1 ],1 );if (envchanged) *envchanged =1 ; (*argv)++; (*argc)--; }else if (!prefixcmp (cmd,"--work-tree=" )) { -setenv (GIT_WORK_TREE_ENVIRONMENT , cmd +12 ,1 ); +xsetenv (GIT_WORK_TREE_ENVIRONMENT , cmd +12 ,1 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"--bare" )) {static char git_dir[PATH_MAX +1 ]; is_bare_repository_cfg =1 ; -setenv (GIT_DIR_ENVIRONMENT ,getcwd (git_dir,sizeof (git_dir)),0 ); +xsetenv (GIT_DIR_ENVIRONMENT ,getcwd (git_dir,sizeof (git_dir)),0 );if (envchanged) *envchanged =1 ; }else if (!strcmp (cmd,"-c" )) {
pager.c +1 -1
@@ -76,7 +76,7 @@ void setup_pager(void)if (!pager)return ; -setenv ("GIT_PAGER_IN_USE" ,"true" ,1 ); +xsetenv ("GIT_PAGER_IN_USE" ,"true" ,1 );/* spawn the pager */ pager_argv[0 ] = pager;
run-command.c +1 -1
@@ -258,7 +258,7 @@ fail_pipe:if (cmd->env) {for (; *cmd->env; cmd->env++) {if (strchr (*cmd->env,'=' )) -putenv ((char *)*cmd->env); +xputenv ((char *)*cmd->env);else unsetenv (*cmd->env); }
setup.c +3 -3
@@ -309,7 +309,7 @@ void setup_work_tree(void)* if $GIT_WORK_TREE is set relative */ if (getenv (GIT_WORK_TREE_ENVIRONMENT )) -setenv (GIT_WORK_TREE_ENVIRONMENT ,"." ,1 ); +xsetenv (GIT_WORK_TREE_ENVIRONMENT ,"." ,1 );set_git_dir (relative_path (git_dir, work_tree)); initialized =1 ;
@@ -683,9 +683,9 @@ const char *setup_git_directory_gently(int *nongit_ok) prefix =setup_git_directory_gently_1 (nongit_ok);if (prefix) -setenv ("GIT_PREFIX" , prefix,1 ); +xsetenv ("GIT_PREFIX" , prefix,1 );else -setenv ("GIT_PREFIX" ,"" ,1 ); +xsetenv ("GIT_PREFIX" ,"" ,1 );if (startup_info) { startup_info->have_repository = !nongit_ok || !*nongit_ok;
Signature · 1 line
1.7.7.1.msysgit.0.272.g9e47e
Re: [PATCH v2 4/4] use wrapper for unchecked setenv/putenv calls
Quoted 2 lines · Erik Faye-Lund
This avoids us from accidentally dropping state, possibly leading to unexpected behaviour.
I do think this is fine in a "be extra cautious" kind of way.
Quoted 2 lines
This is especially important on Windows, where the maximum size of the environment is 32 kB.
But does your patch actually detect that? As Andreas pointed out, these limits don't typically come into play at setenv time. Instead, the environment is allocated on the heap, and then the result is passed to exec/spawn, which will fail.
So your patch is really detecting a failure to malloc, not an overflow of the environment size, and Windows is just as (un)likely to run out of heap as any other platform.
You can check how your platform behaves by applying this patch:
git.c +10 -0
@@ -223,6 +223,16 @@ static int handle_alias(int *argcp, const char ***argv) alias_argv[i] = (*argv)[i]; alias_argv[argc] =NULL ; +/* make gigantic environment */ + { +int len =256 *1024 ; +char *buf =xmalloc (len); +memset (buf,'z' , len); + buf[len-1 ] ='\0' ; +if (setenv ("FOO" , buf,1 )) +die ("setenv failed" ); + } + ret =run_command_v_opt (alias_argv,RUN_USING_SHELL );if (ret >=0 )/* normal exit */ exit (ret);
and then running:
git -c alias.foo='!echo ok' foo
which yields:
fatal: cannot exec 'echo ok': Argument list too long
on Linux.
-Peff
PS I tried to come up with an invocation of git that would demonstrate
this, but it turns out it's really hard. The contents of environment variables we set are either constants, come from the environment (so they can't be too big already!), or come from filesystem paths. So it's possible to overflow now, but you have to have a nearly-full environment in the first place, and then have a long path that tips it over the limit.
Re: [PATCH v2 4/4] use wrapper for unchecked setenv/putenv calls
Quoted 10 lines · Jeff King
Quoted 2 lines · Erik Faye-Lund
This avoids us from accidentally dropping state, possibly leading to unexpected behaviour.
I do think this is fine in a "be extra cautious" kind of way.
Quoted 2 lines
This is especially important on Windows, where the maximum size of the environment is 32 kB.
Quoted 9 lines
But does your patch actually detect that? As Andreas pointed out, these limits don't typically come into play at setenv time. Instead, the environment is allocated on the heap, and then the result is passed to exec/spawn, which will fail.
So your patch is really detecting a failure to malloc, not an overflow of the environment size, and Windows is just as (un)likely to run out of heap as any other platform.
You are right; I just assumed that our setenv was implemented on top of SetEnvironmentVariable, which does impose max-limits on setting (if I read the documentation correctly):
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686206(v=vs.85).aspx
But we don't. We have mingw_setenv, which simply modifies environ.
Quoted 26 lines
You can check how your platform behaves by applying this patch:
git.c +10
-0@@ -223,6 +223,16 @@ static int handle_alias(int *argcp, const char ***argv) alias_argv[i] = (*argv)[i]; alias_argv[argc] = NULL; + /* make gigantic environment */ + { + int len = 256 * 1024; + char *buf = xmalloc(len); + memset(buf, 'z', len); + buf[len-1] = '\0'; + if (setenv("FOO", buf, 1)) + die("setenv failed"); + } + ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);if (ret >= 0) /* normal exit */ exit(ret);
and then running:
Quoted 8 lines
git -c alias.foo='!echo ok' foo
which yields:
fatal: cannot exec 'echo ok': Argument list too long
on Linux.
Yeah, but I'm getting:
$ git -c alias.foo='!echo ok' foo ok
Which is strange to me.
But if I do:
$ git -c alias.foo='!echo $FOO' foo
it does echo a bunch of 'z's. And we get the expected amount:
$ git -c alias.foo='!echo $FOO' foo | wc -c 262144
This strikes me as very strange, because we end up calling the ANSI version of CreateProcess with an environment-block beyond 32767 chars, which MSDN says should fail:
"The ANSI version of this function, CreateProcessA fails if the total size of the environment block for the process exceeds 32,767 characters."
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
Oh well, this is strange behavior in a good way; I'm not going to complain :P