Skip to contentSkip to conversation

expand: Fix out-of-bounds syntax table access

PATCH In review Ayesha Shafique 4 participants 4 messages Aug 17 – Nov 11

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 expand: Fix out-of-bounds syntax table access

mbtodest passes a signed byte to chtodest. Bytes with the high bit set can therefore be negative. The syntax tables are based at SYNBASE so that signed character values can be used as indices. However, the is_type path did not apply this offset, causing chtodest and mbtodest to read before the array:

  $ printf 'v=\377\nw=$v\n' | src/dash
  AddressSanitizer: global-buffer-overflow in chtodest

Apply SYNBASE to is_type, as is already done by the is_special macro.

Fixes c0c860df08c9 ("expand: Count multi-byte characters for VSLENGTH")

---

Hi,

I found this while working on a research project, testing against commit 037bbdf ("Release 0.5.13.5"). Here is the relevant GDB and AddressSanitizer trace:

  $ printf 'v=\xff\nw=$v\n' > /tmp/s.sh
  $ gdb -q --args ./dash /tmp/s.sh
  Reading symbols from ./dash...
  (gdb) b expand.c:895
  Breakpoint 1 at 0x8c028: file expand.c, line 895.
  (gdb) run
  Starting program: /home/aysha/dash/src/dash /tmp/s.sh

Breakpoint 1, chtodest (c=-1,

      syntax=0x55555562d680 <is_type> "",
      out=0x5555556bea42 <stackbase+98> "\377") at expand.c:895

895 {
(gdb) info args
c = -1
syntax = 0x55555562d680 <is_type> ""
out = 0x5555556bea42 <stackbase+98> "\377"
(gdb) n
896 if (syntax[c] == CCTL)
(gdb) p c
$1 = -1
(gdb) n
=================================================================
ERROR: AddressSanitizer: global-buffer-overflow on address 0x55555562d67f
READ of size 1 at 0x55555562d67f thread T0
#0 in chtodest /home/aysha/dash/src/expand.c:896
#1 in mbtodest /home/aysha/dash/src/expand.c:918
#2 in memtodest /home/aysha/dash/src/expand.c:996 #3 in strtodest /home/aysha/dash/src/expand.c:1017 #4 in varvalue /home/aysha/dash/src/expand.c:1140 #5 in evalvar /home/aysha/dash/src/expand.c:811
#6 in argstr /home/aysha/dash/src/expand.c:391
#7 in expandarg /home/aysha/dash/src/expand.c:232
0x55555562d67f is located 1 bytes before global variable 'is_type' defined in 'syntax.c:285:12' (0x55555562d680) of size 257
SUMMARY: AddressSanitizer: global-buffer-overflow /home/aysha/dash/src/expand.c:896 in chtodest

This is my first patch to dash, so apologies for any mistakes I might have made. I'm happy to fix anything you point out.

Thanks!
Ayesha

 src/expand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/expand.c +1 -1
index 76e67ce..7ebb841 100644
@@ -980,7 +980,7 @@ static size_t memtodest(const char *p, size_t len, int flags)
 		p += count;
 
 		syntax = flags & (QUOTES_ESC | EXP_MBCHAR) ?
-			 BASESYNTAX : is_type;
+			 BASESYNTAX : is_type + SYNBASE;
 	} else
 		syntax = SQSYNTAX;
 
Signature · 1 line
2.55.0
#1 lore raw

I hit this on Fedora 44 (0.5.13.1).
It causes the coreutils test suite to fail.
Note that test suite also sets LC_ALL=C which I would have expected to otherwise avoid this multi-byte specific path. So perhaps the following is also an appropriate optimization:

cheers,
Padraig

src/expand.c +1 -1
index 76e67ce..5efa257 100644
@@ -992,7 +992,7 @@ static size_t memtodest(const char *p, size_t len, int flags)
 
                count++;
 
