├── LICENSE ├── README.md ├── code └── opa-gatekeeper-taint-tolerations │ ├── README.md │ ├── constraint.yaml │ ├── constraint_template.yaml │ ├── hello-world.yaml │ ├── src.rego │ └── src_test.rego └── cves └── cve-2019-11247 ├── Dockerfile ├── README.md ├── clusterrbacconfig.yaml ├── cve-2019-11247.yaml ├── entrypoint.sh └── helloworld.yaml /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Example code from [StackRox blog posts](https://www.stackrox.com/post/) 2 | 3 | This repository contains sample code and long-form examples used in posts o 4 | [www.stackrox.com](https://www.stackrox.com) 5 | 6 | ## Table of Contents 7 | 8 | * code/ 9 | * [opa-gatekeeper-taint-tolerations/](https://stackrox.com/post/2020/05/custom-kubernetes-controls-with-open-policy-agent-opa-part-2/) 10 | * cves/ 11 | * [cve-2019-11247/](https://www.stackrox.com/post/2019/08/how-to-remediate-kubernetes-security-vulnerability-cve-2019-11247/) 12 | -------------------------------------------------------------------------------- /code/opa-gatekeeper-taint-tolerations/README.md: -------------------------------------------------------------------------------- 1 | # Open Policy Agent (OPA) Gatekeeper Taint Tolerations Policy 2 | 3 | This is a functional OPA Gatekeeper constraint policy for enforcing allowed 4 | taint tolerations in pods. 5 | 6 | Please see the [companion blog post](https://stackrox.com/post/2020/05/custom-kubernetes-controls-with-open-policy-agent-opa-part-2/) for more information on writing 7 | and testing OPA Gatekeeper constraint policies. 8 | 9 | ## Basic Usage 10 | 11 | This policy was tested against Gatekeeper release[3.1.0-beta.8](https://github.com/open-policy-agent/gatekeeper/releases/tag/v3.1.0-beta.8) 12 | 13 | 1. [Deploy OPA Gatekeeper](https://github.com/open-policy-agent/gatekeeper) in 14 | your Kubernetes cluster. 15 | 1. Apply the `ConstraintTemplate`: `kubectl apply -f constraint_template.yaml` 16 | 1. Apply the sample constraint: `kubectl apply -f constrant.yaml` 17 | 1. Apply a deployment with the forbidden taint and watch it fail: `kubectl apply -f hello-world.yaml` 18 | -------------------------------------------------------------------------------- /code/opa-gatekeeper-taint-tolerations/constraint.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: constraints.gatekeeper.sh/v1beta1 2 | kind: RestrictedTaintToleration 3 | metadata: 4 | name: privileged 5 | spec: 6 | match: 7 | kinds: 8 | - apiGroups: [""] 9 | kinds: ["Pod"] 10 | excludedNamespaces: 11 | - kube-system 12 | parameters: 13 | restrictedTaint: 14 | key: privileged 15 | effect: NoSchedule 16 | value: "true" 17 | allowGlobalToleration: false 18 | -------------------------------------------------------------------------------- /code/opa-gatekeeper-taint-tolerations/constraint_template.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: templates.gatekeeper.sh/v1beta1 2 | kind: ConstraintTemplate 3 | metadata: 4 | name: restrictedtainttoleration 5 | spec: 6 | crd: 7 | spec: 8 | names: 9 | kind: RestrictedTaintToleration 10 | listKind: RestrictedTaintToleration 11 | plural: restrictedtainttolerations 12 | singular: restrictedtainttoleration 13 | validation: 14 | openAPIV3Schema: 15 | properties: 16 | restrictedTaint: 17 | type: object 18 | properties: 19 | key: 20 | type: string 21 | value: 22 | type: string 23 | effect: 24 | type: string 25 | required: 26 | - key 27 | - value 28 | - effect 29 | allowGlobalToleration: 30 | type: boolean 31 | required: 32 | - restrictedTaint 33 | - allowGlobalToleration 34 | targets: 35 | - target: admission.k8s.gatekeeper.sh 36 | rego: | 37 | package restrictedtainttoleration 38 | 39 | # Global assignments 40 | pod := input.review.object 41 | tolerations := pod.spec.tolerations 42 | taint := input.parameters.restrictedTaint 43 | 44 | # This comprehension creates an array with an entry for 45 | # each matching toleration in the tolerations array 46 | matching_tolerations := [match | match := toleration_match(tolerations[_])] 47 | 48 | # A toleration with no key matches all taints 49 | global_tolerations := [key | k := object.get(tolerations[_], "key", "") 50 | k == "" 51 | key := k] 52 | 53 | default allow_global = false 54 | allow_global { 55 | input.parameters.allowGlobalToleration == true 56 | } 57 | 58 | # Fail if matching toleration exists 59 | violation[{"msg": msg}] { 60 | count(global_tolerations) == 0 61 | count(matching_tolerations) > 0 62 | msg := sprintf("Toleration is not allowed for taint %v", [taint]) 63 | } 64 | 65 | # Fail if global toleration exists and we disallow global tolerations for 66 | # this taint 67 | violation[{"msg": msg}] { 68 | count(global_tolerations) > 0 69 | not allow_global 70 | msg := sprintf("Global tolerations not allowed for taint %v", [taint]) 71 | } 72 | 73 | # Functions to test if pod toleration matches the taint 74 | toleration_match(toleration) { 75 | key := object.get(toleration, "key", "") 76 | key == taint.key 77 | effect := object.get(toleration, "effect", "") 78 | effect_check(effect) 79 | operator := object.get(toleration, "operator", "Equal") 80 | operator_check(toleration, operator) 81 | } 82 | 83 | # Functions can be defined multiple times, matching against non-variable args 84 | # This acts as a conditional 85 | 86 | # If effect is empty, match any 87 | effect_check("") { 88 | true 89 | } 90 | 91 | # Otherwise, specific effect must match 92 | effect_check(effect) { 93 | effect == taint.effect 94 | } 95 | 96 | # When the toleration operator is "Equal" we need to match value fields 97 | operator_check(toleration, "Equal") { 98 | value := object.get(toleration, "value", "") 99 | value == taint.value 100 | } 101 | 102 | # When the toleration operator is "Exists", the match is always true 103 | # because we already matched on key and effect 104 | operator_check(toleration, "Exists") { 105 | true 106 | } 107 | 108 | -------------------------------------------------------------------------------- /code/opa-gatekeeper-taint-tolerations/hello-world.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: hello-world 5 | labels: 6 | app: hello-world 7 | spec: 8 | selector: 9 | matchLabels: 10 | app: hello-world 11 | replicas: 1 12 | template: 13 | metadata: 14 | labels: 15 | app: hello-world 16 | spec: 17 | tolerations: 18 | - key: "privileged" 19 | value: "true" 20 | operator: "Equal" 21 | effect: "NoSchedule" 22 | containers: 23 | - name: hello-world 24 | image: gcr.io/google-samples/node-hello:1.0 25 | imagePullPolicy: Always 26 | ports: 27 | - containerPort: 8080 28 | protocol: TCP 29 | resources: 30 | limits: 31 | memory: "500Mi" 32 | requests: 33 | memory: "500Mi" 34 | -------------------------------------------------------------------------------- /code/opa-gatekeeper-taint-tolerations/src.rego: -------------------------------------------------------------------------------- 1 | package restrictedtainttoleration 2 | 3 | # Global assignments 4 | pod := input.review.object 5 | tolerations := pod.spec.tolerations 6 | taint := input.parameters.restrictedTaint 7 | 8 | # This comprehension creates an array with an entry for 9 | # each matching toleration in the tolerations array 10 | matching_tolerations := [match | match := toleration_match(tolerations[_])] 11 | 12 | # A toleration with no key matches all taints 13 | global_tolerations := [key | k := object.get(tolerations[_], "key", "") 14 | k == "" 15 | key := k] 16 | 17 | default allow_global = false 18 | allow_global { 19 | input.parameters.allowGlobalToleration == true 20 | } 21 | 22 | # Fail if matching toleration exists 23 | violation[{"msg": msg}] { 24 | count(global_tolerations) == 0 25 | count(matching_tolerations) > 0 26 | msg := sprintf("Toleration is not allowed for taint %v", [taint]) 27 | } 28 | 29 | # Fail if global toleration exists and we disallow global tolerations for 30 | # this taint 31 | violation[{"msg": msg}] { 32 | count(global_tolerations) > 0 33 | not allow_global 34 | msg := sprintf("Global tolerations not allowed for taint %v", [taint]) 35 | } 36 | 37 | # Functions to test if pod toleration matches the taint 38 | toleration_match(toleration) { 39 | key := object.get(toleration, "key", "") 40 | key == taint.key 41 | effect := object.get(toleration, "effect", "") 42 | effect_check(effect) 43 | operator := object.get(toleration, "operator", "Equal") 44 | operator_check(toleration, operator) 45 | } 46 | 47 | # Functions can be defined multiple times, matching against non-variable args 48 | # This acts as a conditional 49 | 50 | # If effect is empty, match any 51 | effect_check("") { 52 | true 53 | } 54 | 55 | # Otherwise, specific effect must match 56 | effect_check(effect) { 57 | effect == taint.effect 58 | } 59 | 60 | # When the toleration operator is "Equal" we need to match value fields 61 | operator_check(toleration, "Equal") { 62 | value := object.get(toleration, "value", "") 63 | value == taint.value 64 | } 65 | 66 | # When the toleration operator is "Exists", the match is always true 67 | # because we already matched on key and effect 68 | operator_check(toleration, "Exists") { 69 | true 70 | } 71 | 72 | -------------------------------------------------------------------------------- /code/opa-gatekeeper-taint-tolerations/src_test.rego: -------------------------------------------------------------------------------- 1 | package restrictedtainttoleration 2 | 3 | # 4 | # Test methods 5 | # 6 | 7 | test_input_no_global_violation { 8 | input := { "review": input_review_global, 9 | "parameters": input_parameters_no_global } 10 | results := violation with input as input 11 | count(results) > 0 12 | } 13 | 14 | test_input_ok_global_allow { 15 | input := { "review": input_review_global, 16 | "parameters": input_parameters_ok_global } 17 | results := violation with input as input 18 | count(results) == 0 19 | } 20 | 21 | test_input_no_global_equal_match_violation { 22 | input := { "review": input_review_global_and_equal, 23 | "parameters": input_parameters_no_global } 24 | results := violation with input as input 25 | count(results) > 0 26 | } 27 | 28 | test_input_ok_global_equal_match_allow { 29 | input := { "review": input_review_global_and_equal, 30 | "parameters": input_parameters_ok_global } 31 | results := violation with input as input 32 | count(results) == 0 33 | } 34 | 35 | test_input_equal_match_violation { 36 | input := { "review": input_review_equal, 37 | "parameters": input_parameters_ok_global } 38 | results := violation with input as input 39 | count(results) > 0 40 | } 41 | 42 | test_input_equal_no_effect_match_violation { 43 | input := { "review": input_review_no_effect, 44 | "parameters": input_parameters_ok_global } 45 | results := violation with input as input 46 | count(results) > 0 47 | } 48 | 49 | test_input_equal_no_operator_match_violation { 50 | input := { "review": input_review_no_operator, 51 | "parameters": input_parameters_ok_global } 52 | results := violation with input as input 53 | count(results) > 0 54 | } 55 | 56 | test_input_equal_no_effect_no_operator_match_violation { 57 | input := { "review": input_review_no_effect_no_operator, 58 | "parameters": input_parameters_ok_global } 59 | results := violation with input as input 60 | count(results) > 0 61 | } 62 | 63 | test_input_equal_different_value_match_allow { 64 | input := { "review": input_review_different_value, 65 | "parameters": input_parameters_ok_global } 66 | results := violation with input as input 67 | count(results) == 0 68 | } 69 | 70 | test_input_no_toleration_field_allow { 71 | input := { "review": input_review_different_value, 72 | "parameters": input_review_no_toleration_field } 73 | results := violation with input as input 74 | count(results) == 0 75 | } 76 | 77 | # 78 | # Mock objects for testing 79 | # 80 | 81 | input_review_global = { 82 | "object": { 83 | "spec": { 84 | "tolerations": [ 85 | { 86 | "operator": "Exists" 87 | } 88 | ] 89 | } 90 | } 91 | } 92 | 93 | input_review_global_and_equal = { 94 | "object": { 95 | "spec": { 96 | "tolerations": [ 97 | { 98 | "operator": "Exists" 99 | }, 100 | { 101 | "key": "taintname", 102 | "value": "taintvalue", 103 | "effect": "NoSchedule", 104 | "operator": "Equal" 105 | } 106 | ] 107 | } 108 | } 109 | } 110 | 111 | input_review_equal = { 112 | "object": { 113 | "spec": { 114 | "tolerations": [ 115 | { 116 | "key": "taintname", 117 | "value": "taintvalue", 118 | "effect": "NoSchedule", 119 | "operator": "Equal" 120 | } 121 | ] 122 | } 123 | } 124 | } 125 | 126 | input_review_no_operator = { 127 | "object": { 128 | "spec": { 129 | "tolerations": [ 130 | { 131 | "key": "taintname", 132 | "value": "taintvalue", 133 | "effect": "NoSchedule" 134 | } 135 | ] 136 | } 137 | } 138 | } 139 | 140 | input_review_no_effect = { 141 | "object": { 142 | "spec": { 143 | "tolerations": [ 144 | { 145 | "key": "taintname", 146 | "value": "taintvalue", 147 | "operator": "Equal" 148 | } 149 | ] 150 | } 151 | } 152 | } 153 | 154 | input_review_no_effect_no_operator = { 155 | "object": { 156 | "spec": { 157 | "tolerations": [ 158 | { 159 | "key": "taintname", 160 | "value": "taintvalue" 161 | } 162 | ] 163 | } 164 | } 165 | } 166 | 167 | input_review_different_value = { 168 | "object": { 169 | "spec": { 170 | "tolerations": [ 171 | { 172 | "key": "taintname", 173 | "value": "wrongvalue", 174 | "operator": "Equal", 175 | "effect": "NoSchedule" 176 | } 177 | ] 178 | } 179 | } 180 | } 181 | 182 | input_review_exists = { 183 | "object": { 184 | "spec": { 185 | "tolerations": [ 186 | { 187 | "key": "taintname", 188 | "effect": "NoSchedule", 189 | "operator": "Exists" 190 | } 191 | ] 192 | } 193 | } 194 | } 195 | 196 | input_review_no_toleration_field = { 197 | "object": { 198 | "spec": {} 199 | } 200 | } 201 | 202 | # 203 | # Mock policy configurations 204 | # 205 | 206 | input_parameters_ok_global = { 207 | "restrictedTaint": { 208 | "key": "taintname", 209 | "value": "taintvalue", 210 | "effect": "NoSchedule" 211 | }, 212 | "allowGlobalToleration": true 213 | } 214 | 215 | input_parameters_no_global = { 216 | "restrictedTaint": { 217 | "key": "taintname", 218 | "value": "taintvalue", 219 | "effect": "NoSchedule" 220 | }, 221 | "allowGlobalToleration": false 222 | } 223 | -------------------------------------------------------------------------------- /cves/cve-2019-11247/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.9 2 | 3 | ARG KUBECTL_VERSION=1.15.0 4 | 5 | RUN apk --no-cache add \ 6 | ca-certificates=20190108-r0 \ 7 | curl=7.64.0-r2 && \ 8 | curl -L https://dl.k8s.io/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl -o /usr/bin/kubectl && \ 9 | chmod +x /usr/bin/kubectl 10 | 11 | COPY entrypoint.sh /entrypoint.sh 12 | -------------------------------------------------------------------------------- /cves/cve-2019-11247/README.md: -------------------------------------------------------------------------------- 1 | CVE-2019-11247 2 | ============== 3 | 4 | Samples for testing CVE-2019-11247, a vulnerability concerning improper handling 5 | of global-scoped custom resource permissions. 6 | 7 | To deploy: 8 | ---------- 9 | 10 | Using a Kubernetes cluster with an unpatched version, (1.12.x, < 1.13.9, < 1.14.5, < 1.15.1), and Istio 1.1 or greater with mTLS and sidecar injection enabled: 11 | 12 | Note: Do not test this in a production cluster! 13 | 14 | 1. Create the `cve-2019-11247-test` image: `docker build .` 15 | 2. `docker tag /cve-2019-11247:latest`, using a repo that you can push to locally and pull from in your cluster. 16 | 3. `docker push /cve-2019-11247:latest` 17 | 4. Edit the file `cve-2019-11247.yaml`, replacing the Deployment image with the one you pushed to above. 18 | 5. Run `kubectl -R -f .` to deploy all the resources to your cluster. 19 | 6. Get a shell in the test container: `kubectl exec -it -c cve-2019-11247-test "$(kubectl get pods --selector "app=cve-2019-11247-test" -ojsonpath='{.items..metadata.name}')" -- /bin/sh` 20 | 7. Try to access the `helloworld` service (this should return a 403): `curl -D - http://helloworld.default.svc.cluster.local:5000/hello` 21 | 8. Try to delete the ClusterRbacConfig `default` resource without a namespace (this should also fail): `https://$KUBERNETES_PORT_443_TCP_ADDR:$KUBERNETES_SERVICE_PORT_HTTPS/apis/rbac.istio.io/v1alpha1/clusterrbacconfigs/default -X DELETE` 22 | 9. Try to delete the ClusterRbacConfig `default` resource with the `default` namespace (this should succeed): `https://$KUBERNETES_PORT_443_TCP_ADDR:$KUBERNETES_SERVICE_PORT_HTTPS/apis/rbac.istio.io/v1alpha1/namespaces/default/clusterrbacconfigs/default -X DELETE` 23 | 10. Try the `helloworld` service again (this should now succeed): `curl -D - http://helloworld.default.svc.cluster.local:5000/hello` 24 | -------------------------------------------------------------------------------- /cves/cve-2019-11247/clusterrbacconfig.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: "rbac.istio.io/v1alpha1" 3 | kind: ClusterRbacConfig 4 | metadata: 5 | name: default 6 | spec: 7 | # Apply to all namespaces except those exclicitly listed below 8 | mode: 'ON_WITH_EXCLUSION' 9 | exclusion: 10 | # Apply to all namespaces except istio-system and kube-system 11 | namespaces: ["istio-system","kube-system"] 12 | -------------------------------------------------------------------------------- /cves/cve-2019-11247/cve-2019-11247.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | apiVersion: v1 3 | kind: ServiceAccount 4 | metadata: 5 | name: cve-2019-11247-sa 6 | namespace: default 7 | labels: 8 | app: cve-2019-11247-test 9 | 10 | --- 11 | apiVersion: rbac.authorization.k8s.io/v1 12 | kind: Role 13 | metadata: 14 | name: cve-2019-11247-role 15 | namespace: default 16 | labels: 17 | app: cve-2019-11247-test 18 | rules: 19 | - apiGroups: ["*"] 20 | resources: ["*"] 21 | verbs: ["*"] 22 | 23 | --- 24 | apiVersion: rbac.authorization.k8s.io/v1 25 | kind: RoleBinding 26 | metadata: 27 | name: cve-2019-11247-rolebinding 28 | labels: 29 | app: cve-test 30 | roleRef: 31 | apiGroup: rbac.authorization.k8s.io 32 | kind: Role 33 | name: cve-2019-11247-role 34 | subjects: 35 | - kind: ServiceAccount 36 | name: cve-2019-11247-sa 37 | namespace: default 38 | 39 | --- 40 | apiVersion: v1 41 | kind: Service 42 | metadata: 43 | name: cve-2019-11247-test 44 | labels: 45 | app: cve-2019-11247-test 46 | spec: 47 | ports: 48 | - port: 80 49 | name: http 50 | selector: 51 | app: cve-test 52 | 53 | --- 54 | # You will need to point the image below to your registry 55 | apiVersion: extensions/v1beta1 56 | kind: Deployment 57 | metadata: 58 | name: cve-2019-11247-test 59 | spec: 60 | replicas: 1 61 | template: 62 | metadata: 63 | labels: 64 | app: cve-2019-11247-test 65 | version: v1 66 | spec: 67 | serviceAccount: cve-2019-11247-sa 68 | containers: 69 | - name: cve-2019-11247-test 70 | image: $CVE_TEST_IMAGE 71 | command: ["/entrypoint.sh"] 72 | imagePullPolicy: IfNotPresent 73 | ports: 74 | - containerPort: 80 75 | 76 | 77 | -------------------------------------------------------------------------------- /cves/cve-2019-11247/entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | SLEEP_SECONDS="${SLEEP_SECONDS:-6000}" 4 | PREFIX="${PREFIX:-}" 5 | 6 | # Create kubeconfig file 7 | kubectl config set-cluster "cluster" \ 8 | --server=kubernetes.default \ 9 | --certificate-authority=/run/secrets/kubernetes.io/serviceaccount/ca.crt \ 10 | --embed-certs=true 11 | 12 | # Someday kubectl will support file arguments for sensitive strings 13 | kubectl config set-credentials "sa" \ 14 | --token="$(< /run/secrets/kubernetes.io/serviceaccount/token)" 15 | 16 | while [ true ]; do 17 | sleep "$SLEEP_SECONDS" 18 | done 19 | -------------------------------------------------------------------------------- /cves/cve-2019-11247/helloworld.yaml: -------------------------------------------------------------------------------- 1 | # From Istio repo samples 2 | --- 3 | apiVersion: v1 4 | kind: Service 5 | metadata: 6 | name: helloworld 7 | labels: 8 | app: helloworld 9 | spec: 10 | ports: 11 | - port: 5000 12 | name: http 13 | selector: 14 | app: helloworld 15 | 16 | --- 17 | apiVersion: extensions/v1beta1 18 | kind: Deployment 19 | metadata: 20 | name: helloworld-v1 21 | spec: 22 | replicas: 1 23 | template: 24 | metadata: 25 | labels: 26 | app: helloworld 27 | version: v1 28 | spec: 29 | containers: 30 | - name: helloworld 31 | image: istio/examples-helloworld-v1 32 | resources: 33 | requests: 34 | cpu: "100m" 35 | imagePullPolicy: IfNotPresent #Always 36 | ports: 37 | - containerPort: 5000 38 | 39 | --- 40 | apiVersion: networking.istio.io/v1alpha3 41 | kind: VirtualService 42 | metadata: 43 | name: helloworld 44 | spec: 45 | hosts: 46 | - helloworld.default.svc.cluster.local 47 | gateways: 48 | - mesh 49 | http: 50 | - match: 51 | - uri: 52 | exact: /hello 53 | route: 54 | - destination: 55 | host: helloworld.default.svc.cluster.local 56 | port: 57 | number: 5000 58 | 59 | --------------------------------------------------------------------------------