Blame SOURCES/0018-Grub-tolerate-some-invalid-entries.patch

c8163d
From ccebad103f3466d5e8fc2b60f6127af618adf8d2 Mon Sep 17 00:00:00 2001
c8163d
From: David Lutterkort <lutter@watzmann.net>
c8163d
Date: Mon, 4 Jun 2018 10:45:19 -0700
c8163d
Subject: [PATCH] Grub: tolerate some invalid entries
c8163d
c8163d
Refusing to parse an entire file because of invalid entries is too
c8163d
harsh. Try to make the behavior a little friendlier by simply mapping invalid
c8163d
entries to '#error' nodes but still parsing the rest of the file.
c8163d
c8163d
Also remove del_to_eol, that would delete anything up to eol; it was only
c8163d
used in kw_pres, but should have never been used there. kw_pres should only
c8163d
match the keyword and eol. A line like 'quiet foo bar baz' should not be
c8163d
accepted by (kw_pres "quiet").
c8163d
---
c8163d
 lenses/grub.aug            | 58 ++++++++++++++++++++++++++++++++------
c8163d
 lenses/tests/test_grub.aug | 25 ++++++++++++++++
c8163d
 2 files changed, 75 insertions(+), 8 deletions(-)
c8163d
c8163d
diff --git a/lenses/grub.aug b/lenses/grub.aug
c8163d
index 9866f3f7..f99a3a92 100644
c8163d
--- a/lenses/grub.aug
c8163d
+++ b/lenses/grub.aug
c8163d
@@ -29,9 +29,6 @@ module Grub =
c8163d
     (* View: eol *)
c8163d
     let eol = Util.eol
c8163d
 
c8163d
-    (* View: del_to_eol *)
c8163d
-    let del_to_eol = del /[^ \t\n]*/ ""
c8163d
-
c8163d
     (* View: spc *)
c8163d
     let spc = Util.del_ws_spc
c8163d
 
c8163d
@@ -92,7 +89,22 @@ module Grub =
c8163d
       eol ]
c8163d
 
c8163d
     (* View: kw_pres *)
c8163d
-    let kw_pres (kw:string) = [ opt_ws . key kw . del_to_eol . eol ]
c8163d
+    let kw_pres (kw:string) = [ opt_ws . key kw . eol ]
c8163d
+
c8163d
+    (* View: error
c8163d
+     *   Parse a line that looks almost like a valid setting, but isn't,
c8163d
+     *   into an '#error' node. Any line that starts with letters, but not
c8163d
+     *   anything matching kw, is considered an error line.
c8163d
+     *
c8163d
+     *   Parameters:
c8163d
+     *     kw:regexp - the valid keywords that are _not_ considered an
c8163d
+     *                 error
c8163d
+     *)
c8163d
+    let error (kw:regexp) =
c8163d
+      let not_kw = /[a-zA-Z]+/ - kw in
c8163d
+      [ label "#error" . Util.del_opt_ws "\t"
c8163d
+        . store (not_kw . /([^a-zA-Z\n].*[^ \t\n])?/) . eol ]
c8163d
+
c8163d
 
c8163d
 (************************************************************************
c8163d
  * Group:                 BOOT ENTRIES
c8163d
@@ -138,8 +150,8 @@ module Grub =
c8163d
         spc . [ label "from" . store Rx.no_spaces ] )? .
c8163d
       eol ]
c8163d
 
c8163d
-    (* View: menu_setting *)
c8163d
-    let menu_setting = kw_menu_arg "default"
c8163d
+    (* View: menu_entry *)
c8163d
+    let menu_entry = kw_menu_arg "default"
c8163d
                      | kw_menu_arg "fallback"
c8163d
                      | kw_pres "hiddenmenu"
c8163d
                      | kw_menu_arg "timeout"
c8163d
@@ -156,6 +168,21 @@ module Grub =
c8163d
                      | device
c8163d
                      | setkey
c8163d
 
