@matheusbsbs / matheus-bagliano / commits / 5d5a7a6738
Fork 0

fully resolve symlinks when creating lockfiles

Make the code for resolving symlinks in lockfile.c more robust as follows: 1. Handle relative symlinks 2. recursively resolve symlink chains up to 5 [jc: removed lstat/stat calls to do things stupid way] Signed-off-by: Bradford C. Smith <bradford.carl.smith@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bradford C. Smith committed Jul 26, 2007 at 13:34 UTC 5d5a7a67384ad03007eea1f365ee255c02a40fa3
1 file changed +102 -14
lockfile.c
+102 -14
@@ -25,23 +25,111 @@ static void remove_lock_file_on_signal(int signo)
25 raise(signo);
26 }
27
28 +/*
29 + * p = absolute or relative path name
30 + *
31 + * Return a pointer into p showing the beginning of the last path name
32 + * element. If p is empty or the root directory ("/"), just return p.
33 + */
34 +static char *last_path_elm(char *p)
35 +{
36 + /* r starts pointing to null at the end of the string */
37 + char *r = strchr(p, '\0');
38 +
39 + if (r == p)
40 + return p; /* just return empty string */
41 +
42 + r--; /* back up to last non-null character */
43 +
44 + /* back up past trailing slashes, if any */
45 + while (r > p && *r == '/')
46 + r--;
47 +
48 + /*
49 + * then go backwards until I hit a slash, or the beginning of
50 + * the string
51 + */
52 + while (r > p && *(r-1) != '/')
53 + r--;
54 + return r;
55 +}
56 +
57 +
58 +/* We allow "recursive" symbolic links. Only within reason, though */
59 +#define MAXDEPTH 5
60 +
61 +/*
62 + * p = path that may be a symlink
63 + * s = full size of p
64 + *
65 + * If p is a symlink, attempt to overwrite p with a path to the real
66 + * file or directory (which may or may not exist), following a chain of
67 + * symlinks if necessary. Otherwise, leave p unmodified.
68 + *
69 + * This is a best-effort routine. If an error occurs, p will either be
70 + * left unmodified or will name a different symlink in a symlink chain
71 + * that started with p's initial contents.
72 + *
73 + * Always returns p.
74 + */
75 +
76 +static char *resolve_symlink(char *p, size_t s)
77 +{
78 + int depth = MAXDEPTH;
79 +
80 + while (depth--) {
81 + char link[PATH_MAX];
82 + int link_len = readlink(p, link, sizeof(link));
83 + if (link_len < 0) {
84 + /* not a symlink anymore */
85 + return p;
86 + }
87 + else if (link_len < sizeof(link))
88 + /* readlink() never null-terminates */
89 + link[link_len] = '\0';
90 + else {
91 + warning("%s: symlink too long", p);
92 + return p;
93 + }
94 +
95 + if (link[0] == '/') {
96 + /* absolute path simply replaces p */
97 + if (link_len < s)
98 + strcpy(p, link);
99 + else {
100 + warning("%s: symlink too long", p);
101 + return p;
102 + }
103 + } else {
104 + /*
105 + * link is a relative path, so I must replace the
106 + * last element of p with it.
107 + */
108 + char *r = (char*)last_path_elm(p);
109 + if (r - p + link_len < s)
110 + strcpy(r, link);
111 + else {
112 + warning("%s: symlink too long", p);
113 + return p;
114 + }
115 + }
116 + }
117 + return p;
118 +}
119 +
120 +
121 static int lock_file(struct lock_file *lk, const char *path)
122 {
123 int fd;
31 - struct stat st;
32 -
33 - if ((!lstat(path, &st)) && S_ISLNK(st.st_mode)) {
34 - ssize_t sz;
35 - static char target[PATH_MAX];
36 - sz = readlink(path, target, sizeof(target));
37 - if (sz < 0)
38 - warning("Cannot readlink %s", path);
39 - else if (target[0] != '/')
40 - warning("Cannot lock target of relative symlink %s", path);
41 - else
42 - path = target;
43 - }
44 - sprintf(lk->filename, "%s.lock", path);
124 +
125 + if (strlen(path) >= sizeof(lk->filename)) return -1;
126 + strcpy(lk->filename, path);
127 + /*
128 + * subtract 5 from size to make sure there's room for adding
129 + * ".lock" for the lock file name
130 + */
131 + resolve_symlink(lk->filename, sizeof(lk->filename)-5);
132 + strcat(lk->filename, ".lock");
133 fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
134 if (0 <= fd) {
135 if (!lock_file_list) {