]> git.proxmox.com Git - mirror_lxc.git/commitdiff
tree-wide: split open helpers into open_utils.h
authorChristian Brauner <brauner@kernel.org>
Wed, 17 Aug 2022 07:58:34 +0000 (09:58 +0200)
committerChristian Brauner (Microsoft) <christian.brauner@ubuntu.com>
Wed, 17 Aug 2022 07:58:34 +0000 (09:58 +0200)
Signed-off-by: Christian Brauner (Microsoft) <christian.brauner@ubuntu.com>
20 files changed:
src/lxc/attach.c
src/lxc/caps.c
src/lxc/cgroups/cgfsng.c
src/lxc/cgroups/cgroup.c
src/lxc/cgroups/cgroup_utils.c
src/lxc/cmd/meson.build
src/lxc/conf.c
src/lxc/file_utils.c
src/lxc/file_utils.h
src/lxc/lsm/apparmor.c
src/lxc/lsm/selinux.c
src/lxc/lxccontainer.c
src/lxc/meson.build
src/lxc/mount_utils.c
src/lxc/open_utils.h [new file with mode: 0644]
src/lxc/pam/meson.build
src/lxc/storage/dir.c
src/lxc/syscall_wrappers.h
src/lxc/terminal.c
src/lxc/utils.c

index 77da7bb4561292a9342e94e83cb7fd5d8794fbfa..769613d6db4bf3e066b2ef0c0b634b8cb1d813fd 100644 (file)
@@ -40,6 +40,7 @@
 #include "memory_utils.h"
 #include "mount_utils.h"
 #include "namespace.h"
+#include "open_utils.h"
 #include "process_utils.h"
 #include "sync.h"
 #include "syscall_wrappers.h"
index a99048864ca9c1a544b513eeb2c98eb829fef269..273cf08f57a51ff636c78d47455ecee4d8ae5930 100644 (file)
@@ -14,6 +14,7 @@
 #include "log.h"
 #include "macro.h"
 #include "memory_utils.h"
+#include "open_utils.h"
 
 lxc_log_define(caps, lxc);
 
index ee4fc052fd610ef8b45c85644bfde22828d3112e..8a3615893f16a47bea946ac3f9fe72deea0df432 100644 (file)
@@ -45,6 +45,7 @@
 #include "mainloop.h"
 #include "memory_utils.h"
 #include "mount_utils.h"
+#include "open_utils.h"
 #include "storage/storage.h"
 #include "string_utils.h"
 #include "syscall_wrappers.h"
index 8f6e49e04f1a761cfd517612f33ccd3a141ba5c7..5e2a7d09938b78f49ccfcd32c79c76f1429813c8 100644 (file)
@@ -12,8 +12,9 @@
 #include "compiler.h"
 #include "conf.h"
 #include "initutils.h"
-#include "memory_utils.h"
 #include "log.h"
+#include "memory_utils.h"
+#include "open_utils.h"
 #include "start.h"
 #include "string_utils.h"
 
index c5fb91c2ffa0d0ff889422ec8228b391e6346fe8..dc2fbec4b397f16e0688cbb7beab21bcc67260f5 100644 (file)
@@ -14,6 +14,7 @@
 #include "log.h"
 #include "macro.h"
 #include "memory_utils.h"
+#include "open_utils.h"
 #include "utils.h"
 
 lxc_log_define(cgroup_utils, lxc);
index f84269ecbcab6d3d1e1b83d0d35fd5d8a3a4cd79..c7df528d33a48f8e740569e73783f1f28bb397f4 100644 (file)
@@ -20,6 +20,7 @@ cmd_lxc_init_sources = files(
     '../memory_utils.h',
     '../namespace.c',
     '../namespace.h',
+    '../open_utils.h',
     '../string_utils.c',
     '../string_utils.h') + include_sources
 
@@ -41,6 +42,7 @@ cmd_lxc_init_static_sources = files(
     '../memory_utils.h',
     '../namespace.c',
     '../namespace.h',
+    '../open_utils.h',
     '../string_utils.c',
     '../string_utils.h') + include_sources
 
