CVE-2016-6175 (Other, published 2017-02-07T15:59Z)
Description: value: Eval injection vulnerability in php-gettext 1.0.12 and earlier allows remote attackers to execute arbitrary PHP code via a crafted plural forms header.
Severity: HIGH (CVSS v3 base score: 9.8)
Weakness: CWE-94 \u2014 Improper Control of Generation of Code ('Code Injection')
The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
Repository: https://github.com/NagVis/nagvis
Files changed:
ChangeLog
share/server/core/ext/php-gettext-1.0.12/gettext.php
Commit message: Clarified change log on php-gettext 1.0.12 update


Vulnerable code:
  * FIX: A local exploitable security issue has been discovered in the php-gettext
    library which is used to make NagVis localizable on platforms which has no gettext
    support built into PHP.

    A local user with privileges to create/modify the .mo files used by NagVis
    could modify a file in a way that it executes custom code with the privileges of
    the web server which is executing the NagVis code.

    To more than 99% of NagVis installations (installed with the install.sh
    installer or OMD) this is no special threat because a user which is able to .mo
    files is able to modify the .php program files of NagVis. An attacker
    would rather modify the .php files directly instead of injecting it's code through
    the .mo files.

    However, to fix this for most installations minor issue, we included php-gettext
    in version 1.0.12 with this release which fixes the issue.
  function sanitize_plural_expression($expr) {
    // Get rid of disallowed characters.
    $expr = preg_replace('@[^a-zA-Z0-9_:;\(\)\?\|\&=!<>+*/\%-]@', '', $expr);

    // Add parenthesis for tertiary '?' operator.
    $expr .= ';';
    $res = '';
    $p = 0;
    for ($i = 0; $i < strlen($expr); $i++) {
      $ch = $expr[$i];
      switch ($ch) {
      case '?':
        $res .= ' ? (';
        $p++;
        break;
      case ':':
        $res .= ') : (';
        break;
      case ';':
        $res .= str_repeat( ')', $p) . ';';
        $p = 0;
        break;
      default:
        $res .= $ch;
      }
    }
    return $res;
  }
  function extract_plural_forms_header_from_po_header($header) {
    if (preg_match("/(^|\n)plural-forms: ([^\n]*)\n/i", $header, $regs))
      $expr = $regs[2];
    else
      $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
    return $expr;
  }
  function get_plural_forms() {
    // lets assume message number 0 is header
    // this is true, right?
    $this->load_tables();

    // cache header field for plural forms
    if (! is_string($this->pluralheader)) {
      if ($this->enable_cache) {
        $header = $this->cache_translations[""];
      } else {
        $header = $this->get_translation_string(0);
      }
      $expr = $this->extract_plural_forms_header_from_po_header($header);
      $this->pluralheader = $this->sanitize_plural_expression($expr);
    }
    return $this->pluralheader;
  }
  function select_string($n) {
    if (!is_int($n)) {
      throw new InvalidArgumentException(
        "Select_string only accepts integers: " . $n);
    }
    $string = $this->get_plural_forms();
    $string = str_replace('nplurals',"\$total",$string);
    $string = str_replace("n",$n,$string);
    $string = str_replace('plural',"\$plural",$string);

    $total = 0;
    $plural = 0;

    eval("$string");
    if ($plural >= $total) $plural = $total - 1;
    return $plural;
  }
  function ngettext($single, $plural, $number) {
    if ($this->short_circuit) {
      if ($number != 1)
        return $plural;
      else
        return $single;
    }

    // find out the appropriate form
    $select = $this->select_string($number);

    // this should contains all strings separated by NULLs
    $key = $single . chr(0) . $plural;


    if ($this->enable_cache) {
      if (! array_key_exists($key, $this->cache_translations)) {
        return ($number != 1) ? $plural : $single;
      } else {
        $result = $this->cache_translations[$key];
        $list = explode(chr(0), $result);
        return $list[$select];
      }
    } else {
      $num = $this->find_string($key);
      if ($num == -1) {
        return ($number != 1) ? $plural : $single;
      } else {
        $result = $this->get_translation_string($num);
        $list = explode(chr(0), $result);
        return $list[$select];
      }
    }
  }
  function npgettext($context, $singular, $plural, $number) {
    $key = $context . chr(4) . $singular;
    $ret = $this->ngettext($key, $plural, $number);
    if (strpos($ret, "\004") !== FALSE) {
      return $singular;
    } else {
      return $ret;
    }
  }

