CVE-2016-6175: Improper Control of Generation of Code ('Code Injection') (CWE-94)
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.
CWE description: 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.
CVSS v3 base score: 9.8
Published: 2017-02-07T15:59Z
Repository: https://github.com/NagVis/nagvis
Language: Other
Files changed: ChangeLog, share/server/core/ext/php-gettext-1.0.12/gettext.php
Commit date: 2016-06-25 13:21:48 +0200
Commit message: Clarified change log on php-gettext 1.0.12 update

Diff: diff --git a/ChangeLog b/ChangeLog
index 4436a096..9b9b194c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,26 +1,13 @@
 1.9b8
 Core:
   * MK BI backend: Now prints an error message when trying to use it with
     objects that are not aggregations.
   * NDO MySQL backend: Filtering out legacy objects from DB (setting is_active filter)
-  * 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.
+  * 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.
 
 Frontend:
   * FIX: Fixed error in ad-hoc automaps (Network Topology in Check_MK) during state updates
   * FIX: Prevent JS/CSS caching issues during updates of NagVis
   * FIX: Improved error handling in case of modifying objects which
diff --git a/share/server/core/ext/php-gettext-1.0.12/gettext.php b/share/server/core/ext/php-gettext-1.0.12/gettext.php
index edbd9330..e7ca4ab5 100644
--- a/share/server/core/ext/php-gettext-1.0.12/gettext.php
+++ b/share/server/core/ext/php-gettext-1.0.12/gettext.php
@@ -273,145 +273,146 @@ class gettext_reader {
    * Sanitize plural form expression for use in PHP eval call.
    *
    * @access private
    * @return string sanitized plural form expression
    */
-  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 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;
+  //}
 
   /**
    * Parse full PO header and extract only plural forms line.
    *
    * @access private
    * @return string verbatim plural form header field
    */
-  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 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;
+  //}
 
   /**
    * Get possible plural forms from MO header
    *
    * @access private
    * @return string plural form header
    */
-  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 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;
+  //}
 
   /**
    * Detects which plural form to take
    *
    * @access private
    * @param n count
    * @return int array index of the right plural form
    */
-  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 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;
+  //}
 
   /**
    * Plural version of gettext
    *
    * @access public
    * @param string single
    * @param string plural
    * @param string number
    * @return translated plural form
    */
-  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 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 pgettext($context, $msgid) {
     $key = $context . chr(4) . $msgid;
     $ret = $this->translate($key);
     if (strpos($ret, "\004") !== FALSE) {
@@ -419,18 +420,18 @@ class gettext_reader {
     } else {
       return $ret;
     }
   }
 
-  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;
-    }
+  //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;
+  //  }
 
-  }
+  //}
 }
 
 ?>

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;
  //  }
  //}