Code injection in `saved_model_cli` in TensorFlow
Description
TensorFlow is an open source platform for machine learning. Prior to versions 2.9.0, 2.8.1, 2.7.2, and 2.6.4, TensorFlow's saved_model_cli tool is vulnerable to a code injection. This can be used to open a reverse shell. This code path was maintained for compatibility reasons as the maintainers had several test cases where numpy expressions were used as arguments. However, given that the tool is always run manually, the impact of this is still not severe. The maintainers have now removed the safe=False argument, so all parsing is done without calling eval. The patch is available in versions 2.9.0, 2.8.1, 2.7.2, and 2.6.4.
Affected packages
Versions sourced from the GitHub Security Advisory.
| Package | Affected versions | Patched versions |
|---|---|---|
tensorflowPyPI | < 2.6.4 | 2.6.4 |
tensorflow-cpuPyPI | < 2.6.4 | 2.6.4 |
tensorflow-gpuPyPI | < 2.6.4 | 2.6.4 |
tensorflowPyPI | >= 2.7.0, < 2.7.2 | 2.7.2 |
tensorflowPyPI | >= 2.8.0, < 2.8.1 | 2.8.1 |
tensorflow-cpuPyPI | >= 2.7.0, < 2.7.2 | 2.7.2 |
tensorflow-cpuPyPI | >= 2.8.0, < 2.8.1 | 2.8.1 |
tensorflow-gpuPyPI | >= 2.7.0, < 2.7.2 | 2.7.2 |
tensorflow-gpuPyPI | >= 2.8.0, < 2.8.1 | 2.8.1 |
Affected products
1- Range: < 2.6.4
Patches
2c5da7af04861Always do safe parsing
2 files changed · +3 −40
tensorflow/python/tools/saved_model_cli.py+1 −1 modified@@ -684,7 +684,7 @@ def load_inputs_from_input_arg_string(inputs_str, input_exprs_str, tensor_key_feed_dict = {} inputs = preprocess_inputs_arg_string(inputs_str) - input_exprs = preprocess_input_exprs_arg_string(input_exprs_str, safe=False) + input_exprs = preprocess_input_exprs_arg_string(input_exprs_str) input_examples = preprocess_input_examples_arg_string(input_examples_str) for input_tensor_key, (filename, variable_name) in inputs.items():
tensorflow/python/tools/saved_model_cli_test.py+2 −39 modified@@ -486,43 +486,6 @@ def testInputParserPickle(self): self.assertTrue(np.all(feed_dict['y'] == pkl1)) self.assertTrue(np.all(feed_dict['z'] == pkl2)) - def testInputParserPythonExpression(self): - x1 = np.ones([2, 10]) - x2 = np.array([[1], [2], [3]]) - x3 = np.mgrid[0:5, 0:5] - x4 = [[3], [4]] - input_expr_str = ('x1=np.ones([2,10]);x2=np.array([[1],[2],[3]]);' - 'x3=np.mgrid[0:5,0:5];x4=[[3],[4]]') - feed_dict = saved_model_cli.load_inputs_from_input_arg_string( - '', input_expr_str, '') - self.assertTrue(np.all(feed_dict['x1'] == x1)) - self.assertTrue(np.all(feed_dict['x2'] == x2)) - self.assertTrue(np.all(feed_dict['x3'] == x3)) - self.assertTrue(np.all(feed_dict['x4'] == x4)) - - def testInputParserBoth(self): - x0 = np.array([[1], [2]]) - input_path = os.path.join(test.get_temp_dir(), 'input.npz') - np.savez(input_path, a=x0) - x1 = np.ones([2, 10]) - input_str = 'x0=' + input_path + '[a]' - input_expr_str = 'x1=np.ones([2,10])' - feed_dict = saved_model_cli.load_inputs_from_input_arg_string( - input_str, input_expr_str, '') - self.assertTrue(np.all(feed_dict['x0'] == x0)) - self.assertTrue(np.all(feed_dict['x1'] == x1)) - - def testInputParserBothDuplicate(self): - x0 = np.array([[1], [2]]) - input_path = os.path.join(test.get_temp_dir(), 'input.npz') - np.savez(input_path, a=x0) - x1 = np.ones([2, 10]) - input_str = 'x0=' + input_path + '[a]' - input_expr_str = 'x0=np.ones([2,10])' - feed_dict = saved_model_cli.load_inputs_from_input_arg_string( - input_str, input_expr_str, '') - self.assertTrue(np.all(feed_dict['x0'] == x1)) - def testInputParserErrorNoName(self): x0 = np.array([[1], [2]]) x1 = np.array(range(5)) @@ -629,7 +592,7 @@ def testRunCommandInvalidInputKeyError(self, use_tfrt): base_path = test.test_src_dir_path(SAVED_MODEL_PATH) args = self.parser.parse_args([ 'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def', - 'regress_x2_to_y3', '--input_exprs', 'x2=np.ones((3,1))' + 'regress_x2_to_y3', '--input_exprs', 'x2=[1,2,3]' ] + (['--use_tfrt'] if use_tfrt else [])) with self.assertRaises(ValueError): saved_model_cli.run(args) @@ -640,7 +603,7 @@ def testRunCommandInvalidSignature(self, use_tfrt): base_path = test.test_src_dir_path(SAVED_MODEL_PATH) args = self.parser.parse_args([ 'run', '--dir', base_path, '--tag_set', 'serve', '--signature_def', - 'INVALID_SIGNATURE', '--input_exprs', 'x2=np.ones((3,1))' + 'INVALID_SIGNATURE', '--input_exprs', 'x2=[1,2,3]' ] + (['--use_tfrt'] if use_tfrt else [])) with self.assertRaisesRegex(ValueError, 'Could not find signature "INVALID_SIGNATURE"'):
8b202f08d52eRemove use of `eval` when evaluating the input example.
3 files changed · +29 −10
RELEASE.md+2 −0 modified@@ -249,6 +249,8 @@ endpoint. * TF SavedModel: * Custom gradients are now saved by default. See `tf.saved_model.SaveOptions` to disable this. + * The saved_model_cli's `--input_examples` inputs are now restricted to + python literals to avoid code injection. * XLA: * Added a new API that allows custom call functions to signal errors. The old API will be deprecated in a future release. See
tensorflow/python/tools/saved_model_cli.py+19 −7 modified@@ -20,6 +20,7 @@ """ import argparse +import ast import os import re import sys @@ -521,16 +522,18 @@ def preprocess_inputs_arg_string(inputs_str): return input_dict -def preprocess_input_exprs_arg_string(input_exprs_str): +def preprocess_input_exprs_arg_string(input_exprs_str, safe=True): """Parses input arg into dictionary that maps input key to python expression. Parses input string in the format of 'input_key=<python expression>' into a dictionary that maps each input_key to its python expression. Args: input_exprs_str: A string that specifies python expression for input keys. - Each input is separated by semicolon. For each input key: + Each input is separated by semicolon. For each input key: 'input_key=<python expression>' + safe: Whether to evaluate the python expression as literals or allow + arbitrary calls (e.g. numpy usage). Returns: A dictionary that maps input keys to their values. @@ -545,8 +548,15 @@ def preprocess_input_exprs_arg_string(input_exprs_str): raise RuntimeError('--input_exprs "%s" format is incorrect. Please follow' '"<input_key>=<python expression>"' % input_exprs_str) input_key, expr = input_raw.split('=', 1) - # ast.literal_eval does not work with numpy expressions - input_dict[input_key] = eval(expr) # pylint: disable=eval-used + if safe: + try: + input_dict[input_key] = ast.literal_eval(expr) + except: + raise RuntimeError( + f'Expression "{expr}" is not a valid python literal.') + else: + # ast.literal_eval does not work with numpy expressions + input_dict[input_key] = eval(expr) # pylint: disable=eval-used return input_dict @@ -659,7 +669,7 @@ def load_inputs_from_input_arg_string(inputs_str, input_exprs_str, tensor_key_feed_dict = {} inputs = preprocess_inputs_arg_string(inputs_str) - input_exprs = preprocess_input_exprs_arg_string(input_exprs_str) + input_exprs = preprocess_input_exprs_arg_string(input_exprs_str, safe=False) input_examples = preprocess_input_examples_arg_string(input_examples_str) for input_tensor_key, (filename, variable_name) in inputs.items(): @@ -923,8 +933,10 @@ def add_run_subparser(subparsers): parser_run.add_argument('--inputs', type=str, default='', help=msg) msg = ('Specifying inputs by python expressions, in the format of' ' "<input_key>=\'<python expression>\'", separated by \';\'. ' - 'numpy module is available as \'np\'. ' - 'Will override duplicate input keys from --inputs option.') + 'numpy module is available as \'np\'. Please note that the expression ' + 'will be evaluated as-is, and is susceptible to code injection. ' + 'When this is set, the value will override duplicate input keys from ' + '--inputs option.') parser_run.add_argument('--input_exprs', type=str, default='', help=msg) msg = ( 'Specifying tf.Example inputs as list of dictionaries. For example: '
tensorflow/python/tools/saved_model_cli_test.py+8 −3 modified@@ -382,7 +382,7 @@ def testInputPreProcessFormats(self): input_expr_str = 'input3=np.zeros([2,2]);input4=[4,5]' input_dict = saved_model_cli.preprocess_inputs_arg_string(input_str) input_expr_dict = saved_model_cli.preprocess_input_exprs_arg_string( - input_expr_str) + input_expr_str, safe=False) self.assertTrue(input_dict['input1'] == ('/path/file.txt', 'ab3')) self.assertTrue(input_dict['input2'] == ('file2', None)) print(input_expr_dict['input3']) @@ -418,6 +418,11 @@ def testInputPreProcessExamplesWithStrAndBytes(self): } """, feature) + def testInputPreprocessExampleWithCodeInjection(self): + input_examples_str = 'inputs=os.system("echo hacked")' + with self.assertRaisesRegex(RuntimeError, 'not a valid python literal.'): + saved_model_cli.preprocess_input_examples_arg_string(input_examples_str) + def testInputPreProcessFileNames(self): input_str = (r'inputx=C:\Program Files\data.npz[v:0];' r'input:0=c:\PROGRA~1\data.npy') @@ -434,8 +439,8 @@ def testInputPreProcessErrorBadFormat(self): with self.assertRaises(RuntimeError): saved_model_cli.preprocess_inputs_arg_string(input_str) input_str = 'inputx:np.zeros((5))' - with self.assertRaises(RuntimeError): - saved_model_cli.preprocess_input_exprs_arg_string(input_str) + with self.assertRaisesRegex(RuntimeError, 'format is incorrect'): + saved_model_cli.preprocess_input_exprs_arg_string(input_str, safe=False) def testInputParserNPY(self): x0 = np.array([[1], [2]])
Vulnerability mechanics
Generated by null/stub on May 9, 2026. Inputs: CWE entries + fix-commit diffs from this CVE's patches. Citations validated against bundle.
References
10- github.com/advisories/GHSA-75c9-jrh4-79mcghsaADVISORY
- nvd.nist.gov/vuln/detail/CVE-2022-29216ghsaADVISORY
- github.com/tensorflow/tensorflow/blob/f3b9bf4c3c0597563b289c0512e98d4ce81f886e/tensorflow/python/tools/saved_model_cli.pyghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/commit/8b202f08d52e8206af2bdb2112a62fafbc546ec7ghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/commit/c5da7af048611aa29e9382371f0aed5018516cacghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/releases/tag/v2.6.4ghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/releases/tag/v2.7.2ghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/releases/tag/v2.8.1ghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/releases/tag/v2.9.0ghsax_refsource_MISCWEB
- github.com/tensorflow/tensorflow/security/advisories/GHSA-75c9-jrh4-79mcghsax_refsource_CONFIRMWEB
News mentions
0No linked articles in our index yet.