Skip to content

Commit

Permalink
rbd: fixed all potential crashing when decoding volume ID failed
Browse files Browse the repository at this point in the history
Signed-off-by: HF <crazytaxii666@gmail.com>
  • Loading branch information
crazytaxii authored and mergify[bot] committed Sep 6, 2023
1 parent 97df4e7 commit 5411a69
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
36 changes: 30 additions & 6 deletions internal/csi-addons/rbd/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 5 additions & 1 deletion internal/rbd/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 5411a69

Please sign in to comment.