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

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