]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf, lxccontainer: fix length checks in snprintf
authorLiza Tretyakova <elizabet.tretyakova@gmail.com>
Sat, 19 May 2018 13:16:26 +0000 (16:16 +0300)
committerChristian Brauner <christian.brauner@ubuntu.com>
Sun, 22 Jul 2018 13:35:21 +0000 (15:35 +0200)
Signed-off-by: Liza Tretyakova <elizabet.tretyakova@gmail.com>
src/lxc/conf.c
src/lxc/lxccontainer.c

index 208562eeec6aa777a2b74cc16f705f59abd90244..e91f84438507b970fedef4336019c55fd969ed85 100644 (file)
@@ -650,19 +650,13 @@ unsigned long add_required_remount_flags(const char *s, const char *d,
 
 static int add_shmount_to_list(struct lxc_conf *conf) {
        char new_mount[MAXPATHLEN];
-       size_t len_mount;
        /* Offset for the leading '/' since the path_cont
         * is absolute inside the container */
        int ret = -1, offset = 1;
 
-       /* +1 for the separating whitespace */
-       len_mount = strlen(conf->shmount.path_host) + 1
-                       + strlen(conf->shmount.path_cont) - offset
-                       + sizeof(" none bind,create=dir 0 0") - 1;
-
-       ret = snprintf(new_mount, len_mount + 1, "%s %s none bind,create=dir 0 0",
+       ret = snprintf(new_mount, sizeof(new_mount), "%s %s none bind,create=dir 0 0",
                                   conf->shmount.path_host, conf->shmount.path_cont + offset);
-       if (ret < 0 || (size_t)ret >= len_mount + 1)
+       if (ret < 0 || (size_t)ret >= sizeof(new_mount))
                return -1;
 
        ret = add_elem_to_mount_list(new_mount, conf);
index 7a16e1b3ee219504d31e48cb9e8462b18e771673..f1829cf8d4cfca532783e7eaa00cdd470ce809fc 100644 (file)
@@ -4955,7 +4955,6 @@ static int do_lxcapi_mount(struct lxc_container *c, const char *source,
                           struct lxc_mount *mnt)
 {
        char *suff, *sret;
-       size_t len;
        char template[MAXPATHLEN], path[MAXPATHLEN];
        pid_t pid, init_pid;
        struct stat sb;
@@ -4970,10 +4969,9 @@ static int do_lxcapi_mount(struct lxc_container *c, const char *source,
                ERROR("Host path to shared mountpoint must be specified in the config\n");
                return -EINVAL;
        }
-       len = strlen(c->lxc_conf->shmount.path_host) + sizeof("/.lxcmount_XXXXXX") - 1;
 
-       ret = snprintf(template, len + 1, "%s/.lxcmount_XXXXXX", c->lxc_conf->shmount.path_host);
-       if (ret < 0 || (size_t)ret >= len + 1) {
+       ret = snprintf(template, sizeof(template), "%s/.lxcmount_XXXXXX", c->lxc_conf->shmount.path_host);
+       if (ret < 0 || (size_t)ret >= sizeof(template)) {
                SYSERROR("Error writing shmounts tempdir name");
                goto out;
        }
@@ -5048,9 +5046,8 @@ static int do_lxcapi_mount(struct lxc_container *c, const char *source,
                if (!suff)
                        _exit(EXIT_FAILURE);
 
-               len = strlen(c->lxc_conf->shmount.path_cont) + sizeof("/.lxcmount_XXXXXX") - 1;
-               ret = snprintf(path, len + 1, "%s%s", c->lxc_conf->shmount.path_cont, suff);
-               if (ret < 0 || (size_t)ret >= len + 1) {
+               ret = snprintf(path, sizeof(path), "%s%s", c->lxc_conf->shmount.path_cont, suff);
+               if (ret < 0 || (size_t)ret >= sizeof(path)) {
                        SYSERROR("Error writing container mountpoint name");
                        _exit(EXIT_FAILURE);
                }