Talos Vulnerability Report

TALOS-2022-1672

Open Babel Gaussian format orientation out-of-bounds write vulnerability

July 21, 2023
CVE Number

CVE-2022-37331

SUMMARY

An out-of-bounds write vulnerability exists in the Gaussian format orientation functionality of Open Babel 3.1.1 and master commit 530dbfa3. A specially crafted malformed file can lead to arbitrary code execution. An attacker can provide a malicious file to trigger this vulnerability.

CONFIRMED VULNERABLE VERSIONS

The versions below were either tested or verified to be vulnerable by Talos or confirmed to be vulnerable by the vendor.

Open Babel 3.1.1
Open Babel master commit 530dbfa3

PRODUCT URLS

Open Babel - https://openbabel.org/

CVSSv3 SCORE

7.3 - CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:L

CWE

CWE-119 - Improper Restriction of Operations within the Bounds of a Memory Buffer

DETAILS

Open Babel is a popular library for converting chemical file formats, currently supporting about 130 different file formats. It implements bindings for several programming languages. Because of the nature of the library, and since there are many online chemical format converters and molecule viewers which might be using Open Babel in their backend for parsing and conversion, we consider this software as potentially accessible via network.

Open Babel ships a simple converter application called obabel that can be used to trigger the issue described in this advisory. obabel supports -i and -o parameters, which select the input and output formats to perform the conversion. obabel supports multiple input and output files (as does the Open Babel library itself): this technically allows multiple vulnerabilities to trigger in sequence, which in turn could make some vulnerabilities easier to exploit. In this advisory, however, we focus on only one input file and a corresponding output file.

When a single input file and output file are supplied, obabel.cpp records the input and output formats (if supplied) and calls OBConversion::FullConvert in obconversion.cpp. Inside this function, there’s a call to OpenAndSetFormat, which uses FormatFromExt to derive the input format from the filename extension if no -i parameter was supplied. Similarly, OpenInAndOutFiles can be used to derive both input and output formats from the filename extensions when none are supplied.
Depending on how the obabel application is invoked, different paths could actually take place. Eventually, pInFormat and pOutFormat (of base class OBFormat) objects are allocated, which are instances of the classes that implement the selected input and output formats.

The code then proceeds with a call to OBConversion::Convert, which eventually leads to calling pInFormat->ReadMolecule and pOutFormat->WriteMolecule.

