From nobody Sun Jan 16 15:41:14 2022 X-Original-To: dev-commits-src-branches@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 92E17195DD89; Sun, 16 Jan 2022 15:41:16 +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 4JcK5746kSz3Qww; Sun, 16 Jan 2022 15:41:15 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642347676; 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=6sopTc0xDjRxKKq6xLttNoJmLLKlELPqdwoezzHCN5c=; b=JhSJYFqb0h9P1UxhGTbKO1AluJbpC7dcENo4ppAP0J+H20ZgS3xuJLk07tOo6K83yazHXh 3Pio+be12iRN+9XcdyZ6pL9L9b6PLKEQkz2XcfbWxoc7jyvJQ5Ax48ybjro5eJKE0QGNMv GOAB+MHkwHwGYEqf38Uenc0AqvkvexSo++mzOhBBe8eQUiufC8JUX/731fRdAFxI7njcZu iKLJ36678eibQWVBX/beQOV0/QqtrM1ay32RJKJSQrSnEn6Mc87I6qLkMCMGP79tITW+1/ U0uZVcgleLljzWa6IbMqV6KFF2HhX2FkmdKJNTlexGCchUu+Qe3IZZbC0szLrQ== 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 C2DFC1C4FD; Sun, 16 Jan 2022 15:41:14 +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 20GFfEPQ039188; Sun, 16 Jan 2022 15:41:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 20GFfEFi039187; Sun, 16 Jan 2022 15:41:14 GMT (envelope-from git) Date: Sun, 16 Jan 2022 15:41:14 GMT Message-Id: <202201161541.20GFfEFi039187@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: 845458a6742f - stable/13 - tests: Add some regression tests for a couple of KERN_PROC_* sysctls List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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: 845458a6742f3fc7faca61e2e318d55a19822df5 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642347676; 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=6sopTc0xDjRxKKq6xLttNoJmLLKlELPqdwoezzHCN5c=; b=yYBTwZFEYr5pOIO9A5i66mQeCONzajCf/c2MzQB3kFAUBQjDJE7AZmgy1ZkcF1uBZ9bXRH hjxYZbHTaX1Vwxwbx8m6oXTdsRmWY9hArZa5VGs5Bd8tUM8MK5qk8gY0NpT9pHY2Xz2zHw O94fVDrR22HS/USFnRKhOedh6t/VnQrRjCCkRB0ZeUSmW6MyXa8Is/kXFS5AZpYxwkuDgt oSDLk6tsLSKVZmmca0YoPYinFrrbmNgX1gHAFRHvsmo1sXBsFyWgc5Nq0MZdf7Wg5zxqD+ K6WbdLqdBFdNmRqvPeSVH77kAs+N58lUq0QP8pwSMEdwZfHOO3mDAhvRdvqGDQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1642347676; a=rsa-sha256; cv=none; b=eBhhiduqrk+6g9Ter42yyfWLdmyM4qOBiNtrXilcTtbFigcUamXCpMNBII5VJJhCSzk4Ky PlUcH/dxcwYHSCei/alo90Ghkj+6mipBVgcZzNjzS3Vjr87nE9/OywT06HjyFs29Jg3e+P DgfW8V+FjG3/E1tcDgHwrf/GE3Ds6ie14m3noRDsGgq182A6Gu/FQTNoqqjAd/Rg4x+W5E a+f+Aet2rkryTMY/D2WVvmScpw4F1gvKHjduXjSX3lvL+0JkBpQZRylCp5GaaZgmSf/FVp wQ14EF0O6reLqNFKEH25xqfDcwHj8CSFuJ1rU0bOPQEcZrQ8DoeWiLhIe04zQQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=845458a6742f3fc7faca61e2e318d55a19822df5 commit 845458a6742f3fc7faca61e2e318d55a19822df5 Author: Mark Johnston AuthorDate: 2021-12-16 21:06:55 +0000 Commit: Mark Johnston CommitDate: 2022-01-16 15:40:39 +0000 tests: Add some regression tests for a couple of KERN_PROC_* sysctls Sponsored by: The FreeBSD Foundation (cherry picked from commit fff0ae77b9960bb26034297fcb885d26432354bf) --- tests/sys/kern/Makefile | 1 + tests/sys/kern/sysctl_kern_proc.c | 199 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+) diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index b8fee0944c06..fe23803c2267 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -23,6 +23,7 @@ TEST_METADATA.ptrace_test+= timeout="15" ATF_TESTS_C+= reaper ATF_TESTS_C+= sigaltstack PLAIN_TESTS_C+= subr_unit_test +ATF_TESTS_C+= sysctl_kern_proc ATF_TESTS_C+= sys_getrandom ATF_TESTS_C+= unix_passfd_test ATF_TESTS_C+= unix_seqpacket_test diff --git a/tests/sys/kern/sysctl_kern_proc.c b/tests/sys/kern/sysctl_kern_proc.c new file mode 100644 index 000000000000..5bab1eab5fba --- /dev/null +++ b/tests/sys/kern/sysctl_kern_proc.c @@ -0,0 +1,199 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 The FreeBSD Foundation + * + * This software was developed by Mark Johnston under sponsorship from + * the FreeBSD Foundation. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +/* + * These tests exercise the KERN_PROC_* sysctls. + */ + +/* + * Loop through all valid PIDs and try to fetch info for each one. + */ +static void +sysctl_kern_proc_all(int cmd) +{ + int mib[4], pid_max; + void *buf; + size_t sz; + + sz = sizeof(pid_max); + ATF_REQUIRE(sysctlbyname("kern.pid_max", &pid_max, &sz, NULL, 0) == 0); + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = cmd; + for (int i = 1; i <= pid_max; i++) { + mib[3] = i; + + if (sysctl(mib, 4, NULL, &sz, NULL, 0) == 0) { + buf = malloc(sz); + ATF_REQUIRE(buf != NULL); + (void)sysctl(mib, 4, buf, &sz, NULL, 0); + free(buf); + } + } + + mib[3] = -1; + ATF_REQUIRE_ERRNO(ESRCH, sysctl(mib, 4, NULL, &sz, NULL, 0) != 0); +} + +/* + * Validate behaviour of the KERN_PROC_CWD sysctl. + */ +ATF_TC_WITHOUT_HEAD(sysctl_kern_proc_cwd); +ATF_TC_BODY(sysctl_kern_proc_cwd, tc) +{ + struct kinfo_file kfile; + char cwd[PATH_MAX]; + int cmd, mib[4]; + size_t sz; + pid_t child; + int status; + + cmd = KERN_PROC_CWD; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = cmd; + mib[3] = getpid(); + + /* Try querying the kernel for the output buffer size. */ + sz = 0; + ATF_REQUIRE(sysctl(mib, 4, NULL, &sz, NULL, 0) == 0); + ATF_REQUIRE(sz <= sizeof(kfile)); + + sz = sizeof(kfile); + memset(&kfile, 0, sz); + ATF_REQUIRE(sysctl(mib, 4, &kfile, &sz, NULL, 0) == 0); + ATF_REQUIRE(sz <= sizeof(kfile)); + ATF_REQUIRE(sz == (u_int)kfile.kf_structsize); + + /* Make sure that we get the same result from getcwd(2). */ + ATF_REQUIRE(getcwd(cwd, sizeof(cwd)) == cwd); + ATF_REQUIRE(strcmp(cwd, kfile.kf_path) == 0); + + /* Spot-check some of the kinfo fields. */ + ATF_REQUIRE(kfile.kf_type == KF_TYPE_VNODE); + ATF_REQUIRE(kfile.kf_fd == KF_FD_TYPE_CWD); + ATF_REQUIRE(S_ISDIR(kfile.kf_un.kf_file.kf_file_mode)); + ATF_REQUIRE((kfile.kf_status & KF_ATTR_VALID) != 0); + + /* + * Verify that a child process can get our CWD info, and that it + * matches the info we got above. + */ + child = fork(); + ATF_REQUIRE(child != -1); + if (child == 0) { + struct kinfo_file pkfile; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_CWD; + mib[3] = getppid(); + + sz = sizeof(pkfile); + memset(&pkfile, 0, sz); + if (sysctl(mib, 4, &pkfile, &sz, NULL, 0) != 0) + _exit(1); + if (memcmp(&kfile, &pkfile, sizeof(kfile)) != 0) + _exit(2); + _exit(0); + } + ATF_REQUIRE(waitpid(child, &status, 0) == child); + ATF_REQUIRE(WIFEXITED(status)); + ATF_REQUIRE(WEXITSTATUS(status) == 0); + + /* + * Truncate the output buffer ever so slightly and make sure that we get + * an error. + */ + sz--; + ATF_REQUIRE_ERRNO(ENOMEM, sysctl(mib, 4, &kfile, &sz, NULL, 0) != 0); + + sysctl_kern_proc_all(cmd); +} + +/* + * Validate behaviour of the KERN_PROC_FILEDESC sysctl. + */ +ATF_TC_WITHOUT_HEAD(sysctl_kern_proc_filedesc); +ATF_TC_BODY(sysctl_kern_proc_filedesc, tc) +{ + int cmd, fd, mib[4]; + struct kinfo_file *kfile; + char *buf, tmp[16]; + size_t sz, sz1; + + cmd = KERN_PROC_FILEDESC; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = cmd; + mib[3] = getpid(); + + sz = 0; + ATF_REQUIRE(sysctl(mib, 4, NULL, &sz, NULL, 0) == 0); + ATF_REQUIRE(sz >= __offsetof(struct kinfo_file, kf_structsize) + + sizeof(kfile->kf_structsize)); + + buf = malloc(sz); + ATF_REQUIRE(buf != NULL); + + ATF_REQUIRE(sysctl(mib, 4, buf, &sz, NULL, 0) == 0); + + /* Walk over the list of returned files. */ + for (sz1 = 0; sz1 < sz; sz1 += kfile->kf_structsize) { + kfile = (void *)(buf + sz1); + + ATF_REQUIRE((unsigned int)kfile->kf_structsize <= sz); + ATF_REQUIRE((unsigned int)kfile->kf_structsize + sz1 <= sz); + + ATF_REQUIRE((kfile->kf_status & KF_ATTR_VALID) != 0); + } + /* We shouldn't have any trailing bytes. */ + ATF_REQUIRE(sz1 == sz); + + /* + * Open a file. This increases the size of the output buffer, so an + * attempt to re-fetch the records without increasing the buffer size + * should fail with ENOMEM. + */ + snprintf(tmp, sizeof(tmp), "tmp.XXXXXX"); + fd = mkstemp(tmp); + ATF_REQUIRE(fd >= 0); + ATF_REQUIRE_ERRNO(ENOMEM, sysctl(mib, 4, buf, &sz, NULL, 0) != 0); + + ATF_REQUIRE(unlink(tmp) == 0); + ATF_REQUIRE(close(fd) == 0); + + free(buf); + + sysctl_kern_proc_all(cmd); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, sysctl_kern_proc_cwd); + ATF_TP_ADD_TC(tp, sysctl_kern_proc_filedesc); + + return (atf_no_error()); +}