c8163d
+    (* View: menu_error
c8163d
+     *   Accept lines not matching menu_entry and stuff them into
c8163d
+     *   '#error' nodes
c8163d
+     *)
c8163d
+    let menu_error =
c8163d
+      let kw = /default|fallback|hiddenmenu|timeout|splashimage|gfxmenu/
c8163d
+             |/foreground|background|verbose|boot|password|title/
c8163d
+             |/serial|setkey|terminal|color|device/ in
c8163d
+      error kw
c8163d
+
c8163d
+    (* View: menu_setting
c8163d
+     *   a valid menu setting or a line that looks like one but is an #error
c8163d
+     *)
c8163d
+    let menu_setting = menu_entry | menu_error
c8163d
+
c8163d
     (* View: title *)
c8163d
     let title = del /title[ \t=]+/ "title " . value_to_eol . eol
c8163d
 
c8163d
@@ -206,9 +233,9 @@ module Grub =
c8163d
     let configfile =
c8163d
       [ command "configfile" "\t" . spc . store Rx.no_spaces . eol ]
c8163d
 
c8163d
-    (* View: boot_setting
c8163d
+    (* View: boot_entry
c8163d
         <boot> entries *)
c8163d
-    let boot_setting =
c8163d
+    let boot_entry =
c8163d
           let boot_arg_re = "root" | "initrd" | "rootnoverify" | "uuid"
c8163d
                           | "findroot" | "bootfs" (* Solaris extensions *)
c8163d
        in kw_boot_arg boot_arg_re
c8163d
@@ -223,6 +250,21 @@ module Grub =
c8163d
         | kw_pres "makeactive"
c8163d
         | password_arg
c8163d
 
c8163d
+    (* View: boot_error
c8163d
+     *   Accept lines not matching boot_entry and stuff them into
c8163d
+     *   '#error' nodes
c8163d
+     *)
c8163d
+    let boot_error =
c8163d
+      let kw = /lock|uuid|password|root|initrd|rootnoverify|findroot|bootfs/
c8163d
+             |/configfile|chainloader|title|boot|quiet|kernel|module/
c8163d
+             |/makeactive|savedefault|map/ in
c8163d
+      error kw
c8163d
+
c8163d
+    (* View: boot_setting
c8163d
+     *   a valid boot setting or a line that looks like one but is an #error
c8163d
+     *)
c8163d
+    let boot_setting = boot_entry | boot_error
c8163d
+
c8163d
     (* View: boot *)
c8163d
     let boot =
c8163d
       let line = ((boot_setting|comment)* . boot_setting)? in
c8163d
diff --git a/lenses/tests/test_grub.aug b/lenses/tests/test_grub.aug
c8163d
index 8a0d9f4a..75657203 100644
c8163d
--- a/lenses/tests/test_grub.aug
c8163d
+++ b/lenses/tests/test_grub.aug
c8163d
@@ -257,3 +257,28 @@ password --encrypted ^9^32kwzzX./3WISQ0C /boot/grub/custom.lst
c8163d
     { "password" = "secret"
c8163d
       { "md5" }
c8163d
     } }
c8163d
+
c8163d
+  (* Test parsing of invalid entries via menu_error *)
c8163d
+  test Grub.lns get "default=0\ncrud=no\n" =
c8163d
+  { "default" = "0" }
c8163d
+  { "#error" = "crud=no" }
c8163d
+
c8163d
+  (* We handle some pretty bizarre bad syntax *)
c8163d
+  test Grub.lns get "default=0
c8163d
+crud no
c8163d
+valid:nope
c8163d
+nonsense  =   yes
c8163d
+bad arg1 arg2 arg3=v\n" =
c8163d
+  { "default" = "0" }
c8163d
+  { "#error" = "crud no" }
c8163d
+  { "#error" = "valid:nope" }
c8163d
+  { "#error" = "nonsense  =   yes" }
c8163d
+  { "#error" = "bad arg1 arg2 arg3=v" }
c8163d
+
c8163d
+  (* Test parsing of invalid entries via boot_error *)
c8163d
+  test Grub.lns get "title test
c8163d
+    root (hd0,0)
c8163d
+    crud foo\n" =
c8163d
+  { "title" = "test"
c8163d
+    { "root" = "(hd0,0)" }
c8163d
+    { "#error" = "crud foo" } }
c8163d
-- 
9f41aa
2.24.1
c8163d