git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* a problem with git describe
@ 2022-04-23  9:12 Guy Maurel
  2022-04-23 11:27 ` Philip Oakley
  0 siblings, 1 reply; 23+ messages in thread
From: Guy Maurel @ 2022-04-23  9:12 UTC (permalink / raw)
  To: git

Hello!

I am using git version 2.25.1

6-8 weeks ago I don't get any problem.
Now I get :
guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
[sudo] password for guy:
fatal: unsafe repository ('/home/guy/Software/uncrustify' is owned by someone else)

What is to do?

Thanks for any help
guy
-- 
Guy Maurel
Sebastian-Fischer-Weg 13
89077 Ulm/ Germany

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-23  9:12 a problem with git describe Guy Maurel
@ 2022-04-23 11:27 ` Philip Oakley
  2022-04-23 16:09   ` Junio C Hamano
  0 siblings, 1 reply; 23+ messages in thread
From: Philip Oakley @ 2022-04-23 11:27 UTC (permalink / raw)
  To: Guy Maurel, git

Hi Guy,

On 23/04/2022 10:12, Guy Maurel wrote:
> Hello!
>
> I am using git version 2.25.1
>
> 6-8 weeks ago I don't get any problem.
> Now I get :
> guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
> [sudo] password for guy:
> fatal: unsafe repository ('/home/guy/Software/uncrustify' is owned by
> someone else)
>
> What is to do?
>
There was a security warning noted in recent maintenance releases, see
https://lore.kernel.org/git/20220414003839.1616296-2-gitster@pobox.com/
where users could be fooled to access unsafe repositories that they
didn't own.

You may have received a maintenance update that makes this check now the
default.

Could you check what `git version` reports? 

There has also been added an escape hatch of allowing "*" for the
permitted safe directories. but do check the updated manuals, and the
git mailing list archive (update the search in the above link).
--
Philip

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-23 11:27 ` Philip Oakley
@ 2022-04-23 16:09   ` Junio C Hamano
  2022-04-23 23:44     ` Junio C Hamano
                       ` (2 more replies)
  0 siblings, 3 replies; 23+ messages in thread
From: Junio C Hamano @ 2022-04-23 16:09 UTC (permalink / raw)
  To: Philip Oakley, Johannes Schindelin; +Cc: Guy Maurel, git

Philip Oakley <philipoakley@iee.email> writes:

>> guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
> ...
> There has also been added an escape hatch of allowing "*" for the
> permitted safe directories. but do check the updated manuals, and the
> git mailing list archive (update the search in the above link).

In this particular case, I do not think '*' is needed, but you need
to be careful here.  Whose configuration are you suggesting to add
such an entry?  Yourself?  ~root/.gitconfig?

I wonder if we should loosen "the same owner" check somewhat to
cover this situation better.  I expect people also run the
installation in repositories they own with "sudo make install",
and complaining "euid does not own that repository" when it is
merely because they are running as root (and their real identity
is still in ruid) feels a bit too strict to be useful.

Dscho, what do you think?


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-23 16:09   ` Junio C Hamano
@ 2022-04-23 23:44     ` Junio C Hamano
  2022-04-25  2:01       ` Carlo Marcelo Arenas Belón
  2022-04-26 15:45       ` Johannes Schindelin
  2022-04-26  1:52     ` Taylor Blau
  2022-04-26 15:30     ` Johannes Schindelin
  2 siblings, 2 replies; 23+ messages in thread
From: Junio C Hamano @ 2022-04-23 23:44 UTC (permalink / raw)
  To: Philip Oakley; +Cc: Johannes Schindelin, Guy Maurel, git

Junio C Hamano <gitster@pobox.com> writes:

> Philip Oakley <philipoakley@iee.email> writes:
>
>>> guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
>> ...
>> There has also been added an escape hatch of allowing "*" for the
>> permitted safe directories. but do check the updated manuals, and the
>> git mailing list archive (update the search in the above link).
>
> In this particular case, I do not think '*' is needed, but you need
> to be careful here.  Whose configuration are you suggesting to add
> such an entry?  Yourself?  ~root/.gitconfig?
>
> I wonder if we should loosen "the same owner" check somewhat to
> cover this situation better.  I expect people also run the
> installation in repositories they own with "sudo make install",
> and complaining "euid does not own that repository" when it is
> merely because they are running as root (and their real identity
> is still in ruid) feels a bit too strict to be useful.

Actually, not quite.  when "git" runs in "sudo git", the real
identity has long lost, so the below would not help.  Sigh.

 git-compat-util.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git i/git-compat-util.h w/git-compat-util.h
index 63ba89dd31..90dc1b17cd 100644
--- i/git-compat-util.h
+++ w/git-compat-util.h
@@ -398,7 +398,7 @@ static inline int is_path_owned_by_current_uid(const char *path)
 	struct stat st;
 	if (lstat(path, &st))
 		return 0;
-	return st.st_uid == geteuid();
+	return st.st_uid == geteuid() || st.st_uid == getuid();
 }
 
 #define is_path_owned_by_current_user is_path_owned_by_current_uid

^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-23 23:44     ` Junio C Hamano
@ 2022-04-25  2:01       ` Carlo Marcelo Arenas Belón
  2022-04-25  5:05         ` SZEDER Gábor
  2022-04-25  6:39         ` Junio C Hamano
  2022-04-26 15:45       ` Johannes Schindelin
  1 sibling, 2 replies; 23+ messages in thread
