[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 2/2] xen: Fix latent check-endbr.sh bug with 32bit build environments


  • To: Xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • Date: Fri, 15 Jul 2022 14:26:51 +0100
  • Authentication-results: esa5.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none
  • Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, George Dunlap <George.Dunlap@xxxxxxxxxxxxx>, Jan Beulich <JBeulich@xxxxxxxx>, "Stefano Stabellini" <sstabellini@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, Julien Grall <julien@xxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Luca Fancellu <Luca.Fancellu@xxxxxxx>, Mathieu Tarral <mathieu.tarral@xxxxxxxxxxxxxx>, Bertrand Marquis <Bertrand.Marquis@xxxxxxx>
  • Delivery-date: Fri, 15 Jul 2022 13:27:25 +0000
  • Ironport-data: A9a23:B3v3G61K40KjDHM+X/bD5SVxkn2cJEfYwER7XKvMYLTBsI5bpz0Cn zMWXGnVbv/YZWSkeYh/OoS2ox5SusfTmIRnHQNtpC1hF35El5HIVI+TRqvS04J+DSFhoGZPt Zh2hgzodZhsJpPkjk7xdOKn9RGQ7InQLpLkEunIJyttcgFtTSYlmHpLlvUwx4VlmrBVOSvU0 T/Ji5CZaQXNNwJcaDpOsfrc8U4355wehRtD1rAATaET1LPhvyF94KI3fcmZM3b+S49IKe+2L 86rIGaRpz6xE78FU7tJo56jGqE4aue60Tum0xK6b5OKkBlazhHe545gXBYqheW7vB3S9zx54 I0lWZVd0m7FNIWU8AgWe0Ew/y2TocSqUVIISJSymZX78qHIT5fj6/5oDmMOMIIdwbtuOl5Wr v0iCjVOMDnW0opawJrjIgVtrsEqLc2tN4IDoHBwizreCJ7KQ7iaHf+Mv4UBmm5t2IYeRp4yZ OJAAdZrRD3GbwdCJRE8D5Umkf3zrnL+bydZuBSeoq9fD237k1EogOe2aoC9ltqiY90K3Rypq 1n8pUfSDD5EDoOC8WWfyyf57gPItXyiA99DfFGizdZviUGTx2EOTgEfT0GypdGhkEmiXNRaI k0QvCEpqMAa9lGvT9T7dw21pjiDpBF0c/BdCfEg4QeBjI/d+R+EB3MsRyRELtchsaceYSEh3 xmnltXiCDhrrZWcU3fb/bCRxRuQEyUIKW4JZQcfUBAIpdLkpekbnh/JC9puDqOxptn0Ai3rh SCHqjAkgLcehtJN0L+0lXjYhxq8q56PSRQ6ji3MRX6s5A59YI+jZqSr5ELd4PIGK5yWJmRtp 1BdxZLYtrpXS8jQymrdG43hAY1F+d6ONj/+uVNgGqJ87g+S4yWiR6JVo29XcRIB3tk/RdP5X KPCkVoPucADYCr1NPQfj5GZUJpzk/W5fTjxfrWNN4cVPMAsHOOS1Hs2DXN8yVwBh6TFfUsXH Z6AOfihAn8BYUiM5GrnHrxNuVPHK81X+I8yeXwY507+uVZmTCTJIYrpyXPXBgzD0IuKoR/O7 /FUPNaQxhNUXYXWO3eKrt5JdgBRfSJkXvgaTvC7kcbaeGJb9JwJUaeNkdvNhaQ/90iqqgs41 i7kARIJoLYOrXbGNR+LehheVV8bZr4m9ShTFXV1ZT6AgiFzCa7yvfx3X8ZmItEaGBlLkKcco w8tIJ7bXJyii13vplwgUHUKhNczJU/72FjXYXvNjfpWV8cIejElM+TMJmPHnBTixALt3Sfii 9VMDj/mfKc=
  • Ironport-hdrordr: A9a23:Sqxqj6ickQ/t/Xk8JOH9arjRfnBQXt4ji2hC6mlwRA09TySZ// rOoB17726NtN9/YgBCpTntAsa9qDbnhPpICOoqTM6ftWvdyQmVxehZhOOIqVCNJ8S9zJ876U 4JSdkZNDSaNzhHZKjBjjVQa+xQpeW6zA==
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

While Xen's current VMA means it works, the mawk fix (i.e. using $((0xN)) in
the shell) isn't portable in 32bit shells.  See the code comment for the fix.

