Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent host access on VIP addresses in proxy-mode=ipvs #108460

Merged
merged 1 commit into from
Sep 1, 2022

Conversation

uablrek
Copy link
Contributor

@uablrek uablrek commented Mar 2, 2022

What type of PR is this?

/kind bug

What this PR does / why we need it:

In proxy-mode=ipvs ports on the host, e.g. ssh:22, can be accessed from an external machine using an external VIP address or a service address.

Which issue(s) this PR fixes:

Fixes #72236

Special notes for your reviewer:

An attempt at solving this according to #72236 (comment)

  • Create and maintain a KUBE-IPVS0-IPS ipset (hash:ip) that holds all addresses assigned to kube-ipvs0
  • Add iptables rules as below
  • Test really hard every case where both kubelet and kube-proxy updates the INPUT filter chain
iptables -A INPUT -m set --match-set KUBE-LOAD-BALANCER dst,dst -j ACCEPT
iptables -A INPUT -m set --match-set KUBE-CLUSTER-IP dst,dst -j ACCEPT
iptables -A INPUT -m set --match-set KUBE-IPVS0-IPS dst -j REJECT

I am very uncertain about the division of iptables handling between kubelet and kube-proxy. The filter-INPUT chain is from what I can see only updated by kubelet. Please advice on how to update it from kube-proxy without messing up.

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot
Copy link
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. kind/bug Categorizes issue or PR as related to a bug. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Mar 2, 2022
@k8s-ci-robot
Copy link
Contributor

@uablrek: This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Mar 2, 2022
@k8s-ci-robot k8s-ci-robot requested review from dcbw and thockin March 2, 2022 17:18
@k8s-ci-robot k8s-ci-robot added area/ipvs sig/network Categorizes an issue or PR as relevant to SIG Network. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Mar 2, 2022
@uablrek
Copy link
Contributor Author

uablrek commented Mar 2, 2022

/sig network

@uablrek
Copy link
Contributor Author

uablrek commented Mar 2, 2022

@andrewsykim Please review and advise about the update of the filter-INPUT chain.

The update to create a KUBE-IPVS0-IPS ipset was much smaller than I feared 😄

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Mar 3, 2022
@uablrek
Copy link
Contributor Author

uablrek commented Mar 3, 2022

Unfortunately there is more to it. When the iptables rules are applied access to the host ports is rejected ok, but access to the service from main netns on the node doesn't work 😞

This is because traffic from main netns gets the VIP address as source. Example for 10.0.0.0 as VIP;

# ip ro show table local
local 10.0.0.0 dev kube-ipvs0 proto kernel scope host src 10.0.0.0 
...

This causes reply traffic with dest 10.0.0.0:ephemeral-port to be rejected.

@uablrek
Copy link
Contributor Author

uablrek commented Mar 3, 2022

Adding a ctstate solves the problem;

# iptables -S INPUT 
-P INPUT ACCEPT
-A INPUT -m comment --comment "kubernetes health check rules" -j KUBE-NODE-PORT
-A INPUT -j KUBE-FIREWALL
-A INPUT -m set --match-set KUBE-LOAD-BALANCER dst,dst -j ACCEPT
-A INPUT -m set --match-set KUBE-CLUSTER-IP dst,dst -j ACCEPT
-A INPUT -m conntrack --ctstate NEW -m set --match-set KUBE-IPVS0-IPS dst -j REJECT --reject-with icmp-port-unreachable

But are all cases covered? How about UDP?

@uablrek
Copy link
Contributor Author

uablrek commented Mar 3, 2022

/test all

@uablrek
Copy link
Contributor Author

uablrek commented Mar 3, 2022

/test pull-kubernetes-verify

@uablrek
Copy link
Contributor Author

uablrek commented Mar 3, 2022

/test pull-kubernetes-e2e-gci-gce-ipvs

1 similar comment
@uablrek
Copy link
Contributor Author

uablrek commented Mar 3, 2022

/test pull-kubernetes-e2e-gci-gce-ipvs

@uablrek uablrek marked this pull request as ready for review March 3, 2022 15:11
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 3, 2022
@k8s-ci-robot k8s-ci-robot requested a review from freehan March 3, 2022 15:12
@uablrek
Copy link
Contributor Author

uablrek commented Mar 4, 2022

/assign @andrewsykim

@uablrek uablrek requested a review from danwinship June 24, 2022 08:37
@uablrek
Copy link
Contributor Author

uablrek commented Jun 24, 2022

Now it looks like this;

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
KUBE-IPVS-FILTER  all  --  anywhere             anywhere             /* kubernetes ipvs access filter */
KUBE-PROXY-FIREWALL  all  --  anywhere             anywhere             /* kube-proxy firewall rules */
KUBE-NODE-PORT  all  --  anywhere             anywhere             /* kubernetes health check rules */
KUBE-FIREWALL  all  --  anywhere             anywhere            
...

We have both KUBE-PROXY-FIREWALL and KUBE-IPVS-FILTER. I was thinking of re-use KUBE-PROXY-FIREWALL but it is also called from the FORWARD chain, so I didn't.

@danwinship
Copy link
Contributor

The "drop packets to ipvs0 addresses" rule wouldn't be useful in FORWARD, but it would be harmless...

@uablrek
Copy link
Contributor Author

uablrek commented Jun 24, 2022

@danwinship Should I remove KUBE-IPVS-FILTER and use KUBE-PROXY-FIREWALL?

It would be cleaner, but almost everything passes FORWARD, so...?

@Jc2k
Copy link

Jc2k commented Jul 6, 2022

I don't have a vote here, but my gut says to keep them broken out unless there is a strong reason not to. For the reasons you said, but also observability/debugging if something has gone wrong. I think it is quite clean as is, and mixing it into another chain seems a step backward.

(Not a contributor to kube-proxy, just a rando that has been worrying about this security bug from the sidelines).

@thockin thockin removed their assignment Jul 18, 2022
@@ -1779,6 +1790,22 @@ func (proxier *Proxier) writeIptablesRules() {
"-j", "ACCEPT",
)

// Add rules to the filter/KUBE-IPVS-FILTER chain to prevent access to ports on the host through VIP addresses.
// https://github.com/kubernetes/kubernetes/issues/72236
proxier.filterRules.Write(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danwinship We have historically NOT used ipset in iptables mode, but maybe that's something we should consider?

Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 1, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: danwinship, thockin, uablrek

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 1, 2022
@k8s-ci-robot k8s-ci-robot merged commit 9924814 into kubernetes:master Sep 1, 2022
@k8s-ci-robot k8s-ci-robot added this to the v1.26 milestone Sep 1, 2022
@uablrek uablrek deleted the issue-72236 branch September 2, 2022 04:34
uablrek added a commit to uablrek/kubernetes that referenced this pull request Nov 16, 2023
uablrek added a commit to uablrek/kubernetes that referenced this pull request Nov 16, 2023
uablrek added a commit to uablrek/kubernetes that referenced this pull request Dec 14, 2023
richabanker pushed a commit to richabanker/kubernetes that referenced this pull request Jan 9, 2024
atwamahmoud pushed a commit to atwamahmoud/kubernetes that referenced this pull request Jan 10, 2024
jiahuif pushed a commit to jiahuif-forks/kubernetes that referenced this pull request Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/ipvs cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. release-note-none Denotes a PR that doesn't merit a release note. sig/network Categorizes an issue or PR as relevant to SIG Network. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

In IPVS mode, host services become available on ClusterIPs
7 participants