upbound / function-cidr

Composition Function that transforms CIDRs
Apache License 2.0
4 stars 3 forks source link

Segmanetation fault when overflowing available cidr range in `cidrsubnets` function #30

Closed mproffitt closed 1 month ago

mproffitt commented 1 month ago

What happened?

When using the cidrsubnets function, if the number of newBits overflows the available range of the prefix CIDR it causes the pod to crash with a segmentation fault:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x139993a]

goroutine 25 [running]:
github.com/crossplane/function-sdk-go/response.Fatal(0xc0001c8720, {0x0?, 0x0?})
        /go/pkg/mod/github.com/crossplane/function-sdk-go@v0.2.0/response/response.go:106 +0xda
main.(*Function).RunFunction(0xc000428920, {0x152d600?, 0xc00016c9a0?}, 0xc0001c8060)
        /fn/fn.go:182 +0x1398
github.com/crossplane/function-sdk-go/proto/v1beta1._FunctionRunnerService_RunFunction_Handler({0x152d600, 0xc000428920}, {0x193a948, 0xc000200e70}, 0xc0005aa280, 0x0)
        /go/pkg/mod/github.com/crossplane/function-sdk-go@v0.2.0/proto/v1beta1/run_function_grpc.pb.go:104 +0x1a6
google.golang.org/grpc.(*Server).processUnaryRPC(0xc0001e0e00, {0x193a948, 0xc000200600}, {0x1941080, 0xc000388680}, 0xc0005b0120, 0xc00059e8d0, 0x243c0f0, 0x0)
        /go/pkg/mod/google.golang.org/grpc@v1.61.0/server.go:1385 +0xdd1
google.golang.org/grpc.(*Server).handleStream(0xc0001e0e00, {0x1941080, 0xc000388680}, 0xc0005b0120)
        /go/pkg/mod/google.golang.org/grpc@v1.61.0/server.go:1796 +0xfb8
google.golang.org/grpc.(*Server).serveStreams.func2.1()
        /go/pkg/mod/google.golang.org/grpc@v1.61.0/server.go:1029 +0x8b
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 45
        /go/pkg/mod/google.golang.org/grpc@v1.61.0/server.go:1040 +0x125

This stack is caused by wrapping err when err is not defined at line 182

https://github.com/upbound/function-cidr/blob/a1d3d3b63419584fc1736613bdbca7e6518979d6/fn.go#L180-L184

How can we reproduce it?

Set the cidrBlock to a /24 in the cidrsubnets example, and set the newBits field to anything that would cause the range to overflow, e.g.

    cidrBlock: 10.0.0.0/24
    newBits: [
      3, 3, 3, # 3 x /27
      3, 3, 3, # 3 x /27
      4, 4, 4, # 3 x /28
      4, 4, 4, # 3 x /28
      4, 4, 4  # 3 x /28
    ]