The following command can crash ovn-nbctl:

ovn-nbctl --match="ip4" lr-nat-del lr0

Typically, if the router is the sole argument passed to lr-nat-del, the
behavior is to delete all NATs on the logical router. However, the code
specifically skips this action if a --match option is provided.

The problem is that the next thing the code does is to try to read the
NAT type from the command line. Since no NAT type was provided, this
reads past the ctx->argv array and causes a crash.

This commit fixes the issue by doing what likely was intended all along.
That is, if a --match is provided, then delete all NATs whose match
column matches the --match option, no matter the NAT type.

Signed-off-by: Mark Michelson <[email protected]>
---
 tests/ovn-nbctl.at    | 35 +++++++++++++++++++++++++++++++++++
 utilities/ovn-nbctl.c | 24 +++++++++++++++---------
 2 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/tests/ovn-nbctl.at b/tests/ovn-nbctl.at
index b0490cd57..023e5b424 100644
--- a/tests/ovn-nbctl.at
+++ b/tests/ovn-nbctl.at
@@ -3804,3 +3804,38 @@ check ovn-nbctl lr-policy-del lr0 100
 # Double check it was actually deleted.
 wait_row_count nb:Logical_Router_Policy 0
 ])
+
+OVN_NBCTL_TEST([nat_delete_match_no_type], [Delete NAT with match but no 
type], [
+check ovn-nbctl lr-add lr0
+check ovn-nbctl --match="ip4" lr-nat-add lr0 snat 10.0.0.1 192.168.0.1
+check ovn-nbctl --match="ip4" lr-nat-add lr0 dnat 10.0.0.1 192.168.0.1
+check ovn-nbctl --match="ip6" lr-nat-add lr0 snat fd01::1 fd11::1
+check ovn-nbctl --match="ip6" lr-nat-add lr0 dnat fd01::1 fd11::1
+
+check_row_count nb:NAT 4
+
+# This should delete both NATs with this match, even though the NATs are
+# separate types.
+check ovn-nbctl --match="ip4" lr-nat-del lr0
+
+check_row_count nb:NAT 2
+
+# Trying again should produce an error
+AT_CHECK([ovn-nbctl --match="ip4" lr-nat-del lr0], [1], [], [dnl
+ovn-nbctl: no matching NAT with the match (ip4)
+])
+
+check_row_count nb:NAT 2
+
+# Specifying the match and the type should only delete the SNAT and leave the 
DNAT
+check ovn-nbctl --match="ip6" lr-nat-del lr0 snat
+
+check_row_count nb:NAT 1
+
+# Trying again should produce an error
+AT_CHECK([ovn-nbctl --match="ip6" lr-nat-del lr0 snat], [1], [], [dnl
+ovn-nbctl: no matching NAT with the type (snat), match (ip6)
+])
+
+check_row_count nb:NAT 1
+])
diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index c24fd639d..7058d8fd8 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -6075,10 +6075,11 @@ nbctl_lr_nat_del(struct ctl_context *ctx)
         return;
     }
 
-    const char *nat_type = ctx->argv[2];
-    bool is_snat = !strcmp("snat", nat_type);
-    if (strcmp(nat_type, "dnat") && strcmp(nat_type, "snat")
-            && strcmp(nat_type, "dnat_and_snat")) {
+    const char *nat_type = ctx->argc > 2 ? ctx->argv[2] : NULL;
+    bool is_snat = nat_type && !strcmp("snat", nat_type);
+    if (nat_type && strcmp(nat_type, "dnat")
+                 && strcmp(nat_type, "snat")
+                 && strcmp(nat_type, "dnat_and_snat")) {
         ctl_error(ctx, "%s: type must be one of \"dnat\", \"snat\" and "
                   "\"dnat_and_snat\".", nat_type);
         return;
@@ -6113,17 +6114,22 @@ nbctl_lr_nat_del(struct ctl_context *ctx)
     if (must_exist && !lr_nat_del_matching(lr, nat_type, nat_ip, dgw_port,
                                            match, is_snat)) {
         struct ds ds = DS_EMPTY_INITIALIZER;
-        ds_put_format(&ds, "no matching NAT with the type (%s)",
-                      nat_type);
+        ds_put_cstr(&ds, "no matching NAT with the ");
+        if (nat_type) {
+            ds_put_format(&ds, "type (%s), ", nat_type);
+        }
         if (nat_ip) {
-            ds_put_format(&ds, ", %s (%s)",
+            ds_put_format(&ds, "%s (%s), ",
                           is_snat ? "logical_ip" : "external_ip", nat_ip);
         }
         if (dgw_port) {
-            ds_put_format(&ds, ", gateway_port (%s)", dgw_port->name);
+            ds_put_format(&ds, "gateway_port (%s), ", dgw_port->name);
         }
         if (match) {
-            ds_put_format(&ds, ", match (%s)", match);
+            ds_put_format(&ds, "match (%s)", match);
+        } else {
+            ds_chomp(&ds, ' ');
+            ds_chomp(&ds, ',');
         }
 
         ctx->error = xstrdup(ds_cstr_ro(&ds));
-- 
2.55.0

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to