Troy Dawson bb7973
From 8254c266f962d5febe46396d5083bb9c1da74840 Mon Sep 17 00:00:00 2001
Troy Dawson bb7973
From: Brian Coca <brian.coca+git@gmail.com>
Troy Dawson bb7973
Date: Tue, 4 Jun 2019 08:43:15 -0400
Troy Dawson bb7973
Subject: [PATCH 1/2] just dont pass locals
Troy Dawson bb7973
Troy Dawson bb7973
 - also fix globals
Troy Dawson bb7973
 - added tests
Troy Dawson bb7973
---
Troy Dawson bb7973
 changelogs/fragments/fix_safe_eval.yml        |  2 +
Troy Dawson bb7973
 lib/ansible/template/__init__.py              |  2 +-
Troy Dawson bb7973
 lib/ansible/template/safe_eval.py             |  8 ++-
Troy Dawson bb7973
 .../targets/template/corner_cases.yml         | 51 +++++++++++++++++++
Troy Dawson bb7973
 test/integration/targets/template/runme.sh    |  4 ++
Troy Dawson bb7973
 5 files changed, 64 insertions(+), 3 deletions(-)
Troy Dawson bb7973
 create mode 100644 changelogs/fragments/fix_safe_eval.yml
Troy Dawson bb7973
 create mode 100644 test/integration/targets/template/corner_cases.yml
