From patchwork Wed Jul 28 13:21:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: THOBY Simon X-Patchwork-Id: 12405615 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DD876C432BE for ; Wed, 28 Jul 2021 13:21:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C63B460FED for ; Wed, 28 Jul 2021 13:21:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236235AbhG1NV2 (ORCPT ); Wed, 28 Jul 2021 09:21:28 -0400 Received: from mail-eopbgr00120.outbound.protection.outlook.com ([40.107.0.120]:63047 "EHLO EUR02-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236233AbhG1NV2 (ORCPT ); Wed, 28 Jul 2021 09:21:28 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OO8G5STPBY39oCf9rmrx1Y2CYeJaZmHfM53U62d/+r6+FNYSReme2BhgZ0l8K2Xfry0T68m0pr8no9D5htacDscEIt/JqoahDgPk/jNVjsxZNeq7qA97zbX6Z4TCrgNVnSnGHoetf7YhQSnRCA6eOp7VpR+uUkiguNyr1he01wiHtJSNiJqUPEU8cAJBz10bjY2gfsCHHs+azZh+AggGZpGAII/RXRVldZVd3+8Xp8gQzaSexqwTZCMECvozRaVdYTozmweRp30PSx7AkR1MkMOazUqzoSV/bvJLEDpufU0EW1asIOLUDZc2Zcv15vdMaBhWxXABhb8c2YLOqZb6lg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=dDhr08LdfAMxKSKZU/bYMre9XsgZHXXpJyP7gRf9Bq8=; b=VtBzD6xkPp4W3ARBCDb6AmlB4v2YbDfau7oCG1gt8HauLQcIQegZLULlYyk7ACP7+umDO4wqUnYp1QUSOyybfWJKawsZaLddYlSLVv4FUdvARNtI+CEC7ZpuuePCQeb4TOpG3bQiWF5YVcwMA5TVHqtC9ZHVmCBTcZGxvd56cPJf8tfnte/7WT68fJ3ILfIX6jVka4Fe7YIgZMYlMwKzAkEWz/VMOsBtLgISIn6GKkN1UB6wgRiFIciE00FgLdR99XavCQNYXlFwMNG1NfKICi618w83NmjU+V/HD2T4us3+Ehn5Vrj7wlHzdFqCj3t/Sn6u7RascATUkBj0lKOqFQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=viveris.fr; dmarc=pass action=none header.from=viveris.fr; dkim=pass header.d=viveris.fr; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=viverislicensing.onmicrosoft.com; s=selector2-viverislicensing-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=dDhr08LdfAMxKSKZU/bYMre9XsgZHXXpJyP7gRf9Bq8=; b=IC4WUlaBkcZl2jdnhp7muScsL+YUNN4/luCN6+9L8vqoDIEqPLt349hKlgl+z3FsRmltCXWRuXl9uW2xfAbnnaKYem6w0KHkF30sRTzpt1QShM7AHV23w5z7eNGItcttwkWj4ujpDJnqmoeLN2PEwnyQbao5XkXBigxqZb+7MZE= Received: from AM4PR0902MB1748.eurprd09.prod.outlook.com (2603:10a6:200:96::21) by AM0PR09MB3361.eurprd09.prod.outlook.com (2603:10a6:208:170::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4352.28; Wed, 28 Jul 2021 13:21:24 +0000 Received: from AM4PR0902MB1748.eurprd09.prod.outlook.com ([fe80::84a0:780d:1c5c:4432]) by AM4PR0902MB1748.eurprd09.prod.outlook.com ([fe80::84a0:780d:1c5c:4432%9]) with mapi id 15.20.4352.031; Wed, 28 Jul 2021 13:21:24 +0000 From: THOBY Simon To: "zohar@linux.ibm.com" , "dmitry.kasatkin@gmail.com" , "linux-integrity@vger.kernel.org" , BARVAUX Didier CC: THOBY Simon Subject: [PATCH v5 2/5] IMA: block writes of the security.ima xattr with unsupported algorithms Thread-Topic: [PATCH v5 2/5] IMA: block writes of the security.ima xattr with unsupported algorithms Thread-Index: AQHXg7N2un7kevPBzEuzZUqw6D+kDw== Date: Wed, 28 Jul 2021 13:21:24 +0000 Message-ID: <20210728132112.258606-3-simon.thoby@viveris.fr> References: <20210728132112.258606-1-simon.thoby@viveris.fr> In-Reply-To: <20210728132112.258606-1-simon.thoby@viveris.fr> Accept-Language: fr-FR, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: linux.ibm.com; dkim=none (message not signed) header.d=none;linux.ibm.com; dmarc=none action=none header.from=viveris.fr; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: ef2263e1-6ab0-4332-f5d7-08d951ca98fa x-ms-traffictypediagnostic: AM0PR09MB3361: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:9508; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: nqTm+7CEc71j2sCLjVlFeCorYbOxshsHSEUXt3oxYiMEonDpefaP3WP5ZHa4k00OmJvx3tMmcgp+2udNxyuOr91Nx7gR/QsO1UmMytSk+nWouKg/8l0A+WzPKbYKVgrcvNOi/SlJ2mZnYe49Aq0DXenywNFXQnawEuH2U2Hej8cK+xdXh0bAd49IUF3LbU+YW7zEfMXM23tNOZMydHoLAM+JKJhtLKa6jIgalAi7rzCWcQDE3oqBxGRJn9qguDCmlvbA5TD4OKAfpouxVJ7kHDPVctMEDxuuU83uSGxBDoDzFHcvb/KQcU00E8+wK78L293uK99DnEymdIkxHDHQKrbn1WR7c3TYWZ/Ery08+9B3RZA/UG8uUIMFLGZbz3PHe/IiyWvXnSaezyaXrmpCwQzmcpaZUi41lqy+uw8RaWK1aqPT46cHKK5P437MZdjWAdcb17TVanjYApeF83oB1dPfvMs9aJdksIljnYo8+da3Ua0qMxqrHBNoYySmebDSgsmv39owDl/j7yMBh5GE0dOsVXR0XacGDwFS+/dun4DMcBR4d0zHzMUAR6Awom7WWL+34jd6oP++cdEUrU9UVy58auLUd8lJAZyLfizZRuqcVcvIYB+JjiwGKNb8JBzoIwH3v2dYSqcjwdlXtfJtVVX/wrrNF4Et+Nufc6IGSnpbr6Iu7k3feh0nCbgQnm+BQDnWekERaWcBeaWksqcmRA== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:AM4PR0902MB1748.eurprd09.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39840400004)(396003)(136003)(346002)(376002)(8676002)(6512007)(8936002)(6486002)(110136005)(76116006)(186003)(71200400001)(66446008)(26005)(91956017)(83380400001)(6506007)(64756008)(478600001)(66946007)(66476007)(66556008)(38100700002)(316002)(122000001)(86362001)(15650500001)(1076003)(4326008)(2906002)(5660300002)(6636002)(107886003)(38070700005)(36756003)(2616005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?q?Gk3sGcCY0W19atkN+GKaLtk?= =?iso-8859-1?q?T+59nvZRh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RD/WJbhwZ9BYl9hgjY9QcApe/s+zHVGQnP2y?= =?iso-8859-1?q?6XO4vzUO8p3KEmoSJXwKwB6Ch4U1wYNqGYxx7wxml+NjHME58Du4eqPdMYY5?= =?iso-8859-1?q?ZhPDyFH5WY6m7TzFacwEAt0o4mzBwqAoGZzoJ7uwl0K4V+6ypN9kAyjJsmVK?= =?iso-8859-1?q?BFkHlOz3Vs8DVOTvBIJnKTddAkB5SnlhbnGqxes1N3dcpRffWgB6SrDcYFfb?= =?iso-8859-1?q?c9gEzR8NzVO5MFJqHszj7jtoJ4TcTO7/8T6pVdqZjTLWlc3rRxnKHoHHaqFs?= =?iso-8859-1?q?DNBOPDpHwokHfKWvzOd8LPQMH0qjjgxAt9xjxIP4XznNrQiTAdCLVmOIaJUw?= =?iso-8859-1?q?qYQHjg5ewQbavwijB3CAFw44xs2tSO/scg850F/JyC+ylRD9JNSLpunDgzKL?= =?iso-8859-1?q?2XmxOrpwUUrk+BXq7XDi5ZFedheOiYMXoecnqjpqeCC8z6K0S65g8PQPiOXA?= =?iso-8859-1?q?a1YgQiG9A9X/KaJWT9OSEZQ=3D=3D?= MIME-Version: 1.0 X-OriginatorOrg: viveris.fr X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: AM4PR0902MB1748.eurprd09.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: ef2263e1-6ab0-4332-f5d7-08d951ca98fa X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Jul 2021 13:21:24.1103 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 34bab81c-945c-43f1-ad13-592b97e11b40 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: Wz+6hdiUTePoOf5Av9itleikD4vgEYxkn97pyBdjh3716R0JEQkp0JSWKlTW/Pa/Tb/4icX831aL5pkEX8gk3A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR09MB3361 Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org By default, any write to the extended attributes security.ima will be accepted, even if the xattr value uses a hash algorithm not compiled in the kernel (which doesn't make sense, because the kernel wouldn't be able to appraise that file, as it lacks support for validating the hash). Prevent such writes: only writes using hash algorithms available in the current kernel are now allowed. Any attempt to perform these writes will be denied with an audit message. Note however that CONFIG_IMA depends on CONFIG_CRYPTO_SHA1, which somewhat hampers the security benefits of this measure (but MD4 and MD5 can be disabled, which is already a significant improvement). Signed-off-by: Simon Thoby --- security/integrity/ima/ima_appraise.c | 50 +++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c index ef9dcfce45d4..a5e0d3400bd1 100644 --- a/security/integrity/ima/ima_appraise.c +++ b/security/integrity/ima/ima_appraise.c @@ -575,6 +575,53 @@ static void ima_reset_appraise_flags(struct inode *inode, int digsig) clear_bit(IMA_DIGSIG, &iint->atomic_flags); } +/** + * validate_hash_algo() - Block setxattr with invalid digests + * @dentry: object being setxattr()'ed + * @xattr_value: value supplied by userland for the xattr + * @xattr_value_len: length of xattr_value + * + * Context: called when the user tries to write the security.ima xattr. + * The xattr value is mapped to some hash algorithm, and this algorithm + * must be built in the kernel for the setxattr to be allowed. + * + * Emit an audit message when the algorithm is invalid. + * + * Return: 0 on success, else an error. + */ +static int validate_hash_algo(struct dentry *dentry, + const void *xattr_value, + size_t xattr_value_len) +{ + char *path = NULL, *pathbuf = NULL; + enum hash_algo xattr_hash_algo; + + xattr_hash_algo = ima_get_hash_algo((struct evm_ima_xattr_data *)xattr_value, + xattr_value_len); + + if (likely(xattr_hash_algo == ima_hash_algo || + crypto_has_alg(hash_algo_name[xattr_hash_algo], 0, 0))) + return 0; + + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL); + if (pathbuf) { + path = dentry_path(dentry, pathbuf, PATH_MAX); + + /* + * disallow xattr writes with algorithms disabled in the + * kernel configuration + */ + integrity_audit_msg(AUDIT_INTEGRITY_DATA, d_inode(dentry), + path, "collect_data", + "unavailable-hash-algorithm", + -EACCES, 0); + + kfree(pathbuf); + } + + return -EACCES; +} + int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, const void *xattr_value, size_t xattr_value_len) { @@ -595,6 +642,9 @@ int ima_inode_setxattr(struct dentry *dentry, const char *xattr_name, ima_reset_appraise_flags(d_backing_inode(dentry), digsig); if (result == 1) result = 0; + + result = validate_hash_algo(dentry, xattr_value, + xattr_value_len); } return result; }