From 96025f7cddd4e04ce9409d14d109d1d71a497590 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michal=20Pr=C3=ADvozn=C3=ADk?= <mprivozn@redhat.com>
Date: Thu, 14 Jan 2021 09:26:01 +0800
Subject: [PATCH] util: Move virIsDevMapperDevice() to virdevmapper.c

CVE-2020-14339

When introducing virdevmapper.c (in v4.3.0-rc1~427) I didn't
realize there is a function that calls in devmapper. The function
is called virIsDevMapperDevice() and lives in virutil.c. Now that
we have a special file for handling devmapper move it there.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>

cherry-pick from commit dfa0e118f745fe3f4fe95975c6100f0fc6d788be
Signed-off-by: Jiajie Li <lijiajie11@huawei.com>
---
 src/libvirt_private.syms           |  2 +-
 src/storage/parthelper.c           |  2 +-
 src/storage/storage_backend_disk.c |  1 +
 src/util/virdevmapper.c            | 24 ++++++++++++++++++++++++
 src/util/virdevmapper.h            |  3 +++
 src/util/virutil.c                 | 24 ------------------------
 src/util/virutil.h                 |  2 --
 7 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index e276f55bb1..bac96e140e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1931,6 +1931,7 @@ virDBusSetSharedBus;
 
 # util/virdevmapper.h
 virDevMapperGetTargets;
+virIsDevMapperDevice;
 
 
 # util/virdnsmasq.h
@@ -3415,7 +3416,6 @@ virGetUserShell;
 virHostGetDRMRenderNode;
 virHostHasIOMMU;
 virIndexToDiskName;
-virIsDevMapperDevice;
 virMemoryLimitIsSet;
 virMemoryLimitTruncate;
 virMemoryMaxValue;
diff --git a/src/storage/parthelper.c b/src/storage/parthelper.c
index 761a7f93fc..812e90d3cb 100644
--- a/src/storage/parthelper.c
+++ b/src/storage/parthelper.c
@@ -36,10 +36,10 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "virutil.h"
 #include "virfile.h"
 #include "virstring.h"
 #include "virgettext.h"
+#include "virdevmapper.h"
 
 /* we don't need to include the full internal.h just for this */
 #define STREQ(a, b) (strcmp(a, b) == 0)
diff --git a/src/storage/storage_backend_disk.c b/src/storage/storage_backend_disk.c
index 35b07abbfe..eae23ec24a 100644
--- a/src/storage/storage_backend_disk.c
+++ b/src/storage/storage_backend_disk.c
@@ -32,6 +32,7 @@
 #include "virutil.h"
 #include "configmake.h"
 #include "virstring.h"
+#include "virdevmapper.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
diff --git a/src/util/virdevmapper.c b/src/util/virdevmapper.c
index 79dbc3d02a..600e1f6322 100644
--- a/src/util/virdevmapper.c
+++ b/src/util/virdevmapper.c
@@ -212,3 +212,27 @@ virDevMapperGetTargets(const char *path G_GNUC_UNUSED,
     return -1;
 }
 #endif /* ! WITH_DEVMAPPER */
+
+
+#if WITH_DEVMAPPER
+bool
+virIsDevMapperDevice(const char *dev_name)
+{
+    struct stat buf;
+
+    if (!stat(dev_name, &buf) &&
+        S_ISBLK(buf.st_mode) &&
+        dm_is_dm_major(major(buf.st_rdev)))
+            return true;
+
+    return false;
+}
+
+#else /* ! WITH_DEVMAPPER */
+
+bool
+virIsDevMapperDevice(const char *dev_name G_GNUC_UNUSED)
+{
+    return false;
+}
+#endif /* ! WITH_DEVMAPPER */
diff --git a/src/util/virdevmapper.h b/src/util/virdevmapper.h
index 87bbc63cfd..834900692e 100644
--- a/src/util/virdevmapper.h
+++ b/src/util/virdevmapper.h
@@ -25,3 +25,6 @@
 int
 virDevMapperGetTargets(const char *path,
                        char ***devPaths) G_GNUC_NO_INLINE;
+
+bool
+virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1);
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 5b52e4e6d4..bea9749cae 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -37,10 +37,6 @@
 
 #include <sys/types.h>
 
-#if WITH_DEVMAPPER
-# include <libdevmapper.h>
-#endif
-
 #ifdef HAVE_GETPWUID_R
 # include <pwd.h>
 # include <grp.h>
@@ -1340,26 +1336,6 @@ void virWaitForDevices(void)
     ignore_value(virCommandRun(cmd, &exitstatus));
 }
 
-#if WITH_DEVMAPPER
-bool
-virIsDevMapperDevice(const char *dev_name)
-{
-    struct stat buf;
-
-    if (!stat(dev_name, &buf) &&
-        S_ISBLK(buf.st_mode) &&
-        dm_is_dm_major(major(buf.st_rdev)))
-            return true;
-
-    return false;
-}
-#else
-bool virIsDevMapperDevice(const char *dev_name G_GNUC_UNUSED)
-{
-    return false;
-}
-#endif
-
 bool
 virValidateWWN(const char *wwn)
 {
diff --git a/src/util/virutil.h b/src/util/virutil.h
index ee23f0c1f4..e4328564e4 100644
--- a/src/util/virutil.h
+++ b/src/util/virutil.h
@@ -114,8 +114,6 @@ bool virDoesUserExist(const char *name);
 bool virDoesGroupExist(const char *name);
 
 
-bool virIsDevMapperDevice(const char *dev_name) ATTRIBUTE_NONNULL(1);
-
 bool virValidateWWN(const char *wwn);
 
 int virGetDeviceID(const char *path,
-- 
2.27.0

