From owner-dev-commits-src-main@freebsd.org Tue Jul 6 08:54:40 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E6114653BBC; Tue, 6 Jul 2021 08:54:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4GJxFX62dZz4mRX; Tue, 6 Jul 2021 08:54:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id B69BF16E06; Tue, 6 Jul 2021 08:54:40 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1668seIO023704; Tue, 6 Jul 2021 08:54:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1668seVc023703; Tue, 6 Jul 2021 08:54:40 GMT (envelope-from git) Date: Tue, 6 Jul 2021 08:54:40 GMT Message-Id: <202107060854.1668seVc023703@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Edward Tomasz Napierala Subject: git: 13aa56fcd596 - main - cam(4): preserve alloc_flags when copying CCBs MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: trasz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 13aa56fcd59674cd65afc8e9d6b0c15d32bf9f81 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Jul 2021 08:54:41 -0000 The branch main has been updated by trasz: URL: https://cgit.FreeBSD.org/src/commit/?id=13aa56fcd59674cd65afc8e9d6b0c15d32bf9f81 commit 13aa56fcd59674cd65afc8e9d6b0c15d32bf9f81 Author: Edward Tomasz Napierala AuthorDate: 2021-07-06 08:23:25 +0000 Commit: Edward Tomasz Napierala CommitDate: 2021-07-06 08:27:22 +0000 cam(4): preserve alloc_flags when copying CCBs Before UMA CCBs, all CCBs were of the same size, and could be trivially copied using bcopy(9). Now we have to preserve alloc_flags, otherwise we might end up attempting to free stack-allocated CCB to UMA; we also need to take CCB size into account. This fixes kernel panic which would occur when trying to access a stopped (as in, SCSI START STOP, also "ctladm stop") SCSI device. Reported By: Gary Jennejohn Tested By: Gary Jennejohn Reviewed By: imp Sponsored by: NetApp, Inc. Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D31054 --- sys/cam/cam_periph.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/sys/cam/cam_periph.c b/sys/cam/cam_periph.c index 3e1a19936825..90ddf261cb12 100644 --- a/sys/cam/cam_periph.c +++ b/sys/cam/cam_periph.c @@ -1354,6 +1354,7 @@ camperiphdone(struct cam_periph *periph, union ccb *done_ccb) cam_status status; struct scsi_start_stop_unit *scsi_cmd; int error = 0, error_code, sense_key, asc, ascq; + u_int16_t done_flags; scsi_cmd = (struct scsi_start_stop_unit *) &done_ccb->csio.cdb_io.cdb_bytes; @@ -1422,8 +1423,21 @@ camperiphdone(struct cam_periph *periph, union ccb *done_ccb) * blocking by that also any new recovery attempts for this CCB, * and the result will be the final one returned to the CCB owher. */ + + /* + * Copy the CCB back, preserving the alloc_flags field. Things + * will crash horribly if the CCBs are not of the same size. + */ saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr; - bcopy(saved_ccb, done_ccb, sizeof(*done_ccb)); + KASSERT(saved_ccb->ccb_h.func_code == XPT_SCSI_IO, + ("%s: saved_ccb func_code %#x != XPT_SCSI_IO", + __func__, saved_ccb->ccb_h.func_code)); + KASSERT(done_ccb->ccb_h.func_code == XPT_SCSI_IO, + ("%s: done_ccb func_code %#x != XPT_SCSI_IO", + __func__, done_ccb->ccb_h.func_code)); + done_flags = done_ccb->ccb_h.alloc_flags; + bcopy(saved_ccb, done_ccb, sizeof(struct ccb_scsiio)); + done_ccb->ccb_h.alloc_flags = done_flags; xpt_free_ccb(saved_ccb); if (done_ccb->ccb_h.cbfcnp != camperiphdone) periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG; @@ -1619,6 +1633,7 @@ camperiphscsisenseerror(union ccb *ccb, union ccb **orig, struct cam_periph *periph; union ccb *orig_ccb = ccb; int error, recoveryccb; + u_int16_t flags; #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL) @@ -1713,7 +1728,13 @@ camperiphscsisenseerror(union ccb *ccb, union ccb **orig, * this freeze will be dropped as part of ERESTART. */ ccb->ccb_h.status &= ~CAM_DEV_QFRZN; - bcopy(ccb, orig_ccb, sizeof(*orig_ccb)); + + KASSERT(ccb->ccb_h.func_code == XPT_SCSI_IO, + ("%s: ccb func_code %#x != XPT_SCSI_IO", + __func__, ccb->ccb_h.func_code)); + flags = orig_ccb->ccb_h.alloc_flags; + bcopy(ccb, orig_ccb, sizeof(struct ccb_scsiio)); + orig_ccb->ccb_h.alloc_flags = flags; } switch (err_action & SS_MASK) {