Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1566076ybt; Thu, 18 Jun 2020 11:39:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNgXFOhCspASw53yRAiZy0z+RkKPcpbR/JYt8Dgd7BtuOG3Al5i68/YI3iyMGZ/6HordMq X-Received: by 2002:a50:fc04:: with SMTP id i4mr5318559edr.117.1592505550609; Thu, 18 Jun 2020 11:39:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592505550; cv=none; d=google.com; s=arc-20160816; b=DtWtFdjk5aZYWr23stRsLSN3IYlWWW5TxVGoigZg2ufpv90VM+UNeLz0PuyLsFEHda 8P/tVtt7ysAfWTt4i3XLpw2NW90uC9pxQ7wwVQowe/ZvBeLpkCRBp+YIawCUee4l2R7M 9P902Jnsc4uRrdDwHMgPLrE3Lkui/egZRwxmpc4CTkzowYveIFOYQMPXjN9V636cBZg8 +xxwAKAvnPJJpwKGSCYhHWOqWi+if9VnDIGDxMh2v39y5g93NKc2S2YF4QbtCQQVgug9 wTZpP9B0OuxVqAGR4I0rRTE5zKF1OSxR48/cxyx9bLgVGP8064w6gzhN2SOYHLOO8I89 amZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=lhV3kxLGGZHajmcsiiXlOFekyWfd81JMRKYY8cQQdyE=; b=SuNd0RhfkK/UD+s9/GxE/B4cjnSoMmL3tBG1vkeMxFpy6t4tohDuuWaEpiAW08k/vE k5IG7IaQkUThNjd/QrvJzvqmbjl1nzx0of0EESF/wTQw7EP0Ykm26s8GX0ht9zFr2kzg e7JbPveCjM61HRORdoL/wJ82i5gi2Lur685cx/SIJ6seTKMBnrbHHOVaNS9hVfQBlBof BBUMU32FtA2taVxwexyc5ZaAmzSp+LfBq+vNVyOmrcAS/ieMDvEUfCDL3Nw/hAZV0AQM 8id1zqgND7HRwelvQ8NEBX93YbX3jdtWVvPbCa6esAnHp4nGu4lRiPbCSIk4ltVH9xAq JkbA== 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 m9si2272154edp.531.2020.06.18.11.38.48; Thu, 18 Jun 2020 11:39:10 -0700 (PDT) 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 S1728480AbgFRQHh (ORCPT + 99 others); Thu, 18 Jun 2020 12:07:37 -0400 Received: from lhrrgout.huawei.com ([185.176.76.210]:2336 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728008AbgFRQHh (ORCPT ); Thu, 18 Jun 2020 12:07:37 -0400 Received: from lhreml716-chm.china.huawei.com (unknown [172.18.7.107]) by Forcepoint Email with ESMTP id 6E01FC6A77EDF1299CED; Thu, 18 Jun 2020 17:07:35 +0100 (IST) Received: from fraeml714-chm.china.huawei.com (10.206.15.33) by lhreml716-chm.china.huawei.com (10.201.108.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1913.5; Thu, 18 Jun 2020 17:07:35 +0100 Received: from roberto-HP-EliteDesk-800-G2-DM-65W.huawei.com (10.204.65.160) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.1913.5; Thu, 18 Jun 2020 18:07:34 +0200 From: Roberto Sassu To: , CC: , , , , Roberto Sassu Subject: [PATCH 06/11] evm: Allow setxattr() and setattr() if metadata digest won't change Date: Thu, 18 Jun 2020 18:04:53 +0200 Message-ID: <20200618160458.1579-6-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200618160329.1263-2-roberto.sassu@huawei.com> References: <20200618160329.1263-2-roberto.sassu@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.204.65.160] X-ClientProxiedBy: lhreml704-chm.china.huawei.com (10.201.108.53) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If metadata are immutable, they cannot be changed. If metadata are already set to the final value before cp and tar restore the value from the source, those applications display an error even if the operation is legitimate (they don't change the value). This patch determines whether setxattr()/setattr() change metadata and, if not, allows the operations even if metadata are immutable. Signed-off-by: Roberto Sassu --- security/integrity/evm/evm_main.c | 72 +++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/security/integrity/evm/evm_main.c b/security/integrity/evm/evm_main.c index 30072030f05d..41cc6a4aaaab 100644 --- a/security/integrity/evm/evm_main.c +++ b/security/integrity/evm/evm_main.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -305,6 +306,56 @@ static enum integrity_status evm_verify_current_integrity(struct dentry *dentry) return evm_verify_hmac(dentry, NULL, NULL, 0, NULL); } +static int evm_xattr_acl_change(struct dentry *dentry, const char *xattr_name, + const void *xattr_value, size_t xattr_value_len) +{ + umode_t mode; + struct posix_acl *acl = NULL, *acl_res; + struct inode *inode = d_backing_inode(dentry); + int rc; + + /* UID/GID in ACL have been already converted from user to init ns */ + acl = posix_acl_from_xattr(&init_user_ns, xattr_value, xattr_value_len); + if (!acl) + return 1; + + acl_res = acl; + rc = posix_acl_update_mode(inode, &mode, &acl_res); + + posix_acl_release(acl); + + if (rc) + return 1; + + if (acl_res && inode->i_mode != mode) + return 1; + + return 0; +} + +static int evm_xattr_change(struct dentry *dentry, const char *xattr_name, + const void *xattr_value, size_t xattr_value_len) +{ + char *xattr_data = NULL; + int rc = 0; + + if (posix_xattr_acl(xattr_name)) + return evm_xattr_acl_change(dentry, xattr_name, xattr_value, + xattr_value_len); + + rc = vfs_getxattr_alloc(dentry, xattr_name, &xattr_data, 0, GFP_NOFS); + if (rc < 0) + return 1; + + if (rc == xattr_value_len) + rc = memcmp(xattr_value, xattr_data, rc); + else + rc = 1; + + kfree(xattr_data); + return rc; +} + /* * evm_protect_xattr - protect the EVM extended attribute * @@ -361,6 +412,10 @@ static int evm_protect_xattr(struct dentry *dentry, const char *xattr_name, if (evm_status == INTEGRITY_FAIL_IMMUTABLE) return 0; + if (evm_status == INTEGRITY_PASS_IMMUTABLE && + !evm_xattr_change(dentry, xattr_name, xattr_value, xattr_value_len)) + return 0; + if (evm_status != INTEGRITY_PASS) integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry), dentry->d_name.name, "appraise_metadata", @@ -477,6 +532,19 @@ void evm_inode_post_removexattr(struct dentry *dentry, const char *xattr_name) evm_update_evmxattr(dentry, xattr_name, NULL, 0); } +static int evm_attr_change(struct dentry *dentry, struct iattr *attr) +{ + struct inode *inode = d_backing_inode(dentry); + unsigned int ia_valid = attr->ia_valid; + + if ((!(ia_valid & ATTR_UID) || uid_eq(attr->ia_uid, inode->i_uid)) && + (!(ia_valid & ATTR_GID) || gid_eq(attr->ia_gid, inode->i_gid)) && + (!(ia_valid & ATTR_MODE) || attr->ia_mode == inode->i_mode)) + return 0; + + return 1; +} + /** * evm_inode_setattr - prevent updating an invalid EVM extended attribute * @dentry: pointer to the affected dentry @@ -506,6 +574,10 @@ int evm_inode_setattr(struct dentry *dentry, struct iattr *attr) (evm_status == INTEGRITY_FAIL_IMMUTABLE)) return 0; + if (evm_status == INTEGRITY_PASS_IMMUTABLE && + !evm_attr_change(dentry, attr)) + return 0; + integrity_audit_msg(AUDIT_INTEGRITY_METADATA, d_backing_inode(dentry), dentry->d_name.name, "appraise_metadata", integrity_status_msg[evm_status], -EPERM, 0); -- 2.17.1