[PATCH] virFirewallApply: Fix possible NULL dereference on error

Peter Krempa posted 1 patch 3 years, 1 month ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/b1b4c2c60c6bd7efb8f0201cfce1296c2c1b5174.1614937326.git.pkrempa@redhat.com
src/util/virfirewall.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
[PATCH] virFirewallApply: Fix possible NULL dereference on error
Posted by Peter Krempa 3 years, 1 month ago
Commit bbc25f0d03d443efd35381463efc81b01cb6ae96 juggled around some
error reporting. Unfortunately virFirewallApply tries to report the
errno stored in the firewall object and we'd try to do that when the
firewall object is NULL too. Report EINVAL if 'firewall' is NULL.

Found by Coverity.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 src/util/virfirewall.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/util/virfirewall.c b/src/util/virfirewall.c
index c1b7d2268b..0dc0cecd53 100644
--- a/src/util/virfirewall.c
+++ b/src/util/virfirewall.c
@@ -766,8 +766,12 @@ virFirewallApply(virFirewallPtr firewall)
         goto cleanup;
     }
     if (!firewall || firewall->err) {
-        virReportSystemError(firewall->err, "%s",
-                             _("Unable to create rule"));
+        int err = EINVAL;
+
+        if (firewall)
+            err = firewall->err;
+
+        virReportSystemError(err, "%s", _("Unable to create rule"));
         goto cleanup;
     }

-- 
2.29.2

Re: [PATCH] virFirewallApply: Fix possible NULL dereference on error
Posted by Pavel Hrdina 3 years, 1 month ago
On Fri, Mar 05, 2021 at 10:42:06AM +0100, Peter Krempa wrote:
> Commit bbc25f0d03d443efd35381463efc81b01cb6ae96 juggled around some
> error reporting. Unfortunately virFirewallApply tries to report the
> errno stored in the firewall object and we'd try to do that when the
> firewall object is NULL too. Report EINVAL if 'firewall' is NULL.
> 
> Found by Coverity.
> 
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---
>  src/util/virfirewall.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>