Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2552306rda; Wed, 25 Oct 2023 06:13:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE+LyfipSPqcAn9zzcxQo1c2JVgzAd1ULdRCFGBc5px3X8Fvu6PP+J24GsTpWUIZk544nYb X-Received: by 2002:ac8:7d06:0:b0:410:90c7:5185 with SMTP id g6-20020ac87d06000000b0041090c75185mr18278800qtb.54.1698239602521; Wed, 25 Oct 2023 06:13:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698239602; cv=none; d=google.com; s=arc-20160816; b=Wf7Z/ewA1i74dU9o1yZSbek+3+1EncjUVMNmMbF+LlrPt/Rh/fYR6drsk6OCzKuYiq lZhbVJLRJyWPF/OZE3oD8z41kihJihiRO5mQuYQnTz0eN1bV6YuBuNjZ36x30D1xpIKy A0l3jturXGZMmNcW8c//UifNDqpj8S98cy6BSW/WprU2BUdnhPKZu91RMMeHF9S1Y0lI 6rIohOgu2vrcE8OUWLSnuWEcLuv5bYgcVbXtXADVOwO6mNdaWtFyjJu6MO8lQ1bppXMT ZVCcEC08lxVBLlHLU8EyyBv0ER6Y30AGHp51aaNi1NsNKhk03MOu2AiMUbw/15lljNFN S8iQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=lFeJTuWj+8fliI7ZKMp8fCArGM+QdzaiPtmM5GUkmWs=; fh=IQJiqC6uCuQuBJEAg2KcOzyJ2fwj5Wal45ZJg/1YM1o=; b=K9MidPNwf9zIQfFlnsGPn6EoIcqQxtppcvu9d2zBYdI8wBmmd11wxcUrCj+guuw8Pa d6VhDaSGpMfcLJPlXk26AAC2KLTdW23fKrt/uCxvCr3RYhP6oCqPKX+5Hdkj0ulgWvSu 3gTtZijcM99IHC4wAvY7aLcw+GME/u7brfkXNnh5pEgacREoZqEtE3yL6yuYPAogjehz dx/7sgCMqVyxduouW/Sl2w79Fv4Jo8BSNXmK5d819zXVxR9yZpANBFAfQtiTPbnOJW6w nFO1JzRy3UCIHcz2qrB6L3VUo/xmBgss2EKsDp6fzG97I/tp+/eWKBaHLNwZxRxZCOav 7hcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SNF7niw1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id s19-20020a05622a179300b0041cbaed294esi8475420qtk.609.2023.10.25.06.13.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 06:13:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SNF7niw1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id B7FB3801CF85; Wed, 25 Oct 2023 06:13:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344498AbjJYNNN (ORCPT + 99 others); Wed, 25 Oct 2023 09:13:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234959AbjJYNNL (ORCPT ); Wed, 25 Oct 2023 09:13:11 -0400 Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57D01111; Wed, 25 Oct 2023 06:13:09 -0700 (PDT) Received: by mail-qk1-x72a.google.com with SMTP id af79cd13be357-7789cb322deso386381985a.3; Wed, 25 Oct 2023 06:13:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698239588; x=1698844388; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=lFeJTuWj+8fliI7ZKMp8fCArGM+QdzaiPtmM5GUkmWs=; b=SNF7niw1Zq+KJnYAgueR96YtvqRZSCYk2H+2bHeVQF7JuPhataQFwbo+tYL8FLXBlP 4G8iYfKFyPeAO3B3aywtd09Bw138siNMEDkYOuPHwFRD0VrLuRAhZ9G/kVqSlIV1ayjO tNdGeFxNU4oRaC159WT61yzmJvyNxOwcaHl/fJfNR1cIuoV1kASrMcqbRlI+7YFQImKv zsN+F+h0Jp1wHzs8+9aNEGfJgJ7bKDNOYyX+VA5Yrfnwye0SfLpJrus8GXuRpYD74DAQ Y+O3poJtQqPkIVs8A9M0PEBGnTLAEI2DUtqfcto67/xXmvW7VXmm8Av256IVa8Nzl1jn QAAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698239588; x=1698844388; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lFeJTuWj+8fliI7ZKMp8fCArGM+QdzaiPtmM5GUkmWs=; b=Yy2Cdgme7xcpoUiLWNdvnntnhJmslGeA0f0smYc7OvAQP9cLoAK551MLS1yqXXJSso mDTG+kk70Ddbd0i9xj7PmGo4jLmyrD5vp4Z2QkdkbBhvPuGauoGh27eldUzbr9vJPm3c CZkm5lA8YsD2yUkmnWthphqYeHWzLffXRe7LAaNNtwl6uHo5nm8UiPKZy7X8Yu07CspL OaVuIRB5Y1aUUTtxHNGEc23C5nzWBZVZ0Fhc9rYqbyc+9OES95zhjmoz5bXUsGlk4H3q 8I8d4zlVp3kSaTC3oHxuln0E6hOrxWui+lYOBsmngPSOSNFPbJMxypwCxuuDqpbeAfu7 P10g== X-Gm-Message-State: AOJu0YwwJKZ2Zp2xpVeJ7pQr+dSCj/AivJer0sC9nPAml7yZ3w0rtIKR d3uQ6gFzukgWz9Nycx1tr3tdrsGL6aTmDTsmIyw= X-Received: by 2002:a05:6214:1c4a:b0:66d:ba99:ba1e with SMTP id if10-20020a0562141c4a00b0066dba99ba1emr9984191qvb.52.1698239588401; Wed, 25 Oct 2023 06:13:08 -0700 (PDT) MIME-Version: 1.0 References: <20231025112932.84336-1-zohar@linux.ibm.com> In-Reply-To: <20231025112932.84336-1-zohar@linux.ibm.com> From: Amir Goldstein Date: Wed, 25 Oct 2023 16:12:57 +0300 Message-ID: Subject: Re: [PATCH v2] ima: detect changes to the backing overlay file To: Mimi Zohar Cc: linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, Raul Rangel , Eric Snowberg Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 morse.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 (morse.vger.email [0.0.0.0]); Wed, 25 Oct 2023 06:13:19 -0700 (PDT) On Wed, Oct 25, 2023 at 2:29=E2=80=AFPM Mimi Zohar wr= ote: > > Commit 18b44bc5a672 ("ovl: Always reevaluate the file signature for > IMA") forced signature re-evaulation on every file access. > > Instead of always re-evaluating the file's integrity, detect a change > to the backing file, by comparing the cached file metadata with the > backing file's metadata. Verifying just the i_version has not changed > is insufficient. In addition save and compare the i_ino and i_rdev > as well. > > Signed-off-by: Mimi Zohar > --- > Changelog: > - Changes made based on Amir's review: removal of unnecessary overlay > magic test, verify i_version, i_ino and i_rdev haven't changed. > > fs/overlayfs/super.c | 2 +- > security/integrity/ima/ima_api.c | 4 ++++ > security/integrity/ima/ima_main.c | 16 +++++++++++++++- > security/integrity/integrity.h | 2 ++ > 4 files changed, 22 insertions(+), 2 deletions(-) > > diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c > index 3fa2416264a4..c71d185980c0 100644 > --- a/fs/overlayfs/super.c > +++ b/fs/overlayfs/super.c > @@ -1489,7 +1489,7 @@ int ovl_fill_super(struct super_block *sb, struct f= s_context *fc) > ovl_trusted_xattr_handlers; > sb->s_fs_info =3D ofs; > sb->s_flags |=3D SB_POSIXACL; > - sb->s_iflags |=3D SB_I_SKIP_SYNC | SB_I_IMA_UNVERIFIABLE_SIGNATUR= E; > + sb->s_iflags |=3D SB_I_SKIP_SYNC; > > err =3D -ENOMEM; > root_dentry =3D ovl_get_root(sb, ctx->upper.dentry, oe); > diff --git a/security/integrity/ima/ima_api.c b/security/integrity/ima/im= a_api.c > index 452e80b541e5..f191bdcceef8 100644 > --- a/security/integrity/ima/ima_api.c > +++ b/security/integrity/ima/ima_api.c > @@ -243,6 +243,7 @@ int ima_collect_measurement(struct integrity_iint_cac= he *iint, > { > const char *audit_cause =3D "failed"; > struct inode *inode =3D file_inode(file); > + struct inode *real_inode =3D d_real_inode(file_dentry(file)); > const char *filename =3D file->f_path.dentry->d_name.name; > struct ima_max_digest_data hash; > struct kstat stat; > @@ -272,6 +273,7 @@ int ima_collect_measurement(struct integrity_iint_cac= he *iint, > AT_STATX_SYNC_AS_STAT); > if (!result && (stat.result_mask & STATX_CHANGE_COOKIE)) > i_version =3D stat.change_cookie; > + > hash.hdr.algo =3D algo; > hash.hdr.length =3D hash_digest_size[algo]; > > @@ -302,6 +304,8 @@ int ima_collect_measurement(struct integrity_iint_cac= he *iint, > iint->ima_hash =3D tmpbuf; > memcpy(iint->ima_hash, &hash, length); > iint->version =3D i_version; You don't have to store them if real_inode =3D=3D inode. > + iint->real_ino =3D real_inode->i_ino; > + iint->real_rdev =3D real_inode->i_rdev; iint->real_dev =3D real_inode->i_sb->s_dev; i_rdev is something else. it's the device pointed to by a blockdev/chardev inode. Thanks, Amir. > > /* Possibly temporary failure due to type of read (eg. O_DIRECT) = */ > if (!result) > diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/i= ma_main.c > index 365db0e43d7c..4a6a22f8805b 100644 > --- a/security/integrity/ima/ima_main.c > +++ b/security/integrity/ima/ima_main.c > @@ -25,6 +25,7 @@ > #include > #include > #include > +#include > > #include "ima.h" > > @@ -207,7 +208,7 @@ static int process_measurement(struct file *file, con= st struct cred *cred, > u32 secid, char *buf, loff_t size, int mas= k, > enum ima_hooks func) > { > - struct inode *inode =3D file_inode(file); > + struct inode *backing_inode, *inode =3D file_inode(file); > struct integrity_iint_cache *iint =3D NULL; > struct ima_template_desc *template_desc =3D NULL; > char *pathbuf =3D NULL; > @@ -284,6 +285,19 @@ static int process_measurement(struct file *file, co= nst struct cred *cred, > iint->measured_pcrs =3D 0; > } > > + /* Detect and re-evaluate changes made to the backing file. */ > + backing_inode =3D d_real_inode(file_dentry(file)); > + if (backing_inode !=3D inode && > + (action & IMA_DO_MASK) && (iint->flags & IMA_DONE_MASK)) { > + if (!IS_I_VERSION(backing_inode) || > + backing_inode->i_rdev !=3D iint->real_rdev || > + backing_inode->i_ino !=3D iint->real_ino || > + !inode_eq_iversion(backing_inode, iint->version)) { > + iint->flags &=3D ~IMA_DONE_MASK; > + iint->measured_pcrs =3D 0; > + } > + } > + > /* Determine if already appraised/measured based on bitmask > * (IMA_MEASURE, IMA_MEASURED, IMA_XXXX_APPRAISE, IMA_XXXX_APPRAI= SED, > * IMA_AUDIT, IMA_AUDITED) > diff --git a/security/integrity/integrity.h b/security/integrity/integrit= y.h > index d7553c93f5c0..dd2bb2d150f6 100644 > --- a/security/integrity/integrity.h > +++ b/security/integrity/integrity.h > @@ -164,6 +164,8 @@ struct integrity_iint_cache { > unsigned long flags; > unsigned long measured_pcrs; > unsigned long atomic_flags; > + unsigned long real_ino; > + dev_t real_rdev; > enum integrity_status ima_file_status:4; > enum integrity_status ima_mmap_status:4; > enum integrity_status ima_bprm_status:4; > -- > 2.39.3 >