rbd: validate IOContext before getting the list of trashed images

`ensureImageCleanup()` can cause a panic when an image was deleted, but
the journal still contained a reference. By opening the IOContext before
using, an error may be returned instead of a panic when using a `nil` or
freed IOContext.

Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
Niels de Vos 2024-10-01 15:10:27 +02:00 committed by mergify[bot]
parent 9267210da4
commit d33e6b14fe
2 changed files with 8 additions and 8 deletions

View File

@ -881,9 +881,9 @@ func (cs *ControllerServer) checkErrAndUndoReserve(
} }
if errors.Is(err, ErrImageNotFound) { if errors.Is(err, ErrImageNotFound) {
err = rbdVol.ensureImageCleanup(ctx) notFoundErr := rbdVol.ensureImageCleanup(ctx)
if err != nil { if notFoundErr != nil {
return nil, status.Error(codes.Internal, err.Error()) return nil, status.Errorf(codes.Internal, "failed to cleanup image %q: %v", rbdVol, notFoundErr)
} }
} else { } else {
// All errors other than ErrImageNotFound should return an error back to the caller // All errors other than ErrImageNotFound should return an error back to the caller
@ -1538,11 +1538,6 @@ func cleanUpImageAndSnapReservation(ctx context.Context, rbdSnap *rbdSnapshot, c
} }
defer rbdVol.Destroy(ctx) defer rbdVol.Destroy(ctx)
err = rbdVol.openIoctx()
if err != nil {
return status.Error(codes.Internal, err.Error())
}
// cleanup the image from trash if the error is image not found. // cleanup the image from trash if the error is image not found.
err = rbdVol.ensureImageCleanup(ctx) err = rbdVol.ensureImageCleanup(ctx)
if err != nil { if err != nil {

View File

@ -621,6 +621,11 @@ func isCephMgrSupported(ctx context.Context, clusterID string, err error) bool {
// ensureImageCleanup finds image in trash and if found removes it // ensureImageCleanup finds image in trash and if found removes it
// from trash. // from trash.
func (ri *rbdImage) ensureImageCleanup(ctx context.Context) error { func (ri *rbdImage) ensureImageCleanup(ctx context.Context) error {
err := ri.openIoctx()
if err != nil {
return err
}
trashInfoList, err := librbd.GetTrashList(ri.ioctx) trashInfoList, err := librbd.GetTrashList(ri.ioctx)
if err != nil { if err != nil {
log.ErrorLog(ctx, "failed to list images in trash: %v", err) log.ErrorLog(ctx, "failed to list images in trash: %v", err)