Revision 9cf85473209ea8ae2b56c13145c4704d12ee1374 authored by Filip Hejsek on 28 January 2024, 04:09:17 UTC, committed by Johannes Schindelin on 17 April 2024, 20:30:01 UTC
While it is expected to have several git dirs within the `.git/modules/`
tree, it is important that they do not interfere with each other. For
example, if one submodule was called "captain" and another submodule
"captain/hooks", their respective git dirs would clash, as they would be
located in `.git/modules/captain/` and `.git/modules/captain/hooks/`,
respectively, i.e. the latter's files could clash with the actual Git
hooks of the former.

To prevent these clashes, and in particular to prevent hooks from being
written and then executed as part of a recursive clone, we introduced
checks as part of the fix for CVE-2019-1387 in a8dee3ca61 (Disallow
dubiously-nested submodule git directories, 2019-10-01).

It is currently possible to bypass the check for clashing submodule
git dirs in two ways:

1. parallel cloning
2. checkout --recurse-submodules

Let's check not only before, but also after parallel cloning (and before
checking out the submodule), that the git dir is not clashing with
another one, otherwise fail. This addresses the parallel cloning issue.

As to the parallel checkout issue: It requires quite a few manual steps
to create clashing git dirs because Git itself would refuse to
initialize the inner one, as demonstrated by the test case.

Nevertheless, let's teach the recursive checkout (namely, the
`submodule_move_head()` function that is used by the recursive checkout)
to be careful to verify that it does not use a clashing git dir, and if
it does, disable it (by deleting the `HEAD` file so that subsequent Git
calls won't recognize it as a git dir anymore).

Note: The parallel cloning test case contains a `cat err` that proved to
be highly useful when analyzing the racy nature of the operation (the
operation can fail with three different error messages, depending on
timing), and was left on purpose to ease future debugging should the
need arise.

Signed-off-by: Filip Hejsek <filip.hejsek@gmail.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent b20c10f
Raw File
cbtree.c
/*
 * crit-bit tree implementation, does no allocations internally
 * For more information on crit-bit trees: https://cr.yp.to/critbit.html
 * Based on Adam Langley's adaptation of Dan Bernstein's public domain code
 * git clone https://github.com/agl/critbit.git
 */
#include "cbtree.h"

static struct cb_node *cb_node_of(const void *p)
{
	return (struct cb_node *)((uintptr_t)p - 1);
}

/* locate the best match, does not do a final comparision */
static struct cb_node *cb_internal_best_match(struct cb_node *p,
					const uint8_t *k, size_t klen)
{
	while (1 & (uintptr_t)p) {
		struct cb_node *q = cb_node_of(p);
		uint8_t c = q->byte < klen ? k[q->byte] : 0;
		size_t direction = (1 + (q->otherbits | c)) >> 8;

		p = q->child[direction];
	}
	return p;
}

/* returns NULL if successful, existing cb_node if duplicate */
struct cb_node *cb_insert(struct cb_tree *t, struct cb_node *node, size_t klen)
{
	size_t newbyte, newotherbits;
	uint8_t c;
	int newdirection;
	struct cb_node **wherep, *p;

	assert(!((uintptr_t)node & 1)); /* allocations must be aligned */

	if (!t->root) {		/* insert into empty tree */
		t->root = node;
		return NULL;	/* success */
	}

	/* see if a node already exists */
	p = cb_internal_best_match(t->root, node->k, klen);

	/* find first differing byte */
	for (newbyte = 0; newbyte < klen; newbyte++) {
		if (p->k[newbyte] != node->k[newbyte])
			goto different_byte_found;
	}
	return p;	/* element exists, let user deal with it */

different_byte_found:
	newotherbits = p->k[newbyte] ^ node->k[newbyte];
	newotherbits |= newotherbits >> 1;
	newotherbits |= newotherbits >> 2;
	newotherbits |= newotherbits >> 4;
	newotherbits = (newotherbits & ~(newotherbits >> 1)) ^ 255;
	c = p->k[newbyte];
	newdirection = (1 + (newotherbits | c)) >> 8;

	node->byte = newbyte;
	node->otherbits = newotherbits;
	node->child[1 - newdirection] = node;

	/* find a place to insert it */
	wherep = &t->root;
	for (;;) {
		struct cb_node *q;
		size_t direction;

		p = *wherep;
		if (!(1 & (uintptr_t)p))
			break;
		q = cb_node_of(p);
		if (q->byte > newbyte)
			break;
		if (q->byte == newbyte && q->otherbits > newotherbits)
			break;
		c = q->byte < klen ? node->k[q->byte] : 0;
		direction = (1 + (q->otherbits | c)) >> 8;
		wherep = q->child + direction;
	}

	node->child[newdirection] = *wherep;
	*wherep = (struct cb_node *)(1 + (uintptr_t)node);

	return NULL; /* success */
}

struct cb_node *cb_lookup(struct cb_tree *t, const uint8_t *k, size_t klen)
{
	struct cb_node *p = cb_internal_best_match(t->root, k, klen);

	return p && !memcmp(p->k, k, klen) ? p : NULL;
}

static enum cb_next cb_descend(struct cb_node *p, cb_iter fn, void *arg)
{
	if (1 & (uintptr_t)p) {
		struct cb_node *q = cb_node_of(p);
		enum cb_next n = cb_descend(q->child[0], fn, arg);

		return n == CB_BREAK ? n : cb_descend(q->child[1], fn, arg);
	} else {
		return fn(p, arg);
	}
}

void cb_each(struct cb_tree *t, const uint8_t *kpfx, size_t klen,
			cb_iter fn, void *arg)
{
	struct cb_node *p = t->root;
	struct cb_node *top = p;
	size_t i = 0;

	if (!p) return; /* empty tree */

	/* Walk tree, maintaining top pointer */
	while (1 & (uintptr_t)p) {
		struct cb_node *q = cb_node_of(p);
		uint8_t c = q->byte < klen ? kpfx[q->byte] : 0;
		size_t direction = (1 + (q->otherbits | c)) >> 8;

		p = q->child[direction];
		if (q->byte < klen)
			top = p;
	}

	for (i = 0; i < klen; i++) {
		if (p->k[i] != kpfx[i])
			return; /* "best" match failed */
	}
	cb_descend(top, fn, arg);
}
back to top