block: use dynamically allocated target string
authorDaniel Golle <daniel@makrotopia.org>
Wed, 12 May 2021 22:35:46 +0000 (23:35 +0100)
committerDaniel Golle <daniel@makrotopia.org>
Sat, 15 May 2021 23:28:11 +0000 (00:28 +0100)
Dynamically allocate string buffer for target mountpoint if needed in
order to avoid the static buffer overflowing for long (device mapper)
paths.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
block.c

diff --git a/block.c b/block.c
index c6d93d1eb37d125c6efa1aa7e3050641b557ece2..a613fd7648c3112578528f2d7fb5833d08e1e7c1 100644 (file)
--- a/block.c
+++ b/block.c
@@ -994,7 +994,7 @@ static int mount_device(struct probe_info *pr, int type)
 {
        struct mount *m;
        struct stat st;
-       char _target[32];
+       char *_target = NULL;
        char *target;
        char *device;
        char *mp;
@@ -1053,16 +1053,22 @@ static int mount_device(struct probe_info *pr, int type)
                }
 
                if (m->autofs) {
-                       snprintf(_target, sizeof(_target), "/tmp/run/blockd/%s", device);
+                       if (asprintf(&_target, "/tmp/run/blockd/%s", device) == -1)
+                               exit(ENOMEM);
+
                        target = _target;
                } else if (m->target) {
                        target = m->target;
                } else {
-                       snprintf(_target, sizeof(_target), "/mnt/%s", device);
+                       if (asprintf(&_target, "/mnt/%s", device) == -1)
+                               exit(ENOMEM);
+
                        target = _target;
                }
        } else if (anon_mount) {
-               snprintf(_target, sizeof(_target), "/mnt/%s", device);
+               if (asprintf(&_target, "/mnt/%s", device) == -1)
+                       exit(ENOMEM);
+
                target = _target;
        } else {
                /* No reason to mount this device */
@@ -1082,9 +1088,16 @@ static int mount_device(struct probe_info *pr, int type)
        if (err) {
                ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
                                pr->dev, pr->type, target, errno);
+
+               if (_target)
+                       free(_target);
+
                return err;
        }
 
+       if (_target)
+               free(_target);
+
        handle_swapfiles(true);
 
        if (type != TYPE_AUTOFS)