Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp132408rdg; Thu, 12 Oct 2023 00:30:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEnBmcI7rizmn3iMEDm8FwEftVNdg+wdyNfzqbvCa39oehoSG43g0UlvVijx/+RBf6AUepj X-Received: by 2002:a25:8309:0:b0:d9a:c391:504 with SMTP id s9-20020a258309000000b00d9ac3910504mr1160135ybk.12.1697095828575; Thu, 12 Oct 2023 00:30:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697095828; cv=none; d=google.com; s=arc-20160816; b=wsfdatVdjxTl/uIq/KZ6r9w6Sl2LhisS6f03nOnSdfGnowFCsFk4B1dbt9Vvcurhxx Fo6Oo7wQeyYuZ5n7noHkBt6GFhTsh6Py4pDrwHamqN/EGaHf+My5QD61298DxZYSi9Ds hNKxuvkWpsASwS4QGizNgTT8FPte+mCNBjYgNoIs/ykjFi1wqVKfe8tySK/YpqM0osCr yDmGkhwhnhPQ4NFoD8aWhMRxYSZdBwvHYk6hTies3zSjs6ahOGp0XlfyBuIZ5s3biVud /nR+04H7DoPXkn6bZ/yEpHwHFtgw8z8HT8d90mug6Ty7/iOW6nvIz8nuyZGp5HWNhDbx GJiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent :content-transfer-encoding:references:in-reply-to:date:cc:to:from :subject:message-id; bh=tlj7NeXt2r+h4+fk3cM635kzrTKfKu61cyeDK7FSgl8=; fh=yjYWj0LaGhbIyHSUS30fEXRi+5FXXRF+m32ev5/qNgI=; b=oZ4btESBFKKxQoEzp1KR+uFokZJ5JIZaJieRIEHWbSjPpfzbD8rFtAxaYZA/coMnep JrmJJXoZqdoJ2KxbONb0W52X2RHPnXyRlKIzSVyJtra2EzLMUFxfun2DVPx4wam9GkqE IuJfkVwR6Gv/31of3QHc3GjonISJf6BHc5paE41mMcJv5x4+NNUywffZohBXxnTJ7o/j uXhrgROHng6RHDomYCLy19VN9lO3uicZeI4gATkRwrgiNXqQN0tTmovRxHXn+StERA+p hLagDFjvqr28opFwHRCvSZgk3QHa4JZGj1AaN5gRXfCmE8bVAsLncTkYn94I7dwe9pFR fBjw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id l125-20020a633e83000000b005787a30e1b7si1751616pga.369.2023.10.12.00.30.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 00:30:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 9F330806E3E5; Thu, 12 Oct 2023 00:30:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377847AbjJLHaJ convert rfc822-to-8bit (ORCPT + 99 others); Thu, 12 Oct 2023 03:30:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377791AbjJLHaH (ORCPT ); Thu, 12 Oct 2023 03:30:07 -0400 Received: from frasgout11.his.huawei.com (frasgout11.his.huawei.com [14.137.139.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B8689D; Thu, 12 Oct 2023 00:30:04 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.18.147.229]) by frasgout11.his.huawei.com (SkyGuard) with ESMTP id 4S5gts4LNtz9xrtf; Thu, 12 Oct 2023 15:17:09 +0800 (CST) Received: from [127.0.0.1] (unknown [10.204.63.22]) by APP1 (Coremail) with SMTP id LxC2BwCn_5BQoCdlXCMQAg--.31200S2; Thu, 12 Oct 2023 08:29:34 +0100 (CET) Message-ID: <16c8c95f2e63ab9a2fba8cba919bf129d0541b61.camel@huaweicloud.com> Subject: Re: [PATCH v3 02/25] ima: Align ima_post_path_mknod() definition with LSM infrastructure From: Roberto Sassu To: Mimi Zohar , viro@zeniv.linux.org.uk, brauner@kernel.org, chuck.lever@oracle.com, jlayton@kernel.org, neilb@suse.de, kolga@netapp.com, Dai.Ngo@oracle.com, tom@talpey.com, dmitry.kasatkin@gmail.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, dhowells@redhat.com, jarkko@kernel.org, stephen.smalley.work@gmail.com, eparis@parisplace.org, casey@schaufler-ca.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nfs@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, keyrings@vger.kernel.org, selinux@vger.kernel.org, Roberto Sassu Date: Thu, 12 Oct 2023 09:29:16 +0200 In-Reply-To: <8646e30b0074a2932076b5a0a792b14be034de98.camel@linux.ibm.com> References: <20230904133415.1799503-1-roberto.sassu@huaweicloud.com> <20230904133415.1799503-3-roberto.sassu@huaweicloud.com> <8646e30b0074a2932076b5a0a792b14be034de98.camel@linux.ibm.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT User-Agent: Evolution 3.44.4-0ubuntu2 MIME-Version: 1.0 X-CM-TRANSID: LxC2BwCn_5BQoCdlXCMQAg--.31200S2 X-Coremail-Antispam: 1UD129KBjvJXoW3WrWfAw4Utr48uF48Gw1Utrb_yoW7CF1fpF WkJ3WDGrZ5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8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 12 Oct 2023 00:30:25 -0700 (PDT) On Wed, 2023-10-11 at 15:01 -0400, Mimi Zohar wrote: > On Wed, 2023-10-11 at 18:02 +0200, Roberto Sassu wrote: > > On Wed, 2023-10-11 at 10:38 -0400, Mimi Zohar wrote: > > > On Mon, 2023-09-04 at 15:33 +0200, Roberto Sassu wrote: > > > > From: Roberto Sassu > > > > > > > > Change ima_post_path_mknod() definition, so that it can be registered as > > > > implementation of the path_post_mknod hook. Since LSMs see a umask-stripped > > > > mode from security_path_mknod(), pass the same to ima_post_path_mknod() as > > > > well. > > > > Also, make sure that ima_post_path_mknod() is executed only if > > > > (mode & S_IFMT) is equal to zero or S_IFREG. > > > > > > > > Add this check to take into account the different placement of the > > > > path_post_mknod hook (to be introduced) in do_mknodat(). > > > > > > Move "(to be introduced)" to when it is first mentioned. > > > > > > > Since the new hook > > > > will be placed after the switch(), the check ensures that > > > > ima_post_path_mknod() is invoked as originally intended when it is > > > > registered as implementation of path_post_mknod. > > > > > > > > Signed-off-by: Roberto Sassu > > > > --- > > > > fs/namei.c | 9 ++++++--- > > > > include/linux/ima.h | 7 +++++-- > > > > security/integrity/ima/ima_main.c | 10 +++++++++- > > > > 3 files changed, 20 insertions(+), 6 deletions(-) > > > > > > > > diff --git a/fs/namei.c b/fs/namei.c > > > > index e56ff39a79bc..c5e96f716f98 100644 > > > > --- a/fs/namei.c > > > > +++ b/fs/namei.c > > > > @@ -4024,6 +4024,7 @@ static int do_mknodat(int dfd, struct filename *name, umode_t mode, > > > > struct path path; > > > > int error; > > > > unsigned int lookup_flags = 0; > > > > + umode_t mode_stripped; > > > > > > > > error = may_mknod(mode); > > > > if (error) > > > > @@ -4034,8 +4035,9 @@ static int do_mknodat(int dfd, struct filename *name, umode_t mode, > > > > if (IS_ERR(dentry)) > > > > goto out1; > > > > > > > > - error = security_path_mknod(&path, dentry, > > > > - mode_strip_umask(path.dentry->d_inode, mode), dev); > > > > + mode_stripped = mode_strip_umask(path.dentry->d_inode, mode); > > > > + > > > > + error = security_path_mknod(&path, dentry, mode_stripped, dev); > > > > if (error) > > > > goto out2; > > > > > > > > @@ -4045,7 +4047,8 @@ static int do_mknodat(int dfd, struct filename *name, umode_t mode, > > > > error = vfs_create(idmap, path.dentry->d_inode, > > > > dentry, mode, true); > > > > if (!error) > > > > - ima_post_path_mknod(idmap, dentry); > > > > + ima_post_path_mknod(idmap, &path, dentry, > > > > + mode_stripped, dev); > > > > break; > > > > case S_IFCHR: case S_IFBLK: > > > > error = vfs_mknod(idmap, path.dentry->d_inode, > > > > diff --git a/include/linux/ima.h b/include/linux/ima.h > > > > index 910a2f11a906..179ce52013b2 100644 > > > > --- a/include/linux/ima.h > > > > +++ b/include/linux/ima.h > > > > @@ -32,7 +32,8 @@ extern int ima_read_file(struct file *file, enum kernel_read_file_id id, > > > > extern int ima_post_read_file(struct file *file, void *buf, loff_t size, > > > > enum kernel_read_file_id id); > > > > extern void ima_post_path_mknod(struct mnt_idmap *idmap, > > > > - struct dentry *dentry); > > > > + const struct path *dir, struct dentry *dentry, > > > > + umode_t mode, unsigned int dev); > > > > extern int ima_file_hash(struct file *file, char *buf, size_t buf_size); > > > > extern int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size); > > > > extern void ima_kexec_cmdline(int kernel_fd, const void *buf, int size); > > > > @@ -114,7 +115,9 @@ static inline int ima_post_read_file(struct file *file, void *buf, loff_t size, > > > > } > > > > > > > > static inline void ima_post_path_mknod(struct mnt_idmap *idmap, > > > > - struct dentry *dentry) > > > > + const struct path *dir, > > > > + struct dentry *dentry, > > > > + umode_t mode, unsigned int dev) > > > > { > > > > return; > > > > } > > > > diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c > > > > index 365db0e43d7c..76eba92d7f10 100644 > > > > --- a/security/integrity/ima/ima_main.c > > > > +++ b/security/integrity/ima/ima_main.c > > > > @@ -696,18 +696,26 @@ void ima_post_create_tmpfile(struct mnt_idmap *idmap, > > > > /** > > > > * ima_post_path_mknod - mark as a new inode > > > > * @idmap: idmap of the mount the inode was found from > > > > + * @dir: path structure of parent of the new file > > > > * @dentry: newly created dentry > > > > + * @mode: mode of the new file > > > > + * @dev: undecoded device number > > > > * > > > > * Mark files created via the mknodat syscall as new, so that the > > > > * file data can be written later. > > > > */ > > > > void ima_post_path_mknod(struct mnt_idmap *idmap, > > > > - struct dentry *dentry) > > > > + const struct path *dir, struct dentry *dentry, > > > > + umode_t mode, unsigned int dev) > > > > { > > > > struct integrity_iint_cache *iint; > > > > struct inode *inode = dentry->d_inode; > > > > int must_appraise; > > > > > > > > + /* See do_mknodat(), IMA is executed for case 0: and case S_IFREG: */ > > > > + if ((mode & S_IFMT) != 0 && (mode & S_IFMT) != S_IFREG) > > > > + return; > > > > + > > > > > > There's already a check below to make sure that this is a regular file. > > > Are both needed? > > > > You are right, I can remove the first check. > > The question then becomes why modify hook the arguments? We need to make sure that ima_post_path_mknod() has the same parameters as the LSM hook at the time we register it to the LSM infrastructure. Thanks Roberto > > > > > > if (!ima_policy_flag || !S_ISREG(inode->i_mode)) > > > > return; > > > > > > >