Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp919989rdg; Wed, 11 Oct 2023 09:03:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGAQz/BP/6rNdzXO8qQz4Ym8cH6FS58EXEMuomvLiDTuuV8ith2Upq1zgW6mpgUiHdb4P7B X-Received: by 2002:a17:90a:ad09:b0:268:2500:b17e with SMTP id r9-20020a17090aad0900b002682500b17emr20062038pjq.23.1697040197726; Wed, 11 Oct 2023 09:03:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697040197; cv=none; d=google.com; s=arc-20160816; b=eLFm6B7zjtP736I358aiGVmUdQuM9/4d5sajfITjbQLXBvC/INls09YW9AC6MLHsg5 0mK8RNg5lU+boHDiFAn1RqzuRNBwM6J+RHyeObV7ON89la2M8JoPvOBcq+dKksdXLHuC +ZWglqmU7SFHhu/IACA2r4eIETotjVHzjUAhnC7AtT0kr6y5Ain6Or5WRbt1nn2d00om OckFSswt9n5+NS7e2yLxx3KUCPPq3A6KgMvHHBI0E8k4jjyTjEMNrTUDb2hK8QaPowpK Dc5jmDpQc8dwDwVULav9Mz7WEn5Vrqiz9NnRQ2FsRrv+TEhaq8Q8eqIkI/RO9m+um5Gu pFQQ== 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=EOTwxkgKRU7EWyghMbFVieuep1dqvP4ftlaWWx6PqS4=; fh=yjYWj0LaGhbIyHSUS30fEXRi+5FXXRF+m32ev5/qNgI=; b=XIcywfUrKdx/gR5byOi4bB+2knLHkgrKy+oPktYuHZJLbc2Ih/5U0kxgPFeF475u4Q whPkQ2TR5q9E5IH7kaPTko8TPnedKaEcDidVC9A8tjUThG819Ho+4Yml1SgekBhpeasy ZpxxBtr2XJEOA6Vqi1EeiANRvcq/qJ0q7FnFAOOYw5NGx0KhrPNKBIXSHXhD/YzdJNGd Z6sTQFXPePUXKjmm6uSN44NEcZ0Q35Y+qwnmB1U01OUS5tFKm5qeV91G6g3Adb+xwsZd 2hTEw7MyzMRfRPF0E+24kVlmNGauEJsqISXhOscqo6kIu/nFDXSaxPn+smFD9PnV+3Te szwA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id pf15-20020a17090b1d8f00b00274e16f797csi156253pjb.20.2023.10.11.09.03.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Oct 2023 09:03:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-nfs-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 0D4A58255492; Wed, 11 Oct 2023 09:03:00 -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 S1346324AbjJKQC5 convert rfc822-to-8bit (ORCPT + 99 others); Wed, 11 Oct 2023 12:02:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235033AbjJKQC4 (ORCPT ); Wed, 11 Oct 2023 12:02:56 -0400 Received: from frasgout12.his.huawei.com (frasgout12.his.huawei.com [14.137.139.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9BBA5B7; Wed, 11 Oct 2023 09:02:53 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.18.147.227]) by frasgout12.his.huawei.com (SkyGuard) with ESMTP id 4S5HG14sGQz9y5C7; Wed, 11 Oct 2023 23:47:21 +0800 (CST) Received: from [127.0.0.1] (unknown [10.204.63.22]) by APP2 (Coremail) with SMTP id GxC2BwC3WrYCxyZlgBb+AQ--.32343S2; Wed, 11 Oct 2023 17:02:24 +0100 (CET) Message-ID: 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: Wed, 11 Oct 2023 18:02:07 +0200 In-Reply-To: References: <20230904133415.1799503-1-roberto.sassu@huaweicloud.com> <20230904133415.1799503-3-roberto.sassu@huaweicloud.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: GxC2BwC3WrYCxyZlgBb+AQ--.32343S2 X-Coremail-Antispam: 1UD129KBjvJXoWxAF1ftrykJw1fCrWfGw1UGFg_yoW7JrWxpF Wkt3WDG395Ary7uF10vFW5Aa4Fv392qF45GFZag3WSyF9Igrn0gFsa9F4Y9ryrKFWvkryx XF15tr98uw4jyFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk0b4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxV AFwI0_Gr0_Gr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40E x7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1l42xK82IYc2Ij 64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x 8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a6rW5MIIYrxkI7VAKI48JMIIF0xvE 2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42 xK8VAvwI8IcIk0rVWrJr0_WFyUJwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv 6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFYFCUUUUU X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAQAHBF1jj5TqOgAAs3 X-CFilter-Loop: Reflected X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no 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-nfs@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 11 Oct 2023 09:03:00 -0700 (PDT) X-Spam-Level: ** 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. Thanks Roberto > > if (!ima_policy_flag || !S_ISREG(inode->i_mode)) > > return; > >