-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathsafe_openat.c
More file actions
333 lines (283 loc) · 9.08 KB
/
Copy pathsafe_openat.c
File metadata and controls
333 lines (283 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* This code was copied from:
*
* crun - OCI runtime written in C
*
* Copyright (C) 2017, 2018, 2019 Giuseppe Scrivano <giuseppe@scrivano.org>
* crun is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* crun is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with crun. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "utils.h"
#include <sys/syscall.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#ifndef __set_errno
#define __set_errno(val) ((errno) = (val))
#endif
bool opt_force_openat_fallback = false;
#if ASSUMED_KERNEL < BWRAP_KERNEL_VERSION (5, 6, 0)
# define USE_OPENAT_FALLBACK 1
#else
# define USE_OPENAT_FALLBACK 0
#endif
#define cleanup_close cleanup_fd
#define LIKELY(x) __builtin_expect ((x), 1)
#define UNLIKELY(x) __builtin_expect ((x), 0)
/* Adapted from systemd. Include space for the NUL byte. */
#define DECIMAL_STR_MAX(type) \
((size_t) 2U + (sizeof (type) <= 1 ? 3U : sizeof (type) <= 2 ? 5U \
: sizeof (type) <= 4 ? 10U \
: sizeof (type) <= 8 ? 20U \
: sizeof (int[-2 * (sizeof (type) > 8)])))
#define _STRLEN(s) (sizeof (s) - 1)
/* openat2 resolve flags */
#ifndef RESOLVE_IN_ROOT
# define RESOLVE_IN_ROOT 0x10
#endif
#ifndef RESOLVE_NO_MAGICLINKS
# define RESOLVE_NO_MAGICLINKS 0x02
#endif
/* Structures for syscalls */
struct openat2_open_how
{
uint64_t flags;
uint64_t mode;
uint64_t resolve;
};
static inline int
syscall_openat2 (int dirfd, const char *path, uint64_t flags, uint64_t mode, uint64_t resolve)
{
#ifdef __NR_openat2
struct openat2_open_how how = {
.flags = flags,
.mode = mode,
.resolve = resolve,
};
return (int) syscall (__NR_openat2, dirfd, path, &how, sizeof (how), 0);
#else
(void) dirfd;
(void) path;
(void) flags;
(void) mode;
(void) resolve;
errno = ENOSYS;
return -1;
#endif
}
#define consume_slashes(t) \
({ \
__typeof__ (t) _s = (t); \
while (*_s == '/') \
_s++; \
_s; \
})
/* _STRLEN("self") < DECIMAL_STR_MAX (pid_t), so we don't need to calculate the length of both. */
#define PROC_PID_FD_STRLEN (_STRLEN ("/proc/") + DECIMAL_STR_MAX (pid_t) \
+ _STRLEN ("/fd/") + DECIMAL_STR_MAX (int))
/* A buffer long enough to hold either /proc/self/fd/$FD or a /proc/$PID/fd/$FD path. */
typedef char proc_fd_path_t[PROC_PID_FD_STRLEN];
static inline void
get_proc_fd_path (proc_fd_path_t path, pid_t pid, int fd)
{
const size_t max_len = sizeof (proc_fd_path_t);
size_t n;
if (pid)
n = snprintf (path, max_len, "/proc/%d/fd/%d", pid, fd);
else
n = snprintf (path, max_len, "/proc/self/fd/%d", fd);
if (UNLIKELY (n >= max_len))
abort ();
}
static inline void
get_proc_self_fd_path (proc_fd_path_t path, int fd)
{
get_proc_fd_path (path, 0, fd);
}
static int
check_fd_is_path (const char *path, int fd, UNUSED const char *fdname)
{
proc_fd_path_t fdpath;
size_t path_len = strlen (path);
char link[PATH_MAX];
int ret;
get_proc_self_fd_path (fdpath, fd);
ret = TEMP_FAILURE_RETRY (readlink (fdpath, link, sizeof (link)));
if (UNLIKELY (ret < 0))
return -1;
if (((size_t) ret) != path_len || memcmp (link, path, path_len))
{
// crun_make_error (err, 0, "target `%s` does not point to the directory `%s`", fdname, path);
__set_errno(ENOTDIR);
return -1;
}
return 0;
}
#if USE_OPENAT_FALLBACK
static int
check_fd_under_path (const char *rootfs, size_t rootfslen, int fd, UNUSED const char *fdname)
{
proc_fd_path_t fdpath;
char link[PATH_MAX];
int ret;
/* Every path is under "/", there is nothing to verify. The check below
would reject any path since it expects a '/' right after the rootfs. */
if (rootfslen == 0 || (rootfslen == 1 && rootfs[0] == '/'))
return 0;
get_proc_self_fd_path (fdpath, fd);
ret = TEMP_FAILURE_RETRY (readlink (fdpath, link, sizeof (link)));
if (UNLIKELY (ret < 0))
return -1;
if (((size_t) ret) <= rootfslen || memcmp (link, rootfs, rootfslen) != 0 || link[rootfslen] != '/')
{
// crun_make_error (err, 0, "target `%s` not under the directory `%s`", fdname, rootfs);
__set_errno(ENOTDIR);
return -1;
}
return 0;
}
#endif
/* DIRFD must be a file descriptor for ROOTFS itself: PATH is resolved
against ROOTFS and the result is then opened relatively to DIRFD. */
static int
safe_openat_fallback (int dirfd, const char *rootfs, const char *path, int flags,
int mode)
{
#if !USE_OPENAT_FALLBACK
(void) dirfd;
(void) rootfs;
(void) path;
(void) flags;
(void) mode;
errno = ENOSYS;
return -1;
#else
cleanup_free char *parent_path = NULL;
const char *last_component = NULL;
const char *orig_path = path;
const char *path_in_chroot;
cleanup_close int fd = -1;
char resolved[PATH_MAX];
char buffer[PATH_MAX];
size_t rootfs_len = is_empty_string (rootfs) ? 0 : strlen (rootfs);
int ret;
/* chroot_realpath resolves the last component as well, so when O_NOFOLLOW
is requested resolve only the parent directory and let openat(2) deal
with the last component, otherwise a symlink would be followed even
though the caller asked not to. */
if (flags & O_NOFOLLOW)
{
char *sep;
parent_path = xstrdup (path);
sep = strrchr (parent_path, '/');
if (sep == NULL)
{
/* No parent directory, the entire path is the last component. */
last_component = path;
parent_path[0] = '\0';
}
else if (sep[1] != '\0')
{
*sep = '\0';
last_component = path + (sep - parent_path) + 1;
}
/* A trailing '/' forces the symlink to be resolved anyway, so in that
case keep resolving the entire path. */
if (last_component)
path = parent_path;
}
path_in_chroot = chroot_realpath (rootfs, path, buffer);
if (path_in_chroot == NULL)
return -1;
/* When rootfs is "/" or not set, chroot_realpath returns the path
unchanged, so drop the prefix only when it is really there. */
if (rootfs_len > 0 && strncmp (path_in_chroot, rootfs, rootfs_len) == 0)
path_in_chroot += rootfs_len;
path_in_chroot = consume_slashes (path_in_chroot);
if (last_component)
{
ret = snprintf (resolved, sizeof (resolved), "%s%s%s", path_in_chroot,
path_in_chroot[0] == '\0' ? "" : "/", last_component);
if (UNLIKELY (ret >= (int) sizeof (resolved)))
{
__set_errno (ENAMETOOLONG);
return -1;
}
path_in_chroot = resolved;
}
/* If the path is empty we are at the root, dup the dirfd itself. */
if (path_in_chroot[0] == '\0')
{
ret = dup (dirfd);
if (UNLIKELY (ret < 0))
return -1;
return ret;
}
ret = openat (dirfd, path_in_chroot, flags, mode);
if (UNLIKELY (ret < 0))
return -1;
fd = ret;
ret = check_fd_under_path (rootfs, rootfs_len, fd, orig_path);
if (UNLIKELY (ret < 0))
return ret;
ret = fd;
fd = -1;
return ret;
#endif /* USE_OPENAT_FALLBACK */
}
int
safe_openat (int dirfd, const char *rootfs, const char *path, int flags, int mode)
{
static bool openat2_supported = true;
int ret;
if (is_empty_string (path))
{
cleanup_close int fd = -1;
fd = open (rootfs, flags, mode);
if (UNLIKELY (fd < 0))
return -1;
/* Skip the readlink-based check when opening the root
directory itself (rootfs="/", path=""). After pivot_root,
"/" can only refer to the container root so the readlink
verification is redundant, and after setns the /proc-based
readlink may not be reachable by path yet. */
if (rootfs[0] != '/' || rootfs[1] != '\0')
{
ret = check_fd_is_path (rootfs, fd, path);
if (UNLIKELY (ret < 0))
return ret;
}
ret = fd;
fd = -1;
return ret;
}
if (openat2_supported && !opt_force_openat_fallback)
{
repeat:
ret = syscall_openat2 (dirfd, path, flags, mode, RESOLVE_IN_ROOT|RESOLVE_NO_MAGICLINKS);
if (UNLIKELY (ret < 0))
{
if (errno == EINTR || errno == EAGAIN)
goto repeat;
if (errno == ENOSYS)
openat2_supported = false;
if (errno == ENOSYS || errno == EINVAL || errno == EPERM)
return safe_openat_fallback (dirfd, rootfs, path, flags, mode);
return -1;
}
return ret;
}
return safe_openat_fallback (dirfd, rootfs, path, flags, mode);
}