From nobody Wed Jun 29 17:49:15 2022 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 59BA0876737; Wed, 29 Jun 2022 17:49:15 +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 4LY897266hz4TJt; Wed, 29 Jun 2022 17:49:15 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656524955; 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=Gyxj0xsD3blI2KRL/vYvgW/bk0Y5fF7O1oT33FFjQ7Q=; b=OwlJFPnamPKQkcID3UJRweXp08mXItGpAbwf5QGkV7iKFkemGeNx21pxmVNlyQ39DiTTee FnVDB53Fof4/l39O7754CgpEYEtqMFgNntep8zhBqVbpR4suS15Nik31QrNDkDbIGx5yo7 5Ot4WuMPmL+vS8cs+2atosr+RBXhenS3hJAuCB3T74wBX2GVXCOk+JqyZKTAsgE1BZWT6g NEkhY8biIl7ry3+pzp2TOfTKRPMwDjsOxNKsytAqj/Sdfoi3VmiYXY3C94E5lVwz+pt9Qh o0x+eK9+w7r4+Z69RfqcBVnFXerB9GPDLCzix9wMvOp/gscoKCrt7w1B9ulTTg== 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 284B61639C; Wed, 29 Jun 2022 17:49:15 +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 25THnFgU084987; Wed, 29 Jun 2022 17:49:15 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 25THnFQp084986; Wed, 29 Jun 2022 17:49:15 GMT (envelope-from git) Date: Wed, 29 Jun 2022 17:49:15 GMT Message-Id: <202206291749.25THnFQp084986@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Jamie Gritton Subject: git: 7060da62ff18 - main - jail: Remove a prison's shared memory when it dies 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: jamie X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7060da62ff18e8e52c5e41f0794cc4f10dadfc6e Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656524955; 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=Gyxj0xsD3blI2KRL/vYvgW/bk0Y5fF7O1oT33FFjQ7Q=; b=psgATyzNVhfKhNlAti38kvN5yu6TyqJMeeeGG4SgNx/lnd+54lpZNi9NaHPIFjUCTaJD5J +POCfrJwXfCIjK3fHnMIVLWE3MEPq+RIKkd7jD9pp17uabyJKHeb1dvY2pi+/NJQpPaU8U 6yvbgLRjMxu9MmEg6V6WAxIxQmt/46TCPCH7LKsGAvc9LXSNYTWHDU/g6ujw/TUr8GZ0KQ Dh6WUBOlP2SXY1LtBVUSj6AbkfBoD8UBT5+503whlMGoJkN+YZyr1NTsyAEwuB3HTznxOl IrF3y88jpgXZLvPHty9lkI9TNDScz4khgOdVCG5POhEvw1Qas5jDibu2/i39Zg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1656524955; a=rsa-sha256; cv=none; b=jUY9J/6WBkiUFeYu4aos0kmdVInIiMqB7Llgq/5zVV+NrPomjPRvTikSlOZM9QWteCJ34H /LPvB2mPrBpy4py7wslX2zxEy1UTmVz2pDSs3E41A/0osKL/kh4wCeEoPSip5Hh/uKK/mP WYj6xFTuIGUbuOyg8RYkkDTkeFLa1ZQLZPEU28EOmNmiKnT+V7UOMYJWJhXiT1Op1YJkg8 nftxGPBGjnHyuBCjZgHyYqaQFNtIKeQR9e8ti/ptHQt3mklYuJULO5jITCfgn87rTRZNOH ju/8EoOzIPNXGoi06UP6XSOOQbgEI/uLum1v7vwW9UlKbq3AQ+9q0NvZa0ShYQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jamie: URL: https://cgit.FreeBSD.org/src/commit/?id=7060da62ff18e8e52c5e41f0794cc4f10dadfc6e commit 7060da62ff18e8e52c5e41f0794cc4f10dadfc6e Author: Jamie Gritton AuthorDate: 2022-06-29 17:47:39 +0000 Commit: Jamie Gritton CommitDate: 2022-06-29 17:47:39 +0000 jail: Remove a prison's shared memory when it dies Add shm_remove_prison(), that removes all POSIX shared memory segments belonging to a prison. Call it from prison_cleanup() so a prison won't be stuck in a dying state due to the resources still held. PR: 257555 Reported by: grembo --- sys/kern/kern_jail.c | 2 ++ sys/kern/uipc_shm.c | 37 ++++++++++++++++++++++++++++++++----- sys/sys/mman.h | 3 +++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/sys/kern/kern_jail.c b/sys/kern/kern_jail.c index 7ef0afabe49d..965f3379adce 100644 --- a/sys/kern/kern_jail.c +++ b/sys/kern/kern_jail.c @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -3241,6 +3242,7 @@ prison_cleanup(struct prison *pr) { sx_assert(&allprison_lock, SA_XLOCKED); mtx_assert(&pr->pr_mtx, MA_NOTOWNED); + shm_remove_prison(pr); (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); } diff --git a/sys/kern/uipc_shm.c b/sys/kern/uipc_shm.c index 9f556efde3f1..1ca088edfd78 100644 --- a/sys/kern/uipc_shm.c +++ b/sys/kern/uipc_shm.c @@ -125,6 +125,7 @@ static void shm_init(void *arg); static void shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd); static struct shmfd *shm_lookup(char *path, Fnv32_t fnv); static int shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred); +static void shm_doremove(struct shm_mapping *map); static int shm_dotruncate_cookie(struct shmfd *shmfd, off_t length, void *rl_cookie); static int shm_dotruncate_locked(struct shmfd *shmfd, off_t length, @@ -982,6 +983,26 @@ shm_init(void *arg) } SYSINIT(shm_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_init, NULL); +/* + * Remove all shared memory objects that belong to a prison. + */ +void +shm_remove_prison(struct prison *pr) +{ + struct shm_mapping *shmm, *tshmm; + u_long i; + + sx_xlock(&shm_dict_lock); + for (i = 0; i < shm_hash + 1; i++) { + LIST_FOREACH_SAFE(shmm, &shm_dictionary[i], sm_link, tshmm) { + if (shmm->sm_shmfd->shm_object->cred && + shmm->sm_shmfd->shm_object->cred->cr_prison == pr) + shm_doremove(shmm); + } + } + sx_xunlock(&shm_dict_lock); +} + /* * Dictionary management. We maintain an in-kernel dictionary to map * paths to shmfd objects. We use the FNV hash on the path to store @@ -1034,11 +1055,7 @@ shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred) FREAD | FWRITE); if (error) return (error); - map->sm_shmfd->shm_path = NULL; - LIST_REMOVE(map, sm_link); - shm_drop(map->sm_shmfd); - free(map->sm_path, M_SHMFD); - free(map, M_SHMFD); + shm_doremove(map); return (0); } } @@ -1046,6 +1063,16 @@ shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred) return (ENOENT); } +static void +shm_doremove(struct shm_mapping *map) +{ + map->sm_shmfd->shm_path = NULL; + LIST_REMOVE(map, sm_link); + shm_drop(map->sm_shmfd); + free(map->sm_path, M_SHMFD); + free(map, M_SHMFD); +} + int kern_shm_open2(struct thread *td, const char *userpath, int flags, mode_t mode, int shmflags, struct filecaps *fcaps, const char *name __unused) diff --git a/sys/sys/mman.h b/sys/sys/mman.h index a07b3550d51a..71172bfd49a3 100644 --- a/sys/sys/mman.h +++ b/sys/sys/mman.h @@ -300,6 +300,8 @@ struct shmfd { #endif #ifdef _KERNEL +struct prison; + int shm_map(struct file *fp, size_t size, off_t offset, void **memp); int shm_unmap(struct file *fp, void *mem, size_t size); @@ -309,6 +311,7 @@ struct shmfd *shm_hold(struct shmfd *shmfd); void shm_drop(struct shmfd *shmfd); int shm_dotruncate(struct shmfd *shmfd, off_t length); bool shm_largepage(struct shmfd *shmfd); +void shm_remove_prison(struct prison *pr); extern struct fileops shm_ops;