]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
tests: add test for rootfs mount options 3921/head
authorChristian Brauner <christian.brauner@ubuntu.com>
Tue, 3 Aug 2021 12:40:28 +0000 (14:40 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Tue, 3 Aug 2021 12:48:51 +0000 (14:48 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
.gitignore
src/tests/Makefile.am
src/tests/rootfs_options.c [new file with mode: 0644]

index 0ec731bb7c2515a59c0e32f9e0aa6cae38d50580..ac6c1e909d84d3ace6b6a27dbd7a3adb9bc232bd 100644 (file)
@@ -107,6 +107,7 @@ src/tests/lxc-test-basic
 src/tests/lxc-test-cve-2019-5736
 src/tests/lxc-test-mount-injection
 src/tests/lxc-test-sys-mixed
+src/tests/lxc-test-rootfs-options
 
 config/compile
 config/config.guess
index 95cce8504cd2ccb7b82aefb4e6c5eca7761496aa..f22394a2d33640ea0088d40cc088f6c498b1e175 100644 (file)
@@ -759,6 +759,60 @@ if !HAVE_STRCHRNUL
 lxc_test_sys_mixed_SOURCES += ../include/strchrnul.c ../include/strchrnul.h
 endif
 
+lxc_test_rootfs_options_SOURCES = rootfs_options.c \
+                                 ../lxc/af_unix.c ../lxc/af_unix.h \
+                                 ../lxc/caps.c ../lxc/caps.h \
+                                 ../lxc/cgroups/cgfsng.c \
+                                 ../lxc/cgroups/cgroup.c ../lxc/cgroups/cgroup.h \
+                                 ../lxc/cgroups/cgroup2_devices.c ../lxc/cgroups/cgroup2_devices.h \
+                                 ../lxc/cgroups/cgroup_utils.c ../lxc/cgroups/cgroup_utils.h \
+                                 ../lxc/commands.c ../lxc/commands.h \
+                                 ../lxc/commands_utils.c ../lxc/commands_utils.h \
+                                 ../lxc/conf.c ../lxc/conf.h \
+                                 ../lxc/confile.c ../lxc/confile.h \
+                                 ../lxc/confile_utils.c ../lxc/confile_utils.h \
+                                 ../lxc/error.c ../lxc/error.h \
+                                 ../lxc/file_utils.c ../lxc/file_utils.h \
+                                 ../include/netns_ifaddrs.c ../include/netns_ifaddrs.h \
+                                 ../lxc/initutils.c ../lxc/initutils.h \
+                                 ../lxc/log.c ../lxc/log.h \
+                                 ../lxc/lxclock.c ../lxc/lxclock.h \
+                                 ../lxc/mainloop.c ../lxc/mainloop.h \
+                                 ../lxc/monitor.c ../lxc/monitor.h \
+                                 ../lxc/mount_utils.c ../lxc/mount_utils.h \
+                                 ../lxc/namespace.c ../lxc/namespace.h \
+                                 ../lxc/network.c ../lxc/network.h \
+                                 ../lxc/nl.c ../lxc/nl.h \
+                                 ../lxc/parse.c ../lxc/parse.h \
+                                 ../lxc/process_utils.c ../lxc/process_utils.h \
+                                 ../lxc/ringbuf.c ../lxc/ringbuf.h \
+                                 ../lxc/start.c ../lxc/start.h \
+                                 ../lxc/state.c ../lxc/state.h \
+                                 ../lxc/storage/btrfs.c ../lxc/storage/btrfs.h \
+                                 ../lxc/storage/dir.c ../lxc/storage/dir.h \
+                                 ../lxc/storage/loop.c ../lxc/storage/loop.h \
+                                 ../lxc/storage/lvm.c ../lxc/storage/lvm.h \
+                                 ../lxc/storage/nbd.c ../lxc/storage/nbd.h \
+                                 ../lxc/storage/overlay.c ../lxc/storage/overlay.h \
+                                 ../lxc/storage/rbd.c ../lxc/storage/rbd.h \
+                                 ../lxc/storage/rsync.c ../lxc/storage/rsync.h \
+                                 ../lxc/storage/storage.c ../lxc/storage/storage.h \
+                                 ../lxc/storage/storage_utils.c ../lxc/storage/storage_utils.h \
+                                 ../lxc/storage/zfs.c ../lxc/storage/zfs.h \
+                                 ../lxc/sync.c ../lxc/sync.h \
+                                 ../lxc/string_utils.c ../lxc/string_utils.h \
+                                 ../lxc/terminal.c ../lxc/terminal.h \
+                                 ../lxc/utils.c ../lxc/utils.h \
+                                 ../lxc/uuid.c ../lxc/uuid.h \
+                                 $(LSM_SOURCES)
+if ENABLE_SECCOMP
+lxc_test_rootfs_options_SOURCES += ../lxc/seccomp.c ../lxc/lxcseccomp.h
+endif
+
+if !HAVE_STRCHRNUL
+lxc_test_rootfs_options_SOURCES += ../include/strchrnul.c ../include/strchrnul.h
+endif
+
 AM_CFLAGS += -DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \
             -DLXCPATH=\"$(LXCPATH)\" \
             -DLXC_GLOBAL_CONF=\"$(LXC_GLOBAL_CONF)\" \
@@ -818,6 +872,7 @@ bin_PROGRAMS = lxc-test-api-reboot \
               lxc-test-parse-config-file \
               lxc-test-raw-clone \
               lxc-test-reboot \
+              lxc-test-rootfs-options \
               lxc-test-saveconfig \
               lxc-test-share-ns \
               lxc-test-shortlived \
@@ -925,6 +980,7 @@ EXTRA_DIST = arch_parse.c \
             may_control.c \
             mount_injection.c \
             parse_config_file.c \
+            rootfs_options.c \
             saveconfig.c \
             shortlived.c \
             shutdowntest.c \
diff --git a/src/tests/rootfs_options.c b/src/tests/rootfs_options.c
new file mode 100644 (file)
index 0000000..c103cbc
--- /dev/null
@@ -0,0 +1,161 @@
+/* liblxcapi
+ *
+ * Copyright © 2021 Christian Brauner <christian.brauner@ubuntu.com>.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "config.h"
+
+#define __STDC_FORMAT_MACROS
+
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <lxc/lxccontainer.h>
+#include <lxc/attach_options.h>
+
+#ifdef HAVE_STATVFS
+#include <sys/statvfs.h>
+#endif
+
+#include "lxctest.h"
+#include "utils.h"
+
+static int has_mount_properties(const char *path, unsigned int flags)
+{
+#ifdef HAVE_STATVFS
+       int ret;
+       struct statvfs sb;
+
+       ret = statvfs(path, &sb);
+       if (ret < 0)
+               return -errno;
+
+       if ((sb.f_flag & flags) == flags)
+               return 0;
+
+       return -EINVAL;
+
+#else
+       return -EOPNOTSUPP;
+#endif
+}
+
+static int rootfs_options(void *payload)
+{
+       int ret;
+
+       ret = has_mount_properties("/",
+                                  MS_NODEV |
+                                  MS_NOSUID |
+                                  MS_RDONLY);
+       if (ret != 0) {
+               if (ret == -EOPNOTSUPP)
+                       return EXIT_SUCCESS;
+
+               return EXIT_FAILURE;
+       }
+
+       return EXIT_SUCCESS;
+}
+
+int main(int argc, char *argv[])
+{
+       int fret = EXIT_FAILURE;
+       lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
+       int ret;
+       pid_t pid;
+       struct lxc_container *c;
+
+       c = lxc_container_new("rootfs-options", NULL);
+       if (!c) {
+               lxc_error("%s", "Failed to create container \"rootfs-options\"");
+               exit(fret);
+       }
+
+       if (c->is_defined(c)) {
+               lxc_error("%s\n", "Container \"rootfs-options\" is defined");
+               goto on_error_put;
+       }
+
+       if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
+               lxc_error("%s\n", "Failed to create busybox container \"rootfs-options\"");
+               goto on_error_put;
+       }
+
+       if (!c->is_defined(c)) {
+               lxc_error("%s\n", "Container \"rootfs-options\" is not defined");
+               goto on_error_put;
+       }
+
+       c->clear_config(c);
+
+       if (!c->set_config_item(c, "lxc.rootfs.options", "nodev,nosuid,ro")) {
+               lxc_error("%s\n", "Failed to set config item \"lxc.mount.auto=sys:mixed\"");
+               goto on_error_put;
+       }
+
+       if (!c->load_config(c, NULL)) {
+               lxc_error("%s\n", "Failed to load config for container \"rootfs-options\"");
+               goto on_error_stop;
+       }
+
+       if (!c->want_daemonize(c, true)) {
+               lxc_error("%s\n", "Failed to mark container \"rootfs-options\" daemonized");
+               goto on_error_stop;
+       }
+
+       if (!c->startl(c, 0, NULL)) {
+               lxc_error("%s\n", "Failed to start container \"rootfs-options\" daemonized");
+               goto on_error_stop;
+       }
+
+       /* Leave some time for the container to write something to the log. */
+       sleep(2);
+
+       ret = c->attach(c, rootfs_options, NULL, &attach_options, &pid);
+       if (ret < 0) {
+               lxc_error("%s\n", "Failed to run function in container \"rootfs-options\"");
+               goto on_error_stop;
+       }
+
+       ret = wait_for_pid(pid);
+       if (ret < 0) {
+               lxc_error("%s\n", "Function \"rootfs-options\" failed");
+               goto on_error_stop;
+       }
+
+       fret = 0;
+
+on_error_stop:
+       if (c->is_running(c) && !c->stop(c))
+               lxc_error("%s\n", "Failed to stop container \"rootfs-options\"");
+
+       if (!c->destroy(c))
+               lxc_error("%s\n", "Failed to destroy container \"rootfs-options\"");
+
+on_error_put:
+       lxc_container_put(c);
+       exit(fret);
+}