From: Carlo Marcelo Arenas Belón @ 2022-04-25  2:01 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Philip Oakley, Johannes Schindelin, Guy Maurel, git

On Sat, Apr 23, 2022 at 04:44:57PM -0700, Junio C Hamano wrote:
> 
> Actually, not quite.  when "git" runs in "sudo git", the real
> identity has long lost

Right, but in this specific case, the terminal is still a good indication
of who the user is, so the following would work.

diff --git a/git-compat-util.h b/git-compat-util.h
index 58fd813bd01..5d5d91688ee 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -442,6 +442,11 @@ static inline int is_path_owned_by_current_uid(const char *path)
 	struct stat st;
 	if (lstat(path, &st))
 		return 0;
+	if (isatty(1)) {
+		struct stat ttyst;
+		if (!stat(ttyname(1), &ttyst))
+			return st.st_uid == ttyst.st_uid;
+	}
 	return st.st_uid == geteuid();
 }
 
It obviously needs more polishing and portability work, though, and I don't
like that it makes the general case more complicated, so maybe would be better
to only do it running as root?

At that point, though you might as well excempt root from this check

Carlo

^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-25  2:01       ` Carlo Marcelo Arenas Belón
@ 2022-04-25  5:05         ` SZEDER Gábor
  2022-04-25  6:03           ` Carlo Marcelo Arenas Belón
  2022-04-25  6:39         ` Junio C Hamano
  1 sibling, 1 reply; 23+ messages in thread
From: SZEDER Gábor @ 2022-04-25  5:05 UTC (permalink / raw)
  To: Carlo Marcelo Arenas Belón
  Cc: Junio C Hamano, Philip Oakley, Johannes Schindelin, Guy Maurel, git

On Sun, Apr 24, 2022 at 07:01:08PM -0700, Carlo Marcelo Arenas Belón wrote:
> On Sat, Apr 23, 2022 at 04:44:57PM -0700, Junio C Hamano wrote:
> > 
> > Actually, not quite.  when "git" runs in "sudo git", the real
> > identity has long lost
> 
> Right, but in this specific case, the terminal is still a good indication
> of who the user is, so the following would work.
> 
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 58fd813bd01..5d5d91688ee 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -442,6 +442,11 @@ static inline int is_path_owned_by_current_uid(const char *path)
>  	struct stat st;
>  	if (lstat(path, &st))
>  		return 0;
> +	if (isatty(1)) {
> +		struct stat ttyst;
> +		if (!stat(ttyname(1), &ttyst))
> +			return st.st_uid == ttyst.st_uid;
> +	}
>  	return st.st_uid == geteuid();
>  }

Our 'GIT-VERSION-GEN' runs 'var=$(git describe ...)', so standard
output is not a terminal during 'sudo make install'

> It obviously needs more polishing and portability work, though, and I don't
> like that it makes the general case more complicated, so maybe would be better
> to only do it running as root?
> 
> At that point, though you might as well excempt root from this check
> 
> Carlo

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-25  5:05         ` SZEDER Gábor
@ 2022-04-25  6:03           ` Carlo Marcelo Arenas Belón
  0 siblings, 0 replies; 23+ messages in thread
From: Carlo Marcelo Arenas Belón @ 2022-04-25  6:03 UTC (permalink / raw)
  To: SZEDER Gábor
  Cc: Junio C Hamano, Philip Oakley, Johannes Schindelin, Guy Maurel, git

On Mon, Apr 25, 2022 at 07:05:49AM +0200, SZEDER Gábor wrote:
> On Sun, Apr 24, 2022 at 07:01:08PM -0700, Carlo Marcelo Arenas Belón wrote:
> > On Sat, Apr 23, 2022 at 04:44:57PM -0700, Junio C Hamano wrote:
> > > 
> > > Actually, not quite.  when "git" runs in "sudo git", the real
> > > identity has long lost
> > 
> > Right, but in this specific case, the terminal is still a good indication
> > of who the user is, so the following would work.
> > 
> > diff --git a/git-compat-util.h b/git-compat-util.h
> > index 58fd813bd01..5d5d91688ee 100644
> > --- a/git-compat-util.h
> > +++ b/git-compat-util.h
> > @@ -442,6 +442,11 @@ static inline int is_path_owned_by_current_uid(const char *path)
> >  	struct stat st;
> >  	if (lstat(path, &st))
> >  		return 0;
> > +	if (isatty(1)) {
> > +		struct stat ttyst;
> > +		if (!stat(ttyname(1), &ttyst))
> > +			return st.st_uid == ttyst.st_uid;
> > +	}
> >  	return st.st_uid == geteuid();
> >  }
> 
> Our 'GIT-VERSION-GEN' runs 'var=$(git describe ...)', so standard
> output is not a terminal during 'sudo make install'

didn't realize the orginal report was coming from `sudo make install`
in git's own repository, but even that worked for me.

when I meant the original case I went with an artificial test where
I run :

  $ mkdir -p r/t
  $ sudo chown root r
  $ cd r/t
  $ git init
  $ sudo git status

and I get no error, unlike the original version of the code.

of course, part of the "extra work needed" is to figure out if stdout
is the best place to look at, I chose it just because that is the
unoficial way to signal in our code that we are running interactively.

note that usually unless a pipe is involved or nohup is used, the tty
is still attached to your process.

Carlo

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-25  2:01       ` Carlo Marcelo Arenas Belón
  2022-04-25  5:05         ` SZEDER Gábor
