Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp9183702rwl; Wed, 11 Jan 2023 02:33:22 -0800 (PST) X-Google-Smtp-Source: AMrXdXtDxZjjx+OSIHqYoGHEnw+/OpyqCGfEQIblPcNB9FvwbuCamyh75X8AcqbXNMq32+BQZOcb X-Received: by 2002:a17:907:8d01:b0:858:703e:e4e2 with SMTP id tc1-20020a1709078d0100b00858703ee4e2mr4709866ejc.42.1673433202298; Wed, 11 Jan 2023 02:33:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673433202; cv=none; d=google.com; s=arc-20160816; b=caN6/787ow4QtdAEm6I7CYk3xKhR1ciWoGht9y+wCyE8oo+UIeRWtZX7Y6Xmihn9BQ 6XUvBYm+J2vk7HxaCJ19qNplSNhHu73jJl3wGbwPuHzC3nCBHWz0f7lY6Hk1vtzy4jkD 9Cn6dSdd5eJKFv7vtUvfPjaXqbtaDYtJ1wz5VDAUSglXDac9/p+Zti7pK3D4LuLDloGx V4zhMQSSk+D1yq0DiexPqLjs6fk/5zidOW0DGgsGwpIi9QTsH/CazlFyB605D6x9MP7a gBQlfzhxTmKdhFyyddNpBGHTAk6eA9bP8bY5fBblMWeBE29Q98BhUn5W9UD+ab6DY7hF jVJg== 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 :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=OVBFLUuM9Ab01Ju/4O4Uvt/i6gRhr/7xQWk37sSpkss=; b=WJ5oJokQ08XKjAdMbGPldbhHDLnzOArPVODuVmqfBd1Hiag2MDCFYAl5tQB7h9l5Gc iKSqTFuQu1AVWLmjm8o80bUYvcmTDz1sLWe1NhEkI6eiIEAuRieQIwg2cWGtlePluU1h BvF21Gk9CMeLgDojwOkAVBdI5MTr3Dm1mb7X07LMO9pKuKFgSPS7yKgnINJf5afZHYFo DeRki5dE5IEbUucdrbNji+AR1mrUlmua0UgSQtUa+/T6FvFoQFaPOhRdpbzhS+50IiX9 V4d0fX4RV335IQu8JSr/0BEkPCqVwQVAFdXNuklUFxaj76p+PuEQxdJzkN7smTBtK7HC OmAA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xf10-20020a17090731ca00b0084d42589be6si8480267ejb.787.2023.01.11.02.33.09; Wed, 11 Jan 2023 02:33:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237682AbjAKJeW (ORCPT + 53 others); Wed, 11 Jan 2023 04:34:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238635AbjAKJdl (ORCPT ); Wed, 11 Jan 2023 04:33:41 -0500 Received: from frasgout11.his.huawei.com (frasgout11.his.huawei.com [14.137.139.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BACCA2180; Wed, 11 Jan 2023 01:31:48 -0800 (PST) Received: from mail02.huawei.com (unknown [172.18.147.227]) by frasgout11.his.huawei.com (SkyGuard) with ESMTP id 4NsMgh2pz9z9v7Nd; Wed, 11 Jan 2023 17:24:00 +0800 (CST) Received: from roberto-ThinkStation-P620 (unknown [10.204.63.22]) by APP1 (Coremail) with SMTP id LxC2BwB3AAzugb5jAlOJAA--.2952S2; Wed, 11 Jan 2023 10:31:34 +0100 (CET) Message-ID: <4b8688ee3d533d989196004d5f9f2c7eb4093f8b.camel@huaweicloud.com> Subject: Re: [PATCH v2] security: Restore passing final prot to ima_file_mmap() From: Roberto Sassu To: Paul Moore , zohar@linux.ibm.com Cc: jmorris@namei.org, serge@hallyn.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, viro@zeniv.linux.org.uk, Roberto Sassu , stable@vger.kernel.org Date: Wed, 11 Jan 2023 10:30:59 +0100 In-Reply-To: References: <20221221141007.2579770-1-roberto.sassu@huaweicloud.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5-0ubuntu1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-CM-TRANSID: LxC2BwB3AAzugb5jAlOJAA--.2952S2 X-Coremail-Antispam: 1UD129KBjvJXoWxAF18JrWUZFWxWw43uryUZFb_yoW5AFy8pF s8t3WUKrs5JFySyrn7XF17CFySk3y3KFyDWa1vgryqy3Z8WF1xKr1akFW29F18ZrWkuFy0 qa17urZxC3WqyrJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUyEb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxV AFwI0_Gr0_Gr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40E x7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1l42xK82IYc2Ij 64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x 8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE 2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42 xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIE c7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43ZEXa7IU1zuWJUUUUU== X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAQAOBF1jj4dyAQAAsO X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham 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 Fri, 2023-01-06 at 16:14 -0500, Paul Moore wrote: > On Wed, Dec 21, 2022 at 9:10 AM Roberto Sassu > wrote: > > From: Roberto Sassu > > > > Commit 98de59bfe4b2f ("take calculation of final prot in > > security_mmap_file() into a helper") moved the code to update prot with the > > actual protection flags to be granted to the requestor by the kernel to a > > helper called mmap_prot(). However, the patch didn't update the argument > > passed to ima_file_mmap(), making it receive the requested prot instead of > > the final computed prot. > > > > A possible consequence is that files mmapped as executable might not be > > measured/appraised if PROT_EXEC is not requested but subsequently added in > > the final prot. > > > > Replace prot with mmap_prot(file, prot) as the second argument of > > ima_file_mmap() to restore the original behavior. > > > > Cc: stable@vger.kernel.org > > Fixes: 98de59bfe4b2 ("take calculation of final prot in security_mmap_file() into a helper") > > Signed-off-by: Roberto Sassu > > --- > > security/security.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/security/security.c b/security/security.c > > index d1571900a8c7..0d2359d588a1 100644 > > --- a/security/security.c > > +++ b/security/security.c > > @@ -1666,7 +1666,7 @@ int security_mmap_file(struct file *file, unsigned long prot, > > mmap_prot(file, prot), flags); > > if (ret) > > return ret; > > - return ima_file_mmap(file, prot); > > + return ima_file_mmap(file, mmap_prot(file, prot)); > > } > > This seems like a reasonable fix, although as the original commit is > ~10 years old at this point I am a little concerned about the impact > this might have on IMA. Mimi, what do you think? > > Beyond that, my only other comment would be to only call mmap_prot() > once and cache the results in a local variable. You could also fix up > some of the ugly indentation crimes in security_mmap_file() while you > are at it, e.g. something like this: Hi Paul thanks for the comments. With the patch set to move IMA and EVM to the LSM infrastructure we will be back to calling mmap_prot() only once, but I guess we could do anyway, as the patch (if accepted) would be likely backported to stable kernels. Thanks Roberto > diff --git a/security/security.c b/security/security.c > index d1571900a8c7..2f9cad9ecac8 100644 > --- a/security/security.c > +++ b/security/security.c > @@ -1662,11 +1662,12 @@ int security_mmap_file(struct file *file, unsigned long > prot, > unsigned long flags) > { > int ret; > - ret = call_int_hook(mmap_file, 0, file, prot, > - mmap_prot(file, prot), flags); > + unsigned long prot_adj = mmap_prot(file, prot); > + > + ret = call_int_hook(mmap_file, 0, file, prot, prot_adj, flags); > if (ret) > return ret; > - return ima_file_mmap(file, prot); > + return ima_file_mmap(file, prot_adj); > } > > -- > paul-moore.com