From nobody Fri Mar 17 10:23:57 2023 X-Original-To: bugs@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 4PdKwt0D0Qz3yBPt for ; Fri, 17 Mar 2023 10:23:58 +0000 (UTC) (envelope-from bugzilla-noreply@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 4PdKws482vz3FyQ for ; Fri, 17 Mar 2023 10:23:57 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679048637; 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=3FXFM2JTab1ocshcU48pTVNHnhxgCq7oM7Tv1n8iSjs=; b=TfEZ7e+7W9nNHSN4+pQjK2OaGT+cDu5s//WnKqowGhwLH6LhtQ6k2NtSj0hZ+ccjkeS+oa 7WDuJ2GdBA+B+0R9Yq6xlEN9voO4MQhvdizfWIwogls5w5Fuzdnr3cAi2xu0wIMeWHKaRd V7JCgH22wQGiq/xEXsKMn28yfM0ALyEEccQHzwNUiyRNxymQpC3j3Xc2w0HwsagyAGxa2b bZjVWhe3ypR3F9qnHn806QIn3y141aKLale2b+nX0mwW7ob4YGtrCrdYbUArCu6zoEvV4e E3uZNWrFFVf5aQSSi9qJKKchUsd0qJ5AqTOn+a381bTFBS1k4rn8rO23VzmfaA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1679048637; a=rsa-sha256; cv=none; b=V9jbCOj9SOeIfH2TBBKtDMjWOR/V/nPIniPUE3TSIqWavB3VmBg+XyO/3puM07S4jKxtkx H+6biSZeNlzuKnLKRFaPSafyjTq+Znh9Bm/W+rvACieLS+jmvOWTT0r93BL+o9Oq2CQUzL Mi82UQ8Oy3wmBsOOADS/su8IiL3CPBZ5Jw5MHe0NIVZwHqc+UexE9aICHOJUIcPC7fp2Ta DWcTiJOYsTJutJvoNvAgeNr7lxnn0BKDQqZTdDyMURXkaLXE0xhyOBAJHjERpqTEgUP9Bc yoLfHjj5Wmn+80bQiTk/0rkAvaWbkP/kVTrr7P8EL+0vrLxfxBgx8c4ypeviLw== Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (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 4PdKws3DBHzMjs for ; Fri, 17 Mar 2023 10:23:57 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 32HANvC1001524 for ; Fri, 17 Mar 2023 10:23:57 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 32HANv3c001523 for bugs@FreeBSD.org; Fri, 17 Mar 2023 10:23:57 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: bugs@FreeBSD.org Subject: [Bug 270283] would like an even safer LIST_FOREACH_SAFE() Date: Fri, 17 Mar 2023 10:23:57 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: kern X-Bugzilla-Version: CURRENT X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Only Me X-Bugzilla-Who: levon@movementarian.org X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: bugs@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated List-Id: Bug reports List-Archive: https://lists.freebsd.org/archives/freebsd-bugs List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-bugs@freebsd.org MIME-Version: 1.0 X-ThisMailContainsUnwantedMimeParts: N https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D270283 Bug ID: 270283 Summary: would like an even safer LIST_FOREACH_SAFE() Product: Base System Version: CURRENT Hardware: Any OS: Any Status: New Severity: Affects Only Me Priority: --- Component: kern Assignee: bugs@FreeBSD.org Reporter: levon@movementarian.org We hit the following situation: - we have a component with a list of callbacks. - those callbacks are themselves allowed to call back in and remove entries from the list - LIST_FOREACH_SAFE() is used to safely protect against removal of the cur= rent item - however, a callback is also legitimately allowed to remove any other ite= m on the list This falls down when a callback removes the *next* item on the list - the m= acro has already saved this in "tvar", so it will then try to use freed memory on the next iteration. We have fixed this with LIST_FOREACH_SAFER() / LIST_REMOVE_SAFER() variants: #define LIST_FOREACH_SAFER(var, head, field, tvarp) \=20=20=20=20=20=20= =20 for ((var) =3D LIST_FIRST((head));=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20 \ (var) && ((*tvarp) =3D LIST_NEXT((var), field), 1); \ (var) =3D (*tvarp)) #define LIST_REMOVE_SAFER(elm, field, elmp) do { \ if (elmp =3D=3D elm) {=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20 \ elmp =3D LIST_NEXT(elm, field); \ };=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 \ LIST_REMOVE(elm, field);=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20 \ } while (0) Would like thoughts on whether this would be something more widely useful before I prepare a PR and so on, thanks. --=20 You are receiving this mail because: You are the assignee for the bug.=