From nobody Sun Jul 03 19:26:06 2022 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 677C58B39E4; Sun, 3 Jul 2022 19:26:06 +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 4Lbf722Q4Fz3HJ2; Sun, 3 Jul 2022 19:26:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656876366; 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=9EhRFfkoEjWk13T4djysvizSiJqr7ODWTcMTvuVhK1Y=; b=byazFWa+zUSwsjSUQtONZzC0EceM0srr5aBJHbHBYyTWRAyhpueslvZ2tlpVkxJAIFCAx4 HNJ6pGkZv3lhDHzyaBIo2PlJP/poJZkfeRGyoEhpntxxX6nkAfrEY+CS7/uw1wcbirETh9 87bA31oq8IYFKfEJp2gNN+jA3LOIELtwkBN1kdgDp1tEiKOr4gzFxkLH2ngQFpjYnVZmYk mDPBR+pXoqV4OELyLIVOl482CDpMSFbWYYyT0fEaYWfeuwYQ0eBMPHmCgM3/UkhDdrV156 /2j6QHtCFWb0P/9ZbDFt9cD6Y4OxTsU77wNiA9CznWO/YLRjwLrZwaDhucOQ+g== 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 336892A3BA; Sun, 3 Jul 2022 19:26:06 +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 263JQ6xv019053; Sun, 3 Jul 2022 19:26:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 263JQ6Uf019052; Sun, 3 Jul 2022 19:26:06 GMT (envelope-from git) Date: Sun, 3 Jul 2022 19:26:06 GMT Message-Id: <202207031926.263JQ6Uf019052@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Jamie Gritton Subject: git: cf18a61708d3 - stable/13 - MFC jail: Remove a prison's shared memory when it dies 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: jamie X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: cf18a61708d30866f3e97701daa3729f75878094 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1656876366; 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=9EhRFfkoEjWk13T4djysvizSiJqr7ODWTcMTvuVhK1Y=; b=v13efZmXxRnj8LkDNoB5+nUT2lGbdQTPqr+Eige79fK1q02JGCxv1wHR5bqYPJocl5Fa0Q D6oU8v4i7sdpU5sltNGFrujHjBWgP+fkdUcBZoXKfiVmUH6haCyyypDFVnDsyVxSm9CA8G osMvwqDGCNWJclpnVGK+J8LbLehQt09HAfPQ4Ks5iOlYXPA+/9UoU57w4H/OP6uqzIY43N ZAcnVAdtcICgSUbwJZhQUpXs4VLGvJN5OwUfSRt2japL4oy+VVmGyZnYM7XX+IPK8hK4R5 ZjHNJ0rBaWgezr1H+Hkuan+DPhfP7PlnyMlpNqxG8V2EBiv7bpY7b1q2BmFt8A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1656876366; a=rsa-sha256; cv=none; b=vMw3x7bJdDrF/sj8W6OVHXWT1hKpyied/SRAKds1TgShEoapuO8oaOSEDIVOayjHiPaoKZ rwYVfWEahyjclDZ2tD3aWsQTHyKnPpe0FB5D0/lUFjQIx2DyznRlLS+ixvYZ9ibaM9TJUH IuIc1ixTxxVPv64S1mx51lzBTxYeuebzr8EuQYBvjF6+wYLwdcotNhUifGITLePQWk/6JF eVzg22e4+3ssJxuoHjSx3oZpcQVDonnjsNZHIDMAkS+1fHKmfYJFvuUNrabzBeoP3wOnaY UXKazC11Yi4xVkaUGdEHC0NGoj39qIgFyIJCxib0j0IsdrOEQW/yY3L8D7yT4g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by jamie: URL: https://cgit.FreeBSD.org/src/commit/?id=cf18a61708d30866f3e97701daa3729f75878094 commit cf18a61708d30866f3e97701daa3729f75878094 Author: Jamie Gritton AuthorDate: 2022-06-29 17:47:39 +0000 Commit: Jamie Gritton CommitDate: 2022-07-03 19:25:43 +0000 MFC 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 (cherry picked from commit 7060da62ff18e8e52c5e41f0794cc4f10dadfc6e) --- 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 43e101176a71..b8d408a810ea 100644 --- a/sys/kern/kern_jail.c +++ b/sys/kern/kern_jail.c @@ -50,6 +50,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -3023,6 +3024,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 d1038766fb19..2bd7c3a038df 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, @@ -964,6 +965,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 @@ -1016,11 +1037,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); } } @@ -1028,6 +1045,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;