@ 2022-04-25  6:39         ` Junio C Hamano
  2022-04-25  7:02           ` Carlo Marcelo Arenas Belón
  1 sibling, 1 reply; 23+ messages in thread
From: Junio C Hamano @ 2022-04-25  6:39 UTC (permalink / raw)
  To: Carlo Marcelo Arenas Belón
  Cc: Philip Oakley, Johannes Schindelin, Guy Maurel, git

Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:

> At that point, though you might as well excempt root from this check

But "root" or any higher-valued account is what needs this kind of
protection the most, no?  The protection is *not* about people
knowingly accessing their own repository via "root", but those who
are in a directory without being aware of its repository-ness.  You
prepare /var/tmp/.git/ and let others do things that they would do
in /var/tmp/ throw-away directories normally and get affected by
what you leave in /var/tmp/.git/config file.  If "root" is among
these "others", that sounds like you caught a bigger fish X-<.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-25  6:39         ` Junio C Hamano
@ 2022-04-25  7:02           ` Carlo Marcelo Arenas Belón
  2022-04-25  8:40             ` Carlo Marcelo Arenas Belón
  0 siblings, 1 reply; 23+ messages in thread
From: Carlo Marcelo Arenas Belón @ 2022-04-25  7:02 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Philip Oakley, Johannes Schindelin, Guy Maurel, git

On Sun, Apr 24, 2022 at 11:39:27PM -0700, Junio C Hamano wrote:
> Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:
> 
> > At that point, though you might as well excempt root from this check
> 
> But "root" or any higher-valued account is what needs this kind of
> protection the most, no?

correct, and I didn't meant to excempt root from the protection, but
from the check that requires that the config file ownership matches.

if the config file is owned by root, we already lost, regardless of what
uid git is running as.

FWIW the proposed change doesn't weaken the current protection, it just
allows a git process that is running as root (through sudo) to figure
out what real user was the terminal session running as, so it wouldn't
incorrectly triggeer an error.

> The protection is *not* about people
> knowingly accessing their own repository via "root",

nope, but the regression that was described by the original post is

Carlo

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-25  7:02           ` Carlo Marcelo Arenas Belón
@ 2022-04-25  8:40             ` Carlo Marcelo Arenas Belón
  2022-04-25 15:11               ` Guy Maurel
  2022-04-26 15:43               ` Johannes Schindelin
  0 siblings, 2 replies; 23+ messages in thread
From: Carlo Marcelo Arenas Belón @ 2022-04-25  8:40 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Philip Oakley, Johannes Schindelin, Guy Maurel, git

On Mon, Apr 25, 2022 at 12:02:45AM -0700, Carlo Marcelo Arenas Belón wrote:
> On Sun, Apr 24, 2022 at 11:39:27PM -0700, Junio C Hamano wrote:
> > Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:
> > 
> > > At that point, though you might as well excempt root from this check
> > 
> > But "root" or any higher-valued account is what needs this kind of
> > protection the most, no?
> 
> correct, and I didn't meant to excempt root from the protection, but
> from the check that requires that the config file ownership matches.
> 
> if the config file is owned by root, we already lost, regardless of what
> uid git is running as.

apologies for my confusing english, hopefully this C is clearer

diff --git a/git-compat-util.h b/git-compat-util.h
index 58fd813bd01..6a385be7d1d 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -440,9 +440,19 @@ static inline int git_offset_1st_component(const char *path)
 static inline int is_path_owned_by_current_uid(const char *path)
 {
 	struct stat st;
+	uid_t euid;
+
 	if (lstat(path, &st))
 		return 0;
-	return st.st_uid == geteuid();
+
+	euid = geteuid();
+	if (!euid && st.st_uid && isatty(0)) {
+		struct stat ttyst;
+		if (!stat(ttyname(0), &ttyst))
+			euid = ttyst.st_uid;
+	}
+
+	return st.st_uid == euid;
 }
 
 #define is_path_owned_by_current_user is_path_owned_by_current_uid

it uses stdin instead not to fall in the issue that was raised by
Gábor, but I am affraid that it might need to check all stdnandles for
a valid tty to be safe, and it looking even more complex.

Carlo

^ permalink raw reply related	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-25  8:40             ` Carlo Marcelo Arenas Belón
@ 2022-04-25 15:11               ` Guy Maurel
  2022-04-26 15:43               ` Johannes Schindelin
  1 sibling, 0 replies; 23+ messages in thread
From: Guy Maurel @ 2022-04-25 15:11 UTC (permalink / raw)
  To: Carlo Marcelo Arenas Belón, Junio C Hamano
  Cc: Philip Oakley, Johannes Schindelin, git

Hello!

It might be important for your debugging work.
I introduce a new file /root/.gitconfig
-rw-r--r--  1 root root   57 Apr 25 17:03 .gitconfig
with
[safe]
         directory = /home/guy/Software/uncrustify

And the call works pretty well:
uncrustify/build $ sudo ~/Software/Git/2.36.0/tar-gz/git-2.36.0/git-describe
uncrustify-0.74.0-315-ga3466c92

