Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp4413783pxy; Tue, 27 Apr 2021 04:39:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzUwyBHk82LhtrQHZ8D+mK1wOo/MtmZk+kgSUN2/FOW4nnTULAgID3rCDcPBiwT+K0tE+Px X-Received: by 2002:a17:906:507:: with SMTP id j7mr6515421eja.151.1619523562627; Tue, 27 Apr 2021 04:39:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619523562; cv=none; d=google.com; s=arc-20160816; b=aK/tGk4ZZQ15NP1gzv+f+zhtQOY+MORx2qeK+LA2YTTgDnLP+9Se6p+iu/2XQlVF2Y qNVTWxqvPU2xhud9LGTSrnL3FJQPzDM+6iXgLRONXVhkomdLRaVwvA0zYBy2mbvlFH31 dG7Y63hn0dt8A3HQ2Q7EgKAwU8pPDc5KmAmIS5JPLFFvAlTLi7uaP7tSKeLwscNWHByq 2EXqfBt+POmQRcb3DStsQ7qxeyt1zlEekeDoOn50C62bgaukCEBD6tssr8mi2DvGjUWo kZWMkuPFqSStEV0SMZPMht43+MHNBTR1FJTOgw/fhEsHsCK/fei5cdIttvS72PJghgjN LwfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=y1FSwKjJj2p2S/FwpntZSPgn00HsjQodSimxfcIpSgU=; b=r0bLvLiWVIWAxZVWIv5dtvYV2r4MRlofnJz8nXJM+vu+oDZp+Qw7hlbfNaL2daPbVj tTbGH5KJ6Whw5dUSNBdd7ZcSfIxdf9vSvZD64YjwAuaeobmCxI5TxeKTpRsXCUz3SgaC Lf6huh7zmgmpTf9NyhDMg0QH5usSuD6oaoXODzm0JFbXQWspLl4Hqgko88nJcrxgV5pv 7gfMXrBa1pWQLoXKMiDt5Liffpq1u4DzZtL1wYKldjicYzPr/ptjk06FCvDYOE5oVVRE lF0KFEpz49sTQrwP50FVGGan8V+Muj5temvfKoUlXeZWoXTcT+fyQne9jv/kghhqKi0S GB6A== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id qt22si9015007ejb.512.2021.04.27.04.38.58; Tue, 27 Apr 2021 04:39:22 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237469AbhD0Lil (ORCPT + 99 others); Tue, 27 Apr 2021 07:38:41 -0400 Received: from frasgout.his.huawei.com ([185.176.79.56]:2926 "EHLO frasgout.his.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235426AbhD0Lie (ORCPT ); Tue, 27 Apr 2021 07:38:34 -0400 Received: from fraeml714-chm.china.huawei.com (unknown [172.18.147.226]) by frasgout.his.huawei.com (SkyGuard) with ESMTP id 4FV0173tFYz71fd1; Tue, 27 Apr 2021 19:30:03 +0800 (CST) Received: from roberto-ThinkStation-P620.huawei.com (10.204.62.217) by fraeml714-chm.china.huawei.com (10.206.15.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Tue, 27 Apr 2021 13:37:47 +0200 From: Roberto Sassu To: , , , CC: , , , , , , Roberto Sassu Subject: [PATCH v3 2/6] security: Rewrite security_old_inode_init_security() Date: Tue, 27 Apr 2021 13:37:28 +0200 Message-ID: <20210427113732.471066-3-roberto.sassu@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210427113732.471066-1-roberto.sassu@huawei.com> References: <20210427113732.471066-1-roberto.sassu@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.204.62.217] X-ClientProxiedBy: lhreml751-chm.china.huawei.com (10.201.108.201) To fraeml714-chm.china.huawei.com (10.206.15.33) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With upcoming changes, LSMs will be able to write their xattrs in the reserved slots. Boundary checking will be performed to ensure that LSMs don't write outside the passed xattr array. However, the xattr array is created only in security_inode_init_security() and not in security_old_inode_init_security(). Instead of duplicating the code for array allocation, this patch calls security_inode_init_security() from security_old_inode_init_security() and introduces a new callback, called security_initxattrs(), to copy the first element of the xattr array allocated by former function into the destination pointer provided by the latter function. Signed-off-by: Roberto Sassu --- security/security.c | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/security/security.c b/security/security.c index 7f14e59c4f8e..692a148ce764 100644 --- a/security/security.c +++ b/security/security.c @@ -1024,6 +1024,20 @@ int security_dentry_create_files_as(struct dentry *dentry, int mode, } EXPORT_SYMBOL(security_dentry_create_files_as); +static int security_initxattrs(struct inode *inode, const struct xattr *xattrs, + void *fs_info) +{ + struct xattr *dest = (struct xattr *)fs_info; + + if (!dest) + return 0; + + dest->name = xattrs->name; + dest->value = xattrs->value; + dest->value_len = xattrs->value_len; + return 0; +} + int security_inode_init_security(struct inode *inode, struct inode *dir, const struct qstr *qstr, const initxattrs initxattrs, void *fs_data) @@ -1053,8 +1067,14 @@ int security_inode_init_security(struct inode *inode, struct inode *dir, goto out; ret = initxattrs(inode, new_xattrs, fs_data); out: - for (xattr = new_xattrs; xattr->value != NULL; xattr++) + for (xattr = new_xattrs; xattr->value != NULL; xattr++) { + if (xattr == new_xattrs && initxattrs == &security_initxattrs && + !ret && fs_data != NULL) + continue; kfree(xattr->value); + } + if (initxattrs == &security_initxattrs) + return ret; return (ret == -EOPNOTSUPP) ? 0 : ret; } EXPORT_SYMBOL(security_inode_init_security); @@ -1071,10 +1091,25 @@ int security_old_inode_init_security(struct inode *inode, struct inode *dir, const struct qstr *qstr, const char **name, void **value, size_t *len) { + struct xattr xattr = { .name = NULL, .value = NULL, .value_len = 0 }; + struct xattr *lsm_xattr = (name && value && len) ? &xattr : NULL; + int ret; + if (unlikely(IS_PRIVATE(inode))) return -EOPNOTSUPP; - return call_int_hook(inode_init_security, -EOPNOTSUPP, inode, dir, - qstr, name, value, len); + + ret = security_inode_init_security(inode, dir, qstr, + security_initxattrs, lsm_xattr); + if (ret) + return ret; + + if (lsm_xattr) { + *name = lsm_xattr->name; + *value = lsm_xattr->value; + *len = lsm_xattr->value_len; + } + + return 0; } EXPORT_SYMBOL(security_old_inode_init_security); -- 2.25.1