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

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