From c3d99aa175a6db6c479fd2a486d0c63c8f0c0aed Mon Sep 17 00:00:00 2001 From: gatici Date: Thu, 5 Dec 2024 22:11:01 +0300 Subject: [PATCH 1/4] Fix potential problems that may cause a panic during context setup Signed-off-by: gatici --- gmm/sm.go | 42 +++++++++++++++++++++++++++++++++++------- producer/ue_context.go | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 65 insertions(+), 19 deletions(-) diff --git a/gmm/sm.go b/gmm/sm.go index afeb1009..76a68952 100644 --- a/gmm/sm.go +++ b/gmm/sm.go @@ -369,9 +369,17 @@ func SecurityMode(state *fsm.State, event fsm.EventType, args fsm.ArgsType) { func ContextSetup(state *fsm.State, event fsm.EventType, args fsm.ArgsType) { switch event { case fsm.EntryEvent: - amfUe := args[ArgAmfUe].(*context.AmfUe) + amfUe, ok := args[ArgAmfUe].(*context.AmfUe) + if !ok { + logger.GmmLog.Errorln("Invalid type assertion for ArgAmfUe") + return + } gmmMessage := args[ArgNASMessage] - accessType := args[ArgAccessType].(models.AccessType) + accessType, ok := args[ArgAccessType].(models.AccessType) + if !ok { + logger.GmmLog.Errorln("Invalid type assertion for ArgAccessType") + return + } amfUe.GmmLog.Debugln("EntryEvent at GMM State[ContextSetup]") amfUe.PublishUeCtxtInfo() switch message := gmmMessage.(type) { @@ -397,9 +405,21 @@ func ContextSetup(state *fsm.State, event fsm.EventType, args fsm.ArgsType) { logger.GmmLog.Errorf("UE state mismatch: receieve wrong gmm message") } case GmmMessageEvent: - amfUe := args[ArgAmfUe].(*context.AmfUe) - gmmMessage := args[ArgNASMessage].(*nas.GmmMessage) - accessType := args[ArgAccessType].(models.AccessType) + amfUe, ok := args[ArgAmfUe].(*context.AmfUe) + if !ok { + logger.GmmLog.Errorln("Invalid type assertion for ArgAmfUe") + return + } + gmmMessage, ok := args[ArgNASMessage].(*nas.GmmMessage) + if !ok { + logger.GmmLog.Errorln("Invalid type assertion for ArgNASMessage") + return + } + accessType, ok := args[ArgAccessType].(models.AccessType) + if !ok { + logger.GmmLog.Errorln("Invalid type assertion for ArgAccessType") + return + } amfUe.GmmLog.Debugln("GmmMessageEvent at GMM State[ContextSetup]") switch gmmMessage.GetMessageType() { case nas.MsgTypeIdentityResponse: @@ -448,8 +468,16 @@ func ContextSetup(state *fsm.State, event fsm.EventType, args fsm.ArgsType) { logger.GmmLog.Debugln(event) case NwInitiatedDeregistrationEvent: logger.GmmLog.Debugln(event) - amfUe := args[ArgAmfUe].(*context.AmfUe) - accessType := args[ArgAccessType].(models.AccessType) + amfUe, ok := args[ArgAmfUe].(*context.AmfUe) + if !ok { + logger.GmmLog.Errorln("Invalid type assertion for ArgAmfUe") + return + } + accessType, ok := args[ArgAccessType].(models.AccessType) + if !ok { + logger.GmmLog.Errorln("Invalid type assertion for ArgAccessType") + return + } amfUe.T3550.Stop() amfUe.T3550 = nil amfUe.State[accessType].Set(context.Registered) diff --git a/producer/ue_context.go b/producer/ue_context.go index 81f3b560..49adb58b 100644 --- a/producer/ue_context.go +++ b/producer/ue_context.go @@ -564,11 +564,18 @@ func AssignEbiDataProcedure(ueContextID string, assignEbiData models.AssignEbiDa } } -// TS 29.518 5.2.2.2.2 +// HandleRegistrationStatusUpdateRequest TS 29.518 5.2.2.2.2 func HandleRegistrationStatusUpdateRequest(request *httpwrapper.Request) *httpwrapper.Response { logger.CommLog.Info("Handle Registration Status Update Request") - ueRegStatusUpdateReqData := request.Body.(models.UeRegStatusUpdateReqData) + ueRegStatusUpdateReqData, ok := request.Body.(models.UeRegStatusUpdateReqData) + if !ok { + problemDetails := &models.ProblemDetails{ + Status: http.StatusBadRequest, + Cause: "INVALID_BODY_FORMAT", + } + return httpwrapper.NewResponse(http.StatusBadRequest, nil, problemDetails) + } ueContextID := request.Params["ueContextId"] amfSelf := context.AMF_Self() @@ -579,7 +586,7 @@ func HandleRegistrationStatusUpdateRequest(request *httpwrapper.Request) *httpwr Status: http.StatusNotFound, Cause: "CONTEXT_NOT_FOUND", } - return httpwrapper.NewResponse(http.StatusForbidden, nil, problemDetails) + return httpwrapper.NewResponse(http.StatusNotFound, nil, problemDetails) } sbiMsg := context.SbiMsg{ UeContextId: ueContextID, @@ -592,17 +599,28 @@ func HandleRegistrationStatusUpdateRequest(request *httpwrapper.Request) *httpwr ue.EventChannel.SubmitMessage(sbiMsg) msg, read := <-sbiMsg.Result if !read { - return httpwrapper.NewResponse(http.StatusNoContent, nil, nil) - } - if msg.RespData != nil { - ueRegStatusUpdateRspData = msg.RespData.(*models.UeRegStatusUpdateRspData) + problemDetails := &models.ProblemDetails{ + Status: http.StatusNoContent, + Cause: "MESSAGE_NOT_RECEIVED", + } + return httpwrapper.NewResponse(http.StatusNoContent, nil, problemDetails) } - // ueRegStatusUpdateRspData, problemDetails := RegistrationStatusUpdateProcedure(ueContextID, ueRegStatusUpdateReqData) - if msg.ProblemDetails != nil { - return httpwrapper.NewResponse(int(msg.ProblemDetails.(*models.ProblemDetails).Status), nil, msg.ProblemDetails.(*models.ProblemDetails)) - } else { - return httpwrapper.NewResponse(http.StatusOK, nil, ueRegStatusUpdateRspData) + ueRegStatusUpdateRspData, ok = msg.RespData.(*models.UeRegStatusUpdateRspData) + if !ok { + if msg.ProblemDetails != nil { + if problemDetails, ok := msg.ProblemDetails.(*models.ProblemDetails); ok { + return httpwrapper.NewResponse(int(problemDetails.Status), nil, problemDetails) + } + } else { + // Handle unexpected response data type + problemDetails := &models.ProblemDetails{ + Status: http.StatusInternalServerError, + Cause: "UNEXPECTED_RESPONSE_TYPE", + } + return httpwrapper.NewResponse(http.StatusInternalServerError, nil, problemDetails) + } } + return httpwrapper.NewResponse(http.StatusOK, nil, ueRegStatusUpdateRspData) } func RegistrationStatusUpdateProcedure(ueContextID string, ueRegStatusUpdateReqData models.UeRegStatusUpdateReqData) ( From c85f2f70ed2f47e95af58b21fb23aede5df4facb Mon Sep 17 00:00:00 2001 From: gatici Date: Fri, 6 Dec 2024 10:20:22 +0300 Subject: [PATCH 2/4] improve the logic in HandleRegistrationStatusUpdateRequest Signed-off-by: gatici --- producer/ue_context.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/producer/ue_context.go b/producer/ue_context.go index 49adb58b..5e9b5d82 100644 --- a/producer/ue_context.go +++ b/producer/ue_context.go @@ -611,14 +611,14 @@ func HandleRegistrationStatusUpdateRequest(request *httpwrapper.Request) *httpwr if problemDetails, ok := msg.ProblemDetails.(*models.ProblemDetails); ok { return httpwrapper.NewResponse(int(problemDetails.Status), nil, problemDetails) } - } else { - // Handle unexpected response data type - problemDetails := &models.ProblemDetails{ - Status: http.StatusInternalServerError, - Cause: "UNEXPECTED_RESPONSE_TYPE", - } - return httpwrapper.NewResponse(http.StatusInternalServerError, nil, problemDetails) } + // Handle unexpected response data type + problemDetails := &models.ProblemDetails{ + Status: http.StatusInternalServerError, + Cause: "UNEXPECTED_RESPONSE_TYPE", + } + return httpwrapper.NewResponse(http.StatusInternalServerError, nil, problemDetails) + } return httpwrapper.NewResponse(http.StatusOK, nil, ueRegStatusUpdateRspData) } From e2c3649aa9c4b649e1be6a1f36a1aec30edaf6e3 Mon Sep 17 00:00:00 2001 From: gatici Date: Fri, 6 Dec 2024 10:49:02 +0300 Subject: [PATCH 3/4] fix linting Signed-off-by: gatici --- producer/ue_context.go | 1 - 1 file changed, 1 deletion(-) diff --git a/producer/ue_context.go b/producer/ue_context.go index 5e9b5d82..a795dd44 100644 --- a/producer/ue_context.go +++ b/producer/ue_context.go @@ -618,7 +618,6 @@ func HandleRegistrationStatusUpdateRequest(request *httpwrapper.Request) *httpwr Cause: "UNEXPECTED_RESPONSE_TYPE", } return httpwrapper.NewResponse(http.StatusInternalServerError, nil, problemDetails) - } return httpwrapper.NewResponse(http.StatusOK, nil, ueRegStatusUpdateRspData) } From 27a6b46ca062122e8860c38e16bab83c97500091 Mon Sep 17 00:00:00 2001 From: gatici Date: Mon, 9 Dec 2024 10:07:48 +0300 Subject: [PATCH 4/4] Start log messages with lowercase letter Signed-off-by: gatici --- gmm/sm.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/gmm/sm.go b/gmm/sm.go index 76a68952..4de1100d 100644 --- a/gmm/sm.go +++ b/gmm/sm.go @@ -371,13 +371,13 @@ func ContextSetup(state *fsm.State, event fsm.EventType, args fsm.ArgsType) { case fsm.EntryEvent: amfUe, ok := args[ArgAmfUe].(*context.AmfUe) if !ok { - logger.GmmLog.Errorln("Invalid type assertion for ArgAmfUe") + logger.GmmLog.Errorln("invalid type assertion for ArgAmfUe") return } gmmMessage := args[ArgNASMessage] accessType, ok := args[ArgAccessType].(models.AccessType) if !ok { - logger.GmmLog.Errorln("Invalid type assertion for ArgAccessType") + logger.GmmLog.Errorln("invalid type assertion for ArgAccessType") return } amfUe.GmmLog.Debugln("EntryEvent at GMM State[ContextSetup]") @@ -407,17 +407,17 @@ func ContextSetup(state *fsm.State, event fsm.EventType, args fsm.ArgsType) { case GmmMessageEvent: amfUe, ok := args[ArgAmfUe].(*context.AmfUe) if !ok { - logger.GmmLog.Errorln("Invalid type assertion for ArgAmfUe") + logger.GmmLog.Errorln("invalid type assertion for ArgAmfUe") return } gmmMessage, ok := args[ArgNASMessage].(*nas.GmmMessage) if !ok { - logger.GmmLog.Errorln("Invalid type assertion for ArgNASMessage") + logger.GmmLog.Errorln("invalid type assertion for ArgNASMessage") return } accessType, ok := args[ArgAccessType].(models.AccessType) if !ok { - logger.GmmLog.Errorln("Invalid type assertion for ArgAccessType") + logger.GmmLog.Errorln("invalid type assertion for ArgAccessType") return } amfUe.GmmLog.Debugln("GmmMessageEvent at GMM State[ContextSetup]") @@ -470,12 +470,12 @@ func ContextSetup(state *fsm.State, event fsm.EventType, args fsm.ArgsType) { logger.GmmLog.Debugln(event) amfUe, ok := args[ArgAmfUe].(*context.AmfUe) if !ok { - logger.GmmLog.Errorln("Invalid type assertion for ArgAmfUe") + logger.GmmLog.Errorln("invalid type assertion for ArgAmfUe") return } accessType, ok := args[ArgAccessType].(models.AccessType) if !ok { - logger.GmmLog.Errorln("Invalid type assertion for ArgAccessType") + logger.GmmLog.Errorln("invalid type assertion for ArgAccessType") return } amfUe.T3550.Stop()