From nobody Wed Mar 22 18:46:31 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 4PhcrR2wH8z4199K for ; Wed, 22 Mar 2023 18:46:31 +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 4PhcrR1Fg9z42Xt for ; Wed, 22 Mar 2023 18:46:31 +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=1679510791; 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: in-reply-to:in-reply-to:references:references; bh=awgB1su3dZLVjcEp62f7swsyF2n7MT2tJs5rVDlr/K8=; b=as0iVGypdqdaMzvaDG9Y5ADTfYI22m+pwnyn8EyNT4cL82kkWBaa0BpSRT3LTWRCVDICR8 FmZq/jvbjC0kwJ9wbax0W7ZymtN2XPYDJToY/ZaQYa1MTKyyulKd800mFspKFFcrDneZSt stXkWzJH19Jig1Z8iCrNXWU4NbrzJMoxq2gwP4SkQorHj+R7iGbI9vP+fM6nt2rlr2Ynb1 D7Htg+g6kvTnMOo2p9uNa3s6bCatEIYWQABc9DMcd+6ePd8tmC62n1wTnlk3SZ70d5hJLn oiAlHT6YPVPpV8HNsvIS5fnRX7gDQCchdaAJpto4tVMkGsiOLt+H1DfYXWeFIg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1679510791; a=rsa-sha256; cv=none; b=iz12Dpge8XOoVAZPAtFtGfb3jB6uIADsU7IJQb6phy8wp34I4gabsJW9Hd5q1ZRzFvOGPc APQhvKvVKZ1DiM/ESEidXI3pFak33INxxsq9D8pyzPlz9uEk/HzUg8cJG12kZF+yIEk9lb 7qLQCKq8W5uk6CrwFGEL24kw5m3vFD3YCCu2r8i0dIxN1sGF8//73SgmwxYtALLrZnFKka E+i7GGGtWsHVjZ3qpKMI3DmepNOOaiHO8Y7nRvDEUBxG5GPN5SU/wM/7PhOsK+Laprt9di JAFu8yvYYanF6hBajSrzVO1eprQ3uP7ky/tVtIb35bomDqCjI/+O9akxBCI4pA== 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 4PhcrR0DH4z14nl for ; Wed, 22 Mar 2023 18:46:31 +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 32MIkUwo045151 for ; Wed, 22 Mar 2023 18:46:30 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 32MIkUoM045150 for bugs@FreeBSD.org; Wed, 22 Mar 2023 18:46:30 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 266101] ucred reference count may overflow Date: Wed, 22 Mar 2023 18:46:31 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: changed 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: mjg@FreeBSD.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: Message-ID: In-Reply-To: References: 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=3D266101 --- Comment #1 from Mateusz Guzik --- the refcount API is just a poor man's mitigation, especially for the cred stuff. Perf wise, I have to note the counter gets modified *a lot* all while the struct keeps being accessed. Atomic op on a centralized counter is tons of memory traffic which does not need to happen, but I don't have numbers hand= y. Here are some tidbits: 1. One massive limitation of refcount API, which can't be helped, is that *underflows* cannot possibly be detected, thus use-after-free remains possi= ble. 2. Even if there were no conditions triggering premature freeing, buggy code can still have a stale pointer to creds which gets used later. 3. There is nothing being done to prevent overflows from non-cred objects messing with said creds. instead, memory is allocated with malloc: cr =3D malloc(sizeof(*cr), M_CRED, M_WAITOK | M_ZERO); If one is serious about hardening creds, most of cred management has to be reworked. Typical case is that new creds get very temporarily allocated, get modified= in a small manner and assigned. This happens a bunch of times before the proce= ss settles on final state which is then used for a *long time*. Trivial exampl= e: logging in and setgid, setuid etc. calls Apart from the churn, this also happens to increase memory usage -- you log= in twice, you end up with 2 sets of identical creds which persist. Thus the general idea would be to maintain a global hash of creds, where you locally create what creds would look like you and try to find them in said struct. If that succeds, alloc/free trip got avoided, therwise you alloc to= add them. I can't stress enough that creds are unmodifiable after creation and *never* freed. With the proposed scheme ref counting can be literally eliminated(!). The problem to solve here is making sure a nasty userspace cannot keep crea= ting creds, without breaking anything. There is probably some bad idioms in place which result in more allocations than needed. It may be a hybrid approach will be prudent -- you *do* refcount(9) for all= the transient stuff and "stabilize" after enough traffic. There are some gaps to fill in the proposal, but the general idea should be clear. I can't stress enough how the refcount API fails to address anything most of things of importance and that addressing them renders it mostly moot. --=20 You are receiving this mail because: You are the assignee for the bug.=