From nobody Wed Mar 29 14:41:56 2023 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 4Pmq511W8Zz42djm; Wed, 29 Mar 2023 14:41:57 +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 4Pmq510yz3z4P36; Wed, 29 Mar 2023 14:41:57 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1680100917; 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=ChHXnqIi/57RCX0le5II65BLDqAFT/Fkiv7Ep2ZI9Ps=; b=Y0gl/O+XdswDo2kv1jCjBMOgZtzW/OlJq0v/o/cTLhYqLi+9p1WkZvJIF2JFa15M610yW4 DKsO4Fo+hmC4G1NWhkq7xKVqUGMJoQlBoe6Eo7yLBKWkBFFa+JYnSbACNVVwYtGYwCp48U HKAU/83JUOQu64eePXfxjL04RJSNe4hJkFARGcZ3dGqxZr28uf6tvesNATBrO0I/HwfiK/ FtAdfQ/8KsUaREk+68tIqGKpMRQ460XEvpFrcc++LtoiBezxpmDE2teeBqhVUusCr+JYkH kmmeMfhLf9XddPI9BsdYVXuEwbPVESdjvCkF8joXetzctGiZ59pDslsDeHnc1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1680100917; 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=ChHXnqIi/57RCX0le5II65BLDqAFT/Fkiv7Ep2ZI9Ps=; b=YzDb4X/esglLZ/NvHNdsnn1n2d01kkrSmDyMKSZXFnzLS9Dp7H7mopSPlqgz9SOSkmw39J i5BfjIE80leHgsgqU3XHpK4QYlGR2KtPMMylsBEtzwm+VV21r/nEQzrV52TZS9ytvdPZuc UCjOpn2MhcxEwul4qSbAQfzz0V52PZCK5lGNV4sxjCRPfHvfSCOqouJCBxY2fm6WYk0HEL cl4EyTrHkDjZkyJFtTJftsTK0cz/azAt+viq5bYURrFzzH5BX5wB6Dvq4wpD3aD60Zh/lf Ltsxphx6FOyAl+trzdaOHoDnChyxwbsBRwP6aIlfUsM1f62N/UXPZ68uLVspUA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1680100917; a=rsa-sha256; cv=none; b=SeHKSk69xhwz5Tx10e6CyaqNXXP5bpzNBK0yK/aijrgoOr0IeFQek7fwZuqMPGSGiBUoBx iE2BDunKGv+T3xiWUf46ZkPGh4xC0AEmnEuTKEA9TLjl5c7JfS5hIo0hH0N9GtXjaBlHdR Zayt9UHisKM8LsEtRpxMyN9a5ChEnoEA0S3N94hNC5M+f4tx6cFsne4VoKeJ7L9UXfNbCK i29rNOUQ08uUgWR2mBY+wCxt/UD1SY61wHOvwAl+GThvhdWed6j7/4Ah6dpDAGnp9/sbi/ a4Fkws1nZRoqXyMJgh0tpiZKJp1nPwEP+O6ZD2RFoWF+LBKuwM3uY+12ToShnA== 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 4Pmq5102hRzYws; Wed, 29 Mar 2023 14:41:57 +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 32TEfuh2023884; Wed, 29 Mar 2023 14:41:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32TEfuOD023883; Wed, 29 Mar 2023 14:41:56 GMT (envelope-from git) Date: Wed, 29 Mar 2023 14:41:56 GMT Message-Id: <202303291441.32TEfuOD023883@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 6ebf11e77cf5 - stable/13 - path_test: Add a test case for openat(O_EMPTY_PATH) in capability mode 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: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 6ebf11e77cf5490caed1fd629586db3def0db030 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=6ebf11e77cf5490caed1fd629586db3def0db030 commit 6ebf11e77cf5490caed1fd629586db3def0db030 Author: Mark Johnston AuthorDate: 2023-03-22 12:51:58 +0000 Commit: Mark Johnston CommitDate: 2023-03-29 14:41:47 +0000 path_test: Add a test case for openat(O_EMPTY_PATH) in capability mode MFC after: 1 week (cherry picked from commit e5e1d9c7b781470b2eb31d80f40927481e0053b8) --- tests/sys/file/path_test.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/sys/file/path_test.c b/tests/sys/file/path_test.c index a212325c7db6..130acababb21 100644 --- a/tests/sys/file/path_test.c +++ b/tests/sys/file/path_test.c @@ -208,6 +208,80 @@ ATF_TC_BODY(path_capsicum, tc) waitchild(child, 4); } +/* + * Check that a pathfd can be converted to a regular fd using openat() in + * capability mode, but that rights on the pathfd are respected. + */ +ATF_TC_WITHOUT_HEAD(path_capsicum_empty); +ATF_TC_BODY(path_capsicum_empty, tc) +{ + char path[PATH_MAX]; + cap_rights_t rights; + int dfd, fd, pathfd, pathdfd; + + mktfile(path, "path_capsicum.XXXXXX"); + + pathdfd = open(".", O_PATH); + ATF_REQUIRE_MSG(pathdfd >= 0, FMT_ERR("open")); + pathfd = open(path, O_PATH); + ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open")); + + ATF_REQUIRE(cap_enter() == 0); + + dfd = openat(pathdfd, "", O_DIRECTORY | O_EMPTY_PATH); + ATF_REQUIRE(dfd >= 0); + CHECKED_CLOSE(dfd); + + /* + * CAP_READ and CAP_LOOKUP should be sufficient to open a directory. + */ + cap_rights_init(&rights, CAP_READ | CAP_LOOKUP); + ATF_REQUIRE(cap_rights_limit(pathdfd, &rights) == 0); + dfd = openat(pathdfd, "", O_DIRECTORY | O_EMPTY_PATH); + ATF_REQUIRE(dfd >= 0); + CHECKED_CLOSE(dfd); + + /* + * ... CAP_READ on its own is not. + */ + cap_rights_init(&rights, CAP_READ); + ATF_REQUIRE(cap_rights_limit(pathdfd, &rights) == 0); + dfd = openat(pathdfd, "", O_DIRECTORY | O_EMPTY_PATH); + ATF_REQUIRE_ERRNO(ENOTCAPABLE, dfd == -1); + + /* + * Now try with a regular file. + */ + fd = openat(pathfd, "", O_RDWR | O_EMPTY_PATH); + ATF_REQUIRE(fd >= 0); + CHECKED_CLOSE(fd); + + cap_rights_init(&rights, CAP_READ | CAP_LOOKUP | CAP_WRITE); + ATF_REQUIRE(cap_rights_limit(pathfd, &rights) == 0); + fd = openat(pathfd, "", O_RDWR | O_EMPTY_PATH | O_APPEND); + ATF_REQUIRE(fd >= 0); + CHECKED_CLOSE(fd); + + /* + * CAP_SEEK is needed to open a file for writing without O_APPEND. + */ + cap_rights_init(&rights, CAP_READ | CAP_LOOKUP | CAP_WRITE); + ATF_REQUIRE(cap_rights_limit(pathfd, &rights) == 0); + fd = openat(pathfd, "", O_RDWR | O_EMPTY_PATH); + ATF_REQUIRE_ERRNO(ENOTCAPABLE, fd == -1); + + /* + * CAP_LOOKUP isn't sufficient to open a file for reading. + */ + cap_rights_init(&rights, CAP_LOOKUP); + ATF_REQUIRE(cap_rights_limit(pathfd, &rights) == 0); + fd = openat(pathfd, "", O_RDONLY | O_EMPTY_PATH); + ATF_REQUIRE_ERRNO(ENOTCAPABLE, fd == -1); + + CHECKED_CLOSE(pathfd); + CHECKED_CLOSE(pathdfd); +} + /* Make sure that ptrace(PT_COREDUMP) cannot be used to write to a path fd. */ ATF_TC_WITHOUT_HEAD(path_coredump); ATF_TC_BODY(path_coredump, tc) @@ -937,6 +1011,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, path_access); ATF_TP_ADD_TC(tp, path_aio); ATF_TP_ADD_TC(tp, path_capsicum); + ATF_TP_ADD_TC(tp, path_capsicum_empty); ATF_TP_ADD_TC(tp, path_coredump); ATF_TP_ADD_TC(tp, path_directory); ATF_TP_ADD_TC(tp, path_directory_not_root);