From nobody Wed Sep 6 14:01:33 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4RgkZ60gRlz4skMq; Wed, 6 Sep 2023 14:01:34 +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 4RgkZ6088Yz3gcG; Wed, 6 Sep 2023 14:01:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694008894; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hKRpiqUaH3c0Ii+fvK0pM2KerDxdiKcmppqxPAkLYKk=; b=EbhWejEnd3QaPScDLsT+8xcgKpfgNRsRagZ2lHRChsmhKHSrglGJwHrCZGXu7/2QhUoUd6 TmicAMeqjQh3zZBOo2krFmmFALIYppGngjz6qhkz9kMafgdCtOenmPIqRhfVpst/bSr9XG uS7A1Y0Wuj1/+k3dlvAjOSgVwE4viZZscRIOvRQ5QOAPiu4a2fn238L+suB7QwjGUIi+5U djR24cOIcdH6UvzrOTEJa4kFoUjo5UUdoIJmNFuhpEPv1L+CsAYwFdAyXY7vQJv1iiDOfp p4w0H+mnN4y35QMpqBoVEHVdBHNXoaxhd7QqhTxfuypvr7MeqW8pj+2tY7auKw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694008894; a=rsa-sha256; cv=none; b=hmrEnF0DCiPhsn85g7jn3jFnlIrtML77pJFOYZQMUpZV8hcReScpFqVoqncTk+VbssC3Ek 2VYWhAF04ninI9BKMP576H/kSnOso6eO3PlHi0zXA31wI1OkRFStDGqONLVIbxsiK0G14e rpuzGm9xhdUzwN3FmTyPeeRoPWYqLydI0Qyf+XhNTFrZWLzxRuLFCtHQwFZ+C54N5DmQu+ Mkp/KXKGeG04jgaGfwOtIQzV21HotZGGMqcfcqRUkxCgk2eWFk+x1DmsruMxVRTjL1iI40 oq+hgDbn4JE2zPnLR3U6IQOtrvwMTlFEeb7rv8WxA5V12BAOGE2AZhHkDEZbSw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694008894; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hKRpiqUaH3c0Ii+fvK0pM2KerDxdiKcmppqxPAkLYKk=; b=WyrSblsikBZaNIxKjpesKCzQGErnGqL9MT3FGv10RyiiKWM5POQjkiLF2z67KOHI5MPyin i+BrEYMU/615GHv5ir+RHcsAdgUdfddHuBgZFpQyrrIS61jx1GL9J4Dim/yNEijEyH9pG9 wT/IdMagADk0/ws97raBtsHhNRv1ZLxJKRUPe+NH57GMUiFP52HAcP1U/uUkg4YnIzyxoc l3t+5DpeKHc7XuhpcA90l7/1GH/YSdE8EupHJAqpLmP6nfGcHcVwS/TbqnkjIsQyOo6NMS 3GHTuYS1rsWnaKa0QUrS2XmoD/OTiyjjy2RQgqZjFMwO5BVl0vGdyZa9bk2DwQ== 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 4RgkZ56LXNz4MZ; Wed, 6 Sep 2023 14:01:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 386E1XQc030803; Wed, 6 Sep 2023 14:01:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 386E1X2p030800; Wed, 6 Sep 2023 14:01:33 GMT (envelope-from git) Date: Wed, 6 Sep 2023 14:01:33 GMT Message-Id: <202309061401.386E1X2p030800@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 8527bb2aee6c - main - dtrace: Fix a kernel panic in printm() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8527bb2aee6ca9013c34445de88217a954b6628d Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=8527bb2aee6ca9013c34445de88217a954b6628d commit 8527bb2aee6ca9013c34445de88217a954b6628d Author: Domagoj Stolfa AuthorDate: 2023-09-06 13:25:00 +0000 Commit: Mark Johnston CommitDate: 2023-09-06 14:00:59 +0000 dtrace: Fix a kernel panic in printm() When using printm(), one should always pass a scratch pointer to it. This is achieved by calling printm with memref BEGIN { printm(fixed_len, memref(ptr, var_len)); } which will return a pointer to the DTrace scratch space of size sizeof(uintptr_t) * 2. However, one can easily call printm() as follows BEGIN { printm(10, (void *)NULL); } and panic the kernel as a result. This commit does two things: (1) adds a new macro DTRACE_INSCRATCHPTR(mstate, ptr, howmany) which checks if a certain pointer is in the DTrace scratch space; (2) uses DTRACE_INSCRATCHPTR() to implement a check on printm()'s DIFO return value in order to avoid the panic and sets CPU_DTRACE_BADADDR if the address is not in the scratch space. Reviewed by: markj MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D41722 --- .../contrib/opensolaris/uts/common/dtrace/dtrace.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c b/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c index 02a95bfab726..ce02676e0dc1 100644 --- a/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c +++ b/sys/cddl/contrib/opensolaris/uts/common/dtrace/dtrace.c @@ -515,6 +515,11 @@ do { \ ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - \ (mstate)->dtms_scratch_ptr >= (alloc_sz)) +#define DTRACE_INSCRATCHPTR(mstate, ptr, howmany) \ + ((ptr) >= (mstate)->dtms_scratch_base && \ + (ptr) <= \ + ((mstate)->dtms_scratch_base + (mstate)->dtms_scratch_size - (howmany))) + #define DTRACE_LOADFUNC(bits) \ /*CSTYLED*/ \ uint##bits##_t \ @@ -7739,9 +7744,24 @@ dtrace_probe(dtrace_id_t id, uintptr_t arg0, uintptr_t arg1, } case DTRACEACT_PRINTM: { - /* The DIF returns a 'memref'. */ + /* + * printm() assumes that the DIF returns a + * pointer returned by memref(). memref() is a + * subroutine that is used to get around the + * single-valued returns of DIF and is assumed + * to always be allocated in the scratch space. + * Therefore, we need to validate that the + * pointer given to printm() is in the scratch + * space in order to avoid a potential panic. + */ uintptr_t *memref = (uintptr_t *)(uintptr_t) val; + if (!DTRACE_INSCRATCHPTR(&mstate, + (uintptr_t)memref, 2 * sizeof(uintptr_t))) { + *flags |= CPU_DTRACE_BADADDR; + continue; + } + /* Get the size from the memref. */ size = memref[1];