Index Out Of Bounds in protobuf unmarshalling
The gogo/protobuf code generator produced unmarshalling code that lacks a check for integer overflow when adding a skip length to the index. An attacker could send a crafted protobuf message that causes the index to become negative, leading to an out-of-bounds read and a panic that crashes the node.
- Fixed in 57 days
- Paid in 3 months
- Disclosed after 8 months
Report Submission Form
Summary:
I have recently discovered a bug in the gogo/protobuf code generator. This bug allows for an index out of bounds when unmarshalling certain protobuf objects. The bug is that a check is lacking when skipping certain bytes. There are numerous occurrences of this bug (too many to count easily) the following is one such case.
In staging/src/k8s.io/api/certificates/v1beta1/generated.pb.go
1686: skippy, err := skipGenerated(dAtA[iNdEx:])
1690: if skippy < 0 {
1693: if (iNdEx + skippy) > postIndex {
1696: iNdEx += skippy
Here the issue may occur since iNdEx is an int the following iNdEx += skippy may overflow causing a negative value. Next time the dAtA[iNdEx] occurs it will cause an index out of bounds and the program will panic.
Since the bug is so wide spread I have not fully analysed the different impacts but since this appears in many APIs it would likely lead to crashing nodes.
Patch:
The code should have the checks to match the following as seen in the same file staging/src/k8s.io/api/certificates/v1beta1/generated.pb.go
1736: skippy, err := skipGenerated(dAtA[iNdEx:])
1740: if skippy < 0 {
1743: if (iNdEx + skippy) < 0 {
1746: if (iNdEx + skippy) > l {
1749: iNdEx += skippy
Specifically the check if (iNdEx + skippy) < 0
Note: I have contracted the maintainers of gogo/protobuf and they have a patch and will make a release soon. After that it is recommended to re-generate all of the existing protobuf code. Alternatively if waiting for a release is too long then the patch may be applied manually OR I can create a patched version of gogo/protobuf.
Kubernetes Version:
v1.20.2
Component Version:
n/a
Steps To Reproduce:
I have not generated a PoC as the bug was very simple to explain but happy to do so upon request.
Supporting Material/References:
n/a
Impact
Attackers will be able to crash nodes which use the affected protobuf code arbitrarily.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the generated unmarshalling code does not check whether adding the skip length to the current index causes an integer overflow, allowing the index to become negative.
Impact
Attackers could crash nodes that use the affected protobuf code by sending a crafted message that triggers the overflow and panic.
What the attacker needed
Ability to send crafted protobuf messages
The fix
The code should be fixed by adding a check that the sum of the index and skip length is not negative, as shown in the patched example, and then regenerating all protobuf code.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When a program reads data from the network, it uses an index to track its position. If the program adds a large number to that index without checking, the index can wrap around to a negative value. Then, when the program tries to read at that negative position, it crashes. This crash can take down the whole service.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.