1 From c1115e1503bf955c97f4cf3b925a6a9f619764c3 Mon Sep 17 00:00:00 2001
2 From: Christian Brauner <brauner@kernel.org>
3 Date: Tue, 9 Aug 2022 16:14:25 +0200
4 Subject: [PATCH 1/3] build: detect where struct mount_attr is declared
5
6 Fixes: #4176
7 Signed-off-by: Christian Brauner (Microsoft) <christian.brauner@ubuntu.com>
8 ---
9 meson.build | 30 ++++++++++++++++++++++++++++--
10 src/lxc/conf.c | 6 +++---
11 src/lxc/conf.h | 2 +-
12 src/lxc/mount_utils.c | 6 +++---
13 src/lxc/syscall_wrappers.h | 12 ++++++++++--
14 5 files changed, 45 insertions(+), 11 deletions(-)
15
16 diff --git a/meson.build b/meson.build
17 index a145faf069..f679aabbc8 100644
18 --- a/meson.build
19 +++ b/meson.build
20 @@ -590,7 +590,6 @@ decl_headers = '''
21 foreach decl: [
22 '__aligned_u64',
23 'struct clone_args',
24 - 'struct mount_attr',
25 'struct open_how',
26 'struct rtnl_link_stats64',
27 ]
28 @@ -610,7 +609,6 @@ foreach tuple: [
29 ['struct seccomp_notif_sizes'],
30 ['struct clone_args'],
31 ['__aligned_u64'],
32 - ['struct mount_attr'],
33 ['struct open_how'],
34 ['struct rtnl_link_stats64'],
35 ]
36 @@ -630,6 +628,34 @@ foreach tuple: [
37 endif
38 endforeach
39
40 +## Types.
41 +decl_headers = '''
42 +#include <sys/mount.h>
43 +'''
44 +
45 +# We get -1 if the size cannot be determined
46 +if cc.sizeof('struct mount_attr', prefix: decl_headers, args: '-D_GNU_SOURCE') > 0
47 + srcconf.set10('HAVE_' + 'struct mount_attr'.underscorify().to_upper(), true)
48 + found_types += 'struct mount_attr (sys/mount.h)'
49 +else
50 + srcconf.set10('HAVE_' + 'struct mount_attr'.underscorify().to_upper(), false)
51 + missing_types += 'struct mount_attr (sys/mount.h)'
52 +endif
53 +
54 +## Types.
55 +decl_headers = '''
56 +#include <linux/mount.h>
57 +'''
58 +
59 +# We get -1 if the size cannot be determined
60 +if cc.sizeof('struct mount_attr', prefix: decl_headers, args: '-D_GNU_SOURCE') > 0
61 + srcconf.set10('HAVE_UAPI_' + 'struct mount_attr'.underscorify().to_upper(), true)
62 + found_types += 'struct mount_attr (linux/mount.h)'
63 +else
64 + srcconf.set10('HAVE_UAPI_' + 'struct mount_attr'.underscorify().to_upper(), false)
65 + missing_types += 'struct mount_attr (linux/mount.h)'
66 +endif
67 +
68 ## Headers.
69 foreach ident: [
70 ['bpf', '''#include <sys/syscall.h>
71 diff --git a/src/lxc/conf.c b/src/lxc/conf.c
72 index ffbe74c2f6..4193cd07f5 100644
73 --- a/src/lxc/conf.c
74 +++ b/src/lxc/conf.c
75 @@ -2885,7 +2885,7 @@ static int __lxc_idmapped_mounts_child(struct lxc_handler *handler, FILE *f)
76 struct lxc_mount_options opts = {};
77 int dfd_from;
78 const char *source_relative, *target_relative;
79 - struct lxc_mount_attr attr = {};
80 + struct mount_attr attr = {};
81
82 ret = parse_lxc_mount_attrs(&opts, mntent.mnt_opts);
83 if (ret < 0)
84 @@ -3005,7 +3005,7 @@ static int __lxc_idmapped_mounts_child(struct lxc_handler *handler, FILE *f)
85
86 /* Set propagation mount options. */
87 if (opts.attr.propagation) {
88 - attr = (struct lxc_mount_attr) {
89 + attr = (struct mount_attr) {
90 .propagation = opts.attr.propagation,
91 };
92
93 @@ -4109,7 +4109,7 @@ int lxc_idmapped_mounts_parent(struct lxc_handler *handler)
94
95 for (;;) {
96 __do_close int fd_from = -EBADF, fd_userns = -EBADF;
97 - struct lxc_mount_attr attr = {};
98 + struct mount_attr attr = {};
99 struct lxc_mount_options opts = {};
100 ssize_t ret;
101
102 diff --git a/src/lxc/conf.h b/src/lxc/conf.h
103 index 7dc2f15b60..772479f9e1 100644
104 --- a/src/lxc/conf.h
105 +++ b/src/lxc/conf.h
106 @@ -223,7 +223,7 @@ struct lxc_mount_options {
107 unsigned long mnt_flags;
108 unsigned long prop_flags;
109 char *data;
110 - struct lxc_mount_attr attr;
111 + struct mount_attr attr;
112 char *raw_options;
113 };
114
115 diff --git a/src/lxc/mount_utils.c b/src/lxc/mount_utils.c
116 index bba75f933c..88dd73ee36 100644
117 --- a/src/lxc/mount_utils.c
118 +++ b/src/lxc/mount_utils.c
119 @@ -31,7 +31,7 @@ lxc_log_define(mount_utils, lxc);
120 * setting in @attr_set, but must also specify MOUNT_ATTR__ATIME in the
121 * @attr_clr field.
122 */
123 -static inline void set_atime(struct lxc_mount_attr *attr)
124 +static inline void set_atime(struct mount_attr *attr)
125 {
126 switch (attr->attr_set & MOUNT_ATTR__ATIME) {
127 case MOUNT_ATTR_RELATIME:
128 @@ -272,7 +272,7 @@ int create_detached_idmapped_mount(const char *path, int userns_fd,
129 {
130 __do_close int fd_tree_from = -EBADF;
131 unsigned int open_tree_flags = OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC;
132 - struct lxc_mount_attr attr = {
133 + struct mount_attr attr = {
134 .attr_set = MOUNT_ATTR_IDMAP | attr_set,
135 .attr_clr = attr_clr,
136 .userns_fd = userns_fd,
137 @@ -335,7 +335,7 @@ int __fd_bind_mount(int dfd_from, const char *path_from, __u64 o_flags_from,
138 __u64 attr_clr, __u64 propagation, int userns_fd,
139 bool recursive)
140 {
141 - struct lxc_mount_attr attr = {
142 + struct mount_attr attr = {
143 .attr_set = attr_set,
144 .attr_clr = attr_clr,
145 .propagation = propagation,
146 diff --git a/src/lxc/syscall_wrappers.h b/src/lxc/syscall_wrappers.h
147 index a5e98b565c..c8a7d0c7b7 100644
148 --- a/src/lxc/syscall_wrappers.h
149 +++ b/src/lxc/syscall_wrappers.h
150 @@ -18,6 +18,12 @@
151 #include "macro.h"
152 #include "syscall_numbers.h"
153
154 +#if HAVE_STRUCT_MOUNT_ATTR
155 +#include <sys/mount.h>
156 +#elif HAVE_UAPI_STRUCT_MOUNT_ATTR
157 +#include <linux/mount.h>
158 +#endif
159 +
160 #ifdef HAVE_LINUX_MEMFD_H
161 #include <linux/memfd.h>
162 #endif
163 @@ -210,16 +216,18 @@ extern int fsmount(int fs_fd, unsigned int flags, unsigned int attr_flags);
164 /*
165 * mount_setattr()
166 */
167 -struct lxc_mount_attr {
168 +#if !HAVE_STRUCT_MOUNT_ATTR && !HAVE_UAPI_STRUCT_MOUNT_ATTR
169 +struct mount_attr {
170 __u64 attr_set;
171 __u64 attr_clr;
172 __u64 propagation;
173 __u64 userns_fd;
174 };
175 +#endif
176
177 #if !HAVE_MOUNT_SETATTR
178 static inline int mount_setattr(int dfd, const char *path, unsigned int flags,
179 - struct lxc_mount_attr *attr, size_t size)
180 + struct mount_attr *attr, size_t size)
181 {
182 return syscall(__NR_mount_setattr, dfd, path, flags, attr, size);
183 }
184
185 From ef1e0607b82e27350c2d677d649c6a0a9693fd40 Mon Sep 17 00:00:00 2001
186 From: Christian Brauner <brauner@kernel.org>
187 Date: Tue, 9 Aug 2022 16:27:40 +0200
188 Subject: [PATCH 2/3] build: detect sys/pidfd.h availability
189
190 Fixes: #4176
191 Signed-off-by: Christian Brauner (Microsoft) <christian.brauner@ubuntu.com>
192 ---
193 meson.build | 1 +
194 src/lxc/process_utils.h | 6 ++++++
195 2 files changed, 7 insertions(+)
196
197 diff --git a/meson.build b/meson.build
198 index f679aabbc8..e999542336 100644
199 --- a/meson.build
200 +++ b/meson.build
201 @@ -735,6 +735,7 @@ foreach tuple: [
202 ['sys/resource.h'],
203 ['sys/memfd.h'],
204 ['sys/personality.h'],
205 + ['sys/pidfd.h'],
206 ['sys/signalfd.h'],
207 ['sys/timerfd.h'],
208 ['pty.h'],
209 diff --git a/src/lxc/process_utils.h b/src/lxc/process_utils.h
210 index 9c15b15741..ed84741d0e 100644
211 --- a/src/lxc/process_utils.h
212 +++ b/src/lxc/process_utils.h
213 @@ -15,6 +15,10 @@
214 #include <sys/syscall.h>
215 #include <unistd.h>
216
217 +#if HAVE_SYS_PIDFD_H
218 +#include <sys/pidfd.h>
219 +#endif
220 +
221 #include "compiler.h"
222 #include "syscall_numbers.h"
223
224 @@ -136,9 +140,11 @@
225 #endif
226
227 /* waitid */
228 +#if !HAVE_SYS_PIDFD_H
229 #ifndef P_PIDFD
230 #define P_PIDFD 3
231 #endif
232 +#endif
233
234 #ifndef CLONE_ARGS_SIZE_VER0
235 #define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */
236
237 From cbabe8abf11e7e7fb49c123bae31efdd9bc8f1e8 Mon Sep 17 00:00:00 2001
238 From: Christian Brauner <brauner@kernel.org>
239 Date: Tue, 9 Aug 2022 17:19:40 +0200
240 Subject: [PATCH 3/3] build: check for FS_CONFIG_* header symbol in sys/mount.h
241
242 Fixes: #4176
243 Signed-off-by: Christian Brauner (Microsoft) <christian.brauner@ubuntu.com>
244 ---
245 meson.build | 59 +++++++++++++++++++++++++++++++++++++++++--
246 src/lxc/mount_utils.h | 16 ++++++++++++
247 2 files changed, 73 insertions(+), 2 deletions(-)
248
249 diff --git a/meson.build b/meson.build
250 index e999542336..9f8a5de60c 100644
251 --- a/meson.build
252 +++ b/meson.build
253 @@ -639,8 +639,7 @@ if cc.sizeof('struct mount_attr', prefix: decl_headers, args: '-D_GNU_SOURCE') >
254 found_types += 'struct mount_attr (sys/mount.h)'
255 else
256 srcconf.set10('HAVE_' + 'struct mount_attr'.underscorify().to_upper(), false)
257 - missing_types += 'struct mount_attr (sys/mount.h)'
258 -endif
259 + missing_types += 'struct mount_attr (sys/mount.h)' endif
260
261 ## Types.
262 decl_headers = '''
263 @@ -656,6 +655,62 @@ else
264 missing_types += 'struct mount_attr (linux/mount.h)'
265 endif
266
267 +if cc.has_header_symbol('sys/mount.h', 'FSCONFIG_SET_FLAG')
268 + srcconf.set10('HAVE_' + 'FSCONFIG_SET_FLAG'.underscorify().to_upper(), true)
269 + found_types += 'FSCONFIG_SET_FLAG'
270 +else
271 + srcconf.set10('HAVE_' + 'FSCONFIG_SET_FLAG'.underscorify().to_upper(), false)
272 + missing_types += 'FSCONFIG_SET_FLAG'
273 +endif
274 +
275 +if cc.has_header_symbol('sys/mount.h', 'FS_CONFIG_SET_STRING')
276 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_STRING'.underscorify().to_upper(), true)
277 + found_types += 'FS_CONFIG_SET_STRING'
278 +else
279 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_STRING'.underscorify().to_upper(), false)
280 + missing_types += 'FS_CONFIG_SET_STRING'
281 +endif
282 +
283 +if cc.has_header_symbol('sys/mount.h', 'FS_CONFIG_SET_BINARY')
284 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_BINARY'.underscorify().to_upper(), true)
285 + found_types += 'FS_CONFIG_SET_BINARY'
286 +else
287 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_BINARY'.underscorify().to_upper(), false)
288 + missing_types += 'FS_CONFIG_SET_BINARY'
289 +endif
290 +
291 +if cc.has_header_symbol('sys/mount.h', 'FS_CONFIG_SET_PATH_EMPTY')
292 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_PATH_EMPTY'.underscorify().to_upper(), true)
293 + found_types += 'FS_CONFIG_SET_PATH_EMPTY'
294 +else
295 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_PATH_EMPTY'.underscorify().to_upper(), false)
296 + missing_types += 'FS_CONFIG_SET_PATH_EMPTY'
297 +endif
298 +
299 +if cc.has_header_symbol('sys/mount.h', 'FS_CONFIG_SET_PATH_FD')
300 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_PATH_FD'.underscorify().to_upper(), true)
301 + found_types += 'FS_CONFIG_SET_PATH_FD'
302 +else
303 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_PATH_FD'.underscorify().to_upper(), false)
304 + missing_types += 'FS_CONFIG_SET_PATH_FD'
305 +endif
306 +
307 +if cc.has_header_symbol('sys/mount.h', 'FS_CONFIG_SET_CMD_CREATE')
308 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_CMD_CREATE'.underscorify().to_upper(), true)
309 + found_types += 'FS_CONFIG_SET_CMD_CREATE'
310 +else
311 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_CMD_CREATE'.underscorify().to_upper(), false)
312 + missing_types += 'FS_CONFIG_SET_CMD_CREATE'
313 +endif
314 +
315 +if cc.has_header_symbol('sys/mount.h', 'FS_CONFIG_SET_CMD_RECONFIGURE')
316 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_CMD_RECONFIGURE'.underscorify().to_upper(), true)
317 + found_types += 'FS_CONFIG_SET_CMD_RECONFIGURE'
318 +else
319 + srcconf.set10('HAVE_' + 'FS_CONFIG_SET_CMD_RECONFIGURE'.underscorify().to_upper(), false)
320 + missing_types += 'FS_CONFIG_SET_CMD_RECONFIGURE'
321 +endif
322 +
323 ## Headers.
324 foreach ident: [
325 ['bpf', '''#include <sys/syscall.h>
326 diff --git a/src/lxc/mount_utils.h b/src/lxc/mount_utils.h
327 index ea392672d8..fd34739459 100644
328 --- a/src/lxc/mount_utils.h
329 +++ b/src/lxc/mount_utils.h
330 @@ -82,37 +82,53 @@ struct lxc_rootfs;
331 #endif
332
333 /* fsconfig() commands */
334 +#if !HAVE_FSCONFIG_SET_FLAG
335 #ifndef FSCONFIG_SET_FLAG
336 #define FSCONFIG_SET_FLAG 0 /* Set parameter, supplying no value */
337 #endif
338 +#endif
339
340 +#if !HAVE_FSCONFIG_SET_STRING
341 #ifndef FSCONFIG_SET_STRING
342 #define FSCONFIG_SET_STRING 1 /* Set parameter, supplying a string value */
343 #endif
344 +#endif
345
346 +#if !HAVE_FSCONFIG_SET_BINARY
347 #ifndef FSCONFIG_SET_BINARY
348 #define FSCONFIG_SET_BINARY 2 /* Set parameter, supplying a binary blob value */
349 #endif
350 +#endif
351
352 +#if !HAVE_FSCONFIG_SET_PATH
353 #ifndef FSCONFIG_SET_PATH
354 #define FSCONFIG_SET_PATH 3 /* Set parameter, supplying an object by path */
355 #endif
356 +#endif
357
358 +#if !HAVE_FSCONFIG_SET_PATH_EMPTY
359 #ifndef FSCONFIG_SET_PATH_EMPTY
360 #define FSCONFIG_SET_PATH_EMPTY 4 /* Set parameter, supplying an object by (empty) path */
361 #endif
362 +#endif
363
364 +#if !HAVE_FSCONFIG_SET_FD
365 #ifndef FSCONFIG_SET_FD
366 #define FSCONFIG_SET_FD 5 /* Set parameter, supplying an object by fd */
367 #endif
368 +#endif
369
370 +#if !HAVE_FSCONFIG_CMD_CREATE
371 #ifndef FSCONFIG_CMD_CREATE
372 #define FSCONFIG_CMD_CREATE 6 /* Invoke superblock creation */
373 #endif
374 +#endif
375
376 +#if !FSCONFIG_CMD_RECONFIGURE
377 #ifndef FSCONFIG_CMD_RECONFIGURE
378 #define FSCONFIG_CMD_RECONFIGURE 7 /* Invoke superblock reconfiguration */
379 #endif
380 +#endif
381
382 /* fsmount() flags */
383 #ifndef FSMOUNT_CLOEXEC
|