index 4193cd07f52e9b69cb4b404ba86b40dc4dc4e11a..a04bb0de8e507daab699d3b389bf5648901ae04a 100644 (file)
@@ -50,6 +50,7 @@
 #include "mount_utils.h"
 #include "namespace.h"
 #include "network.h"
+#include "open_utils.h"
 #include "parse.h"
 #include "process_utils.h"
 #include "ringbuf.h"
index 38f056766c88934ec01c3e29afb45f400a770f7e..5ee2bea9e5fa50860f52883e7843ac94ceb37c19 100644 (file)
@@ -15,6 +15,7 @@
 #include "file_utils.h"
 #include "macro.h"
 #include "memory_utils.h"
+#include "open_utils.h"
 #include "string_utils.h"
 #include "syscall_wrappers.h"
 #include "utils.h"
@@ -800,3 +801,8 @@ bool same_device(int fda, const char *patha, int fdb, const char *pathb)
 
        return (st_fda.st_rdev == st_fdb.st_rdev);
 }
+
+int open_beneath(int dfd, const char *path, unsigned int flags)
+{
+       return open_at(dfd, path, flags, PROTECT_LOOKUP_BENEATH, 0);
+}
index e169ab8b0f076d31f600e8085c46286f155f9381..fc20da5a2f077d7c5f13e9ccd6b4dfcc88f733ea 100644 (file)
@@ -108,10 +108,7 @@ __hidden extern int open_at(int dfd, const char *path, unsigned int o_flags,
 __hidden extern int open_at_same(int fd_same, int dfd, const char *path,
                                 unsigned int o_flags,
                                 unsigned int resolve_flags, mode_t mode);
-static inline int open_beneath(int dfd, const char *path, unsigned int flags)
-{
-       return open_at(dfd, path, flags, PROTECT_LOOKUP_BENEATH, 0);
-}
+__hidden extern int open_beneath(int dfd, const char *path, unsigned int flags);
 __hidden int fd_make_nonblocking(int fd);
 __hidden extern char *read_file_at(int dfd, const char *fnam,
                                    unsigned int o_flags,
index fa4e4d6e0b9c8bff74b174bf54df1bf232a5a197..bf0f771e287fd996432d4cf238a6d2f70267a63f 100644 (file)
@@ -18,6 +18,7 @@
 #include "file_utils.h"
 #include "log.h"
 #include "lsm.h"
+#include "open_utils.h"
 #include "parse.h"
 #include "process_utils.h"
 #include "utils.h"
index 7a34b9cc46a80ac26b451921b61b98dc1c8fcbc4..9c131ee29c6d99502c453d46be9f13b32c63a631 100644 (file)
@@ -15,6 +15,7 @@
 #include "log.h"
 #include "lsm.h"
 #include "memory_utils.h"
+#include "open_utils.h"
 
 #define DEFAULT_LABEL "unconfined_t"
 
index 4363340b3c86aaf77317092a6227573af3ab7a95..8df60595a88df9971dae036ff600f94382cec8c0 100644 (file)
@@ -47,6 +47,7 @@
 #include "monitor.h"
 #include "namespace.h"
 #include "network.h"
+#include "open_utils.h"
 #include "parse.h"
 #include "process_utils.h"
 #include "start.h"
index 38faf7f5ed5470a4e3272bf2944ca6ed6cfc6160..b4609e2038f5b5c04f1cc3c098990d648d873bb6 100644 (file)
@@ -114,6 +114,7 @@ liblxc_sources = files(
     'nl.h',
     'parse.c',
     'parse.h',
+    'open_utils.h',
     'process_utils.c',
     'process_utils.h',
     'rexec.c',
index 123bbda779da5526bc74ccfc6c7c8a04d688be2d..fe8da8200b6d7cfbcfc40a98b88f6006a0f7ab7e 100644 (file)
@@ -15,6 +15,7 @@
 #include "macro.h"
 #include "memory_utils.h"
 #include "mount_utils.h"
+#include "open_utils.h"
 #include "syscall_numbers.h"
 #include "syscall_wrappers.h"
 
diff --git a/src/lxc/open_utils.h b/src/lxc/open_utils.h
new file mode 100644 (file)
index 0000000..7ff5945
--- /dev/null
@@ -0,0 +1,87 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#ifndef __LXC_OPEN_UTILS_H
+#define __LXC_OPEN_UTILS_H
+
+#include "config.h"
+
+#include "syscall_numbers.h"
+
+/*
+ * Arguments for how openat2(2) should open the target path. If only @flags and
+ * @mode are non-zero, then openat2(2) operates very similarly to openat(2).
+ *
+ * However, unlike openat(2), unknown or invalid bits in @flags result in
+ * -EINVAL rather than being silently ignored. @mode must be zero unless one of
+ * {O_CREAT, O_TMPFILE} are set.
+ *
+ * @flags: O_* flags.
+ * @mode: O_CREAT/O_TMPFILE file mode.
+ * @resolve: RESOLVE_* flags.
+ */
+#if !HAVE_STRUCT_OPEN_HOW
+struct open_how {
+       __u64 flags;
+       __u64 mode;
+       __u64 resolve;
+};
+#endif
+
+/* how->resolve flags for openat2(2). */
+#ifndef RESOLVE_NO_XDEV
+#define RESOLVE_NO_XDEV                0x01 /* Block mount-point crossings
+                                       (includes bind-mounts). */
+#endif
+
+#ifndef RESOLVE_NO_MAGICLINKS
+#define RESOLVE_NO_MAGICLINKS  0x02 /* Block traversal through procfs-style
+                                       "magic-links". */
+#endif
+
+#ifndef RESOLVE_NO_SYMLINKS
+#define RESOLVE_NO_SYMLINKS    0x04 /* Block traversal through all symlinks
+                                       (implies OEXT_NO_MAGICLINKS) */
+#endif
+
+#ifndef RESOLVE_BENEATH
+#define RESOLVE_BENEATH                0x08 /* Block "lexical" trickery like
+                                       "..", symlinks, and absolute
+                                       paths which escape the dirfd. */
+#endif
+
+#ifndef RESOLVE_IN_ROOT
+#define RESOLVE_IN_ROOT                0x10 /* Make all jumps to "/" and ".."
+                                       be scoped inside the dirfd
+                                       (similar to chroot(2)). */
+#endif
+
+#define PROTECT_LOOKUP_BENEATH  (RESOLVE_BENEATH | RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS)
+#define PROTECT_LOOKUP_BENEATH_WITH_SYMLINKS (PROTECT_LOOKUP_BENEATH & ~RESOLVE_NO_SYMLINKS)
+#define PROTECT_LOOKUP_BENEATH_WITH_MAGICLINKS (PROTECT_LOOKUP_BENEATH & ~(RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS))
+#define PROTECT_LOOKUP_BENEATH_XDEV (PROTECT_LOOKUP_BENEATH & ~RESOLVE_NO_XDEV)
+
+#define PROTECT_LOOKUP_ABSOLUTE (PROTECT_LOOKUP_BENEATH & ~RESOLVE_BENEATH)
+#define PROTECT_LOOKUP_ABSOLUTE_WITH_SYMLINKS (PROTECT_LOOKUP_ABSOLUTE & ~RESOLVE_NO_SYMLINKS)
+#define PROTECT_LOOKUP_ABSOLUTE_WITH_MAGICLINKS (PROTECT_LOOKUP_ABSOLUTE & ~(RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS))
+#define PROTECT_LOOKUP_ABSOLUTE_XDEV (PROTECT_LOOKUP_ABSOLUTE & ~RESOLVE_NO_XDEV)
+#define PROTECT_LOOKUP_ABSOLUTE_XDEV_SYMLINKS (PROTECT_LOOKUP_ABSOLUTE_WITH_SYMLINKS & ~RESOLVE_NO_XDEV)
+
+#define PROTECT_OPATH_FILE (O_NOFOLLOW | O_PATH | O_CLOEXEC)
+#define PROTECT_OPATH_DIRECTORY (PROTECT_OPATH_FILE | O_DIRECTORY)
+
+#define PROTECT_OPEN_WITH_TRAILING_SYMLINKS (O_CLOEXEC | O_NOCTTY | O_RDONLY)
+#define PROTECT_OPEN (PROTECT_OPEN_WITH_TRAILING_SYMLINKS | O_NOFOLLOW)
+
+#define PROTECT_OPEN_W_WITH_TRAILING_SYMLINKS (O_CLOEXEC | O_NOCTTY | O_WRONLY)
+#define PROTECT_OPEN_W (PROTECT_OPEN_W_WITH_TRAILING_SYMLINKS | O_NOFOLLOW)
+#define PROTECT_OPEN_RW (O_CLOEXEC | O_NOCTTY | O_RDWR | O_NOFOLLOW)
+
+#if !HAVE_OPENAT2
+static inline int openat2(int dfd, const char *filename, struct open_how *how, size_t size)
+{
+       return syscall(__NR_openat2, dfd, filename, how, size);
+}
+#endif /* HAVE_OPENAT2 */
+
+#endif /* __LXC_OPEN_UTILS_H */
+
index 3078fb1c4279d52351033a06aa7717d634266808..3151c43fc331941ab18a5079e06d2ee8bb795ed9 100644 (file)
@@ -6,6 +6,7 @@ pam_cgfs_sources = files(
     '../file_utils.h',
     '../macro.h',
     '../memory_utils.h',
+    '../open_utils.h',
     '../string_utils.c',
     '../string_utils.h') + include_sources
 
index dca510140a191cdc9435bbdb5f8c7d768382d760..bdf4e3f3ae6bc046d17ce1185f1cee6e0f1fa74c 100644 (file)
@@ -10,6 +10,7 @@
 #include "macro.h"
 #include "memory_utils.h"
 #include "mount_utils.h"
+#include "open_utils.h"
 #include "storage.h"
 #include "utils.h"
 
index d5b7c3bf0efccbd3e5b5e48ecfe65c0af818167c..87e0294fd37b77d10d415c0d01e5b87f6dcfb938 100644 (file)
@@ -143,82 +143,6 @@ static int faccessat(int __fd, const char *__file, int __type, int __flag)
 }
 #endif
 
-/*
- * Arguments for how openat2(2) should open the target path. If only @flags and
- * @mode are non-zero, then openat2(2) operates very similarly to openat(2).
- *
- * However, unlike openat(2), unknown or invalid bits in @flags result in
- * -EINVAL rather than being silently ignored. @mode must be zero unless one of
- * {O_CREAT, O_TMPFILE} are set.
- *
- * @flags: O_* flags.
- * @mode: O_CREAT/O_TMPFILE file mode.
- * @resolve: RESOLVE_* flags.
- */
-#if !HAVE_STRUCT_OPEN_HOW
-struct open_how {
-       __u64 flags;
-       __u64 mode;
-       __u64 resolve;
-};
-#endif
-
-/* how->resolve flags for openat2(2). */
-#ifndef RESOLVE_NO_XDEV
-#define RESOLVE_NO_XDEV                0x01 /* Block mount-point crossings
-                                       (includes bind-mounts). */
-#endif
-
-#ifndef RESOLVE_NO_MAGICLINKS
-#define RESOLVE_NO_MAGICLINKS  0x02 /* Block traversal through procfs-style
-                                       "magic-links". */
-#endif
-
-#ifndef RESOLVE_NO_SYMLINKS
-#define RESOLVE_NO_SYMLINKS    0x04 /* Block traversal through all symlinks
-                                       (implies OEXT_NO_MAGICLINKS) */
-#endif
-
-#ifndef RESOLVE_BENEATH
-#define RESOLVE_BENEATH                0x08 /* Block "lexical" trickery like
-                                       "..", symlinks, and absolute
-                                       paths which escape the dirfd. */
-#endif
-
-#ifndef RESOLVE_IN_ROOT
-#define RESOLVE_IN_ROOT                0x10 /* Make all jumps to "/" and ".."
-                                       be scoped inside the dirfd
-                                       (similar to chroot(2)). */
-#endif
-
-#define PROTECT_LOOKUP_BENEATH  (RESOLVE_BENEATH | RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS)
-#define PROTECT_LOOKUP_BENEATH_WITH_SYMLINKS (PROTECT_LOOKUP_BENEATH & ~RESOLVE_NO_SYMLINKS)
-#define PROTECT_LOOKUP_BENEATH_WITH_MAGICLINKS (PROTECT_LOOKUP_BENEATH & ~(RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS))
-#define PROTECT_LOOKUP_BENEATH_XDEV (PROTECT_LOOKUP_BENEATH & ~RESOLVE_NO_XDEV)
-
-#define PROTECT_LOOKUP_ABSOLUTE (PROTECT_LOOKUP_BENEATH & ~RESOLVE_BENEATH)
-#define PROTECT_LOOKUP_ABSOLUTE_WITH_SYMLINKS (PROTECT_LOOKUP_ABSOLUTE & ~RESOLVE_NO_SYMLINKS)
-#define PROTECT_LOOKUP_ABSOLUTE_WITH_MAGICLINKS (PROTECT_LOOKUP_ABSOLUTE & ~(RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS))
-#define PROTECT_LOOKUP_ABSOLUTE_XDEV (PROTECT_LOOKUP_ABSOLUTE & ~RESOLVE_NO_XDEV)
-#define PROTECT_LOOKUP_ABSOLUTE_XDEV_SYMLINKS (PROTECT_LOOKUP_ABSOLUTE_WITH_SYMLINKS & ~RESOLVE_NO_XDEV)
-
-#define PROTECT_OPATH_FILE (O_NOFOLLOW | O_PATH | O_CLOEXEC)
-#define PROTECT_OPATH_DIRECTORY (PROTECT_OPATH_FILE | O_DIRECTORY)
-
-#define PROTECT_OPEN_WITH_TRAILING_SYMLINKS (O_CLOEXEC | O_NOCTTY | O_RDONLY)
-#define PROTECT_OPEN (PROTECT_OPEN_WITH_TRAILING_SYMLINKS | O_NOFOLLOW)
-
-#define PROTECT_OPEN_W_WITH_TRAILING_SYMLINKS (O_CLOEXEC | O_NOCTTY | O_WRONLY)
-#define PROTECT_OPEN_W (PROTECT_OPEN_W_WITH_TRAILING_SYMLINKS | O_NOFOLLOW)
-#define PROTECT_OPEN_RW (O_CLOEXEC | O_NOCTTY | O_RDWR | O_NOFOLLOW)
-
-#if !HAVE_OPENAT2
-static inline int openat2(int dfd, const char *filename, struct open_how *how, size_t size)
-{
-       return syscall(__NR_openat2, dfd, filename, how, size);
-}
-#endif /* HAVE_OPENAT2 */
-
 #ifndef CLOSE_RANGE_UNSHARE
 #define CLOSE_RANGE_UNSHARE    (1U << 1)
 #endif
index 38ba5c14d01089f9ff0c915eb2cd59de62871922..a1dcc2dc7750fcb9a1b62e0451d8a1b8b3586e78 100644 (file)
@@ -23,6 +23,7 @@
 #include "lxclock.h"
 #include "mainloop.h"
 #include "memory_utils.h"
+#include "open_utils.h"
 #include "start.h"
 #include "syscall_wrappers.h"
 #include "terminal.h"
index 0e2a7188bba72cfcffd90da0cae0cc44b9b404a9..d3d82e23e08c5b013ec55829b5c70538524188a0 100644 (file)
@@ -32,6 +32,7 @@
 #include "lxclock.h"
 #include "memory_utils.h"
 #include "namespace.h"
+#include "open_utils.h"
 #include "parse.h"
 #include "process_utils.h"
 #include "syscall_wrappers.h"