Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp771431ybl; Thu, 23 Jan 2020 07:27:56 -0800 (PST) X-Google-Smtp-Source: APXvYqzQQVRnxvx0+CbciVXy0bE+uTkeZl0A8kGef+P85lhQPZ1pr+/UsawYIjVA1SUiwJOg6YbH X-Received: by 2002:a05:6830:108b:: with SMTP id y11mr968887oto.143.1579793276499; Thu, 23 Jan 2020 07:27:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579793276; cv=none; d=google.com; s=arc-20160816; b=J5xnTK6cCKT4p6mzW+ldN3r6x9s/UGDq4XD9sxOgyBmYISLcYyUebr8cHxgjdRVHyH n6J2GzKeWICczkiq0Srl8m8TOa4Pu2HCw11BGMYwURth6YHxMnD7gI5ru/WadaAvxpTo wr012ZdDGXw7/PCe3maPQLWO9A4NTpZSbIhgsI6qtoWJ9ExYCL9coW65NfxBo0o9kfm8 cwhcOVuDE+W/1jN9P2G2WjOOqGclQ/6Twf4ja9XwJ0oHdVHwF0TKjdLySFZ7HhwSc1+g 837aNpUi8MrJ6j+KWDko7OOzpHGgXOsEVjQAVmgdWrFiawmaGyCiXv7PvzyBp+XiqW/f 3iAA== 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=MmRJe3DElaN6x34LdFhi955VR7hntKawxO/WlNWMHYE=; b=uKRtoqAOpAj6J0goFzIZN1MxGuRSuzdjvZ4KiyQBGygDecn45qaw8XNoBk3EbhKEZx amq0ChMckV2+xb5H7xOJtkpxbnsIrhWu80eRw3K+XURadC2A0OzhmnkWi+S0a+wq80Ag tTAr//jRwJhIy3z6zlD1+OX4tfB44rKHArPi7BOKmf1vhGGTQ9vh3Gq3IoccLmyffqej pL2vaVjMk8EUwugbpzAuQZy7skzWRaX/qcNMNAIq3Etf4hMQ1mreTzYkaGe8O/OuOSwH gsGVkWpWSEL7B8vW9DmT/ZCBdQVm9uGi8LfSF1yurfXgION6uyAzfWP6ULltLPk9XvOo N+3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AUMGBbBR; 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 f189si922279oib.268.2020.01.23.07.27.44; Thu, 23 Jan 2020 07:27:56 -0800 (PST) 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=AUMGBbBR; 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 S1729275AbgAWPZ3 (ORCPT + 99 others); Thu, 23 Jan 2020 10:25:29 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:38251 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728665AbgAWPZ0 (ORCPT ); Thu, 23 Jan 2020 10:25:26 -0500 Received: by mail-pf1-f193.google.com with SMTP id x185so1696279pfc.5 for ; Thu, 23 Jan 2020 07:25:26 -0800 (PST) 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=MmRJe3DElaN6x34LdFhi955VR7hntKawxO/WlNWMHYE=; b=AUMGBbBRa+ZQXAPMJHFivBlNtYgG7SGyNNJddvjw0j9t5D+tl6WBpRtKwA00qZpp6I UBeptFdGetKXP781nQN6RIvmcL1Sv/j6lri1XRHEikkCMl3JgSnbte0w23AH6tk8nJN6 5MQ3BJjv+XiBHauGdU02oiWrDMmlmHwbYOHr4= 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=MmRJe3DElaN6x34LdFhi955VR7hntKawxO/WlNWMHYE=; b=B4Kej2t4IzwgGzTIY9TIb/huq3tS+yEGkLqFZKOEJhZhjPXWa3nYmiwDJJ8viKN5Ji ERnCACASCJlKVJ7C7ABs/NX4wkVxVt+ztZo0UKPIs+LuHc3pNY7q6It+AMOF202PZOuY FoL+HamvISGTynpFj50RzS68m/XBARb7JlltYnU2PF6DnS8hGcEJiLEmg7UEdTH1fL1u 8m5erlZjLTIZfmMLSndBLrmWeHeii+DBslVW8+i+/Rf4tJjihsPjBOSOBdCvazaX9Wcg nemsm5Te7oHaBCcqtgSFnbw+4ze6z6Wo88dWO59D9i6glrGjNTMOS1UsDVY1UQabP1hF laNA== X-Gm-Message-State: APjAAAVdxAfjAKOU87wenTmFmiLcc7DDIpxc/67yUKFLIqfUn6onc0Lm 6T3l7zh/JgJPJnrlWQ2jaciEWDRmDa4Hlg== X-Received: by 2002:a62:7c58:: with SMTP id x85mr4551590pfc.76.1579793125244; Thu, 23 Jan 2020 07:25:25 -0800 (PST) Received: from kpsingh-kernel.localdomain ([2a00:79e1:abc:122:bd8d:3f7b:87f7:16d1]) by smtp.gmail.com with ESMTPSA id v5sm3108118pfn.122.2020.01.23.07.25.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2020 07:25:24 -0800 (PST) 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 , Thomas Garnier , Michael Halcrow , Paul Turner , Brendan Gregg , Jann Horn , Matthew Garrett , Christian Brauner , =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Florent Revest , Brendan Jackman , Martin KaFai Lau , Song Liu , Yonghong Song , "Serge E. Hallyn" , Mauro Carvalho Chehab , "David S. Miller" , Greg Kroah-Hartman , Nicolas Ferre , Stanislav Fomichev , Quentin Monnet , Andrey Ignatov , Joe Stringer Subject: [PATCH bpf-next v3 09/10] bpf: lsm: Add selftests for BPF_PROG_TYPE_LSM Date: Thu, 23 Jan 2020 07:24:39 -0800 Message-Id: <20200123152440.28956-10-kpsingh@chromium.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200123152440.28956-1-kpsingh@chromium.org> References: <20200123152440.28956-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 a BPF program that audits mprotect calls * Attach the program to the "file_mprotect" LSM hook. * Do an mprotect on some memory allocated on the heap * Verify if the audit event was received using the shared global result variable. Signed-off-by: KP Singh Reviewed-by: Brendan Jackman Reviewed-by: Florent Revest Reviewed-by: Thomas Garnier --- MAINTAINERS | 3 + tools/testing/selftests/bpf/lsm_helpers.h | 19 ++++++ .../bpf/prog_tests/lsm_mprotect_audit.c | 58 +++++++++++++++++++ .../selftests/bpf/progs/lsm_mprotect_audit.c | 47 +++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 tools/testing/selftests/bpf/lsm_helpers.h create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c diff --git a/MAINTAINERS b/MAINTAINERS index c606b3d89992..32236d89d00b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -3210,6 +3210,9 @@ L: bpf@vger.kernel.org S: Maintained F: security/bpf/ F: include/linux/bpf_lsm.h +F: tools/testing/selftests/bpf/lsm_helpers.h +F: tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c +F: tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c BROADCOM B44 10/100 ETHERNET DRIVER M: Michael Chan diff --git a/tools/testing/selftests/bpf/lsm_helpers.h b/tools/testing/selftests/bpf/lsm_helpers.h new file mode 100644 index 000000000000..8bad08f77654 --- /dev/null +++ b/tools/testing/selftests/bpf/lsm_helpers.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +/* + * Copyright 2019 Google LLC. + */ +#ifndef _LSM_HELPERS_H +#define _LSM_HELPERS_H + +struct lsm_mprotect_audit_result { + /* This ensures that the LSM Hook only monitors the PID requested + * by the loader + */ + __u32 monitored_pid; + /* The number of mprotect calls for the monitored PID. + */ + __u32 mprotect_count; +}; + +#endif /* _LSM_HELPERS_H */ diff --git a/tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c b/tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c new file mode 100644 index 000000000000..ff90b874eafc --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/lsm_mprotect_audit.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright 2019 Google LLC. + */ + +#include +#include +#include +#include +#include "lsm_helpers.h" +#include "lsm_mprotect_audit.skel.h" + +int heap_mprotect(void) +{ + void *buf; + long sz; + + sz = sysconf(_SC_PAGESIZE); + if (sz < 0) + return sz; + + buf = memalign(sz, 2 * sz); + if (buf == NULL) + return -ENOMEM; + + return mprotect(buf, sz, PROT_READ | PROT_EXEC); +} + +void test_lsm_mprotect_audit(void) +{ + struct lsm_mprotect_audit_result *result; + struct lsm_mprotect_audit *skel = NULL; + int err, duration = 0; + + skel = lsm_mprotect_audit__open_and_load(); + if (CHECK(!skel, "skel_load", "lsm_mprotect_audit skeleton failed\n")) + goto close_prog; + + err = lsm_mprotect_audit__attach(skel); + if (CHECK(err, "attach", "lsm_mprotect_audit attach failed: %d\n", err)) + goto close_prog; + + result = &skel->bss->result; + result->monitored_pid = getpid(); + + err = heap_mprotect(); + if (CHECK(err < 0, "heap_mprotect", "err %d errno %d\n", err, errno)) + goto close_prog; + + /* Make sure mprotect_audit program was triggered + * and detected an mprotect on the heap. + */ + CHECK_FAIL(result->mprotect_count != 1); + +close_prog: + lsm_mprotect_audit__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c b/tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c new file mode 100644 index 000000000000..231cd64fd0f7 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/lsm_mprotect_audit.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright 2019 Google LLC. + */ + +#include +#include +#include "bpf_trace_helpers.h" +#include "lsm_helpers.h" + +char _license[] SEC("license") = "GPL"; + +struct lsm_mprotect_audit_result result = { + .mprotect_count = 0, + .monitored_pid = 0, +}; + +/* + * Define some of the structs used in the BPF program. + * Only the field names and their sizes need to be the + * same as the kernel type, the order is irrelevant. + */ +struct mm_struct { + unsigned long start_brk, brk; +} __attribute__((preserve_access_index)); + +struct vm_area_struct { + unsigned long vm_start, vm_end; + struct mm_struct *vm_mm; +} __attribute__((preserve_access_index)); + +SEC("lsm/file_mprotect") +int BPF_PROG(mprotect_audit, struct vm_area_struct *vma, + unsigned long reqprot, unsigned long prot) +{ + __u32 pid = bpf_get_current_pid_tgid(); + int is_heap = 0; + + is_heap = (vma->vm_start >= vma->vm_mm->start_brk && + vma->vm_end <= vma->vm_mm->brk); + + if (is_heap && result.monitored_pid == pid) + result.mprotect_count++; + + return 0; +} -- 2.20.1