Thanks for helping!
guy
Am 25.04.22 um 10:40 schrieb Carlo Marcelo Arenas Belón:
> On Mon, Apr 25, 2022 at 12:02:45AM -0700, Carlo Marcelo Arenas Belón wrote:
>> On Sun, Apr 24, 2022 at 11:39:27PM -0700, Junio C Hamano wrote:
>>> Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:
>>>
>>>> At that point, though you might as well excempt root from this check
>>>
>>> But "root" or any higher-valued account is what needs this kind of
>>> protection the most, no?
>>
>> correct, and I didn't meant to excempt root from the protection, but
>> from the check that requires that the config file ownership matches.
>>
>> if the config file is owned by root, we already lost, regardless of what
>> uid git is running as.
> 
> apologies for my confusing english, hopefully this C is clearer
> 
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 58fd813bd01..6a385be7d1d 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -440,9 +440,19 @@ static inline int git_offset_1st_component(const char *path)
>   static inline int is_path_owned_by_current_uid(const char *path)
>   {
>   	struct stat st;
> +	uid_t euid;
> +
>   	if (lstat(path, &st))
>   		return 0;
> -	return st.st_uid == geteuid();
> +
> +	euid = geteuid();
> +	if (!euid && st.st_uid && isatty(0)) {
> +		struct stat ttyst;
> +		if (!stat(ttyname(0), &ttyst))
> +			euid = ttyst.st_uid;
> +	}
> +
> +	return st.st_uid == euid;
>   }
>   
>   #define is_path_owned_by_current_user is_path_owned_by_current_uid
> 
> it uses stdin instead not to fall in the issue that was raised by
> Gábor, but I am affraid that it might need to check all stdnandles for
> a valid tty to be safe, and it looking even more complex.
> 
> Carlo

-- 
Guy Maurel
Sebastian-Fischer-Weg 13
89077 Ulm

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-23 16:09   ` Junio C Hamano
  2022-04-23 23:44     ` Junio C Hamano
@ 2022-04-26  1:52     ` Taylor Blau
  2022-04-26 15:41       ` Johannes Schindelin
  2022-04-26 15:30     ` Johannes Schindelin
  2 siblings, 1 reply; 23+ messages in thread
From: Taylor Blau @ 2022-04-26  1:52 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Philip Oakley, Johannes Schindelin, Guy Maurel, git

On Sat, Apr 23, 2022 at 09:09:59AM -0700, Junio C Hamano wrote:
> Philip Oakley <philipoakley@iee.email> writes:
>
> >> guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
> > ...
> > There has also been added an escape hatch of allowing "*" for the
> > permitted safe directories. but do check the updated manuals, and the
> > git mailing list archive (update the search in the above link).
>
> In this particular case, I do not think '*' is needed, but you need
> to be careful here.  Whose configuration are you suggesting to add
> such an entry?  Yourself?  ~root/.gitconfig?
>
> I wonder if we should loosen "the same owner" check somewhat to
> cover this situation better.  I expect people also run the
> installation in repositories they own with "sudo make install",
> and complaining "euid does not own that repository" when it is
> merely because they are running as root (and their real identity
> is still in ruid) feels a bit too strict to be useful.

I was thinking about this today and realized that my original train of
thought along the lines of "ignore the new safety check when the current
user has higher permissions than the user who owns the repository we're
working in" was misguided.

What about loosening the check in a different way? Instead of causing
Git to abort early, what if we:

  - skipped reading the repository's configuration and hooks (unless
    safe.directory includes $CWD)
  - emit a warning (which goes away when safe.directory contains $CWD)
  - otherwise continue executing as normal

That would unbreak the case of $(git describe ...) in our Makefile in
this instance, without opening ourselves up to execution-via-config.

Though I think we'd have to be slightly more careful than that, since
we definitely _do_ want to read repository format extensions.

I'm hesitant to recommend reading some parts of the configuration
without others, though this is slightly different than that. Instead of
saying "read every entry of config except core.editor, core.pager,
core.alternateRefsCommand, core.fsmonitor" and so on, this says "when
operating in a repository not owned by the current user (or the
repository is in our global safe.directory list) only read repository
format extensions, but ignore everything else in config and hooks".

> Dscho, what do you think?

I'd be curious to hear what Johannes thinks of your original mail as
well as my own.

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-23 16:09   ` Junio C Hamano
  2022-04-23 23:44     ` Junio C Hamano
  2022-04-26  1:52     ` Taylor Blau
@ 2022-04-26 15:30     ` Johannes Schindelin
  2022-04-26 15:36       ` Junio C Hamano
  2 siblings, 1 reply; 23+ messages in thread
From: Johannes Schindelin @ 2022-04-26 15:30 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Philip Oakley, Guy Maurel, git

Hi Junio,

On Sat, 23 Apr 2022, Junio C Hamano wrote:

> Philip Oakley <philipoakley@iee.email> writes:
>
> >> guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
> > ...
> > There has also been added an escape hatch of allowing "*" for the
> > permitted safe directories. but do check the updated manuals, and the
> > git mailing list archive (update the search in the above link).
>
> In this particular case, I do not think '*' is needed, but you need
> to be careful here.  Whose configuration are you suggesting to add
> such an entry?  Yourself?  ~root/.gitconfig?

One relatively simple work-around might be to call `sudo chown root .`
before running `sudo make install`, but of course this would require the
`rm -rf` to be run via `sudo`, too.

> I wonder if we should loosen "the same owner" check somewhat to
> cover this situation better.  I expect people also run the
> installation in repositories they own with "sudo make install",
> and complaining "euid does not own that repository" when it is
> merely because they are running as root (and their real identity
> is still in ruid) feels a bit too strict to be useful.

It's too bad that your fix to include euid seems not to work. That would
have been my preference.