-               if (unlikely(c < 0)) {
+               if (unlikely(c < 0) && MB_CUR_MAX > 1) {
                        struct mbpair mbp = mbtodest(p, q, syntax, len);
                        unsigned mlm;
 
Tested-by on v1 1/1
#2 lore raw
In reply to Ayesha Shafique
Quoted 6 lines · Ayesha Shafique

mbtodest passes a signed byte to chtodest. Bytes with the high bit set can therefore be negative. The syntax tables are based at SYNBASE so that signed character values can be used as indices. However, the is_type path did not apply this offset, causing chtodest and mbtodest to read before the array:

Quoted 7 lines
   $ printf 'v=\377\nw=$v\n' | src/dash
   AddressSanitizer: global-buffer-overflow in chtodest

Apply SYNBASE to is_type, as is already done by the is_special macro.

Fixes c0c860df08c9 ("expand: Count multi-byte characters for VSLENGTH")

This out-of-bounds read is also the cause of the stray 0x81 bytes reported separately at

https://lore.kernel.org/dash/CALwy4xJPUYmHJkBvwAr02zp9HfMp0h2upeRxc4NnyXT5ELn4CA@mail.gmail.com/

and it is breaking Git's test suite on Ubuntu 27.04 (dash 0.5.13.5-1ubuntu1) on arm64, ppc64el, and s390x.

https://launchpad.net/~git-core/+archive/ubuntu/candidate/+build/33960211
https://launchpad.net/~git-core/+archive/ubuntu/candidate/+build/33960214
https://launchpad.net/~git-core/+archive/ubuntu/candidate/+build/33960216

Please include this fix in 0.5.13.6.

Thanks,
Anders

#3 lore raw
In reply to Ayesha Shafique
Quoted 59 lines · Ayesha Shafique

mbtodest passes a signed byte to chtodest. Bytes with the high bit set can therefore be negative. The syntax tables are based at SYNBASE so that signed character values can be used as indices. However, the is_type path did not apply this offset, causing chtodest and mbtodest to read before the array:

  $ printf 'v=\377\nw=$v\n' | src/dash
  AddressSanitizer: global-buffer-overflow in chtodest

Apply SYNBASE to is_type, as is already done by the is_special macro.

Fixes c0c860df08c9 ("expand: Count multi-byte characters for VSLENGTH")

---

Hi,

I found this while working on a research project, testing against commit 037bbdf ("Release 0.5.13.5"). Here is the relevant GDB and AddressSanitizer trace:

  $ printf 'v=\xff\nw=$v\n' > /tmp/s.sh
  $ gdb -q --args ./dash /tmp/s.sh
  Reading symbols from ./dash...
  (gdb) b expand.c:895
  Breakpoint 1 at 0x8c028: file expand.c, line 895.
  (gdb) run
  Starting program: /home/aysha/dash/src/dash /tmp/s.sh

Breakpoint 1, chtodest (c=-1,

      syntax=0x55555562d680 <is_type> "",
      out=0x5555556bea42 <stackbase+98> "\377") at expand.c:895

895 {
(gdb) info args
c = -1
syntax = 0x55555562d680 <is_type> ""
out = 0x5555556bea42 <stackbase+98> "\377"
(gdb) n
896 if (syntax[c] == CCTL)
(gdb) p c
$1 = -1
(gdb) n
=================================================================
ERROR: AddressSanitizer: global-buffer-overflow on address 0x55555562d67f
READ of size 1 at 0x55555562d67f thread T0
#0 in chtodest /home/aysha/dash/src/expand.c:896
#1 in mbtodest /home/aysha/dash/src/expand.c:918
#2 in memtodest /home/aysha/dash/src/expand.c:996 #3 in strtodest /home/aysha/dash/src/expand.c:1017 #4 in varvalue /home/aysha/dash/src/expand.c:1140 #5 in evalvar /home/aysha/dash/src/expand.c:811
#6 in argstr /home/aysha/dash/src/expand.c:391
#7 in expandarg /home/aysha/dash/src/expand.c:232
0x55555562d67f is located 1 bytes before global variable 'is_type' defined in 'syntax.c:285:12' (0x55555562d680) of size 257
SUMMARY: AddressSanitizer: global-buffer-overflow /home/aysha/dash/src/expand.c:896 in chtodest

Quoted 8 lines

This is my first patch to dash, so apologies for any mistakes I might have made. I'm happy to fix anything you point out.

Thanks!
Ayesha

 src/expand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt