Skip to content

Commit 60340f0

Browse files
authored
fix: possible race conditions and nil dereference bugs in AMF (#1173)
Signed-off-by: Guillaume Belanger <guillaume.belanger27@gmail.com>
1 parent 61fc9a1 commit 60340f0

File tree

5 files changed

+23
-27
lines changed

5 files changed

+23
-27
lines changed

internal/amf/amf.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ func (amf *AMF) AddAmfUeToUePool(ue *AmfUe) error {
151151
func (amf *AMF) DeregisterAndRemoveAMFUE(ctx context.Context, ue *AmfUe) {
152152
ue.Deregister(ctx)
153153

154-
if ue.ranUe != nil {
155-
err := ue.ranUe.Remove()
154+
ue.Mutex.Lock()
155+
ranUe := ue.ranUe
156+
ue.Mutex.Unlock()
157+
158+
if ranUe != nil {
159+
err := ranUe.Remove()
156160
if err != nil {
157161
logger.AmfLog.Error("failed to remove RAN UE", zap.Error(err))
158162
}

internal/amf/ngap/handle_pdu_session_resource_modify.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,9 @@ func HandlePDUSessionResourceNotify(ctx context.Context, amfInstance *amf.AMF, r
5757
return
5858
}
5959

60-
var ranUe *amf.RanUe
61-
62-
ranUe = ran.FindUEByRanUeNgapID(rANUENGAPID.Value)
63-
if ranUe == nil {
64-
logger.WithTrace(ctx, ran.Log).Warn("No UE Context", zap.Int64("RanUeNgapID", rANUENGAPID.Value))
65-
}
66-
67-
ranUe = amfInstance.FindRanUeByAmfUeNgapID(aMFUENGAPID.Value)
60+
ranUe := ran.FindUEByRanUeNgapID(rANUENGAPID.Value)
6861
if ranUe == nil {
69-
logger.WithTrace(ctx, ran.Log).Warn("UE Context not found", zap.Int64("AmfUeNgapID", aMFUENGAPID.Value))
62+
logger.WithTrace(ctx, ran.Log).Error("No UE Context", zap.Int64("RanUeNgapID", rANUENGAPID.Value), zap.Int64("AmfUeNgapID", aMFUENGAPID.Value))
7063
return
7164
}
7265

internal/amf/ngap/handle_ue_context_release_complete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ func HandleUEContextReleaseComplete(ctx context.Context, amfInstance *amf.AMF, r
193193
logger.WithTrace(ctx, ran.Log).Error(err.Error())
194194
}
195195

196-
// Valid Security is not exist for this UE then only delete AMfUe Context
196+
// No valid security context exists for this UE, so delete the AMF UE context
197197
if !amfUe.SecurityContextAvailable {
198-
logger.WithTrace(ctx, ran.Log).Info("Valid Security is not exist for the UE, so deleting AmfUe Context", logger.SUPI(amfUe.Supi.String()))
198+
logger.WithTrace(ctx, ran.Log).Info("No valid security context for UE, deleting AMF UE context", logger.SUPI(amfUe.Supi.String()))
199199
amfInstance.DeregisterAndRemoveAMFUE(ctx, amfUe)
200200
}
201201
case amf.UeContextReleaseDueToNwInitiatedDeregistraion:

internal/amf/ran_ue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (ranUe *RanUe) UpdateLocation(ctx context.Context, amf *AMF, userLocationIn
346346

347347
if ranUe.amfUe != nil {
348348
ranUe.amfUe.Location = ranUe.Location
349-
ranUe.amfUe.Tai = *ranUe.amfUe.Location.NrLocation.Tai
349+
ranUe.amfUe.Tai = *ranUe.amfUe.Location.EutraLocation.Tai
350350
}
351351
case ngapType.UserLocationInformationPresentUserLocationInformationNR:
352352
locationInfoNR := userLocationInformation.UserLocationInformationNR

internal/amf/status.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ func (amf *AMF) RadioNameForSubscriber(supi etsi.SUPI) string {
2525
defer amf.mu.RUnlock()
2626

2727
ue, ok := amf.UEs[supi]
28-
if !ok || ue.GetState() != Registered || ue.ranUe == nil || ue.ranUe.Radio == nil {
28+
if !ok {
29+
return ""
30+
}
31+
32+
ue.Mutex.Lock()
33+
defer ue.Mutex.Unlock()
34+
35+
if ue.state != Registered || ue.ranUe == nil || ue.ranUe.Radio == nil {
2936
return ""
3037
}
3138

@@ -65,19 +72,11 @@ func (amf *AMF) RegisteredSubscribersForRadio(radioName string) []string {
6572
var imsis []string
6673

6774
for _, ue := range amf.UEs {
68-
if ue.GetState() != Registered {
69-
continue
70-
}
71-
72-
if ue.ranUe == nil || ue.ranUe.Radio == nil {
73-
continue
74-
}
75-
76-
if ue.ranUe.Radio.Name != radioName {
77-
continue
78-
}
75+
ue.Mutex.Lock()
76+
match := ue.state == Registered && ue.ranUe != nil && ue.ranUe.Radio != nil && ue.ranUe.Radio.Name == radioName
77+
ue.Mutex.Unlock()
7978

80-
if ue.Supi.IsValid() && ue.Supi.IsIMSI() {
79+
if match && ue.Supi.IsValid() && ue.Supi.IsIMSI() {
8180
imsis = append(imsis, ue.Supi.IMSI())
8281
}
8382
}

0 commit comments

Comments
 (0)