Skip to contentSkip to conversation

infinite loop in "git ls-tree" for broken symlink

PATCH In review Petr Stodulka 2 participants 6 messages Oct 14, 2016

lore
Share & tools
lore
#0 lore raw

cover infinite loop in "git ls-tree" for broken symlink

Hi,
I have realized that this wasn't fixed after all when refs.c was "rewritten". Issue is caused by broken symlink under refs/heads, which causes infinite loop for "git ls-tree" command. It was replied earlier [0] and Michael previously fixed that in better way probably, then my proposed patch 2/2, but it contains more changes and I have not enough time to study changes in refs* code, so I propose now just my little patch, which was previously modified by Michael.

If you prefer different solution, I am ok with this. It is really just quick proposal. Patch 1/2 contains test for this issue. If you will prefer different solution with different exit code, test should be corrected. Basic idea is, that timeout should'n expire with exit code 124.

[0] http://marc.info/?l=git&m=142712669103790&w=2
[1] https://github.com/mhagger/git, branch "ref-broken-symlinks"

Michael Haggerty (1):
resolve_ref_unsafe(): limit the number of "stat_ref" retries

Petr Stodulka (1):
Add test for ls-tree with broken symlink under refs/heads

 refs/files-backend.c    | 6 ++++--
 refs/refs-internal.h    | 6 ++++++
 t/t3103-ls-tree-misc.sh | 9 +++++++++
 3 files changed, 19 insertions(+), 2 deletions(-)
Signature · 1 line
2.5.5
#1 lore raw

PATCH 1/2 Add test for ls-tree with broken symlink under refs/heads

git ls-tree goes into an infinite loop while serving pretty vanilla requests, if the refs/heads/ directory contains a symlink that's broken. Added test which check if git ends with expected exit code or timeout expires.

---
 t/t3103-ls-tree-misc.sh | 9 +++++++++
 1 file changed, 9 insertions(+)
t/t3103-ls-tree-misc.sh +9 -0
index 09dcf04..faf79c4 100755
@@ -21,4 +21,13 @@ test_expect_success 'ls-tree fails with non-zero exit code on broken tree' '
 	test_must_fail git ls-tree -r HEAD
 '
 
+test_expect_success 'ls-tree fails due to broken symlink instead of infinite loop' '
+	mkdir foo_infinit &&
+	cd foo_infinit &&
+	git init testrepo &&
+	cd testrepo &&
+	mkdir -p .git/refs/remotes &&
+	ln -s ../remotes/foo .git/refs/heads/bar &&
+	test_expect_code 128 timeout 2 git ls-tree bar
+'
 test_done
Signature · 1 line
2.5.5
#2 lore raw

PATCH 2/2 resolve_ref_unsafe(): limit the number of "stat_ref" retries

In reply to Petr Stodulka

From: Michael Haggerty <[email protected]>

If there is a broken symlink where a loose reference file is expected, then the attempt to open() it fails with ENOENT. This error is misinterpreted to mean that the loose reference file itself has disappeared due to a race, causing the lookup to be retried. But in this scenario, the retries all suffer from the same problem, causing an infinite loop.

So put a limit (of 5) on the number of times that the stat_ref step can be retried.

---
 refs/files-backend.c | 6 ++++--
 refs/refs-internal.h | 6 ++++++
 2 files changed, 10 insertions(+), 2 deletions(-)
refs/files-backend.c +4 -2
index d16feb1..245a0b5 100644
@@ -1353,6 +1353,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
 	int fd;
 	int ret = -1;
 	int save_errno;
+	int retries = 0;
 
 	*type = 0;
 	strbuf_reset(&sb_path);
