Skip to content

Commit a2e0bdc

Browse files
authored
Merge pull request #452 from frobware/fix-nil-pointer-access-getBpfAppState
Fix SEGV (nil pointer access) in getBpfAppState logging functions
2 parents ac025cc + cef90e8 commit a2e0bdc

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

controllers/bpfman-agent/cl_application_program.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,7 @@ func (r *ClBpfApplicationReconciler) getBpfAppState(ctx context.Context) (*bpfma
547547

548548
switch len(appProgramList.Items) {
549549
case 1:
550-
// We got exactly one BpfApplicationState, so update r.currentAppState
551-
r.Logger.V(1).Info("Found BpfApplicationState", "Name", r.currentAppState.Name)
550+
r.Logger.V(1).Info("Found BpfApplicationState", "Name", appProgramList.Items[0].Name)
552551
return &appProgramList.Items[0], nil
553552
case 0:
554553
// No BpfApplicationState found, so return nil

controllers/bpfman-agent/cl_application_program_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,55 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3838
)
3939

40+
func TestClBpfApplicationReconcilerGetBpfAppState(t *testing.T) {
41+
s := scheme.Scheme
42+
s.AddKnownTypes(bpfmaniov1alpha1.SchemeGroupVersion, &bpfmaniov1alpha1.ClusterBpfApplication{})
43+
s.AddKnownTypes(bpfmaniov1alpha1.SchemeGroupVersion, &bpfmaniov1alpha1.ClusterBpfApplicationState{})
44+
s.AddKnownTypes(bpfmaniov1alpha1.SchemeGroupVersion, &bpfmaniov1alpha1.ClusterBpfApplicationStateList{})
45+
46+
// Create a mock ClusterBpfApplicationState that will be
47+
// found.
48+
mockAppState := &bpfmaniov1alpha1.ClusterBpfApplicationState{
49+
ObjectMeta: metav1.ObjectMeta{
50+
Name: "test-app-state",
51+
Labels: map[string]string{
52+
internal.BpfAppStateOwner: "test-app",
53+
internal.K8sHostLabel: "test-node",
54+
},
55+
},
56+
}
57+
58+
objs := []runtime.Object{mockAppState}
59+
cl := fake.NewClientBuilder().WithRuntimeObjects(objs...).Build()
60+
cli := agenttestutils.NewBpfmanClientFake()
61+
62+
rc := ReconcilerCommon{
63+
Client: cl,
64+
Scheme: s,
65+
BpfmanClient: cli,
66+
NodeName: "test-node",
67+
}
68+
69+
r := &ClBpfApplicationReconciler{
70+
ReconcilerCommon: rc,
71+
}
72+
73+
// Set currentApp (required for getBpfAppState).
74+
r.currentApp = &bpfmaniov1alpha1.ClusterBpfApplication{
75+
ObjectMeta: metav1.ObjectMeta{
76+
Name: "test-app",
77+
},
78+
}
79+
80+
// Test getBpfAppState when currentAppState is nil.
81+
ctx := context.Background()
82+
result, err := r.getBpfAppState(ctx)
83+
84+
require.NoError(t, err)
85+
require.NotNil(t, result)
86+
require.Equal(t, "test-app-state", result.Name)
87+
}
88+
4089
func TestClBpfApplicationControllerCreate(t *testing.T) {
4190
var (
4291
// global config

controllers/bpfman-agent/ns_application_program.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,7 @@ func (r *NsBpfApplicationReconciler) getBpfAppState(ctx context.Context) (*bpfma
490490

491491
switch len(appProgramList.Items) {
492492
case 1:
493-
// We got exactly one BpfApplicationState, so update r.currentAppState
494-
r.Logger.V(1).Info("Found BpfApplicationState", "Name", r.currentAppState.Name)
493+
r.Logger.V(1).Info("Found BpfApplicationState", "Name", appProgramList.Items[0].Name)
495494
return &appProgramList.Items[0], nil
496495
case 0:
497496
// No BpfApplicationState found, so return nil

controllers/bpfman-agent/ns_application_program_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,55 @@ import (
3737
"k8s.io/client-go/kubernetes/scheme"
3838
)
3939

40+
func TestNsBpfApplicationReconcilerGetBpfAppState(t *testing.T) {
41+
s := scheme.Scheme
42+
s.AddKnownTypes(bpfmaniov1alpha1.SchemeGroupVersion, &bpfmaniov1alpha1.BpfApplication{})
43+
s.AddKnownTypes(bpfmaniov1alpha1.SchemeGroupVersion, &bpfmaniov1alpha1.BpfApplicationState{})
44+
s.AddKnownTypes(bpfmaniov1alpha1.SchemeGroupVersion, &bpfmaniov1alpha1.BpfApplicationStateList{})
45+
46+
// Create a mock BpfApplicationState that will be found.
47+
mockAppState := &bpfmaniov1alpha1.BpfApplicationState{
48+
ObjectMeta: metav1.ObjectMeta{
49+
Name: "test-app-state",
50+
Namespace: "test-namespace",
51+
Labels: map[string]string{
52+
internal.BpfAppStateOwner: "test-app",
53+
internal.K8sHostLabel: "test-node",
54+
},
55+
},
56+
}
57+
58+
objs := []runtime.Object{mockAppState}
59+
cl := fake.NewClientBuilder().WithRuntimeObjects(objs...).Build()
60+
cli := agenttestutils.NewBpfmanClientFake()
61+
62+
rc := ReconcilerCommon{
63+
Client: cl,
64+
Scheme: s,
65+
BpfmanClient: cli,
66+
NodeName: "test-node",
67+
}
68+
69+
r := &NsBpfApplicationReconciler{
70+
ReconcilerCommon: rc,
71+
}
72+
73+
// Set currentApp (required for getBpfAppState).
74+
r.currentApp = &bpfmaniov1alpha1.BpfApplication{
75+
ObjectMeta: metav1.ObjectMeta{
76+
Name: "test-app",
77+
Namespace: "test-namespace",
78+
},
79+
}
80+
81+
ctx := context.Background()
82+
result, err := r.getBpfAppState(ctx)
83+
84+
require.NoError(t, err)
85+
require.NotNil(t, result)
86+
require.Equal(t, "test-app-state", result.Name)
87+
}
88+
4089
func TestNsBpfApplicationControllerCreate(t *testing.T) {
4190
var (
4291
// global config

0 commit comments

Comments
 (0)