All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Andrew Cooper <andrew.cooper3@citrix.com>,
	Ian Jackson <iwj@xenproject.org>, Wei Liu <wl@xen.org>,
	Anthony PERARD <anthony.perard@citrix.com>
Subject: [PATCH v2 04/10] tools/libxl: Fix uninitialised variable in libxl__domain_get_device_model_uid()
Date: Tue, 16 Feb 2021 17:43:27 +0000	[thread overview]
Message-ID: <20210216174327.8086-1-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <20210212153953.4582-5-andrew.cooper3@citrix.com>

Various version of gcc, when compiling with -Og, complain:

  libxl_dm.c: In function 'libxl__domain_get_device_model_uid':
  libxl_dm.c:256:12: error: 'kill_by_uid' may be used uninitialized in this function [-Werror=maybe-uninitialized]
    256 |         if (kill_by_uid)
        |            ^

The logic is very tangled.  Split the out and err labels apart, using out for
success cases only.

assert() that rc is 0 in the success case.  This allows for the removal of the
`if (!rc)` nesting in the out path, which reduces the cyclomatic complexity,
which is the root cause of false positive maybe-uninitialised warnings.

This also allows for dropping of the two paths setting kill_by_uid to false to
work around this warning at other optimisation levels.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Ian Jackson <iwj@xenproject.org>
CC: Wei Liu <wl@xen.org>
CC: Anthony PERARD <anthony.perard@citrix.com>

v2:
 * Split the out and err paths.
---
 tools/libs/light/libxl_dm.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/tools/libs/light/libxl_dm.c b/tools/libs/light/libxl_dm.c
index 291dee9b3f..dbd2ab607d 100644
--- a/tools/libs/light/libxl_dm.c
+++ b/tools/libs/light/libxl_dm.c
@@ -135,8 +135,8 @@ static int libxl__domain_get_device_model_uid(libxl__gc *gc,
         return 0;
 
     /*
-     * From this point onward, all paths should go through the `out`
-     * label.  The invariants should be:
+     * From this point onward, all paths should go through the `out` (success)
+     * or `err` (failure) labels.  The invariants should be:
      * - rc may be 0, or an error code.
      * - if rc is an error code, user and intended_uid are ignored.
      * - if rc is 0, user may be set or not set.
@@ -153,13 +153,13 @@ static int libxl__domain_get_device_model_uid(libxl__gc *gc,
     if (user) {
         rc = userlookup_helper_getpwnam(gc, user, &user_pwbuf, &user_base);
         if (rc)
-            goto out;
+            goto err;
 
         if (!user_base) {
             LOGD(ERROR, guest_domid, "Couldn't find device_model_user %s",
                  user);
             rc = ERROR_INVAL;
-            goto out;
+            goto err;
         }
 
         intended_uid = user_base->pw_uid;
@@ -176,7 +176,6 @@ static int libxl__domain_get_device_model_uid(libxl__gc *gc,
         LOGD(DEBUG, guest_domid,
              "dm_restrict disabled, starting QEMU as root");
         user = NULL; /* Should already be null, but just in case */
-        kill_by_uid = false; /* Keep older versions of gcc happy */
         rc = 0;
         goto out;
     }
