Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2198618imm; Mon, 28 May 2018 03:46:36 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLZpRXTJ3l3JgNmeqyMe/mycnmLdaUZZ1HjxsxwxGnVgv81MKCdulQN7CaKELZGfRSlX3OX X-Received: by 2002:a62:a0c:: with SMTP id s12-v6mr8402068pfi.33.1527504396847; Mon, 28 May 2018 03:46:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527504396; cv=none; d=google.com; s=arc-20160816; b=PUM0OJpeVINXhpe4VhhmCf459a7yLnu5Od8GN06/R/Z3rlpeaSS6zBPfucuxxqB9Ll 4PNGnSM81G5lkHa6sl5xnvFQhJPVqJ9gB9vMfhlJ9zdSFXLHCRkHPGj8MU1inuF8YZSW H/3FowFvZLUaNfsekyRM34w61KPdwaOPcsKdN5uBsFcu72ch2ya15jBE4JTQJRNQuLsy 26qfUFn6HGBg/YAh65WToLvMj2JQ+XW72Znn76MXwAtR2tdXpTeStI+e/HWUGp78mz+M b00LOXRPY3n6Mj5r2lTqLuKezZHKdk09Bzxc6c0A9qxfEElrZhIqCx7xl3hb6iwFSfBf bpxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=e73lK4qzK1UkOO92A2DO/Ek4ZhMNhaKTeY8oaJixEAg=; b=lbUCaoFTIZpC1VaFbMLRv69RnKFq0qlePbk8Z2jlUataFcP242s+rASd1OynmoyfyE 1V8HURN1wmNbNtI2bzWl8lN8OIS85ZgTPN7LC3eETAxkyeTabrAToWtoksxIJLwlbqqd F+efOBIFblB0JFBrrbcbo/kDjV3u1/L81DJ5d8fSxfUMm2B9BktP+H+sl4LLy5XWRQhv C39MaLwUoLD8gCMHYjplTzm1BqKO/HSvUqMxspe6V8uoXFxlAMcK3IHQOwPfjqdlcMHi RGx0kydoqEZjyFBMBWDPHxhkmvBEGXrWiNs3FFuaGEZNS6+ma4N3ZtMgPmN8r+UoJkQB +xNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Xv8oVHsh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o2-v6si28402119plk.527.2018.05.28.03.46.22; Mon, 28 May 2018 03:46:36 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Xv8oVHsh; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1163038AbeE1KqQ (ORCPT + 99 others); Mon, 28 May 2018 06:46:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:36540 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162857AbeE1KqM (ORCPT ); Mon, 28 May 2018 06:46:12 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9FF25204EE; Mon, 28 May 2018 10:46:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527504372; bh=2VFh040DoOqzchGfFaqTuol0VoNgt4vZXv9LsRgt7/Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Xv8oVHshufP/xr4IEcSbVegJ72wPmyilobOq2/HBbjY8kxt4PFItL3ISFEalVtnrU MoVttad94Kz2YHPBJQyVBogwOgzl64lDgmHtHLyCAMi0g4sHYNWF/VEqfPLTsnqg77 iR16Vp6iU82JOJd5+4vdk1daz0whHI/YF2zSWlR8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tycho Andersen , Kees Cook , Sasha Levin Subject: [PATCH 4.14 115/496] seccomp: add a selftest for get_metadata Date: Mon, 28 May 2018 11:58:20 +0200 Message-Id: <20180528100324.761976897@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100319.498712256@linuxfoundation.org> References: <20180528100319.498712256@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tycho Andersen [ Upstream commit d057dc4e35e16050befa3dda943876dab39cbf80 ] Let's test that we get the flags correctly, and that we preserve the filter index across the ptrace(PTRACE_SECCOMP_GET_METADATA) correctly. Signed-off-by: Tycho Andersen CC: Kees Cook Signed-off-by: Kees Cook Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- tools/testing/selftests/seccomp/seccomp_bpf.c | 61 ++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) --- a/tools/testing/selftests/seccomp/seccomp_bpf.c +++ b/tools/testing/selftests/seccomp/seccomp_bpf.c @@ -145,6 +145,15 @@ struct seccomp_data { #define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2) #endif +#ifndef PTRACE_SECCOMP_GET_METADATA +#define PTRACE_SECCOMP_GET_METADATA 0x420d + +struct seccomp_metadata { + __u64 filter_off; /* Input: which filter */ + __u64 flags; /* Output: filter's flags */ +}; +#endif + #ifndef seccomp int seccomp(unsigned int op, unsigned int flags, void *args) { @@ -2861,6 +2870,58 @@ TEST(get_action_avail) EXPECT_EQ(errno, EOPNOTSUPP); } +TEST(get_metadata) +{ + pid_t pid; + int pipefd[2]; + char buf; + struct seccomp_metadata md; + + ASSERT_EQ(0, pipe(pipefd)); + + pid = fork(); + ASSERT_GE(pid, 0); + if (pid == 0) { + struct sock_filter filter[] = { + BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW), + }; + struct sock_fprog prog = { + .len = (unsigned short)ARRAY_SIZE(filter), + .filter = filter, + }; + + /* one with log, one without */ + ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, + SECCOMP_FILTER_FLAG_LOG, &prog)); + ASSERT_EQ(0, seccomp(SECCOMP_SET_MODE_FILTER, 0, &prog)); + + ASSERT_EQ(0, close(pipefd[0])); + ASSERT_EQ(1, write(pipefd[1], "1", 1)); + ASSERT_EQ(0, close(pipefd[1])); + + while (1) + sleep(100); + } + + ASSERT_EQ(0, close(pipefd[1])); + ASSERT_EQ(1, read(pipefd[0], &buf, 1)); + + ASSERT_EQ(0, ptrace(PTRACE_ATTACH, pid)); + ASSERT_EQ(pid, waitpid(pid, NULL, 0)); + + md.filter_off = 0; + ASSERT_EQ(sizeof(md), ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md)); + EXPECT_EQ(md.flags, SECCOMP_FILTER_FLAG_LOG); + EXPECT_EQ(md.filter_off, 0); + + md.filter_off = 1; + ASSERT_EQ(sizeof(md), ptrace(PTRACE_SECCOMP_GET_METADATA, pid, sizeof(md), &md)); + EXPECT_EQ(md.flags, 0); + EXPECT_EQ(md.filter_off, 1); + + ASSERT_EQ(0, kill(pid, SIGKILL)); +} + /* * TODO: * - add microbenchmarks