Blame SOURCES/0012-v2v-Check-that-mac-ip-parameters-are-sensible-RHBZ-1.patch

61e9b3
From fd1cbaa0907b30f639497c38953fe605bfc68ad0 Mon Sep 17 00:00:00 2001
61e9b3
From: "Richard W.M. Jones" <rjones@redhat.com>
61e9b3
Date: Tue, 28 Jul 2020 13:20:10 +0100
61e9b3
Subject: [PATCH] v2v: Check that --mac :ip: parameters are sensible
61e9b3
 (RHBZ#1858775).
61e9b3
MIME-Version: 1.0
61e9b3
Content-Type: text/plain; charset=UTF-8
61e9b3
Content-Transfer-Encoding: 8bit
61e9b3
61e9b3
This is not a complete check since IP addresses come in many forms,
61e9b3
but this at least stops nonsense being written through to the Windows
61e9b3
firstboot script.
61e9b3
61e9b3
$ virt-v2v --mac 11:22:33:44:55:66:ip:hello,world,999,invalid -i disk test1.img -o null
61e9b3
virt-v2v: error: cannot parse --mac ip ipaddr: doesn’t look like
61e9b3
“hello” is an IP address
61e9b3
61e9b3
$ virt-v2v --mac 11:22:33:44:55:66:ip:192.168.0.10,192.168.0.1,999,192.168.2.1,192.168.2.2 -i disk test1.img -o null
61e9b3
virt-v2v: error: --mac ip prefix length field is out of range
61e9b3
61e9b3
Thanks: Zi Liu
61e9b3
(cherry picked from commit e8bcf9615490447e1b53a8b0d3e9d202ab178cf0)
61e9b3
---
61e9b3
 v2v/cmdline.ml | 55 ++++++++++++++++++++++++++++++++------------------
61e9b3
 1 file changed, 35 insertions(+), 20 deletions(-)
61e9b3
61e9b3
diff --git a/v2v/cmdline.ml b/v2v/cmdline.ml
61e9b3
index 249137ab..3b74f307 100644
61e9b3
--- a/v2v/cmdline.ml
61e9b3
+++ b/v2v/cmdline.ml
61e9b3
@@ -47,6 +47,7 @@ type cmdline = {
61e9b3
 
61e9b3
 (* Matches --mac command line parameters. *)
61e9b3
 let mac_re = PCRE.compile ~anchored:true "([[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}:[[:xdigit:]]{2}):(network|bridge|ip):(.*)"
61e9b3
+let mac_ip_re = PCRE.compile ~anchored:true "([[:xdigit:]]|:|\\.)+"
61e9b3
 
61e9b3
 let parse_cmdline () =
61e9b3
   let bandwidth = ref None in
61e9b3
@@ -102,7 +103,7 @@ let parse_cmdline () =
61e9b3
 
61e9b3
   let network_map = Networks.create () in
61e9b3
   let static_ips = ref [] in
61e9b3
-  let add_network str =
61e9b3
+  let rec add_network str =
61e9b3
     match String.split ":" str with
61e9b3
     | "", "" ->
61e9b3
        error (f_"invalid -n/--network parameter")
61e9b3
@@ -110,8 +111,7 @@ let parse_cmdline () =
61e9b3
        Networks.add_default_network network_map out
61e9b3
     | in_, out ->
61e9b3
        Networks.add_network network_map in_ out
61e9b3
-  in
61e9b3
-  let add_bridge str =
61e9b3
+  and add_bridge str =
61e9b3
     match String.split ":" str with
61e9b3
     | "", "" ->
61e9b3
        error (f_"invalid -b/--bridge parameter")
61e9b3
@@ -119,8 +119,7 @@ let parse_cmdline () =
61e9b3
        Networks.add_default_bridge network_map out
61e9b3
     | in_, out ->
61e9b3
        Networks.add_bridge network_map in_ out
61e9b3
-  in
61e9b3
-  let add_mac str =
61e9b3
+  and add_mac str =
61e9b3
     if not (PCRE.matches mac_re str) then
61e9b3
       error (f_"cannot parse --mac \"%s\" parameter") str;
61e9b3
     let mac = PCRE.sub 1 and out = PCRE.sub 3 in
61e9b3
@@ -130,24 +129,40 @@ let parse_cmdline () =
61e9b3
     | "bridge" ->
61e9b3
        Networks.add_mac network_map mac Bridge out
61e9b3
     | "ip" ->
61e9b3
-       let add if_mac_addr if_ip_address if_default_gateway
61e9b3
-               if_prefix_length if_nameservers =
61e9b3
-         List.push_back static_ips
61e9b3
-                        { if_mac_addr; if_ip_address; if_default_gateway;
61e9b3
-                          if_prefix_length; if_nameservers }
61e9b3
-       in
61e9b3
        (match String.nsplit "," out with
61e9b3
-        | [] ->
61e9b3
-           error (f_"invalid --mac ip option")
61e9b3
-        | [ip] -> add mac ip None None []
61e9b3
-        | [ip; gw] -> add mac ip (Some gw) None []
61e9b3
+        | [] -> error (f_"invalid --mac ip option")
61e9b3
+        | [ip] -> add_static_ip mac ip None None []
61e9b3
+        | [ip; gw] -> add_static_ip mac ip (Some gw) None []
61e9b3
         | ip :: gw :: len :: nameservers ->
61e9b3
-           let len =
61e9b3
-             try int_of_string len with
61e9b3
-             | Failure _ -> error (f_"cannot parse --mac ip prefix length field as an integer: %s") len in
61e9b3
-           add mac ip (Some gw) (Some len) nameservers
61e9b3
-       );
61e9b3
+           add_static_ip mac ip (Some gw) (Some len) nameservers
61e9b3
+       )
61e9b3
     | _ -> assert false
61e9b3
+  and add_static_ip if_mac_addr if_ip_address if_default_gateway
61e9b3
+                    if_prefix_length_str if_nameservers =
61e9b3
+    (* Check the IP addresses and prefix length are sensible.  This
61e9b3
+     * is only a very simple test that they are sane, since IP addresses
61e9b3
+     * come in too many valid forms to check thoroughly.
61e9b3
+     *)
61e9b3
+    let rec error_unless_ip_addr what addr =
61e9b3
+      if not (PCRE.matches mac_ip_re addr) then
61e9b3
+        error (f_"cannot parse --mac ip %s: doesn’t look like “%s” is an IP address") what addr
61e9b3
+    in
61e9b3
+    error_unless_ip_addr "ipaddr" if_ip_address;
61e9b3
+    Option.may (error_unless_ip_addr "gw") if_default_gateway;
61e9b3
+    List.iter (error_unless_ip_addr "nameserver") if_nameservers;
61e9b3
+    let if_prefix_length =
61e9b3
+      match if_prefix_length_str with
61e9b3
+      | None -> None
61e9b3
+      | Some len ->
61e9b3
+         let len =
61e9b3
+           try int_of_string len with
61e9b3
+           | Failure _ -> error (f_"cannot parse --mac ip prefix length field as an integer: %s") len in
61e9b3
+         if len < 0 || len > 128 then
61e9b3
+           error (f_"--mac ip prefix length field is out of range");
61e9b3
+         Some len in
61e9b3
+    List.push_back static_ips
61e9b3
+      { if_mac_addr; if_ip_address; if_default_gateway;
61e9b3
+        if_prefix_length; if_nameservers }
61e9b3
   in
61e9b3
 
61e9b3
   let no_trim_warning _ =
61e9b3
-- 
61e9b3
2.27.0
61e9b3