From patchwork Tue May 2 12:40:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kirill Tkhai X-Patchwork-Id: 9707937 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9313560349 for ; Tue, 2 May 2017 12:40:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 83450283FE for ; Tue, 2 May 2017 12:40:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7722228448; Tue, 2 May 2017 12:40:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2EBA3283FE for ; Tue, 2 May 2017 12:40:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751011AbdEBMko (ORCPT ); Tue, 2 May 2017 08:40:44 -0400 Received: from mail-eopbgr50120.outbound.protection.outlook.com ([40.107.5.120]:61000 "EHLO EUR03-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750886AbdEBMkm (ORCPT ); Tue, 2 May 2017 08:40:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=1UiMx7xJoNrElAbzKtCqBRCBkJdr3AMn+5647mE9oqE=; b=O4rLnh+GQyXfCTVyO9lHECVrT3blO3EJvrnHxDn/umv6GJrixT3RXjHAc/lNLRQiQYobXLcilEguBveUXNAPVkaBqDeWYeeFure0XZkj9nd++yDqE4gqFDYsZ+NKyynuWsD/un4+xWUQ+13o8i9ChQ8haBx+XQzNBICWZegWh58= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none; redhat.com; dmarc=none action=none header.from=virtuozzo.com; Received: from localhost.localdomain (195.214.232.6) by HE1PR0802MB2284.eurprd08.prod.outlook.com (10.172.127.14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1047.13; Tue, 2 May 2017 12:40:35 +0000 Subject: [PATCH] security: Use user_namespace::level to avoid redundant iterations in cap_capable() From: Kirill Tkhai To: , , , , , , , , , , , , , , , , , Date: Tue, 2 May 2017 15:40:33 +0300 Message-ID: <149372879732.11927.12565684278286604561.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [195.214.232.6] X-ClientProxiedBy: VI1PR07CA0099.eurprd07.prod.outlook.com (10.165.229.153) To HE1PR0802MB2284.eurprd08.prod.outlook.com (10.172.127.14) X-MS-Office365-Filtering-Correlation-Id: 18ed9659-1fd5-4666-6ae9-08d491586ee4 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(201703131423075)(201703031133081); SRVR:HE1PR0802MB2284; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2284; 3:n5jcqPHcCv6bOSTFOog4+TsnDVNRArRPhLKutQTOrLFnmKF+4Hcvud/Z0e6XzbTn89wRYJbXNSpWwJyC8g4Oxu8niZ5q+YzU5R9SUq5jv9VNHvRr6CqdbLg64FiylcD7m1vEtzNqSZmyEZi1dARs/P1uhWrB9qutyz12Nt1PvIaBuyQgG4tnbXzkgxObwFdlPECMG3Bi1+oxifuZLBgqhELBVFR7BnC+ToKRH1BA/80fKLN5HCncFI81hQAXXdVNA73xPwbM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ooaSvepkLR1Iv6Zsh2PUZ5thje5HIZ5AVd90lIweVwQoJU8arHSPahhN970eugLJsjDI1UJAn7he3wrFyOU4eWvGJdCvQG1iUkVnQL8DYCEein3M3ONpQy29pVSXvOMxly9EsLaAYD7cu3U78Tu0Seru2T3CdgEVfmKDSAk5JrMIlwBiSvxvot9jXQfa+nKZvel10juTxeH4BxqVFc= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(192374486261705); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040450)(601004)(2401047)(8121501046)(5005006)(93006095)(93001095)(3002001)(10201501046)(6041248)(20161123560025)(20161123555025)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(20161123562025)(6072148); SRVR:HE1PR0802MB2284; BCL:0; PCL:0; RULEID:; SRVR:HE1PR0802MB2284; X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2284; 4:mCaaS5+uthT4PGmBGC96Pn/8Ezve/xXoDD7/ryaLFEg1oQUFD1K3FNz4d4qmf+kUCpGy1na8DG5ExqRD2Ry5HRBTizOZKNhZhDeNXpfti3IVKKbLyG60to4/PFpJBBU4ocz9F2WV/QqSob9LzS8SwZdtTSffZ4UMw55sVf25H0rQGaxJE9KwbywXP52L7VuJsh5Zkhh/+LaM94lYhAXCTh38+TiE5p5RiBRrrvm4HRM0x728N6k8nCxCtcitoKpqmJXnobfmgsjhWR6fWVU0pBQkL2+1lp6/sR52y5oa/XxtsB2DWMw8ko6UI+yOtJdrb1Kzt359+MxvxtoK2CbMiKwn75oauOlzYSIkSIpRoVwJm9csj4j4wlpFzJRww3BmXYWh9B9nZbV+bX+g0DzDdNtoSvyht8nSDwG2HAMA+lXOV9pSHcUDt+8Gw8dipXIthhnQINdkAjAxbwhpaf4MBarqz6WIgUNg4Uf0U9lK2zXqcyN69rEOS16VtZAi9t70tclWLG1blU8Wvo/RG7whPkeyAIuh/tzkxpuuDlDrQW/g1eICSOhTgHOpgYJVJ/NX8PH5jPLeWqHGcvxeEPsbssV1sdQ8FqyjXgQX9/VilVRVBZSeg6jntuHck5+/6ibFA1EPLIhlE5d4A/9y5DiFrEroLQxu2c2kzMKFi5FwYU8h20h8FwXmW8PXbf13bK63aqdxpTBzMfiC6mg+J3udAEmKYVlicotQgUYCNT9XACwtbqPeIUADQqWQHasdVHpC X-Forefront-PRVS: 02951C14DC X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10019020)(4630300001)(6069001)(6009001)(39400400002)(39410400002)(39450400003)(39830400002)(4001350100001)(3846002)(2906002)(25786009)(42186005)(15650500001)(103116003)(7416002)(189998001)(6116002)(81166006)(8676002)(508600001)(7736002)(6506006)(55016002)(9686003)(53936002)(83506001)(38730400002)(305945005)(230700001)(86362001)(66066001)(61506002)(47776003)(50466002)(5660300001)(2201001)(50986999)(54356999)(23676002)(33646002)(921003)(1121003); DIR:OUT; SFP:1102; SCL:1; SRVR:HE1PR0802MB2284; H:localhost.localdomain; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtIRTFQUjA4MDJNQjIyODQ7MjM6eXAxakVGM3F0RUNUdWkvdVNHd2xWUG5O?= =?utf-8?B?TUwrZWxwaDNaakhuZStFeE1pcTl6MFpKQ3pZdWJvTzJUWU5FWTNrRnZqODYw?= =?utf-8?B?eHp3VklOTTVuRUZkQVR3MEZ2L0x4NVRFQVorRURaaUN0RWFMS1NXaGwyNFgz?= =?utf-8?B?Rjg1UGNkek00RzloUEVicmZwazNFbHNBV3BEN1F3YXFYbGZvNjcydk5GNGRH?= =?utf-8?B?Y1hTRmJYT2JkQUI1WEhXZXBaVGxJdUdsdngwZTc3a2ExdEQvbXVZUDhWVUQ2?= =?utf-8?B?Y1dGR3kvQmlSTnNySG92WEJWNWFVd29WekhUNFAwOE5YZmJnSVJQL04zdXQx?= =?utf-8?B?U1Q3aUlRRHNJOWlITG1iNEdvZGtab0h5M3RYUzJvQnhLUUpHa2w1Q1NhclVZ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PM X-Microsoft-Exchange-Diagnostics: 1; HE1PR0802MB2284; 7:U5j95q5doPPBCbZo8b6yep5/JXo4Ql+q22UH0xPVbamIKZkLQzQ2+n2XErZNUcPh8ySjCl+C2Uq2UcI4Npj5GIMC+et1WF3CIt5abIufm+yxTj4PV21QzBo1DBOF/XVFcqL9x0gZ/FPf8kGPkisllTT2QWFi21R8zcGIKqlN8UfAvN++UZtAd7mi5FdQXzA5i7KrOwNlQGEvQJtHLnxg/XjDV4FrJmQMgGJ23p5QIhIN/bNftJ8MlSnpDhyI6y+XtxDvZJ1HNGSeW150ZucZJv4Y2IRctH19jT9kZrWQrkLPqB5s1qXVdxZ4HwouWC8W0Dr1eYQh4LXsm9IubjW4BQ==; 20:Hef9CYBL9UESPc/4tkGxICCzHYrOmgwDII7spelyWIgWNnI5dQT4bdzBCoTgvI4bjyuiEtHEMBBUo7Wqb4soOyCblIbuydbxsdhM6waULpYZg506+LEueZeIRTg689nBuyZoJy6MAOd8C9w8Npo0BxXj4/CKdu/qk20AEoB05ww= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 May 2017 12:40:35.0009 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: HE1PR0802MB2284 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When ns->level is not larger then cred->user_ns->level, then ns can't be cred->user_ns's descendant, and there is no a sence to search in parents. So, breake the cycle earlier and skip needless iterations. Signed-off-by: Kirill Tkhai --- security/commoncap.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/security/commoncap.c b/security/commoncap.c index 78b37838a2d3..f6ef78208d2d 100644 --- a/security/commoncap.c +++ b/security/commoncap.c @@ -82,8 +82,11 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns, if (ns == cred->user_ns) return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM; - /* Have we tried all of the parent namespaces? */ - if (ns == &init_user_ns) + /* + * If ns can't be a descendant of cred->user_ns, then it's + * needlessly to go up. + */ + if (ns->level <= cred->user_ns->level) return -EPERM; /*