From patchwork Fri Aug 10 16:13:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 10562939 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F0BFF90E3 for ; Fri, 10 Aug 2018 16:14:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DFDCC2BDC0 for ; Fri, 10 Aug 2018 16:14:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D3D8B2BDBA; Fri, 10 Aug 2018 16:14:04 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham 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 C05032BDC0 for ; Fri, 10 Aug 2018 16:14:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727698AbeHJSob (ORCPT ); Fri, 10 Aug 2018 14:44:31 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:42779 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728066AbeHJSob (ORCPT ); Fri, 10 Aug 2018 14:44:31 -0400 Received: by mail-wr1-f67.google.com with SMTP id e7-v6so8753031wrs.9 for ; Fri, 10 Aug 2018 09:14:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=oXzwaL2WsWN9TbYO7p8Ohzu/SWnflccC2NbbaVHA3iA=; b=SpGlshvaCEzqX/IG5/34EqWXhNBtWJu8AVjD28VmulnfbceIS1PowPbMRdTNOx+5mQ 5IX9aIf2Nwtq5rPZoAM5p2Rbbh3wVvuJ7aCxbCm3V92Te3ffLFwUf7Lq5ZH6ooA27Fij Z9tSJ5bcMkGxCF8c2bBcu9EwIMj75dm3CXvwQlBrbep9wD4EJ4eeEaWE5l37TfMwUzvT pXk+D6zQA+C5vkmtaQl6SI7LBXQVJFBZ6K6opzZCzH3XWQRT2PYd2C1h6nFUUGHIohZ0 1RH++zP0YvN3W+zLMHR2f1zUW8KzBjjv6p4JjO8uY+gL09IQJClLyDZydP6xNQ125uGS SehA== X-Gm-Message-State: AOUpUlG6N9NFudjqXqMk8oUQlomh2aKVpfPD3nvg3068sUujls2cJG8J A1mJcDyVZvo6urS82pNuxXQeABeJX/M= X-Google-Smtp-Source: AA+uWPxVPwpcYewiiNktQ4mo4PC0RATO2Wi25TNDYM4mSj69Ot5jdOzIkY4halow3hTHtgaJygWkTg== X-Received: by 2002:adf:e642:: with SMTP id b2-v6mr4981494wrn.254.1533917640486; Fri, 10 Aug 2018 09:14:00 -0700 (PDT) Received: from localhost.localdomain (u-082-c008.eap.uni-tuebingen.de. [134.2.82.8]) by smtp.gmail.com with ESMTPSA id m13-v6sm9987615wru.93.2018.08.10.09.13.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 10 Aug 2018 09:13:59 -0700 (PDT) From: Christian Brauner To: linux-security-module@vger.kernel.org, containers@lists.linux-foundation.org Cc: serge@hallyn.com, morgan@kernel.org, Christian Brauner Subject: [PATCH 8/9] cap_file: handle run- vs buildtime vfs cap support Date: Fri, 10 Aug 2018 18:13:34 +0200 Message-Id: <20180810161335.27036-9-christian@brauner.io> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180810161335.27036-1-christian@brauner.io> References: <20180810161335.27036-1-christian@brauner.io> Sender: owner-linux-security-module@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP If libcap was compiled on a kernel supporting VFS_CAP_REVISION_3 but running on a kernel that does not support VFS_CAP_REVISION_3 we should always pass down a legacy struct vfs_cap_data if the rootid is 0. On kernels supporting VFS_CAP_REVISION_3 the kernel will take care of translating it from VFS_CAP_REVISION_2 to a VFS_CAP_REVISION_3 version. We can elegantly handle both cases by setting magic to VFS_CAP_REVISION_2 and only passing down XATTR_CAPS_SZ_2 bytes which will leave out the rootid field. If the rootid field is not 0 then we will pass down the VFS_CAP_REVISION_3 and XATTR_CAPS_SZ_3. On kernels supporting VFS_CAP_REVISION_3 this will succeed on kernels not supporting VFS_CAP_REVISION_3 this will fail. The failure on kernels not supporting VFS_CAP_REVISION_3 is wanted since the user explicitly requested an unprivileged file capability but the kernel does not actually support it. So fail hard. Signed-off-by: Christian Brauner Reviewed-by: Serge Hallyn --- libcap/cap_file.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/libcap/cap_file.c b/libcap/cap_file.c index 57c6e3f..a1f3891 100644 --- a/libcap/cap_file.c +++ b/libcap/cap_file.c @@ -202,6 +202,24 @@ static int _fcaps_save(struct vfs_cap_data *rawvfscap, cap_t cap_d, int *bytes_p * endian machine we need to fix this up. */ rawvfscap->rootid = FIXUP_32BITS(cap_d->rootid); + if (rawvfscap->rootid == 0) { + /* If libcap was compiled on a kernel supporting VFS_CAP_REVISION_3 but + * running on a kernel that does not support VFS_CAP_REVISION_3 we + * should always pass down a legacy struct vfs_cap_data if the rootid is + * 0. On kernels supporting VFS_CAP_REVISION_3 the kernel will take care + * of translating it from VFS_CAP_REVISION_2 to a VFS_CAP_REVISION_3 + * version. We can elegantly handle both cases by setting magic to + * VFS_CAP_REVISION_2 and only passing down XATTR_CAPS_SZ_2 bytes which + * will leave out the rootid field. If the rootid field is not 0 then + * we will pass down the VFS_CAP_REVISION_3 and XATTR_CAPS_SZ_3. On + * kernels supporting VFS_CAP_REVISION_3 this will succeed on kernels + * not supporting VFS_CAP_REVISION_3 this will fail. The failure on kernels + * not supporting VFS_CAP_REVISION_3 is wanted since the user explicitly + * requested an unprivileged file capability but the kernel does not + * actually support it. So fail hard. */ + magic = VFS_CAP_REVISION_2; + *bytes_p = XATTR_CAPS_SZ_2; + } #endif if (eff_not_zero == 0) {