Do we want to make use of the environment variable `SUDO_UID` that is set
by `sudo`?

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-26 15:30     ` Johannes Schindelin
@ 2022-04-26 15:36       ` Junio C Hamano
  0 siblings, 0 replies; 23+ messages in thread
From: Junio C Hamano @ 2022-04-26 15:36 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Philip Oakley, Guy Maurel, git

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> On Sat, 23 Apr 2022, Junio C Hamano wrote:
>
>> Philip Oakley <philipoakley@iee.email> writes:
>>
>> >> guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
>> > ...
>> > There has also been added an escape hatch of allowing "*" for the
>> > permitted safe directories. but do check the updated manuals, and the
>> > git mailing list archive (update the search in the above link).
>>
>> In this particular case, I do not think '*' is needed, but you need
>> to be careful here.  Whose configuration are you suggesting to add
>> such an entry?  Yourself?  ~root/.gitconfig?
>
> One relatively simple work-around might be to call `sudo chown root .`
> before running `sudo make install`, but of course this would require the
> `rm -rf` to be run via `sudo`, too.

chown root may make it owned by nobody4, though ;-)

> Do we want to make use of the environment variable `SUDO_UID` that is set
> by `sudo`?

"run this command under 'sudo'" would be a social engineering attack
we do not want to defend against, so I am OK with that, but then
allowing

    sudo "GIT_SAFE_DIRECTORIES=. make install"

does not look too bad, either.




^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-26  1:52     ` Taylor Blau
@ 2022-04-26 15:41       ` Johannes Schindelin
  2022-04-26 15:50         ` Taylor Blau
  0 siblings, 1 reply; 23+ messages in thread
From: Johannes Schindelin @ 2022-04-26 15:41 UTC (permalink / raw)
  To: Taylor Blau; +Cc: Junio C Hamano, Philip Oakley, Guy Maurel, git

Hi Taylor,

On Mon, 25 Apr 2022, Taylor Blau wrote:

> On Sat, Apr 23, 2022 at 09:09:59AM -0700, Junio C Hamano wrote:
> > Philip Oakley <philipoakley@iee.email> writes:
> >
> > >> guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
> > > ...
> > > There has also been added an escape hatch of allowing "*" for the
> > > permitted safe directories. but do check the updated manuals, and the
> > > git mailing list archive (update the search in the above link).
> >
> > In this particular case, I do not think '*' is needed, but you need
> > to be careful here.  Whose configuration are you suggesting to add
> > such an entry?  Yourself?  ~root/.gitconfig?
> >
> > I wonder if we should loosen "the same owner" check somewhat to
> > cover this situation better.  I expect people also run the
> > installation in repositories they own with "sudo make install",
> > and complaining "euid does not own that repository" when it is
> > merely because they are running as root (and their real identity
> > is still in ruid) feels a bit too strict to be useful.
>
> I was thinking about this today and realized that my original train of
> thought along the lines of "ignore the new safety check when the current
> user has higher permissions than the user who owns the repository we're
> working in" was misguided.
>
> What about loosening the check in a different way? Instead of causing
> Git to abort early, what if we:
>
>   - skipped reading the repository's configuration and hooks (unless
>     safe.directory includes $CWD)
>   - emit a warning (which goes away when safe.directory contains $CWD)
>   - otherwise continue executing as normal
>
> That would unbreak the case of $(git describe ...) in our Makefile in
> this instance, without opening ourselves up to execution-via-config.
>
> Though I think we'd have to be slightly more careful than that, since
> we definitely _do_ want to read repository format extensions.
>
> I'm hesitant to recommend reading some parts of the configuration
> without others, though this is slightly different than that. Instead of
> saying "read every entry of config except core.editor, core.pager,
> core.alternateRefsCommand, core.fsmonitor" and so on, this says "when
> operating in a repository not owned by the current user (or the
> repository is in our global safe.directory list) only read repository
> format extensions, but ignore everything else in config and hooks".

This idea to disable "just the unsafe parts" has come up before, and I do
not really like it. It would change Git's behavior in inconsistent,
hard-to-explain ways. For example, we would have to disable clean/smudge
filters, which would then break, say, Git LFS.

Sure, for something like `core.fsmonitor` which is a pure performance
knob, where Git works correctly whether you heed that setting or not (just
with different performance characteristics), it might be fine. But for
things like the clean/smudge filters, it changes behavior. And the worst
part? If we introduce something like this logic ("if the worktree is owned
by somebody else, just ignore the parts of the config that refer to
user-defined programs to be executed"), we give wrong results _without
having a way to tell the user that we do that_. Unless you want to see a
warning "you may see incorrect results" every time you run `sudo git
status`?

Don't get me wrong, we _will_ want to have a serious discussion about the
config and how we let features creep in that can be exploited if an
adversary gains write access to one of the various places from where Git
reads config, and how we can mitigate that. IIRC we used to treat the
config as something safe "because it does not execute code", this came up
e.g. when we introduced `.gitmodules`.

I just don't think that we can use "is this thing owned by us?" as a knob
that asks Git to ignore all config settings referring to paths of
executables it is supposed to run at one stage or another.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-25  8:40             ` Carlo Marcelo Arenas Belón
  2022-04-25 15:11               ` Guy Maurel
@ 2022-04-26 15:43               ` Johannes Schindelin
  2022-04-26 15:56                 ` rsbecker
  2022-04-26 16:35                 ` Carlo Arenas
  1 sibling, 2 replies; 23+ messages in thread