Troy Dawson bb7973
Troy Dawson bb7973
diff --git a/changelogs/fragments/fix_safe_eval.yml b/changelogs/fragments/fix_safe_eval.yml
Troy Dawson bb7973
new file mode 100644
Troy Dawson bb7973
index 000000000000..19220b34ffb1
Troy Dawson bb7973
--- /dev/null
Troy Dawson bb7973
+++ b/changelogs/fragments/fix_safe_eval.yml
Troy Dawson bb7973
@@ -0,0 +1,2 @@
Troy Dawson bb7973
+bugfixes:
Troy Dawson bb7973
+    - Handle improper variable substitution that was happening in safe_eval, it was always meant to just do 'type enforcement' and have Jinja2 deal with all variable interpolation. Also see CVE-2019-10156
Troy Dawson bb7973
diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py
Troy Dawson bb7973
index f88b7165db1f..ec4bf6771379 100644
Troy Dawson bb7973
--- a/lib/ansible/template/__init__.py
Troy Dawson bb7973
+++ b/lib/ansible/template/__init__.py
Troy Dawson bb7973
@@ -543,7 +543,7 @@ def template(self, variable, convert_bare=False, preserve_trailing_newlines=True
Troy Dawson bb7973
                                 # if this looks like a dictionary or list, convert it to such using the safe_eval method
Troy Dawson bb7973
                                 if (result.startswith("{") and not result.startswith(self.environment.variable_start_string)) or \
Troy Dawson bb7973
                                         result.startswith("[") or result in ("True", "False"):
Troy Dawson bb7973
-                                    eval_results = safe_eval(result, locals=self._available_variables, include_exceptions=True)
Troy Dawson bb7973
+                                    eval_results = safe_eval(result, include_exceptions=True)
Troy Dawson bb7973
                                     if eval_results[1] is None:
Troy Dawson bb7973
                                         result = eval_results[0]
Troy Dawson bb7973
                                         if unsafe:
Troy Dawson bb7973
diff --git a/lib/ansible/template/safe_eval.py b/lib/ansible/template/safe_eval.py
Troy Dawson bb7973
index 9c70be4a897d..4f5b856180af 100644
Troy Dawson bb7973
--- a/lib/ansible/template/safe_eval.py
Troy Dawson bb7973
+++ b/lib/ansible/template/safe_eval.py
Troy Dawson bb7973
@@ -42,10 +42,14 @@ def safe_eval(expr, locals=None, include_exceptions=False):
Troy Dawson bb7973
 
Troy Dawson bb7973
     # define certain JSON types
Troy Dawson bb7973
     # eg. JSON booleans are unknown to python eval()
Troy Dawson bb7973
-    JSON_TYPES = {
Troy Dawson bb7973
+    OUR_GLOBALS = {
Troy Dawson bb7973
+        '__builtins__': {},  # avoid global builtins as per eval docs
Troy Dawson bb7973
         'false': False,
Troy Dawson bb7973
         'null': None,
Troy Dawson bb7973
         'true': True,
Troy Dawson bb7973
+        # also add back some builtins we do need
Troy Dawson bb7973
+        'True': True,
Troy Dawson bb7973
+        'False': False,
Troy Dawson bb7973
     }
Troy Dawson bb7973
 
Troy Dawson bb7973
     # this is the whitelist of AST nodes we are going to
Troy Dawson bb7973
@@ -138,7 +142,7 @@ def generic_visit(self, node, inside_call=False):
Troy Dawson bb7973
         # Note: passing our own globals and locals here constrains what
Troy Dawson bb7973
         # callables (and other identifiers) are recognized.  this is in
Troy Dawson bb7973
         # addition to the filtering of builtins done in CleansingNodeVisitor
Troy Dawson bb7973
-        result = eval(compiled, JSON_TYPES, dict(locals))
Troy Dawson bb7973
+        result = eval(compiled, OUR_GLOBALS, dict(locals))
Troy Dawson bb7973
 
Troy Dawson bb7973
         if include_exceptions:
Troy Dawson bb7973
             return (result, None)
Troy Dawson bb7973
diff --git a/test/integration/targets/template/corner_cases.yml b/test/integration/targets/template/corner_cases.yml
Troy Dawson bb7973
new file mode 100644
Troy Dawson bb7973
index 000000000000..48782f79590c
Troy Dawson bb7973
--- /dev/null
Troy Dawson bb7973
+++ b/test/integration/targets/template/corner_cases.yml
Troy Dawson bb7973
@@ -0,0 +1,51 @@
Troy Dawson bb7973
+- name: test tempating corner cases
Troy Dawson bb7973
+  hosts: localhost
Troy Dawson bb7973
+  gather_facts: false
Troy Dawson bb7973
+  vars:
Troy Dawson bb7973
+    empty_list: []
Troy Dawson bb7973
+    dont: I SHOULD NOT BE TEMPLATED
Troy Dawson bb7973
+    other: I WORK
Troy Dawson bb7973
+  tasks:
Troy Dawson bb7973
+    - name: 'ensure we are not interpolating data from outside of j2 delmiters'
Troy Dawson bb7973
+      assert:
Troy Dawson bb7973
+        that:
Troy Dawson bb7973
+            - '"I SHOULD NOT BE TEMPLATED" not in adjacent'
Troy Dawson bb7973
+            - globals1 == "[[], globals()]"
Troy Dawson bb7973
+            - globals2 == "[[], globals]"
Troy Dawson bb7973
+      vars:
Troy Dawson bb7973
+        adjacent: "{{ empty_list }} + [dont]"
Troy Dawson bb7973
+        globals1: "[{{ empty_list }}, globals()]"
Troy Dawson bb7973
+        globals2: "[{{ empty_list }}, globals]"
Troy Dawson bb7973
+
Troy Dawson bb7973
+    - name: 'ensure we can add lists'
Troy Dawson bb7973
+      assert:
Troy Dawson bb7973
+        that:
Troy Dawson bb7973
+            - (empty_list + [other]) == [other]
Troy Dawson bb7973
+            - (empty_list + [other, other]) == [other, other]
Troy Dawson bb7973
+            - (dont_exist|default([]) + [other]) == [other]
Troy Dawson bb7973
+            - ([other] + [empty_list, other]) == [other, [], other]
Troy Dawson bb7973
+
Troy Dawson bb7973
+    - name: 'ensure comments go away and we still dont interpolate in string'
Troy Dawson bb7973
+      assert:
Troy Dawson bb7973
+        that:
Troy Dawson bb7973
+            - 'comm1 == " + [dont]"'
Troy Dawson bb7973
+            - 'comm2 == " #} + [dont]"'
Troy Dawson bb7973
+      vars:
Troy Dawson bb7973
+        comm1: '{# {{nothing}} {# #} + [dont]'
Troy Dawson bb7973
+        comm2: "{# {{nothing}} {# #} #} + [dont]"
Troy Dawson bb7973
+
Troy Dawson bb7973
+    - name: test additions with facts, set them up
Troy Dawson bb7973
+      set_fact:
Troy Dawson bb7973
+        inames: []
Troy Dawson bb7973
+        iname: "{{ prefix ~ '-options' }}"
Troy Dawson bb7973
+        iname_1: "{{ prefix ~ '-options-1' }}"
Troy Dawson bb7973
+      vars:
Troy Dawson bb7973
+        prefix: 'bo'
Troy Dawson bb7973
+
Troy Dawson bb7973
+    - name: add the facts
Troy Dawson bb7973
+      set_fact:
Troy Dawson bb7973
+        inames: '{{ inames + [iname, iname_1] }}'
Troy Dawson bb7973
+
Troy Dawson bb7973
+    - assert:
Troy Dawson bb7973
+        that:
Troy Dawson bb7973
+            - inames == ['bo-options', 'bo-options-1']
Troy Dawson bb7973
diff --git a/test/integration/targets/template/runme.sh b/test/integration/targets/template/runme.sh
Troy Dawson bb7973
index 2b58bc92dd3e..c4f50b1c7e28 100755
Troy Dawson bb7973
--- a/test/integration/targets/template/runme.sh
Troy Dawson bb7973
+++ b/test/integration/targets/template/runme.sh
Troy Dawson bb7973
@@ -12,3 +12,7 @@ ansible-playbook ansible_managed.yml -c  ansible_managed.cfg -i ../../inventory
Troy Dawson bb7973
 
Troy Dawson bb7973
 # Test for #42585
Troy Dawson bb7973
 ANSIBLE_ROLES_PATH=../ ansible-playbook custom_template.yml -i ../../inventory -e @../../integration_config.yml -v "$@"
Troy Dawson bb7973
+
Troy Dawson bb7973
+
Troy Dawson bb7973
+# Test for several corner cases #57188
Troy Dawson bb7973
+ansible-playbook corner_cases.yml -v "$@"
Troy Dawson bb7973
Troy Dawson bb7973
From fbda0028750a17a032d83dad9d1fb284f9ea68a4 Mon Sep 17 00:00:00 2001
Troy Dawson bb7973
From: Brian Coca <brian.coca+git@gmail.com>
Troy Dawson bb7973
Date: Thu, 6 Jun 2019 13:26:14 -0400
Troy Dawson bb7973
Subject: [PATCH 2/2] fixed tests
Troy Dawson bb7973
Troy Dawson bb7973
---
Troy Dawson bb7973
 .../targets/docker_image/tasks/tests/old-options.yml        | 2 +-
Troy Dawson bb7973
 test/integration/targets/meraki_static_route/tasks/main.yml | 6 +++---
Troy Dawson bb7973
 test/integration/targets/netapp_eseries_host/tasks/run.yml  | 4 ++--
Troy Dawson bb7973
 test/integration/targets/postgresql/tasks/main.yml          | 2 +-
Troy Dawson bb7973
 test/legacy/ovs.yaml                                        | 4 ++--
Troy Dawson bb7973
 5 files changed, 9 insertions(+), 9 deletions(-)
Troy Dawson bb7973
Troy Dawson bb7973
diff --git a/test/integration/targets/docker_image/tasks/tests/old-options.yml b/test/integration/targets/docker_image/tasks/tests/old-options.yml
Troy Dawson bb7973
index 5571cf96fabc..5824a56d1fec 100644
Troy Dawson bb7973
--- a/test/integration/targets/docker_image/tasks/tests/old-options.yml
Troy Dawson bb7973
+++ b/test/integration/targets/docker_image/tasks/tests/old-options.yml
Troy Dawson bb7973
@@ -5,7 +5,7 @@
Troy Dawson bb7973
 
Troy Dawson bb7973
 - name: Registering image name
Troy Dawson bb7973
   set_fact:
Troy Dawson bb7973
-    inames: "{{ inames }} + [iname]"
Troy Dawson bb7973
+    inames: "{{ inames + [iname]}}"
Troy Dawson bb7973
 
Troy Dawson bb7973
 ####################################################################
Troy Dawson bb7973
 ## build ###########################################################
Troy Dawson bb7973
diff --git a/test/integration/targets/meraki_static_route/tasks/main.yml b/test/integration/targets/meraki_static_route/tasks/main.yml
Troy Dawson bb7973
index 322e36f855e7..10ba31eab975 100644
Troy Dawson bb7973
--- a/test/integration/targets/meraki_static_route/tasks/main.yml
Troy Dawson bb7973
+++ b/test/integration/targets/meraki_static_route/tasks/main.yml
Troy Dawson bb7973
@@ -35,7 +35,7 @@
Troy Dawson bb7973
     register: create_route
Troy Dawson bb7973
 
Troy Dawson bb7973
   - set_fact:
Troy Dawson bb7973
-      route_ids: "{{ route_ids }} + [ '{{ create_route.data.id }}' ]"
Troy Dawson bb7973
+      route_ids: "{{ route_ids + [create_route.data.id] }}"
Troy Dawson bb7973
 
Troy Dawson bb7973
   - name: Create second static_route
Troy Dawson bb7973
     meraki_static_route:
Troy Dawson bb7973
@@ -50,7 +50,7 @@
Troy Dawson bb7973
     register: second_create
Troy Dawson bb7973
 
Troy Dawson bb7973
   - set_fact:
Troy Dawson bb7973
-      route_ids: "{{ route_ids }} + [ '{{ second_create.data.id }}' ]"
Troy Dawson bb7973
+      route_ids: "{{ route_ids + [second_create.data.id] }}"
Troy Dawson bb7973
 
Troy Dawson bb7973
   - assert:
Troy Dawson bb7973
       that:
Troy Dawson bb7973
@@ -167,4 +167,4 @@
Troy Dawson bb7973
         state: absent
Troy Dawson bb7973
         org_name: '{{test_org_name}}'
Troy Dawson bb7973
         net_name: IntTestNetwork
Troy Dawson bb7973
-      delegate_to: localhost
Troy Dawson bb7973
\ No newline at end of file
Troy Dawson bb7973
+      delegate_to: localhost
Troy Dawson bb7973
diff --git a/test/integration/targets/netapp_eseries_host/tasks/run.yml b/test/integration/targets/netapp_eseries_host/tasks/run.yml
Troy Dawson bb7973
index fd0a8d5fa209..70519b4b9423 100644
Troy Dawson bb7973
--- a/test/integration/targets/netapp_eseries_host/tasks/run.yml
Troy Dawson bb7973
+++ b/test/integration/targets/netapp_eseries_host/tasks/run.yml
Troy Dawson bb7973
@@ -204,7 +204,7 @@
Troy Dawson bb7973
   set_fact:
Troy Dawson bb7973
     port_info: []
Troy Dawson bb7973
 - set_fact:
Troy Dawson bb7973
-    port_info: "{{ port_info }} + [{{ item[0] |combine(item[1]) }}]"
Troy Dawson bb7973
+    port_info: "{{ port_info + [item[0] |combine(item[1])] }}"
Troy Dawson bb7973
   loop: "{{ tmp }}"
Troy Dawson bb7973
 
Troy Dawson bb7973
 # Compile list of expected host port information for verifying changes
Troy Dawson bb7973
@@ -225,7 +225,7 @@
Troy Dawson bb7973
   set_fact:
Troy Dawson bb7973
     expected_port_info: []
Troy Dawson bb7973
 - set_fact:
Troy Dawson bb7973
-    expected_port_info: "{{ expected_port_info }} + [{{ item[0] |combine(item[1]) }}]"
Troy Dawson bb7973
+    expected_port_info: "{{ expected_port_info + [ item[0] |combine(item[1]) ] }}"
Troy Dawson bb7973
   loop: "{{ tmp }}"
Troy Dawson bb7973
 
Troy Dawson bb7973
 # Verify that each host object has the expected protocol type and address/port
Troy Dawson bb7973
diff --git a/test/integration/targets/postgresql/tasks/main.yml b/test/integration/targets/postgresql/tasks/main.yml
Troy Dawson bb7973
index d395b2820f13..5d3a21b61e71 100644
Troy Dawson bb7973
--- a/test/integration/targets/postgresql/tasks/main.yml
Troy Dawson bb7973
+++ b/test/integration/targets/postgresql/tasks/main.yml
Troy Dawson bb7973
@@ -235,7 +235,7 @@
Troy Dawson bb7973
     - 'yes'
Troy Dawson bb7973
 
Troy Dawson bb7973
 - set_fact:
Troy Dawson bb7973
-    encryption_values: '{{ encryption_values }} + ["no"]'
Troy Dawson bb7973
+    encryption_values: '{{ encryption_values + ["no"]}}'
Troy Dawson bb7973
   when: postgres_version_resp.stdout is version('10', '<=')
Troy Dawson bb7973
 
Troy Dawson bb7973
 - include: test_password.yml
Troy Dawson bb7973
diff --git a/test/legacy/ovs.yaml b/test/legacy/ovs.yaml
Troy Dawson bb7973
index 4eff414f1de8..35d3acc0fd2f 100644
Troy Dawson bb7973
--- a/test/legacy/ovs.yaml
Troy Dawson bb7973
+++ b/test/legacy/ovs.yaml
Troy Dawson bb7973
@@ -22,7 +22,7 @@
Troy Dawson bb7973
         when: "limit_to in ['*', 'openvswitch_db']"
Troy Dawson bb7973
       rescue:
Troy Dawson bb7973
         - set_fact:
Troy Dawson bb7973
-            failed_modules: "{{ failed_modules }} + [ 'openvswitch_db' ]"
Troy Dawson bb7973
+            failed_modules: "{{ failed_modules + [ 'openvswitch_db' ]}}"
Troy Dawson bb7973
             test_failed: true
Troy Dawson bb7973
 
Troy Dawson bb7973
 
Troy Dawson bb7973
@@ -33,4 +33,4 @@
Troy Dawson bb7973
     - name: Has any previous test failed?
Troy Dawson bb7973
       fail:
Troy Dawson bb7973
         msg: "One or more tests failed, check log for details"
Troy Dawson bb7973
-      when: test_failed
Troy Dawson bb7973
\ No newline at end of file
Troy Dawson bb7973
+      when: test_failed