Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp726729ybb; Wed, 25 Mar 2020 08:27:34 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtfC2JgWGT+KxnY7abA5taooqciO8g/97Cl2NJCak6QoKb7LigagksP3CAjrBtGikOrHvCT X-Received: by 2002:aca:f07:: with SMTP id 7mr1159882oip.68.1585150054706; Wed, 25 Mar 2020 08:27:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585150054; cv=none; d=google.com; s=arc-20160816; b=jgOLQ7EqExhYj8DHEDzGSxEw9eMdcw9DSFnliu5jcS30xXJAz8Aso2mn/RQlEZFr1t pOTBsYrlQ4qTo/B/MVO1XXmtvx86TRrP6/AEVTrxdsZROkiln4zMWYY5J3NuEKeED6O6 bvzJFLTQso4tA30/K9lqk3byI++8Pac3VYfoY5UiHm/GpMbvo/MJdI6bxBhAcVMli9aW /E8v6sdfwQinh70xcKugHXrxwwCT5/tHK0c5mdvfvkiiRhn7vW0sswIVynmUT9NIx4lY 1BZ9z+N7tJnbQLcxd3b2sYqssTTEKnEAEFtE65/Bvsb2Y0lk5thYz95fjwtgEXCISkOn jUpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=KUtsYuGYKCYnHCpe+G8ZI21IpxgMl/6XL2lPlxEc7PQ=; b=Cu4LoM/vFNuAyOMCj100e32HLRDsNKAZLBifWdqCEFpW6O6urLDixl0iFiNKLw7bNU QthTvRAY5Zpe6UPTl/S27rUvCtREB746/JE75/7xCKg9qfpPAJ7fSwW9lrx2oX7/01mX 2nGrBE2D9TUmPQ32peMXX/1CNxPuIWiNxZWlzEuIga/9tQeZnczgvHCjePWEuvb0/IDl 897yFzBx6E64wft/ljZAaJBWRAlOPfY29sP+go2MG+JvtXJdjVIAVl9fWcqfeO/GW8DH peKe1QwrG5etZBPi5bKwAcVrSVmZOVyNYuLPVxiLtogDd+jrVnZkOgT92rNSVvGqg+gW DnMw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eyGF1fcG; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y5si10730983oiy.150.2020.03.25.08.27.22; Wed, 25 Mar 2020 08:27:34 -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=@chromium.org header.s=google header.b=eyGF1fcG; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727910AbgCYP04 (ORCPT + 99 others); Wed, 25 Mar 2020 11:26:56 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:40502 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727872AbgCYP0x (ORCPT ); Wed, 25 Mar 2020 11:26:53 -0400 Received: by mail-wm1-f65.google.com with SMTP id a81so3162618wmf.5 for ; Wed, 25 Mar 2020 08:26:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KUtsYuGYKCYnHCpe+G8ZI21IpxgMl/6XL2lPlxEc7PQ=; b=eyGF1fcGtLWXdFQodSYLnV4ZWus0OSDECroTFkU9WdxSUpRHpZWheobCOnYGRSQ8iK C1vMsD6lBY+HOyp6F+x+YPrinoes8EgGz4RfZkt13nfaj77lNMjXnuxfMeNeyXkjL8aF V9iTF23rbCtMuDA9wqJgzT8pLm/vx13nQ0O+E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KUtsYuGYKCYnHCpe+G8ZI21IpxgMl/6XL2lPlxEc7PQ=; b=uI1PGemHr0L7y5aRvOIkJFgkE94quMrMbx5u4AK/+4RNB/+XIbigOwV1aC6a1/IPUZ Wloq3sOIBOygbx3gjcDZX+QmW1PnzFOqw3cV/fnM2Wu2sqaERJkhaTDrJsXj05ycVqp7 9oEi2ZY74jVWNMkwhc/ryvuaF1hK9jmdwUUVumFTvEI5VL/z5Ax4DKQUep56liqTiS80 ZfbN1H1+hWzFHptCaqYcJJ2H8lO2L+u0+o6zHylwfPaNlIH/zB2SkTZn8vUtzvFfOxrb GjyS9Kd31Asgklh+Q9LyyIkSleT68Sf41TdA022h0THvWciPsbYe8ee0uCN0XR4JK1Qz MOxg== X-Gm-Message-State: ANhLgQ2HbQWJ8zzm3zVSQEtuS2cPrLN9OgcAOpvbE7f4gj5Om/1VYjf8 +Rv5i9YUpZPDhBZ6pyeiaoyJjuHi+xc= X-Received: by 2002:a7b:c404:: with SMTP id k4mr3771173wmi.37.1585150010996; Wed, 25 Mar 2020 08:26:50 -0700 (PDT) Received: from kpsingh-kernel.localdomain (77-56-209-237.dclient.hispeed.ch. [77.56.209.237]) by smtp.gmail.com with ESMTPSA id a2sm4033701wrp.13.2020.03.25.08.26.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Mar 2020 08:26:50 -0700 (PDT) From: KP Singh To: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-security-module@vger.kernel.org Cc: Brendan Jackman , Florent Revest , Thomas Garnier , Alexei Starovoitov , Daniel Borkmann , James Morris , Kees Cook , Paul Turner , Jann Horn , Florent Revest , Brendan Jackman , Greg Kroah-Hartman Subject: [PATCH bpf-next v6 7/8] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM Date: Wed, 25 Mar 2020 16:26:28 +0100 Message-Id: <20200325152629.6904-8-kpsingh@chromium.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200325152629.6904-1-kpsingh@chromium.org> References: <20200325152629.6904-1-kpsingh@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KP Singh * Load/attach a BPF program that hooks to file_mprotect (int) and bprm_committed_creds (void). * Perform an action that triggers the hook. * Verify if the audit event was received using the shared global variables for the process executed. * Verify if the mprotect returns a -EPERM. Signed-off-by: KP Singh Reviewed-by: Brendan Jackman Reviewed-by: Florent Revest Reviewed-by: Thomas Garnier --- tools/testing/selftests/bpf/config | 2 + .../selftests/bpf/prog_tests/test_lsm.c | 84 +++++++++++++++++++ tools/testing/selftests/bpf/progs/lsm.c | 48 +++++++++++ 3 files changed, 134 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_lsm.c create mode 100644 tools/testing/selftests/bpf/progs/lsm.c diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config index 5dc109f4c097..60e3ae5d4e48 100644 --- a/tools/testing/selftests/bpf/config +++ b/tools/testing/selftests/bpf/config @@ -35,3 +35,5 @@ CONFIG_MPLS_ROUTING=m CONFIG_MPLS_IPTUNNEL=m CONFIG_IPV6_SIT=m CONFIG_BPF_JIT=y +CONFIG_BPF_LSM=y +CONFIG_SECURITY=y diff --git a/tools/testing/selftests/bpf/prog_tests/test_lsm.c b/tools/testing/selftests/bpf/prog_tests/test_lsm.c new file mode 100644 index 000000000000..eb5f5a017500 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_lsm.c @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (C) 2020 Google LLC. + */ + +#include +#include +#include +#include +#include +#include + +#include "lsm.skel.h" + +char *CMD_ARGS[] = {"true", NULL}; + +int heap_mprotect(void) +{ + void *buf; + long sz; + int ret; + + sz = sysconf(_SC_PAGESIZE); + if (sz < 0) + return sz; + + buf = memalign(sz, 2 * sz); + if (buf == NULL) + return -ENOMEM; + + ret = mprotect(buf, sz, PROT_READ | PROT_EXEC); + free(buf); + return ret; +} + +int exec_cmd(int *monitored_pid) +{ + int child_pid; + + child_pid = fork(); + if (child_pid == 0) { + *monitored_pid = getpid(); + execvp(CMD_ARGS[0], CMD_ARGS); + return -EINVAL; + } else if (child_pid > 0) + return wait(NULL); + + return -EINVAL; +} + +void test_test_lsm(void) +{ + struct lsm *skel = NULL; + int err, duration = 0; + + skel = lsm__open_and_load(); + if (CHECK(!skel, "skel_load", "lsm skeleton failed\n")) + goto close_prog; + + err = lsm__attach(skel); + if (CHECK(err, "attach", "lsm attach failed: %d\n", err)) + goto close_prog; + + err = exec_cmd(&skel->bss->monitored_pid); + if (CHECK(err < 0, "exec_cmd", "err %d errno %d\n", err, errno)) + goto close_prog; + + CHECK(skel->bss->bprm_count != 1, "bprm_count", "bprm_count = %d", + skel->bss->bprm_count); + + skel->bss->monitored_pid = getpid(); + + err = heap_mprotect(); + if (CHECK(errno != EPERM, "heap_mprotect", "want errno=EPERM, got %d\n", + errno)) + goto close_prog; + + CHECK(skel->bss->mprotect_count != 1, "mprotect_count", + "mprotect_count = %d", skel->bss->mprotect_count); + +close_prog: + lsm__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/lsm.c b/tools/testing/selftests/bpf/progs/lsm.c new file mode 100644 index 000000000000..a4e3c223028d --- /dev/null +++ b/tools/testing/selftests/bpf/progs/lsm.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright 2020 Google LLC. + */ + +#include "vmlinux.h" +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +int monitored_pid = 0; +int mprotect_count = 0; +int bprm_count = 0; + +SEC("lsm/file_mprotect") +int BPF_PROG(test_int_hook, struct vm_area_struct *vma, + unsigned long reqprot, unsigned long prot, int ret) +{ + if (ret != 0) + return ret; + + __u32 pid = bpf_get_current_pid_tgid() >> 32; + int is_heap = 0; + + is_heap = (vma->vm_start >= vma->vm_mm->start_brk && + vma->vm_end <= vma->vm_mm->brk); + + if (is_heap && monitored_pid == pid) { + mprotect_count++; + ret = -EPERM; + } + + return ret; +} + +SEC("lsm/bprm_committed_creds") +int BPF_PROG(test_void_hook, struct linux_binprm *bprm) +{ + __u32 pid = bpf_get_current_pid_tgid() >> 32; + + if (monitored_pid == pid) + bprm_count++; + + return 0; +} -- 2.20.1