The fix found a second latent bug.  Recombining $vma_hi/lo should have used
printf "%s%08x" and only worked previously because $vma_lo had bits set in
it's top nibble.  Combining with the main fix, %08x becomes %07x.

Fixes: $XXX patch 1
Reported-by: Jan Beulich <JBeulich@xxxxxxxx>
Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
---
CC: George Dunlap <George.Dunlap@xxxxxxxxxxxxx>
CC: Jan Beulich <JBeulich@xxxxxxxx>
CC: Stefano Stabellini <sstabellini@xxxxxxxxxx>
CC: Wei Liu <wl@xxxxxxx>
CC: Julien Grall <julien@xxxxxxx>
CC: Anthony PERARD <anthony.perard@xxxxxxxxxx>
CC: Luca Fancellu <Luca.Fancellu@xxxxxxx>
CC: Mathieu Tarral <mathieu.tarral@xxxxxxxxxxxxxx>
CC: Bertrand Marquis <Bertrand.Marquis@xxxxxxx>

v2:
 * New
---
 xen/tools/check-endbr.sh | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/xen/tools/check-endbr.sh b/xen/tools/check-endbr.sh
index b3febd6a4ccc..d6aa117de13b 100755
--- a/xen/tools/check-endbr.sh
+++ b/xen/tools/check-endbr.sh
@@ -61,19 +61,36 @@ ${OBJDUMP} -j .text $1 -d -w | grep '       endbr64 *$' | 
cut -f 1 -d ':' > $VALID &
 #    the lower bits, rounding integers to the nearest 4k.
 #
 #    Instead, use the fact that Xen's .text is within a 1G aligned region, and
-#    split the VMA in half so AWK's numeric addition is only working on 32 bit
-#    numbers, which don't lose precision.
+#    split the VMA so AWK's numeric addition is only working on <32 bit
+#    numbers, which don't lose precision.  (See point 5)
 #
 # 4) MAWK doesn't support plain hex constants (an optional part of the POSIX
 #    spec), and GAWK and MAWK can't agree on how to work with hex constants in
 #    a string.  Use the shell to convert $vma_lo to decimal before passing to
 #    AWK.
 #
+# 5) Point 4 isn't fully portable.  POSIX only requires that $((0xN)) be
+#    evaluated as long, which in 32bit shells turns negative if bit 31 of the
+#    VMA is set.  AWK then interprets this negative number as a double before
+#    adding the offsets from the binary grep.
+#
+#    Instead of doing an 8/8 split with vma_hi/lo, do a 9/7 split.
+#
+#    The consequence of this is that for all offsets, $vma_lo + offset needs
+#    to be less that 256M (i.e. 7 nibbles) so as to be successfully recombined
+#    with the 9 nibbles of $vma_hi.  This is fine; .text is at the start of a
+#    1G aligned region, and Xen is far far smaller than 256M, but leave safety
+#    check nevertheless.
+#
 eval $(${OBJDUMP} -j .text $1 -h |
-    $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 8), 
substr($4, 9, 16)}')
+    $AWK '$2 == ".text" {printf "vma_hi=%s\nvma_lo=%s\n", substr($4, 1, 9), 
substr($4, 10, 16)}')
 
 ${OBJCOPY} -j .text $1 -O binary $TEXT_BIN
 
+bin_sz=$(stat -c '%s' $TEXT_BIN)
+[ "$bin_sz" -ge $(((1 << 28) - $vma_lo)) ] &&
+    { echo "$MSG_PFX Error: .text offsets can exceed 256M" >&2; exit 1; }
+
 # instruction:    hex:           oct:
 # endbr64         f3 0f 1e fa    363 017 036 372
 # endbr32         f3 0f 1e fb    363 017 036 373
@@ -84,7 +101,7 @@ then
 else
     grep -aob -e "$(printf '\363\17\36\372')" -e "$(printf '\363\17\36\373')" \
          -e "$(printf '\146\17\37\1')" $TEXT_BIN
-fi | $AWK -F':' '{printf "%s%x\n", "'$vma_hi'", int('$((0x$vma_lo))') + $1}' > 
$ALL
+fi | $AWK -F':' '{printf "%s%07x\n", "'$vma_hi'", int('$((0x$vma_lo))') + $1}' 
> $ALL
 
 # Wait for $VALID to become complete
 wait
-- 
2.11.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.