Protect index with sparse prefix
PATCH 4/12 Protect index with sparse prefix
This ensures no one can write to index outside sparse prefix. Protection is usually applied to the_index only. For more complicated cases where temporary index is required, thorough check will be done by check_index_prefix()
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>
--- builtin-commit.c | 11 +++++ builtin-ls-files.c | 2 + builtin-merge-recursive.c | 4 +- builtin-read-tree.c | 5 ++ cache.h | 1 + diff-lib.c | 3 + read-cache.c | 107 ++++++++++++++++++++++++++++++++++++++++++++- unpack-trees.c | 3 + unpack-trees.h | 3 +- 9 files changed, 135 insertions(+), 4 deletions(-)
builtin-commit.c +11 -0
@@ -223,6 +223,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)int fd;struct string_list partial;const char **pathspec =NULL ; +char **saved_sparse_prefix;if (interactive) {interactive_add (argc, argv, prefix);
@@ -306,6 +307,13 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)memset (&partial,0 ,sizeof (partial)); partial.strdup_strings =1 ; + +/* +* this code modifies index but won't write down anything +* so it's safe to turn of sparse_prefix protection for a while +*/ + saved_sparse_prefix =save_sparse_prefix (); +if (list_paths (&partial, initial_commit ?NULL :"HEAD" , prefix, pathspec))exit (1 );
@@ -313,6 +321,9 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)if (read_cache () <0 )die ("cannot read the index" ); +/* re-enable sparse_prefix again */ +restore_sparse_prefix (saved_sparse_prefix); + fd =hold_locked_index (&index_lock,1 );add_remove_files (&partial);refresh_cache (REFRESH_QUIET );
builtin-ls-files.c +2 -0
@@ -606,6 +606,8 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)*/ if (show_stage || show_unmerged)die ("ls-files --with-tree is incompatible with -s or -u" ); +/* no need to restore as ls-files never writes index */ +save_sparse_prefix ();overlay_tree_on_cache (with_tree, prefix); }show_files (&dir, prefix);
builtin-merge-recursive.c +3 -1
@@ -201,8 +201,10 @@ static int git_merge_trees(int index_only,memset (&opts,0 ,sizeof (opts));if (index_only) opts.index_only =1 ; -else +else { opts.update =1 ; + opts.check_index_prefix =1 ; + } opts.merge =1 ; opts.head_idx =2 ; opts.fn = threeway_merge;
builtin-read-tree.c +5 -0
@@ -218,6 +218,11 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) opts.head_idx =1 ; } +if (get_sparse_prefix ()) { +read_cache (); + opts.check_index_prefix =1 ; + } +for (i =0 ; i < nr_trees; i++) {struct tree *tree = trees[i];parse_tree (tree);
cache.h +1 -0
@@ -331,6 +331,7 @@ extern char **split_prefix(const char *env);extern char **combine_prefix_list (char **p1,char **p2);extern void free_prefix_list (char **prefix_list);extern int outside_prefix_list (char **iprefix,const char *prefix); +extern int check_index_prefix (const struct index_state *index,int is_merge);#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
diff-lib.c +3 -0
@@ -73,6 +73,9 @@ int run_diff_files(struct rev_info *revs, unsigned int option)struct cache_entry *ce = active_cache[i];int changed; +if (index_outside_sparse_prefix (ce->name)) +continue ; +if (DIFF_OPT_TST (&revs->diffopt,QUIET ) &&DIFF_OPT_TST (&revs->diffopt,HAS_CHANGES ))break ;
read-cache.c +105 -2
156 lines not on this page · raw message
unpack-trees.c +3 -0
@@ -408,6 +408,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_optionsif (o->trivial_merges_only && o->nontrivial_merge)return unpack_failed (o,"Merge requires file-level merging" ); +if (o->check_index_prefix && !check_index_prefix (&o->result, o->merge && !o->prefix)) +return unpack_failed (o,"Merge outside index prefix" ); + o->src_index =NULL ; ret =check_updates (o) ? (-2 ) :0 ;if (o->dst_index)
unpack-trees.h +2 -1
@@ -26,7 +26,8 @@ struct unpack_trees_options { verbose_update:1, aggressive:1, skip_unmerged:1, - gently:1; + gently:1, + check_index_prefix:1;const char *prefix;int pos;struct dir_struct *dir;
Signature · 1 line
1.5.5.GIT