Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp920714imj; Fri, 15 Feb 2019 09:00:37 -0800 (PST) X-Google-Smtp-Source: AHgI3IYuCRP2L0c4P08I9mACimF12bIXOnKkB3xSpLntoYnzIsQISV864B+kHe4dpWD6+lWNIA1I X-Received: by 2002:a17:902:9a9:: with SMTP id 38mr11089586pln.204.1550250037525; Fri, 15 Feb 2019 09:00:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550250037; cv=none; d=google.com; s=arc-20160816; b=AdfhqzSXqAQLHFf0UzUCKYvvWUWEqq/kqc27HEMWZyLXLoJauhFpFFOdk1W+Sr28s9 u6BhyTkXA0CFqTLYc6jVJ6n9Qh4dNEoyjK2fcAA4ECCPSpzGdBCcEp85bKzp27eKQ7eS r5GuGZUcuZ9g/wZ2Z/qhTodgVwdWiiH/ahLLgePtElmxdoovSWphqCXH4m3wEQ2gEFEX vpkM1kmP1pMnXBrajc9eOefYCHWZrG0xwLE00ORixmHOpsmE6RM+Q7INA52FAzJt2jXM LMMbzXW3XK6b0YQhnk3UVWv/QMQNycefLKev5MUnXCaV20si0RkQBbzwcA5n+so1iSrk DxpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:organization; bh=2bEK0EiU36w7T9VrrB6YL7ikrwtWC53AK7RJCKu8SBs=; b=CYMO/lKfOISZasCmC5EALPa4Wpor38VXANJRQG+WkZ2WAmWEgGBJFJ3Z2+S6wt1YZT mrArYnntdvGzJQRNGUTzLUvWKM+KtpHRs78s6oopcw7DTo7KqWUtlD4rTB24O6gKUIDz UPRWzW7bIAmj2WWMBjrr8E9cck/bTZCt3PVbKHUBFhSsDtm5zL0lFyLEIAswF5cVlA0V cW6N/8zQqzzAgFVc6ClSy//es1R8rXqRvfsesZY9BKYXbn7FnCpDBZZnOooAzOqX7Lmp 8EaH4cm0wjOUhBLTcfigcgpmVzjy9eGH+ncb1+9tR7rHEgGd1LwDFe/w6Pq/f+8snvWf vqbA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d125si5839018pfc.114.2019.02.15.09.00.21; Fri, 15 Feb 2019 09:00:37 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392457AbfBOQMG (ORCPT + 99 others); Fri, 15 Feb 2019 11:12:06 -0500 Received: from mx1.redhat.com ([209.132.183.28]:46436 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388701AbfBOQME (ORCPT ); Fri, 15 Feb 2019 11:12:04 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2E58EC0AE662; Fri, 15 Feb 2019 16:12:04 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-121-129.rdu2.redhat.com [10.10.121.129]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0617D5D6A9; Fri, 15 Feb 2019 16:12:01 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [RFC PATCH 25/27] keys: Provide a way to ask for the container keyring From: David Howells To: keyrings@vger.kernel.org, trond.myklebust@hammerspace.com, sfrench@samba.org Cc: linux-security-module@vger.kernel.org, linux-nfs@vger.kernel.org, linux-cifs@vger.kernel.org, linux-fsdevel@vger.kernel.org, rgb@redhat.com, dhowells@redhat.com, linux-kernel@vger.kernel.org Date: Fri, 15 Feb 2019 16:11:58 +0000 Message-ID: <155024711895.21651.13875852239032339316.stgit@warthog.procyon.org.uk> In-Reply-To: <155024683432.21651.14153938339749694146.stgit@warthog.procyon.org.uk> References: <155024683432.21651.14153938339749694146.stgit@warthog.procyon.org.uk> User-Agent: StGit/unknown-version MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 15 Feb 2019 16:12:04 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Provide a constant that can be used in place of a key ID to indicate the keyring belonging to the current process's container. Used as: key_serial_t container_keyring = keyctl_get_key_ID(KEY_SPEC_CONTAINER_KEYRING, 0); Note that this is merely a 'macro' for the ID of the keyring. To be able to actually do anything with it requires the keyring to grant appropriate permissions to the denizens of the container. Signed-off-by: David Howells --- include/uapi/linux/keyctl.h | 1 + samples/vfs/test-container.c | 15 +++++++++++++++ security/keys/process_keys.c | 7 +++++++ 3 files changed, 23 insertions(+) diff --git a/include/uapi/linux/keyctl.h b/include/uapi/linux/keyctl.h index 7136d14dd4d7..89ab609f774c 100644 --- a/include/uapi/linux/keyctl.h +++ b/include/uapi/linux/keyctl.h @@ -88,6 +88,7 @@ enum key_ace_standard_subject { #define KEY_SPEC_GROUP_KEYRING -6 /* - key ID for GID-specific keyring */ #define KEY_SPEC_REQKEY_AUTH_KEY -7 /* - key ID for assumed request_key auth key */ #define KEY_SPEC_REQUESTOR_KEYRING -8 /* - key ID for request_key() dest keyring */ +#define KEY_SPEC_CONTAINER_KEYRING -9 /* - key ID for current->container's keyring */ /* request-key default keyrings */ #define KEY_REQKEY_DEFL_NO_CHANGE -1 diff --git a/samples/vfs/test-container.c b/samples/vfs/test-container.c index 7b2081693fce..4716dd50b696 100644 --- a/samples/vfs/test-container.c +++ b/samples/vfs/test-container.c @@ -20,6 +20,7 @@ #include #include +#define KEY_SPEC_CONTAINER_KEYRING -9 /* - key ID for current->container's keyring */ #define KEYCTL_CONTAINER_INTERCEPT 31 /* Intercept upcalls inside a container */ #define KEYCTL_SET_CONTAINER_KEYRING 35 /* Attach a keyring to a container */ #define KEYCTL_GRANT_PERMISSION 36 /* Grant a permit to a key */ @@ -160,6 +161,8 @@ static inline int fork_into_container(int containerfd) static __attribute__((noreturn)) void container_init(void) { + key_serial_t ckey; + if (0) { /* Do a bit of debugging on the container. */ struct dirent **dlist; @@ -203,6 +206,12 @@ void container_init(void) exit(1); } + ckey = keyctl_get_keyring_ID(KEY_SPEC_CONTAINER_KEYRING, 0); + if (ckey == -1) + perror("keyctl_get_keyring_ID"); + else + printf("Container keyring %d\n", ckey); + setenv("PS1", "container>", 1); execl("/bin/bash", "bash", NULL); perror("execl"); @@ -310,6 +319,12 @@ int main(int argc, char *argv[]) exit(1); } + if (keyctl(KEYCTL_GRANT_PERMISSION, keyring, + KEY_ACE_SUBJ_STANDARD, KEY_ACE_OWNER, 0) < 0) { + perror("keyctl_grant/s"); + exit(1); + } + if (keyctl(KEYCTL_SET_CONTAINER_KEYRING, cfd, keyring) < 0) { perror("keyctl_set_container_keyring"); exit(1); diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c index f296a1cc979a..f8f580a760c9 100644 --- a/security/keys/process_keys.c +++ b/security/keys/process_keys.c @@ -725,6 +725,13 @@ key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags, key_ref = make_key_ref(key, 1); break; + case KEY_SPEC_CONTAINER_KEYRING: + key = current->container->keyring; + if (!key) + goto error; + key_ref = make_key_ref(key, 0); + goto error; + default: key_ref = ERR_PTR(-EINVAL); if (id < 1)