]> git.proxmox.com Git - mirror_lxc.git/blobdiff - src/lxc/parse.c
github: Update for main branch
[mirror_lxc.git] / src / lxc / parse.c
index e6b0460f57a0b0ff120b9c96729fe5fcad6a511d..c174de96c272eee3ab766cc635ef547ff07d6107 100644 (file)
@@ -1,17 +1,16 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE 1
-#endif
+#include "config.h"
+
 #include <dirent.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/mman.h>
 #include <sys/sendfile.h>
 
-#include "config.h"
 #include "file_utils.h"
 #include "log.h"
 #include "macro.h"
@@ -50,11 +49,12 @@ int lxc_strmunmap(void *addr, size_t length)
 
 int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *data)
 {
-       int saved_errno;
-       ssize_t ret = -1, bytes_sent;
-       char *line;
-       int fd = -1, memfd = -1;
+       __do_close int fd = -EBADF, memfd = -EBADF;
+       ssize_t ret = -1;
        char *buf = NULL;
+       struct stat st = {};
+       ssize_t bytes;
+       char *line;
 
        memfd = memfd_create(".lxc_config_file", MFD_CLOEXEC);
        if (memfd < 0) {
@@ -65,8 +65,7 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *da
                        goto on_error;
                }
 
-               TRACE("Failed to create in-memory file. Falling back to "
-                     "temporary file");
+               TRACE("Failed to create in-memory file. Falling back to temporary file");
                memfd = lxc_make_tmpfile(template, true);
                if (memfd < 0) {
                        SYSERROR("Failed to create temporary file \"%s\"", template);
@@ -80,10 +79,21 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *da
                goto on_error;
        }
 
-       /* sendfile() handles up to 2GB. No config file should be that big. */
-       bytes_sent = lxc_sendfile_nointr(memfd, fd, NULL, LXC_SENDFILE_MAX);
-       if (bytes_sent < 0) {
-               SYSERROR("Failed to sendfile \"%s\"", file);
+       ret = fstat(fd, &st);
+       if (ret) {
+               SYSERROR("Failed to stat file \"%s\"", file);
+               goto on_error;
+       }
+
+       if (st.st_size > INT_MAX) {
+               SYSERROR("Excessively large config file \"%s\"", file);
+               goto on_error;
+       }
+
+
+       bytes = __fd_to_fd(fd, memfd);
+       if (bytes < 0) {
+               SYSERROR("Failed to copy config file \"%s\"", file);
                goto on_error;
        }
 
@@ -92,7 +102,7 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *da
                SYSERROR("Failed to append zero byte");
                goto on_error;
        }
-       bytes_sent++;
+       bytes++;
 
        ret = lseek(memfd, 0, SEEK_SET);
        if (ret < 0) {
@@ -101,8 +111,7 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *da
        }
 
        ret = -1;
-       buf = mmap(NULL, bytes_sent, PROT_READ | PROT_WRITE,
-                  MAP_SHARED | MAP_POPULATE, memfd, 0);
+       buf = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_POPULATE, memfd, 0);
        if (buf == MAP_FAILED) {
                buf = NULL;
                SYSERROR("Failed to mmap");
@@ -117,36 +126,30 @@ int lxc_file_for_each_line_mmap(const char *file, lxc_file_cb callback, void *da
                         * error.
                         */
                        if (ret < 0)
-                               ERROR("Failed to parse config file \"%s\" at "
-                                     "line \"%s\"", file, line);
+                               ERROR("Failed to parse config file \"%s\" at line \"%s\"",
+                                     file, line);
                        break;
                }
        }
 
 on_error:
-       saved_errno = errno;
-       if (fd >= 0)
-               close(fd);
-       if (memfd >= 0)
-               close(memfd);
-       if (buf && munmap(buf, bytes_sent)) {
+       if (buf && munmap(buf, bytes)) {
                SYSERROR("Failed to unmap");
                if (ret == 0)
                        ret = -1;
        }
-       errno = saved_errno;
 
        return ret;
 }
 
 int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
 {
-       FILE *f;
+       __do_fclose FILE *f = NULL;
+       __do_free char *line = NULL;
        int err = 0;
-       char *line = NULL;
        size_t len = 0;
 
-       f = fopen(file, "r");
+       f = fopen(file, "re");
        if (!f) {
                SYSERROR("Failed to open \"%s\"", file);
                return -1;
@@ -164,7 +167,5 @@ int lxc_file_for_each_line(const char *file, lxc_file_cb callback, void *data)
                }
        }
 
-       free(line);
-       fclose(f);
        return err;
 }