In this advisory, we describe an issue in the Gaussian file format (formats/gaussformat.cpp) when parsing an input file via ReadMolecule.

    bool GaussianOutputFormat::ReadMolecule(OBBase* pOb, OBConversion* pConv)
    {
      OBMol* pmol = pOb->CastAndClear<OBMol>();
      if (pmol == nullptr)
        return false;

      //Define some references so we can use the old parameter names
      istream &ifs = *pConv->GetInStream();
      OBMol &mol = *pmol;
      const char* title = pConv->GetTitle();

[1]   char buffer[BUFF_SIZE];
      ...

      int i=0;
      bool no_symmetry=false;
      char coords_type[25];

      //Prescan file to find second instance of "orientation:"
      //This will be the kind of coords used in the chk/fchk file
      //Unless the "nosym" keyword has been requested
[2]   while (ifs.getline(buffer,BUFF_SIZE))
        {
          if (strstr(buffer, "Symmetry turned off by external request.") != nullptr)
            {
              // The "nosym" keyword has been requested
              no_symmetry = true;
            }
[3]       if (strstr(buffer, "orientation:") != nullptr)
            {
              i++;
[4]           tokenize (vs, buffer);
              // gotta check what types of orientation are present
[5]           strncpy (coords_type, vs[0].c_str(), 24);
[6]           strcat (coords_type, " orientation:");
            }
          if ((no_symmetry && i==1) || i==2)
             break;
        }

The function defines many variables (they’ve been removed from the listing above). We’re especially interested in buffer at [1], a char buffer of size 32768 which is used to read lines in the input file. At [2] a line is read, which is expected to contain the string “orientation:” to match the condition at [3].

At [4], the line is tokenized: buffer is split on whitespaces, and each token is put in the vs vector. At [5], the first token is copied into coords_type, with a maximum size of 24 (note that strncpy does not ensure null-termination; this is a separate issue).

coords_type has a size of 25, and the strncpy at [5] might have filled 24 bytes already. However, at [6], another 13 fixed characters are concatenated to coords_type, writing out-of-bounds on the stack.

Depending on how the code is compiled and the stack is laid out, this issue could lead to arbitrary code execution.

Crash Information

$ ./bin/obabel -i gal strcat.gal -o sdf
=================================================================
==1192007==ERROR: AddressSanitizer: stack-buffer-overflow on address 0xffff33c9 at pc 0xf79dcb7d bp 0xffff1518 sp 0xffff10f0
WRITE of size 14 at 0xffff33c9 thread T0
    #0 0xf79dcb7c in __interceptor_memcpy ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827
    #1 0xf478b7ee in OpenBabel::GaussianOutputFormat::ReadMolecule(OpenBabel::OBBase*, OpenBabel::OBConversion*) ./src/formats/gaussformat.cpp:528
    #2 0xf751a915 in OpenBabel::OBMoleculeFormat::ReadChemObjectImpl(OpenBabel::OBConversion*, OpenBabel::OBFormat*) ./src/obmolecformat.cpp:102
    #3 0xf63c358c in OpenBabel::OBMoleculeFormat::ReadChemObject(OpenBabel::OBConversion*) ./include/openbabel/obmolecformat.h:116
    #4 0xf72a204e in OpenBabel::OBConversion::Convert() ./src/obconversion.cpp:545
    #5 0xf72c717a in OpenBabel::OBConversion::Convert(std::istream*, std::ostream*) ./src/obconversion.cpp:481
    #6 0xf72cf4f3 in OpenBabel::OBConversion::FullConvert(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&) ./src/obconversion.cpp:1514
    #7 0x565594ea in main ./tools/obabel.cpp:370
    #8 0xf77923b4 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
    #9 0xf779247e in __libc_start_main_impl ../csu/libc-start.c:389
    #10 0x5655c356 in _start (./bin/obabel+0x7356)

Address 0xffff33c9 is located in stack of thread T0 at offset 7273 in frame
    #0 0xf478912f in OpenBabel::GaussianOutputFormat::ReadMolecule(OpenBabel::OBBase*, OpenBabel::OBConversion*) ./src/formats/gaussformat.cpp:437

  This frame has 228 object(s):
    [32, 33) '<unknown>'
    [48, 49) '<unknown>'
    [64, 65) '<unknown>'
    [80, 81) '<unknown>'
    [96, 97) '<unknown>'
    [112, 113) '<unknown>'
    [128, 129) '<unknown>'
    [144, 145) '<unknown>'
    [160, 161) '<unknown>'
    [176, 177) '<unknown>'
    [192, 193) '<unknown>'
    [208, 209) '<unknown>'
    [224, 225) '<unknown>'
    [240, 241) '<unknown>'
    [256, 257) '<unknown>'
    [272, 273) '<unknown>'
    [288, 289) '<unknown>'
    [304, 305) '<unknown>'
    [320, 321) '<unknown>'
    [336, 337) '<unknown>'
    [352, 353) '<unknown>'
    [368, 369) '<unknown>'
    [384, 385) '<unknown>'
    [400, 401) '<unknown>'
    [416, 417) '<unknown>'
    [432, 433) '<unknown>'
    [448, 449) '<unknown>'
    [464, 465) '<unknown>'
    [480, 481) '<unknown>'
    [496, 497) '<unknown>'
    [512, 513) '<unknown>'
    [528, 529) '<unknown>'
    [544, 545) '<unknown>'
    [560, 561) '<unknown>'
    [576, 577) '<unknown>'
    [592, 593) '<unknown>'
    [608, 609) '<unknown>'
    [624, 625) '<unknown>'
    [640, 641) '<unknown>'
    [656, 657) '<unknown>'
    [672, 673) '<unknown>'
    [688, 689) '<unknown>'
    [704, 705) '<unknown>'
    [720, 721) '<unknown>'
    [736, 737) '<unknown>'
    [752, 753) '<unknown>'
    [768, 769) '<unknown>'
    [784, 785) '<unknown>'
    [800, 801) '<unknown>'
    [816, 817) '<unknown>'
    [832, 833) '<unknown>'
    [848, 849) '<unknown>'
    [864, 865) '<unknown>'
    [880, 881) '<unknown>'
    [896, 898) '<unknown>'
    [912, 916) 'tmpCoords' (line 648)
    [928, 932) 'fgpi' (line 879)
    [944, 948) '<unknown>'
    [960, 964) '<unknown>'
    [976, 980) '<unknown>'
    [992, 996) '<unknown>'
    [1008, 1012) '__guard'
    [1024, 1028) '__dnew'
    [1040, 1044) '__guard'
    [1056, 1060) '__guard'
    [1072, 1076) '__guard'
    [1088, 1092) '__guard'
    [1104, 1108) '__guard'
    [1120, 1124) '__guard'
    [1136, 1140) '__guard'
    [1152, 1156) '__dnew'
    [1168, 1172) '__guard'
    [1184, 1188) '__dnew'
    [1200, 1204) '__guard'
    [1216, 1220) 'd' (line 436)
    [1232, 1236) 'd' (line 436)
    [1248, 1252) 'd' (line 436)
    [1264, 1268) 'd' (line 436)
    [1280, 1284) 'd' (line 436)
    [1296, 1300) 'd' (line 436)
    [1312, 1316) 'd' (line 436)
    [1328, 1332) 'd' (line 436)
    [1344, 1348) 'd' (line 436)
    [1360, 1364) 'd' (line 436)
    [1376, 1380) 'd' (line 436)
    [1392, 1396) 'd' (line 436)
    [1408, 1412) 'd' (line 436)
    [1424, 1428) 'd' (line 436)
    [1440, 1444) 'd' (line 436)
    [1456, 1460) 'd' (line 436)
    [1472, 1476) 'd' (line 436)
    [1488, 1496) 'x' (line 449)
    [1520, 1528) 'y' (line 449)
    [1552, 1560) 'z' (line 449)
    [1584, 1592) 'xx' (line 701)
    [1616, 1624) 'xy' (line 701)
    [1648, 1656) 'yy' (line 701)
    [1680, 1688) 'xz' (line 701)
    [1712, 1720) 'yz' (line 701)
    [1744, 1752) 'zz' (line 701)
    [1776, 1784) '<unknown>'
    [1808, 1816) '<unknown>'
    [1840, 1848) '<unknown>'
    [1872, 1880) 'x' (line 848)
    [1904, 1912) 'y' (line 848)
    [1936, 1944) 'z' (line 848)
    [1968, 1976) 'x' (line 868)
    [2000, 2008) 'y' (line 868)
    [2032, 2040) 'z' (line 868)
    [2064, 2072) '<unknown>'
    [2096, 2104) '<unknown>'
    [2128, 2136) '<unknown>'
    [2160, 2168) '<unknown>'
    [2192, 2200) '<unknown>'
    [2224, 2232) 'wavelength' (line 1114)
    [2256, 2264) 'force' (line 1115)
    [2288, 2296) 's' (line 1127)
    [2320, 2328) 's' (line 1138)
    [2352, 2360) 's' (line 1149)
    [2384, 2392) '<unknown>'
    [2416, 2424) '<unknown>'
    [2448, 2456) '<unknown>'
    [2480, 2488) '<unknown>'
    [2512, 2524) 'vs' (line 451)
    [2544, 2556) 'vs2' (line 451)
    [2576, 2588) 'Scomponents' (line 461)
    [2608, 2620) 'vconf' (line 474)
    [2640, 2652) 'coordinates' (line 475)
    [2672, 2684) 'confDimensions' (line 482)
    [2704, 2716) 'confEnergies' (line 483)
    [2736, 2748) 'confForces' (line 484)
    [2768, 2780) 'Lx' (line 487)
    [2800, 2812) 'Frequencies' (line 488)
    [2832, 2844) 'Intensities' (line 488)
    [2864, 2876) 'RotConsts' (line 490)
    [2896, 2908) 'Forces' (line 499)
    [2928, 2940) 'Wavelengths' (line 499)
    [2960, 2972) 'EDipole' (line 499)
    [2992, 3004) 'RotatoryStrengthsVelocity' (line 500)
    [3024, 3036) 'RotatoryStrengthsLength' (line 500)
    [3056, 3068) 'orbitals' (line 503)
    [3088, 3100) 'symmetries' (line 504)
    [3120, 3132) 'MPA_q' (line 748)
    [3152, 3164) '<unknown>'
    [3184, 3196) 'HPA_q' (line 796)
    [3216, 3228) 'CM5_q' (line 797)
    [3248, 3260) '<unknown>'
    [3280, 3292) '<unknown>'
    [3312, 3324) 'ESP_q' (line 927)
    [3344, 3356) '<unknown>'
    [3376, 3388) 'vib1' (line 997)
    [3408, 3420) 'vib2' (line 997)
    [3440, 3452) 'vib3' (line 997)
    [3472, 3484) '<unknown>'
    [3504, 3516) '<unknown>'
    [3536, 3548) '<unknown>'
    [3568, 3580) 'betaOrbitals' (line 1304)
    [3600, 3612) 'betaSymmetries' (line 1305)
    [3632, 3644) '<unknown>'
    [3664, 3676) '<unknown>'
    [3696, 3708) '<unknown>'
    [3728, 3740) '<unknown>'
    [3760, 3772) '<unknown>'
    [3792, 3804) '<unknown>'
    [3824, 3836) '<unknown>'
    [3856, 3868) '<unknown>'
    [3888, 3904) '<unknown>'
    [3920, 3944) 'str' (line 448)
    [3984, 4008) 'str1' (line 448)
    [4048, 4072) 'str2' (line 448)
    [4112, 4136) 'thermo_method' (line 448)
    [4176, 4200) 'chargeModel' (line 455)
    [4240, 4264) 'comment' (line 543)
    [4304, 4328) '<unknown>'
    [4368, 4392) '<unknown>'
    [4432, 4456) '<unknown>'
    [4496, 4520) '<unknown>'
    [4560, 4584) '<unknown>'
    [4624, 4648) '<unknown>'
    [4688, 4712) '<unknown>'
    [4752, 4776) '<unknown>'
    [4816, 4840) '<unknown>'
    [4880, 4904) '<unknown>'
    [4944, 4968) '<unknown>'
    [5008, 5032) '<unknown>'
    [5072, 5096) '<unknown>'
    [5136, 5160) '<unknown>'
    [5200, 5224) '<unknown>'
    [5264, 5288) '<unknown>'
    [5328, 5352) '<unknown>'
    [5392, 5416) '<unknown>'
    [5456, 5480) '<unknown>'
    [5520, 5544) '<unknown>'
    [5584, 5608) '<unknown>'
    [5648, 5672) '<unknown>'
    [5712, 5736) 'label' (line 1059)
    [5776, 5800) '<unknown>'
    [5840, 5864) '<unknown>'
    [5904, 5928) '<unknown>'
    [5968, 5992) 'shift' (line 1172)
    [6032, 6056) '<unknown>'
    [6096, 6120) '<unknown>'
    [6160, 6184) 'search' (line 1262)
    [6224, 6248) 'mymeth' (line 1263)
    [6288, 6312) 'myindex' (line 1264)
    [6352, 6376) '<unknown>'
    [6416, 6440) '<unknown>'
    [6480, 6504) '<unknown>'
    [6544, 6568) '<unknown>'
    [6608, 6632) '<unknown>'
    [6672, 6696) '<unknown>'
    [6736, 6760) '<unknown>'
    [6800, 6824) '<unknown>'
    [6864, 6888) '<unknown>'
    [6928, 6952) '<unknown>'
    [6992, 7016) '<unknown>'
    [7056, 7080) '<unknown>'
    [7120, 7144) '<unknown>'
    [7184, 7208) '<unknown>'
    [7248, 7273) 'coords_type' (line 510) <== Memory access at offset 7273 overflows this variable
    [7312, 7384) 'translationVectors' (line 495)
    [7424, 7496) 'Q' (line 680)
    [7536, 7608) 'quad' (line 689)
    [7648, 7720) '<unknown>'
    [7760, 7832) 'Q' (line 707)
    [7872, 7944) 'pol' (line 716)
    [7984, 8056) '<unknown>'
    [8096, 40864) 'buffer' (line 447)
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
      (longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827 in __interceptor_memcpy
Shadow bytes around the buggy address:
  0x3fffe620: 00 f2 f2 f2 f2 f2 00 00 00 f2 f2 f2 f2 f2 00 00
  0x3fffe630: 00 f2 f2 f2 f2 f2 00 00 00 f2 f2 f2 f2 f2 00 00
  0x3fffe640: 00 f2 f2 f2 f2 f2 00 00 00 f2 f2 f2 f2 f2 00 00
  0x3fffe650: 00 f2 f2 f2 f2 f2 00 00 00 f2 f2 f2 f2 f2 00 00
  0x3fffe660: 00 f2 f2 f2 f2 f2 00 00 00 f2 f2 f2 f2 f2 00 00
=>0x3fffe670: 00 f2 f2 f2 f2 f2 00 00 00[01]f2 f2 f2 f2 00 00
  0x3fffe680: 00 00 00 00 00 00 00 f2 f2 f2 f2 f2 00 00 00 00
  0x3fffe690: 00 00 00 00 00 f2 f2 f2 f2 f2 00 00 00 00 00 00
  0x3fffe6a0: 00 00 00 f2 f2 f2 f2 f2 00 00 00 00 00 00 00 00
  0x3fffe6b0: 00 f2 f2 f2 f2 f2 00 00 00 00 00 00 00 00 00 f2
  0x3fffe6c0: f2 f2 f2 f2 00 00 00 00 00 00 00 00 00 f2 f2 f2
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
VENDOR RESPONSE

Since the maintainer of this software did not release a patch during the 90 day window specified in our policy, we have now decided to release the information regarding this vulnerability, to make users of the software aware of this problem. See Cisco’s Coordinated Vulnerability Disclosure Policy for more information: https://tools.cisco.com/security/center/resources/vendor_vulnerability_policy.html

TIMELINE

2022-12-20 - Initial Vendor Contact
2023-01-12 - Vendor Disclosure
2023-07-21 - Public Release

Credit

Discovered by Claudio Bozzato of Cisco Talos.