Skip to content

Commit 5a688fe

Browse files
committed
"core.sharedrepository = 0mode" should set, not loosen
This fixes the behaviour of octal notation to how it is defined in the documentation, while keeping the traditional "loosen only" semantics intact for "group" and "everybody". Three main points of this patch are: - For an explicit octal notation, the internal shared_repository variable is set to a negative value, so that we can tell "group" (which is to "OR" in 0660) and 0660 (which is to "SET" to 0660); - git-init did not set shared_repository variable early enough to affect the initial creation of many files, notably copied templates and the configuration. We set it very early when a command-line option specifies a custom value. - Many codepaths create files inside $GIT_DIR by various ways that all involve mkstemp(), and then call move_temp_to_file() to rename it to its final destination. We can add adjust_shared_perm() call here; for the traditional "loosen-only", this would be a no-op for many codepaths because the mode is already loose enough, but with the new behaviour it makes a difference. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 389d176 commit 5a688fe

File tree

5 files changed

+77
-20
lines changed

5 files changed

+77
-20
lines changed

builtin-init-db.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ static int create_default_files(const char *template_path)
195195

196196
git_config(git_default_config, NULL);
197197
is_bare_repository_cfg = init_is_bare_repository;
198+
199+
/* reading existing config may have overwrote it */
198200
if (init_shared_repository != -1)
199201
shared_repository = init_shared_repository;
200202

@@ -313,12 +315,15 @@ int init_db(const char *template_dir, unsigned int flags)
313315
* and compatibility values for PERM_GROUP and
314316
* PERM_EVERYBODY.
315317
*/
316-
if (shared_repository == PERM_GROUP)
318+
if (shared_repository < 0)
319+
/* force to the mode value */
320+
sprintf(buf, "0%o", -shared_repository);
321+
else if (shared_repository == PERM_GROUP)
317322
sprintf(buf, "%d", OLD_PERM_GROUP);
318323
else if (shared_repository == PERM_EVERYBODY)
319324
sprintf(buf, "%d", OLD_PERM_EVERYBODY);
320325
else
321-
sprintf(buf, "0%o", shared_repository);
326+
die("oops");
322327
git_config_set("core.sharedrepository", buf);
323328
git_config_set("receive.denyNonFastforwards", "true");
324329
}
@@ -398,6 +403,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
398403
usage(init_db_usage);
399404
}
400405

406+
if (init_shared_repository != -1)
407+
shared_repository = init_shared_repository;
408+
401409
/*
402410
* GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
403411
* without --bare. Catch the error early.

path.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -314,33 +314,39 @@ char *enter_repo(char *path, int strict)
314314
int adjust_shared_perm(const char *path)
315315
{
316316
struct stat st;
317-
int mode;
317+
int mode, tweak, shared;
318318

319319
if (!shared_repository)
320320
return 0;
321321
if (lstat(path, &st) < 0)
322322
return -1;
323323
mode = st.st_mode;
324-
325-
if (shared_repository) {
326-
int tweak = shared_repository;
327-
if (!(mode & S_IWUSR))
328-
tweak &= ~0222;
324+
if (shared_repository < 0)
325+
shared = -shared_repository;
326+
else
327+
shared = shared_repository;
328+
tweak = shared;
329+
330+
if (!(mode & S_IWUSR))
331+
tweak &= ~0222;
332+
if (mode & S_IXUSR)
333+
/* Copy read bits to execute bits */
334+
tweak |= (tweak & 0444) >> 2;
335+
if (shared_repository < 0)
336+
mode = (mode & ~0777) | tweak;
337+
else
329338
mode |= tweak;
330-
} else {
331-
/* Preserve old PERM_UMASK behaviour */
332-
if (mode & S_IWUSR)
333-
mode |= S_IWGRP;
334-
}
335339

336340
if (S_ISDIR(mode)) {
337-
mode |= FORCE_DIR_SET_GID;
338-
339341
/* Copy read bits to execute bits */
340-
mode |= (shared_repository & 0444) >> 2;
342+
mode |= (shared & 0444) >> 2;
343+
mode |= FORCE_DIR_SET_GID;
341344
}
342345

343-
if ((mode & st.st_mode) != mode && chmod(path, mode) < 0)
346+
if (((shared_repository < 0
347+
? (st.st_mode & (FORCE_DIR_SET_GID | 0777))
348+
: (st.st_mode & mode)) != mode) &&
349+
chmod(path, mode) < 0)
344350
return -2;
345351
return 0;
346352
}

setup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ int git_config_perm(const char *var, const char *value)
434434

435435
/*
436436
* Treat values 0, 1 and 2 as compatibility cases, otherwise it is
437-
* a chmod value.
437+
* a chmod value to restrict to.
438438
*/
439439
switch (i) {
440440
case PERM_UMASK: /* 0 */
@@ -456,7 +456,7 @@ int git_config_perm(const char *var, const char *value)
456456
* Mask filemode value. Others can not get write permission.
457457
* x flags for directories are handled separately.
458458
*/
459-
return i & 0666;
459+
return -(i & 0666);
460460
}
461461

462462
int check_repository_format_version(const char *var, const char *value, void *cb)

sha1_file.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2243,11 +2243,15 @@ static void write_sha1_file_prepare(const void *buf, unsigned long len,
22432243
}
22442244

22452245
/*
2246-
* Move the just written object into its final resting place
2246+
* Move the just written object into its final resting place.
2247+
* NEEDSWORK: this should be renamed to finalize_temp_file() as
2248+
* "moving" is only a part of what it does, when no patch between
2249+
* master to pu changes the call sites of this function.
22472250
*/
22482251
int move_temp_to_file(const char *tmpfile, const char *filename)
22492252
{
22502253
int ret = 0;
2254+
22512255
if (link(tmpfile, filename))
22522256
ret = errno;
22532257

@@ -2275,6 +2279,8 @@ int move_temp_to_file(const char *tmpfile, const char *filename)
22752279
/* FIXME!!! Collision check here ? */
22762280
}
22772281

2282+
if (adjust_shared_perm(filename))
2283+
return error("unable to set permission to '%s'", filename);
22782284
return 0;
22792285
}
22802286

t/t1301-shared-repo.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,41 @@ test_expect_success 'git reflog expire honors core.sharedRepository' '
126126
esac
127127
'
128128

129+
test_expect_success 'forced modes' '
130+
mkdir -p templates/hooks &&
131+
echo update-server-info >templates/hooks/post-update &&
132+
chmod +x templates/hooks/post-update &&
133+
echo : >random-file &&
134+
mkdir new &&
135+
(
136+
cd new &&
137+
umask 002 &&
138+
git init --shared=0660 --template=../templates &&
139+
>frotz &&
140+
git add frotz &&
141+
git commit -a -m initial &&
142+
git repack
143+
) &&
144+
find new/.git -print |
145+
xargs ls -ld >actual &&
146+
147+
# Everything must be unaccessible to others
148+
test -z "$(sed -n -e "/^.......---/d" actual)" &&
149+
150+
# All directories must have 2770
151+
test -z "$(sed -n -e "/^drwxrws---/d" -e "/^d/p" actual)" &&
152+
153+
# post-update hook must be 0770
154+
test -z "$(sed -n -e "/post-update/{
155+
/^-rwxrwx---/d
156+
p
157+
}" actual)" &&
158+
159+
# All files inside objects must be 0440
160+
test -z "$(sed -n -e "/objects\//{
161+
/^d/d
162+
/^-r--r-----/d
163+
}" actual)"
164+
'
165+
129166
test_done

0 commit comments

Comments
 (0)