Re: [PATCH v2 1/3] git-compat-util: avoid failing dir ownership checks if running privileged

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Carlo

On 28/04/2022 11:58, Carlo Marcelo Arenas Belón wrote:
bdc77d1d685 (Add a function to determine whether a path is owned by the
current user, 2022-03-02) checks for the effective uid of the running
process using geteuid() but didn't account for cases where that user was
root (because git was invoked through sudo or a compatible tool) and the
original uid that repository trusted for its config was no longer known,
therefore failing the following otherwise safe call:

   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)

Attempt to detect those cases by using the environment variables that
those tools create to keep track of the original user id, and do the
ownership check using that instead.

This assumes the environment the user is running with after going
privileged can't be tampered with, and also does the check only for
root to keep the most common case less complicated, but as a side effect
will miss cases where sudo (or an equivalent) was used to change to
another unprivileged user or where the equivalent tool used to raise
privileges didn't track the original id in a sudo compatible way.

Reported-by: Guy Maurel <guy.j@xxxxxxxxx>
Helped-by: SZEDER Gábor <szeder.dev@xxxxxxxxx>
Helped-by: Randall Becker <rsbecker@xxxxxxxxxxxxx>
Helped-by: Phillip Wood <phillip.wood123@xxxxxxxxx>
Suggested-by: Johannes Schindelin <Johannes.Schindelin@xxxxxx>
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@xxxxxxxxx>
Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx>
---
  git-compat-util.h | 40 +++++++++++++++++++++++++++++++++++++++-
  1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/git-compat-util.h b/git-compat-util.h
index 63ba89dd31d..dfdd3e4f81a 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -393,12 +393,50 @@ static inline int git_offset_1st_component(const char *path)
  #endif
#ifndef is_path_owned_by_current_user
+
+#ifdef __TANDEM
+#define ROOT_UID 65535
+#else
+#define ROOT_UID 0
+#endif
+
+/*
+ * this helper function overrides a ROOT_UID with the one provided by
+ * an environment variable, do not use unless the original user is
+ * root
+ */
+static inline void extract_id_from_env(const char *env, uid_t *id)
+{
+	const char *real_uid = getenv(env);
+
+	/* discard any empty values */
+	if (real_uid && *real_uid) {
+		char *endptr;
+		unsigned long env_id;
+		int saved_errno = errno;

I still don't understand why you're worried about preserving errno - am I missing something? It's not wrong to save it but I'm not sure why we need to. is_path_owned_by_current_uid() does not save it around the stat call so why do we need to do this here?

+
+		errno = 0;
+		env_id = strtoul(real_uid, &endptr, 10);
+		if (!errno && !*endptr && env_id <= (uid_t)-1)

Thinking out loud:
"!errno && !*endptr" ensures we have read a valid integer from SUDO_UID. If uid_t is unsigned then "env_id <= (uid_t)-1" ensures the value we read fits into a uid_t. If uid_t is signed then this test is always true and we could truncate the value we read. However if we don't trust SUDO_UID then we shouldn't be doing any of this so we are trusting that the truncation never happens which means we probably don't need this test in the first place.

Best Wishes

Phillip

+			*id = env_id;
+
+		errno = saved_errno;
+	}
+}
+
  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 == ROOT_UID)
+		extract_id_from_env("SUDO_UID", &euid);
+
+	return st.st_uid == euid;
  }
#define is_path_owned_by_current_user is_path_owned_by_current_uid



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux