[libvirt] [PATCH 57/75] security: Use g_strdup_printf() instead of virAsprintf()

Michal Privoznik mprivozn at redhat.com
Tue Oct 22 13:58:01 UTC 2019


Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/security/security_apparmor.c | 22 +++++++++---------
 src/security/security_dac.c      | 20 ++++++++--------
 src/security/security_selinux.c  | 40 ++++++++++++++++----------------
 src/security/security_util.c     | 12 +++++-----
 src/security/virt-aa-helper.c    | 26 ++++++++++-----------
 5 files changed, 60 insertions(+), 60 deletions(-)

diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index 7263c2face..5fdc5fd7f5 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -76,11 +76,11 @@ profile_status(const char *str, const int check_enforcing)
     int rc = -2;
 
     /* create string that is '<str> \0' for accurate matching */
-    virAsprintf(&tmp, "%s ", str);
+    tmp = g_strdup_printf("%s ", str);
 
     if (check_enforcing != 0) {
         /* create string that is '<str> (enforce)\0' for accurate matching */
-        virAsprintf(&etmp, "%s (enforce)", str);
+        etmp = g_strdup_printf("%s (enforce)", str);
     }
 
     if (virFileReadAll(APPARMOR_PROFILES_PATH, MAX_FILE_LEN, &content) < 0) {
@@ -126,7 +126,7 @@ profile_status_file(const char *str)
     int rc = -1;
     int len;
 
-    virAsprintf(&profile, "%s/%s", APPARMOR_DIR "/libvirt", str);
+    profile = g_strdup_printf("%s/%s", APPARMOR_DIR "/libvirt", str);
 
     if (!virFileExists(profile))
         goto failed;
@@ -138,7 +138,7 @@ profile_status_file(const char *str)
     }
 
     /* create string that is ' <str> flags=(complain)\0' */
-    virAsprintf(&tmp, " %s flags=(complain)", str);
+    tmp = g_strdup_printf(" %s flags=(complain)", str);
 
     if (strstr(content, tmp) != NULL)
         rc = 0;
@@ -221,7 +221,7 @@ get_profile_name(virDomainDefPtr def)
     char *name = NULL;
 
     virUUIDFormat(def->uuid, uuidstr);
-    virAsprintf(&name, "%s%s", AA_PREFIX, uuidstr);
+    name = g_strdup_printf("%s%s", AA_PREFIX, uuidstr);
 
     return name;
 }
@@ -353,8 +353,8 @@ AppArmorSecurityManagerProbe(const char *virtDriver G_GNUC_UNUSED)
         return rc;
 
     /* see if template file exists */