@@ -1390,7 +1391,8 @@ static int files_read_raw_ref(struct ref_store *ref_store,
 	if (S_ISLNK(st.st_mode)) {
 		strbuf_reset(&sb_contents);
 		if (strbuf_readlink(&sb_contents, path, 0) < 0) {
-			if (errno == ENOENT || errno == EINVAL)
+			if ((errno == ENOENT || errno == EINVAL) &&
+				retries++ < MAXRETRIES) 
 				/* inconsistent with lstat; retry */
 				goto stat_ref;
 			else
@@ -1426,7 +1428,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
 	 */
 	fd = open(path, O_RDONLY);
 	if (fd < 0) {
-		if (errno == ENOENT)
+		if (errno == ENOENT && retries++ < MAXRETRIES)
 			/* inconsistent with lstat; retry */
 			goto stat_ref;
 		else
refs/refs-internal.h +6 -0
index 708b260..37e6b99 100644
@@ -255,6 +255,12 @@ int rename_ref_available(const char *old_refname, const char *new_refname);
 /* We allow "recursive" symbolic refs. Only within reason, though */
 #define SYMREF_MAXDEPTH 5
 
+/* 
+ * We allow only MAXRETRIES tries to jump on stat_ref, because of possible
+ * infinite loop
+ */
+#define MAXRETRIES 5
+
 /* Include broken references in a do_for_each_ref*() iteration: */
 #define DO_FOR_EACH_INCLUDE_BROKEN 0x01
 
Signature · 1 line
2.5.5
#3 lore raw

Re: [PATCH 2/2] resolve_ref_unsafe(): limit the number of "stat_ref" retries

FYI,
I modified the patch slightly.

Quoted 68 lines · Petr Stodulka

From: Michael Haggerty <[email protected]>

If there is a broken symlink where a loose reference file is expected, then the attempt to open() it fails with ENOENT. This error is misinterpreted to mean that the loose reference file itself has disappeared due to a race, causing the lookup to be retried. But in this scenario, the retries all suffer from the same problem, causing an infinite loop.

So put a limit (of 5) on the number of times that the stat_ref step can be retried.

---
 refs/files-backend.c | 6 ++++--
 refs/refs-internal.h | 6 ++++++
 2 files changed, 10 insertions(+), 2 deletions(-)
refs/files-backend.c +4 -2
index d16feb1..245a0b5 100644
@@ -1353,6 +1353,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
 	int fd;
 	int ret = -1;
 	int save_errno;
+	int retries = 0;
 
 	*type = 0;
 	strbuf_reset(&sb_path);
@@ -1390,7 +1391,8 @@ static int files_read_raw_ref(struct ref_store *ref_store,
 	if (S_ISLNK(st.st_mode)) {
 		strbuf_reset(&sb_contents);
 		if (strbuf_readlink(&sb_contents, path, 0) < 0) {
-			if (errno == ENOENT || errno == EINVAL)
+			if ((errno == ENOENT || errno == EINVAL) &&
+				retries++ < MAXRETRIES) 
 				/* inconsistent with lstat; retry */
 				goto stat_ref;
 			else
@@ -1426,7 +1428,7 @@ static int files_read_raw_ref(struct ref_store *ref_store,
 	 */
 	fd = open(path, O_RDONLY);
 	if (fd < 0) {
-		if (errno == ENOENT)
+		if (errno == ENOENT && retries++ < MAXRETRIES)
 			/* inconsistent with lstat; retry */
 			goto stat_ref;
 		else
refs/refs-internal.h +6 -0
index 708b260..37e6b99 100644
@@ -255,6 +255,12 @@ int rename_ref_available(const char *old_refname, const char *new_refname);
 /* We allow "recursive" symbolic refs. Only within reason, though */
 #define SYMREF_MAXDEPTH 5
 
+/* 
+ * We allow only MAXRETRIES tries to jump on stat_ref, because of possible
+ * infinite loop
+ */
+#define MAXRETRIES 5
+
 /* Include broken references in a do_for_each_ref*() iteration: */
 #define DO_FOR_EACH_INCLUDE_BROKEN 0x01
 
#4 lore raw
In reply to Petr Stodulka
Quoted 8 lines · Petr Stodulka

I have realized that this wasn't fixed after all when refs.c was "rewritten". Issue is caused by broken symlink under refs/heads, which causes infinite loop for "git ls-tree" command. It was replied earlier [0] and Michael previously fixed that in better way probably, then my proposed patch 2/2, but it contains more changes and I have not enough time to study changes in refs* code, so I propose now just my little patch, which was previously modified by Michael.

Quoted 8 lines

If you prefer different solution, I am ok with this. It is really just quick proposal. Patch 1/2 contains test for this issue. If you will prefer different solution with different exit code, test should be corrected. Basic idea is, that timeout should'n expire with exit code 124.

[0] http://marc.info/?l=git&m=142712669103790&w=2
[1] https://github.com/mhagger/git, branch "ref-broken-symlinks"

I think I fixed this semi-independently last week; see the thread at:

http://public-inbox.org/git/[email protected]/

I say semi-independently because I didn't actually know about your previous report, but saw it in the wild myself. But I did talk with Michael off-list, and he suggested the belt-and-suspenders retry counter in my second patch.

The fix is at e8c42cb in Junio's tree, and it's currently merged to 'next'.

-Peff

#5 lore raw

Thank you for info, I totally missed that. Yes, this fixes the issue perfectly.

Petr

Quoted 33 lines · Jeff King
Quoted 8 lines · Petr Stodulka

I have realized that this wasn't fixed after all when refs.c was "rewritten". Issue is caused by broken symlink under refs/heads, which causes infinite loop for "git ls-tree" command. It was replied earlier [0] and Michael previously fixed that in better way probably, then my proposed patch 2/2, but it contains more changes and I have not enough time to study changes in refs* code, so I propose now just my little patch, which was previously modified by Michael.

Quoted 8 lines

If you prefer different solution, I am ok with this. It is really just quick proposal. Patch 1/2 contains test for this issue. If you will prefer different solution with different exit code, test should be corrected. Basic idea is, that timeout should'n expire with exit code 124.

[0] http://marc.info/?l=git&m=142712669103790&w=2
[1] https://github.com/mhagger/git, branch "ref-broken-symlinks"

I think I fixed this semi-independently last week; see the thread at:

http://public-inbox.org/git/[email protected]/

I say semi-independently because I didn't actually know about your previous report, but saw it in the wild myself. But I did talk with Michael off-list, and he suggested the belt-and-suspenders retry counter in my second patch.

The fix is at e8c42cb in Junio's tree, and it's currently merged to 'next'.

-Peff