git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2] read-cache: write all indexes with the same permissions

From
Christian Couder <christian.couder@gmail.com>
Date
Nov 16, 2018, 17:31 UTC
Message-ID
<20181116173105.21784-1-chriscool@tuxfamily.org>
From: Ævar Arnfjörð Bjarmason <avarab@gmail.com>

Change the code that writes out the shared index to use mks_tempfile_sm() instead of mks_tempfile().

The create_tempfile() function is used to write out the main ".git/index" (via ".git/index.lock") using lock_file(). The create_tempfile() function respects the umask, as it uses open() with 0666, whereas the mks_tempfile() function uses open() with 0600.

So mks_tempfile() which is used to create the shared index file is likely to create such a file with restricted permissions compared to the main ".git/index" file.

A bug related to this was spotted, fixed and tested for in df801f3f9f ("read-cache: use shared perms when writing shared index", 2017-06-25) and 3ee83f48e5 ("t1700: make sure split-index respects core.sharedrepository", 2017-06-25).

However, as noted in those commits we'd still create the file as 0600, and would just re-chmod it depending on the setting of core.sharedRepository. So without core.splitIndex a system with e.g. the umask set to group writeability would work for the members of the group, but not with core.splitIndex set, as members of the group would not be able to access the shared index file.

Let's instead make the two consistent by using mks_tempfile_sm() and passing 0666 in its `mode` argument.

Note that we cannot use the create_tempfile() function itself that is used to write the main ".git/index" file because we want the XXXXXX part of the "sharedindex_XXXXXX" argument to be replaced by a pseudo random value and create_tempfile() doesn't do that.

Ideally we'd split up the adjust_shared_perm() function to one that can give us the mode we want so we could just call open() instead of open() followed by chmod(), but that's an unrelated cleanup. We already have that minor issue with the "index" file #leftoverbits.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---

This is a simpler fix iterating from Ævar's RFC patch and the following discussions:

https://public-inbox.org/git/20181113153235.25402-1-avarab@gmail.com/
 read-cache.c           |  3 ++-
 t/t1700-split-index.sh | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/read-cache.c b/read-cache.c
index 8c924506dd..ea80600bff 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -3165,7 +3165,8 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
 		struct tempfile *temp;
 		int saved_errno;
 
-		temp = mks_tempfile(git_path("sharedindex_XXXXXX"));
+		/* Same permissions as the main .git/index file */
+		temp = mks_tempfile_sm(git_path("sharedindex_XXXXXX"), 0, 0666);
 		if (!temp) {
 			oidclr(&si->base_oid);
 			ret = do_write_locked_index(istate, lock, flags);
diff --git a/t/t1700-split-index.sh b/t/t1700-split-index.sh
index 2ac47aa0e4..fa1d3d468b 100755
--- a/t/t1700-split-index.sh
+++ b/t/t1700-split-index.sh
@@ -381,6 +381,26 @@ test_expect_success 'check splitIndex.sharedIndexExpire set to "never" and "now"
 	test $(ls .git/sharedindex.* | wc -l) -le 2
 '
 
+test_expect_success POSIXPERM 'same mode for index & split index' '
+	git init same-mode &&
+	(
+		cd same-mode &&
+		test_commit A &&
+		test_modebits .git/index >index_mode &&
+		test_must_fail git config core.sharedRepository &&
+		git -c core.splitIndex=true status &&
+		shared=$(ls .git/sharedindex.*) &&
+		case "$shared" in
+		*" "*)
+			# we have more than one???
+			false ;;
+		*)
+			test_modebits "$shared" >split_index_mode &&
+			test_cmp index_mode split_index_mode ;;
+		esac
+	)
+'
+
 while read -r mode modebits
 do
 	test_expect_success POSIXPERM "split index respects core.sharedrepository $mode" '
-- 
2.19.1.1053.g063ed687ac
Next: Duy Nguyen
Message 1 of 16 in “read-cache: write all indexes with the same permissions”
  1. read-cache: write all indexes with the same permissionsChristian Couder, Nov 16, 2018
  2. Duy NguyenNov 16, 2018
  3. Christian CouderNov 16, 2018
  4. Duy NguyenNov 16, 2018
  5. SZEDER GáborNov 16, 2018
  6. Ævar Arnfjörð BjarmasonNov 16, 2018
  7. Christian CouderNov 16, 2018
  8. Junio C HamanoNov 17, 2018
  9. SZEDER GáborNov 17, 2018
  10. Junio C HamanoNov 17, 2018
  11. Christian CouderNov 17, 2018
  12. Junio C HamanoNov 17, 2018
  13. Ævar Arnfjörð BjarmasonNov 17, 2018
  14. Junio C HamanoNov 18, 2018
  15. Ævar Arnfjörð BjarmasonNov 18, 2018
  16. read-cache: make the split index obey umask settingsÆvar Arnfjörð Bjarmason, Nov 18, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.