From: Johannes Schindelin @ 2022-04-26 15:43 UTC (permalink / raw)
  To: Carlo Marcelo Arenas Belón
  Cc: Junio C Hamano, Philip Oakley, Guy Maurel, git

[-- Attachment #1: Type: text/plain, Size: 1928 bytes --]

Hi Carlo,

On Mon, 25 Apr 2022, Carlo Marcelo Arenas Belón wrote:

> On Mon, Apr 25, 2022 at 12:02:45AM -0700, Carlo Marcelo Arenas Belón wrote:
> > On Sun, Apr 24, 2022 at 11:39:27PM -0700, Junio C Hamano wrote:
> > > Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:
> > >
> > > > At that point, though you might as well excempt root from this check
> > >
> > > But "root" or any higher-valued account is what needs this kind of
> > > protection the most, no?
> >
> > correct, and I didn't meant to excempt root from the protection, but
> > from the check that requires that the config file ownership matches.
> >
> > if the config file is owned by root, we already lost, regardless of what
> > uid git is running as.
>
> apologies for my confusing english, hopefully this C is clearer
>
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 58fd813bd01..6a385be7d1d 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -440,9 +440,19 @@ static inline int git_offset_1st_component(const char *path)
>  static inline int is_path_owned_by_current_uid(const char *path)
>  {
>  	struct stat st;
> +	uid_t euid;
> +
>  	if (lstat(path, &st))
>  		return 0;
> -	return st.st_uid == geteuid();
> +
> +	euid = geteuid();
> +	if (!euid && st.st_uid && isatty(0)) {
> +		struct stat ttyst;
> +		if (!stat(ttyname(0), &ttyst))
> +			euid = ttyst.st_uid;
> +	}
> +
> +	return st.st_uid == euid;
>  }
>
>  #define is_path_owned_by_current_user is_path_owned_by_current_uid
>
> it uses stdin instead not to fall in the issue that was raised by
> Gábor, but I am affraid that it might need to check all stdnandles for
> a valid tty to be safe, and it looking even more complex.

Maybe a better idea for the `sudo` scenario would be to make use of
`SUDO_UID` (assuming that no adversary can gain control over the user's
environment variables)?

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-23 23:44     ` Junio C Hamano
  2022-04-25  2:01       ` Carlo Marcelo Arenas Belón
@ 2022-04-26 15:45       ` Johannes Schindelin
  2022-04-26 16:25         ` Junio C Hamano
  1 sibling, 1 reply; 23+ messages in thread
From: Johannes Schindelin @ 2022-04-26 15:45 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Philip Oakley, Guy Maurel, git

Hi Junio,

On Sat, 23 Apr 2022, Junio C Hamano wrote:

> Junio C Hamano <gitster@pobox.com> writes:
>
> > Philip Oakley <philipoakley@iee.email> writes:
> >
> >>> guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
> >> ...
> >> There has also been added an escape hatch of allowing "*" for the
> >> permitted safe directories. but do check the updated manuals, and the
> >> git mailing list archive (update the search in the above link).
> >
> > In this particular case, I do not think '*' is needed, but you need
> > to be careful here.  Whose configuration are you suggesting to add
> > such an entry?  Yourself?  ~root/.gitconfig?
> >
> > I wonder if we should loosen "the same owner" check somewhat to
> > cover this situation better.  I expect people also run the
> > installation in repositories they own with "sudo make install",
> > and complaining "euid does not own that repository" when it is
> > merely because they are running as root (and their real identity
> > is still in ruid) feels a bit too strict to be useful.
>
> Actually, not quite.  when "git" runs in "sudo git", the real
> identity has long lost, so the below would not help.  Sigh.

Could you help me understand what is going on exactly? How/when is `git`
running `sudo git`? I thought the problem was that `sudo make install`
transitively runs `git describe` with euid 0, but `getuid()` should still
return the non-admin user's ID, no?

Thanks,
Dscho

>
>  git-compat-util.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git i/git-compat-util.h w/git-compat-util.h
> index 63ba89dd31..90dc1b17cd 100644
> --- i/git-compat-util.h
> +++ w/git-compat-util.h
> @@ -398,7 +398,7 @@ static inline int is_path_owned_by_current_uid(const char *path)
>  	struct stat st;
>  	if (lstat(path, &st))
>  		return 0;
> -	return st.st_uid == geteuid();
> +	return st.st_uid == geteuid() || st.st_uid == getuid();
>  }
>
>  #define is_path_owned_by_current_user is_path_owned_by_current_uid
>

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-26 15:41       ` Johannes Schindelin
@ 2022-04-26 15:50         ` Taylor Blau
  0 siblings, 0 replies; 23+ messages in thread
From: Taylor Blau @ 2022-04-26 15:50 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Junio C Hamano, Philip Oakley, Guy Maurel, git

On Tue, Apr 26, 2022 at 05:41:36PM +0200, Johannes Schindelin wrote:
> This idea to disable "just the unsafe parts" has come up before, and I do
> not really like it. It would change Git's behavior in inconsistent,
> hard-to-explain ways. For example, we would have to disable clean/smudge
> filters, which would then break, say, Git LFS.

I share your sentiment on the "don't read the unsafe parts" bit.

I think replacing our behavior from 2.35.2 from "bail on every execution
of Git in an unowned directory not listed in safe.directory" to "emit a
warning and don't read config/hooks in an unowned [...]" is a little
friendlier in the sense that it would avoid breaking too many workflows
without opening us up to the attack described and mitigate by that
release.

A warning is definitely appropriate to tell users when we are and aren't
reading config. I strongly dislike "read some parts of config and not
others" though, since it seems (a) even harder to explain and
inconsistent than this, and (b) extremely error prone and fragile in the
sense that we are unlikely to completely enumerate every possible config
-> execution path in git-config(1).

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 23+ messages in thread

* RE: a problem with git describe
  2022-04-26 15:43               ` Johannes Schindelin
@ 2022-04-26 15:56                 ` rsbecker
  2022-04-26 16:35                 ` Carlo Arenas
  1 sibling, 0 replies; 23+ messages in thread
From: rsbecker @ 2022-04-26 15:56 UTC (permalink / raw)
  To: 'Johannes Schindelin', 'Carlo Marcelo Arenas Belón'
  Cc: 'Junio C Hamano', 'Philip Oakley',
	'Guy Maurel',
	git

On April 26, 2022 11:44 AM, Johannes Schindelin wrote:
>On Mon, 25 Apr 2022, Carlo Marcelo Arenas Belón wrote:
>
>> On Mon, Apr 25, 2022 at 12:02:45AM -0700, Carlo Marcelo Arenas Belón wrote:
>> > On Sun, Apr 24, 2022 at 11:39:27PM -0700, Junio C Hamano wrote:
>> > > Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:
>> > >
>> > > > At that point, though you might as well excempt root from this
>> > > > check
>> > >
>> > > But "root" or any higher-valued account is what needs this kind of
>> > > protection the most, no?
>> >
>> > correct, and I didn't meant to excempt root from the protection, but
>> > from the check that requires that the config file ownership matches.
>> >
>> > if the config file is owned by root, we already lost, regardless of
>> > what uid git is running as.
>>
>> apologies for my confusing english, hopefully this C is clearer
>>
>> diff --git a/git-compat-util.h b/git-compat-util.h index
>> 58fd813bd01..6a385be7d1d 100644
>> --- a/git-compat-util.h
>> +++ b/git-compat-util.h
>> @@ -440,9 +440,19 @@ static inline int git_offset_1st_component(const
>> char *path)  static inline int is_path_owned_by_current_uid(const char
>> *path)  {
>>  	struct stat st;
>> +	uid_t euid;
>> +
>>  	if (lstat(path, &st))
>>  		return 0;
>> -	return st.st_uid == geteuid();
>> +
>> +	euid = geteuid();
>> +	if (!euid && st.st_uid && isatty(0)) {
>> +		struct stat ttyst;
>> +		if (!stat(ttyname(0), &ttyst))
>> +			euid = ttyst.st_uid;
>> +	}
>> +
>> +	return st.st_uid == euid;
>>  }
>>
>>  #define is_path_owned_by_current_user is_path_owned_by_current_uid
>>
>> it uses stdin instead not to fall in the issue that was raised by
>> Gábor, but I am affraid that it might need to check all stdnandles for
>> a valid tty to be safe, and it looking even more complex.
>
>Maybe a better idea for the `sudo` scenario would be to make use of `SUDO_UID`
>(assuming that no adversary can gain control over the user's environment
>variables)?

Please do not assume that the user id 0 represents root. It does not on all systems. This change will cause breakages. We would need a specific comparison in compat.c with platform-specific values.
For example:

#if defined __TANDEM
#define ROOT_UID 65535
#define ROOT_GID 255
#endif

--Randall


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-26 15:45       ` Johannes Schindelin
@ 2022-04-26 16:25         ` Junio C Hamano
  0 siblings, 0 replies; 23+ messages in thread
From: Junio C Hamano @ 2022-04-26 16:25 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Philip Oakley, Guy Maurel, git

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

>> Actually, not quite.  when "git" runs in "sudo git", the real
>> identity has long lost, so the below would not help.  Sigh.
>
> Could you help me understand what is going on exactly? How/when is `git`
> running `sudo git`? I thought the problem was that `sudo make install`
> transitively runs `git describe` with euid 0, but `getuid()` should still
> return the non-admin user's ID, no?

The first "git" is literal English meaning, refers to a person ;-)

Anybody building and installing git would do

	$ sudo make
	$ sudo make install

and no, sudo does not leave a clue in getuid() from whom it came from.


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-26 15:43               ` Johannes Schindelin
  2022-04-26 15:56                 ` rsbecker
@ 2022-04-26 16:35                 ` Carlo Arenas
  2022-04-26 16:46                   ` Junio C Hamano
  1 sibling, 1 reply; 23+ messages in thread
From: Carlo Arenas @ 2022-04-26 16:35 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Junio C Hamano, Philip Oakley, Guy Maurel, git

On Tue, Apr 26, 2022 at 8:43 AM Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
>
> Maybe a better idea for the `sudo` scenario would be to make use of
> `SUDO_UID` (assuming that no adversary can gain control over the user's
> environment variables)?

I like this idea, and will prepare a formal patch soon also including DOAS_UID
for anyone that might have used that instead of sudo to elevate their
privileges.

Still think that (since we are already touching this) removing the
restriction to
root owned directories might make sense though, ex the following (unrealistic
example) would work:

  $ mkdir -p r/t
  $ sudo chown root r
  $ cd r && sudo git init
  $ cd t && git status

IMHO any directories above that are owned by root and might have a git
configuration must be safe, and therefore shouldn't need to be
explicitly exempted.

I also think that Taylor's suggestion to ignore and warn instead of
abort might be
a better solution to the "configuration file shouldn't be trusted"
issue which is at
the root of this regression, and obviously doing that would make this
change less
critical (users will just get a warning instead of being prevented to
do what worked
for them before and is a safe use case)

Carlo

PS. yes hardcoding uid 0 as root would be avoided, not sure where yet though

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-26 16:35                 ` Carlo Arenas
@ 2022-04-26 16:46                   ` Junio C Hamano
  2022-04-26 17:15                     ` Carlo Arenas
  0 siblings, 1 reply; 23+ messages in thread
