Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp2937865pxb; Mon, 16 Nov 2020 00:56:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJwSc4AmHsrdv1Rc9ctyI+2/FTHyiAodejif3Fzk8++z6mlKMV7ZVQAYT4mRbxp5My480K9k X-Received: by 2002:a17:906:d0cc:: with SMTP id bq12mr13123999ejb.127.1605517017970; Mon, 16 Nov 2020 00:56:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605517017; cv=none; d=google.com; s=arc-20160816; b=ZnNhNJOD5YOHraJFUZtB5L+6F/wqErpb7udQ3FmPTIhMFthFD9vHughWbYhj0B36gM Mm3xnYfAtMnPnjQi1nlITCiMi1Ai98IXNunpr36Pt4xb9T4GUzmoJyDZtsT2tjialJW4 f6r3DFaf0IAAdwMzukGNplmBvKKGL00cq/OjBfkHb2Gt8Z8VptANkAwGEDFxQTDiWhtM XTYFmR14BnMl3/pcU6Wev4Gnk7A5oK4LdiWpdeAR2nOBP0/Ov+6yc1FiEy8LvgN4nGOO jun8zBWX3hv/QxdfQQHLA6ODlZSbDMMkWSt18VRA45zd8pUEtWiPS6So4SnHw7J8kbIz aPgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from; bh=jf+FcpdO2NTYmnyLn6fO3r/h2J8gYaJTDTiAS17KSlY=; b=p6E4+w9wv9u4ISPkSoSLDnYKoxAveBRMxxKrCCe3jYO1KYdAo075cxHjEfNWdCfMny v785a74qeiK4cxUXxviqpzc3Ts0ly9OizdvoRcxBSwhVj3d8tQNoppH1I9Atb3WVx3GR 9Sizc3LWF9+FwnEpXAbp2i06lV/Ejlw4GrX4+z8eaiMuaJzNe+8jur28G+DBOp96/CmR Z48EfzVR+v0I4UhlFE7wB3DA+XQOEhwEMApQakpcLL7YHk0tSlNL+OVwPOZSuUkDkY+X VXtFDLaobOEW2ETW7YLSGPySOPg1I7OJ2NwGP5KaFZtoGyI28F800529W/rSDC+88O5U Z+fA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u19si6217302edx.311.2020.11.16.00.56.35; Mon, 16 Nov 2020 00:56:57 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728293AbgKPIwW convert rfc822-to-8bit (ORCPT + 99 others); Mon, 16 Nov 2020 03:52:22 -0500 Received: from frasgout.his.huawei.com ([185.176.79.56]:2104 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726969AbgKPIwW (ORCPT ); Mon, 16 Nov 2020 03:52:22 -0500 Received: from fraeml702-chm.china.huawei.com (unknown [172.18.147.201]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4CZN7s3tFrz67DF4; Mon, 16 Nov 2020 16:50:33 +0800 (CST) Received: from fraeml714-chm.china.huawei.com (10.206.15.33) by fraeml702-chm.china.huawei.com (10.206.15.51) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1913.5; Mon, 16 Nov 2020 09:52:19 +0100 Received: from fraeml714-chm.china.huawei.com ([10.206.15.33]) by fraeml714-chm.china.huawei.com ([10.206.15.33]) with mapi id 15.01.1913.007; Mon, 16 Nov 2020 09:52:19 +0100 From: Roberto Sassu To: Christoph Hellwig CC: "zohar@linux.ibm.com" , "linux-integrity@vger.kernel.org" , "linux-security-module@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Silviu Vlasceanu , "stable@vger.kernel.org" , "torvalds@linux-foundation.org" , "viro@zeniv.linux.org.uk" , "linux-fsdevel@vger.kernel.org" Subject: RE: [RESEND][PATCH] ima: Set and clear FMODE_CAN_READ in ima_calc_file_hash() Thread-Topic: [RESEND][PATCH] ima: Set and clear FMODE_CAN_READ in ima_calc_file_hash() Thread-Index: AQHWuZM+vbqfejrqe02000rC0h3xoqnHabyAgAMLzKA= Date: Mon, 16 Nov 2020 08:52:19 +0000 Message-ID: <0fd0fb3360194d909ba48f13220f9302@huawei.com> References: <20201113080132.16591-1-roberto.sassu@huawei.com> <20201114111057.GA16415@infradead.org> In-Reply-To: <20201114111057.GA16415@infradead.org> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.220.96.108] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > From: Christoph Hellwig [mailto:hch@infradead.org] > Sent: Saturday, November 14, 2020 12:11 PM > On Fri, Nov 13, 2020 at 09:01:32AM +0100, Roberto Sassu wrote: > > Commit a1f9b1c0439db ("integrity/ima: switch to using __kernel_read") > > replaced the __vfs_read() call in integrity_kernel_read() with > > __kernel_read(), a new helper introduced by commit 61a707c543e2a ("fs: > add > > a __kernel_read helper"). > > > > Since the new helper requires that also the FMODE_CAN_READ flag is set > in > > file->f_mode, this patch saves the original f_mode and sets the flag if the > > the file descriptor has the necessary file operation. Lastly, it restores > > the original f_mode at the end of ima_calc_file_hash(). > > This looks bogus. FMODE_CAN_READ has a pretty clear definition and > you can't just go and read things if it is not set. Also f_mode > manipulations on a life file are racy. FMODE_CAN_READ was not set because f_mode does not have FMODE_READ. In the patch, I check if the former can be set similarly to the way it is done in file_table.c and open.c. Is there a better way to read a file when the file was not opened for reading and a new file descriptor cannot be created? Thanks Roberto HUAWEI TECHNOLOGIES Duesseldorf GmbH, HRB 56063 Managing Director: Li Peng, Li Jian, Shi Yanli > > Cc: stable@vger.kernel.org # 5.8.x > > Fixes: a1f9b1c0439db ("integrity/ima: switch to using __kernel_read") > > Signed-off-by: Roberto Sassu > > --- > > security/integrity/ima/ima_crypto.c | 8 ++++++-- > > 1 file changed, 6 insertions(+), 2 deletions(-) > > > > diff --git a/security/integrity/ima/ima_crypto.c > b/security/integrity/ima/ima_crypto.c > > index 21989fa0c107..22ed86a0c964 100644 > > --- a/security/integrity/ima/ima_crypto.c > > +++ b/security/integrity/ima/ima_crypto.c > > @@ -537,6 +537,7 @@ int ima_calc_file_hash(struct file *file, struct > ima_digest_data *hash) > > loff_t i_size; > > int rc; > > struct file *f = file; > > + fmode_t saved_mode; > > bool new_file_instance = false, modified_mode = false; > > > > /* > > @@ -550,7 +551,7 @@ int ima_calc_file_hash(struct file *file, struct > ima_digest_data *hash) > > } > > > > /* Open a new file instance in O_RDONLY if we cannot read */ > > - if (!(file->f_mode & FMODE_READ)) { > > + if (!(file->f_mode & FMODE_READ) || !(file->f_mode & > FMODE_CAN_READ)) { > > int flags = file->f_flags & ~(O_WRONLY | O_APPEND | > > O_TRUNC | O_CREAT | O_NOCTTY | > O_EXCL); > > flags |= O_RDONLY; > > @@ -562,7 +563,10 @@ int ima_calc_file_hash(struct file *file, struct > ima_digest_data *hash) > > */ > > pr_info_ratelimited("Unable to reopen file for > reading.\n"); > > f = file; > > + saved_mode = f->f_mode; > > f->f_mode |= FMODE_READ; > > + if (likely(file->f_op->read || file->f_op->read_iter)) > > + f->f_mode |= FMODE_CAN_READ; > > modified_mode = true; > > } else { > > new_file_instance = true; > > @@ -582,7 +586,7 @@ int ima_calc_file_hash(struct file *file, struct > ima_digest_data *hash) > > if (new_file_instance) > > fput(f); > > else if (modified_mode) > > - f->f_mode &= ~FMODE_READ; > > + f->f_mode = saved_mode; > > return rc; > > } > > > > -- > > 2.27.GIT > > > ---end quoted text---