Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp584515pxb; Tue, 15 Feb 2022 22:54:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJztfwVUd9XlCBkylDxZWnPev9zlKdbs/0i8Y7VF6Lsn9zRif4SSeXcpJooGgXgyHSsthSNy X-Received: by 2002:a17:902:c201:b0:14d:26d2:3da8 with SMTP id 1-20020a170902c20100b0014d26d23da8mr1283130pll.56.1644994494482; Tue, 15 Feb 2022 22:54:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644994494; cv=none; d=google.com; s=arc-20160816; b=e5v5faaj/fBfUV7QjsRbUr5LqJGCRlsTBQqtGaqstyol82V4+5uD+xwahn/1evTzJv 3I4r9gTkgVd8Ff+S5ngRR15hCgAsTmr9VlQ6/4Yuoqy4kqxmK7lo8i0/aiFc/dXmA/gX QGQlqm1eiQNc4DpdGlwfP/af/2w7eQ/WfxPywvnWb+ufjKbDADz2JN6lJmx+TdAwxCc5 SufQtXRFTylLU4UN4Cx6umT/Klyb7zk0ewg+3cQTER1L5DMI8NyaielYswwu4GLRotci zeXfzNC0WT6n2OnUq3YU8hyqdHxQhqyM10Me3D2aBMezGC9WJxWMKzQmYTPBkHrP+8QD H7zg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=Uya3VrJ5mbeW5e3EiyZomdfwW6pYC+/8k7L0vXEy/64=; b=PoNDQYkMlipgTo3p+ICX+78hJj87tPcv0FIJmS30zYymBUxxptmKE2umQZvf83nlXu k0kdDvk57cRCGCEk9S7h4sLrb1sfpEbw98RUvqu3oDwMS+08sMFjLCY4oZXD66Swm8NI Wz/sWQaSOJojX6MkNXT0IsyrPBTW7WTTpEDDYZVg39jkBZfp3+t6TKnsa0peEu1KKJmT XsaLbqPH8J5786QMqCCb1PI7i5Z7R/3tmmiAPN0OuW95FR7sNRO4rA8jEzznjQ0uAN4J Yj9GjHUeWCBMG2ecrnRa9f7W+umfMirxBDy1DU0ydasx7AtlS4RuWyCgp9Z3heuUgcIw TiRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=google header.b=GqhrTd29; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id bf19si5257685pgb.569.2022.02.15.22.54.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 22:54:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=google header.b=GqhrTd29; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 99336716F1; Tue, 15 Feb 2022 22:37:18 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241438AbiBOQLx (ORCPT + 99 others); Tue, 15 Feb 2022 11:11:53 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:42022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241575AbiBOQLV (ORCPT ); Tue, 15 Feb 2022 11:11:21 -0500 Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C966ACE91D for ; Tue, 15 Feb 2022 08:11:07 -0800 (PST) Received: by mail-io1-xd2a.google.com with SMTP id h5so717027ioj.3 for ; Tue, 15 Feb 2022 08:11:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=Uya3VrJ5mbeW5e3EiyZomdfwW6pYC+/8k7L0vXEy/64=; b=GqhrTd29C1Fa/GF7XdeyaZQ3hqUrBB2h17CHBFffHv2hxDx4zjmbEz0vOyg0Iq6R4M 2H7TFNA3sVqU8hkGQKByqZzQE7IH19x0/irG56NJvKU+wpiHH9Dv0RwN43FfYB3shghc QMqwnn6TGVnovhyQd07APJlJB7dev7UiXwUtc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=Uya3VrJ5mbeW5e3EiyZomdfwW6pYC+/8k7L0vXEy/64=; b=gx9CtzNZJ2i3kdylPROMYTJ2oxhn+JNVY4SG2JTtmRt1Dyb8n89sccTr0nFlhwBgx3 rLJAu77dBo+R3mSTCIaw29TKp+UHTHDDF5ajo3xsJ+xDAqAUFIhoydnH2EYMfdqsGAJr 8oUqn0h+2jgZt00GzYq0hXy+bVD3hBWJyIm3auT2WFIIRnIGdJIbMqihtEgccy46hPaj UaAD/teeCKktoeFOCfMLOx8d2zlmHmF8xEy7vLI8lQjopMjfMYtCgFIe+OdC9Jemt4g2 dpD+ElXM+q7dqQhmqrgVNI5/fCB2mx2VsNAGkc+S4o8PCci7Ai+S4oGMF3In81zUrdxv Vjbg== X-Gm-Message-State: AOAM530oYtVeZH7vjgaVvkLy4bHY+1lDrKnfrzkQgF+cMhHFhkdWuh+g LZiqtkPXc0p9btRz2ACJ979O0w== X-Received: by 2002:a05:6638:6bb:: with SMTP id d27mr3009674jad.231.1644941467203; Tue, 15 Feb 2022 08:11:07 -0800 (PST) Received: from [192.168.1.128] ([71.205.29.0]) by smtp.gmail.com with ESMTPSA id l2sm5384520ilv.66.2022.02.15.08.11.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Feb 2022 08:11:06 -0800 (PST) Subject: Re: [PATCH v2 6/6] selftests/bpf: Add test for bpf_lsm_kernel_read_file() To: Roberto Sassu , zohar@linux.ibm.com, shuah@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kpsingh@kernel.org, revest@chromium.org Cc: linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Shuah Khan References: <20220215124042.186506-1-roberto.sassu@huawei.com> <20220215124042.186506-7-roberto.sassu@huawei.com> From: Shuah Khan Message-ID: Date: Tue, 15 Feb 2022 09:11:05 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <20220215124042.186506-7-roberto.sassu@huawei.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=no 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 On 2/15/22 5:40 AM, Roberto Sassu wrote: > Test the ability of bpf_lsm_kernel_read_file() to call the sleepable > functions bpf_ima_inode_hash() or bpf_ima_file_hash() to obtain a > measurement of a loaded IMA policy. > > Signed-off-by: Roberto Sassu > --- > tools/testing/selftests/bpf/ima_setup.sh | 2 ++ > .../selftests/bpf/prog_tests/test_ima.c | 3 +- > tools/testing/selftests/bpf/progs/ima.c | 28 ++++++++++++++++--- > 3 files changed, 28 insertions(+), 5 deletions(-) > > diff --git a/tools/testing/selftests/bpf/ima_setup.sh b/tools/testing/selftests/bpf/ima_setup.sh > index 8e62581113a3..82530f19f85a 100755 > --- a/tools/testing/selftests/bpf/ima_setup.sh > +++ b/tools/testing/selftests/bpf/ima_setup.sh > @@ -51,6 +51,7 @@ setup() > > ensure_mount_securityfs > echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${IMA_POLICY_FILE} > + echo "measure func=BPRM_CHECK fsuuid=${mount_uuid}" > ${mount_dir}/policy_test > } > > cleanup() { > @@ -74,6 +75,7 @@ run() > local mount_dir="${tmp_dir}/mnt" > local copied_bin_path="${mount_dir}/$(basename ${TEST_BINARY})" > > + echo ${mount_dir}/policy_test > ${IMA_POLICY_FILE} > exec "${copied_bin_path}" > } > > diff --git a/tools/testing/selftests/bpf/prog_tests/test_ima.c b/tools/testing/selftests/bpf/prog_tests/test_ima.c > index 62bf0e830453..c4a62d7b70df 100644 > --- a/tools/testing/selftests/bpf/prog_tests/test_ima.c > +++ b/tools/testing/selftests/bpf/prog_tests/test_ima.c > @@ -97,8 +97,9 @@ void test_test_ima(void) > /* > * 1 sample with use_ima_file_hash = false > * 2 samples with use_ima_file_hash = true (./ima_setup.sh, /bin/true) > + * 1 sample with use_ima_file_hash = true (IMA policy) > */ > - ASSERT_EQ(err, 3, "num_samples_or_err"); > + ASSERT_EQ(err, 4, "num_samples_or_err"); > ASSERT_NEQ(ima_hash_from_bpf, 0, "ima_hash"); > > close_clean: > diff --git a/tools/testing/selftests/bpf/progs/ima.c b/tools/testing/selftests/bpf/progs/ima.c > index 9bb63f96cfc0..9b4c03f30a1c 100644 > --- a/tools/testing/selftests/bpf/progs/ima.c > +++ b/tools/testing/selftests/bpf/progs/ima.c > @@ -20,8 +20,7 @@ char _license[] SEC("license") = "GPL"; > > bool use_ima_file_hash; > > -SEC("lsm.s/bprm_committed_creds") > -void BPF_PROG(ima, struct linux_binprm *bprm) > +static void ima_test_common(struct file *file) > { > u64 ima_hash = 0; > u64 *sample; > @@ -31,10 +30,10 @@ void BPF_PROG(ima, struct linux_binprm *bprm) > pid = bpf_get_current_pid_tgid() >> 32; > if (pid == monitored_pid) { > if (!use_ima_file_hash) > - ret = bpf_ima_inode_hash(bprm->file->f_inode, &ima_hash, > + ret = bpf_ima_inode_hash(file->f_inode, &ima_hash, > sizeof(ima_hash)); > else > - ret = bpf_ima_file_hash(bprm->file, &ima_hash, > + ret = bpf_ima_file_hash(file, &ima_hash, > sizeof(ima_hash)); > if (ret < 0 || ima_hash == 0) Is this considered an error? Does it make sense for this test to be void type and not return the error to its callers? One of the callers below seems to care for return values. > return; > @@ -49,3 +48,24 @@ void BPF_PROG(ima, struct linux_binprm *bprm) > > return; > } > + > +SEC("lsm.s/bprm_committed_creds") > +void BPF_PROG(ima, struct linux_binprm *bprm) > +{ > + ima_test_common(bprm->file); > +} > + > +SEC("lsm.s/kernel_read_file") > +int BPF_PROG(kernel_read_file, struct file *file, enum kernel_read_file_id id, > + bool contents) > +{ > + if (!contents) > + return 0; > + > + if (id != READING_POLICY) > + return 0; > + > + ima_test_common(file); This one here. > + > + return 0; > +} > thanks, -- Shuah