Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp189152iog; Sun, 12 Jun 2022 23:44:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzG5YgN/uQ5GYQUvX4/mgKLjHAiz0LA8wXbT/tmB41MD5dKIyFmxvLIK/yuSEDROWabNizf X-Received: by 2002:a17:907:1b0d:b0:6ff:22a1:d58a with SMTP id mp13-20020a1709071b0d00b006ff22a1d58amr53575186ejc.293.1655102688037; Sun, 12 Jun 2022 23:44:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655102688; cv=none; d=google.com; s=arc-20160816; b=avDx0sT934H5j93uZGxFqSfBDBAYRXH62sCeWITqCv/0UtTuoovUKQq0OFLy2X+1et lqSrBmDSZB5RsHzLUW7LcExghRxXD8RGAHAk7HCrSnrxTVW0nHEpgzTNNrMl8KFsqsrx yS+KV5zhpt8qFfAEc1yG3gYXQSQNZ4WAKWHIvaanKVAiHscwMZAMf6jWo2jV0C/HpXWB fNdnp10RQzpZPO6t2HXesAW6hhPo7n1u/bPRYW2TXvsHUqTcdVYISRsdD1VQ69X/94Ki eRk3254q/QgzuStnJIJxtVSl5S6xzlz6S1DM1OpcKVaWwj/q9IcuijYenja6ce0B28L4 xOKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8rpGhRZe7OD9xjNF1m78RIAAcrJRhCm5pjhxYsieoHk=; b=Q60Yj+dz/pWGTQjxov9hQDdXrE26TPcYKDs/7xX8bND2bbJG57cDzOnl9iFJbekaLo 3N+RUpEygx7o9TLEPIUZGjs1tRJBm+v1QmqpY1X1u5i7wUyVcgPxEI+cThMPAx6XS8bq eoS0H9eZHS7Bxn3WZOIX/RrIwJ48PUN2FYZ8D2wRl0NGPrey6dvnGfDv45Gt9y0zey8+ jD0gA/y4IlzJjkla/+T25c0TJUPKFO54DdIerGFq/Nx0cu1WOonYDzCnO5cGrgntQUqP OdTMXxAhcf62dudSNZoNaGkGLxcnq9w3Iop0qPYSg+Qg+l19gkZ2ySDab2SONVmgZPcN 61bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qw13uZoq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hv13-20020a17090760cd00b0070822c5a13asi7746741ejc.104.2022.06.12.23.44.22; Sun, 12 Jun 2022 23:44:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=qw13uZoq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234242AbiFMGHz (ORCPT + 99 others); Mon, 13 Jun 2022 02:07:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233955AbiFMGHn (ORCPT ); Mon, 13 Jun 2022 02:07:43 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A225B7EA for ; Sun, 12 Jun 2022 23:07:37 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id d129so4666495pgc.9 for ; Sun, 12 Jun 2022 23:07:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8rpGhRZe7OD9xjNF1m78RIAAcrJRhCm5pjhxYsieoHk=; b=qw13uZoq36bEli8mqrcMyU1ZDU0GLPkhc5EIb2TvM28HQOU6+Bp6//pg/xE2tf2Zmu C1ws5OPgHK2sMrWJuPCbrXbWfCzu+093uOcErbsEsJh5Hf1fPUiJ6O6eBFicsy/a0gZq BIN9IQXO73EoAsMeLhkWGivLl8jbP0BhRpvY0hneq8o6DtauPwc7S56XXZ8rao7JOx7E G4iPiLb1Jl5ZJeKVZlJsLDtl4KBHCH5elDbssGxKS0tLn6GwhBRxwSjlyBR1D6ksc6j9 A462hq6nvtEGRaas8A0ujrFFNUjRWqqv1YnTlBVqlaI0+nrj+d6VUDg2sOm8PeBViazt DtPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8rpGhRZe7OD9xjNF1m78RIAAcrJRhCm5pjhxYsieoHk=; b=VyTdXlj9/rAveWIjlN+Eyk1og3yzLIv6YpjACGjxCFjzOtdIYfY8HvG1Ca3z4nbJo0 5URhBHs2fQ2+mFMM2wsrkwGbAT/5KQ0OozAuzo3TVgrN6tqPyaKNqv1av2DJ/UBiszHT 6QYgy4yXKa+nZtJ7CW7Acq5pdvfGNy+XEYuXKMqsYWV+joZClRt7E4UO/klJ0sYg4ogk b5hPLJVxLirHO3EpcjCiZkIq2UWA6WyrKLKoqSZ2id+PC822kAvdHjwxySXU4RXqO4Zi Q5/tQqpn9G0tMCVT9ciQqWcBl5mJyiMtRhpMr/BSwCosvON/0N0nUouZsLTNG05A1OM8 wgRQ== X-Gm-Message-State: AOAM530zVjpNkqVhpreYOHMtq7f5wjyWVVOHwzepaQAjMrXjKd10g6xW arRnCsOboI4pw4NW8K84muW7iZ/1iOI= X-Received: by 2002:a05:6a00:a16:b0:518:ffe0:7229 with SMTP id p22-20020a056a000a1600b00518ffe07229mr93599801pfh.49.1655100456409; Sun, 12 Jun 2022 23:07:36 -0700 (PDT) Received: from laptop.hsd1.wa.comcast.net ([2601:600:8500:5f14:d627:c51e:516e:a105]) by smtp.gmail.com with ESMTPSA id p5-20020a170903248500b0015e8d4eb1c8sm420157plw.18.2022.06.12.23.07.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jun 2022 23:07:35 -0700 (PDT) From: Andrei Vagin To: linux-kernel@vger.kernel.org, Dmitry Safonov <0x7f454c46@gmail.com>, Christian Brauner Cc: Florian Weimer , linux-mm@kvack.org, Eric Biederman , Kees Cook , Andrei Vagin Subject: [PATCH 2/2] testing/timens: add a test for vfork+exit Date: Sun, 12 Jun 2022 23:07:23 -0700 Message-Id: <20220613060723.197407-2-avagin@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220613060723.197407-1-avagin@gmail.com> References: <20220613060723.197407-1-avagin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org * check that a child process is in parent's time namespace after vfork. * check that a child process is in the target namespace after exec. Output on success: $ ./vfork_exec 1..1 ok 1 exec Signed-off-by: Andrei Vagin --- tools/testing/selftests/timens/Makefile | 2 +- tools/testing/selftests/timens/vfork_exec.c | 90 +++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/vfork_exec.c diff --git a/tools/testing/selftests/timens/Makefile b/tools/testing/selftests/timens/Makefile index 3a5936cc10ab..f0d51d4d2c87 100644 --- a/tools/testing/selftests/timens/Makefile +++ b/tools/testing/selftests/timens/Makefile @@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs exec futex +TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs exec futex vfork_exec TEST_GEN_PROGS_EXTENDED := gettime_perf CFLAGS := -Wall -Werror -pthread diff --git a/tools/testing/selftests/timens/vfork_exec.c b/tools/testing/selftests/timens/vfork_exec.c new file mode 100644 index 000000000000..e6ccd900f30a --- /dev/null +++ b/tools/testing/selftests/timens/vfork_exec.c @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "timens.h" + +#define OFFSET (36000) + +int main(int argc, char *argv[]) +{ + struct timespec now, tst; + int status, i; + pid_t pid; + + if (argc > 1) { + if (sscanf(argv[1], "%ld", &now.tv_sec) != 1) + return pr_perror("sscanf"); + + for (i = 0; i < 2; i++) { + _gettime(CLOCK_MONOTONIC, &tst, i); + if (abs(tst.tv_sec - now.tv_sec) > 5) + return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec); + } + return 0; + } + + nscheck(); + + ksft_set_plan(1); + + clock_gettime(CLOCK_MONOTONIC, &now); + + if (unshare_timens()) + return 1; + + if (_settime(CLOCK_MONOTONIC, OFFSET)) + return 1; + + for (i = 0; i < 2; i++) { + _gettime(CLOCK_MONOTONIC, &tst, i); + if (abs(tst.tv_sec - now.tv_sec) > 5) + return pr_fail("%ld %ld\n", + now.tv_sec, tst.tv_sec); + } + + pid = vfork(); + if (pid < 0) + return pr_perror("fork"); + + if (pid == 0) { + char now_str[64]; + char *cargv[] = {"exec", now_str, NULL}; + char *cenv[] = {NULL}; + + // Check that we are still in the source timens. + for (i = 0; i < 2; i++) { + _gettime(CLOCK_MONOTONIC, &tst, i); + if (abs(tst.tv_sec - now.tv_sec) > 5) + return pr_fail("%ld %ld\n", + now.tv_sec, tst.tv_sec); + } + + /* Check for proper vvar offsets after execve. */ + snprintf(now_str, sizeof(now_str), "%ld", now.tv_sec + OFFSET); + execve("/proc/self/exe", cargv, cenv); + return pr_perror("execve"); + } + + if (waitpid(pid, &status, 0) != pid) + return pr_perror("waitpid"); + + if (status) + ksft_exit_fail(); + + ksft_test_result_pass("exec\n"); + ksft_exit_pass(); + return 0; +} -- 2.35.1