Lines 131-137
int resolve_dirfd_path(int dirfd, const char *path, char *resolved_path,
Link Here
|
131 |
|
131 |
|
132 |
save_errno(); |
132 |
save_errno(); |
133 |
|
133 |
|
134 |
size_t at_len = resolved_path_len - 1 - 1 - (path ? strlen(path) : 0); |
134 |
/*unused: size_t at_len = resolved_path_len - 1 - 1 - (path ? strlen(path) : 0);*/ |
135 |
if (trace_pid) { |
135 |
if (trace_pid) { |
136 |
sprintf(resolved_path, "/proc/%i/fd/%i", trace_pid, dirfd); |
136 |
sprintf(resolved_path, "/proc/%i/fd/%i", trace_pid, dirfd); |
137 |
} else { |
137 |
} else { |
Lines 141-147
int resolve_dirfd_path(int dirfd, const char *path, char *resolved_path,
Link Here
|
141 |
*/ |
141 |
*/ |
142 |
sprintf(resolved_path, "%s/%i", sb_get_fd_dir(), dirfd); |
142 |
sprintf(resolved_path, "%s/%i", sb_get_fd_dir(), dirfd); |
143 |
} |
143 |
} |
144 |
ssize_t ret = readlink(resolved_path, resolved_path, at_len); |
144 |
|
|
|
145 |
|
146 |
/* avoid undefined behaviour resulting from passing resolved_path |
147 |
* as source and destination buffer to readlink: |
148 |
* C99 warning: passing argument 2 to 'restrict'-qualified |
149 |
* parameter aliases with argument 1 [-Wrestrict] |
150 |
*/ |
151 |
char buffer[resolved_path_len]; |
152 |
|
153 |
ssize_t ret = readlink(resolved_path, buffer, sizeof(buffer)); |
145 |
if (ret == -1) { |
154 |
if (ret == -1) { |
146 |
/* see comments at end of check_syscall() */ |
155 |
/* see comments at end of check_syscall() */ |
147 |
if (errno_is_too_long()) { |
156 |
if (errno_is_too_long()) { |
Lines 153-163
int resolve_dirfd_path(int dirfd, const char *path, char *resolved_path,
Link Here
|
153 |
if (errno == ENOENT) |
162 |
if (errno == ENOENT) |
154 |
errno = EBADF; |
163 |
errno = EBADF; |
155 |
return -1; |
164 |
return -1; |
156 |
} |
165 |
} else if (ret + 1 >= sizeof(buffer)) { |
157 |
resolved_path[ret] = '/'; |
166 |
errno = ENOMEM; |
158 |
resolved_path[ret + 1] = '\0'; |
167 |
sb_debug_dyn("AT_FD LOOKUP: unsufficient buffer space for resolved_path; max len is %ld; %s\n", sizeof(buffer), strerror(errno)); |
159 |
if (path) |
168 |
return -1; |
160 |
strcat(resolved_path, path); |
169 |
} |
|
|
170 |
|
171 |
buffer[ret] = '/'; |
172 |
buffer[ret + 1] = '\0'; |
173 |
if (path) { |
174 |
if ( strlen(buffer) + strlen(path) + 1 < sizeof(buffer) ) { |
175 |
strcat(buffer, path); |
176 |
} else { |
177 |
errno = ENOMEM; |
178 |
sb_debug_dyn("AT_FD LOOKUP: unsufficient buffer space for resolved_path+path; max len is %ld; %s\n", sizeof(buffer), strerror(errno)); |
179 |
return -1; |
180 |
} |
181 |
} |
182 |
|
183 |
strcpy(resolved_path,buffer); |
161 |
|
184 |
|
162 |
restore_errno(); |
185 |
restore_errno(); |
163 |
return 0; |
186 |
return 0; |