@@ -188,7 +187,7 @@ static int libxl__domain_get_device_model_uid(libxl__gc *gc,
     rc = userlookup_helper_getpwnam(gc, LIBXL_QEMU_USER_RANGE_BASE,
                                          &user_pwbuf, &user_base);
     if (rc)
-        goto out;
+        goto err;
     if (user_base) {
         struct passwd *user_clash, user_clash_pwbuf;
 
@@ -196,14 +195,14 @@ static int libxl__domain_get_device_model_uid(libxl__gc *gc,
         rc = userlookup_helper_getpwuid(gc, intended_uid,
                                          &user_clash_pwbuf, &user_clash);
         if (rc)
-            goto out;
+            goto err;
         if (user_clash) {
             LOGD(ERROR, guest_domid,
                  "wanted to use uid %ld (%s + %d) but that is user %s !",
                  (long)intended_uid, LIBXL_QEMU_USER_RANGE_BASE,
                  guest_domid, user_clash->pw_name);
             rc = ERROR_INVAL;
-            goto out;
+            goto err;
         }
 
         LOGD(DEBUG, guest_domid, "using uid %ld", (long)intended_uid);
@@ -222,12 +221,11 @@ static int libxl__domain_get_device_model_uid(libxl__gc *gc,
     user = LIBXL_QEMU_USER_SHARED;
     rc = userlookup_helper_getpwnam(gc, user, &user_pwbuf, &user_base);
     if (rc)
-        goto out;
+        goto err;
     if (user_base) {
         LOGD(WARN, guest_domid, "Could not find user %s, falling back to %s",
              LIBXL_QEMU_USER_RANGE_BASE, LIBXL_QEMU_USER_SHARED);
         intended_uid = user_base->pw_uid;
-        kill_by_uid = false;
         rc = 0;
         goto out;
     }
@@ -240,23 +238,26 @@ static int libxl__domain_get_device_model_uid(libxl__gc *gc,
          "Could not find user %s or range base pseudo-user %s, cannot restrict",
          LIBXL_QEMU_USER_SHARED, LIBXL_QEMU_USER_RANGE_BASE);
     rc = ERROR_INVAL;
+    goto err;
 
 out:
+    assert(rc == 0);
+
     /* First, do a root check if appropriate */
-    if (!rc) {
-        if (user && intended_uid == 0) {
-            LOGD(ERROR, guest_domid, "intended_uid is 0 (root)!");
-            rc = ERROR_INVAL;
-        }
+    if (user && intended_uid == 0) {
+        LOGD(ERROR, guest_domid, "intended_uid is 0 (root)!");
+        rc = ERROR_INVAL;
+        goto err;
     }
 
     /* Then do the final set, if still appropriate */
-    if (!rc && user) {
+    if (user) {
         state->dm_runas = user;
         if (kill_by_uid)
             state->dm_kill_uid = GCSPRINTF("%ld", (long)intended_uid);
     }
 
+ err:
     return rc;
 }
 
-- 
2.11.0



  parent reply	other threads:[~2021-02-16 17:44 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-12 15:39 [PATCH for-4.15 00/10] tools: Support to use abi-dumper on libraries Andrew Cooper
2021-02-12 15:39 ` [PATCH 01/10] tools/xl: Fix exit code for `xl vkbattach` Andrew Cooper
2021-02-16 15:57   ` Ian Jackson
2021-02-16 16:25   ` Ian Jackson
2021-02-12 15:39 ` [PATCH 02/10] tools/libxg: Fix uninitialised variable in write_x86_cpu_policy_records() Andrew Cooper
2021-02-12 15:54   ` Jan Beulich
2021-02-16 15:57   ` Ian Jackson
2021-02-12 15:39 ` [PATCH 03/10] tools/libxg: Fix uninitialised variable in meminit() Andrew Cooper
2021-02-12 15:55   ` Julien Grall
2021-02-12 19:35     ` Andrew Cooper
2021-02-12 20:01   ` [PATCH v1.1 03/10] tools/libxg: Drop stale p2m logic from ARM's meminit() Andrew Cooper
2021-02-16 16:00     ` Ian Jackson
2021-02-16 16:27     ` Julien Grall
2021-02-12 15:39 ` [PATCH 04/10] tools/libxl: Fix uninitialised variable in libxl__domain_get_device_model_uid() Andrew Cooper
2021-02-16 16:07   ` Ian Jackson
2021-02-16 17:43   ` Andrew Cooper [this message]
2021-02-16 17:57     ` [PATCH v2 " Ian Jackson
2021-02-16 18:05       ` Ian Jackson
2021-02-12 15:39 ` [PATCH 05/10] tools/libxl: Fix uninitialised variable in libxl__write_stub_dmargs() Andrew Cooper
2021-02-16 16:07   ` Ian Jackson
2021-02-16 16:26   ` Ian Jackson
2021-02-12 15:39 ` [PATCH 06/10] stubdom/xenstored: Fix uninitialised variables in lu_read_state() Andrew Cooper
2021-02-12 16:08   ` Jürgen Groß
2021-02-12 17:01     ` Andrew Cooper
2021-02-13  9:36       ` Jürgen Groß
2021-02-15 14:12         ` Andrew Cooper
2021-02-15 15:36   ` [PATCH v1.1 " Andrew Cooper
2021-02-16 16:10   ` [PATCH " Ian Jackson
2021-02-12 15:39 ` [PATCH 07/10] tools: Use -Og for debug builds when available Andrew Cooper
2021-02-12 16:04   ` Jan Beulich
2021-02-12 16:09     ` Andrew Cooper
2021-02-12 16:14       ` Jan Beulich
2021-02-16 16:26     ` Ian Jackson
2021-02-16 16:11   ` Ian Jackson
2021-02-12 15:39 ` [PATCH 08/10] tools: Check for abi-dumper in ./configure Andrew Cooper
2021-02-16 16:11   ` Ian Jackson
2021-02-16 16:27   ` Ian Jackson
2021-02-12 15:39 ` [PATCH 09/10] tools/libs: Add rule to generate headers.lst Andrew Cooper
2021-02-16 16:13   ` Ian Jackson
2021-02-16 16:48   ` [PATCH v2 " Andrew Cooper
2021-02-16 17:32     ` Ian Jackson
2021-02-12 15:39 ` [PATCH 10/10] tools/libs: Write out an ABI analysis when abi-dumper is available Andrew Cooper
2021-02-12 16:12   ` Jan Beulich
2021-02-12 17:03     ` Andrew Cooper
2021-02-12 18:01   ` [PATCH v1.1 " Andrew Cooper
2021-02-16 16:15   ` [PATCH " Ian Jackson
2021-02-16 16:30   ` Ian Jackson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210216174327.8086-1-andrew.cooper3@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=anthony.perard@citrix.com \
    --cc=iwj@xenproject.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.