]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/rexec.c
github: Update for main branch
[mirror_lxc.git] / src / lxc / rexec.c
index 0589b4a781e1e56e16e4febd6c81f93f035db1fe..6a017da9c62cb51575b7ac84e4008c8766e33b36 100644 (file)
@@ -1,42 +1,23 @@
-/* liblxcapi
- *
- * Copyright © 2019 Christian Brauner <christian.brauner@ubuntu.com>.
- * Copyright © 2019 Canonical Ltd.
- *
- * This library 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.
-
- * This library 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 this library; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
- */
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include "config.h"
 
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE 1
-#endif
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-#include "config.h"
 #include "file_utils.h"
 #include "macro.h"
 #include "memory_utils.h"
-#include "raw_syscalls.h"
+#include "process_utils.h"
+#include "rexec.h"
 #include "string_utils.h"
 #include "syscall_wrappers.h"
 
-#if IS_BIONIC
-#include "../include/fexecve.h"
+#if IS_BIONIC && !HAVE_FEXECVE
+#include "fexecve.h"
 #endif
 
 #define LXC_MEMFD_REXEC_SEALS \
@@ -77,49 +58,109 @@ static int parse_argv(char ***argv)
        if (ret <= 0)
                return -1;
 
-       steal_ptr(cmdline);
+       move_ptr(cmdline);
        return 0;
 }
 
 static int is_memfd(void)
 {
-       __do_close_prot_errno int fd = -EBADF;
-       int saved_errno, seals;
+       __do_close int fd = -EBADF;
+       int seals;
 
        fd = open("/proc/self/exe", O_RDONLY | O_CLOEXEC);
        if (fd < 0)
                return -ENOTRECOVERABLE;
 
        seals = fcntl(fd, F_GET_SEALS);
-       if (seals < 0)
+       if (seals < 0) {
+               struct stat s = {0};
+
+               if (fstat(fd, &s) == 0)
+                       return (s.st_nlink == 0);
+
                return -EINVAL;
+       }
 
        return seals == LXC_MEMFD_REXEC_SEALS;
 }
 
 static void lxc_rexec_as_memfd(char **argv, char **envp, const char *memfd_name)
 {
-       __do_close_prot_errno int fd = -EBADF, memfd = -EBADF;
-       int saved_errno;
-       ssize_t bytes_sent;
+       __do_close int execfd = -EBADF, fd = -EBADF, memfd = -EBADF,
+                      tmpfd = -EBADF;
+       int ret;
+       ssize_t bytes_sent = 0;
+       struct stat st = {0};
 
        memfd = memfd_create(memfd_name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
-       if (memfd < 0)
-               return;
+       if (memfd < 0) {
+               char template[PATH_MAX];
+
+               ret = strnprintf(template, sizeof(template),
+                                P_tmpdir "/.%s_XXXXXX", memfd_name);
+               if (ret < 0)
+                       return;
+
+               tmpfd = lxc_make_tmpfile(template, true);
+               if (tmpfd < 0)
+                       return;
+
+               ret = fchmod(tmpfd, 0700);
+               if (ret)
+                       return;
+       }
 
        fd = open("/proc/self/exe", O_RDONLY | O_CLOEXEC);
        if (fd < 0)
                return;
 
        /* sendfile() handles up to 2GB. */
-       bytes_sent = lxc_sendfile_nointr(memfd, fd, NULL, LXC_SENDFILE_MAX);
-       if (bytes_sent < 0)
+       ret = fstat(fd, &st);
+       if (ret)
                return;
 
-       if (fcntl(memfd, F_ADD_SEALS, LXC_MEMFD_REXEC_SEALS))
+       while (bytes_sent < st.st_size) {
+               ssize_t sent;
+
+               sent = lxc_sendfile_nointr(memfd >= 0 ? memfd : tmpfd, fd, NULL,
+                                          st.st_size - bytes_sent);
+               if (sent < 0) {
+                       /*
+                        * Fallback to shoveling data between kernel- and
+                        * userspace.
+                        */
+                       if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
+                               fprintf(stderr, "Failed to seek to beginning of file");
+
+                       if (fd_to_fd(fd, memfd >= 0 ? memfd : tmpfd))
+                               break;
+
+                       return;
+               }
+               bytes_sent += sent;
+       }
+       close_prot_errno_disarm(fd);
+
+       if (memfd >= 0) {
+               if (fcntl(memfd, F_ADD_SEALS, LXC_MEMFD_REXEC_SEALS))
+                       return;
+
+               execfd = move_fd(memfd);
+       } else {
+               char procfd[LXC_PROC_PID_FD_LEN];
+
+               ret = strnprintf(procfd, sizeof(procfd), "/proc/self/fd/%d", tmpfd);
+               if (ret < 0)
+                       return;
+
+               execfd = open(procfd, O_PATH | O_CLOEXEC);
+               close_prot_errno_disarm(tmpfd);
+
+       }
+       if (execfd < 0)
                return;
 
-       fexecve(memfd, argv, envp);
+       fexecve(execfd, argv, envp);
 }
 
 /*
@@ -130,13 +171,12 @@ extern char **environ;
 
 int lxc_rexec(const char *memfd_name)
 {
+       __do_free_string_list char **argv = NULL;
        int ret;
-       char **argv = NULL;
 
        ret = is_memfd();
        if (ret < 0 && ret == -ENOTRECOVERABLE) {
-               fprintf(stderr,
-                       "%s - Failed to determine whether this is a memfd\n",
+               fprintf(stderr, "%s - Failed to determine whether this is a memfd\n",
                        strerror(errno));
                return -1;
        } else if (ret > 0) {
@@ -145,8 +185,7 @@ int lxc_rexec(const char *memfd_name)
 
        ret = parse_argv(&argv);
        if (ret < 0) {
-               fprintf(stderr,
-                       "%s - Failed to parse command line parameters\n",
+               fprintf(stderr, "%s - Failed to parse command line parameters\n",
                        strerror(errno));
                return -1;
        }