Fixed code:
  * FIX: Updated php-gettext to 1.0.12 and commented out unused code because it contains
    some exploitable PHP code which could be used to inject code using modified .mo files.
    However, NagVis has never used these functions, so was never affected by this issue.
  //function sanitize_plural_expression($expr) {
  //  // Get rid of disallowed characters.
  //  $expr = preg_replace('@[^a-zA-Z0-9_:;\(\)\?\|\&=!<>+*/\%-]@', '', $expr);

  //  // Add parenthesis for tertiary '?' operator.
  //  $expr .= ';';
  //  $res = '';
  //  $p = 0;
  //  for ($i = 0; $i < strlen($expr); $i++) {
  //    $ch = $expr[$i];
  //    switch ($ch) {
  //    case '?':
  //      $res .= ' ? (';
  //      $p++;
  //      break;
  //    case ':':
  //      $res .= ') : (';
  //      break;
  //    case ';':
  //      $res .= str_repeat( ')', $p) . ';';
  //      $p = 0;
  //      break;
  //    default:
  //      $res .= $ch;
  //    }
  //  }
  //  return $res;
  //}
  //function extract_plural_forms_header_from_po_header($header) {
  //  if (preg_match("/(^|\n)plural-forms: ([^\n]*)\n/i", $header, $regs))
  //    $expr = $regs[2];
  //  else
  //    $expr = "nplurals=2; plural=n == 1 ? 0 : 1;";
  //  return $expr;
  //}
  //function get_plural_forms() {
  //  // lets assume message number 0 is header
  //  // this is true, right?
  //  $this->load_tables();

  //  // cache header field for plural forms
  //  if (! is_string($this->pluralheader)) {
  //    if ($this->enable_cache) {
  //      $header = $this->cache_translations[""];
  //    } else {
  //      $header = $this->get_translation_string(0);
  //    }
  //    print($header);
  //    $expr = $this->extract_plural_forms_header_from_po_header($header);
  //    $this->pluralheader = $this->sanitize_plural_expression($expr);
  //  }
  //  return $this->pluralheader;
  //}
  //function select_string($n) {
  //  if (!is_int($n)) {
  //    throw new InvalidArgumentException(
  //      "Select_string only accepts integers: " . $n);
  //  }
  //  $string = $this->get_plural_forms();
  //  $string = str_replace('nplurals',"\$total",$string);
  //  $string = str_replace("n",$n,$string);
  //  $string = str_replace('plural',"\$plural",$string);

  //  $total = 0;
  //  $plural = 0;

  //  eval("$string");
  //  if ($plural >= $total) $plural = $total - 1;
  //  return $plural;
  //}
  //function ngettext($single, $plural, $number) {
  //  if ($this->short_circuit) {
  //    if ($number != 1)
  //      return $plural;
  //    else
  //      return $single;
  //  }

  //  // find out the appropriate form
  //  $select = $this->select_string($number);

  //  // this should contains all strings separated by NULLs
  //  $key = $single . chr(0) . $plural;


  //  if ($this->enable_cache) {
  //    if (! array_key_exists($key, $this->cache_translations)) {
  //      return ($number != 1) ? $plural : $single;
  //    } else {
  //      $result = $this->cache_translations[$key];
  //      $list = explode(chr(0), $result);
  //      return $list[$select];
  //    }
  //  } else {
  //    $num = $this->find_string($key);
  //    if ($num == -1) {
  //      return ($number != 1) ? $plural : $single;
  //    } else {
  //      $result = $this->get_translation_string($num);
  //      $list = explode(chr(0), $result);
  //      return $list[$select];
  //    }
  //  }
  //}
  //function npgettext($context, $singular, $plural, $number) {
  //  $key = $context . chr(4) . $singular;
  //  $ret = $this->ngettext($key, $plural, $number);
  //  if (strpos($ret, "\004") !== FALSE) {
  //    return $singular;
  //  } else {
  //    return $ret;
  //  }
  //}