From: Junio C Hamano @ 2022-04-26 16:46 UTC (permalink / raw)
  To: Carlo Arenas; +Cc: Johannes Schindelin, Philip Oakley, Guy Maurel, git

Carlo Arenas <carenas@gmail.com> writes:

> Still think that (since we are already touching this) removing the
> restriction to
> root owned directories might make sense though, ex the following (unrealistic
> example) would work:

I think it is essential to protect unsuspecting "root" user from
wandering into an unfamiliar directory that happens to be a trap,
i.e. I may do something like this as an admin:

    $ sudo sh
    # cd / && find usr bin ... >/var/tmp/mylist.txt
    # cd /var/tmp

with the expectation that I'd then do some text processing on the
mylist.txt file, and going there first would allow me to refer to
the files more easily, instead of having to say:

    # sed -e '... processing ...' </var/tmp/mylist.txt >/var/tmp/out.txt

Alas, you as an attacker have done

    $ cd /var/tmp
    $ git init
    $ edit .git/config

to wait for me.  /var/tmp would be owned by 'root' but allows
anybody to write to it, only forbidding people from removing other
people's stuff.



>   $ mkdir -p r/t
>   $ sudo chown root r
>   $ cd r && sudo git init
>   $ cd t && git status
>
> IMHO any directories above that are owned by root and might have a git
> configuration must be safe, and therefore shouldn't need to be
> explicitly exempted.
>
> I also think that Taylor's suggestion to ignore and warn instead of
> abort might be
> a better solution to the "configuration file shouldn't be trusted"
> issue which is at
> the root of this regression, and obviously doing that would make this
> change less
> critical (users will just get a warning instead of being prevented to
> do what worked
> for them before and is a safe use case)
>
> Carlo
>
> PS. yes hardcoding uid 0 as root would be avoided, not sure where yet though

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: a problem with git describe
  2022-04-26 16:46                   ` Junio C Hamano
@ 2022-04-26 17:15                     ` Carlo Arenas
  0 siblings, 0 replies; 23+ messages in thread
From: Carlo Arenas @ 2022-04-26 17:15 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Johannes Schindelin, Philip Oakley, Guy Maurel, git

On Tue, Apr 26, 2022 at 9:46 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> Carlo Arenas <carenas@gmail.com> writes:
>
> > Still think that (since we are already touching this) removing the
> > restriction to
> > root owned directories might make sense though, ex the following (unrealistic
> > example) would work:
>
> I think it is essential to protect unsuspecting "root" user from
> wandering into an unfamiliar directory that happens to be a trap,
> i.e. I may do something like this as an admin:
>
>     $ sudo sh
>     # cd / && find usr bin ... >/var/tmp/mylist.txt
>     # cd /var/tmp
>
> with the expectation that I'd then do some text processing on the
> mylist.txt file, and going there first would allow me to refer to
> the files more easily, instead of having to say:
>
>     # sed -e '... processing ...' </var/tmp/mylist.txt >/var/tmp/out.txt
>
> Alas, you as an attacker have done

Which is a career that has ended prematurely, it seems.

>     $ cd /var/tmp
>     $ git init
>     $ edit .git/config
>
> to wait for me.  /var/tmp would be owned by 'root' but allows
> anybody to write to it, only forbidding people from removing other
> people's stuff.

Since we are doing stat in that directory anyway, we should be able
to notice the sticky bit and not fall from that trap, but I get why
"exempting root" would be a bad idea.

Carlo

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2022-04-26 17:17 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-23  9:12 a problem with git describe Guy Maurel
2022-04-23 11:27 ` Philip Oakley
2022-04-23 16:09   ` Junio C Hamano
2022-04-23 23:44     ` Junio C Hamano
2022-04-25  2:01       ` Carlo Marcelo Arenas Belón
2022-04-25  5:05         ` SZEDER Gábor
2022-04-25  6:03           ` Carlo Marcelo Arenas Belón
2022-04-25  6:39         ` Junio C Hamano
2022-04-25  7:02           ` Carlo Marcelo Arenas Belón
2022-04-25  8:40             ` Carlo Marcelo Arenas Belón
2022-04-25 15:11               ` Guy Maurel
2022-04-26 15:43               ` Johannes Schindelin
2022-04-26 15:56                 ` rsbecker
2022-04-26 16:35                 ` Carlo Arenas
2022-04-26 16:46                   ` Junio C Hamano
2022-04-26 17:15                     ` Carlo Arenas
2022-04-26 15:45       ` Johannes Schindelin
2022-04-26 16:25         ` Junio C Hamano
2022-04-26  1:52     ` Taylor Blau
2022-04-26 15:41       ` Johannes Schindelin
2022-04-26 15:50         ` Taylor Blau
2022-04-26 15:30     ` Johannes Schindelin
2022-04-26 15:36       ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).