From nobody Thu Oct 28 17:50:43 2021 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 EADB5182719C; Thu, 28 Oct 2021 17:50:44 +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 4HgClS2VLBz4Svd; Thu, 28 Oct 2021 17:50:43 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 7E8EC27C67; Thu, 28 Oct 2021 17:50:43 +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 19SHohkE097777; Thu, 28 Oct 2021 17:50:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 19SHohAD097776; Thu, 28 Oct 2021 17:50:43 GMT (envelope-from git) Date: Thu, 28 Oct 2021 17:50:43 GMT Message-Id: <202110281750.19SHohAD097776@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: ee92c8a842d6 - main - sysctl kern.proc.procname: report right hardlink name 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: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: ee92c8a842d61ffda8d111e1b0e398085c5bfb3a Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=ee92c8a842d61ffda8d111e1b0e398085c5bfb3a commit ee92c8a842d61ffda8d111e1b0e398085c5bfb3a Author: Konstantin Belousov AuthorDate: 2021-10-23 19:01:37 +0000 Commit: Konstantin Belousov CommitDate: 2021-10-28 17:50:02 +0000 sysctl kern.proc.procname: report right hardlink name PR: 248184 Reviewed by: markj Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D32611 --- sys/kern/kern_proc.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 2649d1d3a58f..c4c01da1faea 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -45,6 +45,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -54,6 +55,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -2233,32 +2235,74 @@ sysctl_kern_proc_pathname(SYSCTL_HANDLER_ARGS) pid_t *pidp = (pid_t *)arg1; unsigned int arglen = arg2; struct proc *p; - struct vnode *vp; - char *retbuf, *freebuf; + struct vnode *vp, *dvp; + char *retbuf, *freebuf, *binname; + struct nameidata nd; + size_t freepath_size; int error; + bool do_fullpath; if (arglen != 1) return (EINVAL); + binname = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); + binname[0] = '\0'; if (*pidp == -1) { /* -1 means this process */ p = req->td->td_proc; } else { error = pget(*pidp, PGET_CANSEE, &p); - if (error != 0) + if (error != 0) { + free(binname, M_TEMP); return (error); + } } vp = p->p_textvp; if (vp == NULL) { if (*pidp != -1) PROC_UNLOCK(p); + free(binname, M_TEMP); return (0); } vref(vp); + dvp = p->p_textdvp; + if (dvp != NULL) + vref(dvp); + if (p->p_binname != NULL) + strlcpy(binname, p->p_binname, MAXPATHLEN); if (*pidp != -1) PROC_UNLOCK(p); - error = vn_fullpath(vp, &retbuf, &freebuf); + do_fullpath = true; + freebuf = NULL; + if (dvp != NULL && binname[0] != '\0') { + freepath_size = MAXPATHLEN; + if (vn_fullpath_hardlink(vp, dvp, binname, strlen(binname), + &retbuf, &freebuf, &freepath_size) == 0) { + /* + * Recheck the looked up path. The binary + * might have been renamed or replaced, in + * which case we should not report old name. + */ + NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, retbuf, + req->td); + error = namei(&nd); + if (error == 0) { + if (nd.ni_vp == vp) + do_fullpath = false; + vrele(nd.ni_vp); + NDFREE(&nd, NDF_ONLY_PNBUF); + } + } + } + if (do_fullpath) { + free(freebuf, M_TEMP); + freebuf = NULL; + error = vn_fullpath(vp, &retbuf, &freebuf); + } vrele(vp); - if (error) + if (dvp != NULL) + vrele(dvp); + free(binname, M_TEMP); + if (error != 0) return (error); error = SYSCTL_OUT(req, retbuf, strlen(retbuf) + 1); free(freebuf, M_TEMP);