From 05aeaeb122ac1de0b111bc36331af247147d7a25 Mon Sep 17 00:00:00 2001 From: HF Date: Wed, 6 Sep 2023 05:06:06 -0400 Subject: [PATCH] rbd: fixed all potential crashing when decoding volume ID failed Signed-off-by: HF --- internal/csi-addons/rbd/replication.go | 36 +++++++++++++++++++++----- internal/rbd/controllerserver.go | 6 ++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/internal/csi-addons/rbd/replication.go b/internal/csi-addons/rbd/replication.go index 197bb822b4d..a5c400de32f 100644 --- a/internal/csi-addons/rbd/replication.go +++ b/internal/csi-addons/rbd/replication.go @@ -243,7 +243,11 @@ func (rs *ReplicationServer) EnableVolumeReplication(ctx context.Context, defer rs.VolumeLocks.Release(volumeID) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) - defer rbdVol.Destroy() + defer func() { + if rbdVol != nil { + rbdVol.Destroy() + } + }() if err != nil { switch { case errors.Is(err, corerbd.ErrImageNotFound): @@ -305,7 +309,11 @@ func (rs *ReplicationServer) DisableVolumeReplication(ctx context.Context, defer rs.VolumeLocks.Release(volumeID) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) - defer rbdVol.Destroy() + defer func() { + if rbdVol != nil { + rbdVol.Destroy() + } + }() if err != nil { switch { case errors.Is(err, corerbd.ErrImageNotFound): @@ -376,7 +384,11 @@ func (rs *ReplicationServer) PromoteVolume(ctx context.Context, defer rs.VolumeLocks.Release(volumeID) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) - defer rbdVol.Destroy() + defer func() { + if rbdVol != nil { + rbdVol.Destroy() + } + }() if err != nil { switch { case errors.Is(err, corerbd.ErrImageNotFound): @@ -472,7 +484,11 @@ func (rs *ReplicationServer) DemoteVolume(ctx context.Context, defer rs.VolumeLocks.Release(volumeID) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) - defer rbdVol.Destroy() + defer func() { + if rbdVol != nil { + rbdVol.Destroy() + } + }() if err != nil { switch { case errors.Is(err, corerbd.ErrImageNotFound): @@ -585,7 +601,11 @@ func (rs *ReplicationServer) ResyncVolume(ctx context.Context, } defer rs.VolumeLocks.Release(volumeID) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) - defer rbdVol.Destroy() + defer func() { + if rbdVol != nil { + rbdVol.Destroy() + } + }() if err != nil { switch { case errors.Is(err, corerbd.ErrImageNotFound): @@ -798,7 +818,11 @@ func (rs *ReplicationServer) GetVolumeReplicationInfo(ctx context.Context, } defer rs.VolumeLocks.Release(volumeID) rbdVol, err := corerbd.GenVolFromVolID(ctx, volumeID, cr, req.GetSecrets()) - defer rbdVol.Destroy() + defer func() { + if rbdVol != nil { + rbdVol.Destroy() + } + }() if err != nil { switch { case errors.Is(err, corerbd.ErrImageNotFound): diff --git a/internal/rbd/controllerserver.go b/internal/rbd/controllerserver.go index 6283ac771c8..e632518fec4 100644 --- a/internal/rbd/controllerserver.go +++ b/internal/rbd/controllerserver.go @@ -1082,7 +1082,11 @@ func (cs *ControllerServer) CreateSnapshot( // Fetch source volume information rbdVol, err := GenVolFromVolID(ctx, req.GetSourceVolumeId(), cr, req.GetSecrets()) - defer rbdVol.Destroy() + defer func() { + if rbdVol != nil { + rbdVol.Destroy() + } + }() if err != nil { switch { case errors.Is(err, ErrImageNotFound):