-    virAsprintf(&template_qemu, "%s/TEMPLATE.qemu", APPARMOR_DIR "/libvirt");
-    virAsprintf(&template_lxc, "%s/TEMPLATE.lxc", APPARMOR_DIR "/libvirt");
+    template_qemu = g_strdup_printf("%s/TEMPLATE.qemu", APPARMOR_DIR "/libvirt");
+    template_lxc = g_strdup_printf("%s/TEMPLATE.lxc", APPARMOR_DIR "/libvirt");
 
     if (!virFileExists(template_qemu)) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -1026,8 +1026,8 @@ AppArmorSetChardevLabel(virSecurityManagerPtr mgr,
         break;
 
     case VIR_DOMAIN_CHR_TYPE_PIPE:
-        virAsprintf(&in, "%s.in", dev_source->data.file.path);
-        virAsprintf(&out, "%s.out", dev_source->data.file.path);
+        in = g_strdup_printf("%s.in", dev_source->data.file.path);
+        out = g_strdup_printf("%s.out", dev_source->data.file.path);
         if (virFileExists(in)) {
             if (reload_profile(mgr, def, in, true) < 0)
                 goto done;
@@ -1091,7 +1091,7 @@ AppArmorSetPathLabel(virSecurityManagerPtr mgr,
     char *full_path = NULL;
 
     if (allowSubtree) {
-        virAsprintf(&full_path, "%s/{,**}", path);
+        full_path = g_strdup_printf("%s/{,**}", path);
         rc = reload_profile(mgr, def, full_path, true);
         VIR_FREE(full_path);
     } else {
@@ -1123,7 +1123,7 @@ AppArmorSetFDLabel(virSecurityManagerPtr mgr,
     if (!secdef || !secdef->imagelabel)
         return 0;
 
-    virAsprintf(&proc, "/proc/self/fd/%d", fd);
+    proc = g_strdup_printf("/proc/self/fd/%d", fd);
 
     if (virFileResolveLink(proc, &fd_path) < 0) {
         /* it's a deleted file, presumably.  Ignore? */
diff --git a/src/security/security_dac.c b/src/security/security_dac.c
index dfa402d544..56cca6cf9c 100644
--- a/src/security/security_dac.c
+++ b/src/security/security_dac.c
@@ -302,8 +302,8 @@ virSecurityDACSetUserAndGroup(virSecurityManagerPtr mgr,
     priv->user = user;
     priv->group = group;
 
-    virAsprintf(&priv->baselabel, "+%u:+%u", (unsigned int)user,
-                (unsigned int)group);
+    priv->baselabel = g_strdup_printf("+%u:+%u", (unsigned int)user,
+                                      (unsigned int)group);
 
     return 0;
 }
@@ -437,7 +437,7 @@ virSecurityDACRememberLabel(virSecurityDACDataPtr priv G_GNUC_UNUSED,
     char *label = NULL;
     int ret = -1;
 
-    virAsprintf(&label, "+%u:+%u", (unsigned int)uid, (unsigned int)gid);
+    label = g_strdup_printf("+%u:+%u", (unsigned int)uid, (unsigned int)gid);
 
     ret = virSecuritySetRememberedLabel(SECURITY_DAC_NAME, path, label);
     VIR_FREE(label);
@@ -1507,8 +1507,8 @@ virSecurityDACSetChardevLabelHelper(virSecurityManagerPtr mgr,
         break;
 
     case VIR_DOMAIN_CHR_TYPE_PIPE:
-        virAsprintf(&in, "%s.in", dev_source->data.file.path);
-        virAsprintf(&out, "%s.out", dev_source->data.file.path);
+        in = g_strdup_printf("%s.in", dev_source->data.file.path);
+        out = g_strdup_printf("%s.out", dev_source->data.file.path);
         if (virFileExists(in) && virFileExists(out)) {
             if (virSecurityDACSetOwnership(mgr, NULL, in, user, group, remember) < 0 ||
                 virSecurityDACSetOwnership(mgr, NULL, out, user, group, remember) < 0)
@@ -1599,8 +1599,8 @@ virSecurityDACRestoreChardevLabelHelper(virSecurityManagerPtr mgr,
         break;
 
     case VIR_DOMAIN_CHR_TYPE_PIPE:
-        virAsprintf(&out, "%s.out", dev_source->data.file.path);
-        virAsprintf(&in, "%s.in", dev_source->data.file.path);
+        out = g_strdup_printf("%s.out", dev_source->data.file.path);
+        in = g_strdup_printf("%s.in", dev_source->data.file.path);
         if (virFileExists(in) && virFileExists(out)) {
             if (virSecurityDACRestoreFileLabelInternal(mgr, NULL, out, recall) < 0 ||
                 virSecurityDACRestoreFileLabelInternal(mgr, NULL, in, recall) < 0)
@@ -2281,8 +2281,8 @@ virSecurityDACGenLabel(virSecurityManagerPtr mgr,
         }
         break;
     case VIR_DOMAIN_SECLABEL_DYNAMIC:
-        virAsprintf(&seclabel->label, "+%u:+%u", (unsigned int)priv->user,
-                    (unsigned int)priv->group);
+        seclabel->label = g_strdup_printf("+%u:+%u", (unsigned int)priv->user,
+                                          (unsigned int)priv->group);
         if (seclabel->label == NULL) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("cannot generate dac user and group id "
@@ -2333,7 +2333,7 @@ virSecurityDACGetProcessLabelInternal(pid_t pid,
 
     VIR_DEBUG("Getting DAC user and group on process '%d'", pid);
 
-    virAsprintf(&path, "/proc/%d", (int)pid);
+    path = g_strdup_printf("/proc/%d", (int)pid);
 
     if (lstat(path, &sb) < 0) {
         virReportSystemError(errno,
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index 47fc95351f..5a2cd6bbeb 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -381,14 +381,14 @@ virSecuritySELinuxMCSFind(virSecurityManagerPtr mgr,
         VIR_DEBUG("Try cat %s:c%d,c%d", sens, c1 + catMin, c2 + catMin);
 
         if (c1 == c2) {
-            virAsprintf(&mcs, "%s:c%d", sens, catMin + c1);
+            mcs = g_strdup_printf("%s:c%d", sens, catMin + c1);
         } else {
             if (c1 > c2) {
                 int t = c1;
                 c1 = c2;
                 c2 = t;
             }
-            virAsprintf(&mcs, "%s:c%d,c%d", sens, catMin + c1, catMin + c2);
+            mcs = g_strdup_printf("%s:c%d,c%d", sens, catMin + c1, catMin + c2);
         }
 
         if (virHashLookup(data->mcs, mcs) == NULL)
@@ -2202,8 +2202,8 @@ virSecuritySELinuxSetHostdevCapsLabel(virSecurityManagerPtr mgr,
     switch (dev->source.caps.type) {
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: {
         if (vroot) {
-            virAsprintf(&path, "%s/%s", vroot,
-                        dev->source.caps.u.storage.block);
+            path = g_strdup_printf("%s/%s", vroot,
+                                   dev->source.caps.u.storage.block);
         } else {
             path = g_strdup(dev->source.caps.u.storage.block);
         }
@@ -2214,8 +2214,8 @@ virSecuritySELinuxSetHostdevCapsLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC: {
         if (vroot) {
-            virAsprintf(&path, "%s/%s", vroot,
-                        dev->source.caps.u.misc.chardev);
+            path = g_strdup_printf("%s/%s", vroot,
+                                   dev->source.caps.u.misc.chardev);
         } else {
             path = g_strdup(dev->source.caps.u.misc.chardev);
         }
@@ -2433,8 +2433,8 @@ virSecuritySELinuxRestoreHostdevCapsLabel(virSecurityManagerPtr mgr,
     switch (dev->source.caps.type) {
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_STORAGE: {
         if (vroot) {
-            virAsprintf(&path, "%s/%s", vroot,
-                        dev->source.caps.u.storage.block);
+            path = g_strdup_printf("%s/%s", vroot,
+                                   dev->source.caps.u.storage.block);
         } else {
             path = g_strdup(dev->source.caps.u.storage.block);
         }
@@ -2445,8 +2445,8 @@ virSecuritySELinuxRestoreHostdevCapsLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_CAPS_TYPE_MISC: {
         if (vroot) {
-            virAsprintf(&path, "%s/%s", vroot,
-                        dev->source.caps.u.misc.chardev);
+            path = g_strdup_printf("%s/%s", vroot,
+                                   dev->source.caps.u.misc.chardev);
         } else {
             path = g_strdup(dev->source.caps.u.misc.chardev);
         }
@@ -2544,8 +2544,8 @@ virSecuritySELinuxSetChardevLabel(virSecurityManagerPtr mgr,
         break;
 
     case VIR_DOMAIN_CHR_TYPE_PIPE:
-        virAsprintf(&in, "%s.in", dev_source->data.file.path);
-        virAsprintf(&out, "%s.out", dev_source->data.file.path);
+        in = g_strdup_printf("%s.in", dev_source->data.file.path);
+        out = g_strdup_printf("%s.out", dev_source->data.file.path);
         if (virFileExists(in) && virFileExists(out)) {
             if ((virSecuritySELinuxSetFilecon(mgr, in, imagelabel, true) < 0) ||
                 (virSecuritySELinuxSetFilecon(mgr, out, imagelabel, true) < 0)) {
@@ -2618,8 +2618,8 @@ virSecuritySELinuxRestoreChardevLabel(virSecurityManagerPtr mgr,
         break;
 
     case VIR_DOMAIN_CHR_TYPE_PIPE:
-        virAsprintf(&out, "%s.out", dev_source->data.file.path);
-        virAsprintf(&in, "%s.in", dev_source->data.file.path);
+        out = g_strdup_printf("%s.out", dev_source->data.file.path);
+        in = g_strdup_printf("%s.in", dev_source->data.file.path);
         if (virFileExists(in) && virFileExists(out)) {
             if ((virSecuritySELinuxRestoreFileLabel(mgr, out, true) < 0) ||
                 (virSecuritySELinuxRestoreFileLabel(mgr, in, true) < 0)) {
@@ -3257,7 +3257,7 @@ virSecuritySELinuxSetTapFDLabel(virSecurityManagerPtr mgr,
     }
 
     /* Label /dev/tap.* devices only. Leave /dev/net/tun alone! */
-    virAsprintf(&proc, "/proc/self/fd/%d", fd);
+    proc = g_strdup_printf("/proc/self/fd/%d", fd);
 
     if (virFileResolveLink(proc, &fd_path) < 0) {
         virReportSystemError(errno,
@@ -3341,9 +3341,9 @@ virSecuritySELinuxGetSecurityMountOptions(virSecurityManagerPtr mgr,
             secdef->imagelabel = virSecuritySELinuxGenImageLabel(mgr, def);
 
         if (secdef->imagelabel) {
-            virAsprintf(&opts,
-                        ",context=\"%s\"",
-                        (const char*) secdef->imagelabel);
+            opts = g_strdup_printf(
+                                   ",context=\"%s\"",
+                                   (const char*) secdef->imagelabel);
         }
     }
 
@@ -3402,7 +3402,7 @@ virSecuritySELinuxSetFileLabels(virSecurityManagerPtr mgr,
         return -1;
 
     while ((ret = virDirRead(dir, &ent, path)) > 0) {
-        virAsprintf(&filename, "%s/%s", path, ent->d_name);
+        filename = g_strdup_printf("%s/%s", path, ent->d_name);
         ret = virSecuritySELinuxSetFilecon(mgr, filename,
                                            seclabel->imagelabel, true);
         VIR_FREE(filename);
@@ -3448,7 +3448,7 @@ virSecuritySELinuxRestoreFileLabels(virSecurityManagerPtr mgr,
         return -1;
 
     while ((ret = virDirRead(dir, &ent, path)) > 0) {
-        virAsprintf(&filename, "%s/%s", path, ent->d_name);
+        filename = g_strdup_printf("%s/%s", path, ent->d_name);
         ret = virSecuritySELinuxRestoreFileLabel(mgr, filename, true);
         VIR_FREE(filename);
         if (ret < 0)
diff --git a/src/security/security_util.c b/src/security/security_util.c
index 5fb0a3aae5..4f661fd75e 100644
--- a/src/security/security_util.c
+++ b/src/security/security_util.c
@@ -63,7 +63,7 @@ virSecurityGetAttrName(const char *name G_GNUC_UNUSED)
 {
     char *ret = NULL;
 #ifdef XATTR_NAMESPACE
-    virAsprintf(&ret, XATTR_NAMESPACE".libvirt.security.%s", name);
+    ret = g_strdup_printf(XATTR_NAMESPACE".libvirt.security.%s", name);
 #else
     errno = ENOSYS;
     virReportSystemError(errno, "%s",
@@ -78,7 +78,7 @@ virSecurityGetRefCountAttrName(const char *name G_GNUC_UNUSED)
 {
     char *ret = NULL;
 #ifdef XATTR_NAMESPACE
-    virAsprintf(&ret, XATTR_NAMESPACE".libvirt.security.ref_%s", name);
+    ret = g_strdup_printf(XATTR_NAMESPACE".libvirt.security.ref_%s", name);
 #else
     errno = ENOSYS;
     virReportSystemError(errno, "%s",
@@ -93,7 +93,7 @@ static char *
 virSecurityGetTimestampAttrName(const char *name)
 {
     char *ret = NULL;
-    virAsprintf(&ret, XATTR_NAMESPACE ".libvirt.security.timestamp_%s", name);
+    ret = g_strdup_printf(XATTR_NAMESPACE ".libvirt.security.timestamp_%s", name);
     return ret;
 }
 #else /* !XATTR_NAMESPACE */
@@ -120,7 +120,7 @@ virSecurityGetTimestamp(void)
         return NULL;
     }
 
-    virAsprintf(&ret, "%llu", boottime);
+    ret = g_strdup_printf("%llu", boottime);
     return ret;
 }
 
@@ -312,7 +312,7 @@ virSecurityGetRememberedLabel(const char *name,
     refcount--;
 
     if (refcount > 0) {
-        virAsprintf(&value, "%u", refcount);
+        value = g_strdup_printf("%u", refcount);
 
         if (virFileSetXAttr(path, ref_name, value) < 0)
             return -1;
@@ -419,7 +419,7 @@ virSecuritySetRememberedLabel(const char *name,
             return -1;
     }
 
-    virAsprintf(&value, "%u", refcount);
+    value = g_strdup_printf("%u", refcount);
 
     if (virFileSetXAttr(path, ref_name, value) < 0)
         return -1;
diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c
index c2679cdad2..634a532d35 100644
--- a/src/security/virt-aa-helper.c
+++ b/src/security/virt-aa-helper.c
@@ -157,7 +157,7 @@ parserCommand(const char *profile_name, const char cmd)
 
     snprintf(flag, 3, "-%c", cmd);
 
-    virAsprintf(&profile, "%s/%s", APPARMOR_DIR "/libvirt", profile_name);
+    profile = g_strdup_printf("%s/%s", APPARMOR_DIR "/libvirt", profile_name);
 
     if (!virFileExists(profile)) {
         vah_error(NULL, 0, _("profile does not exist"));
@@ -214,9 +214,9 @@ update_include_file(const char *include_file, const char *included_files,
     }
 
     if (append && virFileExists(include_file))
-        virAsprintf(&pcontent, "%s%s", existing, included_files);
+        pcontent = g_strdup_printf("%s%s", existing, included_files);
     else
-        virAsprintf(&pcontent, "%s%s", warning, included_files);
+        pcontent = g_strdup_printf("%s%s", warning, included_files);
 
     plen = strlen(pcontent);
     if (plen > MAX_FILE_LEN) {
@@ -290,7 +290,7 @@ create_profile(const char *profile, const char *profile_name,
         driver_name = virDomainVirtTypeToString(virtType);
     }
 
-    virAsprintf(&template, "%s/TEMPLATE.%s", APPARMOR_DIR "/libvirt", driver_name);
+    template = g_strdup_printf("%s/TEMPLATE.%s", APPARMOR_DIR "/libvirt", driver_name);
 
     if (!virFileExists(template)) {
         vah_error(NULL, 0, _("template does not exist"));
@@ -313,11 +313,11 @@ create_profile(const char *profile, const char *profile_name,
     }
 
     /* '\nprofile <profile_name>\0' */
-    virAsprintf(&replace_name, "\nprofile %s", profile_name);
+    replace_name = g_strdup_printf("\nprofile %s", profile_name);
 
     /* '\n<profile_files>\n}\0' */
     if (virtType != VIR_DOMAIN_VIRT_LXC)
-        virAsprintf(&replace_files, "\n%s\n}", profile_files);
+    replace_files = g_strdup_printf("\n%s\n}", profile_files);
 
     plen = tlen + strlen(replace_name) - strlen(template_name) + 1;
 
@@ -768,7 +768,7 @@ vah_add_path(virBufferPtr buf, const char *path, const char *perms, bool recursi
             vah_error(NULL, 0, _("could not find realpath"));
             goto cleanup;
         }
-        virAsprintf(&tmp, "%s%s", pathreal, pathtmp);
+        tmp = g_strdup_printf("%s%s", pathreal, pathtmp);
     }
 
     perms_new = g_strdup(perms);
@@ -835,13 +835,13 @@ vah_add_file_chardev(virBufferPtr buf,
 
     if (type == VIR_DOMAIN_CHR_TYPE_PIPE) {
         /* add the pipe input */
-        virAsprintf(&pipe_in, "%s.in", path);
+        pipe_in = g_strdup_printf("%s.in", path);
 
         if (vah_add_file(buf, pipe_in, perms) != 0)
             goto clean_pipe_in;
 
         /* add the pipe output */
-        virAsprintf(&pipe_out, "%s.out", path);
+        pipe_out = g_strdup_printf("%s.out", path);
 
         if (vah_add_file(buf, pipe_out, perms) != 0)
             goto clean_pipe_out;
@@ -934,7 +934,7 @@ get_files(vahControl * ctl)
 
     /* verify uuid is same as what we were given on the command line */
     virUUIDFormat(ctl->def->uuid, uuidstr);
-    virAsprintf(&uuid, "%s%s", AA_PREFIX, uuidstr);
+    uuid = g_strdup_printf("%s%s", AA_PREFIX, uuidstr);
 
     if (STRNEQ(uuid, ctl->uuid)) {
         vah_error(ctl, 0, _("given uuid does not match XML uuid"));
@@ -1431,8 +1431,8 @@ main(int argc, char **argv)
     if (vahParseArgv(ctl, argc, argv) != 0)
         vah_error(ctl, 1, _("could not parse arguments"));
 
-    virAsprintf(&profile, "%s/%s", APPARMOR_DIR "/libvirt", ctl->uuid);
-    virAsprintf(&include_file, "%s/%s.files", APPARMOR_DIR "/libvirt", ctl->uuid);
+    profile = g_strdup_printf("%s/%s", APPARMOR_DIR "/libvirt", ctl->uuid);
+    include_file = g_strdup_printf("%s/%s.files", APPARMOR_DIR "/libvirt", ctl->uuid);
 
     if (ctl->cmd == 'a') {
         rc = parserLoad(ctl->uuid);
@@ -1494,7 +1494,7 @@ main(int argc, char **argv)
         /* create the profile from TEMPLATE */
         if (ctl->cmd == 'c') {
             char *tmp = NULL;
-            virAsprintf(&tmp, "  #include <libvirt/%s.files>\n", ctl->uuid);
+            tmp = g_strdup_printf("  #include <libvirt/%s.files>\n", ctl->uuid);
 
             if (ctl->dryrun) {
                 vah_info(profile);
-- 
2.21.0




More information about the libvir-list mailing list