Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp798045pxb; Thu, 25 Feb 2021 15:48:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJwoQE9jUecXb6Jtvc0OgzT75bOQ/hrcdaFOoSETZdhPLRhDxqldirWMrxOwiPo/zy0v6hh1 X-Received: by 2002:a17:906:8043:: with SMTP id x3mr104007ejw.149.1614296880167; Thu, 25 Feb 2021 15:48:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614296880; cv=none; d=google.com; s=arc-20160816; b=rfHFB0sFPU68vMGZjeVOWeWe0nfJJ7pFFRWcG9ZsbfWfxzW0LnIqIara0XXB+ilRLp YN0q3ysAv7+yskgZJA7dW2JnWeoL/0FfffY1g/qBiCR+o1Oenr0wlP24rlds2Wfpke0F 4d9YbR4bkb2KODcyX8qPAC8RsXh9OhlEH2iw4YYHuuCTgD6iYFwiSZYKiIfiV/TYOr7b zgLqWZwOVyGCxDRAadUCGaGBODUyqEWJDe0r3Ylm6wFbEIfEmkgijqYGYHrlVNehjqKK SGmTM8UZIAuJAgNWIEJxEusUTDsn852ZhdMKquUU/HecangosgoOC+598c7C+yvZtT5o qdzw== 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=3mEGsO0haNbHvy2sWt56CvAmLVAanMmXJKhfoJxZJLc=; b=fvSfv5zeT9UMpFUReODJSlOuNgY4Wu2Cs54b9mG0NFVcVDhBc9d/Dqd0eJkjW9FSRr 2ybZ53bpnCa4GJxm+m8qGQlbBYsvV0TXK6dHeOAOxymXLvc/6ltcGg3Xe8sbMx3H5zDV CB50fmAiuYdmunG705kIJz/mBvbDxuisv/fH4EDEhI+EyZTvscRZveO3zTA/W5MyX9qr 5ieRu3BnsGrHD/wrNLhFL8jC75b9MnZIGa05HvJW82nMvgMnGos5LxKj1Th2RswYQk/P jZ6FF4TdvnUprWVBpVHBpbumZui5JqRDYdVxaDf+RCJtpKYXOj/wU7vzhGUCj1c0f8Kk p0XA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=XfWejFy6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s17si4504406eja.463.2021.02.25.15.47.37; Thu, 25 Feb 2021 15:48:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=XfWejFy6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233068AbhBYXo4 (ORCPT + 99 others); Thu, 25 Feb 2021 18:44:56 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:22202 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232735AbhBYXob (ORCPT ); Thu, 25 Feb 2021 18:44:31 -0500 Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 11PNhgQ7014115 for ; Thu, 25 Feb 2021 15:43:49 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=3mEGsO0haNbHvy2sWt56CvAmLVAanMmXJKhfoJxZJLc=; b=XfWejFy66PNHTLrzN0aT4ME8d3YyRoY0SpnMA8eIJS89W44TQC4wW5Z+y6cmA84DBI7B 3cTRDs2Sh8+R0rR4e5unNuRNMJ6uHzqslV4Dn3zHdHek2p6SBe9ddvtXAwKLvz6N9/37 MdS4+DI9EHkwQcWx4esQ0KVEfXKvVqH5HZE= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 36x96bvguh-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 25 Feb 2021 15:43:49 -0800 Received: from intmgw006.03.ash8.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:11d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Thu, 25 Feb 2021 15:43:45 -0800 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 7C59F62E1BF5; Thu, 25 Feb 2021 15:43:41 -0800 (PST) From: Song Liu To: , , CC: , , , , Song Liu Subject: [PATCH v6 bpf-next 3/6] selftests/bpf: add non-BPF_LSM test for task local storage Date: Thu, 25 Feb 2021 15:43:16 -0800 Message-ID: <20210225234319.336131-4-songliubraving@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20210225234319.336131-1-songliubraving@fb.com> References: <20210225234319.336131-1-songliubraving@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-02-25_15:2021-02-24,2021-02-25 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 spamscore=0 lowpriorityscore=0 suspectscore=0 bulkscore=0 phishscore=0 mlxscore=0 adultscore=0 malwarescore=0 impostorscore=0 priorityscore=1501 mlxlogscore=999 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2102250180 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Task local storage is enabled for tracing programs. Add two tests for task local storage without CONFIG_BPF_LSM. The first test stores a value in sys_enter and read it back in sys_exit. The second test checks whether the kernel allows allocating task local storage in exit_creds() (which it should not). Signed-off-by: Song Liu --- .../bpf/prog_tests/task_local_storage.c | 69 +++++++++++++++++++ .../selftests/bpf/progs/task_local_storage.c | 64 +++++++++++++++++ .../bpf/progs/task_local_storage_exit_creds.c | 32 +++++++++ 3 files changed, 165 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_local_sto= rage.c create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage.= c create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_= exit_creds.c diff --git a/tools/testing/selftests/bpf/prog_tests/task_local_storage.c = b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c new file mode 100644 index 0000000000000..dbb7525cdd567 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_local_storage.c @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#define _GNU_SOURCE /* See feature_test_macros(7) */ +#include +#include /* For SYS_xxx definitions */ +#include +#include +#include "task_local_storage.skel.h" +#include "task_local_storage_exit_creds.skel.h" + +static void test_sys_enter_exit(void) +{ + struct task_local_storage *skel; + int err; + + skel =3D task_local_storage__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) + return; + + skel->bss->target_pid =3D syscall(SYS_gettid); + + err =3D task_local_storage__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto out; + + syscall(SYS_gettid); + syscall(SYS_gettid); + + /* 3x syscalls: 1x attach and 2x gettid */ + ASSERT_EQ(skel->bss->enter_cnt, 3, "enter_cnt"); + ASSERT_EQ(skel->bss->exit_cnt, 3, "exit_cnt"); + ASSERT_EQ(skel->bss->mismatch_cnt, 0, "mismatch_cnt"); +out: + task_local_storage__destroy(skel); +} + +static void test_exit_creds(void) +{ + struct task_local_storage_exit_creds *skel; + int err; + + skel =3D task_local_storage_exit_creds__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open_and_load")) + return; + + err =3D task_local_storage_exit_creds__attach(skel); + if (!ASSERT_OK(err, "skel_attach")) + goto out; + + /* trigger at least one exit_creds() */ + if (CHECK_FAIL(system("ls > /dev/null"))) + goto out; + + /* sync rcu to make sure exit_creds() is called for "ls" */ + kern_sync_rcu(); + ASSERT_EQ(skel->bss->valid_ptr_count, 0, "valid_ptr_count"); + ASSERT_NEQ(skel->bss->null_ptr_count, 0, "null_ptr_count"); +out: + task_local_storage_exit_creds__destroy(skel); +} + +void test_task_local_storage(void) +{ + if (test__start_subtest("sys_enter_exit")) + test_sys_enter_exit(); + if (test__start_subtest("exit_creds")) + test_exit_creds(); +} diff --git a/tools/testing/selftests/bpf/progs/task_local_storage.c b/too= ls/testing/selftests/bpf/progs/task_local_storage.c new file mode 100644 index 0000000000000..80a0a20db88d2 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/task_local_storage.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include "vmlinux.h" +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, long); +} enter_id SEC(".maps"); + +#define MAGIC_VALUE 0xabcd1234 + +pid_t target_pid =3D 0; +int mismatch_cnt =3D 0; +int enter_cnt =3D 0; +int exit_cnt =3D 0; + +SEC("tp_btf/sys_enter") +int BPF_PROG(on_enter, struct pt_regs *regs, long id) +{ + struct task_struct *task; + long *ptr; + + task =3D bpf_get_current_task_btf(); + if (task->pid !=3D target_pid) + return 0; + + ptr =3D bpf_task_storage_get(&enter_id, task, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (!ptr) + return 0; + + __sync_fetch_and_add(&enter_cnt, 1); + *ptr =3D MAGIC_VALUE + enter_cnt; + + return 0; +} + +SEC("tp_btf/sys_exit") +int BPF_PROG(on_exit, struct pt_regs *regs, long id) +{ + struct task_struct *task; + long *ptr; + + task =3D bpf_get_current_task_btf(); + if (task->pid !=3D target_pid) + return 0; + + ptr =3D bpf_task_storage_get(&enter_id, task, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (!ptr) + return 0; + + __sync_fetch_and_add(&exit_cnt, 1); + if (*ptr !=3D MAGIC_VALUE + exit_cnt) + __sync_fetch_and_add(&mismatch_cnt, 1); + return 0; +} diff --git a/tools/testing/selftests/bpf/progs/task_local_storage_exit_cr= eds.c b/tools/testing/selftests/bpf/progs/task_local_storage_exit_creds.c new file mode 100644 index 0000000000000..81758c0aef993 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/task_local_storage_exit_creds.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include "vmlinux.h" +#include +#include + +char _license[] SEC("license") =3D "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, __u64); +} task_storage SEC(".maps"); + +int valid_ptr_count =3D 0; +int null_ptr_count =3D 0; + +SEC("fentry/exit_creds") +int BPF_PROG(trace_exit_creds, struct task_struct *task) +{ + __u64 *ptr; + + ptr =3D bpf_task_storage_get(&task_storage, task, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + if (ptr) + __sync_fetch_and_add(&valid_ptr_count, 1); + else + __sync_fetch_and_add(&null_ptr_count, 1); + return 0; +} --=20 2.24.1