From owner-svn-src-all@freebsd.org Thu Aug 10 16:50:15 2017 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 2E24FDD7A9D; Thu, 10 Aug 2017 16:50:15 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id E7F3C6936E; Thu, 10 Aug 2017 16:50:14 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v7AGoDeu090171; Thu, 10 Aug 2017 16:50:13 GMT (envelope-from oshogbo@FreeBSD.org) Received: (from oshogbo@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v7AGoD6I090170; Thu, 10 Aug 2017 16:50:13 GMT (envelope-from oshogbo@FreeBSD.org) Message-Id: <201708101650.v7AGoD6I090170@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: oshogbo set sender to oshogbo@FreeBSD.org using -f From: Mariusz Zaborski Date: Thu, 10 Aug 2017 16:50:13 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r322370 - head/lib/libutil X-SVN-Group: head X-SVN-Commit-Author: oshogbo X-SVN-Commit-Paths: head/lib/libutil X-SVN-Commit-Revision: 322370 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Aug 2017 16:50:15 -0000 Author: oshogbo Date: Thu Aug 10 16:50:13 2017 New Revision: 322370 URL: https://svnweb.freebsd.org/changeset/base/322370 Log: Limit descriptors stored in the pidfh structure. Reviewed by: markj, cem Differential Revision: https://reviews.freebsd.org/D11741 Modified: head/lib/libutil/pidfile.c Modified: head/lib/libutil/pidfile.c ============================================================================== --- head/lib/libutil/pidfile.c Thu Aug 10 16:45:05 2017 (r322369) +++ head/lib/libutil/pidfile.c Thu Aug 10 16:50:13 2017 (r322370) @@ -28,6 +28,7 @@ __FBSDID("$FreeBSD$"); #include +#include #include #include @@ -103,6 +104,7 @@ pidfile_open(const char *path, mode_t mode, pid_t *pid struct stat sb; int error, fd, dirfd, dirlen, filenamelen, count; struct timespec rqtp; + cap_rights_t caprights; pfh = malloc(sizeof(*pfh)); if (pfh == NULL) @@ -179,21 +181,35 @@ pidfile_open(const char *path, mode_t mode, pid_t *pid * to the proper descriptor. */ if (fstat(fd, &sb) == -1) { - error = errno; - unlinkat(dirfd, pfh->pf_filename, 0); - close(dirfd); - close(fd); - free(pfh); - errno = error; - return (NULL); + goto failed; } + if (cap_rights_limit(dirfd, + cap_rights_init(&caprights, CAP_UNLINKAT)) < 0 && errno != ENOSYS) { + goto failed; + } + + if (cap_rights_limit(fd, cap_rights_init(&caprights, CAP_PWRITE, + CAP_FSTAT, CAP_FTRUNCATE)) < 0 && + errno != ENOSYS) { + goto failed; + } + pfh->pf_dirfd = dirfd; pfh->pf_fd = fd; pfh->pf_dev = sb.st_dev; pfh->pf_ino = sb.st_ino; return (pfh); + +failed: + error = errno; + unlinkat(dirfd, pfh->pf_filename, 0); + close(dirfd); + close(fd); + free(pfh); + errno = error; + return (NULL); } int