From nobody Wed Mar 29 05:04:22 2023 X-Original-To: dev-commits-src-main@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 4PmZGZ6n8Pz41ynS; Wed, 29 Mar 2023 05:04:22 +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 4PmZGZ6DTKz4Hgn; Wed, 29 Mar 2023 05:04:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1680066262; 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=VucduRQCSxpKvfym/1eAzJU/2tMabD5BCemDd6/YbaY=; b=Buh/FQuWtDx1v+osG8KFS3b1mhtCwgiMa4B+qSUKtbmELnq4if6CTQ50kWVxnwqdM52IIb Rd8CgUtaQ1ek5346jgPo7zGJkh7rhML+eHKsYM/TxEXdvu84+IXLtyOCiSmlb5jhQE+RT2 q2tqIZvm7Ad/cfj8ikm2iIBqoCtGDAcQfea/eja6Wj6hLo+HDtphQOfsfj8LiYZmJeHuav Xmk8a1lfN95Raz3x5BHrcl5QNum8t62R+zREfNzQNBDirrqgs7K8h7CHRxUVulAlPT/Kul /ZDtm/352/jgki8XRjXmMQssjCAlAHN24GnhA1eewR0INNMdnsul3MG986NmmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1680066262; 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=VucduRQCSxpKvfym/1eAzJU/2tMabD5BCemDd6/YbaY=; b=FL8kY7jkajkhNOh9f9JV2dV8G8K1YBkiC3nolaYerA9wYMfUGfS96MUlo3h4ifiSB5wiOk Pevh5BEO5XVr7Cd/R+c9ON8pNMyb6Y67KbFIQ777zJgEv5bhJUhkxCogVsQKXNPx6TdbP7 tweDojsSn2zA8Nrr2GqaYLl5panh4k+meYWEAeZRe7/qerBbHN+N/qbDNLj842VuVVejuh v7HgVZieT1AgKJ/90o++/ZbItS8Nmjc2FqOTMK5yNxN9b+smrby5dWUYI2uee7PFkobr8K Ca6GyjlyE4N8hx6Eed0dOo5l74rdd/f4Iw2yAE+tiiJXlNWfLp7DYujLy4Ke3A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1680066262; a=rsa-sha256; cv=none; b=Hxzx83XKHGMY646ARINVfuR5oH97OWUwEE3LfSvGH7sJ6m4uUX2OXOXAB/XPupmqrRvlJm dAT9Vh4iF+B0JCxm2txWMhallM8vF+lx3YP33kMAH5wrIgoZZHr4i5IkHh/VOHQz77mQuy 1659Z4EVdl9Mpc06XOMsIk6BLhn1OaD5DGb99Mi+RKXbA1v3On7RZKiBNhSJce2enKXu1z lpzdyj4fRpEsukcjdok/Cr0xTR8B25YIwoQMZb78NsqNGw12oqOt6inWv2fUsPNxR2VfPu EKYIBzVwuZo0yb/1LONH4BIGcjXoqmeDWpDeHLvEoR31W4U+81NEMpeLci48nA== 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 4PmZGZ5LQPzHGX; Wed, 29 Mar 2023 05:04:22 +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 32T54MKA074326; Wed, 29 Mar 2023 05:04:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32T54MaU074325; Wed, 29 Mar 2023 05:04:22 GMT (envelope-from git) Date: Wed, 29 Mar 2023 05:04:22 GMT Message-Id: <202303290504.32T54MaU074325@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Guzik Subject: git: 37337709d333 - main - cred: convert the refcount from int to long List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mjg X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 37337709d3334f32650ba3a7c529fa013ed5e1f2 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mjg: URL: https://cgit.FreeBSD.org/src/commit/?id=37337709d3334f32650ba3a7c529fa013ed5e1f2 commit 37337709d3334f32650ba3a7c529fa013ed5e1f2 Author: Mateusz Guzik AuthorDate: 2023-03-22 20:42:04 +0000 Commit: Mateusz Guzik CommitDate: 2023-03-29 05:02:32 +0000 cred: convert the refcount from int to long On 64-bit platforms this sorts out worries about mitigating bugs which overflow the counter, all while not pessimizng anything -- most notably it avoids whacking per-thread operation in favor of refcount(9) API. The struct already had two instances of 4 byte padding with 256 bytes in size, cr_flags gets moved around to avoid growing it. 32-bit platforms could also get the extended counter, but I did not do it as one day(tm) the mutex protecting centralized operation should be replaced with atomics and 64-bit ops on 32-bit platforms remain quite penalizing. While worries of counter overflow are addressed, the following is not (just like it would not be with conversion to refcount(9)): - counter *underflows* - buffer overruns from adjacent allocations - UAF due to stale cred pointer - .. and other goodies As such, while lipstick was placed, the pig should not be participating in any beauty pageants. Prodded by: emaste Differential Revision: https://reviews.freebsd.org/D39220 --- sys/kern/kern_prot.c | 11 ++++++----- sys/sys/proc.h | 2 +- sys/sys/ucred.h | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c index 983e6ae21493..3d8f3e222b4c 100644 --- a/sys/kern/kern_prot.c +++ b/sys/kern/kern_prot.c @@ -1877,7 +1877,7 @@ crunuse(struct thread *td) __func__, cr->cr_users, cr)); cr->cr_users--; if (cr->cr_users == 0) { - KASSERT(cr->cr_ref > 0, ("%s: ref %d not > 0 on cred %p", + KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p", __func__, cr->cr_ref, cr)); crold = cr; } else { @@ -1905,7 +1905,7 @@ crunusebatch(struct ucred *cr, int users, int ref) mtx_unlock(&cr->cr_mtx); return; } - KASSERT(cr->cr_ref >= 0, ("%s: ref %d not >= 0 on cred %p", + KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p", __func__, cr->cr_ref, cr)); if (cr->cr_ref > 0) { mtx_unlock(&cr->cr_mtx); @@ -2051,7 +2051,7 @@ crfree(struct ucred *cr) mtx_unlock(&cr->cr_mtx); return; } - KASSERT(cr->cr_ref >= 0, ("%s: ref %d not >= 0 on cred %p", + KASSERT(cr->cr_ref >= 0, ("%s: ref %ld not >= 0 on cred %p", __func__, cr->cr_ref, cr)); if (cr->cr_ref > 0) { mtx_unlock(&cr->cr_mtx); @@ -2066,7 +2066,7 @@ crfree_final(struct ucred *cr) KASSERT(cr->cr_users == 0, ("%s: users %d not == 0 on cred %p", __func__, cr->cr_users, cr)); - KASSERT(cr->cr_ref == 0, ("%s: ref %d not == 0 on cred %p", + KASSERT(cr->cr_ref == 0, ("%s: ref %ld not == 0 on cred %p", __func__, cr->cr_ref, cr)); /* @@ -2104,6 +2104,7 @@ crcopy(struct ucred *dest, struct ucred *src) bcopy(&src->cr_startcopy, &dest->cr_startcopy, (unsigned)((caddr_t)&src->cr_endcopy - (caddr_t)&src->cr_startcopy)); + dest->cr_flags = src->cr_flags; crsetgroups(dest, src->cr_ngroups, src->cr_groups); uihold(dest->cr_uidinfo); uihold(dest->cr_ruidinfo); @@ -2210,7 +2211,7 @@ proc_unset_cred(struct proc *p) mtx_lock(&cr->cr_mtx); cr->cr_users--; if (cr->cr_users == 0) - KASSERT(cr->cr_ref > 0, ("%s: ref %d not > 0 on cred %p", + KASSERT(cr->cr_ref > 0, ("%s: ref %ld not > 0 on cred %p", __func__, cr->cr_ref, cr)); mtx_unlock(&cr->cr_mtx); crfree(cr); diff --git a/sys/sys/proc.h b/sys/sys/proc.h index c6670bdc96f8..172e716d8785 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -321,7 +321,7 @@ struct thread { int td_errno; /* (k) Error from last syscall. */ size_t td_vslock_sz; /* (k) amount of vslock-ed space */ struct kcov_info *td_kcov_info; /* (*) Kernel code coverage data */ - u_int td_ucredref; /* (k) references on td_realucred */ + long td_ucredref; /* (k) references on td_realucred */ #define td_endzero td_sigmask /* Copied during fork1(), thread_create(), or kthread_add(). */ diff --git a/sys/sys/ucred.h b/sys/sys/ucred.h index 5ec54be70c2e..73b5fb7ab118 100644 --- a/sys/sys/ucred.h +++ b/sys/sys/ucred.h @@ -61,8 +61,9 @@ struct loginclass; #if defined(_KERNEL) || defined(_WANT_UCRED) struct ucred { struct mtx cr_mtx; - int cr_ref; /* (c) reference count */ + long cr_ref; /* (c) reference count */ u_int cr_users; /* (c) proc + thread using this cred */ + u_int cr_flags; /* credential flags */ struct auditinfo_addr cr_audit; /* Audit properties. */ #define cr_startcopy cr_uid uid_t cr_uid; /* effective user id */ @@ -75,7 +76,6 @@ struct ucred { struct uidinfo *cr_ruidinfo; /* per ruid resource consumption */ struct prison *cr_prison; /* jail(2) */ struct loginclass *cr_loginclass; /* login class */ - u_int cr_flags; /* credential flags */ void *cr_pspare2[2]; /* general use 2 */ #define cr_endcopy cr_label struct label *cr_label; /* MAC label */