Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upPort parsing can overflow (TOB-K8S-015: Overflows when using strconv.Atoi and downcasting the result) #81121
Comments
|
/sig network |
|
/triage unresolved Comment |
|
/assign @thockin |
|
Is there any static checker that can find such cases, ideally with a way to decorate audited sites? Something a la |
|
We should maybe move ParsePort() to k/utils/net and then use that everywhere. |
|
I have re-titled this. I started the ball rolling. This is a great bug for someone who isn't super familiar with the codebase and wants to explore a bit. The only hard part is re-vendoring k/utils and that is not hard. |
|
I'd like to work on this. I'm someone who isn't super familiar with the codebase and wants to explore a bit. |
|
/triage unresolved Comment |
|
/assign |
|
i would like to work on this, let me know if you still need help! |
|
It looks like @vegemitecheesetoast poked first, so please give them a while
to have first crack at it.
Thanks!
Who knew there was so much pent up demand to contribute! I will make it my
personal mission to file more good-first-issue issues.
Tim
…On Sat, Aug 10, 2019, 10:12 AM elieser1101 ***@***.***> wrote:
i would like to work on this, let me know if you still need help!
—
You are receiving this because you were assigned.
Reply to this email directly, view it on GitHub
<#81121?email_source=notifications&email_token=ABKWAVARKK6OPMEDVTFMIW3QD3ZG7A5CNFSM4IKFUZWKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4ARPRA#issuecomment-520165316>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABKWAVHBOCYERVFVRTPY3ULQD3ZG7ANCNFSM4IKFUZWA>
.
|
|
@thockin got it. So thanks, i am able to start contributing but would like to take some |
|
This is my commit with just a few files just to see if I'm on the right track. Would greatly appreciate if someone can check this out and steer me in the right direction if need be. |
|
I run a little variant analysis of this bug class on kubernetes and all their staging packages and I thought you might find the result useful: "call to Atoi","Atoi() used in combination of a int wrapper could cause overflow - ","/home/nico/Semmle/Projects/Kubernetes/staging/apimachinery/revision-2019-August-10--09-40-29/src/pkg/util/intstr/intstr.go@63:46-63:48" |
|
thockin@ let me know that we've added a util function for this: kubernetes/utils#107 Step one is to re-vendor this lib and then use that function: I'm still trying to work out what re-vendoring is and figure out how to do this. Will report back in a few days when I get some progress. |
|
Hey ya!
|
|
My first PR was merged to change the utils for this, now I will work on migrating the logic that uses ParseInt to ParsePort in utils. Hopefully should get to that this weekend. |
|
Ping! @vegemitecheesetoast are you still interested in finishing this one up? I'm an Outreachy Applicant and would love to finish what has been started here. |
|
Still open -- anyone actively working on this? Would like to attempt if not. :) |
|
If it is ports which should be in range 0 to 65535 most sense to me makes unit16 but strconv.ParseUint returns uint64 which could also be a option. ParseUint has a limit on bitsize which can be set to 16 to force port be in correct range link to playground: https://play.golang.org/p/bWlmTBuwKNt
golang "net" package has also unexported function called parsePort with comment:
Don't know if this applies here but maybe ? On the other hand in the net package there is a strict >0 < 65535 requirement. LookupPort method.
So imho eg:
should be fine in this case. |
|
hi! if there is any more work to be done on this, I would love to help. |
|
very interesting . focus on this topic |
|
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
|
/lifecycle frozen |
|
I think this should be covered by #89120 /close |
|
@cmluciano: Closing this issue. In response to this:
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. |
This issue was reported in the Kubernetes Security Audit Report
Description
The strconv.Atoi function parses an int - a machine dependent integer type, which, for 64-bit targets will be int64. There are places throughout the codebase where the result returned from strconv.Atoi is later converted to a smaller type: int16 or int32. This may overflow with a certain input. An example of the issue has been included in Figure 1.
Figure 13.1: pkg/cloudprovider/providers/azure/azure_managedDiskController.go:105
Additionally, there are many code paths that parse ports, and do so differently and in a manner lacking checks for a proper port range. An example of this has been identified within kubectl when handling port values.
Kubectl has the ability to expose particular Pod ports through the use of kubectl expose. This command uses the function updatePodPorts, which uses strconv.Atoi to parse a string into an integer, then downcasts it to an int32 (Figure 2).
Figure 13.2: Relevant snippet of the updatePodPorts function.
This error has been operationalized into a crash within kubectl when overflowing provided ports. Starting with a standard deployment with no services, we can observe the expected behavior (Figure 3).
Figure 13.3: The deployment spec with service and Pod status.
To trigger the overflow, we can now update the deployment through the kubectl expose command with an overflown port, overflowing from 4294967377 to 81 (Figure 4).
Figure 13.4: Overflowing the port parameter.
We are now able to observe this overflown port when listing the services with kubectl get services (Figure 5). We are also able to access the service on the overflown port (Figure 6).
Figure 13.5: The overflown port got exposed.
Figure 13.6: The result of curling the overflown service port.
Furthering this issue, we are able to also overflow the target port. After deleting the service, we can attempt to overflow the target port as well, which will result in a panic in kubectl (Figure 7 and 8).
Figure 13.7: The deletion of the deployment.
Figure 13.8: The panic in kubectl when overflowing the target port.
Despite the panic from kubectl (visible in Figure 8), the service is still exposed (Figure 9) and accessible (Figure 10).
Figure 13.9: The service is exposed despite the kubectl panic and overflow.
Figure 13.10: The service is also accessible after overflow.
Exploit Scenario
A value is parsed from a configuration file with Atoi, resulting in an integer. It is then downcasted to a lower precision value, resulting in a potential overflow or underflow which is not raised as an error or panic.
Recommendation
Short term, when parsing strings into fixed-width integer types, use strconv.ParseInt or strconv.ParseUint with appropriate bitSize argument instead of strconv.Atoi.
Long term, ensure the validity of data and types. Parse and validate values with common functions. For example the ParsePort (cmd/kubeadm/app/util/endpoint.go:117) utility function parses and validates TCP port values, but it is not well used across the codebase.
Anything else we need to know?:
See #81146 for current status of all issues created from these findings.
The vendor gave this issue an ID of TOB-K8S-015 and it was finding 13 of the report.
The vendor considers this issue Medium Severity.
To view the original finding, begin on page 42 of the Kubernetes Security Review Report
Environment: