This patch fix the following issues with _crashkernel_add,
1. it couldn't address a terabyte memory range e.g. _crashkernel_add "128G-1T:4G" "0"
actually returns empty result
2. it allows a memory delta without a unit specified and returns the incorrect result
e.g. _crashkernel_add 1G-4G:256M,4G-64G:320M,64G-:576M 100 actually
returns 1G-4G:268435556,4G-64G:335544420,64G-:603979876
Note a memory delta without unit like 0M is allowed.
Fixes: 64f2827a ("kdump-lib: Harden _crashkernel_add")
Signed-off-by: Coiby Xu <coxu(a)redhat.com>
---
kdump-lib.sh | 9 ++++++++-
spec/kdump-lib_spec.sh | 7 +++++--
2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/kdump-lib.sh b/kdump-lib.sh
index ae39c236..d182bab8 100755
--- a/kdump-lib.sh
+++ b/kdump-lib.sh
@@ -862,7 +862,9 @@ has_aarch64_smmu()
ls /sys/devices/platform/arm-smmu-* 1> /dev/null 2>&1
}
-is_memsize() { [[ "$1" =~ ^[+-]?[0-9]+[KkMmGg]?$ ]]; }
+is_memdelta() { [[ "$1" == 0 || "$1" =~ ^[+-]?[0-9]+[BbKkMmGgTt]$ ]]; }
+
+is_memsize() { [[ "$1" =~ ^[+-]?[0-9]+[BbKkMmGgTt]?$ ]]; }
# range defined for crashkernel parameter
# i.e. <start>-[<end>]
@@ -881,6 +883,9 @@ to_bytes()
is_memsize "$_s" || return 1
case "${_s: -1}" in
+ B|b)
+ _s=${_s::-1}
+ ;;
K|k)
_s=${_s::-1}
_s="$((_s * 1024))"
@@ -964,6 +969,8 @@ _crashkernel_add()
delta="$2"
ret=""
+ is_memdelta "$delta" || return 1
+
while IFS=';' read -r size range offset; do
if [[ -n "$offset" ]]; then
ret="${ret%,}$offset"
diff --git a/spec/kdump-lib_spec.sh b/spec/kdump-lib_spec.sh
index 814f9618..ee6c9b27 100644
--- a/spec/kdump-lib_spec.sh
+++ b/spec/kdump-lib_spec.sh
@@ -55,8 +55,10 @@ Describe 'kdump-lib'
"1G-4G:256M,4G-64G:320M,64G-:576M@4G" "100M" "1G-4G:356M,4G-64G:420M,64G-:676M@4G"
"1G-4G:1G,4G-64G:2G,64G-:3G@4G" "100M" "1G-4G:1124M,4G-64G:2148M,64G-:3172M@4G"
"1G-4G:10000K,4G-64G:20000K,64G-:40000K@4G" "100M" "1G-4G:112400K,4G-64G:122400K,64G-:142400K@4G"
- "1,high" "1" "2,high"
- "1K,low" "1" "1025,low"
+ "128G-1T:4G" "0" "128G-1T:4G"
+ "128G-1T:4G" "0M" "128G-1T:4G"
+ "1,high" "1b" "2,high"
+ "1K,low" "1b" "1025,low"
"1M@1G" "1k" "1025K@1G"
"500M@1G" "-100m" "400M@1G"
"1099511627776" "0" "1024G"
@@ -68,6 +70,7 @@ Describe 'kdump-lib'
End
Context "For invalid input values"
Parameters
+ "1G-4G:256M" "100"
"1G-4G:256M.4G-64G:320M" "100M"
"foo" "1"
"1" "bar"
--
2.41.0